blob: e8cb45ecca3a90d79de9f9e909f9c6365aa5073f [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)
Eric Christophera5762812015-01-26 17:33:46 +000066 : MachineFunctionPass(ID), TM(tm),
67 IsPIC(TM.getRelocationModel() == Reloc::PIC_),
Eric Christopher96e72c62015-01-29 23:27:36 +000068 ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {}
Akira Hatanakaa2159292012-06-14 01:22:24 +000069
Craig Topper56c590a2014-04-29 07:58:02 +000070 const char *getPassName() const override {
Akira Hatanakaa2159292012-06-14 01:22:24 +000071 return "Mips Long Branch";
72 }
73
Craig Topper56c590a2014-04-29 07:58:02 +000074 bool runOnMachineFunction(MachineFunction &F) override;
Akira Hatanakaa2159292012-06-14 01:22:24 +000075
Derek Schuff1dbf7a52016-04-04 17:09:25 +000076 MachineFunctionProperties getRequiredProperties() const override {
77 return MachineFunctionProperties().set(
78 MachineFunctionProperties::Property::AllVRegsAllocated);
79 }
80
Akira Hatanakaa2159292012-06-14 01:22:24 +000081 private:
82 void splitMBB(MachineBasicBlock *MBB);
83 void initMBBInfo();
84 int64_t computeOffset(const MachineInstr *Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +000085 void replaceBranch(MachineBasicBlock &MBB, Iter Br, DebugLoc DL,
86 MachineBasicBlock *MBBOpnd);
87 void expandToLongBranch(MBBInfo &Info);
88
89 const TargetMachine &TM;
Akira Hatanakaa2159292012-06-14 01:22:24 +000090 MachineFunction *MF;
91 SmallVector<MBBInfo, 16> MBBInfos;
Akira Hatanakab5af7122012-08-28 03:03:05 +000092 bool IsPIC;
Daniel Sanderse2e25da2014-10-24 16:15:27 +000093 MipsABIInfo ABI;
Akira Hatanakab5af7122012-08-28 03:03:05 +000094 unsigned LongBranchSeqSize;
Akira Hatanakaa2159292012-06-14 01:22:24 +000095 };
96
97 char MipsLongBranch::ID = 0;
98} // end of anonymous namespace
99
100/// createMipsLongBranchPass - Returns a pass that converts branches to long
101/// branches.
102FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
103 return new MipsLongBranch(tm);
104}
105
106/// Iterate over list of Br's operands and search for a MachineBasicBlock
107/// operand.
108static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
109 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
110 const MachineOperand &MO = Br.getOperand(I);
111
112 if (MO.isMBB())
113 return MO.getMBB();
114 }
115
Craig Topperd3c02f12015-01-05 10:15:49 +0000116 llvm_unreachable("This instruction does not have an MBB operand.");
Akira Hatanakaa2159292012-06-14 01:22:24 +0000117}
118
119// Traverse the list of instructions backwards until a non-debug instruction is
120// found or it reaches E.
121static ReverseIter getNonDebugInstr(ReverseIter B, ReverseIter E) {
122 for (; B != E; ++B)
123 if (!B->isDebugValue())
124 return B;
125
126 return E;
127}
128
129// Split MBB if it has two direct jumps/branches.
130void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
131 ReverseIter End = MBB->rend();
132 ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
133
134 // Return if MBB has no branch instructions.
135 if ((LastBr == End) ||
136 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
137 return;
138
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000139 ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000140
141 // MBB has only one branch instruction if FirstBr is not a branch
142 // instruction.
143 if ((FirstBr == End) ||
144 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
145 return;
146
147 assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
148
149 // Create a new MBB. Move instructions in MBB to the newly created MBB.
150 MachineBasicBlock *NewMBB =
151 MF->CreateMachineBasicBlock(MBB->getBasicBlock());
152
153 // Insert NewMBB and fix control flow.
154 MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
155 NewMBB->transferSuccessors(MBB);
Cong Houc1069892015-12-13 09:26:17 +0000156 NewMBB->removeSuccessor(Tgt, true);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000157 MBB->addSuccessor(NewMBB);
158 MBB->addSuccessor(Tgt);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000159 MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000160
161 NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end());
162}
163
164// Fill MBBInfos.
165void MipsLongBranch::initMBBInfo() {
166 // Split the MBBs if they have two branches. Each basic block should have at
167 // most one branch after this loop is executed.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +0000168 for (auto &MBB : *MF)
169 splitMBB(&MBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000170
171 MF->RenumberBlocks();
172 MBBInfos.clear();
173 MBBInfos.resize(MF->size());
174
Bill Wendlingead89ef2013-06-07 07:04:14 +0000175 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000176 static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000177 for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
178 MachineBasicBlock *MBB = MF->getBlockNumbered(I);
179
180 // Compute size of MBB.
181 for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
182 MI != MBB->instr_end(); ++MI)
183 MBBInfos[I].Size += TII->GetInstSizeInBytes(&*MI);
184
185 // Search for MBB's branch instruction.
186 ReverseIter End = MBB->rend();
187 ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
188
189 if ((Br != End) && !Br->isIndirectBranch() &&
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000190 (Br->isConditionalBranch() ||
191 (Br->isUnconditionalBranch() &&
192 TM.getRelocationModel() == Reloc::PIC_)))
Akira Hatanakaa2159292012-06-14 01:22:24 +0000193 MBBInfos[I].Br = (++Br).base();
194 }
195}
196
197// Compute offset of branch in number of bytes.
198int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
199 int64_t Offset = 0;
200 int ThisMBB = Br->getParent()->getNumber();
201 int TargetMBB = getTargetMBB(*Br)->getNumber();
202
203 // Compute offset of a forward branch.
204 if (ThisMBB < TargetMBB) {
205 for (int N = ThisMBB + 1; N < TargetMBB; ++N)
206 Offset += MBBInfos[N].Size;
207
208 return Offset + 4;
209 }
210
211 // Compute offset of a backward branch.
212 for (int N = ThisMBB; N >= TargetMBB; --N)
213 Offset += MBBInfos[N].Size;
214
215 return -Offset + 4;
216}
217
Akira Hatanakaa2159292012-06-14 01:22:24 +0000218// Replace Br with a branch which has the opposite condition code and a
219// MachineBasicBlock operand MBBOpnd.
220void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
221 DebugLoc DL, MachineBasicBlock *MBBOpnd) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000222 const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
223 MBB.getParent()->getSubtarget().getInstrInfo());
Akira Hatanaka067d8152013-05-13 17:43:19 +0000224 unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000225 const MCInstrDesc &NewDesc = TII->get(NewOpc);
226
227 MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
228
229 for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
230 MachineOperand &MO = Br->getOperand(I);
231
232 if (!MO.isReg()) {
233 assert(MO.isMBB() && "MBB operand expected.");
234 break;
235 }
236
237 MIB.addReg(MO.getReg());
238 }
239
240 MIB.addMBB(MBBOpnd);
241
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000242 if (Br->hasDelaySlot()) {
243 // Bundle the instruction in the delay slot to the newly created branch
244 // and erase the original branch.
245 assert(Br->isBundledWithSucc());
246 MachineBasicBlock::instr_iterator II(Br);
247 MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
248 }
Akira Hatanakaa2159292012-06-14 01:22:24 +0000249 Br->eraseFromParent();
250}
251
252// Expand branch instructions to long branches.
Jozef Kolek9761e962015-01-12 12:03:34 +0000253// TODO: This function has to be fixed for beqz16 and bnez16, because it
254// currently assumes that all branches have 16-bit offsets, and will produce
255// wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
256// are present.
Akira Hatanakaa2159292012-06-14 01:22:24 +0000257void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000258 MachineBasicBlock::iterator Pos;
259 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000260 DebugLoc DL = I.Br->getDebugLoc();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000261 const BasicBlock *BB = MBB->getBasicBlock();
262 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
263 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
Eric Christopher96e72c62015-01-29 23:27:36 +0000264 const MipsSubtarget &Subtarget =
265 static_cast<const MipsSubtarget &>(MF->getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000266 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000267 static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000268
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000269 MF->insert(FallThroughMBB, LongBrMBB);
Cong Houd97c1002015-12-01 05:29:22 +0000270 MBB->replaceSuccessor(TgtMBB, LongBrMBB);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000271
272 if (IsPIC) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000273 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
274 MF->insert(FallThroughMBB, BalTgtMBB);
275 LongBrMBB->addSuccessor(BalTgtMBB);
276 BalTgtMBB->addSuccessor(TgtMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000277
Daniel Sanders86cb3982014-06-13 13:02:52 +0000278 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
279 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
280 // pseudo-instruction wrapping BGEZAL).
Daniel Sanders86cb3982014-06-13 13:02:52 +0000281 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
282
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000283 if (!ABI.IsN64()) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000284 // $longbr:
285 // addiu $sp, $sp, -8
286 // sw $ra, 0($sp)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000287 // lui $at, %hi($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000288 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000289 // addiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000290 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000291 // addu $at, $ra, $at
292 // lw $ra, 0($sp)
293 // jr $at
294 // addiu $sp, $sp, 8
295 // $fallthrough:
296 //
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000297
Akira Hatanakab5af7122012-08-28 03:03:05 +0000298 Pos = LongBrMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000299
Akira Hatanakab5af7122012-08-28 03:03:05 +0000300 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
301 .addReg(Mips::SP).addImm(-8);
302 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
303 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000304
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000305 // LUi and ADDiu instructions create 32-bit offset of the target basic
306 // block from the target of BAL instruction. We cannot use immediate
307 // value for this offset because it cannot be determined accurately when
308 // the program has inline assembly statements. We therefore use the
309 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
310 // are resolved during the fixup, so the values will always be correct.
311 //
312 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
313 // expressions at this point (it is possible only at the MC layer),
314 // we replace LUi and ADDiu with pseudo instructions
315 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
316 // blocks as operands to these instructions. When lowering these pseudo
317 // instructions to LUi and ADDiu in the MC layer, we will create
318 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
319 // operands to lowered instructions.
320
321 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
322 .addMBB(TgtMBB).addMBB(BalTgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000323 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000324 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
325 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
326 .addReg(Mips::AT)
327 .addMBB(TgtMBB)
328 .addMBB(BalTgtMBB));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000329
Akira Hatanakab5af7122012-08-28 03:03:05 +0000330 Pos = BalTgtMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000331
Akira Hatanakab5af7122012-08-28 03:03:05 +0000332 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
333 .addReg(Mips::RA).addReg(Mips::AT);
334 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
335 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000336
Eric Christopher96e72c62015-01-29 23:27:36 +0000337 if (!Subtarget.isTargetNaCl()) {
Sasa Stankovic67814262014-06-05 13:52:08 +0000338 MIBundleBuilder(*BalTgtMBB, Pos)
339 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT))
340 .append(BuildMI(*MF, DL, TII->get(Mips::ADDiu), Mips::SP)
341 .addReg(Mips::SP).addImm(8));
342 } else {
343 // In NaCl, modifying the sp is not allowed in branch delay slot.
344 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
345 .addReg(Mips::SP).addImm(8);
346
347 MIBundleBuilder(*BalTgtMBB, Pos)
348 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT))
349 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
350
351 // Bundle-align the target of indirect branch JR.
352 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
353 }
Akira Hatanakab5af7122012-08-28 03:03:05 +0000354 } else {
355 // $longbr:
356 // daddiu $sp, $sp, -16
357 // sd $ra, 0($sp)
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000358 // daddiu $at, $zero, %hi($tgt - $baltgt)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000359 // dsll $at, $at, 16
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000360 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000361 // daddiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000362 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000363 // daddu $at, $ra, $at
364 // ld $ra, 0($sp)
365 // jr64 $at
366 // daddiu $sp, $sp, 16
367 // $fallthrough:
368 //
369
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000370 // We assume the branch is within-function, and that offset is within
371 // +/- 2GB. High 32 bits will therefore always be zero.
372
373 // Note that this will work even if the offset is negative, because
374 // of the +1 modification that's added in that case. For example, if the
375 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
376 //
377 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
378 //
379 // and the bits [47:32] are zero. For %highest
380 //
381 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
382 //
383 // and the bits [63:48] are zero.
Akira Hatanakab5af7122012-08-28 03:03:05 +0000384
385 Pos = LongBrMBB->begin();
386
387 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
388 .addReg(Mips::SP_64).addImm(-16);
389 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
390 .addReg(Mips::SP_64).addImm(0);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000391 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000392 Mips::AT_64).addReg(Mips::ZERO_64)
393 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000394 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
395 .addReg(Mips::AT_64).addImm(16);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000396
397 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000398 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
399 .append(
400 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
401 .addReg(Mips::AT_64)
402 .addMBB(TgtMBB, MipsII::MO_ABS_LO)
403 .addMBB(BalTgtMBB));
Akira Hatanakab5af7122012-08-28 03:03:05 +0000404
405 Pos = BalTgtMBB->begin();
406
Akira Hatanakab5af7122012-08-28 03:03:05 +0000407 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
408 .addReg(Mips::RA_64).addReg(Mips::AT_64);
409 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
410 .addReg(Mips::SP_64).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000411
412 MIBundleBuilder(*BalTgtMBB, Pos)
413 .append(BuildMI(*MF, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64))
414 .append(BuildMI(*MF, DL, TII->get(Mips::DADDiu), Mips::SP_64)
415 .addReg(Mips::SP_64).addImm(16));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000416 }
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000417
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000418 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000419 } else {
420 // $longbr:
421 // j $tgt
422 // nop
423 // $fallthrough:
424 //
425 Pos = LongBrMBB->begin();
426 LongBrMBB->addSuccessor(TgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000427 MIBundleBuilder(*LongBrMBB, Pos)
428 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
429 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000430
431 assert(LongBrMBB->size() == LongBranchSeqSize);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000432 }
433
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000434 if (I.Br->isUnconditionalBranch()) {
435 // Change branch destination.
436 assert(I.Br->getDesc().getNumOperands() == 1);
437 I.Br->RemoveOperand(0);
438 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
439 } else
440 // Change branch destination and reverse condition.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000441 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000442}
443
444static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
445 MachineBasicBlock &MBB = F.front();
446 MachineBasicBlock::iterator I = MBB.begin();
447 DebugLoc DL = MBB.findDebugLoc(MBB.begin());
448 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
449 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
450 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
451 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
452 MBB.removeLiveIn(Mips::V0);
453}
454
455bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000456 const MipsSubtarget &STI =
457 static_cast<const MipsSubtarget &>(F.getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000458 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000459 static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
460 LongBranchSeqSize =
461 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
Bill Wendlingead89ef2013-06-07 07:04:14 +0000462
Eric Christophera08db01b2014-07-18 20:29:02 +0000463 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
Reed Kotler1595f362013-04-09 19:46:01 +0000464 return false;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000465 if ((TM.getRelocationModel() == Reloc::PIC_) &&
Eric Christopher96e72c62015-01-29 23:27:36 +0000466 static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
Akira Hatanakaa2159292012-06-14 01:22:24 +0000467 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
468 emitGPDisp(F, TII);
469
470 if (SkipLongBranch)
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000471 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000472
473 MF = &F;
474 initMBBInfo();
475
Craig Topperaf0dea12013-07-04 01:31:24 +0000476 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000477 bool EverMadeChange = false, MadeChange = true;
478
479 while (MadeChange) {
480 MadeChange = false;
481
482 for (I = MBBInfos.begin(); I != E; ++I) {
483 // Skip if this MBB doesn't have a branch or the branch has already been
484 // converted to a long branch.
485 if (!I->Br || I->HasLongBranch)
486 continue;
487
Eric Christopher96e72c62015-01-29 23:27:36 +0000488 int ShVal = STI.inMicroMipsMode() ? 2 : 4;
Sasa Stankovic67814262014-06-05 13:52:08 +0000489 int64_t Offset = computeOffset(I->Br) / ShVal;
490
Eric Christopher96e72c62015-01-29 23:27:36 +0000491 if (STI.isTargetNaCl()) {
Sasa Stankovic67814262014-06-05 13:52:08 +0000492 // The offset calculation does not include sandboxing instructions
493 // that will be added later in the MC layer. Since at this point we
494 // don't know the exact amount of code that "sandboxing" will add, we
495 // conservatively estimate that code will not grow more than 100%.
496 Offset *= 2;
497 }
Zoran Jovanovic9d86e262013-11-30 19:12:28 +0000498
Akira Hatanakab5af7122012-08-28 03:03:05 +0000499 // Check if offset fits into 16-bit immediate field of branches.
Sasa Stankovic67814262014-06-05 13:52:08 +0000500 if (!ForceLongBranch && isInt<16>(Offset))
Akira Hatanakab5af7122012-08-28 03:03:05 +0000501 continue;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000502
Akira Hatanakab5af7122012-08-28 03:03:05 +0000503 I->HasLongBranch = true;
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000504 I->Size += LongBranchSeqSize * 4;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000505 ++LongBranches;
506 EverMadeChange = MadeChange = true;
507 }
508 }
509
Akira Hatanakab5af7122012-08-28 03:03:05 +0000510 if (!EverMadeChange)
511 return true;
512
513 // Compute basic block addresses.
514 if (TM.getRelocationModel() == Reloc::PIC_) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000515 uint64_t Address = 0;
516
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000517 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000518 I->Address = Address;
519 }
520
521 // Do the expansion.
522 for (I = MBBInfos.begin(); I != E; ++I)
523 if (I->HasLongBranch)
524 expandToLongBranch(*I);
525
526 MF->RenumberBlocks();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000527
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000528 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000529}