blob: 05e863be75068ac384b44eedaac956833867a149 [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"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000022#include "llvm/CodeGen/Passes.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000025#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000027#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/LeakDetector.h"
29#include "llvm/Support/GraphWriter.h"
Chris Lattnerf8c68f62006-06-28 22:17:39 +000030#include "llvm/Support/Visibility.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000031#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000032#include <fstream>
Reid Spencer954da372004-07-04 12:19:56 +000033#include <iostream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000034#include <sstream>
Tanya Lattner792699c2004-05-24 06:11:51 +000035
Chris Lattner07f32d42003-12-20 09:17:07 +000036using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037
Chris Lattnere316efc2002-10-29 23:18:43 +000038static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000039 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000040
Chris Lattner227c3d32002-10-28 01:12:41 +000041
Chris Lattner227c3d32002-10-28 01:12:41 +000042namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000043 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000044 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000045 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000046
47 Printer (std::ostream *_OS, const std::string &_Banner) :
48 OS (_OS), Banner (_Banner) { }
49
Chris Lattner10491642002-10-30 00:48:05 +000050 const char *getPassName() const { return "MachineFunction Printer"; }
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 }
55
Chris Lattner16c45e92003-12-20 10:20:58 +000056 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000057 (*OS) << Banner;
58 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000059 return false;
60 }
61 };
Chris Lattner227c3d32002-10-28 01:12:41 +000062}
63
Brian Gaeke09caa372004-01-30 21:53:46 +000064/// Returns a newly-created MachineFunction Printer pass. The default output
65/// stream is std::cerr; the default banner is empty.
66///
67FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000068 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000069 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000070}
71
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000072namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000073 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000074 const char *getPassName() const { return "Machine Code Deleter"; }
75
76 bool runOnMachineFunction(MachineFunction &MF) {
77 // Delete the annotation from the function now.
78 MachineFunction::destruct(MF.getFunction());
79 return true;
80 }
81 };
82}
83
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.
87FunctionPass *llvm::createMachineCodeDeleter() {
88 return new Deleter();
89}
90
91
92
Chris Lattner227c3d32002-10-28 01:12:41 +000093//===---------------------------------------------------------------------===//
94// MachineFunction implementation
95//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000096
Chris Lattnerbca81442005-01-30 00:09:23 +000097MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000098 MachineBasicBlock* dummy = new MachineBasicBlock();
99 LeakDetector::removeGarbageObject(dummy);
100 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000101}
102
103void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000104 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
105 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000106 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000107 if (Parent != toList.Parent)
108 for (; first != last; ++first)
109 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000110}
Chris Lattner227c3d32002-10-28 01:12:41 +0000111
Chris Lattner10491642002-10-30 00:48:05 +0000112MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000113 const TargetMachine &TM)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000114 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000115 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000116 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000117 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000118 ConstantPool = new MachineConstantPool(TM.getTargetData());
Nate Begeman37efe672006-04-22 18:53:45 +0000119 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000120 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000121}
122
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000123MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000124 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000125 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000126 delete MFInfo;
127 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000128 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000129 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000130 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000131}
132
133void MachineFunction::dump() const { print(std::cerr); }
134
135void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000136 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000137
138 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000139 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000140
141 // Print JumpTable Information
142 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000143
144 // Print Constant Pool
145 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000146
147 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
148
149 if (livein_begin() != livein_end()) {
150 OS << "Live Ins:";
151 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
152 if (MRI)
153 OS << " " << MRI->getName(I->first);
154 else
155 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000156
157 if (I->second)
158 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000159 }
160 OS << "\n";
161 }
162 if (liveout_begin() != liveout_end()) {
163 OS << "Live Outs:";
164 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
165 if (MRI)
166 OS << " " << MRI->getName(*I);
167 else
168 OS << " Reg #" << *I;
169 OS << "\n";
170 }
171
Brian Gaeke90421cd2004-02-13 04:39:55 +0000172 for (const_iterator BB = begin(); BB != end(); ++BB)
173 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000174
175 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000176}
177
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000178/// 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.
181///
182static bool CFGOnly = false;
183
184namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000185 template<>
186 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
187 static std::string getGraphName(const MachineFunction *F) {
188 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000189 }
190
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000191 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() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000196
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000197 std::ostringstream Out;
198 if (CFGOnly) {
199 Out << Node->getNumber() << ':';
200 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000201 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000202
203 Node->print(Out);
204
205 std::string OutStr = Out.str();
206 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
207
208 // Process string output to make it nicer...
209 for (unsigned i = 0; i != OutStr.length(); ++i)
210 if (OutStr[i] == '\n') { // Left justify
211 OutStr[i] = '\\';
212 OutStr.insert(OutStr.begin()+i+1, 'l');
213 }
214 return OutStr;
215 }
216 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000217}
218
219void MachineFunction::viewCFG() const
220{
Jim Laskey851a22d2005-10-12 12:09:05 +0000221#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000222 ViewGraph(this, "mf" + getFunction()->getName());
223#else
224 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
Jim Laskey851a22d2005-10-12 12:09:05 +0000225 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000226#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000227}
228
229void MachineFunction::viewCFGOnly() const
230{
231 CFGOnly = true;
232 viewCFG();
233 CFGOnly = false;
234}
235
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000236// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237// the MachineCodeForFunction object for the given function.
238// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000239// get() -- Returns a handle to the object.
240// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000241// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000242//
Misha Brukmanfce11432002-10-28 00:28:31 +0000243MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000244MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000245{
Chris Lattnere316efc2002-10-29 23:18:43 +0000246 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000247 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000248 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
249 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000250 return *mcInfo;
251}
252
Chris Lattner16c45e92003-12-20 10:20:58 +0000253void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000254 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000255 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000256}
257
Chris Lattner335d5c32002-10-28 05:58:46 +0000258MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000259{
Chris Lattnere316efc2002-10-29 23:18:43 +0000260 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000261 assert(mc && "Call construct() method first to allocate the object");
262 return *mc;
263}
264
Chris Lattner831fdcf2002-12-25 05:03:22 +0000265void MachineFunction::clearSSARegMap() {
266 delete SSARegMapping;
267 SSARegMapping = 0;
268}
269
Chris Lattner955fad12002-12-28 20:37:16 +0000270//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000271// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000272//===----------------------------------------------------------------------===//
273
Chris Lattner9085d8a2003-01-16 18:35:57 +0000274void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000275 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000276
Chris Lattner955fad12002-12-28 20:37:16 +0000277 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
278 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000279 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000280 if (SO.Size == 0)
281 OS << "variable sized";
282 else
Chris Lattner0db07092005-05-13 22:54:44 +0000283 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
284 OS << " alignment is " << SO.Alignment << " byte"
285 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000286
Chris Lattner955fad12002-12-28 20:37:16 +0000287 if (i < NumFixedObjects)
288 OS << " fixed";
289 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000290 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000291 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000292 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000293 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000294 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000295 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000296 OS << "]";
297 }
298 OS << "\n";
299 }
300
301 if (HasVarSizedObjects)
302 OS << " Stack frame contains variable sized objects\n";
303}
304
Chris Lattner9085d8a2003-01-16 18:35:57 +0000305void MachineFrameInfo::dump(const MachineFunction &MF) const {
306 print(MF, std::cerr);
307}
Chris Lattner955fad12002-12-28 20:37:16 +0000308
309
310//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000311// MachineJumpTableInfo implementation
312//===----------------------------------------------------------------------===//
313
314/// getJumpTableIndex - Create a new jump table entry in the jump table info
315/// or return an existing one.
316///
317unsigned MachineJumpTableInfo::getJumpTableIndex(
318 std::vector<MachineBasicBlock*> &DestBBs) {
319 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
320 if (JumpTables[i].MBBs == DestBBs)
321 return i;
322
323 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
324 return JumpTables.size()-1;
325}
326
327
328void 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()
333 << " entries\n";
334 }
335}
336
Nate Begeman3700a4d2006-04-22 23:52:35 +0000337unsigned MachineJumpTableInfo::getEntrySize() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000338 return TD->getPointerSize();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000339}
340
341unsigned MachineJumpTableInfo::getAlignment() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000342 return TD->getPointerAlignment();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000343}
344
Nate Begeman37efe672006-04-22 18:53:45 +0000345void MachineJumpTableInfo::dump() const { print(std::cerr); }
346
347
348//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000349// MachineConstantPool implementation
350//===----------------------------------------------------------------------===//
351
Chris Lattner3029f922006-02-09 04:46:04 +0000352/// 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.
354///
355unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
356 unsigned Alignment) {
357 assert(Alignment && "Alignment must be specified!");
358 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
359
360 // Check to see if we already have this constant.
361 //
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)
366 return i;
367
368 unsigned Offset = 0;
369 if (!Constants.empty()) {
370 Offset = Constants.back().Offset;
Owen Andersona69571c2006-05-03 01:29:57 +0000371 Offset += TD->getTypeSize(Constants.back().Val->getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000372 Offset = (Offset+AlignMask)&~AlignMask;
373 }
374
375 Constants.push_back(MachineConstantPoolEntry(C, Offset));
376 return Constants.size()-1;
377}
378
379
Chris Lattner4d149cd2003-01-13 00:23:03 +0000380void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000381 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000382 OS << " <cp #" << i << "> is" << *(Value*)Constants[i].Val;
Chris Lattner3029f922006-02-09 04:46:04 +0000383 OS << " , offset=" << Constants[i].Offset;
Evan Chengb8973bd2006-01-31 22:23:14 +0000384 OS << "\n";
385 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000386}
387
388void MachineConstantPool::dump() const { print(std::cerr); }