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
131 // the math for branch ranges and clean this up and make some functions
132 // to calculate things that are done many times identically.
133 // Need to refactor some of the code to call this routine.
134 static unsigned int branchMaxOffsets(unsigned int Opcode
) {
135 unsigned Bits
, Scale
;
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 unsigned postOffset() const { return Offset
+ Size
; }
228 std::vector
<BasicBlockInfo
> BBInfo
;
230 /// WaterList - A sorted list of basic blocks where islands could be placed
231 /// (i.e. blocks that don't fall through to the following block, due
232 /// to a return, unreachable, or unconditional branch).
233 std::vector
<MachineBasicBlock
*> WaterList
;
235 /// NewWaterList - The subset of WaterList that was created since the
236 /// previous iteration by inserting unconditional branches.
237 SmallSet
<MachineBasicBlock
*, 4> NewWaterList
;
239 using water_iterator
= std::vector
<MachineBasicBlock
*>::iterator
;
241 /// CPUser - One user of a constant pool, keeping the machine instruction
242 /// pointer, the constant pool being referenced, and the max displacement
243 /// allowed from the instruction to the CP. The HighWaterMark records the
244 /// highest basic block where a new CPEntry can be placed. To ensure this
245 /// pass terminates, the CP entries are initially placed at the end of the
246 /// function and then move monotonically to lower addresses. The
247 /// exception to this rule is when the current CP entry for a particular
248 /// CPUser is out of range, but there is another CP entry for the same
249 /// constant value in range. We want to use the existing in-range CP
250 /// entry, but if it later moves out of range, the search for new water
251 /// should resume where it left off. The HighWaterMark is used to record
256 MachineBasicBlock
*HighWaterMark
;
260 unsigned LongFormMaxDisp
; // mips16 has 16/32 bit instructions
261 // with different displacements
262 unsigned LongFormOpcode
;
267 CPUser(MachineInstr
*mi
, MachineInstr
*cpemi
, unsigned maxdisp
,
269 unsigned longformmaxdisp
, unsigned longformopcode
)
270 : MI(mi
), CPEMI(cpemi
), MaxDisp(maxdisp
),
271 LongFormMaxDisp(longformmaxdisp
), LongFormOpcode(longformopcode
),
273 HighWaterMark
= CPEMI
->getParent();
276 /// getMaxDisp - Returns the maximum displacement supported by MI.
277 unsigned getMaxDisp() const {
278 unsigned xMaxDisp
= ConstantIslandsSmallOffset
?
279 ConstantIslandsSmallOffset
: MaxDisp
;
283 void setMaxDisp(unsigned val
) {
287 unsigned getLongFormMaxDisp() const {
288 return LongFormMaxDisp
;
291 unsigned getLongFormOpcode() const {
292 return LongFormOpcode
;
296 /// CPUsers - Keep track of all of the machine instructions that use various
297 /// constant pools and their max displacement.
298 std::vector
<CPUser
> CPUsers
;
300 /// CPEntry - One per constant pool entry, keeping the machine instruction
301 /// pointer, the constpool index, and the number of CPUser's which
302 /// reference this entry.
308 CPEntry(MachineInstr
*cpemi
, unsigned cpi
, unsigned rc
= 0)
309 : CPEMI(cpemi
), CPI(cpi
), RefCount(rc
) {}
312 /// CPEntries - Keep track of all of the constant pool entry machine
313 /// instructions. For each original constpool index (i.e. those that
314 /// existed upon entry to this pass), it keeps a vector of entries.
315 /// Original elements are cloned as we go along; the clones are
316 /// put in the vector of the original element, but have distinct CPIs.
317 std::vector
<std::vector
<CPEntry
>> CPEntries
;
319 /// ImmBranch - One per immediate branch, keeping the machine instruction
320 /// pointer, conditional or unconditional, the max displacement,
321 /// and (if isCond is true) the corresponding unconditional branch
325 unsigned MaxDisp
: 31;
329 ImmBranch(MachineInstr
*mi
, unsigned maxdisp
, bool cond
, int ubr
)
330 : MI(mi
), MaxDisp(maxdisp
), isCond(cond
), UncondBr(ubr
) {}
333 /// ImmBranches - Keep track of all the immediate branch instructions.
335 std::vector
<ImmBranch
> ImmBranches
;
337 /// HasFarJump - True if any far jump instruction has been emitted during
338 /// the branch fix up pass.
341 const MipsSubtarget
*STI
= nullptr;
342 const Mips16InstrInfo
*TII
;
343 MipsFunctionInfo
*MFI
;
344 MachineFunction
*MF
= nullptr;
345 MachineConstantPool
*MCP
= nullptr;
347 unsigned PICLabelUId
;
348 bool PrescannedForConstants
= false;
350 void initPICLabelUId(unsigned UId
) {
354 unsigned createPICLabelUId() {
355 return PICLabelUId
++;
361 MipsConstantIslands() : MachineFunctionPass(ID
) {}
363 StringRef
getPassName() const override
{ return "Mips Constant Islands"; }
365 bool runOnMachineFunction(MachineFunction
&F
) override
;
367 MachineFunctionProperties
getRequiredProperties() const override
{
368 return MachineFunctionProperties().set(
369 MachineFunctionProperties::Property::NoVRegs
);
372 void doInitialPlacement(std::vector
<MachineInstr
*> &CPEMIs
);
373 CPEntry
*findConstPoolEntry(unsigned CPI
, const MachineInstr
*CPEMI
);
374 Align
getCPEAlign(const MachineInstr
&CPEMI
);
375 void initializeFunctionInfo(const std::vector
<MachineInstr
*> &CPEMIs
);
376 unsigned getOffsetOf(MachineInstr
*MI
) const;
377 unsigned getUserOffset(CPUser
&) const;
380 bool isOffsetInRange(unsigned UserOffset
, unsigned TrialOffset
,
381 unsigned Disp
, bool NegativeOK
);
382 bool isOffsetInRange(unsigned UserOffset
, unsigned TrialOffset
,
385 void computeBlockSize(MachineBasicBlock
*MBB
);
386 MachineBasicBlock
*splitBlockBeforeInstr(MachineInstr
&MI
);
387 void updateForInsertedWaterBlock(MachineBasicBlock
*NewBB
);
388 void adjustBBOffsetsAfter(MachineBasicBlock
*BB
);
389 bool decrementCPEReferenceCount(unsigned CPI
, MachineInstr
* CPEMI
);
390 int findInRangeCPEntry(CPUser
& U
, unsigned UserOffset
);
391 int findLongFormInRangeCPEntry(CPUser
& U
, unsigned UserOffset
);
392 bool findAvailableWater(CPUser
&U
, unsigned UserOffset
,
393 water_iterator
&WaterIter
);
394 void createNewWater(unsigned CPUserIndex
, unsigned UserOffset
,
395 MachineBasicBlock
*&NewMBB
);
396 bool handleConstantPoolUser(unsigned CPUserIndex
);
397 void removeDeadCPEMI(MachineInstr
*CPEMI
);
398 bool removeUnusedCPEntries();
399 bool isCPEntryInRange(MachineInstr
*MI
, unsigned UserOffset
,
400 MachineInstr
*CPEMI
, unsigned Disp
, bool NegOk
,
401 bool DoDump
= false);
402 bool isWaterInRange(unsigned UserOffset
, MachineBasicBlock
*Water
,
403 CPUser
&U
, unsigned &Growth
);
404 bool isBBInRange(MachineInstr
*MI
, MachineBasicBlock
*BB
, unsigned Disp
);
405 bool fixupImmediateBr(ImmBranch
&Br
);
406 bool fixupConditionalBr(ImmBranch
&Br
);
407 bool fixupUnconditionalBr(ImmBranch
&Br
);
409 void prescanForConstants();
412 } // end anonymous namespace
414 char MipsConstantIslands::ID
= 0;
416 bool MipsConstantIslands::isOffsetInRange
417 (unsigned UserOffset
, unsigned TrialOffset
,
419 return isOffsetInRange(UserOffset
, TrialOffset
,
420 U
.getMaxDisp(), U
.NegOk
);
423 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
424 /// print block size and offset information - debugging
425 LLVM_DUMP_METHOD
void MipsConstantIslands::dumpBBs() {
426 for (unsigned J
= 0, E
= BBInfo
.size(); J
!=E
; ++J
) {
427 const BasicBlockInfo
&BBI
= BBInfo
[J
];
428 dbgs() << format("%08x %bb.%u\t", BBI
.Offset
, J
)
429 << format(" size=%#x\n", BBInfo
[J
].Size
);
434 bool MipsConstantIslands::runOnMachineFunction(MachineFunction
&mf
) {
435 // The intention is for this to be a mips16 only pass for now
438 MCP
= mf
.getConstantPool();
439 STI
= &mf
.getSubtarget
<MipsSubtarget
>();
440 LLVM_DEBUG(dbgs() << "constant island machine function "
442 if (!STI
->inMips16Mode() || !MipsSubtarget::useConstantIslands()) {
445 TII
= (const Mips16InstrInfo
*)STI
->getInstrInfo();
446 MFI
= MF
->getInfo
<MipsFunctionInfo
>();
447 LLVM_DEBUG(dbgs() << "constant island processing "
450 // will need to make predermination if there is any constants we need to
451 // put in constant islands. TBD.
453 if (!PrescannedForConstants
) prescanForConstants();
456 // This pass invalidates liveness information when it splits basic blocks.
457 MF
->getRegInfo().invalidateLiveness();
459 // Renumber all of the machine basic blocks in the function, guaranteeing that
460 // the numbers agree with the position of the block in the function.
461 MF
->RenumberBlocks();
463 bool MadeChange
= false;
465 // Perform the initial placement of the constant pool entries. To start with,
466 // we put them all at the end of the function.
467 std::vector
<MachineInstr
*> CPEMIs
;
469 doInitialPlacement(CPEMIs
);
471 /// The next UID to take is the first unused one.
472 initPICLabelUId(CPEMIs
.size());
474 // Do the initial scan of the function, building up information about the
475 // sizes of each block, the location of all the water, and finding all of the
476 // constant pool users.
477 initializeFunctionInfo(CPEMIs
);
479 LLVM_DEBUG(dumpBBs());
481 /// Remove dead constant pool entries.
482 MadeChange
|= removeUnusedCPEntries();
484 // Iteratively place constant pool entries and fix up branches until there
486 unsigned NoCPIters
= 0, NoBRIters
= 0;
489 LLVM_DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters
<< '\n');
490 bool CPChange
= false;
491 for (unsigned i
= 0, e
= CPUsers
.size(); i
!= e
; ++i
)
492 CPChange
|= handleConstantPoolUser(i
);
493 if (CPChange
&& ++NoCPIters
> 30)
494 report_fatal_error("Constant Island pass failed to converge!");
495 LLVM_DEBUG(dumpBBs());
497 // Clear NewWaterList now. If we split a block for branches, it should
498 // appear as "new water" for the next iteration of constant pool placement.
499 NewWaterList
.clear();
501 LLVM_DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters
<< '\n');
502 bool BRChange
= false;
503 for (unsigned i
= 0, e
= ImmBranches
.size(); i
!= e
; ++i
)
504 BRChange
|= fixupImmediateBr(ImmBranches
[i
]);
505 if (BRChange
&& ++NoBRIters
> 30)
506 report_fatal_error("Branch Fix Up pass failed to converge!");
507 LLVM_DEBUG(dumpBBs());
508 if (!CPChange
&& !BRChange
)
513 LLVM_DEBUG(dbgs() << '\n'; dumpBBs());
523 /// doInitialPlacement - Perform the initial placement of the constant pool
524 /// entries. To start with, we put them all at the end of the function.
526 MipsConstantIslands::doInitialPlacement(std::vector
<MachineInstr
*> &CPEMIs
) {
527 // Create the basic block to hold the CPE's.
528 MachineBasicBlock
*BB
= MF
->CreateMachineBasicBlock();
531 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
532 const Align MaxAlign
= MCP
->getConstantPoolAlign();
534 // Mark the basic block as required by the const-pool.
535 // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
536 BB
->setAlignment(AlignConstantIslands
? MaxAlign
: Align(4));
538 // The function needs to be as aligned as the basic blocks. The linker may
539 // move functions around based on their alignment.
540 MF
->ensureAlignment(BB
->getAlignment());
542 // Order the entries in BB by descending alignment. That ensures correct
543 // alignment of all entries as long as BB is sufficiently aligned. Keep
544 // track of the insertion point for each alignment. We are going to bucket
545 // sort the entries as they are created.
546 SmallVector
<MachineBasicBlock::iterator
, 8> InsPoint(Log2(MaxAlign
) + 1,
549 // Add all of the constants from the constant pool to the end block, use an
550 // identity mapping of CPI's to CPE's.
551 const std::vector
<MachineConstantPoolEntry
> &CPs
= MCP
->getConstants();
553 const DataLayout
&TD
= MF
->getDataLayout();
554 for (unsigned i
= 0, e
= CPs
.size(); i
!= e
; ++i
) {
555 unsigned Size
= CPs
[i
].getSizeInBytes(TD
);
556 assert(Size
>= 4 && "Too small constant pool entry");
557 Align Alignment
= CPs
[i
].getAlign();
558 // Verify that all constant pool entries are a multiple of their alignment.
559 // If not, we would have to pad them out so that instructions stay aligned.
560 assert(isAligned(Alignment
, Size
) && "CP Entry not multiple of 4 bytes!");
562 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
563 unsigned LogAlign
= Log2(Alignment
);
564 MachineBasicBlock::iterator InsAt
= InsPoint
[LogAlign
];
566 MachineInstr
*CPEMI
=
567 BuildMI(*BB
, InsAt
, DebugLoc(), TII
->get(Mips::CONSTPOOL_ENTRY
))
568 .addImm(i
).addConstantPoolIndex(i
).addImm(Size
);
570 CPEMIs
.push_back(CPEMI
);
572 // Ensure that future entries with higher alignment get inserted before
573 // CPEMI. This is bucket sort with iterators.
574 for (unsigned a
= LogAlign
+ 1; a
<= Log2(MaxAlign
); ++a
)
575 if (InsPoint
[a
] == InsAt
)
577 // Add a new CPEntry, but no corresponding CPUser yet.
578 CPEntries
.emplace_back(1, CPEntry(CPEMI
, i
));
580 LLVM_DEBUG(dbgs() << "Moved CPI#" << i
<< " to end of function, size = "
581 << Size
<< ", align = " << Alignment
.value() << '\n');
583 LLVM_DEBUG(BB
->dump());
586 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
587 /// into the block immediately after it.
588 static bool BBHasFallthrough(MachineBasicBlock
*MBB
) {
589 // Get the next machine basic block in the function.
590 MachineFunction::iterator MBBI
= MBB
->getIterator();
591 // Can't fall off end of function.
592 if (std::next(MBBI
) == MBB
->getParent()->end())
595 MachineBasicBlock
*NextBB
= &*std::next(MBBI
);
596 return llvm::is_contained(MBB
->successors(), NextBB
);
599 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
600 /// look up the corresponding CPEntry.
601 MipsConstantIslands::CPEntry
602 *MipsConstantIslands::findConstPoolEntry(unsigned CPI
,
603 const MachineInstr
*CPEMI
) {
604 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
605 // Number of entries per constpool index should be small, just do a
607 for (CPEntry
&CPE
: CPEs
) {
608 if (CPE
.CPEMI
== CPEMI
)
614 /// getCPEAlign - Returns the required alignment of the constant pool entry
615 /// represented by CPEMI. Alignment is measured in log2(bytes) units.
616 Align
MipsConstantIslands::getCPEAlign(const MachineInstr
&CPEMI
) {
617 assert(CPEMI
.getOpcode() == Mips::CONSTPOOL_ENTRY
);
619 // Everything is 4-byte aligned unless AlignConstantIslands is set.
620 if (!AlignConstantIslands
)
623 unsigned CPI
= CPEMI
.getOperand(1).getIndex();
624 assert(CPI
< MCP
->getConstants().size() && "Invalid constant pool index.");
625 return MCP
->getConstants()[CPI
].getAlign();
628 /// initializeFunctionInfo - Do the initial scan of the function, building up
629 /// information about the sizes of each block, the location of all the water,
630 /// and finding all of the constant pool users.
631 void MipsConstantIslands::
632 initializeFunctionInfo(const std::vector
<MachineInstr
*> &CPEMIs
) {
634 BBInfo
.resize(MF
->getNumBlockIDs());
636 // First thing, compute the size of all basic blocks, and see if the function
637 // has any inline assembly in it. If so, we have to be conservative about
638 // alignment assumptions, as we don't know for sure the size of any
639 // instructions in the inline assembly.
640 for (MachineBasicBlock
&MBB
: *MF
)
641 computeBlockSize(&MBB
);
643 // Compute block offsets.
644 adjustBBOffsetsAfter(&MF
->front());
646 // Now go back through the instructions and build up our data structures.
647 for (MachineBasicBlock
&MBB
: *MF
) {
648 // If this block doesn't fall through into the next MBB, then this is
649 // 'water' that a constant pool island could be placed.
650 if (!BBHasFallthrough(&MBB
))
651 WaterList
.push_back(&MBB
);
652 for (MachineInstr
&MI
: MBB
) {
653 if (MI
.isDebugInstr())
656 int Opc
= MI
.getOpcode();
664 continue; // Ignore other branches for now
675 case Mips::BeqzRxImm16
:
681 case Mips::BeqzRxImmX16
:
687 case Mips::BnezRxImm16
:
693 case Mips::BnezRxImmX16
:
724 // Record this immediate branch.
725 unsigned MaxOffs
= ((1 << (Bits
-1))-1) * Scale
;
726 ImmBranches
.push_back(ImmBranch(&MI
, MaxOffs
, isCond
, UOpc
));
729 if (Opc
== Mips::CONSTPOOL_ENTRY
)
732 // Scan the instructions for constant pool operands.
733 for (const MachineOperand
&MO
: MI
.operands())
735 // We found one. The addressing mode tells us the max displacement
736 // from the PC that this instruction permits.
738 // Basic size info comes from the TSFlags field.
742 unsigned LongFormBits
= 0;
743 unsigned LongFormScale
= 0;
744 unsigned LongFormOpcode
= 0;
747 llvm_unreachable("Unknown addressing mode for CP reference!");
748 case Mips::LwRxPcTcp16
:
751 LongFormOpcode
= Mips::LwRxPcTcpX16
;
755 case Mips::LwRxPcTcpX16
:
761 // Remember that this is a user of a CP entry.
762 unsigned CPI
= MO
.getIndex();
763 MachineInstr
*CPEMI
= CPEMIs
[CPI
];
764 unsigned MaxOffs
= ((1 << Bits
)-1) * Scale
;
765 unsigned LongFormMaxOffs
= ((1 << LongFormBits
)-1) * LongFormScale
;
766 CPUsers
.push_back(CPUser(&MI
, CPEMI
, MaxOffs
, NegOk
, LongFormMaxOffs
,
769 // Increment corresponding CPEntry reference count.
770 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
771 assert(CPE
&& "Cannot find a corresponding CPEntry!");
774 // Instructions can only use one CP entry, don't bother scanning the
775 // rest of the operands.
782 /// computeBlockSize - Compute the size and some alignment information for MBB.
783 /// This function updates BBInfo directly.
784 void MipsConstantIslands::computeBlockSize(MachineBasicBlock
*MBB
) {
785 BasicBlockInfo
&BBI
= BBInfo
[MBB
->getNumber()];
788 for (const MachineInstr
&MI
: *MBB
)
789 BBI
.Size
+= TII
->getInstSizeInBytes(MI
);
792 /// getOffsetOf - Return the current offset of the specified machine instruction
793 /// from the start of the function. This offset changes as stuff is moved
794 /// around inside the function.
795 unsigned MipsConstantIslands::getOffsetOf(MachineInstr
*MI
) const {
796 MachineBasicBlock
*MBB
= MI
->getParent();
798 // The offset is composed of two things: the sum of the sizes of all MBB's
799 // before this instruction's block, and the offset from the start of the block
801 unsigned Offset
= BBInfo
[MBB
->getNumber()].Offset
;
803 // Sum instructions before MI in MBB.
804 for (MachineBasicBlock::iterator I
= MBB
->begin(); &*I
!= MI
; ++I
) {
805 assert(I
!= MBB
->end() && "Didn't find MI in its own basic block?");
806 Offset
+= TII
->getInstSizeInBytes(*I
);
811 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
813 static bool CompareMBBNumbers(const MachineBasicBlock
*LHS
,
814 const MachineBasicBlock
*RHS
) {
815 return LHS
->getNumber() < RHS
->getNumber();
818 /// updateForInsertedWaterBlock - When a block is newly inserted into the
819 /// machine function, it upsets all of the block numbers. Renumber the blocks
820 /// and update the arrays that parallel this numbering.
821 void MipsConstantIslands::updateForInsertedWaterBlock
822 (MachineBasicBlock
*NewBB
) {
823 // Renumber the MBB's to keep them consecutive.
824 NewBB
->getParent()->RenumberBlocks(NewBB
);
826 // Insert an entry into BBInfo to align it properly with the (newly
827 // renumbered) block numbers.
828 BBInfo
.insert(BBInfo
.begin() + NewBB
->getNumber(), BasicBlockInfo());
830 // Next, update WaterList. Specifically, we need to add NewMBB as having
831 // available water after it.
832 water_iterator IP
= llvm::lower_bound(WaterList
, NewBB
, CompareMBBNumbers
);
833 WaterList
.insert(IP
, NewBB
);
836 unsigned MipsConstantIslands::getUserOffset(CPUser
&U
) const {
837 return getOffsetOf(U
.MI
);
840 /// Split the basic block containing MI into two blocks, which are joined by
841 /// an unconditional branch. Update data structures and renumber blocks to
842 /// account for this change and returns the newly created block.
844 MipsConstantIslands::splitBlockBeforeInstr(MachineInstr
&MI
) {
845 MachineBasicBlock
*OrigBB
= MI
.getParent();
847 // Create a new MBB for the code after the OrigBB.
848 MachineBasicBlock
*NewBB
=
849 MF
->CreateMachineBasicBlock(OrigBB
->getBasicBlock());
850 MachineFunction::iterator MBBI
= ++OrigBB
->getIterator();
851 MF
->insert(MBBI
, NewBB
);
853 // Splice the instructions starting with MI over to NewBB.
854 NewBB
->splice(NewBB
->end(), OrigBB
, MI
, OrigBB
->end());
856 // Add an unconditional branch from OrigBB to NewBB.
857 // Note the new unconditional branch is not being recorded.
858 // There doesn't seem to be meaningful DebugInfo available; this doesn't
859 // correspond to anything in the source.
860 BuildMI(OrigBB
, DebugLoc(), TII
->get(Mips::Bimm16
)).addMBB(NewBB
);
863 // Update the CFG. All succs of OrigBB are now succs of NewBB.
864 NewBB
->transferSuccessors(OrigBB
);
866 // OrigBB branches to NewBB.
867 OrigBB
->addSuccessor(NewBB
);
869 // Update internal data structures to account for the newly inserted MBB.
870 // This is almost the same as updateForInsertedWaterBlock, except that
871 // the Water goes after OrigBB, not NewBB.
872 MF
->RenumberBlocks(NewBB
);
874 // Insert an entry into BBInfo to align it properly with the (newly
875 // renumbered) block numbers.
876 BBInfo
.insert(BBInfo
.begin() + NewBB
->getNumber(), BasicBlockInfo());
878 // Next, update WaterList. Specifically, we need to add OrigMBB as having
879 // available water after it (but not if it's already there, which happens
880 // when splitting before a conditional branch that is followed by an
881 // unconditional branch - in that case we want to insert NewBB).
882 water_iterator IP
= llvm::lower_bound(WaterList
, OrigBB
, CompareMBBNumbers
);
883 MachineBasicBlock
* WaterBB
= *IP
;
884 if (WaterBB
== OrigBB
)
885 WaterList
.insert(std::next(IP
), NewBB
);
887 WaterList
.insert(IP
, OrigBB
);
888 NewWaterList
.insert(OrigBB
);
890 // Figure out how large the OrigBB is. As the first half of the original
891 // block, it cannot contain a tablejump. The size includes
892 // the new jump we added. (It should be possible to do this without
893 // recounting everything, but it's very confusing, and this is rarely
895 computeBlockSize(OrigBB
);
897 // Figure out how large the NewMBB is. As the second half of the original
898 // block, it may contain a tablejump.
899 computeBlockSize(NewBB
);
901 // All BBOffsets following these blocks must be modified.
902 adjustBBOffsetsAfter(OrigBB
);
907 /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
908 /// reference) is within MaxDisp of TrialOffset (a proposed location of a
909 /// constant pool entry).
910 bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset
,
911 unsigned TrialOffset
, unsigned MaxDisp
,
913 if (UserOffset
<= TrialOffset
) {
914 // User before the Trial.
915 if (TrialOffset
- UserOffset
<= MaxDisp
)
917 } else if (NegativeOK
) {
918 if (UserOffset
- TrialOffset
<= MaxDisp
)
924 /// isWaterInRange - Returns true if a CPE placed after the specified
925 /// Water (a basic block) will be in range for the specific MI.
927 /// Compute how much the function will grow by inserting a CPE after Water.
928 bool MipsConstantIslands::isWaterInRange(unsigned UserOffset
,
929 MachineBasicBlock
* Water
, CPUser
&U
,
931 unsigned CPEOffset
= BBInfo
[Water
->getNumber()].postOffset();
932 unsigned NextBlockOffset
;
933 Align NextBlockAlignment
;
934 MachineFunction::const_iterator NextBlock
= ++Water
->getIterator();
935 if (NextBlock
== MF
->end()) {
936 NextBlockOffset
= BBInfo
[Water
->getNumber()].postOffset();
937 NextBlockAlignment
= Align(1);
939 NextBlockOffset
= BBInfo
[NextBlock
->getNumber()].Offset
;
940 NextBlockAlignment
= NextBlock
->getAlignment();
942 unsigned Size
= U
.CPEMI
->getOperand(2).getImm();
943 unsigned CPEEnd
= CPEOffset
+ Size
;
945 // The CPE may be able to hide in the alignment padding before the next
946 // block. It may also cause more padding to be required if it is more aligned
947 // that the next block.
948 if (CPEEnd
> NextBlockOffset
) {
949 Growth
= CPEEnd
- NextBlockOffset
;
950 // Compute the padding that would go at the end of the CPE to align the next
952 Growth
+= offsetToAlignment(CPEEnd
, NextBlockAlignment
);
954 // If the CPE is to be inserted before the instruction, that will raise
955 // the offset of the instruction. Also account for unknown alignment padding
956 // in blocks between CPE and the user.
957 if (CPEOffset
< UserOffset
)
958 UserOffset
+= Growth
;
960 // CPE fits in existing padding.
963 return isOffsetInRange(UserOffset
, CPEOffset
, U
);
966 /// isCPEntryInRange - Returns true if the distance between specific MI and
967 /// specific ConstPool entry instruction can fit in MI's displacement field.
968 bool MipsConstantIslands::isCPEntryInRange
969 (MachineInstr
*MI
, unsigned UserOffset
,
970 MachineInstr
*CPEMI
, unsigned MaxDisp
,
971 bool NegOk
, bool DoDump
) {
972 unsigned CPEOffset
= getOffsetOf(CPEMI
);
976 unsigned Block
= MI
->getParent()->getNumber();
977 const BasicBlockInfo
&BBI
= BBInfo
[Block
];
978 dbgs() << "User of CPE#" << CPEMI
->getOperand(0).getImm()
979 << " max delta=" << MaxDisp
980 << format(" insn address=%#x", UserOffset
) << " in "
981 << printMBBReference(*MI
->getParent()) << ": "
982 << format("%#x-%x\t", BBI
.Offset
, BBI
.postOffset()) << *MI
983 << format("CPE address=%#x offset=%+d: ", CPEOffset
,
984 int(CPEOffset
- UserOffset
));
988 return isOffsetInRange(UserOffset
, CPEOffset
, MaxDisp
, NegOk
);
992 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
993 /// unconditionally branches to its only successor.
994 static bool BBIsJumpedOver(MachineBasicBlock
*MBB
) {
995 if (MBB
->pred_size() != 1 || MBB
->succ_size() != 1)
997 MachineBasicBlock
*Succ
= *MBB
->succ_begin();
998 MachineBasicBlock
*Pred
= *MBB
->pred_begin();
999 MachineInstr
*PredMI
= &Pred
->back();
1000 if (PredMI
->getOpcode() == Mips::Bimm16
)
1001 return PredMI
->getOperand(0).getMBB() == Succ
;
1006 void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock
*BB
) {
1007 unsigned BBNum
= BB
->getNumber();
1008 for(unsigned i
= BBNum
+ 1, e
= MF
->getNumBlockIDs(); i
< e
; ++i
) {
1009 // Get the offset and known bits at the end of the layout predecessor.
1010 // Include the alignment of the current block.
1011 unsigned Offset
= BBInfo
[i
- 1].Offset
+ BBInfo
[i
- 1].Size
;
1012 BBInfo
[i
].Offset
= Offset
;
1016 /// decrementCPEReferenceCount - find the constant pool entry with index CPI
1017 /// and instruction CPEMI, and decrement its refcount. If the refcount
1018 /// becomes 0 remove the entry and instruction. Returns true if we removed
1019 /// the entry, false if we didn't.
1020 bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI
,
1021 MachineInstr
*CPEMI
) {
1022 // Find the old entry. Eliminate it if it is no longer used.
1023 CPEntry
*CPE
= findConstPoolEntry(CPI
, CPEMI
);
1024 assert(CPE
&& "Unexpected!");
1025 if (--CPE
->RefCount
== 0) {
1026 removeDeadCPEMI(CPEMI
);
1027 CPE
->CPEMI
= nullptr;
1034 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1035 /// if not, see if an in-range clone of the CPE is in range, and if so,
1036 /// change the data structures so the user references the clone. Returns:
1037 /// 0 = no existing entry found
1038 /// 1 = entry found, and there were no code insertions or deletions
1039 /// 2 = entry found, and there were code insertions or deletions
1040 int MipsConstantIslands::findInRangeCPEntry(CPUser
& U
, unsigned UserOffset
)
1042 MachineInstr
*UserMI
= U
.MI
;
1043 MachineInstr
*CPEMI
= U
.CPEMI
;
1045 // Check to see if the CPE is already in-range.
1046 if (isCPEntryInRange(UserMI
, UserOffset
, CPEMI
, U
.getMaxDisp(), U
.NegOk
,
1048 LLVM_DEBUG(dbgs() << "In range\n");
1052 // No. Look for previously created clones of the CPE that are in range.
1053 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1054 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
1055 for (CPEntry
&CPE
: CPEs
) {
1056 // We already tried this one
1057 if (CPE
.CPEMI
== CPEMI
)
1059 // Removing CPEs can leave empty entries, skip
1060 if (CPE
.CPEMI
== nullptr)
1062 if (isCPEntryInRange(UserMI
, UserOffset
, CPE
.CPEMI
, U
.getMaxDisp(),
1064 LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI
<< " with CPE#" << CPE
.CPI
1066 // Point the CPUser node to the replacement
1067 U
.CPEMI
= CPE
.CPEMI
;
1068 // Change the CPI in the instruction operand to refer to the clone.
1069 for (MachineOperand
&MO
: UserMI
->operands())
1071 MO
.setIndex(CPE
.CPI
);
1074 // Adjust the refcount of the clone...
1076 // ...and the original. If we didn't remove the old entry, none of the
1077 // addresses changed, so we don't need another pass.
1078 return decrementCPEReferenceCount(CPI
, CPEMI
) ? 2 : 1;
1084 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1085 /// This version checks if the longer form of the instruction can be used to
1086 /// to satisfy things.
1087 /// if not, see if an in-range clone of the CPE is in range, and if so,
1088 /// change the data structures so the user references the clone. Returns:
1089 /// 0 = no existing entry found
1090 /// 1 = entry found, and there were no code insertions or deletions
1091 /// 2 = entry found, and there were code insertions or deletions
1092 int MipsConstantIslands::findLongFormInRangeCPEntry
1093 (CPUser
& U
, unsigned UserOffset
)
1095 MachineInstr
*UserMI
= U
.MI
;
1096 MachineInstr
*CPEMI
= U
.CPEMI
;
1098 // Check to see if the CPE is already in-range.
1099 if (isCPEntryInRange(UserMI
, UserOffset
, CPEMI
,
1100 U
.getLongFormMaxDisp(), U
.NegOk
,
1102 LLVM_DEBUG(dbgs() << "In range\n");
1103 UserMI
->setDesc(TII
->get(U
.getLongFormOpcode()));
1104 U
.setMaxDisp(U
.getLongFormMaxDisp());
1105 return 2; // instruction is longer length now
1108 // No. Look for previously created clones of the CPE that are in range.
1109 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1110 std::vector
<CPEntry
> &CPEs
= CPEntries
[CPI
];
1111 for (CPEntry
&CPE
: CPEs
) {
1112 // We already tried this one
1113 if (CPE
.CPEMI
== CPEMI
)
1115 // Removing CPEs can leave empty entries, skip
1116 if (CPE
.CPEMI
== nullptr)
1118 if (isCPEntryInRange(UserMI
, UserOffset
, CPE
.CPEMI
, U
.getLongFormMaxDisp(),
1120 LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI
<< " with CPE#" << CPE
.CPI
1122 // Point the CPUser node to the replacement
1123 U
.CPEMI
= CPE
.CPEMI
;
1124 // Change the CPI in the instruction operand to refer to the clone.
1125 for (MachineOperand
&MO
: UserMI
->operands())
1127 MO
.setIndex(CPE
.CPI
);
1130 // Adjust the refcount of the clone...
1132 // ...and the original. If we didn't remove the old entry, none of the
1133 // addresses changed, so we don't need another pass.
1134 return decrementCPEReferenceCount(CPI
, CPEMI
) ? 2 : 1;
1140 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1141 /// the specific unconditional branch instruction.
1142 static inline unsigned getUnconditionalBrDisp(int Opc
) {
1145 return ((1<<10)-1)*2;
1147 return ((1<<16)-1)*2;
1151 return ((1<<16)-1)*2;
1154 /// findAvailableWater - Look for an existing entry in the WaterList in which
1155 /// we can place the CPE referenced from U so it's within range of U's MI.
1156 /// Returns true if found, false if not. If it returns true, WaterIter
1157 /// is set to the WaterList entry.
1158 /// To ensure that this pass
1159 /// terminates, the CPE location for a particular CPUser is only allowed to
1160 /// move to a lower address, so search backward from the end of the list and
1161 /// prefer the first water that is in range.
1162 bool MipsConstantIslands::findAvailableWater(CPUser
&U
, unsigned UserOffset
,
1163 water_iterator
&WaterIter
) {
1164 if (WaterList
.empty())
1167 unsigned BestGrowth
= ~0u;
1168 for (water_iterator IP
= std::prev(WaterList
.end()), B
= WaterList
.begin();;
1170 MachineBasicBlock
* WaterBB
= *IP
;
1171 // Check if water is in range and is either at a lower address than the
1172 // current "high water mark" or a new water block that was created since
1173 // the previous iteration by inserting an unconditional branch. In the
1174 // latter case, we want to allow resetting the high water mark back to
1175 // this new water since we haven't seen it before. Inserting branches
1176 // should be relatively uncommon and when it does happen, we want to be
1177 // sure to take advantage of it for all the CPEs near that block, so that
1178 // we don't insert more branches than necessary.
1180 if (isWaterInRange(UserOffset
, WaterBB
, U
, Growth
) &&
1181 (WaterBB
->getNumber() < U
.HighWaterMark
->getNumber() ||
1182 NewWaterList
.count(WaterBB
)) && Growth
< BestGrowth
) {
1183 // This is the least amount of required padding seen so far.
1184 BestGrowth
= Growth
;
1186 LLVM_DEBUG(dbgs() << "Found water after " << printMBBReference(*WaterBB
)
1187 << " Growth=" << Growth
<< '\n');
1189 // Keep looking unless it is perfect.
1190 if (BestGrowth
== 0)
1196 return BestGrowth
!= ~0u;
1199 /// createNewWater - No existing WaterList entry will work for
1200 /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1201 /// block is used if in range, and the conditional branch munged so control
1202 /// flow is correct. Otherwise the block is split to create a hole with an
1203 /// unconditional branch around it. In either case NewMBB is set to a
1204 /// block following which the new island can be inserted (the WaterList
1205 /// is not adjusted).
1206 void MipsConstantIslands::createNewWater(unsigned CPUserIndex
,
1207 unsigned UserOffset
,
1208 MachineBasicBlock
*&NewMBB
) {
1209 CPUser
&U
= CPUsers
[CPUserIndex
];
1210 MachineInstr
*UserMI
= U
.MI
;
1211 MachineInstr
*CPEMI
= U
.CPEMI
;
1212 MachineBasicBlock
*UserMBB
= UserMI
->getParent();
1213 const BasicBlockInfo
&UserBBI
= BBInfo
[UserMBB
->getNumber()];
1215 // If the block does not end in an unconditional branch already, and if the
1216 // end of the block is within range, make new water there.
1217 if (BBHasFallthrough(UserMBB
)) {
1218 // Size of branch to insert.
1220 // Compute the offset where the CPE will begin.
1221 unsigned CPEOffset
= UserBBI
.postOffset() + Delta
;
1223 if (isOffsetInRange(UserOffset
, CPEOffset
, U
)) {
1224 LLVM_DEBUG(dbgs() << "Split at end of " << printMBBReference(*UserMBB
)
1225 << format(", expected CPE offset %#x\n", CPEOffset
));
1226 NewMBB
= &*++UserMBB
->getIterator();
1227 // Add an unconditional branch from UserMBB to fallthrough block. Record
1228 // it for branch lengthening; this new branch will not get out of range,
1229 // but if the preceding conditional branch is out of range, the targets
1230 // will be exchanged, and the altered branch may be out of range, so the
1231 // machinery has to know about it.
1232 int UncondBr
= Mips::Bimm16
;
1233 BuildMI(UserMBB
, DebugLoc(), TII
->get(UncondBr
)).addMBB(NewMBB
);
1234 unsigned MaxDisp
= getUnconditionalBrDisp(UncondBr
);
1235 ImmBranches
.push_back(ImmBranch(&UserMBB
->back(),
1236 MaxDisp
, false, UncondBr
));
1237 BBInfo
[UserMBB
->getNumber()].Size
+= Delta
;
1238 adjustBBOffsetsAfter(UserMBB
);
1243 // What a big block. Find a place within the block to split it.
1245 // Try to split the block so it's fully aligned. Compute the latest split
1246 // point where we can add a 4-byte branch instruction, and then align to
1247 // Align which is the largest possible alignment in the function.
1248 const Align Align
= MF
->getAlignment();
1249 unsigned BaseInsertOffset
= UserOffset
+ U
.getMaxDisp();
1250 LLVM_DEBUG(dbgs() << format("Split in middle of big block before %#x",
1253 // The 4 in the following is for the unconditional branch we'll be inserting
1254 // Alignment of the island is handled
1255 // inside isOffsetInRange.
1256 BaseInsertOffset
-= 4;
1258 LLVM_DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset
)
1259 << " la=" << Log2(Align
) << '\n');
1261 // This could point off the end of the block if we've already got constant
1262 // pool entries following this block; only the last one is in the water list.
1263 // Back past any possible branches (allow for a conditional and a maximally
1264 // long unconditional).
1265 if (BaseInsertOffset
+ 8 >= UserBBI
.postOffset()) {
1266 BaseInsertOffset
= UserBBI
.postOffset() - 8;
1267 LLVM_DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset
));
1269 unsigned EndInsertOffset
= BaseInsertOffset
+ 4 +
1270 CPEMI
->getOperand(2).getImm();
1271 MachineBasicBlock::iterator MI
= UserMI
;
1273 unsigned CPUIndex
= CPUserIndex
+1;
1274 unsigned NumCPUsers
= CPUsers
.size();
1275 //MachineInstr *LastIT = 0;
1276 for (unsigned Offset
= UserOffset
+ TII
->getInstSizeInBytes(*UserMI
);
1277 Offset
< BaseInsertOffset
;
1278 Offset
+= TII
->getInstSizeInBytes(*MI
), MI
= std::next(MI
)) {
1279 assert(MI
!= UserMBB
->end() && "Fell off end of block");
1280 if (CPUIndex
< NumCPUsers
&& CPUsers
[CPUIndex
].MI
== MI
) {
1281 CPUser
&U
= CPUsers
[CPUIndex
];
1282 if (!isOffsetInRange(Offset
, EndInsertOffset
, U
)) {
1283 // Shift intertion point by one unit of alignment so it is within reach.
1284 BaseInsertOffset
-= Align
.value();
1285 EndInsertOffset
-= Align
.value();
1287 // This is overly conservative, as we don't account for CPEMIs being
1288 // reused within the block, but it doesn't matter much. Also assume CPEs
1289 // are added in order with alignment padding. We may eventually be able
1290 // to pack the aligned CPEs better.
1291 EndInsertOffset
+= U
.CPEMI
->getOperand(2).getImm();
1296 NewMBB
= splitBlockBeforeInstr(*--MI
);
1299 /// handleConstantPoolUser - Analyze the specified user, checking to see if it
1300 /// is out-of-range. If so, pick up the constant pool value and move it some
1301 /// place in-range. Return true if we changed any addresses (thus must run
1302 /// another pass of branch lengthening), false otherwise.
1303 bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex
) {
1304 CPUser
&U
= CPUsers
[CPUserIndex
];
1305 MachineInstr
*UserMI
= U
.MI
;
1306 MachineInstr
*CPEMI
= U
.CPEMI
;
1307 unsigned CPI
= CPEMI
->getOperand(1).getIndex();
1308 unsigned Size
= CPEMI
->getOperand(2).getImm();
1309 // Compute this only once, it's expensive.
1310 unsigned UserOffset
= getUserOffset(U
);
1312 // See if the current entry is within range, or there is a clone of it
1314 int result
= findInRangeCPEntry(U
, UserOffset
);
1315 if (result
==1) return false;
1316 else if (result
==2) return true;
1318 // Look for water where we can place this CPE.
1319 MachineBasicBlock
*NewIsland
= MF
->CreateMachineBasicBlock();
1320 MachineBasicBlock
*NewMBB
;
1322 if (findAvailableWater(U
, UserOffset
, IP
)) {
1323 LLVM_DEBUG(dbgs() << "Found water in range\n");
1324 MachineBasicBlock
*WaterBB
= *IP
;
1326 // If the original WaterList entry was "new water" on this iteration,
1327 // propagate that to the new island. This is just keeping NewWaterList
1328 // updated to match the WaterList, which will be updated below.
1329 if (NewWaterList
.erase(WaterBB
))
1330 NewWaterList
.insert(NewIsland
);
1332 // The new CPE goes before the following block (NewMBB).
1333 NewMBB
= &*++WaterBB
->getIterator();
1336 // we first see if a longer form of the instrucion could have reached
1337 // the constant. in that case we won't bother to split
1338 if (!NoLoadRelaxation
) {
1339 result
= findLongFormInRangeCPEntry(U
, UserOffset
);
1340 if (result
!= 0) return true;
1342 LLVM_DEBUG(dbgs() << "No water found\n");
1343 createNewWater(CPUserIndex
, UserOffset
, NewMBB
);
1345 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1346 // called while handling branches so that the water will be seen on the
1347 // next iteration for constant pools, but in this context, we don't want
1348 // it. Check for this so it will be removed from the WaterList.
1349 // Also remove any entry from NewWaterList.
1350 MachineBasicBlock
*WaterBB
= &*--NewMBB
->getIterator();
1351 IP
= llvm::find(WaterList
, WaterBB
);
1352 if (IP
!= WaterList
.end())
1353 NewWaterList
.erase(WaterBB
);
1355 // We are adding new water. Update NewWaterList.
1356 NewWaterList
.insert(NewIsland
);
1359 // Remove the original WaterList entry; we want subsequent insertions in
1360 // this vicinity to go after the one we're about to insert. This
1361 // considerably reduces the number of times we have to move the same CPE
1362 // more than once and is also important to ensure the algorithm terminates.
1363 if (IP
!= WaterList
.end())
1364 WaterList
.erase(IP
);
1366 // Okay, we know we can put an island before NewMBB now, do it!
1367 MF
->insert(NewMBB
->getIterator(), NewIsland
);
1369 // Update internal data structures to account for the newly inserted MBB.
1370 updateForInsertedWaterBlock(NewIsland
);
1372 // Decrement the old entry, and remove it if refcount becomes 0.
1373 decrementCPEReferenceCount(CPI
, CPEMI
);
1375 // No existing clone of this CPE is within range.
1376 // We will be generating a new clone. Get a UID for it.
1377 unsigned ID
= createPICLabelUId();
1379 // Now that we have an island to add the CPE to, clone the original CPE and
1380 // add it to the island.
1381 U
.HighWaterMark
= NewIsland
;
1382 U
.CPEMI
= BuildMI(NewIsland
, DebugLoc(), TII
->get(Mips::CONSTPOOL_ENTRY
))
1383 .addImm(ID
).addConstantPoolIndex(CPI
).addImm(Size
);
1384 CPEntries
[CPI
].push_back(CPEntry(U
.CPEMI
, ID
, 1));
1387 // Mark the basic block as aligned as required by the const-pool entry.
1388 NewIsland
->setAlignment(getCPEAlign(*U
.CPEMI
));
1390 // Increase the size of the island block to account for the new entry.
1391 BBInfo
[NewIsland
->getNumber()].Size
+= Size
;
1392 adjustBBOffsetsAfter(&*--NewIsland
->getIterator());
1394 // Finally, change the CPI in the instruction operand to be ID.
1395 for (MachineOperand
&MO
: UserMI
->operands())
1402 dbgs() << " Moved CPE to #" << ID
<< " CPI=" << CPI
1403 << format(" offset=%#x\n", BBInfo
[NewIsland
->getNumber()].Offset
));
1408 /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1409 /// sizes and offsets of impacted basic blocks.
1410 void MipsConstantIslands::removeDeadCPEMI(MachineInstr
*CPEMI
) {
1411 MachineBasicBlock
*CPEBB
= CPEMI
->getParent();
1412 unsigned Size
= CPEMI
->getOperand(2).getImm();
1413 CPEMI
->eraseFromParent();
1414 BBInfo
[CPEBB
->getNumber()].Size
-= Size
;
1415 // All succeeding offsets have the current size value added in, fix this.
1416 if (CPEBB
->empty()) {
1417 BBInfo
[CPEBB
->getNumber()].Size
= 0;
1419 // This block no longer needs to be aligned.
1420 CPEBB
->setAlignment(Align(1));
1422 // Entries are sorted by descending alignment, so realign from the front.
1423 CPEBB
->setAlignment(getCPEAlign(*CPEBB
->begin()));
1426 adjustBBOffsetsAfter(CPEBB
);
1427 // An island has only one predecessor BB and one successor BB. Check if
1428 // this BB's predecessor jumps directly to this BB's successor. This
1429 // shouldn't happen currently.
1430 assert(!BBIsJumpedOver(CPEBB
) && "How did this happen?");
1431 // FIXME: remove the empty blocks after all the work is done?
1434 /// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1436 bool MipsConstantIslands::removeUnusedCPEntries() {
1437 unsigned MadeChange
= false;
1438 for (std::vector
<CPEntry
> &CPEs
: CPEntries
) {
1439 for (CPEntry
&CPE
: CPEs
) {
1440 if (CPE
.RefCount
== 0 && CPE
.CPEMI
) {
1441 removeDeadCPEMI(CPE
.CPEMI
);
1442 CPE
.CPEMI
= nullptr;
1450 /// isBBInRange - Returns true if the distance between specific MI and
1451 /// specific BB can fit in MI's displacement field.
1452 bool MipsConstantIslands::isBBInRange
1453 (MachineInstr
*MI
,MachineBasicBlock
*DestBB
, unsigned MaxDisp
) {
1455 unsigned BrOffset
= getOffsetOf(MI
) + PCAdj
;
1456 unsigned DestOffset
= BBInfo
[DestBB
->getNumber()].Offset
;
1458 LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB
)
1459 << " from " << printMBBReference(*MI
->getParent())
1460 << " max delta=" << MaxDisp
<< " from " << getOffsetOf(MI
)
1461 << " to " << DestOffset
<< " offset "
1462 << int(DestOffset
- BrOffset
) << "\t" << *MI
);
1464 if (BrOffset
<= DestOffset
) {
1465 // Branch before the Dest.
1466 if (DestOffset
-BrOffset
<= MaxDisp
)
1469 if (BrOffset
-DestOffset
<= MaxDisp
)
1475 /// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1476 /// away to fit in its displacement field.
1477 bool MipsConstantIslands::fixupImmediateBr(ImmBranch
&Br
) {
1478 MachineInstr
*MI
= Br
.MI
;
1479 unsigned TargetOperand
= branchTargetOperand(MI
);
1480 MachineBasicBlock
*DestBB
= MI
->getOperand(TargetOperand
).getMBB();
1482 // Check to see if the DestBB is already in-range.
1483 if (isBBInRange(MI
, DestBB
, Br
.MaxDisp
))
1487 return fixupUnconditionalBr(Br
);
1488 return fixupConditionalBr(Br
);
1491 /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1492 /// too far away to fit in its displacement field. If the LR register has been
1493 /// spilled in the epilogue, then we can use BL to implement a far jump.
1494 /// Otherwise, add an intermediate branch instruction to a branch.
1496 MipsConstantIslands::fixupUnconditionalBr(ImmBranch
&Br
) {
1497 MachineInstr
*MI
= Br
.MI
;
1498 MachineBasicBlock
*MBB
= MI
->getParent();
1499 MachineBasicBlock
*DestBB
= MI
->getOperand(0).getMBB();
1500 // Use BL to implement far jump.
1501 unsigned BimmX16MaxDisp
= ((1 << 16)-1) * 2;
1502 if (isBBInRange(MI
, DestBB
, BimmX16MaxDisp
)) {
1503 Br
.MaxDisp
= BimmX16MaxDisp
;
1504 MI
->setDesc(TII
->get(Mips::BimmX16
));
1507 // need to give the math a more careful look here
1508 // this is really a segment address and not
1509 // a PC relative address. FIXME. But I think that
1510 // just reducing the bits by 1 as I've done is correct.
1511 // The basic block we are branching too much be longword aligned.
1512 // we know that RA is saved because we always save it right now.
1513 // this requirement will be relaxed later but we also have an alternate
1514 // way to implement this that I will implement that does not need jal.
1515 // We should have a way to back out this alignment restriction
1516 // if we "can" later. but it is not harmful.
1518 DestBB
->setAlignment(Align(4));
1519 Br
.MaxDisp
= ((1<<24)-1) * 2;
1520 MI
->setDesc(TII
->get(Mips::JalB16
));
1522 BBInfo
[MBB
->getNumber()].Size
+= 2;
1523 adjustBBOffsetsAfter(MBB
);
1527 LLVM_DEBUG(dbgs() << " Changed B to long jump " << *MI
);
1532 /// fixupConditionalBr - Fix up a conditional branch whose destination is too
1533 /// far away to fit in its displacement field. It is converted to an inverse
1534 /// conditional branch + an unconditional branch to the destination.
1536 MipsConstantIslands::fixupConditionalBr(ImmBranch
&Br
) {
1537 MachineInstr
*MI
= Br
.MI
;
1538 unsigned TargetOperand
= branchTargetOperand(MI
);
1539 MachineBasicBlock
*DestBB
= MI
->getOperand(TargetOperand
).getMBB();
1540 unsigned Opcode
= MI
->getOpcode();
1541 unsigned LongFormOpcode
= longformBranchOpcode(Opcode
);
1542 unsigned LongFormMaxOff
= branchMaxOffsets(LongFormOpcode
);
1544 // Check to see if the DestBB is already in-range.
1545 if (isBBInRange(MI
, DestBB
, LongFormMaxOff
)) {
1546 Br
.MaxDisp
= LongFormMaxOff
;
1547 MI
->setDesc(TII
->get(LongFormOpcode
));
1551 // Add an unconditional branch to the destination and invert the branch
1552 // condition to jump over it:
1559 // If the branch is at the end of its MBB and that has a fall-through block,
1560 // direct the updated conditional branch to the fall-through block. Otherwise,
1561 // split the MBB before the next instruction.
1562 MachineBasicBlock
*MBB
= MI
->getParent();
1563 MachineInstr
*BMI
= &MBB
->back();
1564 bool NeedSplit
= (BMI
!= MI
) || !BBHasFallthrough(MBB
);
1565 unsigned OppositeBranchOpcode
= TII
->getOppositeBranchOpc(Opcode
);
1569 if (std::next(MachineBasicBlock::iterator(MI
)) == std::prev(MBB
->end()) &&
1570 BMI
->isUnconditionalBranch()) {
1571 // Last MI in the BB is an unconditional branch. Can we simply invert the
1572 // condition and swap destinations:
1578 unsigned BMITargetOperand
= branchTargetOperand(BMI
);
1579 MachineBasicBlock
*NewDest
=
1580 BMI
->getOperand(BMITargetOperand
).getMBB();
1581 if (isBBInRange(MI
, NewDest
, Br
.MaxDisp
)) {
1583 dbgs() << " Invert Bcc condition and swap its destination with "
1585 MI
->setDesc(TII
->get(OppositeBranchOpcode
));
1586 BMI
->getOperand(BMITargetOperand
).setMBB(DestBB
);
1587 MI
->getOperand(TargetOperand
).setMBB(NewDest
);
1594 splitBlockBeforeInstr(*MI
);
1595 // No need for the branch to the next block. We're adding an unconditional
1596 // branch to the destination.
1597 int delta
= TII
->getInstSizeInBytes(MBB
->back());
1598 BBInfo
[MBB
->getNumber()].Size
-= delta
;
1599 MBB
->back().eraseFromParent();
1600 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1602 MachineBasicBlock
*NextBB
= &*++MBB
->getIterator();
1604 LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*DestBB
)
1605 << " also invert condition and change dest. to "
1606 << printMBBReference(*NextBB
) << "\n");
1608 // Insert a new conditional branch and a new unconditional branch.
1609 // Also update the ImmBranch as well as adding a new entry for the new branch.
1610 if (MI
->getNumExplicitOperands() == 2) {
1611 BuildMI(MBB
, DebugLoc(), TII
->get(OppositeBranchOpcode
))
1612 .addReg(MI
->getOperand(0).getReg())
1615 BuildMI(MBB
, DebugLoc(), TII
->get(OppositeBranchOpcode
))
1618 Br
.MI
= &MBB
->back();
1619 BBInfo
[MBB
->getNumber()].Size
+= TII
->getInstSizeInBytes(MBB
->back());
1620 BuildMI(MBB
, DebugLoc(), TII
->get(Br
.UncondBr
)).addMBB(DestBB
);
1621 BBInfo
[MBB
->getNumber()].Size
+= TII
->getInstSizeInBytes(MBB
->back());
1622 unsigned MaxDisp
= getUnconditionalBrDisp(Br
.UncondBr
);
1623 ImmBranches
.push_back(ImmBranch(&MBB
->back(), MaxDisp
, false, Br
.UncondBr
));
1625 // Remove the old conditional branch. It may or may not still be in MBB.
1626 BBInfo
[MI
->getParent()->getNumber()].Size
-= TII
->getInstSizeInBytes(*MI
);
1627 MI
->eraseFromParent();
1628 adjustBBOffsetsAfter(MBB
);
1632 void MipsConstantIslands::prescanForConstants() {
1635 for (MachineBasicBlock
&B
: *MF
) {
1636 for (MachineBasicBlock::instr_iterator I
= B
.instr_begin(),
1639 switch(I
->getDesc().getOpcode()) {
1640 case Mips::LwConstant32
: {
1641 PrescannedForConstants
= true;
1642 LLVM_DEBUG(dbgs() << "constant island constant " << *I
<< "\n");
1643 J
= I
->getNumOperands();
1644 LLVM_DEBUG(dbgs() << "num operands " << J
<< "\n");
1645 MachineOperand
& Literal
= I
->getOperand(1);
1646 if (Literal
.isImm()) {
1647 int64_t V
= Literal
.getImm();
1648 LLVM_DEBUG(dbgs() << "literal " << V
<< "\n");
1650 Type::getInt32Ty(MF
->getFunction().getContext());
1651 const Constant
*C
= ConstantInt::get(Int32Ty
, V
);
1652 unsigned index
= MCP
->getConstantPoolIndex(C
, Align(4));
1653 I
->getOperand(2).ChangeToImmediate(index
);
1654 LLVM_DEBUG(dbgs() << "constant island constant " << *I
<< "\n");
1655 I
->setDesc(TII
->get(Mips::LwRxPcTcp16
));
1656 I
->removeOperand(1);
1657 I
->removeOperand(1);
1658 I
->addOperand(MachineOperand::CreateCPI(index
, 0));
1659 I
->addOperand(MachineOperand::CreateImm(4));
1670 /// Returns a pass that converts branches to long branches.
1671 FunctionPass
*llvm::createMipsConstantIslandPass() {
1672 return new MipsConstantIslands();