Skip to content

Commit 1a14ce2

Browse files
lenaryzmodem
authored andcommitted
[RISCV] Indirect branch generation in position independent code
This fixes the "Unable to insert indirect branch" fatal error sometimes seen when generating position-independent code. Patch by msizanoen1 Reviewed By: jrtc27 Differential Revision: https://reviews.llvm.org/D84833 (cherry picked from commit 5f9ecc5)
1 parent 709830a commit 1a14ce2

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
279279

280280
// Handle a single unconditional branch.
281281
if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
282-
TBB = I->getOperand(0).getMBB();
282+
TBB = getBranchDestBlock(*I);
283283
return false;
284284
}
285285

@@ -293,7 +293,7 @@ bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
293293
if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
294294
I->getDesc().isUnconditionalBranch()) {
295295
parseCondBranch(*std::prev(I), TBB, Cond);
296-
FBB = I->getOperand(0).getMBB();
296+
FBB = getBranchDestBlock(*I);
297297
return false;
298298
}
299299

@@ -384,10 +384,6 @@ unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
384384

385385
MachineFunction *MF = MBB.getParent();
386386
MachineRegisterInfo &MRI = MF->getRegInfo();
387-
const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
388-
389-
if (TM.isPositionIndependent())
390-
report_fatal_error("Unable to insert indirect branch");
391387

392388
if (!isInt<32>(BrOffset))
393389
report_fatal_error(
@@ -399,15 +395,13 @@ unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
399395
Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
400396
auto II = MBB.end();
401397

402-
MachineInstr &LuiMI = *BuildMI(MBB, II, DL, get(RISCV::LUI), ScratchReg)
403-
.addMBB(&DestBB, RISCVII::MO_HI);
404-
BuildMI(MBB, II, DL, get(RISCV::PseudoBRIND))
405-
.addReg(ScratchReg, RegState::Kill)
406-
.addMBB(&DestBB, RISCVII::MO_LO);
398+
MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
399+
.addReg(ScratchReg, RegState::Define | RegState::Dead)
400+
.addMBB(&DestBB, RISCVII::MO_CALL);
407401

408402
RS->enterBasicBlockEnd(MBB);
409403
unsigned Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass,
410-
LuiMI.getIterator(), false, 0);
404+
MI.getIterator(), false, 0);
411405
MRI.replaceRegWith(ScratchReg, Scav);
412406
MRI.clearVirtRegs();
413407
RS->setRegUsed(Scav);
@@ -431,6 +425,7 @@ RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
431425

432426
bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
433427
int64_t BrOffset) const {
428+
unsigned XLen = STI.getXLen();
434429
// Ideally we could determine the supported branch offset from the
435430
// RISCVII::FormMask, but this can't be used for Pseudo instructions like
436431
// PseudoBR.
@@ -447,6 +442,8 @@ bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
447442
case RISCV::JAL:
448443
case RISCV::PseudoBR:
449444
return isIntN(21, BrOffset);
445+
case RISCV::PseudoJump:
446+
return isIntN(32, SignExtend64(BrOffset + 0x800, XLen));
450447
}
451448
}
452449

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,8 @@ def : Pat<(riscv_tail (iPTR tglobaladdr:$dst)),
10121012
def : Pat<(riscv_tail (iPTR texternalsym:$dst)),
10131013
(PseudoTAIL texternalsym:$dst)>;
10141014

1015-
let isCall = 0, isBarrier = 0, isCodeGenOnly = 0, hasSideEffects = 0,
1016-
mayStore = 0, mayLoad = 0 in
1015+
let isCall = 0, isBarrier = 1, isBranch = 1, isTerminator = 1,
1016+
isCodeGenOnly = 0, hasSideEffects = 0, mayStore = 0, mayLoad = 0 in
10171017
def PseudoJump : Pseudo<(outs GPR:$rd), (ins pseudo_jump_symbol:$target), []> {
10181018
let AsmString = "jump\t$target, $rd";
10191019
}

llvm/test/CodeGen/RISCV/branch-relaxation.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -mtriple=riscv32 -verify-machineinstrs -filetype=obj < %s \
33
; RUN: -o /dev/null 2>&1
4+
; RUN: llc -mtriple=riscv32 -relocation-model=pic -verify-machineinstrs \
5+
; RUN: -filetype=obj < %s -o /dev/null 2>&1
46
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s | FileCheck %s
7+
; RUN: llc -mtriple=riscv32 -relocation-model=pic -verify-machineinstrs < %s \
8+
; RUN: | FileCheck %s
59

610
define void @relax_bcc(i1 %a) nounwind {
711
; CHECK-LABEL: relax_bcc:
@@ -25,15 +29,13 @@ tail:
2529
ret void
2630
}
2731

28-
; TODO: Extend simm12's MCOperandPredicate so the jalr zero is printed as a jr.
2932
define i32 @relax_jal(i1 %a) nounwind {
3033
; CHECK-LABEL: relax_jal:
3134
; CHECK: # %bb.0:
3235
; CHECK-NEXT: andi a0, a0, 1
3336
; CHECK-NEXT: bnez a0, .LBB1_1
3437
; CHECK-NEXT: # %bb.3:
35-
; CHECK-NEXT: lui a0, %hi(.LBB1_2)
36-
; CHECK-NEXT: jalr zero, %lo(.LBB1_2)(a0)
38+
; CHECK-NEXT: jump .LBB1_2, a0
3739
; CHECK-NEXT: .LBB1_1: # %iftrue
3840
; CHECK-NEXT: #APP
3941
; CHECK-NEXT: #NO_APP

0 commit comments

Comments
 (0)