1 //===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
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 "ARMAddressingModes.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMInstrInfo.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/Statistic.h"
35 STATISTIC(NumCPEs
, "Number of constpool entries");
36 STATISTIC(NumSplit
, "Number of uncond branches inserted");
37 STATISTIC(NumCBrFixed
, "Number of cond branches fixed");
38 STATISTIC(NumUBrFixed
, "Number of uncond branches fixed");
39 STATISTIC(NumTBs
, "Number of table branches generated");
42 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
43 /// requires constant pool entries to be scattered among the instructions
44 /// inside a function. To do this, it completely ignores the normal LLVM
45 /// constant pool; instead, it places constants wherever it feels like with
46 /// special instructions.
48 /// The terminology used in this pass includes:
49 /// Islands - Clumps of constants placed in the function.
50 /// Water - Potential places where an island could be formed.
51 /// CPE - A constant pool entry that has been placed somewhere, which
52 /// tracks a list of users.
53 class VISIBILITY_HIDDEN ARMConstantIslands
: public MachineFunctionPass
{
54 /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
55 /// by MBB Number. The two-byte pads required for Thumb alignment are
56 /// counted as part of the following block (i.e., the offset and size for
57 /// a padded block will both be ==2 mod 4).
58 std::vector
<unsigned> BBSizes
;
60 /// BBOffsets - the offset of each MBB in bytes, starting from 0.
61 /// The two-byte pads required for Thumb alignment are counted as part of
62 /// the following block.
63 std::vector
<unsigned> BBOffsets
;
65 /// WaterList - A sorted list of basic blocks where islands could be placed
66 /// (i.e. blocks that don't fall through to the following block, due
67 /// to a return, unreachable, or unconditional branch).
68 std::vector
<MachineBasicBlock
*> WaterList
;
70 /// CPUser - One user of a constant pool, keeping the machine instruction
71 /// pointer, the constant pool being referenced, and the max displacement
72 /// allowed from the instruction to the CP.
79 CPUser(MachineInstr
*mi
, MachineInstr
*cpemi
, unsigned maxdisp
,
81 : MI(mi
), CPEMI(cpemi
), MaxDisp(maxdisp
), NegOk(neg
), IsSoImm(soimm
) {}
84 /// CPUsers - Keep track of all of the machine instructions that use various
85 /// constant pools and their max displacement.
86 std::vector
<CPUser
> CPUsers
;
88 /// CPEntry - One per constant pool entry, keeping the machine instruction
89 /// pointer, the constpool index, and the number of CPUser's which
90 /// reference this entry.
95 CPEntry(MachineInstr
*cpemi
, unsigned cpi
, unsigned rc
= 0)
96 : CPEMI(cpemi
), CPI(cpi
), RefCount(rc
) {}
99 /// CPEntries - Keep track of all of the constant pool entry machine
100 /// instructions. For each original constpool index (i.e. those that
101 /// existed upon entry to this pass), it keeps a vector of entries.
102 /// Original elements are cloned as we go along; the clones are
103 /// put in the vector of the original element, but have distinct CPIs.
104 std::vector
<std::vector
<CPEntry
> > CPEntries
;
106 /// ImmBranch - One per immediate branch, keeping the machine instruction
107 /// pointer, conditional or unconditional, the max displacement,
108 /// and (if isCond is true) the corresponding unconditional branch
112 unsigned MaxDisp
: 31;
115 ImmBranch(MachineInstr
*mi
, unsigned maxdisp
, bool cond
, int ubr
)
116 : MI(mi
), MaxDisp(maxdisp
), isCond(cond
), UncondBr(ubr
) {}
119 /// ImmBranches - Keep track of all the immediate branch instructions.
121 std::vector
<ImmBranch
> ImmBranches
;
123 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
125 SmallVector
<MachineInstr
*, 4> PushPopMIs
;
127 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
128 SmallVector
<MachineInstr
*, 4> T2JumpTables
;
130 /// HasFarJump - True if any far jump instruction has been emitted during
131 /// the branch fix up pass.
134 const TargetInstrInfo
*TII
;
135 const ARMSubtarget
*STI
;
136 ARMFunctionInfo
*AFI
;
142 ARMConstantIslands() : MachineFunctionPass(&ID
) {}
144 virtual bool runOnMachineFunction(MachineFunction
&MF
);
146 virtual const char *getPassName() const {
147 return "ARM constant island placement and branch shortening pass";
151 void DoInitialPlacement(MachineFunction
&MF
,
152 std::vector
<MachineInstr
*> &CPEMIs
);
153 CPEntry
*findConstPoolEntry(unsigned CPI
, const MachineInstr
*CPEMI
);
154 void InitialFunctionScan(MachineFunction
&MF
,
155 const std::vector
<MachineInstr
*> &CPEMIs
);
156 MachineBasicBlock
*SplitBlockBeforeInstr(MachineInstr
*MI
);
157 void UpdateForInsertedWaterBlock(MachineBasicBlock
*NewBB
);
158 void AdjustBBOffsetsAfter(MachineBasicBlock
*BB
, int delta
);
159 bool DecrementOldEntry(unsigned CPI
, MachineInstr
* CPEMI
);
160 int LookForExistingCPEntry(CPUser
& U
, unsigned UserOffset
);
161 bool LookForWater(CPUser
&U
, unsigned UserOffset
,
162 MachineBasicBlock
** NewMBB
);
163 MachineBasicBlock
* AcceptWater(MachineBasicBlock
*WaterBB
,
164 std::vector
<MachineBasicBlock
*>::iterator IP
);
165 void CreateNewWater(unsigned CPUserIndex
, unsigned UserOffset
,
166 MachineBasicBlock
** NewMBB
);
167 bool HandleConstantPoolUser(MachineFunction
&MF
, unsigned CPUserIndex
);
168 void RemoveDeadCPEMI(MachineInstr
*CPEMI
);
169 bool RemoveUnusedCPEntries();
170 bool CPEIsInRange(MachineInstr
*MI
, unsigned UserOffset
,
171 MachineInstr
*CPEMI
, unsigned Disp
, bool NegOk
,
172 bool DoDump
= false);
173 bool WaterIsInRange(unsigned UserOffset
, MachineBasicBlock
*Water
,
175 bool OffsetIsInRange(unsigned UserOffset
, unsigned TrialOffset
,
176 unsigned Disp
, bool NegativeOK
, bool IsSoImm
= false);
177 bool BBIsInRange(MachineInstr
*MI
, MachineBasicBlock
*BB
, unsigned Disp
);
178 bool FixUpImmediateBr(MachineFunction
&MF
, ImmBranch
&Br
);
179 bool FixUpConditionalBr(MachineFunction
&MF
, ImmBranch
&Br
);
180 bool FixUpUnconditionalBr(MachineFunction
&MF
, ImmBranch
&Br
);
181 bool UndoLRSpillRestore();
182 bool OptimizeThumb2JumpTables(MachineFunction
&MF
);
184 unsigned GetOffsetOf(MachineInstr
*MI
) const;
186 void verify(MachineFunction
&MF
);
188 char ARMConstantIslands::ID
= 0;
191 /// verify - check BBOffsets, BBSizes, alignment of islands
192 void ARMConstantIslands::verify(MachineFunction
&MF
) {
193 assert(BBOffsets
.size() == BBSizes
.size());
194 for (unsigned i
= 1, e
= BBOffsets
.size(); i
!= e
; ++i
)
195 assert(BBOffsets
[i
-1]+BBSizes
[i
-1] == BBOffsets
[i
]);
199 for (MachineFunction::iterator MBBI
= MF
.begin(), E
= MF
.end();
201 MachineBasicBlock
*MBB
= MBBI
;
203 MBB
->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY
) {
204 unsigned MBBId
= MBB
->getNumber();
205 assert((BBOffsets
[MBBId
]%4 == 0 && BBSizes
[MBBId
]%4 == 0) ||
206 (BBOffsets
[MBBId
]%4 != 0 && BBSizes
[MBBId
]%4 != 0));
212 /// print block size and offset information - debugging
213 void ARMConstantIslands::dumpBBs() {
214 for (unsigned J
= 0, E
= BBOffsets
.size(); J
!=E
; ++J
) {
215 DOUT
<< "block " << J
<< " offset " << BBOffsets
[J
] <<
216 " size " << BBSizes
[J
] << "\n";
220 /// createARMConstantIslandPass - returns an instance of the constpool
222 FunctionPass
*llvm::createARMConstantIslandPass() {
223 return new ARMConstantIslands();
226 bool ARMConstantIslands::runOnMachineFunction(MachineFunction
&MF
) {
227 MachineConstantPool
&MCP
= *MF
.getConstantPool();
229 TII
= MF
.getTarget().getInstrInfo();
230 AFI
= MF
.getInfo
<ARMFunctionInfo
>();
231 STI
= &MF
.getTarget().getSubtarget
<ARMSubtarget
>();
233 isThumb
= AFI
->isThumbFunction();
234 isThumb1
= AFI
->isThumb1OnlyFunction();
235 isThumb2
= AFI
->isThumb2Function();
239 // Renumber all of the machine basic blocks in the function, guaranteeing that
240 // the numbers agree with the position of the block in the function.
243 // Thumb1 functions containing constant pools get 4-byte alignment.
244 // This is so we can keep exact track of where the alignment padding goes.
246 // Set default. Thumb1 function is 2-byte aligned, ARM and Thumb2 are 4-byte
248 AFI
->setAlign(isThumb1
? 1U : 2U);
250 // Perform the initial placement of the constant pool entries. To start with,
251 // we put them all at the end of the function.
252 std::vector
<MachineInstr
*> CPEMIs
;
253 if (!MCP
.isEmpty()) {
254 DoInitialPlacement(MF
, CPEMIs
);
259 /// The next UID to take is the first unused one.
260 AFI
->initConstPoolEntryUId(CPEMIs
.size());
262 // Do the initial scan of the function, building up information about the
263 // sizes of each block, the location of all the water, and finding all of the
264 // constant pool users.
265 InitialFunctionScan(MF
, CPEMIs
);
268 /// Remove dead constant pool entries.
269 RemoveUnusedCPEntries();
271 // Iteratively place constant pool entries and fix up branches until there
273 bool MadeChange
= false;
274 unsigned NoCPIters
= 0, NoBRIters
= 0;
276 bool CPChange
= false;
277 for (unsigned i
= 0, e
= CPUsers
.size(); i
!= e
; ++i
)
278 CPChange
|= HandleConstantPoolUser(MF
, i
);
279 if (CPChange
&& ++NoCPIters
> 30)
280 llvm_unreachable("Constant Island pass failed to converge!");
283 bool BRChange
= false;
284 for (unsigned i
= 0, e
= ImmBranches
.size(); i
!= e
; ++i
)
285 BRChange
|= FixUpImmediateBr(MF
, ImmBranches
[i
]);
286 if (BRChange
&& ++NoBRIters
> 30)
287 llvm_unreachable("Branch Fix Up pass failed to converge!");
290 if (!CPChange
&& !BRChange
)
295 // Let's see if we can use tbb / tbh to do jump tables.
296 MadeChange
|= OptimizeThumb2JumpTables(MF
);
298 // After a while, this might be made debug-only, but it is not expensive.
301 // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
302 // Undo the spill / restore of LR if possible.
303 if (isThumb
&& !HasFarJump
&& AFI
->isLRSpilledForFarJump())
304 MadeChange
|= UndoLRSpillRestore();
313 T2JumpTables
.clear();
318 /// DoInitialPlacement - Perform the initial placement of the constant pool
319 /// entries. To start with, we put them all at the end of the function.
320 void ARMConstantIslands::DoInitialPlacement(MachineFunction
&MF
,
321 std::vector
<MachineInstr
*> &CPEMIs
) {
322 // Create the basic block to hold the CPE's.
323 MachineBasicBlock
*BB
= MF
.CreateMachineBasicBlock();
326 // Add all of the constants from the constant pool to the end block, use an
327 // identity mapping of CPI's to CPE's.
328 const std::vector
<MachineConstantPoolEntry
> &CPs
=
329 MF
.getConstantPool()->getConstants();
331 const TargetData
&TD
= *MF
.getTarget().getTargetData();
332 for (unsigned i
= 0, e
= CPs
.size(); i
!= e
; ++i
) {
333 unsigned Size
= TD
.getTypeAllocSize(CPs
[i
].getType());
334 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
335 // we would have to pad them out or something so that instructions stay
337 assert((Size
& 3) == 0 && "CP Entry not multiple of 4 bytes!");
338 MachineInstr
*CPEMI
=
339 BuildMI(BB
, DebugLoc::getUnknownLoc(), TII
->get(ARM::CONSTPOOL_ENTRY
))
340 .addImm(i
).addConstantPoolIndex(i
).addImm(Size
);
341 CPEMIs
.push_back(CPEMI
);
343 // Add a new CPEntry, but no corresponding CPUser yet.
344 std::vector
<CPEntry
> CPEs
;
345 CPEs
.push_back(CPEntry(CPEMI
, i
));
346 CPEntries
.push_back(CPEs
);
348 DOUT
<< "Moved CPI#" << i
<< " to end of function as #" << i
<< "\n";
352 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
353 /// into the block immediately after it.
354 static bool BBHasFallthrough(MachineBasicBlock
*MBB
) {
355 // Get the next machine basic block in the function.
356 MachineFunction::iterator MBBI
= MBB
;
357 if (next(MBBI
) == MBB
->getParent()->end()) // Can't fall off end of function.
360 MachineBasicBlock
*NextBB
= next(MBBI
);
361 for (MachineBasicBlock::succ_iterator I
= MBB
->succ_begin(),
362 E
= MBB
->succ_end(); I
!= E
; ++I
)
369 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
370 /// look up the corresponding CPEntry.
371 ARMConstantIslands::CPEntry
372 *ARMConstantIslands::findConstPoolEntry(unsigned CPI
,
373 const MachineInstr
*CPEMI
) {
374 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
375 // Number of entries per constpool index should be small, just do a
377 for (unsigned i
= 0, e
= CPEs
.size(); i
!= e
; ++i
) {
378 if (CPEs
[i
].CPEMI
== CPEMI
)
384 /// InitialFunctionScan - Do the initial scan of the function, building up
385 /// information about the sizes of each block, the location of all the water,
386 /// and finding all of the constant pool users.
387 void ARMConstantIslands::InitialFunctionScan(MachineFunction
&MF
,
388 const std::vector
<MachineInstr
*> &CPEMIs
) {
390 for (MachineFunction::iterator MBBI
= MF
.begin(), E
= MF
.end();
392 MachineBasicBlock
&MBB
= *MBBI
;
394 // If this block doesn't fall through into the next MBB, then this is
395 // 'water' that a constant pool island could be placed.
396 if (!BBHasFallthrough(&MBB
))
397 WaterList
.push_back(&MBB
);
399 unsigned MBBSize
= 0;
400 for (MachineBasicBlock::iterator I
= MBB
.begin(), E
= MBB
.end();
402 // Add instruction size to MBBSize.
403 MBBSize
+= TII
->GetInstSizeInBytes(I
);
405 int Opc
= I
->getOpcode();
406 if (I
->getDesc().isBranch()) {
413 continue; // Ignore other JT branches
415 // A Thumb1 table jump may involve padding; for the offsets to
416 // be right, functions containing these must be 4-byte aligned.
418 if ((Offset
+MBBSize
)%4 != 0)
419 // FIXME: Add a pseudo ALIGN instruction instead.
420 MBBSize
+= 2; // padding
421 continue; // Does not get an entry in ImmBranches
423 T2JumpTables
.push_back(I
);
424 continue; // Does not get an entry in ImmBranches
455 // Record this immediate branch.
456 unsigned MaxOffs
= ((1 << (Bits
-1))-1) * Scale
;
457 ImmBranches
.push_back(ImmBranch(I
, MaxOffs
, isCond
, UOpc
));
460 if (Opc
== ARM::tPUSH
|| Opc
== ARM::tPOP_RET
)
461 PushPopMIs
.push_back(I
);
463 if (Opc
== ARM::CONSTPOOL_ENTRY
)
466 // Scan the instructions for constant pool operands.
467 for (unsigned op
= 0, e
= I
->getNumOperands(); op
!= e
; ++op
)
468 if (I
->getOperand(op
).isCPI()) {
469 // We found one. The addressing mode tells us the max displacement
470 // from the PC that this instruction permits.
472 // Basic size info comes from the TSFlags field.
476 bool IsSoImm
= false;
478 // FIXME: Temporary workaround until I can figure out what's going on.
479 unsigned Slack
= T2JumpTables
.empty() ? 0 : 4;
482 llvm_unreachable("Unknown addressing mode for CP reference!");
485 // Taking the address of a CP entry.
487 // This takes a SoImm, which is 8 bit immediate rotated. We'll
488 // pretend the maximum offset is 255 * 4. Since each instruction
489 // 4 byte wide, this is always correct. We'llc heck for other
490 // displacements that fits in a SoImm as well.
496 case ARM::t2LEApcrel
:
508 Bits
= 12; // +-offset_12
515 Scale
= 4; // +(offset_8*4)
521 Scale
= 4; // +-(offset_8*4)
526 // Remember that this is a user of a CP entry.
527 unsigned CPI
= I
->getOperand(op
).getIndex();
528 MachineInstr
*CPEMI
= CPEMIs
[CPI
];
529 unsigned MaxOffs
= ((1 << Bits
)-1) * Scale
- Slack
;
530 CPUsers
.push_back(CPUser(I
, CPEMI
, MaxOffs
, NegOk
, IsSoImm
));
532 // Increment corresponding CPEntry reference count.
533 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
534 assert(CPE
&& "Cannot find a corresponding CPEntry!");
537 // Instructions can only use one CP entry, don't bother scanning the
538 // rest of the operands.
543 // In thumb mode, if this block is a constpool island, we may need padding
544 // so it's aligned on 4 byte boundary.
547 MBB
.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY
&&
551 BBSizes
.push_back(MBBSize
);
552 BBOffsets
.push_back(Offset
);
557 /// GetOffsetOf - Return the current offset of the specified machine instruction
558 /// from the start of the function. This offset changes as stuff is moved
559 /// around inside the function.
560 unsigned ARMConstantIslands::GetOffsetOf(MachineInstr
*MI
) const {
561 MachineBasicBlock
*MBB
= MI
->getParent();
563 // The offset is composed of two things: the sum of the sizes of all MBB's
564 // before this instruction's block, and the offset from the start of the block
566 unsigned Offset
= BBOffsets
[MBB
->getNumber()];
568 // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
569 // alignment padding, and compensate if so.
571 MI
->getOpcode() == ARM::CONSTPOOL_ENTRY
&&
575 // Sum instructions before MI in MBB.
576 for (MachineBasicBlock::iterator I
= MBB
->begin(); ; ++I
) {
577 assert(I
!= MBB
->end() && "Didn't find MI in its own basic block?");
578 if (&*I
== MI
) return Offset
;
579 Offset
+= TII
->GetInstSizeInBytes(I
);
583 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
585 static bool CompareMBBNumbers(const MachineBasicBlock
*LHS
,
586 const MachineBasicBlock
*RHS
) {
587 return LHS
->getNumber() < RHS
->getNumber();
590 /// UpdateForInsertedWaterBlock - When a block is newly inserted into the
591 /// machine function, it upsets all of the block numbers. Renumber the blocks
592 /// and update the arrays that parallel this numbering.
593 void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock
*NewBB
) {
594 // Renumber the MBB's to keep them consequtive.
595 NewBB
->getParent()->RenumberBlocks(NewBB
);
597 // Insert a size into BBSizes to align it properly with the (newly
598 // renumbered) block numbers.
599 BBSizes
.insert(BBSizes
.begin()+NewBB
->getNumber(), 0);
601 // Likewise for BBOffsets.
602 BBOffsets
.insert(BBOffsets
.begin()+NewBB
->getNumber(), 0);
604 // Next, update WaterList. Specifically, we need to add NewMBB as having
605 // available water after it.
606 std::vector
<MachineBasicBlock
*>::iterator IP
=
607 std::lower_bound(WaterList
.begin(), WaterList
.end(), NewBB
,
609 WaterList
.insert(IP
, NewBB
);
613 /// Split the basic block containing MI into two blocks, which are joined by
614 /// an unconditional branch. Update datastructures and renumber blocks to
615 /// account for this change and returns the newly created block.
616 MachineBasicBlock
*ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr
*MI
) {
617 MachineBasicBlock
*OrigBB
= MI
->getParent();
618 MachineFunction
&MF
= *OrigBB
->getParent();
620 // Create a new MBB for the code after the OrigBB.
621 MachineBasicBlock
*NewBB
=
622 MF
.CreateMachineBasicBlock(OrigBB
->getBasicBlock());
623 MachineFunction::iterator MBBI
= OrigBB
; ++MBBI
;
624 MF
.insert(MBBI
, NewBB
);
626 // Splice the instructions starting with MI over to NewBB.
627 NewBB
->splice(NewBB
->end(), OrigBB
, MI
, OrigBB
->end());
629 // Add an unconditional branch from OrigBB to NewBB.
630 // Note the new unconditional branch is not being recorded.
631 // There doesn't seem to be meaningful DebugInfo available; this doesn't
632 // correspond to anything in the source.
633 unsigned Opc
= isThumb
? (isThumb2
? ARM::t2B
: ARM::tB
) : ARM::B
;
634 BuildMI(OrigBB
, DebugLoc::getUnknownLoc(), TII
->get(Opc
)).addMBB(NewBB
);
637 // Update the CFG. All succs of OrigBB are now succs of NewBB.
638 while (!OrigBB
->succ_empty()) {
639 MachineBasicBlock
*Succ
= *OrigBB
->succ_begin();
640 OrigBB
->removeSuccessor(Succ
);
641 NewBB
->addSuccessor(Succ
);
643 // This pass should be run after register allocation, so there should be no
644 // PHI nodes to update.
645 assert((Succ
->empty() || Succ
->begin()->getOpcode() != TargetInstrInfo::PHI
)
646 && "PHI nodes should be eliminated by now!");
649 // OrigBB branches to NewBB.
650 OrigBB
->addSuccessor(NewBB
);
652 // Update internal data structures to account for the newly inserted MBB.
653 // This is almost the same as UpdateForInsertedWaterBlock, except that
654 // the Water goes after OrigBB, not NewBB.
655 MF
.RenumberBlocks(NewBB
);
657 // Insert a size into BBSizes to align it properly with the (newly
658 // renumbered) block numbers.
659 BBSizes
.insert(BBSizes
.begin()+NewBB
->getNumber(), 0);
661 // Likewise for BBOffsets.
662 BBOffsets
.insert(BBOffsets
.begin()+NewBB
->getNumber(), 0);
664 // Next, update WaterList. Specifically, we need to add OrigMBB as having
665 // available water after it (but not if it's already there, which happens
666 // when splitting before a conditional branch that is followed by an
667 // unconditional branch - in that case we want to insert NewBB).
668 std::vector
<MachineBasicBlock
*>::iterator IP
=
669 std::lower_bound(WaterList
.begin(), WaterList
.end(), OrigBB
,
671 MachineBasicBlock
* WaterBB
= *IP
;
672 if (WaterBB
== OrigBB
)
673 WaterList
.insert(next(IP
), NewBB
);
675 WaterList
.insert(IP
, OrigBB
);
677 // Figure out how large the first NewMBB is. (It cannot
678 // contain a constpool_entry or tablejump.)
679 unsigned NewBBSize
= 0;
680 for (MachineBasicBlock::iterator I
= NewBB
->begin(), E
= NewBB
->end();
682 NewBBSize
+= TII
->GetInstSizeInBytes(I
);
684 unsigned OrigBBI
= OrigBB
->getNumber();
685 unsigned NewBBI
= NewBB
->getNumber();
686 // Set the size of NewBB in BBSizes.
687 BBSizes
[NewBBI
] = NewBBSize
;
689 // We removed instructions from UserMBB, subtract that off from its size.
690 // Add 2 or 4 to the block to count the unconditional branch we added to it.
691 int delta
= isThumb1
? 2 : 4;
692 BBSizes
[OrigBBI
] -= NewBBSize
- delta
;
694 // ...and adjust BBOffsets for NewBB accordingly.
695 BBOffsets
[NewBBI
] = BBOffsets
[OrigBBI
] + BBSizes
[OrigBBI
];
697 // All BBOffsets following these blocks must be modified.
698 AdjustBBOffsetsAfter(NewBB
, delta
);
703 /// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
704 /// reference) is within MaxDisp of TrialOffset (a proposed location of a
705 /// constant pool entry).
706 bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset
,
707 unsigned TrialOffset
, unsigned MaxDisp
,
708 bool NegativeOK
, bool IsSoImm
) {
709 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
710 // purposes of the displacement computation; compensate for that here.
711 // Effectively, the valid range of displacements is 2 bytes smaller for such
713 if (isThumb
&& UserOffset
%4 !=0)
715 // CPEs will be rounded up to a multiple of 4.
716 if (isThumb
&& TrialOffset
%4 != 0)
719 if (UserOffset
<= TrialOffset
) {
720 // User before the Trial.
721 if (TrialOffset
- UserOffset
<= MaxDisp
)
723 // FIXME: Make use full range of soimm values.
724 } else if (NegativeOK
) {
725 if (UserOffset
- TrialOffset
<= MaxDisp
)
727 // FIXME: Make use full range of soimm values.
732 /// WaterIsInRange - Returns true if a CPE placed after the specified
733 /// Water (a basic block) will be in range for the specific MI.
735 bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset
,
736 MachineBasicBlock
* Water
, CPUser
&U
) {
737 unsigned MaxDisp
= U
.MaxDisp
;
738 unsigned CPEOffset
= BBOffsets
[Water
->getNumber()] +
739 BBSizes
[Water
->getNumber()];
741 // If the CPE is to be inserted before the instruction, that will raise
742 // the offset of the instruction. (Currently applies only to ARM, so
743 // no alignment compensation attempted here.)
744 if (CPEOffset
< UserOffset
)
745 UserOffset
+= U
.CPEMI
->getOperand(2).getImm();
747 return OffsetIsInRange(UserOffset
, CPEOffset
, MaxDisp
, U
.NegOk
, U
.IsSoImm
);
750 /// CPEIsInRange - Returns true if the distance between specific MI and
751 /// specific ConstPool entry instruction can fit in MI's displacement field.
752 bool ARMConstantIslands::CPEIsInRange(MachineInstr
*MI
, unsigned UserOffset
,
753 MachineInstr
*CPEMI
, unsigned MaxDisp
,
754 bool NegOk
, bool DoDump
) {
755 unsigned CPEOffset
= GetOffsetOf(CPEMI
);
756 assert(CPEOffset
%4 == 0 && "Misaligned CPE");
759 DOUT
<< "User of CPE#" << CPEMI
->getOperand(0).getImm()
760 << " max delta=" << MaxDisp
761 << " insn address=" << UserOffset
762 << " CPE address=" << CPEOffset
763 << " offset=" << int(CPEOffset
-UserOffset
) << "\t" << *MI
;
766 return OffsetIsInRange(UserOffset
, CPEOffset
, MaxDisp
, NegOk
);
770 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
771 /// unconditionally branches to its only successor.
772 static bool BBIsJumpedOver(MachineBasicBlock
*MBB
) {
773 if (MBB
->pred_size() != 1 || MBB
->succ_size() != 1)
776 MachineBasicBlock
*Succ
= *MBB
->succ_begin();
777 MachineBasicBlock
*Pred
= *MBB
->pred_begin();
778 MachineInstr
*PredMI
= &Pred
->back();
779 if (PredMI
->getOpcode() == ARM::B
|| PredMI
->getOpcode() == ARM::tB
780 || PredMI
->getOpcode() == ARM::t2B
)
781 return PredMI
->getOperand(0).getMBB() == Succ
;
786 void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock
*BB
,
788 MachineFunction::iterator MBBI
= BB
; MBBI
= next(MBBI
);
789 for(unsigned i
= BB
->getNumber()+1, e
= BB
->getParent()->getNumBlockIDs();
791 BBOffsets
[i
] += delta
;
792 // If some existing blocks have padding, adjust the padding as needed, a
793 // bit tricky. delta can be negative so don't use % on that.
796 MachineBasicBlock
*MBB
= MBBI
;
798 // Constant pool entries require padding.
799 if (MBB
->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY
) {
800 unsigned OldOffset
= BBOffsets
[i
] - delta
;
801 if ((OldOffset
%4) == 0 && (BBOffsets
[i
]%4) != 0) {
805 } else if ((OldOffset
%4) != 0 && (BBOffsets
[i
]%4) == 0) {
806 // remove existing padding
811 // Thumb1 jump tables require padding. They should be at the end;
812 // following unconditional branches are removed by AnalyzeBranch.
813 MachineInstr
*ThumbJTMI
= prior(MBB
->end());
814 if (ThumbJTMI
->getOpcode() == ARM::tBR_JTr
) {
815 unsigned NewMIOffset
= GetOffsetOf(ThumbJTMI
);
816 unsigned OldMIOffset
= NewMIOffset
- delta
;
817 if ((OldMIOffset
%4) == 0 && (NewMIOffset
%4) != 0) {
818 // remove existing padding
821 } else if ((OldMIOffset
%4) != 0 && (NewMIOffset
%4) == 0) {
834 /// DecrementOldEntry - find the constant pool entry with index CPI
835 /// and instruction CPEMI, and decrement its refcount. If the refcount
836 /// becomes 0 remove the entry and instruction. Returns true if we removed
837 /// the entry, false if we didn't.
839 bool ARMConstantIslands::DecrementOldEntry(unsigned CPI
, MachineInstr
*CPEMI
) {
840 // Find the old entry. Eliminate it if it is no longer used.
841 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
842 assert(CPE
&& "Unexpected!");
843 if (--CPE
->RefCount
== 0) {
844 RemoveDeadCPEMI(CPEMI
);
852 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
853 /// if not, see if an in-range clone of the CPE is in range, and if so,
854 /// change the data structures so the user references the clone. Returns:
855 /// 0 = no existing entry found
856 /// 1 = entry found, and there were no code insertions or deletions
857 /// 2 = entry found, and there were code insertions or deletions
858 int ARMConstantIslands::LookForExistingCPEntry(CPUser
& U
, unsigned UserOffset
)
860 MachineInstr
*UserMI
= U
.MI
;
861 MachineInstr
*CPEMI
= U
.CPEMI
;
863 // Check to see if the CPE is already in-range.
864 if (CPEIsInRange(UserMI
, UserOffset
, CPEMI
, U
.MaxDisp
, U
.NegOk
, true)) {
865 DOUT
<< "In range\n";
869 // No. Look for previously created clones of the CPE that are in range.
870 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
871 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
872 for (unsigned i
= 0, e
= CPEs
.size(); i
!= e
; ++i
) {
873 // We already tried this one
874 if (CPEs
[i
].CPEMI
== CPEMI
)
876 // Removing CPEs can leave empty entries, skip
877 if (CPEs
[i
].CPEMI
== NULL
)
879 if (CPEIsInRange(UserMI
, UserOffset
, CPEs
[i
].CPEMI
, U
.MaxDisp
, U
.NegOk
)) {
880 DOUT
<< "Replacing CPE#" << CPI
<< " with CPE#" << CPEs
[i
].CPI
<< "\n";
881 // Point the CPUser node to the replacement
882 U
.CPEMI
= CPEs
[i
].CPEMI
;
883 // Change the CPI in the instruction operand to refer to the clone.
884 for (unsigned j
= 0, e
= UserMI
->getNumOperands(); j
!= e
; ++j
)
885 if (UserMI
->getOperand(j
).isCPI()) {
886 UserMI
->getOperand(j
).setIndex(CPEs
[i
].CPI
);
889 // Adjust the refcount of the clone...
891 // ...and the original. If we didn't remove the old entry, none of the
892 // addresses changed, so we don't need another pass.
893 return DecrementOldEntry(CPI
, CPEMI
) ? 2 : 1;
899 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
900 /// the specific unconditional branch instruction.
901 static inline unsigned getUnconditionalBrDisp(int Opc
) {
904 return ((1<<10)-1)*2;
906 return ((1<<23)-1)*2;
911 return ((1<<23)-1)*4;
914 /// AcceptWater - Small amount of common code factored out of the following.
916 MachineBasicBlock
* ARMConstantIslands::AcceptWater(MachineBasicBlock
*WaterBB
,
917 std::vector
<MachineBasicBlock
*>::iterator IP
) {
918 DOUT
<< "found water in range\n";
919 // Remove the original WaterList entry; we want subsequent
920 // insertions in this vicinity to go after the one we're
921 // about to insert. This considerably reduces the number
922 // of times we have to move the same CPE more than once.
924 // CPE goes before following block (NewMBB).
925 return next(MachineFunction::iterator(WaterBB
));
928 /// LookForWater - look for an existing entry in the WaterList in which
929 /// we can place the CPE referenced from U so it's within range of U's MI.
930 /// Returns true if found, false if not. If it returns true, *NewMBB
931 /// is set to the WaterList entry.
932 /// For ARM, we prefer the water that's farthest away. For Thumb, prefer
933 /// water that will not introduce padding to water that will; within each
934 /// group, prefer the water that's farthest away.
935 bool ARMConstantIslands::LookForWater(CPUser
&U
, unsigned UserOffset
,
936 MachineBasicBlock
** NewMBB
) {
937 std::vector
<MachineBasicBlock
*>::iterator IPThatWouldPad
;
938 MachineBasicBlock
* WaterBBThatWouldPad
= NULL
;
939 if (!WaterList
.empty()) {
940 for (std::vector
<MachineBasicBlock
*>::iterator IP
= prior(WaterList
.end()),
941 B
= WaterList
.begin();; --IP
) {
942 MachineBasicBlock
* WaterBB
= *IP
;
943 if (WaterIsInRange(UserOffset
, WaterBB
, U
)) {
944 unsigned WBBId
= WaterBB
->getNumber();
946 (BBOffsets
[WBBId
] + BBSizes
[WBBId
])%4 != 0) {
947 // This is valid Water, but would introduce padding. Remember
948 // it in case we don't find any Water that doesn't do this.
949 if (!WaterBBThatWouldPad
) {
950 WaterBBThatWouldPad
= WaterBB
;
954 *NewMBB
= AcceptWater(WaterBB
, IP
);
962 if (isThumb
&& WaterBBThatWouldPad
) {
963 *NewMBB
= AcceptWater(WaterBBThatWouldPad
, IPThatWouldPad
);
969 /// CreateNewWater - No existing WaterList entry will work for
970 /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
971 /// block is used if in range, and the conditional branch munged so control
972 /// flow is correct. Otherwise the block is split to create a hole with an
973 /// unconditional branch around it. In either case *NewMBB is set to a
974 /// block following which the new island can be inserted (the WaterList
975 /// is not adjusted).
977 void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex
,
978 unsigned UserOffset
, MachineBasicBlock
** NewMBB
) {
979 CPUser
&U
= CPUsers
[CPUserIndex
];
980 MachineInstr
*UserMI
= U
.MI
;
981 MachineInstr
*CPEMI
= U
.CPEMI
;
982 MachineBasicBlock
*UserMBB
= UserMI
->getParent();
983 unsigned OffsetOfNextBlock
= BBOffsets
[UserMBB
->getNumber()] +
984 BBSizes
[UserMBB
->getNumber()];
985 assert(OffsetOfNextBlock
== BBOffsets
[UserMBB
->getNumber()+1]);
987 // If the use is at the end of the block, or the end of the block
988 // is within range, make new water there. (The addition below is
989 // for the unconditional branch we will be adding: 4 bytes on ARM + Thumb2,
990 // 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
991 // inside OffsetIsInRange.
992 // If the block ends in an unconditional branch already, it is water,
993 // and is known to be out of range, so we'll always be adding a branch.)
994 if (&UserMBB
->back() == UserMI
||
995 OffsetIsInRange(UserOffset
, OffsetOfNextBlock
+ (isThumb1
? 2: 4),
996 U
.MaxDisp
, U
.NegOk
, U
.IsSoImm
)) {
997 DOUT
<< "Split at end of block\n";
998 if (&UserMBB
->back() == UserMI
)
999 assert(BBHasFallthrough(UserMBB
) && "Expected a fallthrough BB!");
1000 *NewMBB
= next(MachineFunction::iterator(UserMBB
));
1001 // Add an unconditional branch from UserMBB to fallthrough block.
1002 // Record it for branch lengthening; this new branch will not get out of
1003 // range, but if the preceding conditional branch is out of range, the
1004 // targets will be exchanged, and the altered branch may be out of
1005 // range, so the machinery has to know about it.
1006 int UncondBr
= isThumb
? ((isThumb2
) ? ARM::t2B
: ARM::tB
) : ARM::B
;
1007 BuildMI(UserMBB
, DebugLoc::getUnknownLoc(),
1008 TII
->get(UncondBr
)).addMBB(*NewMBB
);
1009 unsigned MaxDisp
= getUnconditionalBrDisp(UncondBr
);
1010 ImmBranches
.push_back(ImmBranch(&UserMBB
->back(),
1011 MaxDisp
, false, UncondBr
));
1012 int delta
= isThumb1
? 2 : 4;
1013 BBSizes
[UserMBB
->getNumber()] += delta
;
1014 AdjustBBOffsetsAfter(UserMBB
, delta
);
1016 // What a big block. Find a place within the block to split it.
1017 // This is a little tricky on Thumb1 since instructions are 2 bytes
1018 // and constant pool entries are 4 bytes: if instruction I references
1019 // island CPE, and instruction I+1 references CPE', it will
1020 // not work well to put CPE as far forward as possible, since then
1021 // CPE' cannot immediately follow it (that location is 2 bytes
1022 // farther away from I+1 than CPE was from I) and we'd need to create
1023 // a new island. So, we make a first guess, then walk through the
1024 // instructions between the one currently being looked at and the
1025 // possible insertion point, and make sure any other instructions
1026 // that reference CPEs will be able to use the same island area;
1027 // if not, we back up the insertion point.
1029 // The 4 in the following is for the unconditional branch we'll be
1030 // inserting (allows for long branch on Thumb1). Alignment of the
1031 // island is handled inside OffsetIsInRange.
1032 unsigned BaseInsertOffset
= UserOffset
+ U
.MaxDisp
-4;
1033 // This could point off the end of the block if we've already got
1034 // constant pool entries following this block; only the last one is
1035 // in the water list. Back past any possible branches (allow for a
1036 // conditional and a maximally long unconditional).
1037 if (BaseInsertOffset
>= BBOffsets
[UserMBB
->getNumber()+1])
1038 BaseInsertOffset
= BBOffsets
[UserMBB
->getNumber()+1] -
1040 unsigned EndInsertOffset
= BaseInsertOffset
+
1041 CPEMI
->getOperand(2).getImm();
1042 MachineBasicBlock::iterator MI
= UserMI
;
1044 unsigned CPUIndex
= CPUserIndex
+1;
1045 for (unsigned Offset
= UserOffset
+TII
->GetInstSizeInBytes(UserMI
);
1046 Offset
< BaseInsertOffset
;
1047 Offset
+= TII
->GetInstSizeInBytes(MI
),
1049 if (CPUIndex
< CPUsers
.size() && CPUsers
[CPUIndex
].MI
== MI
) {
1050 CPUser
&U
= CPUsers
[CPUIndex
];
1051 if (!OffsetIsInRange(Offset
, EndInsertOffset
,
1052 U
.MaxDisp
, U
.NegOk
, U
.IsSoImm
)) {
1053 BaseInsertOffset
-= (isThumb1
? 2 : 4);
1054 EndInsertOffset
-= (isThumb1
? 2 : 4);
1056 // This is overly conservative, as we don't account for CPEMIs
1057 // being reused within the block, but it doesn't matter much.
1058 EndInsertOffset
+= CPUsers
[CPUIndex
].CPEMI
->getOperand(2).getImm();
1062 DOUT
<< "Split in middle of big block\n";
1063 *NewMBB
= SplitBlockBeforeInstr(prior(MI
));
1067 /// HandleConstantPoolUser - Analyze the specified user, checking to see if it
1068 /// is out-of-range. If so, pick up the constant pool value and move it some
1069 /// place in-range. Return true if we changed any addresses (thus must run
1070 /// another pass of branch lengthening), false otherwise.
1071 bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction
&MF
,
1072 unsigned CPUserIndex
) {
1073 CPUser
&U
= CPUsers
[CPUserIndex
];
1074 MachineInstr
*UserMI
= U
.MI
;
1075 MachineInstr
*CPEMI
= U
.CPEMI
;
1076 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1077 unsigned Size
= CPEMI
->getOperand(2).getImm();
1078 MachineBasicBlock
*NewMBB
;
1079 // Compute this only once, it's expensive. The 4 or 8 is the value the
1080 // hardware keeps in the PC (2 insns ahead of the reference).
1081 unsigned UserOffset
= GetOffsetOf(UserMI
) + (isThumb
? 4 : 8);
1083 // See if the current entry is within range, or there is a clone of it
1085 int result
= LookForExistingCPEntry(U
, UserOffset
);
1086 if (result
==1) return false;
1087 else if (result
==2) return true;
1089 // No existing clone of this CPE is within range.
1090 // We will be generating a new clone. Get a UID for it.
1091 unsigned ID
= AFI
->createConstPoolEntryUId();
1093 // Look for water where we can place this CPE. We look for the farthest one
1094 // away that will work. Forward references only for now (although later
1095 // we might find some that are backwards).
1097 if (!LookForWater(U
, UserOffset
, &NewMBB
)) {
1099 DOUT
<< "No water found\n";
1100 CreateNewWater(CPUserIndex
, UserOffset
, &NewMBB
);
1103 // Okay, we know we can put an island before NewMBB now, do it!
1104 MachineBasicBlock
*NewIsland
= MF
.CreateMachineBasicBlock();
1105 MF
.insert(NewMBB
, NewIsland
);
1107 // Update internal data structures to account for the newly inserted MBB.
1108 UpdateForInsertedWaterBlock(NewIsland
);
1110 // Decrement the old entry, and remove it if refcount becomes 0.
1111 DecrementOldEntry(CPI
, CPEMI
);
1113 // Now that we have an island to add the CPE to, clone the original CPE and
1114 // add it to the island.
1115 U
.CPEMI
= BuildMI(NewIsland
, DebugLoc::getUnknownLoc(),
1116 TII
->get(ARM::CONSTPOOL_ENTRY
))
1117 .addImm(ID
).addConstantPoolIndex(CPI
).addImm(Size
);
1118 CPEntries
[CPI
].push_back(CPEntry(U
.CPEMI
, ID
, 1));
1121 BBOffsets
[NewIsland
->getNumber()] = BBOffsets
[NewMBB
->getNumber()];
1122 // Compensate for .align 2 in thumb mode.
1123 if (isThumb
&& BBOffsets
[NewIsland
->getNumber()]%4 != 0)
1125 // Increase the size of the island block to account for the new entry.
1126 BBSizes
[NewIsland
->getNumber()] += Size
;
1127 AdjustBBOffsetsAfter(NewIsland
, Size
);
1129 // Finally, change the CPI in the instruction operand to be ID.
1130 for (unsigned i
= 0, e
= UserMI
->getNumOperands(); i
!= e
; ++i
)
1131 if (UserMI
->getOperand(i
).isCPI()) {
1132 UserMI
->getOperand(i
).setIndex(ID
);
1136 DOUT
<< " Moved CPE to #" << ID
<< " CPI=" << CPI
<< "\t" << *UserMI
;
1141 /// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1142 /// sizes and offsets of impacted basic blocks.
1143 void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr
*CPEMI
) {
1144 MachineBasicBlock
*CPEBB
= CPEMI
->getParent();
1145 unsigned Size
= CPEMI
->getOperand(2).getImm();
1146 CPEMI
->eraseFromParent();
1147 BBSizes
[CPEBB
->getNumber()] -= Size
;
1148 // All succeeding offsets have the current size value added in, fix this.
1149 if (CPEBB
->empty()) {
1150 // In thumb1 mode, the size of island may be padded by two to compensate for
1151 // the alignment requirement. Then it will now be 2 when the block is
1152 // empty, so fix this.
1153 // All succeeding offsets have the current size value added in, fix this.
1154 if (BBSizes
[CPEBB
->getNumber()] != 0) {
1155 Size
+= BBSizes
[CPEBB
->getNumber()];
1156 BBSizes
[CPEBB
->getNumber()] = 0;
1159 AdjustBBOffsetsAfter(CPEBB
, -Size
);
1160 // An island has only one predecessor BB and one successor BB. Check if
1161 // this BB's predecessor jumps directly to this BB's successor. This
1162 // shouldn't happen currently.
1163 assert(!BBIsJumpedOver(CPEBB
) && "How did this happen?");
1164 // FIXME: remove the empty blocks after all the work is done?
1167 /// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1169 bool ARMConstantIslands::RemoveUnusedCPEntries() {
1170 unsigned MadeChange
= false;
1171 for (unsigned i
= 0, e
= CPEntries
.size(); i
!= e
; ++i
) {
1172 std::vector
<CPEntry
> &CPEs
= CPEntries
[i
];
1173 for (unsigned j
= 0, ee
= CPEs
.size(); j
!= ee
; ++j
) {
1174 if (CPEs
[j
].RefCount
== 0 && CPEs
[j
].CPEMI
) {
1175 RemoveDeadCPEMI(CPEs
[j
].CPEMI
);
1176 CPEs
[j
].CPEMI
= NULL
;
1184 /// BBIsInRange - Returns true if the distance between specific MI and
1185 /// specific BB can fit in MI's displacement field.
1186 bool ARMConstantIslands::BBIsInRange(MachineInstr
*MI
,MachineBasicBlock
*DestBB
,
1188 unsigned PCAdj
= isThumb
? 4 : 8;
1189 unsigned BrOffset
= GetOffsetOf(MI
) + PCAdj
;
1190 unsigned DestOffset
= BBOffsets
[DestBB
->getNumber()];
1192 DOUT
<< "Branch of destination BB#" << DestBB
->getNumber()
1193 << " from BB#" << MI
->getParent()->getNumber()
1194 << " max delta=" << MaxDisp
1195 << " from " << GetOffsetOf(MI
) << " to " << DestOffset
1196 << " offset " << int(DestOffset
-BrOffset
) << "\t" << *MI
;
1198 if (BrOffset
<= DestOffset
) {
1199 // Branch before the Dest.
1200 if (DestOffset
-BrOffset
<= MaxDisp
)
1203 if (BrOffset
-DestOffset
<= MaxDisp
)
1209 /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1210 /// away to fit in its displacement field.
1211 bool ARMConstantIslands::FixUpImmediateBr(MachineFunction
&MF
, ImmBranch
&Br
) {
1212 MachineInstr
*MI
= Br
.MI
;
1213 MachineBasicBlock
*DestBB
= MI
->getOperand(0).getMBB();
1215 // Check to see if the DestBB is already in-range.
1216 if (BBIsInRange(MI
, DestBB
, Br
.MaxDisp
))
1220 return FixUpUnconditionalBr(MF
, Br
);
1221 return FixUpConditionalBr(MF
, Br
);
1224 /// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1225 /// too far away to fit in its displacement field. If the LR register has been
1226 /// spilled in the epilogue, then we can use BL to implement a far jump.
1227 /// Otherwise, add an intermediate branch instruction to a branch.
1229 ARMConstantIslands::FixUpUnconditionalBr(MachineFunction
&MF
, ImmBranch
&Br
) {
1230 MachineInstr
*MI
= Br
.MI
;
1231 MachineBasicBlock
*MBB
= MI
->getParent();
1233 llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!");
1235 // Use BL to implement far jump.
1236 Br
.MaxDisp
= (1 << 21) * 2;
1237 MI
->setDesc(TII
->get(ARM::tBfar
));
1238 BBSizes
[MBB
->getNumber()] += 2;
1239 AdjustBBOffsetsAfter(MBB
, 2);
1243 DOUT
<< " Changed B to long jump " << *MI
;
1248 /// FixUpConditionalBr - Fix up a conditional branch whose destination is too
1249 /// far away to fit in its displacement field. It is converted to an inverse
1250 /// conditional branch + an unconditional branch to the destination.
1252 ARMConstantIslands::FixUpConditionalBr(MachineFunction
&MF
, ImmBranch
&Br
) {
1253 MachineInstr
*MI
= Br
.MI
;
1254 MachineBasicBlock
*DestBB
= MI
->getOperand(0).getMBB();
1256 // Add an unconditional branch to the destination and invert the branch
1257 // condition to jump over it:
1263 ARMCC::CondCodes CC
= (ARMCC::CondCodes
)MI
->getOperand(1).getImm();
1264 CC
= ARMCC::getOppositeCondition(CC
);
1265 unsigned CCReg
= MI
->getOperand(2).getReg();
1267 // If the branch is at the end of its MBB and that has a fall-through block,
1268 // direct the updated conditional branch to the fall-through block. Otherwise,
1269 // split the MBB before the next instruction.
1270 MachineBasicBlock
*MBB
= MI
->getParent();
1271 MachineInstr
*BMI
= &MBB
->back();
1272 bool NeedSplit
= (BMI
!= MI
) || !BBHasFallthrough(MBB
);
1276 if (next(MachineBasicBlock::iterator(MI
)) == prior(MBB
->end()) &&
1277 BMI
->getOpcode() == Br
.UncondBr
) {
1278 // Last MI in the BB is an unconditional branch. Can we simply invert the
1279 // condition and swap destinations:
1285 MachineBasicBlock
*NewDest
= BMI
->getOperand(0).getMBB();
1286 if (BBIsInRange(MI
, NewDest
, Br
.MaxDisp
)) {
1287 DOUT
<< " Invert Bcc condition and swap its destination with " << *BMI
;
1288 BMI
->getOperand(0).setMBB(DestBB
);
1289 MI
->getOperand(0).setMBB(NewDest
);
1290 MI
->getOperand(1).setImm(CC
);
1297 SplitBlockBeforeInstr(MI
);
1298 // No need for the branch to the next block. We're adding an unconditional
1299 // branch to the destination.
1300 int delta
= TII
->GetInstSizeInBytes(&MBB
->back());
1301 BBSizes
[MBB
->getNumber()] -= delta
;
1302 MachineBasicBlock
* SplitBB
= next(MachineFunction::iterator(MBB
));
1303 AdjustBBOffsetsAfter(SplitBB
, -delta
);
1304 MBB
->back().eraseFromParent();
1305 // BBOffsets[SplitBB] is wrong temporarily, fixed below
1307 MachineBasicBlock
*NextBB
= next(MachineFunction::iterator(MBB
));
1309 DOUT
<< " Insert B to BB#" << DestBB
->getNumber()
1310 << " also invert condition and change dest. to BB#"
1311 << NextBB
->getNumber() << "\n";
1313 // Insert a new conditional branch and a new unconditional branch.
1314 // Also update the ImmBranch as well as adding a new entry for the new branch.
1315 BuildMI(MBB
, DebugLoc::getUnknownLoc(),
1316 TII
->get(MI
->getOpcode()))
1317 .addMBB(NextBB
).addImm(CC
).addReg(CCReg
);
1318 Br
.MI
= &MBB
->back();
1319 BBSizes
[MBB
->getNumber()] += TII
->GetInstSizeInBytes(&MBB
->back());
1320 BuildMI(MBB
, DebugLoc::getUnknownLoc(), TII
->get(Br
.UncondBr
)).addMBB(DestBB
);
1321 BBSizes
[MBB
->getNumber()] += TII
->GetInstSizeInBytes(&MBB
->back());
1322 unsigned MaxDisp
= getUnconditionalBrDisp(Br
.UncondBr
);
1323 ImmBranches
.push_back(ImmBranch(&MBB
->back(), MaxDisp
, false, Br
.UncondBr
));
1325 // Remove the old conditional branch. It may or may not still be in MBB.
1326 BBSizes
[MI
->getParent()->getNumber()] -= TII
->GetInstSizeInBytes(MI
);
1327 MI
->eraseFromParent();
1329 // The net size change is an addition of one unconditional branch.
1330 int delta
= TII
->GetInstSizeInBytes(&MBB
->back());
1331 AdjustBBOffsetsAfter(MBB
, delta
);
1335 /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
1336 /// LR / restores LR to pc.
1337 bool ARMConstantIslands::UndoLRSpillRestore() {
1338 bool MadeChange
= false;
1339 for (unsigned i
= 0, e
= PushPopMIs
.size(); i
!= e
; ++i
) {
1340 MachineInstr
*MI
= PushPopMIs
[i
];
1341 if (MI
->getOpcode() == ARM::tPOP_RET
&&
1342 MI
->getOperand(0).getReg() == ARM::PC
&&
1343 MI
->getNumExplicitOperands() == 1) {
1344 BuildMI(MI
->getParent(), MI
->getDebugLoc(), TII
->get(ARM::tBX_RET
));
1345 MI
->eraseFromParent();
1352 bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction
&MF
) {
1353 bool MadeChange
= false;
1355 // FIXME: After the tables are shrunk, can we get rid some of the
1356 // constantpool tables?
1357 const MachineJumpTableInfo
*MJTI
= MF
.getJumpTableInfo();
1358 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
1359 for (unsigned i
= 0, e
= T2JumpTables
.size(); i
!= e
; ++i
) {
1360 MachineInstr
*MI
= T2JumpTables
[i
];
1361 const TargetInstrDesc
&TID
= MI
->getDesc();
1362 unsigned NumOps
= TID
.getNumOperands();
1363 unsigned JTOpIdx
= NumOps
- (TID
.isPredicable() ? 3 : 2);
1364 MachineOperand JTOP
= MI
->getOperand(JTOpIdx
);
1365 unsigned JTI
= JTOP
.getIndex();
1366 assert(JTI
< JT
.size());
1369 bool HalfWordOk
= true;
1370 unsigned JTOffset
= GetOffsetOf(MI
) + 4;
1371 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
1372 for (unsigned j
= 0, ee
= JTBBs
.size(); j
!= ee
; ++j
) {
1373 MachineBasicBlock
*MBB
= JTBBs
[j
];
1374 unsigned DstOffset
= BBOffsets
[MBB
->getNumber()];
1375 // Negative offset is not ok. FIXME: We should change BB layout to make
1376 // sure all the branches are forward.
1377 if (ByteOk
&& (DstOffset
- JTOffset
) > ((1<<8)-1)*2)
1379 unsigned TBHLimit
= ((1<<16)-1)*2;
1380 if (STI
->isTargetDarwin())
1381 TBHLimit
>>= 1; // FIXME: Work around an assembler bug.
1382 if (HalfWordOk
&& (DstOffset
- JTOffset
) > TBHLimit
)
1384 if (!ByteOk
&& !HalfWordOk
)
1388 if (ByteOk
|| HalfWordOk
) {
1389 MachineBasicBlock
*MBB
= MI
->getParent();
1390 unsigned BaseReg
= MI
->getOperand(0).getReg();
1391 bool BaseRegKill
= MI
->getOperand(0).isKill();
1394 unsigned IdxReg
= MI
->getOperand(1).getReg();
1395 bool IdxRegKill
= MI
->getOperand(1).isKill();
1396 MachineBasicBlock::iterator PrevI
= MI
;
1397 if (PrevI
== MBB
->begin())
1400 MachineInstr
*AddrMI
= --PrevI
;
1402 // Examine the instruction that calculate the jumptable entry address.
1403 // If it's not the one just before the t2BR_JT, we won't delete it, then
1404 // it's not worth doing the optimization.
1405 for (unsigned k
= 0, eee
= AddrMI
->getNumOperands(); k
!= eee
; ++k
) {
1406 const MachineOperand
&MO
= AddrMI
->getOperand(k
);
1407 if (!MO
.isReg() || !MO
.getReg())
1409 if (MO
.isDef() && MO
.getReg() != BaseReg
) {
1413 if (MO
.isUse() && !MO
.isKill() && MO
.getReg() != IdxReg
) {
1421 // The previous instruction should be a t2LEApcrelJT, we want to delete
1423 MachineInstr
*LeaMI
= --PrevI
;
1424 if (LeaMI
->getOpcode() != ARM::t2LEApcrelJT
||
1425 LeaMI
->getOperand(0).getReg() != BaseReg
)
1431 unsigned Opc
= ByteOk
? ARM::t2TBB
: ARM::t2TBH
;
1432 MachineInstr
*NewJTMI
= BuildMI(MBB
, MI
->getDebugLoc(), TII
->get(Opc
))
1433 .addReg(IdxReg
, getKillRegState(IdxRegKill
))
1434 .addJumpTableIndex(JTI
, JTOP
.getTargetFlags())
1435 .addImm(MI
->getOperand(JTOpIdx
+1).getImm());
1436 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1437 // is 2-byte aligned. For now, asm printer will fix it up.
1438 unsigned NewSize
= TII
->GetInstSizeInBytes(NewJTMI
);
1439 unsigned OrigSize
= TII
->GetInstSizeInBytes(AddrMI
);
1440 OrigSize
+= TII
->GetInstSizeInBytes(LeaMI
);
1441 OrigSize
+= TII
->GetInstSizeInBytes(MI
);
1443 AddrMI
->eraseFromParent();
1444 LeaMI
->eraseFromParent();
1445 MI
->eraseFromParent();
1447 int delta
= OrigSize
- NewSize
;
1448 BBSizes
[MBB
->getNumber()] -= delta
;
1449 AdjustBBOffsetsAfter(MBB
, -delta
);