[llvm-commits] [llvm] r93107 - in /llvm/trunk: lib/Transforms/InstCombine/InstCombineCasts.cpp lib/Transforms/InstCombine/InstCombineShifts.cpp test/Transforms/InstCombine/apint-shift.ll

Chris Lattner sabre at nondot.org
Sat Jan 9 23:08:30 PST 2010


Author: lattner
Date: Sun Jan 10 01:08:30 2010
New Revision: 93107

URL: http://llvm.org/viewvc/llvm-project?rev=93107&view=rev
Log:
change the preferred canonical form for a sign extension to be
lshr+ashr instead of trunc+sext.  We want to avoid type 
conversions whenever possible, it is easier to codegen expressions
without truncates and extensions.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
    llvm/trunk/test/Transforms/InstCombine/apint-shift.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=93107&r1=93106&r2=93107&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Sun Jan 10 01:08:30 2010
@@ -680,6 +680,8 @@
   // types and if the sizes are just right we can convert this into a logical
   // 'and' which will be much cheaper than the pair of casts.
   if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
+    // TODO: Subsume this into EvaluateInDifferentType.
+    
     // Get the sizes of the types involved.  We know that the intermediate type
     // will be smaller than A or C, but don't know the relation between A and C.
     Value *A = CSrc->getOperand(0);
@@ -707,7 +709,7 @@
       APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
       return BinaryOperator::CreateAnd(Trunc, 
                                        ConstantInt::get(Trunc->getType(),
-                                                               AndValue));
+                                                        AndValue));
     }
   }
 
@@ -927,6 +929,7 @@
     // always do the transformation.
     if (NumCastsRemoved >= 2 ||
         NumBitsSExt > DestBitSize-SrcBitSize) {
+      
       // Okay, we can transform this!  Insert the new expression now.
       DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
             " to avoid sign extend: " << CI);
@@ -939,8 +942,10 @@
           ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
         return ReplaceInstUsesWith(CI, Res);
       
-      // We need to emit a cast to truncate, then a cast to sext.
-      return new SExtInst(Builder->CreateTrunc(Res, Src->getType()), DestTy);
+      // We need to emit a shl + ashr to do the sign extend.
+      Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
+      return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
+                                        ShAmt);
     }
   }
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=93107&r1=93106&r2=93107&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Sun Jan 10 01:08:30 2010
@@ -325,26 +325,6 @@
         return BinaryOperator::CreateAnd(X,
                                         ConstantInt::get(I.getContext(), Mask));
       }
-      // We can simplify ((X << C) >>s C) into a trunc + sext.
-      // NOTE: we could do this for any C, but that would make 'unusual' integer
-      // types.  For now, just stick to ones well-supported by the code
-      // generators.
-      const Type *SExtType = 0;
-      switch (Ty->getBitWidth() - ShiftAmt1) {
-      case 1  :
-      case 8  :
-      case 16 :
-      case 32 :
-      case 64 :
-      case 128:
-        SExtType = IntegerType::get(I.getContext(),
-                                    Ty->getBitWidth() - ShiftAmt1);
-        break;
-      default: break;
-      }
-      if (SExtType)
-        return new SExtInst(Builder->CreateTrunc(X, SExtType, "sext"), Ty);
-      // Otherwise, we can't handle it yet.
     } else if (ShiftAmt1 < ShiftAmt2) {
       uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
       

Modified: llvm/trunk/test/Transforms/InstCombine/apint-shift.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/apint-shift.ll?rev=93107&r1=93106&r2=93107&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/apint-shift.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/apint-shift.ll Sun Jan 10 01:08:30 2010
@@ -168,13 +168,6 @@
 	ret i11 %D
 }
 
-define i17 @test24(i17 %X) {
-	%Y = and i17 %X, -5		; <i17> [#uses=1]
-	%Z = shl i17 %Y, 9		; <i17> [#uses=1]
-	%Q = ashr i17 %Z, 9		; <i17> [#uses=1]
-	ret i17 %Q
-}
-
 define i37 @test25(i37 %tmp.2, i37 %AA) {
 	%x = lshr i37 %AA, 17		; <i37> [#uses=1]
 	%tmp.3 = lshr i37 %tmp.2, 17		; <i37> [#uses=1]





More information about the llvm-commits mailing list