blob: f0ece6b3dbbec51691d853cc9e31875e03cfbc9b [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00002//
John Criswellb576c942003-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 Evlogimenos76d9dac2004-09-05 18:41:35 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00009//
Chris Lattner6b944532002-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 Lattnerf2868ce2002-02-03 07:54:50 +000015
Chris Lattner16c45e92003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000018#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000021#include "llvm/CodeGen/Passes.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000023#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000024#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000025#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/LeakDetector.h"
27#include "llvm/Support/GraphWriter.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000028#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000029#include <fstream>
Reid Spencer954da372004-07-04 12:19:56 +000030#include <iostream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000031#include <sstream>
Tanya Lattner792699c2004-05-24 06:11:51 +000032
Chris Lattner07f32d42003-12-20 09:17:07 +000033using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000034
Chris Lattnere316efc2002-10-29 23:18:43 +000035static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000036 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037
Chris Lattner227c3d32002-10-28 01:12:41 +000038
Chris Lattner227c3d32002-10-28 01:12:41 +000039namespace {
Chris Lattner16c45e92003-12-20 10:20:58 +000040 struct Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000041 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000042 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000043
44 Printer (std::ostream *_OS, const std::string &_Banner) :
45 OS (_OS), Banner (_Banner) { }
46
Chris Lattner10491642002-10-30 00:48:05 +000047 const char *getPassName() const { return "MachineFunction Printer"; }
48
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 }
52
Chris Lattner16c45e92003-12-20 10:20:58 +000053 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000054 (*OS) << Banner;
55 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000056 return false;
57 }
58 };
Chris Lattner227c3d32002-10-28 01:12:41 +000059}
60
Brian Gaeke09caa372004-01-30 21:53:46 +000061/// Returns a newly-created MachineFunction Printer pass. The default output
62/// stream is std::cerr; the default banner is empty.
63///
64FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000065 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000066 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000067}
68
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000069namespace {
70 struct Deleter : public MachineFunctionPass {
71 const char *getPassName() const { return "Machine Code Deleter"; }
72
73 bool runOnMachineFunction(MachineFunction &MF) {
74 // Delete the annotation from the function now.
75 MachineFunction::destruct(MF.getFunction());
76 return true;
77 }
78 };
79}
80
81/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
82/// the current function, which should happen after the function has been
83/// emitted to a .s file or to memory.
84FunctionPass *llvm::createMachineCodeDeleter() {
85 return new Deleter();
86}
87
88
89
Chris Lattner227c3d32002-10-28 01:12:41 +000090//===---------------------------------------------------------------------===//
91// MachineFunction implementation
92//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000093
Chris Lattnerbca81442005-01-30 00:09:23 +000094MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000095 MachineBasicBlock* dummy = new MachineBasicBlock();
96 LeakDetector::removeGarbageObject(dummy);
97 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +000098}
99
100void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000101 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
102 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000103 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000104 if (Parent != toList.Parent)
105 for (; first != last; ++first)
106 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000107}
Chris Lattner227c3d32002-10-28 01:12:41 +0000108
Chris Lattner10491642002-10-30 00:48:05 +0000109MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000110 const TargetMachine &TM)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000111 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000112 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000113 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000114 FrameInfo = new MachineFrameInfo();
Chris Lattner4d149cd2003-01-13 00:23:03 +0000115 ConstantPool = new MachineConstantPool();
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000116 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000117}
118
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000119MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000120 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000121 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000122 delete MFInfo;
123 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000124 delete ConstantPool;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000125 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000126}
127
128void MachineFunction::dump() const { print(std::cerr); }
129
130void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000131 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000132
133 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000134 getFrameInfo()->print(*this, OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000135
136 // Print Constant Pool
137 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000138
139 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
140
141 if (livein_begin() != livein_end()) {
142 OS << "Live Ins:";
143 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
144 if (MRI)
145 OS << " " << MRI->getName(I->first);
146 else
147 OS << " Reg #" << I->first;
148 }
149 OS << "\n";
150 }
151 if (liveout_begin() != liveout_end()) {
152 OS << "Live Outs:";
153 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
154 if (MRI)
155 OS << " " << MRI->getName(*I);
156 else
157 OS << " Reg #" << *I;
158 OS << "\n";
159 }
160
Brian Gaeke90421cd2004-02-13 04:39:55 +0000161 for (const_iterator BB = begin(); BB != end(); ++BB)
162 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000163
164 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000165}
166
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000167/// CFGOnly flag - This is used to control whether or not the CFG graph printer
168/// prints out the contents of basic blocks or not. This is acceptable because
169/// this code is only really used for debugging purposes.
170///
171static bool CFGOnly = false;
172
173namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000174 template<>
175 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
176 static std::string getGraphName(const MachineFunction *F) {
177 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000178 }
179
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000180 static std::string getNodeLabel(const MachineBasicBlock *Node,
181 const MachineFunction *Graph) {
182 if (CFGOnly && Node->getBasicBlock() &&
183 !Node->getBasicBlock()->getName().empty())
184 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000185
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000186 std::ostringstream Out;
187 if (CFGOnly) {
188 Out << Node->getNumber() << ':';
189 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000190 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000191
192 Node->print(Out);
193
194 std::string OutStr = Out.str();
195 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
196
197 // Process string output to make it nicer...
198 for (unsigned i = 0; i != OutStr.length(); ++i)
199 if (OutStr[i] == '\n') { // Left justify
200 OutStr[i] = '\\';
201 OutStr.insert(OutStr.begin()+i+1, 'l');
202 }
203 return OutStr;
204 }
205 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000206}
207
208void MachineFunction::viewCFG() const
209{
Jim Laskey851a22d2005-10-12 12:09:05 +0000210#ifndef NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000211 std::string Filename = "/tmp/cfg." + getFunction()->getName() + ".dot";
212 std::cerr << "Writing '" << Filename << "'... ";
213 std::ofstream F(Filename.c_str());
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000214
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000215 if (!F) {
216 std::cerr << " error opening file for writing!\n";
217 return;
218 }
219
220 WriteGraph(F, this);
221 F.close();
222 std::cerr << "\n";
223
Jim Laskey851a22d2005-10-12 12:09:05 +0000224#ifdef HAVE_GRAPHVIZ
225 std::cerr << "Running 'Graphviz' program... " << std::flush;
226 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
227 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
228 } else {
229 system(("rm " + Filename).c_str());
230 return;
231 }
232#endif // HAVE_GRAPHVIZ
233
234#ifdef HAVE_GV
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000235 std::cerr << "Running 'dot' program... " << std::flush;
236 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
237 + " > /tmp/cfg.tempgraph.ps").c_str())) {
238 std::cerr << "Error running dot: 'dot' not in path?\n";
239 } else {
240 std::cerr << "\n";
241 system("gv /tmp/cfg.tempgraph.ps");
242 }
243 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
Jim Laskey851a22d2005-10-12 12:09:05 +0000244 return;
245#endif // HAVE_GV
246#endif // NDEBUG
247 std::cerr << "MachineFunction::viewCFG is only available in debug builds on "
248 << "systems with Graphviz or gv!\n";
249
250#ifndef NDEBUG
251 system(("rm " + Filename).c_str());
252#endif
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000253}
254
255void MachineFunction::viewCFGOnly() const
256{
257 CFGOnly = true;
258 viewCFG();
259 CFGOnly = false;
260}
261
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000262// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000263// the MachineCodeForFunction object for the given function.
264// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000265// get() -- Returns a handle to the object.
266// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000268//
Misha Brukmanfce11432002-10-28 00:28:31 +0000269MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000270MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000271{
Chris Lattnere316efc2002-10-29 23:18:43 +0000272 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000273 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000274 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
275 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000276 return *mcInfo;
277}
278
Chris Lattner16c45e92003-12-20 10:20:58 +0000279void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000280 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000281 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000282}
283
Chris Lattner335d5c32002-10-28 05:58:46 +0000284MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000285{
Chris Lattnere316efc2002-10-29 23:18:43 +0000286 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000287 assert(mc && "Call construct() method first to allocate the object");
288 return *mc;
289}
290
Chris Lattner831fdcf2002-12-25 05:03:22 +0000291void MachineFunction::clearSSARegMap() {
292 delete SSARegMapping;
293 SSARegMapping = 0;
294}
295
Chris Lattner955fad12002-12-28 20:37:16 +0000296//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000297// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000298//===----------------------------------------------------------------------===//
299
Chris Lattner4d149cd2003-01-13 00:23:03 +0000300/// CreateStackObject - Create a stack object for a value of the specified type.
301///
302int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000303 return CreateStackObject((unsigned)TD.getTypeSize(Ty),
Chris Lattner28696be2005-01-08 19:55:00 +0000304 TD.getTypeAlignment(Ty));
Chris Lattner4d149cd2003-01-13 00:23:03 +0000305}
306
Chris Lattner4d149cd2003-01-13 00:23:03 +0000307
Chris Lattner9085d8a2003-01-16 18:35:57 +0000308void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000309 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000310
Chris Lattner955fad12002-12-28 20:37:16 +0000311 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
312 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000313 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000314 if (SO.Size == 0)
315 OS << "variable sized";
316 else
Chris Lattner0db07092005-05-13 22:54:44 +0000317 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
318 OS << " alignment is " << SO.Alignment << " byte"
319 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000320
Chris Lattner955fad12002-12-28 20:37:16 +0000321 if (i < NumFixedObjects)
322 OS << " fixed";
323 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000324 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000325 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000326 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000327 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000328 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000329 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000330 OS << "]";
331 }
332 OS << "\n";
333 }
334
335 if (HasVarSizedObjects)
336 OS << " Stack frame contains variable sized objects\n";
337}
338
Chris Lattner9085d8a2003-01-16 18:35:57 +0000339void MachineFrameInfo::dump(const MachineFunction &MF) const {
340 print(MF, std::cerr);
341}
Chris Lattner955fad12002-12-28 20:37:16 +0000342
343
344//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000345// MachineConstantPool implementation
346//===----------------------------------------------------------------------===//
347
348void MachineConstantPool::print(std::ostream &OS) const {
349 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
350 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
351}
352
353void MachineConstantPool::dump() const { print(std::cerr); }