Skip to content

Commit 4a2db23

Browse files
authored
[RISCV][GISel] Use setDesc in some cases instead of creating new instructions. (#72769)
Slight memory usage improvement by reusing.
1 parent 56c72c7 commit 4a2db23

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ bool RISCVInstructionSelector::selectAddr(MachineInstr &MI,
780780

781781
Register DefReg = MI.getOperand(0).getReg();
782782
const LLT DefTy = MRI.getType(DefReg);
783-
MachineInstr *Result = nullptr;
784783

785784
// When HWASAN is used and tagging of global variables is enabled
786785
// they should be accessed via the GOT, since the tagged address of a global
@@ -791,25 +790,25 @@ bool RISCVInstructionSelector::selectAddr(MachineInstr &MI,
791790
// Use PC-relative addressing to access the symbol. This generates the
792791
// pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym))
793792
// %pcrel_lo(auipc)).
794-
Result =
795-
MIB.buildInstr(RISCV::PseudoLLA, {DefReg}, {}).addDisp(DispMO, 0);
796-
} else {
797-
// Use PC-relative addressing to access the GOT for this symbol, then
798-
// load the address from the GOT. This generates the pattern (PseudoLGA
799-
// sym), which expands to (ld (addi (auipc %got_pcrel_hi(sym))
800-
// %pcrel_lo(auipc))).
801-
MachineFunction &MF = *MI.getParent()->getParent();
802-
MachineMemOperand *MemOp = MF.getMachineMemOperand(
803-
MachinePointerInfo::getGOT(MF),
804-
MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
805-
MachineMemOperand::MOInvariant,
806-
DefTy, Align(DefTy.getSizeInBits() / 8));
807-
808-
Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {})
809-
.addDisp(DispMO, 0)
810-
.addMemOperand(MemOp);
793+
MI.setDesc(TII.get(RISCV::PseudoLLA));
794+
return constrainSelectedInstRegOperands(MI, TII, TRI, RBI);
811795
}
812796

797+
// Use PC-relative addressing to access the GOT for this symbol, then
798+
// load the address from the GOT. This generates the pattern (PseudoLGA
799+
// sym), which expands to (ld (addi (auipc %got_pcrel_hi(sym))
800+
// %pcrel_lo(auipc))).
801+
MachineFunction &MF = *MI.getParent()->getParent();
802+
MachineMemOperand *MemOp = MF.getMachineMemOperand(
803+
MachinePointerInfo::getGOT(MF),
804+
MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
805+
MachineMemOperand::MOInvariant,
806+
DefTy, Align(DefTy.getSizeInBits() / 8));
807+
808+
auto Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {})
809+
.addDisp(DispMO, 0)
810+
.addMemOperand(MemOp);
811+
813812
if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI))
814813
return false;
815814

@@ -834,8 +833,8 @@ bool RISCVInstructionSelector::selectAddr(MachineInstr &MI,
834833
if (!constrainSelectedInstRegOperands(*AddrHi, TII, TRI, RBI))
835834
return false;
836835

837-
Result = MIB.buildInstr(RISCV::ADDI, {DefReg}, {AddrHiDest})
838-
.addDisp(DispMO, 0, RISCVII::MO_LO);
836+
auto Result = MIB.buildInstr(RISCV::ADDI, {DefReg}, {AddrHiDest})
837+
.addDisp(DispMO, 0, RISCVII::MO_LO);
839838

840839
if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI))
841840
return false;
@@ -860,22 +859,22 @@ bool RISCVInstructionSelector::selectAddr(MachineInstr &MI,
860859
MachineMemOperand::MOInvariant,
861860
DefTy, Align(DefTy.getSizeInBits() / 8));
862861

863-
Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {})
864-
.addDisp(DispMO, 0)
865-
.addMemOperand(MemOp);
866-
} else {
867-
// Generate a sequence for accessing addresses within any 2GiB range
868-
// within the address space. This generates the pattern (PseudoLLA sym),
869-
// which expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
870-
Result =
871-
MIB.buildInstr(RISCV::PseudoLLA, {DefReg}, {}).addDisp(DispMO, 0);
872-
}
862+
auto Result = MIB.buildInstr(RISCV::PseudoLGA, {DefReg}, {})
863+
.addDisp(DispMO, 0)
864+
.addMemOperand(MemOp);
873865

874-
if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI))
875-
return false;
866+
if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI))
867+
return false;
876868

877-
MI.eraseFromParent();
878-
return true;
869+
MI.eraseFromParent();
870+
return true;
871+
}
872+
873+
// Generate a sequence for accessing addresses within any 2GiB range
874+
// within the address space. This generates the pattern (PseudoLLA sym),
875+
// which expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
876+
MI.setDesc(TII.get(RISCV::PseudoLLA));
877+
return constrainSelectedInstRegOperands(MI, TII, TRI, RBI);
879878
}
880879

881880
return false;

0 commit comments

Comments
 (0)