mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-14 14:24:05 +00:00
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits (and other associated functions in ValueTracking), adds some (optional) parameters to computeKnownBits and friends. These functions now (optionally) take a "context" instruction pointer, an AssumptionTracker pointer, and also a DomTree pointer, and most of the changes are just to pass this new information when it is easily available from InstSimplify, InstCombine, etc. As explained below, the significant conceptual change is that known properties of a value might depend on the control-flow location of the use (because we care that the @llvm.assume dominates the use because assumptions have control-flow dependencies). This means that, when we ask if bits are known in a value, we might get different answers for different uses. The significant changes are all in ValueTracking. Two main changes: First, as with the rest of the code, new parameters need to be passed around. To make this easier, I grouped them into a structure, and I made internal static versions of the relevant functions that take this structure as a parameter. The new code does as you might expect, it looks for @llvm.assume calls that make use of the value we're trying to learn something about (often indirectly), attempts to pattern match that expression, and uses the result if successful. By making use of the AssumptionTracker, the process of finding @llvm.assume calls is not expensive. Part of the structure being passed around inside ValueTracking is a set of already-considered @llvm.assume calls. This is to prevent a query using, for example, the assume(a == b), to recurse on itself. The context and DT params are used to find applicable assumptions. An assumption needs to dominate the context instruction, or come after it deterministically. In this latter case we only handle the specific case where both the assumption and the context instruction are in the same block, and we need to exclude assumptions from being used to simplify their own ephemeral values (those which contribute only to the assumption) because otherwise the assumption would prove its feeding comparison trivial and would be removed. This commit adds the plumbing and the logic for a simple masked-bit propagation (just enough to write a regression test). Future commits add more patterns (and, correspondingly, more regression tests). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -71,7 +71,7 @@ bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
|
||||
APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
|
||||
|
||||
Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
|
||||
KnownZero, KnownOne, 0);
|
||||
KnownZero, KnownOne, 0, &Inst);
|
||||
if (!V) return false;
|
||||
if (V == &Inst) return true;
|
||||
ReplaceInstUsesWith(Inst, V);
|
||||
@ -85,7 +85,8 @@ bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
|
||||
APInt &KnownZero, APInt &KnownOne,
|
||||
unsigned Depth) {
|
||||
Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
|
||||
KnownZero, KnownOne, Depth);
|
||||
KnownZero, KnownOne, Depth,
|
||||
dyn_cast<Instruction>(U.getUser()));
|
||||
if (!NewVal) return false;
|
||||
U = NewVal;
|
||||
return true;
|
||||
@ -115,7 +116,8 @@ bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
|
||||
/// in the context where the specified bits are demanded, but not for all users.
|
||||
Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
APInt &KnownZero, APInt &KnownOne,
|
||||
unsigned Depth) {
|
||||
unsigned Depth,
|
||||
Instruction *CxtI) {
|
||||
assert(V != nullptr && "Null pointer of Value???");
|
||||
assert(Depth <= 6 && "Limit Search Depth");
|
||||
uint32_t BitWidth = DemandedMask.getBitWidth();
|
||||
@ -158,7 +160,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
|
||||
Instruction *I = dyn_cast<Instruction>(V);
|
||||
if (!I) {
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth);
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
|
||||
return nullptr; // Only analyze instructions.
|
||||
}
|
||||
|
||||
@ -172,8 +174,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
// this instruction has a simpler value in that context.
|
||||
if (I->getOpcode() == Instruction::And) {
|
||||
// If either the LHS or the RHS are Zero, the result is zero.
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
|
||||
// If all of the demanded bits are known 1 on one side, return the other.
|
||||
// These bits cannot contribute to the result of the 'and' in this
|
||||
@ -194,8 +198,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
// only bits from X or Y are demanded.
|
||||
|
||||
// If either the LHS or the RHS are One, the result is One.
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
|
||||
// If all of the demanded bits are known zero on one side, return the
|
||||
// other. These bits cannot contribute to the result of the 'or' in this
|
||||
@ -219,8 +225,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
// We can simplify (X^Y) -> X or Y in the user's context if we know that
|
||||
// only bits from X or Y are demanded.
|
||||
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
|
||||
// If all of the demanded bits are known zero on one side, return the
|
||||
// other.
|
||||
@ -231,7 +239,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
}
|
||||
|
||||
// Compute the KnownZero/KnownOne bits to simplify things downstream.
|
||||
computeKnownBits(I, KnownZero, KnownOne, Depth);
|
||||
computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -244,7 +252,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
|
||||
switch (I->getOpcode()) {
|
||||
default:
|
||||
computeKnownBits(I, KnownZero, KnownOne, Depth);
|
||||
computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
|
||||
break;
|
||||
case Instruction::And:
|
||||
// If either the LHS or the RHS are Zero, the result is zero.
|
||||
@ -595,7 +603,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
|
||||
// Otherwise just hand the sub off to computeKnownBits to fill in
|
||||
// the known zeros and ones.
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth);
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
|
||||
|
||||
// Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
|
||||
// zero.
|
||||
@ -766,7 +774,8 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
// remainder is zero.
|
||||
if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
|
||||
APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
|
||||
computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
|
||||
CxtI);
|
||||
// If it's known zero, our sign bit is also zero.
|
||||
if (LHSKnownZero.isNegative())
|
||||
KnownZero.setBit(KnownZero.getBitWidth() - 1);
|
||||
@ -828,7 +837,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth);
|
||||
computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
|
||||
break;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user