[TwoAddressInstructionPass] When looking for a 3 addr conversion after commuting...
[oota-llvm.git] / test / CodeGen / X86 / cmpxchg-clobber-flags.ll
1 ; RUN: llc -verify-machineinstrs -mtriple=i386-linux-gnu %s -o - | FileCheck %s -check-prefix=i386
2 ; RUN: llc -verify-machineinstrs -mtriple=i386-linux-gnu -pre-RA-sched=fast %s -o - | FileCheck %s -check-prefix=i386f
3 ; RUN: llc -verify-machineinstrs -mtriple=x86_64-linux-gnu %s -o - | FileCheck %s -check-prefix=x8664
4 ; RUN: llc -verify-machineinstrs -mtriple=x86_64-linux-gnu -pre-RA-sched=fast %s -o - | FileCheck %s -check-prefix=x8664
5
6 declare i32 @foo()
7 declare i32 @bar(i64)
8
9 define i64 @test_intervening_call(i64* %foo, i64 %bar, i64 %baz) {
10 ; i386-LABEL: test_intervening_call:
11 ; i386: cmpxchg8b
12 ; i386-NEXT: pushl %eax
13 ; i386-NEXT: seto %al
14 ; i386-NEXT: lahf
15 ; i386-NEXT: movl %eax, [[FLAGS:%.*]]
16 ; i386-NEXT: popl %eax
17 ; i386-NEXT: movl %edx, 4(%esp)
18 ; i386-NEXT: movl %eax, (%esp)
19 ; i386-NEXT: calll bar
20 ; i386-NEXT: movl [[FLAGS]], %eax
21 ; i386-NEXT: addb $127, %al
22 ; i386-NEXT: sahf
23 ; i386-NEXT: jne
24
25 ; i386f-LABEL: test_intervening_call:
26 ; i386f: cmpxchg8b
27 ; i386f-NEXT: movl %eax, (%esp)
28 ; i386f-NEXT: movl %edx, 4(%esp)
29 ; i386f-NEXT: seto %al
30 ; i386f-NEXT: lahf
31 ; i386f-NEXT: movl %eax, [[FLAGS:%.*]]
32 ; i386f-NEXT: calll bar
33 ; i386f-NEXT: movl [[FLAGS]], %eax
34 ; i386f-NEXT: addb $127, %al
35 ; i386f-NEXT: sahf
36 ; i386f-NEXT: jne
37
38 ; x8664-LABEL: test_intervening_call:
39 ; x8664: cmpxchgq
40 ; x8664: pushq %rax
41 ; x8664-NEXT: seto %al
42 ; x8664-NEXT: lahf
43 ; x8664-NEXT: movq %rax, [[FLAGS:%.*]]
44 ; x8664-NEXT: popq %rax
45 ; x8664-NEXT: movq %rax, %rdi
46 ; x8664-NEXT: callq bar
47 ; x8664-NEXT: movq [[FLAGS]], %rax
48 ; x8664-NEXT: addb $127, %al
49 ; x8664-NEXT: sahf
50 ; x8664-NEXT: jne
51
52   %cx = cmpxchg i64* %foo, i64 %bar, i64 %baz seq_cst seq_cst
53   %v = extractvalue { i64, i1 } %cx, 0
54   %p = extractvalue { i64, i1 } %cx, 1
55   call i32 @bar(i64 %v)
56   br i1 %p, label %t, label %f
57
58 t:
59   ret i64 42
60
61 f:
62   ret i64 0
63 }
64
65 ; Interesting in producing a clobber without any function calls.
66 define i32 @test_control_flow(i32* %p, i32 %i, i32 %j) {
67 ; i386-LABEL: test_control_flow:
68 ; i386: cmpxchg
69 ; i386-NEXT: jne
70
71 ; i386f-LABEL: test_control_flow:
72 ; i386f: cmpxchg
73 ; i386f-NEXT: jne
74
75 ; x8664-LABEL: test_control_flow:
76 ; x8664: cmpxchg
77 ; x8664-NEXT: jne
78
79 entry:
80   %cmp = icmp sgt i32 %i, %j
81   br i1 %cmp, label %loop_start, label %cond.end
82
83 loop_start:
84   br label %while.condthread-pre-split.i
85
86 while.condthread-pre-split.i:
87   %.pr.i = load i32, i32* %p, align 4
88   br label %while.cond.i
89
90 while.cond.i:
91   %0 = phi i32 [ %.pr.i, %while.condthread-pre-split.i ], [ 0, %while.cond.i ]
92   %tobool.i = icmp eq i32 %0, 0
93   br i1 %tobool.i, label %while.cond.i, label %while.body.i
94
95 while.body.i:
96   %.lcssa = phi i32 [ %0, %while.cond.i ]
97   %1 = cmpxchg i32* %p, i32 %.lcssa, i32 %.lcssa seq_cst seq_cst
98   %2 = extractvalue { i32, i1 } %1, 1
99   br i1 %2, label %cond.end.loopexit, label %while.condthread-pre-split.i
100
101 cond.end.loopexit:
102   br label %cond.end
103
104 cond.end:
105   %cond = phi i32 [ %i, %entry ], [ 0, %cond.end.loopexit ]
106   ret i32 %cond
107 }
108
109 ; This one is an interesting case because CMOV doesn't have a chain
110 ; operand. Naive attempts to limit cmpxchg EFLAGS use are likely to fail here.
111 define i32 @test_feed_cmov(i32* %addr, i32 %desired, i32 %new) {
112 ; i386-LABEL: test_feed_cmov:
113 ; i386: cmpxchgl
114 ; i386-NEXT: seto %al
115 ; i386-NEXT: lahf
116 ; i386-NEXT: movl %eax, [[FLAGS:%.*]]
117 ; i386-NEXT: calll foo
118 ; i386-NEXT: pushl %eax
119 ; i386-NEXT: movl [[FLAGS]], %eax
120 ; i386-NEXT: addb $127, %al
121 ; i386-NEXT: sahf
122 ; i386-NEXT: popl %eax
123
124 ; i386f-LABEL: test_feed_cmov:
125 ; i386f: cmpxchgl
126 ; i386f-NEXT: seto %al
127 ; i386f-NEXT: lahf
128 ; i386f-NEXT: movl %eax, [[FLAGS:%.*]]
129 ; i386f-NEXT: calll foo
130 ; i386f-NEXT: pushl %eax
131 ; i386f-NEXT: movl [[FLAGS]], %eax
132 ; i386f-NEXT: addb $127, %al
133 ; i386f-NEXT: sahf
134 ; i386f-NEXT: popl %eax
135
136 ; x8664-LABEL: test_feed_cmov:
137 ; x8664: cmpxchgl
138 ; x8664: seto %al
139 ; x8664-NEXT: lahf
140 ; x8664-NEXT: movq %rax, [[FLAGS:%.*]]
141 ; x8664-NEXT: callq foo
142 ; x8664-NEXT: pushq %rax
143 ; x8664-NEXT: movq [[FLAGS]], %rax
144 ; x8664-NEXT: addb $127, %al
145 ; x8664-NEXT: sahf
146 ; x8664-NEXT: popq %rax
147
148   %res = cmpxchg i32* %addr, i32 %desired, i32 %new seq_cst seq_cst
149   %success = extractvalue { i32, i1 } %res, 1
150
151   %rhs = call i32 @foo()
152
153   %ret = select i1 %success, i32 %new, i32 %rhs
154   ret i32 %ret
155 }