MachineFunction.cpp revision 193323
1168404Spjd//===-- MachineFunction.cpp -----------------------------------------------===//
2168404Spjd//
3168404Spjd//                     The LLVM Compiler Infrastructure
4168404Spjd//
5168404Spjd// This file is distributed under the University of Illinois Open Source
6168404Spjd// License. See LICENSE.TXT for details.
7168404Spjd//
8168404Spjd//===----------------------------------------------------------------------===//
9168404Spjd//
10168404Spjd// Collect native machine code information for a function.  This allows
11168404Spjd// target-specific information about the generated code to be stored with each
12168404Spjd// function.
13168404Spjd//
14168404Spjd//===----------------------------------------------------------------------===//
15168404Spjd
16168404Spjd#include "llvm/DerivedTypes.h"
17168404Spjd#include "llvm/CodeGen/MachineConstantPool.h"
18168404Spjd#include "llvm/CodeGen/MachineFunctionPass.h"
19168404Spjd#include "llvm/CodeGen/MachineFrameInfo.h"
20168404Spjd#include "llvm/CodeGen/MachineInstr.h"
21168404Spjd#include "llvm/CodeGen/MachineJumpTableInfo.h"
22219089Spjd#include "llvm/CodeGen/MachineRegisterInfo.h"
23264669Sdelphij#include "llvm/CodeGen/Passes.h"
24168404Spjd#include "llvm/Target/TargetData.h"
25168404Spjd#include "llvm/Target/TargetMachine.h"
26219089Spjd#include "llvm/Target/TargetFrameInfo.h"
27168404Spjd#include "llvm/Function.h"
28168404Spjd#include "llvm/Instructions.h"
29168404Spjd#include "llvm/Support/Compiler.h"
30168404Spjd#include "llvm/Support/GraphWriter.h"
31168404Spjd#include "llvm/Support/raw_ostream.h"
32168404Spjd#include "llvm/ADT/STLExtras.h"
33168404Spjd#include "llvm/Config/config.h"
34168404Spjd#include <fstream>
35219089Spjd#include <sstream>
36168404Spjdusing namespace llvm;
37185029Spjd
38185029Spjdbool MachineFunctionPass::runOnFunction(Function &F) {
39185029Spjd  // Do not codegen any 'available_externally' functions at all, they have
40168404Spjd  // definitions outside the translation unit.
41219089Spjd  if (F.hasAvailableExternallyLinkage())
42168404Spjd    return false;
43219089Spjd
44219089Spjd  return runOnMachineFunction(MachineFunction::get(&F));
45219089Spjd}
46219089Spjd
47219089Spjdnamespace {
48219089Spjd  struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
49219089Spjd    static char ID;
50168404Spjd
51219089Spjd    std::ostream *OS;
52219089Spjd    const std::string Banner;
53219089Spjd
54219089Spjd    Printer (std::ostream *os, const std::string &banner)
55219089Spjd      : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
56219089Spjd
57219089Spjd    const char *getPassName() const { return "MachineFunction Printer"; }
58219089Spjd
59219089Spjd    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60219089Spjd      AU.setPreservesAll();
61219089Spjd    }
62219089Spjd
63219089Spjd    bool runOnMachineFunction(MachineFunction &MF) {
64219089Spjd      (*OS) << Banner;
65219089Spjd      MF.print (*OS);
66219089Spjd      return false;
67219089Spjd    }
68219089Spjd  };
69185029Spjd  char Printer::ID = 0;
70219089Spjd}
71185029Spjd
72219089Spjd/// Returns a newly-created MachineFunction Printer pass. The default output
73219089Spjd/// stream is std::cerr; the default banner is empty.
74185029Spjd///
75219089SpjdFunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
76219089Spjd                                                     const std::string &Banner){
77219089Spjd  return new Printer(OS, Banner);
78219089Spjd}
79219089Spjd
80219089Spjdnamespace {
81219089Spjd  struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
82185029Spjd    static char ID;
83219089Spjd    Deleter() : MachineFunctionPass(&ID) {}
84219089Spjd
85219089Spjd    const char *getPassName() const { return "Machine Code Deleter"; }
86219089Spjd
87219089Spjd    bool runOnMachineFunction(MachineFunction &MF) {
88219089Spjd      // Delete the annotation from the function now.
89219089Spjd      MachineFunction::destruct(MF.getFunction());
90219089Spjd      return true;
91219089Spjd    }
92219089Spjd  };
93219089Spjd  char Deleter::ID = 0;
94219089Spjd}
95219089Spjd
96219089Spjd/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
97219089Spjd/// the current function, which should happen after the function has been
98219089Spjd/// emitted to a .s file or to memory.
99219089SpjdFunctionPass *llvm::createMachineCodeDeleter() {
100219089Spjd  return new Deleter();
101219089Spjd}
102219089Spjd
103219089Spjd
104219089Spjd
105219089Spjd//===---------------------------------------------------------------------===//
106219089Spjd// MachineFunction implementation
107219089Spjd//===---------------------------------------------------------------------===//
108219089Spjd
109219089Spjdvoid ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
110219089Spjd  MBB->getParent()->DeleteMachineBasicBlock(MBB);
111219089Spjd}
112219089Spjd
113219089SpjdMachineFunction::MachineFunction(const Function *F,
114219089Spjd                                 const TargetMachine &TM)
115219089Spjd  : Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
116219089Spjd    Fn(F), Target(TM) {
117185029Spjd  if (TM.getRegisterInfo())
118185029Spjd    RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
119219089Spjd                  MachineRegisterInfo(*TM.getRegisterInfo());
120219089Spjd  else
121219089Spjd    RegInfo = 0;
122185029Spjd  MFInfo = 0;
123185029Spjd  FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
124219089Spjd                  MachineFrameInfo(*TM.getFrameInfo());
125185029Spjd  ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
126219089Spjd                     MachineConstantPool(TM.getTargetData());
127185029Spjd
128185029Spjd  // Set up jump table.
129185029Spjd  const TargetData &TD = *TM.getTargetData();
130185029Spjd  bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
131185029Spjd  unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
132185029Spjd  unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
133185029Spjd                             : TD.getPointerABIAlignment();
134185029Spjd  JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
135219089Spjd                      MachineJumpTableInfo(EntrySize, Alignment);
136219089Spjd}
137185029Spjd
138185029SpjdMachineFunction::~MachineFunction() {
139185029Spjd  BasicBlocks.clear();
140185029Spjd  InstructionRecycler.clear(Allocator);
141185029Spjd  BasicBlockRecycler.clear(Allocator);
142219089Spjd  if (RegInfo)
143219089Spjd    RegInfo->~MachineRegisterInfo();        Allocator.Deallocate(RegInfo);
144185029Spjd  if (MFInfo) {
145185029Spjd    MFInfo->~MachineFunctionInfo();       Allocator.Deallocate(MFInfo);
146185029Spjd  }
147185029Spjd  FrameInfo->~MachineFrameInfo();         Allocator.Deallocate(FrameInfo);
148185029Spjd  ConstantPool->~MachineConstantPool();   Allocator.Deallocate(ConstantPool);
149185029Spjd  JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
150185029Spjd}
151219089Spjd
152219089Spjd
153185029Spjd/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
154185029Spjd/// recomputes them.  This guarantees that the MBB numbers are sequential,
155185029Spjd/// dense, and match the ordering of the blocks within the function.  If a
156185029Spjd/// specific MachineBasicBlock is specified, only that block and those after
157185029Spjd/// it are renumbered.
158185029Spjdvoid MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
159219089Spjd  if (empty()) { MBBNumbering.clear(); return; }
160185029Spjd  MachineFunction::iterator MBBI, E = end();
161185029Spjd  if (MBB == 0)
162219089Spjd    MBBI = begin();
163185029Spjd  else
164185029Spjd    MBBI = MBB;
165185029Spjd
166185029Spjd  // Figure out the block number this should have.
167185029Spjd  unsigned BlockNo = 0;
168185029Spjd  if (MBBI != begin())
169185029Spjd    BlockNo = prior(MBBI)->getNumber()+1;
170185029Spjd
171185029Spjd  for (; MBBI != E; ++MBBI, ++BlockNo) {
172185029Spjd    if (MBBI->getNumber() != (int)BlockNo) {
173219089Spjd      // Remove use of the old number.
174185029Spjd      if (MBBI->getNumber() != -1) {
175185029Spjd        assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
176185029Spjd               "MBB number mismatch!");
177185029Spjd        MBBNumbering[MBBI->getNumber()] = 0;
178219089Spjd      }
179219089Spjd
180219089Spjd      // If BlockNo is already taken, set that block's number to -1.
181185029Spjd      if (MBBNumbering[BlockNo])
182185029Spjd        MBBNumbering[BlockNo]->setNumber(-1);
183219089Spjd
184185029Spjd      MBBNumbering[BlockNo] = MBBI;
185185029Spjd      MBBI->setNumber(BlockNo);
186185029Spjd    }
187219089Spjd  }
188219089Spjd
189185029Spjd  // Okay, all the blocks are renumbered.  If we have compactified the block
190185029Spjd  // numbering, shrink MBBNumbering now.
191185029Spjd  assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
192185029Spjd  MBBNumbering.resize(BlockNo);
193185029Spjd}
194219089Spjd
195219089Spjd/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
196185029Spjd/// of `new MachineInstr'.
197185029Spjd///
198219089SpjdMachineInstr *
199185029SpjdMachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
200185029Spjd                                    DebugLoc DL, bool NoImp) {
201185029Spjd  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
202219089Spjd    MachineInstr(TID, DL, NoImp);
203219089Spjd}
204219089Spjd
205219089Spjd/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
206219089Spjd/// 'Orig' instruction, identical in all ways except the the instruction
207219089Spjd/// has no parent, prev, or next.
208219089Spjd///
209219089SpjdMachineInstr *
210219089SpjdMachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
211219089Spjd  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
212219089Spjd             MachineInstr(*this, *Orig);
213219089Spjd}
214219089Spjd
215219089Spjd/// DeleteMachineInstr - Delete the given MachineInstr.
216219089Spjd///
217219089Spjdvoid
218168404SpjdMachineFunction::DeleteMachineInstr(MachineInstr *MI) {
219168404Spjd  // Clear the instructions memoperands. This must be done manually because
220168404Spjd  // the instruction's parent pointer is now null, so it can't properly
221168404Spjd  // deallocate them on its own.
222168404Spjd  MI->clearMemOperands(*this);
223168404Spjd
224185029Spjd  MI->~MachineInstr();
225168404Spjd  InstructionRecycler.Deallocate(Allocator, MI);
226168404Spjd}
227168404Spjd
228168404Spjd/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
229168404Spjd/// instead of `new MachineBasicBlock'.
230168404Spjd///
231168404SpjdMachineBasicBlock *
232168404SpjdMachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
233168404Spjd  return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
234168404Spjd             MachineBasicBlock(*this, bb);
235168404Spjd}
236168404Spjd
237168404Spjd/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
238168404Spjd///
239168404Spjdvoid
240168404SpjdMachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
241168404Spjd  assert(MBB->getParent() == this && "MBB parent mismatch!");
242168404Spjd  MBB->~MachineBasicBlock();
243168404Spjd  BasicBlockRecycler.Deallocate(Allocator, MBB);
244168404Spjd}
245168404Spjd
246168404Spjdvoid MachineFunction::dump() const {
247168404Spjd  print(*cerr.stream());
248168404Spjd}
249168404Spjd
250168404Spjdvoid MachineFunction::print(std::ostream &OS) const {
251168404Spjd  OS << "# Machine code for " << Fn->getName () << "():\n";
252168404Spjd
253168404Spjd  // Print Frame Information
254168404Spjd  FrameInfo->print(*this, OS);
255168404Spjd
256168404Spjd  // Print JumpTable Information
257168404Spjd  JumpTableInfo->print(OS);
258168404Spjd
259219089Spjd  // Print Constant Pool
260168404Spjd  {
261219089Spjd    raw_os_ostream OSS(OS);
262168404Spjd    ConstantPool->print(OSS);
263168404Spjd  }
264168404Spjd
265168404Spjd  const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
266197150Spjd
267219089Spjd  if (RegInfo && !RegInfo->livein_empty()) {
268168404Spjd    OS << "Live Ins:";
269168404Spjd    for (MachineRegisterInfo::livein_iterator
270197150Spjd         I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
271168404Spjd      if (TRI)
272168404Spjd        OS << " " << TRI->getName(I->first);
273168404Spjd      else
274168404Spjd        OS << " Reg #" << I->first;
275168404Spjd
276168404Spjd      if (I->second)
277168404Spjd        OS << " in VR#" << I->second << " ";
278219089Spjd    }
279219089Spjd    OS << "\n";
280197150Spjd  }
281197150Spjd  if (RegInfo && !RegInfo->liveout_empty()) {
282197150Spjd    OS << "Live Outs:";
283197150Spjd    for (MachineRegisterInfo::liveout_iterator
284197150Spjd         I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
285197150Spjd      if (TRI)
286168404Spjd        OS << " " << TRI->getName(*I);
287168404Spjd      else
288168404Spjd        OS << " Reg #" << *I;
289185029Spjd    OS << "\n";
290168404Spjd  }
291168404Spjd
292168404Spjd  for (const_iterator BB = begin(); BB != end(); ++BB)
293168404Spjd    BB->print(OS);
294185029Spjd
295168404Spjd  OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
296185029Spjd}
297185029Spjd
298168404Spjd/// CFGOnly flag - This is used to control whether or not the CFG graph printer
299185029Spjd/// prints out the contents of basic blocks or not.  This is acceptable because
300219089Spjd/// this code is only really used for debugging purposes.
301168404Spjd///
302185029Spjdstatic bool CFGOnly = false;
303168404Spjd
304168404Spjdnamespace llvm {
305168404Spjd  template<>
306185029Spjd  struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
307219089Spjd    static std::string getGraphName(const MachineFunction *F) {
308219089Spjd      return "CFG for '" + F->getFunction()->getName() + "' function";
309168404Spjd    }
310168404Spjd
311185029Spjd    static std::string getNodeLabel(const MachineBasicBlock *Node,
312185029Spjd                                    const MachineFunction *Graph) {
313185029Spjd      if (CFGOnly && Node->getBasicBlock() &&
314185029Spjd          !Node->getBasicBlock()->getName().empty())
315168404Spjd        return Node->getBasicBlock()->getName() + ":";
316168404Spjd
317168404Spjd      std::ostringstream Out;
318168404Spjd      if (CFGOnly) {
319168404Spjd        Out << Node->getNumber() << ':';
320168404Spjd        return Out.str();
321168404Spjd      }
322168404Spjd
323168404Spjd      Node->print(Out);
324168404Spjd
325168404Spjd      std::string OutStr = Out.str();
326168404Spjd      if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
327168404Spjd
328168404Spjd      // Process string output to make it nicer...
329168404Spjd      for (unsigned i = 0; i != OutStr.length(); ++i)
330168404Spjd        if (OutStr[i] == '\n') {                            // Left justify
331219089Spjd          OutStr[i] = '\\';
332168404Spjd          OutStr.insert(OutStr.begin()+i+1, 'l');
333168404Spjd        }
334168404Spjd      return OutStr;
335168404Spjd    }
336219089Spjd  };
337168404Spjd}
338168404Spjd
339168404Spjdvoid MachineFunction::viewCFG() const
340168404Spjd{
341168404Spjd#ifndef NDEBUG
342168404Spjd  ViewGraph(this, "mf" + getFunction()->getName());
343168404Spjd#else
344168404Spjd  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
345168404Spjd       << "systems with Graphviz or gv!\n";
346168404Spjd#endif // NDEBUG
347168404Spjd}
348168404Spjd
349168404Spjdvoid MachineFunction::viewCFGOnly() const
350168404Spjd{
351168404Spjd  CFGOnly = true;
352168404Spjd  viewCFG();
353168404Spjd  CFGOnly = false;
354168404Spjd}
355168404Spjd
356168404Spjd// The next two methods are used to construct and to retrieve
357168404Spjd// the MachineCodeForFunction object for the given function.
358168404Spjd// construct() -- Allocates and initializes for a given function and target
359168404Spjd// get()       -- Returns a handle to the object.
360168404Spjd//                This should not be called before "construct()"
361168404Spjd//                for a given Function.
362168404Spjd//
363168404SpjdMachineFunction&
364168404SpjdMachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
365168404Spjd{
366168404Spjd  AnnotationID MF_AID =
367168404Spjd                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
368168404Spjd  assert(Fn->getAnnotation(MF_AID) == 0 &&
369168404Spjd         "Object already exists for this function!");
370168404Spjd  MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
371168404Spjd  Fn->addAnnotation(mcInfo);
372168404Spjd  return *mcInfo;
373168404Spjd}
374168404Spjd
375219089Spjdvoid MachineFunction::destruct(const Function *Fn) {
376168404Spjd  AnnotationID MF_AID =
377168404Spjd                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
378168404Spjd  bool Deleted = Fn->deleteAnnotation(MF_AID);
379168404Spjd  assert(Deleted && "Machine code did not exist for function!");
380168404Spjd  Deleted = Deleted; // silence warning when no assertions.
381185029Spjd}
382219089Spjd
383264669SdelphijMachineFunction& MachineFunction::get(const Function *F)
384168404Spjd{
385168404Spjd  AnnotationID MF_AID =
386168404Spjd                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
387168404Spjd  MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
388168404Spjd  assert(mc && "Call construct() method first to allocate the object");
389168404Spjd  return *mc;
390168404Spjd}
391168404Spjd
392168404Spjd/// addLiveIn - Add the specified physical register as a live-in value and
393168404Spjd/// create a corresponding virtual register for it.
394168404Spjdunsigned MachineFunction::addLiveIn(unsigned PReg,
395168404Spjd                                    const TargetRegisterClass *RC) {
396172443Spjd  assert(RC->contains(PReg) && "Not the correct regclass!");
397172443Spjd  unsigned VReg = getRegInfo().createVirtualRegister(RC);
398168404Spjd  getRegInfo().addLiveIn(PReg, VReg);
399168404Spjd  return VReg;
400168404Spjd}
401168404Spjd
402168404Spjd/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
403168404Spjd/// source file, line, and column. If none currently exists, create a new
404168404Spjd/// DebugLocTuple, and insert it into the DebugIdMap.
405168404Spjdunsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
406185029Spjd                                                unsigned Line, unsigned Col) {
407168404Spjd  DebugLocTuple Tuple(CompileUnit, Line, Col);
408168404Spjd  DenseMap<DebugLocTuple, unsigned>::iterator II
409168404Spjd    = DebugLocInfo.DebugIdMap.find(Tuple);
410168404Spjd  if (II != DebugLocInfo.DebugIdMap.end())
411168404Spjd    return II->second;
412168404Spjd  // Add a new tuple.
413168404Spjd  unsigned Id = DebugLocInfo.DebugLocations.size();
414168404Spjd  DebugLocInfo.DebugLocations.push_back(Tuple);
415185029Spjd  DebugLocInfo.DebugIdMap[Tuple] = Id;
416185029Spjd  return Id;
417185029Spjd}
418185029Spjd
419219089Spjd/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
420197150SpjdDebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
421197150Spjd  unsigned Idx = DL.getIndex();
422197150Spjd  assert(Idx < DebugLocInfo.DebugLocations.size() &&
423197172Spjd         "Invalid index into debug locations!");
424197150Spjd  return DebugLocInfo.DebugLocations[Idx];
425197150Spjd}
426185029Spjd
427168404Spjd//===----------------------------------------------------------------------===//
428168404Spjd//  MachineFrameInfo implementation
429168404Spjd//===----------------------------------------------------------------------===//
430168404Spjd
431185029Spjd/// CreateFixedObject - Create a new object at a fixed location on the stack.
432168404Spjd/// All fixed objects should be created before other objects are created for
433168404Spjd/// efficiency. By default, fixed objects are immutable. This returns an
434168404Spjd/// index with a negative value.
435168404Spjd///
436168404Spjdint MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
437168404Spjd                                        bool Immutable) {
438168404Spjd  assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
439168404Spjd  Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
440168404Spjd  return -++NumFixedObjects;
441168404Spjd}
442168404Spjd
443168404Spjd
444168404Spjdvoid MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
445168404Spjd  const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
446168404Spjd  int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
447168404Spjd
448168404Spjd  for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
449168404Spjd    const StackObject &SO = Objects[i];
450168404Spjd    OS << "  <fi#" << (int)(i-NumFixedObjects) << ">: ";
451168404Spjd    if (SO.Size == ~0ULL) {
452168404Spjd      OS << "dead\n";
453168404Spjd      continue;
454168404Spjd    }
455168404Spjd    if (SO.Size == 0)
456168404Spjd      OS << "variable sized";
457168404Spjd    else
458185029Spjd      OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
459168404Spjd    OS << " alignment is " << SO.Alignment << " byte"
460168404Spjd       << (SO.Alignment != 1 ? "s," : ",");
461168404Spjd
462168404Spjd    if (i < NumFixedObjects)
463168404Spjd      OS << " fixed";
464168404Spjd    if (i < NumFixedObjects || SO.SPOffset != -1) {
465168404Spjd      int64_t Off = SO.SPOffset - ValOffset;
466168404Spjd      OS << " at location [SP";
467219089Spjd      if (Off > 0)
468168404Spjd        OS << "+" << Off;
469168404Spjd      else if (Off < 0)
470168404Spjd        OS << Off;
471168404Spjd      OS << "]";
472168404Spjd    }
473168404Spjd    OS << "\n";
474168404Spjd  }
475236884Smm
476168404Spjd  if (HasVarSizedObjects)
477168404Spjd    OS << "  Stack frame contains variable sized objects\n";
478168404Spjd}
479168404Spjd
480168404Spjdvoid MachineFrameInfo::dump(const MachineFunction &MF) const {
481168404Spjd  print(MF, *cerr.stream());
482168404Spjd}
483168404Spjd
484168404Spjd
485168404Spjd//===----------------------------------------------------------------------===//
486168404Spjd//  MachineJumpTableInfo implementation
487168404Spjd//===----------------------------------------------------------------------===//
488168404Spjd
489168404Spjd/// getJumpTableIndex - Create a new jump table entry in the jump table info
490168404Spjd/// or return an existing one.
491168404Spjd///
492168404Spjdunsigned MachineJumpTableInfo::getJumpTableIndex(
493168404Spjd                               const std::vector<MachineBasicBlock*> &DestBBs) {
494168404Spjd  assert(!DestBBs.empty() && "Cannot create an empty jump table!");
495168404Spjd  for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
496168404Spjd    if (JumpTables[i].MBBs == DestBBs)
497168404Spjd      return i;
498168404Spjd
499168404Spjd  JumpTables.push_back(MachineJumpTableEntry(DestBBs));
500168404Spjd  return JumpTables.size()-1;
501168404Spjd}
502168404Spjd
503168404Spjd/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
504168404Spjd/// the jump tables to branch to New instead.
505168404Spjdbool
506168404SpjdMachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
507168404Spjd                                             MachineBasicBlock *New) {
508168404Spjd  assert(Old != New && "Not making a change?");
509168404Spjd  bool MadeChange = false;
510185029Spjd  for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
511168404Spjd    MachineJumpTableEntry &JTE = JumpTables[i];
512168404Spjd    for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
513168404Spjd      if (JTE.MBBs[j] == Old) {
514168404Spjd        JTE.MBBs[j] = New;
515168404Spjd        MadeChange = true;
516168404Spjd      }
517219089Spjd  }
518168404Spjd  return MadeChange;
519168404Spjd}
520240415Smm
521168404Spjdvoid MachineJumpTableInfo::print(std::ostream &OS) const {
522168404Spjd  // FIXME: this is lame, maybe we could print out the MBB numbers or something
523168404Spjd  // like {1, 2, 4, 5, 3, 0}
524168404Spjd  for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
525168404Spjd    OS << "  <jt#" << i << "> has " << JumpTables[i].MBBs.size()
526168404Spjd       << " entries\n";
527168404Spjd  }
528168404Spjd}
529168404Spjd
530168404Spjdvoid MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
531168404Spjd
532168404Spjd
533168404Spjd//===----------------------------------------------------------------------===//
534168404Spjd//  MachineConstantPool implementation
535168404Spjd//===----------------------------------------------------------------------===//
536185029Spjd
537219089Spjdconst Type *MachineConstantPoolEntry::getType() const {
538168404Spjd  if (isMachineConstantPoolEntry())
539168404Spjd      return Val.MachineCPVal->getType();
540219089Spjd  return Val.ConstVal->getType();
541219089Spjd}
542185029Spjd
543168404SpjdMachineConstantPool::~MachineConstantPool() {
544168404Spjd  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
545168404Spjd    if (Constants[i].isMachineConstantPoolEntry())
546168404Spjd      delete Constants[i].Val.MachineCPVal;
547168404Spjd}
548168404Spjd
549168404Spjd/// getConstantPoolIndex - Create a new entry in the constant pool or return
550168404Spjd/// an existing one.  User must specify the log2 of the minimum required
551219089Spjd/// alignment for the object.
552219089Spjd///
553219089Spjdunsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
554219089Spjd                                                   unsigned Alignment) {
555219089Spjd  assert(Alignment && "Alignment must be specified!");
556219089Spjd  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
557219089Spjd
558185029Spjd  // Check to see if we already have this constant.
559168404Spjd  //
560168404Spjd  // FIXME, this could be made much more efficient for large constant pools.
561168404Spjd  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
562185029Spjd    if (Constants[i].Val.ConstVal == C &&
563168404Spjd        (Constants[i].getAlignment() & (Alignment - 1)) == 0)
564168404Spjd      return i;
565219089Spjd
566168404Spjd  Constants.push_back(MachineConstantPoolEntry(C, Alignment));
567168404Spjd  return Constants.size()-1;
568168404Spjd}
569185029Spjd
570168404Spjdunsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
571168404Spjd                                                   unsigned Alignment) {
572168404Spjd  assert(Alignment && "Alignment must be specified!");
573168404Spjd  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
574185029Spjd
575185029Spjd  // Check to see if we already have this constant.
576185029Spjd  //
577185029Spjd  // FIXME, this could be made much more efficient for large constant pools.
578185029Spjd  int Idx = V->getExistingMachineCPValue(this, Alignment);
579185029Spjd  if (Idx != -1)
580168404Spjd    return (unsigned)Idx;
581168404Spjd
582185029Spjd  Constants.push_back(MachineConstantPoolEntry(V, Alignment));
583185029Spjd  return Constants.size()-1;
584168404Spjd}
585168404Spjd
586259813Sdelphijvoid MachineConstantPool::print(raw_ostream &OS) const {
587219089Spjd  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
588219089Spjd    OS << "  <cp#" << i << "> is";
589168404Spjd    if (Constants[i].isMachineConstantPoolEntry())
590168404Spjd      Constants[i].Val.MachineCPVal->print(OS);
591168404Spjd    else
592168404Spjd      OS << *(Value*)Constants[i].Val.ConstVal;
593219089Spjd    OS << " , alignment=" << Constants[i].getAlignment();
594168404Spjd    OS << "\n";
595168404Spjd  }
596168404Spjd}
597168404Spjd
598168404Spjdvoid MachineConstantPool::dump() const { print(errs()); }
599236884Smm