RegBankSelect.cpp 42 KB

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