blob: 5fca5ff2831610b30fc7fdece6056729a10fc257 [file] [log] [blame]
Reed Kotler5bf80202013-02-27 04:20:14 +00001//===-- MipsConstantIslandPass.cpp - Emit Pc Relative loads----------------===//
Reed Kotlerbb3094a2013-02-27 03:33:58 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11// This pass is used to make Pc relative loads of constants.
Reed Kotler4d0313d2013-11-05 12:04:37 +000012// For now, only Mips16 will use this.
Reed Kotlerbb3094a2013-02-27 03:33:58 +000013//
14// Loading constants inline is expensive on Mips16 and it's in general better
15// to place the constant nearby in code space and then it can be loaded with a
16// simple 16 bit load instruction.
17//
18// The constants can be not just numbers but addresses of functions and labels.
19// This can be particularly helpful in static relocation mode for embedded
Alp Tokerf907b892013-12-05 05:44:44 +000020// non-linux targets.
Reed Kotlerbb3094a2013-02-27 03:33:58 +000021//
22//
23
Reed Kotlerbb3094a2013-02-27 03:33:58 +000024#include "Mips.h"
25#include "MCTargetDesc/MipsBaseInfo.h"
Reed Kotler5c8ae092013-11-13 04:37:52 +000026#include "Mips16InstrInfo.h"
Reed Kotler0f007fc2013-11-05 08:14:14 +000027#include "MipsMachineFunction.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000028#include "MipsTargetMachine.h"
29#include "llvm/ADT/Statistic.h"
Reed Kotler91ae9822013-10-27 21:57:36 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000031#include "llvm/CodeGen/MachineConstantPool.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Reed Kotler91ae9822013-10-27 21:57:36 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000035#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000036#include "llvm/IR/InstIterator.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000037#include "llvm/Support/CommandLine.h"
Reed Kotler91ae9822013-10-27 21:57:36 +000038#include "llvm/Support/Debug.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000039#include "llvm/Support/Format.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000040#include "llvm/Support/MathExtras.h"
Reed Kotler91ae9822013-10-27 21:57:36 +000041#include "llvm/Support/raw_ostream.h"
Reed Kotlerbb3094a2013-02-27 03:33:58 +000042#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Target/TargetRegisterInfo.h"
Reed Kotler91ae9822013-10-27 21:57:36 +000045#include <algorithm>
Reed Kotlerbb3094a2013-02-27 03:33:58 +000046
47using namespace llvm;
48
Chandler Carruth84e68b22014-04-22 02:41:26 +000049#define DEBUG_TYPE "mips-constant-islands"
50
Reed Kotler91ae9822013-10-27 21:57:36 +000051STATISTIC(NumCPEs, "Number of constpool entries");
Reed Kotler0f007fc2013-11-05 08:14:14 +000052STATISTIC(NumSplit, "Number of uncond branches inserted");
53STATISTIC(NumCBrFixed, "Number of cond branches fixed");
54STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
Reed Kotler91ae9822013-10-27 21:57:36 +000055
56// FIXME: This option should be removed once it has received sufficient testing.
57static cl::opt<bool>
58AlignConstantIslands("mips-align-constant-islands", cl::Hidden, cl::init(true),
59 cl::desc("Align constant islands in code"));
60
Reed Kotler0f007fc2013-11-05 08:14:14 +000061
62// Rather than do make check tests with huge amounts of code, we force
63// the test to use this amount.
64//
65static cl::opt<int> ConstantIslandsSmallOffset(
66 "mips-constant-islands-small-offset",
67 cl::init(0),
68 cl::desc("Make small offsets be this amount for testing purposes"),
69 cl::Hidden);
70
Reed Kotler45c59272013-11-10 00:09:26 +000071//
72// For testing purposes we tell it to not use relaxed load forms so that it
73// will split blocks.
74//
75static cl::opt<bool> NoLoadRelaxation(
76 "mips-constant-islands-no-load-relaxation",
77 cl::init(false),
78 cl::desc("Don't relax loads to long loads - for testing purposes"),
79 cl::Hidden);
80
Reed Kotler0d409e22013-11-28 00:56:37 +000081static unsigned int branchTargetOperand(MachineInstr *MI) {
82 switch (MI->getOpcode()) {
83 case Mips::Bimm16:
84 case Mips::BimmX16:
85 case Mips::Bteqz16:
86 case Mips::BteqzX16:
87 case Mips::Btnez16:
88 case Mips::BtnezX16:
Reed Kotlerad450f22013-11-29 22:32:56 +000089 case Mips::JalB16:
Reed Kotler0d409e22013-11-28 00:56:37 +000090 return 0;
91 case Mips::BeqzRxImm16:
92 case Mips::BeqzRxImmX16:
93 case Mips::BnezRxImm16:
94 case Mips::BnezRxImmX16:
95 return 1;
96 }
97 llvm_unreachable("Unknown branch type");
98}
99
100static unsigned int longformBranchOpcode(unsigned int Opcode) {
101 switch (Opcode) {
102 case Mips::Bimm16:
103 case Mips::BimmX16:
104 return Mips::BimmX16;
105 case Mips::Bteqz16:
106 case Mips::BteqzX16:
107 return Mips::BteqzX16;
108 case Mips::Btnez16:
109 case Mips::BtnezX16:
110 return Mips::BtnezX16;
Reed Kotlerad450f22013-11-29 22:32:56 +0000111 case Mips::JalB16:
112 return Mips::JalB16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000113 case Mips::BeqzRxImm16:
114 case Mips::BeqzRxImmX16:
115 return Mips::BeqzRxImmX16;
116 case Mips::BnezRxImm16:
117 case Mips::BnezRxImmX16:
118 return Mips::BnezRxImmX16;
119 }
120 llvm_unreachable("Unknown branch type");
121}
122
123//
124// FIXME: need to go through this whole constant islands port and check the math
125// for branch ranges and clean this up and make some functions to calculate things
126// that are done many times identically.
127// Need to refactor some of the code to call this routine.
128//
129static unsigned int branchMaxOffsets(unsigned int Opcode) {
130 unsigned Bits, Scale;
131 switch (Opcode) {
132 case Mips::Bimm16:
133 Bits = 11;
134 Scale = 2;
135 break;
136 case Mips::BimmX16:
137 Bits = 16;
138 Scale = 2;
139 break;
140 case Mips::BeqzRxImm16:
141 Bits = 8;
142 Scale = 2;
143 break;
144 case Mips::BeqzRxImmX16:
145 Bits = 16;
146 Scale = 2;
147 break;
148 case Mips::BnezRxImm16:
149 Bits = 8;
150 Scale = 2;
151 break;
152 case Mips::BnezRxImmX16:
153 Bits = 16;
154 Scale = 2;
155 break;
156 case Mips::Bteqz16:
157 Bits = 8;
158 Scale = 2;
159 break;
160 case Mips::BteqzX16:
161 Bits = 16;
162 Scale = 2;
163 break;
164 case Mips::Btnez16:
165 Bits = 8;
166 Scale = 2;
167 break;
168 case Mips::BtnezX16:
169 Bits = 16;
170 Scale = 2;
171 break;
172 default:
173 llvm_unreachable("Unknown branch type");
174 }
175 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
176 return MaxOffs;
177}
Reed Kotler0f007fc2013-11-05 08:14:14 +0000178
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000179namespace {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000180
181
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000182 typedef MachineBasicBlock::iterator Iter;
183 typedef MachineBasicBlock::reverse_iterator ReverseIter;
184
Reed Kotler0f007fc2013-11-05 08:14:14 +0000185 /// MipsConstantIslands - Due to limited PC-relative displacements, Mips
186 /// requires constant pool entries to be scattered among the instructions
187 /// inside a function. To do this, it completely ignores the normal LLVM
188 /// constant pool; instead, it places constants wherever it feels like with
189 /// special instructions.
190 ///
191 /// The terminology used in this pass includes:
192 /// Islands - Clumps of constants placed in the function.
193 /// Water - Potential places where an island could be formed.
194 /// CPE - A constant pool entry that has been placed somewhere, which
195 /// tracks a list of users.
196
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000197 class MipsConstantIslands : public MachineFunctionPass {
198
Reed Kotler0f007fc2013-11-05 08:14:14 +0000199 /// BasicBlockInfo - Information about the offset and size of a single
200 /// basic block.
201 struct BasicBlockInfo {
202 /// Offset - Distance from the beginning of the function to the beginning
203 /// of this basic block.
204 ///
205 /// Offsets are computed assuming worst case padding before an aligned
206 /// block. This means that subtracting basic block offsets always gives a
207 /// conservative estimate of the real distance which may be smaller.
208 ///
209 /// Because worst case padding is used, the computed offset of an aligned
210 /// block may not actually be aligned.
211 unsigned Offset;
212
213 /// Size - Size of the basic block in bytes. If the block contains
214 /// inline assembly, this is a worst case estimate.
215 ///
216 /// The size does not include any alignment padding whether from the
217 /// beginning of the block, or from an aligned jump table at the end.
218 unsigned Size;
219
Reed Kotler7ded5b62013-11-05 23:36:58 +0000220 // FIXME: ignore LogAlign for this patch
221 //
Reed Kotler0f007fc2013-11-05 08:14:14 +0000222 unsigned postOffset(unsigned LogAlign = 0) const {
223 unsigned PO = Offset + Size;
224 return PO;
225 }
226
Reed Kotler7ded5b62013-11-05 23:36:58 +0000227 BasicBlockInfo() : Offset(0), Size(0) {}
228
Reed Kotler0f007fc2013-11-05 08:14:14 +0000229 };
230
231 std::vector<BasicBlockInfo> BBInfo;
232
233 /// WaterList - A sorted list of basic blocks where islands could be placed
234 /// (i.e. blocks that don't fall through to the following block, due
235 /// to a return, unreachable, or unconditional branch).
236 std::vector<MachineBasicBlock*> WaterList;
237
238 /// NewWaterList - The subset of WaterList that was created since the
239 /// previous iteration by inserting unconditional branches.
240 SmallSet<MachineBasicBlock*, 4> NewWaterList;
241
242 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
243
244 /// CPUser - One user of a constant pool, keeping the machine instruction
245 /// pointer, the constant pool being referenced, and the max displacement
246 /// allowed from the instruction to the CP. The HighWaterMark records the
247 /// highest basic block where a new CPEntry can be placed. To ensure this
248 /// pass terminates, the CP entries are initially placed at the end of the
249 /// function and then move monotonically to lower addresses. The
250 /// exception to this rule is when the current CP entry for a particular
251 /// CPUser is out of range, but there is another CP entry for the same
252 /// constant value in range. We want to use the existing in-range CP
253 /// entry, but if it later moves out of range, the search for new water
254 /// should resume where it left off. The HighWaterMark is used to record
255 /// that point.
256 struct CPUser {
257 MachineInstr *MI;
258 MachineInstr *CPEMI;
259 MachineBasicBlock *HighWaterMark;
260 private:
261 unsigned MaxDisp;
262 unsigned LongFormMaxDisp; // mips16 has 16/32 bit instructions
263 // with different displacements
264 unsigned LongFormOpcode;
265 public:
266 bool NegOk;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000267 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
Reed Kotlerb09ebe92013-11-05 22:34:29 +0000268 bool neg,
Reed Kotler0f007fc2013-11-05 08:14:14 +0000269 unsigned longformmaxdisp, unsigned longformopcode)
270 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp),
271 LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode),
Reed Kotler7ded5b62013-11-05 23:36:58 +0000272 NegOk(neg){
Reed Kotler0f007fc2013-11-05 08:14:14 +0000273 HighWaterMark = CPEMI->getParent();
274 }
275 /// getMaxDisp - Returns the maximum displacement supported by MI.
Reed Kotler0f007fc2013-11-05 08:14:14 +0000276 unsigned getMaxDisp() const {
277 unsigned xMaxDisp = ConstantIslandsSmallOffset?
278 ConstantIslandsSmallOffset: MaxDisp;
Reed Kotler7ded5b62013-11-05 23:36:58 +0000279 return xMaxDisp;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000280 }
Reed Kotler45c59272013-11-10 00:09:26 +0000281 void setMaxDisp(unsigned val) {
282 MaxDisp = val;
283 }
Reed Kotler0f007fc2013-11-05 08:14:14 +0000284 unsigned getLongFormMaxDisp() const {
Reed Kotler7ded5b62013-11-05 23:36:58 +0000285 return LongFormMaxDisp;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000286 }
287 unsigned getLongFormOpcode() const {
288 return LongFormOpcode;
289 }
290 };
291
292 /// CPUsers - Keep track of all of the machine instructions that use various
293 /// constant pools and their max displacement.
294 std::vector<CPUser> CPUsers;
Reed Kotler91ae9822013-10-27 21:57:36 +0000295
296 /// CPEntry - One per constant pool entry, keeping the machine instruction
297 /// pointer, the constpool index, and the number of CPUser's which
298 /// reference this entry.
299 struct CPEntry {
300 MachineInstr *CPEMI;
301 unsigned CPI;
302 unsigned RefCount;
303 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
304 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
305 };
306
307 /// CPEntries - Keep track of all of the constant pool entry machine
308 /// instructions. For each original constpool index (i.e. those that
309 /// existed upon entry to this pass), it keeps a vector of entries.
310 /// Original elements are cloned as we go along; the clones are
311 /// put in the vector of the original element, but have distinct CPIs.
312 std::vector<std::vector<CPEntry> > CPEntries;
313
Reed Kotler0f007fc2013-11-05 08:14:14 +0000314 /// ImmBranch - One per immediate branch, keeping the machine instruction
315 /// pointer, conditional or unconditional, the max displacement,
316 /// and (if isCond is true) the corresponding unconditional branch
317 /// opcode.
318 struct ImmBranch {
319 MachineInstr *MI;
320 unsigned MaxDisp : 31;
321 bool isCond : 1;
322 int UncondBr;
323 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
324 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
325 };
326
327 /// ImmBranches - Keep track of all the immediate branch instructions.
328 ///
329 std::vector<ImmBranch> ImmBranches;
330
331 /// HasFarJump - True if any far jump instruction has been emitted during
332 /// the branch fix up pass.
333 bool HasFarJump;
334
Reed Kotler0f007fc2013-11-05 08:14:14 +0000335 const MipsSubtarget *STI;
Reed Kotler5c8ae092013-11-13 04:37:52 +0000336 const Mips16InstrInfo *TII;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000337 MipsFunctionInfo *MFI;
338 MachineFunction *MF;
339 MachineConstantPool *MCP;
340
341 unsigned PICLabelUId;
342 bool PrescannedForConstants;
343
344 void initPICLabelUId(unsigned UId) {
345 PICLabelUId = UId;
346 }
347
348
349 unsigned createPICLabelUId() {
350 return PICLabelUId++;
351 }
352
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000353 public:
354 static char ID;
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000355 MipsConstantIslands()
356 : MachineFunctionPass(ID), STI(nullptr), MF(nullptr), MCP(nullptr),
357 PrescannedForConstants(false) {}
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000358
Mehdi Amini117296c2016-10-01 02:56:57 +0000359 StringRef getPassName() const override { return "Mips Constant Islands"; }
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000360
Craig Topper56c590a2014-04-29 07:58:02 +0000361 bool runOnMachineFunction(MachineFunction &F) override;
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000362
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000363 MachineFunctionProperties getRequiredProperties() const override {
364 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000365 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000366 }
367
Reed Kotler91ae9822013-10-27 21:57:36 +0000368 void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000369 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000370 unsigned getCPELogAlign(const MachineInstr &CPEMI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000371 void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
372 unsigned getOffsetOf(MachineInstr *MI) const;
373 unsigned getUserOffset(CPUser&) const;
374 void dumpBBs();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000375
376 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
Reed Kotlerb09ebe92013-11-05 22:34:29 +0000377 unsigned Disp, bool NegativeOK);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000378 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
379 const CPUser &U);
380
Reed Kotler0f007fc2013-11-05 08:14:14 +0000381 void computeBlockSize(MachineBasicBlock *MBB);
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000382 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000383 void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
384 void adjustBBOffsetsAfter(MachineBasicBlock *BB);
385 bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
386 int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
387 int findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset);
388 bool findAvailableWater(CPUser&U, unsigned UserOffset,
389 water_iterator &WaterIter);
390 void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
391 MachineBasicBlock *&NewMBB);
392 bool handleConstantPoolUser(unsigned CPUserIndex);
393 void removeDeadCPEMI(MachineInstr *CPEMI);
394 bool removeUnusedCPEntries();
395 bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
396 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
397 bool DoDump = false);
398 bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
399 CPUser &U, unsigned &Growth);
400 bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
401 bool fixupImmediateBr(ImmBranch &Br);
402 bool fixupConditionalBr(ImmBranch &Br);
403 bool fixupUnconditionalBr(ImmBranch &Br);
Reed Kotler91ae9822013-10-27 21:57:36 +0000404
405 void prescanForConstants();
406
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000407 private:
Reed Kotler91ae9822013-10-27 21:57:36 +0000408
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000409 };
410
411 char MipsConstantIslands::ID = 0;
412} // end of anonymous namespace
413
Reed Kotler0f007fc2013-11-05 08:14:14 +0000414bool MipsConstantIslands::isOffsetInRange
415 (unsigned UserOffset, unsigned TrialOffset,
416 const CPUser &U) {
417 return isOffsetInRange(UserOffset, TrialOffset,
Reed Kotlerb09ebe92013-11-05 22:34:29 +0000418 U.getMaxDisp(), U.NegOk);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000419}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000420#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reed Kotler0f007fc2013-11-05 08:14:14 +0000421/// print block size and offset information - debugging
Matthias Braun8c209aa2017-01-28 02:02:38 +0000422LLVM_DUMP_METHOD void MipsConstantIslands::dumpBBs() {
423 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
424 const BasicBlockInfo &BBI = BBInfo[J];
425 dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
426 << format(" size=%#x\n", BBInfo[J].Size);
427 }
Reed Kotler0f007fc2013-11-05 08:14:14 +0000428}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000429#endif
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000430/// Returns a pass that converts branches to long branches.
431FunctionPass *llvm::createMipsConstantIslandPass() {
432 return new MipsConstantIslands();
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000433}
434
Reed Kotler91ae9822013-10-27 21:57:36 +0000435bool MipsConstantIslands::runOnMachineFunction(MachineFunction &mf) {
Reed Kotler1595f362013-04-09 19:46:01 +0000436 // The intention is for this to be a mips16 only pass for now
437 // FIXME:
Reed Kotler91ae9822013-10-27 21:57:36 +0000438 MF = &mf;
439 MCP = mf.getConstantPool();
Eric Christopher96e72c62015-01-29 23:27:36 +0000440 STI = &static_cast<const MipsSubtarget &>(mf.getSubtarget());
Reed Kotler91ae9822013-10-27 21:57:36 +0000441 DEBUG(dbgs() << "constant island machine function " << "\n");
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000442 if (!STI->inMips16Mode() || !MipsSubtarget::useConstantIslands()) {
Reed Kotler91ae9822013-10-27 21:57:36 +0000443 return false;
444 }
Eric Christopher96e72c62015-01-29 23:27:36 +0000445 TII = (const Mips16InstrInfo *)STI->getInstrInfo();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000446 MFI = MF->getInfo<MipsFunctionInfo>();
Reed Kotler91ae9822013-10-27 21:57:36 +0000447 DEBUG(dbgs() << "constant island processing " << "\n");
448 //
449 // will need to make predermination if there is any constants we need to
450 // put in constant islands. TBD.
451 //
Reed Kotler0f007fc2013-11-05 08:14:14 +0000452 if (!PrescannedForConstants) prescanForConstants();
Reed Kotler91ae9822013-10-27 21:57:36 +0000453
Reed Kotler0f007fc2013-11-05 08:14:14 +0000454 HasFarJump = false;
Reed Kotler91ae9822013-10-27 21:57:36 +0000455 // This pass invalidates liveness information when it splits basic blocks.
456 MF->getRegInfo().invalidateLiveness();
457
458 // Renumber all of the machine basic blocks in the function, guaranteeing that
459 // the numbers agree with the position of the block in the function.
460 MF->RenumberBlocks();
461
Reed Kotler0f007fc2013-11-05 08:14:14 +0000462 bool MadeChange = false;
463
Reed Kotler91ae9822013-10-27 21:57:36 +0000464 // Perform the initial placement of the constant pool entries. To start with,
465 // we put them all at the end of the function.
466 std::vector<MachineInstr*> CPEMIs;
467 if (!MCP->isEmpty())
468 doInitialPlacement(CPEMIs);
469
Reed Kotler0f007fc2013-11-05 08:14:14 +0000470 /// The next UID to take is the first unused one.
471 initPICLabelUId(CPEMIs.size());
472
473 // Do the initial scan of the function, building up information about the
474 // sizes of each block, the location of all the water, and finding all of the
475 // constant pool users.
476 initializeFunctionInfo(CPEMIs);
477 CPEMIs.clear();
478 DEBUG(dumpBBs());
479
480 /// Remove dead constant pool entries.
481 MadeChange |= removeUnusedCPEntries();
482
483 // Iteratively place constant pool entries and fix up branches until there
484 // is no change.
485 unsigned NoCPIters = 0, NoBRIters = 0;
486 (void)NoBRIters;
487 while (true) {
488 DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
489 bool CPChange = false;
490 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
491 CPChange |= handleConstantPoolUser(i);
492 if (CPChange && ++NoCPIters > 30)
493 report_fatal_error("Constant Island pass failed to converge!");
494 DEBUG(dumpBBs());
495
496 // Clear NewWaterList now. If we split a block for branches, it should
497 // appear as "new water" for the next iteration of constant pool placement.
498 NewWaterList.clear();
499
500 DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
501 bool BRChange = false;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000502 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
503 BRChange |= fixupImmediateBr(ImmBranches[i]);
504 if (BRChange && ++NoBRIters > 30)
505 report_fatal_error("Branch Fix Up pass failed to converge!");
506 DEBUG(dumpBBs());
Reed Kotler0f007fc2013-11-05 08:14:14 +0000507 if (!CPChange && !BRChange)
508 break;
509 MadeChange = true;
510 }
511
512 DEBUG(dbgs() << '\n'; dumpBBs());
513
514 BBInfo.clear();
515 WaterList.clear();
516 CPUsers.clear();
517 CPEntries.clear();
518 ImmBranches.clear();
519 return MadeChange;
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000520}
521
Reed Kotler91ae9822013-10-27 21:57:36 +0000522/// doInitialPlacement - Perform the initial placement of the constant pool
523/// entries. To start with, we put them all at the end of the function.
524void
525MipsConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
526 // Create the basic block to hold the CPE's.
527 MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
528 MF->push_back(BB);
529
530
531 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
532 unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
533
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 : 2);
537
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());
541
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(MaxAlign + 1, BB->end());
547
548 // Add all of the constants from the constant pool to the end block, use an
549 // identity mapping of CPI's to CPE's.
550 const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
551
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000552 const DataLayout &TD = MF->getDataLayout();
Reed Kotler91ae9822013-10-27 21:57:36 +0000553 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
554 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
555 assert(Size >= 4 && "Too small constant pool entry");
556 unsigned Align = CPs[i].getAlignment();
557 assert(isPowerOf2_32(Align) && "Invalid alignment");
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((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
561
562 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
563 unsigned LogAlign = Log2_32(Align);
564 MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
565
566 MachineInstr *CPEMI =
567 BuildMI(*BB, InsAt, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
568 .addImm(i).addConstantPoolIndex(i).addImm(Size);
569
570 CPEMIs.push_back(CPEMI);
571
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 <= MaxAlign; ++a)
575 if (InsPoint[a] == InsAt)
576 InsPoint[a] = CPEMI;
577 // Add a new CPEntry, but no corresponding CPUser yet.
Benjamin Kramere12a6ba2014-10-03 18:33:16 +0000578 CPEntries.emplace_back(1, CPEntry(CPEMI, i));
Reed Kotler91ae9822013-10-27 21:57:36 +0000579 ++NumCPEs;
580 DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
581 << Size << ", align = " << Align <<'\n');
582 }
583 DEBUG(BB->dump());
584}
585
Reed Kotler0f007fc2013-11-05 08:14:14 +0000586/// BBHasFallthrough - Return true if the specified basic block can fallthrough
587/// into the block immediately after it.
588static bool BBHasFallthrough(MachineBasicBlock *MBB) {
589 // Get the next machine basic block in the function.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000590 MachineFunction::iterator MBBI = MBB->getIterator();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000591 // Can't fall off end of function.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000592 if (std::next(MBBI) == MBB->getParent()->end())
Reed Kotler0f007fc2013-11-05 08:14:14 +0000593 return false;
594
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000595 MachineBasicBlock *NextBB = &*std::next(MBBI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000596 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
597 E = MBB->succ_end(); I != E; ++I)
598 if (*I == NextBB)
599 return true;
600
601 return false;
602}
603
604/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
605/// look up the corresponding CPEntry.
606MipsConstantIslands::CPEntry
607*MipsConstantIslands::findConstPoolEntry(unsigned CPI,
608 const MachineInstr *CPEMI) {
609 std::vector<CPEntry> &CPEs = CPEntries[CPI];
610 // Number of entries per constpool index should be small, just do a
611 // linear search.
612 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
613 if (CPEs[i].CPEMI == CPEMI)
614 return &CPEs[i];
615 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000616 return nullptr;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000617}
618
619/// getCPELogAlign - Returns the required alignment of the constant pool entry
620/// represented by CPEMI. Alignment is measured in log2(bytes) units.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000621unsigned MipsConstantIslands::getCPELogAlign(const MachineInstr &CPEMI) {
622 assert(CPEMI.getOpcode() == Mips::CONSTPOOL_ENTRY);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000623
624 // Everything is 4-byte aligned unless AlignConstantIslands is set.
625 if (!AlignConstantIslands)
626 return 2;
627
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000628 unsigned CPI = CPEMI.getOperand(1).getIndex();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000629 assert(CPI < MCP->getConstants().size() && "Invalid constant pool index.");
630 unsigned Align = MCP->getConstants()[CPI].getAlignment();
631 assert(isPowerOf2_32(Align) && "Invalid CPE alignment");
632 return Log2_32(Align);
633}
634
635/// initializeFunctionInfo - Do the initial scan of the function, building up
636/// information about the sizes of each block, the location of all the water,
637/// and finding all of the constant pool users.
638void MipsConstantIslands::
639initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
640 BBInfo.clear();
641 BBInfo.resize(MF->getNumBlockIDs());
642
643 // First thing, compute the size of all basic blocks, and see if the function
644 // has any inline assembly in it. If so, we have to be conservative about
645 // alignment assumptions, as we don't know for sure the size of any
646 // instructions in the inline assembly.
647 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000648 computeBlockSize(&*I);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000649
Reed Kotler0f007fc2013-11-05 08:14:14 +0000650
651 // Compute block offsets.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000652 adjustBBOffsetsAfter(&MF->front());
Reed Kotler0f007fc2013-11-05 08:14:14 +0000653
654 // Now go back through the instructions and build up our data structures.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000655 for (MachineBasicBlock &MBB : *MF) {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000656 // If this block doesn't fall through into the next MBB, then this is
657 // 'water' that a constant pool island could be placed.
658 if (!BBHasFallthrough(&MBB))
659 WaterList.push_back(&MBB);
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000660 for (MachineInstr &MI : MBB) {
661 if (MI.isDebugValue())
Reed Kotler0f007fc2013-11-05 08:14:14 +0000662 continue;
663
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000664 int Opc = MI.getOpcode();
665 if (MI.isBranch()) {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000666 bool isCond = false;
667 unsigned Bits = 0;
668 unsigned Scale = 1;
669 int UOpc = Opc;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000670 switch (Opc) {
671 default:
Reed Kotler4b7afe52013-11-13 23:52:18 +0000672 continue; // Ignore other branches for now
673 case Mips::Bimm16:
674 Bits = 11;
675 Scale = 2;
676 isCond = false;
677 break;
678 case Mips::BimmX16:
679 Bits = 16;
680 Scale = 2;
681 isCond = false;
Reed Kotler0d409e22013-11-28 00:56:37 +0000682 break;
683 case Mips::BeqzRxImm16:
Reed Kotler59975c22013-12-03 23:42:51 +0000684 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000685 Bits = 8;
686 Scale = 2;
687 isCond = true;
688 break;
689 case Mips::BeqzRxImmX16:
Reed Kotler59975c22013-12-03 23:42:51 +0000690 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000691 Bits = 16;
692 Scale = 2;
693 isCond = true;
694 break;
695 case Mips::BnezRxImm16:
Reed Kotler59975c22013-12-03 23:42:51 +0000696 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000697 Bits = 8;
698 Scale = 2;
699 isCond = true;
700 break;
701 case Mips::BnezRxImmX16:
Reed Kotler59975c22013-12-03 23:42:51 +0000702 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000703 Bits = 16;
704 Scale = 2;
705 isCond = true;
706 break;
707 case Mips::Bteqz16:
Reed Kotler59975c22013-12-03 23:42:51 +0000708 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000709 Bits = 8;
710 Scale = 2;
711 isCond = true;
712 break;
713 case Mips::BteqzX16:
Reed Kotler59975c22013-12-03 23:42:51 +0000714 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000715 Bits = 16;
716 Scale = 2;
717 isCond = true;
718 break;
719 case Mips::Btnez16:
Reed Kotler59975c22013-12-03 23:42:51 +0000720 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000721 Bits = 8;
722 Scale = 2;
723 isCond = true;
724 break;
725 case Mips::BtnezX16:
Reed Kotler59975c22013-12-03 23:42:51 +0000726 UOpc=Mips::Bimm16;
Reed Kotler0d409e22013-11-28 00:56:37 +0000727 Bits = 16;
728 Scale = 2;
729 isCond = true;
730 break;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000731 }
732 // Record this immediate branch.
733 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000734 ImmBranches.push_back(ImmBranch(&MI, MaxOffs, isCond, UOpc));
Reed Kotler0f007fc2013-11-05 08:14:14 +0000735 }
Reed Kotler0f007fc2013-11-05 08:14:14 +0000736
737 if (Opc == Mips::CONSTPOOL_ENTRY)
738 continue;
739
740
741 // Scan the instructions for constant pool operands.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000742 for (unsigned op = 0, e = MI.getNumOperands(); op != e; ++op)
743 if (MI.getOperand(op).isCPI()) {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000744
745 // We found one. The addressing mode tells us the max displacement
746 // from the PC that this instruction permits.
747
748 // Basic size info comes from the TSFlags field.
749 unsigned Bits = 0;
750 unsigned Scale = 1;
751 bool NegOk = false;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000752 unsigned LongFormBits = 0;
753 unsigned LongFormScale = 0;
754 unsigned LongFormOpcode = 0;
755 switch (Opc) {
756 default:
757 llvm_unreachable("Unknown addressing mode for CP reference!");
758 case Mips::LwRxPcTcp16:
759 Bits = 8;
Reed Kotler3d7b33f2013-11-06 04:29:52 +0000760 Scale = 4;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000761 LongFormOpcode = Mips::LwRxPcTcpX16;
Reed Kotler43788a22014-01-16 00:47:46 +0000762 LongFormBits = 14;
Reed Kotler45c59272013-11-10 00:09:26 +0000763 LongFormScale = 1;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000764 break;
765 case Mips::LwRxPcTcpX16:
Reed Kotler43788a22014-01-16 00:47:46 +0000766 Bits = 14;
Reed Kotler3d7b33f2013-11-06 04:29:52 +0000767 Scale = 1;
768 NegOk = true;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000769 break;
770 }
771 // Remember that this is a user of a CP entry.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000772 unsigned CPI = MI.getOperand(op).getIndex();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000773 MachineInstr *CPEMI = CPEMIs[CPI];
774 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
775 unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale;
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000776 CPUsers.push_back(CPUser(&MI, CPEMI, MaxOffs, NegOk, LongFormMaxOffs,
777 LongFormOpcode));
Reed Kotler0f007fc2013-11-05 08:14:14 +0000778
779 // Increment corresponding CPEntry reference count.
780 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
781 assert(CPE && "Cannot find a corresponding CPEntry!");
782 CPE->RefCount++;
783
784 // Instructions can only use one CP entry, don't bother scanning the
785 // rest of the operands.
786 break;
787
788 }
789
790 }
791 }
792
793}
794
795/// computeBlockSize - Compute the size and some alignment information for MBB.
796/// This function updates BBInfo directly.
797void MipsConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
798 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
799 BBI.Size = 0;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000800
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000801 for (const MachineInstr &MI : *MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000802 BBI.Size += TII->getInstSizeInBytes(MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000803}
804
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.
808unsigned MipsConstantIslands::getOffsetOf(MachineInstr *MI) const {
809 MachineBasicBlock *MBB = MI->getParent();
810
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
813 // it is in.
814 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
815
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?");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000819 Offset += TII->getInstSizeInBytes(*I);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000820 }
821 return Offset;
822}
823
824/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
825/// ID.
826static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
827 const MachineBasicBlock *RHS) {
828 return LHS->getNumber() < RHS->getNumber();
829}
830
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.
834void MipsConstantIslands::updateForInsertedWaterBlock
835 (MachineBasicBlock *NewBB) {
836 // Renumber the MBB's to keep them consecutive.
837 NewBB->getParent()->RenumberBlocks(NewBB);
838
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());
842
843 // Next, update WaterList. Specifically, we need to add NewMBB as having
844 // available water after it.
845 water_iterator IP =
846 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
847 CompareMBBNumbers);
848 WaterList.insert(IP, NewBB);
849}
850
Reed Kotler0f007fc2013-11-05 08:14:14 +0000851unsigned MipsConstantIslands::getUserOffset(CPUser &U) const {
Reed Kotler0eb87392013-11-05 21:39:57 +0000852 return getOffsetOf(U.MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000853}
854
855/// Split the basic block containing MI into two blocks, which are joined by
856/// an unconditional branch. Update data structures and renumber blocks to
857/// account for this change and returns the newly created block.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000858MachineBasicBlock *
859MipsConstantIslands::splitBlockBeforeInstr(MachineInstr &MI) {
860 MachineBasicBlock *OrigBB = MI.getParent();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000861
862 // Create a new MBB for the code after the OrigBB.
863 MachineBasicBlock *NewBB =
864 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000865 MachineFunction::iterator MBBI = ++OrigBB->getIterator();
Reed Kotler0f007fc2013-11-05 08:14:14 +0000866 MF->insert(MBBI, NewBB);
867
868 // Splice the instructions starting with MI over to NewBB.
869 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
870
871 // Add an unconditional branch from OrigBB to NewBB.
872 // Note the new unconditional branch is not being recorded.
873 // There doesn't seem to be meaningful DebugInfo available; this doesn't
874 // correspond to anything in the source.
Reed Kotlerf0e69682013-11-12 02:27:12 +0000875 BuildMI(OrigBB, DebugLoc(), TII->get(Mips::Bimm16)).addMBB(NewBB);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000876 ++NumSplit;
877
878 // Update the CFG. All succs of OrigBB are now succs of NewBB.
879 NewBB->transferSuccessors(OrigBB);
880
881 // OrigBB branches to NewBB.
882 OrigBB->addSuccessor(NewBB);
883
884 // Update internal data structures to account for the newly inserted MBB.
885 // This is almost the same as updateForInsertedWaterBlock, except that
886 // the Water goes after OrigBB, not NewBB.
887 MF->RenumberBlocks(NewBB);
888
889 // Insert an entry into BBInfo to align it properly with the (newly
890 // renumbered) block numbers.
891 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
892
893 // Next, update WaterList. Specifically, we need to add OrigMBB as having
894 // available water after it (but not if it's already there, which happens
895 // when splitting before a conditional branch that is followed by an
896 // unconditional branch - in that case we want to insert NewBB).
897 water_iterator IP =
898 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
899 CompareMBBNumbers);
900 MachineBasicBlock* WaterBB = *IP;
901 if (WaterBB == OrigBB)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000902 WaterList.insert(std::next(IP), NewBB);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000903 else
904 WaterList.insert(IP, OrigBB);
905 NewWaterList.insert(OrigBB);
906
907 // Figure out how large the OrigBB is. As the first half of the original
908 // block, it cannot contain a tablejump. The size includes
909 // the new jump we added. (It should be possible to do this without
910 // recounting everything, but it's very confusing, and this is rarely
911 // executed.)
912 computeBlockSize(OrigBB);
913
914 // Figure out how large the NewMBB is. As the second half of the original
915 // block, it may contain a tablejump.
916 computeBlockSize(NewBB);
917
918 // All BBOffsets following these blocks must be modified.
919 adjustBBOffsetsAfter(OrigBB);
920
921 return NewBB;
922}
923
924
925
926/// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
927/// reference) is within MaxDisp of TrialOffset (a proposed location of a
928/// constant pool entry).
Reed Kotler0f007fc2013-11-05 08:14:14 +0000929bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset,
930 unsigned TrialOffset, unsigned MaxDisp,
Reed Kotlerb09ebe92013-11-05 22:34:29 +0000931 bool NegativeOK) {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000932 if (UserOffset <= TrialOffset) {
933 // User before the Trial.
934 if (TrialOffset - UserOffset <= MaxDisp)
935 return true;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000936 } else if (NegativeOK) {
937 if (UserOffset - TrialOffset <= MaxDisp)
938 return true;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000939 }
940 return false;
941}
942
943/// isWaterInRange - Returns true if a CPE placed after the specified
944/// Water (a basic block) will be in range for the specific MI.
945///
946/// Compute how much the function will grow by inserting a CPE after Water.
947bool MipsConstantIslands::isWaterInRange(unsigned UserOffset,
948 MachineBasicBlock* Water, CPUser &U,
949 unsigned &Growth) {
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000950 unsigned CPELogAlign = getCPELogAlign(*U.CPEMI);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000951 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
952 unsigned NextBlockOffset, NextBlockAlignment;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000953 MachineFunction::const_iterator NextBlock = ++Water->getIterator();
954 if (NextBlock == MF->end()) {
Reed Kotler0f007fc2013-11-05 08:14:14 +0000955 NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
956 NextBlockAlignment = 0;
957 } else {
958 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
959 NextBlockAlignment = NextBlock->getAlignment();
960 }
961 unsigned Size = U.CPEMI->getOperand(2).getImm();
962 unsigned CPEEnd = CPEOffset + Size;
963
964 // The CPE may be able to hide in the alignment padding before the next
965 // block. It may also cause more padding to be required if it is more aligned
966 // that the next block.
967 if (CPEEnd > NextBlockOffset) {
968 Growth = CPEEnd - NextBlockOffset;
969 // Compute the padding that would go at the end of the CPE to align the next
970 // block.
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000971 Growth += OffsetToAlignment(CPEEnd, 1ULL << NextBlockAlignment);
Reed Kotler0f007fc2013-11-05 08:14:14 +0000972
973 // If the CPE is to be inserted before the instruction, that will raise
974 // the offset of the instruction. Also account for unknown alignment padding
975 // in blocks between CPE and the user.
976 if (CPEOffset < UserOffset)
Reed Kotler7ded5b62013-11-05 23:36:58 +0000977 UserOffset += Growth;
Reed Kotler0f007fc2013-11-05 08:14:14 +0000978 } else
979 // CPE fits in existing padding.
980 Growth = 0;
981
982 return isOffsetInRange(UserOffset, CPEOffset, U);
983}
984
985/// isCPEntryInRange - Returns true if the distance between specific MI and
986/// specific ConstPool entry instruction can fit in MI's displacement field.
987bool MipsConstantIslands::isCPEntryInRange
988 (MachineInstr *MI, unsigned UserOffset,
989 MachineInstr *CPEMI, unsigned MaxDisp,
990 bool NegOk, bool DoDump) {
991 unsigned CPEOffset = getOffsetOf(CPEMI);
992
993 if (DoDump) {
994 DEBUG({
995 unsigned Block = MI->getParent()->getNumber();
996 const BasicBlockInfo &BBI = BBInfo[Block];
997 dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
998 << " max delta=" << MaxDisp
999 << format(" insn address=%#x", UserOffset)
1000 << " in BB#" << Block << ": "
1001 << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
1002 << format("CPE address=%#x offset=%+d: ", CPEOffset,
1003 int(CPEOffset-UserOffset));
1004 });
1005 }
1006
1007 return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
1008}
1009
1010#ifndef NDEBUG
1011/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
1012/// unconditionally branches to its only successor.
1013static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
1014 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
1015 return false;
1016 MachineBasicBlock *Succ = *MBB->succ_begin();
1017 MachineBasicBlock *Pred = *MBB->pred_begin();
1018 MachineInstr *PredMI = &Pred->back();
Reed Kotlerf0e69682013-11-12 02:27:12 +00001019 if (PredMI->getOpcode() == Mips::Bimm16)
Reed Kotler0f007fc2013-11-05 08:14:14 +00001020 return PredMI->getOperand(0).getMBB() == Succ;
1021 return false;
1022}
1023#endif
1024
1025void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
1026 unsigned BBNum = BB->getNumber();
1027 for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
1028 // Get the offset and known bits at the end of the layout predecessor.
1029 // Include the alignment of the current block.
Reed Kotler7ded5b62013-11-05 23:36:58 +00001030 unsigned Offset = BBInfo[i - 1].Offset + BBInfo[i - 1].Size;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001031 BBInfo[i].Offset = Offset;
1032 }
1033}
1034
1035/// decrementCPEReferenceCount - find the constant pool entry with index CPI
1036/// and instruction CPEMI, and decrement its refcount. If the refcount
1037/// becomes 0 remove the entry and instruction. Returns true if we removed
1038/// the entry, false if we didn't.
1039
1040bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI,
1041 MachineInstr *CPEMI) {
1042 // Find the old entry. Eliminate it if it is no longer used.
1043 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1044 assert(CPE && "Unexpected!");
1045 if (--CPE->RefCount == 0) {
1046 removeDeadCPEMI(CPEMI);
Craig Topper062a2ba2014-04-25 05:30:21 +00001047 CPE->CPEMI = nullptr;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001048 --NumCPEs;
1049 return true;
1050 }
1051 return false;
1052}
1053
1054/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1055/// if not, see if an in-range clone of the CPE is in range, and if so,
1056/// change the data structures so the user references the clone. Returns:
1057/// 0 = no existing entry found
1058/// 1 = entry found, and there were no code insertions or deletions
1059/// 2 = entry found, and there were code insertions or deletions
1060int MipsConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
1061{
1062 MachineInstr *UserMI = U.MI;
1063 MachineInstr *CPEMI = U.CPEMI;
1064
1065 // Check to see if the CPE is already in-range.
1066 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
1067 true)) {
1068 DEBUG(dbgs() << "In range\n");
1069 return 1;
1070 }
1071
1072 // No. Look for previously created clones of the CPE that are in range.
1073 unsigned CPI = CPEMI->getOperand(1).getIndex();
1074 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1075 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1076 // We already tried this one
1077 if (CPEs[i].CPEMI == CPEMI)
1078 continue;
1079 // Removing CPEs can leave empty entries, skip
Craig Topper062a2ba2014-04-25 05:30:21 +00001080 if (CPEs[i].CPEMI == nullptr)
Reed Kotler0f007fc2013-11-05 08:14:14 +00001081 continue;
1082 if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
1083 U.NegOk)) {
1084 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1085 << CPEs[i].CPI << "\n");
1086 // Point the CPUser node to the replacement
1087 U.CPEMI = CPEs[i].CPEMI;
1088 // Change the CPI in the instruction operand to refer to the clone.
1089 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1090 if (UserMI->getOperand(j).isCPI()) {
1091 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1092 break;
1093 }
1094 // Adjust the refcount of the clone...
1095 CPEs[i].RefCount++;
1096 // ...and the original. If we didn't remove the old entry, none of the
1097 // addresses changed, so we don't need another pass.
1098 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1099 }
1100 }
1101 return 0;
1102}
1103
1104/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1105/// This version checks if the longer form of the instruction can be used to
1106/// to satisfy things.
1107/// if not, see if an in-range clone of the CPE is in range, and if so,
1108/// change the data structures so the user references the clone. Returns:
1109/// 0 = no existing entry found
1110/// 1 = entry found, and there were no code insertions or deletions
1111/// 2 = entry found, and there were code insertions or deletions
1112int MipsConstantIslands::findLongFormInRangeCPEntry
1113 (CPUser& U, unsigned UserOffset)
1114{
1115 MachineInstr *UserMI = U.MI;
1116 MachineInstr *CPEMI = U.CPEMI;
1117
1118 // Check to see if the CPE is already in-range.
1119 if (isCPEntryInRange(UserMI, UserOffset, CPEMI,
1120 U.getLongFormMaxDisp(), U.NegOk,
1121 true)) {
1122 DEBUG(dbgs() << "In range\n");
1123 UserMI->setDesc(TII->get(U.getLongFormOpcode()));
Reed Kotler45c59272013-11-10 00:09:26 +00001124 U.setMaxDisp(U.getLongFormMaxDisp());
Reed Kotler0f007fc2013-11-05 08:14:14 +00001125 return 2; // instruction is longer length now
1126 }
1127
1128 // No. Look for previously created clones of the CPE that are in range.
1129 unsigned CPI = CPEMI->getOperand(1).getIndex();
1130 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1131 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1132 // We already tried this one
1133 if (CPEs[i].CPEMI == CPEMI)
1134 continue;
1135 // Removing CPEs can leave empty entries, skip
Craig Topper062a2ba2014-04-25 05:30:21 +00001136 if (CPEs[i].CPEMI == nullptr)
Reed Kotler0f007fc2013-11-05 08:14:14 +00001137 continue;
1138 if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI,
1139 U.getLongFormMaxDisp(), U.NegOk)) {
1140 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1141 << CPEs[i].CPI << "\n");
1142 // Point the CPUser node to the replacement
1143 U.CPEMI = CPEs[i].CPEMI;
1144 // Change the CPI in the instruction operand to refer to the clone.
1145 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1146 if (UserMI->getOperand(j).isCPI()) {
1147 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1148 break;
1149 }
1150 // Adjust the refcount of the clone...
1151 CPEs[i].RefCount++;
1152 // ...and the original. If we didn't remove the old entry, none of the
1153 // addresses changed, so we don't need another pass.
1154 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1155 }
1156 }
1157 return 0;
1158}
1159
1160/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1161/// the specific unconditional branch instruction.
1162static inline unsigned getUnconditionalBrDisp(int Opc) {
1163 switch (Opc) {
Reed Kotlerf0e69682013-11-12 02:27:12 +00001164 case Mips::Bimm16:
1165 return ((1<<10)-1)*2;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001166 case Mips::BimmX16:
1167 return ((1<<16)-1)*2;
1168 default:
1169 break;
1170 }
1171 return ((1<<16)-1)*2;
1172}
1173
1174/// findAvailableWater - Look for an existing entry in the WaterList in which
1175/// we can place the CPE referenced from U so it's within range of U's MI.
1176/// Returns true if found, false if not. If it returns true, WaterIter
Reed Kotler4d0313d2013-11-05 12:04:37 +00001177/// is set to the WaterList entry.
1178/// To ensure that this pass
Reed Kotler0f007fc2013-11-05 08:14:14 +00001179/// terminates, the CPE location for a particular CPUser is only allowed to
1180/// move to a lower address, so search backward from the end of the list and
1181/// prefer the first water that is in range.
1182bool MipsConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
1183 water_iterator &WaterIter) {
1184 if (WaterList.empty())
1185 return false;
1186
1187 unsigned BestGrowth = ~0u;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001188 for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001189 --IP) {
1190 MachineBasicBlock* WaterBB = *IP;
1191 // Check if water is in range and is either at a lower address than the
1192 // current "high water mark" or a new water block that was created since
1193 // the previous iteration by inserting an unconditional branch. In the
1194 // latter case, we want to allow resetting the high water mark back to
1195 // this new water since we haven't seen it before. Inserting branches
1196 // should be relatively uncommon and when it does happen, we want to be
1197 // sure to take advantage of it for all the CPEs near that block, so that
1198 // we don't insert more branches than necessary.
1199 unsigned Growth;
1200 if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1201 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1202 NewWaterList.count(WaterBB)) && Growth < BestGrowth) {
1203 // This is the least amount of required padding seen so far.
1204 BestGrowth = Growth;
1205 WaterIter = IP;
1206 DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber()
1207 << " Growth=" << Growth << '\n');
1208
1209 // Keep looking unless it is perfect.
1210 if (BestGrowth == 0)
1211 return true;
1212 }
1213 if (IP == B)
1214 break;
1215 }
1216 return BestGrowth != ~0u;
1217}
1218
1219/// createNewWater - No existing WaterList entry will work for
1220/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1221/// block is used if in range, and the conditional branch munged so control
1222/// flow is correct. Otherwise the block is split to create a hole with an
1223/// unconditional branch around it. In either case NewMBB is set to a
1224/// block following which the new island can be inserted (the WaterList
1225/// is not adjusted).
1226void MipsConstantIslands::createNewWater(unsigned CPUserIndex,
1227 unsigned UserOffset,
1228 MachineBasicBlock *&NewMBB) {
1229 CPUser &U = CPUsers[CPUserIndex];
1230 MachineInstr *UserMI = U.MI;
1231 MachineInstr *CPEMI = U.CPEMI;
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +00001232 unsigned CPELogAlign = getCPELogAlign(*CPEMI);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001233 MachineBasicBlock *UserMBB = UserMI->getParent();
1234 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
1235
1236 // If the block does not end in an unconditional branch already, and if the
Reed Kotler4d0313d2013-11-05 12:04:37 +00001237 // end of the block is within range, make new water there.
Reed Kotler0f007fc2013-11-05 08:14:14 +00001238 if (BBHasFallthrough(UserMBB)) {
1239 // Size of branch to insert.
1240 unsigned Delta = 2;
1241 // Compute the offset where the CPE will begin.
1242 unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
1243
1244 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1245 DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
1246 << format(", expected CPE offset %#x\n", CPEOffset));
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001247 NewMBB = &*++UserMBB->getIterator();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001248 // Add an unconditional branch from UserMBB to fallthrough block. Record
1249 // it for branch lengthening; this new branch will not get out of range,
1250 // but if the preceding conditional branch is out of range, the targets
1251 // will be exchanged, and the altered branch may be out of range, so the
1252 // machinery has to know about it.
Reed Kotlerf0e69682013-11-12 02:27:12 +00001253 int UncondBr = Mips::Bimm16;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001254 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1255 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
1256 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
1257 MaxDisp, false, UncondBr));
1258 BBInfo[UserMBB->getNumber()].Size += Delta;
1259 adjustBBOffsetsAfter(UserMBB);
1260 return;
1261 }
1262 }
1263
Reed Kotler4d0313d2013-11-05 12:04:37 +00001264 // What a big block. Find a place within the block to split it.
Reed Kotler0f007fc2013-11-05 08:14:14 +00001265
1266 // Try to split the block so it's fully aligned. Compute the latest split
1267 // point where we can add a 4-byte branch instruction, and then align to
1268 // LogAlign which is the largest possible alignment in the function.
1269 unsigned LogAlign = MF->getAlignment();
1270 assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
Reed Kotler7ded5b62013-11-05 23:36:58 +00001271 unsigned BaseInsertOffset = UserOffset + U.getMaxDisp();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001272 DEBUG(dbgs() << format("Split in middle of big block before %#x",
1273 BaseInsertOffset));
1274
1275 // The 4 in the following is for the unconditional branch we'll be inserting
Reed Kotler4d0313d2013-11-05 12:04:37 +00001276 // Alignment of the island is handled
Reed Kotler0f007fc2013-11-05 08:14:14 +00001277 // inside isOffsetInRange.
1278 BaseInsertOffset -= 4;
1279
1280 DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
Reed Kotler7ded5b62013-11-05 23:36:58 +00001281 << " la=" << LogAlign << '\n');
Reed Kotler0f007fc2013-11-05 08:14:14 +00001282
1283 // This could point off the end of the block if we've already got constant
1284 // pool entries following this block; only the last one is in the water list.
1285 // Back past any possible branches (allow for a conditional and a maximally
1286 // long unconditional).
1287 if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
Reed Kotler7ded5b62013-11-05 23:36:58 +00001288 BaseInsertOffset = UserBBI.postOffset() - 8;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001289 DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
1290 }
Reed Kotler7ded5b62013-11-05 23:36:58 +00001291 unsigned EndInsertOffset = BaseInsertOffset + 4 +
Reed Kotler0f007fc2013-11-05 08:14:14 +00001292 CPEMI->getOperand(2).getImm();
1293 MachineBasicBlock::iterator MI = UserMI;
1294 ++MI;
1295 unsigned CPUIndex = CPUserIndex+1;
1296 unsigned NumCPUsers = CPUsers.size();
1297 //MachineInstr *LastIT = 0;
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001298 for (unsigned Offset = UserOffset + TII->getInstSizeInBytes(*UserMI);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001299 Offset < BaseInsertOffset;
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001300 Offset += TII->getInstSizeInBytes(*MI), MI = std::next(MI)) {
Reed Kotler0f007fc2013-11-05 08:14:14 +00001301 assert(MI != UserMBB->end() && "Fell off end of block");
Duncan P. N. Exon Smithf197b1f2016-08-12 05:05:36 +00001302 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
Reed Kotler0f007fc2013-11-05 08:14:14 +00001303 CPUser &U = CPUsers[CPUIndex];
1304 if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
1305 // Shift intertion point by one unit of alignment so it is within reach.
1306 BaseInsertOffset -= 1u << LogAlign;
1307 EndInsertOffset -= 1u << LogAlign;
1308 }
1309 // This is overly conservative, as we don't account for CPEMIs being
1310 // reused within the block, but it doesn't matter much. Also assume CPEs
1311 // are added in order with alignment padding. We may eventually be able
1312 // to pack the aligned CPEs better.
1313 EndInsertOffset += U.CPEMI->getOperand(2).getImm();
1314 CPUIndex++;
1315 }
1316 }
1317
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +00001318 NewMBB = splitBlockBeforeInstr(*--MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001319}
1320
1321/// handleConstantPoolUser - Analyze the specified user, checking to see if it
1322/// is out-of-range. If so, pick up the constant pool value and move it some
1323/// place in-range. Return true if we changed any addresses (thus must run
1324/// another pass of branch lengthening), false otherwise.
1325bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
1326 CPUser &U = CPUsers[CPUserIndex];
1327 MachineInstr *UserMI = U.MI;
1328 MachineInstr *CPEMI = U.CPEMI;
1329 unsigned CPI = CPEMI->getOperand(1).getIndex();
1330 unsigned Size = CPEMI->getOperand(2).getImm();
1331 // Compute this only once, it's expensive.
1332 unsigned UserOffset = getUserOffset(U);
1333
1334 // See if the current entry is within range, or there is a clone of it
1335 // in range.
1336 int result = findInRangeCPEntry(U, UserOffset);
1337 if (result==1) return false;
1338 else if (result==2) return true;
1339
1340
1341 // Look for water where we can place this CPE.
1342 MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1343 MachineBasicBlock *NewMBB;
1344 water_iterator IP;
1345 if (findAvailableWater(U, UserOffset, IP)) {
1346 DEBUG(dbgs() << "Found water in range\n");
1347 MachineBasicBlock *WaterBB = *IP;
1348
1349 // If the original WaterList entry was "new water" on this iteration,
1350 // propagate that to the new island. This is just keeping NewWaterList
1351 // updated to match the WaterList, which will be updated below.
1352 if (NewWaterList.erase(WaterBB))
1353 NewWaterList.insert(NewIsland);
1354
1355 // The new CPE goes before the following block (NewMBB).
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001356 NewMBB = &*++WaterBB->getIterator();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001357 } else {
1358 // No water found.
1359 // we first see if a longer form of the instrucion could have reached
1360 // the constant. in that case we won't bother to split
Reed Kotler45c59272013-11-10 00:09:26 +00001361 if (!NoLoadRelaxation) {
1362 result = findLongFormInRangeCPEntry(U, UserOffset);
1363 if (result != 0) return true;
1364 }
Reed Kotler0f007fc2013-11-05 08:14:14 +00001365 DEBUG(dbgs() << "No water found\n");
1366 createNewWater(CPUserIndex, UserOffset, NewMBB);
1367
1368 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1369 // called while handling branches so that the water will be seen on the
1370 // next iteration for constant pools, but in this context, we don't want
1371 // it. Check for this so it will be removed from the WaterList.
1372 // Also remove any entry from NewWaterList.
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001373 MachineBasicBlock *WaterBB = &*--NewMBB->getIterator();
David Majnemer0d955d02016-08-11 22:21:41 +00001374 IP = find(WaterList, WaterBB);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001375 if (IP != WaterList.end())
1376 NewWaterList.erase(WaterBB);
1377
1378 // We are adding new water. Update NewWaterList.
1379 NewWaterList.insert(NewIsland);
1380 }
1381
1382 // Remove the original WaterList entry; we want subsequent insertions in
1383 // this vicinity to go after the one we're about to insert. This
1384 // considerably reduces the number of times we have to move the same CPE
1385 // more than once and is also important to ensure the algorithm terminates.
1386 if (IP != WaterList.end())
1387 WaterList.erase(IP);
1388
1389 // Okay, we know we can put an island before NewMBB now, do it!
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001390 MF->insert(NewMBB->getIterator(), NewIsland);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001391
1392 // Update internal data structures to account for the newly inserted MBB.
1393 updateForInsertedWaterBlock(NewIsland);
1394
1395 // Decrement the old entry, and remove it if refcount becomes 0.
1396 decrementCPEReferenceCount(CPI, CPEMI);
1397
Reed Kotlerd3b28eb2013-11-24 02:53:09 +00001398 // No existing clone of this CPE is within range.
1399 // We will be generating a new clone. Get a UID for it.
1400 unsigned ID = createPICLabelUId();
1401
Reed Kotler0f007fc2013-11-05 08:14:14 +00001402 // Now that we have an island to add the CPE to, clone the original CPE and
1403 // add it to the island.
1404 U.HighWaterMark = NewIsland;
1405 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
1406 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
1407 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
1408 ++NumCPEs;
1409
1410 // Mark the basic block as aligned as required by the const-pool entry.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +00001411 NewIsland->setAlignment(getCPELogAlign(*U.CPEMI));
Reed Kotler0f007fc2013-11-05 08:14:14 +00001412
1413 // Increase the size of the island block to account for the new entry.
1414 BBInfo[NewIsland->getNumber()].Size += Size;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001415 adjustBBOffsetsAfter(&*--NewIsland->getIterator());
Reed Kotler0f007fc2013-11-05 08:14:14 +00001416
1417 // Finally, change the CPI in the instruction operand to be ID.
1418 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1419 if (UserMI->getOperand(i).isCPI()) {
1420 UserMI->getOperand(i).setIndex(ID);
1421 break;
1422 }
1423
1424 DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
1425 << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
1426
1427 return true;
1428}
1429
1430/// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1431/// sizes and offsets of impacted basic blocks.
1432void MipsConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
1433 MachineBasicBlock *CPEBB = CPEMI->getParent();
1434 unsigned Size = CPEMI->getOperand(2).getImm();
1435 CPEMI->eraseFromParent();
1436 BBInfo[CPEBB->getNumber()].Size -= Size;
1437 // All succeeding offsets have the current size value added in, fix this.
1438 if (CPEBB->empty()) {
1439 BBInfo[CPEBB->getNumber()].Size = 0;
1440
1441 // This block no longer needs to be aligned.
1442 CPEBB->setAlignment(0);
1443 } else
1444 // Entries are sorted by descending alignment, so realign from the front.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +00001445 CPEBB->setAlignment(getCPELogAlign(*CPEBB->begin()));
Reed Kotler0f007fc2013-11-05 08:14:14 +00001446
1447 adjustBBOffsetsAfter(CPEBB);
1448 // An island has only one predecessor BB and one successor BB. Check if
1449 // this BB's predecessor jumps directly to this BB's successor. This
1450 // shouldn't happen currently.
1451 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1452 // FIXME: remove the empty blocks after all the work is done?
1453}
1454
1455/// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1456/// are zero.
1457bool MipsConstantIslands::removeUnusedCPEntries() {
1458 unsigned MadeChange = false;
1459 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1460 std::vector<CPEntry> &CPEs = CPEntries[i];
1461 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1462 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1463 removeDeadCPEMI(CPEs[j].CPEMI);
Craig Topper062a2ba2014-04-25 05:30:21 +00001464 CPEs[j].CPEMI = nullptr;
Reed Kotler0f007fc2013-11-05 08:14:14 +00001465 MadeChange = true;
1466 }
1467 }
1468 }
1469 return MadeChange;
1470}
1471
1472/// isBBInRange - Returns true if the distance between specific MI and
1473/// specific BB can fit in MI's displacement field.
1474bool MipsConstantIslands::isBBInRange
1475 (MachineInstr *MI,MachineBasicBlock *DestBB, unsigned MaxDisp) {
1476
1477unsigned PCAdj = 4;
1478
1479 unsigned BrOffset = getOffsetOf(MI) + PCAdj;
1480 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
1481
1482 DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
1483 << " from BB#" << MI->getParent()->getNumber()
1484 << " max delta=" << MaxDisp
1485 << " from " << getOffsetOf(MI) << " to " << DestOffset
1486 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
1487
1488 if (BrOffset <= DestOffset) {
1489 // Branch before the Dest.
1490 if (DestOffset-BrOffset <= MaxDisp)
1491 return true;
1492 } else {
1493 if (BrOffset-DestOffset <= MaxDisp)
1494 return true;
1495 }
1496 return false;
1497}
1498
1499/// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1500/// away to fit in its displacement field.
1501bool MipsConstantIslands::fixupImmediateBr(ImmBranch &Br) {
1502 MachineInstr *MI = Br.MI;
Reed Kotler0d409e22013-11-28 00:56:37 +00001503 unsigned TargetOperand = branchTargetOperand(MI);
1504 MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001505
1506 // Check to see if the DestBB is already in-range.
1507 if (isBBInRange(MI, DestBB, Br.MaxDisp))
1508 return false;
1509
1510 if (!Br.isCond)
1511 return fixupUnconditionalBr(Br);
1512 return fixupConditionalBr(Br);
1513}
1514
1515/// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1516/// too far away to fit in its displacement field. If the LR register has been
1517/// spilled in the epilogue, then we can use BL to implement a far jump.
1518/// Otherwise, add an intermediate branch instruction to a branch.
1519bool
1520MipsConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
1521 MachineInstr *MI = Br.MI;
1522 MachineBasicBlock *MBB = MI->getParent();
Reed Kotler2fc05be2013-11-21 05:13:23 +00001523 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001524 // Use BL to implement far jump.
Reed Kotler2fc05be2013-11-21 05:13:23 +00001525 unsigned BimmX16MaxDisp = ((1 << 16)-1) * 2;
1526 if (isBBInRange(MI, DestBB, BimmX16MaxDisp)) {
1527 Br.MaxDisp = BimmX16MaxDisp;
1528 MI->setDesc(TII->get(Mips::BimmX16));
1529 }
1530 else {
1531 // need to give the math a more careful look here
1532 // this is really a segment address and not
1533 // a PC relative address. FIXME. But I think that
1534 // just reducing the bits by 1 as I've done is correct.
1535 // The basic block we are branching too much be longword aligned.
1536 // we know that RA is saved because we always save it right now.
1537 // this requirement will be relaxed later but we also have an alternate
1538 // way to implement this that I will implement that does not need jal.
1539 // We should have a way to back out this alignment restriction if we "can" later.
1540 // but it is not harmful.
1541 //
1542 DestBB->setAlignment(2);
1543 Br.MaxDisp = ((1<<24)-1) * 2;
Reed Kotlerad450f22013-11-29 22:32:56 +00001544 MI->setDesc(TII->get(Mips::JalB16));
Reed Kotler2fc05be2013-11-21 05:13:23 +00001545 }
Reed Kotler0f007fc2013-11-05 08:14:14 +00001546 BBInfo[MBB->getNumber()].Size += 2;
1547 adjustBBOffsetsAfter(MBB);
1548 HasFarJump = true;
1549 ++NumUBrFixed;
1550
1551 DEBUG(dbgs() << " Changed B to long jump " << *MI);
1552
1553 return true;
1554}
1555
Reed Kotler0d409e22013-11-28 00:56:37 +00001556
Reed Kotler0f007fc2013-11-05 08:14:14 +00001557/// fixupConditionalBr - Fix up a conditional branch whose destination is too
1558/// far away to fit in its displacement field. It is converted to an inverse
1559/// conditional branch + an unconditional branch to the destination.
1560bool
1561MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) {
1562 MachineInstr *MI = Br.MI;
Reed Kotler0d409e22013-11-28 00:56:37 +00001563 unsigned TargetOperand = branchTargetOperand(MI);
1564 MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB();
1565 unsigned Opcode = MI->getOpcode();
1566 unsigned LongFormOpcode = longformBranchOpcode(Opcode);
1567 unsigned LongFormMaxOff = branchMaxOffsets(LongFormOpcode);
1568
1569 // Check to see if the DestBB is already in-range.
1570 if (isBBInRange(MI, DestBB, LongFormMaxOff)) {
1571 Br.MaxDisp = LongFormMaxOff;
1572 MI->setDesc(TII->get(LongFormOpcode));
1573 return true;
1574 }
Reed Kotler0f007fc2013-11-05 08:14:14 +00001575
1576 // Add an unconditional branch to the destination and invert the branch
1577 // condition to jump over it:
Reed Kotlerad450f22013-11-29 22:32:56 +00001578 // bteqz L1
Reed Kotler0f007fc2013-11-05 08:14:14 +00001579 // =>
Reed Kotlerad450f22013-11-29 22:32:56 +00001580 // bnez L2
Reed Kotler0f007fc2013-11-05 08:14:14 +00001581 // b L1
1582 // L2:
Reed Kotler0f007fc2013-11-05 08:14:14 +00001583
1584 // If the branch is at the end of its MBB and that has a fall-through block,
1585 // direct the updated conditional branch to the fall-through block. Otherwise,
1586 // split the MBB before the next instruction.
1587 MachineBasicBlock *MBB = MI->getParent();
1588 MachineInstr *BMI = &MBB->back();
1589 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Reed Kotler47f3c64a2013-12-19 00:43:08 +00001590 unsigned OppositeBranchOpcode = TII->getOppositeBranchOpc(Opcode);
Reed Kotlerad450f22013-11-29 22:32:56 +00001591
Reed Kotler0f007fc2013-11-05 08:14:14 +00001592 ++NumCBrFixed;
1593 if (BMI != MI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001594 if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) &&
Daniel Sandersf9d8b8c2016-05-06 13:23:51 +00001595 BMI->isUnconditionalBranch()) {
Reed Kotler0f007fc2013-11-05 08:14:14 +00001596 // Last MI in the BB is an unconditional branch. Can we simply invert the
1597 // condition and swap destinations:
Reed Kotlerad450f22013-11-29 22:32:56 +00001598 // beqz L1
Reed Kotler0f007fc2013-11-05 08:14:14 +00001599 // b L2
1600 // =>
Reed Kotlerad450f22013-11-29 22:32:56 +00001601 // bnez L2
Reed Kotler0f007fc2013-11-05 08:14:14 +00001602 // b L1
Reed Kotlerad450f22013-11-29 22:32:56 +00001603 unsigned BMITargetOperand = branchTargetOperand(BMI);
1604 MachineBasicBlock *NewDest =
1605 BMI->getOperand(BMITargetOperand).getMBB();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001606 if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
1607 DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
1608 << *BMI);
Reed Kotler59975c22013-12-03 23:42:51 +00001609 MI->setDesc(TII->get(OppositeBranchOpcode));
Reed Kotlerad450f22013-11-29 22:32:56 +00001610 BMI->getOperand(BMITargetOperand).setMBB(DestBB);
1611 MI->getOperand(TargetOperand).setMBB(NewDest);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001612 return true;
1613 }
1614 }
1615 }
1616
Reed Kotlerad450f22013-11-29 22:32:56 +00001617
Reed Kotler0f007fc2013-11-05 08:14:14 +00001618 if (NeedSplit) {
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +00001619 splitBlockBeforeInstr(*MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001620 // No need for the branch to the next block. We're adding an unconditional
1621 // branch to the destination.
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001622 int delta = TII->getInstSizeInBytes(MBB->back());
Reed Kotler0f007fc2013-11-05 08:14:14 +00001623 BBInfo[MBB->getNumber()].Size -= delta;
1624 MBB->back().eraseFromParent();
1625 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1626 }
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001627 MachineBasicBlock *NextBB = &*++MBB->getIterator();
Reed Kotler0f007fc2013-11-05 08:14:14 +00001628
1629 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
1630 << " also invert condition and change dest. to BB#"
1631 << NextBB->getNumber() << "\n");
1632
1633 // Insert a new conditional branch and a new unconditional branch.
1634 // Also update the ImmBranch as well as adding a new entry for the new branch.
Reed Kotler59975c22013-12-03 23:42:51 +00001635 if (MI->getNumExplicitOperands() == 2) {
1636 BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode))
1637 .addReg(MI->getOperand(0).getReg())
1638 .addMBB(NextBB);
Reed Kotler47f3c64a2013-12-19 00:43:08 +00001639 } else {
1640 BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode))
1641 .addMBB(NextBB);
Reed Kotler59975c22013-12-03 23:42:51 +00001642 }
Reed Kotler0f007fc2013-11-05 08:14:14 +00001643 Br.MI = &MBB->back();
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001644 BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back());
Reed Kotler0f007fc2013-11-05 08:14:14 +00001645 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001646 BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back());
Reed Kotler0f007fc2013-11-05 08:14:14 +00001647 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
1648 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
1649
1650 // Remove the old conditional branch. It may or may not still be in MBB.
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001651 BBInfo[MI->getParent()->getNumber()].Size -= TII->getInstSizeInBytes(*MI);
Reed Kotler0f007fc2013-11-05 08:14:14 +00001652 MI->eraseFromParent();
1653 adjustBBOffsetsAfter(MBB);
1654 return true;
1655}
1656
Reed Kotler91ae9822013-10-27 21:57:36 +00001657
1658void MipsConstantIslands::prescanForConstants() {
Reed Kotler0f007fc2013-11-05 08:14:14 +00001659 unsigned J = 0;
1660 (void)J;
Reed Kotler91ae9822013-10-27 21:57:36 +00001661 for (MachineFunction::iterator B =
1662 MF->begin(), E = MF->end(); B != E; ++B) {
1663 for (MachineBasicBlock::instr_iterator I =
1664 B->instr_begin(), EB = B->instr_end(); I != EB; ++I) {
1665 switch(I->getDesc().getOpcode()) {
1666 case Mips::LwConstant32: {
Reed Kotlera787aa22013-11-24 06:18:50 +00001667 PrescannedForConstants = true;
Reed Kotler91ae9822013-10-27 21:57:36 +00001668 DEBUG(dbgs() << "constant island constant " << *I << "\n");
1669 J = I->getNumOperands();
1670 DEBUG(dbgs() << "num operands " << J << "\n");
1671 MachineOperand& Literal = I->getOperand(1);
1672 if (Literal.isImm()) {
1673 int64_t V = Literal.getImm();
1674 DEBUG(dbgs() << "literal " << V << "\n");
1675 Type *Int32Ty =
1676 Type::getInt32Ty(MF->getFunction()->getContext());
1677 const Constant *C = ConstantInt::get(Int32Ty, V);
1678 unsigned index = MCP->getConstantPoolIndex(C, 4);
1679 I->getOperand(2).ChangeToImmediate(index);
1680 DEBUG(dbgs() << "constant island constant " << *I << "\n");
Reed Kotler0f007fc2013-11-05 08:14:14 +00001681 I->setDesc(TII->get(Mips::LwRxPcTcp16));
Reed Kotler91ae9822013-10-27 21:57:36 +00001682 I->RemoveOperand(1);
1683 I->RemoveOperand(1);
1684 I->addOperand(MachineOperand::CreateCPI(index, 0));
Reed Kotler0f007fc2013-11-05 08:14:14 +00001685 I->addOperand(MachineOperand::CreateImm(4));
Reed Kotler91ae9822013-10-27 21:57:36 +00001686 }
1687 break;
1688 }
1689 default:
1690 break;
1691 }
1692 }
1693 }
1694}