-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[RISCV] Fix a correctness issue in optimizeCondBranch. Prevent optimizing compare with x0. NFC #145440
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
[RISCV] Fix a correctness issue in optimizeCondBranch. Prevent optimizing compare with x0. NFC #145440
Changes from 2 commits
edf44b5
26cba07
b1b61f6
32dd880
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1449,11 +1449,14 @@ bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const { | |||||
| return Register(); | ||||||
| }; | ||||||
|
|
||||||
| if (isFromLoadImm(MRI, LHS, C0) && MRI.hasOneUse(LHS.getReg())) { | ||||||
| if (isFromLoadImm(MRI, LHS, C0) && LHS.getReg().isVirtual() && | ||||||
| MRI.hasOneUse(LHS.getReg())) { | ||||||
| assert(isInt<12>(C0) && "Unexpected immediate"); | ||||||
| // Might be case 1. | ||||||
| // Signed integer overflow is UB. (UINT64_MAX is bigger so we don't need | ||||||
| // to worry about unsigned overflow here) | ||||||
| if (C0 < INT64_MAX) | ||||||
| // Don't change 0 to -1 since we can use x0. | ||||||
|
||||||
| // Don't change 0 to -1 since we can use x0. | |
| // Don't change 0 to 1 since we can use x0. |
Outdated
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.
Style wise, folding the C0 is non-zero check into the prior if-clause might make the code easier to read in both cases.
Outdated
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.
(A || B) || C is just A || B || C isn't it?
Outdated
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.
Ok, staring at this, I'm questioning my sanity. Isn't this just completely unsound?
Given X <=u 22, this produces 21 >u X doesn't it?
Isn't that just complete wrong for X = 22?
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.
It can only be x < 22 or x >= 22 here. LT/GE are the only valid condition codes. Which will become 21 >= x or 21 < x.
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.
Where is that precondition established?
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.
The RISC-V ISA. There are 6 branch instructions. EQ, NE, BLT, BGE, BLTU, BGEU.
EQ/NE are excluded earlier. This code handles the remaining 4.
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.
Ok, revised example.
BLT X, INT_MIN ==> BGE INT_MAX, X
The former is statically false. The later is statically true.
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.
Protected by the assert(isInt<12>(C0)
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.
Ah! That was the bit I was missing. We can't have INT_MIN here until we handle LUI/ADDI sequences.
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 was having the same question with Philip as where did we prevent sign overflow from happening, so perhaps we can leave a comment here?