Skip to content

Commit 06f0664

Browse files
committedFeb 15, 2023
[SeparateConstOffsetFromGEP] Fix: b - a matched a - b during reuniteExts
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. Reviewed By: nikic, spatel Differential Revision: https://reviews.llvm.org/D143542
1 parent f93da39 commit 06f0664

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
 

‎llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,8 +1232,8 @@ bool SeparateConstOffsetFromGEP::reuniteExts(Instruction *I) {
12321232
}
12331233
} else if (match(I, m_Sub(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
12341234
if (LHS->getType() == RHS->getType()) {
1235-
const SCEV *Key =
1236-
SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1235+
const SCEV *Key = SE->getAddExpr(
1236+
SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS)));
12371237
if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingSubs)) {
12381238
Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I);
12391239
NewSExt->takeName(I);
@@ -1253,8 +1253,8 @@ bool SeparateConstOffsetFromGEP::reuniteExts(Instruction *I) {
12531253
}
12541254
} else if (match(I, m_NSWSub(m_Value(LHS), m_Value(RHS)))) {
12551255
if (programUndefinedIfPoison(I)) {
1256-
const SCEV *Key =
1257-
SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1256+
const SCEV *Key = SE->getAddExpr(
1257+
SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS)));
12581258
DominatingSubs[Key].push_back(I);
12591259
}
12601260
}

‎llvm/test/Transforms/SeparateConstOffsetFromGEP/split-gep-sub.ll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ define void @test_A_sub_B_add_ConstantInt(ptr %p) {
2828
; CHECK-NEXT: [[CMP26:%.*]] = icmp ult i32 [[SUB1]], 512
2929
; CHECK-NEXT: br i1 [[CMP26]], label [[COND_TRUE:%.*]], label [[COND_END]]
3030
; CHECK: cond.true:
31-
; CHECK-NEXT: [[SUB22:%.*]] = sext i32 [[SUB1]] to i64
32-
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i64
33-
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[SUB22]], 2
34-
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP1]], [[TMP2]]
35-
; CHECK-NEXT: [[TMP4:%.*]] = add i64 [[TMP3]], 2044
36-
; CHECK-NEXT: [[TMP5:%.*]] = inttoptr i64 [[TMP4]] to ptr
37-
; CHECK-NEXT: store float 1.000000e+00, ptr [[TMP5]], align 4
31+
; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[MUL]] to i64
32+
; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[REM]] to i64
33+
; CHECK-NEXT: [[SUB22:%.*]] = sub i64 [[TMP2]], [[TMP1]]
34+
; CHECK-NEXT: [[TMP3:%.*]] = ptrtoint ptr [[P:%.*]] to i64
35+
; CHECK-NEXT: [[TMP4:%.*]] = shl i64 [[SUB22]], 2
36+
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[TMP3]], [[TMP4]]
37+
; CHECK-NEXT: [[TMP6:%.*]] = add i64 [[TMP5]], 2044
38+
; CHECK-NEXT: [[TMP7:%.*]] = inttoptr i64 [[TMP6]] to ptr
39+
; CHECK-NEXT: store float 1.000000e+00, ptr [[TMP7]], align 4
3840
; CHECK-NEXT: br label [[COND_END]]
3941
; CHECK: cond.end:
4042
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[K]], 1

0 commit comments

Comments
 (0)
Please sign in to comment.