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