Utils.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. //===- llvm/CodeGen/GlobalISel/Utils.cpp -------------------------*- 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 This file implements the utility functions used by the GlobalISel
  9. /// pipeline.
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/CodeGen/GlobalISel/Utils.h"
  12. #include "llvm/ADT/APFloat.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/CodeGen/CodeGenCommonISel.h"
  15. #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
  16. #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
  17. #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
  18. #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h"
  19. #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/MachineSizeOpts.h"
  25. #include "llvm/CodeGen/RegisterBankInfo.h"
  26. #include "llvm/CodeGen/StackProtector.h"
  27. #include "llvm/CodeGen/TargetInstrInfo.h"
  28. #include "llvm/CodeGen/TargetLowering.h"
  29. #include "llvm/CodeGen/TargetPassConfig.h"
  30. #include "llvm/CodeGen/TargetRegisterInfo.h"
  31. #include "llvm/IR/Constants.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. #include "llvm/Transforms/Utils/SizeOpts.h"
  34. #include <numeric>
  35. #include <optional>
  36. #define DEBUG_TYPE "globalisel-utils"
  37. using namespace llvm;
  38. using namespace MIPatternMatch;
  39. Register llvm::constrainRegToClass(MachineRegisterInfo &MRI,
  40. const TargetInstrInfo &TII,
  41. const RegisterBankInfo &RBI, Register Reg,
  42. const TargetRegisterClass &RegClass) {
  43. if (!RBI.constrainGenericRegister(Reg, RegClass, MRI))
  44. return MRI.createVirtualRegister(&RegClass);
  45. return Reg;
  46. }
  47. Register llvm::constrainOperandRegClass(
  48. const MachineFunction &MF, const TargetRegisterInfo &TRI,
  49. MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
  50. const RegisterBankInfo &RBI, MachineInstr &InsertPt,
  51. const TargetRegisterClass &RegClass, MachineOperand &RegMO) {
  52. Register Reg = RegMO.getReg();
  53. // Assume physical registers are properly constrained.
  54. assert(Reg.isVirtual() && "PhysReg not implemented");
  55. // Save the old register class to check whether
  56. // the change notifications will be required.
  57. // TODO: A better approach would be to pass
  58. // the observers to constrainRegToClass().
  59. auto *OldRegClass = MRI.getRegClassOrNull(Reg);
  60. Register ConstrainedReg = constrainRegToClass(MRI, TII, RBI, Reg, RegClass);
  61. // If we created a new virtual register because the class is not compatible
  62. // then create a copy between the new and the old register.
  63. if (ConstrainedReg != Reg) {
  64. MachineBasicBlock::iterator InsertIt(&InsertPt);
  65. MachineBasicBlock &MBB = *InsertPt.getParent();
  66. // FIXME: The copy needs to have the classes constrained for its operands.
  67. // Use operand's regbank to get the class for old register (Reg).
  68. if (RegMO.isUse()) {
  69. BuildMI(MBB, InsertIt, InsertPt.getDebugLoc(),
  70. TII.get(TargetOpcode::COPY), ConstrainedReg)
  71. .addReg(Reg);
  72. } else {
  73. assert(RegMO.isDef() && "Must be a definition");
  74. BuildMI(MBB, std::next(InsertIt), InsertPt.getDebugLoc(),
  75. TII.get(TargetOpcode::COPY), Reg)
  76. .addReg(ConstrainedReg);
  77. }
  78. if (GISelChangeObserver *Observer = MF.getObserver()) {
  79. Observer->changingInstr(*RegMO.getParent());
  80. }
  81. RegMO.setReg(ConstrainedReg);
  82. if (GISelChangeObserver *Observer = MF.getObserver()) {
  83. Observer->changedInstr(*RegMO.getParent());
  84. }
  85. } else if (OldRegClass != MRI.getRegClassOrNull(Reg)) {
  86. if (GISelChangeObserver *Observer = MF.getObserver()) {
  87. if (!RegMO.isDef()) {
  88. MachineInstr *RegDef = MRI.getVRegDef(Reg);
  89. Observer->changedInstr(*RegDef);
  90. }
  91. Observer->changingAllUsesOfReg(MRI, Reg);
  92. Observer->finishedChangingAllUsesOfReg();
  93. }
  94. }
  95. return ConstrainedReg;
  96. }
  97. Register llvm::constrainOperandRegClass(
  98. const MachineFunction &MF, const TargetRegisterInfo &TRI,
  99. MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
  100. const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
  101. MachineOperand &RegMO, unsigned OpIdx) {
  102. Register Reg = RegMO.getReg();
  103. // Assume physical registers are properly constrained.
  104. assert(Reg.isVirtual() && "PhysReg not implemented");
  105. const TargetRegisterClass *OpRC = TII.getRegClass(II, OpIdx, &TRI, MF);
  106. // Some of the target independent instructions, like COPY, may not impose any
  107. // register class constraints on some of their operands: If it's a use, we can
  108. // skip constraining as the instruction defining the register would constrain
  109. // it.
  110. if (OpRC) {
  111. // Obtain the RC from incoming regbank if it is a proper sub-class. Operands
  112. // can have multiple regbanks for a superclass that combine different
  113. // register types (E.g., AMDGPU's VGPR and AGPR). The regbank ambiguity
  114. // resolved by targets during regbankselect should not be overridden.
  115. if (const auto *SubRC = TRI.getCommonSubClass(
  116. OpRC, TRI.getConstrainedRegClassForOperand(RegMO, MRI)))
  117. OpRC = SubRC;
  118. OpRC = TRI.getAllocatableClass(OpRC);
  119. }
  120. if (!OpRC) {
  121. assert((!isTargetSpecificOpcode(II.getOpcode()) || RegMO.isUse()) &&
  122. "Register class constraint is required unless either the "
  123. "instruction is target independent or the operand is a use");
  124. // FIXME: Just bailing out like this here could be not enough, unless we
  125. // expect the users of this function to do the right thing for PHIs and
  126. // COPY:
  127. // v1 = COPY v0
  128. // v2 = COPY v1
  129. // v1 here may end up not being constrained at all. Please notice that to
  130. // reproduce the issue we likely need a destination pattern of a selection
  131. // rule producing such extra copies, not just an input GMIR with them as
  132. // every existing target using selectImpl handles copies before calling it
  133. // and they never reach this function.
  134. return Reg;
  135. }
  136. return constrainOperandRegClass(MF, TRI, MRI, TII, RBI, InsertPt, *OpRC,
  137. RegMO);
  138. }
  139. bool llvm::constrainSelectedInstRegOperands(MachineInstr &I,
  140. const TargetInstrInfo &TII,
  141. const TargetRegisterInfo &TRI,
  142. const RegisterBankInfo &RBI) {
  143. assert(!isPreISelGenericOpcode(I.getOpcode()) &&
  144. "A selected instruction is expected");
  145. MachineBasicBlock &MBB = *I.getParent();
  146. MachineFunction &MF = *MBB.getParent();
  147. MachineRegisterInfo &MRI = MF.getRegInfo();
  148. for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
  149. MachineOperand &MO = I.getOperand(OpI);
  150. // There's nothing to be done on non-register operands.
  151. if (!MO.isReg())
  152. continue;
  153. LLVM_DEBUG(dbgs() << "Converting operand: " << MO << '\n');
  154. assert(MO.isReg() && "Unsupported non-reg operand");
  155. Register Reg = MO.getReg();
  156. // Physical registers don't need to be constrained.
  157. if (Reg.isPhysical())
  158. continue;
  159. // Register operands with a value of 0 (e.g. predicate operands) don't need
  160. // to be constrained.
  161. if (Reg == 0)
  162. continue;
  163. // If the operand is a vreg, we should constrain its regclass, and only
  164. // insert COPYs if that's impossible.
  165. // constrainOperandRegClass does that for us.
  166. constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), MO, OpI);
  167. // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
  168. // done.
  169. if (MO.isUse()) {
  170. int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
  171. if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
  172. I.tieOperands(DefIdx, OpI);
  173. }
  174. }
  175. return true;
  176. }
  177. bool llvm::canReplaceReg(Register DstReg, Register SrcReg,
  178. MachineRegisterInfo &MRI) {
  179. // Give up if either DstReg or SrcReg is a physical register.
  180. if (DstReg.isPhysical() || SrcReg.isPhysical())
  181. return false;
  182. // Give up if the types don't match.
  183. if (MRI.getType(DstReg) != MRI.getType(SrcReg))
  184. return false;
  185. // Replace if either DstReg has no constraints or the register
  186. // constraints match.
  187. return !MRI.getRegClassOrRegBank(DstReg) ||
  188. MRI.getRegClassOrRegBank(DstReg) == MRI.getRegClassOrRegBank(SrcReg);
  189. }
  190. bool llvm::isTriviallyDead(const MachineInstr &MI,
  191. const MachineRegisterInfo &MRI) {
  192. // FIXME: This logical is mostly duplicated with
  193. // DeadMachineInstructionElim::isDead. Why is LOCAL_ESCAPE not considered in
  194. // MachineInstr::isLabel?
  195. // Don't delete frame allocation labels.
  196. if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE)
  197. return false;
  198. // LIFETIME markers should be preserved even if they seem dead.
  199. if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
  200. MI.getOpcode() == TargetOpcode::LIFETIME_END)
  201. return false;
  202. // If we can move an instruction, we can remove it. Otherwise, it has
  203. // a side-effect of some sort.
  204. bool SawStore = false;
  205. if (!MI.isSafeToMove(/*AA=*/nullptr, SawStore) && !MI.isPHI())
  206. return false;
  207. // Instructions without side-effects are dead iff they only define dead vregs.
  208. for (const auto &MO : MI.operands()) {
  209. if (!MO.isReg() || !MO.isDef())
  210. continue;
  211. Register Reg = MO.getReg();
  212. if (Reg.isPhysical() || !MRI.use_nodbg_empty(Reg))
  213. return false;
  214. }
  215. return true;
  216. }
  217. static void reportGISelDiagnostic(DiagnosticSeverity Severity,
  218. MachineFunction &MF,
  219. const TargetPassConfig &TPC,
  220. MachineOptimizationRemarkEmitter &MORE,
  221. MachineOptimizationRemarkMissed &R) {
  222. bool IsFatal = Severity == DS_Error &&
  223. TPC.isGlobalISelAbortEnabled();
  224. // Print the function name explicitly if we don't have a debug location (which
  225. // makes the diagnostic less useful) or if we're going to emit a raw error.
  226. if (!R.getLocation().isValid() || IsFatal)
  227. R << (" (in function: " + MF.getName() + ")").str();
  228. if (IsFatal)
  229. report_fatal_error(Twine(R.getMsg()));
  230. else
  231. MORE.emit(R);
  232. }
  233. void llvm::reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
  234. MachineOptimizationRemarkEmitter &MORE,
  235. MachineOptimizationRemarkMissed &R) {
  236. reportGISelDiagnostic(DS_Warning, MF, TPC, MORE, R);
  237. }
  238. void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
  239. MachineOptimizationRemarkEmitter &MORE,
  240. MachineOptimizationRemarkMissed &R) {
  241. MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
  242. reportGISelDiagnostic(DS_Error, MF, TPC, MORE, R);
  243. }
  244. void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
  245. MachineOptimizationRemarkEmitter &MORE,
  246. const char *PassName, StringRef Msg,
  247. const MachineInstr &MI) {
  248. MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
  249. MI.getDebugLoc(), MI.getParent());
  250. R << Msg;
  251. // Printing MI is expensive; only do it if expensive remarks are enabled.
  252. if (TPC.isGlobalISelAbortEnabled() || MORE.allowExtraAnalysis(PassName))
  253. R << ": " << ore::MNV("Inst", MI);
  254. reportGISelFailure(MF, TPC, MORE, R);
  255. }
  256. std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
  257. const MachineRegisterInfo &MRI) {
  258. std::optional<ValueAndVReg> ValAndVReg = getIConstantVRegValWithLookThrough(
  259. VReg, MRI, /*LookThroughInstrs*/ false);
  260. assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
  261. "Value found while looking through instrs");
  262. if (!ValAndVReg)
  263. return std::nullopt;
  264. return ValAndVReg->Value;
  265. }
  266. std::optional<int64_t>
  267. llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI) {
  268. std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
  269. if (Val && Val->getBitWidth() <= 64)
  270. return Val->getSExtValue();
  271. return std::nullopt;
  272. }
  273. namespace {
  274. typedef std::function<bool(const MachineInstr *)> IsOpcodeFn;
  275. typedef std::function<std::optional<APInt>(const MachineInstr *MI)> GetAPCstFn;
  276. std::optional<ValueAndVReg> getConstantVRegValWithLookThrough(
  277. Register VReg, const MachineRegisterInfo &MRI, IsOpcodeFn IsConstantOpcode,
  278. GetAPCstFn getAPCstValue, bool LookThroughInstrs = true,
  279. bool LookThroughAnyExt = false) {
  280. SmallVector<std::pair<unsigned, unsigned>, 4> SeenOpcodes;
  281. MachineInstr *MI;
  282. while ((MI = MRI.getVRegDef(VReg)) && !IsConstantOpcode(MI) &&
  283. LookThroughInstrs) {
  284. switch (MI->getOpcode()) {
  285. case TargetOpcode::G_ANYEXT:
  286. if (!LookThroughAnyExt)
  287. return std::nullopt;
  288. [[fallthrough]];
  289. case TargetOpcode::G_TRUNC:
  290. case TargetOpcode::G_SEXT:
  291. case TargetOpcode::G_ZEXT:
  292. SeenOpcodes.push_back(std::make_pair(
  293. MI->getOpcode(),
  294. MRI.getType(MI->getOperand(0).getReg()).getSizeInBits()));
  295. VReg = MI->getOperand(1).getReg();
  296. break;
  297. case TargetOpcode::COPY:
  298. VReg = MI->getOperand(1).getReg();
  299. if (VReg.isPhysical())
  300. return std::nullopt;
  301. break;
  302. case TargetOpcode::G_INTTOPTR:
  303. VReg = MI->getOperand(1).getReg();
  304. break;
  305. default:
  306. return std::nullopt;
  307. }
  308. }
  309. if (!MI || !IsConstantOpcode(MI))
  310. return std::nullopt;
  311. std::optional<APInt> MaybeVal = getAPCstValue(MI);
  312. if (!MaybeVal)
  313. return std::nullopt;
  314. APInt &Val = *MaybeVal;
  315. while (!SeenOpcodes.empty()) {
  316. std::pair<unsigned, unsigned> OpcodeAndSize = SeenOpcodes.pop_back_val();
  317. switch (OpcodeAndSize.first) {
  318. case TargetOpcode::G_TRUNC:
  319. Val = Val.trunc(OpcodeAndSize.second);
  320. break;
  321. case TargetOpcode::G_ANYEXT:
  322. case TargetOpcode::G_SEXT:
  323. Val = Val.sext(OpcodeAndSize.second);
  324. break;
  325. case TargetOpcode::G_ZEXT:
  326. Val = Val.zext(OpcodeAndSize.second);
  327. break;
  328. }
  329. }
  330. return ValueAndVReg{Val, VReg};
  331. }
  332. bool isIConstant(const MachineInstr *MI) {
  333. if (!MI)
  334. return false;
  335. return MI->getOpcode() == TargetOpcode::G_CONSTANT;
  336. }
  337. bool isFConstant(const MachineInstr *MI) {
  338. if (!MI)
  339. return false;
  340. return MI->getOpcode() == TargetOpcode::G_FCONSTANT;
  341. }
  342. bool isAnyConstant(const MachineInstr *MI) {
  343. if (!MI)
  344. return false;
  345. unsigned Opc = MI->getOpcode();
  346. return Opc == TargetOpcode::G_CONSTANT || Opc == TargetOpcode::G_FCONSTANT;
  347. }
  348. std::optional<APInt> getCImmAsAPInt(const MachineInstr *MI) {
  349. const MachineOperand &CstVal = MI->getOperand(1);
  350. if (CstVal.isCImm())
  351. return CstVal.getCImm()->getValue();
  352. return std::nullopt;
  353. }
  354. std::optional<APInt> getCImmOrFPImmAsAPInt(const MachineInstr *MI) {
  355. const MachineOperand &CstVal = MI->getOperand(1);
  356. if (CstVal.isCImm())
  357. return CstVal.getCImm()->getValue();
  358. if (CstVal.isFPImm())
  359. return CstVal.getFPImm()->getValueAPF().bitcastToAPInt();
  360. return std::nullopt;
  361. }
  362. } // end anonymous namespace
  363. std::optional<ValueAndVReg> llvm::getIConstantVRegValWithLookThrough(
  364. Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
  365. return getConstantVRegValWithLookThrough(VReg, MRI, isIConstant,
  366. getCImmAsAPInt, LookThroughInstrs);
  367. }
  368. std::optional<ValueAndVReg> llvm::getAnyConstantVRegValWithLookThrough(
  369. Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs,
  370. bool LookThroughAnyExt) {
  371. return getConstantVRegValWithLookThrough(
  372. VReg, MRI, isAnyConstant, getCImmOrFPImmAsAPInt, LookThroughInstrs,
  373. LookThroughAnyExt);
  374. }
  375. std::optional<FPValueAndVReg> llvm::getFConstantVRegValWithLookThrough(
  376. Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs) {
  377. auto Reg = getConstantVRegValWithLookThrough(
  378. VReg, MRI, isFConstant, getCImmOrFPImmAsAPInt, LookThroughInstrs);
  379. if (!Reg)
  380. return std::nullopt;
  381. return FPValueAndVReg{getConstantFPVRegVal(Reg->VReg, MRI)->getValueAPF(),
  382. Reg->VReg};
  383. }
  384. const ConstantFP *
  385. llvm::getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI) {
  386. MachineInstr *MI = MRI.getVRegDef(VReg);
  387. if (TargetOpcode::G_FCONSTANT != MI->getOpcode())
  388. return nullptr;
  389. return MI->getOperand(1).getFPImm();
  390. }
  391. std::optional<DefinitionAndSourceRegister>
  392. llvm::getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI) {
  393. Register DefSrcReg = Reg;
  394. auto *DefMI = MRI.getVRegDef(Reg);
  395. auto DstTy = MRI.getType(DefMI->getOperand(0).getReg());
  396. if (!DstTy.isValid())
  397. return std::nullopt;
  398. unsigned Opc = DefMI->getOpcode();
  399. while (Opc == TargetOpcode::COPY || isPreISelGenericOptimizationHint(Opc)) {
  400. Register SrcReg = DefMI->getOperand(1).getReg();
  401. auto SrcTy = MRI.getType(SrcReg);
  402. if (!SrcTy.isValid())
  403. break;
  404. DefMI = MRI.getVRegDef(SrcReg);
  405. DefSrcReg = SrcReg;
  406. Opc = DefMI->getOpcode();
  407. }
  408. return DefinitionAndSourceRegister{DefMI, DefSrcReg};
  409. }
  410. MachineInstr *llvm::getDefIgnoringCopies(Register Reg,
  411. const MachineRegisterInfo &MRI) {
  412. std::optional<DefinitionAndSourceRegister> DefSrcReg =
  413. getDefSrcRegIgnoringCopies(Reg, MRI);
  414. return DefSrcReg ? DefSrcReg->MI : nullptr;
  415. }
  416. Register llvm::getSrcRegIgnoringCopies(Register Reg,
  417. const MachineRegisterInfo &MRI) {
  418. std::optional<DefinitionAndSourceRegister> DefSrcReg =
  419. getDefSrcRegIgnoringCopies(Reg, MRI);
  420. return DefSrcReg ? DefSrcReg->Reg : Register();
  421. }
  422. MachineInstr *llvm::getOpcodeDef(unsigned Opcode, Register Reg,
  423. const MachineRegisterInfo &MRI) {
  424. MachineInstr *DefMI = getDefIgnoringCopies(Reg, MRI);
  425. return DefMI && DefMI->getOpcode() == Opcode ? DefMI : nullptr;
  426. }
  427. APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
  428. if (Size == 32)
  429. return APFloat(float(Val));
  430. if (Size == 64)
  431. return APFloat(Val);
  432. if (Size != 16)
  433. llvm_unreachable("Unsupported FPConstant size");
  434. bool Ignored;
  435. APFloat APF(Val);
  436. APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
  437. return APF;
  438. }
  439. std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
  440. const Register Op1,
  441. const Register Op2,
  442. const MachineRegisterInfo &MRI) {
  443. auto MaybeOp2Cst = getAnyConstantVRegValWithLookThrough(Op2, MRI, false);
  444. if (!MaybeOp2Cst)
  445. return std::nullopt;
  446. auto MaybeOp1Cst = getAnyConstantVRegValWithLookThrough(Op1, MRI, false);
  447. if (!MaybeOp1Cst)
  448. return std::nullopt;
  449. const APInt &C1 = MaybeOp1Cst->Value;
  450. const APInt &C2 = MaybeOp2Cst->Value;
  451. switch (Opcode) {
  452. default:
  453. break;
  454. case TargetOpcode::G_ADD:
  455. case TargetOpcode::G_PTR_ADD:
  456. return C1 + C2;
  457. case TargetOpcode::G_AND:
  458. return C1 & C2;
  459. case TargetOpcode::G_ASHR:
  460. return C1.ashr(C2);
  461. case TargetOpcode::G_LSHR:
  462. return C1.lshr(C2);
  463. case TargetOpcode::G_MUL:
  464. return C1 * C2;
  465. case TargetOpcode::G_OR:
  466. return C1 | C2;
  467. case TargetOpcode::G_SHL:
  468. return C1 << C2;
  469. case TargetOpcode::G_SUB:
  470. return C1 - C2;
  471. case TargetOpcode::G_XOR:
  472. return C1 ^ C2;
  473. case TargetOpcode::G_UDIV:
  474. if (!C2.getBoolValue())
  475. break;
  476. return C1.udiv(C2);
  477. case TargetOpcode::G_SDIV:
  478. if (!C2.getBoolValue())
  479. break;
  480. return C1.sdiv(C2);
  481. case TargetOpcode::G_UREM:
  482. if (!C2.getBoolValue())
  483. break;
  484. return C1.urem(C2);
  485. case TargetOpcode::G_SREM:
  486. if (!C2.getBoolValue())
  487. break;
  488. return C1.srem(C2);
  489. case TargetOpcode::G_SMIN:
  490. return APIntOps::smin(C1, C2);
  491. case TargetOpcode::G_SMAX:
  492. return APIntOps::smax(C1, C2);
  493. case TargetOpcode::G_UMIN:
  494. return APIntOps::umin(C1, C2);
  495. case TargetOpcode::G_UMAX:
  496. return APIntOps::umax(C1, C2);
  497. }
  498. return std::nullopt;
  499. }
  500. std::optional<APFloat>
  501. llvm::ConstantFoldFPBinOp(unsigned Opcode, const Register Op1,
  502. const Register Op2, const MachineRegisterInfo &MRI) {
  503. const ConstantFP *Op2Cst = getConstantFPVRegVal(Op2, MRI);
  504. if (!Op2Cst)
  505. return std::nullopt;
  506. const ConstantFP *Op1Cst = getConstantFPVRegVal(Op1, MRI);
  507. if (!Op1Cst)
  508. return std::nullopt;
  509. APFloat C1 = Op1Cst->getValueAPF();
  510. const APFloat &C2 = Op2Cst->getValueAPF();
  511. switch (Opcode) {
  512. case TargetOpcode::G_FADD:
  513. C1.add(C2, APFloat::rmNearestTiesToEven);
  514. return C1;
  515. case TargetOpcode::G_FSUB:
  516. C1.subtract(C2, APFloat::rmNearestTiesToEven);
  517. return C1;
  518. case TargetOpcode::G_FMUL:
  519. C1.multiply(C2, APFloat::rmNearestTiesToEven);
  520. return C1;
  521. case TargetOpcode::G_FDIV:
  522. C1.divide(C2, APFloat::rmNearestTiesToEven);
  523. return C1;
  524. case TargetOpcode::G_FREM:
  525. C1.mod(C2);
  526. return C1;
  527. case TargetOpcode::G_FCOPYSIGN:
  528. C1.copySign(C2);
  529. return C1;
  530. case TargetOpcode::G_FMINNUM:
  531. return minnum(C1, C2);
  532. case TargetOpcode::G_FMAXNUM:
  533. return maxnum(C1, C2);
  534. case TargetOpcode::G_FMINIMUM:
  535. return minimum(C1, C2);
  536. case TargetOpcode::G_FMAXIMUM:
  537. return maximum(C1, C2);
  538. case TargetOpcode::G_FMINNUM_IEEE:
  539. case TargetOpcode::G_FMAXNUM_IEEE:
  540. // FIXME: These operations were unfortunately named. fminnum/fmaxnum do not
  541. // follow the IEEE behavior for signaling nans and follow libm's fmin/fmax,
  542. // and currently there isn't a nice wrapper in APFloat for the version with
  543. // correct snan handling.
  544. break;
  545. default:
  546. break;
  547. }
  548. return std::nullopt;
  549. }
  550. SmallVector<APInt>
  551. llvm::ConstantFoldVectorBinop(unsigned Opcode, const Register Op1,
  552. const Register Op2,
  553. const MachineRegisterInfo &MRI) {
  554. auto *SrcVec2 = getOpcodeDef<GBuildVector>(Op2, MRI);
  555. if (!SrcVec2)
  556. return SmallVector<APInt>();
  557. auto *SrcVec1 = getOpcodeDef<GBuildVector>(Op1, MRI);
  558. if (!SrcVec1)
  559. return SmallVector<APInt>();
  560. SmallVector<APInt> FoldedElements;
  561. for (unsigned Idx = 0, E = SrcVec1->getNumSources(); Idx < E; ++Idx) {
  562. auto MaybeCst = ConstantFoldBinOp(Opcode, SrcVec1->getSourceReg(Idx),
  563. SrcVec2->getSourceReg(Idx), MRI);
  564. if (!MaybeCst)
  565. return SmallVector<APInt>();
  566. FoldedElements.push_back(*MaybeCst);
  567. }
  568. return FoldedElements;
  569. }
  570. bool llvm::isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
  571. bool SNaN) {
  572. const MachineInstr *DefMI = MRI.getVRegDef(Val);
  573. if (!DefMI)
  574. return false;
  575. const TargetMachine& TM = DefMI->getMF()->getTarget();
  576. if (DefMI->getFlag(MachineInstr::FmNoNans) || TM.Options.NoNaNsFPMath)
  577. return true;
  578. // If the value is a constant, we can obviously see if it is a NaN or not.
  579. if (const ConstantFP *FPVal = getConstantFPVRegVal(Val, MRI)) {
  580. return !FPVal->getValueAPF().isNaN() ||
  581. (SNaN && !FPVal->getValueAPF().isSignaling());
  582. }
  583. if (DefMI->getOpcode() == TargetOpcode::G_BUILD_VECTOR) {
  584. for (const auto &Op : DefMI->uses())
  585. if (!isKnownNeverNaN(Op.getReg(), MRI, SNaN))
  586. return false;
  587. return true;
  588. }
  589. switch (DefMI->getOpcode()) {
  590. default:
  591. break;
  592. case TargetOpcode::G_FADD:
  593. case TargetOpcode::G_FSUB:
  594. case TargetOpcode::G_FMUL:
  595. case TargetOpcode::G_FDIV:
  596. case TargetOpcode::G_FREM:
  597. case TargetOpcode::G_FSIN:
  598. case TargetOpcode::G_FCOS:
  599. case TargetOpcode::G_FMA:
  600. case TargetOpcode::G_FMAD:
  601. if (SNaN)
  602. return true;
  603. // TODO: Need isKnownNeverInfinity
  604. return false;
  605. case TargetOpcode::G_FMINNUM_IEEE:
  606. case TargetOpcode::G_FMAXNUM_IEEE: {
  607. if (SNaN)
  608. return true;
  609. // This can return a NaN if either operand is an sNaN, or if both operands
  610. // are NaN.
  611. return (isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI) &&
  612. isKnownNeverSNaN(DefMI->getOperand(2).getReg(), MRI)) ||
  613. (isKnownNeverSNaN(DefMI->getOperand(1).getReg(), MRI) &&
  614. isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI));
  615. }
  616. case TargetOpcode::G_FMINNUM:
  617. case TargetOpcode::G_FMAXNUM: {
  618. // Only one needs to be known not-nan, since it will be returned if the
  619. // other ends up being one.
  620. return isKnownNeverNaN(DefMI->getOperand(1).getReg(), MRI, SNaN) ||
  621. isKnownNeverNaN(DefMI->getOperand(2).getReg(), MRI, SNaN);
  622. }
  623. }
  624. if (SNaN) {
  625. // FP operations quiet. For now, just handle the ones inserted during
  626. // legalization.
  627. switch (DefMI->getOpcode()) {
  628. case TargetOpcode::G_FPEXT:
  629. case TargetOpcode::G_FPTRUNC:
  630. case TargetOpcode::G_FCANONICALIZE:
  631. return true;
  632. default:
  633. return false;
  634. }
  635. }
  636. return false;
  637. }
  638. Align llvm::inferAlignFromPtrInfo(MachineFunction &MF,
  639. const MachinePointerInfo &MPO) {
  640. auto PSV = MPO.V.dyn_cast<const PseudoSourceValue *>();
  641. if (auto FSPV = dyn_cast_or_null<FixedStackPseudoSourceValue>(PSV)) {
  642. MachineFrameInfo &MFI = MF.getFrameInfo();
  643. return commonAlignment(MFI.getObjectAlign(FSPV->getFrameIndex()),
  644. MPO.Offset);
  645. }
  646. if (const Value *V = MPO.V.dyn_cast<const Value *>()) {
  647. const Module *M = MF.getFunction().getParent();
  648. return V->getPointerAlignment(M->getDataLayout());
  649. }
  650. return Align(1);
  651. }
  652. Register llvm::getFunctionLiveInPhysReg(MachineFunction &MF,
  653. const TargetInstrInfo &TII,
  654. MCRegister PhysReg,
  655. const TargetRegisterClass &RC,
  656. const DebugLoc &DL, LLT RegTy) {
  657. MachineBasicBlock &EntryMBB = MF.front();
  658. MachineRegisterInfo &MRI = MF.getRegInfo();
  659. Register LiveIn = MRI.getLiveInVirtReg(PhysReg);
  660. if (LiveIn) {
  661. MachineInstr *Def = MRI.getVRegDef(LiveIn);
  662. if (Def) {
  663. // FIXME: Should the verifier check this is in the entry block?
  664. assert(Def->getParent() == &EntryMBB && "live-in copy not in entry block");
  665. return LiveIn;
  666. }
  667. // It's possible the incoming argument register and copy was added during
  668. // lowering, but later deleted due to being/becoming dead. If this happens,
  669. // re-insert the copy.
  670. } else {
  671. // The live in register was not present, so add it.
  672. LiveIn = MF.addLiveIn(PhysReg, &RC);
  673. if (RegTy.isValid())
  674. MRI.setType(LiveIn, RegTy);
  675. }
  676. BuildMI(EntryMBB, EntryMBB.begin(), DL, TII.get(TargetOpcode::COPY), LiveIn)
  677. .addReg(PhysReg);
  678. if (!EntryMBB.isLiveIn(PhysReg))
  679. EntryMBB.addLiveIn(PhysReg);
  680. return LiveIn;
  681. }
  682. std::optional<APInt> llvm::ConstantFoldExtOp(unsigned Opcode,
  683. const Register Op1, uint64_t Imm,
  684. const MachineRegisterInfo &MRI) {
  685. auto MaybeOp1Cst = getIConstantVRegVal(Op1, MRI);
  686. if (MaybeOp1Cst) {
  687. switch (Opcode) {
  688. default:
  689. break;
  690. case TargetOpcode::G_SEXT_INREG: {
  691. LLT Ty = MRI.getType(Op1);
  692. return MaybeOp1Cst->trunc(Imm).sext(Ty.getScalarSizeInBits());
  693. }
  694. }
  695. }
  696. return std::nullopt;
  697. }
  698. std::optional<APFloat>
  699. llvm::ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
  700. const MachineRegisterInfo &MRI) {
  701. assert(Opcode == TargetOpcode::G_SITOFP || Opcode == TargetOpcode::G_UITOFP);
  702. if (auto MaybeSrcVal = getIConstantVRegVal(Src, MRI)) {
  703. APFloat DstVal(getFltSemanticForLLT(DstTy));
  704. DstVal.convertFromAPInt(*MaybeSrcVal, Opcode == TargetOpcode::G_SITOFP,
  705. APFloat::rmNearestTiesToEven);
  706. return DstVal;
  707. }
  708. return std::nullopt;
  709. }
  710. std::optional<SmallVector<unsigned>>
  711. llvm::ConstantFoldCTLZ(Register Src, const MachineRegisterInfo &MRI) {
  712. LLT Ty = MRI.getType(Src);
  713. SmallVector<unsigned> FoldedCTLZs;
  714. auto tryFoldScalar = [&](Register R) -> std::optional<unsigned> {
  715. auto MaybeCst = getIConstantVRegVal(R, MRI);
  716. if (!MaybeCst)
  717. return std::nullopt;
  718. return MaybeCst->countLeadingZeros();
  719. };
  720. if (Ty.isVector()) {
  721. // Try to constant fold each element.
  722. auto *BV = getOpcodeDef<GBuildVector>(Src, MRI);
  723. if (!BV)
  724. return std::nullopt;
  725. for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
  726. if (auto MaybeFold = tryFoldScalar(BV->getSourceReg(SrcIdx))) {
  727. FoldedCTLZs.emplace_back(*MaybeFold);
  728. continue;
  729. }
  730. return std::nullopt;
  731. }
  732. return FoldedCTLZs;
  733. }
  734. if (auto MaybeCst = tryFoldScalar(Src)) {
  735. FoldedCTLZs.emplace_back(*MaybeCst);
  736. return FoldedCTLZs;
  737. }
  738. return std::nullopt;
  739. }
  740. bool llvm::isKnownToBeAPowerOfTwo(Register Reg, const MachineRegisterInfo &MRI,
  741. GISelKnownBits *KB) {
  742. std::optional<DefinitionAndSourceRegister> DefSrcReg =
  743. getDefSrcRegIgnoringCopies(Reg, MRI);
  744. if (!DefSrcReg)
  745. return false;
  746. const MachineInstr &MI = *DefSrcReg->MI;
  747. const LLT Ty = MRI.getType(Reg);
  748. switch (MI.getOpcode()) {
  749. case TargetOpcode::G_CONSTANT: {
  750. unsigned BitWidth = Ty.getScalarSizeInBits();
  751. const ConstantInt *CI = MI.getOperand(1).getCImm();
  752. return CI->getValue().zextOrTrunc(BitWidth).isPowerOf2();
  753. }
  754. case TargetOpcode::G_SHL: {
  755. // A left-shift of a constant one will have exactly one bit set because
  756. // shifting the bit off the end is undefined.
  757. // TODO: Constant splat
  758. if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
  759. if (*ConstLHS == 1)
  760. return true;
  761. }
  762. break;
  763. }
  764. case TargetOpcode::G_LSHR: {
  765. if (auto ConstLHS = getIConstantVRegVal(MI.getOperand(1).getReg(), MRI)) {
  766. if (ConstLHS->isSignMask())
  767. return true;
  768. }
  769. break;
  770. }
  771. case TargetOpcode::G_BUILD_VECTOR: {
  772. // TODO: Probably should have a recursion depth guard since you could have
  773. // bitcasted vector elements.
  774. for (const MachineOperand &MO : llvm::drop_begin(MI.operands()))
  775. if (!isKnownToBeAPowerOfTwo(MO.getReg(), MRI, KB))
  776. return false;
  777. return true;
  778. }
  779. case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
  780. // Only handle constants since we would need to know if number of leading
  781. // zeros is greater than the truncation amount.
  782. const unsigned BitWidth = Ty.getScalarSizeInBits();
  783. for (const MachineOperand &MO : llvm::drop_begin(MI.operands())) {
  784. auto Const = getIConstantVRegVal(MO.getReg(), MRI);
  785. if (!Const || !Const->zextOrTrunc(BitWidth).isPowerOf2())
  786. return false;
  787. }
  788. return true;
  789. }
  790. default:
  791. break;
  792. }
  793. if (!KB)
  794. return false;
  795. // More could be done here, though the above checks are enough
  796. // to handle some common cases.
  797. // Fall back to computeKnownBits to catch other known cases.
  798. KnownBits Known = KB->getKnownBits(Reg);
  799. return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
  800. }
  801. void llvm::getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU) {
  802. AU.addPreserved<StackProtector>();
  803. }
  804. LLT llvm::getLCMType(LLT OrigTy, LLT TargetTy) {
  805. const unsigned OrigSize = OrigTy.getSizeInBits();
  806. const unsigned TargetSize = TargetTy.getSizeInBits();
  807. if (OrigSize == TargetSize)
  808. return OrigTy;
  809. if (OrigTy.isVector()) {
  810. const LLT OrigElt = OrigTy.getElementType();
  811. if (TargetTy.isVector()) {
  812. const LLT TargetElt = TargetTy.getElementType();
  813. if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
  814. int GCDElts =
  815. std::gcd(OrigTy.getNumElements(), TargetTy.getNumElements());
  816. // Prefer the original element type.
  817. ElementCount Mul = OrigTy.getElementCount() * TargetTy.getNumElements();
  818. return LLT::vector(Mul.divideCoefficientBy(GCDElts),
  819. OrigTy.getElementType());
  820. }
  821. } else {
  822. if (OrigElt.getSizeInBits() == TargetSize)
  823. return OrigTy;
  824. }
  825. unsigned LCMSize = std::lcm(OrigSize, TargetSize);
  826. return LLT::fixed_vector(LCMSize / OrigElt.getSizeInBits(), OrigElt);
  827. }
  828. if (TargetTy.isVector()) {
  829. unsigned LCMSize = std::lcm(OrigSize, TargetSize);
  830. return LLT::fixed_vector(LCMSize / OrigSize, OrigTy);
  831. }
  832. unsigned LCMSize = std::lcm(OrigSize, TargetSize);
  833. // Preserve pointer types.
  834. if (LCMSize == OrigSize)
  835. return OrigTy;
  836. if (LCMSize == TargetSize)
  837. return TargetTy;
  838. return LLT::scalar(LCMSize);
  839. }
  840. LLT llvm::getCoverTy(LLT OrigTy, LLT TargetTy) {
  841. if (!OrigTy.isVector() || !TargetTy.isVector() || OrigTy == TargetTy ||
  842. (OrigTy.getScalarSizeInBits() != TargetTy.getScalarSizeInBits()))
  843. return getLCMType(OrigTy, TargetTy);
  844. unsigned OrigTyNumElts = OrigTy.getNumElements();
  845. unsigned TargetTyNumElts = TargetTy.getNumElements();
  846. if (OrigTyNumElts % TargetTyNumElts == 0)
  847. return OrigTy;
  848. unsigned NumElts = alignTo(OrigTyNumElts, TargetTyNumElts);
  849. return LLT::scalarOrVector(ElementCount::getFixed(NumElts),
  850. OrigTy.getElementType());
  851. }
  852. LLT llvm::getGCDType(LLT OrigTy, LLT TargetTy) {
  853. const unsigned OrigSize = OrigTy.getSizeInBits();
  854. const unsigned TargetSize = TargetTy.getSizeInBits();
  855. if (OrigSize == TargetSize)
  856. return OrigTy;
  857. if (OrigTy.isVector()) {
  858. LLT OrigElt = OrigTy.getElementType();
  859. if (TargetTy.isVector()) {
  860. LLT TargetElt = TargetTy.getElementType();
  861. if (OrigElt.getSizeInBits() == TargetElt.getSizeInBits()) {
  862. int GCD = std::gcd(OrigTy.getNumElements(), TargetTy.getNumElements());
  863. return LLT::scalarOrVector(ElementCount::getFixed(GCD), OrigElt);
  864. }
  865. } else {
  866. // If the source is a vector of pointers, return a pointer element.
  867. if (OrigElt.getSizeInBits() == TargetSize)
  868. return OrigElt;
  869. }
  870. unsigned GCD = std::gcd(OrigSize, TargetSize);
  871. if (GCD == OrigElt.getSizeInBits())
  872. return OrigElt;
  873. // If we can't produce the original element type, we have to use a smaller
  874. // scalar.
  875. if (GCD < OrigElt.getSizeInBits())
  876. return LLT::scalar(GCD);
  877. return LLT::fixed_vector(GCD / OrigElt.getSizeInBits(), OrigElt);
  878. }
  879. if (TargetTy.isVector()) {
  880. // Try to preserve the original element type.
  881. LLT TargetElt = TargetTy.getElementType();
  882. if (TargetElt.getSizeInBits() == OrigSize)
  883. return OrigTy;
  884. }
  885. unsigned GCD = std::gcd(OrigSize, TargetSize);
  886. return LLT::scalar(GCD);
  887. }
  888. std::optional<int> llvm::getSplatIndex(MachineInstr &MI) {
  889. assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
  890. "Only G_SHUFFLE_VECTOR can have a splat index!");
  891. ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
  892. auto FirstDefinedIdx = find_if(Mask, [](int Elt) { return Elt >= 0; });
  893. // If all elements are undefined, this shuffle can be considered a splat.
  894. // Return 0 for better potential for callers to simplify.
  895. if (FirstDefinedIdx == Mask.end())
  896. return 0;
  897. // Make sure all remaining elements are either undef or the same
  898. // as the first non-undef value.
  899. int SplatValue = *FirstDefinedIdx;
  900. if (any_of(make_range(std::next(FirstDefinedIdx), Mask.end()),
  901. [&SplatValue](int Elt) { return Elt >= 0 && Elt != SplatValue; }))
  902. return std::nullopt;
  903. return SplatValue;
  904. }
  905. static bool isBuildVectorOp(unsigned Opcode) {
  906. return Opcode == TargetOpcode::G_BUILD_VECTOR ||
  907. Opcode == TargetOpcode::G_BUILD_VECTOR_TRUNC;
  908. }
  909. namespace {
  910. std::optional<ValueAndVReg> getAnyConstantSplat(Register VReg,
  911. const MachineRegisterInfo &MRI,
  912. bool AllowUndef) {
  913. MachineInstr *MI = getDefIgnoringCopies(VReg, MRI);
  914. if (!MI)
  915. return std::nullopt;
  916. bool isConcatVectorsOp = MI->getOpcode() == TargetOpcode::G_CONCAT_VECTORS;
  917. if (!isBuildVectorOp(MI->getOpcode()) && !isConcatVectorsOp)
  918. return std::nullopt;
  919. std::optional<ValueAndVReg> SplatValAndReg;
  920. for (MachineOperand &Op : MI->uses()) {
  921. Register Element = Op.getReg();
  922. // If we have a G_CONCAT_VECTOR, we recursively look into the
  923. // vectors that we're concatenating to see if they're splats.
  924. auto ElementValAndReg =
  925. isConcatVectorsOp
  926. ? getAnyConstantSplat(Element, MRI, AllowUndef)
  927. : getAnyConstantVRegValWithLookThrough(Element, MRI, true, true);
  928. // If AllowUndef, treat undef as value that will result in a constant splat.
  929. if (!ElementValAndReg) {
  930. if (AllowUndef && isa<GImplicitDef>(MRI.getVRegDef(Element)))
  931. continue;
  932. return std::nullopt;
  933. }
  934. // Record splat value
  935. if (!SplatValAndReg)
  936. SplatValAndReg = ElementValAndReg;
  937. // Different constant than the one already recorded, not a constant splat.
  938. if (SplatValAndReg->Value != ElementValAndReg->Value)
  939. return std::nullopt;
  940. }
  941. return SplatValAndReg;
  942. }
  943. } // end anonymous namespace
  944. bool llvm::isBuildVectorConstantSplat(const Register Reg,
  945. const MachineRegisterInfo &MRI,
  946. int64_t SplatValue, bool AllowUndef) {
  947. if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef))
  948. return mi_match(SplatValAndReg->VReg, MRI, m_SpecificICst(SplatValue));
  949. return false;
  950. }
  951. bool llvm::isBuildVectorConstantSplat(const MachineInstr &MI,
  952. const MachineRegisterInfo &MRI,
  953. int64_t SplatValue, bool AllowUndef) {
  954. return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
  955. AllowUndef);
  956. }
  957. std::optional<APInt>
  958. llvm::getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI) {
  959. if (auto SplatValAndReg =
  960. getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false)) {
  961. std::optional<ValueAndVReg> ValAndVReg =
  962. getIConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
  963. return ValAndVReg->Value;
  964. }
  965. return std::nullopt;
  966. }
  967. std::optional<APInt>
  968. llvm::getIConstantSplatVal(const MachineInstr &MI,
  969. const MachineRegisterInfo &MRI) {
  970. return getIConstantSplatVal(MI.getOperand(0).getReg(), MRI);
  971. }
  972. std::optional<int64_t>
  973. llvm::getIConstantSplatSExtVal(const Register Reg,
  974. const MachineRegisterInfo &MRI) {
  975. if (auto SplatValAndReg =
  976. getAnyConstantSplat(Reg, MRI, /* AllowUndef */ false))
  977. return getIConstantVRegSExtVal(SplatValAndReg->VReg, MRI);
  978. return std::nullopt;
  979. }
  980. std::optional<int64_t>
  981. llvm::getIConstantSplatSExtVal(const MachineInstr &MI,
  982. const MachineRegisterInfo &MRI) {
  983. return getIConstantSplatSExtVal(MI.getOperand(0).getReg(), MRI);
  984. }
  985. std::optional<FPValueAndVReg>
  986. llvm::getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
  987. bool AllowUndef) {
  988. if (auto SplatValAndReg = getAnyConstantSplat(VReg, MRI, AllowUndef))
  989. return getFConstantVRegValWithLookThrough(SplatValAndReg->VReg, MRI);
  990. return std::nullopt;
  991. }
  992. bool llvm::isBuildVectorAllZeros(const MachineInstr &MI,
  993. const MachineRegisterInfo &MRI,
  994. bool AllowUndef) {
  995. return isBuildVectorConstantSplat(MI, MRI, 0, AllowUndef);
  996. }
  997. bool llvm::isBuildVectorAllOnes(const MachineInstr &MI,
  998. const MachineRegisterInfo &MRI,
  999. bool AllowUndef) {
  1000. return isBuildVectorConstantSplat(MI, MRI, -1, AllowUndef);
  1001. }
  1002. std::optional<RegOrConstant>
  1003. llvm::getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI) {
  1004. unsigned Opc = MI.getOpcode();
  1005. if (!isBuildVectorOp(Opc))
  1006. return std::nullopt;
  1007. if (auto Splat = getIConstantSplatSExtVal(MI, MRI))
  1008. return RegOrConstant(*Splat);
  1009. auto Reg = MI.getOperand(1).getReg();
  1010. if (any_of(make_range(MI.operands_begin() + 2, MI.operands_end()),
  1011. [&Reg](const MachineOperand &Op) { return Op.getReg() != Reg; }))
  1012. return std::nullopt;
  1013. return RegOrConstant(Reg);
  1014. }
  1015. static bool isConstantScalar(const MachineInstr &MI,
  1016. const MachineRegisterInfo &MRI,
  1017. bool AllowFP = true,
  1018. bool AllowOpaqueConstants = true) {
  1019. switch (MI.getOpcode()) {
  1020. case TargetOpcode::G_CONSTANT:
  1021. case TargetOpcode::G_IMPLICIT_DEF:
  1022. return true;
  1023. case TargetOpcode::G_FCONSTANT:
  1024. return AllowFP;
  1025. case TargetOpcode::G_GLOBAL_VALUE:
  1026. case TargetOpcode::G_FRAME_INDEX:
  1027. case TargetOpcode::G_BLOCK_ADDR:
  1028. case TargetOpcode::G_JUMP_TABLE:
  1029. return AllowOpaqueConstants;
  1030. default:
  1031. return false;
  1032. }
  1033. }
  1034. bool llvm::isConstantOrConstantVector(MachineInstr &MI,
  1035. const MachineRegisterInfo &MRI) {
  1036. Register Def = MI.getOperand(0).getReg();
  1037. if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
  1038. return true;
  1039. GBuildVector *BV = dyn_cast<GBuildVector>(&MI);
  1040. if (!BV)
  1041. return false;
  1042. for (unsigned SrcIdx = 0; SrcIdx < BV->getNumSources(); ++SrcIdx) {
  1043. if (getIConstantVRegValWithLookThrough(BV->getSourceReg(SrcIdx), MRI) ||
  1044. getOpcodeDef<GImplicitDef>(BV->getSourceReg(SrcIdx), MRI))
  1045. continue;
  1046. return false;
  1047. }
  1048. return true;
  1049. }
  1050. bool llvm::isConstantOrConstantVector(const MachineInstr &MI,
  1051. const MachineRegisterInfo &MRI,
  1052. bool AllowFP, bool AllowOpaqueConstants) {
  1053. if (isConstantScalar(MI, MRI, AllowFP, AllowOpaqueConstants))
  1054. return true;
  1055. if (!isBuildVectorOp(MI.getOpcode()))
  1056. return false;
  1057. const unsigned NumOps = MI.getNumOperands();
  1058. for (unsigned I = 1; I != NumOps; ++I) {
  1059. const MachineInstr *ElementDef = MRI.getVRegDef(MI.getOperand(I).getReg());
  1060. if (!isConstantScalar(*ElementDef, MRI, AllowFP, AllowOpaqueConstants))
  1061. return false;
  1062. }
  1063. return true;
  1064. }
  1065. std::optional<APInt>
  1066. llvm::isConstantOrConstantSplatVector(MachineInstr &MI,
  1067. const MachineRegisterInfo &MRI) {
  1068. Register Def = MI.getOperand(0).getReg();
  1069. if (auto C = getIConstantVRegValWithLookThrough(Def, MRI))
  1070. return C->Value;
  1071. auto MaybeCst = getIConstantSplatSExtVal(MI, MRI);
  1072. if (!MaybeCst)
  1073. return std::nullopt;
  1074. const unsigned ScalarSize = MRI.getType(Def).getScalarSizeInBits();
  1075. return APInt(ScalarSize, *MaybeCst, true);
  1076. }
  1077. bool llvm::isNullOrNullSplat(const MachineInstr &MI,
  1078. const MachineRegisterInfo &MRI, bool AllowUndefs) {
  1079. switch (MI.getOpcode()) {
  1080. case TargetOpcode::G_IMPLICIT_DEF:
  1081. return AllowUndefs;
  1082. case TargetOpcode::G_CONSTANT:
  1083. return MI.getOperand(1).getCImm()->isNullValue();
  1084. case TargetOpcode::G_FCONSTANT: {
  1085. const ConstantFP *FPImm = MI.getOperand(1).getFPImm();
  1086. return FPImm->isZero() && !FPImm->isNegative();
  1087. }
  1088. default:
  1089. if (!AllowUndefs) // TODO: isBuildVectorAllZeros assumes undef is OK already
  1090. return false;
  1091. return isBuildVectorAllZeros(MI, MRI);
  1092. }
  1093. }
  1094. bool llvm::isAllOnesOrAllOnesSplat(const MachineInstr &MI,
  1095. const MachineRegisterInfo &MRI,
  1096. bool AllowUndefs) {
  1097. switch (MI.getOpcode()) {
  1098. case TargetOpcode::G_IMPLICIT_DEF:
  1099. return AllowUndefs;
  1100. case TargetOpcode::G_CONSTANT:
  1101. return MI.getOperand(1).getCImm()->isAllOnesValue();
  1102. default:
  1103. if (!AllowUndefs) // TODO: isBuildVectorAllOnes assumes undef is OK already
  1104. return false;
  1105. return isBuildVectorAllOnes(MI, MRI);
  1106. }
  1107. }
  1108. bool llvm::matchUnaryPredicate(
  1109. const MachineRegisterInfo &MRI, Register Reg,
  1110. std::function<bool(const Constant *ConstVal)> Match, bool AllowUndefs) {
  1111. const MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
  1112. if (AllowUndefs && Def->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
  1113. return Match(nullptr);
  1114. // TODO: Also handle fconstant
  1115. if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
  1116. return Match(Def->getOperand(1).getCImm());
  1117. if (Def->getOpcode() != TargetOpcode::G_BUILD_VECTOR)
  1118. return false;
  1119. for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
  1120. Register SrcElt = Def->getOperand(I).getReg();
  1121. const MachineInstr *SrcDef = getDefIgnoringCopies(SrcElt, MRI);
  1122. if (AllowUndefs && SrcDef->getOpcode() == TargetOpcode::G_IMPLICIT_DEF) {
  1123. if (!Match(nullptr))
  1124. return false;
  1125. continue;
  1126. }
  1127. if (SrcDef->getOpcode() != TargetOpcode::G_CONSTANT ||
  1128. !Match(SrcDef->getOperand(1).getCImm()))
  1129. return false;
  1130. }
  1131. return true;
  1132. }
  1133. bool llvm::isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
  1134. bool IsFP) {
  1135. switch (TLI.getBooleanContents(IsVector, IsFP)) {
  1136. case TargetLowering::UndefinedBooleanContent:
  1137. return Val & 0x1;
  1138. case TargetLowering::ZeroOrOneBooleanContent:
  1139. return Val == 1;
  1140. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  1141. return Val == -1;
  1142. }
  1143. llvm_unreachable("Invalid boolean contents");
  1144. }
  1145. bool llvm::isConstFalseVal(const TargetLowering &TLI, int64_t Val,
  1146. bool IsVector, bool IsFP) {
  1147. switch (TLI.getBooleanContents(IsVector, IsFP)) {
  1148. case TargetLowering::UndefinedBooleanContent:
  1149. return ~Val & 0x1;
  1150. case TargetLowering::ZeroOrOneBooleanContent:
  1151. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  1152. return Val == 0;
  1153. }
  1154. llvm_unreachable("Invalid boolean contents");
  1155. }
  1156. int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
  1157. bool IsFP) {
  1158. switch (TLI.getBooleanContents(IsVector, IsFP)) {
  1159. case TargetLowering::UndefinedBooleanContent:
  1160. case TargetLowering::ZeroOrOneBooleanContent:
  1161. return 1;
  1162. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  1163. return -1;
  1164. }
  1165. llvm_unreachable("Invalid boolean contents");
  1166. }
  1167. bool llvm::shouldOptForSize(const MachineBasicBlock &MBB,
  1168. ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
  1169. const auto &F = MBB.getParent()->getFunction();
  1170. return F.hasOptSize() || F.hasMinSize() ||
  1171. llvm::shouldOptimizeForSize(MBB.getBasicBlock(), PSI, BFI);
  1172. }
  1173. void llvm::saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI,
  1174. LostDebugLocObserver *LocObserver,
  1175. SmallInstListTy &DeadInstChain) {
  1176. for (MachineOperand &Op : MI.uses()) {
  1177. if (Op.isReg() && Op.getReg().isVirtual())
  1178. DeadInstChain.insert(MRI.getVRegDef(Op.getReg()));
  1179. }
  1180. LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
  1181. DeadInstChain.remove(&MI);
  1182. MI.eraseFromParent();
  1183. if (LocObserver)
  1184. LocObserver->checkpoint(false);
  1185. }
  1186. void llvm::eraseInstrs(ArrayRef<MachineInstr *> DeadInstrs,
  1187. MachineRegisterInfo &MRI,
  1188. LostDebugLocObserver *LocObserver) {
  1189. SmallInstListTy DeadInstChain;
  1190. for (MachineInstr *MI : DeadInstrs)
  1191. saveUsesAndErase(*MI, MRI, LocObserver, DeadInstChain);
  1192. while (!DeadInstChain.empty()) {
  1193. MachineInstr *Inst = DeadInstChain.pop_back_val();
  1194. if (!isTriviallyDead(*Inst, MRI))
  1195. continue;
  1196. saveUsesAndErase(*Inst, MRI, LocObserver, DeadInstChain);
  1197. }
  1198. }
  1199. void llvm::eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI,
  1200. LostDebugLocObserver *LocObserver) {
  1201. return eraseInstrs({&MI}, MRI, LocObserver);
  1202. }
  1203. void llvm::salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI) {
  1204. for (auto &Def : MI.defs()) {
  1205. assert(Def.isReg() && "Must be a reg");
  1206. SmallVector<MachineOperand *, 16> DbgUsers;
  1207. for (auto &MOUse : MRI.use_operands(Def.getReg())) {
  1208. MachineInstr *DbgValue = MOUse.getParent();
  1209. // Ignore partially formed DBG_VALUEs.
  1210. if (DbgValue->isNonListDebugValue() && DbgValue->getNumOperands() == 4) {
  1211. DbgUsers.push_back(&MOUse);
  1212. }
  1213. }
  1214. if (!DbgUsers.empty()) {
  1215. salvageDebugInfoForDbgValue(MRI, MI, DbgUsers);
  1216. }
  1217. }
  1218. }