blob: f19ea0918f0b1f521620f8bc22544335791c6760 [file] [log] [blame]
Chris Lattner8494d082002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00009//
Chris Lattner8494d082002-10-28 01:16:38 +000010// 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//===----------------------------------------------------------------------===//
Chris Lattnereda6bd72002-02-03 07:54:50 +000015
Chris Lattner4cbb97b2003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner448fb452002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner448fb452002-12-25 05:03:22 +000018#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerca4362f2002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc6807e82003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4cbb97b2003-12-20 10:20:58 +000021#include "llvm/CodeGen/Passes.h"
Chris Lattnereda6bd72002-02-03 07:54:50 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner871e5912002-12-28 21:00:25 +000023#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000024#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000025#include "llvm/Instructions.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/LeakDetector.h"
27#include "llvm/Support/GraphWriter.h"
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +000028#include <fstream>
Reid Spencereb04d9b2004-07-04 12:19:56 +000029#include <iostream>
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +000030#include <sstream>
Tanya Lattnera578cb72004-05-24 06:11:51 +000031
Chris Lattner9a3478e2003-12-20 09:17:07 +000032using namespace llvm;
Chris Lattnereda6bd72002-02-03 07:54:50 +000033
Chris Lattnerbbd68ad2002-10-29 23:18:43 +000034static AnnotationID MF_AID(
Alkis Evlogimenos58350a72004-09-05 18:41:35 +000035 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnereda6bd72002-02-03 07:54:50 +000036
Chris Lattner6d8a6c62002-10-28 01:12:41 +000037
Chris Lattner6d8a6c62002-10-28 01:12:41 +000038namespace {
Chris Lattner4cbb97b2003-12-20 10:20:58 +000039 struct Printer : public MachineFunctionPass {
Brian Gaeke845c0dd2004-01-30 21:53:46 +000040 std::ostream *OS;
Chris Lattnercae054f2004-02-01 05:25:07 +000041 const std::string Banner;
Brian Gaeke845c0dd2004-01-30 21:53:46 +000042
43 Printer (std::ostream *_OS, const std::string &_Banner) :
44 OS (_OS), Banner (_Banner) { }
45
Chris Lattner214808f2002-10-30 00:48:05 +000046 const char *getPassName() const { return "MachineFunction Printer"; }
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 }
51
Chris Lattner4cbb97b2003-12-20 10:20:58 +000052 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke845c0dd2004-01-30 21:53:46 +000053 (*OS) << Banner;
54 MF.print (*OS);
Chris Lattner214808f2002-10-30 00:48:05 +000055 return false;
56 }
57 };
Chris Lattner6d8a6c62002-10-28 01:12:41 +000058}
59
Brian Gaeke845c0dd2004-01-30 21:53:46 +000060/// Returns a newly-created MachineFunction Printer pass. The default output
61/// stream is std::cerr; the default banner is empty.
62///
63FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattner304053c2005-01-23 22:13:58 +000064 const std::string &Banner){
Brian Gaeke845c0dd2004-01-30 21:53:46 +000065 return new Printer(OS, Banner);
Chris Lattner214808f2002-10-30 00:48:05 +000066}
67
Alkis Evlogimenos6a355162004-02-15 00:03:15 +000068namespace {
69 struct Deleter : public MachineFunctionPass {
70 const char *getPassName() const { return "Machine Code Deleter"; }
71
72 bool runOnMachineFunction(MachineFunction &MF) {
73 // Delete the annotation from the function now.
74 MachineFunction::destruct(MF.getFunction());
75 return true;
76 }
77 };
78}
79
80/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
81/// the current function, which should happen after the function has been
82/// emitted to a .s file or to memory.
83FunctionPass *llvm::createMachineCodeDeleter() {
84 return new Deleter();
85}
86
87
88
Chris Lattner6d8a6c62002-10-28 01:12:41 +000089//===---------------------------------------------------------------------===//
90// MachineFunction implementation
91//===---------------------------------------------------------------------===//
Tanya Lattnera578cb72004-05-24 06:11:51 +000092MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode()
93{
Alkis Evlogimenos58350a72004-09-05 18:41:35 +000094 MachineBasicBlock* dummy = new MachineBasicBlock();
95 LeakDetector::removeGarbageObject(dummy);
96 return dummy;
Tanya Lattnera578cb72004-05-24 06:11:51 +000097}
98
99void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000100 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
101 ilist_iterator<MachineBasicBlock> first,
102 ilist_iterator<MachineBasicBlock> last)
Tanya Lattnera578cb72004-05-24 06:11:51 +0000103{
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000104 if (Parent != toList.Parent)
105 for (; first != last; ++first)
106 first->Parent = toList.Parent;
Tanya Lattnera578cb72004-05-24 06:11:51 +0000107}
Chris Lattner6d8a6c62002-10-28 01:12:41 +0000108
Chris Lattner214808f2002-10-30 00:48:05 +0000109MachineFunction::MachineFunction(const Function *F,
Chris Lattner32525642002-12-28 20:37:16 +0000110 const TargetMachine &TM)
Chris Lattner304053c2005-01-23 22:13:58 +0000111 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmand5b111a2002-11-20 18:55:27 +0000112 SSARegMapping = new SSARegMap();
Chris Lattnera1d78022004-08-16 22:36:34 +0000113 MFInfo = 0;
Chris Lattnerca4362f2002-12-28 21:08:26 +0000114 FrameInfo = new MachineFrameInfo();
Chris Lattnerc6807e82003-01-13 00:23:03 +0000115 ConstantPool = new MachineConstantPool();
Tanya Lattner91fa3a92004-05-24 07:14:35 +0000116 BasicBlocks.Parent = this;
Chris Lattner448fb452002-12-25 05:03:22 +0000117}
118
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000119MachineFunction::~MachineFunction() {
Chris Lattner9a60c532004-07-01 06:29:07 +0000120 BasicBlocks.clear();
Chris Lattner448fb452002-12-25 05:03:22 +0000121 delete SSARegMapping;
Chris Lattner32525642002-12-28 20:37:16 +0000122 delete MFInfo;
123 delete FrameInfo;
Chris Lattnerc6807e82003-01-13 00:23:03 +0000124 delete ConstantPool;
Chris Lattner304053c2005-01-23 22:13:58 +0000125 delete[] UsedPhysRegs;
Chris Lattner214808f2002-10-30 00:48:05 +0000126}
127
128void MachineFunction::dump() const { print(std::cerr); }
129
130void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke2fe0ac92004-03-29 21:58:31 +0000131 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner32525642002-12-28 20:37:16 +0000132
133 // Print Frame Information
Chris Lattnereb45c982003-01-16 18:35:57 +0000134 getFrameInfo()->print(*this, OS);
Chris Lattnerc6807e82003-01-13 00:23:03 +0000135
136 // Print Constant Pool
137 getConstantPool()->print(OS);
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000138
Brian Gaeke52440fd2004-02-13 04:39:55 +0000139 for (const_iterator BB = begin(); BB != end(); ++BB)
140 BB->print(OS);
Brian Gaeke2fe0ac92004-03-29 21:58:31 +0000141
142 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner214808f2002-10-30 00:48:05 +0000143}
144
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000145/// CFGOnly flag - This is used to control whether or not the CFG graph printer
146/// prints out the contents of basic blocks or not. This is acceptable because
147/// this code is only really used for debugging purposes.
148///
149static bool CFGOnly = false;
150
151namespace llvm {
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000152 template<>
153 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
154 static std::string getGraphName(const MachineFunction *F) {
155 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000156 }
157
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000158 static std::string getNodeLabel(const MachineBasicBlock *Node,
159 const MachineFunction *Graph) {
160 if (CFGOnly && Node->getBasicBlock() &&
161 !Node->getBasicBlock()->getName().empty())
162 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000163
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000164 std::ostringstream Out;
165 if (CFGOnly) {
166 Out << Node->getNumber() << ':';
167 return Out.str();
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000168 }
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000169
170 Node->print(Out);
171
172 std::string OutStr = Out.str();
173 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
174
175 // Process string output to make it nicer...
176 for (unsigned i = 0; i != OutStr.length(); ++i)
177 if (OutStr[i] == '\n') { // Left justify
178 OutStr[i] = '\\';
179 OutStr.insert(OutStr.begin()+i+1, 'l');
180 }
181 return OutStr;
182 }
183 };
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000184}
185
186void MachineFunction::viewCFG() const
187{
188 std::string Filename = "/tmp/cfg." + getFunction()->getName() + ".dot";
189 std::cerr << "Writing '" << Filename << "'... ";
190 std::ofstream F(Filename.c_str());
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000191
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000192 if (!F) {
193 std::cerr << " error opening file for writing!\n";
194 return;
195 }
196
197 WriteGraph(F, this);
198 F.close();
199 std::cerr << "\n";
200
201 std::cerr << "Running 'dot' program... " << std::flush;
202 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
203 + " > /tmp/cfg.tempgraph.ps").c_str())) {
204 std::cerr << "Error running dot: 'dot' not in path?\n";
205 } else {
206 std::cerr << "\n";
207 system("gv /tmp/cfg.tempgraph.ps");
208 }
209 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
210}
211
212void MachineFunction::viewCFGOnly() const
213{
214 CFGOnly = true;
215 viewCFG();
216 CFGOnly = false;
217}
218
Chris Lattnereda6bd72002-02-03 07:54:50 +0000219// The next two methods are used to construct and to retrieve
Chris Lattner62b7fd12002-04-07 20:49:59 +0000220// the MachineCodeForFunction object for the given function.
221// construct() -- Allocates and initializes for a given function and target
Chris Lattnereda6bd72002-02-03 07:54:50 +0000222// get() -- Returns a handle to the object.
223// This should not be called before "construct()"
Chris Lattner62b7fd12002-04-07 20:49:59 +0000224// for a given Function.
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000225//
Misha Brukman7ae7f842002-10-28 00:28:31 +0000226MachineFunction&
Chris Lattnerba3a8062002-10-28 05:58:46 +0000227MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000228{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000229 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner62b7fd12002-04-07 20:49:59 +0000230 "Object already exists for this function!");
Chris Lattnerba3a8062002-10-28 05:58:46 +0000231 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
232 Fn->addAnnotation(mcInfo);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000233 return *mcInfo;
234}
235
Chris Lattner4cbb97b2003-12-20 10:20:58 +0000236void MachineFunction::destruct(const Function *Fn) {
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000237 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner62b7fd12002-04-07 20:49:59 +0000238 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnereda6bd72002-02-03 07:54:50 +0000239}
240
Chris Lattnerba3a8062002-10-28 05:58:46 +0000241MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000242{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000243 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000244 assert(mc && "Call construct() method first to allocate the object");
245 return *mc;
246}
247
Chris Lattner448fb452002-12-25 05:03:22 +0000248void MachineFunction::clearSSARegMap() {
249 delete SSARegMapping;
250 SSARegMapping = 0;
251}
252
Chris Lattner32525642002-12-28 20:37:16 +0000253//===----------------------------------------------------------------------===//
Chris Lattnerca4362f2002-12-28 21:08:26 +0000254// MachineFrameInfo implementation
Chris Lattner32525642002-12-28 20:37:16 +0000255//===----------------------------------------------------------------------===//
256
Chris Lattnerc6807e82003-01-13 00:23:03 +0000257/// CreateStackObject - Create a stack object for a value of the specified type.
258///
259int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
Chris Lattnere64ff1c2005-01-08 19:55:00 +0000260 return CreateStackObject((unsigned)TD.getTypeSize(Ty),
261 TD.getTypeAlignment(Ty));
Chris Lattnerc6807e82003-01-13 00:23:03 +0000262}
263
Chris Lattnerc6807e82003-01-13 00:23:03 +0000264
Chris Lattnereb45c982003-01-16 18:35:57 +0000265void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner185fa542004-06-02 05:56:52 +0000266 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattnereb45c982003-01-16 18:35:57 +0000267
Chris Lattner32525642002-12-28 20:37:16 +0000268 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
269 const StackObject &SO = Objects[i];
Chris Lattnerc6807e82003-01-13 00:23:03 +0000270 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
Chris Lattner32525642002-12-28 20:37:16 +0000271 if (SO.Size == 0)
272 OS << "variable sized";
273 else
274 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000275
Chris Lattner32525642002-12-28 20:37:16 +0000276 if (i < NumFixedObjects)
277 OS << " fixed";
278 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner5888b5d2004-06-11 06:37:11 +0000279 int Off = SO.SPOffset - ValOffset;
Chris Lattner32525642002-12-28 20:37:16 +0000280 OS << " at location [SP";
Chris Lattnereb45c982003-01-16 18:35:57 +0000281 if (Off > 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000282 OS << "+" << Off;
Chris Lattnereb45c982003-01-16 18:35:57 +0000283 else if (Off < 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000284 OS << Off;
Chris Lattner32525642002-12-28 20:37:16 +0000285 OS << "]";
286 }
287 OS << "\n";
288 }
289
290 if (HasVarSizedObjects)
291 OS << " Stack frame contains variable sized objects\n";
292}
293
Chris Lattnereb45c982003-01-16 18:35:57 +0000294void MachineFrameInfo::dump(const MachineFunction &MF) const {
295 print(MF, std::cerr);
296}
Chris Lattner32525642002-12-28 20:37:16 +0000297
298
299//===----------------------------------------------------------------------===//
Chris Lattnerc6807e82003-01-13 00:23:03 +0000300// MachineConstantPool implementation
301//===----------------------------------------------------------------------===//
302
303void MachineConstantPool::print(std::ostream &OS) const {
304 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
305 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
306}
307
308void MachineConstantPool::dump() const { print(std::cerr); }