Skip to content

[CodeGen] Avoid quadratic operand removal in MachineInstr::insert - #220165

Merged
arsenm merged 1 commit into
llvm:mainfrom
LittleJianCH:opt/machineinstr-insert-linear-time
Sep 8, 2026
Merged

[CodeGen] Avoid quadratic operand removal in MachineInstr::insert#220165
arsenm merged 1 commit into
llvm:mainfrom
LittleJianCH:opt/machineinstr-insert-linear-time

Conversation

@LittleJianCH

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

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.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

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 questions

How 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 @ followed by their GitHub username.

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,
The LLVM Community

@LittleJianCH

Copy link
Copy Markdown
Contributor Author

No AI tools were used for this contribution.

@arsenm Would you mind taking a look at this? Thanks!

Comment thread llvm/lib/CodeGen/MachineInstr.cpp Outdated
Comment on lines 2743 to 2748
for (unsigned I = OpIdx; I < NumOperands; ++I) {
MovingOps.emplace_back(getOperand(I));
}
while (getNumOperands() != OpIdx) {
removeOperand(getNumOperands() - 1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can drop the braces

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread llvm/lib/CodeGen/MachineInstr.cpp Outdated
MovingOps.emplace_back(getOperand(I));
}
while (getNumOperands() != OpIdx) {
removeOperand(getNumOperands() - 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@nickdesaulniers

Copy link
Copy Markdown
Member

This MR

What's "MR?"

@arsenm

arsenm commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

This MR

What's "MR?"

GItlab speak for merge request

Comment thread llvm/lib/CodeGen/MachineInstr.cpp Outdated
Comment on lines 2746 to 2748
while (getNumOperands() != OpIdx) {
removeOperand(getNumOperands() - 1);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps

for (unsigned I = getNumOperands(); I > OpIdx; --I)
  removeOperand(I - 1);

@LittleJianCH
LittleJianCH force-pushed the opt/machineinstr-insert-linear-time branch from b18a1bd to 57406d1 Compare September 6, 2026 17:18
@LittleJianCH
LittleJianCH force-pushed the opt/machineinstr-insert-linear-time branch from 57406d1 to e29d6d2 Compare September 6, 2026 17:24
@LittleJianCH

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! I've updated the patch.

@nickdesaulniers nickdesaulniers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit body is currently empty. Consider putting something in there, such as matching the PR's description (modulo the MR term).

@arsenm
arsenm merged commit c0a6b4b into llvm:main Sep 8, 2026
12 checks passed
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

@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!

vadimkotov pushed a commit to vadimkotov/llvm-project that referenced this pull request Sep 11, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants