diff options
author | Chris Lattner <sabre@nondot.org> | 2010-01-10 07:08:30 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2010-01-10 07:08:30 +0000 |
commit | f4fb91181cbd3f63d487a2519c7f7be9d5e51709 (patch) | |
tree | d18a7539bd531d2349c1b1f38a1c036bf2ce87ae /lib/Transforms | |
parent | abff82d99e8251774dfdc31f6c9ca0e10a146f5a (diff) | |
download | external_llvm-f4fb91181cbd3f63d487a2519c7f7be9d5e51709.zip external_llvm-f4fb91181cbd3f63d487a2519c7f7be9d5e51709.tar.gz external_llvm-f4fb91181cbd3f63d487a2519c7f7be9d5e51709.tar.bz2 |
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93107 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/InstCombine/InstCombineCasts.cpp | 11 | ||||
-rw-r--r-- | lib/Transforms/InstCombine/InstCombineShifts.cpp | 20 |
2 files changed, 8 insertions, 23 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index 01c4f8c..b0d017e 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -680,6 +680,8 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) { // 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 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) { APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Trunc->getType(), - AndValue)); + AndValue)); } } @@ -927,6 +929,7 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) { // 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 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) { 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); } } diff --git a/lib/Transforms/InstCombine/InstCombineShifts.cpp b/lib/Transforms/InstCombine/InstCombineShifts.cpp index a2d528b..fe91da1 100644 --- a/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -325,26 +325,6 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, 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; |