-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[InstCombine] Fold dependent IVs #81151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1378,6 +1378,58 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN, | |||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Fold iv = phi(start, iv.next = iv2.next op start) | ||||||||||||||||||||||||||||||||||||||||||||||
| // where iv2 = phi(iv2.start, iv2.next = iv2 + iv2.step) | ||||||||||||||||||||||||||||||||||||||||||||||
| // and iv2.start op start = start | ||||||||||||||||||||||||||||||||||||||||||||||
| // to iv = iv2 op start | ||||||||||||||||||||||||||||||||||||||||||||||
| static Value *foldDependentIVs(PHINode &PN, IRBuilderBase &Builder) { | ||||||||||||||||||||||||||||||||||||||||||||||
| BasicBlock *BB = PN.getParent(); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (PN.getNumIncomingValues() != 2) | ||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Value *Start; | ||||||||||||||||||||||||||||||||||||||||||||||
| Instruction *IvNext; | ||||||||||||||||||||||||||||||||||||||||||||||
| BinaryOperator *Iv2Next; | ||||||||||||||||||||||||||||||||||||||||||||||
| auto MatchOuterIV = [&](Value *V1, Value *V2) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (match(V2, m_c_BinOp(m_Specific(V1), m_BinOp(Iv2Next))) || | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
llvm-project/llvm/include/llvm/IR/PatternMatch.h Lines 911 to 927 in 8f7ae64
llvm-project/llvm/include/llvm/IR/PatternMatch.h Lines 2410 to 2414 in 8f7ae64
It doesn't cause miscompilation because
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm aware -- this is why there is an isCommutative() assertion below, in case the behavior of getBinOpIdentity() ever changes. |
||||||||||||||||||||||||||||||||||||||||||||||
| match(V2, m_GEP(m_Specific(V1), m_BinOp(Iv2Next)))) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Start = V1; | ||||||||||||||||||||||||||||||||||||||||||||||
| IvNext = cast<Instruction>(V2); | ||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (!MatchOuterIV(PN.getIncomingValue(0), PN.getIncomingValue(1)) && | ||||||||||||||||||||||||||||||||||||||||||||||
| !MatchOuterIV(PN.getIncomingValue(1), PN.getIncomingValue(0))) | ||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| PHINode *Iv2; | ||||||||||||||||||||||||||||||||||||||||||||||
| Value *Iv2Start, *Iv2Step; | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!matchSimpleRecurrence(Iv2Next, Iv2, Iv2Start, Iv2Step) || | ||||||||||||||||||||||||||||||||||||||||||||||
| Iv2->getParent() != BB) | ||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| auto *BO = dyn_cast<BinaryOperator>(IvNext); | ||||||||||||||||||||||||||||||||||||||||||||||
| Constant *Identity = | ||||||||||||||||||||||||||||||||||||||||||||||
| BO ? ConstantExpr::getBinOpIdentity(BO->getOpcode(), Iv2Start->getType()) | ||||||||||||||||||||||||||||||||||||||||||||||
| : Constant::getNullValue(Iv2Start->getType()); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (Iv2Start != Identity) | ||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Builder.SetInsertPoint(&*BB, BB->getFirstInsertionPt()); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!BO) { | ||||||||||||||||||||||||||||||||||||||||||||||
| auto *GEP = cast<GEPOperator>(IvNext); | ||||||||||||||||||||||||||||||||||||||||||||||
| return Builder.CreateGEP(GEP->getSourceElementType(), Start, Iv2, "", | ||||||||||||||||||||||||||||||||||||||||||||||
| cast<GEPOperator>(IvNext)->isInBounds()); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| assert(BO->isCommutative() && "Must be commutative"); | ||||||||||||||||||||||||||||||||||||||||||||||
| Value *Res = Builder.CreateBinOp(BO->getOpcode(), Iv2, Start); | ||||||||||||||||||||||||||||||||||||||||||||||
| cast<Instruction>(Res)->copyIRFlags(BO); | ||||||||||||||||||||||||||||||||||||||||||||||
| return Res; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // PHINode simplification | ||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||
| Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1595,5 +1647,8 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) { | |||||||||||||||||||||||||||||||||||||||||||||
| if (auto *V = simplifyUsingControlFlow(*this, PN, DT)) | ||||||||||||||||||||||||||||||||||||||||||||||
| return replaceInstUsesWith(PN, V); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (Value *Res = foldDependentIVs(PN, Builder)) | ||||||||||||||||||||||||||||||||||||||||||||||
| return replaceInstUsesWith(PN, Res); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe add a todo for detecting if
Iv2Nextis rhs/lhs to cover more binops (like sub).