44 #define DEBUG_TYPE "mips-long-branch"
46 STATISTIC(LongBranches,
"Number of long branches.");
49 "skip-mips-long-branch",
51 cl::desc(
"MIPS: Skip long branch pass."),
55 "force-mips-long-branch",
57 cl::desc(
"MIPS: Expand all branches to long format."),
68 bool HasLongBranch =
false;
81 StringRef getPassName()
const override {
return "Mips Long Branch"; }
96 void expandToLongBranch(MBBInfo &Info);
102 unsigned LongBranchSeqSize;
126 if (!B->isDebugValue())
138 if ((LastBr == End) ||
139 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
146 if ((FirstBr == End) ||
147 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
150 assert(!FirstBr->isIndirectBranch() &&
"Unexpected indirect branch found.");
158 NewMBB->transferSuccessors(MBB);
159 NewMBB->removeSuccessor(Tgt,
true);
164 NewMBB->splice(NewMBB->end(), MBB, LastBr.getReverse(), MBB->
end());
168 void MipsLongBranch::initMBBInfo() {
171 for (
auto &MBB : *MF)
174 MF->RenumberBlocks();
176 MBBInfos.resize(MF->size());
179 static_cast<const MipsInstrInfo *
>(MF->getSubtarget().getInstrInfo());
180 for (
unsigned I = 0,
E = MBBInfos.size();
I <
E; ++
I) {
189 ReverseIter End = MBB->
rend();
192 if ((Br != End) && !Br->isIndirectBranch() &&
193 (Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
194 MBBInfos[
I].Br = &*Br;
199 int64_t MipsLongBranch::computeOffset(
const MachineInstr *Br) {
205 if (ThisMBB < TargetMBB) {
206 for (
int N = ThisMBB + 1;
N < TargetMBB; ++
N)
207 Offset += MBBInfos[
N].
Size;
213 for (
int N = ThisMBB;
N >= TargetMBB; --
N)
214 Offset += MBBInfos[
N].
Size;
231 for (
unsigned I = 0, E = Br->getDesc().getNumOperands();
I <
E; ++
I) {
244 if (Br->hasDelaySlot()) {
247 assert(Br->isBundledWithSucc());
251 Br->eraseFromParent();
259 void MipsLongBranch::expandToLongBranch(MBBInfo &
I) {
271 MF->insert(FallThroughMBB, LongBrMBB);
276 MF->
insert(FallThroughMBB, BalTgtMBB);
283 unsigned BalOp = Subtarget.
hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
300 Pos = LongBrMBB->
begin();
302 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
303 .addReg(Mips::SP).
addImm(-8);
304 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
323 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
324 .addMBB(TgtMBB).
addMBB(BalTgtMBB);
327 .
append(
BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
332 Pos = BalTgtMBB->
begin();
334 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
335 .addReg(Mips::RA).
addReg(Mips::AT);
336 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
337 .addReg(Mips::SP).
addImm(0);
341 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
342 .addReg(Mips::SP).
addImm(8);
345 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR))
346 .addReg(Mips::ZERO).
addReg(Mips::AT);
348 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
351 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
355 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
356 .addReg(Mips::SP).
addImm(8);
358 BalTgtMBB->
rbegin()->bundleWithPred();
390 Pos = LongBrMBB->
begin();
392 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
393 .addReg(Mips::SP_64).
addImm(-16);
394 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
396 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
397 Mips::AT_64).addReg(Mips::ZERO_64)
399 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
400 .addReg(Mips::AT_64).
addImm(16);
405 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
410 Pos = BalTgtMBB->
begin();
412 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
413 .addReg(Mips::RA_64).
addReg(Mips::AT_64);
415 .addReg(Mips::SP_64).
addImm(0);
418 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR64))
419 .addReg(Mips::ZERO_64).
addReg(Mips::AT_64);
421 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
423 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
424 .addReg(Mips::SP_64).
addImm(16);
425 BalTgtMBB->
rbegin()->bundleWithPred();
428 assert(LongBrMBB->
size() + BalTgtMBB->
size() == LongBranchSeqSize);
435 Pos = LongBrMBB->
begin();
441 assert(LongBrMBB->
size() == LongBranchSeqSize);
444 if (I.Br->isUnconditionalBranch()) {
446 assert(I.Br->getDesc().getNumOperands() == 1);
447 I.Br->RemoveOperand(0);
451 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
458 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
460 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
477 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.
isTargetNaCl() ? 9 : 10));
481 if (IsPIC && static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
492 bool EverMadeChange =
false, MadeChange =
true;
497 for (I = MBBInfos.
begin(); I !=
E; ++
I) {
500 if (!I->Br || I->HasLongBranch)
504 int64_t Offset = computeOffset(I->Br) / ShVal;
518 I->HasLongBranch =
true;
519 I->Size += LongBranchSeqSize * 4;
521 EverMadeChange = MadeChange =
true;
532 for (I = MBBInfos.
begin(); I !=
E; Address += I->Size, ++
I)
533 I->Address = Address;
537 for (I = MBBInfos.
begin(); I !=
E; ++
I)
538 if (I->HasLongBranch)
539 expandToLongBranch(*I);
541 MF->RenumberBlocks();
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
Return the number of bytes of code the specified instruction may be.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator instr_begin()
instr_iterator instr_end()
STATISTIC(NumFunctions,"Total number of functions")
MachineBasicBlock * getMBB() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Describe properties that are true of each instruction in the target description file.
const MipsInstrInfo * getInstrInfo() const override
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
constexpr bool isInt< 16 >(int64_t x)
virtual unsigned getOppositeBranchOpc(unsigned Opc) const =0
void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII)
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MO_ABS_HI/LO - Represents the hi or low part of an absolute symbol address.
const MachineBasicBlock & front() const
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
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
const MachineBasicBlock * getParent() const
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)
LLVM Basic Block Representation.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE instructions.
const MachineOperand & getOperand(unsigned i) const
static cl::opt< bool > SkipLongBranch("skip-mips-long-branch", cl::init(false), cl::desc("MIPS: Skip long branch pass."), cl::Hidden)
bool isPositionIndependent() const
static const unsigned End
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.
bool inMicroMipsMode() const
bool inMips16Mode() const
Iterator for intrusive lists based on ilist_node.
bool isTargetNaCl() const
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...
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
typename SuperClass::iterator iterator
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
MachineFunctionProperties & set(Property P)
static const unsigned MIPS_NACL_BUNDLE_ALIGN
Representation of each machine instruction.
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0)
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned char TargetFlags=0) const
static cl::opt< bool > ForceLongBranch("force-mips-long-branch", cl::init(false), cl::desc("MIPS: Expand all branches to long format."), cl::Hidden)
FunctionPass * createMipsLongBranchPass()
createMipsLongBranchPass - Returns a pass that converts branches to long branches.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
bool enableLongBranchPass() const
Instructions::iterator instr_iterator
unsigned getReg() const
getReg - Returns the register number.
MIBundleBuilder & append(MachineInstr *MI)
Insert MI into MBB by appending it to the instructions in the bundle.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E)
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
static MachineBasicBlock * getTargetMBB(const MachineInstr &Br)
Iterate over list of Br's operands and search for a MachineBasicBlock operand.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Properties which a MachineFunction may have at a given point in time.
Helper class for constructing bundles of MachineInstrs.