1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/SSARegMap.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineJumpTableInfo.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Support/LeakDetector.h"
29 #include "llvm/Support/GraphWriter.h"
30 #include "llvm/Support/Visibility.h"
31 #include "llvm/Config/config.h"
38 static AnnotationID MF_AID(
39 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
43 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
45 const std::string Banner;
47 Printer (std::ostream *_OS, const std::string &_Banner) :
48 OS (_OS), Banner (_Banner) { }
50 const char *getPassName() const { return "MachineFunction Printer"; }
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 bool runOnMachineFunction(MachineFunction &MF) {
64 /// Returns a newly-created MachineFunction Printer pass. The default output
65 /// stream is std::cerr; the default banner is empty.
67 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
68 const std::string &Banner){
69 return new Printer(OS, Banner);
73 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
74 const char *getPassName() const { return "Machine Code Deleter"; }
76 bool runOnMachineFunction(MachineFunction &MF) {
77 // Delete the annotation from the function now.
78 MachineFunction::destruct(MF.getFunction());
84 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
85 /// the current function, which should happen after the function has been
86 /// emitted to a .s file or to memory.
87 FunctionPass *llvm::createMachineCodeDeleter() {
93 //===---------------------------------------------------------------------===//
94 // MachineFunction implementation
95 //===---------------------------------------------------------------------===//
97 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
98 MachineBasicBlock* dummy = new MachineBasicBlock();
99 LeakDetector::removeGarbageObject(dummy);
103 void ilist_traits<MachineBasicBlock>::transferNodesFromList(
104 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
105 ilist_iterator<MachineBasicBlock> first,
106 ilist_iterator<MachineBasicBlock> last) {
107 if (Parent != toList.Parent)
108 for (; first != last; ++first)
109 first->Parent = toList.Parent;
112 MachineFunction::MachineFunction(const Function *F,
113 const TargetMachine &TM)
114 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
115 SSARegMapping = new SSARegMap();
117 FrameInfo = new MachineFrameInfo();
118 ConstantPool = new MachineConstantPool(TM.getTargetData());
119 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
120 BasicBlocks.Parent = this;
123 MachineFunction::~MachineFunction() {
125 delete SSARegMapping;
129 delete JumpTableInfo;
130 delete[] UsedPhysRegs;
133 void MachineFunction::dump() const { print(std::cerr); }
135 void MachineFunction::print(std::ostream &OS) const {
136 OS << "# Machine code for " << Fn->getName () << "():\n";
138 // Print Frame Information
139 getFrameInfo()->print(*this, OS);
141 // Print JumpTable Information
142 getJumpTableInfo()->print(OS);
144 // Print Constant Pool
145 getConstantPool()->print(OS);
147 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
149 if (livein_begin() != livein_end()) {
151 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
153 OS << " " << MRI->getName(I->first);
155 OS << " Reg #" << I->first;
158 OS << " in VR#" << I->second << " ";
162 if (liveout_begin() != liveout_end()) {
164 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
166 OS << " " << MRI->getName(*I);
168 OS << " Reg #" << *I;
172 for (const_iterator BB = begin(); BB != end(); ++BB)
175 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
178 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
179 /// prints out the contents of basic blocks or not. This is acceptable because
180 /// this code is only really used for debugging purposes.
182 static bool CFGOnly = false;
186 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
187 static std::string getGraphName(const MachineFunction *F) {
188 return "CFG for '" + F->getFunction()->getName() + "' function";
191 static std::string getNodeLabel(const MachineBasicBlock *Node,
192 const MachineFunction *Graph) {
193 if (CFGOnly && Node->getBasicBlock() &&
194 !Node->getBasicBlock()->getName().empty())
195 return Node->getBasicBlock()->getName() + ":";
197 std::ostringstream Out;
199 Out << Node->getNumber() << ':';
205 std::string OutStr = Out.str();
206 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
208 // Process string output to make it nicer...
209 for (unsigned i = 0; i != OutStr.length(); ++i)
210 if (OutStr[i] == '\n') { // Left justify
212 OutStr.insert(OutStr.begin()+i+1, 'l');
219 void MachineFunction::viewCFG() const
222 ViewGraph(this, "mf" + getFunction()->getName());
224 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
225 << "systems with Graphviz or gv!\n";
229 void MachineFunction::viewCFGOnly() const
236 // The next two methods are used to construct and to retrieve
237 // the MachineCodeForFunction object for the given function.
238 // construct() -- Allocates and initializes for a given function and target
239 // get() -- Returns a handle to the object.
240 // This should not be called before "construct()"
241 // for a given Function.
244 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
246 assert(Fn->getAnnotation(MF_AID) == 0 &&
247 "Object already exists for this function!");
248 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
249 Fn->addAnnotation(mcInfo);
253 void MachineFunction::destruct(const Function *Fn) {
254 bool Deleted = Fn->deleteAnnotation(MF_AID);
255 assert(Deleted && "Machine code did not exist for function!");
258 MachineFunction& MachineFunction::get(const Function *F)
260 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
261 assert(mc && "Call construct() method first to allocate the object");
265 void MachineFunction::clearSSARegMap() {
266 delete SSARegMapping;
270 //===----------------------------------------------------------------------===//
271 // MachineFrameInfo implementation
272 //===----------------------------------------------------------------------===//
274 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
275 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
277 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
278 const StackObject &SO = Objects[i];
279 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
281 OS << "variable sized";
283 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
284 OS << " alignment is " << SO.Alignment << " byte"
285 << (SO.Alignment != 1 ? "s," : ",");
287 if (i < NumFixedObjects)
289 if (i < NumFixedObjects || SO.SPOffset != -1) {
290 int Off = SO.SPOffset - ValOffset;
291 OS << " at location [SP";
301 if (HasVarSizedObjects)
302 OS << " Stack frame contains variable sized objects\n";
305 void MachineFrameInfo::dump(const MachineFunction &MF) const {
306 print(MF, std::cerr);
310 //===----------------------------------------------------------------------===//
311 // MachineJumpTableInfo implementation
312 //===----------------------------------------------------------------------===//
314 /// getJumpTableIndex - Create a new jump table entry in the jump table info
315 /// or return an existing one.
317 unsigned MachineJumpTableInfo::getJumpTableIndex(
318 std::vector<MachineBasicBlock*> &DestBBs) {
319 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
320 if (JumpTables[i].MBBs == DestBBs)
323 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
324 return JumpTables.size()-1;
328 void MachineJumpTableInfo::print(std::ostream &OS) const {
329 // FIXME: this is lame, maybe we could print out the MBB numbers or something
330 // like {1, 2, 4, 5, 3, 0}
331 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
332 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
337 unsigned MachineJumpTableInfo::getEntrySize() const {
338 return TD->getPointerSize();
341 unsigned MachineJumpTableInfo::getAlignment() const {
342 return TD->getPointerAlignment();
345 void MachineJumpTableInfo::dump() const { print(std::cerr); }
348 //===----------------------------------------------------------------------===//
349 // MachineConstantPool implementation
350 //===----------------------------------------------------------------------===//
352 /// getConstantPoolIndex - Create a new entry in the constant pool or return
353 /// an existing one. User must specify an alignment in bytes for the object.
355 unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
356 unsigned Alignment) {
357 assert(Alignment && "Alignment must be specified!");
358 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
360 // Check to see if we already have this constant.
362 // FIXME, this could be made much more efficient for large constant pools.
363 unsigned AlignMask = (1 << Alignment)-1;
364 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
365 if (Constants[i].Val == C && (Constants[i].Offset & AlignMask) == 0)
369 if (!Constants.empty()) {
370 Offset = Constants.back().Offset;
371 Offset += TD->getTypeSize(Constants.back().Val->getType());
372 Offset = (Offset+AlignMask)&~AlignMask;
375 Constants.push_back(MachineConstantPoolEntry(C, Offset));
376 return Constants.size()-1;
380 void MachineConstantPool::print(std::ostream &OS) const {
381 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
382 OS << " <cp #" << i << "> is" << *(Value*)Constants[i].Val;
383 OS << " , offset=" << Constants[i].Offset;
388 void MachineConstantPool::dump() const { print(std::cerr); }