Reed Kotler | 5bf8020 | 2013-02-27 04:20:14 +0000 | [diff] [blame] | 1 | //===-- MipsConstantIslandPass.cpp - Emit Pc Relative loads----------------===// |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 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 | // |
| 11 | // This pass is used to make Pc relative loads of constants. |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 12 | // For now, only Mips16 will use this. |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 13 | // |
| 14 | // Loading constants inline is expensive on Mips16 and it's in general better |
| 15 | // to place the constant nearby in code space and then it can be loaded with a |
| 16 | // simple 16 bit load instruction. |
| 17 | // |
| 18 | // The constants can be not just numbers but addresses of functions and labels. |
| 19 | // This can be particularly helpful in static relocation mode for embedded |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 20 | // non-linux targets. |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 21 | // |
| 22 | // |
| 23 | |
| 24 | #define DEBUG_TYPE "mips-constant-islands" |
| 25 | |
| 26 | #include "Mips.h" |
| 27 | #include "MCTargetDesc/MipsBaseInfo.h" |
Reed Kotler | 5c8ae09 | 2013-11-13 04:37:52 +0000 | [diff] [blame] | 28 | #include "Mips16InstrInfo.h" |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 29 | #include "MipsMachineFunction.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 30 | #include "MipsTargetMachine.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 34 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Function.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame^] | 37 | #include "llvm/IR/InstIterator.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Format.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MathExtras.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetInstrInfo.h" |
| 44 | #include "llvm/Target/TargetMachine.h" |
| 45 | #include "llvm/Target/TargetRegisterInfo.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 46 | #include <algorithm> |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 47 | |
| 48 | using namespace llvm; |
| 49 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 50 | STATISTIC(NumCPEs, "Number of constpool entries"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 51 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 52 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 53 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 54 | |
| 55 | // FIXME: This option should be removed once it has received sufficient testing. |
| 56 | static cl::opt<bool> |
| 57 | AlignConstantIslands("mips-align-constant-islands", cl::Hidden, cl::init(true), |
| 58 | cl::desc("Align constant islands in code")); |
| 59 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 60 | |
| 61 | // Rather than do make check tests with huge amounts of code, we force |
| 62 | // the test to use this amount. |
| 63 | // |
| 64 | static cl::opt<int> ConstantIslandsSmallOffset( |
| 65 | "mips-constant-islands-small-offset", |
| 66 | cl::init(0), |
| 67 | cl::desc("Make small offsets be this amount for testing purposes"), |
| 68 | cl::Hidden); |
| 69 | |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 70 | // |
| 71 | // For testing purposes we tell it to not use relaxed load forms so that it |
| 72 | // will split blocks. |
| 73 | // |
| 74 | static cl::opt<bool> NoLoadRelaxation( |
| 75 | "mips-constant-islands-no-load-relaxation", |
| 76 | cl::init(false), |
| 77 | cl::desc("Don't relax loads to long loads - for testing purposes"), |
| 78 | cl::Hidden); |
| 79 | |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 80 | static unsigned int branchTargetOperand(MachineInstr *MI) { |
| 81 | switch (MI->getOpcode()) { |
| 82 | case Mips::Bimm16: |
| 83 | case Mips::BimmX16: |
| 84 | case Mips::Bteqz16: |
| 85 | case Mips::BteqzX16: |
| 86 | case Mips::Btnez16: |
| 87 | case Mips::BtnezX16: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 88 | case Mips::JalB16: |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | case Mips::BeqzRxImm16: |
| 91 | case Mips::BeqzRxImmX16: |
| 92 | case Mips::BnezRxImm16: |
| 93 | case Mips::BnezRxImmX16: |
| 94 | return 1; |
| 95 | } |
| 96 | llvm_unreachable("Unknown branch type"); |
| 97 | } |
| 98 | |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 99 | static bool isUnconditionalBranch(unsigned int Opcode) { |
| 100 | switch (Opcode) { |
| 101 | default: return false; |
| 102 | case Mips::Bimm16: |
| 103 | case Mips::BimmX16: |
| 104 | case Mips::JalB16: |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 109 | static unsigned int longformBranchOpcode(unsigned int Opcode) { |
| 110 | switch (Opcode) { |
| 111 | case Mips::Bimm16: |
| 112 | case Mips::BimmX16: |
| 113 | return Mips::BimmX16; |
| 114 | case Mips::Bteqz16: |
| 115 | case Mips::BteqzX16: |
| 116 | return Mips::BteqzX16; |
| 117 | case Mips::Btnez16: |
| 118 | case Mips::BtnezX16: |
| 119 | return Mips::BtnezX16; |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 120 | case Mips::JalB16: |
| 121 | return Mips::JalB16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 122 | case Mips::BeqzRxImm16: |
| 123 | case Mips::BeqzRxImmX16: |
| 124 | return Mips::BeqzRxImmX16; |
| 125 | case Mips::BnezRxImm16: |
| 126 | case Mips::BnezRxImmX16: |
| 127 | return Mips::BnezRxImmX16; |
| 128 | } |
| 129 | llvm_unreachable("Unknown branch type"); |
| 130 | } |
| 131 | |
| 132 | // |
| 133 | // FIXME: need to go through this whole constant islands port and check the math |
| 134 | // for branch ranges and clean this up and make some functions to calculate things |
| 135 | // that are done many times identically. |
| 136 | // Need to refactor some of the code to call this routine. |
| 137 | // |
| 138 | static unsigned int branchMaxOffsets(unsigned int Opcode) { |
| 139 | unsigned Bits, Scale; |
| 140 | switch (Opcode) { |
| 141 | case Mips::Bimm16: |
| 142 | Bits = 11; |
| 143 | Scale = 2; |
| 144 | break; |
| 145 | case Mips::BimmX16: |
| 146 | Bits = 16; |
| 147 | Scale = 2; |
| 148 | break; |
| 149 | case Mips::BeqzRxImm16: |
| 150 | Bits = 8; |
| 151 | Scale = 2; |
| 152 | break; |
| 153 | case Mips::BeqzRxImmX16: |
| 154 | Bits = 16; |
| 155 | Scale = 2; |
| 156 | break; |
| 157 | case Mips::BnezRxImm16: |
| 158 | Bits = 8; |
| 159 | Scale = 2; |
| 160 | break; |
| 161 | case Mips::BnezRxImmX16: |
| 162 | Bits = 16; |
| 163 | Scale = 2; |
| 164 | break; |
| 165 | case Mips::Bteqz16: |
| 166 | Bits = 8; |
| 167 | Scale = 2; |
| 168 | break; |
| 169 | case Mips::BteqzX16: |
| 170 | Bits = 16; |
| 171 | Scale = 2; |
| 172 | break; |
| 173 | case Mips::Btnez16: |
| 174 | Bits = 8; |
| 175 | Scale = 2; |
| 176 | break; |
| 177 | case Mips::BtnezX16: |
| 178 | Bits = 16; |
| 179 | Scale = 2; |
| 180 | break; |
| 181 | default: |
| 182 | llvm_unreachable("Unknown branch type"); |
| 183 | } |
| 184 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 185 | return MaxOffs; |
| 186 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 187 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 188 | namespace { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 189 | |
| 190 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 191 | typedef MachineBasicBlock::iterator Iter; |
| 192 | typedef MachineBasicBlock::reverse_iterator ReverseIter; |
| 193 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 194 | /// MipsConstantIslands - Due to limited PC-relative displacements, Mips |
| 195 | /// requires constant pool entries to be scattered among the instructions |
| 196 | /// inside a function. To do this, it completely ignores the normal LLVM |
| 197 | /// constant pool; instead, it places constants wherever it feels like with |
| 198 | /// special instructions. |
| 199 | /// |
| 200 | /// The terminology used in this pass includes: |
| 201 | /// Islands - Clumps of constants placed in the function. |
| 202 | /// Water - Potential places where an island could be formed. |
| 203 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 204 | /// tracks a list of users. |
| 205 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 206 | class MipsConstantIslands : public MachineFunctionPass { |
| 207 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 208 | /// BasicBlockInfo - Information about the offset and size of a single |
| 209 | /// basic block. |
| 210 | struct BasicBlockInfo { |
| 211 | /// Offset - Distance from the beginning of the function to the beginning |
| 212 | /// of this basic block. |
| 213 | /// |
| 214 | /// Offsets are computed assuming worst case padding before an aligned |
| 215 | /// block. This means that subtracting basic block offsets always gives a |
| 216 | /// conservative estimate of the real distance which may be smaller. |
| 217 | /// |
| 218 | /// Because worst case padding is used, the computed offset of an aligned |
| 219 | /// block may not actually be aligned. |
| 220 | unsigned Offset; |
| 221 | |
| 222 | /// Size - Size of the basic block in bytes. If the block contains |
| 223 | /// inline assembly, this is a worst case estimate. |
| 224 | /// |
| 225 | /// The size does not include any alignment padding whether from the |
| 226 | /// beginning of the block, or from an aligned jump table at the end. |
| 227 | unsigned Size; |
| 228 | |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 229 | // FIXME: ignore LogAlign for this patch |
| 230 | // |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 231 | unsigned postOffset(unsigned LogAlign = 0) const { |
| 232 | unsigned PO = Offset + Size; |
| 233 | return PO; |
| 234 | } |
| 235 | |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 236 | BasicBlockInfo() : Offset(0), Size(0) {} |
| 237 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | std::vector<BasicBlockInfo> BBInfo; |
| 241 | |
| 242 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 243 | /// (i.e. blocks that don't fall through to the following block, due |
| 244 | /// to a return, unreachable, or unconditional branch). |
| 245 | std::vector<MachineBasicBlock*> WaterList; |
| 246 | |
| 247 | /// NewWaterList - The subset of WaterList that was created since the |
| 248 | /// previous iteration by inserting unconditional branches. |
| 249 | SmallSet<MachineBasicBlock*, 4> NewWaterList; |
| 250 | |
| 251 | typedef std::vector<MachineBasicBlock*>::iterator water_iterator; |
| 252 | |
| 253 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 254 | /// pointer, the constant pool being referenced, and the max displacement |
| 255 | /// allowed from the instruction to the CP. The HighWaterMark records the |
| 256 | /// highest basic block where a new CPEntry can be placed. To ensure this |
| 257 | /// pass terminates, the CP entries are initially placed at the end of the |
| 258 | /// function and then move monotonically to lower addresses. The |
| 259 | /// exception to this rule is when the current CP entry for a particular |
| 260 | /// CPUser is out of range, but there is another CP entry for the same |
| 261 | /// constant value in range. We want to use the existing in-range CP |
| 262 | /// entry, but if it later moves out of range, the search for new water |
| 263 | /// should resume where it left off. The HighWaterMark is used to record |
| 264 | /// that point. |
| 265 | struct CPUser { |
| 266 | MachineInstr *MI; |
| 267 | MachineInstr *CPEMI; |
| 268 | MachineBasicBlock *HighWaterMark; |
| 269 | private: |
| 270 | unsigned MaxDisp; |
| 271 | unsigned LongFormMaxDisp; // mips16 has 16/32 bit instructions |
| 272 | // with different displacements |
| 273 | unsigned LongFormOpcode; |
| 274 | public: |
| 275 | bool NegOk; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 276 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 277 | bool neg, |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 278 | unsigned longformmaxdisp, unsigned longformopcode) |
| 279 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), |
| 280 | LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode), |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 281 | NegOk(neg){ |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 282 | HighWaterMark = CPEMI->getParent(); |
| 283 | } |
| 284 | /// getMaxDisp - Returns the maximum displacement supported by MI. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 285 | unsigned getMaxDisp() const { |
| 286 | unsigned xMaxDisp = ConstantIslandsSmallOffset? |
| 287 | ConstantIslandsSmallOffset: MaxDisp; |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 288 | return xMaxDisp; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 289 | } |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 290 | void setMaxDisp(unsigned val) { |
| 291 | MaxDisp = val; |
| 292 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 293 | unsigned getLongFormMaxDisp() const { |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 294 | return LongFormMaxDisp; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 295 | } |
| 296 | unsigned getLongFormOpcode() const { |
| 297 | return LongFormOpcode; |
| 298 | } |
| 299 | }; |
| 300 | |
| 301 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 302 | /// constant pools and their max displacement. |
| 303 | std::vector<CPUser> CPUsers; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 304 | |
| 305 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 306 | /// pointer, the constpool index, and the number of CPUser's which |
| 307 | /// reference this entry. |
| 308 | struct CPEntry { |
| 309 | MachineInstr *CPEMI; |
| 310 | unsigned CPI; |
| 311 | unsigned RefCount; |
| 312 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 313 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 314 | }; |
| 315 | |
| 316 | /// CPEntries - Keep track of all of the constant pool entry machine |
| 317 | /// instructions. For each original constpool index (i.e. those that |
| 318 | /// existed upon entry to this pass), it keeps a vector of entries. |
| 319 | /// Original elements are cloned as we go along; the clones are |
| 320 | /// put in the vector of the original element, but have distinct CPIs. |
| 321 | std::vector<std::vector<CPEntry> > CPEntries; |
| 322 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 323 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 324 | /// pointer, conditional or unconditional, the max displacement, |
| 325 | /// and (if isCond is true) the corresponding unconditional branch |
| 326 | /// opcode. |
| 327 | struct ImmBranch { |
| 328 | MachineInstr *MI; |
| 329 | unsigned MaxDisp : 31; |
| 330 | bool isCond : 1; |
| 331 | int UncondBr; |
| 332 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 333 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
| 334 | }; |
| 335 | |
| 336 | /// ImmBranches - Keep track of all the immediate branch instructions. |
| 337 | /// |
| 338 | std::vector<ImmBranch> ImmBranches; |
| 339 | |
| 340 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 341 | /// the branch fix up pass. |
| 342 | bool HasFarJump; |
| 343 | |
| 344 | const TargetMachine &TM; |
| 345 | bool IsPIC; |
| 346 | unsigned ABI; |
| 347 | const MipsSubtarget *STI; |
Reed Kotler | 5c8ae09 | 2013-11-13 04:37:52 +0000 | [diff] [blame] | 348 | const Mips16InstrInfo *TII; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 349 | MipsFunctionInfo *MFI; |
| 350 | MachineFunction *MF; |
| 351 | MachineConstantPool *MCP; |
| 352 | |
| 353 | unsigned PICLabelUId; |
| 354 | bool PrescannedForConstants; |
| 355 | |
| 356 | void initPICLabelUId(unsigned UId) { |
| 357 | PICLabelUId = UId; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | unsigned createPICLabelUId() { |
| 362 | return PICLabelUId++; |
| 363 | } |
| 364 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 365 | public: |
| 366 | static char ID; |
| 367 | MipsConstantIslands(TargetMachine &tm) |
| 368 | : MachineFunctionPass(ID), TM(tm), |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 369 | IsPIC(TM.getRelocationModel() == Reloc::PIC_), |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 370 | ABI(TM.getSubtarget<MipsSubtarget>().getTargetABI()), |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 371 | STI(&TM.getSubtarget<MipsSubtarget>()), MF(0), MCP(0), |
| 372 | PrescannedForConstants(false){} |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 373 | |
| 374 | virtual const char *getPassName() const { |
| 375 | return "Mips Constant Islands"; |
| 376 | } |
| 377 | |
| 378 | bool runOnMachineFunction(MachineFunction &F); |
| 379 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 380 | void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 381 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
| 382 | unsigned getCPELogAlign(const MachineInstr *CPEMI); |
| 383 | void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs); |
| 384 | unsigned getOffsetOf(MachineInstr *MI) const; |
| 385 | unsigned getUserOffset(CPUser&) const; |
| 386 | void dumpBBs(); |
| 387 | void verify(); |
| 388 | |
| 389 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 390 | unsigned Disp, bool NegativeOK); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 391 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
| 392 | const CPUser &U); |
| 393 | |
| 394 | bool isLongFormOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
| 395 | const CPUser &U); |
| 396 | |
| 397 | void computeBlockSize(MachineBasicBlock *MBB); |
| 398 | MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI); |
| 399 | void updateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
| 400 | void adjustBBOffsetsAfter(MachineBasicBlock *BB); |
| 401 | bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI); |
| 402 | int findInRangeCPEntry(CPUser& U, unsigned UserOffset); |
| 403 | int findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset); |
| 404 | bool findAvailableWater(CPUser&U, unsigned UserOffset, |
| 405 | water_iterator &WaterIter); |
| 406 | void createNewWater(unsigned CPUserIndex, unsigned UserOffset, |
| 407 | MachineBasicBlock *&NewMBB); |
| 408 | bool handleConstantPoolUser(unsigned CPUserIndex); |
| 409 | void removeDeadCPEMI(MachineInstr *CPEMI); |
| 410 | bool removeUnusedCPEntries(); |
| 411 | bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, |
| 412 | MachineInstr *CPEMI, unsigned Disp, bool NegOk, |
| 413 | bool DoDump = false); |
| 414 | bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water, |
| 415 | CPUser &U, unsigned &Growth); |
| 416 | bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
| 417 | bool fixupImmediateBr(ImmBranch &Br); |
| 418 | bool fixupConditionalBr(ImmBranch &Br); |
| 419 | bool fixupUnconditionalBr(ImmBranch &Br); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 420 | |
| 421 | void prescanForConstants(); |
| 422 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 423 | private: |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 424 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | char MipsConstantIslands::ID = 0; |
| 428 | } // end of anonymous namespace |
| 429 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 430 | |
| 431 | bool MipsConstantIslands::isLongFormOffsetInRange |
| 432 | (unsigned UserOffset, unsigned TrialOffset, |
| 433 | const CPUser &U) { |
| 434 | return isOffsetInRange(UserOffset, TrialOffset, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 435 | U.getLongFormMaxDisp(), U.NegOk); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | bool MipsConstantIslands::isOffsetInRange |
| 439 | (unsigned UserOffset, unsigned TrialOffset, |
| 440 | const CPUser &U) { |
| 441 | return isOffsetInRange(UserOffset, TrialOffset, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 442 | U.getMaxDisp(), U.NegOk); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 443 | } |
| 444 | /// print block size and offset information - debugging |
| 445 | void MipsConstantIslands::dumpBBs() { |
| 446 | DEBUG({ |
| 447 | for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) { |
| 448 | const BasicBlockInfo &BBI = BBInfo[J]; |
| 449 | dbgs() << format("%08x BB#%u\t", BBI.Offset, J) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 450 | << format(" size=%#x\n", BBInfo[J].Size); |
| 451 | } |
| 452 | }); |
| 453 | } |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 454 | /// createMipsLongBranchPass - Returns a pass that converts branches to long |
| 455 | /// branches. |
| 456 | FunctionPass *llvm::createMipsConstantIslandPass(MipsTargetMachine &tm) { |
| 457 | return new MipsConstantIslands(tm); |
| 458 | } |
| 459 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 460 | bool MipsConstantIslands::runOnMachineFunction(MachineFunction &mf) { |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 461 | // The intention is for this to be a mips16 only pass for now |
| 462 | // FIXME: |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 463 | MF = &mf; |
| 464 | MCP = mf.getConstantPool(); |
| 465 | DEBUG(dbgs() << "constant island machine function " << "\n"); |
| 466 | if (!TM.getSubtarget<MipsSubtarget>().inMips16Mode() || |
| 467 | !MipsSubtarget::useConstantIslands()) { |
| 468 | return false; |
| 469 | } |
Reed Kotler | 5c8ae09 | 2013-11-13 04:37:52 +0000 | [diff] [blame] | 470 | TII = (const Mips16InstrInfo*)MF->getTarget().getInstrInfo(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 471 | MFI = MF->getInfo<MipsFunctionInfo>(); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 472 | DEBUG(dbgs() << "constant island processing " << "\n"); |
| 473 | // |
| 474 | // will need to make predermination if there is any constants we need to |
| 475 | // put in constant islands. TBD. |
| 476 | // |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 477 | if (!PrescannedForConstants) prescanForConstants(); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 478 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 479 | HasFarJump = false; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 480 | // This pass invalidates liveness information when it splits basic blocks. |
| 481 | MF->getRegInfo().invalidateLiveness(); |
| 482 | |
| 483 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 484 | // the numbers agree with the position of the block in the function. |
| 485 | MF->RenumberBlocks(); |
| 486 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 487 | bool MadeChange = false; |
| 488 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 489 | // Perform the initial placement of the constant pool entries. To start with, |
| 490 | // we put them all at the end of the function. |
| 491 | std::vector<MachineInstr*> CPEMIs; |
| 492 | if (!MCP->isEmpty()) |
| 493 | doInitialPlacement(CPEMIs); |
| 494 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 495 | /// The next UID to take is the first unused one. |
| 496 | initPICLabelUId(CPEMIs.size()); |
| 497 | |
| 498 | // Do the initial scan of the function, building up information about the |
| 499 | // sizes of each block, the location of all the water, and finding all of the |
| 500 | // constant pool users. |
| 501 | initializeFunctionInfo(CPEMIs); |
| 502 | CPEMIs.clear(); |
| 503 | DEBUG(dumpBBs()); |
| 504 | |
| 505 | /// Remove dead constant pool entries. |
| 506 | MadeChange |= removeUnusedCPEntries(); |
| 507 | |
| 508 | // Iteratively place constant pool entries and fix up branches until there |
| 509 | // is no change. |
| 510 | unsigned NoCPIters = 0, NoBRIters = 0; |
| 511 | (void)NoBRIters; |
| 512 | while (true) { |
| 513 | DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n'); |
| 514 | bool CPChange = false; |
| 515 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
| 516 | CPChange |= handleConstantPoolUser(i); |
| 517 | if (CPChange && ++NoCPIters > 30) |
| 518 | report_fatal_error("Constant Island pass failed to converge!"); |
| 519 | DEBUG(dumpBBs()); |
| 520 | |
| 521 | // Clear NewWaterList now. If we split a block for branches, it should |
| 522 | // appear as "new water" for the next iteration of constant pool placement. |
| 523 | NewWaterList.clear(); |
| 524 | |
| 525 | DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n'); |
| 526 | bool BRChange = false; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 527 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
| 528 | BRChange |= fixupImmediateBr(ImmBranches[i]); |
| 529 | if (BRChange && ++NoBRIters > 30) |
| 530 | report_fatal_error("Branch Fix Up pass failed to converge!"); |
| 531 | DEBUG(dumpBBs()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 532 | if (!CPChange && !BRChange) |
| 533 | break; |
| 534 | MadeChange = true; |
| 535 | } |
| 536 | |
| 537 | DEBUG(dbgs() << '\n'; dumpBBs()); |
| 538 | |
| 539 | BBInfo.clear(); |
| 540 | WaterList.clear(); |
| 541 | CPUsers.clear(); |
| 542 | CPEntries.clear(); |
| 543 | ImmBranches.clear(); |
| 544 | return MadeChange; |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 547 | /// doInitialPlacement - Perform the initial placement of the constant pool |
| 548 | /// entries. To start with, we put them all at the end of the function. |
| 549 | void |
| 550 | MipsConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) { |
| 551 | // Create the basic block to hold the CPE's. |
| 552 | MachineBasicBlock *BB = MF->CreateMachineBasicBlock(); |
| 553 | MF->push_back(BB); |
| 554 | |
| 555 | |
| 556 | // MachineConstantPool measures alignment in bytes. We measure in log2(bytes). |
| 557 | unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment()); |
| 558 | |
| 559 | // Mark the basic block as required by the const-pool. |
| 560 | // If AlignConstantIslands isn't set, use 4-byte alignment for everything. |
| 561 | BB->setAlignment(AlignConstantIslands ? MaxAlign : 2); |
| 562 | |
| 563 | // The function needs to be as aligned as the basic blocks. The linker may |
| 564 | // move functions around based on their alignment. |
| 565 | MF->ensureAlignment(BB->getAlignment()); |
| 566 | |
| 567 | // Order the entries in BB by descending alignment. That ensures correct |
| 568 | // alignment of all entries as long as BB is sufficiently aligned. Keep |
| 569 | // track of the insertion point for each alignment. We are going to bucket |
| 570 | // sort the entries as they are created. |
| 571 | SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end()); |
| 572 | |
| 573 | // Add all of the constants from the constant pool to the end block, use an |
| 574 | // identity mapping of CPI's to CPE's. |
| 575 | const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants(); |
| 576 | |
| 577 | const DataLayout &TD = *MF->getTarget().getDataLayout(); |
| 578 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
| 579 | unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); |
| 580 | assert(Size >= 4 && "Too small constant pool entry"); |
| 581 | unsigned Align = CPs[i].getAlignment(); |
| 582 | assert(isPowerOf2_32(Align) && "Invalid alignment"); |
| 583 | // Verify that all constant pool entries are a multiple of their alignment. |
| 584 | // If not, we would have to pad them out so that instructions stay aligned. |
| 585 | assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 586 | |
| 587 | // Insert CONSTPOOL_ENTRY before entries with a smaller alignment. |
| 588 | unsigned LogAlign = Log2_32(Align); |
| 589 | MachineBasicBlock::iterator InsAt = InsPoint[LogAlign]; |
| 590 | |
| 591 | MachineInstr *CPEMI = |
| 592 | BuildMI(*BB, InsAt, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY)) |
| 593 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| 594 | |
| 595 | CPEMIs.push_back(CPEMI); |
| 596 | |
| 597 | // Ensure that future entries with higher alignment get inserted before |
| 598 | // CPEMI. This is bucket sort with iterators. |
| 599 | for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a) |
| 600 | if (InsPoint[a] == InsAt) |
| 601 | InsPoint[a] = CPEMI; |
| 602 | // Add a new CPEntry, but no corresponding CPUser yet. |
| 603 | std::vector<CPEntry> CPEs; |
| 604 | CPEs.push_back(CPEntry(CPEMI, i)); |
| 605 | CPEntries.push_back(CPEs); |
| 606 | ++NumCPEs; |
| 607 | DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = " |
| 608 | << Size << ", align = " << Align <<'\n'); |
| 609 | } |
| 610 | DEBUG(BB->dump()); |
| 611 | } |
| 612 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 613 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
| 614 | /// into the block immediately after it. |
| 615 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 616 | // Get the next machine basic block in the function. |
| 617 | MachineFunction::iterator MBBI = MBB; |
| 618 | // Can't fall off end of function. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 619 | if (std::next(MBBI) == MBB->getParent()->end()) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 620 | return false; |
| 621 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 622 | MachineBasicBlock *NextBB = std::next(MBBI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 623 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 624 | E = MBB->succ_end(); I != E; ++I) |
| 625 | if (*I == NextBB) |
| 626 | return true; |
| 627 | |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 632 | /// look up the corresponding CPEntry. |
| 633 | MipsConstantIslands::CPEntry |
| 634 | *MipsConstantIslands::findConstPoolEntry(unsigned CPI, |
| 635 | const MachineInstr *CPEMI) { |
| 636 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 637 | // Number of entries per constpool index should be small, just do a |
| 638 | // linear search. |
| 639 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 640 | if (CPEs[i].CPEMI == CPEMI) |
| 641 | return &CPEs[i]; |
| 642 | } |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | /// getCPELogAlign - Returns the required alignment of the constant pool entry |
| 647 | /// represented by CPEMI. Alignment is measured in log2(bytes) units. |
| 648 | unsigned MipsConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) { |
| 649 | assert(CPEMI && CPEMI->getOpcode() == Mips::CONSTPOOL_ENTRY); |
| 650 | |
| 651 | // Everything is 4-byte aligned unless AlignConstantIslands is set. |
| 652 | if (!AlignConstantIslands) |
| 653 | return 2; |
| 654 | |
| 655 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 656 | assert(CPI < MCP->getConstants().size() && "Invalid constant pool index."); |
| 657 | unsigned Align = MCP->getConstants()[CPI].getAlignment(); |
| 658 | assert(isPowerOf2_32(Align) && "Invalid CPE alignment"); |
| 659 | return Log2_32(Align); |
| 660 | } |
| 661 | |
| 662 | /// initializeFunctionInfo - Do the initial scan of the function, building up |
| 663 | /// information about the sizes of each block, the location of all the water, |
| 664 | /// and finding all of the constant pool users. |
| 665 | void MipsConstantIslands:: |
| 666 | initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) { |
| 667 | BBInfo.clear(); |
| 668 | BBInfo.resize(MF->getNumBlockIDs()); |
| 669 | |
| 670 | // First thing, compute the size of all basic blocks, and see if the function |
| 671 | // has any inline assembly in it. If so, we have to be conservative about |
| 672 | // alignment assumptions, as we don't know for sure the size of any |
| 673 | // instructions in the inline assembly. |
| 674 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) |
| 675 | computeBlockSize(I); |
| 676 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 677 | |
| 678 | // Compute block offsets. |
| 679 | adjustBBOffsetsAfter(MF->begin()); |
| 680 | |
| 681 | // Now go back through the instructions and build up our data structures. |
| 682 | for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); |
| 683 | MBBI != E; ++MBBI) { |
| 684 | MachineBasicBlock &MBB = *MBBI; |
| 685 | |
| 686 | // If this block doesn't fall through into the next MBB, then this is |
| 687 | // 'water' that a constant pool island could be placed. |
| 688 | if (!BBHasFallthrough(&MBB)) |
| 689 | WaterList.push_back(&MBB); |
| 690 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 691 | I != E; ++I) { |
| 692 | if (I->isDebugValue()) |
| 693 | continue; |
| 694 | |
| 695 | int Opc = I->getOpcode(); |
| 696 | if (I->isBranch()) { |
| 697 | bool isCond = false; |
| 698 | unsigned Bits = 0; |
| 699 | unsigned Scale = 1; |
| 700 | int UOpc = Opc; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 701 | switch (Opc) { |
| 702 | default: |
Reed Kotler | 4b7afe5 | 2013-11-13 23:52:18 +0000 | [diff] [blame] | 703 | continue; // Ignore other branches for now |
| 704 | case Mips::Bimm16: |
| 705 | Bits = 11; |
| 706 | Scale = 2; |
| 707 | isCond = false; |
| 708 | break; |
| 709 | case Mips::BimmX16: |
| 710 | Bits = 16; |
| 711 | Scale = 2; |
| 712 | isCond = false; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 713 | break; |
| 714 | case Mips::BeqzRxImm16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 715 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 716 | Bits = 8; |
| 717 | Scale = 2; |
| 718 | isCond = true; |
| 719 | break; |
| 720 | case Mips::BeqzRxImmX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 721 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 722 | Bits = 16; |
| 723 | Scale = 2; |
| 724 | isCond = true; |
| 725 | break; |
| 726 | case Mips::BnezRxImm16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 727 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 728 | Bits = 8; |
| 729 | Scale = 2; |
| 730 | isCond = true; |
| 731 | break; |
| 732 | case Mips::BnezRxImmX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 733 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 734 | Bits = 16; |
| 735 | Scale = 2; |
| 736 | isCond = true; |
| 737 | break; |
| 738 | case Mips::Bteqz16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 739 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 740 | Bits = 8; |
| 741 | Scale = 2; |
| 742 | isCond = true; |
| 743 | break; |
| 744 | case Mips::BteqzX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 745 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 746 | Bits = 16; |
| 747 | Scale = 2; |
| 748 | isCond = true; |
| 749 | break; |
| 750 | case Mips::Btnez16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 751 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 752 | Bits = 8; |
| 753 | Scale = 2; |
| 754 | isCond = true; |
| 755 | break; |
| 756 | case Mips::BtnezX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 757 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 758 | Bits = 16; |
| 759 | Scale = 2; |
| 760 | isCond = true; |
| 761 | break; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 762 | } |
| 763 | // Record this immediate branch. |
| 764 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 765 | ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 766 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 767 | |
| 768 | if (Opc == Mips::CONSTPOOL_ENTRY) |
| 769 | continue; |
| 770 | |
| 771 | |
| 772 | // Scan the instructions for constant pool operands. |
| 773 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 774 | if (I->getOperand(op).isCPI()) { |
| 775 | |
| 776 | // We found one. The addressing mode tells us the max displacement |
| 777 | // from the PC that this instruction permits. |
| 778 | |
| 779 | // Basic size info comes from the TSFlags field. |
| 780 | unsigned Bits = 0; |
| 781 | unsigned Scale = 1; |
| 782 | bool NegOk = false; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 783 | unsigned LongFormBits = 0; |
| 784 | unsigned LongFormScale = 0; |
| 785 | unsigned LongFormOpcode = 0; |
| 786 | switch (Opc) { |
| 787 | default: |
| 788 | llvm_unreachable("Unknown addressing mode for CP reference!"); |
| 789 | case Mips::LwRxPcTcp16: |
| 790 | Bits = 8; |
Reed Kotler | 3d7b33f | 2013-11-06 04:29:52 +0000 | [diff] [blame] | 791 | Scale = 4; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 792 | LongFormOpcode = Mips::LwRxPcTcpX16; |
Reed Kotler | 43788a2 | 2014-01-16 00:47:46 +0000 | [diff] [blame] | 793 | LongFormBits = 14; |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 794 | LongFormScale = 1; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 795 | break; |
| 796 | case Mips::LwRxPcTcpX16: |
Reed Kotler | 43788a2 | 2014-01-16 00:47:46 +0000 | [diff] [blame] | 797 | Bits = 14; |
Reed Kotler | 3d7b33f | 2013-11-06 04:29:52 +0000 | [diff] [blame] | 798 | Scale = 1; |
| 799 | NegOk = true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | // Remember that this is a user of a CP entry. |
| 803 | unsigned CPI = I->getOperand(op).getIndex(); |
| 804 | MachineInstr *CPEMI = CPEMIs[CPI]; |
| 805 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
| 806 | unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale; |
| 807 | CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 808 | LongFormMaxOffs, LongFormOpcode)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 809 | |
| 810 | // Increment corresponding CPEntry reference count. |
| 811 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 812 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 813 | CPE->RefCount++; |
| 814 | |
| 815 | // Instructions can only use one CP entry, don't bother scanning the |
| 816 | // rest of the operands. |
| 817 | break; |
| 818 | |
| 819 | } |
| 820 | |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | } |
| 825 | |
| 826 | /// computeBlockSize - Compute the size and some alignment information for MBB. |
| 827 | /// This function updates BBInfo directly. |
| 828 | void MipsConstantIslands::computeBlockSize(MachineBasicBlock *MBB) { |
| 829 | BasicBlockInfo &BBI = BBInfo[MBB->getNumber()]; |
| 830 | BBI.Size = 0; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 831 | |
| 832 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; |
| 833 | ++I) |
| 834 | BBI.Size += TII->GetInstSizeInBytes(I); |
| 835 | |
| 836 | } |
| 837 | |
| 838 | /// getOffsetOf - Return the current offset of the specified machine instruction |
| 839 | /// from the start of the function. This offset changes as stuff is moved |
| 840 | /// around inside the function. |
| 841 | unsigned MipsConstantIslands::getOffsetOf(MachineInstr *MI) const { |
| 842 | MachineBasicBlock *MBB = MI->getParent(); |
| 843 | |
| 844 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 845 | // before this instruction's block, and the offset from the start of the block |
| 846 | // it is in. |
| 847 | unsigned Offset = BBInfo[MBB->getNumber()].Offset; |
| 848 | |
| 849 | // Sum instructions before MI in MBB. |
| 850 | for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) { |
| 851 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| 852 | Offset += TII->GetInstSizeInBytes(I); |
| 853 | } |
| 854 | return Offset; |
| 855 | } |
| 856 | |
| 857 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 858 | /// ID. |
| 859 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 860 | const MachineBasicBlock *RHS) { |
| 861 | return LHS->getNumber() < RHS->getNumber(); |
| 862 | } |
| 863 | |
| 864 | /// updateForInsertedWaterBlock - When a block is newly inserted into the |
| 865 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 866 | /// and update the arrays that parallel this numbering. |
| 867 | void MipsConstantIslands::updateForInsertedWaterBlock |
| 868 | (MachineBasicBlock *NewBB) { |
| 869 | // Renumber the MBB's to keep them consecutive. |
| 870 | NewBB->getParent()->RenumberBlocks(NewBB); |
| 871 | |
| 872 | // Insert an entry into BBInfo to align it properly with the (newly |
| 873 | // renumbered) block numbers. |
| 874 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| 875 | |
| 876 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
| 877 | // available water after it. |
| 878 | water_iterator IP = |
| 879 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 880 | CompareMBBNumbers); |
| 881 | WaterList.insert(IP, NewBB); |
| 882 | } |
| 883 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 884 | unsigned MipsConstantIslands::getUserOffset(CPUser &U) const { |
Reed Kotler | 0eb8739 | 2013-11-05 21:39:57 +0000 | [diff] [blame] | 885 | return getOffsetOf(U.MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /// Split the basic block containing MI into two blocks, which are joined by |
| 889 | /// an unconditional branch. Update data structures and renumber blocks to |
| 890 | /// account for this change and returns the newly created block. |
| 891 | MachineBasicBlock *MipsConstantIslands::splitBlockBeforeInstr |
| 892 | (MachineInstr *MI) { |
| 893 | MachineBasicBlock *OrigBB = MI->getParent(); |
| 894 | |
| 895 | // Create a new MBB for the code after the OrigBB. |
| 896 | MachineBasicBlock *NewBB = |
| 897 | MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); |
| 898 | MachineFunction::iterator MBBI = OrigBB; ++MBBI; |
| 899 | MF->insert(MBBI, NewBB); |
| 900 | |
| 901 | // Splice the instructions starting with MI over to NewBB. |
| 902 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
| 903 | |
| 904 | // Add an unconditional branch from OrigBB to NewBB. |
| 905 | // Note the new unconditional branch is not being recorded. |
| 906 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 907 | // correspond to anything in the source. |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 908 | BuildMI(OrigBB, DebugLoc(), TII->get(Mips::Bimm16)).addMBB(NewBB); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 909 | ++NumSplit; |
| 910 | |
| 911 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| 912 | NewBB->transferSuccessors(OrigBB); |
| 913 | |
| 914 | // OrigBB branches to NewBB. |
| 915 | OrigBB->addSuccessor(NewBB); |
| 916 | |
| 917 | // Update internal data structures to account for the newly inserted MBB. |
| 918 | // This is almost the same as updateForInsertedWaterBlock, except that |
| 919 | // the Water goes after OrigBB, not NewBB. |
| 920 | MF->RenumberBlocks(NewBB); |
| 921 | |
| 922 | // Insert an entry into BBInfo to align it properly with the (newly |
| 923 | // renumbered) block numbers. |
| 924 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| 925 | |
| 926 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
| 927 | // available water after it (but not if it's already there, which happens |
| 928 | // when splitting before a conditional branch that is followed by an |
| 929 | // unconditional branch - in that case we want to insert NewBB). |
| 930 | water_iterator IP = |
| 931 | std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, |
| 932 | CompareMBBNumbers); |
| 933 | MachineBasicBlock* WaterBB = *IP; |
| 934 | if (WaterBB == OrigBB) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 935 | WaterList.insert(std::next(IP), NewBB); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 936 | else |
| 937 | WaterList.insert(IP, OrigBB); |
| 938 | NewWaterList.insert(OrigBB); |
| 939 | |
| 940 | // Figure out how large the OrigBB is. As the first half of the original |
| 941 | // block, it cannot contain a tablejump. The size includes |
| 942 | // the new jump we added. (It should be possible to do this without |
| 943 | // recounting everything, but it's very confusing, and this is rarely |
| 944 | // executed.) |
| 945 | computeBlockSize(OrigBB); |
| 946 | |
| 947 | // Figure out how large the NewMBB is. As the second half of the original |
| 948 | // block, it may contain a tablejump. |
| 949 | computeBlockSize(NewBB); |
| 950 | |
| 951 | // All BBOffsets following these blocks must be modified. |
| 952 | adjustBBOffsetsAfter(OrigBB); |
| 953 | |
| 954 | return NewBB; |
| 955 | } |
| 956 | |
| 957 | |
| 958 | |
| 959 | /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool |
| 960 | /// reference) is within MaxDisp of TrialOffset (a proposed location of a |
| 961 | /// constant pool entry). |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 962 | bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset, |
| 963 | unsigned TrialOffset, unsigned MaxDisp, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 964 | bool NegativeOK) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 965 | if (UserOffset <= TrialOffset) { |
| 966 | // User before the Trial. |
| 967 | if (TrialOffset - UserOffset <= MaxDisp) |
| 968 | return true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 969 | } else if (NegativeOK) { |
| 970 | if (UserOffset - TrialOffset <= MaxDisp) |
| 971 | return true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 972 | } |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | /// isWaterInRange - Returns true if a CPE placed after the specified |
| 977 | /// Water (a basic block) will be in range for the specific MI. |
| 978 | /// |
| 979 | /// Compute how much the function will grow by inserting a CPE after Water. |
| 980 | bool MipsConstantIslands::isWaterInRange(unsigned UserOffset, |
| 981 | MachineBasicBlock* Water, CPUser &U, |
| 982 | unsigned &Growth) { |
| 983 | unsigned CPELogAlign = getCPELogAlign(U.CPEMI); |
| 984 | unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign); |
| 985 | unsigned NextBlockOffset, NextBlockAlignment; |
| 986 | MachineFunction::const_iterator NextBlock = Water; |
| 987 | if (++NextBlock == MF->end()) { |
| 988 | NextBlockOffset = BBInfo[Water->getNumber()].postOffset(); |
| 989 | NextBlockAlignment = 0; |
| 990 | } else { |
| 991 | NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset; |
| 992 | NextBlockAlignment = NextBlock->getAlignment(); |
| 993 | } |
| 994 | unsigned Size = U.CPEMI->getOperand(2).getImm(); |
| 995 | unsigned CPEEnd = CPEOffset + Size; |
| 996 | |
| 997 | // The CPE may be able to hide in the alignment padding before the next |
| 998 | // block. It may also cause more padding to be required if it is more aligned |
| 999 | // that the next block. |
| 1000 | if (CPEEnd > NextBlockOffset) { |
| 1001 | Growth = CPEEnd - NextBlockOffset; |
| 1002 | // Compute the padding that would go at the end of the CPE to align the next |
| 1003 | // block. |
| 1004 | Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment); |
| 1005 | |
| 1006 | // If the CPE is to be inserted before the instruction, that will raise |
| 1007 | // the offset of the instruction. Also account for unknown alignment padding |
| 1008 | // in blocks between CPE and the user. |
| 1009 | if (CPEOffset < UserOffset) |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1010 | UserOffset += Growth; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1011 | } else |
| 1012 | // CPE fits in existing padding. |
| 1013 | Growth = 0; |
| 1014 | |
| 1015 | return isOffsetInRange(UserOffset, CPEOffset, U); |
| 1016 | } |
| 1017 | |
| 1018 | /// isCPEntryInRange - Returns true if the distance between specific MI and |
| 1019 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
| 1020 | bool MipsConstantIslands::isCPEntryInRange |
| 1021 | (MachineInstr *MI, unsigned UserOffset, |
| 1022 | MachineInstr *CPEMI, unsigned MaxDisp, |
| 1023 | bool NegOk, bool DoDump) { |
| 1024 | unsigned CPEOffset = getOffsetOf(CPEMI); |
| 1025 | |
| 1026 | if (DoDump) { |
| 1027 | DEBUG({ |
| 1028 | unsigned Block = MI->getParent()->getNumber(); |
| 1029 | const BasicBlockInfo &BBI = BBInfo[Block]; |
| 1030 | dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 1031 | << " max delta=" << MaxDisp |
| 1032 | << format(" insn address=%#x", UserOffset) |
| 1033 | << " in BB#" << Block << ": " |
| 1034 | << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI |
| 1035 | << format("CPE address=%#x offset=%+d: ", CPEOffset, |
| 1036 | int(CPEOffset-UserOffset)); |
| 1037 | }); |
| 1038 | } |
| 1039 | |
| 1040 | return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk); |
| 1041 | } |
| 1042 | |
| 1043 | #ifndef NDEBUG |
| 1044 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 1045 | /// unconditionally branches to its only successor. |
| 1046 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 1047 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 1048 | return false; |
| 1049 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 1050 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 1051 | MachineInstr *PredMI = &Pred->back(); |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1052 | if (PredMI->getOpcode() == Mips::Bimm16) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1053 | return PredMI->getOperand(0).getMBB() == Succ; |
| 1054 | return false; |
| 1055 | } |
| 1056 | #endif |
| 1057 | |
| 1058 | void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) { |
| 1059 | unsigned BBNum = BB->getNumber(); |
| 1060 | for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) { |
| 1061 | // Get the offset and known bits at the end of the layout predecessor. |
| 1062 | // Include the alignment of the current block. |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1063 | unsigned Offset = BBInfo[i - 1].Offset + BBInfo[i - 1].Size; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1064 | BBInfo[i].Offset = Offset; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /// decrementCPEReferenceCount - find the constant pool entry with index CPI |
| 1069 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
| 1070 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
| 1071 | /// the entry, false if we didn't. |
| 1072 | |
| 1073 | bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI, |
| 1074 | MachineInstr *CPEMI) { |
| 1075 | // Find the old entry. Eliminate it if it is no longer used. |
| 1076 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 1077 | assert(CPE && "Unexpected!"); |
| 1078 | if (--CPE->RefCount == 0) { |
| 1079 | removeDeadCPEMI(CPEMI); |
| 1080 | CPE->CPEMI = NULL; |
| 1081 | --NumCPEs; |
| 1082 | return true; |
| 1083 | } |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1088 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1089 | /// change the data structures so the user references the clone. Returns: |
| 1090 | /// 0 = no existing entry found |
| 1091 | /// 1 = entry found, and there were no code insertions or deletions |
| 1092 | /// 2 = entry found, and there were code insertions or deletions |
| 1093 | int MipsConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset) |
| 1094 | { |
| 1095 | MachineInstr *UserMI = U.MI; |
| 1096 | MachineInstr *CPEMI = U.CPEMI; |
| 1097 | |
| 1098 | // Check to see if the CPE is already in-range. |
| 1099 | if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk, |
| 1100 | true)) { |
| 1101 | DEBUG(dbgs() << "In range\n"); |
| 1102 | return 1; |
| 1103 | } |
| 1104 | |
| 1105 | // No. Look for previously created clones of the CPE that are in range. |
| 1106 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1107 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1108 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1109 | // We already tried this one |
| 1110 | if (CPEs[i].CPEMI == CPEMI) |
| 1111 | continue; |
| 1112 | // Removing CPEs can leave empty entries, skip |
| 1113 | if (CPEs[i].CPEMI == NULL) |
| 1114 | continue; |
| 1115 | if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(), |
| 1116 | U.NegOk)) { |
| 1117 | DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" |
| 1118 | << CPEs[i].CPI << "\n"); |
| 1119 | // Point the CPUser node to the replacement |
| 1120 | U.CPEMI = CPEs[i].CPEMI; |
| 1121 | // Change the CPI in the instruction operand to refer to the clone. |
| 1122 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| 1123 | if (UserMI->getOperand(j).isCPI()) { |
| 1124 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
| 1125 | break; |
| 1126 | } |
| 1127 | // Adjust the refcount of the clone... |
| 1128 | CPEs[i].RefCount++; |
| 1129 | // ...and the original. If we didn't remove the old entry, none of the |
| 1130 | // addresses changed, so we don't need another pass. |
| 1131 | return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; |
| 1132 | } |
| 1133 | } |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1138 | /// This version checks if the longer form of the instruction can be used to |
| 1139 | /// to satisfy things. |
| 1140 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1141 | /// change the data structures so the user references the clone. Returns: |
| 1142 | /// 0 = no existing entry found |
| 1143 | /// 1 = entry found, and there were no code insertions or deletions |
| 1144 | /// 2 = entry found, and there were code insertions or deletions |
| 1145 | int MipsConstantIslands::findLongFormInRangeCPEntry |
| 1146 | (CPUser& U, unsigned UserOffset) |
| 1147 | { |
| 1148 | MachineInstr *UserMI = U.MI; |
| 1149 | MachineInstr *CPEMI = U.CPEMI; |
| 1150 | |
| 1151 | // Check to see if the CPE is already in-range. |
| 1152 | if (isCPEntryInRange(UserMI, UserOffset, CPEMI, |
| 1153 | U.getLongFormMaxDisp(), U.NegOk, |
| 1154 | true)) { |
| 1155 | DEBUG(dbgs() << "In range\n"); |
| 1156 | UserMI->setDesc(TII->get(U.getLongFormOpcode())); |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 1157 | U.setMaxDisp(U.getLongFormMaxDisp()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1158 | return 2; // instruction is longer length now |
| 1159 | } |
| 1160 | |
| 1161 | // No. Look for previously created clones of the CPE that are in range. |
| 1162 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1163 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1164 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1165 | // We already tried this one |
| 1166 | if (CPEs[i].CPEMI == CPEMI) |
| 1167 | continue; |
| 1168 | // Removing CPEs can leave empty entries, skip |
| 1169 | if (CPEs[i].CPEMI == NULL) |
| 1170 | continue; |
| 1171 | if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, |
| 1172 | U.getLongFormMaxDisp(), U.NegOk)) { |
| 1173 | DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" |
| 1174 | << CPEs[i].CPI << "\n"); |
| 1175 | // Point the CPUser node to the replacement |
| 1176 | U.CPEMI = CPEs[i].CPEMI; |
| 1177 | // Change the CPI in the instruction operand to refer to the clone. |
| 1178 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| 1179 | if (UserMI->getOperand(j).isCPI()) { |
| 1180 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
| 1181 | break; |
| 1182 | } |
| 1183 | // Adjust the refcount of the clone... |
| 1184 | CPEs[i].RefCount++; |
| 1185 | // ...and the original. If we didn't remove the old entry, none of the |
| 1186 | // addresses changed, so we don't need another pass. |
| 1187 | return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; |
| 1188 | } |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 1194 | /// the specific unconditional branch instruction. |
| 1195 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
| 1196 | switch (Opc) { |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1197 | case Mips::Bimm16: |
| 1198 | return ((1<<10)-1)*2; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1199 | case Mips::BimmX16: |
| 1200 | return ((1<<16)-1)*2; |
| 1201 | default: |
| 1202 | break; |
| 1203 | } |
| 1204 | return ((1<<16)-1)*2; |
| 1205 | } |
| 1206 | |
| 1207 | /// findAvailableWater - Look for an existing entry in the WaterList in which |
| 1208 | /// we can place the CPE referenced from U so it's within range of U's MI. |
| 1209 | /// Returns true if found, false if not. If it returns true, WaterIter |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1210 | /// is set to the WaterList entry. |
| 1211 | /// To ensure that this pass |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1212 | /// terminates, the CPE location for a particular CPUser is only allowed to |
| 1213 | /// move to a lower address, so search backward from the end of the list and |
| 1214 | /// prefer the first water that is in range. |
| 1215 | bool MipsConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset, |
| 1216 | water_iterator &WaterIter) { |
| 1217 | if (WaterList.empty()) |
| 1218 | return false; |
| 1219 | |
| 1220 | unsigned BestGrowth = ~0u; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1221 | for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1222 | --IP) { |
| 1223 | MachineBasicBlock* WaterBB = *IP; |
| 1224 | // Check if water is in range and is either at a lower address than the |
| 1225 | // current "high water mark" or a new water block that was created since |
| 1226 | // the previous iteration by inserting an unconditional branch. In the |
| 1227 | // latter case, we want to allow resetting the high water mark back to |
| 1228 | // this new water since we haven't seen it before. Inserting branches |
| 1229 | // should be relatively uncommon and when it does happen, we want to be |
| 1230 | // sure to take advantage of it for all the CPEs near that block, so that |
| 1231 | // we don't insert more branches than necessary. |
| 1232 | unsigned Growth; |
| 1233 | if (isWaterInRange(UserOffset, WaterBB, U, Growth) && |
| 1234 | (WaterBB->getNumber() < U.HighWaterMark->getNumber() || |
| 1235 | NewWaterList.count(WaterBB)) && Growth < BestGrowth) { |
| 1236 | // This is the least amount of required padding seen so far. |
| 1237 | BestGrowth = Growth; |
| 1238 | WaterIter = IP; |
| 1239 | DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber() |
| 1240 | << " Growth=" << Growth << '\n'); |
| 1241 | |
| 1242 | // Keep looking unless it is perfect. |
| 1243 | if (BestGrowth == 0) |
| 1244 | return true; |
| 1245 | } |
| 1246 | if (IP == B) |
| 1247 | break; |
| 1248 | } |
| 1249 | return BestGrowth != ~0u; |
| 1250 | } |
| 1251 | |
| 1252 | /// createNewWater - No existing WaterList entry will work for |
| 1253 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 1254 | /// block is used if in range, and the conditional branch munged so control |
| 1255 | /// flow is correct. Otherwise the block is split to create a hole with an |
| 1256 | /// unconditional branch around it. In either case NewMBB is set to a |
| 1257 | /// block following which the new island can be inserted (the WaterList |
| 1258 | /// is not adjusted). |
| 1259 | void MipsConstantIslands::createNewWater(unsigned CPUserIndex, |
| 1260 | unsigned UserOffset, |
| 1261 | MachineBasicBlock *&NewMBB) { |
| 1262 | CPUser &U = CPUsers[CPUserIndex]; |
| 1263 | MachineInstr *UserMI = U.MI; |
| 1264 | MachineInstr *CPEMI = U.CPEMI; |
| 1265 | unsigned CPELogAlign = getCPELogAlign(CPEMI); |
| 1266 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
| 1267 | const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()]; |
| 1268 | |
| 1269 | // If the block does not end in an unconditional branch already, and if the |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1270 | // end of the block is within range, make new water there. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1271 | if (BBHasFallthrough(UserMBB)) { |
| 1272 | // Size of branch to insert. |
| 1273 | unsigned Delta = 2; |
| 1274 | // Compute the offset where the CPE will begin. |
| 1275 | unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta; |
| 1276 | |
| 1277 | if (isOffsetInRange(UserOffset, CPEOffset, U)) { |
| 1278 | DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber() |
| 1279 | << format(", expected CPE offset %#x\n", CPEOffset)); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1280 | NewMBB = std::next(MachineFunction::iterator(UserMBB)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1281 | // Add an unconditional branch from UserMBB to fallthrough block. Record |
| 1282 | // it for branch lengthening; this new branch will not get out of range, |
| 1283 | // but if the preceding conditional branch is out of range, the targets |
| 1284 | // will be exchanged, and the altered branch may be out of range, so the |
| 1285 | // machinery has to know about it. |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1286 | int UncondBr = Mips::Bimm16; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1287 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB); |
| 1288 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
| 1289 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
| 1290 | MaxDisp, false, UncondBr)); |
| 1291 | BBInfo[UserMBB->getNumber()].Size += Delta; |
| 1292 | adjustBBOffsetsAfter(UserMBB); |
| 1293 | return; |
| 1294 | } |
| 1295 | } |
| 1296 | |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1297 | // What a big block. Find a place within the block to split it. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1298 | |
| 1299 | // Try to split the block so it's fully aligned. Compute the latest split |
| 1300 | // point where we can add a 4-byte branch instruction, and then align to |
| 1301 | // LogAlign which is the largest possible alignment in the function. |
| 1302 | unsigned LogAlign = MF->getAlignment(); |
| 1303 | assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry"); |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1304 | unsigned BaseInsertOffset = UserOffset + U.getMaxDisp(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1305 | DEBUG(dbgs() << format("Split in middle of big block before %#x", |
| 1306 | BaseInsertOffset)); |
| 1307 | |
| 1308 | // The 4 in the following is for the unconditional branch we'll be inserting |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1309 | // Alignment of the island is handled |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1310 | // inside isOffsetInRange. |
| 1311 | BaseInsertOffset -= 4; |
| 1312 | |
| 1313 | DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset) |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1314 | << " la=" << LogAlign << '\n'); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1315 | |
| 1316 | // This could point off the end of the block if we've already got constant |
| 1317 | // pool entries following this block; only the last one is in the water list. |
| 1318 | // Back past any possible branches (allow for a conditional and a maximally |
| 1319 | // long unconditional). |
| 1320 | if (BaseInsertOffset + 8 >= UserBBI.postOffset()) { |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1321 | BaseInsertOffset = UserBBI.postOffset() - 8; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1322 | DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset)); |
| 1323 | } |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1324 | unsigned EndInsertOffset = BaseInsertOffset + 4 + |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1325 | CPEMI->getOperand(2).getImm(); |
| 1326 | MachineBasicBlock::iterator MI = UserMI; |
| 1327 | ++MI; |
| 1328 | unsigned CPUIndex = CPUserIndex+1; |
| 1329 | unsigned NumCPUsers = CPUsers.size(); |
| 1330 | //MachineInstr *LastIT = 0; |
| 1331 | for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI); |
| 1332 | Offset < BaseInsertOffset; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1333 | Offset += TII->GetInstSizeInBytes(MI), MI = std::next(MI)) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1334 | assert(MI != UserMBB->end() && "Fell off end of block"); |
| 1335 | if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) { |
| 1336 | CPUser &U = CPUsers[CPUIndex]; |
| 1337 | if (!isOffsetInRange(Offset, EndInsertOffset, U)) { |
| 1338 | // Shift intertion point by one unit of alignment so it is within reach. |
| 1339 | BaseInsertOffset -= 1u << LogAlign; |
| 1340 | EndInsertOffset -= 1u << LogAlign; |
| 1341 | } |
| 1342 | // This is overly conservative, as we don't account for CPEMIs being |
| 1343 | // reused within the block, but it doesn't matter much. Also assume CPEs |
| 1344 | // are added in order with alignment padding. We may eventually be able |
| 1345 | // to pack the aligned CPEs better. |
| 1346 | EndInsertOffset += U.CPEMI->getOperand(2).getImm(); |
| 1347 | CPUIndex++; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | --MI; |
| 1352 | NewMBB = splitBlockBeforeInstr(MI); |
| 1353 | } |
| 1354 | |
| 1355 | /// handleConstantPoolUser - Analyze the specified user, checking to see if it |
| 1356 | /// is out-of-range. If so, pick up the constant pool value and move it some |
| 1357 | /// place in-range. Return true if we changed any addresses (thus must run |
| 1358 | /// another pass of branch lengthening), false otherwise. |
| 1359 | bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) { |
| 1360 | CPUser &U = CPUsers[CPUserIndex]; |
| 1361 | MachineInstr *UserMI = U.MI; |
| 1362 | MachineInstr *CPEMI = U.CPEMI; |
| 1363 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1364 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1365 | // Compute this only once, it's expensive. |
| 1366 | unsigned UserOffset = getUserOffset(U); |
| 1367 | |
| 1368 | // See if the current entry is within range, or there is a clone of it |
| 1369 | // in range. |
| 1370 | int result = findInRangeCPEntry(U, UserOffset); |
| 1371 | if (result==1) return false; |
| 1372 | else if (result==2) return true; |
| 1373 | |
| 1374 | |
| 1375 | // Look for water where we can place this CPE. |
| 1376 | MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock(); |
| 1377 | MachineBasicBlock *NewMBB; |
| 1378 | water_iterator IP; |
| 1379 | if (findAvailableWater(U, UserOffset, IP)) { |
| 1380 | DEBUG(dbgs() << "Found water in range\n"); |
| 1381 | MachineBasicBlock *WaterBB = *IP; |
| 1382 | |
| 1383 | // If the original WaterList entry was "new water" on this iteration, |
| 1384 | // propagate that to the new island. This is just keeping NewWaterList |
| 1385 | // updated to match the WaterList, which will be updated below. |
| 1386 | if (NewWaterList.erase(WaterBB)) |
| 1387 | NewWaterList.insert(NewIsland); |
| 1388 | |
| 1389 | // The new CPE goes before the following block (NewMBB). |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1390 | NewMBB = std::next(MachineFunction::iterator(WaterBB)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1391 | |
| 1392 | } else { |
| 1393 | // No water found. |
| 1394 | // we first see if a longer form of the instrucion could have reached |
| 1395 | // the constant. in that case we won't bother to split |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 1396 | if (!NoLoadRelaxation) { |
| 1397 | result = findLongFormInRangeCPEntry(U, UserOffset); |
| 1398 | if (result != 0) return true; |
| 1399 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1400 | DEBUG(dbgs() << "No water found\n"); |
| 1401 | createNewWater(CPUserIndex, UserOffset, NewMBB); |
| 1402 | |
| 1403 | // splitBlockBeforeInstr adds to WaterList, which is important when it is |
| 1404 | // called while handling branches so that the water will be seen on the |
| 1405 | // next iteration for constant pools, but in this context, we don't want |
| 1406 | // it. Check for this so it will be removed from the WaterList. |
| 1407 | // Also remove any entry from NewWaterList. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1408 | MachineBasicBlock *WaterBB = std::prev(MachineFunction::iterator(NewMBB)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1409 | IP = std::find(WaterList.begin(), WaterList.end(), WaterBB); |
| 1410 | if (IP != WaterList.end()) |
| 1411 | NewWaterList.erase(WaterBB); |
| 1412 | |
| 1413 | // We are adding new water. Update NewWaterList. |
| 1414 | NewWaterList.insert(NewIsland); |
| 1415 | } |
| 1416 | |
| 1417 | // Remove the original WaterList entry; we want subsequent insertions in |
| 1418 | // this vicinity to go after the one we're about to insert. This |
| 1419 | // considerably reduces the number of times we have to move the same CPE |
| 1420 | // more than once and is also important to ensure the algorithm terminates. |
| 1421 | if (IP != WaterList.end()) |
| 1422 | WaterList.erase(IP); |
| 1423 | |
| 1424 | // Okay, we know we can put an island before NewMBB now, do it! |
| 1425 | MF->insert(NewMBB, NewIsland); |
| 1426 | |
| 1427 | // Update internal data structures to account for the newly inserted MBB. |
| 1428 | updateForInsertedWaterBlock(NewIsland); |
| 1429 | |
| 1430 | // Decrement the old entry, and remove it if refcount becomes 0. |
| 1431 | decrementCPEReferenceCount(CPI, CPEMI); |
| 1432 | |
Reed Kotler | d3b28eb | 2013-11-24 02:53:09 +0000 | [diff] [blame] | 1433 | // No existing clone of this CPE is within range. |
| 1434 | // We will be generating a new clone. Get a UID for it. |
| 1435 | unsigned ID = createPICLabelUId(); |
| 1436 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1437 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 1438 | // add it to the island. |
| 1439 | U.HighWaterMark = NewIsland; |
| 1440 | U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY)) |
| 1441 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
| 1442 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
| 1443 | ++NumCPEs; |
| 1444 | |
| 1445 | // Mark the basic block as aligned as required by the const-pool entry. |
| 1446 | NewIsland->setAlignment(getCPELogAlign(U.CPEMI)); |
| 1447 | |
| 1448 | // Increase the size of the island block to account for the new entry. |
| 1449 | BBInfo[NewIsland->getNumber()].Size += Size; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1450 | adjustBBOffsetsAfter(std::prev(MachineFunction::iterator(NewIsland))); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1451 | |
Reed Kotler | d3b28eb | 2013-11-24 02:53:09 +0000 | [diff] [blame] | 1452 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Finally, change the CPI in the instruction operand to be ID. |
| 1455 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
| 1456 | if (UserMI->getOperand(i).isCPI()) { |
| 1457 | UserMI->getOperand(i).setIndex(ID); |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI |
| 1462 | << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset)); |
| 1463 | |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
| 1467 | /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update |
| 1468 | /// sizes and offsets of impacted basic blocks. |
| 1469 | void MipsConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) { |
| 1470 | MachineBasicBlock *CPEBB = CPEMI->getParent(); |
| 1471 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1472 | CPEMI->eraseFromParent(); |
| 1473 | BBInfo[CPEBB->getNumber()].Size -= Size; |
| 1474 | // All succeeding offsets have the current size value added in, fix this. |
| 1475 | if (CPEBB->empty()) { |
| 1476 | BBInfo[CPEBB->getNumber()].Size = 0; |
| 1477 | |
| 1478 | // This block no longer needs to be aligned. |
| 1479 | CPEBB->setAlignment(0); |
| 1480 | } else |
| 1481 | // Entries are sorted by descending alignment, so realign from the front. |
| 1482 | CPEBB->setAlignment(getCPELogAlign(CPEBB->begin())); |
| 1483 | |
| 1484 | adjustBBOffsetsAfter(CPEBB); |
| 1485 | // An island has only one predecessor BB and one successor BB. Check if |
| 1486 | // this BB's predecessor jumps directly to this BB's successor. This |
| 1487 | // shouldn't happen currently. |
| 1488 | assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); |
| 1489 | // FIXME: remove the empty blocks after all the work is done? |
| 1490 | } |
| 1491 | |
| 1492 | /// removeUnusedCPEntries - Remove constant pool entries whose refcounts |
| 1493 | /// are zero. |
| 1494 | bool MipsConstantIslands::removeUnusedCPEntries() { |
| 1495 | unsigned MadeChange = false; |
| 1496 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 1497 | std::vector<CPEntry> &CPEs = CPEntries[i]; |
| 1498 | for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { |
| 1499 | if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { |
| 1500 | removeDeadCPEMI(CPEs[j].CPEMI); |
| 1501 | CPEs[j].CPEMI = NULL; |
| 1502 | MadeChange = true; |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | return MadeChange; |
| 1507 | } |
| 1508 | |
| 1509 | /// isBBInRange - Returns true if the distance between specific MI and |
| 1510 | /// specific BB can fit in MI's displacement field. |
| 1511 | bool MipsConstantIslands::isBBInRange |
| 1512 | (MachineInstr *MI,MachineBasicBlock *DestBB, unsigned MaxDisp) { |
| 1513 | |
| 1514 | unsigned PCAdj = 4; |
| 1515 | |
| 1516 | unsigned BrOffset = getOffsetOf(MI) + PCAdj; |
| 1517 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
| 1518 | |
| 1519 | DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber() |
| 1520 | << " from BB#" << MI->getParent()->getNumber() |
| 1521 | << " max delta=" << MaxDisp |
| 1522 | << " from " << getOffsetOf(MI) << " to " << DestOffset |
| 1523 | << " offset " << int(DestOffset-BrOffset) << "\t" << *MI); |
| 1524 | |
| 1525 | if (BrOffset <= DestOffset) { |
| 1526 | // Branch before the Dest. |
| 1527 | if (DestOffset-BrOffset <= MaxDisp) |
| 1528 | return true; |
| 1529 | } else { |
| 1530 | if (BrOffset-DestOffset <= MaxDisp) |
| 1531 | return true; |
| 1532 | } |
| 1533 | return false; |
| 1534 | } |
| 1535 | |
| 1536 | /// fixupImmediateBr - Fix up an immediate branch whose destination is too far |
| 1537 | /// away to fit in its displacement field. |
| 1538 | bool MipsConstantIslands::fixupImmediateBr(ImmBranch &Br) { |
| 1539 | MachineInstr *MI = Br.MI; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 1540 | unsigned TargetOperand = branchTargetOperand(MI); |
| 1541 | MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Check to see if the DestBB is already in-range. |
| 1544 | if (isBBInRange(MI, DestBB, Br.MaxDisp)) |
| 1545 | return false; |
| 1546 | |
| 1547 | if (!Br.isCond) |
| 1548 | return fixupUnconditionalBr(Br); |
| 1549 | return fixupConditionalBr(Br); |
| 1550 | } |
| 1551 | |
| 1552 | /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is |
| 1553 | /// too far away to fit in its displacement field. If the LR register has been |
| 1554 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
| 1555 | /// Otherwise, add an intermediate branch instruction to a branch. |
| 1556 | bool |
| 1557 | MipsConstantIslands::fixupUnconditionalBr(ImmBranch &Br) { |
| 1558 | MachineInstr *MI = Br.MI; |
| 1559 | MachineBasicBlock *MBB = MI->getParent(); |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1560 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1561 | // Use BL to implement far jump. |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1562 | unsigned BimmX16MaxDisp = ((1 << 16)-1) * 2; |
| 1563 | if (isBBInRange(MI, DestBB, BimmX16MaxDisp)) { |
| 1564 | Br.MaxDisp = BimmX16MaxDisp; |
| 1565 | MI->setDesc(TII->get(Mips::BimmX16)); |
| 1566 | } |
| 1567 | else { |
| 1568 | // need to give the math a more careful look here |
| 1569 | // this is really a segment address and not |
| 1570 | // a PC relative address. FIXME. But I think that |
| 1571 | // just reducing the bits by 1 as I've done is correct. |
| 1572 | // The basic block we are branching too much be longword aligned. |
| 1573 | // we know that RA is saved because we always save it right now. |
| 1574 | // this requirement will be relaxed later but we also have an alternate |
| 1575 | // way to implement this that I will implement that does not need jal. |
| 1576 | // We should have a way to back out this alignment restriction if we "can" later. |
| 1577 | // but it is not harmful. |
| 1578 | // |
| 1579 | DestBB->setAlignment(2); |
| 1580 | Br.MaxDisp = ((1<<24)-1) * 2; |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1581 | MI->setDesc(TII->get(Mips::JalB16)); |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1582 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1583 | BBInfo[MBB->getNumber()].Size += 2; |
| 1584 | adjustBBOffsetsAfter(MBB); |
| 1585 | HasFarJump = true; |
| 1586 | ++NumUBrFixed; |
| 1587 | |
| 1588 | DEBUG(dbgs() << " Changed B to long jump " << *MI); |
| 1589 | |
| 1590 | return true; |
| 1591 | } |
| 1592 | |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 1593 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1594 | /// fixupConditionalBr - Fix up a conditional branch whose destination is too |
| 1595 | /// far away to fit in its displacement field. It is converted to an inverse |
| 1596 | /// conditional branch + an unconditional branch to the destination. |
| 1597 | bool |
| 1598 | MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) { |
| 1599 | MachineInstr *MI = Br.MI; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 1600 | unsigned TargetOperand = branchTargetOperand(MI); |
| 1601 | MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB(); |
| 1602 | unsigned Opcode = MI->getOpcode(); |
| 1603 | unsigned LongFormOpcode = longformBranchOpcode(Opcode); |
| 1604 | unsigned LongFormMaxOff = branchMaxOffsets(LongFormOpcode); |
| 1605 | |
| 1606 | // Check to see if the DestBB is already in-range. |
| 1607 | if (isBBInRange(MI, DestBB, LongFormMaxOff)) { |
| 1608 | Br.MaxDisp = LongFormMaxOff; |
| 1609 | MI->setDesc(TII->get(LongFormOpcode)); |
| 1610 | return true; |
| 1611 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1612 | |
| 1613 | // Add an unconditional branch to the destination and invert the branch |
| 1614 | // condition to jump over it: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1615 | // bteqz L1 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1616 | // => |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1617 | // bnez L2 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1618 | // b L1 |
| 1619 | // L2: |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1620 | |
| 1621 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1622 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1623 | // split the MBB before the next instruction. |
| 1624 | MachineBasicBlock *MBB = MI->getParent(); |
| 1625 | MachineInstr *BMI = &MBB->back(); |
| 1626 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
Reed Kotler | 47f3c64a | 2013-12-19 00:43:08 +0000 | [diff] [blame] | 1627 | unsigned OppositeBranchOpcode = TII->getOppositeBranchOpc(Opcode); |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1628 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1629 | ++NumCBrFixed; |
| 1630 | if (BMI != MI) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1631 | if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) && |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1632 | isUnconditionalBranch(BMI->getOpcode())) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1633 | // Last MI in the BB is an unconditional branch. Can we simply invert the |
| 1634 | // condition and swap destinations: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1635 | // beqz L1 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1636 | // b L2 |
| 1637 | // => |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1638 | // bnez L2 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1639 | // b L1 |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1640 | unsigned BMITargetOperand = branchTargetOperand(BMI); |
| 1641 | MachineBasicBlock *NewDest = |
| 1642 | BMI->getOperand(BMITargetOperand).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1643 | if (isBBInRange(MI, NewDest, Br.MaxDisp)) { |
| 1644 | DEBUG(dbgs() << " Invert Bcc condition and swap its destination with " |
| 1645 | << *BMI); |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1646 | MI->setDesc(TII->get(OppositeBranchOpcode)); |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1647 | BMI->getOperand(BMITargetOperand).setMBB(DestBB); |
| 1648 | MI->getOperand(TargetOperand).setMBB(NewDest); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1649 | return true; |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1654 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1655 | if (NeedSplit) { |
| 1656 | splitBlockBeforeInstr(MI); |
| 1657 | // No need for the branch to the next block. We're adding an unconditional |
| 1658 | // branch to the destination. |
| 1659 | int delta = TII->GetInstSizeInBytes(&MBB->back()); |
| 1660 | BBInfo[MBB->getNumber()].Size -= delta; |
| 1661 | MBB->back().eraseFromParent(); |
| 1662 | // BBInfo[SplitBB].Offset is wrong temporarily, fixed below |
| 1663 | } |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1664 | MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(MBB)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1665 | |
| 1666 | DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber() |
| 1667 | << " also invert condition and change dest. to BB#" |
| 1668 | << NextBB->getNumber() << "\n"); |
| 1669 | |
| 1670 | // Insert a new conditional branch and a new unconditional branch. |
| 1671 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1672 | if (MI->getNumExplicitOperands() == 2) { |
| 1673 | BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode)) |
| 1674 | .addReg(MI->getOperand(0).getReg()) |
| 1675 | .addMBB(NextBB); |
Reed Kotler | 47f3c64a | 2013-12-19 00:43:08 +0000 | [diff] [blame] | 1676 | } else { |
| 1677 | BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode)) |
| 1678 | .addMBB(NextBB); |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1679 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1680 | Br.MI = &MBB->back(); |
| 1681 | BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); |
| 1682 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); |
| 1683 | BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); |
| 1684 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
| 1685 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
| 1686 | |
| 1687 | // Remove the old conditional branch. It may or may not still be in MBB. |
| 1688 | BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI); |
| 1689 | MI->eraseFromParent(); |
| 1690 | adjustBBOffsetsAfter(MBB); |
| 1691 | return true; |
| 1692 | } |
| 1693 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1694 | |
| 1695 | void MipsConstantIslands::prescanForConstants() { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1696 | unsigned J = 0; |
| 1697 | (void)J; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1698 | for (MachineFunction::iterator B = |
| 1699 | MF->begin(), E = MF->end(); B != E; ++B) { |
| 1700 | for (MachineBasicBlock::instr_iterator I = |
| 1701 | B->instr_begin(), EB = B->instr_end(); I != EB; ++I) { |
| 1702 | switch(I->getDesc().getOpcode()) { |
| 1703 | case Mips::LwConstant32: { |
Reed Kotler | a787aa2 | 2013-11-24 06:18:50 +0000 | [diff] [blame] | 1704 | PrescannedForConstants = true; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1705 | DEBUG(dbgs() << "constant island constant " << *I << "\n"); |
| 1706 | J = I->getNumOperands(); |
| 1707 | DEBUG(dbgs() << "num operands " << J << "\n"); |
| 1708 | MachineOperand& Literal = I->getOperand(1); |
| 1709 | if (Literal.isImm()) { |
| 1710 | int64_t V = Literal.getImm(); |
| 1711 | DEBUG(dbgs() << "literal " << V << "\n"); |
| 1712 | Type *Int32Ty = |
| 1713 | Type::getInt32Ty(MF->getFunction()->getContext()); |
| 1714 | const Constant *C = ConstantInt::get(Int32Ty, V); |
| 1715 | unsigned index = MCP->getConstantPoolIndex(C, 4); |
| 1716 | I->getOperand(2).ChangeToImmediate(index); |
| 1717 | DEBUG(dbgs() << "constant island constant " << *I << "\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1718 | I->setDesc(TII->get(Mips::LwRxPcTcp16)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1719 | I->RemoveOperand(1); |
| 1720 | I->RemoveOperand(1); |
| 1721 | I->addOperand(MachineOperand::CreateCPI(index, 0)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1722 | I->addOperand(MachineOperand::CreateImm(4)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1723 | } |
| 1724 | break; |
| 1725 | } |
| 1726 | default: |
| 1727 | break; |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1732 | |