1 //===-- MipsConstantIslandPass.cpp - Emit Pc Relative loads----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // This pass is used to make Pc relative loads of constants.
12 // For now, only Mips16 will use this. While it has the same name and
13 // uses many ideas from the LLVM ARM Constant Island Pass, it's not intended
14 // to reuse any of the code from the ARM version.
16 // Loading constants inline is expensive on Mips16 and it's in general better
17 // to place the constant nearby in code space and then it can be loaded with a
18 // simple 16 bit load instruction.
20 // The constants can be not just numbers but addresses of functions and labels.
21 // This can be particularly helpful in static relocation mode for embedded
26 #define DEBUG_TYPE "mips-constant-islands"
29 #include "MCTargetDesc/MipsBaseInfo.h"
30 #include "MipsMachineFunction.h"
31 #include "MipsTargetMachine.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/InstIterator.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetInstrInfo.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetRegisterInfo.h"
46 #include "llvm/Support/Format.h"
51 STATISTIC(NumCPEs, "Number of constpool entries");
52 STATISTIC(NumSplit, "Number of uncond branches inserted");
54 STATISTIC(NumCBrFixed, "Number of cond branches fixed");
56 STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
58 // FIXME: This option should be removed once it has received sufficient testing.
60 AlignConstantIslands("mips-align-constant-islands", cl::Hidden, cl::init(true),
61 cl::desc("Align constant islands in code"));
64 // Rather than do make check tests with huge amounts of code, we force
65 // the test to use this amount.
67 static cl::opt<int> ConstantIslandsSmallOffset(
68 "mips-constant-islands-small-offset",
70 cl::desc("Make small offsets be this amount for testing purposes"),
73 /// UnknownPadding - Return the worst case padding that could result from
74 /// unknown offset bits. This does not include alignment padding caused by
75 /// known offset bits.
77 /// @param LogAlign log2(alignment)
78 /// @param KnownBits Number of known low offset bits.
79 static inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) {
80 if (KnownBits < LogAlign)
81 return (1u << LogAlign) - (1u << KnownBits);
88 typedef MachineBasicBlock::iterator Iter;
89 typedef MachineBasicBlock::reverse_iterator ReverseIter;
91 /// MipsConstantIslands - Due to limited PC-relative displacements, Mips
92 /// requires constant pool entries to be scattered among the instructions
93 /// inside a function. To do this, it completely ignores the normal LLVM
94 /// constant pool; instead, it places constants wherever it feels like with
95 /// special instructions.
97 /// The terminology used in this pass includes:
98 /// Islands - Clumps of constants placed in the function.
99 /// Water - Potential places where an island could be formed.
100 /// CPE - A constant pool entry that has been placed somewhere, which
101 /// tracks a list of users.
103 class MipsConstantIslands : public MachineFunctionPass {
105 /// BasicBlockInfo - Information about the offset and size of a single
107 struct BasicBlockInfo {
108 /// Offset - Distance from the beginning of the function to the beginning
109 /// of this basic block.
111 /// Offsets are computed assuming worst case padding before an aligned
112 /// block. This means that subtracting basic block offsets always gives a
113 /// conservative estimate of the real distance which may be smaller.
115 /// Because worst case padding is used, the computed offset of an aligned
116 /// block may not actually be aligned.
119 /// Size - Size of the basic block in bytes. If the block contains
120 /// inline assembly, this is a worst case estimate.
122 /// The size does not include any alignment padding whether from the
123 /// beginning of the block, or from an aligned jump table at the end.
126 /// KnownBits - The number of low bits in Offset that are known to be
127 /// exact. The remaining bits of Offset are an upper bound.
130 /// Unalign - When non-zero, the block contains instructions (inline asm)
131 /// of unknown size. The real size may be smaller than Size bytes by a
132 /// multiple of 1 << Unalign.
135 /// PostAlign - When non-zero, the block terminator contains a .align
136 /// directive, so the end of the block is aligned to 1 << PostAlign
140 BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0),
143 /// Compute the number of known offset bits internally to this block.
144 /// This number should be used to predict worst case padding when
145 /// splitting the block.
146 unsigned internalKnownBits() const {
147 unsigned Bits = Unalign ? Unalign : KnownBits;
148 // If the block size isn't a multiple of the known bits, assume the
149 // worst case padding.
150 if (Size & ((1u << Bits) - 1))
151 Bits = countTrailingZeros(Size);
155 /// Compute the offset immediately following this block. If LogAlign is
156 /// specified, return the offset the successor block will get if it has
158 unsigned postOffset(unsigned LogAlign = 0) const {
159 unsigned PO = Offset + Size;
163 /// Compute the number of known low bits of postOffset. If this block
164 /// contains inline asm, the number of known bits drops to the
165 /// instruction alignment. An aligned terminator may increase the number
167 /// If LogAlign is given, also consider the alignment of the next block.
168 unsigned postKnownBits(unsigned LogAlign = 0) const {
169 return std::max(std::max(unsigned(PostAlign), LogAlign),
170 internalKnownBits());
174 std::vector<BasicBlockInfo> BBInfo;
176 /// WaterList - A sorted list of basic blocks where islands could be placed
177 /// (i.e. blocks that don't fall through to the following block, due
178 /// to a return, unreachable, or unconditional branch).
179 std::vector<MachineBasicBlock*> WaterList;
181 /// NewWaterList - The subset of WaterList that was created since the
182 /// previous iteration by inserting unconditional branches.
183 SmallSet<MachineBasicBlock*, 4> NewWaterList;
185 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
187 /// CPUser - One user of a constant pool, keeping the machine instruction
188 /// pointer, the constant pool being referenced, and the max displacement
189 /// allowed from the instruction to the CP. The HighWaterMark records the
190 /// highest basic block where a new CPEntry can be placed. To ensure this
191 /// pass terminates, the CP entries are initially placed at the end of the
192 /// function and then move monotonically to lower addresses. The
193 /// exception to this rule is when the current CP entry for a particular
194 /// CPUser is out of range, but there is another CP entry for the same
195 /// constant value in range. We want to use the existing in-range CP
196 /// entry, but if it later moves out of range, the search for new water
197 /// should resume where it left off. The HighWaterMark is used to record
202 MachineBasicBlock *HighWaterMark;
205 unsigned LongFormMaxDisp; // mips16 has 16/32 bit instructions
206 // with different displacements
207 unsigned LongFormOpcode;
212 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
213 bool neg, bool soimm, unsigned longformmaxdisp, unsigned longformopcode)
214 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp),
215 LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode),
216 NegOk(neg), IsSoImm(soimm), KnownAlignment(false) {
217 HighWaterMark = CPEMI->getParent();
219 /// getMaxDisp - Returns the maximum displacement supported by MI.
220 /// Correct for unknown alignment.
221 /// Conservatively subtract 2 bytes to handle weird alignment effects.
222 unsigned getMaxDisp() const {
223 unsigned xMaxDisp = ConstantIslandsSmallOffset? ConstantIslandsSmallOffset: MaxDisp;
224 return (KnownAlignment ? xMaxDisp : xMaxDisp - 2) - 2;
226 unsigned getLongFormMaxDisp() const {
227 return (KnownAlignment ? LongFormMaxDisp : LongFormMaxDisp - 2) - 2;
229 unsigned getLongFormOpcode() const {
230 return LongFormOpcode;
234 /// CPUsers - Keep track of all of the machine instructions that use various
235 /// constant pools and their max displacement.
236 std::vector<CPUser> CPUsers;
238 /// CPEntry - One per constant pool entry, keeping the machine instruction
239 /// pointer, the constpool index, and the number of CPUser's which
240 /// reference this entry.
245 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
246 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
249 /// CPEntries - Keep track of all of the constant pool entry machine
250 /// instructions. For each original constpool index (i.e. those that
251 /// existed upon entry to this pass), it keeps a vector of entries.
252 /// Original elements are cloned as we go along; the clones are
253 /// put in the vector of the original element, but have distinct CPIs.
254 std::vector<std::vector<CPEntry> > CPEntries;
256 /// ImmBranch - One per immediate branch, keeping the machine instruction
257 /// pointer, conditional or unconditional, the max displacement,
258 /// and (if isCond is true) the corresponding unconditional branch
262 unsigned MaxDisp : 31;
265 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
266 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
269 /// ImmBranches - Keep track of all the immediate branch instructions.
271 std::vector<ImmBranch> ImmBranches;
273 /// HasFarJump - True if any far jump instruction has been emitted during
274 /// the branch fix up pass.
277 const TargetMachine &TM;
280 const MipsSubtarget *STI;
281 const MipsInstrInfo *TII;
282 MipsFunctionInfo *MFI;
284 MachineConstantPool *MCP;
286 unsigned PICLabelUId;
287 bool PrescannedForConstants;
289 void initPICLabelUId(unsigned UId) {
294 unsigned createPICLabelUId() {
295 return PICLabelUId++;
300 MipsConstantIslands(TargetMachine &tm)
301 : MachineFunctionPass(ID), TM(tm),
302 IsPIC(TM.getRelocationModel() == Reloc::PIC_),
303 ABI(TM.getSubtarget<MipsSubtarget>().getTargetABI()),
304 STI(&TM.getSubtarget<MipsSubtarget>()), MF(0), MCP(0), PrescannedForConstants(false){}
306 virtual const char *getPassName() const {
307 return "Mips Constant Islands";
310 bool runOnMachineFunction(MachineFunction &F);
312 void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
313 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
314 unsigned getCPELogAlign(const MachineInstr *CPEMI);
315 void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
316 unsigned getOffsetOf(MachineInstr *MI) const;
317 unsigned getUserOffset(CPUser&) const;
321 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
322 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
323 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
325 return isOffsetInRange(UserOffset, TrialOffset,
326 U.getMaxDisp(), U.NegOk, U.IsSoImm);
328 bool isLongFormOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
330 return isOffsetInRange(UserOffset, TrialOffset,
331 U.getLongFormMaxDisp(), U.NegOk, U.IsSoImm);
333 void computeBlockSize(MachineBasicBlock *MBB);
334 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI);
335 void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
336 void adjustBBOffsetsAfter(MachineBasicBlock *BB);
337 bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
338 int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
339 int findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset);
340 bool findAvailableWater(CPUser&U, unsigned UserOffset,
341 water_iterator &WaterIter);
342 void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
343 MachineBasicBlock *&NewMBB);
344 bool handleConstantPoolUser(unsigned CPUserIndex);
345 void removeDeadCPEMI(MachineInstr *CPEMI);
346 bool removeUnusedCPEntries();
347 bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
348 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
349 bool DoDump = false);
350 bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
351 CPUser &U, unsigned &Growth);
352 bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
353 bool fixupImmediateBr(ImmBranch &Br);
354 bool fixupConditionalBr(ImmBranch &Br);
355 bool fixupUnconditionalBr(ImmBranch &Br);
357 void prescanForConstants();
363 char MipsConstantIslands::ID = 0;
364 } // end of anonymous namespace
366 /// print block size and offset information - debugging
367 void MipsConstantIslands::dumpBBs() {
369 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
370 const BasicBlockInfo &BBI = BBInfo[J];
371 dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
372 << " kb=" << unsigned(BBI.KnownBits)
373 << " ua=" << unsigned(BBI.Unalign)
374 << " pa=" << unsigned(BBI.PostAlign)
375 << format(" size=%#x\n", BBInfo[J].Size);
379 /// createMipsLongBranchPass - Returns a pass that converts branches to long
381 FunctionPass *llvm::createMipsConstantIslandPass(MipsTargetMachine &tm) {
382 return new MipsConstantIslands(tm);
385 bool MipsConstantIslands::runOnMachineFunction(MachineFunction &mf) {
386 // The intention is for this to be a mips16 only pass for now
389 MCP = mf.getConstantPool();
390 DEBUG(dbgs() << "constant island machine function " << "\n");
391 if (!TM.getSubtarget<MipsSubtarget>().inMips16Mode() ||
392 !MipsSubtarget::useConstantIslands()) {
395 TII = (const MipsInstrInfo*)MF->getTarget().getInstrInfo();
396 MFI = MF->getInfo<MipsFunctionInfo>();
397 DEBUG(dbgs() << "constant island processing " << "\n");
399 // will need to make predermination if there is any constants we need to
400 // put in constant islands. TBD.
402 if (!PrescannedForConstants) prescanForConstants();
405 // This pass invalidates liveness information when it splits basic blocks.
406 MF->getRegInfo().invalidateLiveness();
408 // Renumber all of the machine basic blocks in the function, guaranteeing that
409 // the numbers agree with the position of the block in the function.
410 MF->RenumberBlocks();
412 bool MadeChange = false;
414 // Perform the initial placement of the constant pool entries. To start with,
415 // we put them all at the end of the function.
416 std::vector<MachineInstr*> CPEMIs;
418 doInitialPlacement(CPEMIs);
420 /// The next UID to take is the first unused one.
421 initPICLabelUId(CPEMIs.size());
423 // Do the initial scan of the function, building up information about the
424 // sizes of each block, the location of all the water, and finding all of the
425 // constant pool users.
426 initializeFunctionInfo(CPEMIs);
430 /// Remove dead constant pool entries.
431 MadeChange |= removeUnusedCPEntries();
433 // Iteratively place constant pool entries and fix up branches until there
435 unsigned NoCPIters = 0, NoBRIters = 0;
438 DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
439 bool CPChange = false;
440 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
441 CPChange |= handleConstantPoolUser(i);
442 if (CPChange && ++NoCPIters > 30)
443 report_fatal_error("Constant Island pass failed to converge!");
446 // Clear NewWaterList now. If we split a block for branches, it should
447 // appear as "new water" for the next iteration of constant pool placement.
448 NewWaterList.clear();
450 DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
451 bool BRChange = false;
453 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
454 BRChange |= fixupImmediateBr(ImmBranches[i]);
455 if (BRChange && ++NoBRIters > 30)
456 report_fatal_error("Branch Fix Up pass failed to converge!");
459 if (!CPChange && !BRChange)
464 DEBUG(dbgs() << '\n'; dumpBBs());
474 /// doInitialPlacement - Perform the initial placement of the constant pool
475 /// entries. To start with, we put them all at the end of the function.
477 MipsConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
478 // Create the basic block to hold the CPE's.
479 MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
483 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
484 unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
486 // Mark the basic block as required by the const-pool.
487 // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
488 BB->setAlignment(AlignConstantIslands ? MaxAlign : 2);
490 // The function needs to be as aligned as the basic blocks. The linker may
491 // move functions around based on their alignment.
492 MF->ensureAlignment(BB->getAlignment());
494 // Order the entries in BB by descending alignment. That ensures correct
495 // alignment of all entries as long as BB is sufficiently aligned. Keep
496 // track of the insertion point for each alignment. We are going to bucket
497 // sort the entries as they are created.
498 SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end());
500 // Add all of the constants from the constant pool to the end block, use an
501 // identity mapping of CPI's to CPE's.
502 const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
504 const DataLayout &TD = *MF->getTarget().getDataLayout();
505 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
506 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
507 assert(Size >= 4 && "Too small constant pool entry");
508 unsigned Align = CPs[i].getAlignment();
509 assert(isPowerOf2_32(Align) && "Invalid alignment");
510 // Verify that all constant pool entries are a multiple of their alignment.
511 // If not, we would have to pad them out so that instructions stay aligned.
512 assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
514 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
515 unsigned LogAlign = Log2_32(Align);
516 MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
518 MachineInstr *CPEMI =
519 BuildMI(*BB, InsAt, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
520 .addImm(i).addConstantPoolIndex(i).addImm(Size);
522 CPEMIs.push_back(CPEMI);
524 // Ensure that future entries with higher alignment get inserted before
525 // CPEMI. This is bucket sort with iterators.
526 for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a)
527 if (InsPoint[a] == InsAt)
529 // Add a new CPEntry, but no corresponding CPUser yet.
530 std::vector<CPEntry> CPEs;
531 CPEs.push_back(CPEntry(CPEMI, i));
532 CPEntries.push_back(CPEs);
534 DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
535 << Size << ", align = " << Align <<'\n');
540 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
541 /// into the block immediately after it.
542 static bool BBHasFallthrough(MachineBasicBlock *MBB) {
543 // Get the next machine basic block in the function.
544 MachineFunction::iterator MBBI = MBB;
545 // Can't fall off end of function.
546 if (llvm::next(MBBI) == MBB->getParent()->end())
549 MachineBasicBlock *NextBB = llvm::next(MBBI);
550 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
551 E = MBB->succ_end(); I != E; ++I)
558 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
559 /// look up the corresponding CPEntry.
560 MipsConstantIslands::CPEntry
561 *MipsConstantIslands::findConstPoolEntry(unsigned CPI,
562 const MachineInstr *CPEMI) {
563 std::vector<CPEntry> &CPEs = CPEntries[CPI];
564 // Number of entries per constpool index should be small, just do a
566 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
567 if (CPEs[i].CPEMI == CPEMI)
573 /// getCPELogAlign - Returns the required alignment of the constant pool entry
574 /// represented by CPEMI. Alignment is measured in log2(bytes) units.
575 unsigned MipsConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
576 assert(CPEMI && CPEMI->getOpcode() == Mips::CONSTPOOL_ENTRY);
578 // Everything is 4-byte aligned unless AlignConstantIslands is set.
579 if (!AlignConstantIslands)
582 unsigned CPI = CPEMI->getOperand(1).getIndex();
583 assert(CPI < MCP->getConstants().size() && "Invalid constant pool index.");
584 unsigned Align = MCP->getConstants()[CPI].getAlignment();
585 assert(isPowerOf2_32(Align) && "Invalid CPE alignment");
586 return Log2_32(Align);
589 /// initializeFunctionInfo - Do the initial scan of the function, building up
590 /// information about the sizes of each block, the location of all the water,
591 /// and finding all of the constant pool users.
592 void MipsConstantIslands::
593 initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
595 BBInfo.resize(MF->getNumBlockIDs());
597 // First thing, compute the size of all basic blocks, and see if the function
598 // has any inline assembly in it. If so, we have to be conservative about
599 // alignment assumptions, as we don't know for sure the size of any
600 // instructions in the inline assembly.
601 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
604 // The known bits of the entry block offset are determined by the function
606 BBInfo.front().KnownBits = MF->getAlignment();
608 // Compute block offsets.
609 adjustBBOffsetsAfter(MF->begin());
611 // Now go back through the instructions and build up our data structures.
612 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
614 MachineBasicBlock &MBB = *MBBI;
616 // If this block doesn't fall through into the next MBB, then this is
617 // 'water' that a constant pool island could be placed.
618 if (!BBHasFallthrough(&MBB))
619 WaterList.push_back(&MBB);
620 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
622 if (I->isDebugValue())
625 int Opc = I->getOpcode();
634 continue; // Ignore other JT branches
637 T2JumpTables.push_back(I);
638 continue; // Does not get an entry in ImmBranches
669 // Record this immediate branch.
670 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
671 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
676 if (Opc == Mips::CONSTPOOL_ENTRY)
680 // Scan the instructions for constant pool operands.
681 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
682 if (I->getOperand(op).isCPI()) {
684 // We found one. The addressing mode tells us the max displacement
685 // from the PC that this instruction permits.
687 // Basic size info comes from the TSFlags field.
691 bool IsSoImm = false;
692 unsigned LongFormBits = 0;
693 unsigned LongFormScale = 0;
694 unsigned LongFormOpcode = 0;
697 llvm_unreachable("Unknown addressing mode for CP reference!");
698 case Mips::LwRxPcTcp16:
701 LongFormOpcode = Mips::LwRxPcTcpX16;
703 case Mips::LwRxPcTcpX16:
708 // Taking the address of a CP entry.
710 // This takes a SoImm, which is 8 bit immediate rotated. We'll
711 // pretend the maximum offset is 255 * 4. Since each instruction
712 // 4 byte wide, this is always correct. We'll check for other
713 // displacements that fits in a SoImm as well.
719 case ARM::t2LEApcrel:
732 Bits = 12; // +-offset_12
738 Scale = 4; // +(offset_8*4)
744 Scale = 4; // +-(offset_8*4)
749 // Remember that this is a user of a CP entry.
750 unsigned CPI = I->getOperand(op).getIndex();
751 MachineInstr *CPEMI = CPEMIs[CPI];
752 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
753 unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale;
754 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm, LongFormMaxOffs,
757 // Increment corresponding CPEntry reference count.
758 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
759 assert(CPE && "Cannot find a corresponding CPEntry!");
762 // Instructions can only use one CP entry, don't bother scanning the
763 // rest of the operands.
773 /// computeBlockSize - Compute the size and some alignment information for MBB.
774 /// This function updates BBInfo directly.
775 void MipsConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
776 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
781 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
783 BBI.Size += TII->GetInstSizeInBytes(I);
787 /// getOffsetOf - Return the current offset of the specified machine instruction
788 /// from the start of the function. This offset changes as stuff is moved
789 /// around inside the function.
790 unsigned MipsConstantIslands::getOffsetOf(MachineInstr *MI) const {
791 MachineBasicBlock *MBB = MI->getParent();
793 // The offset is composed of two things: the sum of the sizes of all MBB's
794 // before this instruction's block, and the offset from the start of the block
796 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
798 // Sum instructions before MI in MBB.
799 for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) {
800 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
801 Offset += TII->GetInstSizeInBytes(I);
806 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
808 static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
809 const MachineBasicBlock *RHS) {
810 return LHS->getNumber() < RHS->getNumber();
813 /// updateForInsertedWaterBlock - When a block is newly inserted into the
814 /// machine function, it upsets all of the block numbers. Renumber the blocks
815 /// and update the arrays that parallel this numbering.
816 void MipsConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
817 // Renumber the MBB's to keep them consecutive.
818 NewBB->getParent()->RenumberBlocks(NewBB);
820 // Insert an entry into BBInfo to align it properly with the (newly
821 // renumbered) block numbers.
822 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
824 // Next, update WaterList. Specifically, we need to add NewMBB as having
825 // available water after it.
827 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
829 WaterList.insert(IP, NewBB);
832 /// getUserOffset - Compute the offset of U.MI as seen by the hardware
833 /// displacement computation. Update U.KnownAlignment to match its current
834 /// basic block location.
835 unsigned MipsConstantIslands::getUserOffset(CPUser &U) const {
836 unsigned UserOffset = getOffsetOf(U.MI);
837 const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()];
838 unsigned KnownBits = BBI.internalKnownBits();
840 // The value read from PC is offset from the actual instruction address.
842 UserOffset += (isThumb ? 4 : 8);
845 // Because of inline assembly, we may not know the alignment (mod 4) of U.MI.
846 // Make sure U.getMaxDisp() returns a constrained range.
847 U.KnownAlignment = (KnownBits >= 2);
849 // On Thumb, offsets==2 mod 4 are rounded down by the hardware for
850 // purposes of the displacement computation; compensate for that here.
851 // For unknown alignments, getMaxDisp() constrains the range instead.
853 if (isThumb && U.KnownAlignment)
860 /// Split the basic block containing MI into two blocks, which are joined by
861 /// an unconditional branch. Update data structures and renumber blocks to
862 /// account for this change and returns the newly created block.
863 MachineBasicBlock *MipsConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
864 MachineBasicBlock *OrigBB = MI->getParent();
866 // Create a new MBB for the code after the OrigBB.
867 MachineBasicBlock *NewBB =
868 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
869 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
870 MF->insert(MBBI, NewBB);
872 // Splice the instructions starting with MI over to NewBB.
873 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
875 // Add an unconditional branch from OrigBB to NewBB.
876 // Note the new unconditional branch is not being recorded.
877 // There doesn't seem to be meaningful DebugInfo available; this doesn't
878 // correspond to anything in the source.
879 BuildMI(OrigBB, DebugLoc(), TII->get(Mips::BimmX16)).addMBB(NewBB);
881 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
883 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
885 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
886 .addImm(ARMCC::AL).addReg(0);
890 // Update the CFG. All succs of OrigBB are now succs of NewBB.
891 NewBB->transferSuccessors(OrigBB);
893 // OrigBB branches to NewBB.
894 OrigBB->addSuccessor(NewBB);
896 // Update internal data structures to account for the newly inserted MBB.
897 // This is almost the same as updateForInsertedWaterBlock, except that
898 // the Water goes after OrigBB, not NewBB.
899 MF->RenumberBlocks(NewBB);
901 // Insert an entry into BBInfo to align it properly with the (newly
902 // renumbered) block numbers.
903 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
905 // Next, update WaterList. Specifically, we need to add OrigMBB as having
906 // available water after it (but not if it's already there, which happens
907 // when splitting before a conditional branch that is followed by an
908 // unconditional branch - in that case we want to insert NewBB).
910 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
912 MachineBasicBlock* WaterBB = *IP;
913 if (WaterBB == OrigBB)
914 WaterList.insert(llvm::next(IP), NewBB);
916 WaterList.insert(IP, OrigBB);
917 NewWaterList.insert(OrigBB);
919 // Figure out how large the OrigBB is. As the first half of the original
920 // block, it cannot contain a tablejump. The size includes
921 // the new jump we added. (It should be possible to do this without
922 // recounting everything, but it's very confusing, and this is rarely
924 computeBlockSize(OrigBB);
926 // Figure out how large the NewMBB is. As the second half of the original
927 // block, it may contain a tablejump.
928 computeBlockSize(NewBB);
930 // All BBOffsets following these blocks must be modified.
931 adjustBBOffsetsAfter(OrigBB);
939 /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
940 /// reference) is within MaxDisp of TrialOffset (a proposed location of a
941 /// constant pool entry).
942 /// UserOffset is computed by getUserOffset above to include PC adjustments. If
943 /// the mod 4 alignment of UserOffset is not known, the uncertainty must be
944 /// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
945 bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset,
946 unsigned TrialOffset, unsigned MaxDisp,
947 bool NegativeOK, bool IsSoImm) {
948 if (UserOffset <= TrialOffset) {
949 // User before the Trial.
950 if (TrialOffset - UserOffset <= MaxDisp)
952 // FIXME: Make use full range of soimm values.
953 } else if (NegativeOK) {
954 if (UserOffset - TrialOffset <= MaxDisp)
956 // FIXME: Make use full range of soimm values.
961 /// isWaterInRange - Returns true if a CPE placed after the specified
962 /// Water (a basic block) will be in range for the specific MI.
964 /// Compute how much the function will grow by inserting a CPE after Water.
965 bool MipsConstantIslands::isWaterInRange(unsigned UserOffset,
966 MachineBasicBlock* Water, CPUser &U,
968 unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
969 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
970 unsigned NextBlockOffset, NextBlockAlignment;
971 MachineFunction::const_iterator NextBlock = Water;
972 if (++NextBlock == MF->end()) {
973 NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
974 NextBlockAlignment = 0;
976 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
977 NextBlockAlignment = NextBlock->getAlignment();
979 unsigned Size = U.CPEMI->getOperand(2).getImm();
980 unsigned CPEEnd = CPEOffset + Size;
982 // The CPE may be able to hide in the alignment padding before the next
983 // block. It may also cause more padding to be required if it is more aligned
984 // that the next block.
985 if (CPEEnd > NextBlockOffset) {
986 Growth = CPEEnd - NextBlockOffset;
987 // Compute the padding that would go at the end of the CPE to align the next
989 Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment);
991 // If the CPE is to be inserted before the instruction, that will raise
992 // the offset of the instruction. Also account for unknown alignment padding
993 // in blocks between CPE and the user.
994 if (CPEOffset < UserOffset)
995 UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign);
997 // CPE fits in existing padding.
1000 return isOffsetInRange(UserOffset, CPEOffset, U);
1003 /// isCPEntryInRange - Returns true if the distance between specific MI and
1004 /// specific ConstPool entry instruction can fit in MI's displacement field.
1005 bool MipsConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
1006 MachineInstr *CPEMI, unsigned MaxDisp,
1007 bool NegOk, bool DoDump) {
1008 unsigned CPEOffset = getOffsetOf(CPEMI);
1012 unsigned Block = MI->getParent()->getNumber();
1013 const BasicBlockInfo &BBI = BBInfo[Block];
1014 dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
1015 << " max delta=" << MaxDisp
1016 << format(" insn address=%#x", UserOffset)
1017 << " in BB#" << Block << ": "
1018 << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
1019 << format("CPE address=%#x offset=%+d: ", CPEOffset,
1020 int(CPEOffset-UserOffset));
1024 return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
1027 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
1028 /// unconditionally branches to its only successor.
1029 static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
1030 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
1034 MachineBasicBlock *Succ = *MBB->succ_begin();
1035 MachineBasicBlock *Pred = *MBB->pred_begin();
1036 MachineInstr *PredMI = &Pred->back();
1037 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
1038 || PredMI->getOpcode() == ARM::t2B)
1039 return PredMI->getOperand(0).getMBB() == Succ;
1045 void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
1046 unsigned BBNum = BB->getNumber();
1047 for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
1048 // Get the offset and known bits at the end of the layout predecessor.
1049 // Include the alignment of the current block.
1050 unsigned Offset = BBInfo[i - 1].postOffset();
1051 BBInfo[i].Offset = Offset;
1055 /// decrementCPEReferenceCount - find the constant pool entry with index CPI
1056 /// and instruction CPEMI, and decrement its refcount. If the refcount
1057 /// becomes 0 remove the entry and instruction. Returns true if we removed
1058 /// the entry, false if we didn't.
1060 bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI,
1061 MachineInstr *CPEMI) {
1062 // Find the old entry. Eliminate it if it is no longer used.
1063 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1064 assert(CPE && "Unexpected!");
1065 if (--CPE->RefCount == 0) {
1066 removeDeadCPEMI(CPEMI);
1074 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1075 /// if not, see if an in-range clone of the CPE is in range, and if so,
1076 /// change the data structures so the user references the clone. Returns:
1077 /// 0 = no existing entry found
1078 /// 1 = entry found, and there were no code insertions or deletions
1079 /// 2 = entry found, and there were code insertions or deletions
1080 int MipsConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
1082 MachineInstr *UserMI = U.MI;
1083 MachineInstr *CPEMI = U.CPEMI;
1085 // Check to see if the CPE is already in-range.
1086 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
1088 DEBUG(dbgs() << "In range\n");
1092 // No. Look for previously created clones of the CPE that are in range.
1093 unsigned CPI = CPEMI->getOperand(1).getIndex();
1094 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1095 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1096 // We already tried this one
1097 if (CPEs[i].CPEMI == CPEMI)
1099 // Removing CPEs can leave empty entries, skip
1100 if (CPEs[i].CPEMI == NULL)
1102 if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
1104 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1105 << CPEs[i].CPI << "\n");
1106 // Point the CPUser node to the replacement
1107 U.CPEMI = CPEs[i].CPEMI;
1108 // Change the CPI in the instruction operand to refer to the clone.
1109 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1110 if (UserMI->getOperand(j).isCPI()) {
1111 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1114 // Adjust the refcount of the clone...
1116 // ...and the original. If we didn't remove the old entry, none of the
1117 // addresses changed, so we don't need another pass.
1118 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1124 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1125 /// This version checks if the longer form of the instruction can be used to
1126 /// to satisfy things.
1127 /// if not, see if an in-range clone of the CPE is in range, and if so,
1128 /// change the data structures so the user references the clone. Returns:
1129 /// 0 = no existing entry found
1130 /// 1 = entry found, and there were no code insertions or deletions
1131 /// 2 = entry found, and there were code insertions or deletions
1132 int MipsConstantIslands::findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset)
1134 MachineInstr *UserMI = U.MI;
1135 MachineInstr *CPEMI = U.CPEMI;
1137 // Check to see if the CPE is already in-range.
1138 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getLongFormMaxDisp(), U.NegOk,
1140 DEBUG(dbgs() << "In range\n");
1141 UserMI->setDesc(TII->get(U.getLongFormOpcode()));
1142 return 2; // instruction is longer length now
1145 // No. Look for previously created clones of the CPE that are in range.
1146 unsigned CPI = CPEMI->getOperand(1).getIndex();
1147 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1148 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1149 // We already tried this one
1150 if (CPEs[i].CPEMI == CPEMI)
1152 // Removing CPEs can leave empty entries, skip
1153 if (CPEs[i].CPEMI == NULL)
1155 if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getLongFormMaxDisp(),
1157 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1158 << CPEs[i].CPI << "\n");
1159 // Point the CPUser node to the replacement
1160 U.CPEMI = CPEs[i].CPEMI;
1161 // Change the CPI in the instruction operand to refer to the clone.
1162 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1163 if (UserMI->getOperand(j).isCPI()) {
1164 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1167 // Adjust the refcount of the clone...
1169 // ...and the original. If we didn't remove the old entry, none of the
1170 // addresses changed, so we don't need another pass.
1171 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1177 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1178 /// the specific unconditional branch instruction.
1179 static inline unsigned getUnconditionalBrDisp(int Opc) {
1183 return ((1<<10)-1)*2;
1185 return ((1<<23)-1)*2;
1190 return ((1<<23)-1)*4;
1193 /// findAvailableWater - Look for an existing entry in the WaterList in which
1194 /// we can place the CPE referenced from U so it's within range of U's MI.
1195 /// Returns true if found, false if not. If it returns true, WaterIter
1196 /// is set to the WaterList entry. For Thumb, prefer water that will not
1197 /// introduce padding to water that will. To ensure that this pass
1198 /// terminates, the CPE location for a particular CPUser is only allowed to
1199 /// move to a lower address, so search backward from the end of the list and
1200 /// prefer the first water that is in range.
1201 bool MipsConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
1202 water_iterator &WaterIter) {
1203 if (WaterList.empty())
1206 unsigned BestGrowth = ~0u;
1207 for (water_iterator IP = prior(WaterList.end()), B = WaterList.begin();;
1209 MachineBasicBlock* WaterBB = *IP;
1210 // Check if water is in range and is either at a lower address than the
1211 // current "high water mark" or a new water block that was created since
1212 // the previous iteration by inserting an unconditional branch. In the
1213 // latter case, we want to allow resetting the high water mark back to
1214 // this new water since we haven't seen it before. Inserting branches
1215 // should be relatively uncommon and when it does happen, we want to be
1216 // sure to take advantage of it for all the CPEs near that block, so that
1217 // we don't insert more branches than necessary.
1219 if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1220 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1221 NewWaterList.count(WaterBB)) && Growth < BestGrowth) {
1222 // This is the least amount of required padding seen so far.
1223 BestGrowth = Growth;
1225 DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber()
1226 << " Growth=" << Growth << '\n');
1228 // Keep looking unless it is perfect.
1229 if (BestGrowth == 0)
1235 return BestGrowth != ~0u;
1238 /// createNewWater - No existing WaterList entry will work for
1239 /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1240 /// block is used if in range, and the conditional branch munged so control
1241 /// flow is correct. Otherwise the block is split to create a hole with an
1242 /// unconditional branch around it. In either case NewMBB is set to a
1243 /// block following which the new island can be inserted (the WaterList
1244 /// is not adjusted).
1245 void MipsConstantIslands::createNewWater(unsigned CPUserIndex,
1246 unsigned UserOffset,
1247 MachineBasicBlock *&NewMBB) {
1248 CPUser &U = CPUsers[CPUserIndex];
1249 MachineInstr *UserMI = U.MI;
1250 MachineInstr *CPEMI = U.CPEMI;
1251 unsigned CPELogAlign = getCPELogAlign(CPEMI);
1252 MachineBasicBlock *UserMBB = UserMI->getParent();
1253 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
1255 // If the block does not end in an unconditional branch already, and if the
1256 // end of the block is within range, make new water there. (The addition
1257 // below is for the unconditional branch we will be adding: 4 bytes on ARM +
1258 // Thumb2, 2 on Thumb1.
1259 if (BBHasFallthrough(UserMBB)) {
1260 // Size of branch to insert.
1262 unsigned Delta = isThumb1 ? 2 : 4;
1266 // Compute the offset where the CPE will begin.
1267 unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
1269 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1270 DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
1271 << format(", expected CPE offset %#x\n", CPEOffset));
1272 NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
1273 // Add an unconditional branch from UserMBB to fallthrough block. Record
1274 // it for branch lengthening; this new branch will not get out of range,
1275 // but if the preceding conditional branch is out of range, the targets
1276 // will be exchanged, and the altered branch may be out of range, so the
1277 // machinery has to know about it.
1279 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
1281 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1283 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
1284 .addImm(ARMCC::AL).addReg(0);
1288 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
1289 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
1290 MaxDisp, false, UncondBr));
1291 BBInfo[UserMBB->getNumber()].Size += Delta;
1292 adjustBBOffsetsAfter(UserMBB);
1297 // What a big block. Find a place within the block to split it. This is a
1298 // little tricky on Thumb1 since instructions are 2 bytes and constant pool
1299 // entries are 4 bytes: if instruction I references island CPE, and
1300 // instruction I+1 references CPE', it will not work well to put CPE as far
1301 // forward as possible, since then CPE' cannot immediately follow it (that
1302 // location is 2 bytes farther away from I+1 than CPE was from I) and we'd
1303 // need to create a new island. So, we make a first guess, then walk through
1304 // the instructions between the one currently being looked at and the
1305 // possible insertion point, and make sure any other instructions that
1306 // reference CPEs will be able to use the same island area; if not, we back
1307 // up the insertion point.
1309 // Try to split the block so it's fully aligned. Compute the latest split
1310 // point where we can add a 4-byte branch instruction, and then align to
1311 // LogAlign which is the largest possible alignment in the function.
1312 unsigned LogAlign = MF->getAlignment();
1313 assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
1314 unsigned KnownBits = UserBBI.internalKnownBits();
1315 unsigned UPad = UnknownPadding(LogAlign, KnownBits);
1316 unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad;
1317 DEBUG(dbgs() << format("Split in middle of big block before %#x",
1320 // The 4 in the following is for the unconditional branch we'll be inserting
1321 // (allows for long branch on Thumb1). Alignment of the island is handled
1322 // inside isOffsetInRange.
1323 BaseInsertOffset -= 4;
1325 DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
1326 << " la=" << LogAlign
1327 << " kb=" << KnownBits
1328 << " up=" << UPad << '\n');
1330 // This could point off the end of the block if we've already got constant
1331 // pool entries following this block; only the last one is in the water list.
1332 // Back past any possible branches (allow for a conditional and a maximally
1333 // long unconditional).
1334 if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
1335 BaseInsertOffset = UserBBI.postOffset() - UPad - 8;
1336 DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
1338 unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad +
1339 CPEMI->getOperand(2).getImm();
1340 MachineBasicBlock::iterator MI = UserMI;
1342 unsigned CPUIndex = CPUserIndex+1;
1343 unsigned NumCPUsers = CPUsers.size();
1344 MachineInstr *LastIT = 0;
1345 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
1346 Offset < BaseInsertOffset;
1347 Offset += TII->GetInstSizeInBytes(MI),
1348 MI = llvm::next(MI)) {
1349 assert(MI != UserMBB->end() && "Fell off end of block");
1350 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
1351 CPUser &U = CPUsers[CPUIndex];
1352 if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
1353 // Shift intertion point by one unit of alignment so it is within reach.
1354 BaseInsertOffset -= 1u << LogAlign;
1355 EndInsertOffset -= 1u << LogAlign;
1357 // This is overly conservative, as we don't account for CPEMIs being
1358 // reused within the block, but it doesn't matter much. Also assume CPEs
1359 // are added in order with alignment padding. We may eventually be able
1360 // to pack the aligned CPEs better.
1361 EndInsertOffset += U.CPEMI->getOperand(2).getImm();
1365 // Remember the last IT instruction.
1366 if (MI->getOpcode() == ARM::t2IT)
1373 // Avoid splitting an IT block.
1376 unsigned PredReg = 0;
1377 ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg);
1378 if (CC != ARMCC::AL)
1382 NewMBB = splitBlockBeforeInstr(MI);
1385 /// handleConstantPoolUser - Analyze the specified user, checking to see if it
1386 /// is out-of-range. If so, pick up the constant pool value and move it some
1387 /// place in-range. Return true if we changed any addresses (thus must run
1388 /// another pass of branch lengthening), false otherwise.
1389 bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
1390 CPUser &U = CPUsers[CPUserIndex];
1391 MachineInstr *UserMI = U.MI;
1392 MachineInstr *CPEMI = U.CPEMI;
1393 unsigned CPI = CPEMI->getOperand(1).getIndex();
1394 unsigned Size = CPEMI->getOperand(2).getImm();
1395 // Compute this only once, it's expensive.
1396 unsigned UserOffset = getUserOffset(U);
1398 // See if the current entry is within range, or there is a clone of it
1400 int result = findInRangeCPEntry(U, UserOffset);
1401 if (result==1) return false;
1402 else if (result==2) return true;
1405 // Look for water where we can place this CPE.
1406 MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1407 MachineBasicBlock *NewMBB;
1409 if (findAvailableWater(U, UserOffset, IP)) {
1410 DEBUG(dbgs() << "Found water in range\n");
1411 MachineBasicBlock *WaterBB = *IP;
1413 // If the original WaterList entry was "new water" on this iteration,
1414 // propagate that to the new island. This is just keeping NewWaterList
1415 // updated to match the WaterList, which will be updated below.
1416 if (NewWaterList.erase(WaterBB))
1417 NewWaterList.insert(NewIsland);
1419 // The new CPE goes before the following block (NewMBB).
1420 NewMBB = llvm::next(MachineFunction::iterator(WaterBB));
1424 // we first see if a longer form of the instrucion could have reached the constant.
1425 // in that case we won't bother to split
1427 result = findLongFormInRangeCPEntry(U, UserOffset);
1429 DEBUG(dbgs() << "No water found\n");
1430 createNewWater(CPUserIndex, UserOffset, NewMBB);
1432 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1433 // called while handling branches so that the water will be seen on the
1434 // next iteration for constant pools, but in this context, we don't want
1435 // it. Check for this so it will be removed from the WaterList.
1436 // Also remove any entry from NewWaterList.
1437 MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB));
1438 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1439 if (IP != WaterList.end())
1440 NewWaterList.erase(WaterBB);
1442 // We are adding new water. Update NewWaterList.
1443 NewWaterList.insert(NewIsland);
1446 // Remove the original WaterList entry; we want subsequent insertions in
1447 // this vicinity to go after the one we're about to insert. This
1448 // considerably reduces the number of times we have to move the same CPE
1449 // more than once and is also important to ensure the algorithm terminates.
1450 if (IP != WaterList.end())
1451 WaterList.erase(IP);
1453 // Okay, we know we can put an island before NewMBB now, do it!
1454 MF->insert(NewMBB, NewIsland);
1456 // Update internal data structures to account for the newly inserted MBB.
1457 updateForInsertedWaterBlock(NewIsland);
1459 // Decrement the old entry, and remove it if refcount becomes 0.
1460 decrementCPEReferenceCount(CPI, CPEMI);
1462 // Now that we have an island to add the CPE to, clone the original CPE and
1463 // add it to the island.
1464 U.HighWaterMark = NewIsland;
1465 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
1466 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
1467 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
1470 // Mark the basic block as aligned as required by the const-pool entry.
1471 NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
1473 // Increase the size of the island block to account for the new entry.
1474 BBInfo[NewIsland->getNumber()].Size += Size;
1475 adjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland)));
1477 // No existing clone of this CPE is within range.
1478 // We will be generating a new clone. Get a UID for it.
1479 unsigned ID = createPICLabelUId();
1481 // Finally, change the CPI in the instruction operand to be ID.
1482 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1483 if (UserMI->getOperand(i).isCPI()) {
1484 UserMI->getOperand(i).setIndex(ID);
1488 DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
1489 << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
1494 /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1495 /// sizes and offsets of impacted basic blocks.
1496 void MipsConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
1497 MachineBasicBlock *CPEBB = CPEMI->getParent();
1498 unsigned Size = CPEMI->getOperand(2).getImm();
1499 CPEMI->eraseFromParent();
1500 BBInfo[CPEBB->getNumber()].Size -= Size;
1501 // All succeeding offsets have the current size value added in, fix this.
1502 if (CPEBB->empty()) {
1503 BBInfo[CPEBB->getNumber()].Size = 0;
1505 // This block no longer needs to be aligned.
1506 CPEBB->setAlignment(0);
1508 // Entries are sorted by descending alignment, so realign from the front.
1509 CPEBB->setAlignment(getCPELogAlign(CPEBB->begin()));
1511 adjustBBOffsetsAfter(CPEBB);
1512 // An island has only one predecessor BB and one successor BB. Check if
1513 // this BB's predecessor jumps directly to this BB's successor. This
1514 // shouldn't happen currently.
1515 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1516 // FIXME: remove the empty blocks after all the work is done?
1519 /// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1521 bool MipsConstantIslands::removeUnusedCPEntries() {
1522 unsigned MadeChange = false;
1523 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1524 std::vector<CPEntry> &CPEs = CPEntries[i];
1525 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1526 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1527 removeDeadCPEMI(CPEs[j].CPEMI);
1528 CPEs[j].CPEMI = NULL;
1536 /// isBBInRange - Returns true if the distance between specific MI and
1537 /// specific BB can fit in MI's displacement field.
1538 bool MipsConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1541 unsigned PCAdj = isThumb ? 4 : 8;
1545 unsigned BrOffset = getOffsetOf(MI) + PCAdj;
1546 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
1548 DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
1549 << " from BB#" << MI->getParent()->getNumber()
1550 << " max delta=" << MaxDisp
1551 << " from " << getOffsetOf(MI) << " to " << DestOffset
1552 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
1554 if (BrOffset <= DestOffset) {
1555 // Branch before the Dest.
1556 if (DestOffset-BrOffset <= MaxDisp)
1559 if (BrOffset-DestOffset <= MaxDisp)
1565 /// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1566 /// away to fit in its displacement field.
1567 bool MipsConstantIslands::fixupImmediateBr(ImmBranch &Br) {
1568 MachineInstr *MI = Br.MI;
1569 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1571 // Check to see if the DestBB is already in-range.
1572 if (isBBInRange(MI, DestBB, Br.MaxDisp))
1576 return fixupUnconditionalBr(Br);
1577 return fixupConditionalBr(Br);
1580 /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1581 /// too far away to fit in its displacement field. If the LR register has been
1582 /// spilled in the epilogue, then we can use BL to implement a far jump.
1583 /// Otherwise, add an intermediate branch instruction to a branch.
1585 MipsConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
1586 MachineInstr *MI = Br.MI;
1587 MachineBasicBlock *MBB = MI->getParent();
1590 llvm_unreachable("fixupUnconditionalBr is Thumb1 only!");
1592 // Use BL to implement far jump.
1593 Br.MaxDisp = (1 << 21) * 2;
1595 MI->setDesc(TII->get(ARM::tBfar));
1597 BBInfo[MBB->getNumber()].Size += 2;
1598 adjustBBOffsetsAfter(MBB);
1602 DEBUG(dbgs() << " Changed B to long jump " << *MI);
1607 /// fixupConditionalBr - Fix up a conditional branch whose destination is too
1608 /// far away to fit in its displacement field. It is converted to an inverse
1609 /// conditional branch + an unconditional branch to the destination.
1611 MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) {
1613 MachineInstr *MI = Br.MI;
1614 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1616 // Add an unconditional branch to the destination and invert the branch
1617 // condition to jump over it:
1623 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
1624 CC = ARMCC::getOppositeCondition(CC);
1625 unsigned CCReg = MI->getOperand(2).getReg();
1627 // If the branch is at the end of its MBB and that has a fall-through block,
1628 // direct the updated conditional branch to the fall-through block. Otherwise,
1629 // split the MBB before the next instruction.
1630 MachineBasicBlock *MBB = MI->getParent();
1631 MachineInstr *BMI = &MBB->back();
1632 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
1636 if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
1637 BMI->getOpcode() == Br.UncondBr) {
1638 // Last MI in the BB is an unconditional branch. Can we simply invert the
1639 // condition and swap destinations:
1645 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
1646 if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
1647 DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
1649 BMI->getOperand(0).setMBB(DestBB);
1650 MI->getOperand(0).setMBB(NewDest);
1651 MI->getOperand(1).setImm(CC);
1658 splitBlockBeforeInstr(MI);
1659 // No need for the branch to the next block. We're adding an unconditional
1660 // branch to the destination.
1661 int delta = TII->GetInstSizeInBytes(&MBB->back());
1662 BBInfo[MBB->getNumber()].Size -= delta;
1663 MBB->back().eraseFromParent();
1664 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1666 MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB));
1668 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
1669 << " also invert condition and change dest. to BB#"
1670 << NextBB->getNumber() << "\n");
1672 // Insert a new conditional branch and a new unconditional branch.
1673 // Also update the ImmBranch as well as adding a new entry for the new branch.
1674 BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
1675 .addMBB(NextBB).addImm(CC).addReg(CCReg);
1676 Br.MI = &MBB->back();
1677 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1679 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
1680 .addImm(ARMCC::AL).addReg(0);
1682 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
1683 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1684 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
1685 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
1687 // Remove the old conditional branch. It may or may not still be in MBB.
1688 BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
1689 MI->eraseFromParent();
1690 adjustBBOffsetsAfter(MBB);
1696 void MipsConstantIslands::prescanForConstants() {
1698 PrescannedForConstants = true;
1699 for (MachineFunction::iterator B =
1700 MF->begin(), E = MF->end(); B != E; ++B) {
1701 for (MachineBasicBlock::instr_iterator I =
1702 B->instr_begin(), EB = B->instr_end(); I != EB; ++I) {
1703 switch(I->getDesc().getOpcode()) {
1704 case Mips::LwConstant32: {
1705 DEBUG(dbgs() << "constant island constant " << *I << "\n");
1706 J = I->getNumOperands();
1707 DEBUG(dbgs() << "num operands " << J << "\n");
1708 MachineOperand& Literal = I->getOperand(1);
1709 if (Literal.isImm()) {
1710 int64_t V = Literal.getImm();
1711 DEBUG(dbgs() << "literal " << V << "\n");
1713 Type::getInt32Ty(MF->getFunction()->getContext());
1714 const Constant *C = ConstantInt::get(Int32Ty, V);
1715 unsigned index = MCP->getConstantPoolIndex(C, 4);
1716 I->getOperand(2).ChangeToImmediate(index);
1717 DEBUG(dbgs() << "constant island constant " << *I << "\n");
1718 I->setDesc(TII->get(Mips::LwRxPcTcp16));
1719 I->RemoveOperand(1);
1720 I->RemoveOperand(1);
1721 I->addOperand(MachineOperand::CreateCPI(index, 0));
1722 I->addOperand(MachineOperand::CreateImm(4));