Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 1 | //===-- 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 Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 13 | // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries. |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 16 | #include "Mips.h" |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsBaseInfo.h" |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/MipsMCNaCl.h" |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 19 | #include "MipsMachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "MipsTargetMachine.h" |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 25 | #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 | |
| 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "mips-long-branch" |
| 34 | |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 35 | STATISTIC(LongBranches, "Number of long branches."); |
| 36 | |
| 37 | static 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 | |
| 43 | static 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 | |
| 49 | namespace { |
| 50 | typedef MachineBasicBlock::iterator Iter; |
| 51 | typedef MachineBasicBlock::reverse_iterator ReverseIter; |
| 52 | |
| 53 | struct MBBInfo { |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 54 | uint64_t Size, Address; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 55 | bool HasLongBranch; |
| 56 | MachineInstr *Br; |
| 57 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 58 | MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {} |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class MipsLongBranch : public MachineFunctionPass { |
| 62 | |
| 63 | public: |
| 64 | static char ID; |
| 65 | MipsLongBranch(TargetMachine &tm) |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 66 | : MachineFunctionPass(ID), TM(tm), |
| 67 | IsPIC(TM.getRelocationModel() == Reloc::PIC_), |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 68 | ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {} |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 69 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 70 | const char *getPassName() const override { |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 71 | return "Mips Long Branch"; |
| 72 | } |
| 73 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 74 | bool runOnMachineFunction(MachineFunction &F) override; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 75 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 76 | MachineFunctionProperties getRequiredProperties() const override { |
| 77 | return MachineFunctionProperties().set( |
| 78 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 79 | } |
| 80 | |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 81 | private: |
| 82 | void splitMBB(MachineBasicBlock *MBB); |
| 83 | void initMBBInfo(); |
| 84 | int64_t computeOffset(const MachineInstr *Br); |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 85 | void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL, |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 86 | MachineBasicBlock *MBBOpnd); |
| 87 | void expandToLongBranch(MBBInfo &Info); |
| 88 | |
| 89 | const TargetMachine &TM; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 90 | MachineFunction *MF; |
| 91 | SmallVector<MBBInfo, 16> MBBInfos; |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 92 | bool IsPIC; |
Daniel Sanders | e2e25da | 2014-10-24 16:15:27 +0000 | [diff] [blame] | 93 | MipsABIInfo ABI; |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 94 | unsigned LongBranchSeqSize; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | char MipsLongBranch::ID = 0; |
| 98 | } // end of anonymous namespace |
| 99 | |
| 100 | /// createMipsLongBranchPass - Returns a pass that converts branches to long |
| 101 | /// branches. |
| 102 | FunctionPass *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. |
| 108 | static 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 Topper | d3c02f1 | 2015-01-05 10:15:49 +0000 | [diff] [blame] | 116 | llvm_unreachable("This instruction does not have an MBB operand."); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Traverse the list of instructions backwards until a non-debug instruction is |
| 120 | // found or it reaches E. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 121 | static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) { |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 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. |
| 130 | void 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 Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 139 | ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 140 | |
| 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 Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 156 | NewMBB->removeSuccessor(Tgt, true); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 157 | MBB->addSuccessor(NewMBB); |
| 158 | MBB->addSuccessor(Tgt); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 159 | MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 160 | |
| 161 | NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end()); |
| 162 | } |
| 163 | |
| 164 | // Fill MBBInfos. |
| 165 | void 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 Kalintiris | 5a971a4 | 2016-04-15 20:43:17 +0000 | [diff] [blame] | 168 | for (auto &MBB : *MF) |
| 169 | splitMBB(&MBB); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 170 | |
| 171 | MF->RenumberBlocks(); |
| 172 | MBBInfos.clear(); |
| 173 | MBBInfos.resize(MF->size()); |
| 174 | |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 175 | const MipsInstrInfo *TII = |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 176 | static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo()); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 177 | 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 Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 190 | (Br->isConditionalBranch() || |
| 191 | (Br->isUnconditionalBranch() && |
| 192 | TM.getRelocationModel() == Reloc::PIC_))) |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 193 | MBBInfos[I].Br = (++Br).base(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Compute offset of branch in number of bytes. |
| 198 | int64_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 Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 218 | // Replace Br with a branch which has the opposite condition code and a |
| 219 | // MachineBasicBlock operand MBBOpnd. |
| 220 | void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 221 | const DebugLoc &DL, |
| 222 | MachineBasicBlock *MBBOpnd) { |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 223 | const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>( |
| 224 | MBB.getParent()->getSubtarget().getInstrInfo()); |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 225 | unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode()); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 226 | 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 Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 243 | 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 Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 250 | Br->eraseFromParent(); |
| 251 | } |
| 252 | |
| 253 | // Expand branch instructions to long branches. |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 254 | // 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 Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 258 | void MipsLongBranch::expandToLongBranch(MBBInfo &I) { |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 259 | MachineBasicBlock::iterator Pos; |
| 260 | MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 261 | DebugLoc DL = I.Br->getDebugLoc(); |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 262 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 263 | MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB); |
| 264 | MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 265 | const MipsSubtarget &Subtarget = |
| 266 | static_cast<const MipsSubtarget &>(MF->getSubtarget()); |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 267 | const MipsInstrInfo *TII = |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 268 | static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo()); |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 269 | |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 270 | MF->insert(FallThroughMBB, LongBrMBB); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 271 | MBB->replaceSuccessor(TgtMBB, LongBrMBB); |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 272 | |
| 273 | if (IsPIC) { |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 274 | MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB); |
| 275 | MF->insert(FallThroughMBB, BalTgtMBB); |
| 276 | LongBrMBB->addSuccessor(BalTgtMBB); |
| 277 | BalTgtMBB->addSuccessor(TgtMBB); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 278 | |
Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 279 | // 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 Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 282 | unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR; |
| 283 | |
Daniel Sanders | e2e25da | 2014-10-24 16:15:27 +0000 | [diff] [blame] | 284 | if (!ABI.IsN64()) { |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 285 | // $longbr: |
| 286 | // addiu $sp, $sp, -8 |
| 287 | // sw $ra, 0($sp) |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 288 | // lui $at, %hi($tgt - $baltgt) |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 289 | // bal $baltgt |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 290 | // addiu $at, $at, %lo($tgt - $baltgt) |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 291 | // $baltgt: |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 292 | // addu $at, $ra, $at |
| 293 | // lw $ra, 0($sp) |
| 294 | // jr $at |
| 295 | // addiu $sp, $sp, 8 |
| 296 | // $fallthrough: |
| 297 | // |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 298 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 299 | Pos = LongBrMBB->begin(); |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 300 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 301 | 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 Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 305 | |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 306 | // 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 Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 324 | MIBundleBuilder(*LongBrMBB, Pos) |
Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 325 | .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 Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 330 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 331 | Pos = BalTgtMBB->begin(); |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 332 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 333 | 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 Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 337 | |
Vasileios Kalintiris | 0cf68df | 2016-06-18 15:39:43 +0000 | [diff] [blame^] | 338 | // In NaCl, modifying the sp is not allowed in branch delay slot. |
| 339 | if (Subtarget.isTargetNaCl()) |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 340 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) |
| 341 | .addReg(Mips::SP).addImm(8); |
| 342 | |
Vasileios Kalintiris | 0cf68df | 2016-06-18 15:39:43 +0000 | [diff] [blame^] | 343 | 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 Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 348 | |
Vasileios Kalintiris | 0cf68df | 2016-06-18 15:39:43 +0000 | [diff] [blame^] | 349 | if (Subtarget.isTargetNaCl()) { |
| 350 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP)); |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 351 | // Bundle-align the target of indirect branch JR. |
| 352 | TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN); |
Vasileios Kalintiris | 0cf68df | 2016-06-18 15:39:43 +0000 | [diff] [blame^] | 353 | } else |
| 354 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) |
| 355 | .addReg(Mips::SP).addImm(8); |
| 356 | |
| 357 | BalTgtMBB->rbegin()->bundleWithPred(); |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 358 | } else { |
| 359 | // $longbr: |
| 360 | // daddiu $sp, $sp, -16 |
| 361 | // sd $ra, 0($sp) |
Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 362 | // daddiu $at, $zero, %hi($tgt - $baltgt) |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 363 | // dsll $at, $at, 16 |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 364 | // bal $baltgt |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 365 | // daddiu $at, $at, %lo($tgt - $baltgt) |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 366 | // $baltgt: |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 367 | // daddu $at, $ra, $at |
| 368 | // ld $ra, 0($sp) |
| 369 | // jr64 $at |
| 370 | // daddiu $sp, $sp, 16 |
| 371 | // $fallthrough: |
| 372 | // |
| 373 | |
Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 374 | // 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 Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 388 | |
| 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 Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 395 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu), |
Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 396 | Mips::AT_64).addReg(Mips::ZERO_64) |
| 397 | .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB); |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 398 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64) |
| 399 | .addReg(Mips::AT_64).addImm(16); |
Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 400 | |
| 401 | MIBundleBuilder(*LongBrMBB, Pos) |
Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 402 | .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 Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 408 | |
| 409 | Pos = BalTgtMBB->begin(); |
| 410 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 411 | 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 Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 415 | |
Vasileios Kalintiris | 0cf68df | 2016-06-18 15:39:43 +0000 | [diff] [blame^] | 416 | 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 Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 425 | } |
Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 426 | |
Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 427 | assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize); |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 428 | } else { |
| 429 | // $longbr: |
| 430 | // j $tgt |
| 431 | // nop |
| 432 | // $fallthrough: |
| 433 | // |
| 434 | Pos = LongBrMBB->begin(); |
| 435 | LongBrMBB->addSuccessor(TgtMBB); |
Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 436 | MIBundleBuilder(*LongBrMBB, Pos) |
| 437 | .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB)) |
| 438 | .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); |
Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 439 | |
| 440 | assert(LongBrMBB->size() == LongBranchSeqSize); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 443 | 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 Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 450 | replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static 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 | |
| 464 | bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) { |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 465 | const MipsSubtarget &STI = |
| 466 | static_cast<const MipsSubtarget &>(F.getSubtarget()); |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 467 | const MipsInstrInfo *TII = |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 468 | static_cast<const MipsInstrInfo *>(STI.getInstrInfo()); |
| 469 | LongBranchSeqSize = |
| 470 | !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10)); |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 471 | |
Eric Christopher | a08db01b | 2014-07-18 20:29:02 +0000 | [diff] [blame] | 472 | if (STI.inMips16Mode() || !STI.enableLongBranchPass()) |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 473 | return false; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 474 | if ((TM.getRelocationModel() == Reloc::PIC_) && |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 475 | static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() && |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 476 | F.getInfo<MipsFunctionInfo>()->globalBaseRegSet()) |
| 477 | emitGPDisp(F, TII); |
| 478 | |
| 479 | if (SkipLongBranch) |
Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 480 | return true; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 481 | |
| 482 | MF = &F; |
| 483 | initMBBInfo(); |
| 484 | |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 485 | SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end(); |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 486 | 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 Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 497 | int ShVal = STI.inMicroMipsMode() ? 2 : 4; |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 498 | int64_t Offset = computeOffset(I->Br) / ShVal; |
| 499 | |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 500 | if (STI.isTargetNaCl()) { |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 501 | // 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 Jovanovic | 9d86e26 | 2013-11-30 19:12:28 +0000 | [diff] [blame] | 507 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 508 | // Check if offset fits into 16-bit immediate field of branches. |
Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 509 | if (!ForceLongBranch && isInt<16>(Offset)) |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 510 | continue; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 511 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 512 | I->HasLongBranch = true; |
Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 513 | I->Size += LongBranchSeqSize * 4; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 514 | ++LongBranches; |
| 515 | EverMadeChange = MadeChange = true; |
| 516 | } |
| 517 | } |
| 518 | |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 519 | if (!EverMadeChange) |
| 520 | return true; |
| 521 | |
| 522 | // Compute basic block addresses. |
| 523 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 524 | uint64_t Address = 0; |
| 525 | |
Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 526 | for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I) |
Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 527 | 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 Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 536 | |
Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 537 | return true; |
Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 538 | } |