MachineFunction.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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/STLExtras.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/Analysis/ConstantFolding.h"
  19. #include "llvm/CodeGen/MachineConstantPool.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineFunctionPass.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  24. #include "llvm/CodeGen/MachineModuleInfo.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/Passes.h"
  27. #include "llvm/DebugInfo.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/MC/MCAsmInfo.h"
  31. #include "llvm/MC/MCContext.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/GraphWriter.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/Target/TargetFrameLowering.h"
  36. #include "llvm/Target/TargetLowering.h"
  37. #include "llvm/Target/TargetMachine.h"
  38. using namespace llvm;
  39. //===----------------------------------------------------------------------===//
  40. // MachineFunction implementation
  41. //===----------------------------------------------------------------------===//
  42. // Out of line virtual method.
  43. MachineFunctionInfo::~MachineFunctionInfo() {}
  44. void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
  45. MBB->getParent()->DeleteMachineBasicBlock(MBB);
  46. }
  47. MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
  48. unsigned FunctionNum, MachineModuleInfo &mmi,
  49. GCModuleInfo* gmi)
  50. : Fn(F), Target(TM), Ctx(mmi.getContext()), MMI(mmi), GMI(gmi) {
  51. if (TM.getRegisterInfo())
  52. RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo());
  53. else
  54. RegInfo = 0;
  55. MFInfo = 0;
  56. FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameLowering(),
  57. TM.Options.RealignStack);
  58. if (Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
  59. Attribute::StackAlignment))
  60. FrameInfo->ensureMaxAlignment(Fn->getAttributes().
  61. getStackAlignment(AttributeSet::FunctionIndex));
  62. ConstantPool = new (Allocator) MachineConstantPool(TM.getDataLayout());
  63. Alignment = TM.getTargetLowering()->getMinFunctionAlignment();
  64. // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
  65. if (!Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
  66. Attribute::OptimizeForSize))
  67. Alignment = std::max(Alignment,
  68. TM.getTargetLowering()->getPrefFunctionAlignment());
  69. FunctionNumber = FunctionNum;
  70. JumpTableInfo = 0;
  71. }
  72. MachineFunction::~MachineFunction() {
  73. // Don't call destructors on MachineInstr and MachineOperand. All of their
  74. // memory comes from the BumpPtrAllocator which is about to be purged.
  75. //
  76. // Do call MachineBasicBlock destructors, it contains std::vectors.
  77. for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
  78. I->Insts.clearAndLeakNodesUnsafely();
  79. InstructionRecycler.clear(Allocator);
  80. OperandRecycler.clear(Allocator);
  81. BasicBlockRecycler.clear(Allocator);
  82. if (RegInfo) {
  83. RegInfo->~MachineRegisterInfo();
  84. Allocator.Deallocate(RegInfo);
  85. }
  86. if (MFInfo) {
  87. MFInfo->~MachineFunctionInfo();
  88. Allocator.Deallocate(MFInfo);
  89. }
  90. FrameInfo->~MachineFrameInfo();
  91. Allocator.Deallocate(FrameInfo);
  92. ConstantPool->~MachineConstantPool();
  93. Allocator.Deallocate(ConstantPool);
  94. if (JumpTableInfo) {
  95. JumpTableInfo->~MachineJumpTableInfo();
  96. Allocator.Deallocate(JumpTableInfo);
  97. }
  98. }
  99. /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
  100. /// does already exist, allocate one.
  101. MachineJumpTableInfo *MachineFunction::
  102. getOrCreateJumpTableInfo(unsigned EntryKind) {
  103. if (JumpTableInfo) return JumpTableInfo;
  104. JumpTableInfo = new (Allocator)
  105. MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
  106. return JumpTableInfo;
  107. }
  108. /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
  109. /// recomputes them. This guarantees that the MBB numbers are sequential,
  110. /// dense, and match the ordering of the blocks within the function. If a
  111. /// specific MachineBasicBlock is specified, only that block and those after
  112. /// it are renumbered.
  113. void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
  114. if (empty()) { MBBNumbering.clear(); return; }
  115. MachineFunction::iterator MBBI, E = end();
  116. if (MBB == 0)
  117. MBBI = begin();
  118. else
  119. MBBI = MBB;
  120. // Figure out the block number this should have.
  121. unsigned BlockNo = 0;
  122. if (MBBI != begin())
  123. BlockNo = prior(MBBI)->getNumber()+1;
  124. for (; MBBI != E; ++MBBI, ++BlockNo) {
  125. if (MBBI->getNumber() != (int)BlockNo) {
  126. // Remove use of the old number.
  127. if (MBBI->getNumber() != -1) {
  128. assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
  129. "MBB number mismatch!");
  130. MBBNumbering[MBBI->getNumber()] = 0;
  131. }
  132. // If BlockNo is already taken, set that block's number to -1.
  133. if (MBBNumbering[BlockNo])
  134. MBBNumbering[BlockNo]->setNumber(-1);
  135. MBBNumbering[BlockNo] = MBBI;
  136. MBBI->setNumber(BlockNo);
  137. }
  138. }
  139. // Okay, all the blocks are renumbered. If we have compactified the block
  140. // numbering, shrink MBBNumbering now.
  141. assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
  142. MBBNumbering.resize(BlockNo);
  143. }
  144. /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
  145. /// of `new MachineInstr'.
  146. ///
  147. MachineInstr *
  148. MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
  149. DebugLoc DL, bool NoImp) {
  150. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  151. MachineInstr(*this, MCID, DL, NoImp);
  152. }
  153. /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
  154. /// 'Orig' instruction, identical in all ways except the instruction
  155. /// has no parent, prev, or next.
  156. ///
  157. MachineInstr *
  158. MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
  159. return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
  160. MachineInstr(*this, *Orig);
  161. }
  162. /// DeleteMachineInstr - Delete the given MachineInstr.
  163. ///
  164. /// This function also serves as the MachineInstr destructor - the real
  165. /// ~MachineInstr() destructor must be empty.
  166. void
  167. MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
  168. // Strip it for parts. The operand array and the MI object itself are
  169. // independently recyclable.
  170. if (MI->Operands)
  171. deallocateOperandArray(MI->CapOperands, MI->Operands);
  172. // Don't call ~MachineInstr() which must be trivial anyway because
  173. // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
  174. // destructors.
  175. InstructionRecycler.Deallocate(Allocator, MI);
  176. }
  177. /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
  178. /// instead of `new MachineBasicBlock'.
  179. ///
  180. MachineBasicBlock *
  181. MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
  182. return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
  183. MachineBasicBlock(*this, bb);
  184. }
  185. /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
  186. ///
  187. void
  188. MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
  189. assert(MBB->getParent() == this && "MBB parent mismatch!");
  190. MBB->~MachineBasicBlock();
  191. BasicBlockRecycler.Deallocate(Allocator, MBB);
  192. }
  193. MachineMemOperand *
  194. MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
  195. uint64_t s, unsigned base_alignment,
  196. const MDNode *TBAAInfo,
  197. const MDNode *Ranges) {
  198. return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment,
  199. TBAAInfo, Ranges);
  200. }
  201. MachineMemOperand *
  202. MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
  203. int64_t Offset, uint64_t Size) {
  204. return new (Allocator)
  205. MachineMemOperand(MachinePointerInfo(MMO->getValue(),
  206. MMO->getOffset()+Offset),
  207. MMO->getFlags(), Size,
  208. MMO->getBaseAlignment(), 0);
  209. }
  210. MachineInstr::mmo_iterator
  211. MachineFunction::allocateMemRefsArray(unsigned long Num) {
  212. return Allocator.Allocate<MachineMemOperand *>(Num);
  213. }
  214. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  215. MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
  216. MachineInstr::mmo_iterator End) {
  217. // Count the number of load mem refs.
  218. unsigned Num = 0;
  219. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  220. if ((*I)->isLoad())
  221. ++Num;
  222. // Allocate a new array and populate it with the load information.
  223. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  224. unsigned Index = 0;
  225. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  226. if ((*I)->isLoad()) {
  227. if (!(*I)->isStore())
  228. // Reuse the MMO.
  229. Result[Index] = *I;
  230. else {
  231. // Clone the MMO and unset the store flag.
  232. MachineMemOperand *JustLoad =
  233. getMachineMemOperand((*I)->getPointerInfo(),
  234. (*I)->getFlags() & ~MachineMemOperand::MOStore,
  235. (*I)->getSize(), (*I)->getBaseAlignment(),
  236. (*I)->getTBAAInfo());
  237. Result[Index] = JustLoad;
  238. }
  239. ++Index;
  240. }
  241. }
  242. return std::make_pair(Result, Result + Num);
  243. }
  244. std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
  245. MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
  246. MachineInstr::mmo_iterator End) {
  247. // Count the number of load mem refs.
  248. unsigned Num = 0;
  249. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
  250. if ((*I)->isStore())
  251. ++Num;
  252. // Allocate a new array and populate it with the store information.
  253. MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
  254. unsigned Index = 0;
  255. for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
  256. if ((*I)->isStore()) {
  257. if (!(*I)->isLoad())
  258. // Reuse the MMO.
  259. Result[Index] = *I;
  260. else {
  261. // Clone the MMO and unset the load flag.
  262. MachineMemOperand *JustStore =
  263. getMachineMemOperand((*I)->getPointerInfo(),
  264. (*I)->getFlags() & ~MachineMemOperand::MOLoad,
  265. (*I)->getSize(), (*I)->getBaseAlignment(),
  266. (*I)->getTBAAInfo());
  267. Result[Index] = JustStore;
  268. }
  269. ++Index;
  270. }
  271. }
  272. return std::make_pair(Result, Result + Num);
  273. }
  274. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  275. void MachineFunction::dump() const {
  276. print(dbgs());
  277. }
  278. #endif
  279. StringRef MachineFunction::getName() const {
  280. assert(getFunction() && "No function!");
  281. return getFunction()->getName();
  282. }
  283. void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
  284. OS << "# Machine code for function " << getName() << ": ";
  285. if (RegInfo) {
  286. OS << (RegInfo->isSSA() ? "SSA" : "Post SSA");
  287. if (!RegInfo->tracksLiveness())
  288. OS << ", not tracking liveness";
  289. }
  290. OS << '\n';
  291. // Print Frame Information
  292. FrameInfo->print(*this, OS);
  293. // Print JumpTable Information
  294. if (JumpTableInfo)
  295. JumpTableInfo->print(OS);
  296. // Print Constant Pool
  297. ConstantPool->print(OS);
  298. const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
  299. if (RegInfo && !RegInfo->livein_empty()) {
  300. OS << "Function Live Ins: ";
  301. for (MachineRegisterInfo::livein_iterator
  302. I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
  303. OS << PrintReg(I->first, TRI);
  304. if (I->second)
  305. OS << " in " << PrintReg(I->second, TRI);
  306. if (llvm::next(I) != E)
  307. OS << ", ";
  308. }
  309. OS << '\n';
  310. }
  311. if (RegInfo && !RegInfo->liveout_empty()) {
  312. OS << "Function Live Outs:";
  313. for (MachineRegisterInfo::liveout_iterator
  314. I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
  315. OS << ' ' << PrintReg(*I, TRI);
  316. OS << '\n';
  317. }
  318. for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
  319. OS << '\n';
  320. BB->print(OS, Indexes);
  321. }
  322. OS << "\n# End machine code for function " << getName() << ".\n\n";
  323. }
  324. namespace llvm {
  325. template<>
  326. struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
  327. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  328. static std::string getGraphName(const MachineFunction *F) {
  329. return "CFG for '" + F->getName().str() + "' function";
  330. }
  331. std::string getNodeLabel(const MachineBasicBlock *Node,
  332. const MachineFunction *Graph) {
  333. std::string OutStr;
  334. {
  335. raw_string_ostream OSS(OutStr);
  336. if (isSimple()) {
  337. OSS << "BB#" << Node->getNumber();
  338. if (const BasicBlock *BB = Node->getBasicBlock())
  339. OSS << ": " << BB->getName();
  340. } else
  341. Node->print(OSS);
  342. }
  343. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  344. // Process string output to make it nicer...
  345. for (unsigned i = 0; i != OutStr.length(); ++i)
  346. if (OutStr[i] == '\n') { // Left justify
  347. OutStr[i] = '\\';
  348. OutStr.insert(OutStr.begin()+i+1, 'l');
  349. }
  350. return OutStr;
  351. }
  352. };
  353. }
  354. void MachineFunction::viewCFG() const
  355. {
  356. #ifndef NDEBUG
  357. ViewGraph(this, "mf" + getName());
  358. #else
  359. errs() << "MachineFunction::viewCFG is only available in debug builds on "
  360. << "systems with Graphviz or gv!\n";
  361. #endif // NDEBUG
  362. }
  363. void MachineFunction::viewCFGOnly() const
  364. {
  365. #ifndef NDEBUG
  366. ViewGraph(this, "mf" + getName(), true);
  367. #else
  368. errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
  369. << "systems with Graphviz or gv!\n";
  370. #endif // NDEBUG
  371. }
  372. /// addLiveIn - Add the specified physical register as a live-in value and
  373. /// create a corresponding virtual register for it.
  374. unsigned MachineFunction::addLiveIn(unsigned PReg,
  375. const TargetRegisterClass *RC) {
  376. MachineRegisterInfo &MRI = getRegInfo();
  377. unsigned VReg = MRI.getLiveInVirtReg(PReg);
  378. if (VReg) {
  379. assert(MRI.getRegClass(VReg) == RC && "Register class mismatch!");
  380. return VReg;
  381. }
  382. VReg = MRI.createVirtualRegister(RC);
  383. MRI.addLiveIn(PReg, VReg);
  384. return VReg;
  385. }
  386. /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
  387. /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
  388. /// normal 'L' label is returned.
  389. MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
  390. bool isLinkerPrivate) const {
  391. assert(JumpTableInfo && "No jump tables");
  392. assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
  393. const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
  394. const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
  395. MAI.getPrivateGlobalPrefix();
  396. SmallString<60> Name;
  397. raw_svector_ostream(Name)
  398. << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
  399. return Ctx.GetOrCreateSymbol(Name.str());
  400. }
  401. /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
  402. /// base.
  403. MCSymbol *MachineFunction::getPICBaseSymbol() const {
  404. const MCAsmInfo &MAI = *Target.getMCAsmInfo();
  405. return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
  406. Twine(getFunctionNumber())+"$pb");
  407. }
  408. //===----------------------------------------------------------------------===//
  409. // MachineFrameInfo implementation
  410. //===----------------------------------------------------------------------===//
  411. /// ensureMaxAlignment - Make sure the function is at least Align bytes
  412. /// aligned.
  413. void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
  414. if (!TFI.isStackRealignable() || !RealignOption)
  415. assert(Align <= TFI.getStackAlignment() &&
  416. "For targets without stack realignment, Align is out of limit!");
  417. if (MaxAlignment < Align) MaxAlignment = Align;
  418. }
  419. /// clampStackAlignment - Clamp the alignment if requested and emit a warning.
  420. static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned PrefAlign,
  421. unsigned MinAlign, unsigned StackAlign,
  422. const AllocaInst *Alloca = 0) {
  423. if (!ShouldClamp || PrefAlign <= StackAlign)
  424. return PrefAlign;
  425. if (Alloca && MinAlign > StackAlign)
  426. Alloca->getParent()->getContext().emitWarning(Alloca,
  427. "Requested alignment exceeds the stack alignment!");
  428. else
  429. assert(MinAlign <= StackAlign &&
  430. "Requested alignment exceeds the stack alignment!");
  431. return StackAlign;
  432. }
  433. /// CreateStackObjectWithMinAlign - Create a new statically sized stack
  434. /// object, returning a nonnegative identifier to represent it. This function
  435. /// takes a preferred alignment and a minimal alignment.
  436. ///
  437. int MachineFrameInfo::CreateStackObjectWithMinAlign(uint64_t Size,
  438. unsigned PrefAlignment, unsigned MinAlignment,
  439. bool isSS, bool MayNeedSP, const AllocaInst *Alloca) {
  440. assert(Size != 0 && "Cannot allocate zero size stack objects!");
  441. unsigned Alignment = clampStackAlignment(
  442. !TFI.isStackRealignable() || !RealignOption,
  443. PrefAlignment, MinAlignment,
  444. TFI.getStackAlignment(), Alloca);
  445. Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, MayNeedSP,
  446. Alloca));
  447. int Index = (int)Objects.size() - NumFixedObjects - 1;
  448. assert(Index >= 0 && "Bad frame index!");
  449. ensureMaxAlignment(Alignment);
  450. return Index;
  451. }
  452. /// CreateSpillStackObject - Create a new statically sized stack object that
  453. /// represents a spill slot, returning a nonnegative identifier to represent
  454. /// it.
  455. ///
  456. int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
  457. unsigned Alignment) {
  458. Alignment = clampStackAlignment(!TFI.isStackRealignable() || !RealignOption,
  459. Alignment, 0,
  460. TFI.getStackAlignment());
  461. CreateStackObject(Size, Alignment, true, false);
  462. int Index = (int)Objects.size() - NumFixedObjects - 1;
  463. ensureMaxAlignment(Alignment);
  464. return Index;
  465. }
  466. /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
  467. /// variable sized object has been created. This must be created whenever a
  468. /// variable sized object is created, whether or not the index returned is
  469. /// actually used.
  470. ///
  471. int MachineFrameInfo::CreateVariableSizedObject(unsigned PrefAlignment,
  472. unsigned MinAlignment, const AllocaInst *Alloca) {
  473. HasVarSizedObjects = true;
  474. unsigned Alignment = clampStackAlignment(
  475. !TFI.isStackRealignable() || !RealignOption,
  476. PrefAlignment, MinAlignment,
  477. TFI.getStackAlignment(), Alloca);
  478. Objects.push_back(StackObject(0, Alignment, 0, false, false, true, 0));
  479. ensureMaxAlignment(Alignment);
  480. return (int)Objects.size()-NumFixedObjects-1;
  481. }
  482. /// CreateFixedObject - Create a new object at a fixed location on the stack.
  483. /// All fixed objects should be created before other objects are created for
  484. /// efficiency. By default, fixed objects are immutable. This returns an
  485. /// index with a negative value.
  486. ///
  487. int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
  488. bool Immutable) {
  489. assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
  490. // The alignment of the frame index can be determined from its offset from
  491. // the incoming frame position. If the frame object is at offset 32 and
  492. // the stack is guaranteed to be 16-byte aligned, then we know that the
  493. // object is 16-byte aligned.
  494. unsigned StackAlign = TFI.getStackAlignment();
  495. unsigned Align = MinAlign(SPOffset, StackAlign);
  496. Align = clampStackAlignment(!TFI.isStackRealignable() || !RealignOption,
  497. Align, 0, TFI.getStackAlignment());
  498. Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
  499. /*isSS*/ false,
  500. /*NeedSP*/ false,
  501. /*Alloca*/ 0));
  502. return -++NumFixedObjects;
  503. }
  504. BitVector
  505. MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
  506. assert(MBB && "MBB must be valid");
  507. const MachineFunction *MF = MBB->getParent();
  508. assert(MF && "MBB must be part of a MachineFunction");
  509. const TargetMachine &TM = MF->getTarget();
  510. const TargetRegisterInfo *TRI = TM.getRegisterInfo();
  511. BitVector BV(TRI->getNumRegs());
  512. // Before CSI is calculated, no registers are considered pristine. They can be
  513. // freely used and PEI will make sure they are saved.
  514. if (!isCalleeSavedInfoValid())
  515. return BV;
  516. for (const uint16_t *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
  517. BV.set(*CSR);
  518. // The entry MBB always has all CSRs pristine.
  519. if (MBB == &MF->front())
  520. return BV;
  521. // On other MBBs the saved CSRs are not pristine.
  522. const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
  523. for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
  524. E = CSI.end(); I != E; ++I)
  525. BV.reset(I->getReg());
  526. return BV;
  527. }
  528. void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
  529. if (Objects.empty()) return;
  530. const TargetFrameLowering *FI = MF.getTarget().getFrameLowering();
  531. int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
  532. OS << "Frame Objects:\n";
  533. for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
  534. const StackObject &SO = Objects[i];
  535. OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
  536. if (SO.Size == ~0ULL) {
  537. OS << "dead\n";
  538. continue;
  539. }
  540. if (SO.Size == 0)
  541. OS << "variable sized";
  542. else
  543. OS << "size=" << SO.Size;
  544. OS << ", align=" << SO.Alignment;
  545. if (i < NumFixedObjects)
  546. OS << ", fixed";
  547. if (i < NumFixedObjects || SO.SPOffset != -1) {
  548. int64_t Off = SO.SPOffset - ValOffset;
  549. OS << ", at location [SP";
  550. if (Off > 0)
  551. OS << "+" << Off;
  552. else if (Off < 0)
  553. OS << Off;
  554. OS << "]";
  555. }
  556. OS << "\n";
  557. }
  558. }
  559. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  560. void MachineFrameInfo::dump(const MachineFunction &MF) const {
  561. print(MF, dbgs());
  562. }
  563. #endif
  564. //===----------------------------------------------------------------------===//
  565. // MachineJumpTableInfo implementation
  566. //===----------------------------------------------------------------------===//
  567. /// getEntrySize - Return the size of each entry in the jump table.
  568. unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
  569. // The size of a jump table entry is 4 bytes unless the entry is just the
  570. // address of a block, in which case it is the pointer size.
  571. switch (getEntryKind()) {
  572. case MachineJumpTableInfo::EK_BlockAddress:
  573. return TD.getPointerSize();
  574. case MachineJumpTableInfo::EK_GPRel64BlockAddress:
  575. return 8;
  576. case MachineJumpTableInfo::EK_GPRel32BlockAddress:
  577. case MachineJumpTableInfo::EK_LabelDifference32:
  578. case MachineJumpTableInfo::EK_Custom32:
  579. return 4;
  580. case MachineJumpTableInfo::EK_Inline:
  581. return 0;
  582. }
  583. llvm_unreachable("Unknown jump table encoding!");
  584. }
  585. /// getEntryAlignment - Return the alignment of each entry in the jump table.
  586. unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
  587. // The alignment of a jump table entry is the alignment of int32 unless the
  588. // entry is just the address of a block, in which case it is the pointer
  589. // alignment.
  590. switch (getEntryKind()) {
  591. case MachineJumpTableInfo::EK_BlockAddress:
  592. return TD.getPointerABIAlignment();
  593. case MachineJumpTableInfo::EK_GPRel64BlockAddress:
  594. return TD.getABIIntegerTypeAlignment(64);
  595. case MachineJumpTableInfo::EK_GPRel32BlockAddress:
  596. case MachineJumpTableInfo::EK_LabelDifference32:
  597. case MachineJumpTableInfo::EK_Custom32:
  598. return TD.getABIIntegerTypeAlignment(32);
  599. case MachineJumpTableInfo::EK_Inline:
  600. return 1;
  601. }
  602. llvm_unreachable("Unknown jump table encoding!");
  603. }
  604. /// createJumpTableIndex - Create a new jump table entry in the jump table info.
  605. ///
  606. unsigned MachineJumpTableInfo::createJumpTableIndex(
  607. const std::vector<MachineBasicBlock*> &DestBBs) {
  608. assert(!DestBBs.empty() && "Cannot create an empty jump table!");
  609. JumpTables.push_back(MachineJumpTableEntry(DestBBs));
  610. return JumpTables.size()-1;
  611. }
  612. /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
  613. /// the jump tables to branch to New instead.
  614. bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
  615. MachineBasicBlock *New) {
  616. assert(Old != New && "Not making a change?");
  617. bool MadeChange = false;
  618. for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
  619. ReplaceMBBInJumpTable(i, Old, New);
  620. return MadeChange;
  621. }
  622. /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
  623. /// the jump table to branch to New instead.
  624. bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
  625. MachineBasicBlock *Old,
  626. MachineBasicBlock *New) {
  627. assert(Old != New && "Not making a change?");
  628. bool MadeChange = false;
  629. MachineJumpTableEntry &JTE = JumpTables[Idx];
  630. for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
  631. if (JTE.MBBs[j] == Old) {
  632. JTE.MBBs[j] = New;
  633. MadeChange = true;
  634. }
  635. return MadeChange;
  636. }
  637. void MachineJumpTableInfo::print(raw_ostream &OS) const {
  638. if (JumpTables.empty()) return;
  639. OS << "Jump Tables:\n";
  640. for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
  641. OS << " jt#" << i << ": ";
  642. for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
  643. OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
  644. }
  645. OS << '\n';
  646. }
  647. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  648. void MachineJumpTableInfo::dump() const { print(dbgs()); }
  649. #endif
  650. //===----------------------------------------------------------------------===//
  651. // MachineConstantPool implementation
  652. //===----------------------------------------------------------------------===//
  653. void MachineConstantPoolValue::anchor() { }
  654. Type *MachineConstantPoolEntry::getType() const {
  655. if (isMachineConstantPoolEntry())
  656. return Val.MachineCPVal->getType();
  657. return Val.ConstVal->getType();
  658. }
  659. unsigned MachineConstantPoolEntry::getRelocationInfo() const {
  660. if (isMachineConstantPoolEntry())
  661. return Val.MachineCPVal->getRelocationInfo();
  662. return Val.ConstVal->getRelocationInfo();
  663. }
  664. MachineConstantPool::~MachineConstantPool() {
  665. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  666. if (Constants[i].isMachineConstantPoolEntry())
  667. delete Constants[i].Val.MachineCPVal;
  668. for (DenseSet<MachineConstantPoolValue*>::iterator I =
  669. MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
  670. I != E; ++I)
  671. delete *I;
  672. }
  673. /// CanShareConstantPoolEntry - Test whether the given two constants
  674. /// can be allocated the same constant pool entry.
  675. static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
  676. const DataLayout *TD) {
  677. // Handle the trivial case quickly.
  678. if (A == B) return true;
  679. // If they have the same type but weren't the same constant, quickly
  680. // reject them.
  681. if (A->getType() == B->getType()) return false;
  682. // We can't handle structs or arrays.
  683. if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
  684. isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
  685. return false;
  686. // For now, only support constants with the same size.
  687. uint64_t StoreSize = TD->getTypeStoreSize(A->getType());
  688. if (StoreSize != TD->getTypeStoreSize(B->getType()) ||
  689. StoreSize > 128)
  690. return false;
  691. Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
  692. // Try constant folding a bitcast of both instructions to an integer. If we
  693. // get two identical ConstantInt's, then we are good to share them. We use
  694. // the constant folding APIs to do this so that we get the benefit of
  695. // DataLayout.
  696. if (isa<PointerType>(A->getType()))
  697. A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
  698. const_cast<Constant*>(A), TD);
  699. else if (A->getType() != IntTy)
  700. A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
  701. const_cast<Constant*>(A), TD);
  702. if (isa<PointerType>(B->getType()))
  703. B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
  704. const_cast<Constant*>(B), TD);
  705. else if (B->getType() != IntTy)
  706. B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
  707. const_cast<Constant*>(B), TD);
  708. return A == B;
  709. }
  710. /// getConstantPoolIndex - Create a new entry in the constant pool or return
  711. /// an existing one. User must specify the log2 of the minimum required
  712. /// alignment for the object.
  713. ///
  714. unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
  715. unsigned Alignment) {
  716. assert(Alignment && "Alignment must be specified!");
  717. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  718. // Check to see if we already have this constant.
  719. //
  720. // FIXME, this could be made much more efficient for large constant pools.
  721. for (unsigned i = 0, e = Constants.size(); i != e; ++i)
  722. if (!Constants[i].isMachineConstantPoolEntry() &&
  723. CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
  724. if ((unsigned)Constants[i].getAlignment() < Alignment)
  725. Constants[i].Alignment = Alignment;
  726. return i;
  727. }
  728. Constants.push_back(MachineConstantPoolEntry(C, Alignment));
  729. return Constants.size()-1;
  730. }
  731. unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
  732. unsigned Alignment) {
  733. assert(Alignment && "Alignment must be specified!");
  734. if (Alignment > PoolAlignment) PoolAlignment = Alignment;
  735. // Check to see if we already have this constant.
  736. //
  737. // FIXME, this could be made much more efficient for large constant pools.
  738. int Idx = V->getExistingMachineCPValue(this, Alignment);
  739. if (Idx != -1) {
  740. MachineCPVsSharingEntries.insert(V);
  741. return (unsigned)Idx;
  742. }
  743. Constants.push_back(MachineConstantPoolEntry(V, Alignment));
  744. return Constants.size()-1;
  745. }
  746. void MachineConstantPool::print(raw_ostream &OS) const {
  747. if (Constants.empty()) return;
  748. OS << "Constant Pool:\n";
  749. for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
  750. OS << " cp#" << i << ": ";
  751. if (Constants[i].isMachineConstantPoolEntry())
  752. Constants[i].Val.MachineCPVal->print(OS);
  753. else
  754. OS << *(const Value*)Constants[i].Val.ConstVal;
  755. OS << ", align=" << Constants[i].getAlignment();
  756. OS << "\n";
  757. }
  758. }
  759. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  760. void MachineConstantPool::dump() const { print(dbgs()); }
  761. #endif