This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] transform MI to W variant to remove sext.w
ClosedPublic

Authored by mohammed-nurulhoque on Feb 16 2022, 3:20 AM.

Details

Summary

Backwards search
The sext.w removal pass (before the new patch) checks if the input to sext.w is already in sign-extended form, so it can eliminate it. It does that by checking every definition/source that reaches the sext.w is an instruction that produces a sign-extended value, either by definition (e.g. ADDW), or it propagates sign-extension (e.g. OR) so we check its sources recursively.

Forward search
Sometimes, one of the sources is an instruction that doesn't always produce a sign-extended value, but it has a W-version that does (e.g. ADD / ADDW). If we transform the ADD to ADDW, the sext.w can be removed (assuming other def paths are satisfied), but this transformation is sound only if every use of this ADD/W only reqruires the lower 32-bits either directly (like sll %x, 32) or they propagate dependency (lower word of output only depends on lower word of input) so we check its uses recursively.

When searching backwards, if an instruction that can be replaced with W-variant is encountered, this pass runs the forward search to verify it can be replaced, then adds it to a list of fixable instructions. After verifying all paths, it replaces the instruction and removes the sext.w.

Diff Detail

Event Timeline

mohammed-nurulhoque requested review of this revision.Feb 16 2022, 3:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2022, 5:53 AM
mohammed-nurulhoque added a comment.EditedMar 11 2022, 5:55 AM

I addressed the comments made in https://reviews.llvm.org/D119532 here, except the one for ADD_UW. I couldn't find a straightforward way to find, given an operand, what's its order in its parent instruction! The way it is currently is correct, but of course it would be stronger if we can differentiate the 2 operands.

craig.topper added inline comments.Mar 21 2022, 10:26 AM
llvm/lib/Target/RISCV/RISCVSExtWRemoval.cpp
256

Drop else after return.

276

Why do we need a cast to uint32_t here?

Fix style and typos

mohammed-nurulhoque marked 2 inline comments as done.Mar 28 2022, 1:45 AM
craig.topper added inline comments.Mar 29 2022, 2:57 PM
llvm/lib/Target/RISCV/RISCVSExtWRemoval.cpp
478

If this converts a load instruction, you need to copy the MachineMemOperand.

Add memoperands to replacement instruction

mohammed-nurulhoque marked an inline comment as done.Apr 21 2022, 9:24 AM
This revision is now accepted and ready to land.Apr 22 2022, 9:04 AM

Thanks. Could you commit the change? Here are my details
Name: Mohammed Nurul Hoque
email: mntalateyya@live.com

This revision was automatically updated to reflect the committed changes.