summaryrefslogtreecommitdiffstats
path: root/test/CodeGen/switch-dce.c
blob: a18d3bc89e7d325fe3fd9bce4094641094aa5bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// RUN: %clang_cc1 -triple i386-unknown-unknown -O0 %s -emit-llvm -o - | FileCheck %s

// PR9322 and rdar://6970405

// CHECK: @test1
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32 {{.*}}, 1
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
int i;
void dead();

void test1() {
  switch (1)
    case 1:
      ++i;

  switch (0)
    case 1:
      dead();
} 


// CHECK: @test2
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32 {{.*}}, 2
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test2() {
  switch (4) {
  case 1:
    dead();
    break;
  case 4:
    i += 2;
    // Fall off the end of the switch.
  } 
}


// CHECK: @test3
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32 {{.*}}, 2
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test3() {
  switch (4) {
  case 1:
    dead();
    break;
  case 4: {
    i += 2;
    break;
  }
  } 
}

// CHECK: @test4
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32 {{.*}}, 2
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test4() {
  switch (4) {
    case 1:
      dead();
      break;
    default: {
      i += 2;
      break;
    }
  } 
}

// This shouldn't crash codegen, but we don't have to optimize out the switch
// in this case.
void test5() {
  switch (1) {
    int x;  // eliding var decl?
    case 1:
      x = 4;
      i = x;
      break;
  } 
}

// CHECK: @test6
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test6() {
  // Neither case is reachable.
  switch (40) {
  case 1:
   dead();
    break;
  case 4: {
    dead();
    break;
  }
  } 
}

// CHECK: @test7
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test7() {
  switch (4) {
  case 1:
      dead();
    break;
    {
      case 4:   // crazy brace scenario
        ++i;
    }
    break;
  } 
}

// CHECK: @test8
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test8() {
  switch (4) {
  case 1:
    dead();
    break;
  case 4:
    ++i;
    // Fall off the end of the switch.
  } 
}

// CHECK: @test9
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: add nsw i32
// CHECK: add nsw i32
// CHECK-NOT: switch
// CHECK-NOT: @dead
// CHECK: ret void
void test9(int i) {
  switch (1) {
  case 5:
    dead();
  case 1:
    ++i;
    // Fall through is fine.
  case 4:
    ++i;
    break;
  } 
}

// CHECK: @test10
// CHECK-NOT: switch
// CHECK: ret i32
int test10(void) {
	switch(8) {
		case 8:
			break;
		case 4:
			break;
		default:
			dead();
	}
	
	return 0;
}

// CHECK: @test11
// CHECK-NOT: switch
// CHECK: ret void
void test11() {
  switch (1) {
    case 1:
      break;
    case 42: ;
      int x;  // eliding var decl?
      x = 4;
      break;
  }
}

// CHECK: @test12
// CHECK-NOT: switch
// CHECK: ret void
void test12() {
  switch (1) {
  case 2: {
     int a;   // Ok to skip this vardecl.
     a = 42;
   }
  case 1:
    break;
  case 42: ;
    int x;  // eliding var decl?
    x = 4;
    break;
  }
}

// Verify that case 42 only calls test14 once.
// CHECK: @test13
// CHECK: call void @test13(i32 97)
// CHECK-NEXT: br label %[[EPILOG2:[0-9.a-z]+]]
// CHECK: [[EPILOG2]]
// CHECK-NEXT: br label [[EPILOG:%[0-9.a-z]+]]
// CHECK: call void @test13(i32 42)
// CHECK-NEXT: br label [[EPILOG]]
void test13(int x) {
  switch (x) {
    case 42: test13(97);  // fallthrough
    case 11: break;
    default: test13(42); break;
  }
}