[PATCH] D143542: Fix: `b - a` matched `a - b` during reuniteExts
Liren.Peng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 7 17:21:10 PST 2023
Peakulorain created this revision.
Peakulorain added reviewers: ajwock, spatel, lebedev.ri, jdoerfert, reames, nikic, nlopes, regehr.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
Peakulorain requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
During the SeparateConstOffsetFromGEP pass, `a - b` and `b - a` will be
considered equivalent in some instances.
An example- the IR contains:
BB1:
%add = add %a, 511
br label %BB2
BB2:
%sub2 = sub %b, %a
br label %BB3
BB3:
%sub1 = sub %add, %b
%gep = getelementptr float, ptr %p, %sub1
Step 1 in the SeparateConstOffsetFromGEP pass, after split constant index:
BB1:
%add = add %a, 511
br label %BB2
BB2:
%sub2 = sub %b, %a
br label %BB3
BB3:
%sub.t = sub %a, %b
%gep.base = getelementptr float, ptr %p, %sub.t
%gep = getelementptr float, ptr %gep.base, 511
Step 2, after reuniteExts:
BB1:
br label %BB2
BB2:
%sub2 = sub %b, %a
br label %BB3
BB3:
%gep.base = getelementptr float, ptr %p, %sub2
%gep = getelementptr float, ptr %gep.base, 511
Obviously, reuniteExts treated `a - b` and `b - a` as equivalent.
This patch fixes that.
https://reviews.llvm.org/D143542
Files:
llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/split-gep-sub.ll
Index: llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/split-gep-sub.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/split-gep-sub.ll
@@ -0,0 +1,40 @@
+; RUN: llc --mtriple aarch64-unknown-linux -O3 -aarch64-enable-gep-opt \
+; RUN: -start-before=separate-const-offset-from-gep \
+; RUN: -print-after=separate-const-offset-from-gep < %s 2>&1 | FileCheck %s
+
+define void @test(ptr %p) {
+entry:
+ %0 = tail call i32 @foo()
+ %rem = srem i32 %0, 5
+ %add = add nsw i32 %rem , 511
+ br label %for.body
+
+for.body:
+ %k = phi i32 [ 0, %entry ], [ %inc, %cond.end ]
+ %mul = mul nuw nsw i32 %k, 5
+ %sub1 = sub nsw i32 %mul, %rem
+ %cmp26 = icmp ult i32 %sub1, 512
+ br i1 %cmp26, label %cond.true, label %cond.end
+
+cond.true:
+; CHECK-LABEL: cond.true:
+; CHECK-NEXT: %1 = sext i32 %mul to i64
+; CHECK-NEXT: %2 = sext i32 %rem to i64
+; CHECK-NEXT: %sub22 = sub i64 %2, %1
+; CHECK-NOT: %sub22 = sext i32 %sub1 to i64
+ %sub2 = sub nsw i32 %add, %mul
+ %idxprom = sext i32 %sub2 to i64
+ %arryidx = getelementptr inbounds float, ptr %p, i64 %idxprom
+ store float 1.0, ptr %arryidx, align 4
+ br label %cond.end
+
+cond.end:
+ %inc = add nuw nsw i32 %k, 1
+ %exitcond = icmp ne i32 %inc, 100
+ br i1 %exitcond, label %for.body, label %for.end
+
+for.end:
+ ret void
+}
+
+declare i32 @foo()
\ No newline at end of file
Index: llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1232,8 +1232,8 @@
}
} else if (match(I, m_Sub(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
if (LHS->getType() == RHS->getType()) {
- const SCEV *Key =
- SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
+ const SCEV *Key = SE->getAddExpr(
+ SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS)));
if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingSubs)) {
Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I);
NewSExt->takeName(I);
@@ -1253,8 +1253,8 @@
}
} else if (match(I, m_NSWSub(m_Value(LHS), m_Value(RHS)))) {
if (programUndefinedIfPoison(I)) {
- const SCEV *Key =
- SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
+ const SCEV *Key = SE->getAddExpr(
+ SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS)));
DominatingSubs[Key].push_back(I);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143542.495691.patch
Type: text/x-patch
Size: 2653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230208/f0303c1c/attachment.bin>
More information about the llvm-commits
mailing list