MachineOperand.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
  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. //
  9. /// \file Methods common to all machine operands.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineOperand.h"
  13. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/Analysis/Loads.h"
  15. #include "llvm/Analysis/MemoryLocation.h"
  16. #include "llvm/CodeGen/MIRPrinter.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/CodeGen/TargetRegisterInfo.h"
  22. #include "llvm/Config/llvm-config.h"
  23. #include "llvm/IR/Constants.h"
  24. #include "llvm/IR/IRPrintingPasses.h"
  25. #include "llvm/IR/ModuleSlotTracker.h"
  26. #include "llvm/MC/MCDwarf.h"
  27. #include "llvm/Target/TargetIntrinsicInfo.h"
  28. #include "llvm/Target/TargetMachine.h"
  29. using namespace llvm;
  30. static cl::opt<int>
  31. PrintRegMaskNumRegs("print-regmask-num-regs",
  32. cl::desc("Number of registers to limit to when "
  33. "printing regmask operands in IR dumps. "
  34. "unlimited = -1"),
  35. cl::init(32), cl::Hidden);
  36. static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
  37. if (const MachineInstr *MI = MO.getParent())
  38. if (const MachineBasicBlock *MBB = MI->getParent())
  39. if (const MachineFunction *MF = MBB->getParent())
  40. return MF;
  41. return nullptr;
  42. }
  43. static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
  44. return const_cast<MachineFunction *>(
  45. getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
  46. }
  47. void MachineOperand::setReg(Register Reg) {
  48. if (getReg() == Reg)
  49. return; // No change.
  50. // Clear the IsRenamable bit to keep it conservatively correct.
  51. IsRenamable = false;
  52. // Otherwise, we have to change the register. If this operand is embedded
  53. // into a machine function, we need to update the old and new register's
  54. // use/def lists.
  55. if (MachineFunction *MF = getMFIfAvailable(*this)) {
  56. MachineRegisterInfo &MRI = MF->getRegInfo();
  57. MRI.removeRegOperandFromUseList(this);
  58. SmallContents.RegNo = Reg;
  59. MRI.addRegOperandToUseList(this);
  60. return;
  61. }
  62. // Otherwise, just change the register, no problem. :)
  63. SmallContents.RegNo = Reg;
  64. }
  65. void MachineOperand::substVirtReg(Register Reg, unsigned SubIdx,
  66. const TargetRegisterInfo &TRI) {
  67. assert(Reg.isVirtual());
  68. if (SubIdx && getSubReg())
  69. SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
  70. setReg(Reg);
  71. if (SubIdx)
  72. setSubReg(SubIdx);
  73. }
  74. void MachineOperand::substPhysReg(MCRegister Reg, const TargetRegisterInfo &TRI) {
  75. assert(Reg.isPhysical());
  76. if (getSubReg()) {
  77. Reg = TRI.getSubReg(Reg, getSubReg());
  78. // Note that getSubReg() may return 0 if the sub-register doesn't exist.
  79. // That won't happen in legal code.
  80. setSubReg(0);
  81. if (isDef())
  82. setIsUndef(false);
  83. }
  84. setReg(Reg);
  85. }
  86. /// Change a def to a use, or a use to a def.
  87. void MachineOperand::setIsDef(bool Val) {
  88. assert(isReg() && "Wrong MachineOperand accessor");
  89. assert((!Val || !isDebug()) && "Marking a debug operation as def");
  90. if (IsDef == Val)
  91. return;
  92. assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
  93. // MRI may keep uses and defs in different list positions.
  94. if (MachineFunction *MF = getMFIfAvailable(*this)) {
  95. MachineRegisterInfo &MRI = MF->getRegInfo();
  96. MRI.removeRegOperandFromUseList(this);
  97. IsDef = Val;
  98. MRI.addRegOperandToUseList(this);
  99. return;
  100. }
  101. IsDef = Val;
  102. }
  103. bool MachineOperand::isRenamable() const {
  104. assert(isReg() && "Wrong MachineOperand accessor");
  105. assert(Register::isPhysicalRegister(getReg()) &&
  106. "isRenamable should only be checked on physical registers");
  107. if (!IsRenamable)
  108. return false;
  109. const MachineInstr *MI = getParent();
  110. if (!MI)
  111. return true;
  112. if (isDef())
  113. return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle);
  114. assert(isUse() && "Reg is not def or use");
  115. return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle);
  116. }
  117. void MachineOperand::setIsRenamable(bool Val) {
  118. assert(isReg() && "Wrong MachineOperand accessor");
  119. assert(Register::isPhysicalRegister(getReg()) &&
  120. "setIsRenamable should only be called on physical registers");
  121. IsRenamable = Val;
  122. }
  123. // If this operand is currently a register operand, and if this is in a
  124. // function, deregister the operand from the register's use/def list.
  125. void MachineOperand::removeRegFromUses() {
  126. if (!isReg() || !isOnRegUseList())
  127. return;
  128. if (MachineFunction *MF = getMFIfAvailable(*this))
  129. MF->getRegInfo().removeRegOperandFromUseList(this);
  130. }
  131. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  132. /// the specified value. If an operand is known to be an immediate already,
  133. /// the setImm method should be used.
  134. void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
  135. assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
  136. removeRegFromUses();
  137. OpKind = MO_Immediate;
  138. Contents.ImmVal = ImmVal;
  139. }
  140. void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
  141. assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
  142. removeRegFromUses();
  143. OpKind = MO_FPImmediate;
  144. Contents.CFP = FPImm;
  145. }
  146. void MachineOperand::ChangeToES(const char *SymName,
  147. unsigned TargetFlags) {
  148. assert((!isReg() || !isTied()) &&
  149. "Cannot change a tied operand into an external symbol");
  150. removeRegFromUses();
  151. OpKind = MO_ExternalSymbol;
  152. Contents.OffsetedInfo.Val.SymbolName = SymName;
  153. setOffset(0); // Offset is always 0.
  154. setTargetFlags(TargetFlags);
  155. }
  156. void MachineOperand::ChangeToGA(const GlobalValue *GV, int64_t Offset,
  157. unsigned TargetFlags) {
  158. assert((!isReg() || !isTied()) &&
  159. "Cannot change a tied operand into a global address");
  160. removeRegFromUses();
  161. OpKind = MO_GlobalAddress;
  162. Contents.OffsetedInfo.Val.GV = GV;
  163. setOffset(Offset);
  164. setTargetFlags(TargetFlags);
  165. }
  166. void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
  167. assert((!isReg() || !isTied()) &&
  168. "Cannot change a tied operand into an MCSymbol");
  169. removeRegFromUses();
  170. OpKind = MO_MCSymbol;
  171. Contents.Sym = Sym;
  172. }
  173. void MachineOperand::ChangeToFrameIndex(int Idx) {
  174. assert((!isReg() || !isTied()) &&
  175. "Cannot change a tied operand into a FrameIndex");
  176. removeRegFromUses();
  177. OpKind = MO_FrameIndex;
  178. setIndex(Idx);
  179. }
  180. void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
  181. unsigned TargetFlags) {
  182. assert((!isReg() || !isTied()) &&
  183. "Cannot change a tied operand into a FrameIndex");
  184. removeRegFromUses();
  185. OpKind = MO_TargetIndex;
  186. setIndex(Idx);
  187. setOffset(Offset);
  188. setTargetFlags(TargetFlags);
  189. }
  190. /// ChangeToRegister - Replace this operand with a new register operand of
  191. /// the specified value. If an operand is known to be an register already,
  192. /// the setReg method should be used.
  193. void MachineOperand::ChangeToRegister(Register Reg, bool isDef, bool isImp,
  194. bool isKill, bool isDead, bool isUndef,
  195. bool isDebug) {
  196. MachineRegisterInfo *RegInfo = nullptr;
  197. if (MachineFunction *MF = getMFIfAvailable(*this))
  198. RegInfo = &MF->getRegInfo();
  199. // If this operand is already a register operand, remove it from the
  200. // register's use/def lists.
  201. bool WasReg = isReg();
  202. if (RegInfo && WasReg)
  203. RegInfo->removeRegOperandFromUseList(this);
  204. // Change this to a register and set the reg#.
  205. assert(!(isDead && !isDef) && "Dead flag on non-def");
  206. assert(!(isKill && isDef) && "Kill flag on def");
  207. OpKind = MO_Register;
  208. SmallContents.RegNo = Reg;
  209. SubReg_TargetFlags = 0;
  210. IsDef = isDef;
  211. IsImp = isImp;
  212. IsDeadOrKill = isKill | isDead;
  213. IsRenamable = false;
  214. IsUndef = isUndef;
  215. IsInternalRead = false;
  216. IsEarlyClobber = false;
  217. IsDebug = isDebug;
  218. // Ensure isOnRegUseList() returns false.
  219. Contents.Reg.Prev = nullptr;
  220. // Preserve the tie when the operand was already a register.
  221. if (!WasReg)
  222. TiedTo = 0;
  223. // If this operand is embedded in a function, add the operand to the
  224. // register's use/def list.
  225. if (RegInfo)
  226. RegInfo->addRegOperandToUseList(this);
  227. }
  228. /// isIdenticalTo - Return true if this operand is identical to the specified
  229. /// operand. Note that this should stay in sync with the hash_value overload
  230. /// below.
  231. bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
  232. if (getType() != Other.getType() ||
  233. getTargetFlags() != Other.getTargetFlags())
  234. return false;
  235. switch (getType()) {
  236. case MachineOperand::MO_Register:
  237. return getReg() == Other.getReg() && isDef() == Other.isDef() &&
  238. getSubReg() == Other.getSubReg();
  239. case MachineOperand::MO_Immediate:
  240. return getImm() == Other.getImm();
  241. case MachineOperand::MO_CImmediate:
  242. return getCImm() == Other.getCImm();
  243. case MachineOperand::MO_FPImmediate:
  244. return getFPImm() == Other.getFPImm();
  245. case MachineOperand::MO_MachineBasicBlock:
  246. return getMBB() == Other.getMBB();
  247. case MachineOperand::MO_FrameIndex:
  248. return getIndex() == Other.getIndex();
  249. case MachineOperand::MO_ConstantPoolIndex:
  250. case MachineOperand::MO_TargetIndex:
  251. return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
  252. case MachineOperand::MO_JumpTableIndex:
  253. return getIndex() == Other.getIndex();
  254. case MachineOperand::MO_GlobalAddress:
  255. return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
  256. case MachineOperand::MO_ExternalSymbol:
  257. return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
  258. getOffset() == Other.getOffset();
  259. case MachineOperand::MO_BlockAddress:
  260. return getBlockAddress() == Other.getBlockAddress() &&
  261. getOffset() == Other.getOffset();
  262. case MachineOperand::MO_RegisterMask:
  263. case MachineOperand::MO_RegisterLiveOut: {
  264. // Shallow compare of the two RegMasks
  265. const uint32_t *RegMask = getRegMask();
  266. const uint32_t *OtherRegMask = Other.getRegMask();
  267. if (RegMask == OtherRegMask)
  268. return true;
  269. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  270. // Calculate the size of the RegMask
  271. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  272. unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
  273. // Deep compare of the two RegMasks
  274. return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
  275. }
  276. // We don't know the size of the RegMask, so we can't deep compare the two
  277. // reg masks.
  278. return false;
  279. }
  280. case MachineOperand::MO_MCSymbol:
  281. return getMCSymbol() == Other.getMCSymbol();
  282. case MachineOperand::MO_CFIIndex:
  283. return getCFIIndex() == Other.getCFIIndex();
  284. case MachineOperand::MO_Metadata:
  285. return getMetadata() == Other.getMetadata();
  286. case MachineOperand::MO_IntrinsicID:
  287. return getIntrinsicID() == Other.getIntrinsicID();
  288. case MachineOperand::MO_Predicate:
  289. return getPredicate() == Other.getPredicate();
  290. case MachineOperand::MO_ShuffleMask:
  291. return getShuffleMask() == Other.getShuffleMask();
  292. }
  293. llvm_unreachable("Invalid machine operand type");
  294. }
  295. // Note: this must stay exactly in sync with isIdenticalTo above.
  296. hash_code llvm::hash_value(const MachineOperand &MO) {
  297. switch (MO.getType()) {
  298. case MachineOperand::MO_Register:
  299. // Register operands don't have target flags.
  300. return hash_combine(MO.getType(), (unsigned)MO.getReg(), MO.getSubReg(), MO.isDef());
  301. case MachineOperand::MO_Immediate:
  302. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
  303. case MachineOperand::MO_CImmediate:
  304. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
  305. case MachineOperand::MO_FPImmediate:
  306. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
  307. case MachineOperand::MO_MachineBasicBlock:
  308. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
  309. case MachineOperand::MO_FrameIndex:
  310. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  311. case MachineOperand::MO_ConstantPoolIndex:
  312. case MachineOperand::MO_TargetIndex:
  313. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
  314. MO.getOffset());
  315. case MachineOperand::MO_JumpTableIndex:
  316. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  317. case MachineOperand::MO_ExternalSymbol:
  318. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
  319. StringRef(MO.getSymbolName()));
  320. case MachineOperand::MO_GlobalAddress:
  321. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
  322. MO.getOffset());
  323. case MachineOperand::MO_BlockAddress:
  324. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
  325. MO.getOffset());
  326. case MachineOperand::MO_RegisterMask:
  327. case MachineOperand::MO_RegisterLiveOut:
  328. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
  329. case MachineOperand::MO_Metadata:
  330. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
  331. case MachineOperand::MO_MCSymbol:
  332. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
  333. case MachineOperand::MO_CFIIndex:
  334. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
  335. case MachineOperand::MO_IntrinsicID:
  336. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
  337. case MachineOperand::MO_Predicate:
  338. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
  339. case MachineOperand::MO_ShuffleMask:
  340. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getShuffleMask());
  341. }
  342. llvm_unreachable("Invalid machine operand type");
  343. }
  344. // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
  345. // it.
  346. static void tryToGetTargetInfo(const MachineOperand &MO,
  347. const TargetRegisterInfo *&TRI,
  348. const TargetIntrinsicInfo *&IntrinsicInfo) {
  349. if (const MachineFunction *MF = getMFIfAvailable(MO)) {
  350. TRI = MF->getSubtarget().getRegisterInfo();
  351. IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
  352. }
  353. }
  354. static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
  355. const auto *TII = MF.getSubtarget().getInstrInfo();
  356. assert(TII && "expected instruction info");
  357. auto Indices = TII->getSerializableTargetIndices();
  358. auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
  359. return I.first == Index;
  360. });
  361. if (Found != Indices.end())
  362. return Found->second;
  363. return nullptr;
  364. }
  365. static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
  366. auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
  367. for (const auto &I : Flags) {
  368. if (I.first == TF) {
  369. return I.second;
  370. }
  371. }
  372. return nullptr;
  373. }
  374. static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
  375. const TargetRegisterInfo *TRI) {
  376. if (!TRI) {
  377. OS << "%dwarfreg." << DwarfReg;
  378. return;
  379. }
  380. int Reg = TRI->getLLVMRegNum(DwarfReg, true);
  381. if (Reg == -1) {
  382. OS << "<badreg>";
  383. return;
  384. }
  385. OS << printReg(Reg, TRI);
  386. }
  387. static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
  388. ModuleSlotTracker &MST) {
  389. OS << "%ir-block.";
  390. if (BB.hasName()) {
  391. printLLVMNameWithoutPrefix(OS, BB.getName());
  392. return;
  393. }
  394. Optional<int> Slot;
  395. if (const Function *F = BB.getParent()) {
  396. if (F == MST.getCurrentFunction()) {
  397. Slot = MST.getLocalSlot(&BB);
  398. } else if (const Module *M = F->getParent()) {
  399. ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
  400. CustomMST.incorporateFunction(*F);
  401. Slot = CustomMST.getLocalSlot(&BB);
  402. }
  403. }
  404. if (Slot)
  405. MachineOperand::printIRSlotNumber(OS, *Slot);
  406. else
  407. OS << "<unknown>";
  408. }
  409. static void printIRValueReference(raw_ostream &OS, const Value &V,
  410. ModuleSlotTracker &MST) {
  411. if (isa<GlobalValue>(V)) {
  412. V.printAsOperand(OS, /*PrintType=*/false, MST);
  413. return;
  414. }
  415. if (isa<Constant>(V)) {
  416. // Machine memory operands can load/store to/from constant value pointers.
  417. OS << '`';
  418. V.printAsOperand(OS, /*PrintType=*/true, MST);
  419. OS << '`';
  420. return;
  421. }
  422. OS << "%ir.";
  423. if (V.hasName()) {
  424. printLLVMNameWithoutPrefix(OS, V.getName());
  425. return;
  426. }
  427. int Slot = MST.getCurrentFunction() ? MST.getLocalSlot(&V) : -1;
  428. MachineOperand::printIRSlotNumber(OS, Slot);
  429. }
  430. static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
  431. SyncScope::ID SSID,
  432. SmallVectorImpl<StringRef> &SSNs) {
  433. switch (SSID) {
  434. case SyncScope::System:
  435. break;
  436. default:
  437. if (SSNs.empty())
  438. Context.getSyncScopeNames(SSNs);
  439. OS << "syncscope(\"";
  440. printEscapedString(SSNs[SSID], OS);
  441. OS << "\") ";
  442. break;
  443. }
  444. }
  445. static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
  446. unsigned TMMOFlag) {
  447. auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
  448. for (const auto &I : Flags) {
  449. if (I.first == TMMOFlag) {
  450. return I.second;
  451. }
  452. }
  453. return nullptr;
  454. }
  455. static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
  456. const MachineFrameInfo *MFI) {
  457. StringRef Name;
  458. if (MFI) {
  459. IsFixed = MFI->isFixedObjectIndex(FrameIndex);
  460. if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
  461. if (Alloca->hasName())
  462. Name = Alloca->getName();
  463. if (IsFixed)
  464. FrameIndex -= MFI->getObjectIndexBegin();
  465. }
  466. MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
  467. }
  468. void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
  469. const TargetRegisterInfo *TRI) {
  470. OS << "%subreg.";
  471. if (TRI)
  472. OS << TRI->getSubRegIndexName(Index);
  473. else
  474. OS << Index;
  475. }
  476. void MachineOperand::printTargetFlags(raw_ostream &OS,
  477. const MachineOperand &Op) {
  478. if (!Op.getTargetFlags())
  479. return;
  480. const MachineFunction *MF = getMFIfAvailable(Op);
  481. if (!MF)
  482. return;
  483. const auto *TII = MF->getSubtarget().getInstrInfo();
  484. assert(TII && "expected instruction info");
  485. auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
  486. OS << "target-flags(";
  487. const bool HasDirectFlags = Flags.first;
  488. const bool HasBitmaskFlags = Flags.second;
  489. if (!HasDirectFlags && !HasBitmaskFlags) {
  490. OS << "<unknown>) ";
  491. return;
  492. }
  493. if (HasDirectFlags) {
  494. if (const auto *Name = getTargetFlagName(TII, Flags.first))
  495. OS << Name;
  496. else
  497. OS << "<unknown target flag>";
  498. }
  499. if (!HasBitmaskFlags) {
  500. OS << ") ";
  501. return;
  502. }
  503. bool IsCommaNeeded = HasDirectFlags;
  504. unsigned BitMask = Flags.second;
  505. auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
  506. for (const auto &Mask : BitMasks) {
  507. // Check if the flag's bitmask has the bits of the current mask set.
  508. if ((BitMask & Mask.first) == Mask.first) {
  509. if (IsCommaNeeded)
  510. OS << ", ";
  511. IsCommaNeeded = true;
  512. OS << Mask.second;
  513. // Clear the bits which were serialized from the flag's bitmask.
  514. BitMask &= ~(Mask.first);
  515. }
  516. }
  517. if (BitMask) {
  518. // When the resulting flag's bitmask isn't zero, we know that we didn't
  519. // serialize all of the bit flags.
  520. if (IsCommaNeeded)
  521. OS << ", ";
  522. OS << "<unknown bitmask target flag>";
  523. }
  524. OS << ") ";
  525. }
  526. void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
  527. OS << "<mcsymbol " << Sym << ">";
  528. }
  529. void MachineOperand::printStackObjectReference(raw_ostream &OS,
  530. unsigned FrameIndex,
  531. bool IsFixed, StringRef Name) {
  532. if (IsFixed) {
  533. OS << "%fixed-stack." << FrameIndex;
  534. return;
  535. }
  536. OS << "%stack." << FrameIndex;
  537. if (!Name.empty())
  538. OS << '.' << Name;
  539. }
  540. void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
  541. if (Offset == 0)
  542. return;
  543. if (Offset < 0) {
  544. OS << " - " << -Offset;
  545. return;
  546. }
  547. OS << " + " << Offset;
  548. }
  549. void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
  550. if (Slot == -1)
  551. OS << "<badref>";
  552. else
  553. OS << Slot;
  554. }
  555. static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
  556. const TargetRegisterInfo *TRI) {
  557. switch (CFI.getOperation()) {
  558. case MCCFIInstruction::OpSameValue:
  559. OS << "same_value ";
  560. if (MCSymbol *Label = CFI.getLabel())
  561. MachineOperand::printSymbol(OS, *Label);
  562. printCFIRegister(CFI.getRegister(), OS, TRI);
  563. break;
  564. case MCCFIInstruction::OpRememberState:
  565. OS << "remember_state ";
  566. if (MCSymbol *Label = CFI.getLabel())
  567. MachineOperand::printSymbol(OS, *Label);
  568. break;
  569. case MCCFIInstruction::OpRestoreState:
  570. OS << "restore_state ";
  571. if (MCSymbol *Label = CFI.getLabel())
  572. MachineOperand::printSymbol(OS, *Label);
  573. break;
  574. case MCCFIInstruction::OpOffset:
  575. OS << "offset ";
  576. if (MCSymbol *Label = CFI.getLabel())
  577. MachineOperand::printSymbol(OS, *Label);
  578. printCFIRegister(CFI.getRegister(), OS, TRI);
  579. OS << ", " << CFI.getOffset();
  580. break;
  581. case MCCFIInstruction::OpDefCfaRegister:
  582. OS << "def_cfa_register ";
  583. if (MCSymbol *Label = CFI.getLabel())
  584. MachineOperand::printSymbol(OS, *Label);
  585. printCFIRegister(CFI.getRegister(), OS, TRI);
  586. break;
  587. case MCCFIInstruction::OpDefCfaOffset:
  588. OS << "def_cfa_offset ";
  589. if (MCSymbol *Label = CFI.getLabel())
  590. MachineOperand::printSymbol(OS, *Label);
  591. OS << CFI.getOffset();
  592. break;
  593. case MCCFIInstruction::OpDefCfa:
  594. OS << "def_cfa ";
  595. if (MCSymbol *Label = CFI.getLabel())
  596. MachineOperand::printSymbol(OS, *Label);
  597. printCFIRegister(CFI.getRegister(), OS, TRI);
  598. OS << ", " << CFI.getOffset();
  599. break;
  600. case MCCFIInstruction::OpRelOffset:
  601. OS << "rel_offset ";
  602. if (MCSymbol *Label = CFI.getLabel())
  603. MachineOperand::printSymbol(OS, *Label);
  604. printCFIRegister(CFI.getRegister(), OS, TRI);
  605. OS << ", " << CFI.getOffset();
  606. break;
  607. case MCCFIInstruction::OpAdjustCfaOffset:
  608. OS << "adjust_cfa_offset ";
  609. if (MCSymbol *Label = CFI.getLabel())
  610. MachineOperand::printSymbol(OS, *Label);
  611. OS << CFI.getOffset();
  612. break;
  613. case MCCFIInstruction::OpRestore:
  614. OS << "restore ";
  615. if (MCSymbol *Label = CFI.getLabel())
  616. MachineOperand::printSymbol(OS, *Label);
  617. printCFIRegister(CFI.getRegister(), OS, TRI);
  618. break;
  619. case MCCFIInstruction::OpEscape: {
  620. OS << "escape ";
  621. if (MCSymbol *Label = CFI.getLabel())
  622. MachineOperand::printSymbol(OS, *Label);
  623. if (!CFI.getValues().empty()) {
  624. size_t e = CFI.getValues().size() - 1;
  625. for (size_t i = 0; i < e; ++i)
  626. OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
  627. OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
  628. }
  629. break;
  630. }
  631. case MCCFIInstruction::OpUndefined:
  632. OS << "undefined ";
  633. if (MCSymbol *Label = CFI.getLabel())
  634. MachineOperand::printSymbol(OS, *Label);
  635. printCFIRegister(CFI.getRegister(), OS, TRI);
  636. break;
  637. case MCCFIInstruction::OpRegister:
  638. OS << "register ";
  639. if (MCSymbol *Label = CFI.getLabel())
  640. MachineOperand::printSymbol(OS, *Label);
  641. printCFIRegister(CFI.getRegister(), OS, TRI);
  642. OS << ", ";
  643. printCFIRegister(CFI.getRegister2(), OS, TRI);
  644. break;
  645. case MCCFIInstruction::OpWindowSave:
  646. OS << "window_save ";
  647. if (MCSymbol *Label = CFI.getLabel())
  648. MachineOperand::printSymbol(OS, *Label);
  649. break;
  650. case MCCFIInstruction::OpNegateRAState:
  651. OS << "negate_ra_sign_state ";
  652. if (MCSymbol *Label = CFI.getLabel())
  653. MachineOperand::printSymbol(OS, *Label);
  654. break;
  655. default:
  656. // TODO: Print the other CFI Operations.
  657. OS << "<unserializable cfi directive>";
  658. break;
  659. }
  660. }
  661. void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
  662. const TargetIntrinsicInfo *IntrinsicInfo) const {
  663. print(OS, LLT{}, TRI, IntrinsicInfo);
  664. }
  665. void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint,
  666. const TargetRegisterInfo *TRI,
  667. const TargetIntrinsicInfo *IntrinsicInfo) const {
  668. tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
  669. ModuleSlotTracker DummyMST(nullptr);
  670. print(OS, DummyMST, TypeToPrint, /*PrintDef=*/false, /*IsStandalone=*/true,
  671. /*ShouldPrintRegisterTies=*/true,
  672. /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
  673. }
  674. void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
  675. LLT TypeToPrint, bool PrintDef, bool IsStandalone,
  676. bool ShouldPrintRegisterTies,
  677. unsigned TiedOperandIdx,
  678. const TargetRegisterInfo *TRI,
  679. const TargetIntrinsicInfo *IntrinsicInfo) const {
  680. printTargetFlags(OS, *this);
  681. switch (getType()) {
  682. case MachineOperand::MO_Register: {
  683. Register Reg = getReg();
  684. if (isImplicit())
  685. OS << (isDef() ? "implicit-def " : "implicit ");
  686. else if (PrintDef && isDef())
  687. // Print the 'def' flag only when the operand is defined after '='.
  688. OS << "def ";
  689. if (isInternalRead())
  690. OS << "internal ";
  691. if (isDead())
  692. OS << "dead ";
  693. if (isKill())
  694. OS << "killed ";
  695. if (isUndef())
  696. OS << "undef ";
  697. if (isEarlyClobber())
  698. OS << "early-clobber ";
  699. if (Register::isPhysicalRegister(getReg()) && isRenamable())
  700. OS << "renamable ";
  701. // isDebug() is exactly true for register operands of a DBG_VALUE. So we
  702. // simply infer it when parsing and do not need to print it.
  703. const MachineRegisterInfo *MRI = nullptr;
  704. if (Register::isVirtualRegister(Reg)) {
  705. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  706. MRI = &MF->getRegInfo();
  707. }
  708. }
  709. OS << printReg(Reg, TRI, 0, MRI);
  710. // Print the sub register.
  711. if (unsigned SubReg = getSubReg()) {
  712. if (TRI)
  713. OS << '.' << TRI->getSubRegIndexName(SubReg);
  714. else
  715. OS << ".subreg" << SubReg;
  716. }
  717. // Print the register class / bank.
  718. if (Register::isVirtualRegister(Reg)) {
  719. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  720. const MachineRegisterInfo &MRI = MF->getRegInfo();
  721. if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
  722. OS << ':';
  723. OS << printRegClassOrBank(Reg, MRI, TRI);
  724. }
  725. }
  726. }
  727. // Print ties.
  728. if (ShouldPrintRegisterTies && isTied() && !isDef())
  729. OS << "(tied-def " << TiedOperandIdx << ")";
  730. // Print types.
  731. if (TypeToPrint.isValid())
  732. OS << '(' << TypeToPrint << ')';
  733. break;
  734. }
  735. case MachineOperand::MO_Immediate:
  736. OS << getImm();
  737. break;
  738. case MachineOperand::MO_CImmediate:
  739. getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
  740. break;
  741. case MachineOperand::MO_FPImmediate:
  742. getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
  743. break;
  744. case MachineOperand::MO_MachineBasicBlock:
  745. OS << printMBBReference(*getMBB());
  746. break;
  747. case MachineOperand::MO_FrameIndex: {
  748. int FrameIndex = getIndex();
  749. bool IsFixed = false;
  750. const MachineFrameInfo *MFI = nullptr;
  751. if (const MachineFunction *MF = getMFIfAvailable(*this))
  752. MFI = &MF->getFrameInfo();
  753. printFrameIndex(OS, FrameIndex, IsFixed, MFI);
  754. break;
  755. }
  756. case MachineOperand::MO_ConstantPoolIndex:
  757. OS << "%const." << getIndex();
  758. printOperandOffset(OS, getOffset());
  759. break;
  760. case MachineOperand::MO_TargetIndex: {
  761. OS << "target-index(";
  762. const char *Name = "<unknown>";
  763. if (const MachineFunction *MF = getMFIfAvailable(*this))
  764. if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
  765. Name = TargetIndexName;
  766. OS << Name << ')';
  767. printOperandOffset(OS, getOffset());
  768. break;
  769. }
  770. case MachineOperand::MO_JumpTableIndex:
  771. OS << printJumpTableEntryReference(getIndex());
  772. break;
  773. case MachineOperand::MO_GlobalAddress:
  774. getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
  775. printOperandOffset(OS, getOffset());
  776. break;
  777. case MachineOperand::MO_ExternalSymbol: {
  778. StringRef Name = getSymbolName();
  779. OS << '&';
  780. if (Name.empty()) {
  781. OS << "\"\"";
  782. } else {
  783. printLLVMNameWithoutPrefix(OS, Name);
  784. }
  785. printOperandOffset(OS, getOffset());
  786. break;
  787. }
  788. case MachineOperand::MO_BlockAddress: {
  789. OS << "blockaddress(";
  790. getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
  791. MST);
  792. OS << ", ";
  793. printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
  794. OS << ')';
  795. MachineOperand::printOperandOffset(OS, getOffset());
  796. break;
  797. }
  798. case MachineOperand::MO_RegisterMask: {
  799. OS << "<regmask";
  800. if (TRI) {
  801. unsigned NumRegsInMask = 0;
  802. unsigned NumRegsEmitted = 0;
  803. for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
  804. unsigned MaskWord = i / 32;
  805. unsigned MaskBit = i % 32;
  806. if (getRegMask()[MaskWord] & (1 << MaskBit)) {
  807. if (PrintRegMaskNumRegs < 0 ||
  808. NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
  809. OS << " " << printReg(i, TRI);
  810. NumRegsEmitted++;
  811. }
  812. NumRegsInMask++;
  813. }
  814. }
  815. if (NumRegsEmitted != NumRegsInMask)
  816. OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
  817. } else {
  818. OS << " ...";
  819. }
  820. OS << ">";
  821. break;
  822. }
  823. case MachineOperand::MO_RegisterLiveOut: {
  824. const uint32_t *RegMask = getRegLiveOut();
  825. OS << "liveout(";
  826. if (!TRI) {
  827. OS << "<unknown>";
  828. } else {
  829. bool IsCommaNeeded = false;
  830. for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
  831. if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
  832. if (IsCommaNeeded)
  833. OS << ", ";
  834. OS << printReg(Reg, TRI);
  835. IsCommaNeeded = true;
  836. }
  837. }
  838. }
  839. OS << ")";
  840. break;
  841. }
  842. case MachineOperand::MO_Metadata:
  843. getMetadata()->printAsOperand(OS, MST);
  844. break;
  845. case MachineOperand::MO_MCSymbol:
  846. printSymbol(OS, *getMCSymbol());
  847. break;
  848. case MachineOperand::MO_CFIIndex: {
  849. if (const MachineFunction *MF = getMFIfAvailable(*this))
  850. printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
  851. else
  852. OS << "<cfi directive>";
  853. break;
  854. }
  855. case MachineOperand::MO_IntrinsicID: {
  856. Intrinsic::ID ID = getIntrinsicID();
  857. if (ID < Intrinsic::num_intrinsics)
  858. OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
  859. else if (IntrinsicInfo)
  860. OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')';
  861. else
  862. OS << "intrinsic(" << ID << ')';
  863. break;
  864. }
  865. case MachineOperand::MO_Predicate: {
  866. auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
  867. OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
  868. << CmpInst::getPredicateName(Pred) << ')';
  869. break;
  870. }
  871. case MachineOperand::MO_ShuffleMask:
  872. OS << "shufflemask(";
  873. const Constant* C = getShuffleMask();
  874. const int NumElts = C->getType()->getVectorNumElements();
  875. StringRef Separator;
  876. for (int I = 0; I != NumElts; ++I) {
  877. OS << Separator;
  878. C->getAggregateElement(I)->printAsOperand(OS, false, MST);
  879. Separator = ", ";
  880. }
  881. OS << ')';
  882. break;
  883. }
  884. }
  885. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  886. LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
  887. #endif
  888. //===----------------------------------------------------------------------===//
  889. // MachineMemOperand Implementation
  890. //===----------------------------------------------------------------------===//
  891. /// getAddrSpace - Return the LLVM IR address space number that this pointer
  892. /// points into.
  893. unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
  894. /// isDereferenceable - Return true if V is always dereferenceable for
  895. /// Offset + Size byte.
  896. bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
  897. const DataLayout &DL) const {
  898. if (!V.is<const Value *>())
  899. return false;
  900. const Value *BasePtr = V.get<const Value *>();
  901. if (BasePtr == nullptr)
  902. return false;
  903. return isDereferenceableAndAlignedPointer(
  904. BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
  905. }
  906. /// getConstantPool - Return a MachinePointerInfo record that refers to the
  907. /// constant pool.
  908. MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
  909. return MachinePointerInfo(MF.getPSVManager().getConstantPool());
  910. }
  911. /// getFixedStack - Return a MachinePointerInfo record that refers to the
  912. /// the specified FrameIndex.
  913. MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
  914. int FI, int64_t Offset) {
  915. return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
  916. }
  917. MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
  918. return MachinePointerInfo(MF.getPSVManager().getJumpTable());
  919. }
  920. MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
  921. return MachinePointerInfo(MF.getPSVManager().getGOT());
  922. }
  923. MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
  924. int64_t Offset, uint8_t ID) {
  925. return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
  926. }
  927. MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
  928. return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
  929. }
  930. MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
  931. uint64_t s, uint64_t a,
  932. const AAMDNodes &AAInfo,
  933. const MDNode *Ranges, SyncScope::ID SSID,
  934. AtomicOrdering Ordering,
  935. AtomicOrdering FailureOrdering)
  936. : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
  937. AAInfo(AAInfo), Ranges(Ranges) {
  938. assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
  939. isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
  940. "invalid pointer value");
  941. assert(getBaseAlignment() == a && a != 0 && "Alignment is not a power of 2!");
  942. assert((isLoad() || isStore()) && "Not a load/store!");
  943. AtomicInfo.SSID = static_cast<unsigned>(SSID);
  944. assert(getSyncScopeID() == SSID && "Value truncated");
  945. AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
  946. assert(getOrdering() == Ordering && "Value truncated");
  947. AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
  948. assert(getFailureOrdering() == FailureOrdering && "Value truncated");
  949. }
  950. /// Profile - Gather unique data for the object.
  951. ///
  952. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  953. ID.AddInteger(getOffset());
  954. ID.AddInteger(Size);
  955. ID.AddPointer(getOpaqueValue());
  956. ID.AddInteger(getFlags());
  957. ID.AddInteger(getBaseAlignment());
  958. }
  959. void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
  960. // The Value and Offset may differ due to CSE. But the flags and size
  961. // should be the same.
  962. assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
  963. assert(MMO->getSize() == getSize() && "Size mismatch!");
  964. if (MMO->getBaseAlignment() >= getBaseAlignment()) {
  965. // Update the alignment value.
  966. BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
  967. // Also update the base and offset, because the new alignment may
  968. // not be applicable with the old ones.
  969. PtrInfo = MMO->PtrInfo;
  970. }
  971. }
  972. /// getAlignment - Return the minimum known alignment in bytes of the
  973. /// actual memory reference.
  974. uint64_t MachineMemOperand::getAlignment() const {
  975. return MinAlign(getBaseAlignment(), getOffset());
  976. }
  977. void MachineMemOperand::print(raw_ostream &OS) const {
  978. ModuleSlotTracker DummyMST(nullptr);
  979. print(OS, DummyMST);
  980. }
  981. void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
  982. SmallVector<StringRef, 0> SSNs;
  983. LLVMContext Ctx;
  984. print(OS, MST, SSNs, Ctx, nullptr, nullptr);
  985. }
  986. void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
  987. SmallVectorImpl<StringRef> &SSNs,
  988. const LLVMContext &Context,
  989. const MachineFrameInfo *MFI,
  990. const TargetInstrInfo *TII) const {
  991. OS << '(';
  992. if (isVolatile())
  993. OS << "volatile ";
  994. if (isNonTemporal())
  995. OS << "non-temporal ";
  996. if (isDereferenceable())
  997. OS << "dereferenceable ";
  998. if (isInvariant())
  999. OS << "invariant ";
  1000. if (getFlags() & MachineMemOperand::MOTargetFlag1)
  1001. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
  1002. << "\" ";
  1003. if (getFlags() & MachineMemOperand::MOTargetFlag2)
  1004. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
  1005. << "\" ";
  1006. if (getFlags() & MachineMemOperand::MOTargetFlag3)
  1007. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
  1008. << "\" ";
  1009. assert((isLoad() || isStore()) &&
  1010. "machine memory operand must be a load or store (or both)");
  1011. if (isLoad())
  1012. OS << "load ";
  1013. if (isStore())
  1014. OS << "store ";
  1015. printSyncScope(OS, Context, getSyncScopeID(), SSNs);
  1016. if (getOrdering() != AtomicOrdering::NotAtomic)
  1017. OS << toIRString(getOrdering()) << ' ';
  1018. if (getFailureOrdering() != AtomicOrdering::NotAtomic)
  1019. OS << toIRString(getFailureOrdering()) << ' ';
  1020. if (getSize() == MemoryLocation::UnknownSize)
  1021. OS << "unknown-size";
  1022. else
  1023. OS << getSize();
  1024. if (const Value *Val = getValue()) {
  1025. OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
  1026. printIRValueReference(OS, *Val, MST);
  1027. } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
  1028. OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
  1029. assert(PVal && "Expected a pseudo source value");
  1030. switch (PVal->kind()) {
  1031. case PseudoSourceValue::Stack:
  1032. OS << "stack";
  1033. break;
  1034. case PseudoSourceValue::GOT:
  1035. OS << "got";
  1036. break;
  1037. case PseudoSourceValue::JumpTable:
  1038. OS << "jump-table";
  1039. break;
  1040. case PseudoSourceValue::ConstantPool:
  1041. OS << "constant-pool";
  1042. break;
  1043. case PseudoSourceValue::FixedStack: {
  1044. int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
  1045. bool IsFixed = true;
  1046. printFrameIndex(OS, FrameIndex, IsFixed, MFI);
  1047. break;
  1048. }
  1049. case PseudoSourceValue::GlobalValueCallEntry:
  1050. OS << "call-entry ";
  1051. cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
  1052. OS, /*PrintType=*/false, MST);
  1053. break;
  1054. case PseudoSourceValue::ExternalSymbolCallEntry:
  1055. OS << "call-entry &";
  1056. printLLVMNameWithoutPrefix(
  1057. OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
  1058. break;
  1059. default:
  1060. // FIXME: This is not necessarily the correct MIR serialization format for
  1061. // a custom pseudo source value, but at least it allows
  1062. // -print-machineinstrs to work on a target with custom pseudo source
  1063. // values.
  1064. OS << "custom ";
  1065. PVal->printCustom(OS);
  1066. break;
  1067. }
  1068. }
  1069. MachineOperand::printOperandOffset(OS, getOffset());
  1070. if (getBaseAlignment() != getSize())
  1071. OS << ", align " << getBaseAlignment();
  1072. auto AAInfo = getAAInfo();
  1073. if (AAInfo.TBAA) {
  1074. OS << ", !tbaa ";
  1075. AAInfo.TBAA->printAsOperand(OS, MST);
  1076. }
  1077. if (AAInfo.Scope) {
  1078. OS << ", !alias.scope ";
  1079. AAInfo.Scope->printAsOperand(OS, MST);
  1080. }
  1081. if (AAInfo.NoAlias) {
  1082. OS << ", !noalias ";
  1083. AAInfo.NoAlias->printAsOperand(OS, MST);
  1084. }
  1085. if (getRanges()) {
  1086. OS << ", !range ";
  1087. getRanges()->printAsOperand(OS, MST);
  1088. }
  1089. // FIXME: Implement addrspace printing/parsing in MIR.
  1090. // For now, print this even though parsing it is not available in MIR.
  1091. if (unsigned AS = getAddrSpace())
  1092. OS << ", addrspace " << AS;
  1093. OS << ')';
  1094. }