| 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); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 85 | void replaceBranch(MachineBasicBlock &MBB, Iter Br, DebugLoc DL, | 
|  | 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. | 
|  | 121 | static ReverseIter getNonDebugInstr(ReverseIter B, ReverseIter E) { | 
|  | 122 | for (; B != E; ++B) | 
|  | 123 | if (!B->isDebugValue()) | 
|  | 124 | return B; | 
|  | 125 |  | 
|  | 126 | return E; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | // Split MBB if it has two direct jumps/branches. | 
|  | 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, | 
|  | 221 | DebugLoc DL, MachineBasicBlock *MBBOpnd) { | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 222 | const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>( | 
|  | 223 | MBB.getParent()->getSubtarget().getInstrInfo()); | 
| Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 224 | unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode()); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 225 | const MCInstrDesc &NewDesc = TII->get(NewOpc); | 
|  | 226 |  | 
|  | 227 | MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc); | 
|  | 228 |  | 
|  | 229 | for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) { | 
|  | 230 | MachineOperand &MO = Br->getOperand(I); | 
|  | 231 |  | 
|  | 232 | if (!MO.isReg()) { | 
|  | 233 | assert(MO.isMBB() && "MBB operand expected."); | 
|  | 234 | break; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | MIB.addReg(MO.getReg()); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | MIB.addMBB(MBBOpnd); | 
|  | 241 |  | 
| Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 242 | if (Br->hasDelaySlot()) { | 
|  | 243 | // Bundle the instruction in the delay slot to the newly created branch | 
|  | 244 | // and erase the original branch. | 
|  | 245 | assert(Br->isBundledWithSucc()); | 
|  | 246 | MachineBasicBlock::instr_iterator II(Br); | 
|  | 247 | MIBundleBuilder(&*MIB).append((++II)->removeFromBundle()); | 
|  | 248 | } | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 249 | Br->eraseFromParent(); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // Expand branch instructions to long branches. | 
| Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 253 | // TODO: This function has to be fixed for beqz16 and bnez16, because it | 
|  | 254 | // currently assumes that all branches have 16-bit offsets, and will produce | 
|  | 255 | // wrong code if branches whose allowed offsets are [-128, -126, ..., 126] | 
|  | 256 | // are present. | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 257 | void MipsLongBranch::expandToLongBranch(MBBInfo &I) { | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 258 | MachineBasicBlock::iterator Pos; | 
|  | 259 | MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 260 | DebugLoc DL = I.Br->getDebugLoc(); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 261 | const BasicBlock *BB = MBB->getBasicBlock(); | 
|  | 262 | MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB); | 
|  | 263 | MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB); | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 264 | const MipsSubtarget &Subtarget = | 
|  | 265 | static_cast<const MipsSubtarget &>(MF->getSubtarget()); | 
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 266 | const MipsInstrInfo *TII = | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 267 | static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo()); | 
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 268 |  | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 269 | MF->insert(FallThroughMBB, LongBrMBB); | 
| Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 270 | MBB->replaceSuccessor(TgtMBB, LongBrMBB); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 271 |  | 
|  | 272 | if (IsPIC) { | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 273 | MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB); | 
|  | 274 | MF->insert(FallThroughMBB, BalTgtMBB); | 
|  | 275 | LongBrMBB->addSuccessor(BalTgtMBB); | 
|  | 276 | BalTgtMBB->addSuccessor(TgtMBB); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 277 |  | 
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 278 | // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal | 
|  | 279 | // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an | 
|  | 280 | // pseudo-instruction wrapping BGEZAL). | 
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 281 | unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR; | 
|  | 282 |  | 
| Daniel Sanders | e2e25da | 2014-10-24 16:15:27 +0000 | [diff] [blame] | 283 | if (!ABI.IsN64()) { | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 284 | // $longbr: | 
|  | 285 | //  addiu $sp, $sp, -8 | 
|  | 286 | //  sw $ra, 0($sp) | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 287 | //  lui $at, %hi($tgt - $baltgt) | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 288 | //  bal $baltgt | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 289 | //  addiu $at, $at, %lo($tgt - $baltgt) | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 290 | // $baltgt: | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 291 | //  addu $at, $ra, $at | 
|  | 292 | //  lw $ra, 0($sp) | 
|  | 293 | //  jr $at | 
|  | 294 | //  addiu $sp, $sp, 8 | 
|  | 295 | // $fallthrough: | 
|  | 296 | // | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 297 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 298 | Pos = LongBrMBB->begin(); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 299 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 300 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) | 
|  | 301 | .addReg(Mips::SP).addImm(-8); | 
|  | 302 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA) | 
|  | 303 | .addReg(Mips::SP).addImm(0); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 304 |  | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 305 | // LUi and ADDiu instructions create 32-bit offset of the target basic | 
|  | 306 | // block from the target of BAL instruction.  We cannot use immediate | 
|  | 307 | // value for this offset because it cannot be determined accurately when | 
|  | 308 | // the program has inline assembly statements.  We therefore use the | 
|  | 309 | // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which | 
|  | 310 | // are resolved during the fixup, so the values will always be correct. | 
|  | 311 | // | 
|  | 312 | // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt) | 
|  | 313 | // expressions at this point (it is possible only at the MC layer), | 
|  | 314 | // we replace LUi and ADDiu with pseudo instructions | 
|  | 315 | // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic | 
|  | 316 | // blocks as operands to these instructions.  When lowering these pseudo | 
|  | 317 | // instructions to LUi and ADDiu in the MC layer, we will create | 
|  | 318 | // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as | 
|  | 319 | // operands to lowered instructions. | 
|  | 320 |  | 
|  | 321 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT) | 
|  | 322 | .addMBB(TgtMBB).addMBB(BalTgtMBB); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 323 | MIBundleBuilder(*LongBrMBB, Pos) | 
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 324 | .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) | 
|  | 325 | .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT) | 
|  | 326 | .addReg(Mips::AT) | 
|  | 327 | .addMBB(TgtMBB) | 
|  | 328 | .addMBB(BalTgtMBB)); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 329 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 330 | Pos = BalTgtMBB->begin(); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 331 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 332 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT) | 
|  | 333 | .addReg(Mips::RA).addReg(Mips::AT); | 
|  | 334 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA) | 
|  | 335 | .addReg(Mips::SP).addImm(0); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 336 |  | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 337 | if (!Subtarget.isTargetNaCl()) { | 
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 338 | MIBundleBuilder(*BalTgtMBB, Pos) | 
|  | 339 | .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) | 
|  | 340 | .append(BuildMI(*MF, DL, TII->get(Mips::ADDiu), Mips::SP) | 
|  | 341 | .addReg(Mips::SP).addImm(8)); | 
|  | 342 | } else { | 
|  | 343 | // In NaCl, modifying the sp is not allowed in branch delay slot. | 
|  | 344 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) | 
|  | 345 | .addReg(Mips::SP).addImm(8); | 
|  | 346 |  | 
|  | 347 | MIBundleBuilder(*BalTgtMBB, Pos) | 
|  | 348 | .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) | 
|  | 349 | .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); | 
|  | 350 |  | 
|  | 351 | // Bundle-align the target of indirect branch JR. | 
|  | 352 | TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN); | 
|  | 353 | } | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 354 | } else { | 
|  | 355 | // $longbr: | 
|  | 356 | //  daddiu $sp, $sp, -16 | 
|  | 357 | //  sd $ra, 0($sp) | 
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 358 | //  daddiu $at, $zero, %hi($tgt - $baltgt) | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 359 | //  dsll $at, $at, 16 | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 360 | //  bal $baltgt | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 361 | //  daddiu $at, $at, %lo($tgt - $baltgt) | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 362 | // $baltgt: | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 363 | //  daddu $at, $ra, $at | 
|  | 364 | //  ld $ra, 0($sp) | 
|  | 365 | //  jr64 $at | 
|  | 366 | //  daddiu $sp, $sp, 16 | 
|  | 367 | // $fallthrough: | 
|  | 368 | // | 
|  | 369 |  | 
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 370 | // We assume the branch is within-function, and that offset is within | 
|  | 371 | // +/- 2GB.  High 32 bits will therefore always be zero. | 
|  | 372 |  | 
|  | 373 | // Note that this will work even if the offset is negative, because | 
|  | 374 | // of the +1 modification that's added in that case.  For example, if the | 
|  | 375 | // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is | 
|  | 376 | // | 
|  | 377 | // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000 | 
|  | 378 | // | 
|  | 379 | // and the bits [47:32] are zero.  For %highest | 
|  | 380 | // | 
|  | 381 | // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000 | 
|  | 382 | // | 
|  | 383 | // and the bits [63:48] are zero. | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 384 |  | 
|  | 385 | Pos = LongBrMBB->begin(); | 
|  | 386 |  | 
|  | 387 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64) | 
|  | 388 | .addReg(Mips::SP_64).addImm(-16); | 
|  | 389 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64) | 
|  | 390 | .addReg(Mips::SP_64).addImm(0); | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 391 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu), | 
| Sasa Stankovic | e41db2f | 2014-05-27 18:53:06 +0000 | [diff] [blame] | 392 | Mips::AT_64).addReg(Mips::ZERO_64) | 
|  | 393 | .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB); | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 394 | BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64) | 
|  | 395 | .addReg(Mips::AT_64).addImm(16); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 396 |  | 
|  | 397 | MIBundleBuilder(*LongBrMBB, Pos) | 
| Daniel Sanders | 86cb398 | 2014-06-13 13:02:52 +0000 | [diff] [blame] | 398 | .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) | 
|  | 399 | .append( | 
|  | 400 | BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64) | 
|  | 401 | .addReg(Mips::AT_64) | 
|  | 402 | .addMBB(TgtMBB, MipsII::MO_ABS_LO) | 
|  | 403 | .addMBB(BalTgtMBB)); | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 404 |  | 
|  | 405 | Pos = BalTgtMBB->begin(); | 
|  | 406 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 407 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64) | 
|  | 408 | .addReg(Mips::RA_64).addReg(Mips::AT_64); | 
|  | 409 | BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64) | 
|  | 410 | .addReg(Mips::SP_64).addImm(0); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 411 |  | 
|  | 412 | MIBundleBuilder(*BalTgtMBB, Pos) | 
|  | 413 | .append(BuildMI(*MF, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64)) | 
|  | 414 | .append(BuildMI(*MF, DL, TII->get(Mips::DADDiu), Mips::SP_64) | 
|  | 415 | .addReg(Mips::SP_64).addImm(16)); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 416 | } | 
| Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 417 |  | 
| Sasa Stankovic | 7b061a4 | 2014-04-30 15:06:25 +0000 | [diff] [blame] | 418 | assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize); | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 419 | } else { | 
|  | 420 | // $longbr: | 
|  | 421 | //  j $tgt | 
|  | 422 | //  nop | 
|  | 423 | // $fallthrough: | 
|  | 424 | // | 
|  | 425 | Pos = LongBrMBB->begin(); | 
|  | 426 | LongBrMBB->addSuccessor(TgtMBB); | 
| Jakob Stoklund Olesen | 97030e0 | 2012-12-07 04:23:40 +0000 | [diff] [blame] | 427 | MIBundleBuilder(*LongBrMBB, Pos) | 
|  | 428 | .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB)) | 
|  | 429 | .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); | 
| Akira Hatanaka | 5fdeac3 | 2012-11-15 20:05:11 +0000 | [diff] [blame] | 430 |  | 
|  | 431 | assert(LongBrMBB->size() == LongBranchSeqSize); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 432 | } | 
|  | 433 |  | 
| Akira Hatanaka | f72efdb | 2012-07-21 03:30:44 +0000 | [diff] [blame] | 434 | if (I.Br->isUnconditionalBranch()) { | 
|  | 435 | // Change branch destination. | 
|  | 436 | assert(I.Br->getDesc().getNumOperands() == 1); | 
|  | 437 | I.Br->RemoveOperand(0); | 
|  | 438 | I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB)); | 
|  | 439 | } else | 
|  | 440 | // Change branch destination and reverse condition. | 
| Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 441 | replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
|  | 444 | static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) { | 
|  | 445 | MachineBasicBlock &MBB = F.front(); | 
|  | 446 | MachineBasicBlock::iterator I = MBB.begin(); | 
|  | 447 | DebugLoc DL = MBB.findDebugLoc(MBB.begin()); | 
|  | 448 | BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0) | 
|  | 449 | .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI); | 
|  | 450 | BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0) | 
|  | 451 | .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO); | 
|  | 452 | MBB.removeLiveIn(Mips::V0); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) { | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 456 | const MipsSubtarget &STI = | 
|  | 457 | static_cast<const MipsSubtarget &>(F.getSubtarget()); | 
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 458 | const MipsInstrInfo *TII = | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 459 | static_cast<const MipsInstrInfo *>(STI.getInstrInfo()); | 
|  | 460 | LongBranchSeqSize = | 
|  | 461 | !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10)); | 
| Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 462 |  | 
| Eric Christopher | a08db01b | 2014-07-18 20:29:02 +0000 | [diff] [blame] | 463 | if (STI.inMips16Mode() || !STI.enableLongBranchPass()) | 
| Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 464 | return false; | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 465 | if ((TM.getRelocationModel() == Reloc::PIC_) && | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 466 | static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() && | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 467 | F.getInfo<MipsFunctionInfo>()->globalBaseRegSet()) | 
|  | 468 | emitGPDisp(F, TII); | 
|  | 469 |  | 
|  | 470 | if (SkipLongBranch) | 
| Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 471 | return true; | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 472 |  | 
|  | 473 | MF = &F; | 
|  | 474 | initMBBInfo(); | 
|  | 475 |  | 
| Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 476 | SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end(); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 477 | bool EverMadeChange = false, MadeChange = true; | 
|  | 478 |  | 
|  | 479 | while (MadeChange) { | 
|  | 480 | MadeChange = false; | 
|  | 481 |  | 
|  | 482 | for (I = MBBInfos.begin(); I != E; ++I) { | 
|  | 483 | // Skip if this MBB doesn't have a branch or the branch has already been | 
|  | 484 | // converted to a long branch. | 
|  | 485 | if (!I->Br || I->HasLongBranch) | 
|  | 486 | continue; | 
|  | 487 |  | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 488 | int ShVal = STI.inMicroMipsMode() ? 2 : 4; | 
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 489 | int64_t Offset = computeOffset(I->Br) / ShVal; | 
|  | 490 |  | 
| Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 491 | if (STI.isTargetNaCl()) { | 
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 492 | // The offset calculation does not include sandboxing instructions | 
|  | 493 | // that will be added later in the MC layer.  Since at this point we | 
|  | 494 | // don't know the exact amount of code that "sandboxing" will add, we | 
|  | 495 | // conservatively estimate that code will not grow more than 100%. | 
|  | 496 | Offset *= 2; | 
|  | 497 | } | 
| Zoran Jovanovic | 9d86e26 | 2013-11-30 19:12:28 +0000 | [diff] [blame] | 498 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 499 | // Check if offset fits into 16-bit immediate field of branches. | 
| Sasa Stankovic | 6781426 | 2014-06-05 13:52:08 +0000 | [diff] [blame] | 500 | if (!ForceLongBranch && isInt<16>(Offset)) | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 501 | continue; | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 502 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 503 | I->HasLongBranch = true; | 
| Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 504 | I->Size += LongBranchSeqSize * 4; | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 505 | ++LongBranches; | 
|  | 506 | EverMadeChange = MadeChange = true; | 
|  | 507 | } | 
|  | 508 | } | 
|  | 509 |  | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 510 | if (!EverMadeChange) | 
|  | 511 | return true; | 
|  | 512 |  | 
|  | 513 | // Compute basic block addresses. | 
|  | 514 | if (TM.getRelocationModel() == Reloc::PIC_) { | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 515 | uint64_t Address = 0; | 
|  | 516 |  | 
| Akira Hatanaka | 206cefe | 2012-08-28 18:58:57 +0000 | [diff] [blame] | 517 | for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I) | 
| Akira Hatanaka | b5af712 | 2012-08-28 03:03:05 +0000 | [diff] [blame] | 518 | I->Address = Address; | 
|  | 519 | } | 
|  | 520 |  | 
|  | 521 | // Do the expansion. | 
|  | 522 | for (I = MBBInfos.begin(); I != E; ++I) | 
|  | 523 | if (I->HasLongBranch) | 
|  | 524 | expandToLongBranch(*I); | 
|  | 525 |  | 
|  | 526 | MF->RenumberBlocks(); | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 527 |  | 
| Akira Hatanaka | 9f96bb8 | 2012-06-19 03:45:29 +0000 | [diff] [blame] | 528 | return true; | 
| Akira Hatanaka | a215929 | 2012-06-14 01:22:24 +0000 | [diff] [blame] | 529 | } |