blob: dfb08101956045d0c3b5115fbcc35afa9cd81076 [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)
Rafael Espindolab30e66b2016-06-28 14:33:28 +000066 : MachineFunctionPass(ID), TM(tm), IsPIC(TM.isPositionIndependent()),
Eric Christopher96e72c62015-01-29 23:27:36 +000067 ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {}
Akira Hatanakaa2159292012-06-14 01:22:24 +000068
Craig Topper56c590a2014-04-29 07:58:02 +000069 const char *getPassName() const override {
Akira Hatanakaa2159292012-06-14 01:22:24 +000070 return "Mips Long Branch";
71 }
72
Craig Topper56c590a2014-04-29 07:58:02 +000073 bool runOnMachineFunction(MachineFunction &F) override;
Akira Hatanakaa2159292012-06-14 01:22:24 +000074
Derek Schuff1dbf7a52016-04-04 17:09:25 +000075 MachineFunctionProperties getRequiredProperties() const override {
76 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000077 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000078 }
79
Akira Hatanakaa2159292012-06-14 01:22:24 +000080 private:
81 void splitMBB(MachineBasicBlock *MBB);
82 void initMBBInfo();
83 int64_t computeOffset(const MachineInstr *Br);
Benjamin Kramerbdc49562016-06-12 15:39:02 +000084 void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
Akira Hatanakaa2159292012-06-14 01:22:24 +000085 MachineBasicBlock *MBBOpnd);
86 void expandToLongBranch(MBBInfo &Info);
87
88 const TargetMachine &TM;
Akira Hatanakaa2159292012-06-14 01:22:24 +000089 MachineFunction *MF;
90 SmallVector<MBBInfo, 16> MBBInfos;
Akira Hatanakab5af7122012-08-28 03:03:05 +000091 bool IsPIC;
Daniel Sanderse2e25da2014-10-24 16:15:27 +000092 MipsABIInfo ABI;
Akira Hatanakab5af7122012-08-28 03:03:05 +000093 unsigned LongBranchSeqSize;
Akira Hatanakaa2159292012-06-14 01:22:24 +000094 };
95
96 char MipsLongBranch::ID = 0;
97} // end of anonymous namespace
98
99/// createMipsLongBranchPass - Returns a pass that converts branches to long
100/// branches.
101FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
102 return new MipsLongBranch(tm);
103}
104
105/// Iterate over list of Br's operands and search for a MachineBasicBlock
106/// operand.
107static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
108 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
109 const MachineOperand &MO = Br.getOperand(I);
110
111 if (MO.isMBB())
112 return MO.getMBB();
113 }
114
Craig Topperd3c02f12015-01-05 10:15:49 +0000115 llvm_unreachable("This instruction does not have an MBB operand.");
Akira Hatanakaa2159292012-06-14 01:22:24 +0000116}
117
118// Traverse the list of instructions backwards until a non-debug instruction is
119// found or it reaches E.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000120static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) {
Akira Hatanakaa2159292012-06-14 01:22:24 +0000121 for (; B != E; ++B)
122 if (!B->isDebugValue())
123 return B;
124
125 return E;
126}
127
128// Split MBB if it has two direct jumps/branches.
129void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
130 ReverseIter End = MBB->rend();
131 ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
132
133 // Return if MBB has no branch instructions.
134 if ((LastBr == End) ||
135 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
136 return;
137
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000138 ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000139
140 // MBB has only one branch instruction if FirstBr is not a branch
141 // instruction.
142 if ((FirstBr == End) ||
143 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
144 return;
145
146 assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
147
148 // Create a new MBB. Move instructions in MBB to the newly created MBB.
149 MachineBasicBlock *NewMBB =
150 MF->CreateMachineBasicBlock(MBB->getBasicBlock());
151
152 // Insert NewMBB and fix control flow.
153 MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
154 NewMBB->transferSuccessors(MBB);
Cong Houc1069892015-12-13 09:26:17 +0000155 NewMBB->removeSuccessor(Tgt, true);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000156 MBB->addSuccessor(NewMBB);
157 MBB->addSuccessor(Tgt);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000158 MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000159
160 NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end());
161}
162
163// Fill MBBInfos.
164void MipsLongBranch::initMBBInfo() {
165 // Split the MBBs if they have two branches. Each basic block should have at
166 // most one branch after this loop is executed.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +0000167 for (auto &MBB : *MF)
168 splitMBB(&MBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000169
170 MF->RenumberBlocks();
171 MBBInfos.clear();
172 MBBInfos.resize(MF->size());
173
Bill Wendlingead89ef2013-06-07 07:04:14 +0000174 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000175 static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo());
Akira Hatanakaa2159292012-06-14 01:22:24 +0000176 for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
177 MachineBasicBlock *MBB = MF->getBlockNumbered(I);
178
179 // Compute size of MBB.
180 for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
181 MI != MBB->instr_end(); ++MI)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000182 MBBInfos[I].Size += TII->getInstSizeInBytes(*MI);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000183
184 // Search for MBB's branch instruction.
185 ReverseIter End = MBB->rend();
186 ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
187
188 if ((Br != End) && !Br->isIndirectBranch() &&
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000189 (Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000190 MBBInfos[I].Br = &*(++Br).base();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000191 }
192}
193
194// Compute offset of branch in number of bytes.
195int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
196 int64_t Offset = 0;
197 int ThisMBB = Br->getParent()->getNumber();
198 int TargetMBB = getTargetMBB(*Br)->getNumber();
199
200 // Compute offset of a forward branch.
201 if (ThisMBB < TargetMBB) {
202 for (int N = ThisMBB + 1; N < TargetMBB; ++N)
203 Offset += MBBInfos[N].Size;
204
205 return Offset + 4;
206 }
207
208 // Compute offset of a backward branch.
209 for (int N = ThisMBB; N >= TargetMBB; --N)
210 Offset += MBBInfos[N].Size;
211
212 return -Offset + 4;
213}
214
Akira Hatanakaa2159292012-06-14 01:22:24 +0000215// Replace Br with a branch which has the opposite condition code and a
216// MachineBasicBlock operand MBBOpnd.
217void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000218 const DebugLoc &DL,
219 MachineBasicBlock *MBBOpnd) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000220 const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
221 MBB.getParent()->getSubtarget().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());
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000244 MachineBasicBlock::instr_iterator II = Br.getInstrIterator();
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000245 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.
Jozef Kolek9761e962015-01-12 12:03:34 +0000251// TODO: This function has to be fixed for beqz16 and bnez16, because it
252// currently assumes that all branches have 16-bit offsets, and will produce
253// wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
254// are present.
Akira Hatanakaa2159292012-06-14 01:22:24 +0000255void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000256 MachineBasicBlock::iterator Pos;
257 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000258 DebugLoc DL = I.Br->getDebugLoc();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000259 const BasicBlock *BB = MBB->getBasicBlock();
260 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
261 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
Eric Christopher96e72c62015-01-29 23:27:36 +0000262 const MipsSubtarget &Subtarget =
263 static_cast<const MipsSubtarget &>(MF->getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000264 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000265 static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000266
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000267 MF->insert(FallThroughMBB, LongBrMBB);
Cong Houd97c1002015-12-01 05:29:22 +0000268 MBB->replaceSuccessor(TgtMBB, LongBrMBB);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000269
270 if (IsPIC) {
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000271 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
272 MF->insert(FallThroughMBB, BalTgtMBB);
273 LongBrMBB->addSuccessor(BalTgtMBB);
274 BalTgtMBB->addSuccessor(TgtMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000275
Daniel Sanders86cb3982014-06-13 13:02:52 +0000276 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
277 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
278 // pseudo-instruction wrapping BGEZAL).
Daniel Sanders86cb3982014-06-13 13:02:52 +0000279 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
280
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000281 if (!ABI.IsN64()) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000282 // $longbr:
283 // addiu $sp, $sp, -8
284 // sw $ra, 0($sp)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000285 // lui $at, %hi($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000286 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000287 // addiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000288 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000289 // addu $at, $ra, $at
290 // lw $ra, 0($sp)
291 // jr $at
292 // addiu $sp, $sp, 8
293 // $fallthrough:
294 //
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000295
Akira Hatanakab5af7122012-08-28 03:03:05 +0000296 Pos = LongBrMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000297
Akira Hatanakab5af7122012-08-28 03:03:05 +0000298 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
299 .addReg(Mips::SP).addImm(-8);
300 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
301 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000302
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000303 // LUi and ADDiu instructions create 32-bit offset of the target basic
304 // block from the target of BAL instruction. We cannot use immediate
305 // value for this offset because it cannot be determined accurately when
306 // the program has inline assembly statements. We therefore use the
307 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
308 // are resolved during the fixup, so the values will always be correct.
309 //
310 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
311 // expressions at this point (it is possible only at the MC layer),
312 // we replace LUi and ADDiu with pseudo instructions
313 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
314 // blocks as operands to these instructions. When lowering these pseudo
315 // instructions to LUi and ADDiu in the MC layer, we will create
316 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
317 // operands to lowered instructions.
318
319 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
320 .addMBB(TgtMBB).addMBB(BalTgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000321 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000322 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
323 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
324 .addReg(Mips::AT)
325 .addMBB(TgtMBB)
326 .addMBB(BalTgtMBB));
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000327
Akira Hatanakab5af7122012-08-28 03:03:05 +0000328 Pos = BalTgtMBB->begin();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000329
Akira Hatanakab5af7122012-08-28 03:03:05 +0000330 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
331 .addReg(Mips::RA).addReg(Mips::AT);
332 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
333 .addReg(Mips::SP).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000334
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000335 // In NaCl, modifying the sp is not allowed in branch delay slot.
336 if (Subtarget.isTargetNaCl())
Sasa Stankovic67814262014-06-05 13:52:08 +0000337 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
338 .addReg(Mips::SP).addImm(8);
339
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000340 if (Subtarget.hasMips32r6())
341 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR))
342 .addReg(Mips::ZERO).addReg(Mips::AT);
343 else
344 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
Sasa Stankovic67814262014-06-05 13:52:08 +0000345
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000346 if (Subtarget.isTargetNaCl()) {
347 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
Sasa Stankovic67814262014-06-05 13:52:08 +0000348 // Bundle-align the target of indirect branch JR.
349 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000350 } else
351 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
352 .addReg(Mips::SP).addImm(8);
353
354 BalTgtMBB->rbegin()->bundleWithPred();
Akira Hatanakab5af7122012-08-28 03:03:05 +0000355 } else {
356 // $longbr:
357 // daddiu $sp, $sp, -16
358 // sd $ra, 0($sp)
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000359 // daddiu $at, $zero, %hi($tgt - $baltgt)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000360 // dsll $at, $at, 16
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000361 // bal $baltgt
Akira Hatanakab5af7122012-08-28 03:03:05 +0000362 // daddiu $at, $at, %lo($tgt - $baltgt)
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000363 // $baltgt:
Akira Hatanakab5af7122012-08-28 03:03:05 +0000364 // daddu $at, $ra, $at
365 // ld $ra, 0($sp)
366 // jr64 $at
367 // daddiu $sp, $sp, 16
368 // $fallthrough:
369 //
370
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000371 // We assume the branch is within-function, and that offset is within
372 // +/- 2GB. High 32 bits will therefore always be zero.
373
374 // Note that this will work even if the offset is negative, because
375 // of the +1 modification that's added in that case. For example, if the
376 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
377 //
378 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
379 //
380 // and the bits [47:32] are zero. For %highest
381 //
382 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
383 //
384 // and the bits [63:48] are zero.
Akira Hatanakab5af7122012-08-28 03:03:05 +0000385
386 Pos = LongBrMBB->begin();
387
388 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
389 .addReg(Mips::SP_64).addImm(-16);
390 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
391 .addReg(Mips::SP_64).addImm(0);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000392 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
Sasa Stankovice41db2f2014-05-27 18:53:06 +0000393 Mips::AT_64).addReg(Mips::ZERO_64)
394 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000395 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
396 .addReg(Mips::AT_64).addImm(16);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000397
398 MIBundleBuilder(*LongBrMBB, Pos)
Daniel Sanders86cb3982014-06-13 13:02:52 +0000399 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
400 .append(
401 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
402 .addReg(Mips::AT_64)
403 .addMBB(TgtMBB, MipsII::MO_ABS_LO)
404 .addMBB(BalTgtMBB));
Akira Hatanakab5af7122012-08-28 03:03:05 +0000405
406 Pos = BalTgtMBB->begin();
407
Akira Hatanakab5af7122012-08-28 03:03:05 +0000408 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
409 .addReg(Mips::RA_64).addReg(Mips::AT_64);
410 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
411 .addReg(Mips::SP_64).addImm(0);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000412
Vasileios Kalintiris0cf68df2016-06-18 15:39:43 +0000413 if (Subtarget.hasMips64r6())
414 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR64))
415 .addReg(Mips::ZERO_64).addReg(Mips::AT_64);
416 else
417 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
418
419 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
420 .addReg(Mips::SP_64).addImm(16);
421 BalTgtMBB->rbegin()->bundleWithPred();
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000422 }
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000423
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000424 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000425 } else {
426 // $longbr:
427 // j $tgt
428 // nop
429 // $fallthrough:
430 //
431 Pos = LongBrMBB->begin();
432 LongBrMBB->addSuccessor(TgtMBB);
Jakob Stoklund Olesen97030e02012-12-07 04:23:40 +0000433 MIBundleBuilder(*LongBrMBB, Pos)
434 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
435 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
Akira Hatanaka5fdeac32012-11-15 20:05:11 +0000436
437 assert(LongBrMBB->size() == LongBranchSeqSize);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000438 }
439
Akira Hatanakaf72efdb2012-07-21 03:30:44 +0000440 if (I.Br->isUnconditionalBranch()) {
441 // Change branch destination.
442 assert(I.Br->getDesc().getNumOperands() == 1);
443 I.Br->RemoveOperand(0);
444 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
445 } else
446 // Change branch destination and reverse condition.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000447 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
Akira Hatanakaa2159292012-06-14 01:22:24 +0000448}
449
450static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
451 MachineBasicBlock &MBB = F.front();
452 MachineBasicBlock::iterator I = MBB.begin();
453 DebugLoc DL = MBB.findDebugLoc(MBB.begin());
454 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
455 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
456 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
457 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
458 MBB.removeLiveIn(Mips::V0);
459}
460
461bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000462 const MipsSubtarget &STI =
463 static_cast<const MipsSubtarget &>(F.getSubtarget());
Bill Wendlingead89ef2013-06-07 07:04:14 +0000464 const MipsInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000465 static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
466 LongBranchSeqSize =
467 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
Bill Wendlingead89ef2013-06-07 07:04:14 +0000468
Eric Christophera08db01b2014-07-18 20:29:02 +0000469 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
Reed Kotler1595f362013-04-09 19:46:01 +0000470 return false;
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000471 if (IsPIC && static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
Akira Hatanakaa2159292012-06-14 01:22:24 +0000472 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
473 emitGPDisp(F, TII);
474
475 if (SkipLongBranch)
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000476 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000477
478 MF = &F;
479 initMBBInfo();
480
Craig Topperaf0dea12013-07-04 01:31:24 +0000481 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000482 bool EverMadeChange = false, MadeChange = true;
483
484 while (MadeChange) {
485 MadeChange = false;
486
487 for (I = MBBInfos.begin(); I != E; ++I) {
488 // Skip if this MBB doesn't have a branch or the branch has already been
489 // converted to a long branch.
490 if (!I->Br || I->HasLongBranch)
491 continue;
492
Eric Christopher96e72c62015-01-29 23:27:36 +0000493 int ShVal = STI.inMicroMipsMode() ? 2 : 4;
Sasa Stankovic67814262014-06-05 13:52:08 +0000494 int64_t Offset = computeOffset(I->Br) / ShVal;
495
Eric Christopher96e72c62015-01-29 23:27:36 +0000496 if (STI.isTargetNaCl()) {
Sasa Stankovic67814262014-06-05 13:52:08 +0000497 // The offset calculation does not include sandboxing instructions
498 // that will be added later in the MC layer. Since at this point we
499 // don't know the exact amount of code that "sandboxing" will add, we
500 // conservatively estimate that code will not grow more than 100%.
501 Offset *= 2;
502 }
Zoran Jovanovic9d86e262013-11-30 19:12:28 +0000503
Akira Hatanakab5af7122012-08-28 03:03:05 +0000504 // Check if offset fits into 16-bit immediate field of branches.
Sasa Stankovic67814262014-06-05 13:52:08 +0000505 if (!ForceLongBranch && isInt<16>(Offset))
Akira Hatanakab5af7122012-08-28 03:03:05 +0000506 continue;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000507
Akira Hatanakab5af7122012-08-28 03:03:05 +0000508 I->HasLongBranch = true;
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000509 I->Size += LongBranchSeqSize * 4;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000510 ++LongBranches;
511 EverMadeChange = MadeChange = true;
512 }
513 }
514
Akira Hatanakab5af7122012-08-28 03:03:05 +0000515 if (!EverMadeChange)
516 return true;
517
518 // Compute basic block addresses.
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000519 if (IsPIC) {
Akira Hatanakab5af7122012-08-28 03:03:05 +0000520 uint64_t Address = 0;
521
Akira Hatanaka206cefe2012-08-28 18:58:57 +0000522 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
Akira Hatanakab5af7122012-08-28 03:03:05 +0000523 I->Address = Address;
524 }
525
526 // Do the expansion.
527 for (I = MBBInfos.begin(); I != E; ++I)
528 if (I->HasLongBranch)
529 expandToLongBranch(*I);
530
531 MF->RenumberBlocks();
Akira Hatanakaa2159292012-06-14 01:22:24 +0000532
Akira Hatanaka9f96bb82012-06-19 03:45:29 +0000533 return true;
Akira Hatanakaa2159292012-06-14 01:22:24 +0000534}