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