-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Match intrinsic recurrences when known to be hoisted #149858
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1532,6 +1532,51 @@ static Instruction *foldBitOrderCrossLogicOp(Value *V, | |||||||||||||
return nullptr; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Helper to match idempotent binary intrinsics, namely, intrinsics where | ||||||||||||||
/// `f(f(x, y), y) == f(x, y)` holds. | ||||||||||||||
static bool isIdempotentBinaryIntrinsic(Intrinsic::ID IID) { | ||||||||||||||
switch (IID) { | ||||||||||||||
case Intrinsic::smax: | ||||||||||||||
case Intrinsic::smin: | ||||||||||||||
case Intrinsic::umax: | ||||||||||||||
case Intrinsic::umin: | ||||||||||||||
case Intrinsic::maximum: | ||||||||||||||
case Intrinsic::minimum: | ||||||||||||||
case Intrinsic::maximumnum: | ||||||||||||||
case Intrinsic::minimumnum: | ||||||||||||||
case Intrinsic::maxnum: | ||||||||||||||
case Intrinsic::minnum: | ||||||||||||||
return true; | ||||||||||||||
default: | ||||||||||||||
return false; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Attempt to simplify value-accumulating recurrences of kind: | ||||||||||||||
/// %umax.acc = phi i8 [ %umax, %backedge ], [ %a, %entry ] | ||||||||||||||
/// %umax = call i8 @llvm.umax.i8(i8 %umax.acc, i8 %b) | ||||||||||||||
/// And let the idempotent binary intrinsic be hoisted, when the operands are | ||||||||||||||
/// known to be loop-invariant. | ||||||||||||||
static Value *foldIdempotentBinaryIntrinsicRecurrence(InstCombinerImpl &IC, | ||||||||||||||
IntrinsicInst *II) { | ||||||||||||||
PHINode *PN; | ||||||||||||||
Value *Init, *OtherOp; | ||||||||||||||
|
||||||||||||||
// A binary intrinsic recurrence with loop-invariant operands is equivalent to | ||||||||||||||
// `call @llvm.binary.intrinsic(Init, OtherOp)`. | ||||||||||||||
auto IID = II->getIntrinsicID(); | ||||||||||||||
if (!isIdempotentBinaryIntrinsic(IID) || | ||||||||||||||
!matchSimpleBinaryIntrinsicRecurrence(II, PN, Init, OtherOp) || | ||||||||||||||
!IC.getDominatorTree().dominates(OtherOp, PN)) | ||||||||||||||
return nullptr; | ||||||||||||||
|
||||||||||||||
auto *InvariantBinaryInst = | ||||||||||||||
IC.Builder.CreateBinaryIntrinsic(IID, Init, OtherOp); | ||||||||||||||
if (isa<FPMathOperator>(InvariantBinaryInst)) | ||||||||||||||
cast<Instruction>(InvariantBinaryInst)->copyFastMathFlags(II); | ||||||||||||||
|
auto *InvariantBinaryInst = | |
IC.Builder.CreateBinaryIntrinsic(IID, Init, OtherOp); | |
if (isa<FPMathOperator>(InvariantBinaryInst)) | |
cast<Instruction>(InvariantBinaryInst)->copyFastMathFlags(II); | |
auto *InvariantBinaryInst = | |
IC.Builder.CreateBinaryIntrinsic(IID, Init, OtherOp, /*FMFSource=*/II); |
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.
That's unfortunately not possible per assertion isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"'
on non-fast-math binary intrinsics. Perhaps we may want to change this in IRBuilder intrinsic creation in the future.
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.
Use dyn_cast<FPMathOperator>(II)
?
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.
In this case, I think you'd need another dyn_cast<Instruction>(InvariantBinaryInst)
to check that CreateBinaryIntrinsic
didn't constant-fold, as copyFastMathFlags is private to FPMathOperator. Think the current one may be cleaner.
Uh oh!
There was an error while loading. Please reload this page.