llvm-6502/lib/Transforms/Utils
Jim Grosbach dc2dc390f6 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
..
AddDiscriminators.cpp
ASanStackFrameLayout.cpp
BasicBlockUtils.cpp
BreakCriticalEdges.cpp
BuildLibCalls.cpp
BypassSlowDivision.cpp
CloneFunction.cpp Revert "Patch for function cloning to inline all blocks whose address is taken" 2014-05-19 16:04:10 +00:00
CloneModule.cpp Allow alias to point to an arbitrary ConstantExpr. 2014-06-03 02:41:57 +00:00
CMakeLists.txt
CmpInstAnalysis.cpp
CodeExtractor.cpp
CtorUtils.cpp
DemoteRegToStack.cpp
FlattenCFG.cpp
GlobalStatus.cpp
InlineFunction.cpp Fix line numbers for code inlined from __nodebug__ functions. 2014-06-09 09:09:19 +00:00
InstructionNamer.cpp
IntegerDivision.cpp
LCSSA.cpp
LLVMBuild.txt
Local.cpp
LoopSimplify.cpp
LoopUnroll.cpp LCSSA should be performed on the outermost affected loop while unrolling loop. 2014-05-29 06:47:23 +00:00
LoopUnrollRuntime.cpp
LowerExpectIntrinsic.cpp
LowerInvoke.cpp
LowerSwitch.cpp LowerSwitch: track bounding range for the condition tree. 2014-06-16 16:55:20 +00:00
Makefile
Mem2Reg.cpp
MetaRenamer.cpp
ModuleUtils.cpp
PromoteMemoryToRegister.cpp
SimplifyCFG.cpp Make bitcast, extractelement, and insertelement considered cheap for speculation. 2014-05-30 18:34:43 +00:00
SimplifyIndVar.cpp
SimplifyInstructions.cpp
SimplifyLibCalls.cpp Add support for missed and analysis optimization remarks. 2014-05-22 14:19:46 +00:00
SpecialCaseList.cpp Remove 'using std::errro_code' from lib. 2014-06-13 02:24:39 +00:00
SSAUpdater.cpp
UnifyFunctionExitNodes.cpp
Utils.cpp
ValueMapper.cpp