diff options
| author | Craig Topper <craig.topper@gmail.com> | 2017-05-03 23:12:29 +0000 |
|---|---|---|
| committer | Craig Topper <craig.topper@gmail.com> | 2017-05-03 23:12:29 +0000 |
| commit | 8189a87a1ed98a4f3fc3966e52ebad984f18f262 (patch) | |
| tree | 3efca7e7607b0f58d48f1d185d0127bb1774477d | |
| parent | b201a20eabd5292b2b4a2b0948eb26330d5da65b (diff) | |
| download | bcm5719-llvm-8189a87a1ed98a4f3fc3966e52ebad984f18f262.tar.gz bcm5719-llvm-8189a87a1ed98a4f3fc3966e52ebad984f18f262.zip | |
[KnownBits] Add methods for determining if KnownBits is a constant value
This patch adds isConstant and getConstant for determining if KnownBits represents a constant value and to retrieve the value. Use them to simplify code.
Differential Revision: https://reviews.llvm.org/D32785
llvm-svn: 302091
| -rw-r--r-- | llvm/include/llvm/Support/KnownBits.h | 16 | ||||
| -rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 9 | ||||
| -rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 4 | ||||
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 8 |
4 files changed, 26 insertions, 11 deletions
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h index 77e43b5ac9e..0765fe05f07 100644 --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -43,6 +43,22 @@ public: return Zero.getBitWidth(); } + /// Returns true if there is conflicting information. + bool hasConflict() const { return Zero.intersects(One); } + + /// Returns true if we know the value of all bits. + bool isConstant() const { + assert(!hasConflict() && "KnownBits conflict!"); + return Zero.countPopulation() + One.countPopulation() == getBitWidth(); + } + + /// Returns the value when all bits have a known value. This just returns One + /// with a protective assertion. + const APInt &getConstant() const { + assert(isConstant() && "Can only get value when all bits are known"); + return One; + } + /// Returns true if this value is known to be negative. bool isNegative() const { return One.isSignBitSet(); } diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 863fbdba7e6..130e917e49d 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -701,11 +701,10 @@ Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, return Op1; } - APInt KnownZero = Known0.Zero | Known1.Zero; - APInt KnownOne = Known0.One & Known1.One; - if ((KnownZero | KnownOne).isAllOnesValue()) { - return ConstantInt::get(Op0->getType(), KnownOne); - } + Known0.Zero |= Known1.Zero; + Known0.One &= Known1.One; + if (Known0.isConstant()) + return ConstantInt::get(Op0->getType(), Known0.getConstant()); } // If the constant expr is something like &A[123] - &A[4].f, fold this into a diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 7aa6abf8fa4..5e4801d7fb3 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4595,8 +4595,8 @@ Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ, unsigned BitWidth = I->getType()->getScalarSizeInBits(); KnownBits Known(BitWidth); computeKnownBits(I, Known, Q.DL, /*Depth*/ 0, Q.AC, I, Q.DT, ORE); - if ((Known.Zero | Known.One).isAllOnesValue()) - Result = ConstantInt::get(I->getType(), Known.One); + if (Known.isConstant()) + Result = ConstantInt::get(I->getType(), Known.getConstant()); } /// If called on unreachable code, the above logic may report that the diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 1eb98b18bfb..1792cb585f8 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2182,8 +2182,8 @@ Instruction *InstCombiner::visitReturnInst(ReturnInst &RI) { // determine the value. If so, constant fold it. KnownBits Known(VTy->getPrimitiveSizeInBits()); computeKnownBits(ResultOp, Known, 0, &RI); - if ((Known.Zero|Known.One).isAllOnesValue()) - RI.setOperand(0, Constant::getIntegerValue(VTy, Known.One)); + if (Known.isConstant()) + RI.setOperand(0, Constant::getIntegerValue(VTy, Known.getConstant())); return nullptr; } @@ -2863,8 +2863,8 @@ bool InstCombiner::run() { unsigned BitWidth = Ty->getScalarSizeInBits(); KnownBits Known(BitWidth); computeKnownBits(I, Known, /*Depth*/0, I); - if ((Known.Zero | Known.One).isAllOnesValue()) { - Constant *C = ConstantInt::get(Ty, Known.One); + if (Known.isConstant()) { + Constant *C = ConstantInt::get(Ty, Known.getConstant()); DEBUG(dbgs() << "IC: ConstFold (all bits known) to: " << *C << " from: " << *I << '\n'); |

