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
|
// Make sure that the dead code of an `if` (and `else if`) is elminated when the condition is constant.
// If the condition value is constant, there are two cases:
// 1) It is 0 (false) -> Generate the else block (if it exists) with no branching.
// 2) It is non-0 (true) -> Generate the if block with no branching.
// Also, verify it _does_ generate correct code when it is not constant.
// RUN: %ldc -O0 -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
// RUN: %ldc -O0 -run %s
// RUN: %ldc -O3 -run %s
extern(C): //to avoid name mangling.
void g();
// CHECK-LABEL: @foo
void foo()
{
// CHECK-NOT: %a = alloca
// CHECK: %b = alloca
// CHECK-NOT: br
// CHECK-NOT: store i32 1, ptr %a
// CHECK: store i32 2, ptr %b
if (0)
{
int a = 1;
}
else
{
int b = 2;
}
}
// CHECK-LABEL: @bar
void bar()
{
// CHECK-NOT: %a = alloca
// CHECK: store i32 2, ptr %b
if (0)
{
int a = 1;
}
else if(1)
{
int b = 2;
}
}
// CHECK-LABEL: @only_ret
void only_ret()
{
// CHECK-NEXT: ret void
// CHECK-NEXT: }
if (1 && (2 - 2))
{
int a = 1;
}
}
// CHECK-LABEL: @only_ret2
void only_ret2()
{
// CHECK-NEXT: ret void
// CHECK-NEXT: }
if (0)
{
int a = 1;
}
else if(0)
{
int b = 2;
}
}
// CHECK-LABEL: @only_ret3
void only_ret3()
{
// CHECK-NEXT: ret void
// CHECK-NEXT: }
if (!cast(void*)&only_ret2)
{
int a = 1;
}
}
// CHECK-LABEL: @gen_br
void gen_br(immutable int a)
{
// CHECK-COUNT-1: br
if (a)
{
int b = 1;
}
}
// CHECK-LABEL: @gh4556
void gh4556()
{
if ("a" is "b")
{
assert(false);
}
if ("a" !is "b")
{
return;
}
else
{
assert(false);
}
}
void main() {
gh4556();
}
|