Skip to content

LICM: extend hoistAddSub to unsigned case #106373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions llvm/lib/Transforms/Scalar/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2537,14 +2537,19 @@ static bool hoistAdd(ICmpInst::Predicate Pred, Value *VariantLHS,
Value *InvariantRHS, ICmpInst &ICmp, Loop &L,
ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU,
AssumptionCache *AC, DominatorTree *DT) {
assert(ICmpInst::isSigned(Pred) && "Not supported yet!");
assert(!L.isLoopInvariant(VariantLHS) && "Precondition.");
assert(L.isLoopInvariant(InvariantRHS) && "Precondition.");

bool IsSigned = ICmpInst::isSigned(Pred);

// Try to represent VariantLHS as sum of invariant and variant operands.
using namespace PatternMatch;
Value *VariantOp, *InvariantOp;
if (!match(VariantLHS, m_NSWAdd(m_Value(VariantOp), m_Value(InvariantOp))))
if (IsSigned &&
!match(VariantLHS, m_NSWAdd(m_Value(VariantOp), m_Value(InvariantOp))))
return false;
if (!IsSigned &&
!match(VariantLHS, m_NUWAdd(m_Value(VariantOp), m_Value(InvariantOp))))
return false;

// LHS itself is a loop-variant, try to represent it in the form:
Expand All @@ -2559,17 +2564,20 @@ static bool hoistAdd(ICmpInst::Predicate Pred, Value *VariantLHS,
// normal linear arithmetics). Overflows make things much more complicated, so
// we want to avoid this.
auto &DL = L.getHeader()->getDataLayout();
bool ProvedNoOverflowAfterReassociate =
computeOverflowForSignedSub(InvariantRHS, InvariantOp,
SimplifyQuery(DL, DT, AC, &ICmp)) ==
llvm::OverflowResult::NeverOverflows;
if (!ProvedNoOverflowAfterReassociate)
SimplifyQuery SQ(DL, DT, AC, &ICmp);
if (IsSigned && computeOverflowForSignedSub(InvariantRHS, InvariantOp, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
if (!IsSigned &&
computeOverflowForUnsignedSub(InvariantRHS, InvariantOp, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
auto *Preheader = L.getLoopPreheader();
assert(Preheader && "Loop is not in simplify form?");
IRBuilder<> Builder(Preheader->getTerminator());
Value *NewCmpOp = Builder.CreateSub(InvariantRHS, InvariantOp, "invariant.op",
/*HasNUW*/ false, /*HasNSW*/ true);
Value *NewCmpOp =
Builder.CreateSub(InvariantRHS, InvariantOp, "invariant.op",
/*HasNUW*/ !IsSigned, /*HasNSW*/ IsSigned);
ICmp.setPredicate(Pred);
ICmp.setOperand(0, VariantOp);
ICmp.setOperand(1, NewCmpOp);
Expand All @@ -2584,14 +2592,19 @@ static bool hoistSub(ICmpInst::Predicate Pred, Value *VariantLHS,
Value *InvariantRHS, ICmpInst &ICmp, Loop &L,
ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU,
AssumptionCache *AC, DominatorTree *DT) {
assert(ICmpInst::isSigned(Pred) && "Not supported yet!");
assert(!L.isLoopInvariant(VariantLHS) && "Precondition.");
assert(L.isLoopInvariant(InvariantRHS) && "Precondition.");

bool IsSigned = ICmpInst::isSigned(Pred);

// Try to represent VariantLHS as sum of invariant and variant operands.
using namespace PatternMatch;
Value *VariantOp, *InvariantOp;
if (!match(VariantLHS, m_NSWSub(m_Value(VariantOp), m_Value(InvariantOp))))
if (IsSigned &&
!match(VariantLHS, m_NSWSub(m_Value(VariantOp), m_Value(InvariantOp))))
return false;
if (!IsSigned &&
!match(VariantLHS, m_NUWSub(m_Value(VariantOp), m_Value(InvariantOp))))
return false;

bool VariantSubtracted = false;
Expand All @@ -2613,26 +2626,36 @@ static bool hoistSub(ICmpInst::Predicate Pred, Value *VariantLHS,
// "C1 - C2" does not overflow.
auto &DL = L.getHeader()->getDataLayout();
SimplifyQuery SQ(DL, DT, AC, &ICmp);
if (VariantSubtracted) {
if (VariantSubtracted && IsSigned) {
// C1 - LV < C2 --> LV > C1 - C2
if (computeOverflowForSignedSub(InvariantOp, InvariantRHS, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
} else {
} else if (VariantSubtracted && !IsSigned) {
// C1 - LV < C2 --> LV > C1 - C2
if (computeOverflowForUnsignedSub(InvariantOp, InvariantRHS, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
} else if (!VariantSubtracted && IsSigned) {
// LV - C1 < C2 --> LV < C1 + C2
if (computeOverflowForSignedAdd(InvariantOp, InvariantRHS, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
} else { // !VariantSubtracted && !IsSigned
// LV - C1 < C2 --> LV < C1 + C2
if (computeOverflowForUnsignedAdd(InvariantOp, InvariantRHS, SQ) !=
llvm::OverflowResult::NeverOverflows)
return false;
}
auto *Preheader = L.getLoopPreheader();
assert(Preheader && "Loop is not in simplify form?");
IRBuilder<> Builder(Preheader->getTerminator());
Value *NewCmpOp =
VariantSubtracted
? Builder.CreateSub(InvariantOp, InvariantRHS, "invariant.op",
/*HasNUW*/ false, /*HasNSW*/ true)
/*HasNUW*/ !IsSigned, /*HasNSW*/ IsSigned)
: Builder.CreateAdd(InvariantOp, InvariantRHS, "invariant.op",
/*HasNUW*/ false, /*HasNSW*/ true);
/*HasNUW*/ !IsSigned, /*HasNSW*/ IsSigned);
ICmp.setPredicate(Pred);
ICmp.setOperand(0, VariantOp);
ICmp.setOperand(1, NewCmpOp);
Expand All @@ -2650,10 +2673,6 @@ static bool hoistAddSub(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
if (!match(&I, m_ICmp(Pred, m_Value(LHS), m_Value(RHS))))
return false;

// TODO: Support unsigned predicates?
if (!ICmpInst::isSigned(Pred))
return false;

// Put variant operand to LHS position.
if (L.isLoopInvariant(LHS)) {
std::swap(LHS, RHS);
Expand Down
Loading
Loading