blob: 8a48d863c8b8f59854c768297c3e3b6fb018feb3 [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);
Benjamin Kramerbdc49562016-06-12 15:39:02 +000085 void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
Akira Hatanakaa2159292012-06-14 01:22:24 +000086 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.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000121static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) {
Akira Hatanakaa2159292012-06-14 01:22:24 +0000122 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,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000221 const DebugLoc &DL,
222 MachineBasicBlock *MBBOpnd) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000223 const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
224 MBB.getParent()->getSubtarget().getInstrInfo());
Akira Hatanaka067d8152013-05-13 17:43:19 +0000225 unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000226 const MCInstrDesc &NewDesc = TII->get(NewOpc);
227
228 MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
229
230 for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
231 MachineOperand &MO = Br->getOperand(I);
232
233 if (!MO.isReg()) {
234 assert(MO.isMBB() && "MBB operand expected.");
235 break;
236 }
237
238 MIB.addReg(MO.getReg());
239 }
240
241 MIB.addMBB(MBBOpnd);
242
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000243 if (Br->hasDelaySlot()) {
244 // Bundle the instruction in the delay slot to the newly created branch
245 // and erase the original branch.
246 assert(Br->isBundledWithSucc());
247 MachineBasicBlock::instr_iterator II(Br);
248 MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
249 }
Akira Hatanakaa2159292012-06-14 01:22:24 +0000250 Br->eraseFromParent();
251}
252
253// Expand branch instructions to long branches.
Jozef Kolek9761e962015-01-12 12:03:34 +0000254// TODO: This function has to be fixed for beqz16 and bnez16, because it
255// currently assumes that all branches have 16-bit offsets, and will produce
256// wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
257// are present.
Akira Hatanakaa2159292012-06-14 01:22:24 +0000258void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000259 MachineBasicBlock::iterator Pos;
260 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000261 DebugLoc DL = I.Br->getDebugLoc();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000262 const BasicBlock *BB = MBB->getBasicBlock();
263 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
264 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
Eric Christopher96e72c62015-01-29 23:27:36 +0000265 const MipsSubtarget &Subtarget =
266 static_cast<const MipsSubtarget &>(MF->getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000267 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000268 static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000269
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000270 MF->insert(FallThroughMBB, LongBrMBB);
Cong Houd97c1002015-12-01 05:29:22 +0000271 MBB->replaceSuccessor(TgtMBB, LongBrMBB);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000272
273 if (IsPIC) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000274 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
275 MF->insert(FallThroughMBB, BalTgtMBB);
276 LongBrMBB->addSuccessor(BalTgtMBB);
277 BalTgtMBB->addSuccessor(TgtMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000278
Daniel Sanders86cb3982014-06-13 13:02:52 +0000279 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
280 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
281 // pseudo-instruction wrapping BGEZAL).
Daniel Sanders86cb3982014-06-13 13:02:52 +0000282 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
283
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000284 if (!ABI.IsN64()) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000285 // $longbr:
286 // addiu $sp, $sp, -8
287 // sw $ra, 0($sp)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000288 // lui $at, %hi($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000289 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000290 // addiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000291 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000292 // addu $at, $ra, $at
293 // lw $ra, 0($sp)
294 // jr $at
295 // addiu $sp, $sp, 8
296 // $fallthrough:
297 //
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000298
Akira Hatanakab5af7122012-08-28 03:03:05 +0000299 Pos = LongBrMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000300
Akira Hatanakab5af7122012-08-28 03:03:05 +0000301 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
302 .addReg(Mips::SP).addImm(-8);
303 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
304 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000305
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000306 // LUi and ADDiu instructions create 32-bit offset of the target basic
307 // block from the target of BAL instruction. We cannot use immediate
308 // value for this offset because it cannot be determined accurately when
309 // the program has inline assembly statements. We therefore use the
310 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
311 // are resolved during the fixup, so the values will always be correct.
312 //
313 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
314 // expressions at this point (it is possible only at the MC layer),
315 // we replace LUi and ADDiu with pseudo instructions
316 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
317 // blocks as operands to these instructions. When lowering these pseudo
318 // instructions to LUi and ADDiu in the MC layer, we will create
319 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
320 // operands to lowered instructions.
321
322 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
323 .addMBB(TgtMBB).addMBB(BalTgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000324 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000325 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
326 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
327 .addReg(Mips::AT)
328 .addMBB(TgtMBB)
329 .addMBB(BalTgtMBB));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000330
Akira Hatanakab5af7122012-08-28 03:03:05 +0000331 Pos = BalTgtMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000332
Akira Hatanakab5af7122012-08-28 03:03:05 +0000333 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
334 .addReg(Mips::RA).addReg(Mips::AT);
335 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
336 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000337
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000338 // In NaCl, modifying the sp is not allowed in branch delay slot.
339 if (Subtarget.isTargetNaCl())
Sasa Stankovic67814262014-06-05 13:52:08 +0000340 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
341 .addReg(Mips::SP).addImm(8);
342
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000343 if (Subtarget.hasMips32r6())
344 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR))
345 .addReg(Mips::ZERO).addReg(Mips::AT);
346 else
347 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
Sasa Stankovic67814262014-06-05 13:52:08 +0000348
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000349 if (Subtarget.isTargetNaCl()) {
350 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
Sasa Stankovic67814262014-06-05 13:52:08 +0000351 // Bundle-align the target of indirect branch JR.
352 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000353 } else
354 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
355 .addReg(Mips::SP).addImm(8);
356
357 BalTgtMBB->rbegin()->bundleWithPred();
Akira Hatanakab5af7122012-08-28 03:03:05 +0000358 } else {
359 // $longbr:
360 // daddiu $sp, $sp, -16
361 // sd $ra, 0($sp)
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000362 // daddiu $at, $zero, %hi($tgt - $baltgt)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000363 // dsll $at, $at, 16
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000364 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000365 // daddiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000366 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000367 // daddu $at, $ra, $at
368 // ld $ra, 0($sp)
369 // jr64 $at
370 // daddiu $sp, $sp, 16
371 // $fallthrough:
372 //
373
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000374 // We assume the branch is within-function, and that offset is within
375 // +/- 2GB. High 32 bits will therefore always be zero.
376
377 // Note that this will work even if the offset is negative, because
378 // of the +1 modification that's added in that case. For example, if the
379 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
380 //
381 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
382 //
383 // and the bits [47:32] are zero. For %highest
384 //
385 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
386 //
387 // and the bits [63:48] are zero.
Akira Hatanakab5af7122012-08-28 03:03:05 +0000388
389 Pos = LongBrMBB->begin();
390
391 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
392 .addReg(Mips::SP_64).addImm(-16);
393 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
394 .addReg(Mips::SP_64).addImm(0);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000395 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000396 Mips::AT_64).addReg(Mips::ZERO_64)
397 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000398 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
399 .addReg(Mips::AT_64).addImm(16);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000400
401 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000402 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
403 .append(
404 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
405 .addReg(Mips::AT_64)
406 .addMBB(TgtMBB, MipsII::MO_ABS_LO)
407 .addMBB(BalTgtMBB));
Akira Hatanakab5af7122012-08-28 03:03:05 +0000408
409 Pos = BalTgtMBB->begin();
410
Akira Hatanakab5af7122012-08-28 03:03:05 +0000411 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
412 .addReg(Mips::RA_64).addReg(Mips::AT_64);
413 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
414 .addReg(Mips::SP_64).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000415
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000416 if (Subtarget.hasMips64r6())
417 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR64))
418 .addReg(Mips::ZERO_64).addReg(Mips::AT_64);
419 else
420 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
421
422 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
423 .addReg(Mips::SP_64).addImm(16);
424 BalTgtMBB->rbegin()->bundleWithPred();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000425 }
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000426
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000427 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000428 } else {
429 // $longbr:
430 // j $tgt
431 // nop
432 // $fallthrough:
433 //
434 Pos = LongBrMBB->begin();
435 LongBrMBB->addSuccessor(TgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000436 MIBundleBuilder(*LongBrMBB, Pos)
437 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
438 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000439
440 assert(LongBrMBB->size() == LongBranchSeqSize);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000441 }
442
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000443 if (I.Br->isUnconditionalBranch()) {
444 // Change branch destination.
445 assert(I.Br->getDesc().getNumOperands() == 1);
446 I.Br->RemoveOperand(0);
447 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
448 } else
449 // Change branch destination and reverse condition.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000450 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000451}
452
453static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
454 MachineBasicBlock &MBB = F.front();
455 MachineBasicBlock::iterator I = MBB.begin();
456 DebugLoc DL = MBB.findDebugLoc(MBB.begin());
457 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
458 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
459 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
460 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
461 MBB.removeLiveIn(Mips::V0);
462}
463
464bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000465 const MipsSubtarget &STI =
466 static_cast<const MipsSubtarget &>(F.getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000467 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000468 static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
469 LongBranchSeqSize =
470 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
Bill Wendlingead89ef2013-06-07 07:04:14 +0000471
Eric Christophera08db01b2014-07-18 20:29:02 +0000472 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
Reed Kotler1595f362013-04-09 19:46:01 +0000473 return false;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000474 if ((TM.getRelocationModel() == Reloc::PIC_) &&
Eric Christopher96e72c62015-01-29 23:27:36 +0000475 static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
Akira Hatanakaa2159292012-06-14 01:22:24 +0000476 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
477 emitGPDisp(F, TII);
478
479 if (SkipLongBranch)
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000480 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000481
482 MF = &F;
483 initMBBInfo();
484
Craig Topperaf0dea12013-07-04 01:31:24 +0000485 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000486 bool EverMadeChange = false, MadeChange = true;
487
488 while (MadeChange) {
489 MadeChange = false;
490
491 for (I = MBBInfos.begin(); I != E; ++I) {
492 // Skip if this MBB doesn't have a branch or the branch has already been
493 // converted to a long branch.
494 if (!I->Br || I->HasLongBranch)
495 continue;
496
Eric Christopher96e72c62015-01-29 23:27:36 +0000497 int ShVal = STI.inMicroMipsMode() ? 2 : 4;
Sasa Stankovic67814262014-06-05 13:52:08 +0000498 int64_t Offset = computeOffset(I->Br) / ShVal;
499
Eric Christopher96e72c62015-01-29 23:27:36 +0000500 if (STI.isTargetNaCl()) {
Sasa Stankovic67814262014-06-05 13:52:08 +0000501 // The offset calculation does not include sandboxing instructions
502 // that will be added later in the MC layer. Since at this point we
503 // don't know the exact amount of code that "sandboxing" will add, we
504 // conservatively estimate that code will not grow more than 100%.
505 Offset *= 2;
506 }
Zoran Jovanovic9d86e262013-11-30 19:12:28 +0000507
Akira Hatanakab5af7122012-08-28 03:03:05 +0000508 // Check if offset fits into 16-bit immediate field of branches.
Sasa Stankovic67814262014-06-05 13:52:08 +0000509 if (!ForceLongBranch && isInt<16>(Offset))
Akira Hatanakab5af7122012-08-28 03:03:05 +0000510 continue;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000511
Akira Hatanakab5af7122012-08-28 03:03:05 +0000512 I->HasLongBranch = true;
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000513 I->Size += LongBranchSeqSize * 4;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000514 ++LongBranches;
515 EverMadeChange = MadeChange = true;
516 }
517 }
518
Akira Hatanakab5af7122012-08-28 03:03:05 +0000519 if (!EverMadeChange)
520 return true;
521
522 // Compute basic block addresses.
523 if (TM.getRelocationModel() == Reloc::PIC_) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000524 uint64_t Address = 0;
525
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000526 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000527 I->Address = Address;
528 }
529
530 // Do the expansion.
531 for (I = MBBInfos.begin(); I != E; ++I)
532 if (I->HasLongBranch)
533 expandToLongBranch(*I);
534
535 MF->RenumberBlocks();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000536
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000537 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000538}