diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/DemandedBits.cpp | 66 | ||||
-rw-r--r-- | llvm/lib/Transforms/Scalar/BDCE.cpp | 13 |
2 files changed, 28 insertions, 51 deletions
diff --git a/llvm/lib/Analysis/DemandedBits.cpp b/llvm/lib/Analysis/DemandedBits.cpp index ea5a509eeb8..6bef77176cb 100644 --- a/llvm/lib/Analysis/DemandedBits.cpp +++ b/llvm/lib/Analysis/DemandedBits.cpp @@ -39,7 +39,6 @@ #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/PassManager.h" -#include "llvm/IR/PatternMatch.h" #include "llvm/IR/Type.h" #include "llvm/IR/Use.h" #include "llvm/Pass.h" @@ -51,7 +50,6 @@ #include <cstdint> using namespace llvm; -using namespace llvm::PatternMatch; #define DEBUG_TYPE "demanded-bits" @@ -145,17 +143,17 @@ void DemandedBits::determineLiveOperandBits( } break; case Intrinsic::fshl: - case Intrinsic::fshr: { - const APInt *SA; + case Intrinsic::fshr: if (OperandNo == 2) { // Shift amount is modulo the bitwidth. For powers of two we have // SA % BW == SA & (BW - 1). if (isPowerOf2_32(BitWidth)) AB = BitWidth - 1; - } else if (match(II->getOperand(2), m_APInt(SA))) { + } else if (auto *SA = dyn_cast<ConstantInt>(II->getOperand(2))) { + // TODO: Support vectors. // Normalize to funnel shift left. APInt shifts of BitWidth are well- // defined, so no need to special-case zero shifts here. - uint64_t ShiftAmt = SA->urem(BitWidth); + uint64_t ShiftAmt = SA->getValue().urem(BitWidth); if (II->getIntrinsicID() == Intrinsic::fshr) ShiftAmt = BitWidth - ShiftAmt; @@ -166,7 +164,6 @@ void DemandedBits::determineLiveOperandBits( } break; } - } break; case Instruction::Add: case Instruction::Sub: @@ -177,9 +174,8 @@ void DemandedBits::determineLiveOperandBits( AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits()); break; case Instruction::Shl: - if (OperandNo == 0) { - const APInt *ShiftAmtC; - if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { + if (OperandNo == 0) + if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); AB = AOut.lshr(ShiftAmt); @@ -191,12 +187,10 @@ void DemandedBits::determineLiveOperandBits( else if (S->hasNoUnsignedWrap()) AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt); } - } break; case Instruction::LShr: - if (OperandNo == 0) { - const APInt *ShiftAmtC; - if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { + if (OperandNo == 0) + if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); AB = AOut.shl(ShiftAmt); @@ -205,12 +199,10 @@ void DemandedBits::determineLiveOperandBits( if (cast<LShrOperator>(UserI)->isExact()) AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt); } - } break; case Instruction::AShr: - if (OperandNo == 0) { - const APInt *ShiftAmtC; - if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { + if (OperandNo == 0) + if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); AB = AOut.shl(ShiftAmt); // Because the high input bit is replicated into the @@ -225,7 +217,6 @@ void DemandedBits::determineLiveOperandBits( if (cast<AShrOperator>(UserI)->isExact()) AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt); } - } break; case Instruction::And: AB = AOut; @@ -283,15 +274,6 @@ void DemandedBits::determineLiveOperandBits( if (OperandNo != 0) AB = AOut; break; - case Instruction::ExtractElement: - if (OperandNo == 0) - AB = AOut; - break; - case Instruction::InsertElement: - case Instruction::ShuffleVector: - if (OperandNo == 0 || OperandNo == 1) - AB = AOut; - break; } } @@ -327,9 +309,8 @@ void DemandedBits::performAnalysis() { // bits and add the instruction to the work list. For other instructions // add their operands to the work list (for integer values operands, mark // all bits as live). - Type *T = I.getType(); - if (T->isIntOrIntVectorTy()) { - if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second) + if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { + if (AliveBits.try_emplace(&I, IT->getBitWidth(), 0).second) Worklist.push_back(&I); continue; @@ -338,9 +319,8 @@ void DemandedBits::performAnalysis() { // Non-integer-typed instructions... for (Use &OI : I.operands()) { if (Instruction *J = dyn_cast<Instruction>(OI)) { - Type *T = J->getType(); - if (T->isIntOrIntVectorTy()) - AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits()); + if (IntegerType *IT = dyn_cast<IntegerType>(J->getType())) + AliveBits[J] = APInt::getAllOnesValue(IT->getBitWidth()); Worklist.push_back(J); } } @@ -356,13 +336,13 @@ void DemandedBits::performAnalysis() { LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI); APInt AOut; - if (UserI->getType()->isIntOrIntVectorTy()) { + if (UserI->getType()->isIntegerTy()) { AOut = AliveBits[UserI]; LLVM_DEBUG(dbgs() << " Alive Out: " << AOut); } LLVM_DEBUG(dbgs() << "\n"); - if (!UserI->getType()->isIntOrIntVectorTy()) + if (!UserI->getType()->isIntegerTy()) Visited.insert(UserI); KnownBits Known, Known2; @@ -371,11 +351,10 @@ void DemandedBits::performAnalysis() { // operand is added to the work-list. for (Use &OI : UserI->operands()) { if (Instruction *I = dyn_cast<Instruction>(OI)) { - Type *T = I->getType(); - if (T->isIntOrIntVectorTy()) { - unsigned BitWidth = T->getScalarSizeInBits(); + if (IntegerType *IT = dyn_cast<IntegerType>(I->getType())) { + unsigned BitWidth = IT->getBitWidth(); APInt AB = APInt::getAllOnesValue(BitWidth); - if (UserI->getType()->isIntOrIntVectorTy() && !AOut && + if (UserI->getType()->isIntegerTy() && !AOut && !isAlwaysLive(UserI)) { AB = APInt(BitWidth, 0); } else { @@ -408,14 +387,13 @@ void DemandedBits::performAnalysis() { } APInt DemandedBits::getDemandedBits(Instruction *I) { - assert(I->getType()->isIntOrIntVectorTy() && - "Not an integer or vector of integer instruction"); - performAnalysis(); + + const DataLayout &DL = I->getModule()->getDataLayout(); auto Found = AliveBits.find(I); if (Found != AliveBits.end()) return Found->second; - return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits()); + return APInt::getAllOnesValue(DL.getTypeSizeInBits(I->getType())); } bool DemandedBits::isInstructionDead(Instruction *I) { diff --git a/llvm/lib/Transforms/Scalar/BDCE.cpp b/llvm/lib/Transforms/Scalar/BDCE.cpp index f63182e57c1..3a8ef073cb4 100644 --- a/llvm/lib/Transforms/Scalar/BDCE.cpp +++ b/llvm/lib/Transforms/Scalar/BDCE.cpp @@ -38,8 +38,7 @@ STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)"); /// instruction may need to be cleared of assumptions that can no longer be /// guaranteed correct. static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) { - assert(I->getType()->isIntOrIntVectorTy() && - "Trivializing a non-integer value?"); + assert(I->getType()->isIntegerTy() && "Trivializing a non-integer value?"); // Initialize the worklist with eligible direct users. SmallVector<Instruction *, 16> WorkList; @@ -47,13 +46,13 @@ static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) { // If all bits of a user are demanded, then we know that nothing below that // in the def-use chain needs to be changed. auto *J = dyn_cast<Instruction>(JU); - if (J && J->getType()->isIntOrIntVectorTy() && + if (J && J->getType()->isSized() && !DB.getDemandedBits(J).isAllOnesValue()) WorkList.push_back(J); - // Note that we need to check for non-int types above before asking for + // Note that we need to check for unsized types above before asking for // demanded bits. Normally, the only way to reach an instruction with an - // non-int type is via an instruction that has side effects (or otherwise + // unsized type is via an instruction that has side effects (or otherwise // will demand its input bits). However, if we have a readnone function // that returns an unsized type (e.g., void), we must avoid asking for the // demanded bits of the function call's return value. A void-returning @@ -79,7 +78,7 @@ static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) { // If all bits of a user are demanded, then we know that nothing below // that in the def-use chain needs to be changed. auto *K = dyn_cast<Instruction>(KU); - if (K && !Visited.count(K) && K->getType()->isIntOrIntVectorTy() && + if (K && !Visited.count(K) && K->getType()->isSized() && !DB.getDemandedBits(K).isAllOnesValue()) WorkList.push_back(K); } @@ -96,7 +95,7 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) { if (I.mayHaveSideEffects() && I.use_empty()) continue; - if (I.getType()->isIntOrIntVectorTy() && + if (I.getType()->isIntegerTy() && !DB.getDemandedBits(&I).getBoolValue()) { // For live instructions that have all dead bits, first make them dead by // replacing all uses with something else. Then, if they don't need to |