1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
14 //===----------------------------------------------------------------------===//
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/Function.h"
26 #include "llvm/iOther.h"
27 #include "Support/LeakDetector.h"
31 static AnnotationID MF_AID(
32 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
36 struct Printer : public MachineFunctionPass {
38 const std::string Banner;
40 Printer (std::ostream *_OS, const std::string &_Banner) :
41 OS (_OS), Banner (_Banner) { }
43 const char *getPassName() const { return "MachineFunction Printer"; }
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 bool runOnMachineFunction(MachineFunction &MF) {
57 /// Returns a newly-created MachineFunction Printer pass. The default output
58 /// stream is std::cerr; the default banner is empty.
60 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
61 const std::string &Banner) {
62 return new Printer(OS, Banner);
66 struct Deleter : public MachineFunctionPass {
67 const char *getPassName() const { return "Machine Code Deleter"; }
69 bool runOnMachineFunction(MachineFunction &MF) {
70 // Delete the annotation from the function now.
71 MachineFunction::destruct(MF.getFunction());
77 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
78 /// the current function, which should happen after the function has been
79 /// emitted to a .s file or to memory.
80 FunctionPass *llvm::createMachineCodeDeleter() {
86 //===---------------------------------------------------------------------===//
87 // MachineFunction implementation
88 //===---------------------------------------------------------------------===//
89 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode()
91 MachineBasicBlock* dummy = new MachineBasicBlock();
92 LeakDetector::removeGarbageObject(dummy);
96 void ilist_traits<MachineBasicBlock>::transferNodesFromList(
97 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
98 ilist_iterator<MachineBasicBlock> first,
99 ilist_iterator<MachineBasicBlock> last)
101 if (Parent != toList.Parent)
102 for (; first != last; ++first)
103 first->Parent = toList.Parent;
106 MachineFunction::MachineFunction(const Function *F,
107 const TargetMachine &TM)
108 : Annotation(MF_AID), Fn(F), Target(TM), NextMBBNumber(0) {
109 SSARegMapping = new SSARegMap();
110 MFInfo = new MachineFunctionInfo(*this);
111 FrameInfo = new MachineFrameInfo();
112 ConstantPool = new MachineConstantPool();
113 BasicBlocks.Parent = this;
116 MachineFunction::~MachineFunction() {
117 delete SSARegMapping;
123 void MachineFunction::dump() const { print(std::cerr); }
125 void MachineFunction::print(std::ostream &OS) const {
126 OS << "# Machine code for " << Fn->getName () << "():\n";
128 // Print Frame Information
129 getFrameInfo()->print(*this, OS);
131 // Print Constant Pool
132 getConstantPool()->print(OS);
134 for (const_iterator BB = begin(); BB != end(); ++BB)
137 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
140 // The next two methods are used to construct and to retrieve
141 // the MachineCodeForFunction object for the given function.
142 // construct() -- Allocates and initializes for a given function and target
143 // get() -- Returns a handle to the object.
144 // This should not be called before "construct()"
145 // for a given Function.
148 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
150 assert(Fn->getAnnotation(MF_AID) == 0 &&
151 "Object already exists for this function!");
152 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
153 Fn->addAnnotation(mcInfo);
157 void MachineFunction::destruct(const Function *Fn) {
158 bool Deleted = Fn->deleteAnnotation(MF_AID);
159 assert(Deleted && "Machine code did not exist for function!");
162 MachineFunction& MachineFunction::get(const Function *F)
164 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
165 assert(mc && "Call construct() method first to allocate the object");
169 void MachineFunction::clearSSARegMap() {
170 delete SSARegMapping;
174 //===----------------------------------------------------------------------===//
175 // MachineFrameInfo implementation
176 //===----------------------------------------------------------------------===//
178 /// CreateStackObject - Create a stack object for a value of the specified type.
180 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
181 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
184 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
185 return CreateStackObject(RC->getSize(), RC->getAlignment());
189 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
190 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
192 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
193 const StackObject &SO = Objects[i];
194 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
196 OS << "variable sized";
198 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
200 if (i < NumFixedObjects)
202 if (i < NumFixedObjects || SO.SPOffset != -1) {
203 int Off = SO.SPOffset + ValOffset;
204 OS << " at location [SP";
214 if (HasVarSizedObjects)
215 OS << " Stack frame contains variable sized objects\n";
218 void MachineFrameInfo::dump(const MachineFunction &MF) const {
219 print(MF, std::cerr);
223 //===----------------------------------------------------------------------===//
224 // MachineConstantPool implementation
225 //===----------------------------------------------------------------------===//
227 void MachineConstantPool::print(std::ostream &OS) const {
228 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
229 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
232 void MachineConstantPool::dump() const { print(std::cerr); }
234 //===----------------------------------------------------------------------===//
235 // MachineFunctionInfo implementation
236 //===----------------------------------------------------------------------===//
239 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
240 unsigned &maxOptionalNumArgs)
242 const TargetFrameInfo &frameInfo = target.getFrameInfo();
244 unsigned maxSize = 0;
246 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
247 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
248 if (const CallInst *callInst = dyn_cast<CallInst>(I))
250 unsigned numOperands = callInst->getNumOperands() - 1;
251 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
255 unsigned sizeForThisCall;
256 if (frameInfo.argsOnStackHaveFixedSize())
258 int argSize = frameInfo.getSizeOfEachArgOnStack();
259 sizeForThisCall = numExtra * (unsigned) argSize;
263 assert(0 && "UNTESTED CODE: Size per stack argument is not "
264 "fixed on this architecture: use actual arg sizes to "
265 "compute MaxOptionalArgsSize");
267 for (unsigned i = 0; i < numOperands; ++i)
268 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
269 getOperand(i)->getType());
272 if (maxSize < sizeForThisCall)
273 maxSize = sizeForThisCall;
275 if ((int)maxOptionalNumArgs < numExtra)
276 maxOptionalNumArgs = (unsigned) numExtra;
282 // Align data larger than one L1 cache line on L1 cache line boundaries.
283 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
284 // but not higher than the alignment of the largest type we support
285 // (currently a double word). -- see class TargetData).
287 // This function is similar to the corresponding function in EmitAssembly.cpp
288 // but they are unrelated. This one does not align at more than a
289 // double-word boundary whereas that one might.
292 SizeToAlignment(unsigned size, const TargetMachine& target)
294 const unsigned short cacheLineSize = 16;
295 if (size > (unsigned) cacheLineSize / 2)
296 return cacheLineSize;
298 for (unsigned sz=1; /*no condition*/; sz *= 2)
299 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
304 void MachineFunctionInfo::CalculateArgSize() {
305 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
308 staticStackSize = maxOptionalArgsSize
309 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
313 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
314 unsigned &getPaddedSize,
318 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
319 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
322 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
324 int offset = growUp? firstOffset + getAutomaticVarsSize()
325 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
327 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
328 getPaddedSize = sizeToUse + abs(aligned - offset);
334 int MachineFunctionInfo::allocateLocalVar(const Value* val,
335 unsigned sizeToUse) {
336 assert(! automaticVarsAreaFrozen &&
337 "Size of auto vars area has been used to compute an offset so "
338 "no more automatic vars should be allocated!");
340 // Check if we've allocated a stack slot for this value already
342 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
343 if (pair != offsets.end())
346 unsigned getPaddedSize;
347 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
348 offsets[val] = offset;
349 incrementAutomaticVarsSize(getPaddedSize);
354 MachineFunctionInfo::allocateSpilledValue(const Type* type)
356 assert(! spillsAreaFrozen &&
357 "Size of reg spills area has been used to compute an offset so "
358 "no more register spill slots should be allocated!");
360 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
361 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
364 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
366 int offset = growUp? firstOffset + getRegSpillsSize()
367 : firstOffset - (getRegSpillsSize() + size);
369 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
370 size += abs(aligned - offset); // include alignment padding in size
372 incrementRegSpillsSize(size); // update size of reg. spills area
378 MachineFunctionInfo::pushTempValue(unsigned size)
380 unsigned align = SizeToAlignment(size, MF.getTarget());
383 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
385 int offset = growUp? firstOffset + currentTmpValuesSize
386 : firstOffset - (currentTmpValuesSize + size);
388 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
390 size += abs(aligned - offset); // include alignment padding in size
392 incrementTmpAreaSize(size); // update "current" size of tmp area
397 void MachineFunctionInfo::popAllTempValues() {
398 resetTmpAreaSize(); // clear tmp area to reuse