MachineFunction.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/CodeGen/MachineFrameInfo.h"
  24. #include "llvm/CodeGen/MachineInstr.h"
  25. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/CodeGen/Passes.h"
  28. #include "llvm/Target/TargetData.h"
  29. #include "llvm/Target/TargetLowering.h"
  30. #include "llvm/Target/TargetMachine.h"
  31. #include "llvm/Target/TargetFrameInfo.h"
  32. #include "llvm/Support/GraphWriter.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. using namespace llvm;
  35. namespace {
  36. struct Printer : public MachineFunctionPass {
  37. static char ID;
  38. raw_ostream &OS;
  39. const std::string Banner;
  40. Printer(raw_ostream &os, const std::string &banner)
  41. : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
  42. const char *getPassName() const { return "MachineFunction Printer"; }
  43. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  44. AU.setPreservesAll();
  45. MachineFunctionPass::getAnalysisUsage(AU);
  46. }
  47. bool runOnMachineFunction(MachineFunction &MF) {
  48. OS << "# " << Banner << ":\n";
  49. MF.print(OS);
  50. return false;
  51. }
  52. };
  53. char Printer::ID = 0;
  54. }
  55. /// Returns a newly-created MachineFunction Printer pass. The default banner is
  56. /// empty.
  57. ///
  58. FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
  59. const std::string &Banner){
  60. return new Printer(OS, Banner);
  61. }
  62. //===---------------------------------------------------------------------===//
  63. // MachineFunction implementation
  64. //===---------------------------------------------------------------------===//
  65. // Out of line virtual method.
  66. MachineFunctionInfo::~MachineFunctionInfo() {}
  67. void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
  68. MBB->getParent()->DeleteMachineBasicBlock(MBB);
  69. }
  70. MachineFunction::MachineFunction(Function *F,
  71. const TargetMachine &TM)
  72. : Fn(F), Target(TM) {
  73. if (TM.getRegisterInfo())
  74. RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
  75. MachineRegisterInfo(*TM.getRegisterInfo());
  76. else
  77. RegInfo = 0;
  78. MFInfo = 0;
  79. FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
  80. MachineFrameInfo(*TM.getFrameInfo());
  81. ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
  82. MachineConstantPool(TM.getTargetData());
  83. Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
  84. // Set up jump table.
  85. const TargetData &TD = *TM.getTargetData();
  86. bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
  87. unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
  88. unsigned TyAlignment = IsPic ?
  89. TD.getABITypeAlignment(Type::getInt32Ty(F->getContext()))
  90. : TD.getPointerABIAlignment();
  91. JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
  92. MachineJumpTableInfo(EntrySize, TyAlignment);
  93. }
  94. MachineFunction::~MachineFunction() {
  95. BasicBlocks.clear();
  96. InstructionRecycler.clear(Allocator);
  97. BasicBlockRecycler.clear(Allocator);
  98. if (RegInfo) {
  99. RegInfo->~MachineRegisterInfo();
  100. Allocator.Deallocate(RegInfo);
  101. }
  102. if (MFInfo) {
  103. MFInfo->~MachineFunctionInfo();
  104. Allocator.Deallocate(MFInfo);
  105. }
  106. FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
  107. ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
  108. JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
  109. }
  110. /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
  111. /// recomputes them. This guarantees that the MBB numbers are sequential,
  112. /// dense, and match the ordering of the blocks within the function. If a
  113. /// specific MachineBasicBlock is specified, only that block and those after
  114. /// it are renumbered.
  115. void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
  116. if (empty()) { MBBNumbering.clear(); return; }
  117. MachineFunction::iterator MBBI, E = end();
  118. if (MBB == 0)
  119. MBBI = begin();
  120. else
  121. MBBI = MBB;
  122. // Figure out the block number this should have.
  123. unsigned BlockNo = 0;
  124. if (MBBI != begin())
  125. BlockNo = prior(MBBI)->getNumber()+1;
  126. for (; MBBI != E; ++MBBI, ++BlockNo) {
  127. if (MBBI->getNumber() != (int)BlockNo) {
  128. // Remove use of the old number.
  129. if (MBBI->getNumber() != -1) {
  130. assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
  131. "MBB number mismatch!");
  132. MBBNumbering[MBBI->getNumber()] = 0;
  133. }
  134. // If BlockNo is already taken, set that block's number to -1.
  135. if (MBBNumbering[BlockNo])
  136. MBBNumbering[BlockNo]->setNumber(-1);
  137. MBBNumbering[BlockNo] = MBBI;
  138. MBBI->setNumber(BlockNo);
  139. }
  140. }
  141. // Okay, all the blocks are renumbered. If we have compactified the block
  142. // numbering, shrink MBBNumbering now.
  143. assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
  144. MBBNumbering.resize(BlockNo);
  145. }
  146. /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
  147. /// of `new MachineInstr'.
  148. ///
  149. MachineInstr *
  150. MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
  151. DebugLoc DL, bool NoImp) {
  152. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  153. MachineInstr(TID, DL, NoImp);
  154. }
  155. /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
  156. /// 'Orig' instruction, identical in all ways except the the instruction
  157. /// has no parent, prev, or next.
  158. ///
  159. MachineInstr *
  160. MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
  161. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  162. MachineInstr(*this, *Orig);
  163. }
  164. /// DeleteMachineInstr - Delete the given MachineInstr.
  165. ///
  166. void
  167. MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
  168. MI->~MachineInstr();
  169. InstructionRecycler.Deallocate(Allocator, MI);
  170. }
  171. /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
  172. /// instead of `new MachineBasicBlock'.
  173. ///
  174. MachineBasicBlock *
  175. MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
  176. return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
  177. MachineBasicBlock(*this, bb);
  178. }
  179. /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
  180. ///
  181. void
  182. MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
  183. assert(MBB->getParent() == this && "MBB parent mismatch!");
  184. MBB->~MachineBasicBlock();
  185. BasicBlockRecycler.Deallocate(Allocator, MBB);
  186. }
  187. MachineMemOperand *
  188. MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
  189. int64_t o, uint64_t s,
  190. unsigned base_alignment) {
  191. return new (Allocator.Allocate<MachineMemOperand>())
  192. MachineMemOperand(v, f, o, s, base_alignment);
  193. }
  194. MachineMemOperand *
  195. MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
  196. int64_t Offset, uint64_t Size) {
  197. return new (Allocator.Allocate<MachineMemOperand>())
  198. MachineMemOperand(MMO->getValue(), MMO->getFlags(),
  199. int64_t(uint64_t(MMO->getOffset()) +
  200. uint64_t(Offset)),
  201. Size, MMO->getBaseAlignment());
  202. }
  203. MachineInstr::mmo_iterator
  204. MachineFunction::allocateMemRefsArray(unsigned long Num) {
  205. return Allocator.Allocate<MachineMemOperand *>(Num);
  206. }
  207. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  208. MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
  209. MachineInstr::mmo_iterator End) {
  210. // Count the number of load mem refs.
  211. unsigned Num = 0;
  212. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  213. if ((*I)->isLoad())
  214. ++Num;
  215. // Allocate a new array and populate it with the load information.
  216. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  217. unsigned Index = 0;
  218. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  219. if ((*I)->isLoad()) {
  220. if (!(*I)->isStore())
  221. // Reuse the MMO.
  222. Result[Index] = *I;
  223. else {
  224. // Clone the MMO and unset the store flag.
  225. MachineMemOperand *JustLoad =
  226. getMachineMemOperand((*I)->getValue(),
  227. (*I)->getFlags() & ~MachineMemOperand::MOStore,
  228. (*I)->getOffset(), (*I)->getSize(),
  229. (*I)->getBaseAlignment());
  230. Result[Index] = JustLoad;
  231. }
  232. ++Index;
  233. }
  234. }
  235. return std::make_pair(Result, Result + Num);
  236. }
  237. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  238. MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
  239. MachineInstr::mmo_iterator End) {
  240. // Count the number of load mem refs.
  241. unsigned Num = 0;
  242. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  243. if ((*I)->isStore())
  244. ++Num;
  245. // Allocate a new array and populate it with the store information.
  246. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  247. unsigned Index = 0;
  248. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  249. if ((*I)->isStore()) {
  250. if (!(*I)->isLoad())
  251. // Reuse the MMO.
  252. Result[Index] = *I;
  253. else {
  254. // Clone the MMO and unset the load flag.
  255. MachineMemOperand *JustStore =
  256. getMachineMemOperand((*I)->getValue(),
  257. (*I)->getFlags() & ~MachineMemOperand::MOLoad,
  258. (*I)->getOffset(), (*I)->getSize(),
  259. (*I)->getBaseAlignment());
  260. Result[Index] = JustStore;
  261. }
  262. ++Index;
  263. }
  264. }
  265. return std::make_pair(Result, Result + Num);
  266. }
  267. void MachineFunction::dump() const {
  268. print(errs());
  269. }
  270. void MachineFunction::print(raw_ostream &OS) const {
  271. OS << "# Machine code for function " << Fn->getName() << ":\n";
  272. // Print Frame Information
  273. FrameInfo->print(*this, OS);
  274. // Print JumpTable Information
  275. JumpTableInfo->print(OS);
  276. // Print Constant Pool
  277. ConstantPool->print(OS);
  278. const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
  279. if (RegInfo && !RegInfo->livein_empty()) {
  280. OS << "Function Live Ins: ";
  281. for (MachineRegisterInfo::livein_iterator
  282. I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
  283. if (TRI)
  284. OS << "%" << TRI->getName(I->first);
  285. else
  286. OS << " %physreg" << I->first;
  287. if (I->second)
  288. OS << " in reg%" << I->second;
  289. if (next(I) != E)
  290. OS << ", ";
  291. }
  292. OS << '\n';
  293. }
  294. if (RegInfo && !RegInfo->liveout_empty()) {
  295. OS << "Function Live Outs: ";
  296. for (MachineRegisterInfo::liveout_iterator
  297. I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
  298. if (TRI)
  299. OS << '%' << TRI->getName(*I);
  300. else
  301. OS << "%physreg" << *I;
  302. if (next(I) != E)
  303. OS << " ";
  304. }
  305. OS << '\n';
  306. }
  307. for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
  308. OS << '\n';
  309. BB->print(OS);
  310. }
  311. OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
  312. }
  313. namespace llvm {
  314. template<>
  315. struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
  316. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  317. static std::string getGraphName(const MachineFunction *F) {
  318. return "CFG for '" + F->getFunction()->getNameStr() + "' function";
  319. }
  320. static std::string getNodeLabel(const MachineBasicBlock *Node,
  321. const MachineFunction *Graph,
  322. bool ShortNames) {
  323. if (ShortNames && Node->getBasicBlock() &&
  324. !Node->getBasicBlock()->getName().empty())
  325. return Node->getBasicBlock()->getNameStr() + ":";
  326. std::string OutStr;
  327. {
  328. raw_string_ostream OSS(OutStr);
  329. if (ShortNames)
  330. OSS << Node->getNumber() << ':';
  331. else
  332. Node->print(OSS);
  333. }
  334. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  335. // Process string output to make it nicer...
  336. for (unsigned i = 0; i != OutStr.length(); ++i)
  337. if (OutStr[i] == '\n') { // Left justify
  338. OutStr[i] = '\\';
  339. OutStr.insert(OutStr.begin()+i+1, 'l');
  340. }
  341. return OutStr;
  342. }
  343. };
  344. }
  345. void MachineFunction::viewCFG() const
  346. {
  347. #ifndef NDEBUG
  348. ViewGraph(this, "mf" + getFunction()->getNameStr());
  349. #else
  350. errs() << "SelectionDAG::viewGraph is only available in debug builds on "
  351. << "systems with Graphviz or gv!\n";
  352. #endif // NDEBUG
  353. }
  354. void MachineFunction::viewCFGOnly() const
  355. {
  356. #ifndef NDEBUG
  357. ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
  358. #else
  359. errs() << "SelectionDAG::viewGraph is only available in debug builds on "
  360. << "systems with Graphviz or gv!\n";
  361. #endif // NDEBUG
  362. }
  363. /// addLiveIn - Add the specified physical register as a live-in value and
  364. /// create a corresponding virtual register for it.
  365. unsigned MachineFunction::addLiveIn(unsigned PReg,
  366. const TargetRegisterClass *RC) {
  367. assert(RC->contains(PReg) && "Not the correct regclass!");
  368. unsigned VReg = getRegInfo().createVirtualRegister(RC);
  369. getRegInfo().addLiveIn(PReg, VReg);
  370. return VReg;
  371. }
  372. /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
  373. DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
  374. unsigned Idx = DL.getIndex();
  375. assert(Idx < DebugLocInfo.DebugLocations.size() &&
  376. "Invalid index into debug locations!");
  377. return DebugLocInfo.DebugLocations[Idx];
  378. }
  379. //===----------------------------------------------------------------------===//
  380. // MachineFrameInfo implementation
  381. //===----------------------------------------------------------------------===//
  382. /// CreateFixedObject - Create a new object at a fixed location on the stack.
  383. /// All fixed objects should be created before other objects are created for
  384. /// efficiency. By default, fixed objects are immutable. This returns an
  385. /// index with a negative value.
  386. ///
  387. int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
  388. bool Immutable, bool isSS) {
  389. assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
  390. Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
  391. isSS));
  392. return -++NumFixedObjects;
  393. }
  394. BitVector
  395. MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
  396. assert(MBB && "MBB must be valid");
  397. const MachineFunction *MF = MBB->getParent();
  398. assert(MF && "MBB must be part of a MachineFunction");
  399. const TargetMachine &TM = MF->getTarget();
  400. const TargetRegisterInfo *TRI = TM.getRegisterInfo();
  401. BitVector BV(TRI->getNumRegs());
  402. // Before CSI is calculated, no registers are considered pristine. They can be
  403. // freely used and PEI will make sure they are saved.
  404. if (!isCalleeSavedInfoValid())
  405. return BV;
  406. for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
  407. BV.set(*CSR);
  408. // The entry MBB always has all CSRs pristine.
  409. if (MBB == &MF->front())
  410. return BV;
  411. // On other MBBs the saved CSRs are not pristine.
  412. const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
  413. for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
  414. E = CSI.end(); I != E; ++I)
  415. BV.reset(I->getReg());
  416. return BV;
  417. }
  418. void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
  419. if (Objects.empty()) return;
  420. const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
  421. int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
  422. OS << "Frame Objects:\n";
  423. for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
  424. const StackObject &SO = Objects[i];
  425. OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
  426. if (SO.Size == ~0ULL) {
  427. OS << "dead\n";
  428. continue;
  429. }
  430. if (SO.Size == 0)
  431. OS << "variable sized";
  432. else
  433. OS << "size=" << SO.Size;
  434. OS << ", align=" << SO.Alignment;
  435. if (i < NumFixedObjects)
  436. OS << ", fixed";
  437. if (i < NumFixedObjects || SO.SPOffset != -1) {
  438. int64_t Off = SO.SPOffset - ValOffset;
  439. OS << ", at location [SP";
  440. if (Off > 0)
  441. OS << "+" << Off;
  442. else if (Off < 0)
  443. OS << Off;
  444. OS << "]";
  445. }
  446. OS << "\n";
  447. }
  448. }
  449. void MachineFrameInfo::dump(const MachineFunction &MF) const {
  450. print(MF, errs());
  451. }
  452. //===----------------------------------------------------------------------===//
  453. // MachineJumpTableInfo implementation
  454. //===----------------------------------------------------------------------===//
  455. /// getJumpTableIndex - Create a new jump table entry in the jump table info
  456. /// or return an existing one.
  457. ///
  458. unsigned MachineJumpTableInfo::getJumpTableIndex(
  459. const std::vector<MachineBasicBlock*> &DestBBs) {
  460. assert(!DestBBs.empty() && "Cannot create an empty jump table!");
  461. JumpTables.push_back(MachineJumpTableEntry(DestBBs));
  462. return JumpTables.size()-1;
  463. }
  464. /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
  465. /// the jump tables to branch to New instead.
  466. bool
  467. MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
  468. MachineBasicBlock *New) {
  469. assert(Old != New && "Not making a change?");
  470. bool MadeChange = false;
  471. for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
  472. ReplaceMBBInJumpTable(i, Old, New);
  473. return MadeChange;
  474. }
  475. /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
  476. /// the jump table to branch to New instead.
  477. bool
  478. MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
  479. MachineBasicBlock *Old,
  480. MachineBasicBlock *New) {
  481. assert(Old != New && "Not making a change?");
  482. bool MadeChange = false;
  483. MachineJumpTableEntry &JTE = JumpTables[Idx];
  484. for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
  485. if (JTE.MBBs[j] == Old) {
  486. JTE.MBBs[j] = New;
  487. MadeChange = true;
  488. }
  489. return MadeChange;
  490. }
  491. void MachineJumpTableInfo::print(raw_ostream &OS) const {
  492. if (JumpTables.empty()) return;
  493. OS << "Jump Tables:\n";
  494. for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
  495. OS << " jt#" << i << ": ";
  496. for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
  497. OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
  498. }
  499. OS << '\n';
  500. }
  501. void MachineJumpTableInfo::dump() const { print(errs()); }
  502. //===----------------------------------------------------------------------===//
  503. // MachineConstantPool implementation
  504. //===----------------------------------------------------------------------===//
  505. const Type *MachineConstantPoolEntry::getType() const {
  506. if (isMachineConstantPoolEntry())
  507. return Val.MachineCPVal->getType();
  508. return Val.ConstVal->getType();
  509. }
  510. unsigned MachineConstantPoolEntry::getRelocationInfo() const {
  511. if (isMachineConstantPoolEntry())
  512. return Val.MachineCPVal->getRelocationInfo();
  513. return Val.ConstVal->getRelocationInfo();
  514. }
  515. MachineConstantPool::~MachineConstantPool() {
  516. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  517. if (Constants[i].isMachineConstantPoolEntry())
  518. delete Constants[i].Val.MachineCPVal;
  519. }
  520. /// CanShareConstantPoolEntry - Test whether the given two constants
  521. /// can be allocated the same constant pool entry.
  522. static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
  523. const TargetData *TD) {
  524. // Handle the trivial case quickly.
  525. if (A == B) return true;
  526. // If they have the same type but weren't the same constant, quickly
  527. // reject them.
  528. if (A->getType() == B->getType()) return false;
  529. // For now, only support constants with the same size.
  530. if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
  531. return false;
  532. // If a floating-point value and an integer value have the same encoding,
  533. // they can share a constant-pool entry.
  534. if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
  535. if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
  536. return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
  537. if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
  538. if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
  539. return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
  540. // Two vectors can share an entry if each pair of corresponding
  541. // elements could.
  542. if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
  543. if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
  544. if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
  545. return false;
  546. for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
  547. if (!CanShareConstantPoolEntry(AV->getOperand(i),
  548. BV->getOperand(i), TD))
  549. return false;
  550. return true;
  551. }
  552. // TODO: Handle other cases.
  553. return false;
  554. }
  555. /// getConstantPoolIndex - Create a new entry in the constant pool or return
  556. /// an existing one. User must specify the log2 of the minimum required
  557. /// alignment for the object.
  558. ///
  559. unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
  560. unsigned Alignment) {
  561. assert(Alignment && "Alignment must be specified!");
  562. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  563. // Check to see if we already have this constant.
  564. //
  565. // FIXME, this could be made much more efficient for large constant pools.
  566. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  567. if (!Constants[i].isMachineConstantPoolEntry() &&
  568. CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
  569. if ((unsigned)Constants[i].getAlignment() < Alignment)
  570. Constants[i].Alignment = Alignment;
  571. return i;
  572. }
  573. Constants.push_back(MachineConstantPoolEntry(C, Alignment));
  574. return Constants.size()-1;
  575. }
  576. unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
  577. unsigned Alignment) {
  578. assert(Alignment && "Alignment must be specified!");
  579. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  580. // Check to see if we already have this constant.
  581. //
  582. // FIXME, this could be made much more efficient for large constant pools.
  583. int Idx = V->getExistingMachineCPValue(this, Alignment);
  584. if (Idx != -1)
  585. return (unsigned)Idx;
  586. Constants.push_back(MachineConstantPoolEntry(V, Alignment));
  587. return Constants.size()-1;
  588. }
  589. void MachineConstantPool::print(raw_ostream &OS) const {
  590. if (Constants.empty()) return;
  591. OS << "Constant Pool:\n";
  592. for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
  593. OS << " cp#" << i << ": ";
  594. if (Constants[i].isMachineConstantPoolEntry())
  595. Constants[i].Val.MachineCPVal->print(OS);
  596. else
  597. OS << *(Value*)Constants[i].Val.ConstVal;
  598. OS << ", align=" << Constants[i].getAlignment();
  599. OS << "\n";
  600. }
  601. }
  602. void MachineConstantPool::dump() const { print(errs()); }