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