-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[InstCombine] Fold select Cond, not X, X
into Cond ^ X
#90089
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
6dffd8b
ac7ded4
d1e3e81
1731798
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 |
---|---|---|
|
@@ -3985,5 +3985,13 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) { | |
} | ||
} | ||
|
||
// select Cond, !X, X -> xor Cond, X | ||
// Note: We don't fold select Cond, Y, X -> X (iff X->Y & !X->!Y) here as | ||
// it indicates that these two patterns should be canonicalized. | ||
if (CondVal->getType() == SI.getType() && impliesPoison(FalseVal, TrueVal) && | ||
isImpliedCondition(FalseVal, TrueVal, DL, /*LHSIsTrue=*/true) == false && | ||
isImpliedCondition(FalseVal, TrueVal, DL, /*LHSIsTrue=*/false) == true) | ||
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. Shouldnt the check be 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. When FVal implies TVal and !FVal -> !TVal, TVal must be the same as FVal. If not, we need a canonicalization for this pattern. BTW, I tried 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. Maybe add a comment that the inverse case doesn't naturally occur in code to avoid people re-testing / re-submitting. |
||
return BinaryOperator::CreateXor(CondVal, FalseVal); | ||
|
||
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.
Would it cover the motivating cases to check for icmp with same operands and inverted predicate?
This is a pretty weird check and I'm not sure if doing the poison check in one direction is sufficient here.
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.
Hm, I guess it's fine as FalseVal is the value we're replacing with.
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.
I'd still like an answer to this question. This is a very roundabout way to check whether the conditions are inverses of each other, and it does have some overhead (http://llvm-compile-time-tracker.com/compare.php?from=76a3be7c766bd55221c3d0d0a74c42f82c5d76ed&to=1731798e2ef457fb5cabd29ae054c700b8b13c14&stat=instructions%3Au). Not enough to make me overly concerned, about enough to contribute to the death by a thousand cuts :)
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.
I can simplify the implementation to just handle
select Cond, icmp X, C1, icmp X, C2
, which covers all the motivating cases.