Skip to content

[SCEV] Handle more adds in computeConstantDifference() #101339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11923,8 +11923,9 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
// fairly deep in the call stack (i.e. is called many times).

// X - X = 0.
unsigned BW = getTypeSizeInBits(More->getType());
if (More == Less)
return APInt(getTypeSizeInBits(More->getType()), 0);
return APInt(BW, 0);

if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
const auto *LAR = cast<SCEVAddRecExpr>(Less);
Expand All @@ -11947,33 +11948,31 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
// fall through
}

if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) {
const auto &M = cast<SCEVConstant>(More)->getAPInt();
const auto &L = cast<SCEVConstant>(Less)->getAPInt();
return M - L;
}

SCEV::NoWrapFlags Flags;
const SCEV *LLess = nullptr, *RLess = nullptr;
const SCEV *LMore = nullptr, *RMore = nullptr;
const SCEVConstant *C1 = nullptr, *C2 = nullptr;
// Compare (X + C1) vs X.
if (splitBinaryAdd(Less, LLess, RLess, Flags))
if ((C1 = dyn_cast<SCEVConstant>(LLess)))
if (RLess == More)
return -(C1->getAPInt());

// Compare X vs (X + C2).
if (splitBinaryAdd(More, LMore, RMore, Flags))
if ((C2 = dyn_cast<SCEVConstant>(LMore)))
if (RMore == Less)
return C2->getAPInt();
// Try to cancel out common factors in two add expressions.
SmallDenseMap<const SCEV *, int, 8> Multiplicity;
APInt Diff(BW, 0);
auto Add = [&](const SCEV *S, int Mul) {
if (auto *C = dyn_cast<SCEVConstant>(S))
Diff += C->getAPInt() * Mul;
else
Multiplicity[S] += Mul;
};
auto Decompose = [&](const SCEV *S, int Mul) {
if (isa<SCEVAddExpr>(S)) {
for (const SCEV *Op : S->operands())
Add(Op, Mul);
} else
Add(S, Mul);
};
Decompose(More, 1);
Decompose(Less, -1);

// Compare (X + C1) vs (X + C2).
if (C1 && C2 && RLess == RMore)
return C2->getAPInt() - C1->getAPInt();
// Check whether all the non-constants cancel out.
for (const auto [_, Mul] : Multiplicity)
if (Mul != 0)
return std::nullopt;

return std::nullopt;
return Diff;
}

bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart(
Expand Down
8 changes: 7 additions & 1 deletion llvm/unittests/Analysis/ScalarEvolutionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,10 +1117,12 @@ TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
LLVMContext C;
SMDiagnostic Err;
std::unique_ptr<Module> M = parseAssemblyString(
"define void @foo(i32 %sz, i32 %pp) { "
"define void @foo(i32 %sz, i32 %pp, i32 %x) { "
"entry: "
" %v0 = add i32 %pp, 0 "
" %v3 = add i32 %pp, 3 "
" %vx = add i32 %pp, %x "
" %vx3 = add i32 %vx, 3 "
" br label %loop.body "
"loop.body: "
" %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
Expand All @@ -1141,6 +1143,9 @@ TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
auto *ScevV0 = SE.getSCEV(getInstructionByName(F, "v0")); // %pp
auto *ScevV3 = SE.getSCEV(getInstructionByName(F, "v3")); // (3 + %pp)
auto *ScevVX = SE.getSCEV(getInstructionByName(F, "vx")); // (%pp + %x)
// (%pp + %x + 3)
auto *ScevVX3 = SE.getSCEV(getInstructionByName(F, "vx3"));
auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
auto *ScevXA = SE.getSCEV(getInstructionByName(F, "xa")); // {%pp,+,1}
auto *ScevYY = SE.getSCEV(getInstructionByName(F, "yy")); // {(3 + %pp),+,1}
Expand All @@ -1162,6 +1167,7 @@ TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
EXPECT_EQ(diff(ScevV0, ScevV3), -3);
EXPECT_EQ(diff(ScevV0, ScevV0), 0);
EXPECT_EQ(diff(ScevV3, ScevV3), 0);
EXPECT_EQ(diff(ScevVX3, ScevVX), 3);
EXPECT_EQ(diff(ScevIV, ScevIV), 0);
EXPECT_EQ(diff(ScevXA, ScevXB), 0);
EXPECT_EQ(diff(ScevXA, ScevYY), -3);
Expand Down
Loading