1 //===- MipsConstantIslandPass.cpp - Emit Pc Relative loads ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass is used to make Pc relative loads of constants.
10 // For now, only Mips16 will use this.
12 // Loading constants inline is expensive on Mips16 and it's in general better
13 // to place the constant nearby in code space and then it can be loaded with a
14 // simple 16 bit load instruction.
16 // The constants can be not just numbers but addresses of functions and labels.
17 // This can be particularly helpful in static relocation mode for embedded
20 //===----------------------------------------------------------------------===//
23 #include "Mips16InstrInfo.h"
24 #include "MipsMachineFunction.h"
25 #include "MipsSubtarget.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineConstantPool.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/Config/llvm-config.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DataLayout.h"
42 #include "llvm/IR/DebugLoc.h"
43 #include "llvm/IR/Function.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/Support/CommandLine.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/Debug.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/MathExtras.h"
51 #include "llvm/Support/raw_ostream.h"
60 #define DEBUG_TYPE "mips-constant-islands"
62 STATISTIC(NumCPEs
, "Number of constpool entries");
63 STATISTIC(NumSplit
, "Number of uncond branches inserted");
64 STATISTIC(NumCBrFixed
, "Number of cond branches fixed");
65 STATISTIC(NumUBrFixed
, "Number of uncond branches fixed");
67 // FIXME: This option should be removed once it has received sufficient testing.
69 AlignConstantIslands("mips-align-constant-islands", cl::Hidden
, cl::init(true),
70 cl::desc("Align constant islands in code"));
72 // Rather than do make check tests with huge amounts of code, we force
73 // the test to use this amount.
74 static cl::opt
<int> ConstantIslandsSmallOffset(
75 "mips-constant-islands-small-offset",
77 cl::desc("Make small offsets be this amount for testing purposes"),
80 // For testing purposes we tell it to not use relaxed load forms so that it
82 static cl::opt
<bool> NoLoadRelaxation(
83 "mips-constant-islands-no-load-relaxation",
85 cl::desc("Don't relax loads to long loads - for testing purposes"),
88 static unsigned int branchTargetOperand(MachineInstr
*MI
) {
89 switch (MI
->getOpcode()) {
98 case Mips::BeqzRxImm16
:
99 case Mips::BeqzRxImmX16
:
100 case Mips::BnezRxImm16
:
101 case Mips::BnezRxImmX16
:
104 llvm_unreachable("Unknown branch type");
107 static unsigned int longformBranchOpcode(unsigned int Opcode
) {
111 return Mips::BimmX16
;
114 return Mips::BteqzX16
;
117 return Mips::BtnezX16
;
120 case Mips::BeqzRxImm16
:
121 case Mips::BeqzRxImmX16
:
122 return Mips::BeqzRxImmX16
;
123 case Mips::BnezRxImm16
:
124 case Mips::BnezRxImmX16
:
125 return Mips::BnezRxImmX16
;
127 llvm_unreachable("Unknown branch type");
130 // FIXME: need to go through this whole constant islands port and check the math
131 // for branch ranges and clean this up and make some functions to calculate things
132 // that are done many times identically.
133 // Need to refactor some of the code to call this routine.
134 static unsigned int branchMaxOffsets(unsigned int Opcode
) {
135 unsigned Bits
, Scale
;
145 case Mips::BeqzRxImm16
:
149 case Mips::BeqzRxImmX16
:
153 case Mips::BnezRxImm16
:
157 case Mips::BnezRxImmX16
:
178 llvm_unreachable("Unknown branch type");
180 unsigned MaxOffs
= ((1 << (Bits
-1))-1) * Scale
;
186 using Iter
= MachineBasicBlock::iterator
;
187 using ReverseIter
= MachineBasicBlock::reverse_iterator
;
189 /// MipsConstantIslands - Due to limited PC-relative displacements, Mips
190 /// requires constant pool entries to be scattered among the instructions
191 /// inside a function. To do this, it completely ignores the normal LLVM
192 /// constant pool; instead, it places constants wherever it feels like with
193 /// special instructions.
195 /// The terminology used in this pass includes:
196 /// Islands - Clumps of constants placed in the function.
197 /// Water - Potential places where an island could be formed.
198 /// CPE - A constant pool entry that has been placed somewhere, which
199 /// tracks a list of users.
201 class MipsConstantIslands
: public MachineFunctionPass
{
202 /// BasicBlockInfo - Information about the offset and size of a single
204 struct BasicBlockInfo
{
205 /// Offset - Distance from the beginning of the function to the beginning
206 /// of this basic block.
208 /// Offsets are computed assuming worst case padding before an aligned
209 /// block. This means that subtracting basic block offsets always gives a
210 /// conservative estimate of the real distance which may be smaller.
212 /// Because worst case padding is used, the computed offset of an aligned
213 /// block may not actually be aligned.
216 /// Size - Size of the basic block in bytes. If the block contains
217 /// inline assembly, this is a worst case estimate.
219 /// The size does not include any alignment padding whether from the
220 /// beginning of the block, or from an aligned jump table at the end.
223 BasicBlockInfo() = default;
225 // FIXME: ignore LogAlign for this patch
227 unsigned postOffset(unsigned LogAlign
= 0) const {
228 unsigned PO
= Offset
+ Size
;
233 std::vector
<BasicBlockInfo
> BBInfo
;
235 /// WaterList - A sorted list of basic blocks where islands could be placed
236 /// (i.e. blocks that don't fall through to the following block, due
237 /// to a return, unreachable, or unconditional branch).
238 std::vector
<MachineBasicBlock
*> WaterList
;
240 /// NewWaterList - The subset of WaterList that was created since the
241 /// previous iteration by inserting unconditional branches.
242 SmallSet
<MachineBasicBlock
*, 4> NewWaterList
;
244 using water_iterator
= std::vector
<MachineBasicBlock
*>::iterator
;
246 /// CPUser - One user of a constant pool, keeping the machine instruction
247 /// pointer, the constant pool being referenced, and the max displacement
248 /// allowed from the instruction to the CP. The HighWaterMark records the
249 /// highest basic block where a new CPEntry can be placed. To ensure this
250 /// pass terminates, the CP entries are initially placed at the end of the
251 /// function and then move monotonically to lower addresses. The
252 /// exception to this rule is when the current CP entry for a particular
253 /// CPUser is out of range, but there is another CP entry for the same
254 /// constant value in range. We want to use the existing in-range CP
255 /// entry, but if it later moves out of range, the search for new water
256 /// should resume where it left off. The HighWaterMark is used to record
261 MachineBasicBlock
*HighWaterMark
;
265 unsigned LongFormMaxDisp
; // mips16 has 16/32 bit instructions
266 // with different displacements
267 unsigned LongFormOpcode
;
272 CPUser(MachineInstr
*mi
, MachineInstr
*cpemi
, unsigned maxdisp
,
274 unsigned longformmaxdisp
, unsigned longformopcode
)
275 : MI(mi
), CPEMI(cpemi
), MaxDisp(maxdisp
),
276 LongFormMaxDisp(longformmaxdisp
), LongFormOpcode(longformopcode
),
278 HighWaterMark
= CPEMI
->getParent();
281 /// getMaxDisp - Returns the maximum displacement supported by MI.
282 unsigned getMaxDisp() const {
283 unsigned xMaxDisp
= ConstantIslandsSmallOffset
?
284 ConstantIslandsSmallOffset
: MaxDisp
;
288 void setMaxDisp(unsigned val
) {
292 unsigned getLongFormMaxDisp() const {
293 return LongFormMaxDisp
;
296 unsigned getLongFormOpcode() const {
297 return LongFormOpcode
;
301 /// CPUsers - Keep track of all of the machine instructions that use various
302 /// constant pools and their max displacement.
303 std::vector
<CPUser
> CPUsers
;
305 /// CPEntry - One per constant pool entry, keeping the machine instruction
306 /// pointer, the constpool index, and the number of CPUser's which
307 /// reference this entry.
313 CPEntry(MachineInstr
*cpemi
, unsigned cpi
, unsigned rc
= 0)
314 : CPEMI(cpemi
), CPI(cpi
), RefCount(rc
) {}
317 /// CPEntries - Keep track of all of the constant pool entry machine
318 /// instructions. For each original constpool index (i.e. those that
319 /// existed upon entry to this pass), it keeps a vector of entries.
320 /// Original elements are cloned as we go along; the clones are
321 /// put in the vector of the original element, but have distinct CPIs.
322 std::vector
<std::vector
<CPEntry
>> CPEntries
;
324 /// ImmBranch - One per immediate branch, keeping the machine instruction
325 /// pointer, conditional or unconditional, the max displacement,
326 /// and (if isCond is true) the corresponding unconditional branch
330 unsigned MaxDisp
: 31;
334 ImmBranch(MachineInstr
*mi
, unsigned maxdisp
, bool cond
, int ubr
)
335 : MI(mi
), MaxDisp(maxdisp
), isCond(cond
), UncondBr(ubr
) {}
338 /// ImmBranches - Keep track of all the immediate branch instructions.
340 std::vector
<ImmBranch
> ImmBranches
;
342 /// HasFarJump - True if any far jump instruction has been emitted during
343 /// the branch fix up pass.
346 const MipsSubtarget
*STI
= nullptr;
347 const Mips16InstrInfo
*TII
;
348 MipsFunctionInfo
*MFI
;
349 MachineFunction
*MF
= nullptr;
350 MachineConstantPool
*MCP
= nullptr;
352 unsigned PICLabelUId
;
353 bool PrescannedForConstants
= false;
355 void initPICLabelUId(unsigned UId
) {
359 unsigned createPICLabelUId() {
360 return PICLabelUId
++;
366 MipsConstantIslands() : MachineFunctionPass(ID
) {}
368 StringRef
getPassName() const override
{ return "Mips Constant Islands"; }
370 bool runOnMachineFunction(MachineFunction
&F
) override
;
372 MachineFunctionProperties
getRequiredProperties() const override
{
373 return MachineFunctionProperties().set(
374 MachineFunctionProperties::Property::NoVRegs
);
377 void doInitialPlacement(std::vector
<MachineInstr
*> &CPEMIs
);
378 CPEntry
*findConstPoolEntry(unsigned CPI
, const MachineInstr
*CPEMI
);
379 unsigned getCPELogAlign(const MachineInstr
&CPEMI
);
380 void initializeFunctionInfo(const std::vector
<MachineInstr
*> &CPEMIs
);
381 unsigned getOffsetOf(MachineInstr
*MI
) const;
382 unsigned getUserOffset(CPUser
&) const;
385 bool isOffsetInRange(unsigned UserOffset
, unsigned TrialOffset
,
386 unsigned Disp
, bool NegativeOK
);
387 bool isOffsetInRange(unsigned UserOffset
, unsigned TrialOffset
,
390 void computeBlockSize(MachineBasicBlock
*MBB
);
391 MachineBasicBlock
*splitBlockBeforeInstr(MachineInstr
&MI
);
392 void updateForInsertedWaterBlock(MachineBasicBlock
*NewBB
);
393 void adjustBBOffsetsAfter(MachineBasicBlock
*BB
);
394 bool decrementCPEReferenceCount(unsigned CPI
, MachineInstr
* CPEMI
);
395 int findInRangeCPEntry(CPUser
& U
, unsigned UserOffset
);
396 int findLongFormInRangeCPEntry(CPUser
& U
, unsigned UserOffset
);
397 bool findAvailableWater(CPUser
&U
, unsigned UserOffset
,
398 water_iterator
&WaterIter
);
399 void createNewWater(unsigned CPUserIndex
, unsigned UserOffset
,
400 MachineBasicBlock
*&NewMBB
);
401 bool handleConstantPoolUser(unsigned CPUserIndex
);
402 void removeDeadCPEMI(MachineInstr
*CPEMI
);
403 bool removeUnusedCPEntries();
404 bool isCPEntryInRange(MachineInstr
*MI
, unsigned UserOffset
,
405 MachineInstr
*CPEMI
, unsigned Disp
, bool NegOk
,
406 bool DoDump
= false);
407 bool isWaterInRange(unsigned UserOffset
, MachineBasicBlock
*Water
,
408 CPUser
&U
, unsigned &Growth
);
409 bool isBBInRange(MachineInstr
*MI
, MachineBasicBlock
*BB
, unsigned Disp
);
410 bool fixupImmediateBr(ImmBranch
&Br
);
411 bool fixupConditionalBr(ImmBranch
&Br
);
412 bool fixupUnconditionalBr(ImmBranch
&Br
);
414 void prescanForConstants();
417 } // end anonymous namespace
419 char MipsConstantIslands::ID
= 0;
421 bool MipsConstantIslands::isOffsetInRange
422 (unsigned UserOffset
, unsigned TrialOffset
,
424 return isOffsetInRange(UserOffset
, TrialOffset
,
425 U
.getMaxDisp(), U
.NegOk
);
428 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
429 /// print block size and offset information - debugging
430 LLVM_DUMP_METHOD
void MipsConstantIslands::dumpBBs() {
431 for (unsigned J
= 0, E
= BBInfo
.size(); J
!=E
; ++J
) {
432 const BasicBlockInfo
&BBI
= BBInfo
[J
];
433 dbgs() << format("%08x %bb.%u\t", BBI
.Offset
, J
)
434 << format(" size=%#x\n", BBInfo
[J
].Size
);
439 bool MipsConstantIslands::runOnMachineFunction(MachineFunction
&mf
) {
440 // The intention is for this to be a mips16 only pass for now
443 MCP
= mf
.getConstantPool();
444 STI
= &static_cast<const MipsSubtarget
&>(mf
.getSubtarget());
445 LLVM_DEBUG(dbgs() << "constant island machine function "
447 if (!STI
->inMips16Mode() || !MipsSubtarget::useConstantIslands()) {
450 TII
= (const Mips16InstrInfo
*)STI
->getInstrInfo();
451 MFI
= MF
->getInfo
<MipsFunctionInfo
>();
452 LLVM_DEBUG(dbgs() << "constant island processing "
455 // will need to make predermination if there is any constants we need to
456 // put in constant islands. TBD.
458 if (!PrescannedForConstants
) prescanForConstants();
461 // This pass invalidates liveness information when it splits basic blocks.
462 MF
->getRegInfo().invalidateLiveness();
464 // Renumber all of the machine basic blocks in the function, guaranteeing that
465 // the numbers agree with the position of the block in the function.
466 MF
->RenumberBlocks();
468 bool MadeChange
= false;
470 // Perform the initial placement of the constant pool entries. To start with,
471 // we put them all at the end of the function.
472 std::vector
<MachineInstr
*> CPEMIs
;
474 doInitialPlacement(CPEMIs
);
476 /// The next UID to take is the first unused one.
477 initPICLabelUId(CPEMIs
.size());
479 // Do the initial scan of the function, building up information about the
480 // sizes of each block, the location of all the water, and finding all of the
481 // constant pool users.
482 initializeFunctionInfo(CPEMIs
);
484 LLVM_DEBUG(dumpBBs());
486 /// Remove dead constant pool entries.
487 MadeChange
|= removeUnusedCPEntries();
489 // Iteratively place constant pool entries and fix up branches until there
491 unsigned NoCPIters
= 0, NoBRIters
= 0;
494 LLVM_DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters
<< '\n');
495 bool CPChange
= false;
496 for (unsigned i
= 0, e
= CPUsers
.size(); i
!= e
; ++i
)
497 CPChange
|= handleConstantPoolUser(i
);
498 if (CPChange
&& ++NoCPIters
> 30)
499 report_fatal_error("Constant Island pass failed to converge!");
500 LLVM_DEBUG(dumpBBs());
502 // Clear NewWaterList now. If we split a block for branches, it should
503 // appear as "new water" for the next iteration of constant pool placement.
504 NewWaterList
.clear();
506 LLVM_DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters
<< '\n');
507 bool BRChange
= false;
508 for (unsigned i
= 0, e
= ImmBranches
.size(); i
!= e
; ++i
)
509 BRChange
|= fixupImmediateBr(ImmBranches
[i
]);
510 if (BRChange
&& ++NoBRIters
> 30)
511 report_fatal_error("Branch Fix Up pass failed to converge!");
512 LLVM_DEBUG(dumpBBs());
513 if (!CPChange
&& !BRChange
)
518 LLVM_DEBUG(dbgs() << '\n'; dumpBBs());
528 /// doInitialPlacement - Perform the initial placement of the constant pool
529 /// entries. To start with, we put them all at the end of the function.
531 MipsConstantIslands::doInitialPlacement(std::vector
<MachineInstr
*> &CPEMIs
) {
532 // Create the basic block to hold the CPE's.
533 MachineBasicBlock
*BB
= MF
->CreateMachineBasicBlock();
536 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
537 unsigned MaxLogAlign
= Log2_32(MCP
->getConstantPoolAlignment());
539 // Mark the basic block as required by the const-pool.
540 // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
541 BB
->setLogAlignment(AlignConstantIslands
? MaxLogAlign
: 2);
543 // The function needs to be as aligned as the basic blocks. The linker may
544 // move functions around based on their alignment.
545 MF
->ensureAlignment(BB
->getAlignment());
547 // Order the entries in BB by descending alignment. That ensures correct
548 // alignment of all entries as long as BB is sufficiently aligned. Keep
549 // track of the insertion point for each alignment. We are going to bucket
550 // sort the entries as they are created.
551 SmallVector
<MachineBasicBlock::iterator
, 8> InsPoint(MaxLogAlign
+ 1,
554 // Add all of the constants from the constant pool to the end block, use an
555 // identity mapping of CPI's to CPE's.
556 const std::vector
<MachineConstantPoolEntry
> &CPs
= MCP
->getConstants();
558 const DataLayout
&TD
= MF
->getDataLayout();
559 for (unsigned i
= 0, e
= CPs
.size(); i
!= e
; ++i
) {
560 unsigned Size
= TD
.getTypeAllocSize(CPs
[i
].getType());
561 assert(Size
>= 4 && "Too small constant pool entry");
562 unsigned Align
= CPs
[i
].getAlignment();
563 assert(isPowerOf2_32(Align
) && "Invalid alignment");
564 // Verify that all constant pool entries are a multiple of their alignment.
565 // If not, we would have to pad them out so that instructions stay aligned.
566 assert((Size
% Align
) == 0 && "CP Entry not multiple of 4 bytes!");
568 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
569 unsigned LogAlign
= Log2_32(Align
);
570 MachineBasicBlock::iterator InsAt
= InsPoint
[LogAlign
];
572 MachineInstr
*CPEMI
=
573 BuildMI(*BB
, InsAt
, DebugLoc(), TII
->get(Mips::CONSTPOOL_ENTRY
))
574 .addImm(i
).addConstantPoolIndex(i
).addImm(Size
);
576 CPEMIs
.push_back(CPEMI
);
578 // Ensure that future entries with higher alignment get inserted before
579 // CPEMI. This is bucket sort with iterators.
580 for (unsigned a
= LogAlign
+ 1; a
<= MaxLogAlign
; ++a
)
581 if (InsPoint
[a
] == InsAt
)
583 // Add a new CPEntry, but no corresponding CPUser yet.
584 CPEntries
.emplace_back(1, CPEntry(CPEMI
, i
));
586 LLVM_DEBUG(dbgs() << "Moved CPI#" << i
<< " to end of function, size = "
587 << Size
<< ", align = " << Align
<< '\n');
589 LLVM_DEBUG(BB
->dump());
592 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
593 /// into the block immediately after it.
594 static bool BBHasFallthrough(MachineBasicBlock
*MBB
) {
595 // Get the next machine basic block in the function.
596 MachineFunction::iterator MBBI
= MBB
->getIterator();
597 // Can't fall off end of function.
598 if (std::next(MBBI
) == MBB
->getParent()->end())
601 MachineBasicBlock
*NextBB
= &*std::next(MBBI
);
602 for (MachineBasicBlock::succ_iterator I
= MBB
->succ_begin(),
603 E
= MBB
->succ_end(); I
!= E
; ++I
)
610 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
611 /// look up the corresponding CPEntry.
612 MipsConstantIslands::CPEntry
613 *MipsConstantIslands::findConstPoolEntry(unsigned CPI
,
614 const MachineInstr
*CPEMI
) {
615 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
616 // Number of entries per constpool index should be small, just do a
618 for (unsigned i
= 0, e
= CPEs
.size(); i
!= e
; ++i
) {
619 if (CPEs
[i
].CPEMI
== CPEMI
)
625 /// getCPELogAlign - Returns the required alignment of the constant pool entry
626 /// represented by CPEMI. Alignment is measured in log2(bytes) units.
627 unsigned MipsConstantIslands::getCPELogAlign(const MachineInstr
&CPEMI
) {
628 assert(CPEMI
.getOpcode() == Mips::CONSTPOOL_ENTRY
);
630 // Everything is 4-byte aligned unless AlignConstantIslands is set.
631 if (!AlignConstantIslands
)
634 unsigned CPI
= CPEMI
.getOperand(1).getIndex();
635 assert(CPI
< MCP
->getConstants().size() && "Invalid constant pool index.");
636 unsigned Align
= MCP
->getConstants()[CPI
].getAlignment();
637 assert(isPowerOf2_32(Align
) && "Invalid CPE alignment");
638 return Log2_32(Align
);
641 /// initializeFunctionInfo - Do the initial scan of the function, building up
642 /// information about the sizes of each block, the location of all the water,
643 /// and finding all of the constant pool users.
644 void MipsConstantIslands::
645 initializeFunctionInfo(const std::vector
<MachineInstr
*> &CPEMIs
) {
647 BBInfo
.resize(MF
->getNumBlockIDs());
649 // First thing, compute the size of all basic blocks, and see if the function
650 // has any inline assembly in it. If so, we have to be conservative about
651 // alignment assumptions, as we don't know for sure the size of any
652 // instructions in the inline assembly.
653 for (MachineFunction::iterator I
= MF
->begin(), E
= MF
->end(); I
!= E
; ++I
)
654 computeBlockSize(&*I
);
656 // Compute block offsets.
657 adjustBBOffsetsAfter(&MF
->front());
659 // Now go back through the instructions and build up our data structures.
660 for (MachineBasicBlock
&MBB
: *MF
) {
661 // If this block doesn't fall through into the next MBB, then this is
662 // 'water' that a constant pool island could be placed.
663 if (!BBHasFallthrough(&MBB
))
664 WaterList
.push_back(&MBB
);
665 for (MachineInstr
&MI
: MBB
) {
666 if (MI
.isDebugInstr())
669 int Opc
= MI
.getOpcode();
677 continue; // Ignore other branches for now
688 case Mips::BeqzRxImm16
:
694 case Mips::BeqzRxImmX16
:
700 case Mips::BnezRxImm16
:
706 case Mips::BnezRxImmX16
:
737 // Record this immediate branch.
738 unsigned MaxOffs
= ((1 << (Bits
-1))-1) * Scale
;
739 ImmBranches
.push_back(ImmBranch(&MI
, MaxOffs
, isCond
, UOpc
));
742 if (Opc
== Mips::CONSTPOOL_ENTRY
)
745 // Scan the instructions for constant pool operands.
746 for (unsigned op
= 0, e
= MI
.getNumOperands(); op
!= e
; ++op
)
747 if (MI
.getOperand(op
).isCPI()) {
748 // We found one. The addressing mode tells us the max displacement
749 // from the PC that this instruction permits.
751 // Basic size info comes from the TSFlags field.
755 unsigned LongFormBits
= 0;
756 unsigned LongFormScale
= 0;
757 unsigned LongFormOpcode
= 0;
760 llvm_unreachable("Unknown addressing mode for CP reference!");
761 case Mips::LwRxPcTcp16
:
764 LongFormOpcode
= Mips::LwRxPcTcpX16
;
768 case Mips::LwRxPcTcpX16
:
774 // Remember that this is a user of a CP entry.
775 unsigned CPI
= MI
.getOperand(op
).getIndex();
776 MachineInstr
*CPEMI
= CPEMIs
[CPI
];
777 unsigned MaxOffs
= ((1 << Bits
)-1) * Scale
;
778 unsigned LongFormMaxOffs
= ((1 << LongFormBits
)-1) * LongFormScale
;
779 CPUsers
.push_back(CPUser(&MI
, CPEMI
, MaxOffs
, NegOk
, LongFormMaxOffs
,
782 // Increment corresponding CPEntry reference count.
783 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
784 assert(CPE
&& "Cannot find a corresponding CPEntry!");
787 // Instructions can only use one CP entry, don't bother scanning the
788 // rest of the operands.
795 /// computeBlockSize - Compute the size and some alignment information for MBB.
796 /// This function updates BBInfo directly.
797 void MipsConstantIslands::computeBlockSize(MachineBasicBlock
*MBB
) {
798 BasicBlockInfo
&BBI
= BBInfo
[MBB
->getNumber()];
801 for (const MachineInstr
&MI
: *MBB
)
802 BBI
.Size
+= TII
->getInstSizeInBytes(MI
);
805 /// getOffsetOf - Return the current offset of the specified machine instruction
806 /// from the start of the function. This offset changes as stuff is moved
807 /// around inside the function.
808 unsigned MipsConstantIslands::getOffsetOf(MachineInstr
*MI
) const {
809 MachineBasicBlock
*MBB
= MI
->getParent();
811 // The offset is composed of two things: the sum of the sizes of all MBB's
812 // before this instruction's block, and the offset from the start of the block
814 unsigned Offset
= BBInfo
[MBB
->getNumber()].Offset
;
816 // Sum instructions before MI in MBB.
817 for (MachineBasicBlock::iterator I
= MBB
->begin(); &*I
!= MI
; ++I
) {
818 assert(I
!= MBB
->end() && "Didn't find MI in its own basic block?");
819 Offset
+= TII
->getInstSizeInBytes(*I
);
824 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
826 static bool CompareMBBNumbers(const MachineBasicBlock
*LHS
,
827 const MachineBasicBlock
*RHS
) {
828 return LHS
->getNumber() < RHS
->getNumber();
831 /// updateForInsertedWaterBlock - When a block is newly inserted into the
832 /// machine function, it upsets all of the block numbers. Renumber the blocks
833 /// and update the arrays that parallel this numbering.
834 void MipsConstantIslands::updateForInsertedWaterBlock
835 (MachineBasicBlock
*NewBB
) {
836 // Renumber the MBB's to keep them consecutive.
837 NewBB
->getParent()->RenumberBlocks(NewBB
);
839 // Insert an entry into BBInfo to align it properly with the (newly
840 // renumbered) block numbers.
841 BBInfo
.insert(BBInfo
.begin() + NewBB
->getNumber(), BasicBlockInfo());
843 // Next, update WaterList. Specifically, we need to add NewMBB as having
844 // available water after it.
845 water_iterator IP
= llvm::lower_bound(WaterList
, NewBB
, CompareMBBNumbers
);
846 WaterList
.insert(IP
, NewBB
);
849 unsigned MipsConstantIslands::getUserOffset(CPUser
&U
) const {
850 return getOffsetOf(U
.MI
);
853 /// Split the basic block containing MI into two blocks, which are joined by
854 /// an unconditional branch. Update data structures and renumber blocks to
855 /// account for this change and returns the newly created block.
857 MipsConstantIslands::splitBlockBeforeInstr(MachineInstr
&MI
) {
858 MachineBasicBlock
*OrigBB
= MI
.getParent();
860 // Create a new MBB for the code after the OrigBB.
861 MachineBasicBlock
*NewBB
=
862 MF
->CreateMachineBasicBlock(OrigBB
->getBasicBlock());
863 MachineFunction::iterator MBBI
= ++OrigBB
->getIterator();
864 MF
->insert(MBBI
, NewBB
);
866 // Splice the instructions starting with MI over to NewBB.
867 NewBB
->splice(NewBB
->end(), OrigBB
, MI
, OrigBB
->end());
869 // Add an unconditional branch from OrigBB to NewBB.
870 // Note the new unconditional branch is not being recorded.
871 // There doesn't seem to be meaningful DebugInfo available; this doesn't
872 // correspond to anything in the source.
873 BuildMI(OrigBB
, DebugLoc(), TII
->get(Mips::Bimm16
)).addMBB(NewBB
);
876 // Update the CFG. All succs of OrigBB are now succs of NewBB.
877 NewBB
->transferSuccessors(OrigBB
);
879 // OrigBB branches to NewBB.
880 OrigBB
->addSuccessor(NewBB
);
882 // Update internal data structures to account for the newly inserted MBB.
883 // This is almost the same as updateForInsertedWaterBlock, except that
884 // the Water goes after OrigBB, not NewBB.
885 MF
->RenumberBlocks(NewBB
);
887 // Insert an entry into BBInfo to align it properly with the (newly
888 // renumbered) block numbers.
889 BBInfo
.insert(BBInfo
.begin() + NewBB
->getNumber(), BasicBlockInfo());
891 // Next, update WaterList. Specifically, we need to add OrigMBB as having
892 // available water after it (but not if it's already there, which happens
893 // when splitting before a conditional branch that is followed by an
894 // unconditional branch - in that case we want to insert NewBB).
895 water_iterator IP
= llvm::lower_bound(WaterList
, OrigBB
, CompareMBBNumbers
);
896 MachineBasicBlock
* WaterBB
= *IP
;
897 if (WaterBB
== OrigBB
)
898 WaterList
.insert(std::next(IP
), NewBB
);
900 WaterList
.insert(IP
, OrigBB
);
901 NewWaterList
.insert(OrigBB
);
903 // Figure out how large the OrigBB is. As the first half of the original
904 // block, it cannot contain a tablejump. The size includes
905 // the new jump we added. (It should be possible to do this without
906 // recounting everything, but it's very confusing, and this is rarely
908 computeBlockSize(OrigBB
);
910 // Figure out how large the NewMBB is. As the second half of the original
911 // block, it may contain a tablejump.
912 computeBlockSize(NewBB
);
914 // All BBOffsets following these blocks must be modified.
915 adjustBBOffsetsAfter(OrigBB
);
920 /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
921 /// reference) is within MaxDisp of TrialOffset (a proposed location of a
922 /// constant pool entry).
923 bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset
,
924 unsigned TrialOffset
, unsigned MaxDisp
,
926 if (UserOffset
<= TrialOffset
) {
927 // User before the Trial.
928 if (TrialOffset
- UserOffset
<= MaxDisp
)
930 } else if (NegativeOK
) {
931 if (UserOffset
- TrialOffset
<= MaxDisp
)
937 /// isWaterInRange - Returns true if a CPE placed after the specified
938 /// Water (a basic block) will be in range for the specific MI.
940 /// Compute how much the function will grow by inserting a CPE after Water.
941 bool MipsConstantIslands::isWaterInRange(unsigned UserOffset
,
942 MachineBasicBlock
* Water
, CPUser
&U
,
944 unsigned CPELogAlign
= getCPELogAlign(*U
.CPEMI
);
945 unsigned CPEOffset
= BBInfo
[Water
->getNumber()].postOffset(CPELogAlign
);
946 unsigned NextBlockOffset
;
947 llvm::Align NextBlockAlignment
;
948 MachineFunction::const_iterator NextBlock
= ++Water
->getIterator();
949 if (NextBlock
== MF
->end()) {
950 NextBlockOffset
= BBInfo
[Water
->getNumber()].postOffset();
951 NextBlockAlignment
= llvm::Align::None();
953 NextBlockOffset
= BBInfo
[NextBlock
->getNumber()].Offset
;
954 NextBlockAlignment
= NextBlock
->getAlignment();
956 unsigned Size
= U
.CPEMI
->getOperand(2).getImm();
957 unsigned CPEEnd
= CPEOffset
+ Size
;
959 // The CPE may be able to hide in the alignment padding before the next
960 // block. It may also cause more padding to be required if it is more aligned
961 // that the next block.
962 if (CPEEnd
> NextBlockOffset
) {
963 Growth
= CPEEnd
- NextBlockOffset
;
964 // Compute the padding that would go at the end of the CPE to align the next
966 Growth
+= offsetToAlignment(CPEEnd
, NextBlockAlignment
);
968 // If the CPE is to be inserted before the instruction, that will raise
969 // the offset of the instruction. Also account for unknown alignment padding
970 // in blocks between CPE and the user.
971 if (CPEOffset
< UserOffset
)
972 UserOffset
+= Growth
;
974 // CPE fits in existing padding.
977 return isOffsetInRange(UserOffset
, CPEOffset
, U
);
980 /// isCPEntryInRange - Returns true if the distance between specific MI and
981 /// specific ConstPool entry instruction can fit in MI's displacement field.
982 bool MipsConstantIslands::isCPEntryInRange
983 (MachineInstr
*MI
, unsigned UserOffset
,
984 MachineInstr
*CPEMI
, unsigned MaxDisp
,
985 bool NegOk
, bool DoDump
) {
986 unsigned CPEOffset
= getOffsetOf(CPEMI
);
990 unsigned Block
= MI
->getParent()->getNumber();
991 const BasicBlockInfo
&BBI
= BBInfo
[Block
];
992 dbgs() << "User of CPE#" << CPEMI
->getOperand(0).getImm()
993 << " max delta=" << MaxDisp
994 << format(" insn address=%#x", UserOffset
) << " in "
995 << printMBBReference(*MI
->getParent()) << ": "
996 << format("%#x-%x\t", BBI
.Offset
, BBI
.postOffset()) << *MI
997 << format("CPE address=%#x offset=%+d: ", CPEOffset
,
998 int(CPEOffset
- UserOffset
));
1002 return isOffsetInRange(UserOffset
, CPEOffset
, MaxDisp
, NegOk
);
1006 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
1007 /// unconditionally branches to its only successor.
1008 static bool BBIsJumpedOver(MachineBasicBlock
*MBB
) {
1009 if (MBB
->pred_size() != 1 || MBB
->succ_size() != 1)
1011 MachineBasicBlock
*Succ
= *MBB
->succ_begin();
1012 MachineBasicBlock
*Pred
= *MBB
->pred_begin();
1013 MachineInstr
*PredMI
= &Pred
->back();
1014 if (PredMI
->getOpcode() == Mips::Bimm16
)
1015 return PredMI
->getOperand(0).getMBB() == Succ
;
1020 void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock
*BB
) {
1021 unsigned BBNum
= BB
->getNumber();
1022 for(unsigned i
= BBNum
+ 1, e
= MF
->getNumBlockIDs(); i
< e
; ++i
) {
1023 // Get the offset and known bits at the end of the layout predecessor.
1024 // Include the alignment of the current block.
1025 unsigned Offset
= BBInfo
[i
- 1].Offset
+ BBInfo
[i
- 1].Size
;
1026 BBInfo
[i
].Offset
= Offset
;
1030 /// decrementCPEReferenceCount - find the constant pool entry with index CPI
1031 /// and instruction CPEMI, and decrement its refcount. If the refcount
1032 /// becomes 0 remove the entry and instruction. Returns true if we removed
1033 /// the entry, false if we didn't.
1034 bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI
,
1035 MachineInstr
*CPEMI
) {
1036 // Find the old entry. Eliminate it if it is no longer used.
1037 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
1038 assert(CPE
&& "Unexpected!");
1039 if (--CPE
->RefCount
== 0) {
1040 removeDeadCPEMI(CPEMI
);
1041 CPE
->CPEMI
= nullptr;
1048 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1049 /// if not, see if an in-range clone of the CPE is in range, and if so,
1050 /// change the data structures so the user references the clone. Returns:
1051 /// 0 = no existing entry found
1052 /// 1 = entry found, and there were no code insertions or deletions
1053 /// 2 = entry found, and there were code insertions or deletions
1054 int MipsConstantIslands::findInRangeCPEntry(CPUser
& U
, unsigned UserOffset
)
1056 MachineInstr
*UserMI
= U
.MI
;
1057 MachineInstr
*CPEMI
= U
.CPEMI
;
1059 // Check to see if the CPE is already in-range.
1060 if (isCPEntryInRange(UserMI
, UserOffset
, CPEMI
, U
.getMaxDisp(), U
.NegOk
,
1062 LLVM_DEBUG(dbgs() << "In range\n");
1066 // No. Look for previously created clones of the CPE that are in range.
1067 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1068 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
1069 for (unsigned i
= 0, e
= CPEs
.size(); i
!= e
; ++i
) {
1070 // We already tried this one
1071 if (CPEs
[i
].CPEMI
== CPEMI
)
1073 // Removing CPEs can leave empty entries, skip
1074 if (CPEs
[i
].CPEMI
== nullptr)
1076 if (isCPEntryInRange(UserMI
, UserOffset
, CPEs
[i
].CPEMI
, U
.getMaxDisp(),
1078 LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI
<< " with CPE#"
1079 << CPEs
[i
].CPI
<< "\n");
1080 // Point the CPUser node to the replacement
1081 U
.CPEMI
= CPEs
[i
].CPEMI
;
1082 // Change the CPI in the instruction operand to refer to the clone.
1083 for (unsigned j
= 0, e
= UserMI
->getNumOperands(); j
!= e
; ++j
)
1084 if (UserMI
->getOperand(j
).isCPI()) {
1085 UserMI
->getOperand(j
).setIndex(CPEs
[i
].CPI
);
1088 // Adjust the refcount of the clone...
1090 // ...and the original. If we didn't remove the old entry, none of the
1091 // addresses changed, so we don't need another pass.
1092 return decrementCPEReferenceCount(CPI
, CPEMI
) ? 2 : 1;
1098 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1099 /// This version checks if the longer form of the instruction can be used to
1100 /// to satisfy things.
1101 /// if not, see if an in-range clone of the CPE is in range, and if so,
1102 /// change the data structures so the user references the clone. Returns:
1103 /// 0 = no existing entry found
1104 /// 1 = entry found, and there were no code insertions or deletions
1105 /// 2 = entry found, and there were code insertions or deletions
1106 int MipsConstantIslands::findLongFormInRangeCPEntry
1107 (CPUser
& U
, unsigned UserOffset
)
1109 MachineInstr
*UserMI
= U
.MI
;
1110 MachineInstr
*CPEMI
= U
.CPEMI
;
1112 // Check to see if the CPE is already in-range.
1113 if (isCPEntryInRange(UserMI
, UserOffset
, CPEMI
,
1114 U
.getLongFormMaxDisp(), U
.NegOk
,
1116 LLVM_DEBUG(dbgs() << "In range\n");
1117 UserMI
->setDesc(TII
->get(U
.getLongFormOpcode()));
1118 U
.setMaxDisp(U
.getLongFormMaxDisp());
1119 return 2; // instruction is longer length now
1122 // No. Look for previously created clones of the CPE that are in range.
1123 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1124 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
1125 for (unsigned i
= 0, e
= CPEs
.size(); i
!= e
; ++i
) {
1126 // We already tried this one
1127 if (CPEs
[i
].CPEMI
== CPEMI
)
1129 // Removing CPEs can leave empty entries, skip
1130 if (CPEs
[i
].CPEMI
== nullptr)
1132 if (isCPEntryInRange(UserMI
, UserOffset
, CPEs
[i
].CPEMI
,
1133 U
.getLongFormMaxDisp(), U
.NegOk
)) {
1134 LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI
<< " with CPE#"
1135 << CPEs
[i
].CPI
<< "\n");
1136 // Point the CPUser node to the replacement
1137 U
.CPEMI
= CPEs
[i
].CPEMI
;
1138 // Change the CPI in the instruction operand to refer to the clone.
1139 for (unsigned j
= 0, e
= UserMI
->getNumOperands(); j
!= e
; ++j
)
1140 if (UserMI
->getOperand(j
).isCPI()) {
1141 UserMI
->getOperand(j
).setIndex(CPEs
[i
].CPI
);
1144 // Adjust the refcount of the clone...
1146 // ...and the original. If we didn't remove the old entry, none of the
1147 // addresses changed, so we don't need another pass.
1148 return decrementCPEReferenceCount(CPI
, CPEMI
) ? 2 : 1;
1154 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1155 /// the specific unconditional branch instruction.
1156 static inline unsigned getUnconditionalBrDisp(int Opc
) {
1159 return ((1<<10)-1)*2;
1161 return ((1<<16)-1)*2;
1165 return ((1<<16)-1)*2;
1168 /// findAvailableWater - Look for an existing entry in the WaterList in which
1169 /// we can place the CPE referenced from U so it's within range of U's MI.
1170 /// Returns true if found, false if not. If it returns true, WaterIter
1171 /// is set to the WaterList entry.
1172 /// To ensure that this pass
1173 /// terminates, the CPE location for a particular CPUser is only allowed to
1174 /// move to a lower address, so search backward from the end of the list and
1175 /// prefer the first water that is in range.
1176 bool MipsConstantIslands::findAvailableWater(CPUser
&U
, unsigned UserOffset
,
1177 water_iterator
&WaterIter
) {
1178 if (WaterList
.empty())
1181 unsigned BestGrowth
= ~0u;
1182 for (water_iterator IP
= std::prev(WaterList
.end()), B
= WaterList
.begin();;
1184 MachineBasicBlock
* WaterBB
= *IP
;
1185 // Check if water is in range and is either at a lower address than the
1186 // current "high water mark" or a new water block that was created since
1187 // the previous iteration by inserting an unconditional branch. In the
1188 // latter case, we want to allow resetting the high water mark back to
1189 // this new water since we haven't seen it before. Inserting branches
1190 // should be relatively uncommon and when it does happen, we want to be
1191 // sure to take advantage of it for all the CPEs near that block, so that
1192 // we don't insert more branches than necessary.
1194 if (isWaterInRange(UserOffset
, WaterBB
, U
, Growth
) &&
1195 (WaterBB
->getNumber() < U
.HighWaterMark
->getNumber() ||
1196 NewWaterList
.count(WaterBB
)) && Growth
< BestGrowth
) {
1197 // This is the least amount of required padding seen so far.
1198 BestGrowth
= Growth
;
1200 LLVM_DEBUG(dbgs() << "Found water after " << printMBBReference(*WaterBB
)
1201 << " Growth=" << Growth
<< '\n');
1203 // Keep looking unless it is perfect.
1204 if (BestGrowth
== 0)
1210 return BestGrowth
!= ~0u;
1213 /// createNewWater - No existing WaterList entry will work for
1214 /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1215 /// block is used if in range, and the conditional branch munged so control
1216 /// flow is correct. Otherwise the block is split to create a hole with an
1217 /// unconditional branch around it. In either case NewMBB is set to a
1218 /// block following which the new island can be inserted (the WaterList
1219 /// is not adjusted).
1220 void MipsConstantIslands::createNewWater(unsigned CPUserIndex
,
1221 unsigned UserOffset
,
1222 MachineBasicBlock
*&NewMBB
) {
1223 CPUser
&U
= CPUsers
[CPUserIndex
];
1224 MachineInstr
*UserMI
= U
.MI
;
1225 MachineInstr
*CPEMI
= U
.CPEMI
;
1226 unsigned CPELogAlign
= getCPELogAlign(*CPEMI
);
1227 MachineBasicBlock
*UserMBB
= UserMI
->getParent();
1228 const BasicBlockInfo
&UserBBI
= BBInfo
[UserMBB
->getNumber()];
1230 // If the block does not end in an unconditional branch already, and if the
1231 // end of the block is within range, make new water there.
1232 if (BBHasFallthrough(UserMBB
)) {
1233 // Size of branch to insert.
1235 // Compute the offset where the CPE will begin.
1236 unsigned CPEOffset
= UserBBI
.postOffset(CPELogAlign
) + Delta
;
1238 if (isOffsetInRange(UserOffset
, CPEOffset
, U
)) {
1239 LLVM_DEBUG(dbgs() << "Split at end of " << printMBBReference(*UserMBB
)
1240 << format(", expected CPE offset %#x\n", CPEOffset
));
1241 NewMBB
= &*++UserMBB
->getIterator();
1242 // Add an unconditional branch from UserMBB to fallthrough block. Record
1243 // it for branch lengthening; this new branch will not get out of range,
1244 // but if the preceding conditional branch is out of range, the targets
1245 // will be exchanged, and the altered branch may be out of range, so the
1246 // machinery has to know about it.
1247 int UncondBr
= Mips::Bimm16
;
1248 BuildMI(UserMBB
, DebugLoc(), TII
->get(UncondBr
)).addMBB(NewMBB
);
1249 unsigned MaxDisp
= getUnconditionalBrDisp(UncondBr
);
1250 ImmBranches
.push_back(ImmBranch(&UserMBB
->back(),
1251 MaxDisp
, false, UncondBr
));
1252 BBInfo
[UserMBB
->getNumber()].Size
+= Delta
;
1253 adjustBBOffsetsAfter(UserMBB
);
1258 // What a big block. Find a place within the block to split it.
1260 // Try to split the block so it's fully aligned. Compute the latest split
1261 // point where we can add a 4-byte branch instruction, and then align to
1262 // LogAlign which is the largest possible alignment in the function.
1263 unsigned LogAlign
= Log2(MF
->getAlignment());
1264 assert(LogAlign
>= CPELogAlign
&& "Over-aligned constant pool entry");
1265 unsigned BaseInsertOffset
= UserOffset
+ U
.getMaxDisp();
1266 LLVM_DEBUG(dbgs() << format("Split in middle of big block before %#x",
1269 // The 4 in the following is for the unconditional branch we'll be inserting
1270 // Alignment of the island is handled
1271 // inside isOffsetInRange.
1272 BaseInsertOffset
-= 4;
1274 LLVM_DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset
)
1275 << " la=" << LogAlign
<< '\n');
1277 // This could point off the end of the block if we've already got constant
1278 // pool entries following this block; only the last one is in the water list.
1279 // Back past any possible branches (allow for a conditional and a maximally
1280 // long unconditional).
1281 if (BaseInsertOffset
+ 8 >= UserBBI
.postOffset()) {
1282 BaseInsertOffset
= UserBBI
.postOffset() - 8;
1283 LLVM_DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset
));
1285 unsigned EndInsertOffset
= BaseInsertOffset
+ 4 +
1286 CPEMI
->getOperand(2).getImm();
1287 MachineBasicBlock::iterator MI
= UserMI
;
1289 unsigned CPUIndex
= CPUserIndex
+1;
1290 unsigned NumCPUsers
= CPUsers
.size();
1291 //MachineInstr *LastIT = 0;
1292 for (unsigned Offset
= UserOffset
+ TII
->getInstSizeInBytes(*UserMI
);
1293 Offset
< BaseInsertOffset
;
1294 Offset
+= TII
->getInstSizeInBytes(*MI
), MI
= std::next(MI
)) {
1295 assert(MI
!= UserMBB
->end() && "Fell off end of block");
1296 if (CPUIndex
< NumCPUsers
&& CPUsers
[CPUIndex
].MI
== MI
) {
1297 CPUser
&U
= CPUsers
[CPUIndex
];
1298 if (!isOffsetInRange(Offset
, EndInsertOffset
, U
)) {
1299 // Shift intertion point by one unit of alignment so it is within reach.
1300 BaseInsertOffset
-= 1u << LogAlign
;
1301 EndInsertOffset
-= 1u << LogAlign
;
1303 // This is overly conservative, as we don't account for CPEMIs being
1304 // reused within the block, but it doesn't matter much. Also assume CPEs
1305 // are added in order with alignment padding. We may eventually be able
1306 // to pack the aligned CPEs better.
1307 EndInsertOffset
+= U
.CPEMI
->getOperand(2).getImm();
1312 NewMBB
= splitBlockBeforeInstr(*--MI
);
1315 /// handleConstantPoolUser - Analyze the specified user, checking to see if it
1316 /// is out-of-range. If so, pick up the constant pool value and move it some
1317 /// place in-range. Return true if we changed any addresses (thus must run
1318 /// another pass of branch lengthening), false otherwise.
1319 bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex
) {
1320 CPUser
&U
= CPUsers
[CPUserIndex
];
1321 MachineInstr
*UserMI
= U
.MI
;
1322 MachineInstr
*CPEMI
= U
.CPEMI
;
1323 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1324 unsigned Size
= CPEMI
->getOperand(2).getImm();
1325 // Compute this only once, it's expensive.
1326 unsigned UserOffset
= getUserOffset(U
);
1328 // See if the current entry is within range, or there is a clone of it
1330 int result
= findInRangeCPEntry(U
, UserOffset
);
1331 if (result
==1) return false;
1332 else if (result
==2) return true;
1334 // Look for water where we can place this CPE.
1335 MachineBasicBlock
*NewIsland
= MF
->CreateMachineBasicBlock();
1336 MachineBasicBlock
*NewMBB
;
1338 if (findAvailableWater(U
, UserOffset
, IP
)) {
1339 LLVM_DEBUG(dbgs() << "Found water in range\n");
1340 MachineBasicBlock
*WaterBB
= *IP
;
1342 // If the original WaterList entry was "new water" on this iteration,
1343 // propagate that to the new island. This is just keeping NewWaterList
1344 // updated to match the WaterList, which will be updated below.
1345 if (NewWaterList
.erase(WaterBB
))
1346 NewWaterList
.insert(NewIsland
);
1348 // The new CPE goes before the following block (NewMBB).
1349 NewMBB
= &*++WaterBB
->getIterator();
1352 // we first see if a longer form of the instrucion could have reached
1353 // the constant. in that case we won't bother to split
1354 if (!NoLoadRelaxation
) {
1355 result
= findLongFormInRangeCPEntry(U
, UserOffset
);
1356 if (result
!= 0) return true;
1358 LLVM_DEBUG(dbgs() << "No water found\n");
1359 createNewWater(CPUserIndex
, UserOffset
, NewMBB
);
1361 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1362 // called while handling branches so that the water will be seen on the
1363 // next iteration for constant pools, but in this context, we don't want
1364 // it. Check for this so it will be removed from the WaterList.
1365 // Also remove any entry from NewWaterList.
1366 MachineBasicBlock
*WaterBB
= &*--NewMBB
->getIterator();
1367 IP
= llvm::find(WaterList
, WaterBB
);
1368 if (IP
!= WaterList
.end())
1369 NewWaterList
.erase(WaterBB
);
1371 // We are adding new water. Update NewWaterList.
1372 NewWaterList
.insert(NewIsland
);
1375 // Remove the original WaterList entry; we want subsequent insertions in
1376 // this vicinity to go after the one we're about to insert. This
1377 // considerably reduces the number of times we have to move the same CPE
1378 // more than once and is also important to ensure the algorithm terminates.
1379 if (IP
!= WaterList
.end())
1380 WaterList
.erase(IP
);
1382 // Okay, we know we can put an island before NewMBB now, do it!
1383 MF
->insert(NewMBB
->getIterator(), NewIsland
);
1385 // Update internal data structures to account for the newly inserted MBB.
1386 updateForInsertedWaterBlock(NewIsland
);
1388 // Decrement the old entry, and remove it if refcount becomes 0.
1389 decrementCPEReferenceCount(CPI
, CPEMI
);
1391 // No existing clone of this CPE is within range.
1392 // We will be generating a new clone. Get a UID for it.
1393 unsigned ID
= createPICLabelUId();
1395 // Now that we have an island to add the CPE to, clone the original CPE and
1396 // add it to the island.
1397 U
.HighWaterMark
= NewIsland
;
1398 U
.CPEMI
= BuildMI(NewIsland
, DebugLoc(), TII
->get(Mips::CONSTPOOL_ENTRY
))
1399 .addImm(ID
).addConstantPoolIndex(CPI
).addImm(Size
);
1400 CPEntries
[CPI
].push_back(CPEntry(U
.CPEMI
, ID
, 1));
1403 // Mark the basic block as aligned as required by the const-pool entry.
1404 NewIsland
->setLogAlignment(getCPELogAlign(*U
.CPEMI
));
1406 // Increase the size of the island block to account for the new entry.
1407 BBInfo
[NewIsland
->getNumber()].Size
+= Size
;
1408 adjustBBOffsetsAfter(&*--NewIsland
->getIterator());
1410 // Finally, change the CPI in the instruction operand to be ID.
1411 for (unsigned i
= 0, e
= UserMI
->getNumOperands(); i
!= e
; ++i
)
1412 if (UserMI
->getOperand(i
).isCPI()) {
1413 UserMI
->getOperand(i
).setIndex(ID
);
1418 dbgs() << " Moved CPE to #" << ID
<< " CPI=" << CPI
1419 << format(" offset=%#x\n", BBInfo
[NewIsland
->getNumber()].Offset
));
1424 /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1425 /// sizes and offsets of impacted basic blocks.
1426 void MipsConstantIslands::removeDeadCPEMI(MachineInstr
*CPEMI
) {
1427 MachineBasicBlock
*CPEBB
= CPEMI
->getParent();
1428 unsigned Size
= CPEMI
->getOperand(2).getImm();
1429 CPEMI
->eraseFromParent();
1430 BBInfo
[CPEBB
->getNumber()].Size
-= Size
;
1431 // All succeeding offsets have the current size value added in, fix this.
1432 if (CPEBB
->empty()) {
1433 BBInfo
[CPEBB
->getNumber()].Size
= 0;
1435 // This block no longer needs to be aligned.
1436 CPEBB
->setLogAlignment(0);
1438 // Entries are sorted by descending alignment, so realign from the front.
1439 CPEBB
->setLogAlignment(getCPELogAlign(*CPEBB
->begin()));
1441 adjustBBOffsetsAfter(CPEBB
);
1442 // An island has only one predecessor BB and one successor BB. Check if
1443 // this BB's predecessor jumps directly to this BB's successor. This
1444 // shouldn't happen currently.
1445 assert(!BBIsJumpedOver(CPEBB
) && "How did this happen?");
1446 // FIXME: remove the empty blocks after all the work is done?
1449 /// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1451 bool MipsConstantIslands::removeUnusedCPEntries() {
1452 unsigned MadeChange
= false;
1453 for (unsigned i
= 0, e
= CPEntries
.size(); i
!= e
; ++i
) {
1454 std::vector
<CPEntry
> &CPEs
= CPEntries
[i
];
1455 for (unsigned j
= 0, ee
= CPEs
.size(); j
!= ee
; ++j
) {
1456 if (CPEs
[j
].RefCount
== 0 && CPEs
[j
].CPEMI
) {
1457 removeDeadCPEMI(CPEs
[j
].CPEMI
);
1458 CPEs
[j
].CPEMI
= nullptr;
1466 /// isBBInRange - Returns true if the distance between specific MI and
1467 /// specific BB can fit in MI's displacement field.
1468 bool MipsConstantIslands::isBBInRange
1469 (MachineInstr
*MI
,MachineBasicBlock
*DestBB
, unsigned MaxDisp
) {
1471 unsigned BrOffset
= getOffsetOf(MI
) + PCAdj
;
1472 unsigned DestOffset
= BBInfo
[DestBB
->getNumber()].Offset
;
1474 LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB
)
1475 << " from " << printMBBReference(*MI
->getParent())
1476 << " max delta=" << MaxDisp
<< " from " << getOffsetOf(MI
)
1477 << " to " << DestOffset
<< " offset "
1478 << int(DestOffset
- BrOffset
) << "\t" << *MI
);
1480 if (BrOffset
<= DestOffset
) {
1481 // Branch before the Dest.
1482 if (DestOffset
-BrOffset
<= MaxDisp
)
1485 if (BrOffset
-DestOffset
<= MaxDisp
)
1491 /// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1492 /// away to fit in its displacement field.
1493 bool MipsConstantIslands::fixupImmediateBr(ImmBranch
&Br
) {
1494 MachineInstr
*MI
= Br
.MI
;
1495 unsigned TargetOperand
= branchTargetOperand(MI
);
1496 MachineBasicBlock
*DestBB
= MI
->getOperand(TargetOperand
).getMBB();
1498 // Check to see if the DestBB is already in-range.
1499 if (isBBInRange(MI
, DestBB
, Br
.MaxDisp
))
1503 return fixupUnconditionalBr(Br
);
1504 return fixupConditionalBr(Br
);
1507 /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1508 /// too far away to fit in its displacement field. If the LR register has been
1509 /// spilled in the epilogue, then we can use BL to implement a far jump.
1510 /// Otherwise, add an intermediate branch instruction to a branch.
1512 MipsConstantIslands::fixupUnconditionalBr(ImmBranch
&Br
) {
1513 MachineInstr
*MI
= Br
.MI
;
1514 MachineBasicBlock
*MBB
= MI
->getParent();
1515 MachineBasicBlock
*DestBB
= MI
->getOperand(0).getMBB();
1516 // Use BL to implement far jump.
1517 unsigned BimmX16MaxDisp
= ((1 << 16)-1) * 2;
1518 if (isBBInRange(MI
, DestBB
, BimmX16MaxDisp
)) {
1519 Br
.MaxDisp
= BimmX16MaxDisp
;
1520 MI
->setDesc(TII
->get(Mips::BimmX16
));
1523 // need to give the math a more careful look here
1524 // this is really a segment address and not
1525 // a PC relative address. FIXME. But I think that
1526 // just reducing the bits by 1 as I've done is correct.
1527 // The basic block we are branching too much be longword aligned.
1528 // we know that RA is saved because we always save it right now.
1529 // this requirement will be relaxed later but we also have an alternate
1530 // way to implement this that I will implement that does not need jal.
1531 // We should have a way to back out this alignment restriction if we "can" later.
1532 // but it is not harmful.
1534 DestBB
->setLogAlignment(2);
1535 Br
.MaxDisp
= ((1<<24)-1) * 2;
1536 MI
->setDesc(TII
->get(Mips::JalB16
));
1538 BBInfo
[MBB
->getNumber()].Size
+= 2;
1539 adjustBBOffsetsAfter(MBB
);
1543 LLVM_DEBUG(dbgs() << " Changed B to long jump " << *MI
);
1548 /// fixupConditionalBr - Fix up a conditional branch whose destination is too
1549 /// far away to fit in its displacement field. It is converted to an inverse
1550 /// conditional branch + an unconditional branch to the destination.
1552 MipsConstantIslands::fixupConditionalBr(ImmBranch
&Br
) {
1553 MachineInstr
*MI
= Br
.MI
;
1554 unsigned TargetOperand
= branchTargetOperand(MI
);
1555 MachineBasicBlock
*DestBB
= MI
->getOperand(TargetOperand
).getMBB();
1556 unsigned Opcode
= MI
->getOpcode();
1557 unsigned LongFormOpcode
= longformBranchOpcode(Opcode
);
1558 unsigned LongFormMaxOff
= branchMaxOffsets(LongFormOpcode
);
1560 // Check to see if the DestBB is already in-range.
1561 if (isBBInRange(MI
, DestBB
, LongFormMaxOff
)) {
1562 Br
.MaxDisp
= LongFormMaxOff
;
1563 MI
->setDesc(TII
->get(LongFormOpcode
));
1567 // Add an unconditional branch to the destination and invert the branch
1568 // condition to jump over it:
1575 // If the branch is at the end of its MBB and that has a fall-through block,
1576 // direct the updated conditional branch to the fall-through block. Otherwise,
1577 // split the MBB before the next instruction.
1578 MachineBasicBlock
*MBB
= MI
->getParent();
1579 MachineInstr
*BMI
= &MBB
->back();
1580 bool NeedSplit
= (BMI
!= MI
) || !BBHasFallthrough(MBB
);
1581 unsigned OppositeBranchOpcode
= TII
->getOppositeBranchOpc(Opcode
);
1585 if (std::next(MachineBasicBlock::iterator(MI
)) == std::prev(MBB
->end()) &&
1586 BMI
->isUnconditionalBranch()) {
1587 // Last MI in the BB is an unconditional branch. Can we simply invert the
1588 // condition and swap destinations:
1594 unsigned BMITargetOperand
= branchTargetOperand(BMI
);
1595 MachineBasicBlock
*NewDest
=
1596 BMI
->getOperand(BMITargetOperand
).getMBB();
1597 if (isBBInRange(MI
, NewDest
, Br
.MaxDisp
)) {
1599 dbgs() << " Invert Bcc condition and swap its destination with "
1601 MI
->setDesc(TII
->get(OppositeBranchOpcode
));
1602 BMI
->getOperand(BMITargetOperand
).setMBB(DestBB
);
1603 MI
->getOperand(TargetOperand
).setMBB(NewDest
);
1610 splitBlockBeforeInstr(*MI
);
1611 // No need for the branch to the next block. We're adding an unconditional
1612 // branch to the destination.
1613 int delta
= TII
->getInstSizeInBytes(MBB
->back());
1614 BBInfo
[MBB
->getNumber()].Size
-= delta
;
1615 MBB
->back().eraseFromParent();
1616 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1618 MachineBasicBlock
*NextBB
= &*++MBB
->getIterator();
1620 LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*DestBB
)
1621 << " also invert condition and change dest. to "
1622 << printMBBReference(*NextBB
) << "\n");
1624 // Insert a new conditional branch and a new unconditional branch.
1625 // Also update the ImmBranch as well as adding a new entry for the new branch.
1626 if (MI
->getNumExplicitOperands() == 2) {
1627 BuildMI(MBB
, DebugLoc(), TII
->get(OppositeBranchOpcode
))
1628 .addReg(MI
->getOperand(0).getReg())
1631 BuildMI(MBB
, DebugLoc(), TII
->get(OppositeBranchOpcode
))
1634 Br
.MI
= &MBB
->back();
1635 BBInfo
[MBB
->getNumber()].Size
+= TII
->getInstSizeInBytes(MBB
->back());
1636 BuildMI(MBB
, DebugLoc(), TII
->get(Br
.UncondBr
)).addMBB(DestBB
);
1637 BBInfo
[MBB
->getNumber()].Size
+= TII
->getInstSizeInBytes(MBB
->back());
1638 unsigned MaxDisp
= getUnconditionalBrDisp(Br
.UncondBr
);
1639 ImmBranches
.push_back(ImmBranch(&MBB
->back(), MaxDisp
, false, Br
.UncondBr
));
1641 // Remove the old conditional branch. It may or may not still be in MBB.
1642 BBInfo
[MI
->getParent()->getNumber()].Size
-= TII
->getInstSizeInBytes(*MI
);
1643 MI
->eraseFromParent();
1644 adjustBBOffsetsAfter(MBB
);
1648 void MipsConstantIslands::prescanForConstants() {
1651 for (MachineFunction::iterator B
=
1652 MF
->begin(), E
= MF
->end(); B
!= E
; ++B
) {
1653 for (MachineBasicBlock::instr_iterator I
=
1654 B
->instr_begin(), EB
= B
->instr_end(); I
!= EB
; ++I
) {
1655 switch(I
->getDesc().getOpcode()) {
1656 case Mips::LwConstant32
: {
1657 PrescannedForConstants
= true;
1658 LLVM_DEBUG(dbgs() << "constant island constant " << *I
<< "\n");
1659 J
= I
->getNumOperands();
1660 LLVM_DEBUG(dbgs() << "num operands " << J
<< "\n");
1661 MachineOperand
& Literal
= I
->getOperand(1);
1662 if (Literal
.isImm()) {
1663 int64_t V
= Literal
.getImm();
1664 LLVM_DEBUG(dbgs() << "literal " << V
<< "\n");
1666 Type::getInt32Ty(MF
->getFunction().getContext());
1667 const Constant
*C
= ConstantInt::get(Int32Ty
, V
);
1668 unsigned index
= MCP
->getConstantPoolIndex(C
, 4);
1669 I
->getOperand(2).ChangeToImmediate(index
);
1670 LLVM_DEBUG(dbgs() << "constant island constant " << *I
<< "\n");
1671 I
->setDesc(TII
->get(Mips::LwRxPcTcp16
));
1672 I
->RemoveOperand(1);
1673 I
->RemoveOperand(1);
1674 I
->addOperand(MachineOperand::CreateCPI(index
, 0));
1675 I
->addOperand(MachineOperand::CreateImm(4));
1686 /// Returns a pass that converts branches to long branches.
1687 FunctionPass
*llvm::createMipsConstantIslandPass() {
1688 return new MipsConstantIslands();