blob: cf267c94f5e0cff7377d6e7d1976f3e1771f4a8e [file] [log] [blame]
Akira Hatanakaa2159292012-06-14 01:22:24 +00001//===-- MipsLongBranch.cpp - Emit long branches ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass expands a branch or jump instruction into a long branch if its
11// offset is too large to fit into its immediate field.
12//
Sasa Stankovic7b061a42014-04-30 15:06:25 +000013// FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries.
Akira Hatanakaa2159292012-06-14 01:22:24 +000014//===----------------------------------------------------------------------===//
15
Akira Hatanakaa2159292012-06-14 01:22:24 +000016#include "Mips.h"
Akira Hatanakaa2159292012-06-14 01:22:24 +000017#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic67814262014-06-05 13:52:08 +000018#include "MCTargetDesc/MipsMCNaCl.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000019#include "MipsMachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "MipsTargetMachine.h"
Akira Hatanakaa2159292012-06-14 01:22:24 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Akira Hatanakaa2159292012-06-14 01:22:24 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetRegisterInfo.h"
30
31using namespace llvm;
32
Chandler Carruth84e68b22014-04-22 02:41:26 +000033#define DEBUG_TYPE "mips-long-branch"
34
Akira Hatanakaa2159292012-06-14 01:22:24 +000035STATISTIC(LongBranches, "Number of long branches.");
36
37static cl::opt<bool> SkipLongBranch(
38 "skip-mips-long-branch",
39 cl::init(false),
40 cl::desc("MIPS: Skip long branch pass."),
41 cl::Hidden);
42
43static cl::opt<bool> ForceLongBranch(
44 "force-mips-long-branch",
45 cl::init(false),
46 cl::desc("MIPS: Expand all branches to long format."),
47 cl::Hidden);
48
49namespace {
50 typedef MachineBasicBlock::iterator Iter;
51 typedef MachineBasicBlock::reverse_iterator ReverseIter;
52
53 struct MBBInfo {
Akira Hatanakab5af7122012-08-28 03:03:05 +000054 uint64_t Size, Address;
Akira Hatanakaa2159292012-06-14 01:22:24 +000055 bool HasLongBranch;
56 MachineInstr *Br;
57
Craig Topper062a2ba2014-04-25 05:30:21 +000058 MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {}
Akira Hatanakaa2159292012-06-14 01:22:24 +000059 };
60
61 class MipsLongBranch : public MachineFunctionPass {
62
63 public:
64 static char ID;
65 MipsLongBranch(TargetMachine &tm)
66 : MachineFunctionPass(ID), TM(tm),
Akira Hatanakab5af7122012-08-28 03:03:05 +000067 IsPIC(TM.getRelocationModel() == Reloc::PIC_),
Daniel Sanderse2e25da2014-10-24 16:15:27 +000068 ABI(TM.getSubtarget<MipsSubtarget>().getABI()),
69 LongBranchSeqSize(!IsPIC ? 2 : (ABI.IsN64() ? 10 :
Sasa Stankovic67814262014-06-05 13:52:08 +000070 (!TM.getSubtarget<MipsSubtarget>().isTargetNaCl() ? 9 : 10))) {}
Akira Hatanakaa2159292012-06-14 01:22:24 +000071
Craig Topper56c590a2014-04-29 07:58:02 +000072 const char *getPassName() const override {
Akira Hatanakaa2159292012-06-14 01:22:24 +000073 return "Mips Long Branch";
74 }
75
Craig Topper56c590a2014-04-29 07:58:02 +000076 bool runOnMachineFunction(MachineFunction &F) override;
Akira Hatanakaa2159292012-06-14 01:22:24 +000077
78 private:
79 void splitMBB(MachineBasicBlock *MBB);
80 void initMBBInfo();
81 int64_t computeOffset(const MachineInstr *Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +000082 void replaceBranch(MachineBasicBlock &MBB, Iter Br, DebugLoc DL,
83 MachineBasicBlock *MBBOpnd);
84 void expandToLongBranch(MBBInfo &Info);
85
86 const TargetMachine &TM;
Akira Hatanakaa2159292012-06-14 01:22:24 +000087 MachineFunction *MF;
88 SmallVector<MBBInfo, 16> MBBInfos;
Akira Hatanakab5af7122012-08-28 03:03:05 +000089 bool IsPIC;
Daniel Sanderse2e25da2014-10-24 16:15:27 +000090 MipsABIInfo ABI;
Akira Hatanakab5af7122012-08-28 03:03:05 +000091 unsigned LongBranchSeqSize;
Akira Hatanakaa2159292012-06-14 01:22:24 +000092 };
93
94 char MipsLongBranch::ID = 0;
95} // end of anonymous namespace
96
97/// createMipsLongBranchPass - Returns a pass that converts branches to long
98/// branches.
99FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
100 return new MipsLongBranch(tm);
101}
102
103/// Iterate over list of Br's operands and search for a MachineBasicBlock
104/// operand.
105static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
106 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
107 const MachineOperand &MO = Br.getOperand(I);
108
109 if (MO.isMBB())
110 return MO.getMBB();
111 }
112
113 assert(false && "This instruction does not have an MBB operand.");
Craig Topper062a2ba2014-04-25 05:30:21 +0000114 return nullptr;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000115}
116
117// Traverse the list of instructions backwards until a non-debug instruction is
118// found or it reaches E.
119static ReverseIter getNonDebugInstr(ReverseIter B, ReverseIter E) {
120 for (; B != E; ++B)
121 if (!B->isDebugValue())
122 return B;
123
124 return E;
125}
126
127// Split MBB if it has two direct jumps/branches.
128void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
129 ReverseIter End = MBB->rend();
130 ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
131
132 // Return if MBB has no branch instructions.
133 if ((LastBr == End) ||
134 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
135 return;
136
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000137 ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000138
139 // MBB has only one branch instruction if FirstBr is not a branch
140 // instruction.
141 if ((FirstBr == End) ||
142 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
143 return;
144
145 assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
146
147 // Create a new MBB. Move instructions in MBB to the newly created MBB.
148 MachineBasicBlock *NewMBB =
149 MF->CreateMachineBasicBlock(MBB->getBasicBlock());
150
151 // Insert NewMBB and fix control flow.
152 MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
153 NewMBB->transferSuccessors(MBB);
154 NewMBB->removeSuccessor(Tgt);
155 MBB->addSuccessor(NewMBB);
156 MBB->addSuccessor(Tgt);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000157 MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000158
159 NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end());
160}
161
162// Fill MBBInfos.
163void MipsLongBranch::initMBBInfo() {
164 // Split the MBBs if they have two branches. Each basic block should have at
165 // most one branch after this loop is executed.
166 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E;)
167 splitMBB(I++);
168
169 MF->RenumberBlocks();
170 MBBInfos.clear();
171 MBBInfos.resize(MF->size());
172
Bill Wendlingead89ef2013-06-07 07:04:14 +0000173 const MipsInstrInfo *TII =
Eric Christopherd9134482014-08-04 21:25:23 +0000174 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000175 for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
176 MachineBasicBlock *MBB = MF->getBlockNumbered(I);
177
178 // Compute size of MBB.
179 for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
180 MI != MBB->instr_end(); ++MI)
181 MBBInfos[I].Size += TII->GetInstSizeInBytes(&*MI);
182
183 // Search for MBB's branch instruction.
184 ReverseIter End = MBB->rend();
185 ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
186
187 if ((Br != End) && !Br->isIndirectBranch() &&
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000188 (Br->isConditionalBranch() ||
189 (Br->isUnconditionalBranch() &&
190 TM.getRelocationModel() == Reloc::PIC_)))
Akira Hatanakaa2159292012-06-14 01:22:24 +0000191 MBBInfos[I].Br = (++Br).base();
192 }
193}
194
195// Compute offset of branch in number of bytes.
196int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
197 int64_t Offset = 0;
198 int ThisMBB = Br->getParent()->getNumber();
199 int TargetMBB = getTargetMBB(*Br)->getNumber();
200
201 // Compute offset of a forward branch.
202 if (ThisMBB < TargetMBB) {
203 for (int N = ThisMBB + 1; N < TargetMBB; ++N)
204 Offset += MBBInfos[N].Size;
205
206 return Offset + 4;
207 }
208
209 // Compute offset of a backward branch.
210 for (int N = ThisMBB; N >= TargetMBB; --N)
211 Offset += MBBInfos[N].Size;
212
213 return -Offset + 4;
214}
215
Akira Hatanakaa2159292012-06-14 01:22:24 +0000216// Replace Br with a branch which has the opposite condition code and a
217// MachineBasicBlock operand MBBOpnd.
218void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
219 DebugLoc DL, MachineBasicBlock *MBBOpnd) {
Bill Wendlingead89ef2013-06-07 07:04:14 +0000220 const MipsInstrInfo *TII =
Eric Christopherd9134482014-08-04 21:25:23 +0000221 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo());
Akira Hatanaka067d8152013-05-13 17:43:19 +0000222 unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000223 const MCInstrDesc &NewDesc = TII->get(NewOpc);
224
225 MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
226
227 for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
228 MachineOperand &MO = Br->getOperand(I);
229
230 if (!MO.isReg()) {
231 assert(MO.isMBB() && "MBB operand expected.");
232 break;
233 }
234
235 MIB.addReg(MO.getReg());
236 }
237
238 MIB.addMBB(MBBOpnd);
239
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000240 if (Br->hasDelaySlot()) {
241 // Bundle the instruction in the delay slot to the newly created branch
242 // and erase the original branch.
243 assert(Br->isBundledWithSucc());
244 MachineBasicBlock::instr_iterator II(Br);
245 MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
246 }
Akira Hatanakaa2159292012-06-14 01:22:24 +0000247 Br->eraseFromParent();
248}
249
250// Expand branch instructions to long branches.
251void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000252 MachineBasicBlock::iterator Pos;
253 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000254 DebugLoc DL = I.Br->getDebugLoc();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000255 const BasicBlock *BB = MBB->getBasicBlock();
256 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
257 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000258
Bill Wendlingead89ef2013-06-07 07:04:14 +0000259 const MipsInstrInfo *TII =
Eric Christopherd9134482014-08-04 21:25:23 +0000260 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000261
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000262 MF->insert(FallThroughMBB, LongBrMBB);
263 MBB->removeSuccessor(TgtMBB);
264 MBB->addSuccessor(LongBrMBB);
265
266 if (IsPIC) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000267 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
268 MF->insert(FallThroughMBB, BalTgtMBB);
269 LongBrMBB->addSuccessor(BalTgtMBB);
270 BalTgtMBB->addSuccessor(TgtMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000271
Daniel Sanders86cb3982014-06-13 13:02:52 +0000272 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
273 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
274 // pseudo-instruction wrapping BGEZAL).
275
276 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
277 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
278
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000279 if (!ABI.IsN64()) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000280 // $longbr:
281 // addiu $sp, $sp, -8
282 // sw $ra, 0($sp)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000283 // lui $at, %hi($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000284 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000285 // addiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000286 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000287 // addu $at, $ra, $at
288 // lw $ra, 0($sp)
289 // jr $at
290 // addiu $sp, $sp, 8
291 // $fallthrough:
292 //
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000293
Akira Hatanakab5af7122012-08-28 03:03:05 +0000294 Pos = LongBrMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000295
Akira Hatanakab5af7122012-08-28 03:03:05 +0000296 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
297 .addReg(Mips::SP).addImm(-8);
298 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
299 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000300
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000301 // LUi and ADDiu instructions create 32-bit offset of the target basic
302 // block from the target of BAL instruction. We cannot use immediate
303 // value for this offset because it cannot be determined accurately when
304 // the program has inline assembly statements. We therefore use the
305 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
306 // are resolved during the fixup, so the values will always be correct.
307 //
308 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
309 // expressions at this point (it is possible only at the MC layer),
310 // we replace LUi and ADDiu with pseudo instructions
311 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
312 // blocks as operands to these instructions. When lowering these pseudo
313 // instructions to LUi and ADDiu in the MC layer, we will create
314 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
315 // operands to lowered instructions.
316
317 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
318 .addMBB(TgtMBB).addMBB(BalTgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000319 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000320 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
321 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
322 .addReg(Mips::AT)
323 .addMBB(TgtMBB)
324 .addMBB(BalTgtMBB));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000325
Akira Hatanakab5af7122012-08-28 03:03:05 +0000326 Pos = BalTgtMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000327
Akira Hatanakab5af7122012-08-28 03:03:05 +0000328 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
329 .addReg(Mips::RA).addReg(Mips::AT);
330 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
331 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000332
Sasa Stankovic67814262014-06-05 13:52:08 +0000333 if (!TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) {
334 MIBundleBuilder(*BalTgtMBB, Pos)
335 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT))
336 .append(BuildMI(*MF, DL, TII->get(Mips::ADDiu), Mips::SP)
337 .addReg(Mips::SP).addImm(8));
338 } else {
339 // In NaCl, modifying the sp is not allowed in branch delay slot.
340 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
341 .addReg(Mips::SP).addImm(8);
342
343 MIBundleBuilder(*BalTgtMBB, Pos)
344 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT))
345 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
346
347 // Bundle-align the target of indirect branch JR.
348 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
349 }
Akira Hatanakab5af7122012-08-28 03:03:05 +0000350 } else {
351 // $longbr:
352 // daddiu $sp, $sp, -16
353 // sd $ra, 0($sp)
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000354 // daddiu $at, $zero, %hi($tgt - $baltgt)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000355 // dsll $at, $at, 16
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000356 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000357 // daddiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000358 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000359 // daddu $at, $ra, $at
360 // ld $ra, 0($sp)
361 // jr64 $at
362 // daddiu $sp, $sp, 16
363 // $fallthrough:
364 //
365
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000366 // We assume the branch is within-function, and that offset is within
367 // +/- 2GB. High 32 bits will therefore always be zero.
368
369 // Note that this will work even if the offset is negative, because
370 // of the +1 modification that's added in that case. For example, if the
371 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
372 //
373 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
374 //
375 // and the bits [47:32] are zero. For %highest
376 //
377 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
378 //
379 // and the bits [63:48] are zero.
Akira Hatanakab5af7122012-08-28 03:03:05 +0000380
381 Pos = LongBrMBB->begin();
382
383 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
384 .addReg(Mips::SP_64).addImm(-16);
385 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
386 .addReg(Mips::SP_64).addImm(0);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000387 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000388 Mips::AT_64).addReg(Mips::ZERO_64)
389 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000390 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
391 .addReg(Mips::AT_64).addImm(16);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000392
393 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000394 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
395 .append(
396 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
397 .addReg(Mips::AT_64)
398 .addMBB(TgtMBB, MipsII::MO_ABS_LO)
399 .addMBB(BalTgtMBB));
Akira Hatanakab5af7122012-08-28 03:03:05 +0000400
401 Pos = BalTgtMBB->begin();
402
Akira Hatanakab5af7122012-08-28 03:03:05 +0000403 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
404 .addReg(Mips::RA_64).addReg(Mips::AT_64);
405 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
406 .addReg(Mips::SP_64).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000407
408 MIBundleBuilder(*BalTgtMBB, Pos)
409 .append(BuildMI(*MF, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64))
410 .append(BuildMI(*MF, DL, TII->get(Mips::DADDiu), Mips::SP_64)
411 .addReg(Mips::SP_64).addImm(16));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000412 }
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000413
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000414 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000415 } else {
416 // $longbr:
417 // j $tgt
418 // nop
419 // $fallthrough:
420 //
421 Pos = LongBrMBB->begin();
422 LongBrMBB->addSuccessor(TgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000423 MIBundleBuilder(*LongBrMBB, Pos)
424 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
425 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000426
427 assert(LongBrMBB->size() == LongBranchSeqSize);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000428 }
429
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000430 if (I.Br->isUnconditionalBranch()) {
431 // Change branch destination.
432 assert(I.Br->getDesc().getNumOperands() == 1);
433 I.Br->RemoveOperand(0);
434 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
435 } else
436 // Change branch destination and reverse condition.
437 replaceBranch(*MBB, I.Br, DL, FallThroughMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000438}
439
440static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
441 MachineBasicBlock &MBB = F.front();
442 MachineBasicBlock::iterator I = MBB.begin();
443 DebugLoc DL = MBB.findDebugLoc(MBB.begin());
444 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
445 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
446 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
447 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
448 MBB.removeLiveIn(Mips::V0);
449}
450
451bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
Bill Wendlingead89ef2013-06-07 07:04:14 +0000452 const MipsInstrInfo *TII =
Eric Christopherd9134482014-08-04 21:25:23 +0000453 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000454
Eric Christophera08db01b2014-07-18 20:29:02 +0000455 const MipsSubtarget &STI = TM.getSubtarget<MipsSubtarget>();
456 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
Reed Kotler1595f362013-04-09 19:46:01 +0000457 return false;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000458 if ((TM.getRelocationModel() == Reloc::PIC_) &&
459 TM.getSubtarget<MipsSubtarget>().isABI_O32() &&
460 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
461 emitGPDisp(F, TII);
462
463 if (SkipLongBranch)
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000464 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000465
466 MF = &F;
467 initMBBInfo();
468
Craig Topperaf0dea12013-07-04 01:31:24 +0000469 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000470 bool EverMadeChange = false, MadeChange = true;
471
472 while (MadeChange) {
473 MadeChange = false;
474
475 for (I = MBBInfos.begin(); I != E; ++I) {
476 // Skip if this MBB doesn't have a branch or the branch has already been
477 // converted to a long branch.
478 if (!I->Br || I->HasLongBranch)
479 continue;
480
Zoran Jovanovic9d86e262013-11-30 19:12:28 +0000481 int ShVal = TM.getSubtarget<MipsSubtarget>().inMicroMipsMode() ? 2 : 4;
Sasa Stankovic67814262014-06-05 13:52:08 +0000482 int64_t Offset = computeOffset(I->Br) / ShVal;
483
484 if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) {
485 // The offset calculation does not include sandboxing instructions
486 // that will be added later in the MC layer. Since at this point we
487 // don't know the exact amount of code that "sandboxing" will add, we
488 // conservatively estimate that code will not grow more than 100%.
489 Offset *= 2;
490 }
Zoran Jovanovic9d86e262013-11-30 19:12:28 +0000491
Akira Hatanakab5af7122012-08-28 03:03:05 +0000492 // Check if offset fits into 16-bit immediate field of branches.
Sasa Stankovic67814262014-06-05 13:52:08 +0000493 if (!ForceLongBranch && isInt<16>(Offset))
Akira Hatanakab5af7122012-08-28 03:03:05 +0000494 continue;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000495
Akira Hatanakab5af7122012-08-28 03:03:05 +0000496 I->HasLongBranch = true;
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000497 I->Size += LongBranchSeqSize * 4;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000498 ++LongBranches;
499 EverMadeChange = MadeChange = true;
500 }
501 }
502
Akira Hatanakab5af7122012-08-28 03:03:05 +0000503 if (!EverMadeChange)
504 return true;
505
506 // Compute basic block addresses.
507 if (TM.getRelocationModel() == Reloc::PIC_) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000508 uint64_t Address = 0;
509
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000510 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000511 I->Address = Address;
512 }
513
514 // Do the expansion.
515 for (I = MBBInfos.begin(); I != E; ++I)
516 if (I->HasLongBranch)
517 expandToLongBranch(*I);
518
519 MF->RenumberBlocks();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000520
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000521 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000522}