Skip to content

Commit

Permalink
[KnownBits] Add methods for determining if KnownBits is a constant value
Browse files Browse the repository at this point in the history
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302091 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
topperc committed May 3, 2017
1 parent f990516 commit 64c8cdf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
16 changes: 16 additions & 0 deletions include/llvm/Support/KnownBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ struct KnownBits {
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(); }

Expand Down
9 changes: 4 additions & 5 deletions lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit 64c8cdf

Please sign in to comment.