mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-05-15 16:38:41 +00:00
SCEV: Rewrite TrandformForPostIncUse to handle expression DAGs, not
just expression trees. Partially fixes PR11090. Test case will be with the full fix. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141868 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
ce1823cd1b
commit
94f01db27b
@ -60,20 +60,40 @@ static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
namespace {
|
||||||
const SCEV *S,
|
|
||||||
Instruction *User,
|
/// Hold the state used during post-inc expression transformation, including a
|
||||||
Value *OperandValToReplace,
|
/// map of transformed expressions.
|
||||||
PostIncLoopSet &Loops,
|
class PostIncTransform {
|
||||||
ScalarEvolution &SE,
|
TransformKind Kind;
|
||||||
DominatorTree &DT) {
|
PostIncLoopSet &Loops;
|
||||||
if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S))
|
ScalarEvolution &SE;
|
||||||
return S;
|
DominatorTree &DT;
|
||||||
|
|
||||||
|
DenseMap<const SCEV*, const SCEV*> Transformed;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PostIncTransform(TransformKind kind, PostIncLoopSet &loops,
|
||||||
|
ScalarEvolution &se, DominatorTree &dt):
|
||||||
|
Kind(kind), Loops(loops), SE(se), DT(dt) {}
|
||||||
|
|
||||||
|
const SCEV *TransformSubExpr(const SCEV *S, Instruction *User,
|
||||||
|
Value *OperandValToReplace);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
const SCEV *TransformImpl(const SCEV *S, Instruction *User,
|
||||||
|
Value *OperandValToReplace);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
/// Implement post-inc transformation for all valid expression types.
|
||||||
|
const SCEV *PostIncTransform::
|
||||||
|
TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
|
||||||
|
|
||||||
if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) {
|
if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) {
|
||||||
const SCEV *O = X->getOperand();
|
const SCEV *O = X->getOperand();
|
||||||
const SCEV *N = TransformForPostIncUse(Kind, O, User, OperandValToReplace,
|
const SCEV *N = TransformSubExpr(O, User, OperandValToReplace);
|
||||||
Loops, SE, DT);
|
|
||||||
if (O != N)
|
if (O != N)
|
||||||
switch (S->getSCEVType()) {
|
switch (S->getSCEVType()) {
|
||||||
case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType());
|
case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType());
|
||||||
@ -93,9 +113,7 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
// Transform each operand.
|
// Transform each operand.
|
||||||
for (SCEVNAryExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
|
for (SCEVNAryExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
const SCEV *O = *I;
|
Operands.push_back(TransformSubExpr(*I, LUser, 0));
|
||||||
const SCEV *N = TransformForPostIncUse(Kind, O, LUser, 0, Loops, SE, DT);
|
|
||||||
Operands.push_back(N);
|
|
||||||
}
|
}
|
||||||
// Conservatively use AnyWrap until/unless we need FlagNW.
|
// Conservatively use AnyWrap until/unless we need FlagNW.
|
||||||
const SCEV *Result = SE.getAddRecExpr(Operands, L, SCEV::FlagAnyWrap);
|
const SCEV *Result = SE.getAddRecExpr(Operands, L, SCEV::FlagAnyWrap);
|
||||||
@ -104,8 +122,8 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
case NormalizeAutodetect:
|
case NormalizeAutodetect:
|
||||||
if (IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) {
|
if (IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) {
|
||||||
const SCEV *TransformedStep =
|
const SCEV *TransformedStep =
|
||||||
TransformForPostIncUse(Kind, AR->getStepRecurrence(SE),
|
TransformSubExpr(AR->getStepRecurrence(SE),
|
||||||
User, OperandValToReplace, Loops, SE, DT);
|
User, OperandValToReplace);
|
||||||
Result = SE.getMinusSCEV(Result, TransformedStep);
|
Result = SE.getMinusSCEV(Result, TransformedStep);
|
||||||
Loops.insert(L);
|
Loops.insert(L);
|
||||||
}
|
}
|
||||||
@ -114,24 +132,20 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
// sometimes fails to canonicalize two equal SCEVs to exactly the same
|
// sometimes fails to canonicalize two equal SCEVs to exactly the same
|
||||||
// form. It's possibly a pessimization when this happens, but it isn't a
|
// form. It's possibly a pessimization when this happens, but it isn't a
|
||||||
// correctness problem, so disable this assert for now.
|
// correctness problem, so disable this assert for now.
|
||||||
assert(S == TransformForPostIncUse(Denormalize, Result,
|
assert(S == TransformSubExpr(Result, User, OperandValToReplace) &&
|
||||||
User, OperandValToReplace,
|
|
||||||
Loops, SE, DT) &&
|
|
||||||
"SCEV normalization is not invertible!");
|
"SCEV normalization is not invertible!");
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case Normalize:
|
case Normalize:
|
||||||
if (Loops.count(L)) {
|
if (Loops.count(L)) {
|
||||||
const SCEV *TransformedStep =
|
const SCEV *TransformedStep =
|
||||||
TransformForPostIncUse(Kind, AR->getStepRecurrence(SE),
|
TransformSubExpr(AR->getStepRecurrence(SE),
|
||||||
User, OperandValToReplace, Loops, SE, DT);
|
User, OperandValToReplace);
|
||||||
Result = SE.getMinusSCEV(Result, TransformedStep);
|
Result = SE.getMinusSCEV(Result, TransformedStep);
|
||||||
}
|
}
|
||||||
#if 0
|
#if 0
|
||||||
// See the comment on the assert above.
|
// See the comment on the assert above.
|
||||||
assert(S == TransformForPostIncUse(Denormalize, Result,
|
assert(S == TransformSubExpr(Result, User, OperandValToReplace) &&
|
||||||
User, OperandValToReplace,
|
|
||||||
Loops, SE, DT) &&
|
|
||||||
"SCEV normalization is not invertible!");
|
"SCEV normalization is not invertible!");
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
@ -150,8 +164,7 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end();
|
for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
const SCEV *O = *I;
|
const SCEV *O = *I;
|
||||||
const SCEV *N = TransformForPostIncUse(Kind, O, User, OperandValToReplace,
|
const SCEV *N = TransformSubExpr(O, User, OperandValToReplace);
|
||||||
Loops, SE, DT);
|
|
||||||
Changed |= N != O;
|
Changed |= N != O;
|
||||||
Operands.push_back(N);
|
Operands.push_back(N);
|
||||||
}
|
}
|
||||||
@ -170,10 +183,8 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) {
|
if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) {
|
||||||
const SCEV *LO = X->getLHS();
|
const SCEV *LO = X->getLHS();
|
||||||
const SCEV *RO = X->getRHS();
|
const SCEV *RO = X->getRHS();
|
||||||
const SCEV *LN = TransformForPostIncUse(Kind, LO, User, OperandValToReplace,
|
const SCEV *LN = TransformSubExpr(LO, User, OperandValToReplace);
|
||||||
Loops, SE, DT);
|
const SCEV *RN = TransformSubExpr(RO, User, OperandValToReplace);
|
||||||
const SCEV *RN = TransformForPostIncUse(Kind, RO, User, OperandValToReplace,
|
|
||||||
Loops, SE, DT);
|
|
||||||
if (LO != LN || RO != RN)
|
if (LO != LN || RO != RN)
|
||||||
return SE.getUDivExpr(LN, RN);
|
return SE.getUDivExpr(LN, RN);
|
||||||
return S;
|
return S;
|
||||||
@ -182,3 +193,32 @@ const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
|||||||
llvm_unreachable("Unexpected SCEV kind!");
|
llvm_unreachable("Unexpected SCEV kind!");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Manage recursive transformation across an expression DAG. Revisiting
|
||||||
|
/// expressions would lead to exponential recursion.
|
||||||
|
const SCEV *PostIncTransform::
|
||||||
|
TransformSubExpr(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
|
||||||
|
|
||||||
|
if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S))
|
||||||
|
return S;
|
||||||
|
|
||||||
|
const SCEV *&ExprRef = Transformed[S];
|
||||||
|
if (ExprRef)
|
||||||
|
return ExprRef;
|
||||||
|
|
||||||
|
ExprRef = TransformImpl(S, User, OperandValToReplace);
|
||||||
|
return ExprRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Top level driver for transforming an expression DAG into its requested
|
||||||
|
/// post-inc form (either "Normalized" or "Denormalized".
|
||||||
|
const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
|
||||||
|
const SCEV *S,
|
||||||
|
Instruction *User,
|
||||||
|
Value *OperandValToReplace,
|
||||||
|
PostIncLoopSet &Loops,
|
||||||
|
ScalarEvolution &SE,
|
||||||
|
DominatorTree &DT) {
|
||||||
|
PostIncTransform Transform(Kind, Loops, SE, DT);
|
||||||
|
return Transform.TransformSubExpr(S, User, OperandValToReplace);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user