1 //===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that splits the constant pool up into 'islands'
11 // which are scattered through-out the function. This is required due to the
12 // limited pc-relative displacements that ARM has.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "arm-cp-islands"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMInstrInfo.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/Statistic.h"
32 STATISTIC(NumCPEs, "Number of constpool entries");
33 STATISTIC(NumSplit, "Number of uncond branches inserted");
34 STATISTIC(NumCBrFixed, "Number of cond branches fixed");
35 STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
38 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
39 /// requires constant pool entries to be scattered among the instructions
40 /// inside a function. To do this, it completely ignores the normal LLVM
41 /// constant pool; instead, it places constants wherever it feels like with
42 /// special instructions.
44 /// The terminology used in this pass includes:
45 /// Islands - Clumps of constants placed in the function.
46 /// Water - Potential places where an island could be formed.
47 /// CPE - A constant pool entry that has been placed somewhere, which
48 /// tracks a list of users.
49 class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
50 /// NextUID - Assign unique ID's to CPE's.
53 /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
55 std::vector<unsigned> BBSizes;
57 /// WaterList - A sorted list of basic blocks where islands could be placed
58 /// (i.e. blocks that don't fall through to the following block, due
59 /// to a return, unreachable, or unconditional branch).
60 std::vector<MachineBasicBlock*> WaterList;
62 // WaterListOffsets - the offset of the beginning of each WaterList block.
63 // This is computed as needed in HandleConstantPoolUser; not necessarily
64 // valid at arbitrary times.
65 std::vector<unsigned> WaterListOffsets;
67 /// CPUser - One user of a constant pool, keeping the machine instruction
68 /// pointer, the constant pool being referenced, and the max displacement
69 /// allowed from the instruction to the CP.
74 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp)
75 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp) {}
78 /// CPUsers - Keep track of all of the machine instructions that use various
79 /// constant pools and their max displacement.
80 std::vector<CPUser> CPUsers;
82 /// CPEntry - One per constant pool entry, keeping the machine instruction
83 /// pointer, the constpool index, and the number of CPUser's which
84 /// reference this entry.
89 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
90 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
93 /// CPEntries - Keep track of all of the constant pool entry machine
94 /// instructions. For each original constpool index (i.e. those that
95 /// existed upon entry to this pass), it keeps a vector of entries.
96 /// Original elements are cloned as we go along; the clones are
97 /// put in the vector of the original element, but have distinct CPIs.
98 std::vector<std::vector<CPEntry> > CPEntries;
100 /// ImmBranch - One per immediate branch, keeping the machine instruction
101 /// pointer, conditional or unconditional, the max displacement,
102 /// and (if isCond is true) the corresponding unconditional branch
106 unsigned MaxDisp : 31;
109 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
110 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
113 /// Branches - Keep track of all the immediate branch instructions.
115 std::vector<ImmBranch> ImmBranches;
117 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
119 SmallVector<MachineInstr*, 4> PushPopMIs;
121 /// HasFarJump - True if any far jump instruction has been emitted during
122 /// the branch fix up pass.
125 const TargetInstrInfo *TII;
126 const ARMFunctionInfo *AFI;
128 virtual bool runOnMachineFunction(MachineFunction &Fn);
130 virtual const char *getPassName() const {
131 return "ARM constant island placement and branch shortening pass";
135 void DoInitialPlacement(MachineFunction &Fn,
136 std::vector<MachineInstr*> &CPEMIs);
137 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
138 void InitialFunctionScan(MachineFunction &Fn,
139 const std::vector<MachineInstr*> &CPEMIs);
140 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
141 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
142 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI, unsigned Size);
143 void ComputeWaterListOffsets(MachineFunction &Fn);
144 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
145 bool HandleConstantPoolUser(MachineFunction &Fn, CPUser &U);
146 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
147 MachineInstr *CPEMI, unsigned Disp,
149 bool WaterIsInRange(unsigned UserOffset,
150 std::vector<MachineBasicBlock*>::iterator IP,
152 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
153 bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br);
154 bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br);
155 bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br);
156 bool UndoLRSpillRestore();
158 unsigned GetOffsetOf(MachineInstr *MI) const;
159 unsigned GetOffsetOf(MachineBasicBlock *MBB) const;
163 /// createARMConstantIslandPass - returns an instance of the constpool
165 FunctionPass *llvm::createARMConstantIslandPass() {
166 return new ARMConstantIslands();
169 bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) {
170 MachineConstantPool &MCP = *Fn.getConstantPool();
172 TII = Fn.getTarget().getInstrInfo();
173 AFI = Fn.getInfo<ARMFunctionInfo>();
177 // Renumber all of the machine basic blocks in the function, guaranteeing that
178 // the numbers agree with the position of the block in the function.
181 // Perform the initial placement of the constant pool entries. To start with,
182 // we put them all at the end of the function.
183 std::vector<MachineInstr*> CPEMIs;
185 DoInitialPlacement(Fn, CPEMIs);
187 /// The next UID to take is the first unused one.
188 NextUID = CPEMIs.size();
190 // Do the initial scan of the function, building up information about the
191 // sizes of each block, the location of all the water, and finding all of the
192 // constant pool users.
193 InitialFunctionScan(Fn, CPEMIs);
196 // Iteratively place constant pool entries and fix up branches until there
198 bool MadeChange = false;
201 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
202 Change |= HandleConstantPoolUser(Fn, CPUsers[i]);
203 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
204 Change |= FixUpImmediateBr(Fn, ImmBranches[i]);
210 // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
211 // Undo the spill / restore of LR if possible.
212 if (!HasFarJump && AFI->isLRForceSpilled() && AFI->isThumbFunction())
213 MadeChange |= UndoLRSpillRestore();
225 /// DoInitialPlacement - Perform the initial placement of the constant pool
226 /// entries. To start with, we put them all at the end of the function.
227 void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn,
228 std::vector<MachineInstr*> &CPEMIs){
229 // Create the basic block to hold the CPE's.
230 MachineBasicBlock *BB = new MachineBasicBlock();
231 Fn.getBasicBlockList().push_back(BB);
233 // Add all of the constants from the constant pool to the end block, use an
234 // identity mapping of CPI's to CPE's.
235 const std::vector<MachineConstantPoolEntry> &CPs =
236 Fn.getConstantPool()->getConstants();
238 const TargetData &TD = *Fn.getTarget().getTargetData();
239 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
240 unsigned Size = TD.getTypeSize(CPs[i].getType());
241 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
242 // we would have to pad them out or something so that instructions stay
244 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
245 MachineInstr *CPEMI =
246 BuildMI(BB, TII->get(ARM::CONSTPOOL_ENTRY))
247 .addImm(i).addConstantPoolIndex(i).addImm(Size);
248 CPEMIs.push_back(CPEMI);
250 // Add a new CPEntry, but no corresponding CPUser yet.
251 std::vector<CPEntry> CPEs;
252 CPEs.push_back(CPEntry(CPEMI, i));
253 CPEntries.push_back(CPEs);
255 DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n";
259 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
260 /// into the block immediately after it.
261 static bool BBHasFallthrough(MachineBasicBlock *MBB) {
262 // Get the next machine basic block in the function.
263 MachineFunction::iterator MBBI = MBB;
264 if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function.
267 MachineBasicBlock *NextBB = next(MBBI);
268 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
269 E = MBB->succ_end(); I != E; ++I)
276 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
277 /// look up the corresponding CPEntry.
278 ARMConstantIslands::CPEntry
279 *ARMConstantIslands::findConstPoolEntry(unsigned CPI,
280 const MachineInstr *CPEMI) {
281 std::vector<CPEntry> &CPEs = CPEntries[CPI];
282 // Number of entries per constpool index should be small, just do a
284 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
285 if (CPEs[i].CPEMI == CPEMI)
291 /// InitialFunctionScan - Do the initial scan of the function, building up
292 /// information about the sizes of each block, the location of all the water,
293 /// and finding all of the constant pool users.
294 void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn,
295 const std::vector<MachineInstr*> &CPEMIs) {
296 for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
298 MachineBasicBlock &MBB = *MBBI;
300 // If this block doesn't fall through into the next MBB, then this is
301 // 'water' that a constant pool island could be placed.
302 if (!BBHasFallthrough(&MBB))
303 WaterList.push_back(&MBB);
305 unsigned MBBSize = 0;
306 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
308 // Add instruction size to MBBSize.
309 MBBSize += ARM::GetInstSize(I);
311 int Opc = I->getOpcode();
312 if (TII->isBranch(Opc)) {
319 continue; // Ignore JT branches
340 // Record this immediate branch.
341 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
342 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
345 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
346 PushPopMIs.push_back(I);
348 // Scan the instructions for constant pool operands.
349 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
350 if (I->getOperand(op).isConstantPoolIndex()) {
351 // We found one. The addressing mode tells us the max displacement
352 // from the PC that this instruction permits.
354 // Basic size info comes from the TSFlags field.
357 unsigned TSFlags = I->getInstrDescriptor()->TSFlags;
358 switch (TSFlags & ARMII::AddrModeMask) {
360 // Constant pool entries can reach anything.
361 if (I->getOpcode() == ARM::CONSTPOOL_ENTRY)
363 assert(0 && "Unknown addressing mode for CP reference!");
364 case ARMII::AddrMode1: // AM1: 8 bits << 2
366 Scale = 4; // Taking the address of a CP entry.
368 case ARMII::AddrMode2:
369 Bits = 12; // +-offset_12
371 case ARMII::AddrMode3:
372 Bits = 8; // +-offset_8
374 // addrmode4 has no immediate offset.
375 case ARMII::AddrMode5:
377 Scale = 4; // +-(offset_8*4)
379 case ARMII::AddrModeT1:
380 Bits = 5; // +offset_5
382 case ARMII::AddrModeT2:
384 Scale = 2; // +(offset_5*2)
386 case ARMII::AddrModeT4:
388 Scale = 4; // +(offset_5*4)
390 case ARMII::AddrModeTs:
392 Scale = 4; // +(offset_8*4)
396 // Remember that this is a user of a CP entry.
397 unsigned CPI = I->getOperand(op).getConstantPoolIndex();
398 MachineInstr *CPEMI = CPEMIs[CPI];
399 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
400 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs));
402 // Increment corresponding CPEntry reference count.
403 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
404 assert(CPE && "Cannot find a corresponding CPEntry!");
407 // Instructions can only use one CP entry, don't bother scanning the
408 // rest of the operands.
413 // In thumb mode, if this block is a constpool island, pessimistically
414 // assume it needs to be padded by two byte so it's aligned on 4 byte
416 if (AFI->isThumbFunction() &&
418 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY)
421 BBSizes.push_back(MBBSize);
425 /// GetOffsetOf - Return the current offset of the specified machine instruction
426 /// from the start of the function. This offset changes as stuff is moved
427 /// around inside the function.
428 unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
429 MachineBasicBlock *MBB = MI->getParent();
431 // The offset is composed of two things: the sum of the sizes of all MBB's
432 // before this instruction's block, and the offset from the start of the block
436 // Sum block sizes before MBB.
437 for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
438 Offset += BBSizes[BB];
440 // Sum instructions before MI in MBB.
441 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
442 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
443 if (&*I == MI) return Offset;
444 Offset += ARM::GetInstSize(I);
448 /// GetOffsetOf - Return the current offset of the specified machine BB
449 /// from the start of the function. This offset changes as stuff is moved
450 /// around inside the function.
451 unsigned ARMConstantIslands::GetOffsetOf(MachineBasicBlock *MBB) const {
452 // Sum block sizes before MBB.
454 for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
455 Offset += BBSizes[BB];
460 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
462 static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
463 const MachineBasicBlock *RHS) {
464 return LHS->getNumber() < RHS->getNumber();
467 /// UpdateForInsertedWaterBlock - When a block is newly inserted into the
468 /// machine function, it upsets all of the block numbers. Renumber the blocks
469 /// and update the arrays that parallel this numbering.
470 void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
471 // Renumber the MBB's to keep them consequtive.
472 NewBB->getParent()->RenumberBlocks(NewBB);
474 // Insert a size into BBSizes to align it properly with the (newly
475 // renumbered) block numbers.
476 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
478 // Next, update WaterList. Specifically, we need to add NewMBB as having
479 // available water after it.
480 std::vector<MachineBasicBlock*>::iterator IP =
481 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
483 WaterList.insert(IP, NewBB);
487 /// Split the basic block containing MI into two blocks, which are joined by
488 /// an unconditional branch. Update datastructures and renumber blocks to
489 /// account for this change and returns the newly created block.
490 MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
491 MachineBasicBlock *OrigBB = MI->getParent();
492 bool isThumb = AFI->isThumbFunction();
494 // Create a new MBB for the code after the OrigBB.
495 MachineBasicBlock *NewBB = new MachineBasicBlock(OrigBB->getBasicBlock());
496 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
497 OrigBB->getParent()->getBasicBlockList().insert(MBBI, NewBB);
499 // Splice the instructions starting with MI over to NewBB.
500 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
502 // Add an unconditional branch from OrigBB to NewBB.
503 // Note the new unconditional branch is not being recorded.
504 BuildMI(OrigBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewBB);
507 // Update the CFG. All succs of OrigBB are now succs of NewBB.
508 while (!OrigBB->succ_empty()) {
509 MachineBasicBlock *Succ = *OrigBB->succ_begin();
510 OrigBB->removeSuccessor(Succ);
511 NewBB->addSuccessor(Succ);
513 // This pass should be run after register allocation, so there should be no
514 // PHI nodes to update.
515 assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
516 && "PHI nodes should be eliminated by now!");
519 // OrigBB branches to NewBB.
520 OrigBB->addSuccessor(NewBB);
522 // Update internal data structures to account for the newly inserted MBB.
523 // This is almost the same as UpdateForInsertedWaterBlock, except that
524 // the Water goes after OrigBB, not NewBB.
525 NewBB->getParent()->RenumberBlocks(NewBB);
527 // Insert a size into BBSizes to align it properly with the (newly
528 // renumbered) block numbers.
529 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
531 // Next, update WaterList. Specifically, we need to add OrigMBB as having
532 // available water after it (but not if it's already there, which happens
533 // when splitting before a conditional branch that is followed by an
534 // unconditional branch - in that case we want to insert NewBB).
535 std::vector<MachineBasicBlock*>::iterator IP =
536 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
538 MachineBasicBlock* WaterBB = *IP;
539 if (WaterBB == OrigBB)
540 WaterList.insert(next(IP), NewBB);
542 WaterList.insert(IP, OrigBB);
544 // Figure out how large the first NewMBB is.
545 unsigned NewBBSize = 0;
546 for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
548 NewBBSize += ARM::GetInstSize(I);
550 // Set the size of NewBB in BBSizes.
551 BBSizes[NewBB->getNumber()] = NewBBSize;
553 // We removed instructions from UserMBB, subtract that off from its size.
554 // Add 2 or 4 to the block to count the unconditional branch we added to it.
555 BBSizes[OrigBB->getNumber()] -= NewBBSize - (isThumb ? 2 : 4);
560 /// WaterIsInRange - Returns true if a CPE placed after the specified
561 /// Water (a basic block) will be in range for the specific MI.
563 bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
564 std::vector<MachineBasicBlock*>::iterator IP,
567 MachineBasicBlock *Water = *IP;
568 unsigned Index = IP - WaterList.begin();
569 unsigned CPEOffset = WaterListOffsets[Index] +
570 BBSizes[Water->getNumber()];
571 // If the Water is a constpool island, it has already been aligned.
573 if (AFI->isThumbFunction() &&
575 Water->begin()->getOpcode() != ARM::CONSTPOOL_ENTRY))
578 if (UserOffset <= CPEOffset) {
579 // User before the CPE.
580 if (CPEOffset-UserOffset <= MaxDisp)
582 } else if (!AFI->isThumbFunction()) {
583 // Thumb LDR cannot encode negative offset.
584 if (UserOffset-CPEOffset <= MaxDisp)
590 /// CPEIsInRange - Returns true if the distance between specific MI and
591 /// specific ConstPool entry instruction can fit in MI's displacement field.
592 bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
594 unsigned MaxDisp, bool DoDump) {
595 // In thumb mode, pessimistically assumes the .align 2 before the first CPE
596 // in the island adds two byte padding.
597 unsigned AlignAdj = AFI->isThumbFunction() ? 2 : 0;
598 unsigned CPEOffset = GetOffsetOf(CPEMI) + AlignAdj;
601 DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm()
602 << " max delta=" << MaxDisp
603 << " insn address=" << UserOffset
604 << " CPE address=" << CPEOffset
605 << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI;
608 if (UserOffset <= CPEOffset) {
609 // User before the CPE.
610 if (CPEOffset-UserOffset <= MaxDisp)
612 } else if (!AFI->isThumbFunction()) {
613 // Thumb LDR cannot encode negative offset.
614 if (UserOffset-CPEOffset <= MaxDisp)
620 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
621 /// unconditionally branches to its only successor.
622 static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
623 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
626 MachineBasicBlock *Succ = *MBB->succ_begin();
627 MachineBasicBlock *Pred = *MBB->pred_begin();
628 MachineInstr *PredMI = &Pred->back();
629 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB)
630 return PredMI->getOperand(0).getMBB() == Succ;
634 /// DecrementOldEntry - find the constant pool entry with index CPI
635 /// and instruction CPEMI, and decrement its refcount. If the refcount
636 /// becomes 0 remove the entry and instruction. Returns true if we removed
637 /// the entry, false if we didn't.
639 bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI,
641 // Find the old entry. Eliminate it if it is no longer used.
642 CPEntry *OldCPE = findConstPoolEntry(CPI, CPEMI);
643 assert(OldCPE && "Unexpected!");
644 if (--OldCPE->RefCount == 0) {
645 MachineBasicBlock *OldCPEBB = OldCPE->CPEMI->getParent();
646 if (OldCPEBB->empty()) {
647 // In thumb mode, the size of island is padded by two to compensate for
648 // the alignment requirement. Thus it will now be 2 when the block is
649 // empty, so fix this.
650 BBSizes[OldCPEBB->getNumber()] = 0;
651 // An island has only one predecessor BB and one successor BB. Check if
652 // this BB's predecessor jumps directly to this BB's successor. This
653 // shouldn't happen currently.
654 assert(!BBIsJumpedOver(OldCPEBB) && "How did this happen?");
655 // FIXME: remove the empty blocks after all the work is done?
657 BBSizes[OldCPEBB->getNumber()] -= Size;
658 OldCPE->CPEMI->eraseFromParent();
659 OldCPE->CPEMI = NULL;
666 /// ComputeWaterListOffsets - just what you think.
667 /// This vector is built to avoid re-adding BBSizes for each WaterBB under test
668 /// (which would cause the algorithm to be n^2).
669 void ARMConstantIslands::ComputeWaterListOffsets(MachineFunction &Fn) {
670 unsigned WaterListIndex = 0;
673 WaterListOffsets.clear();
674 for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
675 MBBI != E; ++BB, ++MBBI) {
676 MachineBasicBlock *MBB = MBBI;
677 if (MBB == WaterList[WaterListIndex]) {
678 WaterListOffsets.push_back(Offset);
681 Offset += BBSizes[BB];
685 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
686 /// if not, see if an in-range clone of the CPE is in range, and if so,
687 /// change the data structures so the user references the clone. Returns:
688 /// 0 = no existing entry found
689 /// 1 = entry found, and there were no code insertions or deletions
690 /// 2 = entry found, and there were code insertions or deletions
691 int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
693 MachineInstr *UserMI = U.MI;
694 MachineInstr *CPEMI = U.CPEMI;
696 // Check to see if the CPE is already in-range.
697 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, true)) {
698 DOUT << "In range\n";
702 // No. Look for previously created clones of the CPE that are in range.
703 unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex();
704 std::vector<CPEntry> &CPEs = CPEntries[CPI];
705 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
706 // We already tried this one
707 if (CPEs[i].CPEMI == CPEMI)
709 // Removing CPEs can leave empty entries, skip
710 if (CPEs[i].CPEMI == NULL)
712 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, false)) {
713 DOUT << "Replacing CPE#" << CPI << " with CPE#" << CPEs[i].CPI << "\n";
714 // Point the CPUser node to the replacement
715 U.CPEMI = CPEs[i].CPEMI;
716 // Change the CPI in the instruction operand to refer to the clone.
717 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
718 if (UserMI->getOperand(j).isConstantPoolIndex()) {
719 UserMI->getOperand(j).setConstantPoolIndex(CPEs[i].CPI);
722 // Adjust the refcount of the clone...
724 // ...and the original. If we didn't remove the old entry, none of the
725 // addresses changed, so we don't need another pass.
726 unsigned Size = CPEMI->getOperand(2).getImm();
727 return DecrementOldEntry(CPI, CPEMI, Size) ? 2 : 1;
733 /// HandleConstantPoolUser - Analyze the specified user, checking to see if it
734 /// is out-of-range. If so, pick it up the constant pool value and move it some
735 /// place in-range. Return true if we changed any addresses (thus must run
736 /// another pass of branch lengthening), false otherwise.
737 bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn, CPUser &U){
738 MachineInstr *UserMI = U.MI;
739 MachineInstr *CPEMI = U.CPEMI;
740 unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex();
741 unsigned Size = CPEMI->getOperand(2).getImm();
742 bool isThumb = AFI->isThumbFunction();
743 MachineBasicBlock *NewMBB;
744 // Compute this only once, it's expensive
745 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
747 // See if the current entry is within range, or there is a clone of it
749 int result = LookForExistingCPEntry(U, UserOffset);
750 if (result==1) return false;
751 else if (result==2) return true;
753 // No existing clone of this CPE is within range.
754 // We will be generating a new clone. Get a UID for it.
755 unsigned ID = NextUID++;
757 // Look for water where we can place this CPE. We look for the farthest one
758 // away that will work. Forward references only for now (although later
759 // we might find some that are backwards).
760 bool WaterFound = false;
761 if (!WaterList.empty()) {
762 // Compute offsets for the blocks in the current WaterList.
763 // It is a big compile-time speed win to do this only once
764 // rather than for each WaterList entry.
765 ComputeWaterListOffsets(Fn);
766 assert(WaterList.size() == WaterListOffsets.size());
767 for (std::vector<MachineBasicBlock*>::iterator IP = prior(WaterList.end()),
768 B = WaterList.begin();; --IP) {
769 MachineBasicBlock* WaterBB = *IP;
770 if (WaterIsInRange(UserOffset, IP, U.MaxDisp)) {
772 DOUT << "found water in range\n";
773 // CPE goes before following block (NewMBB).
774 NewMBB = next(MachineFunction::iterator(WaterBB));
775 // Remove the original WaterList entry; we want subsequent
776 // insertions in this vicinity to go after the one we're
777 // about to insert. This considerably reduces the number
778 // of times we have to move the same CPE more than once.
789 // Solution of last resort: split the user's MBB right after the user
790 // and insert a clone of the CPE into the newly created water.
792 DOUT << "No water found\n";
793 MachineBasicBlock *UserMBB = UserMI->getParent();
795 // TODO: Search for the best place to split the code. In practice, using
796 // loop nesting information to insert these guys outside of loops would be
798 if (&UserMBB->back() == UserMI) {
799 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
800 NewMBB = next(MachineFunction::iterator(UserMBB));
801 // Add an unconditional branch from UserMBB to fallthrough block.
802 // Note the new unconditional branch is not being recorded.
803 BuildMI(UserMBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewMBB);
804 BBSizes[UserMBB->getNumber()] += isThumb ? 2 : 4;
806 MachineInstr *NextMI = next(MachineBasicBlock::iterator(UserMI));
807 NewMBB = SplitBlockBeforeInstr(NextMI);
811 // Okay, we know we can put an island before NewMBB now, do it!
812 MachineBasicBlock *NewIsland = new MachineBasicBlock();
813 Fn.getBasicBlockList().insert(NewMBB, NewIsland);
815 // Update internal data structures to account for the newly inserted MBB.
816 UpdateForInsertedWaterBlock(NewIsland);
818 // Decrement the old entry, and remove it if refcount becomes 0.
819 DecrementOldEntry(CPI, CPEMI, Size);
821 // Now that we have an island to add the CPE to, clone the original CPE and
822 // add it to the island.
823 U.CPEMI = BuildMI(NewIsland, TII->get(ARM::CONSTPOOL_ENTRY))
824 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
825 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
828 // Compensate for .align 2 in thumb mode.
829 if (isThumb) Size += 2;
830 // Increase the size of the island block to account for the new entry.
831 BBSizes[NewIsland->getNumber()] += Size;
833 // Finally, change the CPI in the instruction operand to be ID.
834 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
835 if (UserMI->getOperand(i).isConstantPoolIndex()) {
836 UserMI->getOperand(i).setConstantPoolIndex(ID);
840 DOUT << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI;
845 /// BBIsInRange - Returns true if the distance between specific MI and
846 /// specific BB can fit in MI's displacement field.
847 bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
849 unsigned PCAdj = AFI->isThumbFunction() ? 4 : 8;
850 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
851 unsigned DestOffset = GetOffsetOf(DestBB);
853 DOUT << "Branch of destination BB#" << DestBB->getNumber()
854 << " from BB#" << MI->getParent()->getNumber()
855 << " max delta=" << MaxDisp
856 << " at offset " << int(DestOffset-BrOffset) << "\t" << *MI;
858 if (BrOffset <= DestOffset) {
859 if (DestOffset - BrOffset <= MaxDisp)
862 if (BrOffset - DestOffset <= MaxDisp)
868 /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
869 /// away to fit in its displacement field.
870 bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) {
871 MachineInstr *MI = Br.MI;
872 MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock();
874 // Check to see if the DestBB is already in-range.
875 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
879 return FixUpUnconditionalBr(Fn, Br);
880 return FixUpConditionalBr(Fn, Br);
883 /// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
884 /// too far away to fit in its displacement field. If the LR register has been
885 /// spilled in the epilogue, then we can use BL to implement a far jump.
886 /// Otherwise, add an intermediate branch instruction to to a branch.
888 ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) {
889 MachineInstr *MI = Br.MI;
890 MachineBasicBlock *MBB = MI->getParent();
891 assert(AFI->isThumbFunction() && "Expected a Thumb function!");
893 // Use BL to implement far jump.
894 Br.MaxDisp = (1 << 21) * 2;
895 MI->setInstrDescriptor(TII->get(ARM::tBfar));
896 BBSizes[MBB->getNumber()] += 2;
900 DOUT << " Changed B to long jump " << *MI;
905 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
906 /// the specific unconditional branch instruction.
907 static inline unsigned getUnconditionalBrDisp(int Opc) {
908 return (Opc == ARM::tB) ? (1<<10)*2 : (1<<23)*4;
911 /// FixUpConditionalBr - Fix up a conditional branch whose destination is too
912 /// far away to fit in its displacement field. It is converted to an inverse
913 /// conditional branch + an unconditional branch to the destination.
915 ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) {
916 MachineInstr *MI = Br.MI;
917 MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock();
919 // Add a unconditional branch to the destination and invert the branch
920 // condition to jump over it:
926 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImmedValue();
927 CC = ARMCC::getOppositeCondition(CC);
929 // If the branch is at the end of its MBB and that has a fall-through block,
930 // direct the updated conditional branch to the fall-through block. Otherwise,
931 // split the MBB before the next instruction.
932 MachineBasicBlock *MBB = MI->getParent();
933 MachineInstr *BMI = &MBB->back();
934 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
938 if (next(MachineBasicBlock::iterator(MI)) == MBB->back() &&
939 BMI->getOpcode() == Br.UncondBr) {
940 // Last MI in the BB is a unconditional branch. Can we simply invert the
941 // condition and swap destinations:
947 MachineBasicBlock *NewDest = BMI->getOperand(0).getMachineBasicBlock();
948 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
949 DOUT << " Invert Bcc condition and swap its destination with " << *BMI;
950 BMI->getOperand(0).setMachineBasicBlock(DestBB);
951 MI->getOperand(0).setMachineBasicBlock(NewDest);
952 MI->getOperand(1).setImm(CC);
959 SplitBlockBeforeInstr(MI);
960 // No need for the branch to the next block. We're adding a unconditional
961 // branch to the destination.
962 MBB->back().eraseFromParent();
964 MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
966 DOUT << " Insert B to BB#" << DestBB->getNumber()
967 << " also invert condition and change dest. to BB#"
968 << NextBB->getNumber() << "\n";
970 // Insert a unconditional branch and replace the conditional branch.
971 // Also update the ImmBranch as well as adding a new entry for the new branch.
972 BuildMI(MBB, TII->get(MI->getOpcode())).addMBB(NextBB).addImm(CC);
973 Br.MI = &MBB->back();
974 BuildMI(MBB, TII->get(Br.UncondBr)).addMBB(DestBB);
975 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
976 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
977 MI->eraseFromParent();
979 // Increase the size of MBB to account for the new unconditional branch.
980 BBSizes[MBB->getNumber()] += ARM::GetInstSize(&MBB->back());
984 /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
985 /// LR / restores LR to pc.
986 bool ARMConstantIslands::UndoLRSpillRestore() {
987 bool MadeChange = false;
988 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
989 MachineInstr *MI = PushPopMIs[i];
990 if (MI->getNumOperands() == 1) {
991 if (MI->getOpcode() == ARM::tPOP_RET &&
992 MI->getOperand(0).getReg() == ARM::PC)
993 BuildMI(MI->getParent(), TII->get(ARM::tBX_RET));
994 MI->eraseFromParent();