MachineFunction.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. //===- MachineFunction.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. // Collect native machine code information for a function. This allows
  11. // target-specific information about the generated code to be stored with each
  12. // function.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/ADT/BitVector.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/DenseSet.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/ADT/Twine.h"
  24. #include "llvm/Analysis/ConstantFolding.h"
  25. #include "llvm/Analysis/EHPersonalities.h"
  26. #include "llvm/CodeGen/MachineBasicBlock.h"
  27. #include "llvm/CodeGen/MachineConstantPool.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineInstr.h"
  30. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  31. #include "llvm/CodeGen/MachineMemOperand.h"
  32. #include "llvm/CodeGen/MachineModuleInfo.h"
  33. #include "llvm/CodeGen/MachineRegisterInfo.h"
  34. #include "llvm/CodeGen/PseudoSourceValue.h"
  35. #include "llvm/CodeGen/TargetFrameLowering.h"
  36. #include "llvm/CodeGen/TargetLowering.h"
  37. #include "llvm/CodeGen/TargetRegisterInfo.h"
  38. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  39. #include "llvm/CodeGen/WasmEHFuncInfo.h"
  40. #include "llvm/CodeGen/WinEHFuncInfo.h"
  41. #include "llvm/Config/llvm-config.h"
  42. #include "llvm/IR/Attributes.h"
  43. #include "llvm/IR/BasicBlock.h"
  44. #include "llvm/IR/Constant.h"
  45. #include "llvm/IR/DataLayout.h"
  46. #include "llvm/IR/DerivedTypes.h"
  47. #include "llvm/IR/Function.h"
  48. #include "llvm/IR/GlobalValue.h"
  49. #include "llvm/IR/Instruction.h"
  50. #include "llvm/IR/Instructions.h"
  51. #include "llvm/IR/Metadata.h"
  52. #include "llvm/IR/Module.h"
  53. #include "llvm/IR/ModuleSlotTracker.h"
  54. #include "llvm/IR/Value.h"
  55. #include "llvm/MC/MCContext.h"
  56. #include "llvm/MC/MCSymbol.h"
  57. #include "llvm/MC/SectionKind.h"
  58. #include "llvm/Support/Casting.h"
  59. #include "llvm/Support/CommandLine.h"
  60. #include "llvm/Support/Compiler.h"
  61. #include "llvm/Support/DOTGraphTraits.h"
  62. #include "llvm/Support/Debug.h"
  63. #include "llvm/Support/ErrorHandling.h"
  64. #include "llvm/Support/GraphWriter.h"
  65. #include "llvm/Support/raw_ostream.h"
  66. #include "llvm/Target/TargetMachine.h"
  67. #include <algorithm>
  68. #include <cassert>
  69. #include <cstddef>
  70. #include <cstdint>
  71. #include <iterator>
  72. #include <string>
  73. #include <utility>
  74. #include <vector>
  75. using namespace llvm;
  76. #define DEBUG_TYPE "codegen"
  77. static cl::opt<unsigned>
  78. AlignAllFunctions("align-all-functions",
  79. cl::desc("Force the alignment of all functions."),
  80. cl::init(0), cl::Hidden);
  81. static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
  82. using P = MachineFunctionProperties::Property;
  83. switch(Prop) {
  84. case P::FailedISel: return "FailedISel";
  85. case P::IsSSA: return "IsSSA";
  86. case P::Legalized: return "Legalized";
  87. case P::NoPHIs: return "NoPHIs";
  88. case P::NoVRegs: return "NoVRegs";
  89. case P::RegBankSelected: return "RegBankSelected";
  90. case P::Selected: return "Selected";
  91. case P::TracksLiveness: return "TracksLiveness";
  92. }
  93. llvm_unreachable("Invalid machine function property");
  94. }
  95. void MachineFunctionProperties::print(raw_ostream &OS) const {
  96. const char *Separator = "";
  97. for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
  98. if (!Properties[I])
  99. continue;
  100. OS << Separator << getPropertyName(static_cast<Property>(I));
  101. Separator = ", ";
  102. }
  103. }
  104. //===----------------------------------------------------------------------===//
  105. // MachineFunction implementation
  106. //===----------------------------------------------------------------------===//
  107. // Out-of-line virtual method.
  108. MachineFunctionInfo::~MachineFunctionInfo() = default;
  109. void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
  110. MBB->getParent()->DeleteMachineBasicBlock(MBB);
  111. }
  112. static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
  113. const Function &F) {
  114. if (F.hasFnAttribute(Attribute::StackAlignment))
  115. return F.getFnStackAlignment();
  116. return STI->getFrameLowering()->getStackAlignment();
  117. }
  118. MachineFunction::MachineFunction(const Function &F, const TargetMachine &Target,
  119. const TargetSubtargetInfo &STI,
  120. unsigned FunctionNum, MachineModuleInfo &mmi)
  121. : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
  122. FunctionNumber = FunctionNum;
  123. init();
  124. }
  125. void MachineFunction::init() {
  126. // Assume the function starts in SSA form with correct liveness.
  127. Properties.set(MachineFunctionProperties::Property::IsSSA);
  128. Properties.set(MachineFunctionProperties::Property::TracksLiveness);
  129. if (STI->getRegisterInfo())
  130. RegInfo = new (Allocator) MachineRegisterInfo(this);
  131. else
  132. RegInfo = nullptr;
  133. MFInfo = nullptr;
  134. // We can realign the stack if the target supports it and the user hasn't
  135. // explicitly asked us not to.
  136. bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
  137. !F.hasFnAttribute("no-realign-stack");
  138. FrameInfo = new (Allocator) MachineFrameInfo(
  139. getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
  140. /*ForceRealign=*/CanRealignSP &&
  141. F.hasFnAttribute(Attribute::StackAlignment));
  142. if (F.hasFnAttribute(Attribute::StackAlignment))
  143. FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
  144. ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
  145. Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
  146. // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
  147. // FIXME: Use Function::optForSize().
  148. if (!F.hasFnAttribute(Attribute::OptimizeForSize))
  149. Alignment = std::max(Alignment,
  150. STI->getTargetLowering()->getPrefFunctionAlignment());
  151. if (AlignAllFunctions)
  152. Alignment = AlignAllFunctions;
  153. JumpTableInfo = nullptr;
  154. if (isFuncletEHPersonality(classifyEHPersonality(
  155. F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
  156. WinEHInfo = new (Allocator) WinEHFuncInfo();
  157. }
  158. if (isScopedEHPersonality(classifyEHPersonality(
  159. F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
  160. WasmEHInfo = new (Allocator) WasmEHFuncInfo();
  161. }
  162. assert(Target.isCompatibleDataLayout(getDataLayout()) &&
  163. "Can't create a MachineFunction using a Module with a "
  164. "Target-incompatible DataLayout attached\n");
  165. PSVManager =
  166. llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
  167. getInstrInfo()));
  168. }
  169. MachineFunction::~MachineFunction() {
  170. clear();
  171. }
  172. void MachineFunction::clear() {
  173. Properties.reset();
  174. // Don't call destructors on MachineInstr and MachineOperand. All of their
  175. // memory comes from the BumpPtrAllocator which is about to be purged.
  176. //
  177. // Do call MachineBasicBlock destructors, it contains std::vectors.
  178. for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
  179. I->Insts.clearAndLeakNodesUnsafely();
  180. MBBNumbering.clear();
  181. InstructionRecycler.clear(Allocator);
  182. OperandRecycler.clear(Allocator);
  183. BasicBlockRecycler.clear(Allocator);
  184. CodeViewAnnotations.clear();
  185. VariableDbgInfos.clear();
  186. if (RegInfo) {
  187. RegInfo->~MachineRegisterInfo();
  188. Allocator.Deallocate(RegInfo);
  189. }
  190. if (MFInfo) {
  191. MFInfo->~MachineFunctionInfo();
  192. Allocator.Deallocate(MFInfo);
  193. }
  194. FrameInfo->~MachineFrameInfo();
  195. Allocator.Deallocate(FrameInfo);
  196. ConstantPool->~MachineConstantPool();
  197. Allocator.Deallocate(ConstantPool);
  198. if (JumpTableInfo) {
  199. JumpTableInfo->~MachineJumpTableInfo();
  200. Allocator.Deallocate(JumpTableInfo);
  201. }
  202. if (WinEHInfo) {
  203. WinEHInfo->~WinEHFuncInfo();
  204. Allocator.Deallocate(WinEHInfo);
  205. }
  206. }
  207. const DataLayout &MachineFunction::getDataLayout() const {
  208. return F.getParent()->getDataLayout();
  209. }
  210. /// Get the JumpTableInfo for this function.
  211. /// If it does not already exist, allocate one.
  212. MachineJumpTableInfo *MachineFunction::
  213. getOrCreateJumpTableInfo(unsigned EntryKind) {
  214. if (JumpTableInfo) return JumpTableInfo;
  215. JumpTableInfo = new (Allocator)
  216. MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
  217. return JumpTableInfo;
  218. }
  219. /// Should we be emitting segmented stack stuff for the function
  220. bool MachineFunction::shouldSplitStack() const {
  221. return getFunction().hasFnAttribute("split-stack");
  222. }
  223. /// This discards all of the MachineBasicBlock numbers and recomputes them.
  224. /// This guarantees that the MBB numbers are sequential, dense, and match the
  225. /// ordering of the blocks within the function. If a specific MachineBasicBlock
  226. /// is specified, only that block and those after it are renumbered.
  227. void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
  228. if (empty()) { MBBNumbering.clear(); return; }
  229. MachineFunction::iterator MBBI, E = end();
  230. if (MBB == nullptr)
  231. MBBI = begin();
  232. else
  233. MBBI = MBB->getIterator();
  234. // Figure out the block number this should have.
  235. unsigned BlockNo = 0;
  236. if (MBBI != begin())
  237. BlockNo = std::prev(MBBI)->getNumber() + 1;
  238. for (; MBBI != E; ++MBBI, ++BlockNo) {
  239. if (MBBI->getNumber() != (int)BlockNo) {
  240. // Remove use of the old number.
  241. if (MBBI->getNumber() != -1) {
  242. assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
  243. "MBB number mismatch!");
  244. MBBNumbering[MBBI->getNumber()] = nullptr;
  245. }
  246. // If BlockNo is already taken, set that block's number to -1.
  247. if (MBBNumbering[BlockNo])
  248. MBBNumbering[BlockNo]->setNumber(-1);
  249. MBBNumbering[BlockNo] = &*MBBI;
  250. MBBI->setNumber(BlockNo);
  251. }
  252. }
  253. // Okay, all the blocks are renumbered. If we have compactified the block
  254. // numbering, shrink MBBNumbering now.
  255. assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
  256. MBBNumbering.resize(BlockNo);
  257. }
  258. /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
  259. MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
  260. const DebugLoc &DL,
  261. bool NoImp) {
  262. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  263. MachineInstr(*this, MCID, DL, NoImp);
  264. }
  265. /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
  266. /// identical in all ways except the instruction has no parent, prev, or next.
  267. MachineInstr *
  268. MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
  269. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  270. MachineInstr(*this, *Orig);
  271. }
  272. MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
  273. MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
  274. MachineInstr *FirstClone = nullptr;
  275. MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
  276. while (true) {
  277. MachineInstr *Cloned = CloneMachineInstr(&*I);
  278. MBB.insert(InsertBefore, Cloned);
  279. if (FirstClone == nullptr) {
  280. FirstClone = Cloned;
  281. } else {
  282. Cloned->bundleWithPred();
  283. }
  284. if (!I->isBundledWithSucc())
  285. break;
  286. ++I;
  287. }
  288. return *FirstClone;
  289. }
  290. /// Delete the given MachineInstr.
  291. ///
  292. /// This function also serves as the MachineInstr destructor - the real
  293. /// ~MachineInstr() destructor must be empty.
  294. void
  295. MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
  296. // Strip it for parts. The operand array and the MI object itself are
  297. // independently recyclable.
  298. if (MI->Operands)
  299. deallocateOperandArray(MI->CapOperands, MI->Operands);
  300. // Don't call ~MachineInstr() which must be trivial anyway because
  301. // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
  302. // destructors.
  303. InstructionRecycler.Deallocate(Allocator, MI);
  304. }
  305. /// Allocate a new MachineBasicBlock. Use this instead of
  306. /// `new MachineBasicBlock'.
  307. MachineBasicBlock *
  308. MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
  309. return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
  310. MachineBasicBlock(*this, bb);
  311. }
  312. /// Delete the given MachineBasicBlock.
  313. void
  314. MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
  315. assert(MBB->getParent() == this && "MBB parent mismatch!");
  316. MBB->~MachineBasicBlock();
  317. BasicBlockRecycler.Deallocate(Allocator, MBB);
  318. }
  319. MachineMemOperand *MachineFunction::getMachineMemOperand(
  320. MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
  321. unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
  322. SyncScope::ID SSID, AtomicOrdering Ordering,
  323. AtomicOrdering FailureOrdering) {
  324. return new (Allocator)
  325. MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
  326. SSID, Ordering, FailureOrdering);
  327. }
  328. MachineMemOperand *
  329. MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
  330. int64_t Offset, uint64_t Size) {
  331. if (MMO->getValue())
  332. return new (Allocator)
  333. MachineMemOperand(MachinePointerInfo(MMO->getValue(),
  334. MMO->getOffset()+Offset),
  335. MMO->getFlags(), Size, MMO->getBaseAlignment(),
  336. AAMDNodes(), nullptr, MMO->getSyncScopeID(),
  337. MMO->getOrdering(), MMO->getFailureOrdering());
  338. return new (Allocator)
  339. MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
  340. MMO->getOffset()+Offset),
  341. MMO->getFlags(), Size, MMO->getBaseAlignment(),
  342. AAMDNodes(), nullptr, MMO->getSyncScopeID(),
  343. MMO->getOrdering(), MMO->getFailureOrdering());
  344. }
  345. MachineMemOperand *
  346. MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
  347. const AAMDNodes &AAInfo) {
  348. MachinePointerInfo MPI = MMO->getValue() ?
  349. MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
  350. MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
  351. return new (Allocator)
  352. MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
  353. MMO->getBaseAlignment(), AAInfo,
  354. MMO->getRanges(), MMO->getSyncScopeID(),
  355. MMO->getOrdering(), MMO->getFailureOrdering());
  356. }
  357. MachineInstr::mmo_iterator
  358. MachineFunction::allocateMemRefsArray(unsigned long Num) {
  359. return Allocator.Allocate<MachineMemOperand *>(Num);
  360. }
  361. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  362. MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
  363. MachineInstr::mmo_iterator End) {
  364. // Count the number of load mem refs.
  365. unsigned Num = 0;
  366. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  367. if ((*I)->isLoad())
  368. ++Num;
  369. // Allocate a new array and populate it with the load information.
  370. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  371. unsigned Index = 0;
  372. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  373. if ((*I)->isLoad()) {
  374. if (!(*I)->isStore())
  375. // Reuse the MMO.
  376. Result[Index] = *I;
  377. else {
  378. // Clone the MMO and unset the store flag.
  379. MachineMemOperand *JustLoad =
  380. getMachineMemOperand((*I)->getPointerInfo(),
  381. (*I)->getFlags() & ~MachineMemOperand::MOStore,
  382. (*I)->getSize(), (*I)->getBaseAlignment(),
  383. (*I)->getAAInfo(), nullptr,
  384. (*I)->getSyncScopeID(), (*I)->getOrdering(),
  385. (*I)->getFailureOrdering());
  386. Result[Index] = JustLoad;
  387. }
  388. ++Index;
  389. }
  390. }
  391. return std::make_pair(Result, Result + Num);
  392. }
  393. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  394. MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
  395. MachineInstr::mmo_iterator End) {
  396. // Count the number of load mem refs.
  397. unsigned Num = 0;
  398. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  399. if ((*I)->isStore())
  400. ++Num;
  401. // Allocate a new array and populate it with the store information.
  402. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  403. unsigned Index = 0;
  404. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  405. if ((*I)->isStore()) {
  406. if (!(*I)->isLoad())
  407. // Reuse the MMO.
  408. Result[Index] = *I;
  409. else {
  410. // Clone the MMO and unset the load flag.
  411. MachineMemOperand *JustStore =
  412. getMachineMemOperand((*I)->getPointerInfo(),
  413. (*I)->getFlags() & ~MachineMemOperand::MOLoad,
  414. (*I)->getSize(), (*I)->getBaseAlignment(),
  415. (*I)->getAAInfo(), nullptr,
  416. (*I)->getSyncScopeID(), (*I)->getOrdering(),
  417. (*I)->getFailureOrdering());
  418. Result[Index] = JustStore;
  419. }
  420. ++Index;
  421. }
  422. }
  423. return std::make_pair(Result, Result + Num);
  424. }
  425. const char *MachineFunction::createExternalSymbolName(StringRef Name) {
  426. char *Dest = Allocator.Allocate<char>(Name.size() + 1);
  427. std::copy(Name.begin(), Name.end(), Dest);
  428. Dest[Name.size()] = 0;
  429. return Dest;
  430. }
  431. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  432. LLVM_DUMP_METHOD void MachineFunction::dump() const {
  433. print(dbgs());
  434. }
  435. #endif
  436. StringRef MachineFunction::getName() const {
  437. return getFunction().getName();
  438. }
  439. void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
  440. OS << "# Machine code for function " << getName() << ": ";
  441. getProperties().print(OS);
  442. OS << '\n';
  443. // Print Frame Information
  444. FrameInfo->print(*this, OS);
  445. // Print JumpTable Information
  446. if (JumpTableInfo)
  447. JumpTableInfo->print(OS);
  448. // Print Constant Pool
  449. ConstantPool->print(OS);
  450. const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
  451. if (RegInfo && !RegInfo->livein_empty()) {
  452. OS << "Function Live Ins: ";
  453. for (MachineRegisterInfo::livein_iterator
  454. I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
  455. OS << printReg(I->first, TRI);
  456. if (I->second)
  457. OS << " in " << printReg(I->second, TRI);
  458. if (std::next(I) != E)
  459. OS << ", ";
  460. }
  461. OS << '\n';
  462. }
  463. ModuleSlotTracker MST(getFunction().getParent());
  464. MST.incorporateFunction(getFunction());
  465. for (const auto &BB : *this) {
  466. OS << '\n';
  467. // If we print the whole function, print it at its most verbose level.
  468. BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
  469. }
  470. OS << "\n# End machine code for function " << getName() << ".\n\n";
  471. }
  472. namespace llvm {
  473. template<>
  474. struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
  475. DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
  476. static std::string getGraphName(const MachineFunction *F) {
  477. return ("CFG for '" + F->getName() + "' function").str();
  478. }
  479. std::string getNodeLabel(const MachineBasicBlock *Node,
  480. const MachineFunction *Graph) {
  481. std::string OutStr;
  482. {
  483. raw_string_ostream OSS(OutStr);
  484. if (isSimple()) {
  485. OSS << printMBBReference(*Node);
  486. if (const BasicBlock *BB = Node->getBasicBlock())
  487. OSS << ": " << BB->getName();
  488. } else
  489. Node->print(OSS);
  490. }
  491. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  492. // Process string output to make it nicer...
  493. for (unsigned i = 0; i != OutStr.length(); ++i)
  494. if (OutStr[i] == '\n') { // Left justify
  495. OutStr[i] = '\\';
  496. OutStr.insert(OutStr.begin()+i+1, 'l');
  497. }
  498. return OutStr;
  499. }
  500. };
  501. } // end namespace llvm
  502. void MachineFunction::viewCFG() const
  503. {
  504. #ifndef NDEBUG
  505. ViewGraph(this, "mf" + getName());
  506. #else
  507. errs() << "MachineFunction::viewCFG is only available in debug builds on "
  508. << "systems with Graphviz or gv!\n";
  509. #endif // NDEBUG
  510. }
  511. void MachineFunction::viewCFGOnly() const
  512. {
  513. #ifndef NDEBUG
  514. ViewGraph(this, "mf" + getName(), true);
  515. #else
  516. errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
  517. << "systems with Graphviz or gv!\n";
  518. #endif // NDEBUG
  519. }
  520. /// Add the specified physical register as a live-in value and
  521. /// create a corresponding virtual register for it.
  522. unsigned MachineFunction::addLiveIn(unsigned PReg,
  523. const TargetRegisterClass *RC) {
  524. MachineRegisterInfo &MRI = getRegInfo();
  525. unsigned VReg = MRI.getLiveInVirtReg(PReg);
  526. if (VReg) {
  527. const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
  528. (void)VRegRC;
  529. // A physical register can be added several times.
  530. // Between two calls, the register class of the related virtual register
  531. // may have been constrained to match some operation constraints.
  532. // In that case, check that the current register class includes the
  533. // physical register and is a sub class of the specified RC.
  534. assert((VRegRC == RC || (VRegRC->contains(PReg) &&
  535. RC->hasSubClassEq(VRegRC))) &&
  536. "Register class mismatch!");
  537. return VReg;
  538. }
  539. VReg = MRI.createVirtualRegister(RC);
  540. MRI.addLiveIn(PReg, VReg);
  541. return VReg;
  542. }
  543. /// Return the MCSymbol for the specified non-empty jump table.
  544. /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
  545. /// normal 'L' label is returned.
  546. MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
  547. bool isLinkerPrivate) const {
  548. const DataLayout &DL = getDataLayout();
  549. assert(JumpTableInfo && "No jump tables");
  550. assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
  551. StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
  552. : DL.getPrivateGlobalPrefix();
  553. SmallString<60> Name;
  554. raw_svector_ostream(Name)
  555. << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
  556. return Ctx.getOrCreateSymbol(Name);
  557. }
  558. /// Return a function-local symbol to represent the PIC base.
  559. MCSymbol *MachineFunction::getPICBaseSymbol() const {
  560. const DataLayout &DL = getDataLayout();
  561. return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
  562. Twine(getFunctionNumber()) + "$pb");
  563. }
  564. /// \name Exception Handling
  565. /// \{
  566. LandingPadInfo &
  567. MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
  568. unsigned N = LandingPads.size();
  569. for (unsigned i = 0; i < N; ++i) {
  570. LandingPadInfo &LP = LandingPads[i];
  571. if (LP.LandingPadBlock == LandingPad)
  572. return LP;
  573. }
  574. LandingPads.push_back(LandingPadInfo(LandingPad));
  575. return LandingPads[N];
  576. }
  577. void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
  578. MCSymbol *BeginLabel, MCSymbol *EndLabel) {
  579. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  580. LP.BeginLabels.push_back(BeginLabel);
  581. LP.EndLabels.push_back(EndLabel);
  582. }
  583. MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
  584. MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
  585. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  586. LP.LandingPadLabel = LandingPadLabel;
  587. return LandingPadLabel;
  588. }
  589. void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
  590. ArrayRef<const GlobalValue *> TyInfo) {
  591. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  592. for (unsigned N = TyInfo.size(); N; --N)
  593. LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
  594. }
  595. void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
  596. ArrayRef<const GlobalValue *> TyInfo) {
  597. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  598. std::vector<unsigned> IdsInFilter(TyInfo.size());
  599. for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
  600. IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
  601. LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
  602. }
  603. void MachineFunction::tidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
  604. for (unsigned i = 0; i != LandingPads.size(); ) {
  605. LandingPadInfo &LandingPad = LandingPads[i];
  606. if (LandingPad.LandingPadLabel &&
  607. !LandingPad.LandingPadLabel->isDefined() &&
  608. (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
  609. LandingPad.LandingPadLabel = nullptr;
  610. // Special case: we *should* emit LPs with null LP MBB. This indicates
  611. // "nounwind" case.
  612. if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
  613. LandingPads.erase(LandingPads.begin() + i);
  614. continue;
  615. }
  616. for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
  617. MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
  618. MCSymbol *EndLabel = LandingPad.EndLabels[j];
  619. if ((BeginLabel->isDefined() ||
  620. (LPMap && (*LPMap)[BeginLabel] != 0)) &&
  621. (EndLabel->isDefined() ||
  622. (LPMap && (*LPMap)[EndLabel] != 0))) continue;
  623. LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
  624. LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
  625. --j;
  626. --e;
  627. }
  628. // Remove landing pads with no try-ranges.
  629. if (LandingPads[i].BeginLabels.empty()) {
  630. LandingPads.erase(LandingPads.begin() + i);
  631. continue;
  632. }
  633. // If there is no landing pad, ensure that the list of typeids is empty.
  634. // If the only typeid is a cleanup, this is the same as having no typeids.
  635. if (!LandingPad.LandingPadBlock ||
  636. (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
  637. LandingPad.TypeIds.clear();
  638. ++i;
  639. }
  640. }
  641. void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
  642. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  643. LP.TypeIds.push_back(0);
  644. }
  645. void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
  646. const Function *Filter,
  647. const BlockAddress *RecoverBA) {
  648. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  649. SEHHandler Handler;
  650. Handler.FilterOrFinally = Filter;
  651. Handler.RecoverBA = RecoverBA;
  652. LP.SEHHandlers.push_back(Handler);
  653. }
  654. void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
  655. const Function *Cleanup) {
  656. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  657. SEHHandler Handler;
  658. Handler.FilterOrFinally = Cleanup;
  659. Handler.RecoverBA = nullptr;
  660. LP.SEHHandlers.push_back(Handler);
  661. }
  662. void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
  663. ArrayRef<unsigned> Sites) {
  664. LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
  665. }
  666. unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
  667. for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
  668. if (TypeInfos[i] == TI) return i + 1;
  669. TypeInfos.push_back(TI);
  670. return TypeInfos.size();
  671. }
  672. int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
  673. // If the new filter coincides with the tail of an existing filter, then
  674. // re-use the existing filter. Folding filters more than this requires
  675. // re-ordering filters and/or their elements - probably not worth it.
  676. for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
  677. E = FilterEnds.end(); I != E; ++I) {
  678. unsigned i = *I, j = TyIds.size();
  679. while (i && j)
  680. if (FilterIds[--i] != TyIds[--j])
  681. goto try_next;
  682. if (!j)
  683. // The new filter coincides with range [i, end) of the existing filter.
  684. return -(1 + i);
  685. try_next:;
  686. }
  687. // Add the new filter.
  688. int FilterID = -(1 + FilterIds.size());
  689. FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
  690. FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
  691. FilterEnds.push_back(FilterIds.size());
  692. FilterIds.push_back(0); // terminator
  693. return FilterID;
  694. }
  695. void llvm::addLandingPadInfo(const LandingPadInst &I, MachineBasicBlock &MBB) {
  696. MachineFunction &MF = *MBB.getParent();
  697. if (const auto *PF = dyn_cast<Function>(
  698. I.getParent()->getParent()->getPersonalityFn()->stripPointerCasts()))
  699. MF.getMMI().addPersonality(PF);
  700. if (I.isCleanup())
  701. MF.addCleanup(&MBB);
  702. // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
  703. // but we need to do it this way because of how the DWARF EH emitter
  704. // processes the clauses.
  705. for (unsigned i = I.getNumClauses(); i != 0; --i) {
  706. Value *Val = I.getClause(i - 1);
  707. if (I.isCatch(i - 1)) {
  708. MF.addCatchTypeInfo(&MBB,
  709. dyn_cast<GlobalValue>(Val->stripPointerCasts()));
  710. } else {
  711. // Add filters in a list.
  712. Constant *CVal = cast<Constant>(Val);
  713. SmallVector<const GlobalValue *, 4> FilterList;
  714. for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
  715. II != IE; ++II)
  716. FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
  717. MF.addFilterTypeInfo(&MBB, FilterList);
  718. }
  719. }
  720. }
  721. /// \}
  722. //===----------------------------------------------------------------------===//
  723. // MachineJumpTableInfo implementation
  724. //===----------------------------------------------------------------------===//
  725. /// Return the size of each entry in the jump table.
  726. unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
  727. // The size of a jump table entry is 4 bytes unless the entry is just the
  728. // address of a block, in which case it is the pointer size.
  729. switch (getEntryKind()) {
  730. case MachineJumpTableInfo::EK_BlockAddress:
  731. return TD.getPointerSize();
  732. case MachineJumpTableInfo::EK_GPRel64BlockAddress:
  733. return 8;
  734. case MachineJumpTableInfo::EK_GPRel32BlockAddress:
  735. case MachineJumpTableInfo::EK_LabelDifference32:
  736. case MachineJumpTableInfo::EK_Custom32:
  737. return 4;
  738. case MachineJumpTableInfo::EK_Inline:
  739. return 0;
  740. }
  741. llvm_unreachable("Unknown jump table encoding!");
  742. }
  743. /// Return the alignment of each entry in the jump table.
  744. unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
  745. // The alignment of a jump table entry is the alignment of int32 unless the
  746. // entry is just the address of a block, in which case it is the pointer
  747. // alignment.
  748. switch (getEntryKind()) {
  749. case MachineJumpTableInfo::EK_BlockAddress:
  750. return TD.getPointerABIAlignment(0);
  751. case MachineJumpTableInfo::EK_GPRel64BlockAddress:
  752. return TD.getABIIntegerTypeAlignment(64);
  753. case MachineJumpTableInfo::EK_GPRel32BlockAddress:
  754. case MachineJumpTableInfo::EK_LabelDifference32:
  755. case MachineJumpTableInfo::EK_Custom32:
  756. return TD.getABIIntegerTypeAlignment(32);
  757. case MachineJumpTableInfo::EK_Inline:
  758. return 1;
  759. }
  760. llvm_unreachable("Unknown jump table encoding!");
  761. }
  762. /// Create a new jump table entry in the jump table info.
  763. unsigned MachineJumpTableInfo::createJumpTableIndex(
  764. const std::vector<MachineBasicBlock*> &DestBBs) {
  765. assert(!DestBBs.empty() && "Cannot create an empty jump table!");
  766. JumpTables.push_back(MachineJumpTableEntry(DestBBs));
  767. return JumpTables.size()-1;
  768. }
  769. /// If Old is the target of any jump tables, update the jump tables to branch
  770. /// to New instead.
  771. bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
  772. MachineBasicBlock *New) {
  773. assert(Old != New && "Not making a change?");
  774. bool MadeChange = false;
  775. for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
  776. ReplaceMBBInJumpTable(i, Old, New);
  777. return MadeChange;
  778. }
  779. /// If Old is a target of the jump tables, update the jump table to branch to
  780. /// New instead.
  781. bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
  782. MachineBasicBlock *Old,
  783. MachineBasicBlock *New) {
  784. assert(Old != New && "Not making a change?");
  785. bool MadeChange = false;
  786. MachineJumpTableEntry &JTE = JumpTables[Idx];
  787. for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
  788. if (JTE.MBBs[j] == Old) {
  789. JTE.MBBs[j] = New;
  790. MadeChange = true;
  791. }
  792. return MadeChange;
  793. }
  794. void MachineJumpTableInfo::print(raw_ostream &OS) const {
  795. if (JumpTables.empty()) return;
  796. OS << "Jump Tables:\n";
  797. for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
  798. OS << printJumpTableEntryReference(i) << ": ";
  799. for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
  800. OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
  801. }
  802. OS << '\n';
  803. }
  804. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  805. LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
  806. #endif
  807. Printable llvm::printJumpTableEntryReference(unsigned Idx) {
  808. return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
  809. }
  810. //===----------------------------------------------------------------------===//
  811. // MachineConstantPool implementation
  812. //===----------------------------------------------------------------------===//
  813. void MachineConstantPoolValue::anchor() {}
  814. Type *MachineConstantPoolEntry::getType() const {
  815. if (isMachineConstantPoolEntry())
  816. return Val.MachineCPVal->getType();
  817. return Val.ConstVal->getType();
  818. }
  819. bool MachineConstantPoolEntry::needsRelocation() const {
  820. if (isMachineConstantPoolEntry())
  821. return true;
  822. return Val.ConstVal->needsRelocation();
  823. }
  824. SectionKind
  825. MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
  826. if (needsRelocation())
  827. return SectionKind::getReadOnlyWithRel();
  828. switch (DL->getTypeAllocSize(getType())) {
  829. case 4:
  830. return SectionKind::getMergeableConst4();
  831. case 8:
  832. return SectionKind::getMergeableConst8();
  833. case 16:
  834. return SectionKind::getMergeableConst16();
  835. case 32:
  836. return SectionKind::getMergeableConst32();
  837. default:
  838. return SectionKind::getReadOnly();
  839. }
  840. }
  841. MachineConstantPool::~MachineConstantPool() {
  842. // A constant may be a member of both Constants and MachineCPVsSharingEntries,
  843. // so keep track of which we've deleted to avoid double deletions.
  844. DenseSet<MachineConstantPoolValue*> Deleted;
  845. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  846. if (Constants[i].isMachineConstantPoolEntry()) {
  847. Deleted.insert(Constants[i].Val.MachineCPVal);
  848. delete Constants[i].Val.MachineCPVal;
  849. }
  850. for (DenseSet<MachineConstantPoolValue*>::iterator I =
  851. MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
  852. I != E; ++I) {
  853. if (Deleted.count(*I) == 0)
  854. delete *I;
  855. }
  856. }
  857. /// Test whether the given two constants can be allocated the same constant pool
  858. /// entry.
  859. static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
  860. const DataLayout &DL) {
  861. // Handle the trivial case quickly.
  862. if (A == B) return true;
  863. // If they have the same type but weren't the same constant, quickly
  864. // reject them.
  865. if (A->getType() == B->getType()) return false;
  866. // We can't handle structs or arrays.
  867. if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
  868. isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
  869. return false;
  870. // For now, only support constants with the same size.
  871. uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
  872. if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
  873. return false;
  874. Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
  875. // Try constant folding a bitcast of both instructions to an integer. If we
  876. // get two identical ConstantInt's, then we are good to share them. We use
  877. // the constant folding APIs to do this so that we get the benefit of
  878. // DataLayout.
  879. if (isa<PointerType>(A->getType()))
  880. A = ConstantFoldCastOperand(Instruction::PtrToInt,
  881. const_cast<Constant *>(A), IntTy, DL);
  882. else if (A->getType() != IntTy)
  883. A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
  884. IntTy, DL);
  885. if (isa<PointerType>(B->getType()))
  886. B = ConstantFoldCastOperand(Instruction::PtrToInt,
  887. const_cast<Constant *>(B), IntTy, DL);
  888. else if (B->getType() != IntTy)
  889. B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
  890. IntTy, DL);
  891. return A == B;
  892. }
  893. /// Create a new entry in the constant pool or return an existing one.
  894. /// User must specify the log2 of the minimum required alignment for the object.
  895. unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
  896. unsigned Alignment) {
  897. assert(Alignment && "Alignment must be specified!");
  898. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  899. // Check to see if we already have this constant.
  900. //
  901. // FIXME, this could be made much more efficient for large constant pools.
  902. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  903. if (!Constants[i].isMachineConstantPoolEntry() &&
  904. CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
  905. if ((unsigned)Constants[i].getAlignment() < Alignment)
  906. Constants[i].Alignment = Alignment;
  907. return i;
  908. }
  909. Constants.push_back(MachineConstantPoolEntry(C, Alignment));
  910. return Constants.size()-1;
  911. }
  912. unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
  913. unsigned Alignment) {
  914. assert(Alignment && "Alignment must be specified!");
  915. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  916. // Check to see if we already have this constant.
  917. //
  918. // FIXME, this could be made much more efficient for large constant pools.
  919. int Idx = V->getExistingMachineCPValue(this, Alignment);
  920. if (Idx != -1) {
  921. MachineCPVsSharingEntries.insert(V);
  922. return (unsigned)Idx;
  923. }
  924. Constants.push_back(MachineConstantPoolEntry(V, Alignment));
  925. return Constants.size()-1;
  926. }
  927. void MachineConstantPool::print(raw_ostream &OS) const {
  928. if (Constants.empty()) return;
  929. OS << "Constant Pool:\n";
  930. for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
  931. OS << " cp#" << i << ": ";
  932. if (Constants[i].isMachineConstantPoolEntry())
  933. Constants[i].Val.MachineCPVal->print(OS);
  934. else
  935. Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
  936. OS << ", align=" << Constants[i].getAlignment();
  937. OS << "\n";
  938. }
  939. }
  940. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  941. LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
  942. #endif