Skip to content

Commit 9b88f56

Browse files
committed
Remove add/sub handling
1 parent ef331b6 commit 9b88f56

File tree

2 files changed

+11
-66
lines changed

2 files changed

+11
-66
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,10 @@ static bool foldFCmpToFPClassTest(CmpInst *Cmp, const TargetLowering &TLI,
19751975
return true;
19761976
}
19771977

1978-
static bool isRemOfLoopIncrementWithLoopInvariant(
1979-
Instruction *Rem, const LoopInfo *LI, Value *&RemAmtOut,
1980-
std::optional<bool> &AddOrSubOut, Value *&AddOrSubInstOut,
1981-
Value *&AddOrSubOffsetOut, PHINode *&LoopIncrPNOut) {
1978+
static bool isRemOfLoopIncrementWithLoopInvariant(Instruction *Rem,
1979+
const LoopInfo *LI,
1980+
Value *&RemAmtOut,
1981+
PHINode *&LoopIncrPNOut) {
19821982
Value *Incr, *RemAmt;
19831983
// NB: If RemAmt is a power of 2 it *should* have been transformed by now.
19841984
if (!match(Rem, m_URem(m_Value(Incr), m_Value(RemAmt))))
@@ -1989,33 +1989,8 @@ static bool isRemOfLoopIncrementWithLoopInvariant(
19891989
if (!L || !L->getLoopPreheader() || !L->getLoopLatch())
19901990
return false;
19911991

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

20201995
if (!PN)
20211996
return false;
@@ -2052,8 +2027,6 @@ static bool isRemOfLoopIncrementWithLoopInvariant(
20522027
// Set output variables.
20532028
RemAmtOut = RemAmt;
20542029
LoopIncrPNOut = PN;
2055-
AddOrSubOut = AddOrSub;
2056-
AddOrSubOffsetOut = AddOrSubOffset;
20572030

20582031
return true;
20592032
}
@@ -2069,16 +2042,14 @@ static bool isRemOfLoopIncrementWithLoopInvariant(
20692042
// for(i = Start; i < End; ++i, ++rem)
20702043
// Rem = rem == RemAmtLoopInvariant ? 0 : Rem;
20712044
//
2072-
// Currently only implemented for `Start` and `IncrLoopInvariant` being zero.
2045+
// Currently only implemented for `IncrLoopInvariant` being zero.
20732046
static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
20742047
const LoopInfo *LI,
20752048
SmallSet<BasicBlock *, 32> &FreshBBs,
20762049
bool IsHuge) {
2077-
std::optional<bool> AddOrSub;
2078-
Value *AddOrSubOffset, *RemAmt, *AddOrSubInst;
2050+
Value *RemAmt;
20792051
PHINode *LoopIncrPN;
2080-
if (!isRemOfLoopIncrementWithLoopInvariant(
2081-
Rem, LI, RemAmt, AddOrSub, AddOrSubInst, AddOrSubOffset, LoopIncrPN))
2052+
if (!isRemOfLoopIncrementWithLoopInvariant(Rem, LI, RemAmt, LoopIncrPN))
20822053
return false;
20832054

20842055
// Only non-constant remainder as the extra IV is probably not profitable
@@ -2102,26 +2073,6 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
21022073
// Only proceed if the expression simplifies (otherwise we can't fully
21032074
// optimize out the urem).
21042075
Value *Start = LoopIncrPN->getIncomingValueForBlock(L->getLoopPreheader());
2105-
if (AddOrSub) {
2106-
assert(AddOrSubOffset && AddOrSubInst &&
2107-
"We found an add/sub but missing values");
2108-
// Without dom-condition/assumption cache we aren't likely to get much out
2109-
// of a context instruction.
2110-
const SimplifyQuery Q(*DL);
2111-
bool NSW = cast<OverflowingBinaryOperator>(AddOrSubInst)->hasNoSignedWrap();
2112-
if (*AddOrSub)
2113-
Start = simplifyAddInst(Start, AddOrSubOffset, /*IsNSW=*/NSW,
2114-
/*IsNUW=*/true, Q);
2115-
else
2116-
Start = simplifySubInst(Start, AddOrSubOffset, /*IsNSW=*/NSW,
2117-
/*IsNUW=*/true, Q);
2118-
if (!Start)
2119-
return false;
2120-
2121-
Start = simplifyURemInst(Start, RemAmt, Q);
2122-
if (!Start)
2123-
return false;
2124-
}
21252076

21262077
// Create new remainder with induction variable.
21272078
Type *Ty = Rem->getType();
@@ -2148,8 +2099,6 @@ static bool foldURemOfLoopIncrement(Instruction *Rem, const DataLayout *DL,
21482099

21492100
replaceAllUsesWith(Rem, NewRem, FreshBBs, IsHuge);
21502101
Rem->eraseFromParent();
2151-
if (AddOrSubInst && AddOrSubInst->use_empty())
2152-
cast<Instruction>(AddOrSubInst)->eraseFromParent();
21532102
return true;
21542103
}
21552104

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,10 @@ 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]] ]
710709
; 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]]
711712
; 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]]
715713
; CHECK-NEXT: [[INC]] = add nuw i32 [[I_04]], 1
716714
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[N]]
717715
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]
@@ -819,12 +817,10 @@ define void @simple_urem_to_sel_non_zero_start_through_sub(i32 %N, i32 %rem_amt,
819817
; CHECK: [[FOR_COND_CLEANUP]]:
820818
; CHECK-NEXT: ret void
821819
; CHECK: [[FOR_BODY]]:
822-
; CHECK-NEXT: [[REM:%.*]] = phi i32 [ 0, %[[FOR_BODY_PREHEADER]] ], [ [[TMP3:%.*]], %[[FOR_BODY]] ]
823820
; 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]]
824823
; 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]]
828824
; CHECK-NEXT: [[INC]] = add nuw i32 [[I_04]], 1
829825
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[N]]
830826
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[FOR_COND_CLEANUP]], label %[[FOR_BODY]]

0 commit comments

Comments
 (0)