| 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 | |
| 76 | private: |
| 77 | void splitMBB(MachineBasicBlock *MBB); |
| 78 | void initMBBInfo(); |
| 79 | int64_t computeOffset(const MachineInstr *Br); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 80 | void replaceBranch(MachineBasicBlock &MBB, Iter Br, DebugLoc DL, |
| 81 | MachineBasicBlock *MBBOpnd); |
| 82 | void expandToLongBranch(MBBInfo &Info); |
| 83 | |
| 84 | const TargetMachine &TM; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 85 | MachineFunction *MF; |
| 86 | SmallVector<MBBInfo, 16> MBBInfos; |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 87 | bool IsPIC; |
| Daniel Sanders | e2e25da | 2014-10-24 16:15:27 +0000 | [diff] [blame] | 88 | MipsABIInfo ABI; |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 89 | unsigned LongBranchSeqSize; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | char MipsLongBranch::ID = 0; |
| 93 | } // end of anonymous namespace |
| 94 | |
| 95 | /// createMipsLongBranchPass - Returns a pass that converts branches to long |
| 96 | /// branches. |
| 97 | FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) { |
| 98 | return new MipsLongBranch(tm); |
| 99 | } |
| 100 | |
| 101 | /// Iterate over list of Br's operands and search for a MachineBasicBlock |
| 102 | /// operand. |
| 103 | static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) { |
| 104 | for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) { |
| 105 | const MachineOperand &MO = Br.getOperand(I); |
| 106 | |
| 107 | if (MO.isMBB()) |
| 108 | return MO.getMBB(); |
| 109 | } |
| 110 | |
| Craig Topper | d3c02f1 | 2015-01-05 10:15:49 +0000 | [diff] [blame] | 111 | llvm_unreachable("This instruction does not have an MBB operand."); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Traverse the list of instructions backwards until a non-debug instruction is |
| 115 | // found or it reaches E. |
| 116 | static ReverseIter getNonDebugInstr(ReverseIter B, ReverseIter E) { |
| 117 | for (; B != E; ++B) |
| 118 | if (!B->isDebugValue()) |
| 119 | return B; |
| 120 | |
| 121 | return E; |
| 122 | } |
| 123 | |
| 124 | // Split MBB if it has two direct jumps/branches. |
| 125 | void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) { |
| 126 | ReverseIter End = MBB->rend(); |
| 127 | ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End); |
| 128 | |
| 129 | // Return if MBB has no branch instructions. |
| 130 | if ((LastBr == End) || |
| 131 | (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch())) |
| 132 | return; |
| 133 | |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 134 | ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 135 | |
| 136 | // MBB has only one branch instruction if FirstBr is not a branch |
| 137 | // instruction. |
| 138 | if ((FirstBr == End) || |
| 139 | (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch())) |
| 140 | return; |
| 141 | |
| 142 | assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found."); |
| 143 | |
| 144 | // Create a new MBB. Move instructions in MBB to the newly created MBB. |
| 145 | MachineBasicBlock *NewMBB = |
| 146 | MF->CreateMachineBasicBlock(MBB->getBasicBlock()); |
| 147 | |
| 148 | // Insert NewMBB and fix control flow. |
| 149 | MachineBasicBlock *Tgt = getTargetMBB(*FirstBr); |
| 150 | NewMBB->transferSuccessors(MBB); |
| 151 | NewMBB->removeSuccessor(Tgt); |
| 152 | MBB->addSuccessor(NewMBB); |
| 153 | MBB->addSuccessor(Tgt); |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 154 | MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 155 | |
| 156 | NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end()); |
| 157 | } |
| 158 | |
| 159 | // Fill MBBInfos. |
| 160 | void MipsLongBranch::initMBBInfo() { |
| 161 | // Split the MBBs if they have two branches. Each basic block should have at |
| 162 | // most one branch after this loop is executed. |
| 163 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E;) |
| 164 | splitMBB(I++); |
| 165 | |
| 166 | MF->RenumberBlocks(); |
| 167 | MBBInfos.clear(); |
| 168 | MBBInfos.resize(MF->size()); |
| 169 | |
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 170 | const MipsInstrInfo *TII = |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 171 | static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo()); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 172 | for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) { |
| 173 | MachineBasicBlock *MBB = MF->getBlockNumbered(I); |
| 174 | |
| 175 | // Compute size of MBB. |
| 176 | for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin(); |
| 177 | MI != MBB->instr_end(); ++MI) |
| 178 | MBBInfos[I].Size += TII->GetInstSizeInBytes(&*MI); |
| 179 | |
| 180 | // Search for MBB's branch instruction. |
| 181 | ReverseIter End = MBB->rend(); |
| 182 | ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End); |
| 183 | |
| 184 | if ((Br != End) && !Br->isIndirectBranch() && |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 185 | (Br->isConditionalBranch() || |
| 186 | (Br->isUnconditionalBranch() && |
| 187 | TM.getRelocationModel() == Reloc::PIC_))) |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 188 | MBBInfos[I].Br = (++Br).base(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Compute offset of branch in number of bytes. |
| 193 | int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) { |
| 194 | int64_t Offset = 0; |
| 195 | int ThisMBB = Br->getParent()->getNumber(); |
| 196 | int TargetMBB = getTargetMBB(*Br)->getNumber(); |
| 197 | |
| 198 | // Compute offset of a forward branch. |
| 199 | if (ThisMBB < TargetMBB) { |
| 200 | for (int N = ThisMBB + 1; N < TargetMBB; ++N) |
| 201 | Offset += MBBInfos[N].Size; |
| 202 | |
| 203 | return Offset + 4; |
| 204 | } |
| 205 | |
| 206 | // Compute offset of a backward branch. |
| 207 | for (int N = ThisMBB; N >= TargetMBB; --N) |
| 208 | Offset += MBBInfos[N].Size; |
| 209 | |
| 210 | return -Offset + 4; |
| 211 | } |
| 212 | |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 213 | // Replace Br with a branch which has the opposite condition code and a |
| 214 | // MachineBasicBlock operand MBBOpnd. |
| 215 | void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br, |
| 216 | DebugLoc DL, MachineBasicBlock *MBBOpnd) { |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 217 | const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>( |
| 218 | MBB.getParent()->getSubtarget().getInstrInfo()); |
| Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 219 | unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode()); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 220 | const MCInstrDesc &NewDesc = TII->get(NewOpc); |
| 221 | |
| 222 | MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc); |
| 223 | |
| 224 | for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) { |
| 225 | MachineOperand &MO = Br->getOperand(I); |
| 226 | |
| 227 | if (!MO.isReg()) { |
| 228 | assert(MO.isMBB() && "MBB operand expected."); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | MIB.addReg(MO.getReg()); |
| 233 | } |
| 234 | |
| 235 | MIB.addMBB(MBBOpnd); |
| 236 | |
| Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 237 | if (Br->hasDelaySlot()) { |
| 238 | // Bundle the instruction in the delay slot to the newly created branch |
| 239 | // and erase the original branch. |
| 240 | assert(Br->isBundledWithSucc()); |
| 241 | MachineBasicBlock::instr_iterator II(Br); |
| 242 | MIBundleBuilder(&*MIB).append((++II)->removeFromBundle()); |
| 243 | } |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 244 | Br->eraseFromParent(); |
| 245 | } |
| 246 | |
| 247 | // Expand branch instructions to long branches. |
| Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 248 | // TODO: This function has to be fixed for beqz16 and bnez16, because it |
| 249 | // currently assumes that all branches have 16-bit offsets, and will produce |
| 250 | // wrong code if branches whose allowed offsets are [-128, -126, ..., 126] |
| 251 | // are present. |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 252 | void MipsLongBranch::expandToLongBranch(MBBInfo &I) { |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 253 | MachineBasicBlock::iterator Pos; |
| 254 | MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 255 | DebugLoc DL = I.Br->getDebugLoc(); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 256 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 257 | MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB); |
| 258 | MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB); |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 259 | const MipsSubtarget &Subtarget = |
| 260 | static_cast<const MipsSubtarget &>(MF->getSubtarget()); |
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 261 | const MipsInstrInfo *TII = |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 262 | static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo()); |
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 263 | |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 264 | MF->insert(FallThroughMBB, LongBrMBB); |
| 265 | MBB->removeSuccessor(TgtMBB); |
| 266 | MBB->addSuccessor(LongBrMBB); |
| 267 | |
| 268 | if (IsPIC) { |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 269 | MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB); |
| 270 | MF->insert(FallThroughMBB, BalTgtMBB); |
| 271 | LongBrMBB->addSuccessor(BalTgtMBB); |
| 272 | BalTgtMBB->addSuccessor(TgtMBB); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 273 | |
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 274 | // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal |
| 275 | // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an |
| 276 | // pseudo-instruction wrapping BGEZAL). |
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 277 | unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR; |
| 278 | |
| Daniel Sanders | e2e25da | 2014-10-24 16:15:27 +0000 | [diff] [blame] | 279 | if (!ABI.IsN64()) { |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 280 | // $longbr: |
| 281 | // addiu $sp, $sp, -8 |
| 282 | // sw $ra, 0($sp) |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 283 | // lui $at, %hi($tgt - $baltgt) |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 284 | // bal $baltgt |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 285 | // addiu $at, $at, %lo($tgt - $baltgt) |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 286 | // $baltgt: |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 287 | // addu $at, $ra, $at |
| 288 | // lw $ra, 0($sp) |
| 289 | // jr $at |
| 290 | // addiu $sp, $sp, 8 |
| 291 | // $fallthrough: |
| 292 | // |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 293 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 294 | Pos = LongBrMBB->begin(); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 295 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 296 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) |
| 297 | .addReg(Mips::SP).addImm(-8); |
| 298 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA) |
| 299 | .addReg(Mips::SP).addImm(0); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 300 | |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 301 | // LUi and ADDiu instructions create 32-bit offset of the target basic |
| 302 | // block from the target of BAL instruction. We cannot use immediate |
| 303 | // value for this offset because it cannot be determined accurately when |
| 304 | // the program has inline assembly statements. We therefore use the |
| 305 | // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which |
| 306 | // are resolved during the fixup, so the values will always be correct. |
| 307 | // |
| 308 | // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt) |
| 309 | // expressions at this point (it is possible only at the MC layer), |
| 310 | // we replace LUi and ADDiu with pseudo instructions |
| 311 | // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic |
| 312 | // blocks as operands to these instructions. When lowering these pseudo |
| 313 | // instructions to LUi and ADDiu in the MC layer, we will create |
| 314 | // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as |
| 315 | // operands to lowered instructions. |
| 316 | |
| 317 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT) |
| 318 | .addMBB(TgtMBB).addMBB(BalTgtMBB); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 319 | MIBundleBuilder(*LongBrMBB, Pos) |
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 320 | .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) |
| 321 | .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT) |
| 322 | .addReg(Mips::AT) |
| 323 | .addMBB(TgtMBB) |
| 324 | .addMBB(BalTgtMBB)); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 325 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 326 | Pos = BalTgtMBB->begin(); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 327 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 328 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT) |
| 329 | .addReg(Mips::RA).addReg(Mips::AT); |
| 330 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA) |
| 331 | .addReg(Mips::SP).addImm(0); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 332 | |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 333 | if (!Subtarget.isTargetNaCl()) { |
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 334 | MIBundleBuilder(*BalTgtMBB, Pos) |
| 335 | .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) |
| 336 | .append(BuildMI(*MF, DL, TII->get(Mips::ADDiu), Mips::SP) |
| 337 | .addReg(Mips::SP).addImm(8)); |
| 338 | } else { |
| 339 | // In NaCl, modifying the sp is not allowed in branch delay slot. |
| 340 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) |
| 341 | .addReg(Mips::SP).addImm(8); |
| 342 | |
| 343 | MIBundleBuilder(*BalTgtMBB, Pos) |
| 344 | .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) |
| 345 | .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); |
| 346 | |
| 347 | // Bundle-align the target of indirect branch JR. |
| 348 | TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN); |
| 349 | } |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 350 | } else { |
| 351 | // $longbr: |
| 352 | // daddiu $sp, $sp, -16 |
| 353 | // sd $ra, 0($sp) |
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 354 | // daddiu $at, $zero, %hi($tgt - $baltgt) |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 355 | // dsll $at, $at, 16 |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 356 | // bal $baltgt |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 357 | // daddiu $at, $at, %lo($tgt - $baltgt) |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 358 | // $baltgt: |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 359 | // daddu $at, $ra, $at |
| 360 | // ld $ra, 0($sp) |
| 361 | // jr64 $at |
| 362 | // daddiu $sp, $sp, 16 |
| 363 | // $fallthrough: |
| 364 | // |
| 365 | |
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 366 | // We assume the branch is within-function, and that offset is within |
| 367 | // +/- 2GB. High 32 bits will therefore always be zero. |
| 368 | |
| 369 | // Note that this will work even if the offset is negative, because |
| 370 | // of the +1 modification that's added in that case. For example, if the |
| 371 | // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is |
| 372 | // |
| 373 | // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000 |
| 374 | // |
| 375 | // and the bits [47:32] are zero. For %highest |
| 376 | // |
| 377 | // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000 |
| 378 | // |
| 379 | // and the bits [63:48] are zero. |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 380 | |
| 381 | Pos = LongBrMBB->begin(); |
| 382 | |
| 383 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64) |
| 384 | .addReg(Mips::SP_64).addImm(-16); |
| 385 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64) |
| 386 | .addReg(Mips::SP_64).addImm(0); |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 387 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu), |
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 388 | Mips::AT_64).addReg(Mips::ZERO_64) |
| 389 | .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB); |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 390 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64) |
| 391 | .addReg(Mips::AT_64).addImm(16); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 392 | |
| 393 | MIBundleBuilder(*LongBrMBB, Pos) |
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 394 | .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) |
| 395 | .append( |
| 396 | BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64) |
| 397 | .addReg(Mips::AT_64) |
| 398 | .addMBB(TgtMBB, MipsII::MO_ABS_LO) |
| 399 | .addMBB(BalTgtMBB)); |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 400 | |
| 401 | Pos = BalTgtMBB->begin(); |
| 402 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 403 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64) |
| 404 | .addReg(Mips::RA_64).addReg(Mips::AT_64); |
| 405 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64) |
| 406 | .addReg(Mips::SP_64).addImm(0); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 407 | |
| 408 | MIBundleBuilder(*BalTgtMBB, Pos) |
| 409 | .append(BuildMI(*MF, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64)) |
| 410 | .append(BuildMI(*MF, DL, TII->get(Mips::DADDiu), Mips::SP_64) |
| 411 | .addReg(Mips::SP_64).addImm(16)); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 412 | } |
| Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 413 | |
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 414 | assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize); |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 415 | } else { |
| 416 | // $longbr: |
| 417 | // j $tgt |
| 418 | // nop |
| 419 | // $fallthrough: |
| 420 | // |
| 421 | Pos = LongBrMBB->begin(); |
| 422 | LongBrMBB->addSuccessor(TgtMBB); |
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 423 | MIBundleBuilder(*LongBrMBB, Pos) |
| 424 | .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB)) |
| 425 | .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); |
| Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 426 | |
| 427 | assert(LongBrMBB->size() == LongBranchSeqSize); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 430 | if (I.Br->isUnconditionalBranch()) { |
| 431 | // Change branch destination. |
| 432 | assert(I.Br->getDesc().getNumOperands() == 1); |
| 433 | I.Br->RemoveOperand(0); |
| 434 | I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB)); |
| 435 | } else |
| 436 | // Change branch destination and reverse condition. |
| 437 | replaceBranch(*MBB, I.Br, DL, FallThroughMBB); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) { |
| 441 | MachineBasicBlock &MBB = F.front(); |
| 442 | MachineBasicBlock::iterator I = MBB.begin(); |
| 443 | DebugLoc DL = MBB.findDebugLoc(MBB.begin()); |
| 444 | BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0) |
| 445 | .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI); |
| 446 | BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0) |
| 447 | .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO); |
| 448 | MBB.removeLiveIn(Mips::V0); |
| 449 | } |
| 450 | |
| 451 | bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) { |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 452 | const MipsSubtarget &STI = |
| 453 | static_cast<const MipsSubtarget &>(F.getSubtarget()); |
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 454 | const MipsInstrInfo *TII = |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 455 | static_cast<const MipsInstrInfo *>(STI.getInstrInfo()); |
| 456 | LongBranchSeqSize = |
| 457 | !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10)); |
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 458 | |
| Eric Christopher | a08db01b | 2014-07-18 20:29:02 +0000 | [diff] [blame] | 459 | if (STI.inMips16Mode() || !STI.enableLongBranchPass()) |
| Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 460 | return false; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 461 | if ((TM.getRelocationModel() == Reloc::PIC_) && |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 462 | static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() && |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 463 | F.getInfo<MipsFunctionInfo>()->globalBaseRegSet()) |
| 464 | emitGPDisp(F, TII); |
| 465 | |
| 466 | if (SkipLongBranch) |
| Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 467 | return true; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 468 | |
| 469 | MF = &F; |
| 470 | initMBBInfo(); |
| 471 | |
| Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 472 | SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end(); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 473 | bool EverMadeChange = false, MadeChange = true; |
| 474 | |
| 475 | while (MadeChange) { |
| 476 | MadeChange = false; |
| 477 | |
| 478 | for (I = MBBInfos.begin(); I != E; ++I) { |
| 479 | // Skip if this MBB doesn't have a branch or the branch has already been |
| 480 | // converted to a long branch. |
| 481 | if (!I->Br || I->HasLongBranch) |
| 482 | continue; |
| 483 | |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 484 | int ShVal = STI.inMicroMipsMode() ? 2 : 4; |
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 485 | int64_t Offset = computeOffset(I->Br) / ShVal; |
| 486 | |
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 487 | if (STI.isTargetNaCl()) { |
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 488 | // The offset calculation does not include sandboxing instructions |
| 489 | // that will be added later in the MC layer. Since at this point we |
| 490 | // don't know the exact amount of code that "sandboxing" will add, we |
| 491 | // conservatively estimate that code will not grow more than 100%. |
| 492 | Offset *= 2; |
| 493 | } |
| Zoran Jovanovic | 9d86e26 | 2013-11-30 19:12:28 +0000 | [diff] [blame] | 494 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 495 | // Check if offset fits into 16-bit immediate field of branches. |
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 496 | if (!ForceLongBranch && isInt<16>(Offset)) |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 497 | continue; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 498 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 499 | I->HasLongBranch = true; |
| Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 500 | I->Size += LongBranchSeqSize * 4; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 501 | ++LongBranches; |
| 502 | EverMadeChange = MadeChange = true; |
| 503 | } |
| 504 | } |
| 505 | |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 506 | if (!EverMadeChange) |
| 507 | return true; |
| 508 | |
| 509 | // Compute basic block addresses. |
| 510 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 511 | uint64_t Address = 0; |
| 512 | |
| Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 513 | for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I) |
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 514 | I->Address = Address; |
| 515 | } |
| 516 | |
| 517 | // Do the expansion. |
| 518 | for (I = MBBInfos.begin(); I != E; ++I) |
| 519 | if (I->HasLongBranch) |
| 520 | expandToLongBranch(*I); |
| 521 | |
| 522 | MF->RenumberBlocks(); |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 523 | |
| Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 524 | return true; |
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 525 | } |