OLD | NEW |
1 ; This tests the optimization of atomic cmpxchg w/ following cmp + branches. | 1 ; This tests the optimization of atomic cmpxchg w/ following cmp + branches. |
2 | 2 |
3 ; RUN: %llvm2ice -O2 --verbose none %s \ | 3 ; RUN: %llvm2ice -O2 --verbose none %s \ |
4 ; RUN: | llvm-mc -triple=i686-none-nacl -x86-asm-syntax=intel -filetype=obj \ | 4 ; RUN: | llvm-mc -triple=i686-none-nacl -x86-asm-syntax=intel -filetype=obj \ |
5 ; RUN: | llvm-objdump -d -symbolize -x86-asm-syntax=intel - \ | 5 ; RUN: | llvm-objdump -d -symbolize -x86-asm-syntax=intel - \ |
6 ; RUN: | FileCheck --check-prefix=O2 %s | 6 ; RUN: | FileCheck --check-prefix=O2 %s |
7 ; RUN: %llvm2ice -Om1 --verbose none %s \ | 7 ; RUN: %llvm2ice -Om1 --verbose none %s \ |
8 ; RUN: | llvm-mc -triple=i686-none-nacl -x86-asm-syntax=intel -filetype=obj \ | 8 ; RUN: | llvm-mc -triple=i686-none-nacl -x86-asm-syntax=intel -filetype=obj \ |
9 ; RUN: | llvm-objdump -d -symbolize -x86-asm-syntax=intel - \ | 9 ; RUN: | llvm-objdump -d -symbolize -x86-asm-syntax=intel - \ |
10 ; RUN: | FileCheck --check-prefix=OM1 %s | 10 ; RUN: | FileCheck --check-prefix=OM1 %s |
11 ; RUN: %llvm2ice --verbose none %s | FileCheck --check-prefix=ERRORS %s | 11 ; RUN: %llvm2ice --verbose none %s | FileCheck --check-prefix=ERRORS %s |
12 ; RUN: %llvm2iceinsts %s | %szdiff %s | FileCheck --check-prefix=DUMP %s | 12 ; RUN: %llvm2iceinsts %s | %szdiff %s | FileCheck --check-prefix=DUMP %s |
13 ; RUN: %llvm2iceinsts --pnacl %s | %szdiff %s \ | 13 ; RUN: %llvm2iceinsts --pnacl %s | %szdiff %s \ |
14 ; RUN: | FileCheck --check-prefix=DUMP %s | 14 ; RUN: | FileCheck --check-prefix=DUMP %s |
15 | 15 |
16 declare i32 @llvm.nacl.atomic.cmpxchg.i32(i32*, i32, i32, i32, i32) | 16 declare i32 @llvm.nacl.atomic.cmpxchg.i32(i32*, i32, i32, i32, i32) |
17 | 17 |
18 | 18 |
19 ; Test that a cmpxchg followed by icmp eq and branch can be optimized to | 19 ; Test that a cmpxchg followed by icmp eq and branch can be optimized to |
20 ; reuse the flags set by the cmpxchg instruction itself. | 20 ; reuse the flags set by the cmpxchg instruction itself. |
21 ; This is only expected to work w/ O2, based on lightweight liveness. | 21 ; This is only expected to work w/ O2, based on lightweight liveness. |
22 ; (Or if we had other means to detect the only use). | 22 ; (Or if we had other means to detect the only use). |
23 declare void @use_value(i32); | 23 declare void @use_value(i32) |
24 | 24 |
25 define i32 @test_atomic_cmpxchg_loop(i32 %iptr, i32 %expected, i32 %desired) { | 25 define i32 @test_atomic_cmpxchg_loop(i32 %iptr, i32 %expected, i32 %desired) { |
26 entry: | 26 entry: |
27 br label %loop | 27 br label %loop |
28 | 28 |
29 loop: | 29 loop: |
30 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] | 30 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] |
31 %succeeded_first_try = phi i32 [ 1, %entry ], [ 2, %loop ] | 31 %succeeded_first_try = phi i32 [ 1, %entry ], [ 2, %loop ] |
32 %ptr = inttoptr i32 %iptr to i32* | 32 %ptr = inttoptr i32 %iptr to i32* |
33 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, | 33 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, |
34 i32 %desired, i32 6, i32 6) | 34 i32 %desired, i32 6, i32 6) |
35 %success = icmp eq i32 %expected_loop, %old | 35 %success = icmp eq i32 %expected_loop, %old |
36 br i1 %success, label %done, label %loop | 36 br i1 %success, label %done, label %loop |
37 | 37 |
38 done: | 38 done: |
39 call void @use_value(i32 %old) | 39 call void @use_value(i32 %old) |
40 ret i32 %succeeded_first_try | 40 ret i32 %succeeded_first_try |
41 } | 41 } |
42 ; O2-LABEL: test_atomic_cmpxchg_loop | 42 ; O2-LABEL: test_atomic_cmpxchg_loop |
43 ; O2: lock | 43 ; O2: lock |
44 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 44 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
45 ; O2-NOT: cmp | 45 ; O2-NOT: cmp |
46 ; Make sure the phi assignment for succeeded_first_try is still there. | 46 ; Make sure the phi assignment for succeeded_first_try is still there. |
47 ; O2: mov {{.*}}, 2 | 47 ; O2: mov {{.*}}, 2 |
48 ; O2-NOT: cmp | 48 ; O2-NOT: cmp |
49 ; O2: je | 49 ; O2: jne |
50 ; Make sure the call isn't accidentally deleted. | 50 ; Make sure the call isn't accidentally deleted. |
51 ; O2: call | 51 ; O2: call |
52 ; | 52 ; |
53 ; Check that the unopt version does have a cmp | 53 ; Check that the unopt version does have a cmp |
54 ; OM1-LABEL: test_atomic_cmpxchg_loop | 54 ; OM1-LABEL: test_atomic_cmpxchg_loop |
55 ; OM1: lock | 55 ; OM1: lock |
56 ; OM1-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 56 ; OM1-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
57 ; OM1: cmp | 57 ; OM1: cmp |
58 ; OM1: je | 58 ; OM1: je |
59 ; OM1: call | 59 ; OM1: call |
(...skipping 11 matching lines...) Expand all Loading... |
71 %success = icmp eq i32 %old, %expected_loop | 71 %success = icmp eq i32 %old, %expected_loop |
72 br i1 %success, label %done, label %loop | 72 br i1 %success, label %done, label %loop |
73 | 73 |
74 done: | 74 done: |
75 ret i32 %old | 75 ret i32 %old |
76 } | 76 } |
77 ; O2-LABEL: test_atomic_cmpxchg_loop2 | 77 ; O2-LABEL: test_atomic_cmpxchg_loop2 |
78 ; O2: lock | 78 ; O2: lock |
79 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 79 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
80 ; O2-NOT: cmp | 80 ; O2-NOT: cmp |
81 ; O2: je | 81 ; O2: jne |
82 | 82 |
83 | 83 |
84 ; Still works if the compare operands are constants. | 84 ; Still works if the compare operands are constants. |
85 define i32 @test_atomic_cmpxchg_loop_const(i32 %iptr, i32 %desired) { | 85 define i32 @test_atomic_cmpxchg_loop_const(i32 %iptr, i32 %desired) { |
86 entry: | 86 entry: |
87 br label %loop | 87 br label %loop |
88 | 88 |
89 loop: | 89 loop: |
90 %succeeded_first_try = phi i32 [ 1, %entry ], [ 0, %loop ] | 90 %succeeded_first_try = phi i32 [ 1, %entry ], [ 0, %loop ] |
91 %ptr = inttoptr i32 %iptr to i32* | 91 %ptr = inttoptr i32 %iptr to i32* |
92 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 0, | 92 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 0, |
93 i32 %desired, i32 6, i32 6) | 93 i32 %desired, i32 6, i32 6) |
94 %success = icmp eq i32 %old, 0 | 94 %success = icmp eq i32 %old, 0 |
95 br i1 %success, label %done, label %loop | 95 br i1 %success, label %done, label %loop |
96 | 96 |
97 done: | 97 done: |
98 ret i32 %succeeded_first_try | 98 ret i32 %succeeded_first_try |
99 } | 99 } |
100 ; O2-LABEL: test_atomic_cmpxchg_loop_const | 100 ; O2-LABEL: test_atomic_cmpxchg_loop_const |
101 ; O2: lock | 101 ; O2: lock |
102 ; Should be using NEXT: see issue 3929 | 102 ; Should be using NEXT: see issue 3929 |
103 ; O2: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 103 ; O2: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
104 ; O2-NOT: cmp | 104 ; O2-NOT: cmp |
105 ; O2: je | 105 ; O2: jne |
106 | 106 |
107 ; This is a case where the flags cannot be reused (compare is for some | 107 ; This is a case where the flags cannot be reused (compare is for some |
108 ; other condition). | 108 ; other condition). |
109 define i32 @test_atomic_cmpxchg_no_opt(i32 %iptr, i32 %expected, i32 %desired) { | 109 define i32 @test_atomic_cmpxchg_no_opt(i32 %iptr, i32 %expected, i32 %desired) { |
110 entry: | 110 entry: |
111 br label %loop | 111 br label %loop |
112 | 112 |
113 loop: | 113 loop: |
114 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] | 114 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] |
115 %ptr = inttoptr i32 %iptr to i32* | 115 %ptr = inttoptr i32 %iptr to i32* |
116 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, | 116 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, |
117 i32 %desired, i32 6, i32 6) | 117 i32 %desired, i32 6, i32 6) |
118 %success = icmp sgt i32 %old, %expected | 118 %success = icmp sgt i32 %old, %expected |
119 br i1 %success, label %done, label %loop | 119 br i1 %success, label %done, label %loop |
120 | 120 |
121 done: | 121 done: |
122 ret i32 %old | 122 ret i32 %old |
123 } | 123 } |
124 ; O2-LABEL: test_atomic_cmpxchg_no_opt | 124 ; O2-LABEL: test_atomic_cmpxchg_no_opt |
125 ; O2: lock | 125 ; O2: lock |
126 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 126 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
127 ; O2: mov {{.*}} | 127 ; O2: mov {{.*}} |
128 ; O2: cmp | 128 ; O2: cmp |
129 ; O2: jg | 129 ; O2: jle |
130 | 130 |
131 ; Another case where the flags cannot be reused (the comparison result | 131 ; Another case where the flags cannot be reused (the comparison result |
132 ; is used somewhere else). | 132 ; is used somewhere else). |
133 define i32 @test_atomic_cmpxchg_no_opt2(i32 %iptr, i32 %expected, i32 %desired)
{ | 133 define i32 @test_atomic_cmpxchg_no_opt2(i32 %iptr, i32 %expected, i32 %desired)
{ |
134 entry: | 134 entry: |
135 br label %loop | 135 br label %loop |
136 | 136 |
137 loop: | 137 loop: |
138 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] | 138 %expected_loop = phi i32 [ %expected, %entry ], [ %old, %loop ] |
139 %ptr = inttoptr i32 %iptr to i32* | 139 %ptr = inttoptr i32 %iptr to i32* |
140 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, | 140 %old = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* %ptr, i32 %expected_loop, |
141 i32 %desired, i32 6, i32 6) | 141 i32 %desired, i32 6, i32 6) |
142 %success = icmp eq i32 %old, %expected | 142 %success = icmp eq i32 %old, %expected |
143 br i1 %success, label %done, label %loop | 143 br i1 %success, label %done, label %loop |
144 | 144 |
145 done: | 145 done: |
146 %r = zext i1 %success to i32 | 146 %r = zext i1 %success to i32 |
147 ret i32 %r | 147 ret i32 %r |
148 } | 148 } |
149 ; O2-LABEL: test_atomic_cmpxchg_no_opt2 | 149 ; O2-LABEL: test_atomic_cmpxchg_no_opt2 |
150 ; O2: lock | 150 ; O2: lock |
151 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} | 151 ; O2-NEXT: cmpxchg dword ptr [e{{[^a].}}], e{{[^a]}} |
152 ; O2: mov {{.*}} | 152 ; O2: mov {{.*}} |
153 ; O2: cmp | 153 ; O2: cmp |
154 ; O2: je | 154 ; O2: je |
155 | 155 |
156 ; ERRORS-NOT: ICE translation error | 156 ; ERRORS-NOT: ICE translation error |
157 ; DUMP-NOT: SZ | 157 ; DUMP-NOT: SZ |
OLD | NEW |