MachineOperand.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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(unsigned 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(unsigned Reg, unsigned SubIdx,
  66. const TargetRegisterInfo &TRI) {
  67. assert(TargetRegisterInfo::isVirtualRegister(Reg));
  68. if (SubIdx && getSubReg())
  69. SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
  70. setReg(Reg);
  71. if (SubIdx)
  72. setSubReg(SubIdx);
  73. }
  74. void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
  75. assert(TargetRegisterInfo::isPhysicalRegister(Reg));
  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(TargetRegisterInfo::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(TargetRegisterInfo::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 char 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 char 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 char 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(unsigned 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. }
  291. llvm_unreachable("Invalid machine operand type");
  292. }
  293. // Note: this must stay exactly in sync with isIdenticalTo above.
  294. hash_code llvm::hash_value(const MachineOperand &MO) {
  295. switch (MO.getType()) {
  296. case MachineOperand::MO_Register:
  297. // Register operands don't have target flags.
  298. return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
  299. case MachineOperand::MO_Immediate:
  300. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
  301. case MachineOperand::MO_CImmediate:
  302. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
  303. case MachineOperand::MO_FPImmediate:
  304. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
  305. case MachineOperand::MO_MachineBasicBlock:
  306. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
  307. case MachineOperand::MO_FrameIndex:
  308. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  309. case MachineOperand::MO_ConstantPoolIndex:
  310. case MachineOperand::MO_TargetIndex:
  311. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
  312. MO.getOffset());
  313. case MachineOperand::MO_JumpTableIndex:
  314. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  315. case MachineOperand::MO_ExternalSymbol:
  316. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
  317. StringRef(MO.getSymbolName()));
  318. case MachineOperand::MO_GlobalAddress:
  319. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
  320. MO.getOffset());
  321. case MachineOperand::MO_BlockAddress:
  322. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
  323. MO.getOffset());
  324. case MachineOperand::MO_RegisterMask:
  325. case MachineOperand::MO_RegisterLiveOut:
  326. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
  327. case MachineOperand::MO_Metadata:
  328. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
  329. case MachineOperand::MO_MCSymbol:
  330. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
  331. case MachineOperand::MO_CFIIndex:
  332. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
  333. case MachineOperand::MO_IntrinsicID:
  334. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
  335. case MachineOperand::MO_Predicate:
  336. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
  337. }
  338. llvm_unreachable("Invalid machine operand type");
  339. }
  340. // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
  341. // it.
  342. static void tryToGetTargetInfo(const MachineOperand &MO,
  343. const TargetRegisterInfo *&TRI,
  344. const TargetIntrinsicInfo *&IntrinsicInfo) {
  345. if (const MachineFunction *MF = getMFIfAvailable(MO)) {
  346. TRI = MF->getSubtarget().getRegisterInfo();
  347. IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
  348. }
  349. }
  350. static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
  351. const auto *TII = MF.getSubtarget().getInstrInfo();
  352. assert(TII && "expected instruction info");
  353. auto Indices = TII->getSerializableTargetIndices();
  354. auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
  355. return I.first == Index;
  356. });
  357. if (Found != Indices.end())
  358. return Found->second;
  359. return nullptr;
  360. }
  361. static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
  362. auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
  363. for (const auto &I : Flags) {
  364. if (I.first == TF) {
  365. return I.second;
  366. }
  367. }
  368. return nullptr;
  369. }
  370. static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
  371. const TargetRegisterInfo *TRI) {
  372. if (!TRI) {
  373. OS << "%dwarfreg." << DwarfReg;
  374. return;
  375. }
  376. int Reg = TRI->getLLVMRegNum(DwarfReg, true);
  377. if (Reg == -1) {
  378. OS << "<badreg>";
  379. return;
  380. }
  381. OS << printReg(Reg, TRI);
  382. }
  383. static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
  384. ModuleSlotTracker &MST) {
  385. OS << "%ir-block.";
  386. if (BB.hasName()) {
  387. printLLVMNameWithoutPrefix(OS, BB.getName());
  388. return;
  389. }
  390. Optional<int> Slot;
  391. if (const Function *F = BB.getParent()) {
  392. if (F == MST.getCurrentFunction()) {
  393. Slot = MST.getLocalSlot(&BB);
  394. } else if (const Module *M = F->getParent()) {
  395. ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
  396. CustomMST.incorporateFunction(*F);
  397. Slot = CustomMST.getLocalSlot(&BB);
  398. }
  399. }
  400. if (Slot)
  401. MachineOperand::printIRSlotNumber(OS, *Slot);
  402. else
  403. OS << "<unknown>";
  404. }
  405. static void printIRValueReference(raw_ostream &OS, const Value &V,
  406. ModuleSlotTracker &MST) {
  407. if (isa<GlobalValue>(V)) {
  408. V.printAsOperand(OS, /*PrintType=*/false, MST);
  409. return;
  410. }
  411. if (isa<Constant>(V)) {
  412. // Machine memory operands can load/store to/from constant value pointers.
  413. OS << '`';
  414. V.printAsOperand(OS, /*PrintType=*/true, MST);
  415. OS << '`';
  416. return;
  417. }
  418. OS << "%ir.";
  419. if (V.hasName()) {
  420. printLLVMNameWithoutPrefix(OS, V.getName());
  421. return;
  422. }
  423. int Slot = MST.getCurrentFunction() ? MST.getLocalSlot(&V) : -1;
  424. MachineOperand::printIRSlotNumber(OS, Slot);
  425. }
  426. static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
  427. SyncScope::ID SSID,
  428. SmallVectorImpl<StringRef> &SSNs) {
  429. switch (SSID) {
  430. case SyncScope::System:
  431. break;
  432. default:
  433. if (SSNs.empty())
  434. Context.getSyncScopeNames(SSNs);
  435. OS << "syncscope(\"";
  436. printEscapedString(SSNs[SSID], OS);
  437. OS << "\") ";
  438. break;
  439. }
  440. }
  441. static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
  442. unsigned TMMOFlag) {
  443. auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
  444. for (const auto &I : Flags) {
  445. if (I.first == TMMOFlag) {
  446. return I.second;
  447. }
  448. }
  449. return nullptr;
  450. }
  451. static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
  452. const MachineFrameInfo *MFI) {
  453. StringRef Name;
  454. if (MFI) {
  455. IsFixed = MFI->isFixedObjectIndex(FrameIndex);
  456. if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
  457. if (Alloca->hasName())
  458. Name = Alloca->getName();
  459. if (IsFixed)
  460. FrameIndex -= MFI->getObjectIndexBegin();
  461. }
  462. MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
  463. }
  464. void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
  465. const TargetRegisterInfo *TRI) {
  466. OS << "%subreg.";
  467. if (TRI)
  468. OS << TRI->getSubRegIndexName(Index);
  469. else
  470. OS << Index;
  471. }
  472. void MachineOperand::printTargetFlags(raw_ostream &OS,
  473. const MachineOperand &Op) {
  474. if (!Op.getTargetFlags())
  475. return;
  476. const MachineFunction *MF = getMFIfAvailable(Op);
  477. if (!MF)
  478. return;
  479. const auto *TII = MF->getSubtarget().getInstrInfo();
  480. assert(TII && "expected instruction info");
  481. auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
  482. OS << "target-flags(";
  483. const bool HasDirectFlags = Flags.first;
  484. const bool HasBitmaskFlags = Flags.second;
  485. if (!HasDirectFlags && !HasBitmaskFlags) {
  486. OS << "<unknown>) ";
  487. return;
  488. }
  489. if (HasDirectFlags) {
  490. if (const auto *Name = getTargetFlagName(TII, Flags.first))
  491. OS << Name;
  492. else
  493. OS << "<unknown target flag>";
  494. }
  495. if (!HasBitmaskFlags) {
  496. OS << ") ";
  497. return;
  498. }
  499. bool IsCommaNeeded = HasDirectFlags;
  500. unsigned BitMask = Flags.second;
  501. auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
  502. for (const auto &Mask : BitMasks) {
  503. // Check if the flag's bitmask has the bits of the current mask set.
  504. if ((BitMask & Mask.first) == Mask.first) {
  505. if (IsCommaNeeded)
  506. OS << ", ";
  507. IsCommaNeeded = true;
  508. OS << Mask.second;
  509. // Clear the bits which were serialized from the flag's bitmask.
  510. BitMask &= ~(Mask.first);
  511. }
  512. }
  513. if (BitMask) {
  514. // When the resulting flag's bitmask isn't zero, we know that we didn't
  515. // serialize all of the bit flags.
  516. if (IsCommaNeeded)
  517. OS << ", ";
  518. OS << "<unknown bitmask target flag>";
  519. }
  520. OS << ") ";
  521. }
  522. void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
  523. OS << "<mcsymbol " << Sym << ">";
  524. }
  525. void MachineOperand::printStackObjectReference(raw_ostream &OS,
  526. unsigned FrameIndex,
  527. bool IsFixed, StringRef Name) {
  528. if (IsFixed) {
  529. OS << "%fixed-stack." << FrameIndex;
  530. return;
  531. }
  532. OS << "%stack." << FrameIndex;
  533. if (!Name.empty())
  534. OS << '.' << Name;
  535. }
  536. void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
  537. if (Offset == 0)
  538. return;
  539. if (Offset < 0) {
  540. OS << " - " << -Offset;
  541. return;
  542. }
  543. OS << " + " << Offset;
  544. }
  545. void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
  546. if (Slot == -1)
  547. OS << "<badref>";
  548. else
  549. OS << Slot;
  550. }
  551. static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
  552. const TargetRegisterInfo *TRI) {
  553. switch (CFI.getOperation()) {
  554. case MCCFIInstruction::OpSameValue:
  555. OS << "same_value ";
  556. if (MCSymbol *Label = CFI.getLabel())
  557. MachineOperand::printSymbol(OS, *Label);
  558. printCFIRegister(CFI.getRegister(), OS, TRI);
  559. break;
  560. case MCCFIInstruction::OpRememberState:
  561. OS << "remember_state ";
  562. if (MCSymbol *Label = CFI.getLabel())
  563. MachineOperand::printSymbol(OS, *Label);
  564. break;
  565. case MCCFIInstruction::OpRestoreState:
  566. OS << "restore_state ";
  567. if (MCSymbol *Label = CFI.getLabel())
  568. MachineOperand::printSymbol(OS, *Label);
  569. break;
  570. case MCCFIInstruction::OpOffset:
  571. OS << "offset ";
  572. if (MCSymbol *Label = CFI.getLabel())
  573. MachineOperand::printSymbol(OS, *Label);
  574. printCFIRegister(CFI.getRegister(), OS, TRI);
  575. OS << ", " << CFI.getOffset();
  576. break;
  577. case MCCFIInstruction::OpDefCfaRegister:
  578. OS << "def_cfa_register ";
  579. if (MCSymbol *Label = CFI.getLabel())
  580. MachineOperand::printSymbol(OS, *Label);
  581. printCFIRegister(CFI.getRegister(), OS, TRI);
  582. break;
  583. case MCCFIInstruction::OpDefCfaOffset:
  584. OS << "def_cfa_offset ";
  585. if (MCSymbol *Label = CFI.getLabel())
  586. MachineOperand::printSymbol(OS, *Label);
  587. OS << CFI.getOffset();
  588. break;
  589. case MCCFIInstruction::OpDefCfa:
  590. OS << "def_cfa ";
  591. if (MCSymbol *Label = CFI.getLabel())
  592. MachineOperand::printSymbol(OS, *Label);
  593. printCFIRegister(CFI.getRegister(), OS, TRI);
  594. OS << ", " << CFI.getOffset();
  595. break;
  596. case MCCFIInstruction::OpRelOffset:
  597. OS << "rel_offset ";
  598. if (MCSymbol *Label = CFI.getLabel())
  599. MachineOperand::printSymbol(OS, *Label);
  600. printCFIRegister(CFI.getRegister(), OS, TRI);
  601. OS << ", " << CFI.getOffset();
  602. break;
  603. case MCCFIInstruction::OpAdjustCfaOffset:
  604. OS << "adjust_cfa_offset ";
  605. if (MCSymbol *Label = CFI.getLabel())
  606. MachineOperand::printSymbol(OS, *Label);
  607. OS << CFI.getOffset();
  608. break;
  609. case MCCFIInstruction::OpRestore:
  610. OS << "restore ";
  611. if (MCSymbol *Label = CFI.getLabel())
  612. MachineOperand::printSymbol(OS, *Label);
  613. printCFIRegister(CFI.getRegister(), OS, TRI);
  614. break;
  615. case MCCFIInstruction::OpEscape: {
  616. OS << "escape ";
  617. if (MCSymbol *Label = CFI.getLabel())
  618. MachineOperand::printSymbol(OS, *Label);
  619. if (!CFI.getValues().empty()) {
  620. size_t e = CFI.getValues().size() - 1;
  621. for (size_t i = 0; i < e; ++i)
  622. OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
  623. OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
  624. }
  625. break;
  626. }
  627. case MCCFIInstruction::OpUndefined:
  628. OS << "undefined ";
  629. if (MCSymbol *Label = CFI.getLabel())
  630. MachineOperand::printSymbol(OS, *Label);
  631. printCFIRegister(CFI.getRegister(), OS, TRI);
  632. break;
  633. case MCCFIInstruction::OpRegister:
  634. OS << "register ";
  635. if (MCSymbol *Label = CFI.getLabel())
  636. MachineOperand::printSymbol(OS, *Label);
  637. printCFIRegister(CFI.getRegister(), OS, TRI);
  638. OS << ", ";
  639. printCFIRegister(CFI.getRegister2(), OS, TRI);
  640. break;
  641. case MCCFIInstruction::OpWindowSave:
  642. OS << "window_save ";
  643. if (MCSymbol *Label = CFI.getLabel())
  644. MachineOperand::printSymbol(OS, *Label);
  645. break;
  646. case MCCFIInstruction::OpNegateRAState:
  647. OS << "negate_ra_sign_state ";
  648. if (MCSymbol *Label = CFI.getLabel())
  649. MachineOperand::printSymbol(OS, *Label);
  650. break;
  651. default:
  652. // TODO: Print the other CFI Operations.
  653. OS << "<unserializable cfi directive>";
  654. break;
  655. }
  656. }
  657. void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
  658. const TargetIntrinsicInfo *IntrinsicInfo) const {
  659. print(OS, LLT{}, TRI, IntrinsicInfo);
  660. }
  661. void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint,
  662. const TargetRegisterInfo *TRI,
  663. const TargetIntrinsicInfo *IntrinsicInfo) const {
  664. tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
  665. ModuleSlotTracker DummyMST(nullptr);
  666. print(OS, DummyMST, TypeToPrint, /*PrintDef=*/false, /*IsStandalone=*/true,
  667. /*ShouldPrintRegisterTies=*/true,
  668. /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
  669. }
  670. void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
  671. LLT TypeToPrint, bool PrintDef, bool IsStandalone,
  672. bool ShouldPrintRegisterTies,
  673. unsigned TiedOperandIdx,
  674. const TargetRegisterInfo *TRI,
  675. const TargetIntrinsicInfo *IntrinsicInfo) const {
  676. printTargetFlags(OS, *this);
  677. switch (getType()) {
  678. case MachineOperand::MO_Register: {
  679. unsigned Reg = getReg();
  680. if (isImplicit())
  681. OS << (isDef() ? "implicit-def " : "implicit ");
  682. else if (PrintDef && isDef())
  683. // Print the 'def' flag only when the operand is defined after '='.
  684. OS << "def ";
  685. if (isInternalRead())
  686. OS << "internal ";
  687. if (isDead())
  688. OS << "dead ";
  689. if (isKill())
  690. OS << "killed ";
  691. if (isUndef())
  692. OS << "undef ";
  693. if (isEarlyClobber())
  694. OS << "early-clobber ";
  695. if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable())
  696. OS << "renamable ";
  697. // isDebug() is exactly true for register operands of a DBG_VALUE. So we
  698. // simply infer it when parsing and do not need to print it.
  699. const MachineRegisterInfo *MRI = nullptr;
  700. if (TargetRegisterInfo::isVirtualRegister(Reg)) {
  701. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  702. MRI = &MF->getRegInfo();
  703. }
  704. }
  705. OS << printReg(Reg, TRI, 0, MRI);
  706. // Print the sub register.
  707. if (unsigned SubReg = getSubReg()) {
  708. if (TRI)
  709. OS << '.' << TRI->getSubRegIndexName(SubReg);
  710. else
  711. OS << ".subreg" << SubReg;
  712. }
  713. // Print the register class / bank.
  714. if (TargetRegisterInfo::isVirtualRegister(Reg)) {
  715. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  716. const MachineRegisterInfo &MRI = MF->getRegInfo();
  717. if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
  718. OS << ':';
  719. OS << printRegClassOrBank(Reg, MRI, TRI);
  720. }
  721. }
  722. }
  723. // Print ties.
  724. if (ShouldPrintRegisterTies && isTied() && !isDef())
  725. OS << "(tied-def " << TiedOperandIdx << ")";
  726. // Print types.
  727. if (TypeToPrint.isValid())
  728. OS << '(' << TypeToPrint << ')';
  729. break;
  730. }
  731. case MachineOperand::MO_Immediate:
  732. OS << getImm();
  733. break;
  734. case MachineOperand::MO_CImmediate:
  735. getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
  736. break;
  737. case MachineOperand::MO_FPImmediate:
  738. getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
  739. break;
  740. case MachineOperand::MO_MachineBasicBlock:
  741. OS << printMBBReference(*getMBB());
  742. break;
  743. case MachineOperand::MO_FrameIndex: {
  744. int FrameIndex = getIndex();
  745. bool IsFixed = false;
  746. const MachineFrameInfo *MFI = nullptr;
  747. if (const MachineFunction *MF = getMFIfAvailable(*this))
  748. MFI = &MF->getFrameInfo();
  749. printFrameIndex(OS, FrameIndex, IsFixed, MFI);
  750. break;
  751. }
  752. case MachineOperand::MO_ConstantPoolIndex:
  753. OS << "%const." << getIndex();
  754. printOperandOffset(OS, getOffset());
  755. break;
  756. case MachineOperand::MO_TargetIndex: {
  757. OS << "target-index(";
  758. const char *Name = "<unknown>";
  759. if (const MachineFunction *MF = getMFIfAvailable(*this))
  760. if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
  761. Name = TargetIndexName;
  762. OS << Name << ')';
  763. printOperandOffset(OS, getOffset());
  764. break;
  765. }
  766. case MachineOperand::MO_JumpTableIndex:
  767. OS << printJumpTableEntryReference(getIndex());
  768. break;
  769. case MachineOperand::MO_GlobalAddress:
  770. getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
  771. printOperandOffset(OS, getOffset());
  772. break;
  773. case MachineOperand::MO_ExternalSymbol: {
  774. StringRef Name = getSymbolName();
  775. OS << '&';
  776. if (Name.empty()) {
  777. OS << "\"\"";
  778. } else {
  779. printLLVMNameWithoutPrefix(OS, Name);
  780. }
  781. printOperandOffset(OS, getOffset());
  782. break;
  783. }
  784. case MachineOperand::MO_BlockAddress: {
  785. OS << "blockaddress(";
  786. getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
  787. MST);
  788. OS << ", ";
  789. printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
  790. OS << ')';
  791. MachineOperand::printOperandOffset(OS, getOffset());
  792. break;
  793. }
  794. case MachineOperand::MO_RegisterMask: {
  795. OS << "<regmask";
  796. if (TRI) {
  797. unsigned NumRegsInMask = 0;
  798. unsigned NumRegsEmitted = 0;
  799. for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
  800. unsigned MaskWord = i / 32;
  801. unsigned MaskBit = i % 32;
  802. if (getRegMask()[MaskWord] & (1 << MaskBit)) {
  803. if (PrintRegMaskNumRegs < 0 ||
  804. NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
  805. OS << " " << printReg(i, TRI);
  806. NumRegsEmitted++;
  807. }
  808. NumRegsInMask++;
  809. }
  810. }
  811. if (NumRegsEmitted != NumRegsInMask)
  812. OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
  813. } else {
  814. OS << " ...";
  815. }
  816. OS << ">";
  817. break;
  818. }
  819. case MachineOperand::MO_RegisterLiveOut: {
  820. const uint32_t *RegMask = getRegLiveOut();
  821. OS << "liveout(";
  822. if (!TRI) {
  823. OS << "<unknown>";
  824. } else {
  825. bool IsCommaNeeded = false;
  826. for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
  827. if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
  828. if (IsCommaNeeded)
  829. OS << ", ";
  830. OS << printReg(Reg, TRI);
  831. IsCommaNeeded = true;
  832. }
  833. }
  834. }
  835. OS << ")";
  836. break;
  837. }
  838. case MachineOperand::MO_Metadata:
  839. getMetadata()->printAsOperand(OS, MST);
  840. break;
  841. case MachineOperand::MO_MCSymbol:
  842. printSymbol(OS, *getMCSymbol());
  843. break;
  844. case MachineOperand::MO_CFIIndex: {
  845. if (const MachineFunction *MF = getMFIfAvailable(*this))
  846. printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
  847. else
  848. OS << "<cfi directive>";
  849. break;
  850. }
  851. case MachineOperand::MO_IntrinsicID: {
  852. Intrinsic::ID ID = getIntrinsicID();
  853. if (ID < Intrinsic::num_intrinsics)
  854. OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
  855. else if (IntrinsicInfo)
  856. OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')';
  857. else
  858. OS << "intrinsic(" << ID << ')';
  859. break;
  860. }
  861. case MachineOperand::MO_Predicate: {
  862. auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
  863. OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
  864. << CmpInst::getPredicateName(Pred) << ')';
  865. break;
  866. }
  867. }
  868. }
  869. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  870. LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
  871. #endif
  872. //===----------------------------------------------------------------------===//
  873. // MachineMemOperand Implementation
  874. //===----------------------------------------------------------------------===//
  875. /// getAddrSpace - Return the LLVM IR address space number that this pointer
  876. /// points into.
  877. unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
  878. /// isDereferenceable - Return true if V is always dereferenceable for
  879. /// Offset + Size byte.
  880. bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
  881. const DataLayout &DL) const {
  882. if (!V.is<const Value *>())
  883. return false;
  884. const Value *BasePtr = V.get<const Value *>();
  885. if (BasePtr == nullptr)
  886. return false;
  887. return isDereferenceableAndAlignedPointer(
  888. BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
  889. }
  890. /// getConstantPool - Return a MachinePointerInfo record that refers to the
  891. /// constant pool.
  892. MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
  893. return MachinePointerInfo(MF.getPSVManager().getConstantPool());
  894. }
  895. /// getFixedStack - Return a MachinePointerInfo record that refers to the
  896. /// the specified FrameIndex.
  897. MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
  898. int FI, int64_t Offset) {
  899. return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
  900. }
  901. MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
  902. return MachinePointerInfo(MF.getPSVManager().getJumpTable());
  903. }
  904. MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
  905. return MachinePointerInfo(MF.getPSVManager().getGOT());
  906. }
  907. MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
  908. int64_t Offset, uint8_t ID) {
  909. return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
  910. }
  911. MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
  912. return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
  913. }
  914. MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
  915. uint64_t s, uint64_t a,
  916. const AAMDNodes &AAInfo,
  917. const MDNode *Ranges, SyncScope::ID SSID,
  918. AtomicOrdering Ordering,
  919. AtomicOrdering FailureOrdering)
  920. : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
  921. AAInfo(AAInfo), Ranges(Ranges) {
  922. assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
  923. isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
  924. "invalid pointer value");
  925. assert(getBaseAlignment() == a && a != 0 && "Alignment is not a power of 2!");
  926. assert((isLoad() || isStore()) && "Not a load/store!");
  927. AtomicInfo.SSID = static_cast<unsigned>(SSID);
  928. assert(getSyncScopeID() == SSID && "Value truncated");
  929. AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
  930. assert(getOrdering() == Ordering && "Value truncated");
  931. AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
  932. assert(getFailureOrdering() == FailureOrdering && "Value truncated");
  933. }
  934. /// Profile - Gather unique data for the object.
  935. ///
  936. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  937. ID.AddInteger(getOffset());
  938. ID.AddInteger(Size);
  939. ID.AddPointer(getOpaqueValue());
  940. ID.AddInteger(getFlags());
  941. ID.AddInteger(getBaseAlignment());
  942. }
  943. void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
  944. // The Value and Offset may differ due to CSE. But the flags and size
  945. // should be the same.
  946. assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
  947. assert(MMO->getSize() == getSize() && "Size mismatch!");
  948. if (MMO->getBaseAlignment() >= getBaseAlignment()) {
  949. // Update the alignment value.
  950. BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
  951. // Also update the base and offset, because the new alignment may
  952. // not be applicable with the old ones.
  953. PtrInfo = MMO->PtrInfo;
  954. }
  955. }
  956. /// getAlignment - Return the minimum known alignment in bytes of the
  957. /// actual memory reference.
  958. uint64_t MachineMemOperand::getAlignment() const {
  959. return MinAlign(getBaseAlignment(), getOffset());
  960. }
  961. void MachineMemOperand::print(raw_ostream &OS) const {
  962. ModuleSlotTracker DummyMST(nullptr);
  963. print(OS, DummyMST);
  964. }
  965. void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
  966. SmallVector<StringRef, 0> SSNs;
  967. LLVMContext Ctx;
  968. print(OS, MST, SSNs, Ctx, nullptr, nullptr);
  969. }
  970. void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
  971. SmallVectorImpl<StringRef> &SSNs,
  972. const LLVMContext &Context,
  973. const MachineFrameInfo *MFI,
  974. const TargetInstrInfo *TII) const {
  975. OS << '(';
  976. if (isVolatile())
  977. OS << "volatile ";
  978. if (isNonTemporal())
  979. OS << "non-temporal ";
  980. if (isDereferenceable())
  981. OS << "dereferenceable ";
  982. if (isInvariant())
  983. OS << "invariant ";
  984. if (getFlags() & MachineMemOperand::MOTargetFlag1)
  985. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
  986. << "\" ";
  987. if (getFlags() & MachineMemOperand::MOTargetFlag2)
  988. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
  989. << "\" ";
  990. if (getFlags() & MachineMemOperand::MOTargetFlag3)
  991. OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
  992. << "\" ";
  993. assert((isLoad() || isStore()) &&
  994. "machine memory operand must be a load or store (or both)");
  995. if (isLoad())
  996. OS << "load ";
  997. if (isStore())
  998. OS << "store ";
  999. printSyncScope(OS, Context, getSyncScopeID(), SSNs);
  1000. if (getOrdering() != AtomicOrdering::NotAtomic)
  1001. OS << toIRString(getOrdering()) << ' ';
  1002. if (getFailureOrdering() != AtomicOrdering::NotAtomic)
  1003. OS << toIRString(getFailureOrdering()) << ' ';
  1004. if (getSize() == MemoryLocation::UnknownSize)
  1005. OS << "unknown-size";
  1006. else
  1007. OS << getSize();
  1008. if (const Value *Val = getValue()) {
  1009. OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
  1010. printIRValueReference(OS, *Val, MST);
  1011. } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
  1012. OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
  1013. assert(PVal && "Expected a pseudo source value");
  1014. switch (PVal->kind()) {
  1015. case PseudoSourceValue::Stack:
  1016. OS << "stack";
  1017. break;
  1018. case PseudoSourceValue::GOT:
  1019. OS << "got";
  1020. break;
  1021. case PseudoSourceValue::JumpTable:
  1022. OS << "jump-table";
  1023. break;
  1024. case PseudoSourceValue::ConstantPool:
  1025. OS << "constant-pool";
  1026. break;
  1027. case PseudoSourceValue::FixedStack: {
  1028. int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
  1029. bool IsFixed = true;
  1030. printFrameIndex(OS, FrameIndex, IsFixed, MFI);
  1031. break;
  1032. }
  1033. case PseudoSourceValue::GlobalValueCallEntry:
  1034. OS << "call-entry ";
  1035. cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
  1036. OS, /*PrintType=*/false, MST);
  1037. break;
  1038. case PseudoSourceValue::ExternalSymbolCallEntry:
  1039. OS << "call-entry &";
  1040. printLLVMNameWithoutPrefix(
  1041. OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
  1042. break;
  1043. case PseudoSourceValue::TargetCustom:
  1044. // FIXME: This is not necessarily the correct MIR serialization format for
  1045. // a custom pseudo source value, but at least it allows
  1046. // -print-machineinstrs to work on a target with custom pseudo source
  1047. // values.
  1048. OS << "custom ";
  1049. PVal->printCustom(OS);
  1050. break;
  1051. }
  1052. }
  1053. MachineOperand::printOperandOffset(OS, getOffset());
  1054. if (getBaseAlignment() != getSize())
  1055. OS << ", align " << getBaseAlignment();
  1056. auto AAInfo = getAAInfo();
  1057. if (AAInfo.TBAA) {
  1058. OS << ", !tbaa ";
  1059. AAInfo.TBAA->printAsOperand(OS, MST);
  1060. }
  1061. if (AAInfo.Scope) {
  1062. OS << ", !alias.scope ";
  1063. AAInfo.Scope->printAsOperand(OS, MST);
  1064. }
  1065. if (AAInfo.NoAlias) {
  1066. OS << ", !noalias ";
  1067. AAInfo.NoAlias->printAsOperand(OS, MST);
  1068. }
  1069. if (getRanges()) {
  1070. OS << ", !range ";
  1071. getRanges()->printAsOperand(OS, MST);
  1072. }
  1073. // FIXME: Implement addrspace printing/parsing in MIR.
  1074. // For now, print this even though parsing it is not available in MIR.
  1075. if (unsigned AS = getAddrSpace())
  1076. OS << ", addrspace " << AS;
  1077. OS << ')';
  1078. }