Skip to content

[RISCV][TII] Add and use new hook to optimize/canonicalize instructions after MachineCopyPropagation #137973

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

Merged
merged 25 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2ad6cc8
[RISCV][TII] Add and use new hook fo optmize/canonicalize instruction…
asb Apr 30, 2025
d23c66c
Add changes missed in first push
asb Apr 30, 2025
5d00e6f
Fix errors in some patterns and remove PACK/PACKW
asb Apr 30, 2025
3048ba9
Use commuteInstruction helper
asb May 1, 2025
7da3f92
Move optimizeInstructoin to after the preprocessor undefs
asb May 1, 2025
109a5ba
(cherry-pick) [RISCV][NFC] Add missed // clang-format on
asb May 1, 2025
a02dc45
Reformat now issue with clang-format being disabled was fixed
asb May 1, 2025
8e73913
Consistently avoid use of pseudoinstructions in comments for transforms
asb May 1, 2025
1c8715d
Pull optimizeInstruction call outside of loop
asb May 1, 2025
825b1f6
Simplify and/mul* case as suggested in review
asb May 1, 2025
9500cee
Re-order instructions in switch
asb May 1, 2025
bb9e2c1
clang-format
asb May 1, 2025
3f2049f
reorder comment to match code logic
asb May 6, 2025
47ca1a0
generalise sltiu check to any non-zero immediate
asb May 6, 2025
ff42dca
Add sll/srl/sra mv-like case
asb May 6, 2025
dc4b2e5
handle sltiu rd, zero, 0
asb May 6, 2025
c765d58
Add suggested comments for 'normalize' pre-transforms
asb May 6, 2025
3ee7da3
Get rid of redundant newline
asb May 6, 2025
436a5e6
Set Changed=true if optimizeInstruction returned true in call from MCP
asb May 7, 2025
e2a1be0
Cover ADD as well
asb May 7, 2025
44f6605
Rename hook to simplifyInstruction
asb May 7, 2025
c93404a
Merge remote-tracking branch 'origin/main' into 2025q2-riscv-optimize…
asb May 8, 2025
19f4dc1
Add comment to .mir file
asb May 8, 2025
d6f8428
Rename test
asb May 8, 2025
920f408
Merge remote-tracking branch 'origin/main' into 2025q2-riscv-optimize…
asb May 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ class TargetInstrInfo : public MCInstrInfo {
return false;
}

/// If possible, converts the instruction to a simplified/canonical form.
/// Returns true if the instruction was modified.
///
/// This function is only called after register allocation. The MI will be
/// modified in place. This is called by passes such as
/// MachineCopyPropagation, where their mutation of the MI operands may
/// expose opportunities to convert the instruction to a simpler form (e.g.
/// a load of 0).
virtual bool simplifyInstruction(MachineInstr &MI) const { return false; }

/// A pair composed of a register and a sub-register index.
/// Used to give some type checking when modeling Reg:SubReg.
struct RegSubRegPair {
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/MachineCopyPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,12 @@ void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
++NumCopyForwards;
Changed = true;
}
// Attempt to canonicalize/optimize the instruction now its arguments have
// been mutated.
if (TII->simplifyInstruction(MI)) {
Changed = true;
LLVM_DEBUG(dbgs() << "MCP: After optimizeInstruction: " << MI);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to set Changed = true here or is guaranteed it was already set earlier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. It's not clear to me that Changed = true will always have been set, so I've added an explicit assignment so it's obvious.

}
}

void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
Expand Down
225 changes: 225 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,21 @@ static unsigned getSHXADDShiftAmount(unsigned Opc) {
}
}

// Returns the shift amount from a SHXADD.UW instruction. Returns 0 if the
// instruction is not a SHXADD.UW.
static unsigned getSHXADDUWShiftAmount(unsigned Opc) {
switch (Opc) {
default:
return 0;
case RISCV::SH1ADD_UW:
return 1;
case RISCV::SH2ADD_UW:
return 2;
case RISCV::SH3ADD_UW:
return 3;
}
}

