Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 1 | ; RUN: opt -basicaa -dse -S < %s | FileCheck %s |
| 2 | |
| 3 | target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" |
| 4 | target triple = "x86_64-apple-macosx10.7.0" |
| 5 | |
| 6 | ; Sanity tests for atomic stores. |
| 7 | ; Note that it turns out essentially every transformation DSE does is legal on |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 8 | ; atomic ops, just some transformations are not allowed across release-acquire pairs. |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 9 | |
| 10 | @x = common global i32 0, align 4 |
| 11 | @y = common global i32 0, align 4 |
| 12 | |
| 13 | declare void @randomop(i32*) |
| 14 | |
| 15 | ; DSE across unordered store (allowed) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 16 | define void @test1() { |
| 17 | ; CHECK-LABEL: test1 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 18 | ; CHECK-NOT: store i32 0 |
| 19 | ; CHECK: store i32 1 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 20 | store i32 0, i32* @x |
| 21 | store atomic i32 0, i32* @y unordered, align 4 |
| 22 | store i32 1, i32* @x |
| 23 | ret void |
| 24 | } |
| 25 | |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 26 | ; DSE across seq_cst load (allowed) |
| 27 | define i32 @test2() { |
| 28 | ; CHECK-LABEL: test2 |
| 29 | ; CHECK-NOT: store i32 0 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 30 | ; CHECK: store i32 1 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 31 | store i32 0, i32* @x |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 32 | %x = load atomic i32, i32* @y seq_cst, align 4 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 33 | store i32 1, i32* @x |
| 34 | ret i32 %x |
| 35 | } |
| 36 | |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 37 | ; DSE across seq_cst store (allowed) |
| 38 | define void @test3() { |
| 39 | ; CHECK-LABEL: test3 |
| 40 | ; CHECK-NOT: store i32 0 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 41 | ; CHECK: store atomic i32 2 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 42 | store i32 0, i32* @x |
| 43 | store atomic i32 2, i32* @y seq_cst, align 4 |
| 44 | store i32 1, i32* @x |
| 45 | ret void |
| 46 | } |
| 47 | |
| 48 | ; DSE remove unordered store (allowed) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 49 | define void @test4() { |
| 50 | ; CHECK-LABEL: test4 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 51 | ; CHECK-NOT: store atomic |
| 52 | ; CHECK: store i32 1 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 53 | store atomic i32 0, i32* @x unordered, align 4 |
| 54 | store i32 1, i32* @x |
| 55 | ret void |
| 56 | } |
| 57 | |
| 58 | ; DSE unordered store overwriting non-atomic store (allowed) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 59 | define void @test5() { |
| 60 | ; CHECK-LABEL: test5 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 61 | ; CHECK: store atomic i32 1 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 62 | store i32 0, i32* @x |
| 63 | store atomic i32 1, i32* @x unordered, align 4 |
| 64 | ret void |
| 65 | } |
| 66 | |
| 67 | ; DSE no-op unordered atomic store (allowed) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 68 | define void @test6() { |
| 69 | ; CHECK-LABEL: test6 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 70 | ; CHECK-NOT: store |
| 71 | ; CHECK: ret void |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 72 | %x = load atomic i32, i32* @x unordered, align 4 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 73 | store atomic i32 %x, i32* @x unordered, align 4 |
| 74 | ret void |
| 75 | } |
| 76 | |
| 77 | ; DSE seq_cst store (be conservative; DSE doesn't have infrastructure |
| 78 | ; to reason about atomic operations). |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 79 | define void @test7() { |
| 80 | ; CHECK-LABEL: test7 |
| 81 | ; CHECK: store atomic |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 82 | %a = alloca i32 |
| 83 | store atomic i32 0, i32* %a seq_cst, align 4 |
| 84 | ret void |
| 85 | } |
| 86 | |
| 87 | ; DSE and seq_cst load (be conservative; DSE doesn't have infrastructure |
| 88 | ; to reason about atomic operations). |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 89 | define i32 @test8() { |
| 90 | ; CHECK-LABEL: test8 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 91 | ; CHECK: store |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 92 | ; CHECK: load atomic |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 93 | %a = alloca i32 |
| 94 | call void @randomop(i32* %a) |
| 95 | store i32 0, i32* %a, align 4 |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 96 | %x = load atomic i32, i32* @x seq_cst, align 4 |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 97 | ret i32 %x |
| 98 | } |
| 99 | |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 100 | ; DSE across monotonic load (allowed as long as the eliminated store isUnordered) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 101 | define i32 @test9() { |
| 102 | ; CHECK-LABEL: test9 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 103 | ; CHECK-NOT: store i32 0 |
| 104 | ; CHECK: store i32 1 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 105 | store i32 0, i32* @x |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 106 | %x = load atomic i32, i32* @y monotonic, align 4 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 107 | store i32 1, i32* @x |
| 108 | ret i32 %x |
| 109 | } |
| 110 | |
| 111 | ; DSE across monotonic store (allowed as long as the eliminated store isUnordered) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 112 | define void @test10() { |
| 113 | ; CHECK-LABEL: test10 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 114 | ; CHECK-NOT: store i32 0 |
| 115 | ; CHECK: store i32 1 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 116 | store i32 0, i32* @x |
| 117 | store atomic i32 42, i32* @y monotonic, align 4 |
| 118 | store i32 1, i32* @x |
| 119 | ret void |
| 120 | } |
| 121 | |
| 122 | ; DSE across monotonic load (forbidden since the eliminated store is atomic) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 123 | define i32 @test11() { |
| 124 | ; CHECK-LABEL: test11 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 125 | ; CHECK: store atomic i32 0 |
| 126 | ; CHECK: store atomic i32 1 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 127 | store atomic i32 0, i32* @x monotonic, align 4 |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 128 | %x = load atomic i32, i32* @y monotonic, align 4 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 129 | store atomic i32 1, i32* @x monotonic, align 4 |
| 130 | ret i32 %x |
| 131 | } |
| 132 | |
| 133 | ; DSE across monotonic store (forbidden since the eliminated store is atomic) |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 134 | define void @test12() { |
| 135 | ; CHECK-LABEL: test12 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 136 | ; CHECK: store atomic i32 0 |
| 137 | ; CHECK: store atomic i32 1 |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 138 | store atomic i32 0, i32* @x monotonic, align 4 |
| 139 | store atomic i32 42, i32* @y monotonic, align 4 |
| 140 | store atomic i32 1, i32* @x monotonic, align 4 |
| 141 | ret void |
| 142 | } |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 143 | |
| 144 | ; DSE is allowed across a pair of an atomic read and then write. |
| 145 | define i32 @test13() { |
| 146 | ; CHECK-LABEL: test13 |
| 147 | ; CHECK-NOT: store i32 0 |
| 148 | ; CHECK: store i32 1 |
| 149 | store i32 0, i32* @x |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 150 | %x = load atomic i32, i32* @y seq_cst, align 4 |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 151 | store atomic i32 %x, i32* @y seq_cst, align 4 |
| 152 | store i32 1, i32* @x |
| 153 | ret i32 %x |
| 154 | } |
| 155 | |
| 156 | ; Same if it is acquire-release instead of seq_cst/seq_cst |
| 157 | define i32 @test14() { |
| 158 | ; CHECK-LABEL: test14 |
| 159 | ; CHECK-NOT: store i32 0 |
| 160 | ; CHECK: store i32 1 |
| 161 | store i32 0, i32* @x |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 162 | %x = load atomic i32, i32* @y acquire, align 4 |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 163 | store atomic i32 %x, i32* @y release, align 4 |
| 164 | store i32 1, i32* @x |
| 165 | ret i32 %x |
| 166 | } |
| 167 | |
| 168 | ; But DSE is not allowed across a release-acquire pair. |
| 169 | define i32 @test15() { |
| 170 | ; CHECK-LABEL: test15 |
| 171 | ; CHECK: store i32 0 |
| 172 | ; CHECK: store i32 1 |
| 173 | store i32 0, i32* @x |
| 174 | store atomic i32 0, i32* @y release, align 4 |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame^] | 175 | %x = load atomic i32, i32* @y acquire, align 4 |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 176 | store i32 1, i32* @x |
| 177 | ret i32 %x |
| 178 | } |