MachineOperand.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. /// \file Methods common to all machine operands.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineOperand.h"
  14. #include "llvm/Analysis/Loads.h"
  15. #include "llvm/CodeGen/MIRPrinter.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/ModuleSlotTracker.h"
  20. #include "llvm/Target/TargetIntrinsicInfo.h"
  21. #include "llvm/Target/TargetMachine.h"
  22. using namespace llvm;
  23. static cl::opt<int>
  24. PrintRegMaskNumRegs("print-regmask-num-regs",
  25. cl::desc("Number of registers to limit to when "
  26. "printing regmask operands in IR dumps. "
  27. "unlimited = -1"),
  28. cl::init(32), cl::Hidden);
  29. static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
  30. if (const MachineInstr *MI = MO.getParent())
  31. if (const MachineBasicBlock *MBB = MI->getParent())
  32. if (const MachineFunction *MF = MBB->getParent())
  33. return MF;
  34. return nullptr;
  35. }
  36. static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
  37. return const_cast<MachineFunction *>(
  38. getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
  39. }
  40. void MachineOperand::setReg(unsigned Reg) {
  41. if (getReg() == Reg)
  42. return; // No change.
  43. // Otherwise, we have to change the register. If this operand is embedded
  44. // into a machine function, we need to update the old and new register's
  45. // use/def lists.
  46. if (MachineFunction *MF = getMFIfAvailable(*this)) {
  47. MachineRegisterInfo &MRI = MF->getRegInfo();
  48. MRI.removeRegOperandFromUseList(this);
  49. SmallContents.RegNo = Reg;
  50. MRI.addRegOperandToUseList(this);
  51. return;
  52. }
  53. // Otherwise, just change the register, no problem. :)
  54. SmallContents.RegNo = Reg;
  55. }
  56. void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
  57. const TargetRegisterInfo &TRI) {
  58. assert(TargetRegisterInfo::isVirtualRegister(Reg));
  59. if (SubIdx && getSubReg())
  60. SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
  61. setReg(Reg);
  62. if (SubIdx)
  63. setSubReg(SubIdx);
  64. }
  65. void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
  66. assert(TargetRegisterInfo::isPhysicalRegister(Reg));
  67. if (getSubReg()) {
  68. Reg = TRI.getSubReg(Reg, getSubReg());
  69. // Note that getSubReg() may return 0 if the sub-register doesn't exist.
  70. // That won't happen in legal code.
  71. setSubReg(0);
  72. if (isDef())
  73. setIsUndef(false);
  74. }
  75. setReg(Reg);
  76. }
  77. /// Change a def to a use, or a use to a def.
  78. void MachineOperand::setIsDef(bool Val) {
  79. assert(isReg() && "Wrong MachineOperand accessor");
  80. assert((!Val || !isDebug()) && "Marking a debug operation as def");
  81. if (IsDef == Val)
  82. return;
  83. assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
  84. // MRI may keep uses and defs in different list positions.
  85. if (MachineFunction *MF = getMFIfAvailable(*this)) {
  86. MachineRegisterInfo &MRI = MF->getRegInfo();
  87. MRI.removeRegOperandFromUseList(this);
  88. IsDef = Val;
  89. MRI.addRegOperandToUseList(this);
  90. return;
  91. }
  92. IsDef = Val;
  93. }
  94. bool MachineOperand::isRenamable() const {
  95. assert(isReg() && "Wrong MachineOperand accessor");
  96. assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
  97. "isRenamable should only be checked on physical registers");
  98. return IsRenamable;
  99. }
  100. void MachineOperand::setIsRenamable(bool Val) {
  101. assert(isReg() && "Wrong MachineOperand accessor");
  102. assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
  103. "setIsRenamable should only be called on physical registers");
  104. if (const MachineInstr *MI = getParent())
  105. if ((isDef() && MI->hasExtraDefRegAllocReq()) ||
  106. (isUse() && MI->hasExtraSrcRegAllocReq()))
  107. assert(!Val && "isRenamable should be false for "
  108. "hasExtraDefRegAllocReq/hasExtraSrcRegAllocReq opcodes");
  109. IsRenamable = Val;
  110. }
  111. void MachineOperand::setIsRenamableIfNoExtraRegAllocReq() {
  112. if (const MachineInstr *MI = getParent())
  113. if ((isDef() && MI->hasExtraDefRegAllocReq()) ||
  114. (isUse() && MI->hasExtraSrcRegAllocReq()))
  115. return;
  116. setIsRenamable(true);
  117. }
  118. // If this operand is currently a register operand, and if this is in a
  119. // function, deregister the operand from the register's use/def list.
  120. void MachineOperand::removeRegFromUses() {
  121. if (!isReg() || !isOnRegUseList())
  122. return;
  123. if (MachineFunction *MF = getMFIfAvailable(*this))
  124. MF->getRegInfo().removeRegOperandFromUseList(this);
  125. }
  126. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  127. /// the specified value. If an operand is known to be an immediate already,
  128. /// the setImm method should be used.
  129. void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
  130. assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
  131. removeRegFromUses();
  132. OpKind = MO_Immediate;
  133. Contents.ImmVal = ImmVal;
  134. }
  135. void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
  136. assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
  137. removeRegFromUses();
  138. OpKind = MO_FPImmediate;
  139. Contents.CFP = FPImm;
  140. }
  141. void MachineOperand::ChangeToES(const char *SymName,
  142. unsigned char TargetFlags) {
  143. assert((!isReg() || !isTied()) &&
  144. "Cannot change a tied operand into an external symbol");
  145. removeRegFromUses();
  146. OpKind = MO_ExternalSymbol;
  147. Contents.OffsetedInfo.Val.SymbolName = SymName;
  148. setOffset(0); // Offset is always 0.
  149. setTargetFlags(TargetFlags);
  150. }
  151. void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
  152. assert((!isReg() || !isTied()) &&
  153. "Cannot change a tied operand into an MCSymbol");
  154. removeRegFromUses();
  155. OpKind = MO_MCSymbol;
  156. Contents.Sym = Sym;
  157. }
  158. void MachineOperand::ChangeToFrameIndex(int Idx) {
  159. assert((!isReg() || !isTied()) &&
  160. "Cannot change a tied operand into a FrameIndex");
  161. removeRegFromUses();
  162. OpKind = MO_FrameIndex;
  163. setIndex(Idx);
  164. }
  165. void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
  166. unsigned char TargetFlags) {
  167. assert((!isReg() || !isTied()) &&
  168. "Cannot change a tied operand into a FrameIndex");
  169. removeRegFromUses();
  170. OpKind = MO_TargetIndex;
  171. setIndex(Idx);
  172. setOffset(Offset);
  173. setTargetFlags(TargetFlags);
  174. }
  175. /// ChangeToRegister - Replace this operand with a new register operand of
  176. /// the specified value. If an operand is known to be an register already,
  177. /// the setReg method should be used.
  178. void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
  179. bool isKill, bool isDead, bool isUndef,
  180. bool isDebug) {
  181. MachineRegisterInfo *RegInfo = nullptr;
  182. if (MachineFunction *MF = getMFIfAvailable(*this))
  183. RegInfo = &MF->getRegInfo();
  184. // If this operand is already a register operand, remove it from the
  185. // register's use/def lists.
  186. bool WasReg = isReg();
  187. if (RegInfo && WasReg)
  188. RegInfo->removeRegOperandFromUseList(this);
  189. // Change this to a register and set the reg#.
  190. assert(!(isDead && !isDef) && "Dead flag on non-def");
  191. assert(!(isKill && isDef) && "Kill flag on def");
  192. OpKind = MO_Register;
  193. SmallContents.RegNo = Reg;
  194. SubReg_TargetFlags = 0;
  195. IsDef = isDef;
  196. IsImp = isImp;
  197. IsDeadOrKill = isKill | isDead;
  198. IsRenamable = false;
  199. IsUndef = isUndef;
  200. IsInternalRead = false;
  201. IsEarlyClobber = false;
  202. IsDebug = isDebug;
  203. // Ensure isOnRegUseList() returns false.
  204. Contents.Reg.Prev = nullptr;
  205. // Preserve the tie when the operand was already a register.
  206. if (!WasReg)
  207. TiedTo = 0;
  208. // If this operand is embedded in a function, add the operand to the
  209. // register's use/def list.
  210. if (RegInfo)
  211. RegInfo->addRegOperandToUseList(this);
  212. }
  213. /// isIdenticalTo - Return true if this operand is identical to the specified
  214. /// operand. Note that this should stay in sync with the hash_value overload
  215. /// below.
  216. bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
  217. if (getType() != Other.getType() ||
  218. getTargetFlags() != Other.getTargetFlags())
  219. return false;
  220. switch (getType()) {
  221. case MachineOperand::MO_Register:
  222. return getReg() == Other.getReg() && isDef() == Other.isDef() &&
  223. getSubReg() == Other.getSubReg();
  224. case MachineOperand::MO_Immediate:
  225. return getImm() == Other.getImm();
  226. case MachineOperand::MO_CImmediate:
  227. return getCImm() == Other.getCImm();
  228. case MachineOperand::MO_FPImmediate:
  229. return getFPImm() == Other.getFPImm();
  230. case MachineOperand::MO_MachineBasicBlock:
  231. return getMBB() == Other.getMBB();
  232. case MachineOperand::MO_FrameIndex:
  233. return getIndex() == Other.getIndex();
  234. case MachineOperand::MO_ConstantPoolIndex:
  235. case MachineOperand::MO_TargetIndex:
  236. return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
  237. case MachineOperand::MO_JumpTableIndex:
  238. return getIndex() == Other.getIndex();
  239. case MachineOperand::MO_GlobalAddress:
  240. return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
  241. case MachineOperand::MO_ExternalSymbol:
  242. return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
  243. getOffset() == Other.getOffset();
  244. case MachineOperand::MO_BlockAddress:
  245. return getBlockAddress() == Other.getBlockAddress() &&
  246. getOffset() == Other.getOffset();
  247. case MachineOperand::MO_RegisterMask:
  248. case MachineOperand::MO_RegisterLiveOut: {
  249. // Shallow compare of the two RegMasks
  250. const uint32_t *RegMask = getRegMask();
  251. const uint32_t *OtherRegMask = Other.getRegMask();
  252. if (RegMask == OtherRegMask)
  253. return true;
  254. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  255. // Calculate the size of the RegMask
  256. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  257. unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
  258. // Deep compare of the two RegMasks
  259. return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
  260. }
  261. // We don't know the size of the RegMask, so we can't deep compare the two
  262. // reg masks.
  263. return false;
  264. }
  265. case MachineOperand::MO_MCSymbol:
  266. return getMCSymbol() == Other.getMCSymbol();
  267. case MachineOperand::MO_CFIIndex:
  268. return getCFIIndex() == Other.getCFIIndex();
  269. case MachineOperand::MO_Metadata:
  270. return getMetadata() == Other.getMetadata();
  271. case MachineOperand::MO_IntrinsicID:
  272. return getIntrinsicID() == Other.getIntrinsicID();
  273. case MachineOperand::MO_Predicate:
  274. return getPredicate() == Other.getPredicate();
  275. }
  276. llvm_unreachable("Invalid machine operand type");
  277. }
  278. // Note: this must stay exactly in sync with isIdenticalTo above.
  279. hash_code llvm::hash_value(const MachineOperand &MO) {
  280. switch (MO.getType()) {
  281. case MachineOperand::MO_Register:
  282. // Register operands don't have target flags.
  283. return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
  284. case MachineOperand::MO_Immediate:
  285. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
  286. case MachineOperand::MO_CImmediate:
  287. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
  288. case MachineOperand::MO_FPImmediate:
  289. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
  290. case MachineOperand::MO_MachineBasicBlock:
  291. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
  292. case MachineOperand::MO_FrameIndex:
  293. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  294. case MachineOperand::MO_ConstantPoolIndex:
  295. case MachineOperand::MO_TargetIndex:
  296. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
  297. MO.getOffset());
  298. case MachineOperand::MO_JumpTableIndex:
  299. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
  300. case MachineOperand::MO_ExternalSymbol:
  301. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
  302. MO.getSymbolName());
  303. case MachineOperand::MO_GlobalAddress:
  304. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
  305. MO.getOffset());
  306. case MachineOperand::MO_BlockAddress:
  307. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
  308. MO.getOffset());
  309. case MachineOperand::MO_RegisterMask:
  310. case MachineOperand::MO_RegisterLiveOut:
  311. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
  312. case MachineOperand::MO_Metadata:
  313. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
  314. case MachineOperand::MO_MCSymbol:
  315. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
  316. case MachineOperand::MO_CFIIndex:
  317. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
  318. case MachineOperand::MO_IntrinsicID:
  319. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
  320. case MachineOperand::MO_Predicate:
  321. return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
  322. }
  323. llvm_unreachable("Invalid machine operand type");
  324. }
  325. // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
  326. // it.
  327. static void tryToGetTargetInfo(const MachineOperand &MO,
  328. const TargetRegisterInfo *&TRI,
  329. const TargetIntrinsicInfo *&IntrinsicInfo) {
  330. if (const MachineFunction *MF = getMFIfAvailable(MO)) {
  331. TRI = MF->getSubtarget().getRegisterInfo();
  332. IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
  333. }
  334. }
  335. void MachineOperand::printSubregIdx(raw_ostream &OS, uint64_t Index,
  336. const TargetRegisterInfo *TRI) {
  337. OS << "%subreg.";
  338. if (TRI)
  339. OS << TRI->getSubRegIndexName(Index);
  340. else
  341. OS << Index;
  342. }
  343. void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
  344. const TargetIntrinsicInfo *IntrinsicInfo) const {
  345. tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
  346. ModuleSlotTracker DummyMST(nullptr);
  347. print(OS, DummyMST, LLT{}, /*PrintDef=*/false,
  348. /*ShouldPrintRegisterTies=*/true,
  349. /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
  350. }
  351. void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
  352. LLT TypeToPrint, bool PrintDef,
  353. bool ShouldPrintRegisterTies,
  354. unsigned TiedOperandIdx,
  355. const TargetRegisterInfo *TRI,
  356. const TargetIntrinsicInfo *IntrinsicInfo) const {
  357. switch (getType()) {
  358. case MachineOperand::MO_Register: {
  359. unsigned Reg = getReg();
  360. if (isImplicit())
  361. OS << (isDef() ? "implicit-def " : "implicit ");
  362. else if (PrintDef && isDef())
  363. // Print the 'def' flag only when the operand is defined after '='.
  364. OS << "def ";
  365. if (isInternalRead())
  366. OS << "internal ";
  367. if (isDead())
  368. OS << "dead ";
  369. if (isKill())
  370. OS << "killed ";
  371. if (isUndef())
  372. OS << "undef ";
  373. if (isEarlyClobber())
  374. OS << "early-clobber ";
  375. if (isDebug())
  376. OS << "debug-use ";
  377. if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable())
  378. OS << "renamable ";
  379. OS << printReg(Reg, TRI);
  380. // Print the sub register.
  381. if (unsigned SubReg = getSubReg()) {
  382. if (TRI)
  383. OS << '.' << TRI->getSubRegIndexName(SubReg);
  384. else
  385. OS << ".subreg" << SubReg;
  386. }
  387. // Print the register class / bank.
  388. if (TargetRegisterInfo::isVirtualRegister(Reg)) {
  389. if (const MachineFunction *MF = getMFIfAvailable(*this)) {
  390. const MachineRegisterInfo &MRI = MF->getRegInfo();
  391. if (!PrintDef || MRI.def_empty(Reg)) {
  392. OS << ':';
  393. OS << printRegClassOrBank(Reg, MRI, TRI);
  394. }
  395. }
  396. }
  397. // Print ties.
  398. if (ShouldPrintRegisterTies && isTied() && !isDef())
  399. OS << "(tied-def " << TiedOperandIdx << ")";
  400. // Print types.
  401. if (TypeToPrint.isValid())
  402. OS << '(' << TypeToPrint << ')';
  403. break;
  404. }
  405. case MachineOperand::MO_Immediate:
  406. OS << getImm();
  407. break;
  408. case MachineOperand::MO_CImmediate:
  409. getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
  410. break;
  411. case MachineOperand::MO_FPImmediate:
  412. if (getFPImm()->getType()->isFloatTy()) {
  413. OS << getFPImm()->getValueAPF().convertToFloat();
  414. } else if (getFPImm()->getType()->isHalfTy()) {
  415. APFloat APF = getFPImm()->getValueAPF();
  416. bool Unused;
  417. APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
  418. OS << "half " << APF.convertToFloat();
  419. } else if (getFPImm()->getType()->isFP128Ty()) {
  420. APFloat APF = getFPImm()->getValueAPF();
  421. SmallString<16> Str;
  422. getFPImm()->getValueAPF().toString(Str);
  423. OS << "quad " << Str;
  424. } else if (getFPImm()->getType()->isX86_FP80Ty()) {
  425. APFloat APF = getFPImm()->getValueAPF();
  426. OS << "x86_fp80 0xK";
  427. APInt API = APF.bitcastToAPInt();
  428. OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
  429. /*Upper=*/true);
  430. OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
  431. /*Upper=*/true);
  432. } else {
  433. OS << getFPImm()->getValueAPF().convertToDouble();
  434. }
  435. break;
  436. case MachineOperand::MO_MachineBasicBlock:
  437. OS << printMBBReference(*getMBB());
  438. break;
  439. case MachineOperand::MO_FrameIndex:
  440. OS << "<fi#" << getIndex() << '>';
  441. break;
  442. case MachineOperand::MO_ConstantPoolIndex:
  443. OS << "<cp#" << getIndex();
  444. if (getOffset())
  445. OS << "+" << getOffset();
  446. OS << '>';
  447. break;
  448. case MachineOperand::MO_TargetIndex:
  449. OS << "<ti#" << getIndex();
  450. if (getOffset())
  451. OS << "+" << getOffset();
  452. OS << '>';
  453. break;
  454. case MachineOperand::MO_JumpTableIndex:
  455. OS << "<jt#" << getIndex() << '>';
  456. break;
  457. case MachineOperand::MO_GlobalAddress:
  458. OS << "<ga:";
  459. getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
  460. if (getOffset())
  461. OS << "+" << getOffset();
  462. OS << '>';
  463. break;
  464. case MachineOperand::MO_ExternalSymbol:
  465. OS << "<es:" << getSymbolName();
  466. if (getOffset())
  467. OS << "+" << getOffset();
  468. OS << '>';
  469. break;
  470. case MachineOperand::MO_BlockAddress:
  471. OS << '<';
  472. getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
  473. if (getOffset())
  474. OS << "+" << getOffset();
  475. OS << '>';
  476. break;
  477. case MachineOperand::MO_RegisterMask: {
  478. OS << "<regmask";
  479. if (TRI) {
  480. unsigned NumRegsInMask = 0;
  481. unsigned NumRegsEmitted = 0;
  482. for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
  483. unsigned MaskWord = i / 32;
  484. unsigned MaskBit = i % 32;
  485. if (getRegMask()[MaskWord] & (1 << MaskBit)) {
  486. if (PrintRegMaskNumRegs < 0 ||
  487. NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
  488. OS << " " << printReg(i, TRI);
  489. NumRegsEmitted++;
  490. }
  491. NumRegsInMask++;
  492. }
  493. }
  494. if (NumRegsEmitted != NumRegsInMask)
  495. OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
  496. } else {
  497. OS << " ...";
  498. }
  499. OS << ">";
  500. break;
  501. }
  502. case MachineOperand::MO_RegisterLiveOut:
  503. OS << "<regliveout>";
  504. break;
  505. case MachineOperand::MO_Metadata:
  506. OS << '<';
  507. getMetadata()->printAsOperand(OS, MST);
  508. OS << '>';
  509. break;
  510. case MachineOperand::MO_MCSymbol:
  511. OS << "<MCSym=" << *getMCSymbol() << '>';
  512. break;
  513. case MachineOperand::MO_CFIIndex:
  514. OS << "<call frame instruction>";
  515. break;
  516. case MachineOperand::MO_IntrinsicID: {
  517. Intrinsic::ID ID = getIntrinsicID();
  518. if (ID < Intrinsic::num_intrinsics)
  519. OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
  520. else if (IntrinsicInfo)
  521. OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
  522. else
  523. OS << "<intrinsic:" << ID << '>';
  524. break;
  525. }
  526. case MachineOperand::MO_Predicate: {
  527. auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
  528. OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
  529. << CmpInst::getPredicateName(Pred) << '>';
  530. break;
  531. }
  532. }
  533. if (unsigned TF = getTargetFlags())
  534. OS << "[TF=" << TF << ']';
  535. }
  536. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  537. LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
  538. #endif
  539. //===----------------------------------------------------------------------===//
  540. // MachineMemOperand Implementation
  541. //===----------------------------------------------------------------------===//
  542. /// getAddrSpace - Return the LLVM IR address space number that this pointer
  543. /// points into.
  544. unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
  545. /// isDereferenceable - Return true if V is always dereferenceable for
  546. /// Offset + Size byte.
  547. bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
  548. const DataLayout &DL) const {
  549. if (!V.is<const Value *>())
  550. return false;
  551. const Value *BasePtr = V.get<const Value *>();
  552. if (BasePtr == nullptr)
  553. return false;
  554. return isDereferenceableAndAlignedPointer(
  555. BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
  556. }
  557. /// getConstantPool - Return a MachinePointerInfo record that refers to the
  558. /// constant pool.
  559. MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
  560. return MachinePointerInfo(MF.getPSVManager().getConstantPool());
  561. }
  562. /// getFixedStack - Return a MachinePointerInfo record that refers to the
  563. /// the specified FrameIndex.
  564. MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
  565. int FI, int64_t Offset) {
  566. return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
  567. }
  568. MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
  569. return MachinePointerInfo(MF.getPSVManager().getJumpTable());
  570. }
  571. MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
  572. return MachinePointerInfo(MF.getPSVManager().getGOT());
  573. }
  574. MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
  575. int64_t Offset, uint8_t ID) {
  576. return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
  577. }
  578. MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
  579. return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
  580. }
  581. MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
  582. uint64_t s, unsigned int a,
  583. const AAMDNodes &AAInfo,
  584. const MDNode *Ranges, SyncScope::ID SSID,
  585. AtomicOrdering Ordering,
  586. AtomicOrdering FailureOrdering)
  587. : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
  588. AAInfo(AAInfo), Ranges(Ranges) {
  589. assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
  590. isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
  591. "invalid pointer value");
  592. assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
  593. assert((isLoad() || isStore()) && "Not a load/store!");
  594. AtomicInfo.SSID = static_cast<unsigned>(SSID);
  595. assert(getSyncScopeID() == SSID && "Value truncated");
  596. AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
  597. assert(getOrdering() == Ordering && "Value truncated");
  598. AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
  599. assert(getFailureOrdering() == FailureOrdering && "Value truncated");
  600. }
  601. /// Profile - Gather unique data for the object.
  602. ///
  603. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  604. ID.AddInteger(getOffset());
  605. ID.AddInteger(Size);
  606. ID.AddPointer(getOpaqueValue());
  607. ID.AddInteger(getFlags());
  608. ID.AddInteger(getBaseAlignment());
  609. }
  610. void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
  611. // The Value and Offset may differ due to CSE. But the flags and size
  612. // should be the same.
  613. assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
  614. assert(MMO->getSize() == getSize() && "Size mismatch!");
  615. if (MMO->getBaseAlignment() >= getBaseAlignment()) {
  616. // Update the alignment value.
  617. BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
  618. // Also update the base and offset, because the new alignment may
  619. // not be applicable with the old ones.
  620. PtrInfo = MMO->PtrInfo;
  621. }
  622. }
  623. /// getAlignment - Return the minimum known alignment in bytes of the
  624. /// actual memory reference.
  625. uint64_t MachineMemOperand::getAlignment() const {
  626. return MinAlign(getBaseAlignment(), getOffset());
  627. }
  628. void MachineMemOperand::print(raw_ostream &OS) const {
  629. ModuleSlotTracker DummyMST(nullptr);
  630. print(OS, DummyMST);
  631. }
  632. void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
  633. assert((isLoad() || isStore()) && "SV has to be a load, store or both.");
  634. if (isVolatile())
  635. OS << "Volatile ";
  636. if (isLoad())
  637. OS << "LD";
  638. if (isStore())
  639. OS << "ST";
  640. OS << getSize();
  641. // Print the address information.
  642. OS << "[";
  643. if (const Value *V = getValue())
  644. V->printAsOperand(OS, /*PrintType=*/false, MST);
  645. else if (const PseudoSourceValue *PSV = getPseudoValue())
  646. PSV->printCustom(OS);
  647. else
  648. OS << "<unknown>";
  649. unsigned AS = getAddrSpace();
  650. if (AS != 0)
  651. OS << "(addrspace=" << AS << ')';
  652. // If the alignment of the memory reference itself differs from the alignment
  653. // of the base pointer, print the base alignment explicitly, next to the base
  654. // pointer.
  655. if (getBaseAlignment() != getAlignment())
  656. OS << "(align=" << getBaseAlignment() << ")";
  657. if (getOffset() != 0)
  658. OS << "+" << getOffset();
  659. OS << "]";
  660. // Print the alignment of the reference.
  661. if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
  662. OS << "(align=" << getAlignment() << ")";
  663. // Print TBAA info.
  664. if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
  665. OS << "(tbaa=";
  666. if (TBAAInfo->getNumOperands() > 0)
  667. TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
  668. else
  669. OS << "<unknown>";
  670. OS << ")";
  671. }
  672. // Print AA scope info.
  673. if (const MDNode *ScopeInfo = getAAInfo().Scope) {
  674. OS << "(alias.scope=";
  675. if (ScopeInfo->getNumOperands() > 0)
  676. for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
  677. ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
  678. if (i != ie - 1)
  679. OS << ",";
  680. }
  681. else
  682. OS << "<unknown>";
  683. OS << ")";
  684. }
  685. // Print AA noalias scope info.
  686. if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
  687. OS << "(noalias=";
  688. if (NoAliasInfo->getNumOperands() > 0)
  689. for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
  690. NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
  691. if (i != ie - 1)
  692. OS << ",";
  693. }
  694. else
  695. OS << "<unknown>";
  696. OS << ")";
  697. }
  698. if (const MDNode *Ranges = getRanges()) {
  699. unsigned NumRanges = Ranges->getNumOperands();
  700. if (NumRanges != 0) {
  701. OS << "(ranges=";
  702. for (unsigned I = 0; I != NumRanges; ++I) {
  703. Ranges->getOperand(I)->printAsOperand(OS, MST);
  704. if (I != NumRanges - 1)
  705. OS << ',';
  706. }
  707. OS << ')';
  708. }
  709. }
  710. if (isNonTemporal())
  711. OS << "(nontemporal)";
  712. if (isDereferenceable())
  713. OS << "(dereferenceable)";
  714. if (isInvariant())
  715. OS << "(invariant)";
  716. if (getFlags() & MOTargetFlag1)
  717. OS << "(flag1)";
  718. if (getFlags() & MOTargetFlag2)
  719. OS << "(flag2)";
  720. if (getFlags() & MOTargetFlag3)
  721. OS << "(flag3)";
  722. }