Skip to content

Commit 71b83ed

Browse files
committed
[CGP][CodeGenPrepare] Folding urem with loop invariant value plus offset
This extends the existing fold: ``` for(i = Start; i < End; ++i) Rem = (i nuw+- IncrLoopInvariant) u% RemAmtLoopInvariant; ``` -> ``` Rem = (Start nuw+- IncrLoopInvariant) % RemAmtLoopInvariant; for(i = Start; i < End; ++i, ++rem) Rem = rem == RemAmtLoopInvariant ? 0 : Rem; ``` To work with a non-zero `IncrLoopInvariant`. This is a common usage in cases such as: ``` for(i = 0; i < N; ++i) if ((i + 1) % X) == 0) do_something_occasionally_but_not_first_iter(); ``` Alive2 w/ i4/unrolled 6x (needs to be ran locally due to timeout): https://alive2.llvm.org/ce/z/6tgyN3 Exhaust proof over all uint8_t combinations in C++: https://godbolt.org/z/WYa561388
1 parent 6b11573 commit 71b83ed

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,14 +1979,36 @@ static bool foldFCmpToFPClassTest(CmpInst *Cmp, const TargetLowering &TLI,
19791979
static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
19801980
const LoopInfo *LI,
19811981
Value *&RemAmtOut,
1982+
Value *&AddInstOut,
1983+
Value *&AddOffsetOut,
19821984
PHINode *&LoopIncrPNOut) {
19831985
Value *Incr, *RemAmt;
19841986
// NB: If RemAmt is a power of 2 it *should* have been transformed by now.
19851987
if (!match(Rem, m_URem(m_Value(Incr), m_Value(RemAmt))))
19861988
return false;
19871989

1990+
Value *AddInst, *AddOffset;
19881991
// Find out loop increment PHI.
19891992
auto *PN = dyn_cast<PHINode>(Incr);
1993+
if (PN != nullptr) {
1994+
AddInst = nullptr;
1995+
AddOffset = nullptr;
1996+
} else {
1997+
// Search through a NUW add on top of the loop increment.
1998+
Value *V0, *V1;
1999+
if (!match(Incr, m_NUWAdd(m_Value(V0), m_Value(V1))))
2000+
return false;
2001+
2002+
AddInst = Incr;
2003+
PN = dyn_cast<PHINode>(V0);
2004+
if (PN != nullptr) {
2005+
AddOffset = V1;
2006+
} else {
2007+
PN = dyn_cast<PHINode>(V1);
2008+
AddOffset = V0;
2009+
}
2010+
}
2011+
19902012
if (!PN)
19912013
return false;
19922014

@@ -2026,6 +2048,8 @@ static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
20262048
// Set output variables.
20272049
RemAmtOut = RemAmt;
20282050
LoopIncrPNOut = PN;
2051+
AddInstOut = AddInst;
2052+
AddOffsetOut = AddOffset;
20292053

20302054
return true;
20312055
}
@@ -2040,15 +2064,14 @@ static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
20402064
// Rem = (Start nuw+ IncrLoopInvariant) % RemAmtLoopInvariant;
20412065
// for(i = Start; i < End; ++i, ++rem)
20422066
// Rem = rem == RemAmtLoopInvariant ? 0 : Rem;
2043-
//
2044-
// Currently only implemented for `IncrLoopInvariant` being zero.
20452067
static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20462068
const LoopInfo *LI,
20472069
SmallSet<BasicBlock *, 32> &FreshBBs,
20482070
bool IsHuge) {
2049-
Value *RemAmt;
2071+
Value *AddOffset, *RemAmt, *AddInst;
20502072
PHINode *LoopIncrPN;
2051-
if (!isRemOfLoopIncrementWithLoopInvariant(Rem, LI, RemAmt, LoopIncrPN))
2073+
if (!isRemOfLoopIncrementWithLoopInvariant(Rem, LI, RemAmt, AddInst,
2074+
AddOffset, LoopIncrPN))
20522075
return false;
20532076

20542077
// Only non-constant remainder as the extra IV is probably not profitable
@@ -2066,6 +2089,23 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20662089

20672090
Loop *L = LI->getLoopFor(LoopIncrPN->getParent());
20682091
Value *Start = LoopIncrPN->getIncomingValueForBlock(L->getLoopPreheader());
2092+
// If we have add create initial value for remainder.
2093+
// The logic here is:
2094+
// (urem (add nuw Start, IncrLoopInvariant), RemAmtLoopInvariant
2095+
//
2096+
// Only proceed if the expression simplifies (otherwise we can't fully
2097+
// optimize out the urem).
2098+
if (AddInst) {
2099+
assert(AddOffset && "We found an add but missing values");
2100+
// Without dom-condition/assumption cache we aren't likely to get much out
2101+
// of a context instruction.
2102+
Start = simplifyAddInst(Start, AddOffset,
2103+
match(AddInst, m_NSWAdd(m_Value(), m_Value())),
2104+
/*IsNUW=*/true, *DL);
2105+
if (!Start)
2106+
return false;
2107+
}
2108+
20692109
// If we can't fully optimize out the `rem`, skip this transform.
20702110
Start = simplifyURemInst(Start, RemAmt, *DL);
20712111
if (!Start)
@@ -2093,9 +2133,12 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20932133
FreshBBs.insert(LoopIncrPN->getParent());
20942134
FreshBBs.insert(L->getLoopLatch());
20952135
FreshBBs.insert(Rem->getParent());
2096-
2136+
if (AddInst)
2137+
FreshBBs.insert(cast<Instruction>(AddInst)->getParent());
20972138
replaceAllUsesWith(Rem, NewRem, FreshBBs, IsHuge);
20982139
Rem->eraseFromParent();
2140+
if (AddInst && AddInst->use_empty())
2141+
cast<Instruction>(AddInst)->eraseFromParent();
20992142
return true;
21002143
}
21012144

llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,20 @@ for.body.tail:
319319
define void @simple_urem_to_sel_vec(<2 x i64> %rem_amt) nounwind {
320320
; CHECK-LABEL: define void @simple_urem_to_sel_vec(
321321
; CHECK-SAME: <2 x i64> [[REM_AMT:%.*]]) #[[ATTR0]] {
322-
; CHECK-NEXT: [[FOR_COND_CLEANUP:.*]]:
322+
; CHECK-NEXT: [[ENTRY:.*]]:
323323
; CHECK-NEXT: br label %[[FOR_BODY:.*]]
324-
; CHECK: [[ENTRY:.*]]:
324+
; CHECK: [[FOR_COND_CLEANUP:.*]]:
325325
; CHECK-NEXT: ret void
326326
; CHECK: [[FOR_BODY]]:
327-
; CHECK-NEXT: [[REM:%.*]] = phi <2 x i64> [ zeroinitializer, %[[FOR_COND_CLEANUP]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
328-
; CHECK-NEXT: [[I_04:%.*]] = phi <2 x i64> [ [[INC:%.*]], %[[FOR_BODY]] ], [ zeroinitializer, %[[FOR_COND_CLEANUP]] ]
327+
; CHECK-NEXT: [[REM:%.*]] = phi <2 x i64> [ zeroinitializer, %[[ENTRY]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
328+
; CHECK-NEXT: [[I_04:%.*]] = phi <2 x i64> [ [[INC:%.*]], %[[FOR_BODY]] ], [ zeroinitializer, %[[ENTRY]] ]
329329
; CHECK-NEXT: tail call void @use.2xi64(<2 x i64> [[REM]])
330330
; CHECK-NEXT: [[TMP1:%.*]] = add nuw <2 x i64> [[REM]], <i64 1, i64 1>
331331
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], [[REM_AMT]]
332332
; CHECK-NEXT: [[TMP3]] = select <2 x i1> [[TMP2]], <2 x i64> zeroinitializer, <2 x i64> [[TMP1]]
333333
; CHECK-NEXT: [[INC]] = add nuw <2 x i64> [[I_04]], <i64 1, i64 1>
334334
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = call i1 @get.i1()
335-
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[ENTRY]], label %[[FOR_BODY]]
335+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]
336336
;
337337
entry:
338338
br label %for.body
@@ -892,10 +892,12 @@ define void @simple_urem_to_sel_non_zero_start_through_add(i32 %N, i32 %rem_amt_
892892
; CHECK: [[FOR_COND_CLEANUP]]:
893893
; CHECK-NEXT: ret void
894894
; CHECK: [[FOR_BODY]]:
895+
; CHECK-NEXT: [[REM:%.*]] = phi i32 [ 7, %[[FOR_BODY_PREHEADER]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
895896
; CHECK-NEXT: [[I_04:%.*]] = phi i32 [ [[INC:%.*]], %[[FOR_BODY]] ], [ 2, %[[FOR_BODY_PREHEADER]] ]
896-
; CHECK-NEXT: [[I_WITH_OFF:%.*]] = add nuw i32 [[I_04]], 5
897-
; CHECK-NEXT: [[REM:%.*]] = urem i32 [[I_WITH_OFF]], [[REM_AMT]]
898897
; CHECK-NEXT: tail call void @use.i32(i32 [[REM]])
898+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[REM]], 1
899+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], [[REM_AMT]]
900+
; CHECK-NEXT: [[TMP3]] = select i1 [[TMP2]], i32 0, i32 [[TMP1]]
899901
; CHECK-NEXT: [[INC]] = add nuw i32 [[I_04]], 1
900902
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[N]]
901903
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]

0 commit comments

Comments
 (0)