MachineOperand.cpp 39 KB

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