From 4f9797d683156bc5b59f5be66d0c01a369935ba1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 24 Mar 2009 18:15:30 +0000 Subject: [PATCH] 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 --- .../Scalar/InstructionCombining.cpp | 72 ++++++++----------- test/Transforms/InstCombine/shift.ll | 7 ++ 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 26963280bbe..e4e7971549e 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<hasOneUse() && RHSV == 0 && - ICI.isEquality() && !Shift->isArithmeticShift() && - isa(Shift->getOperand(0))) { + ICI.isEquality() && !Shift->isArithmeticShift()/* && + isa(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(Src->getType())->getBitWidth(); + + // 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); + } - if (Instruction *SrcI = dyn_cast(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(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; + // 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); } } diff --git a/test/Transforms/InstCombine/shift.ll b/test/Transforms/InstCombine/shift.ll index e17fd35fe40..9dc7755c1b8 100644 --- a/test/Transforms/InstCombine/shift.ll +++ b/test/Transforms/InstCombine/shift.ll @@ -206,4 +206,11 @@ define i32 @test26(i32 %A) { %D = shl i32 %C, 1 ; [#uses=1] ret i32 %D } + + +define i1 @test27(i32 %x) nounwind { + %y = lshr i32 %x, 3 + %z = trunc i32 %y to i1 + ret i1 %z +}