MachineOperand.cpp 41 KB

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