[llvm] a95b4d1 - [SCEV] Use SCEVUse for FoldID. (#219813)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 13:41:56 PDT 2026
Author: Florian Hahn
Date: 2026-08-30T21:41:51+01:00
New Revision: a95b4d18e69744778c065b0a3927b3fc5c95a07f
URL: https://github.com/llvm/llvm-project/commit/a95b4d18e69744778c065b0a3927b3fc5c95a07f
DIFF: https://github.com/llvm/llvm-project/commit/a95b4d18e69744778c065b0a3927b3fc5c95a07f.diff
LOG: [SCEV] Use SCEVUse for FoldID. (#219813)
As pointed out in https://github.com/llvm/llvm-project/pull/216761,
FoldID was not properly migrated to use SCEVUse. This caused incorrect
lookups, when an expression was cached with an operand with SCEVUse
flags and queried for the same expression without flags.
PR: https://github.com/llvm/llvm-project/pull/219813
Added:
Modified:
llvm/include/llvm/Analysis/ScalarEvolution.h
llvm/unittests/Analysis/ScalarEvolutionTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 95e545477fa77..385a8e3f0d3bb 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1625,13 +1625,13 @@ class ScalarEvolution {
SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
class FoldID {
- const SCEV *Op = nullptr;
+ SCEVUse Op;
const Type *Ty = nullptr;
unsigned short C;
public:
- FoldID(SCEVTypes C, const SCEV *Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) {
- assert(Op);
+ FoldID(SCEVTypes C, SCEVUse Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) {
+ assert(Op.getPointer());
assert(Ty);
}
@@ -1639,8 +1639,9 @@ class ScalarEvolution {
unsigned computeHash() const {
return detail::combineHashValue(
- C, detail::combineHashValue(reinterpret_cast<uintptr_t>(Op),
- reinterpret_cast<uintptr_t>(Ty)));
+ C, detail::combineHashValue(
+ reinterpret_cast<uintptr_t>(Op.getOpaqueValue()),
+ reinterpret_cast<uintptr_t>(Ty)));
}
bool operator==(const FoldID &RHS) const {
diff --git a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
index 41d792bd171b3..a50566cb64093 100644
--- a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
+++ b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
@@ -2330,4 +2330,48 @@ TEST_F(ScalarEvolutionsTest, CastsOfUsesWithNoWrapFlags) {
CheckCast(SE.getAnyExtendExpr(NUWAdd, I64), SE.getAnyExtendExpr(Add, I64));
});
}
+
+TEST_F(ScalarEvolutionsTest, ExtendFoldCacheKeysUseFlags) {
+ LLVMContext C;
+ SMDiagnostic Err;
+ std::unique_ptr<Module> M = parseAssemblyString(
+ R"(define void @f(i32 %x) {
+ entry:
+ ret void
+ })",
+ Err, C);
+
+ if (!M) {
+ Err.print("ScalarEvolutionTest", errs());
+ ASSERT_TRUE(M && "Could not parse module?");
+ }
+ ASSERT_TRUE(!verifyModule(*M, &errs()) && "Must have been well formed!");
+
+ runWithSE(*M, "f", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
+ Type *I32 = Type::getInt32Ty(F.getContext());
+ Type *I64 = Type::getInt64Ty(F.getContext());
+
+ // Build (-2 * smax(smin(%x, 0), -50)), which is known to be in [0, 100],
+ // but cannot be proven to not wrap in an unsigned sense.
+ const SCEV *X = SE.getSCEV(getArgByName(F, "x"));
+ const SCEV *Bounded = SE.getSMaxExpr(SE.getSMinExpr(X, SE.getZero(I32)),
+ SE.getConstant(I32, -50, true));
+ SCEVUse Mul = SE.getMulExpr(SE.getConstant(I32, -2, true), Bounded);
+ ASSERT_TRUE(isa<SCEVMulExpr>(Mul));
+ ASSERT_FALSE(cast<SCEVMulExpr>(Mul.getPointer())->hasNoUnsignedWrap());
+ ASSERT_TRUE(SE.isKnownNonNegative(Mul));
+
+ SCEVUse MulNUW(Mul, SCEV::FlagNUW);
+ ASSERT_TRUE(MulNUW.hasUseFlags());
+
+ // For a known non-negative operand, getSignExtendExpr folds to a zero
+ // extend of the same operand use, and the fold gets cached. Make sure flags
+ // of SCEVUse operands are handled correctly.
+ const SCEV *SExtNUW = SE.getSignExtendExpr(MulNUW, I64);
+ const SCEV *SExtPlain = SE.getSignExtendExpr(Mul, I64);
+ EXPECT_NE(SExtNUW, SExtPlain);
+ EXPECT_EQ(cast<SCEVZeroExtendExpr>(SExtNUW)->getOperand(), MulNUW);
+ EXPECT_EQ(cast<SCEVZeroExtendExpr>(SExtPlain)->getOperand(), Mul);
+ });
+}
} // end namespace llvm
More information about the llvm-commits
mailing list