Adding new Modulo Scheduling graph files.
[oota-llvm.git] / lib / CodeGen / MachineFunction.cpp
1 //===-- MachineFunction.cpp -----------------------------------------------===//
2 // 
3 //                     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.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // 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 //===----------------------------------------------------------------------===//
15
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/SSARegMap.h"
19 #include "llvm/CodeGen/MachineFunctionInfo.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetFrameInfo.h"
25 #include "llvm/Target/TargetCacheInfo.h"
26 #include "llvm/Function.h"
27 #include "llvm/iOther.h"
28 using namespace llvm;
29
30 static AnnotationID MF_AID(
31                  AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
32
33
34 namespace {
35   struct Printer : public MachineFunctionPass {
36     std::ostream *OS;
37     const std::string Banner;
38
39     Printer (std::ostream *_OS, const std::string &_Banner) :
40       OS (_OS), Banner (_Banner) { }
41
42     const char *getPassName() const { return "MachineFunction Printer"; }
43
44     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45       AU.setPreservesAll();
46     }
47
48     bool runOnMachineFunction(MachineFunction &MF) {
49       (*OS) << Banner;
50       MF.print (*OS);
51       return false;
52     }
53   };
54 }
55
56 /// Returns a newly-created MachineFunction Printer pass. The default output
57 /// stream is std::cerr; the default banner is empty.
58 ///
59 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
60                                                      const std::string &Banner) {
61   return new Printer(OS, Banner);
62 }
63
64 namespace {
65   struct Deleter : public MachineFunctionPass {
66     const char *getPassName() const { return "Machine Code Deleter"; }
67
68     bool runOnMachineFunction(MachineFunction &MF) {
69       // Delete the annotation from the function now.
70       MachineFunction::destruct(MF.getFunction());
71       return true;
72     }
73   };
74 }
75
76 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
77 /// the current function, which should happen after the function has been
78 /// emitted to a .s file or to memory.
79 FunctionPass *llvm::createMachineCodeDeleter() {
80   return new Deleter();
81 }
82
83
84
85 //===---------------------------------------------------------------------===//
86 // MachineFunction implementation
87 //===---------------------------------------------------------------------===//
88
89 MachineFunction::MachineFunction(const Function *F,
90                                  const TargetMachine &TM)
91   : Annotation(MF_AID), Fn(F), Target(TM) {
92   SSARegMapping = new SSARegMap();
93   MFInfo = new MachineFunctionInfo(*this);
94   FrameInfo = new MachineFrameInfo();
95   ConstantPool = new MachineConstantPool();
96 }
97
98 MachineFunction::~MachineFunction() { 
99   delete SSARegMapping;
100   delete MFInfo;
101   delete FrameInfo;
102   delete ConstantPool;
103 }
104
105 void MachineFunction::dump() const { print(std::cerr); }
106
107 void MachineFunction::print(std::ostream &OS) const {
108   OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
109      << "\"\n";
110
111   // Print Frame Information
112   getFrameInfo()->print(*this, OS);
113
114   // Print Constant Pool
115   getConstantPool()->print(OS);
116   
117   for (const_iterator BB = begin(); BB != end(); ++BB)
118     BB->print(OS);
119   OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
120 }
121
122 // The next two methods are used to construct and to retrieve
123 // the MachineCodeForFunction object for the given function.
124 // construct() -- Allocates and initializes for a given function and target
125 // get()       -- Returns a handle to the object.
126 //                This should not be called before "construct()"
127 //                for a given Function.
128 // 
129 MachineFunction&
130 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
131 {
132   assert(Fn->getAnnotation(MF_AID) == 0 &&
133          "Object already exists for this function!");
134   MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
135   Fn->addAnnotation(mcInfo);
136   return *mcInfo;
137 }
138
139 void MachineFunction::destruct(const Function *Fn) {
140   bool Deleted = Fn->deleteAnnotation(MF_AID);
141   assert(Deleted && "Machine code did not exist for function!");
142 }
143
144 MachineFunction& MachineFunction::get(const Function *F)
145 {
146   MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
147   assert(mc && "Call construct() method first to allocate the object");
148   return *mc;
149 }
150
151 void MachineFunction::clearSSARegMap() {
152   delete SSARegMapping;
153   SSARegMapping = 0;
154 }
155
156 //===----------------------------------------------------------------------===//
157 //  MachineFrameInfo implementation
158 //===----------------------------------------------------------------------===//
159
160 /// CreateStackObject - Create a stack object for a value of the specified type.
161 ///
162 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
163   return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
164 }
165
166 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
167   return CreateStackObject(RC->getSize(), RC->getAlignment());
168 }
169
170
171 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
172   int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
173
174   for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
175     const StackObject &SO = Objects[i];
176     OS << "  <fi #" << (int)(i-NumFixedObjects) << "> is ";
177     if (SO.Size == 0)
178       OS << "variable sized";
179     else
180       OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
181     
182     if (i < NumFixedObjects)
183       OS << " fixed";
184     if (i < NumFixedObjects || SO.SPOffset != -1) {
185       int Off = SO.SPOffset + ValOffset;
186       OS << " at location [SP";
187       if (Off > 0)
188         OS << "+" << Off;
189       else if (Off < 0)
190         OS << Off;
191       OS << "]";
192     }
193     OS << "\n";
194   }
195
196   if (HasVarSizedObjects)
197     OS << "  Stack frame contains variable sized objects\n";
198 }
199
200 void MachineFrameInfo::dump(const MachineFunction &MF) const {
201   print(MF, std::cerr);
202 }
203
204
205 //===----------------------------------------------------------------------===//
206 //  MachineConstantPool implementation
207 //===----------------------------------------------------------------------===//
208
209 void MachineConstantPool::print(std::ostream &OS) const {
210   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
211     OS << "  <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
212 }
213
214 void MachineConstantPool::dump() const { print(std::cerr); }
215
216 //===----------------------------------------------------------------------===//
217 //  MachineFunctionInfo implementation
218 //===----------------------------------------------------------------------===//
219
220 static unsigned
221 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
222                            unsigned &maxOptionalNumArgs)
223 {
224   const TargetFrameInfo &frameInfo = target.getFrameInfo();
225   
226   unsigned maxSize = 0;
227   
228   for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
229     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
230       if (const CallInst *callInst = dyn_cast<CallInst>(I))
231         {
232           unsigned numOperands = callInst->getNumOperands() - 1;
233           int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
234           if (numExtra <= 0)
235             continue;
236           
237           unsigned sizeForThisCall;
238           if (frameInfo.argsOnStackHaveFixedSize())
239             {
240               int argSize = frameInfo.getSizeOfEachArgOnStack(); 
241               sizeForThisCall = numExtra * (unsigned) argSize;
242             }
243           else
244             {
245               assert(0 && "UNTESTED CODE: Size per stack argument is not "
246                      "fixed on this architecture: use actual arg sizes to "
247                      "compute MaxOptionalArgsSize");
248               sizeForThisCall = 0;
249               for (unsigned i = 0; i < numOperands; ++i)
250                 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
251                                               getOperand(i)->getType());
252             }
253           
254           if (maxSize < sizeForThisCall)
255             maxSize = sizeForThisCall;
256           
257           if ((int)maxOptionalNumArgs < numExtra)
258             maxOptionalNumArgs = (unsigned) numExtra;
259         }
260   
261   return maxSize;
262 }
263
264 // Align data larger than one L1 cache line on L1 cache line boundaries.
265 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
266 // but not higher than the alignment of the largest type we support
267 // (currently a double word). -- see class TargetData).
268 //
269 // This function is similar to the corresponding function in EmitAssembly.cpp
270 // but they are unrelated.  This one does not align at more than a
271 // double-word boundary whereas that one might.
272 // 
273 inline unsigned
274 SizeToAlignment(unsigned size, const TargetMachine& target)
275 {
276   unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); 
277   if (size > (unsigned) cacheLineSize / 2)
278     return cacheLineSize;
279   else
280     for (unsigned sz=1; /*no condition*/; sz *= 2)
281       if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
282         return sz;
283 }
284
285
286 void MachineFunctionInfo::CalculateArgSize() {
287   maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
288                                                    MF.getFunction(),
289                                                    maxOptionalNumArgs);
290   staticStackSize = maxOptionalArgsSize
291     + MF.getTarget().getFrameInfo().getMinStackFrameSize();
292 }
293
294 int
295 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
296                                               unsigned &getPaddedSize,
297                                               unsigned  sizeToUse)
298 {
299   if (sizeToUse == 0)
300     sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
301   unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
302
303   bool growUp;
304   int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
305                                                                              growUp);
306   int offset = growUp? firstOffset + getAutomaticVarsSize()
307                      : firstOffset - (getAutomaticVarsSize() + sizeToUse);
308
309   int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
310   getPaddedSize = sizeToUse + abs(aligned - offset);
311
312   return aligned;
313 }
314
315
316 int MachineFunctionInfo::allocateLocalVar(const Value* val,
317                                           unsigned sizeToUse) {
318   assert(! automaticVarsAreaFrozen &&
319          "Size of auto vars area has been used to compute an offset so "
320          "no more automatic vars should be allocated!");
321   
322   // Check if we've allocated a stack slot for this value already
323   // 
324   hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
325   if (pair != offsets.end())
326     return pair->second;
327
328   unsigned getPaddedSize;
329   unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
330   offsets[val] = offset;
331   incrementAutomaticVarsSize(getPaddedSize);
332   return offset;
333 }
334
335 int
336 MachineFunctionInfo::allocateSpilledValue(const Type* type)
337 {
338   assert(! spillsAreaFrozen &&
339          "Size of reg spills area has been used to compute an offset so "
340          "no more register spill slots should be allocated!");
341   
342   unsigned size  = MF.getTarget().getTargetData().getTypeSize(type);
343   unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
344   
345   bool growUp;
346   int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
347   
348   int offset = growUp? firstOffset + getRegSpillsSize()
349                      : firstOffset - (getRegSpillsSize() + size);
350
351   int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
352   size += abs(aligned - offset); // include alignment padding in size
353   
354   incrementRegSpillsSize(size);  // update size of reg. spills area
355
356   return aligned;
357 }
358
359 int
360 MachineFunctionInfo::pushTempValue(unsigned size)
361 {
362   unsigned align = SizeToAlignment(size, MF.getTarget());
363
364   bool growUp;
365   int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
366
367   int offset = growUp? firstOffset + currentTmpValuesSize
368                      : firstOffset - (currentTmpValuesSize + size);
369
370   int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
371                                                               align);
372   size += abs(aligned - offset); // include alignment padding in size
373
374   incrementTmpAreaSize(size);    // update "current" size of tmp area
375
376   return aligned;
377 }
378
379 void MachineFunctionInfo::popAllTempValues() {
380   resetTmpAreaSize();            // clear tmp area to reuse
381 }
382