MachineOperand.cpp 43 KB

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