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 "llvm/Type.h"
28 #include "Support/LeakDetector.h"
32 static AnnotationID MF_AID(
33 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
37 struct Printer : public MachineFunctionPass {
39 const std::string Banner;
41 Printer (std::ostream *_OS, const std::string &_Banner) :
42 OS (_OS), Banner (_Banner) { }
44 const char *getPassName() const { return "MachineFunction Printer"; }
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 bool runOnMachineFunction(MachineFunction &MF) {
58 /// Returns a newly-created MachineFunction Printer pass. The default output
59 /// stream is std::cerr; the default banner is empty.
61 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
62 const std::string &Banner) {
63 return new Printer(OS, Banner);
67 struct Deleter : public MachineFunctionPass {
68 const char *getPassName() const { return "Machine Code Deleter"; }
70 bool runOnMachineFunction(MachineFunction &MF) {
71 // Delete the annotation from the function now.
72 MachineFunction::destruct(MF.getFunction());
78 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
79 /// the current function, which should happen after the function has been
80 /// emitted to a .s file or to memory.
81 FunctionPass *llvm::createMachineCodeDeleter() {
87 //===---------------------------------------------------------------------===//
88 // MachineFunction implementation
89 //===---------------------------------------------------------------------===//
90 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode()
92 MachineBasicBlock* dummy = new MachineBasicBlock();
93 LeakDetector::removeGarbageObject(dummy);
97 void ilist_traits<MachineBasicBlock>::transferNodesFromList(
98 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
99 ilist_iterator<MachineBasicBlock> first,
100 ilist_iterator<MachineBasicBlock> last)
102 if (Parent != toList.Parent)
103 for (; first != last; ++first)
104 first->Parent = toList.Parent;
107 MachineFunction::MachineFunction(const Function *F,
108 const TargetMachine &TM)
109 : Annotation(MF_AID), Fn(F), Target(TM) {
110 SSARegMapping = new SSARegMap();
111 MFInfo = new MachineFunctionInfo(*this);
112 FrameInfo = new MachineFrameInfo();
113 ConstantPool = new MachineConstantPool();
114 BasicBlocks.Parent = this;
117 MachineFunction::~MachineFunction() {
119 delete SSARegMapping;
125 void MachineFunction::dump() const { print(std::cerr); }
127 void MachineFunction::print(std::ostream &OS) const {
128 OS << "# Machine code for " << Fn->getName () << "():\n";
130 // Print Frame Information
131 getFrameInfo()->print(*this, OS);
133 // Print Constant Pool
134 getConstantPool()->print(OS);
136 for (const_iterator BB = begin(); BB != end(); ++BB)
139 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
142 // The next two methods are used to construct and to retrieve
143 // the MachineCodeForFunction object for the given function.
144 // construct() -- Allocates and initializes for a given function and target
145 // get() -- Returns a handle to the object.
146 // This should not be called before "construct()"
147 // for a given Function.
150 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
152 assert(Fn->getAnnotation(MF_AID) == 0 &&
153 "Object already exists for this function!");
154 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
155 Fn->addAnnotation(mcInfo);
159 void MachineFunction::destruct(const Function *Fn) {
160 bool Deleted = Fn->deleteAnnotation(MF_AID);
161 assert(Deleted && "Machine code did not exist for function!");
164 MachineFunction& MachineFunction::get(const Function *F)
166 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
167 assert(mc && "Call construct() method first to allocate the object");
171 void MachineFunction::clearSSARegMap() {
172 delete SSARegMapping;
176 //===----------------------------------------------------------------------===//
177 // MachineFrameInfo implementation
178 //===----------------------------------------------------------------------===//
180 /// CreateStackObject - Create a stack object for a value of the specified type.
182 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
183 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
186 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
187 return CreateStackObject(RC->getSize(), RC->getAlignment());
191 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
192 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
194 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
195 const StackObject &SO = Objects[i];
196 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
198 OS << "variable sized";
200 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
202 if (i < NumFixedObjects)
204 if (i < NumFixedObjects || SO.SPOffset != -1) {
205 int Off = SO.SPOffset - ValOffset;
206 OS << " at location [SP";
216 if (HasVarSizedObjects)
217 OS << " Stack frame contains variable sized objects\n";
220 void MachineFrameInfo::dump(const MachineFunction &MF) const {
221 print(MF, std::cerr);
225 //===----------------------------------------------------------------------===//
226 // MachineConstantPool implementation
227 //===----------------------------------------------------------------------===//
229 void MachineConstantPool::print(std::ostream &OS) const {
230 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
231 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
234 void MachineConstantPool::dump() const { print(std::cerr); }
236 //===----------------------------------------------------------------------===//
237 // MachineFunctionInfo implementation
238 //===----------------------------------------------------------------------===//
241 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
242 unsigned &maxOptionalNumArgs)
244 const TargetFrameInfo &frameInfo = *target.getFrameInfo();
246 unsigned maxSize = 0;
248 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
249 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
250 if (const CallInst *callInst = dyn_cast<CallInst>(I))
252 unsigned numOperands = callInst->getNumOperands() - 1;
253 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
257 unsigned sizeForThisCall;
258 if (frameInfo.argsOnStackHaveFixedSize())
260 int argSize = frameInfo.getSizeOfEachArgOnStack();
261 sizeForThisCall = numExtra * (unsigned) argSize;
265 assert(0 && "UNTESTED CODE: Size per stack argument is not "
266 "fixed on this architecture: use actual arg sizes to "
267 "compute MaxOptionalArgsSize");
269 for (unsigned i = 0; i < numOperands; ++i)
270 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
271 getOperand(i)->getType());
274 if (maxSize < sizeForThisCall)
275 maxSize = sizeForThisCall;
277 if ((int)maxOptionalNumArgs < numExtra)
278 maxOptionalNumArgs = (unsigned) numExtra;
284 // Align data larger than one L1 cache line on L1 cache line boundaries.
285 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
286 // but not higher than the alignment of the largest type we support
287 // (currently a double word). -- see class TargetData).
289 // This function is similar to the corresponding function in EmitAssembly.cpp
290 // but they are unrelated. This one does not align at more than a
291 // double-word boundary whereas that one might.
294 SizeToAlignment(unsigned size, const TargetMachine& target)
296 const unsigned short cacheLineSize = 16;
297 if (size > (unsigned) cacheLineSize / 2)
298 return cacheLineSize;
300 for (unsigned sz=1; /*no condition*/; sz *= 2)
301 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
306 void MachineFunctionInfo::CalculateArgSize() {
307 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
310 staticStackSize = maxOptionalArgsSize
311 + MF.getTarget().getFrameInfo()->getMinStackFrameSize();
315 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
316 unsigned &getPaddedSize,
319 if (sizeToUse == 0) {
320 // All integer types smaller than ints promote to 4 byte integers.
321 if (val->getType()->isIntegral() && val->getType()->getPrimitiveSize() < 4)
324 sizeToUse = MF.getTarget().getTargetData().getTypeSize(val->getType());
326 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
329 int firstOffset = MF.getTarget().getFrameInfo()->getFirstAutomaticVarOffset(MF,
331 int offset = growUp? firstOffset + getAutomaticVarsSize()
332 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
334 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
335 getPaddedSize = sizeToUse + abs(aligned - offset);
341 int MachineFunctionInfo::allocateLocalVar(const Value* val,
342 unsigned sizeToUse) {
343 assert(! automaticVarsAreaFrozen &&
344 "Size of auto vars area has been used to compute an offset so "
345 "no more automatic vars should be allocated!");
347 // Check if we've allocated a stack slot for this value already
349 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
350 if (pair != offsets.end())
353 unsigned getPaddedSize;
354 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
355 offsets[val] = offset;
356 incrementAutomaticVarsSize(getPaddedSize);
361 MachineFunctionInfo::allocateSpilledValue(const Type* type)
363 assert(! spillsAreaFrozen &&
364 "Size of reg spills area has been used to compute an offset so "
365 "no more register spill slots should be allocated!");
367 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
368 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
371 int firstOffset = MF.getTarget().getFrameInfo()->getRegSpillAreaOffset(MF, growUp);
373 int offset = growUp? firstOffset + getRegSpillsSize()
374 : firstOffset - (getRegSpillsSize() + size);
376 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
377 size += abs(aligned - offset); // include alignment padding in size
379 incrementRegSpillsSize(size); // update size of reg. spills area
385 MachineFunctionInfo::pushTempValue(unsigned size)
387 unsigned align = SizeToAlignment(size, MF.getTarget());
390 int firstOffset = MF.getTarget().getFrameInfo()->getTmpAreaOffset(MF, growUp);
392 int offset = growUp? firstOffset + currentTmpValuesSize
393 : firstOffset - (currentTmpValuesSize + size);
395 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp,
397 size += abs(aligned - offset); // include alignment padding in size
399 incrementTmpAreaSize(size); // update "current" size of tmp area
404 void MachineFunctionInfo::popAllTempValues() {
405 resetTmpAreaSize(); // clear tmp area to reuse