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