Skip to content

Commit 5d2670c

Browse files
committed
ScalarEvolution: Construct SCEVDivision's Derived type instead of itself
SCEVDivision::divide constructed an object of SCEVDivision<Derived> instead of Derived. divide would call visit which would cast the SCEVDivision<Derived> to type Derived. As it happens, SCEVDivision<Derived> and Derived currently have the same layout but this is fragile and grounds for UB. Instead, just construct Derived. No functional change intended. llvm-svn: 222126
1 parent 970b0d5 commit 5d2670c

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ struct SCEVDivision : public SCEVVisitor<Derived, void> {
767767
const SCEV **Remainder) {
768768
assert(Numerator && Denominator && "Uninitialized SCEV");
769769

770-
SCEVDivision<Derived> D(SE, Numerator, Denominator);
770+
Derived D(SE, Numerator, Denominator);
771771

772772
// Check for the trivial case here to avoid having to check for it in the
773773
// rest of the code.
@@ -808,17 +808,6 @@ struct SCEVDivision : public SCEVVisitor<Derived, void> {
808808
*Remainder = D.Remainder;
809809
}
810810

811-
SCEVDivision(ScalarEvolution &S, const SCEV *Numerator, const SCEV *Denominator)
812-
: SE(S), Denominator(Denominator) {
813-
Zero = SE.getConstant(Denominator->getType(), 0);
814-
One = SE.getConstant(Denominator->getType(), 1);
815-
816-
// By default, we don't know how to divide Expr by Denominator.
817-
// Providing the default here simplifies the rest of the code.
818-
Quotient = Zero;
819-
Remainder = Numerator;
820-
}
821-
822811
// Except in the trivial case described above, we do not know how to divide
823812
// Expr by Denominator for the following functions with empty implementation.
824813
void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
@@ -953,6 +942,18 @@ struct SCEVDivision : public SCEVVisitor<Derived, void> {
953942
}
954943

955944
private:
945+
SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
946+
const SCEV *Denominator)
947+
: SE(S), Denominator(Denominator) {
948+
Zero = SE.getConstant(Denominator->getType(), 0);
949+
One = SE.getConstant(Denominator->getType(), 1);
950+
951+
// By default, we don't know how to divide Expr by Denominator.
952+
// Providing the default here simplifies the rest of the code.
953+
Quotient = Zero;
954+
Remainder = Numerator;
955+
}
956+
956957
ScalarEvolution &SE;
957958
const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One;
958959

@@ -961,6 +962,10 @@ struct SCEVDivision : public SCEVVisitor<Derived, void> {
961962
};
962963

963964
struct SCEVSDivision : public SCEVDivision<SCEVSDivision> {
965+
SCEVSDivision(ScalarEvolution &S, const SCEV *Numerator,
966+
const SCEV *Denominator)
967+
: SCEVDivision(S, Numerator, Denominator) {}
968+
964969
void visitConstant(const SCEVConstant *Numerator) {
965970
if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
966971
Quotient = SE.getConstant(sdiv(Numerator, D));
@@ -971,6 +976,10 @@ struct SCEVSDivision : public SCEVDivision<SCEVSDivision> {
971976
};
972977

973978
struct SCEVUDivision : public SCEVDivision<SCEVUDivision> {
979+
SCEVUDivision(ScalarEvolution &S, const SCEV *Numerator,
980+
const SCEV *Denominator)
981+
: SCEVDivision(S, Numerator, Denominator) {}
982+
974983
void visitConstant(const SCEVConstant *Numerator) {
975984
if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
976985
Quotient = SE.getConstant(udiv(Numerator, D));

0 commit comments

Comments
 (0)