Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-unknown-unknown -O0 %s -emit-llvm -o - | FileCheck %s |
| 2 | |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 3 | // PR9322 and rdar://6970405 |
| 4 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 5 | // CHECK: @test1 |
| 6 | // CHECK-NOT: switch |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 7 | // CHECK-NOT: @dead |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 8 | // CHECK: add nsw i32 {{.*}}, 1 |
| 9 | // CHECK-NOT: switch |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 10 | // CHECK-NOT: @dead |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 11 | // CHECK: ret void |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 12 | int i; |
| 13 | void dead(); |
| 14 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 15 | void test1() { |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 16 | switch (1) |
| 17 | case 1: |
| 18 | ++i; |
| 19 | |
| 20 | switch (0) |
| 21 | case 1: |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 22 | dead(); |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 23 | } |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame^] | 24 | |
| 25 | |
| 26 | // CHECK: @test2 |
| 27 | // CHECK-NOT: switch |
| 28 | // CHECK-NOT: @dead |
| 29 | // CHECK: add nsw i32 {{.*}}, 2 |
| 30 | // CHECK-NOT: switch |
| 31 | // CHECK-NOT: @dead |
| 32 | // CHECK: ret void |
| 33 | void test2() { |
| 34 | switch (4) { |
| 35 | case 1: |
| 36 | dead(); |
| 37 | break; |
| 38 | case 4: |
| 39 | i += 2; |
| 40 | // Fall off the end of the switch. |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | // CHECK: @test3 |
| 46 | // CHECK-NOT: switch |
| 47 | // CHECK-NOT: @dead |
| 48 | // CHECK: add nsw i32 {{.*}}, 2 |
| 49 | // CHECK-NOT: switch |
| 50 | // CHECK-NOT: @dead |
| 51 | // CHECK: ret void |
| 52 | void test3() { |
| 53 | switch (4) { |
| 54 | case 1: |
| 55 | dead(); |
| 56 | break; |
| 57 | case 4: { |
| 58 | i += 2; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // CHECK: @test4 |
| 65 | // CHECK-NOT: switch |
| 66 | // CHECK-NOT: @dead |
| 67 | // CHECK: add nsw i32 {{.*}}, 2 |
| 68 | // CHECK-NOT: switch |
| 69 | // CHECK-NOT: @dead |
| 70 | // CHECK: ret void |
| 71 | void test4() { |
| 72 | switch (4) { |
| 73 | case 1: |
| 74 | dead(); |
| 75 | break; |
| 76 | default: { |
| 77 | i += 2; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // This shouldn't crash codegen, but we don't have to optimize out the switch |
| 84 | // in this case. |
| 85 | void test5() { |
| 86 | switch (1) { |
| 87 | int x; // eliding var decl? |
| 88 | case 1: |
| 89 | x = 4; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // CHECK: @test6 |
| 95 | // CHECK-NOT: switch |
| 96 | // CHECK-NOT: @dead |
| 97 | // CHECK: ret void |
| 98 | void test6() { |
| 99 | // Neither case is reachable. |
| 100 | switch (40) { |
| 101 | case 1: |
| 102 | dead(); |
| 103 | break; |
| 104 | case 4: { |
| 105 | dead(); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // CHECK: @test7 |
| 112 | // CHECK-NOT: switch |
| 113 | // CHECK-NOT: @dead |
| 114 | // CHECK: add nsw i32 |
| 115 | // CHECK-NOT: switch |
| 116 | // CHECK-NOT: @dead |
| 117 | // CHECK: ret void |
| 118 | void test7() { |
| 119 | switch (4) { |
| 120 | case 1: |
| 121 | dead(); |
| 122 | break; |
| 123 | { |
| 124 | case 4: // crazy brace scenario |
| 125 | ++i; |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // CHECK: @test8 |
| 132 | // CHECK-NOT: switch |
| 133 | // CHECK-NOT: @dead |
| 134 | // CHECK: add nsw i32 |
| 135 | // CHECK-NOT: switch |
| 136 | // CHECK-NOT: @dead |
| 137 | // CHECK: ret void |
| 138 | void test8() { |
| 139 | switch (4) { |
| 140 | case 1: |
| 141 | dead(); |
| 142 | break; |
| 143 | case 4: |
| 144 | ++i; |
| 145 | // Fall off the end of the switch. |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // CHECK: @test9 |
| 150 | // CHECK-NOT: switch |
| 151 | // CHECK-NOT: @dead |
| 152 | // CHECK: add nsw i32 |
| 153 | // CHECK: add nsw i32 |
| 154 | // CHECK-NOT: switch |
| 155 | // CHECK-NOT: @dead |
| 156 | // CHECK: ret void |
| 157 | void test9(int i) { |
| 158 | switch (1) { |
| 159 | case 5: |
| 160 | dead(); |
| 161 | case 1: |
| 162 | ++i; |
| 163 | // Fall through is fine. |
| 164 | case 4: |
| 165 | ++i; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |