blob: 4a052ca84cbf601dddb007ed21ffd682fc518a90 [file] [log] [blame]
Quentin Colombet8e8e85c2016-04-05 19:06:01 +00001//===- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect -*- C++ -*-==//
2//
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/// \file
10/// This file implements the RegBankSelect class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Quentin Colombetcfd97b92016-05-20 00:35:26 +000014#include "llvm/ADT/PostOrderIterator.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000015#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
Quentin Colombet55650752016-05-20 00:49:10 +000016#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
17#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Quentin Colombeta5530122016-05-20 17:36:54 +000019#include "llvm/IR/Function.h"
Quentin Colombetcfd97b92016-05-20 00:35:26 +000020#include "llvm/Support/BlockFrequency.h"
Quentin Colombete16f5612016-04-07 23:53:55 +000021#include "llvm/Support/Debug.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000022#include "llvm/Target/TargetSubtargetInfo.h"
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000023
24#define DEBUG_TYPE "regbankselect"
25
26using namespace llvm;
27
28char RegBankSelect::ID = 0;
29INITIALIZE_PASS(RegBankSelect, "regbankselect",
30 "Assign register bank of generic virtual registers",
31 false, false);
32
Quentin Colombet46df7222016-05-20 16:55:35 +000033RegBankSelect::RegBankSelect(Mode RunningMode)
34 : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr),
35 OptMode(RunningMode) {
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000036 initializeRegBankSelectPass(*PassRegistry::getPassRegistry());
37}
38
Quentin Colombet40ad5732016-04-07 18:19:27 +000039void RegBankSelect::init(MachineFunction &MF) {
40 RBI = MF.getSubtarget().getRegBankInfo();
41 assert(RBI && "Cannot work without RegisterBankInfo");
42 MRI = &MF.getRegInfo();
Quentin Colombetaac71a42016-04-07 21:32:23 +000043 TRI = MF.getSubtarget().getRegisterInfo();
Quentin Colombet46df7222016-05-20 16:55:35 +000044 assert(OptMode == Mode::Fast && "Non-fast mode not implemented");
Quentin Colombet40ad5732016-04-07 18:19:27 +000045 MIRBuilder.setMF(MF);
46}
47
48bool RegBankSelect::assignmentMatch(
Quentin Colombet0d77da42016-05-20 00:42:57 +000049 unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping,
50 bool &OnlyAssign) const {
51 // By default we assume we will have to repair something.
52 OnlyAssign = false;
Quentin Colombet40ad5732016-04-07 18:19:27 +000053 // Each part of a break down needs to end up in a different register.
54 // In other word, Reg assignement does not match.
55 if (ValMapping.BreakDown.size() > 1)
56 return false;
57
Quentin Colombet6d6d6af2016-04-08 16:48:16 +000058 const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
59 const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank;
Quentin Colombet0d77da42016-05-20 00:42:57 +000060 // Reg is free of assignment, a simple assignment will make the
61 // register bank to match.
62 OnlyAssign = CurRegBank == nullptr;
Quentin Colombet6d6d6af2016-04-08 16:48:16 +000063 DEBUG(dbgs() << "Does assignment already match: ";
64 if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none";
65 dbgs() << " against ";
66 assert(DesiredRegBrank && "The mapping must be valid");
67 dbgs() << *DesiredRegBrank << '\n';);
68 return CurRegBank == DesiredRegBrank;
Quentin Colombet40ad5732016-04-07 18:19:27 +000069}
70
Quentin Colombetd84d00b2016-05-20 00:55:51 +000071void RegBankSelect::repairReg(
72 MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping,
73 RegBankSelect::RepairingPlacement &RepairPt,
74 const iterator_range<SmallVectorImpl<unsigned>::iterator> &NewVRegs) {
75 assert(ValMapping.BreakDown.size() == 1 && "Not yet implemented");
76 // Assume we are repairing a use and thus, the original reg will be
77 // the source of the repairing.
78 unsigned Src = MO.getReg();
79 unsigned Dst = *NewVRegs.begin();
80 if (ValMapping.BreakDown.size() == 1)
81 MO.setReg(Dst);
Quentin Colombet904a2c72016-04-12 00:12:59 +000082
Quentin Colombetd84d00b2016-05-20 00:55:51 +000083 // If we repair a definition, swap the source and destination for
84 // the repairing.
85 if (MO.isDef())
Quentin Colombet904a2c72016-04-12 00:12:59 +000086 std::swap(Src, Dst);
Quentin Colombet904a2c72016-04-12 00:12:59 +000087
Quentin Colombetd84d00b2016-05-20 00:55:51 +000088 assert((RepairPt.getNumInsertPoints() == 1 ||
89 TargetRegisterInfo::isPhysicalRegister(Dst)) &&
90 "We are about to create several defs for Dst");
Quentin Colombet904a2c72016-04-12 00:12:59 +000091
Quentin Colombetd84d00b2016-05-20 00:55:51 +000092 // Build the instruction used to repair, then clone it at the right places.
93 MachineInstr *MI = MIRBuilder.buildInstr(TargetOpcode::COPY, Dst, Src);
94 MI->removeFromParent();
95 DEBUG(dbgs() << "Copy: " << PrintReg(Src) << " to: " << PrintReg(Dst)
96 << '\n');
97 // TODO:
98 // Check if MI is legal. if not, we need to legalize all the
99 // instructions we are going to insert.
100 std::unique_ptr<MachineInstr *[]> NewInstrs(
101 new MachineInstr *[RepairPt.getNumInsertPoints()]);
102 bool IsFirst = true;
103 unsigned Idx = 0;
104 for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
105 MachineInstr *CurMI;
106 if (IsFirst)
107 CurMI = MI;
108 else
109 CurMI = MIRBuilder.getMF().CloneMachineInstr(MI);
110 InsertPt->insert(*CurMI);
111 NewInstrs[Idx++] = CurMI;
112 IsFirst = false;
113 }
114 // TODO:
115 // Legalize NewInstrs if need be.
Quentin Colombet40ad5732016-04-07 18:19:27 +0000116}
117
Quentin Colombet904a2c72016-04-12 00:12:59 +0000118void RegBankSelect::setSafeInsertionPoint(MachineInstr &InsertPt, bool Before) {
119 // Check that we are not looking to insert before a phi.
120 // Indeed, we would need more information on what to do.
121 // By default that should be all the predecessors, but this is
122 // probably not what we want in general.
123 assert((!Before || !InsertPt.isPHI()) &&
124 "Insertion before phis not implemented");
125 // The same kind of observation hold for terminators if we try to
126 // insert after them.
127 assert((Before || !InsertPt.isTerminator()) &&
128 "Insertion after terminatos not implemented");
129 if (InsertPt.isPHI()) {
130 assert(!Before && "Not supported!!");
131 MachineBasicBlock *MBB = InsertPt.getParent();
132 assert(MBB && "Insertion point is not in a basic block");
133 MachineBasicBlock::iterator FirstNonPHIPt = MBB->getFirstNonPHI();
134 if (FirstNonPHIPt == MBB->end()) {
135 // If there is not any non-phi instruction, insert at the end of MBB.
136 MIRBuilder.setMBB(*MBB, /*Beginning*/ false);
137 return;
138 }
139 // The insertion point before the first non-phi instruction.
140 MIRBuilder.setInstr(*FirstNonPHIPt, /*Before*/ true);
141 return;
142 }
143 if (InsertPt.isTerminator()) {
144 MachineBasicBlock *MBB = InsertPt.getParent();
145 assert(MBB && "Insertion point is not in a basic block");
146 MIRBuilder.setInstr(*MBB->getFirstTerminator(), /*Before*/ true);
147 return;
148 }
149 MIRBuilder.setInstr(InsertPt, /*Before*/ Before);
150}
151
Quentin Colombetf75c2bf2016-05-20 16:36:12 +0000152void RegBankSelect::tryAvoidingSplit(
153 RegBankSelect::RepairingPlacement &RepairPt, const MachineOperand &MO,
154 const RegisterBankInfo::ValueMapping &ValMapping) const {
155 const MachineInstr &MI = *MO.getParent();
156 assert(RepairPt.hasSplit() && "We should not have to adjust for split");
157 // Splitting should only occur for PHIs or between terminators,
158 // because we only do local repairing.
159 assert((MI.isPHI() || MI.isTerminator()) && "Why do we split?");
160
161 assert(&MI.getOperand(RepairPt.getOpIdx()) == &MO &&
162 "Repairing placement does not match operand");
163
164 // If we need splitting for phis, that means it is because we
165 // could not find an insertion point before the terminators of
166 // the predecessor block for this argument. In other words,
167 // the input value is defined by one of the terminators.
168 assert((!MI.isPHI() || !MO.isDef()) && "Need split for phi def?");
169
170 // We split to repair the use of a phi or a terminator.
171 if (!MO.isDef()) {
172 if (MI.isTerminator()) {
173 assert(&MI != &(*MI.getParent()->getFirstTerminator()) &&
174 "Need to split for the first terminator?!");
175 } else {
176 // For the PHI case, the split may not be actually required.
177 // In the copy case, a phi is already a copy on the incoming edge,
178 // therefore there is no need to split.
179 if (ValMapping.BreakDown.size() == 1)
180 // This is a already a copy, there is nothing to do.
181 RepairPt.switchTo(RepairingPlacement::RepairingKind::Reassign);
182 }
183 return;
184 }
185
186 // At this point, we need to repair a defintion of a terminator.
187
188 // Technically we need to fix the def of MI on all outgoing
189 // edges of MI to keep the repairing local. In other words, we
190 // will create several definitions of the same register. This
191 // does not work for SSA unless that definition is a physical
192 // register.
193 // However, there are other cases where we can get away with
194 // that while still keeping the repairing local.
195 assert(MI.isTerminator() && MO.isDef() &&
196 "This code is for the def of a terminator");
197
198 // Since we use RPO traversal, if we need to repair a definition
199 // this means this definition could be:
200 // 1. Used by PHIs (i.e., this VReg has been visited as part of the
201 // uses of a phi.), or
202 // 2. Part of a target specific instruction (i.e., the target applied
203 // some register class constraints when creating the instruction.)
204 // If the constraints come for #2, the target said that another mapping
205 // is supported so we may just drop them. Indeed, if we do not change
206 // the number of registers holding that value, the uses will get fixed
207 // when we get to them.
208 // Uses in PHIs may have already been proceeded though.
209 // If the constraints come for #1, then, those are weak constraints and
210 // no actual uses may rely on them. However, the problem remains mainly
211 // the same as for #2. If the value stays in one register, we could
212 // just switch the register bank of the definition, but we would need to
213 // account for a repairing cost for each phi we silently change.
214 //
215 // In any case, if the value needs to be broken down into several
216 // registers, the repairing is not local anymore as we need to patch
217 // every uses to rebuild the value in just one register.
218 //
219 // To summarize:
220 // - If the value is in a physical register, we can do the split and
221 // fix locally.
222 // Otherwise if the value is in a virtual register:
223 // - If the value remains in one register, we do not have to split
224 // just switching the register bank would do, but we need to account
225 // in the repairing cost all the phi we changed.
226 // - If the value spans several registers, then we cannot do a local
227 // repairing.
228
229 // Check if this is a physical or virtual register.
230 unsigned Reg = MO.getReg();
231 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
232 // We are going to split every outgoing edges.
233 // Check that this is possible.
234 // FIXME: The machine representation is currently broken
235 // since it also several terminators in one basic block.
236 // Because of that we would technically need a way to get
237 // the targets of just one terminator to know which edges
238 // we have to split.
239 // Assert that we do not hit the ill-formed representation.
240
241 // If there are other terminators before that one, some of
242 // the outgoing edges may not be dominated by this definition.
243 assert(&MI == &(*MI.getParent()->getFirstTerminator()) &&
244 "Do not know which outgoing edges are relevant");
245 const MachineInstr *Next = MI.getNextNode();
246 assert((!Next || Next->isUnconditionalBranch()) &&
247 "Do not know where each terminator ends up");
248 if (Next)
249 // If the next terminator uses Reg, this means we have
250 // to split right after MI and thus we need a way to ask
251 // which outgoing edges are affected.
252 assert(!Next->readsRegister(Reg) && "Need to split between terminators");
253 // We will split all the edges and repair there.
254 } else {
255 // This is a virtual register defined by a terminator.
256 if (ValMapping.BreakDown.size() == 1) {
257 // There is nothing to repair, but we may actually lie on
258 // the repairing cost because of the PHIs already proceeded
259 // as already stated.
260 // Though the code will be correct.
261 assert(0 && "Repairing cost may not be accurate");
262 } else {
263 // We need to do non-local repairing. Basically, patch all
264 // the uses (i.e., phis) that we already proceeded.
265 // For now, just say this mapping is not possible.
266 RepairPt.switchTo(RepairingPlacement::RepairingKind::Impossible);
267 }
268 }
269}
270
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000271RegBankSelect::MappingCost RegBankSelect::computeMapping(
272 MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
273 SmallVectorImpl<RepairingPlacement> &RepairPts) {
Quentin Colombete16f5612016-04-07 23:53:55 +0000274
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000275 // If mapped with InstrMapping, MI will have the recorded cost.
276 MappingCost Cost(1);
277 bool Saturated = Cost.addLocalCost(InstrMapping.getCost());
278 assert(!Saturated && "Possible mapping saturated the cost");
279 DEBUG(dbgs() << "Evaluating mapping cost for: " << MI);
280 DEBUG(dbgs() << "With: " << InstrMapping << '\n');
281 RepairPts.clear();
282 // Moreover, to realize this mapping, the register bank of each operand must
283 // match this mapping. In other words, we may need to locally reassign the
284 // register banks. Account for that repairing cost as well.
285 // In this context, local means in the surrounding of MI.
286 for (unsigned OpIdx = 0, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000287 ++OpIdx) {
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000288 const MachineOperand &MO = MI.getOperand(OpIdx);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000289 if (!MO.isReg())
290 continue;
291 unsigned Reg = MO.getReg();
292 if (!Reg)
293 continue;
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000294 DEBUG(dbgs() << "Opd" << OpIdx);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000295 const RegisterBankInfo::ValueMapping &ValMapping =
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000296 InstrMapping.getOperandMapping(OpIdx);
297 // If Reg is already properly mapped, this is free.
298 bool Assign;
299 if (assignmentMatch(Reg, ValMapping, Assign)) {
300 DEBUG(dbgs() << " is free (match).\n");
Quentin Colombet40ad5732016-04-07 18:19:27 +0000301 continue;
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000302 }
303 if (Assign) {
304 DEBUG(dbgs() << " is free (simple assignment).\n");
305 RepairPts.emplace_back(RepairingPlacement(MI, OpIdx, *TRI, *this,
306 RepairingPlacement::Reassign));
307 continue;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000308 }
Quentin Colombet904a2c72016-04-12 00:12:59 +0000309
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000310 // TODO:
311 // Ask the repairing module how much it would cost to get this mapping.
312 // Use: NewSources <- Val.
313 // Same size: copy.
314 // Different size: Src1, Src2, ... =
315 // extract_value Val, Src1Begin, Src1Len, Src2Begin, Src2Len, ...
316 // Def: Val <- NewDefs
317 // Same size: copy
318 // Different size: Val = build_sequence Defs1, Defs2, ...
319 // We should remember that this value is available somewhere else to
320 // coalesce the value.
Quentin Colombet904a2c72016-04-12 00:12:59 +0000321
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000322 // Find the insertion point for the repairing code.
323 RepairPts.emplace_back(
324 RepairingPlacement(MI, OpIdx, *TRI, *this, RepairingPlacement::Insert));
325 RepairingPlacement &RepairPt = RepairPts.back();
326
Quentin Colombetf75c2bf2016-05-20 16:36:12 +0000327 // If we need to split a basic block to materialize this insertion point,
328 // we may give a higher cost to this mapping.
329 // Nevertheless, we may get away with the split, so try that first.
330 if (RepairPt.hasSplit())
331 tryAvoidingSplit(RepairPt, MO, ValMapping);
332
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000333 // Check that the materialization of the repairing is possible.
334 if (!RepairPt.canMaterialize())
335 return MappingCost::ImpossibleCost();
336
337 // Account for the split cost and repair cost.
338 // Unless the cost is already saturated.
339 if (Saturated)
340 continue;
341
342 // Sums up the repairing cost of at each insertion point.
343 // TODO: Get the actual repairing cost.
344 uint64_t RepairCost = 1;
345 // Bias used for splitting: 5%.
346 const uint64_t PercentageForBias = 5;
347 uint64_t Bias = (RepairCost * PercentageForBias + 99) / 100;
348 // We should not need more than a couple of instructions to repair
349 // an assignment. In other words, the computation should not
350 // overflow because the repairing cost is free of basic block
351 // frequency.
352 assert(((RepairCost < RepairCost * PercentageForBias) &&
353 (RepairCost * PercentageForBias <
354 RepairCost * PercentageForBias + 99)) &&
355 "Repairing involves more than a billion of instructions?!");
356 for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
357 assert(InsertPt->canMaterialize() && "We should not have made it here");
358 // We will applied some basic block frequency and those uses uint64_t.
359 if (!InsertPt->isSplit())
360 Saturated = Cost.addLocalCost(RepairCost);
361 else {
362 uint64_t CostForInsertPt = RepairCost;
363 // Again we shouldn't overflow here givent that
364 // CostForInsertPt is frequency free at this point.
365 assert(CostForInsertPt + Bias > CostForInsertPt &&
366 "Repairing + split bias overflows");
367 CostForInsertPt += Bias;
368 uint64_t PtCost = InsertPt->frequency(*this) * CostForInsertPt;
369 // Check if we just overflowed.
370 if ((Saturated = PtCost < CostForInsertPt))
371 Cost.saturate();
372 else
373 Saturated = Cost.addNonLocalCost(PtCost);
374 }
375 // No need to accumulate more cost information.
376 // We need to still gather the repairing information though.
377 if (Saturated)
378 break;
379 }
Quentin Colombet40ad5732016-04-07 18:19:27 +0000380 }
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000381 return Cost;
382}
383
384void RegBankSelect::applyMapping(
385 MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
386 SmallVectorImpl<RegBankSelect::RepairingPlacement> &RepairPts) {
387 assert(InstrMapping.getID() == RegisterBankInfo::DefaultMappingID &&
388 "Rewriting of MI not implemented yet");
389 // First, place the repairing code.
390 bool NeedRewrite = false;
391 SmallVector<unsigned, 8> NewVRegs;
392 for (RepairingPlacement &RepairPt : RepairPts) {
393 assert(RepairPt.canMaterialize() &&
394 RepairPt.getKind() != RepairingPlacement::Impossible &&
395 "This mapping is impossible");
396 assert(RepairPt.getKind() != RepairingPlacement::None &&
397 "This should not make its way in the list");
398 unsigned OpIdx = RepairPt.getOpIdx();
399 MachineOperand &MO = MI.getOperand(OpIdx);
400 const RegisterBankInfo::ValueMapping &ValMapping =
401 InstrMapping.getOperandMapping(OpIdx);
402 unsigned BreakDownSize = ValMapping.BreakDown.size();
403 unsigned Reg = MO.getReg();
404 NeedRewrite = BreakDownSize != 1;
405
406 switch (RepairPt.getKind()) {
407 case RepairingPlacement::Reassign:
408 assert(BreakDownSize == 1 &&
409 "Reassignment should only be for simple mapping");
410 MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
411 break;
412 case RepairingPlacement::Insert:
413 // We need as many new virtual registers as the number of partial mapping.
414 for (const RegisterBankInfo::PartialMapping &PartMap :
415 ValMapping.BreakDown) {
416 unsigned Tmp = MRI->createGenericVirtualRegister(PartMap.Length);
417 MRI->setRegBank(Tmp, *PartMap.RegBank);
418 NewVRegs.push_back(Tmp);
419 }
420 repairReg(MO, ValMapping, RepairPt,
421 make_range(NewVRegs.end() - BreakDownSize, NewVRegs.end()));
422 break;
423 default:
424 llvm_unreachable("Other kind should not happen");
425 }
426 }
427 // Second, rewrite the instruction.
428 (void)NeedRewrite;
429 assert(!NeedRewrite && "Not implemented yet");
430}
431
432void RegBankSelect::assignInstr(MachineInstr &MI) {
433 DEBUG(dbgs() << "Assign: " << MI);
434 RegisterBankInfo::InstructionMapping DefaultMapping =
435 RBI->getInstrMapping(MI);
436 // Remember the repairing placement for all the operands.
437 SmallVector<RepairingPlacement, 4> RepairPts;
438
439 MappingCost DefaultCost = computeMapping(MI, DefaultMapping, RepairPts);
440 (void)DefaultCost;
441 assert(DefaultCost != MappingCost::ImpossibleCost() &&
442 "Default mapping is not suited");
443
444 // Make sure the mapping is valid for MI.
445 assert(DefaultMapping.verify(MI) && "Invalid instruction mapping");
446
447 DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n');
448
449 applyMapping(MI, DefaultMapping, RepairPts);
450
Quentin Colombete16f5612016-04-07 23:53:55 +0000451 DEBUG(dbgs() << "Assigned: " << MI);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000452}
453
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000454bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombete16f5612016-04-07 23:53:55 +0000455 DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
Quentin Colombeta5530122016-05-20 17:36:54 +0000456 const Function *F = MF.getFunction();
457 Mode SaveOptMode = OptMode;
458 if (F->hasFnAttribute(Attribute::OptimizeNone))
459 OptMode = Mode::Fast;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000460 init(MF);
461 // Walk the function and assign register banks to all operands.
Quentin Colombetab8c21f2016-04-08 17:19:10 +0000462 // Use a RPOT to make sure all registers are assigned before we choose
463 // the best mapping of the current instruction.
464 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000465 for (MachineBasicBlock *MBB : RPOT) {
466 // Set a sensible insertion point so that subsequent calls to
467 // MIRBuilder.
468 MIRBuilder.setMBB(*MBB);
Quentin Colombetab8c21f2016-04-08 17:19:10 +0000469 for (MachineInstr &MI : *MBB)
Quentin Colombet40ad5732016-04-07 18:19:27 +0000470 assignInstr(MI);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000471 }
Quentin Colombeta5530122016-05-20 17:36:54 +0000472 OptMode = SaveOptMode;
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000473 return false;
474}
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000475
476//------------------------------------------------------------------------------
Quentin Colombet55650752016-05-20 00:49:10 +0000477// Helper Classes Implementation
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000478//------------------------------------------------------------------------------
Quentin Colombet55650752016-05-20 00:49:10 +0000479RegBankSelect::RepairingPlacement::RepairingPlacement(
480 MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P,
481 RepairingPlacement::RepairingKind Kind)
482 // Default is, we are going to insert code to repair OpIdx.
483 : Kind(Kind),
484 OpIdx(OpIdx),
485 CanMaterialize(Kind != RepairingKind::Impossible),
486 HasSplit(false),
487 P(P) {
488 const MachineOperand &MO = MI.getOperand(OpIdx);
489 assert(MO.isReg() && "Trying to repair a non-reg operand");
490
491 if (Kind != RepairingKind::Insert)
492 return;
493
494 // Repairings for definitions happen after MI, uses happen before.
495 bool Before = !MO.isDef();
496
497 // Check if we are done with MI.
498 if (!MI.isPHI() && !MI.isTerminator()) {
499 addInsertPoint(MI, Before);
500 // We are done with the initialization.
501 return;
502 }
503
504 // Now, look for the special cases.
505 if (MI.isPHI()) {
506 // - PHI must be the first instructions:
507 // * Before, we have to split the related incoming edge.
508 // * After, move the insertion point past the last phi.
509 if (!Before) {
510 MachineBasicBlock::iterator It = MI.getParent()->getFirstNonPHI();
511 if (It != MI.getParent()->end())
512 addInsertPoint(*It, /*Before*/ true);
513 else
514 addInsertPoint(*(--It), /*Before*/ false);
515 return;
516 }
517 // We repair a use of a phi, we may need to split the related edge.
518 MachineBasicBlock &Pred = *MI.getOperand(OpIdx + 1).getMBB();
519 // Check if we can move the insertion point prior to the
520 // terminators of the predecessor.
521 unsigned Reg = MO.getReg();
522 MachineBasicBlock::iterator It = Pred.getLastNonDebugInstr();
523 for (auto Begin = Pred.begin(); It != Begin && It->isTerminator(); --It)
524 if (It->modifiesRegister(Reg, &TRI)) {
525 // We cannot hoist the repairing code in the predecessor.
526 // Split the edge.
527 addInsertPoint(Pred, *MI.getParent());
528 return;
529 }
530 // At this point, we can insert in Pred.
531
532 // - If It is invalid, Pred is empty and we can insert in Pred
533 // wherever we want.
534 // - If It is valid, It is the first non-terminator, insert after It.
535 if (It == Pred.end())
536 addInsertPoint(Pred, /*Beginning*/ false);
537 else
538 addInsertPoint(*It, /*Before*/ false);
539 } else {
540 // - Terminators must be the last instructions:
541 // * Before, move the insert point before the first terminator.
542 // * After, we have to split the outcoming edges.
543 unsigned Reg = MO.getReg();
544 if (Before) {
545 // Check whether Reg is defined by any terminator.
546 MachineBasicBlock::iterator It = MI;
547 for (auto Begin = MI.getParent()->begin();
548 --It != Begin && It->isTerminator();)
549 if (It->modifiesRegister(Reg, &TRI)) {
550 // Insert the repairing code right after the definition.
551 addInsertPoint(*It, /*Before*/ false);
552 return;
553 }
554 addInsertPoint(*It, /*Before*/ true);
555 return;
556 }
557 // Make sure Reg is not redefined by other terminators, otherwise
558 // we do not know how to split.
559 for (MachineBasicBlock::iterator It = MI, End = MI.getParent()->end();
560 ++It != End;)
561 // The machine verifier should reject this kind of code.
562 assert(It->modifiesRegister(Reg, &TRI) && "Do not know where to split");
563 // Split each outcoming edges.
564 MachineBasicBlock &Src = *MI.getParent();
565 for (auto &Succ : Src.successors())
566 addInsertPoint(Src, Succ);
567 }
568}
569
570void RegBankSelect::RepairingPlacement::addInsertPoint(MachineInstr &MI,
571 bool Before) {
572 addInsertPoint(*new InstrInsertPoint(MI, Before));
573}
574
575void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &MBB,
576 bool Beginning) {
577 addInsertPoint(*new MBBInsertPoint(MBB, Beginning));
578}
579
580void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &Src,
581 MachineBasicBlock &Dst) {
582 addInsertPoint(*new EdgeInsertPoint(Src, Dst, P));
583}
584
585void RegBankSelect::RepairingPlacement::addInsertPoint(
586 RegBankSelect::InsertPoint &Point) {
587 CanMaterialize &= Point.canMaterialize();
588 HasSplit |= Point.isSplit();
589 InsertPoints.emplace_back(&Point);
590}
591
592RegBankSelect::InstrInsertPoint::InstrInsertPoint(MachineInstr &Instr,
593 bool Before)
594 : InsertPoint(), Instr(Instr), Before(Before) {
595 // Since we do not support splitting, we do not need to update
596 // liveness and such, so do not do anything with P.
597 assert((!Before || !Instr.isPHI()) &&
598 "Splitting before phis requires more points");
599 assert((!Before || !Instr.getNextNode() || !Instr.getNextNode()->isPHI()) &&
600 "Splitting between phis does not make sense");
601}
602
603void RegBankSelect::InstrInsertPoint::materialize() {
604 if (isSplit()) {
605 // Slice and return the beginning of the new block.
606 // If we need to split between the terminators, we theoritically
607 // need to know where the first and second set of terminators end
608 // to update the successors properly.
609 // Now, in pratice, we should have a maximum of 2 branch
610 // instructions; one conditional and one unconditional. Therefore
611 // we know how to update the successor by looking at the target of
612 // the unconditional branch.
613 // If we end up splitting at some point, then, we should update
614 // the liveness information and such. I.e., we would need to
615 // access P here.
616 // The machine verifier should actually make sure such cases
617 // cannot happen.
618 llvm_unreachable("Not yet implemented");
619 }
620 // Otherwise the insertion point is just the current or next
621 // instruction depending on Before. I.e., there is nothing to do
622 // here.
623}
624
625bool RegBankSelect::InstrInsertPoint::isSplit() const {
626 // If the insertion point is after a terminator, we need to split.
627 if (!Before)
628 return Instr.isTerminator();
629 // If we insert before an instruction that is after a terminator,
630 // we are still after a terminator.
631 return Instr.getPrevNode() && Instr.getPrevNode()->isTerminator();
632}
633
634uint64_t RegBankSelect::InstrInsertPoint::frequency(const Pass &P) const {
635 // Even if we need to split, because we insert between terminators,
636 // this split has actually the same frequency as the instruction.
637 const MachineBlockFrequencyInfo *MBFI =
638 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
639 if (!MBFI)
640 return 1;
641 return MBFI->getBlockFreq(Instr.getParent()).getFrequency();
642}
643
644uint64_t RegBankSelect::MBBInsertPoint::frequency(const Pass &P) const {
645 const MachineBlockFrequencyInfo *MBFI =
646 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
647 if (!MBFI)
648 return 1;
649 return MBFI->getBlockFreq(&MBB).getFrequency();
650}
651
652void RegBankSelect::EdgeInsertPoint::materialize() {
653 // If we end up repairing twice at the same place before materializing the
654 // insertion point, we may think we have to split an edge twice.
655 // We should have a factory for the insert point such that identical points
656 // are the same instance.
657 assert(Src.isSuccessor(DstOrSplit) && DstOrSplit->isPredecessor(&Src) &&
658 "This point has already been split");
659 MachineBasicBlock *NewBB = Src.SplitCriticalEdge(DstOrSplit, P);
660 assert(NewBB && "Invalid call to materialize");
661 // We reuse the destination block to hold the information of the new block.
662 DstOrSplit = NewBB;
663}
664
665uint64_t RegBankSelect::EdgeInsertPoint::frequency(const Pass &P) const {
666 const MachineBlockFrequencyInfo *MBFI =
667 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
668 if (!MBFI)
669 return 1;
670 if (WasMaterialized)
671 return MBFI->getBlockFreq(DstOrSplit).getFrequency();
672
673 const MachineBranchProbabilityInfo *MBPI =
674 P.getAnalysisIfAvailable<MachineBranchProbabilityInfo>();
675 if (!MBPI)
676 return 1;
677 // The basic block will be on the edge.
678 return (MBFI->getBlockFreq(&Src) * MBPI->getEdgeProbability(&Src, DstOrSplit))
679 .getFrequency();
680}
681
682bool RegBankSelect::EdgeInsertPoint::canMaterialize() const {
683 // If this is not a critical edge, we should not have used this insert
684 // point. Indeed, either the successor or the predecessor should
685 // have do.
686 assert(Src.succ_size() > 1 && DstOrSplit->pred_size() > 1 &&
687 "Edge is not critical");
688 return Src.canSplitCriticalEdge(DstOrSplit);
689}
690
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000691RegBankSelect::MappingCost::MappingCost(const BlockFrequency &LocalFreq)
692 : LocalCost(0), NonLocalCost(0), LocalFreq(LocalFreq.getFrequency()) {}
693
694bool RegBankSelect::MappingCost::addLocalCost(uint64_t Cost) {
695 // Check if this overflows.
696 if (LocalCost + Cost < LocalCost) {
697 saturate();
698 return true;
699 }
700 LocalCost += Cost;
701 return isSaturated();
702}
703
704bool RegBankSelect::MappingCost::addNonLocalCost(uint64_t Cost) {
705 // Check if this overflows.
706 if (NonLocalCost + Cost < NonLocalCost) {
707 saturate();
708 return true;
709 }
710 NonLocalCost += Cost;
711 return isSaturated();
712}
713
714bool RegBankSelect::MappingCost::isSaturated() const {
715 return LocalCost == UINT64_MAX - 1 && NonLocalCost == UINT64_MAX &&
716 LocalFreq == UINT64_MAX;
717}
718
719void RegBankSelect::MappingCost::saturate() {
720 *this = ImpossibleCost();
721 --LocalCost;
722}
723
724RegBankSelect::MappingCost RegBankSelect::MappingCost::ImpossibleCost() {
725 return MappingCost(UINT64_MAX, UINT64_MAX, UINT64_MAX);
726}
727
728bool RegBankSelect::MappingCost::operator<(const MappingCost &Cost) const {
729 // Sort out the easy cases.
730 if (*this == Cost)
731 return false;
732 // If one is impossible to realize the other is cheaper unless it is
733 // impossible as well.
734 if ((*this == ImpossibleCost()) || (Cost == ImpossibleCost()))
735 return (*this == ImpossibleCost()) < (Cost == ImpossibleCost());
736 // If one is saturated the other is cheaper, unless it is saturated
737 // as well.
738 if (isSaturated() || Cost.isSaturated())
739 return isSaturated() < Cost.isSaturated();
740 // At this point we know both costs hold sensible values.
741
742 // If both values have a different base frequency, there is no much
743 // we can do but to scale everything.
744 // However, if they have the same base frequency we can avoid making
745 // complicated computation.
746 uint64_t ThisLocalAdjust;
747 uint64_t OtherLocalAdjust;
748 if (LLVM_LIKELY(LocalFreq == Cost.LocalFreq)) {
749
750 // At this point, we know the local costs are comparable.
751 // Do the case that do not involve potential overflow first.
752 if (NonLocalCost == Cost.NonLocalCost)
753 // Since the non-local costs do not discriminate on the result,
754 // just compare the local costs.
755 return LocalCost < Cost.LocalCost;
756
757 // The base costs are comparable so we may only keep the relative
758 // value to increase our chances of avoiding overflows.
759 ThisLocalAdjust = 0;
760 OtherLocalAdjust = 0;
761 if (LocalCost < Cost.LocalCost)
762 OtherLocalAdjust = Cost.LocalCost - LocalCost;
763 else
764 ThisLocalAdjust = LocalCost - Cost.LocalCost;
765
766 } else {
767 ThisLocalAdjust = LocalCost;
768 OtherLocalAdjust = Cost.LocalCost;
769 }
770
771 // The non-local costs are comparable, just keep the relative value.
772 uint64_t ThisNonLocalAdjust = 0;
773 uint64_t OtherNonLocalAdjust = 0;
774 if (NonLocalCost < Cost.NonLocalCost)
775 OtherNonLocalAdjust = Cost.NonLocalCost - NonLocalCost;
776 else
777 ThisNonLocalAdjust = NonLocalCost - Cost.NonLocalCost;
778 // Scale everything to make them comparable.
779 uint64_t ThisScaledCost = ThisLocalAdjust * LocalFreq;
780 // Check for overflow on that operation.
781 bool ThisOverflows = ThisLocalAdjust && (ThisScaledCost < ThisLocalAdjust ||
782 ThisScaledCost < LocalFreq);
783 uint64_t OtherScaledCost = OtherLocalAdjust * Cost.LocalFreq;
784 // Check for overflow on the last operation.
785 bool OtherOverflows =
786 OtherLocalAdjust &&
787 (OtherScaledCost < OtherLocalAdjust || OtherScaledCost < Cost.LocalFreq);
788 // Add the non-local costs.
789 ThisOverflows |= ThisNonLocalAdjust &&
790 ThisScaledCost + ThisNonLocalAdjust < ThisNonLocalAdjust;
791 ThisScaledCost += ThisNonLocalAdjust;
792 OtherOverflows |= OtherNonLocalAdjust &&
793 OtherScaledCost + OtherNonLocalAdjust < OtherNonLocalAdjust;
794 OtherScaledCost += OtherNonLocalAdjust;
795 // If both overflows, we cannot compare without additional
796 // precision, e.g., APInt. Just give up on that case.
797 if (ThisOverflows && OtherOverflows)
798 return false;
799 // If one overflows but not the other, we can still compare.
800 if (ThisOverflows || OtherOverflows)
801 return ThisOverflows < OtherOverflows;
802 // Otherwise, just compare the values.
803 return ThisScaledCost < OtherScaledCost;
804}
805
806bool RegBankSelect::MappingCost::operator==(const MappingCost &Cost) const {
807 return LocalCost == Cost.LocalCost && NonLocalCost == Cost.NonLocalCost &&
808 LocalFreq == Cost.LocalFreq;
809}