[llvm-commits] [llvm] r121680 - in /llvm/trunk: lib/Transforms/Utils/SimplifyCFG.cpp test/Transforms/SimplifyCFG/switch_create.ll

Chris Lattner sabre at nondot.org
Sun Dec 12 20:50:39 PST 2010


Author: lattner
Date: Sun Dec 12 22:50:38 2010
New Revision: 121680

URL: http://llvm.org/viewvc/llvm-project?rev=121680&view=rev
Log:
enhance the "change or icmp's into switch" xform to handle one value in an 
'or sequence' that it doesn't understand.  This allows us to optimize
something insane like this:

int crud (unsigned char c, unsigned x)
 {
   if(((((((((( (int) c <= 32 ||
                    (int) c == 46) || (int) c == 44)
                  || (int) c == 58) || (int) c == 59) || (int) c == 60)
               || (int) c == 62) || (int) c == 34) || (int) c == 92)
            || (int) c == 39) != 0)
     foo();
 }

into:

define i32 @crud(i8 zeroext %c, i32 %x) nounwind ssp noredzone {
entry:
  %cmp = icmp ult i8 %c, 33
  br i1 %cmp, label %if.then, label %switch.early.test

switch.early.test:                                ; preds = %entry
  switch i8 %c, label %if.end [
    i8 39, label %if.then
    i8 44, label %if.then
    i8 58, label %if.then
    i8 59, label %if.then
    i8 60, label %if.then
    i8 62, label %if.then
    i8 46, label %if.then
    i8 92, label %if.then
    i8 34, label %if.then
  ]

by pulling the < comparison out ahead of the newly formed switch.


Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/test/Transforms/SimplifyCFG/switch_create.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=121680&r1=121679&r2=121680&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sun Dec 12 22:50:38 2010
@@ -301,6 +301,7 @@
   Instruction *I = dyn_cast<Instruction>(V);
   if (I == 0) return 0;
   
+  // If this is an icmp against a constant, handle this as one of the cases.
   if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
     if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE))
       if (ConstantInt *C = GetConstantInt(I->getOperand(1), TD)) {
@@ -310,17 +311,43 @@
     return 0;
   }
   
+  // Otherwise, we can only handle an | or &, depending on isEQ.
   if (I->getOpcode() != (isEQ ? Instruction::Or : Instruction::And))
     return 0;
   
+  unsigned NumValsBeforeLHS = Vals.size();
   if (Value *LHS = GatherConstantCompares(I->getOperand(0), Vals, Extra, TD,
                                           isEQ)) {
+    unsigned NumVals = Vals.size();
     if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, TD,
                                             isEQ)) {
       if (LHS == RHS)
         return LHS;
     }
+    Vals.resize(NumVals);
+
+    // The RHS of the or/and can't be folded in and we haven't used "Extra" yet,
+    // set it and return success.
+    if (Extra == 0 || Extra == I->getOperand(1)) {
+      Extra = I->getOperand(1);
+      return LHS;
+    }
+    
+    Vals.resize(NumValsBeforeLHS);
+    return 0;
+  }
+  
+  // If the LHS can't be folded in, but Extra is available and RHS can, try to
+  // use LHS as Extra.
+  if (Extra == 0 || Extra == I->getOperand(0)) {
+    Extra = I->getOperand(0);
+    if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, TD,
+                                            isEQ))
+      return RHS;
+    Vals.resize(NumValsBeforeLHS);
+    Extra = 0;
   }
+  
   return 0;
 }
       
@@ -2059,7 +2086,9 @@
         }
       }
 
-      if (CompVal) {
+      // We do this transformation if we'll end up creating a switch with at
+      // least two values if Extra is used.
+      if (CompVal && (!ExtraCase || Values.size() > 1)) {
         // There might be duplicate constants in the list, which the switch
         // instruction can't handle, remove them now.
         array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
@@ -2070,6 +2099,19 @@
         BasicBlock *EdgeBB    = BI->getSuccessor(0);
         if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
         
+        // If there are any extra values that couldn't be folded into the switch
+        // then we evaluate them with an explicit branch first.  Split the block
+        // right before the condbr to handle it.
+        if (ExtraCase) {
+          BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test");
+          // Remove the uncond branch added to the old block.
+          TerminatorInst *OldTI = BB->getTerminator();
+          
+          BranchInst::Create(EdgeBB, NewBB, ExtraCase, OldTI);
+          OldTI->eraseFromParent();
+          BB = NewBB;
+        }
+         
         // Convert pointer to int before we switch.
         if (CompVal->getType()->isPointerTy()) {
           assert(TD && "Cannot switch on pointer without TargetData");
@@ -2079,8 +2121,8 @@
         }
         
         // Create the new switch instruction now.
-        SwitchInst *New = SwitchInst::Create(CompVal, DefaultBB,
-                                             Values.size(), BI);
+        SwitchInst *New =
+          SwitchInst::Create(CompVal, DefaultBB, Values.size(), BI);
         
         // Add all of the 'cases' to the switch instruction.
         for (unsigned i = 0, e = Values.size(); i != e; ++i)

Modified: llvm/trunk/test/Transforms/SimplifyCFG/switch_create.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/switch_create.ll?rev=121680&r1=121679&r2=121680&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/switch_create.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/switch_create.ll Sun Dec 12 22:50:38 2010
@@ -149,7 +149,30 @@
 ; CHECK:       i32 18, label %UnifiedReturnBlock
 ; CHECK:       i32 19, label %switch.edge
 ; CHECK:     ]
-
 }
 
+define void @test7(i8 zeroext %c, i32 %x) nounwind ssp noredzone {
+entry:
+  %cmp = icmp ult i32 %x, 32
+  %cmp4 = icmp eq i8 %c, 97
+  %or.cond = or i1 %cmp, %cmp4
+  %cmp9 = icmp eq i8 %c, 99
+  %or.cond11 = or i1 %or.cond, %cmp9
+  br i1 %or.cond11, label %if.then, label %if.end
+
+if.then:                                          ; preds = %entry
+  tail call void @foo1() nounwind noredzone
+  ret void
 
+if.end:                                           ; preds = %entry
+  ret void
+  
+; CHECK: @test7
+; CHECK:   %cmp = icmp ult i32 %x, 32
+; CHECK:   br i1 %cmp, label %if.then, label %switch.early.test
+; CHECK: switch.early.test:
+; CHECK:   switch i8 %c, label %if.end [
+; CHECK:     i8 99, label %if.then
+; CHECK:     i8 97, label %if.then
+; CHECK:   ]
+}





More information about the llvm-commits mailing list