-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[RISCV] Add a MIR pass to reassociate shXadd, add, and slli to form more shXadd. #87544
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
//===- RISCVOptZba.cpp - MI Zba instruction optimizations -----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
// | ||
// This pass reassociates expressions like | ||
// (sh3add Z, (add X, (slli Y, 5))) | ||
// To | ||
// (sh3add (sh2add Y, Z), X) | ||
// | ||
// If the shift amount is small enough. The outer shXadd keeps its original | ||
// opcode. The inner shXadd shift amount is the difference between the slli | ||
// shift amount and the outer shXadd shift amount. | ||
// | ||
// This pattern can appear when indexing a two dimensional array, but it is not | ||
// limited to that. | ||
// | ||
// TODO: We can also support slli.uw by using shXadd.uw for the inner shXadd. | ||
// TODO: This can be generalized to deeper expressions. | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#include "RISCV.h" | ||
#include "RISCVSubtarget.h" | ||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "riscv-opt-zba" | ||
#define RISCV_OPT_ZBA_NAME "RISC-V Optimize Zba" | ||
|
||
namespace { | ||
|
||
class RISCVOptZba : public MachineFunctionPass { | ||
public: | ||
static char ID; | ||
|
||
RISCVOptZba() : MachineFunctionPass(ID) {} | ||
|
||
bool runOnMachineFunction(MachineFunction &MF) override; | ||
|
||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
AU.setPreservesCFG(); | ||
MachineFunctionPass::getAnalysisUsage(AU); | ||
} | ||
|
||
StringRef getPassName() const override { return RISCV_OPT_ZBA_NAME; } | ||
}; | ||
|
||
} // end anonymous namespace | ||
|
||
char RISCVOptZba::ID = 0; | ||
INITIALIZE_PASS(RISCVOptZba, DEBUG_TYPE, RISCV_OPT_ZBA_NAME, false, false) | ||
|
||
FunctionPass *llvm::createRISCVOptZbaPass() { return new RISCVOptZba(); } | ||
|
||
static MachineInstr *findShift(Register Reg, const MachineBasicBlock &MBB, | ||
MachineRegisterInfo &MRI) { | ||
if (!Reg.isVirtual()) | ||
return nullptr; | ||
|
||
MachineInstr *Shift = MRI.getVRegDef(Reg); | ||
if (!Shift || Shift->getOpcode() != RISCV::SLLI || | ||
Shift->getParent() != &MBB || !MRI.hasOneNonDBGUse(Reg)) | ||
return nullptr; | ||
|
||
return Shift; | ||
} | ||
|
||
bool RISCVOptZba::runOnMachineFunction(MachineFunction &MF) { | ||
if (skipFunction(MF.getFunction())) | ||
return false; | ||
|
||
MachineRegisterInfo &MRI = MF.getRegInfo(); | ||
const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>(); | ||
const RISCVInstrInfo &TII = *ST.getInstrInfo(); | ||
|
||
if (!ST.hasStdExtZba()) | ||
return false; | ||
|
||
bool MadeChange = true; | ||
|
||
for (MachineBasicBlock &MBB : MF) { | ||
for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) { | ||
unsigned OuterShiftAmt; | ||
switch (MI.getOpcode()) { | ||
default: | ||
continue; | ||
case RISCV::SH1ADD: | ||
OuterShiftAmt = 1; | ||
break; | ||
case RISCV::SH2ADD: | ||
OuterShiftAmt = 2; | ||
break; | ||
case RISCV::SH3ADD: | ||
OuterShiftAmt = 3; | ||
break; | ||
} | ||
|
||
// Second operand must be virtual. | ||
Register UnshiftedReg = MI.getOperand(2).getReg(); | ||
if (!UnshiftedReg.isVirtual()) | ||
continue; | ||
|
||
MachineInstr *Add = MRI.getVRegDef(UnshiftedReg); | ||
if (!Add || Add->getOpcode() != RISCV::ADD || Add->getParent() != &MBB || | ||
!MRI.hasOneNonDBGUse(UnshiftedReg)) | ||
continue; | ||
|
||
Register AddReg0 = Add->getOperand(1).getReg(); | ||
Register AddReg1 = Add->getOperand(2).getReg(); | ||
|
||
MachineInstr *InnerShift; | ||
Register X; | ||
if ((InnerShift = findShift(AddReg0, MBB, MRI))) | ||
X = AddReg1; | ||
else if ((InnerShift = findShift(AddReg1, MBB, MRI))) | ||
X = AddReg0; | ||
else | ||
continue; | ||
|
||
unsigned InnerShiftAmt = InnerShift->getOperand(2).getImm(); | ||
|
||
// The inner shift amount must be at least as large as the outer shift | ||
// amount. | ||
if (OuterShiftAmt > InnerShiftAmt) | ||
continue; | ||
|
||
unsigned InnerOpc; | ||
switch (InnerShiftAmt - OuterShiftAmt) { | ||
default: | ||
continue; | ||
case 0: | ||
InnerOpc = RISCV::ADD; | ||
break; | ||
case 1: | ||
InnerOpc = RISCV::SH1ADD; | ||
break; | ||
case 2: | ||
InnerOpc = RISCV::SH2ADD; | ||
break; | ||
case 3: | ||
InnerOpc = RISCV::SH3ADD; | ||
break; | ||
} | ||
|
||
Register Y = InnerShift->getOperand(1).getReg(); | ||
Register Z = MI.getOperand(1).getReg(); | ||
|
||
Register NewReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); | ||
BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(InnerOpc), NewReg) | ||
.addReg(Y) | ||
.addReg(Z); | ||
BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(MI.getOpcode()), | ||
MI.getOperand(0).getReg()) | ||
.addReg(NewReg) | ||
.addReg(X); | ||
|
||
MI.eraseFromParent(); | ||
Add->eraseFromParent(); | ||
InnerShift->eraseFromParent(); | ||
MadeChange = true; | ||
} | ||
} | ||
|
||
return MadeChange; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we can still reassociate this, but that can be a separate patch. :)
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.
That would have to be a different reassociation of some kind. Do you have an example?