Chandler Carruth | 1353f9a | 2017-04-27 18:45:20 +0000 | [diff] [blame] | 1 | ; RUN: opt -passes='loop(unswitch),verify<loops>' -S < %s | FileCheck %s |
| 2 | |
| 3 | declare void @unknown() |
| 4 | declare void @unknown2() |
| 5 | |
| 6 | @y = global i64 0, align 8 |
| 7 | |
| 8 | ; The following is approximately: |
| 9 | ; void f(bool *x) { |
| 10 | ; for (int i = 0; i < 1; ++i) { |
| 11 | ; if (*x) { |
| 12 | ; if (y) |
| 13 | ; unknown(); |
| 14 | ; else |
| 15 | ; break; |
| 16 | ; } |
| 17 | ; } |
| 18 | ; } |
| 19 | ; With MemorySanitizer, the loop can not be unswitched on "y", because "y" could |
| 20 | ; be uninitialized when x == false. |
| 21 | ; Test that the branch on "y" is inside the loop (after the first unconditional |
| 22 | ; branch). |
| 23 | |
| 24 | define void @may_not_execute_trivial(i1* %x) sanitize_memory { |
| 25 | ; CHECK-LABEL: @may_not_execute_trivial( |
| 26 | entry: |
| 27 | %y = load i64, i64* @y, align 8 |
| 28 | %y.cmp = icmp eq i64 %y, 0 |
| 29 | br label %for.body |
| 30 | ; CHECK: %[[Y:.*]] = load i64, i64* @y |
| 31 | ; CHECK: %[[YCMP:.*]] = icmp eq i64 %[[Y]], 0 |
| 32 | ; CHECK-NOT: br i1 |
| 33 | ; CHECK: br label %for.body |
| 34 | |
| 35 | for.body: |
| 36 | %i = phi i32 [ 0, %entry ], [ %inc, %for.inc ] |
| 37 | %x.load = load i1, i1* %x |
| 38 | br i1 %x.load, label %for.inc, label %if.then |
| 39 | ; CHECK: %[[XLOAD:.*]] = load i1, i1* %x |
| 40 | ; CHECK: br i1 %[[XLOAD]] |
| 41 | |
| 42 | if.then: |
| 43 | br i1 %y.cmp, label %for.end, label %if.then4 |
| 44 | ; CHECK: br i1 %[[YCMP]] |
| 45 | |
| 46 | if.then4: |
| 47 | call void @unknown() |
| 48 | br label %for.inc |
| 49 | |
| 50 | for.inc: |
| 51 | %inc = add nsw i32 %i, 1 |
| 52 | %cmp = icmp slt i32 %inc, 1 |
| 53 | br i1 %cmp, label %for.body, label %for.end |
| 54 | |
| 55 | for.end: |
| 56 | ret void |
| 57 | } |
| 58 | |
| 59 | |
| 60 | ; The same as above, but "y" is a function parameter instead of a global. |
| 61 | ; This shows that it is not enough to suppress hoisting of load instructions, |
| 62 | ; the actual problem is in the speculative branching. |
| 63 | |
| 64 | define void @may_not_execute2_trivial(i1* %x, i1 %y) sanitize_memory { |
| 65 | ; CHECK-LABEL: @may_not_execute2_trivial( |
| 66 | entry: |
| 67 | br label %for.body |
| 68 | ; CHECK-NOT: br i1 |
| 69 | ; CHECK: br label %for.body |
| 70 | |
| 71 | for.body: |
| 72 | %i = phi i32 [ 0, %entry ], [ %inc, %for.inc ] |
| 73 | %x.load = load i1, i1* %x |
| 74 | br i1 %x.load, label %for.inc, label %if.then |
| 75 | ; CHECK: %[[XLOAD:.*]] = load i1, i1* %x |
| 76 | ; CHECK: br i1 %[[XLOAD]] |
| 77 | |
| 78 | if.then: |
| 79 | br i1 %y, label %for.end, label %if.then4 |
| 80 | ; CHECK: br i1 %y |
| 81 | |
| 82 | if.then4: |
| 83 | call void @unknown() |
| 84 | br label %for.inc |
| 85 | |
| 86 | for.inc: |
| 87 | %inc = add nsw i32 %i, 1 |
| 88 | %cmp = icmp slt i32 %inc, 1 |
| 89 | br i1 %cmp, label %for.body, label %for.end |
| 90 | |
| 91 | for.end: |
| 92 | ret void |
| 93 | } |
| 94 | |
| 95 | |
| 96 | ; The following is approximately: |
| 97 | ; void f() { |
| 98 | ; for (int i = 0; i < 1; ++i) { |
| 99 | ; if (y) |
| 100 | ; unknown(); |
| 101 | ; else |
| 102 | ; break; |
| 103 | ; } |
| 104 | ; } |
| 105 | ; "if (y)" is guaranteed to execute; the loop can be unswitched. |
| 106 | |
| 107 | define void @must_execute_trivial() sanitize_memory { |
| 108 | ; CHECK-LABEL: @must_execute_trivial( |
| 109 | entry: |
| 110 | %y = load i64, i64* @y, align 8 |
| 111 | %y.cmp = icmp eq i64 %y, 0 |
| 112 | br label %for.body |
| 113 | ; CHECK: %[[Y:.*]] = load i64, i64* @y |
| 114 | ; CHECK: %[[YCMP:.*]] = icmp eq i64 %[[Y]], 0 |
| 115 | ; CHECK: br i1 %[[YCMP]], label %[[EXIT_SPLIT:.*]], label %[[PH:.*]] |
| 116 | ; |
| 117 | ; CHECK: [[PH]]: |
| 118 | ; CHECK: br label %for.body |
| 119 | |
| 120 | for.body: |
| 121 | %i = phi i32 [ 0, %entry ], [ %inc, %for.inc ] |
| 122 | br i1 %y.cmp, label %for.end, label %if.then4 |
| 123 | ; CHECK: br label %if.then4 |
| 124 | |
| 125 | if.then4: |
| 126 | call void @unknown() |
| 127 | br label %for.inc |
| 128 | |
| 129 | for.inc: |
| 130 | %inc = add nsw i32 %i, 1 |
| 131 | %cmp = icmp slt i32 %inc, 1 |
| 132 | br i1 %cmp, label %for.body, label %for.end |
| 133 | |
| 134 | for.end: |
| 135 | ret void |
| 136 | ; CHECK: for.end: |
| 137 | ; CHECK: br label %[[EXIT_SPLIT]] |
| 138 | ; |
| 139 | ; CHECK: [[EXIT_SPLIT]]: |
| 140 | ; CHECK: ret void |
| 141 | } |