MachineFunction.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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/DerivedTypes.h"
  16. #include "llvm/Function.h"
  17. #include "llvm/Instructions.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Config/config.h"
  20. #include "llvm/CodeGen/MachineConstantPool.h"
  21. #include "llvm/CodeGen/MachineFunctionPass.h"
  22. #include "llvm/CodeGen/MachineFrameInfo.h"
  23. #include "llvm/CodeGen/MachineInstr.h"
  24. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/Passes.h"
  27. #include "llvm/Target/TargetData.h"
  28. #include "llvm/Target/TargetLowering.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. #include "llvm/Target/TargetFrameInfo.h"
  31. #include "llvm/Support/Compiler.h"
  32. #include "llvm/Support/GraphWriter.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <fstream>
  35. #include <sstream>
  36. using namespace llvm;
  37. bool MachineFunctionPass::runOnFunction(Function &F) {
  38. // Do not codegen any 'available_externally' functions at all, they have
  39. // definitions outside the translation unit.
  40. if (F.hasAvailableExternallyLinkage())
  41. return false;
  42. return runOnMachineFunction(MachineFunction::get(&F));
  43. }
  44. namespace {
  45. struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
  46. static char ID;
  47. std::ostream *OS;
  48. const std::string Banner;
  49. Printer (std::ostream *os, const std::string &banner)
  50. : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
  51. const char *getPassName() const { return "MachineFunction Printer"; }
  52. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  53. AU.setPreservesAll();
  54. }
  55. bool runOnMachineFunction(MachineFunction &MF) {
  56. (*OS) << Banner;
  57. MF.print (*OS);
  58. return false;
  59. }
  60. };
  61. char Printer::ID = 0;
  62. }
  63. /// Returns a newly-created MachineFunction Printer pass. The default output
  64. /// stream is std::cerr; the default banner is empty.
  65. ///
  66. FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
  67. const std::string &Banner){
  68. return new Printer(OS, Banner);
  69. }
  70. namespace {
  71. struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
  72. static char ID;
  73. Deleter() : MachineFunctionPass(&ID) {}
  74. const char *getPassName() const { return "Machine Code Deleter"; }
  75. bool runOnMachineFunction(MachineFunction &MF) {
  76. // Delete the annotation from the function now.
  77. MachineFunction::destruct(MF.getFunction());
  78. return true;
  79. }
  80. };
  81. char Deleter::ID = 0;
  82. }
  83. /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
  84. /// the current function, which should happen after the function has been
  85. /// emitted to a .s file or to memory.
  86. FunctionPass *llvm::createMachineCodeDeleter() {
  87. return new Deleter();
  88. }
  89. //===---------------------------------------------------------------------===//
  90. // MachineFunction implementation
  91. //===---------------------------------------------------------------------===//
  92. void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
  93. MBB->getParent()->DeleteMachineBasicBlock(MBB);
  94. }
  95. MachineFunction::MachineFunction(const Function *F,
  96. const TargetMachine &TM)
  97. : Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
  98. Fn(F), Target(TM) {
  99. if (TM.getRegisterInfo())
  100. RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
  101. MachineRegisterInfo(*TM.getRegisterInfo());
  102. else
  103. RegInfo = 0;
  104. MFInfo = 0;
  105. FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
  106. MachineFrameInfo(*TM.getFrameInfo());
  107. ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
  108. MachineConstantPool(TM.getTargetData());
  109. Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
  110. // Set up jump table.
  111. const TargetData &TD = *TM.getTargetData();
  112. bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
  113. unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
  114. unsigned TyAlignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
  115. : TD.getPointerABIAlignment();
  116. JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
  117. MachineJumpTableInfo(EntrySize, TyAlignment);
  118. }
  119. MachineFunction::~MachineFunction() {
  120. BasicBlocks.clear();
  121. InstructionRecycler.clear(Allocator);
  122. BasicBlockRecycler.clear(Allocator);
  123. if (RegInfo) {
  124. RegInfo->~MachineRegisterInfo();
  125. Allocator.Deallocate(RegInfo);
  126. }
  127. if (MFInfo) {
  128. MFInfo->~MachineFunctionInfo();
  129. Allocator.Deallocate(MFInfo);
  130. }
  131. FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
  132. ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
  133. JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
  134. }
  135. /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
  136. /// recomputes them. This guarantees that the MBB numbers are sequential,
  137. /// dense, and match the ordering of the blocks within the function. If a
  138. /// specific MachineBasicBlock is specified, only that block and those after
  139. /// it are renumbered.
  140. void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
  141. if (empty()) { MBBNumbering.clear(); return; }
  142. MachineFunction::iterator MBBI, E = end();
  143. if (MBB == 0)
  144. MBBI = begin();
  145. else
  146. MBBI = MBB;
  147. // Figure out the block number this should have.
  148. unsigned BlockNo = 0;
  149. if (MBBI != begin())
  150. BlockNo = prior(MBBI)->getNumber()+1;
  151. for (; MBBI != E; ++MBBI, ++BlockNo) {
  152. if (MBBI->getNumber() != (int)BlockNo) {
  153. // Remove use of the old number.
  154. if (MBBI->getNumber() != -1) {
  155. assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
  156. "MBB number mismatch!");
  157. MBBNumbering[MBBI->getNumber()] = 0;
  158. }
  159. // If BlockNo is already taken, set that block's number to -1.
  160. if (MBBNumbering[BlockNo])
  161. MBBNumbering[BlockNo]->setNumber(-1);
  162. MBBNumbering[BlockNo] = MBBI;
  163. MBBI->setNumber(BlockNo);
  164. }
  165. }
  166. // Okay, all the blocks are renumbered. If we have compactified the block
  167. // numbering, shrink MBBNumbering now.
  168. assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
  169. MBBNumbering.resize(BlockNo);
  170. }
  171. /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
  172. /// of `new MachineInstr'.
  173. ///
  174. MachineInstr *
  175. MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
  176. DebugLoc DL, bool NoImp) {
  177. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  178. MachineInstr(TID, DL, NoImp);
  179. }
  180. /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
  181. /// 'Orig' instruction, identical in all ways except the the instruction
  182. /// has no parent, prev, or next.
  183. ///
  184. MachineInstr *
  185. MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
  186. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  187. MachineInstr(*this, *Orig);
  188. }
  189. /// DeleteMachineInstr - Delete the given MachineInstr.
  190. ///
  191. void
  192. MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
  193. // Clear the instructions memoperands. This must be done manually because
  194. // the instruction's parent pointer is now null, so it can't properly
  195. // deallocate them on its own.
  196. MI->clearMemOperands(*this);
  197. MI->~MachineInstr();
  198. InstructionRecycler.Deallocate(Allocator, MI);
  199. }
  200. /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
  201. /// instead of `new MachineBasicBlock'.
  202. ///
  203. MachineBasicBlock *
  204. MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
  205. return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
  206. MachineBasicBlock(*this, bb);
  207. }
  208. /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
  209. ///
  210. void
  211. MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
  212. assert(MBB->getParent() == this && "MBB parent mismatch!");
  213. MBB->~MachineBasicBlock();
  214. BasicBlockRecycler.Deallocate(Allocator, MBB);
  215. }
  216. void MachineFunction::dump() const {
  217. print(*cerr.stream());
  218. }
  219. void MachineFunction::print(std::ostream &OS) const {
  220. OS << "# Machine code for " << Fn->getName () << "():\n";
  221. // Print Frame Information
  222. FrameInfo->print(*this, OS);
  223. // Print JumpTable Information
  224. JumpTableInfo->print(OS);
  225. // Print Constant Pool
  226. {
  227. raw_os_ostream OSS(OS);
  228. ConstantPool->print(OSS);
  229. }
  230. const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
  231. if (RegInfo && !RegInfo->livein_empty()) {
  232. OS << "Live Ins:";
  233. for (MachineRegisterInfo::livein_iterator
  234. I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
  235. if (TRI)
  236. OS << " " << TRI->getName(I->first);
  237. else
  238. OS << " Reg #" << I->first;
  239. if (I->second)
  240. OS << " in VR#" << I->second << " ";
  241. }
  242. OS << "\n";
  243. }
  244. if (RegInfo && !RegInfo->liveout_empty()) {
  245. OS << "Live Outs:";
  246. for (MachineRegisterInfo::liveout_iterator
  247. I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
  248. if (TRI)
  249. OS << " " << TRI->getName(*I);
  250. else
  251. OS << " Reg #" << *I;
  252. OS << "\n";
  253. }
  254. for (const_iterator BB = begin(); BB != end(); ++BB)
  255. BB->print(OS);
  256. OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
  257. }
  258. namespace llvm {
  259. template<>
  260. struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
  261. static std::string getGraphName(const MachineFunction *F) {
  262. return "CFG for '" + F->getFunction()->getNameStr() + "' function";
  263. }
  264. static std::string getNodeLabel(const MachineBasicBlock *Node,
  265. const MachineFunction *Graph,
  266. bool ShortNames) {
  267. if (ShortNames && Node->getBasicBlock() &&
  268. !Node->getBasicBlock()->getName().empty())
  269. return Node->getBasicBlock()->getNameStr() + ":";
  270. std::ostringstream Out;
  271. if (ShortNames) {
  272. Out << Node->getNumber() << ':';
  273. return Out.str();
  274. }
  275. Node->print(Out);
  276. std::string OutStr = Out.str();
  277. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  278. // Process string output to make it nicer...
  279. for (unsigned i = 0; i != OutStr.length(); ++i)
  280. if (OutStr[i] == '\n') { // Left justify
  281. OutStr[i] = '\\';
  282. OutStr.insert(OutStr.begin()+i+1, 'l');
  283. }
  284. return OutStr;
  285. }
  286. };
  287. }
  288. void MachineFunction::viewCFG() const
  289. {
  290. #ifndef NDEBUG
  291. ViewGraph(this, "mf" + getFunction()->getNameStr());
  292. #else
  293. cerr << "SelectionDAG::viewGraph is only available in debug builds on "
  294. << "systems with Graphviz or gv!\n";
  295. #endif // NDEBUG
  296. }
  297. void MachineFunction::viewCFGOnly() const
  298. {
  299. #ifndef NDEBUG
  300. ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
  301. #else
  302. cerr << "SelectionDAG::viewGraph is only available in debug builds on "
  303. << "systems with Graphviz or gv!\n";
  304. #endif // NDEBUG
  305. }
  306. // The next two methods are used to construct and to retrieve
  307. // the MachineCodeForFunction object for the given function.
  308. // construct() -- Allocates and initializes for a given function and target
  309. // get() -- Returns a handle to the object.
  310. // This should not be called before "construct()"
  311. // for a given Function.
  312. //
  313. MachineFunction&
  314. MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
  315. {
  316. AnnotationID MF_AID =
  317. AnnotationManager::getID("CodeGen::MachineCodeForFunction");
  318. assert(Fn->getAnnotation(MF_AID) == 0 &&
  319. "Object already exists for this function!");
  320. MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
  321. Fn->addAnnotation(mcInfo);
  322. return *mcInfo;
  323. }
  324. void MachineFunction::destruct(const Function *Fn) {
  325. AnnotationID MF_AID =
  326. AnnotationManager::getID("CodeGen::MachineCodeForFunction");
  327. bool Deleted = Fn->deleteAnnotation(MF_AID);
  328. assert(Deleted && "Machine code did not exist for function!");
  329. Deleted = Deleted; // silence warning when no assertions.
  330. }
  331. MachineFunction& MachineFunction::get(const Function *F)
  332. {
  333. AnnotationID MF_AID =
  334. AnnotationManager::getID("CodeGen::MachineCodeForFunction");
  335. MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
  336. assert(mc && "Call construct() method first to allocate the object");
  337. return *mc;
  338. }
  339. /// addLiveIn - Add the specified physical register as a live-in value and
  340. /// create a corresponding virtual register for it.
  341. unsigned MachineFunction::addLiveIn(unsigned PReg,
  342. const TargetRegisterClass *RC) {
  343. assert(RC->contains(PReg) && "Not the correct regclass!");
  344. unsigned VReg = getRegInfo().createVirtualRegister(RC);
  345. getRegInfo().addLiveIn(PReg, VReg);
  346. return VReg;
  347. }
  348. /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
  349. /// source file, line, and column. If none currently exists, create a new
  350. /// DebugLocTuple, and insert it into the DebugIdMap.
  351. unsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
  352. unsigned Line, unsigned Col) {
  353. DebugLocTuple Tuple(CompileUnit, Line, Col);
  354. DenseMap<DebugLocTuple, unsigned>::iterator II
  355. = DebugLocInfo.DebugIdMap.find(Tuple);
  356. if (II != DebugLocInfo.DebugIdMap.end())
  357. return II->second;
  358. // Add a new tuple.
  359. unsigned Id = DebugLocInfo.DebugLocations.size();
  360. DebugLocInfo.DebugLocations.push_back(Tuple);
  361. DebugLocInfo.DebugIdMap[Tuple] = Id;
  362. return Id;
  363. }
  364. /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
  365. DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
  366. unsigned Idx = DL.getIndex();
  367. assert(Idx < DebugLocInfo.DebugLocations.size() &&
  368. "Invalid index into debug locations!");
  369. return DebugLocInfo.DebugLocations[Idx];
  370. }
  371. //===----------------------------------------------------------------------===//
  372. // MachineFrameInfo implementation
  373. //===----------------------------------------------------------------------===//
  374. /// CreateFixedObject - Create a new object at a fixed location on the stack.
  375. /// All fixed objects should be created before other objects are created for
  376. /// efficiency. By default, fixed objects are immutable. This returns an
  377. /// index with a negative value.
  378. ///
  379. int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
  380. bool Immutable) {
  381. assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
  382. Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
  383. return -++NumFixedObjects;
  384. }
  385. void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
  386. const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
  387. int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
  388. for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
  389. const StackObject &SO = Objects[i];
  390. OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
  391. if (SO.Size == ~0ULL) {
  392. OS << "dead\n";
  393. continue;
  394. }
  395. if (SO.Size == 0)
  396. OS << "variable sized";
  397. else
  398. OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
  399. OS << " alignment is " << SO.Alignment << " byte"
  400. << (SO.Alignment != 1 ? "s," : ",");
  401. if (i < NumFixedObjects)
  402. OS << " fixed";
  403. if (i < NumFixedObjects || SO.SPOffset != -1) {
  404. int64_t Off = SO.SPOffset - ValOffset;
  405. OS << " at location [SP";
  406. if (Off > 0)
  407. OS << "+" << Off;
  408. else if (Off < 0)
  409. OS << Off;
  410. OS << "]";
  411. }
  412. OS << "\n";
  413. }
  414. if (HasVarSizedObjects)
  415. OS << " Stack frame contains variable sized objects\n";
  416. }
  417. void MachineFrameInfo::dump(const MachineFunction &MF) const {
  418. print(MF, *cerr.stream());
  419. }
  420. //===----------------------------------------------------------------------===//
  421. // MachineJumpTableInfo implementation
  422. //===----------------------------------------------------------------------===//
  423. /// getJumpTableIndex - Create a new jump table entry in the jump table info
  424. /// or return an existing one.
  425. ///
  426. unsigned MachineJumpTableInfo::getJumpTableIndex(
  427. const std::vector<MachineBasicBlock*> &DestBBs) {
  428. assert(!DestBBs.empty() && "Cannot create an empty jump table!");
  429. for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
  430. if (JumpTables[i].MBBs == DestBBs)
  431. return i;
  432. JumpTables.push_back(MachineJumpTableEntry(DestBBs));
  433. return JumpTables.size()-1;
  434. }
  435. /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
  436. /// the jump tables to branch to New instead.
  437. bool
  438. MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
  439. MachineBasicBlock *New) {
  440. assert(Old != New && "Not making a change?");
  441. bool MadeChange = false;
  442. for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
  443. MachineJumpTableEntry &JTE = JumpTables[i];
  444. for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
  445. if (JTE.MBBs[j] == Old) {
  446. JTE.MBBs[j] = New;
  447. MadeChange = true;
  448. }
  449. }
  450. return MadeChange;
  451. }
  452. void MachineJumpTableInfo::print(std::ostream &OS) const {
  453. // FIXME: this is lame, maybe we could print out the MBB numbers or something
  454. // like {1, 2, 4, 5, 3, 0}
  455. for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
  456. OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
  457. << " entries\n";
  458. }
  459. }
  460. void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
  461. //===----------------------------------------------------------------------===//
  462. // MachineConstantPool implementation
  463. //===----------------------------------------------------------------------===//
  464. const Type *MachineConstantPoolEntry::getType() const {
  465. if (isMachineConstantPoolEntry())
  466. return Val.MachineCPVal->getType();
  467. return Val.ConstVal->getType();
  468. }
  469. unsigned MachineConstantPoolEntry::getRelocationInfo() const {
  470. if (isMachineConstantPoolEntry())
  471. return Val.MachineCPVal->getRelocationInfo();
  472. return Val.ConstVal->getRelocationInfo();
  473. }
  474. MachineConstantPool::~MachineConstantPool() {
  475. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  476. if (Constants[i].isMachineConstantPoolEntry())
  477. delete Constants[i].Val.MachineCPVal;
  478. }
  479. /// getConstantPoolIndex - Create a new entry in the constant pool or return
  480. /// an existing one. User must specify the log2 of the minimum required
  481. /// alignment for the object.
  482. ///
  483. unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
  484. unsigned Alignment) {
  485. assert(Alignment && "Alignment must be specified!");
  486. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  487. // Check to see if we already have this constant.
  488. //
  489. // FIXME, this could be made much more efficient for large constant pools.
  490. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  491. if (Constants[i].Val.ConstVal == C &&
  492. (Constants[i].getAlignment() & (Alignment - 1)) == 0)
  493. return i;
  494. Constants.push_back(MachineConstantPoolEntry(C, Alignment));
  495. return Constants.size()-1;
  496. }
  497. unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
  498. unsigned Alignment) {
  499. assert(Alignment && "Alignment must be specified!");
  500. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  501. // Check to see if we already have this constant.
  502. //
  503. // FIXME, this could be made much more efficient for large constant pools.
  504. int Idx = V->getExistingMachineCPValue(this, Alignment);
  505. if (Idx != -1)
  506. return (unsigned)Idx;
  507. Constants.push_back(MachineConstantPoolEntry(V, Alignment));
  508. return Constants.size()-1;
  509. }
  510. void MachineConstantPool::print(raw_ostream &OS) const {
  511. for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
  512. OS << " <cp#" << i << "> is";
  513. if (Constants[i].isMachineConstantPoolEntry())
  514. Constants[i].Val.MachineCPVal->print(OS);
  515. else
  516. OS << *(Value*)Constants[i].Val.ConstVal;
  517. OS << " , alignment=" << Constants[i].getAlignment();
  518. OS << "\n";
  519. }
  520. }
  521. void MachineConstantPool::dump() const { print(errs()); }