Utils.cpp 42 KB

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