RegBankSelect.cpp 42 KB

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