blob: 2e3088c7e3b1ac9adacc799390632e289d4ef927 [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,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000064 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000065 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//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000092
Chris Lattnerbca81442005-01-30 00:09:23 +000093MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
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,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000102 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000103 if (Parent != toList.Parent)
104 for (; first != last; ++first)
105 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000106}
Chris Lattner227c3d32002-10-28 01:12:41 +0000107
Chris Lattner10491642002-10-30 00:48:05 +0000108MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000109 const TargetMachine &TM)
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000110 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000111 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000112 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000113 FrameInfo = new MachineFrameInfo();
Chris Lattner4d149cd2003-01-13 00:23:03 +0000114 ConstantPool = new MachineConstantPool();
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000115 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000116}
117
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000118MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000119 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000120 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000121 delete MFInfo;
122 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000123 delete ConstantPool;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000124 delete[] UsedPhysRegs;
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);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000137
138 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
139
140 if (livein_begin() != livein_end()) {
141 OS << "Live Ins:";
142 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
143 if (MRI)
144 OS << " " << MRI->getName(I->first);
145 else
146 OS << " Reg #" << I->first;
147 }
148 OS << "\n";
149 }
150 if (liveout_begin() != liveout_end()) {
151 OS << "Live Outs:";
152 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
153 if (MRI)
154 OS << " " << MRI->getName(*I);
155 else
156 OS << " Reg #" << *I;
157 OS << "\n";
158 }
159
Brian Gaeke90421cd2004-02-13 04:39:55 +0000160 for (const_iterator BB = begin(); BB != end(); ++BB)
161 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000162
163 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000164}
165
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000166/// CFGOnly flag - This is used to control whether or not the CFG graph printer
167/// prints out the contents of basic blocks or not. This is acceptable because
168/// this code is only really used for debugging purposes.
169///
170static bool CFGOnly = false;
171
172namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000173 template<>
174 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
175 static std::string getGraphName(const MachineFunction *F) {
176 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000177 }
178
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000179 static std::string getNodeLabel(const MachineBasicBlock *Node,
180 const MachineFunction *Graph) {
181 if (CFGOnly && Node->getBasicBlock() &&
182 !Node->getBasicBlock()->getName().empty())
183 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000184
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000185 std::ostringstream Out;
186 if (CFGOnly) {
187 Out << Node->getNumber() << ':';
188 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000189 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000190
191 Node->print(Out);
192
193 std::string OutStr = Out.str();
194 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
195
196 // Process string output to make it nicer...
197 for (unsigned i = 0; i != OutStr.length(); ++i)
198 if (OutStr[i] == '\n') { // Left justify
199 OutStr[i] = '\\';
200 OutStr.insert(OutStr.begin()+i+1, 'l');
201 }
202 return OutStr;
203 }
204 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000205}
206
207void MachineFunction::viewCFG() const
208{
209 std::string Filename = "/tmp/cfg." + getFunction()->getName() + ".dot";
210 std::cerr << "Writing '" << Filename << "'... ";
211 std::ofstream F(Filename.c_str());
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000212
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000213 if (!F) {
214 std::cerr << " error opening file for writing!\n";
215 return;
216 }
217
218 WriteGraph(F, this);
219 F.close();
220 std::cerr << "\n";
221
222 std::cerr << "Running 'dot' program... " << std::flush;
223 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
224 + " > /tmp/cfg.tempgraph.ps").c_str())) {
225 std::cerr << "Error running dot: 'dot' not in path?\n";
226 } else {
227 std::cerr << "\n";
228 system("gv /tmp/cfg.tempgraph.ps");
229 }
230 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
231}
232
233void MachineFunction::viewCFGOnly() const
234{
235 CFGOnly = true;
236 viewCFG();
237 CFGOnly = false;
238}
239
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000240// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000241// the MachineCodeForFunction object for the given function.
242// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000243// get() -- Returns a handle to the object.
244// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000245// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000246//
Misha Brukmanfce11432002-10-28 00:28:31 +0000247MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000248MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000249{
Chris Lattnere316efc2002-10-29 23:18:43 +0000250 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000251 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000252 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
253 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000254 return *mcInfo;
255}
256
Chris Lattner16c45e92003-12-20 10:20:58 +0000257void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000258 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000259 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000260}
261
Chris Lattner335d5c32002-10-28 05:58:46 +0000262MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000263{
Chris Lattnere316efc2002-10-29 23:18:43 +0000264 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000265 assert(mc && "Call construct() method first to allocate the object");
266 return *mc;
267}
268
Chris Lattner831fdcf2002-12-25 05:03:22 +0000269void MachineFunction::clearSSARegMap() {
270 delete SSARegMapping;
271 SSARegMapping = 0;
272}
273
Chris Lattner955fad12002-12-28 20:37:16 +0000274//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000275// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000276//===----------------------------------------------------------------------===//
277
Chris Lattner4d149cd2003-01-13 00:23:03 +0000278/// CreateStackObject - Create a stack object for a value of the specified type.
279///
280int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000281 return CreateStackObject((unsigned)TD.getTypeSize(Ty),
Chris Lattner28696be2005-01-08 19:55:00 +0000282 TD.getTypeAlignment(Ty));
Chris Lattner4d149cd2003-01-13 00:23:03 +0000283}
284
Chris Lattner4d149cd2003-01-13 00:23:03 +0000285
Chris Lattner9085d8a2003-01-16 18:35:57 +0000286void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000287 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000288
Chris Lattner955fad12002-12-28 20:37:16 +0000289 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
290 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000291 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000292 if (SO.Size == 0)
293 OS << "variable sized";
294 else
Chris Lattner0db07092005-05-13 22:54:44 +0000295 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
296 OS << " alignment is " << SO.Alignment << " byte"
297 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000298
Chris Lattner955fad12002-12-28 20:37:16 +0000299 if (i < NumFixedObjects)
300 OS << " fixed";
301 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000302 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000303 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000304 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000305 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000306 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000307 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000308 OS << "]";
309 }
310 OS << "\n";
311 }
312
313 if (HasVarSizedObjects)
314 OS << " Stack frame contains variable sized objects\n";
315}
316
Chris Lattner9085d8a2003-01-16 18:35:57 +0000317void MachineFrameInfo::dump(const MachineFunction &MF) const {
318 print(MF, std::cerr);
319}
Chris Lattner955fad12002-12-28 20:37:16 +0000320
321
322//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000323// MachineConstantPool implementation
324//===----------------------------------------------------------------------===//
325
326void MachineConstantPool::print(std::ostream &OS) const {
327 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
328 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
329}
330
331void MachineConstantPool::dump() const { print(std::cerr); }