[llvm-branch-commits] [llvm-branch] r115646 - /llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp

Gabor Greif ggreif at gmail.com
Tue Oct 5 11:33:29 PDT 2010


Author: ggreif
Date: Tue Oct  5 13:33:29 2010
New Revision: 115646

URL: http://llvm.org/viewvc/llvm-project?rev=115646&view=rev
Log:
extract middle value

Modified:
    llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp

Modified: llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp?rev=115646&r1=115645&r2=115646&view=diff
==============================================================================
--- llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp (original)
+++ llvm/branches/ggreif/switch-opts/lib/Transforms/Scalar/CodeGenPrepare.cpp Tue Oct  5 13:33:29 2010
@@ -876,25 +876,26 @@
 }
 
 
-static bool OptimizeSwitchInst(SwitchInst *I, Value *condition) {
+static bool OptimizeSwitchInst(SwitchInst *I, Value *OrigCondition) {
   BasicBlock *Old = I->getParent();
 
-  if (Instruction *C = dyn_cast<Instruction>(condition)) {
+  if (Instruction *C = dyn_cast<Instruction>(OrigCondition)) {
     if (Old != C->getParent())
       return false;
   }
   else
     return false;
 
-  if (condition != --BasicBlock::iterator(I))
+  if (OrigCondition != --BasicBlock::iterator(I))
     return false;
 
-  const Type *Ty = condition->getType();
+  Value *condition = OrigCondition;
+  const Type *Ty = OrigCondition->getType();
 
   if (TruncInst *T = dyn_cast<TruncInst>(condition)) {
     if (Instruction *P = dyn_cast<Instruction>(T->getOperand(0))) { 
       if (Old != P->getParent() ||
-	condition != next(BasicBlock::iterator(P))/* TODO: CHEAP TRUNCATES*/)
+          condition != next(BasicBlock::iterator(P))/* TODO: CHEAP TRUNCATES*/)
       return false;
       condition = P;
     }
@@ -908,14 +909,14 @@
       if (unsigned Leg = I->findCaseValue(Zero)) {
         BasicBlock *New = Old->splitBasicBlock(I, Old->getName()+".switch");        
         Old->getTerminator()->eraseFromParent();
-        Instruction *Cmp = new ICmpInst(*Old, CmpInst::ICMP_EQ, I->getCondition(), Zero, "tst");
+        Instruction *Cmp = new ICmpInst(*Old, CmpInst::ICMP_EQ, OrigCondition, Zero, "tst");
         BranchInst::Create(I->getSuccessor(Leg), New, Cmp, Old);
         I->removeCase(Leg);
 
-const Type *Ty2 = A->getType();
-APInt Mask(cast<IntegerType>(Ty2)->getMask());
+				//const Type *Ty2 = A->getType();
+APInt Mask(cast<IntegerType>(Ty)->getMask());
 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
-ComputeMaskedBits(A, Mask, KnownZero, KnownOne);
+ComputeMaskedBits(OrigCondition, Mask, KnownZero, KnownOne);
 APInt KnownZeroInverted(~KnownZero);
 unsigned unknown = KnownZeroInverted.countPopulation();
 if (unknown > 2) return true;
@@ -923,6 +924,14 @@
 if (unknown > 2) return true;
 APInt Middle(KnownZeroInverted | KnownOne);
 Middle.clear(KnownZeroInverted.countTrailingZeros());
+ ConstantInt *Mid(cast<ConstantInt>(ConstantInt::get(Ty, Middle)));
+if (unsigned MidLeg = I->findCaseValue(Mid)) {
+        BasicBlock *New2 = New->splitBasicBlock(I, New->getName()+".switch2");        
+        New->getTerminator()->eraseFromParent();
+				Instruction *MidCmp = new ICmpInst(*New, CmpInst::ICMP_EQ, OrigCondition, Mid, "mid?");
+        BranchInst::Create(I->getSuccessor(MidLeg), New2, MidCmp, New);
+        I->removeCase(MidLeg);
+}
         return true;
       }
     }





More information about the llvm-branch-commits mailing list