Skip to content

Commit 7c66607

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 c64ce8b commit 7c66607

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,17 +1976,43 @@ static bool foldFCmpToFPClassTest(CmpInst *Cmp, const TargetLowering &TLI,
19761976
return true;
19771977
}
19781978

1979-
static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
1980-
const LoopInfo *LI,
1981-
Value *&RemAmtOut,
1982-
PHINode *&LoopIncrPNOut) {
1979+
static bool isRemOfLoopIncrementWithLoopInvariant(
1980+
Instruction *Rem, const LoopInfo *LI, Value *&RemAmtOut,
1981+
std::optional<bool> &AddOrSubOut, Value *&AddOrSubInstOut,
1982+
Value *&AddOrSubOffsetOut, PHINode *&LoopIncrPNOut) {
19831983
Value *Incr, *RemAmt;
19841984
// NB: If RemAmt is a power of 2 it *should* have been transformed by now.
19851985
if (!match(Rem, m_URem(m_Value(Incr), m_Value(RemAmt))))
19861986
return false;
19871987

1988+
std::optional<bool> AddOrSub;
1989+
Value *AddOrSubOffset;
19881990
// Find out loop increment PHI.
19891991
auto *PN = dyn_cast<PHINode>(Incr);
1992+
if (PN != nullptr) {
1993+
AddOrSub = std::nullopt;
1994+
AddOrSubOffset = nullptr;
1995+
} else {
1996+
// Search through a NUW add/sub on top of the loop increment.
1997+
Value *V0, *V1;
1998+
if (match(Incr, m_NUWAddLike(m_Value(V0), m_Value(V1))))
1999+
AddOrSub = true;
2000+
else if (match(Incr, m_NUWSub(m_Value(V0), m_Value(V1))))
2001+
AddOrSub = false;
2002+
else
2003+
return false;
2004+
2005+
AddOrSubInstOut = Incr;
2006+
2007+
PN = dyn_cast<PHINode>(V0);
2008+
if (PN != nullptr) {
2009+
AddOrSubOffset = V1;
2010+
} else if (*AddOrSub) {
2011+
PN = dyn_cast<PHINode>(V1);
2012+
AddOrSubOffset = V0;
2013+
}
2014+
}
2015+
19902016
if (!PN)
19912017
return false;
19922018

@@ -2027,6 +2053,8 @@ static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
20272053
// Set output variables.
20282054
RemAmtOut = RemAmt;
20292055
LoopIncrPNOut = PN;
2056+
AddOrSubOut = AddOrSub;
2057+
AddOrSubOffsetOut = AddOrSubOffset;
20302058

20312059
return true;
20322060
}
@@ -2041,15 +2069,15 @@ static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
20412069
// Rem = (Start nuw+ IncrLoopInvariant) % RemAmtLoopInvariant;
20422070
// for(i = Start; i < End; ++i, ++rem)
20432071
// Rem = rem == RemAmtLoopInvariant ? 0 : Rem;
2044-
//
2045-
// Currently only implemented for `IncrLoopInvariant` being zero.
20462072
static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20472073
const LoopInfo *LI,
20482074
SmallSet<BasicBlock *, 32> &FreshBBs,
20492075
bool IsHuge) {
2050-
Value *RemAmt;
2076+
std::optional<bool> AddOrSub;
2077+
Value *AddOrSubOffset, *RemAmt, *AddOrSubInst;
20512078
PHINode *LoopIncrPN;
2052-
if (!isRemOfLoopIncrementWithLoopInvariant(Rem, LI, RemAmt, LoopIncrPN))
2079+
if (!isRemOfLoopIncrementWithLoopInvariant(
2080+
Rem, LI, RemAmt, AddOrSub, AddOrSubInst, AddOrSubOffset, LoopIncrPN))
20532081
return false;
20542082

20552083
// Only non-constant remainder as the extra IV is probably not profitable
@@ -2066,7 +2094,33 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20662094
return false;
20672095
Loop *L = LI->getLoopFor(Rem->getParent());
20682096

2097+
// If we have add/sub create initial value for remainder.
2098+
// The logic here is:
2099+
// (urem (add/sub nuw Start, IncrLoopInvariant), RemAmtLoopInvariant
2100+
//
2101+
// Only proceed if the expression simplifies (otherwise we can't fully
2102+
// optimize out the urem).
20692103
Value *Start = LoopIncrPN->getIncomingValueForBlock(L->getLoopPreheader());
2104+
if (AddOrSub) {
2105+
assert(AddOrSubOffset && AddOrSubInst &&
2106+
"We found an add/sub but missing values");
2107+
// Without dom-condition/assumption cache we aren't likely to get much out
2108+
// of a context instruction.
2109+
const SimplifyQuery Q(*DL);
2110+
bool NSW = cast<OverflowingBinaryOperator>(AddOrSubInst)->hasNoSignedWrap();
2111+
if (*AddOrSub)
2112+
Start = simplifyAddInst(Start, AddOrSubOffset, /*IsNSW=*/NSW,
2113+
/*IsNUW=*/true, Q);
2114+
else
2115+
Start = simplifySubInst(Start, AddOrSubOffset, /*IsNSW=*/NSW,
2116+
/*IsNUW=*/true, Q);
2117+
if (!Start)
2118+
return false;
2119+
2120+
Start = simplifyURemInst(Start, RemAmt, Q);
2121+
if (!Start)
2122+
return false;
2123+
}
20702124

20712125
// Create new remainder with induction variable.
20722126
Type *Ty = Rem->getType();
@@ -2093,6 +2147,8 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20932147

20942148
replaceAllUsesWith(Rem, NewRem, FreshBBs, IsHuge);
20952149
Rem->eraseFromParent();
2150+
if (AddOrSubInst && AddOrSubInst->use_empty())
2151+
cast<Instruction>(AddOrSubInst)->eraseFromParent();
20962152
return true;
20972153
}
20982154

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,12 @@ define void @simple_urem_to_sel_non_zero_start_through_add(i32 %N, i32 %rem_amt_
706706
; CHECK: [[FOR_COND_CLEANUP]]:
707707
; CHECK-NEXT: ret void
708708
; CHECK: [[FOR_BODY]]:
709+
; CHECK-NEXT: [[REM:%.*]] = phi i32 [ 7, %[[FOR_BODY_PREHEADER]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
709710
; CHECK-NEXT: [[I_04:%.*]] = phi i32 [ [[INC:%.*]], %[[FOR_BODY]] ], [ 2, %[[FOR_BODY_PREHEADER]] ]
710-
; CHECK-NEXT: [[I_WITH_OFF:%.*]] = add nuw i32 [[I_04]], 5
711-
; CHECK-NEXT: [[REM:%.*]] = urem i32 [[I_WITH_OFF]], [[REM_AMT]]
712711
; CHECK-NEXT: tail call void @use.i32(i32 [[REM]])
712+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[REM]], 1
713+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], [[REM_AMT]]
714+
; CHECK-NEXT: [[TMP3]] = select i1 [[TMP2]], i32 0, i32 [[TMP1]]
713715
; CHECK-NEXT: [[INC]] = add nuw i32 [[I_04]], 1
714716
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[N]]
715717
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]
@@ -817,10 +819,12 @@ define void @simple_urem_to_sel_non_zero_start_through_sub(i32 %N, i32 %rem_amt,
817819
; CHECK: [[FOR_COND_CLEANUP]]:
818820
; CHECK-NEXT: ret void
819821
; CHECK: [[FOR_BODY]]:
822+
; CHECK-NEXT: [[REM:%.*]] = phi i32 [ 0, %[[FOR_BODY_PREHEADER]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
820823
; CHECK-NEXT: [[I_04:%.*]] = phi i32 [ [[INC:%.*]], %[[FOR_BODY]] ], [ [[START]], %[[FOR_BODY_PREHEADER]] ]
821-
; CHECK-NEXT: [[I_WITH_OFF:%.*]] = sub nuw i32 [[I_04]], [[START]]
822-
; CHECK-NEXT: [[REM:%.*]] = urem i32 [[I_WITH_OFF]], [[REM_AMT]]
823824
; CHECK-NEXT: tail call void @use.i32(i32 [[REM]])
825+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[REM]], 1
826+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], [[REM_AMT]]
827+
; CHECK-NEXT: [[TMP3]] = select i1 [[TMP2]], i32 0, i32 [[TMP1]]
824828
; CHECK-NEXT: [[INC]] = add nuw i32 [[I_04]], 1
825829
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[N]]
826830
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]

0 commit comments

Comments
 (0)