blob: 3862858bc7172a03e71eda87e977be4167fdbb7e [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"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000028#include <fstream>
Reid Spencer954da372004-07-04 12:19:56 +000029#include <iostream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000030#include <sstream>
Tanya Lattner792699c2004-05-24 06:11:51 +000031
Chris Lattner07f32d42003-12-20 09:17:07 +000032using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000033
Chris Lattnere316efc2002-10-29 23:18:43 +000034static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000035 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000036
Chris Lattner227c3d32002-10-28 01:12:41 +000037
Chris Lattner227c3d32002-10-28 01:12:41 +000038namespace {
Chris Lattner16c45e92003-12-20 10:20:58 +000039 struct Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000040 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000041 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000042
43 Printer (std::ostream *_OS, const std::string &_Banner) :
44 OS (_OS), Banner (_Banner) { }
45
Chris Lattner10491642002-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 Lattner16c45e92003-12-20 10:20:58 +000052 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000053 (*OS) << Banner;
54 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000055 return false;
56 }
57 };
Chris Lattner227c3d32002-10-28 01:12:41 +000058}
59
Brian Gaeke09caa372004-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,
64 const std::string &Banner) {
65 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000066}
67
Alkis Evlogimenosc81efdc2004-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 Lattner227c3d32002-10-28 01:12:41 +000089//===---------------------------------------------------------------------===//
90// MachineFunction implementation
91//===---------------------------------------------------------------------===//
Tanya Lattner792699c2004-05-24 06:11:51 +000092MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode()
93{
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000094 MachineBasicBlock* dummy = new MachineBasicBlock();
95 LeakDetector::removeGarbageObject(dummy);
96 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +000097}
98
99void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000100 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
101 ilist_iterator<MachineBasicBlock> first,
102 ilist_iterator<MachineBasicBlock> last)
Tanya Lattner792699c2004-05-24 06:11:51 +0000103{
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)
Chris Lattner51289aa2004-07-01 06:02:07 +0000111 : Annotation(MF_AID), Fn(F), Target(TM) {
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 Lattner10491642002-10-30 00:48:05 +0000125}
126
127void MachineFunction::dump() const { print(std::cerr); }
128
129void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000130 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000131
132 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000133 getFrameInfo()->print(*this, OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000134
135 // Print Constant Pool
136 getConstantPool()->print(OS);
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000137
Brian Gaeke90421cd2004-02-13 04:39:55 +0000138 for (const_iterator BB = begin(); BB != end(); ++BB)
139 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000140
141 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000142}
143
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000144/// CFGOnly flag - This is used to control whether or not the CFG graph printer
145/// prints out the contents of basic blocks or not. This is acceptable because
146/// this code is only really used for debugging purposes.
147///
148static bool CFGOnly = false;
149
150namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000151 template<>
152 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
153 static std::string getGraphName(const MachineFunction *F) {
154 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000155 }
156
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000157 static std::string getNodeLabel(const MachineBasicBlock *Node,
158 const MachineFunction *Graph) {
159 if (CFGOnly && Node->getBasicBlock() &&
160 !Node->getBasicBlock()->getName().empty())
161 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000162
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000163 std::ostringstream Out;
164 if (CFGOnly) {
165 Out << Node->getNumber() << ':';
166 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000167 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000168
169 Node->print(Out);
170
171 std::string OutStr = Out.str();
172 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
173
174 // Process string output to make it nicer...
175 for (unsigned i = 0; i != OutStr.length(); ++i)
176 if (OutStr[i] == '\n') { // Left justify
177 OutStr[i] = '\\';
178 OutStr.insert(OutStr.begin()+i+1, 'l');
179 }
180 return OutStr;
181 }
182 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000183}
184
185void MachineFunction::viewCFG() const
186{
187 std::string Filename = "/tmp/cfg." + getFunction()->getName() + ".dot";
188 std::cerr << "Writing '" << Filename << "'... ";
189 std::ofstream F(Filename.c_str());
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000190
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000191 if (!F) {
192 std::cerr << " error opening file for writing!\n";
193 return;
194 }
195
196 WriteGraph(F, this);
197 F.close();
198 std::cerr << "\n";
199
200 std::cerr << "Running 'dot' program... " << std::flush;
201 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
202 + " > /tmp/cfg.tempgraph.ps").c_str())) {
203 std::cerr << "Error running dot: 'dot' not in path?\n";
204 } else {
205 std::cerr << "\n";
206 system("gv /tmp/cfg.tempgraph.ps");
207 }
208 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
209}
210
211void MachineFunction::viewCFGOnly() const
212{
213 CFGOnly = true;
214 viewCFG();
215 CFGOnly = false;
216}
217
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000218// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000219// the MachineCodeForFunction object for the given function.
220// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000221// get() -- Returns a handle to the object.
222// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000223// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000224//
Misha Brukmanfce11432002-10-28 00:28:31 +0000225MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000226MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000227{
Chris Lattnere316efc2002-10-29 23:18:43 +0000228 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000229 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000230 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
231 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000232 return *mcInfo;
233}
234
Chris Lattner16c45e92003-12-20 10:20:58 +0000235void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000236 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000238}
239
Chris Lattner335d5c32002-10-28 05:58:46 +0000240MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000241{
Chris Lattnere316efc2002-10-29 23:18:43 +0000242 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000243 assert(mc && "Call construct() method first to allocate the object");
244 return *mc;
245}
246
Chris Lattner831fdcf2002-12-25 05:03:22 +0000247void MachineFunction::clearSSARegMap() {
248 delete SSARegMapping;
249 SSARegMapping = 0;
250}
251
Chris Lattner955fad12002-12-28 20:37:16 +0000252//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000253// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000254//===----------------------------------------------------------------------===//
255
Chris Lattner4d149cd2003-01-13 00:23:03 +0000256/// CreateStackObject - Create a stack object for a value of the specified type.
257///
258int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
259 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
260}
261
Chris Lattner4d149cd2003-01-13 00:23:03 +0000262
Chris Lattner9085d8a2003-01-16 18:35:57 +0000263void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000264 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000265
Chris Lattner955fad12002-12-28 20:37:16 +0000266 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
267 const StackObject &SO = Objects[i];
Chris Lattner4d149cd2003-01-13 00:23:03 +0000268 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
Chris Lattner955fad12002-12-28 20:37:16 +0000269 if (SO.Size == 0)
270 OS << "variable sized";
271 else
272 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000273
Chris Lattner955fad12002-12-28 20:37:16 +0000274 if (i < NumFixedObjects)
275 OS << " fixed";
276 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000277 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000278 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000279 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000280 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000281 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000282 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000283 OS << "]";
284 }
285 OS << "\n";
286 }
287
288 if (HasVarSizedObjects)
289 OS << " Stack frame contains variable sized objects\n";
290}
291
Chris Lattner9085d8a2003-01-16 18:35:57 +0000292void MachineFrameInfo::dump(const MachineFunction &MF) const {
293 print(MF, std::cerr);
294}
Chris Lattner955fad12002-12-28 20:37:16 +0000295
296
297//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000298// MachineConstantPool implementation
299//===----------------------------------------------------------------------===//
300
301void MachineConstantPool::print(std::ostream &OS) const {
302 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
303 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
304}
305
306void MachineConstantPool::dump() const { print(std::cerr); }