1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 20:23:11 +01:00

[ValueTracking] reduce code duplication; NFC

The start value can't be null for something to be a non-zero
recurrence, so hoist that common check out of the switch.

Subsequent checks may be incomplete or over-specified as noted in:
D100408
This commit is contained in:
Sanjay Patel 2021-04-14 08:30:42 -04:00
parent 9c1bc49f11
commit b568083407

View File

@ -2213,33 +2213,31 @@ static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value)
return true; return true;
} }
/// Try to detect a recurrence that monotonically increases from a non-zero
/// starting value. These are common as induction variables.
static bool isNonZeroRecurrence(const PHINode *PN) { static bool isNonZeroRecurrence(const PHINode *PN) {
// Try and detect a recurrence that monotonically increases from a
// starting value, as these are common as induction variables.
BinaryOperator *BO = nullptr; BinaryOperator *BO = nullptr;
Value *Start = nullptr, *Step = nullptr; Value *Start = nullptr, *Step = nullptr;
const APInt *StartC, *StepC; const APInt *StartC, *StepC;
if (!matchSimpleRecurrence(PN, BO, Start, Step) || if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
!match(Start, m_APInt(StartC))) !match(Start, m_APInt(StartC)) || StartC->isNullValue())
return false; return false;
switch (BO->getOpcode()) { switch (BO->getOpcode()) {
case Instruction::Add: case Instruction::Add:
return match(Step, m_APInt(StepC)) && return match(Step, m_APInt(StepC)) &&
((BO->hasNoUnsignedWrap() && !StartC->isNullValue() && ((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) ||
!StepC->isNullValue()) ||
(BO->hasNoSignedWrap() && StartC->isStrictlyPositive() && (BO->hasNoSignedWrap() && StartC->isStrictlyPositive() &&
StepC->isNonNegative())); StepC->isNonNegative()));
case Instruction::Mul: case Instruction::Mul:
return !StartC->isNullValue() && match(Step, m_APInt(StepC)) && return match(Step, m_APInt(StepC)) &&
((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) || ((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) ||
(BO->hasNoSignedWrap() && StepC->isStrictlyPositive())); (BO->hasNoSignedWrap() && StepC->isStrictlyPositive()));
case Instruction::Shl: case Instruction::Shl:
return !StartC->isNullValue() && return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
(BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap());
case Instruction::AShr: case Instruction::AShr:
case Instruction::LShr: case Instruction::LShr:
return !StartC->isNullValue() && BO->isExact(); return BO->isExact();
default: default:
return false; return false;
} }