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