@@ -279,7 +279,7 @@ bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
279
279
280
280
// Handle a single unconditional branch.
281
281
if (NumTerminators == 1 && I->getDesc ().isUnconditionalBranch ()) {
282
- TBB = I-> getOperand ( 0 ). getMBB ( );
282
+ TBB = getBranchDestBlock (*I );
283
283
return false ;
284
284
}
285
285
@@ -293,7 +293,7 @@ bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
293
293
if (NumTerminators == 2 && std::prev (I)->getDesc ().isConditionalBranch () &&
294
294
I->getDesc ().isUnconditionalBranch ()) {
295
295
parseCondBranch (*std::prev (I), TBB, Cond);
296
- FBB = I-> getOperand ( 0 ). getMBB ( );
296
+ FBB = getBranchDestBlock (*I );
297
297
return false ;
298
298
}
299
299
@@ -384,10 +384,6 @@ unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
384
384
385
385
MachineFunction *MF = MBB.getParent ();
386
386
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" );
391
387
392
388
if (!isInt<32 >(BrOffset))
393
389
report_fatal_error (
@@ -399,15 +395,13 @@ unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
399
395
Register ScratchReg = MRI.createVirtualRegister (&RISCV::GPRRegClass);
400
396
auto II = MBB.end ();
401
397
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);
407
401
408
402
RS->enterBasicBlockEnd (MBB);
409
403
unsigned Scav = RS->scavengeRegisterBackwards (RISCV::GPRRegClass,
410
- LuiMI .getIterator (), false , 0 );
404
+ MI .getIterator (), false , 0 );
411
405
MRI.replaceRegWith (ScratchReg, Scav);
412
406
MRI.clearVirtRegs ();
413
407
RS->setRegUsed (Scav);
@@ -431,6 +425,7 @@ RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
431
425
432
426
bool RISCVInstrInfo::isBranchOffsetInRange (unsigned BranchOp,
433
427
int64_t BrOffset) const {
428
+ unsigned XLen = STI.getXLen ();
434
429
// Ideally we could determine the supported branch offset from the
435
430
// RISCVII::FormMask, but this can't be used for Pseudo instructions like
436
431
// PseudoBR.
@@ -447,6 +442,8 @@ bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
447
442
case RISCV::JAL:
448
443
case RISCV::PseudoBR:
449
444
return isIntN (21 , BrOffset);
445
+ case RISCV::PseudoJump:
446
+ return isIntN (32 , SignExtend64 (BrOffset + 0x800 , XLen));
450
447
}
451
448
}
452
449
0 commit comments