105 lines
3.7 KiB
LLVM
Raw Normal View History

; RUN: opt < %s -lowerswitch -S | FileCheck %s
; We have switch on input.
; On output we should got binary comparison tree. Check that all is fine.
LowerSwitch: track bounding range for the condition tree. When LowerSwitch transforms a switch instruction into a tree of ifs it is actually performing a binary search into the various case ranges, to see if the current value falls into one cases range of values. So, if we have a program with something like this: switch (a) { case 0: do0(); break; case 1: do1(); break; case 2: do2(); break; default: break; } the code produced is something like this: if (a < 1) { if (a == 0) { do0(); } } else { if (a < 2) { if (a == 1) { do1(); } } else { if (a == 2) { do2(); } } } This code is inefficient because the check (a == 1) to execute do1() is not needed. The reason is that because we already checked that (a >= 1) initially by checking that also (a < 2) we basically already inferred that (a == 1) without the need of an extra basic block spawned to check if actually (a == 1). The patch addresses this problem by keeping track of already checked bounds in the LowerSwitch algorithm, so that when the time arrives to produce a Leaf Block that checks the equality with the case value / range the algorithm can decide if that block is really needed depending on the already checked bounds . For example, the above with "a = 1" would work like this: the bounds start as LB: NONE , UB: NONE as (a < 1) is emitted the bounds for the else path become LB: 1 UB: NONE. This happens because by failing the test (a < 1) we know that the value "a" cannot be smaller than 1 if we enter the else branch. After the emitting the check (a < 2) the bounds in the if branch become LB: 1 UB: 1. This is because by checking that "a" is smaller than 2 then the upper bound becomes 2 - 1 = 1. When it is time to emit the leaf block for "case 1:" we notice that 1 can be squeezed exactly in between the LB and UB, which means that if we arrived to that block there is no need to emit a block that checks if (a == 1). Patch by: Marcello Maggioni <hayarms@gmail.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211038 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-16 16:55:20 +00:00
;CHECK: entry:
;CHECK-NEXT: br label %NodeBlock.19
;CHECK: NodeBlock.19: ; preds = %entry
;CHECK-NEXT: %Pivot.20 = icmp slt i32 %tmp158, 10
;CHECK-NEXT: br i1 %Pivot.20, label %NodeBlock.5, label %NodeBlock.17
;CHECK: NodeBlock.17: ; preds = %NodeBlock.19
;CHECK-NEXT: %Pivot.18 = icmp slt i32 %tmp158, 13
;CHECK-NEXT: br i1 %Pivot.18, label %NodeBlock.9, label %NodeBlock.15
;CHECK: NodeBlock.15: ; preds = %NodeBlock.17
;CHECK-NEXT: %Pivot.16 = icmp slt i32 %tmp158, 14
;CHECK-NEXT: br i1 %Pivot.16, label %bb330, label %NodeBlock.13
;CHECK: NodeBlock.13: ; preds = %NodeBlock.15
;CHECK-NEXT: %Pivot.14 = icmp slt i32 %tmp158, 15
;CHECK-NEXT: br i1 %Pivot.14, label %bb332, label %LeafBlock.11
;CHECK: LeafBlock.11: ; preds = %NodeBlock.13
LowerSwitch: track bounding range for the condition tree. When LowerSwitch transforms a switch instruction into a tree of ifs it is actually performing a binary search into the various case ranges, to see if the current value falls into one cases range of values. So, if we have a program with something like this: switch (a) { case 0: do0(); break; case 1: do1(); break; case 2: do2(); break; default: break; } the code produced is something like this: if (a < 1) { if (a == 0) { do0(); } } else { if (a < 2) { if (a == 1) { do1(); } } else { if (a == 2) { do2(); } } } This code is inefficient because the check (a == 1) to execute do1() is not needed. The reason is that because we already checked that (a >= 1) initially by checking that also (a < 2) we basically already inferred that (a == 1) without the need of an extra basic block spawned to check if actually (a == 1). The patch addresses this problem by keeping track of already checked bounds in the LowerSwitch algorithm, so that when the time arrives to produce a Leaf Block that checks the equality with the case value / range the algorithm can decide if that block is really needed depending on the already checked bounds . For example, the above with "a = 1" would work like this: the bounds start as LB: NONE , UB: NONE as (a < 1) is emitted the bounds for the else path become LB: 1 UB: NONE. This happens because by failing the test (a < 1) we know that the value "a" cannot be smaller than 1 if we enter the else branch. After the emitting the check (a < 2) the bounds in the if branch become LB: 1 UB: 1. This is because by checking that "a" is smaller than 2 then the upper bound becomes 2 - 1 = 1. When it is time to emit the leaf block for "case 1:" we notice that 1 can be squeezed exactly in between the LB and UB, which means that if we arrived to that block there is no need to emit a block that checks if (a == 1). Patch by: Marcello Maggioni <hayarms@gmail.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211038 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-16 16:55:20 +00:00
;CHECK-NEXT: %SwitchLeaf12 = icmp eq i32 %tmp158, 15
;CHECK-NEXT: br i1 %SwitchLeaf12, label %bb334, label %NewDefault
;CHECK: NodeBlock.9: ; preds = %NodeBlock.17
;CHECK-NEXT: %Pivot.10 = icmp slt i32 %tmp158, 11
;CHECK-NEXT: br i1 %Pivot.10, label %bb324, label %NodeBlock.7
;CHECK: NodeBlock.7: ; preds = %NodeBlock.9
;CHECK-NEXT: %Pivot.8 = icmp slt i32 %tmp158, 12
;CHECK-NEXT: br i1 %Pivot.8, label %bb326, label %bb328
;CHECK: NodeBlock.5: ; preds = %NodeBlock.19
;CHECK-NEXT: %Pivot.6 = icmp slt i32 %tmp158, 7
;CHECK-NEXT: br i1 %Pivot.6, label %NodeBlock, label %NodeBlock.3
;CHECK: NodeBlock.3: ; preds = %NodeBlock.5
;CHECK-NEXT: %Pivot.4 = icmp slt i32 %tmp158, 8
;CHECK-NEXT: br i1 %Pivot.4, label %bb, label %NodeBlock.1
;CHECK: NodeBlock.1: ; preds = %NodeBlock.3
;CHECK-NEXT: %Pivot.2 = icmp slt i32 %tmp158, 9
;CHECK-NEXT: br i1 %Pivot.2, label %bb338, label %bb322
;CHECK: NodeBlock: ; preds = %NodeBlock.5
LowerSwitch: track bounding range for the condition tree. When LowerSwitch transforms a switch instruction into a tree of ifs it is actually performing a binary search into the various case ranges, to see if the current value falls into one cases range of values. So, if we have a program with something like this: switch (a) { case 0: do0(); break; case 1: do1(); break; case 2: do2(); break; default: break; } the code produced is something like this: if (a < 1) { if (a == 0) { do0(); } } else { if (a < 2) { if (a == 1) { do1(); } } else { if (a == 2) { do2(); } } } This code is inefficient because the check (a == 1) to execute do1() is not needed. The reason is that because we already checked that (a >= 1) initially by checking that also (a < 2) we basically already inferred that (a == 1) without the need of an extra basic block spawned to check if actually (a == 1). The patch addresses this problem by keeping track of already checked bounds in the LowerSwitch algorithm, so that when the time arrives to produce a Leaf Block that checks the equality with the case value / range the algorithm can decide if that block is really needed depending on the already checked bounds . For example, the above with "a = 1" would work like this: the bounds start as LB: NONE , UB: NONE as (a < 1) is emitted the bounds for the else path become LB: 1 UB: NONE. This happens because by failing the test (a < 1) we know that the value "a" cannot be smaller than 1 if we enter the else branch. After the emitting the check (a < 2) the bounds in the if branch become LB: 1 UB: 1. This is because by checking that "a" is smaller than 2 then the upper bound becomes 2 - 1 = 1. When it is time to emit the leaf block for "case 1:" we notice that 1 can be squeezed exactly in between the LB and UB, which means that if we arrived to that block there is no need to emit a block that checks if (a == 1). Patch by: Marcello Maggioni <hayarms@gmail.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211038 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-16 16:55:20 +00:00
;CHECK-NEXT: %Pivot = icmp slt i32 %tmp158, 0
;CHECK-NEXT: br i1 %Pivot, label %LeafBlock, label %bb338
LowerSwitch: track bounding range for the condition tree. When LowerSwitch transforms a switch instruction into a tree of ifs it is actually performing a binary search into the various case ranges, to see if the current value falls into one cases range of values. So, if we have a program with something like this: switch (a) { case 0: do0(); break; case 1: do1(); break; case 2: do2(); break; default: break; } the code produced is something like this: if (a < 1) { if (a == 0) { do0(); } } else { if (a < 2) { if (a == 1) { do1(); } } else { if (a == 2) { do2(); } } } This code is inefficient because the check (a == 1) to execute do1() is not needed. The reason is that because we already checked that (a >= 1) initially by checking that also (a < 2) we basically already inferred that (a == 1) without the need of an extra basic block spawned to check if actually (a == 1). The patch addresses this problem by keeping track of already checked bounds in the LowerSwitch algorithm, so that when the time arrives to produce a Leaf Block that checks the equality with the case value / range the algorithm can decide if that block is really needed depending on the already checked bounds . For example, the above with "a = 1" would work like this: the bounds start as LB: NONE , UB: NONE as (a < 1) is emitted the bounds for the else path become LB: 1 UB: NONE. This happens because by failing the test (a < 1) we know that the value "a" cannot be smaller than 1 if we enter the else branch. After the emitting the check (a < 2) the bounds in the if branch become LB: 1 UB: 1. This is because by checking that "a" is smaller than 2 then the upper bound becomes 2 - 1 = 1. When it is time to emit the leaf block for "case 1:" we notice that 1 can be squeezed exactly in between the LB and UB, which means that if we arrived to that block there is no need to emit a block that checks if (a == 1). Patch by: Marcello Maggioni <hayarms@gmail.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211038 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-16 16:55:20 +00:00
;CHECK: LeafBlock: ; preds = %NodeBlock
;CHECK-NEXT: %tmp158.off = add i32 %tmp158, 6
;CHECK-NEXT: %SwitchLeaf = icmp ule i32 %tmp158.off, 4
;CHECK-NEXT: br i1 %SwitchLeaf, label %bb338, label %NewDefault
define i32 @main(i32 %tmp158) {
entry:
switch i32 %tmp158, label %bb336 [
i32 -2, label %bb338
i32 -3, label %bb338
i32 -4, label %bb338
i32 -5, label %bb338
i32 -6, label %bb338
i32 0, label %bb338
i32 1, label %bb338
i32 2, label %bb338
i32 3, label %bb338
i32 4, label %bb338
i32 5, label %bb338
i32 6, label %bb338
i32 7, label %bb
i32 8, label %bb338
i32 9, label %bb322
i32 10, label %bb324
i32 11, label %bb326
i32 12, label %bb328
i32 13, label %bb330
i32 14, label %bb332
i32 15, label %bb334
]
bb:
ret i32 2
bb322:
ret i32 3
bb324:
ret i32 4
bb326:
ret i32 5
bb328:
ret i32 6
bb330:
ret i32 7
bb332:
ret i32 8
bb334:
ret i32 9
bb336:
ret i32 10
bb338:
ret i32 11
}