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