106 using namespace llvm;
108 #define DEBUG_TYPE "mips-branch-expansion" 110 STATISTIC(NumInsertedNops,
"Number of nops inserted");
111 STATISTIC(LongBranches,
"Number of long branches.");
119 cl::desc(
"MIPS: Expand all branches to long format."),
129 bool HasLongBranch =
false;
143 return "Mips Branch Expansion Pass";
159 void expandToLongBranch(MBBInfo &Info);
160 bool handleForbiddenSlot();
161 bool handlePossibleLongBranch();
170 unsigned LongBranchSeqSize;
171 bool ForceLongBranchFirstPass =
false;
179 "Expand out of range branch instructions and prevent forbidden" 185 return new MipsBranchExpansion();
191 Iter
I =
Position,
E = Position->getParent()->end();
193 [](
const Iter &Insn) {
return Insn->isTransient(); });
202 if (Position == Parent->
end()) {
205 if (Succ !=
nullptr && Parent->
isSuccessor(Succ)) {
206 Position = Succ->
begin();
209 return std::make_pair(Position,
true);
211 }
while (Parent->
empty());
215 if (Instr == Parent->
end()) {
218 return std::make_pair(Instr,
false);
238 if (!B->isDebugInstr())
246 ReverseIter End = MBB->
rend();
250 if ((LastBr == End) ||
251 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
258 if ((FirstBr == End) ||
259 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
262 assert(!FirstBr->isIndirectBranch() &&
"Unexpected indirect branch found.");
270 NewMBB->transferSuccessors(MBB);
271 NewMBB->removeSuccessor(Tgt,
true);
276 NewMBB->splice(NewMBB->end(), MBB, LastBr.getReverse(), MBB->
end());
280 void MipsBranchExpansion::initMBBInfo() {
283 for (
auto &MBB : *MFp)
286 MFp->RenumberBlocks();
288 MBBInfos.resize(MFp->size());
290 for (
unsigned I = 0,
E = MBBInfos.size();
I <
E; ++
I) {
296 MBBInfos[
I].
Size +=
TII->getInstSizeInBytes(*
MI);
299 ReverseIter End = MBB->
rend();
302 if ((Br != End) && !Br->isIndirectBranch() &&
303 (Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
304 MBBInfos[
I].Br = &*Br;
309 int64_t MipsBranchExpansion::computeOffset(
const MachineInstr *Br) {
315 if (ThisMBB < TargetMBB) {
316 for (
int N = ThisMBB + 1;
N < TargetMBB; ++
N)
317 Offset += MBBInfos[
N].
Size;
323 for (
int N = ThisMBB;
N >= TargetMBB; --
N)
324 Offset += MBBInfos[
N].
Size;
334 unsigned NewOpc =
TII->getOppositeBranchOpc(Br->getOpcode());
339 for (
unsigned I = 0,
E = Br->getDesc().getNumOperands();
I <
E; ++
I) {
352 if (Br->hasDelaySlot()) {
355 assert(Br->isBundledWithSucc());
359 Br->eraseFromParent();
367 void MipsBranchExpansion::expandToLongBranch(MBBInfo &
I) {
375 MFp->
insert(FallThroughMBB, LongBrMBB);
380 MFp->
insert(FallThroughMBB, BalTgtMBB);
387 const unsigned BalOp =
389 ? STI->inMicroMipsMode() ? Mips::BALC_MMR6 : Mips::BALC
390 : STI->inMicroMipsMode() ? Mips::BAL_BR_MM : Mips::BAL_BR;
422 Pos = LongBrMBB->
begin();
424 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::ADDiu), Mips::SP)
427 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::SW))
448 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
453 BuildMI(*MFp, DL,
TII->get(BalOp)).addMBB(BalTgtMBB);
455 BuildMI(*MFp, DL,
TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
459 if (STI->hasMips32r6()) {
460 LongBrMBB->
insert(Pos, ADDiuInstr);
461 LongBrMBB->
insert(Pos, BalInstr);
463 LongBrMBB->
insert(Pos, BalInstr);
464 LongBrMBB->
insert(Pos, ADDiuInstr);
465 LongBrMBB->
rbegin()->bundleWithPred();
468 Pos = BalTgtMBB->
begin();
470 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::ADDu), Mips::AT)
476 if (STI->isTargetNaCl())
482 if (STI->isTargetNaCl() ||
483 (STI->hasMips32r6() && !STI->useIndirectJumpsHazard()))
484 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::ADDiu), Mips::SP)
488 if (STI->hasMips32r6() && !STI->useIndirectJumpsHazard()) {
489 const unsigned JICOp =
490 STI->inMicroMipsMode() ? Mips::JIC_MMR6 : Mips::JIC;
497 STI->useIndirectJumpsHazard()
498 ? (STI->hasMips32r6() ? Mips::JR_HB_R6 : Mips::JR_HB)
500 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(JROp)).addReg(Mips::AT);
502 if (STI->isTargetNaCl()) {
503 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::NOP));
505 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::ADDiu), Mips::SP)
509 BalTgtMBB->
rbegin()->bundleWithPred();
557 Pos = LongBrMBB->
begin();
559 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DADDiu), Mips::SP_64)
562 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::SD))
566 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::LONG_BRANCH_DADDiu),
568 .addReg(Mips::ZERO_64)
571 BuildMI(*LongBrMBB, Pos, DL,
TII->get(Mips::DSLL), Mips::AT_64)
576 BuildMI(*MFp, DL,
TII->get(BalOp)).addMBB(BalTgtMBB);
578 BuildMI(*MFp, DL,
TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
582 if (STI->hasMips32r6()) {
583 LongBrMBB->
insert(Pos, DADDiuInstr);
584 LongBrMBB->
insert(Pos, BalInstr);
586 LongBrMBB->
insert(Pos, BalInstr);
587 LongBrMBB->
insert(Pos, DADDiuInstr);
588 LongBrMBB->
rbegin()->bundleWithPred();
591 Pos = BalTgtMBB->
begin();
593 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::DADDu), Mips::AT_64)
600 if (STI->hasMips64r6() && !STI->useIndirectJumpsHazard()) {
601 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::DADDiu), Mips::SP_64)
604 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::JIC64))
609 STI->useIndirectJumpsHazard()
610 ? (STI->hasMips32r6() ? Mips::JR_HB64_R6 : Mips::JR_HB64)
612 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(JROp)).addReg(Mips::AT_64);
613 BuildMI(*BalTgtMBB, Pos, DL,
TII->get(Mips::DADDiu), Mips::SP_64)
616 BalTgtMBB->
rbegin()->bundleWithPred();
620 assert(LongBrMBB->
size() + BalTgtMBB->
size() == LongBranchSeqSize);
628 Pos = LongBrMBB->
begin();
630 if (STI->hasMips32r6())
632 TII->get(STI->inMicroMipsMode() ? Mips::BC_MMR6 : Mips::BC))
639 assert(LongBrMBB->
size() == LongBranchSeqSize);
642 if (I.Br->isUnconditionalBranch()) {
644 assert(I.Br->getDesc().getNumOperands() == 1);
645 I.Br->RemoveOperand(0);
649 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
656 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
658 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
664 bool MipsBranchExpansion::handleForbiddenSlot() {
666 if (!STI->hasMips32r6() || STI->inMicroMipsMode())
671 bool Changed =
false;
674 for (Iter I = FI->begin(); I != FI->end(); ++
I) {
681 bool LastInstInFunction =
682 std::next(I) == FI->end() && std::next(FI) == MFp->end();
683 if (!LastInstInFunction) {
685 LastInstInFunction |= Res.second;
692 if (std::next(Iit) == FI->end() ||
693 std::next(Iit)->getOpcode() != Mips::NOP) {
696 BuildMI(*MFp, I->getDebugLoc(), TII->get(Mips::NOP)));
706 bool MipsBranchExpansion::handlePossibleLongBranch() {
708 LongBranchSeqSize = IsPIC ? ((ABI.IsN64() || STI->isTargetNaCl()) ? 10 : 9)
709 : (STI->hasMips32r6() ? 1 : 2);
711 if (STI->inMips16Mode() || !STI->enableLongBranchPass())
720 bool EverMadeChange =
false, MadeChange =
true;
725 for (I = MBBInfos.
begin(); I !=
E; ++
I) {
728 if (!I->Br || I->HasLongBranch)
731 int64_t
Offset = computeOffset(I->Br);
733 if (STI->isTargetNaCl()) {
742 if (!ForceLongBranchFirstPass &&
743 TII->isBranchOffsetInRange(I->Br->getOpcode(),
Offset))
746 I->HasLongBranch =
true;
747 I->
Size += LongBranchSeqSize * 4;
749 EverMadeChange = MadeChange =
true;
753 ForceLongBranchFirstPass =
false;
759 for (I = MBBInfos.
begin(); I !=
E; ++
I)
760 if (I->HasLongBranch) {
761 expandToLongBranch(*I);
764 MFp->RenumberBlocks();
776 if (IsPIC && ABI.IsO32() &&
784 bool longBranchChanged = handlePossibleLongBranch();
785 bool forbiddenSlotChanged = handleForbiddenSlot();
787 bool Changed = longBranchChanged || forbiddenSlotChanged;
790 while (forbiddenSlotChanged) {
791 longBranchChanged = handlePossibleLongBranch();
792 if (!longBranchChanged)
794 forbiddenSlotChanged = handleForbiddenSlot();
void initializeMipsBranchExpansionPass(PassRegistry &)
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.
Compute iterated dominance frontiers using a linear time algorithm.
static MachineBasicBlock * getTargetMBB(const MachineInstr &Br)
Iterate over list of Br's operands and search for a MachineBasicBlock operand.
FunctionPass * createMipsBranchExpansion()
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E)
bool SafeInForbiddenSlot(const MachineInstr &MI) const
Predicate to determine if an instruction can go in a forbidden slot.
static Iter getNextMachineInstrInBB(Iter Position)
STATISTIC(NumFunctions, "Total number of functions")
bool HasForbiddenSlot(const MachineInstr &MI) const
Predicate to determine if an instruction has a forbidden slot.
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
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
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.
INITIALIZE_PASS(MipsBranchExpansion, DEBUG_TYPE, "Expand out of range branch instructions and prevent forbidden" " slot hazards", false, false) FunctionPass *llvm
Returns a pass that clears pipeline hazards.
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()
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")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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)
static const unsigned MIPS_NACL_BUNDLE_ALIGN
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0)
bool isPositionIndependent() const
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned char TargetFlags=0) 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...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
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())
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
const MachineOperand & getOperand(unsigned i) const
static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII)
static bool splitMBB(BlockSplitInfo &BSI)
Splits a MachineBasicBlock to branch before SplitBefore.
Properties which a MachineFunction may have at a given point in time.
Helper class for constructing bundles of MachineInstrs.