// Look for opportunities to combine (sh3add Z, (add X, (slli Y, 5))) into
// (sh3add (sh2add Y, Z), X).
static bool getSHXADDPatterns(const MachineInstr &Root,
Expand Down Expand Up @@ -3876,6 +3891,216 @@ MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI,
#undef CASE_VFMA_OPCODE_VV
#undef CASE_VFMA_SPLATS

bool RISCVInstrInfo::simplifyInstruction(MachineInstr &MI) const {
switch (MI.getOpcode()) {
default:
break;
case RISCV::ADD:
case RISCV::OR:
case RISCV::XOR:
// Normalize (so we hit the next if clause).
// add/[x]or rd, zero, rs => add/[x]or rd, rs, zero
if (MI.getOperand(1).getReg() == RISCV::X0)
commuteInstruction(MI);
// add/[x]or rd, rs, zero => addi rd, rs, 0
if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
// xor rd, rs, rs => addi rd, zero, 0
if (MI.getOpcode() == RISCV::XOR &&
MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
MI.getOperand(1).setReg(RISCV::X0);
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::ORI:
case RISCV::XORI:
// [x]ori rd, zero, N => addi rd, zero, N
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SUB:
// sub rd, rs, zero => addi rd, rs, 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does add rd, rs, zero never show up or we just aren't converting it to ADDI?

Copy link
Contributor Author

@asb asb May 7, 2025

Choose a reason for hiding this comment

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

It does show up, but we do have a a CompressPat that produces c.mv for either case. So there's no real difference if C is enabled, and I've been running my script on build directories that have C enabled.

But there's no reason not to handle ADD here, and in the case of a target without the C extension it at least means you'll get a canonical mv in such cases which is nicer to read in disassembly if nothing else. I've gone ahead and added it and a test.

if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SUBW:
// subw rd, rs, zero => addiw rd, rs, 0
if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDIW));
return true;
}
break;
case RISCV::ADDW:
// Normalize (so we hit the next if clause).
// addw rd, zero, rs => addw rd, rs, zero
if (MI.getOperand(1).getReg() == RISCV::X0)
commuteInstruction(MI);
// addw rd, rs, zero => addiw rd, rs, 0
if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDIW));
return true;
}
break;
case RISCV::SH1ADD:
case RISCV::SH1ADD_UW:
case RISCV::SH2ADD:
case RISCV::SH2ADD_UW:
case RISCV::SH3ADD:
case RISCV::SH3ADD_UW:
// shNadd[.uw] rd, zero, rs => addi rd, rs, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.removeOperand(1);
MI.addOperand(MachineOperand::CreateImm(0));
MI.setDesc(get(RISCV::ADDI));
return true;
}
// shNadd[.uw] rd, rs, zero => slli[.uw] rd, rs, N
if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.removeOperand(2);
unsigned Opc = MI.getOpcode();
if (Opc == RISCV::SH1ADD_UW || Opc == RISCV::SH2ADD_UW ||
Opc == RISCV::SH3ADD_UW) {
MI.addOperand(MachineOperand::CreateImm(getSHXADDUWShiftAmount(Opc)));
MI.setDesc(get(RISCV::SLLI_UW));
return true;
}
MI.addOperand(MachineOperand::CreateImm(getSHXADDShiftAmount(Opc)));
MI.setDesc(get(RISCV::SLLI));
return true;
}
break;
case RISCV::AND:
case RISCV::MUL:
case RISCV::MULH:
case RISCV::MULHSU:
case RISCV::MULHU:
case RISCV::MULW:
// and rd, zero, rs => addi rd, zero, 0
// mul* rd, zero, rs => addi rd, zero, 0
// and rd, rs, zero => addi rd, zero, 0
// mul* rd, rs, zero => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0 ||
MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(1).setReg(RISCV::X0);
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::ANDI:
// andi rd, zero, C => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.getOperand(2).setImm(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SLL:
case RISCV::SRL:
case RISCV::SRA:
// shift rd, zero, rs => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
// shift rd, rs, zero => addi rd, rs, 0
if (MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SLLW:
case RISCV::SRLW:
case RISCV::SRAW:
// shiftw rd, zero, rs => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SLLI:
case RISCV::SRLI:
case RISCV::SRAI:
case RISCV::SLLIW:
case RISCV::SRLIW:
case RISCV::SRAIW:
case RISCV::SLLI_UW:
// shiftimm rd, zero, N => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.getOperand(2).setImm(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SLTU:
case RISCV::ADD_UW:
// sltu rd, zero, zero => addi rd, zero, 0
// add.uw rd, zero, zero => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0 &&
MI.getOperand(2).getReg() == RISCV::X0) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
// add.uw rd, zero, rs => addi rd, rs, 0
if (MI.getOpcode() == RISCV::ADD_UW &&
MI.getOperand(1).getReg() == RISCV::X0) {
MI.removeOperand(1);
MI.addOperand(MachineOperand::CreateImm(0));
MI.setDesc(get(RISCV::ADDI));
}
break;
case RISCV::SLTIU:
// sltiu rd, zero, NZC => addi rd, zero, 1
// sltiu rd, zero, 0 => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.getOperand(2).setImm(MI.getOperand(2).getImm() != 0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::SEXT_H:
case RISCV::SEXT_B:
case RISCV::ZEXT_H_RV32:
case RISCV::ZEXT_H_RV64:
// sext.[hb] rd, zero => addi rd, zero, 0
// zext.h rd, zero => addi rd, zero, 0
if (MI.getOperand(1).getReg() == RISCV::X0) {
MI.addOperand(MachineOperand::CreateImm(0));
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
case RISCV::MIN:
case RISCV::MINU:
case RISCV::MAX:
case RISCV::MAXU:
// min|max rd, rs, rs => addi rd, rs, 0
if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
MI.getOperand(2).ChangeToImmediate(0);
MI.setDesc(get(RISCV::ADDI));
return true;
}
break;
}
return false;
}

// clang-format off
#define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \
RISCV::PseudoV##OP##_##LMUL##_TIED
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
unsigned OpIdx1,
unsigned OpIdx2) const override;

bool simplifyInstruction(MachineInstr &MI) const override;

MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
LiveIntervals *LIS) const override;

Expand Down
Loading
Loading