-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[DAGCombiner] Fix the "subtraction if above a constant threshold" miscompile #140042
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
fd9fa66
f052064
74ca22c
b61f74e
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 |
|---|---|---|
|
|
@@ -12140,11 +12140,21 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) { | |
| // (select (ult x, C), x, (add x, -C)) -> (umin x, (add x, -C)) | ||
| APInt C; | ||
| if (sd_match(Cond1, m_ConstInt(C)) && hasUMin(VT)) { | ||
| if ((CC == ISD::SETUGT && Cond0 == N2 && | ||
| sd_match(N1, m_Add(m_Specific(N2), m_SpecificInt(~C)))) || | ||
| (CC == ISD::SETULT && Cond0 == N1 && | ||
| sd_match(N2, m_Add(m_Specific(N1), m_SpecificInt(-C))))) | ||
| return DAG.getNode(ISD::UMIN, DL, VT, N1, N2); | ||
| if (CC == ISD::SETUGT && Cond0 == N2 && | ||
| sd_match(N1, m_Add(m_Specific(N2), m_SpecificInt(~C)))) { | ||
|
||
| // The resulting code relies on an unsigned wrap in ADD. | ||
| // Recreating ADD to drop possible nuw/nsw flags. | ||
| SDValue AddC = DAG.getConstant(~C, DL, VT); | ||
| SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N2, AddC); | ||
| return DAG.getNode(ISD::UMIN, DL, VT, Add, N2); | ||
| } | ||
| if (CC == ISD::SETULT && Cond0 == N1 && | ||
|
||
| sd_match(N2, m_Add(m_Specific(N1), m_SpecificInt(-C)))) { | ||
| // Ditto. | ||
| SDValue AddC = DAG.getConstant(-C, DL, VT); | ||
| SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N1, AddC); | ||
| return DAG.getNode(ISD::UMIN, DL, VT, N1, Add); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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: Could add a small comment here telling why we need to recreate the ADD. Just to avoid that someone tries to simplify this in the future, or if someone wonder why we can do this without introducing FREEZE.
Maybe something like this:
// Need to drop any poison generating flags from the ADD, to ensure that the result isn't more poisonous.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.
Done.