aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CodeGen/ms-intrinsics-underaligned.c
blob: 34e2afb09f4b9a78ab261a89d65e258d2c0f295b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// RUN: %clang_cc1 -ffreestanding -fms-extensions -fms-compatibility -fms-compatibility-version=17.00 \
// RUN:         -triple x86_64--windows -Oz -emit-llvm -target-feature +cx16 %s -o - \
// RUN:         | FileCheck %s --check-prefixes=CHECK

// RUN: %clang_cc1 -ffreestanding -fms-extensions -fms-compatibility -fms-compatibility-version=17.00 \
// RUN:         -triple aarch64--windows -Oz -emit-llvm %s -o - \
// RUN:         | FileCheck %s --check-prefixes=CHECK,CHECK-AARCH64

// Ensure that we emit _Interlocked atomic operations specifying natural
// alignment, even when clang's usual alignment derivation would result in a
// lower alignment value.

// intrin.h needs size_t, but -ffreestanding prevents us from getting it from
// stddef.h.  Work around it with this typedef.
typedef __SIZE_TYPE__ size_t;

#include <intrin.h>

#pragma pack(1)
typedef struct {
  char a;
  short b;
  long c;
  long long d;
  void *p;
} X;

_Static_assert(sizeof(X) == 23, "");
_Static_assert(__alignof__(X) == 1, "");

// CHECK-LABEL: @test_InterlockedExchangePointer(
// CHECK:   atomicrmw {{.*}} align 8
void *test_InterlockedExchangePointer(X *x) {
  return _InterlockedExchangePointer(&x->p, 0);
}

// CHECK-LABEL: @test_InterlockedExchange8(
// CHECK:   atomicrmw {{.*}} align 1
char test_InterlockedExchange8(X *x) {
  return _InterlockedExchange8(&x->a, 0);
}

// CHECK-LABEL: @test_InterlockedExchange16(
// CHECK:   atomicrmw {{.*}} align 2
short test_InterlockedExchange16(X *x) {
  return _InterlockedExchange16(&x->b, 0);
}

// CHECK-LABEL: @test_InterlockedExchange(
// CHECK:   atomicrmw {{.*}} align 4
long test_InterlockedExchange(X *x) {
  return _InterlockedExchange(&x->c, 0);
}

// CHECK-LABEL: @test_InterlockedExchange64(
// CHECK:   atomicrmw {{.*}} align 8
long long test_InterlockedExchange64(X *x) {
  return _InterlockedExchange64(&x->d, 0);
}

// CHECK-LABEL: @test_InterlockedIncrement(
// CHECK:   atomicrmw {{.*}} align 4
long test_InterlockedIncrement(X *x) {
  return _InterlockedIncrement(&x->c);
}

// CHECK-LABEL: @test_InterlockedDecrement16(
// CHECK:   atomicrmw {{.*}} align 2
short test_InterlockedDecrement16(X *x) {
  return _InterlockedDecrement16(&x->b);
}


// CHECK-LABEL: @test_InterlockedCompareExchangePointer(
// CHECK:   cmpxchg {{.*}} align 8
void *test_InterlockedCompareExchangePointer(X *x) {
  return _InterlockedCompareExchangePointer(&x->p, 0, 0);
}

// CHECK-LABEL: @test_InterlockedCompareExchange8(
// CHECK:   cmpxchg {{.*}} align 1
char test_InterlockedCompareExchange8(X *x) {
  return _InterlockedCompareExchange8(&x->a, 0, 0);
}

// CHECK-LABEL: @test_InterlockedCompareExchange16(
// CHECK:   cmpxchg {{.*}} align 2
short test_InterlockedCompareExchange16(X *x) {
  return _InterlockedCompareExchange16(&x->b, 0, 0);
}

// CHECK-LABEL: @test_InterlockedCompareExchange(
// CHECK:   cmpxchg {{.*}} align 4
long test_InterlockedCompareExchange(X *x) {
  return _InterlockedCompareExchange(&x->c, 0, 0);
}

// CHECK-LABEL: @test_InterlockedCompareExchange64(
// CHECK:   cmpxchg {{.*}} align 8
long long test_InterlockedCompareExchange64(X *x) {
  return _InterlockedCompareExchange64(&x->d, 0, 0);
}

#ifdef __aarch64__
// CHECK-AARCH64-LABEL: @test_InterlockedAdd(
// CHECK-AARCH64:   atomicrmw {{.*}} align 4
long test_InterlockedAdd(X *x) {
  return _InterlockedAdd(&x->c, 4);
}

// CHECK-AARCH64-LABEL: @test_InterlockedAdd64(
// CHECK-AARCH64:   atomicrmw {{.*}} align 8
long test_InterlockedAdd64(X *x) {
  return _InterlockedAdd64(&x->c, 4);
}
#endif