[CodeGen] Avoid quadratic operand removal in MachineInstr::insert - #220165
Conversation
|
Hello @LittleJianCH 👋 Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description. Frequently asked questionsHow do I add reviewers? This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically. You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using What if there are no comments? If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers. Are any special GitHub settings required to contribute to LLVM? We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details. If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse. Thank you, |
|
No AI tools were used for this contribution. @arsenm Would you mind taking a look at this? Thanks! |
| for (unsigned I = OpIdx; I < NumOperands; ++I) { | ||
| MovingOps.emplace_back(getOperand(I)); | ||
| } | ||
| while (getNumOperands() != OpIdx) { | ||
| removeOperand(getNumOperands() - 1); | ||
| } |
There was a problem hiding this comment.
| MovingOps.emplace_back(getOperand(I)); | ||
| } | ||
| while (getNumOperands() != OpIdx) { | ||
| removeOperand(getNumOperands() - 1); |
There was a problem hiding this comment.
InsertBefore and operands_end() are MachineOperand * iterators, so you can initialize MovingOps directly with the iterator range constructor rather than manually looping with emplace_back.
This allows simplifying lines 2736–2748 to:
unsigned OpIdx = getOperandNo(InsertBefore);
SmallVector<MachineOperand> MovingOps(InsertBefore, operands_end());
while (getNumOperands() > OpIdx)
removeOperand(getNumOperands() - 1);(This also lets you eliminate NumOperands and OpsToMove above).
What's "MR?" |
GItlab speak for merge request |
| while (getNumOperands() != OpIdx) { | ||
| removeOperand(getNumOperands() - 1); | ||
| } |
There was a problem hiding this comment.
perhaps
for (unsigned I = getNumOperands(); I > OpIdx; --I)
removeOperand(I - 1);
b18a1bd to
57406d1
Compare
57406d1 to
e29d6d2
Compare
|
Thanks for the reviews! I've updated the patch. |
nickdesaulniers
left a comment
There was a problem hiding this comment.
The commit body is currently empty. Consider putting something in there, such as matching the PR's description (modulo the MR term).
|
@LittleJianCH Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…vm#220165) This optimizes MachineInstr::insert by copying trailing operands first and then removing them from the end. This avoids repeatedly shifting the remaining operands, reducing the operation from quadratic to linear time without changing behavior.
This MR optimizes MachineInstr::insert by copying trailing operands first and then removing them from the end. This avoids repeatedly shifting the remaining operands, reducing the operation from quadratic to linear time without changing behavior.