[BasicAA] Extract linear expression multiplication (NFC)
Extract a common method for multiplying a linear expression by a factor.
This commit is contained in:
parent
a43d1aa852
commit
cdf45f98ca
|
@ -358,6 +358,11 @@ struct LinearExpression {
|
||||||
Scale = APInt(BitWidth, 1);
|
Scale = APInt(BitWidth, 1);
|
||||||
Offset = APInt(BitWidth, 0);
|
Offset = APInt(BitWidth, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LinearExpression mul(const APInt &Other, bool MulIsNSW) const {
|
||||||
|
return LinearExpression(Val, Scale * Other, Offset * Other,
|
||||||
|
IsNSW && (Other.isOne() || MulIsNSW));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,14 +425,11 @@ static LinearExpression GetLinearExpression(
|
||||||
E.IsNSW &= NSW;
|
E.IsNSW &= NSW;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Instruction::Mul: {
|
case Instruction::Mul:
|
||||||
E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
|
E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
|
||||||
Depth + 1, AC, DT);
|
Depth + 1, AC, DT)
|
||||||
E.Offset *= RHS;
|
.mul(RHS, NSW);
|
||||||
E.Scale *= RHS;
|
|
||||||
E.IsNSW &= NSW;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case Instruction::Shl:
|
case Instruction::Shl:
|
||||||
// We're trying to linearize an expression of the kind:
|
// We're trying to linearize an expression of the kind:
|
||||||
// shl i8 -128, 36
|
// shl i8 -128, 36
|
||||||
|
@ -645,8 +647,6 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
|
||||||
|
|
||||||
GepHasConstantOffset = false;
|
GepHasConstantOffset = false;
|
||||||
|
|
||||||
APInt Scale(MaxPointerSize,
|
|
||||||
DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize());
|
|
||||||
// If the integer type is smaller than the pointer size, it is implicitly
|
// If the integer type is smaller than the pointer size, it is implicitly
|
||||||
// sign extended to pointer size.
|
// sign extended to pointer size.
|
||||||
unsigned Width = Index->getType()->getIntegerBitWidth();
|
unsigned Width = Index->getType()->getIntegerBitWidth();
|
||||||
|
@ -655,11 +655,12 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
|
||||||
LinearExpression LE = GetLinearExpression(
|
LinearExpression LE = GetLinearExpression(
|
||||||
CastedValue(Index, 0, SExtBits, TruncBits), DL, 0, AC, DT);
|
CastedValue(Index, 0, SExtBits, TruncBits), DL, 0, AC, DT);
|
||||||
|
|
||||||
// The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
|
// Scale by the type size.
|
||||||
// This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
|
unsigned TypeSize =
|
||||||
LE.IsNSW &= GEPOp->isInBounds() || Scale.isOne();
|
DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize();
|
||||||
Decomposed.Offset += LE.Offset.sextOrTrunc(MaxPointerSize) * Scale;
|
LE = LE.mul(APInt(PointerSize, TypeSize), GEPOp->isInBounds());
|
||||||
Scale *= LE.Scale.sextOrTrunc(MaxPointerSize);
|
Decomposed.Offset += LE.Offset.sextOrSelf(MaxPointerSize);
|
||||||
|
APInt Scale = LE.Scale.sextOrSelf(MaxPointerSize);
|
||||||
|
|
||||||
// If we already had an occurrence of this index variable, merge this
|
// If we already had an occurrence of this index variable, merge this
|
||||||
// scale into it. For example, we want to handle:
|
// scale into it. For example, we want to handle:
|
||||||
|
|
Loading…
Reference in New Issue