1*9880d681SAndroid Build Coastguard Worker //===-- MipsLongBranch.cpp - Emit long branches ---------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass expands a branch or jump instruction into a long branch if its
11*9880d681SAndroid Build Coastguard Worker // offset is too large to fit into its immediate field.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries.
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "Mips.h"
17*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsBaseInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsMCNaCl.h"
19*9880d681SAndroid Build Coastguard Worker #include "MipsMachineFunction.h"
20*9880d681SAndroid Build Coastguard Worker #include "MipsTargetMachine.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "mips-long-branch"
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker STATISTIC(LongBranches, "Number of long branches.");
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> SkipLongBranch(
38*9880d681SAndroid Build Coastguard Worker "skip-mips-long-branch",
39*9880d681SAndroid Build Coastguard Worker cl::init(false),
40*9880d681SAndroid Build Coastguard Worker cl::desc("MIPS: Skip long branch pass."),
41*9880d681SAndroid Build Coastguard Worker cl::Hidden);
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> ForceLongBranch(
44*9880d681SAndroid Build Coastguard Worker "force-mips-long-branch",
45*9880d681SAndroid Build Coastguard Worker cl::init(false),
46*9880d681SAndroid Build Coastguard Worker cl::desc("MIPS: Expand all branches to long format."),
47*9880d681SAndroid Build Coastguard Worker cl::Hidden);
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker namespace {
50*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::iterator Iter;
51*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::reverse_iterator ReverseIter;
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker struct MBBInfo {
54*9880d681SAndroid Build Coastguard Worker uint64_t Size, Address;
55*9880d681SAndroid Build Coastguard Worker bool HasLongBranch;
56*9880d681SAndroid Build Coastguard Worker MachineInstr *Br;
57*9880d681SAndroid Build Coastguard Worker
MBBInfo__anonaa8d4bfd0111::MBBInfo58*9880d681SAndroid Build Coastguard Worker MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {}
59*9880d681SAndroid Build Coastguard Worker };
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker class MipsLongBranch : public MachineFunctionPass {
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker public:
64*9880d681SAndroid Build Coastguard Worker static char ID;
MipsLongBranch(TargetMachine & tm)65*9880d681SAndroid Build Coastguard Worker MipsLongBranch(TargetMachine &tm)
66*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), TM(tm), IsPIC(TM.isPositionIndependent()),
67*9880d681SAndroid Build Coastguard Worker ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {}
68*9880d681SAndroid Build Coastguard Worker
getPassName() const69*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
70*9880d681SAndroid Build Coastguard Worker return "Mips Long Branch";
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &F) override;
74*9880d681SAndroid Build Coastguard Worker
getRequiredProperties() const75*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties getRequiredProperties() const override {
76*9880d681SAndroid Build Coastguard Worker return MachineFunctionProperties().set(
77*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties::Property::AllVRegsAllocated);
78*9880d681SAndroid Build Coastguard Worker }
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker private:
81*9880d681SAndroid Build Coastguard Worker void splitMBB(MachineBasicBlock *MBB);
82*9880d681SAndroid Build Coastguard Worker void initMBBInfo();
83*9880d681SAndroid Build Coastguard Worker int64_t computeOffset(const MachineInstr *Br);
84*9880d681SAndroid Build Coastguard Worker void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
85*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBBOpnd);
86*9880d681SAndroid Build Coastguard Worker void expandToLongBranch(MBBInfo &Info);
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM;
89*9880d681SAndroid Build Coastguard Worker MachineFunction *MF;
90*9880d681SAndroid Build Coastguard Worker SmallVector<MBBInfo, 16> MBBInfos;
91*9880d681SAndroid Build Coastguard Worker bool IsPIC;
92*9880d681SAndroid Build Coastguard Worker MipsABIInfo ABI;
93*9880d681SAndroid Build Coastguard Worker unsigned LongBranchSeqSize;
94*9880d681SAndroid Build Coastguard Worker };
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker char MipsLongBranch::ID = 0;
97*9880d681SAndroid Build Coastguard Worker } // end of anonymous namespace
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard Worker /// createMipsLongBranchPass - Returns a pass that converts branches to long
100*9880d681SAndroid Build Coastguard Worker /// branches.
createMipsLongBranchPass(MipsTargetMachine & tm)101*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
102*9880d681SAndroid Build Coastguard Worker return new MipsLongBranch(tm);
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker /// Iterate over list of Br's operands and search for a MachineBasicBlock
106*9880d681SAndroid Build Coastguard Worker /// operand.
getTargetMBB(const MachineInstr & Br)107*9880d681SAndroid Build Coastguard Worker static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
108*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
109*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = Br.getOperand(I);
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker if (MO.isMBB())
112*9880d681SAndroid Build Coastguard Worker return MO.getMBB();
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker llvm_unreachable("This instruction does not have an MBB operand.");
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker // Traverse the list of instructions backwards until a non-debug instruction is
119*9880d681SAndroid Build Coastguard Worker // found or it reaches E.
getNonDebugInstr(ReverseIter B,const ReverseIter & E)120*9880d681SAndroid Build Coastguard Worker static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) {
121*9880d681SAndroid Build Coastguard Worker for (; B != E; ++B)
122*9880d681SAndroid Build Coastguard Worker if (!B->isDebugValue())
123*9880d681SAndroid Build Coastguard Worker return B;
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker return E;
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker // Split MBB if it has two direct jumps/branches.
splitMBB(MachineBasicBlock * MBB)129*9880d681SAndroid Build Coastguard Worker void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
130*9880d681SAndroid Build Coastguard Worker ReverseIter End = MBB->rend();
131*9880d681SAndroid Build Coastguard Worker ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker // Return if MBB has no branch instructions.
134*9880d681SAndroid Build Coastguard Worker if ((LastBr == End) ||
135*9880d681SAndroid Build Coastguard Worker (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
136*9880d681SAndroid Build Coastguard Worker return;
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // MBB has only one branch instruction if FirstBr is not a branch
141*9880d681SAndroid Build Coastguard Worker // instruction.
142*9880d681SAndroid Build Coastguard Worker if ((FirstBr == End) ||
143*9880d681SAndroid Build Coastguard Worker (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
144*9880d681SAndroid Build Coastguard Worker return;
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker // Create a new MBB. Move instructions in MBB to the newly created MBB.
149*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *NewMBB =
150*9880d681SAndroid Build Coastguard Worker MF->CreateMachineBasicBlock(MBB->getBasicBlock());
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Insert NewMBB and fix control flow.
153*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
154*9880d681SAndroid Build Coastguard Worker NewMBB->transferSuccessors(MBB);
155*9880d681SAndroid Build Coastguard Worker NewMBB->removeSuccessor(Tgt, true);
156*9880d681SAndroid Build Coastguard Worker MBB->addSuccessor(NewMBB);
157*9880d681SAndroid Build Coastguard Worker MBB->addSuccessor(Tgt);
158*9880d681SAndroid Build Coastguard Worker MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end());
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker // Fill MBBInfos.
initMBBInfo()164*9880d681SAndroid Build Coastguard Worker void MipsLongBranch::initMBBInfo() {
165*9880d681SAndroid Build Coastguard Worker // Split the MBBs if they have two branches. Each basic block should have at
166*9880d681SAndroid Build Coastguard Worker // most one branch after this loop is executed.
167*9880d681SAndroid Build Coastguard Worker for (auto &MBB : *MF)
168*9880d681SAndroid Build Coastguard Worker splitMBB(&MBB);
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker MF->RenumberBlocks();
171*9880d681SAndroid Build Coastguard Worker MBBInfos.clear();
172*9880d681SAndroid Build Coastguard Worker MBBInfos.resize(MF->size());
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII =
175*9880d681SAndroid Build Coastguard Worker static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo());
176*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
177*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MF->getBlockNumbered(I);
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker // Compute size of MBB.
180*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
181*9880d681SAndroid Build Coastguard Worker MI != MBB->instr_end(); ++MI)
182*9880d681SAndroid Build Coastguard Worker MBBInfos[I].Size += TII->GetInstSizeInBytes(*MI);
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker // Search for MBB's branch instruction.
185*9880d681SAndroid Build Coastguard Worker ReverseIter End = MBB->rend();
186*9880d681SAndroid Build Coastguard Worker ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker if ((Br != End) && !Br->isIndirectBranch() &&
189*9880d681SAndroid Build Coastguard Worker (Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
190*9880d681SAndroid Build Coastguard Worker MBBInfos[I].Br = &*(++Br).base();
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // Compute offset of branch in number of bytes.
computeOffset(const MachineInstr * Br)195*9880d681SAndroid Build Coastguard Worker int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
196*9880d681SAndroid Build Coastguard Worker int64_t Offset = 0;
197*9880d681SAndroid Build Coastguard Worker int ThisMBB = Br->getParent()->getNumber();
198*9880d681SAndroid Build Coastguard Worker int TargetMBB = getTargetMBB(*Br)->getNumber();
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker // Compute offset of a forward branch.
201*9880d681SAndroid Build Coastguard Worker if (ThisMBB < TargetMBB) {
202*9880d681SAndroid Build Coastguard Worker for (int N = ThisMBB + 1; N < TargetMBB; ++N)
203*9880d681SAndroid Build Coastguard Worker Offset += MBBInfos[N].Size;
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker return Offset + 4;
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker // Compute offset of a backward branch.
209*9880d681SAndroid Build Coastguard Worker for (int N = ThisMBB; N >= TargetMBB; --N)
210*9880d681SAndroid Build Coastguard Worker Offset += MBBInfos[N].Size;
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker return -Offset + 4;
213*9880d681SAndroid Build Coastguard Worker }
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker // Replace Br with a branch which has the opposite condition code and a
216*9880d681SAndroid Build Coastguard Worker // MachineBasicBlock operand MBBOpnd.
replaceBranch(MachineBasicBlock & MBB,Iter Br,const DebugLoc & DL,MachineBasicBlock * MBBOpnd)217*9880d681SAndroid Build Coastguard Worker void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
218*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL,
219*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBBOpnd) {
220*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
221*9880d681SAndroid Build Coastguard Worker MBB.getParent()->getSubtarget().getInstrInfo());
222*9880d681SAndroid Build Coastguard Worker unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
223*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &NewDesc = TII->get(NewOpc);
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
228*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = Br->getOperand(I);
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker if (!MO.isReg()) {
231*9880d681SAndroid Build Coastguard Worker assert(MO.isMBB() && "MBB operand expected.");
232*9880d681SAndroid Build Coastguard Worker break;
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker MIB.addReg(MO.getReg());
236*9880d681SAndroid Build Coastguard Worker }
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker MIB.addMBB(MBBOpnd);
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker if (Br->hasDelaySlot()) {
241*9880d681SAndroid Build Coastguard Worker // Bundle the instruction in the delay slot to the newly created branch
242*9880d681SAndroid Build Coastguard Worker // and erase the original branch.
243*9880d681SAndroid Build Coastguard Worker assert(Br->isBundledWithSucc());
244*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator II = Br.getInstrIterator();
245*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
246*9880d681SAndroid Build Coastguard Worker }
247*9880d681SAndroid Build Coastguard Worker Br->eraseFromParent();
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker
250*9880d681SAndroid Build Coastguard Worker // Expand branch instructions to long branches.
251*9880d681SAndroid Build Coastguard Worker // TODO: This function has to be fixed for beqz16 and bnez16, because it
252*9880d681SAndroid Build Coastguard Worker // currently assumes that all branches have 16-bit offsets, and will produce
253*9880d681SAndroid Build Coastguard Worker // wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
254*9880d681SAndroid Build Coastguard Worker // are present.
expandToLongBranch(MBBInfo & I)255*9880d681SAndroid Build Coastguard Worker void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
256*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator Pos;
257*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
258*9880d681SAndroid Build Coastguard Worker DebugLoc DL = I.Br->getDebugLoc();
259*9880d681SAndroid Build Coastguard Worker const BasicBlock *BB = MBB->getBasicBlock();
260*9880d681SAndroid Build Coastguard Worker MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
261*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
262*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &Subtarget =
263*9880d681SAndroid Build Coastguard Worker static_cast<const MipsSubtarget &>(MF->getSubtarget());
264*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII =
265*9880d681SAndroid Build Coastguard Worker static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker MF->insert(FallThroughMBB, LongBrMBB);
268*9880d681SAndroid Build Coastguard Worker MBB->replaceSuccessor(TgtMBB, LongBrMBB);
269*9880d681SAndroid Build Coastguard Worker
270*9880d681SAndroid Build Coastguard Worker if (IsPIC) {
271*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
272*9880d681SAndroid Build Coastguard Worker MF->insert(FallThroughMBB, BalTgtMBB);
273*9880d681SAndroid Build Coastguard Worker LongBrMBB->addSuccessor(BalTgtMBB);
274*9880d681SAndroid Build Coastguard Worker BalTgtMBB->addSuccessor(TgtMBB);
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
277*9880d681SAndroid Build Coastguard Worker // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
278*9880d681SAndroid Build Coastguard Worker // pseudo-instruction wrapping BGEZAL).
279*9880d681SAndroid Build Coastguard Worker unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker if (!ABI.IsN64()) {
282*9880d681SAndroid Build Coastguard Worker // $longbr:
283*9880d681SAndroid Build Coastguard Worker // addiu $sp, $sp, -8
284*9880d681SAndroid Build Coastguard Worker // sw $ra, 0($sp)
285*9880d681SAndroid Build Coastguard Worker // lui $at, %hi($tgt - $baltgt)
286*9880d681SAndroid Build Coastguard Worker // bal $baltgt
287*9880d681SAndroid Build Coastguard Worker // addiu $at, $at, %lo($tgt - $baltgt)
288*9880d681SAndroid Build Coastguard Worker // $baltgt:
289*9880d681SAndroid Build Coastguard Worker // addu $at, $ra, $at
290*9880d681SAndroid Build Coastguard Worker // lw $ra, 0($sp)
291*9880d681SAndroid Build Coastguard Worker // jr $at
292*9880d681SAndroid Build Coastguard Worker // addiu $sp, $sp, 8
293*9880d681SAndroid Build Coastguard Worker // $fallthrough:
294*9880d681SAndroid Build Coastguard Worker //
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard Worker Pos = LongBrMBB->begin();
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
299*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP).addImm(-8);
300*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
301*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP).addImm(0);
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker // LUi and ADDiu instructions create 32-bit offset of the target basic
304*9880d681SAndroid Build Coastguard Worker // block from the target of BAL instruction. We cannot use immediate
305*9880d681SAndroid Build Coastguard Worker // value for this offset because it cannot be determined accurately when
306*9880d681SAndroid Build Coastguard Worker // the program has inline assembly statements. We therefore use the
307*9880d681SAndroid Build Coastguard Worker // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
308*9880d681SAndroid Build Coastguard Worker // are resolved during the fixup, so the values will always be correct.
309*9880d681SAndroid Build Coastguard Worker //
310*9880d681SAndroid Build Coastguard Worker // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
311*9880d681SAndroid Build Coastguard Worker // expressions at this point (it is possible only at the MC layer),
312*9880d681SAndroid Build Coastguard Worker // we replace LUi and ADDiu with pseudo instructions
313*9880d681SAndroid Build Coastguard Worker // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
314*9880d681SAndroid Build Coastguard Worker // blocks as operands to these instructions. When lowering these pseudo
315*9880d681SAndroid Build Coastguard Worker // instructions to LUi and ADDiu in the MC layer, we will create
316*9880d681SAndroid Build Coastguard Worker // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
317*9880d681SAndroid Build Coastguard Worker // operands to lowered instructions.
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
320*9880d681SAndroid Build Coastguard Worker .addMBB(TgtMBB).addMBB(BalTgtMBB);
321*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(*LongBrMBB, Pos)
322*9880d681SAndroid Build Coastguard Worker .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
323*9880d681SAndroid Build Coastguard Worker .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
324*9880d681SAndroid Build Coastguard Worker .addReg(Mips::AT)
325*9880d681SAndroid Build Coastguard Worker .addMBB(TgtMBB)
326*9880d681SAndroid Build Coastguard Worker .addMBB(BalTgtMBB));
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard Worker Pos = BalTgtMBB->begin();
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
331*9880d681SAndroid Build Coastguard Worker .addReg(Mips::RA).addReg(Mips::AT);
332*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
333*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP).addImm(0);
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // In NaCl, modifying the sp is not allowed in branch delay slot.
336*9880d681SAndroid Build Coastguard Worker if (Subtarget.isTargetNaCl())
337*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
338*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP).addImm(8);
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker if (Subtarget.hasMips32r6())
341*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR))
342*9880d681SAndroid Build Coastguard Worker .addReg(Mips::ZERO).addReg(Mips::AT);
343*9880d681SAndroid Build Coastguard Worker else
344*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker if (Subtarget.isTargetNaCl()) {
347*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
348*9880d681SAndroid Build Coastguard Worker // Bundle-align the target of indirect branch JR.
349*9880d681SAndroid Build Coastguard Worker TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
350*9880d681SAndroid Build Coastguard Worker } else
351*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
352*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP).addImm(8);
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker BalTgtMBB->rbegin()->bundleWithPred();
355*9880d681SAndroid Build Coastguard Worker } else {
356*9880d681SAndroid Build Coastguard Worker // $longbr:
357*9880d681SAndroid Build Coastguard Worker // daddiu $sp, $sp, -16
358*9880d681SAndroid Build Coastguard Worker // sd $ra, 0($sp)
359*9880d681SAndroid Build Coastguard Worker // daddiu $at, $zero, %hi($tgt - $baltgt)
360*9880d681SAndroid Build Coastguard Worker // dsll $at, $at, 16
361*9880d681SAndroid Build Coastguard Worker // bal $baltgt
362*9880d681SAndroid Build Coastguard Worker // daddiu $at, $at, %lo($tgt - $baltgt)
363*9880d681SAndroid Build Coastguard Worker // $baltgt:
364*9880d681SAndroid Build Coastguard Worker // daddu $at, $ra, $at
365*9880d681SAndroid Build Coastguard Worker // ld $ra, 0($sp)
366*9880d681SAndroid Build Coastguard Worker // jr64 $at
367*9880d681SAndroid Build Coastguard Worker // daddiu $sp, $sp, 16
368*9880d681SAndroid Build Coastguard Worker // $fallthrough:
369*9880d681SAndroid Build Coastguard Worker //
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker // We assume the branch is within-function, and that offset is within
372*9880d681SAndroid Build Coastguard Worker // +/- 2GB. High 32 bits will therefore always be zero.
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker // Note that this will work even if the offset is negative, because
375*9880d681SAndroid Build Coastguard Worker // of the +1 modification that's added in that case. For example, if the
376*9880d681SAndroid Build Coastguard Worker // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
377*9880d681SAndroid Build Coastguard Worker //
378*9880d681SAndroid Build Coastguard Worker // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
379*9880d681SAndroid Build Coastguard Worker //
380*9880d681SAndroid Build Coastguard Worker // and the bits [47:32] are zero. For %highest
381*9880d681SAndroid Build Coastguard Worker //
382*9880d681SAndroid Build Coastguard Worker // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
383*9880d681SAndroid Build Coastguard Worker //
384*9880d681SAndroid Build Coastguard Worker // and the bits [63:48] are zero.
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker Pos = LongBrMBB->begin();
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
389*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP_64).addImm(-16);
390*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
391*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP_64).addImm(0);
392*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
393*9880d681SAndroid Build Coastguard Worker Mips::AT_64).addReg(Mips::ZERO_64)
394*9880d681SAndroid Build Coastguard Worker .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
395*9880d681SAndroid Build Coastguard Worker BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
396*9880d681SAndroid Build Coastguard Worker .addReg(Mips::AT_64).addImm(16);
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(*LongBrMBB, Pos)
399*9880d681SAndroid Build Coastguard Worker .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
400*9880d681SAndroid Build Coastguard Worker .append(
401*9880d681SAndroid Build Coastguard Worker BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
402*9880d681SAndroid Build Coastguard Worker .addReg(Mips::AT_64)
403*9880d681SAndroid Build Coastguard Worker .addMBB(TgtMBB, MipsII::MO_ABS_LO)
404*9880d681SAndroid Build Coastguard Worker .addMBB(BalTgtMBB));
405*9880d681SAndroid Build Coastguard Worker
406*9880d681SAndroid Build Coastguard Worker Pos = BalTgtMBB->begin();
407*9880d681SAndroid Build Coastguard Worker
408*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
409*9880d681SAndroid Build Coastguard Worker .addReg(Mips::RA_64).addReg(Mips::AT_64);
410*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
411*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP_64).addImm(0);
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker if (Subtarget.hasMips64r6())
414*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR64))
415*9880d681SAndroid Build Coastguard Worker .addReg(Mips::ZERO_64).addReg(Mips::AT_64);
416*9880d681SAndroid Build Coastguard Worker else
417*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
420*9880d681SAndroid Build Coastguard Worker .addReg(Mips::SP_64).addImm(16);
421*9880d681SAndroid Build Coastguard Worker BalTgtMBB->rbegin()->bundleWithPred();
422*9880d681SAndroid Build Coastguard Worker }
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
425*9880d681SAndroid Build Coastguard Worker } else {
426*9880d681SAndroid Build Coastguard Worker // $longbr:
427*9880d681SAndroid Build Coastguard Worker // j $tgt
428*9880d681SAndroid Build Coastguard Worker // nop
429*9880d681SAndroid Build Coastguard Worker // $fallthrough:
430*9880d681SAndroid Build Coastguard Worker //
431*9880d681SAndroid Build Coastguard Worker Pos = LongBrMBB->begin();
432*9880d681SAndroid Build Coastguard Worker LongBrMBB->addSuccessor(TgtMBB);
433*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(*LongBrMBB, Pos)
434*9880d681SAndroid Build Coastguard Worker .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
435*9880d681SAndroid Build Coastguard Worker .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker assert(LongBrMBB->size() == LongBranchSeqSize);
438*9880d681SAndroid Build Coastguard Worker }
439*9880d681SAndroid Build Coastguard Worker
440*9880d681SAndroid Build Coastguard Worker if (I.Br->isUnconditionalBranch()) {
441*9880d681SAndroid Build Coastguard Worker // Change branch destination.
442*9880d681SAndroid Build Coastguard Worker assert(I.Br->getDesc().getNumOperands() == 1);
443*9880d681SAndroid Build Coastguard Worker I.Br->RemoveOperand(0);
444*9880d681SAndroid Build Coastguard Worker I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
445*9880d681SAndroid Build Coastguard Worker } else
446*9880d681SAndroid Build Coastguard Worker // Change branch destination and reverse condition.
447*9880d681SAndroid Build Coastguard Worker replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker
emitGPDisp(MachineFunction & F,const MipsInstrInfo * TII)450*9880d681SAndroid Build Coastguard Worker static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
451*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = F.front();
452*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.begin();
453*9880d681SAndroid Build Coastguard Worker DebugLoc DL = MBB.findDebugLoc(MBB.begin());
454*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
455*9880d681SAndroid Build Coastguard Worker .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
456*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
457*9880d681SAndroid Build Coastguard Worker .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
458*9880d681SAndroid Build Coastguard Worker MBB.removeLiveIn(Mips::V0);
459*9880d681SAndroid Build Coastguard Worker }
460*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & F)461*9880d681SAndroid Build Coastguard Worker bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
462*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &STI =
463*9880d681SAndroid Build Coastguard Worker static_cast<const MipsSubtarget &>(F.getSubtarget());
464*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII =
465*9880d681SAndroid Build Coastguard Worker static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
466*9880d681SAndroid Build Coastguard Worker LongBranchSeqSize =
467*9880d681SAndroid Build Coastguard Worker !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
468*9880d681SAndroid Build Coastguard Worker
469*9880d681SAndroid Build Coastguard Worker if (STI.inMips16Mode() || !STI.enableLongBranchPass())
470*9880d681SAndroid Build Coastguard Worker return false;
471*9880d681SAndroid Build Coastguard Worker if (IsPIC && static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
472*9880d681SAndroid Build Coastguard Worker F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
473*9880d681SAndroid Build Coastguard Worker emitGPDisp(F, TII);
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard Worker if (SkipLongBranch)
476*9880d681SAndroid Build Coastguard Worker return true;
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker MF = &F;
479*9880d681SAndroid Build Coastguard Worker initMBBInfo();
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
482*9880d681SAndroid Build Coastguard Worker bool EverMadeChange = false, MadeChange = true;
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker while (MadeChange) {
485*9880d681SAndroid Build Coastguard Worker MadeChange = false;
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker for (I = MBBInfos.begin(); I != E; ++I) {
488*9880d681SAndroid Build Coastguard Worker // Skip if this MBB doesn't have a branch or the branch has already been
489*9880d681SAndroid Build Coastguard Worker // converted to a long branch.
490*9880d681SAndroid Build Coastguard Worker if (!I->Br || I->HasLongBranch)
491*9880d681SAndroid Build Coastguard Worker continue;
492*9880d681SAndroid Build Coastguard Worker
493*9880d681SAndroid Build Coastguard Worker int ShVal = STI.inMicroMipsMode() ? 2 : 4;
494*9880d681SAndroid Build Coastguard Worker int64_t Offset = computeOffset(I->Br) / ShVal;
495*9880d681SAndroid Build Coastguard Worker
496*9880d681SAndroid Build Coastguard Worker if (STI.isTargetNaCl()) {
497*9880d681SAndroid Build Coastguard Worker // The offset calculation does not include sandboxing instructions
498*9880d681SAndroid Build Coastguard Worker // that will be added later in the MC layer. Since at this point we
499*9880d681SAndroid Build Coastguard Worker // don't know the exact amount of code that "sandboxing" will add, we
500*9880d681SAndroid Build Coastguard Worker // conservatively estimate that code will not grow more than 100%.
501*9880d681SAndroid Build Coastguard Worker Offset *= 2;
502*9880d681SAndroid Build Coastguard Worker }
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker // Check if offset fits into 16-bit immediate field of branches.
505*9880d681SAndroid Build Coastguard Worker if (!ForceLongBranch && isInt<16>(Offset))
506*9880d681SAndroid Build Coastguard Worker continue;
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker I->HasLongBranch = true;
509*9880d681SAndroid Build Coastguard Worker I->Size += LongBranchSeqSize * 4;
510*9880d681SAndroid Build Coastguard Worker ++LongBranches;
511*9880d681SAndroid Build Coastguard Worker EverMadeChange = MadeChange = true;
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker
515*9880d681SAndroid Build Coastguard Worker if (!EverMadeChange)
516*9880d681SAndroid Build Coastguard Worker return true;
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker // Compute basic block addresses.
519*9880d681SAndroid Build Coastguard Worker if (IsPIC) {
520*9880d681SAndroid Build Coastguard Worker uint64_t Address = 0;
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
523*9880d681SAndroid Build Coastguard Worker I->Address = Address;
524*9880d681SAndroid Build Coastguard Worker }
525*9880d681SAndroid Build Coastguard Worker
526*9880d681SAndroid Build Coastguard Worker // Do the expansion.
527*9880d681SAndroid Build Coastguard Worker for (I = MBBInfos.begin(); I != E; ++I)
528*9880d681SAndroid Build Coastguard Worker if (I->HasLongBranch)
529*9880d681SAndroid Build Coastguard Worker expandToLongBranch(*I);
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker MF->RenumberBlocks();
532*9880d681SAndroid Build Coastguard Worker
533*9880d681SAndroid Build Coastguard Worker return true;
534*9880d681SAndroid Build Coastguard Worker }
535