105 using namespace llvm;
107 #define DEBUG_TYPE "mips-branch-expansion" 109 STATISTIC(NumInsertedNops,
"Number of nops inserted");
110 STATISTIC(LongBranches,
"Number of long branches.");
118 cl::desc(
"MIPS: Expand all branches to long format."),
128 bool HasLongBranch =
false;
143 return "Mips Branch Expansion Pass";
157 uint64_t computeOffsetFromTheBeginning(
int MBB);
162 void expandToLongBranch(MBBInfo &
Info);
163 bool handleForbiddenSlot();
164 bool handlePossibleLongBranch();
173 bool ForceLongBranchFirstPass =
false;
181 "Expand out of range branch instructions and fix forbidden" 187 return new MipsBranchExpansion();
193 Iter
I =
Position,
E = Position->getParent()->end();
195 [](
const Iter &Insn) {
return Insn->isTransient(); });
204 if (Position == Parent->
end()) {
207 if (Succ !=
nullptr && Parent->
isSuccessor(Succ)) {
208 Position = Succ->
begin();
211 return std::make_pair(Position,
true);
213 }
while (Parent->
empty());
217 if (Instr == Parent->
end()) {
220 return std::make_pair(Instr,
false);
240 if (!B->isDebugInstr())
248 ReverseIter End = MBB->
rend();
252 if ((LastBr == End) ||
253 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
260 if ((FirstBr == End) ||
261 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
264 assert(!FirstBr->isIndirectBranch() &&
"Unexpected indirect branch found.");
272 NewMBB->transferSuccessors(MBB);
274 NewMBB->removeSuccessor(Tgt,
true);
279 NewMBB->splice(NewMBB->end(), MBB, LastBr.getReverse(), MBB->
end());
283 void MipsBranchExpansion::initMBBInfo() {
286 for (
auto &MBB : *MFp)
289 MFp->RenumberBlocks();
291 MBBInfos.resize(MFp->size());
293 for (
unsigned I = 0,
E = MBBInfos.size();
I <
E; ++
I) {
299 MBBInfos[
I].
Size +=
TII->getInstSizeInBytes(*
MI);
304 int64_t MipsBranchExpansion::computeOffset(
const MachineInstr *Br) {
310 if (ThisMBB < TargetMBB) {
311 for (
int N = ThisMBB + 1;
N < TargetMBB; ++
N)
312 Offset += MBBInfos[
N].
Size;
318 for (
int N = ThisMBB;
N >= TargetMBB; --
N)
319 Offset += MBBInfos[
N].
Size;
325 uint64_t MipsBranchExpansion::computeOffsetFromTheBeginning(
int MBB) {
327 for (
int N = 0;
N < MBB; ++
N)
328 Offset += MBBInfos[
N].
Size;
337 unsigned NewOpc =
TII->getOppositeBranchOpc(Br->getOpcode());
342 for (
unsigned I = 0,
E = Br->getDesc().getNumOperands();
I <
E; ++
I) {
355 if (Br->hasDelaySlot()) {
358 assert(Br->isBundledWithSucc());
362 Br->eraseFromParent();
368 bool HasR6 =
ABI.IsN64() ? STI->hasMips64r6() : STI->hasMips32r6();
369 bool AddImm = HasR6 && !STI->useIndirectJumpsHazard();
371 unsigned JR =
ABI.IsN64() ? Mips::JR64 : Mips::JR;
372 unsigned JIC =
ABI.IsN64() ? Mips::JIC64 : Mips::JIC;
373 unsigned JR_HB =
ABI.IsN64() ? Mips::JR_HB64 : Mips::JR_HB;
374 unsigned JR_HB_R6 =
ABI.IsN64() ? Mips::JR_HB64_R6 : Mips::JR_HB_R6;
377 if (STI->useIndirectJumpsHazard())
378 JumpOp = HasR6 ? JR_HB_R6 : JR_HB;
380 JumpOp = HasR6 ? JIC : JR;
382 if (JumpOp == Mips::JIC && STI->inMicroMipsMode())
383 JumpOp = Mips::JIC_MMR6;
385 unsigned ATReg =
ABI.IsN64() ? Mips::AT_64 : Mips::AT;
387 BuildMI(*MBB, Pos, DL,
TII->get(JumpOp)).addReg(ATReg);
399 void MipsBranchExpansion::expandToLongBranch(MBBInfo &
I) {
407 MFp->
insert(FallThroughMBB, LongBrMBB);
412 MFp->
insert(FallThroughMBB, BalTgtMBB);
419 const unsigned BalOp =
421 ? STI->inMicroMipsMode() ? Mips::BALC_MMR6 : Mips::BALC
422 : STI->inMicroMipsMode() ? Mips::BAL_BR_MM : Mips::BAL_BR;
454 Pos = LongBrMBB->
begin();
456 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::ADDiu), Mips::SP)
459 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::SW))
480 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
485 BuildMI(*MFp, DL,
TII->get(BalOp)).addMBB(BalTgtMBB);
487 BuildMI(*MFp, DL,
TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
491 if (STI->hasMips32r6()) {
492 LongBrMBB->
insert(Pos, ADDiuInstr);
493 LongBrMBB->
insert(Pos, BalInstr);
495 LongBrMBB->
insert(Pos, BalInstr);
496 LongBrMBB->
insert(Pos, ADDiuInstr);
497 LongBrMBB->
rbegin()->bundleWithPred();
500 Pos = BalTgtMBB->
begin();
502 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::ADDu), Mips::AT)
508 if (STI->isTargetNaCl())
514 bool hasDelaySlot = buildProperJumpMI(BalTgtMBB, Pos, DL);
516 if (STI->isTargetNaCl() || !hasDelaySlot) {
517 BuildMI(*BalTgtMBB, std::prev(Pos), DL,
TII->get(Mips::ADDiu), Mips::SP)
522 if (STI->isTargetNaCl()) {
523 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::NOP));
525 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::ADDiu), Mips::SP)
529 BalTgtMBB->
rbegin()->bundleWithPred();
577 Pos = LongBrMBB->
begin();
579 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DADDiu), Mips::SP_64)
582 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::SD))
586 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_DADDiu),
588 .addReg(Mips::ZERO_64)
591 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DSLL), Mips::AT_64)
596 BuildMI(*MFp, DL,
TII->get(BalOp)).addMBB(BalTgtMBB);
598 BuildMI(*MFp, DL,
TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
602 if (STI->hasMips32r6()) {
603 LongBrMBB->
insert(Pos, DADDiuInstr);
604 LongBrMBB->
insert(Pos, BalInstr);
606 LongBrMBB->
insert(Pos, BalInstr);
607 LongBrMBB->
insert(Pos, DADDiuInstr);
608 LongBrMBB->
rbegin()->bundleWithPred();
611 Pos = BalTgtMBB->
begin();
613 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::DADDu), Mips::AT_64)
620 bool hasDelaySlot = buildProperJumpMI(BalTgtMBB, Pos, DL);
623 BuildMI(*BalTgtMBB, std::prev(Pos), DL,
TII->get(Mips::DADDiu),
628 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::DADDiu), Mips::SP_64)
631 BalTgtMBB->
rbegin()->bundleWithPred();
635 Pos = LongBrMBB->
begin();
640 uint64_t JOffset = computeOffsetFromTheBeginning(MBB->
getNumber()) +
642 uint64_t TgtMBBOffset = computeOffsetFromTheBeginning(TgtMBB->getNumber());
645 if (JOffset < TgtMBBOffset)
646 TgtMBBOffset += 2 * 4;
648 bool SameSegmentJump = JOffset >> 28 == TgtMBBOffset >> 28;
650 if (STI->hasMips32r6() &&
TII->isBranchOffsetInRange(Mips::BC, I.Offset)) {
657 TII->get(STI->inMicroMipsMode() ? Mips::BC_MMR6 : Mips::BC))
659 }
else if (SameSegmentJump) {
676 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_LUi2Op_64),
679 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_DADDiu2Op),
683 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DSLL), Mips::AT_64)
686 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_DADDiu2Op),
690 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DSLL), Mips::AT_64)
693 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_DADDiu2Op),
698 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_LUi2Op),
701 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_ADDiu2Op),
706 buildProperJumpMI(LongBrMBB, Pos, DL);
710 if (I.Br->isUnconditionalBranch()) {
712 assert(I.Br->getDesc().getNumOperands() == 1);
713 I.Br->RemoveOperand(0);
717 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
724 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
726 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
732 bool MipsBranchExpansion::handleForbiddenSlot() {
734 if (!STI->hasMips32r6() || STI->inMicroMipsMode())
737 bool Changed =
false;
740 for (Iter I = FI->begin(); I != FI->end(); ++
I) {
743 if (!
TII->HasForbiddenSlot(*I))
747 bool LastInstInFunction =
748 std::next(I) == FI->end() && std::next(FI) == MFp->end();
749 if (!LastInstInFunction) {
751 LastInstInFunction |= Res.second;
755 if (LastInstInFunction || !
TII->SafeInForbiddenSlot(*Inst)) {
758 if (std::next(Iit) == FI->end() ||
759 std::next(Iit)->getOpcode() != Mips::NOP) {
762 BuildMI(*MFp, I->getDebugLoc(),
TII->get(Mips::NOP)));
772 bool MipsBranchExpansion::handlePossibleLongBranch() {
773 if (STI->inMips16Mode() || !STI->enableLongBranchPass())
779 bool EverMadeChange =
false, MadeChange =
true;
786 for (
unsigned I = 0,
E = MBBInfos.size(); I <
E; ++
I) {
789 ReverseIter End = MBB->
rend();
792 if ((Br != End) && Br->isBranch() && !Br->isIndirectBranch() &&
793 (Br->isConditionalBranch() ||
794 (Br->isUnconditionalBranch() && IsPIC))) {
795 int64_t
Offset = computeOffset(&*Br);
797 if (STI->isTargetNaCl()) {
805 if (ForceLongBranchFirstPass ||
806 !
TII->isBranchOffsetInRange(Br->getOpcode(),
Offset)) {
808 MBBInfos[
I].Br = &*Br;
813 ForceLongBranchFirstPass =
false;
817 for (I = MBBInfos.
begin(); I !=
E; ++
I) {
823 expandToLongBranch(*I);
825 EverMadeChange = MadeChange =
true;
828 MFp->RenumberBlocks();
831 return EverMadeChange;
841 if (IsPIC &&
ABI.IsO32() &&
849 bool longBranchChanged = handlePossibleLongBranch();
850 bool forbiddenSlotChanged = handleForbiddenSlot();
852 bool Changed = longBranchChanged || forbiddenSlotChanged;
855 while (forbiddenSlotChanged) {
856 longBranchChanged = handlePossibleLongBranch();
857 if (!longBranchChanged)
859 forbiddenSlotChanged = handleForbiddenSlot();
void initializeMipsBranchExpansionPass(PassRegistry &)
static const Align MIPS_NACL_BUNDLE_ALIGN
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
instr_iterator instr_end()
MachineBasicBlock * getMBB() const
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This class represents lattice values for constants.
static MachineBasicBlock * getTargetMBB(const MachineInstr &Br)
Iterate over list of Br's operands and search for a MachineBasicBlock operand.
MO_HIGHER/HIGHEST - Represents the highest or higher half word of a 64-bit symbol address...
FunctionPass * createMipsBranchExpansion()
Describe properties that are true of each instruction in the target description file.
static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E)
static Iter getNextMachineInstrInBB(Iter Position)
STATISTIC(NumFunctions, "Total number of functions")
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
SI optimize exec mask operations pre RA
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Position
Position to insert a new instruction relative to an existing instruction.
MO_ABS_HI/LO - Represents the hi or low part of an absolute symbol address.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
reverse_iterator rbegin()
Analysis containing CSE Info
BasicBlockListType::iterator iterator
static cl::opt< bool > ForceLongBranch("force-mips-long-branch", cl::init(false), cl::desc("MIPS: Expand all branches to long format."), cl::Hidden)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
MachineInstrBundleIterator< MachineInstr > iterator
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
const MachineBasicBlock & front() const
static cl::opt< bool > SkipLongBranch("skip-mips-long-branch", cl::init(false), cl::desc("MIPS: Skip branch expansion pass."), cl::Hidden)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Iterator for intrusive lists based on ilist_node.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static std::pair< Iter, bool > getNextMachineInstr(Iter Position, MachineBasicBlock *Parent)
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
const MachineBasicBlock * getParent() const
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
bool isPositionIndependent() const
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
INITIALIZE_PASS(MipsBranchExpansion, DEBUG_TYPE, "Expand out of range branch instructions and fix forbidden" " slot hazards", false, false) FunctionPass *llvm
Returns a pass that clears pipeline hazards.
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MIBundleBuilder & append(MachineInstr *MI)
Insert MI into MBB by appending it to the instructions in the bundle.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII)
static bool splitMBB(BlockSplitInfo &BSI)
Splits a MachineBasicBlock to branch before SplitBefore.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
Properties which a MachineFunction may have at a given point in time.
Helper class for constructing bundles of MachineInstrs.