diff options
author | Chris Lattner <sabre@nondot.org> | 2009-03-24 18:15:30 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-03-24 18:15:30 +0000 |
commit | 4f9797d683156bc5b59f5be66d0c01a369935ba1 (patch) | |
tree | 82b5a2e75f2964565e2b7da2dfe13328171026f1 /lib | |
parent | e330ae5b32fa5f798894cec207fd9e4fcc578382 (diff) | |
download | external_llvm-4f9797d683156bc5b59f5be66d0c01a369935ba1.zip external_llvm-4f9797d683156bc5b59f5be66d0c01a369935ba1.tar.gz external_llvm-4f9797d683156bc5b59f5be66d0c01a369935ba1.tar.bz2 |
two changes:
1. Make instcombine always canonicalize trunc x to i1 into an icmp(x&1). This
exposes the AND to other instcombine xforms and is more of what the code
generator expects.
2. Rewrite the remaining trunc pattern match to use 'match', which
simplifies it a lot.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@67635 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 74 |
1 files changed, 29 insertions, 45 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 2696328..e4e7971 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -6575,8 +6575,8 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // preferable because it allows the C<<Y expression to be hoisted out // of a loop if Y is invariant and X is not. if (Shift && Shift->hasOneUse() && RHSV == 0 && - ICI.isEquality() && !Shift->isArithmeticShift() && - isa<Instruction>(Shift->getOperand(0))) { + ICI.isEquality() && !Shift->isArithmeticShift()/* && + isa<Instruction>(Shift->getOperand(0))*/) { // Compute C << Y. Value *NS; if (Shift->getOpcode() == Instruction::LShr) { @@ -8147,49 +8147,33 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) { const Type *Ty = CI.getType(); uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); - - if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { - switch (SrcI->getOpcode()) { - default: break; - case Instruction::LShr: - // We can shrink lshr to something smaller if we know the bits shifted in - // are already zeros. - if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { - uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); - - // Get a mask for the bits shifting in. - APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); - Value* SrcIOp0 = SrcI->getOperand(0); - if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { - if (ShAmt >= DestBitWidth) // All zeros. - return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); - - // Okay, we can shrink this. Truncate the input, then return a new - // shift. - Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); - Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), - Ty, CI); - return BinaryOperator::CreateLShr(V1, V2); - } - } else { // This is a variable shr. - - // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is - // more LLVM instructions, but allows '1 << Y' to be hoisted if - // loop-invariant and CSE'd. - if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { - Value *One = ConstantInt::get(SrcI->getType(), 1); - - Value *V = InsertNewInstBefore( - BinaryOperator::CreateShl(One, SrcI->getOperand(1), - "tmp"), CI); - V = InsertNewInstBefore(BinaryOperator::CreateAnd(V, - SrcI->getOperand(0), - "tmp"), CI); - Value *Zero = Constant::getNullValue(V->getType()); - return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); - } - } - break; + + // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0) + if (DestBitWidth == 1) { + Constant *One = ConstantInt::get(Src->getType(), 1); + Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI); + Value *Zero = Constant::getNullValue(Src->getType()); + return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero); + } + + // Optimize trunc(lshr(), c) to pull the shift through the truncate. + ConstantInt *ShAmtV = 0; + Value *ShiftOp = 0; + if (Src->hasOneUse() && + match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) { + uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); + + // Get a mask for the bits shifting in. + APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); + if (MaskedValueIsZero(ShiftOp, Mask)) { + if (ShAmt >= DestBitWidth) // All zeros. + return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); + + // Okay, we can shrink this. Truncate the input, then return a new + // shift. + Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI); + Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty); + return BinaryOperator::CreateLShr(V1, V2); } } |