MachineOperand.cpp 39 KB

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