Skip to content

Commit b1ee4d0

Browse files
authored
[RISCV] Refactor some code in RISCVMergeBaseOffset.NFC (#170839)
1 parent 6d8714b commit b1ee4d0

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,22 @@ INITIALIZE_PASS(RISCVMergeBaseOffsetOpt, DEBUG_TYPE,
8383
// 3) The offset value in the Global Address or Constant Pool is 0.
8484
bool RISCVMergeBaseOffsetOpt::detectFoldable(MachineInstr &Hi,
8585
MachineInstr *&Lo) {
86-
if (Hi.getOpcode() != RISCV::LUI && Hi.getOpcode() != RISCV::AUIPC &&
87-
Hi.getOpcode() != RISCV::PseudoMovAddr)
86+
auto HiOpc = Hi.getOpcode();
87+
if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
88+
HiOpc != RISCV::PseudoMovAddr)
8889
return false;
8990

9091
const MachineOperand &HiOp1 = Hi.getOperand(1);
9192
unsigned ExpectedFlags =
92-
Hi.getOpcode() == RISCV::AUIPC ? RISCVII::MO_PCREL_HI : RISCVII::MO_HI;
93+
HiOpc == RISCV::AUIPC ? RISCVII::MO_PCREL_HI : RISCVII::MO_HI;
9394
if (HiOp1.getTargetFlags() != ExpectedFlags)
9495
return false;
9596

9697
if (!(HiOp1.isGlobal() || HiOp1.isCPI() || HiOp1.isBlockAddress()) ||
9798
HiOp1.getOffset() != 0)
9899
return false;
99100

100-
if (Hi.getOpcode() == RISCV::PseudoMovAddr) {
101+
if (HiOpc == RISCV::PseudoMovAddr) {
101102
// Most of the code should handle it correctly without modification by
102103
// setting Lo and Hi both point to PseudoMovAddr
103104
Lo = &Hi;
@@ -112,13 +113,13 @@ bool RISCVMergeBaseOffsetOpt::detectFoldable(MachineInstr &Hi,
112113
}
113114

114115
const MachineOperand &LoOp2 = Lo->getOperand(2);
115-
if (Hi.getOpcode() == RISCV::LUI || Hi.getOpcode() == RISCV::PseudoMovAddr) {
116+
if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
116117
if (LoOp2.getTargetFlags() != RISCVII::MO_LO ||
117118
!(LoOp2.isGlobal() || LoOp2.isCPI() || LoOp2.isBlockAddress()) ||
118119
LoOp2.getOffset() != 0)
119120
return false;
120121
} else {
121-
assert(Hi.getOpcode() == RISCV::AUIPC);
122+
assert(HiOpc == RISCV::AUIPC);
122123
if (LoOp2.getTargetFlags() != RISCVII::MO_PCREL_LO ||
123124
LoOp2.getType() != MachineOperand::MO_MCSymbol)
124125
return false;
@@ -148,7 +149,8 @@ bool RISCVMergeBaseOffsetOpt::foldOffset(MachineInstr &Hi, MachineInstr &Lo,
148149
// If Hi is an AUIPC, don't fold the offset if it is outside the bounds of
149150
// the global object. The object may be within 2GB of the PC, but addresses
150151
// outside of the object might not be.
151-
if (Hi.getOpcode() == RISCV::AUIPC && Hi.getOperand(1).isGlobal()) {
152+
auto HiOpc = Hi.getOpcode();
153+
if (HiOpc == RISCV::AUIPC && Hi.getOperand(1).isGlobal()) {
152154
const GlobalValue *GV = Hi.getOperand(1).getGlobal();
153155
Type *Ty = GV->getValueType();
154156
if (!Ty->isSized() || Offset < 0 ||
@@ -158,12 +160,13 @@ bool RISCVMergeBaseOffsetOpt::foldOffset(MachineInstr &Hi, MachineInstr &Lo,
158160

159161
// Put the offset back in Hi and the Lo
160162
Hi.getOperand(1).setOffset(Offset);
161-
if (Hi.getOpcode() != RISCV::AUIPC)
163+
if (HiOpc != RISCV::AUIPC)
162164
Lo.getOperand(2).setOffset(Offset);
163165
// Delete the tail instruction.
164-
MRI->constrainRegClass(Lo.getOperand(0).getReg(),
165-
MRI->getRegClass(Tail.getOperand(0).getReg()));
166-
MRI->replaceRegWith(Tail.getOperand(0).getReg(), Lo.getOperand(0).getReg());
166+
Register LoOp0Reg = Lo.getOperand(0).getReg();
167+
Register TailOp0Reg = Tail.getOperand(0).getReg();
168+
MRI->constrainRegClass(LoOp0Reg, MRI->getRegClass(TailOp0Reg));
169+
MRI->replaceRegWith(TailOp0Reg, LoOp0Reg);
167170
Tail.eraseFromParent();
168171
LLVM_DEBUG(dbgs() << " Merged offset " << Offset << " into base.\n"
169172
<< " " << Hi << " " << Lo;);
@@ -204,8 +207,8 @@ bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &Hi,
204207
return false;
205208
// This can point to an ADDI(W) or a LUI:
206209
MachineInstr &OffsetTail = *MRI->getVRegDef(Reg);
207-
if (OffsetTail.getOpcode() == RISCV::ADDI ||
208-
OffsetTail.getOpcode() == RISCV::ADDIW) {
210+
auto OffsetTailOpc = OffsetTail.getOpcode();
211+
if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
209212
// The offset value has non zero bits in both %hi and %lo parts.
210213
// Detect an ADDI that feeds from a LUI instruction.
211214
MachineOperand &AddiImmOp = OffsetTail.getOperand(2);
@@ -232,7 +235,7 @@ bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &Hi,
232235
int64_t Offset = SignExtend64<32>(LuiImmOp.getImm() << 12);
233236
Offset += OffLo;
234237
// RV32 ignores the upper 32 bits. ADDIW sign extends the result.
235-
if (!ST->is64Bit() || OffsetTail.getOpcode() == RISCV::ADDIW)
238+
if (!ST->is64Bit() || OffsetTailOpc == RISCV::ADDIW)
236239
Offset = SignExtend64<32>(Offset);
237240
// We can only fold simm32 offsets.
238241
if (!isInt<32>(Offset))
@@ -244,7 +247,7 @@ bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &Hi,
244247
OffsetTail.eraseFromParent();
245248
OffsetLui.eraseFromParent();
246249
return true;
247-
} else if (OffsetTail.getOpcode() == RISCV::LUI) {
250+
} else if (OffsetTailOpc == RISCV::LUI) {
248251
// The offset value has all zero bits in the lower 12 bits. Only LUI
249252
// exists.
250253
LLVM_DEBUG(dbgs() << " Offset Instr: " << OffsetTail);
@@ -503,14 +506,15 @@ bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &Hi,
503506

504507
Hi.getOperand(1).setOffset(NewOffset);
505508
MachineOperand &ImmOp = Lo.getOperand(2);
509+
auto HiOpc = Hi.getOpcode();
506510
// Expand PseudoMovAddr into LUI
507-
if (Hi.getOpcode() == RISCV::PseudoMovAddr) {
511+
if (HiOpc == RISCV::PseudoMovAddr) {
508512
auto *TII = ST->getInstrInfo();
509513
Hi.setDesc(TII->get(RISCV::LUI));
510514
Hi.removeOperand(2);
511515
}
512516

513-
if (Hi.getOpcode() != RISCV::AUIPC)
517+
if (HiOpc != RISCV::AUIPC)
514518
ImmOp.setOffset(NewOffset);
515519

516520
// Update the immediate in the load/store instructions to add the offset.

0 commit comments

Comments
 (0)