aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/LoopUtils.cpp
diff options
context:
space:
mode:
authorFlorian Hahn <flo@fhahn.com>2023-11-23 11:35:21 +0000
committerFlorian Hahn <flo@fhahn.com>2023-11-23 11:35:21 +0000
commit19e6d541889f24e21c7a9a6e021aeb82efd4dcb2 (patch)
treea8658fb0d430a5df4f238c94c9dfaad8f5b0cc32 /llvm/lib/Transforms/Utils/LoopUtils.cpp
parent0d9f557b6c36da3aa92daff4c0d37ea821d7ae1e (diff)
downloadllvm-19e6d541889f24e21c7a9a6e021aeb82efd4dcb2.zip
llvm-19e6d541889f24e21c7a9a6e021aeb82efd4dcb2.tar.gz
llvm-19e6d541889f24e21c7a9a6e021aeb82efd4dcb2.tar.bz2
[LV] Re-use existing compare if possible for diff checks.
SCEV simplifying the subtraction may result in redundant compares that are all OR'd together. Keep track of the generated operands in SeenCompares, with the key being the pair of operands for the compare. If we alrady generated the same compare previously, skip it.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/LoopUtils.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 85e28ea..acce8d1 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -1787,17 +1787,28 @@ Value *llvm::addDiffRuntimeChecks(
// Our instructions might fold to a constant.
Value *MemoryRuntimeCheck = nullptr;
+ auto &SE = *Expander.getSE();
+ // Map to keep track of created compares, The key is the pair of operands for
+ // the compare, to allow detecting and re-using redundant compares.
+ DenseMap<std::pair<Value *, Value *>, Value *> SeenCompares;
for (const auto &C : Checks) {
Type *Ty = C.SinkStart->getType();
// Compute VF * IC * AccessSize.
auto *VFTimesUFTimesSize =
ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
ConstantInt::get(Ty, IC * C.AccessSize));
- auto &SE = *Expander.getSE();
Value *Diff = Expander.expandCodeFor(
SE.getMinusSCEV(C.SinkStart, C.SrcStart), Ty, Loc);
- Value *IsConflict =
+
+ // Check if the same compare has already been created earlier. In that case,
+ // there is no need to check it again.
+ Value *IsConflict = SeenCompares.lookup({Diff, VFTimesUFTimesSize});
+ if (IsConflict)
+ continue;
+
+ IsConflict =
ChkBuilder.CreateICmpULT(Diff, VFTimesUFTimesSize, "diff.check");
+ SeenCompares.insert({{Diff, VFTimesUFTimesSize}, IsConflict});
if (C.NeedsFreeze)
IsConflict =
ChkBuilder.CreateFreeze(IsConflict, IsConflict->getName() + ".fr");