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/MachineCodeForInstruction.h"
19 #include "llvm/CodeGen/SSARegMap.h"
20 #include "llvm/CodeGen/MachineFunctionInfo.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Target/TargetCacheInfo.h"
27 #include "llvm/Function.h"
28 #include "llvm/iOther.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);
65 //===---------------------------------------------------------------------===//
66 // MachineFunction implementation
67 //===---------------------------------------------------------------------===//
69 MachineFunction::MachineFunction(const Function *F,
70 const TargetMachine &TM)
71 : Annotation(MF_AID), Fn(F), Target(TM) {
72 SSARegMapping = new SSARegMap();
73 MFInfo = new MachineFunctionInfo(*this);
74 FrameInfo = new MachineFrameInfo();
75 ConstantPool = new MachineConstantPool();
78 MachineFunction::~MachineFunction() {
85 void MachineFunction::dump() const { print(std::cerr); }
87 void MachineFunction::print(std::ostream &OS) const {
88 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
91 // Print Frame Information
92 getFrameInfo()->print(*this, OS);
94 // Print Constant Pool
95 getConstantPool()->print(OS);
97 for (const_iterator BB = begin(); BB != end(); ++BB) {
98 const BasicBlock *LBB = BB->getBasicBlock();
99 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
100 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
102 I->print(OS, Target);
105 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
109 // The next two methods are used to construct and to retrieve
110 // the MachineCodeForFunction object for the given function.
111 // construct() -- Allocates and initializes for a given function and target
112 // get() -- Returns a handle to the object.
113 // This should not be called before "construct()"
114 // for a given Function.
117 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
119 assert(Fn->getAnnotation(MF_AID) == 0 &&
120 "Object already exists for this function!");
121 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
122 Fn->addAnnotation(mcInfo);
126 void MachineFunction::destruct(const Function *Fn) {
127 bool Deleted = Fn->deleteAnnotation(MF_AID);
128 assert(Deleted && "Machine code did not exist for function!");
131 MachineFunction& MachineFunction::get(const Function *F)
133 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
134 assert(mc && "Call construct() method first to allocate the object");
138 void MachineFunction::clearSSARegMap() {
139 delete SSARegMapping;
143 //===----------------------------------------------------------------------===//
144 // MachineFrameInfo implementation
145 //===----------------------------------------------------------------------===//
147 /// CreateStackObject - Create a stack object for a value of the specified type.
149 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
150 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
153 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
154 return CreateStackObject(RC->getSize(), RC->getAlignment());
158 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
159 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
161 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
162 const StackObject &SO = Objects[i];
163 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
165 OS << "variable sized";
167 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
169 if (i < NumFixedObjects)
171 if (i < NumFixedObjects || SO.SPOffset != -1) {
172 int Off = SO.SPOffset + ValOffset;
173 OS << " at location [SP";
183 if (HasVarSizedObjects)
184 OS << " Stack frame contains variable sized objects\n";
187 void MachineFrameInfo::dump(const MachineFunction &MF) const {
188 print(MF, std::cerr);
192 //===----------------------------------------------------------------------===//
193 // MachineConstantPool implementation
194 //===----------------------------------------------------------------------===//
196 void MachineConstantPool::print(std::ostream &OS) const {
197 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
198 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
201 void MachineConstantPool::dump() const { print(std::cerr); }
203 //===----------------------------------------------------------------------===//
204 // MachineFunctionInfo implementation
205 //===----------------------------------------------------------------------===//
208 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
209 unsigned &maxOptionalNumArgs)
211 const TargetFrameInfo &frameInfo = target.getFrameInfo();
213 unsigned maxSize = 0;
215 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
216 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
217 if (const CallInst *callInst = dyn_cast<CallInst>(I))
219 unsigned numOperands = callInst->getNumOperands() - 1;
220 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
224 unsigned sizeForThisCall;
225 if (frameInfo.argsOnStackHaveFixedSize())
227 int argSize = frameInfo.getSizeOfEachArgOnStack();
228 sizeForThisCall = numExtra * (unsigned) argSize;
232 assert(0 && "UNTESTED CODE: Size per stack argument is not "
233 "fixed on this architecture: use actual arg sizes to "
234 "compute MaxOptionalArgsSize");
236 for (unsigned i = 0; i < numOperands; ++i)
237 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
238 getOperand(i)->getType());
241 if (maxSize < sizeForThisCall)
242 maxSize = sizeForThisCall;
244 if ((int)maxOptionalNumArgs < numExtra)
245 maxOptionalNumArgs = (unsigned) numExtra;
251 // Align data larger than one L1 cache line on L1 cache line boundaries.
252 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
253 // but not higher than the alignment of the largest type we support
254 // (currently a double word). -- see class TargetData).
256 // This function is similar to the corresponding function in EmitAssembly.cpp
257 // but they are unrelated. This one does not align at more than a
258 // double-word boundary whereas that one might.
261 SizeToAlignment(unsigned size, const TargetMachine& target)
263 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
264 if (size > (unsigned) cacheLineSize / 2)
265 return cacheLineSize;
267 for (unsigned sz=1; /*no condition*/; sz *= 2)
268 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
273 void MachineFunctionInfo::CalculateArgSize() {
274 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
277 staticStackSize = maxOptionalArgsSize
278 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
282 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
283 unsigned &getPaddedSize,
287 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
288 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
291 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
293 int offset = growUp? firstOffset + getAutomaticVarsSize()
294 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
296 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
297 getPaddedSize = sizeToUse + abs(aligned - offset);
303 int MachineFunctionInfo::allocateLocalVar(const Value* val,
304 unsigned sizeToUse) {
305 assert(! automaticVarsAreaFrozen &&
306 "Size of auto vars area has been used to compute an offset so "
307 "no more automatic vars should be allocated!");
309 // Check if we've allocated a stack slot for this value already
311 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
312 if (pair != offsets.end())
315 unsigned getPaddedSize;
316 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
317 offsets[val] = offset;
318 incrementAutomaticVarsSize(getPaddedSize);
323 MachineFunctionInfo::allocateSpilledValue(const Type* type)
325 assert(! spillsAreaFrozen &&
326 "Size of reg spills area has been used to compute an offset so "
327 "no more register spill slots should be allocated!");
329 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
330 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
333 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
335 int offset = growUp? firstOffset + getRegSpillsSize()
336 : firstOffset - (getRegSpillsSize() + size);
338 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
339 size += abs(aligned - offset); // include alignment padding in size
341 incrementRegSpillsSize(size); // update size of reg. spills area
347 MachineFunctionInfo::pushTempValue(unsigned size)
349 unsigned align = SizeToAlignment(size, MF.getTarget());
352 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
354 int offset = growUp? firstOffset + currentTmpValuesSize
355 : firstOffset - (currentTmpValuesSize + size);
357 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
359 size += abs(aligned - offset); // include alignment padding in size
361 incrementTmpAreaSize(size); // update "current" size of tmp area
366 void MachineFunctionInfo::popAllTempValues() {
367 resetTmpAreaSize(); // clear tmp area to reuse