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/MachineFunction.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/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 #include "llvm/Pass.h"
29 #include "Config/limits.h"
33 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
35 static AnnotationID MF_AID(
36 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
39 //===---------------------------------------------------------------------===//
40 // Code generation/destruction passes
41 //===---------------------------------------------------------------------===//
44 class ConstructMachineFunction : public FunctionPass {
45 TargetMachine &Target;
47 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
49 const char *getPassName() const {
50 return "ConstructMachineFunction";
53 bool runOnFunction(Function &F) {
54 MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
59 struct DestroyMachineFunction : public FunctionPass {
60 const char *getPassName() const { return "FreeMachineFunction"; }
62 static void freeMachineCode(Instruction &I) {
63 MachineCodeForInstruction::destroy(&I);
66 bool runOnFunction(Function &F) {
67 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
68 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
69 MachineCodeForInstruction::get(I).dropAllReferences();
71 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
72 for_each(FI->begin(), FI->end(), freeMachineCode);
78 struct Printer : public FunctionPass {
79 const char *getPassName() const { return "MachineFunction Printer"; }
81 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 bool runOnFunction(Function &F) {
86 MachineFunction::get(&F).dump();
92 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
93 return new ConstructMachineFunction(Target);
96 FunctionPass *createMachineCodeDestructionPass() {
97 return new DestroyMachineFunction();
100 FunctionPass *createMachineFunctionPrinterPass() {
101 return new Printer();
105 //===---------------------------------------------------------------------===//
106 // MachineFunction implementation
107 //===---------------------------------------------------------------------===//
109 MachineFunction::MachineFunction(const Function *F,
110 const TargetMachine &TM)
111 : Annotation(MF_AID), Fn(F), Target(TM) {
112 SSARegMapping = new SSARegMap();
113 MFInfo = new MachineFunctionInfo(*this);
114 FrameInfo = new MachineFrameInfo();
115 ConstantPool = new MachineConstantPool();
118 MachineFunction::~MachineFunction() {
119 delete SSARegMapping;
125 void MachineFunction::dump() const { print(std::cerr); }
127 void MachineFunction::print(std::ostream &OS) const {
128 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
131 // Print Frame Information
132 getFrameInfo()->print(*this, OS);
134 // Print Constant Pool
135 getConstantPool()->print(OS);
137 for (const_iterator BB = begin(); BB != end(); ++BB) {
138 const BasicBlock *LBB = BB->getBasicBlock();
139 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
140 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
142 (*I)->print(OS, Target);
145 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
149 // The next two methods are used to construct and to retrieve
150 // the MachineCodeForFunction object for the given function.
151 // construct() -- Allocates and initializes for a given function and target
152 // get() -- Returns a handle to the object.
153 // This should not be called before "construct()"
154 // for a given Function.
157 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
159 assert(Fn->getAnnotation(MF_AID) == 0 &&
160 "Object already exists for this function!");
161 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
162 Fn->addAnnotation(mcInfo);
167 MachineFunction::destruct(const Function *Fn)
169 bool Deleted = Fn->deleteAnnotation(MF_AID);
170 assert(Deleted && "Machine code did not exist for function!");
173 MachineFunction& MachineFunction::get(const Function *F)
175 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
176 assert(mc && "Call construct() method first to allocate the object");
180 void MachineFunction::clearSSARegMap() {
181 delete SSARegMapping;
185 //===----------------------------------------------------------------------===//
186 // MachineFrameInfo implementation
187 //===----------------------------------------------------------------------===//
189 /// CreateStackObject - Create a stack object for a value of the specified type.
191 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
192 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
195 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
196 return CreateStackObject(RC->getSize(), RC->getAlignment());
200 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
201 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
203 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
204 const StackObject &SO = Objects[i];
205 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
207 OS << "variable sized";
209 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
211 if (i < NumFixedObjects)
213 if (i < NumFixedObjects || SO.SPOffset != -1) {
214 int Off = SO.SPOffset + ValOffset;
215 OS << " at location [SP";
225 if (HasVarSizedObjects)
226 OS << " Stack frame contains variable sized objects\n";
229 void MachineFrameInfo::dump(const MachineFunction &MF) const {
230 print(MF, std::cerr);
234 //===----------------------------------------------------------------------===//
235 // MachineConstantPool implementation
236 //===----------------------------------------------------------------------===//
238 void MachineConstantPool::print(std::ostream &OS) const {
239 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
240 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
243 void MachineConstantPool::dump() const { print(std::cerr); }
245 //===----------------------------------------------------------------------===//
246 // MachineFunctionInfo implementation
247 //===----------------------------------------------------------------------===//
250 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
251 unsigned &maxOptionalNumArgs)
253 const TargetFrameInfo &frameInfo = target.getFrameInfo();
255 unsigned maxSize = 0;
257 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
258 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
259 if (const CallInst *callInst = dyn_cast<CallInst>(I))
261 unsigned numOperands = callInst->getNumOperands() - 1;
262 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
266 unsigned sizeForThisCall;
267 if (frameInfo.argsOnStackHaveFixedSize())
269 int argSize = frameInfo.getSizeOfEachArgOnStack();
270 sizeForThisCall = numExtra * (unsigned) argSize;
274 assert(0 && "UNTESTED CODE: Size per stack argument is not "
275 "fixed on this architecture: use actual arg sizes to "
276 "compute MaxOptionalArgsSize");
278 for (unsigned i = 0; i < numOperands; ++i)
279 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
280 getOperand(i)->getType());
283 if (maxSize < sizeForThisCall)
284 maxSize = sizeForThisCall;
286 if ((int)maxOptionalNumArgs < numExtra)
287 maxOptionalNumArgs = (unsigned) numExtra;
293 // Align data larger than one L1 cache line on L1 cache line boundaries.
294 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
295 // but not higher than the alignment of the largest type we support
296 // (currently a double word). -- see class TargetData).
298 // This function is similar to the corresponding function in EmitAssembly.cpp
299 // but they are unrelated. This one does not align at more than a
300 // double-word boundary whereas that one might.
303 SizeToAlignment(unsigned size, const TargetMachine& target)
305 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
306 if (size > (unsigned) cacheLineSize / 2)
307 return cacheLineSize;
309 for (unsigned sz=1; /*no condition*/; sz *= 2)
310 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
315 void MachineFunctionInfo::CalculateArgSize() {
316 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
319 staticStackSize = maxOptionalArgsSize
320 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
324 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
325 unsigned &getPaddedSize,
329 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
330 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
333 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
335 int offset = growUp? firstOffset + getAutomaticVarsSize()
336 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
338 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
339 getPaddedSize = sizeToUse + abs(aligned - offset);
345 MachineFunctionInfo::allocateLocalVar(const Value* val,
348 assert(! automaticVarsAreaFrozen &&
349 "Size of auto vars area has been used to compute an offset so "
350 "no more automatic vars should be allocated!");
352 // Check if we've allocated a stack slot for this value already
354 int offset = getOffset(val);
355 if (offset == INVALID_FRAME_OFFSET)
357 unsigned getPaddedSize;
358 offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
359 offsets[val] = offset;
360 incrementAutomaticVarsSize(getPaddedSize);
366 MachineFunctionInfo::allocateSpilledValue(const Type* type)
368 assert(! spillsAreaFrozen &&
369 "Size of reg spills area has been used to compute an offset so "
370 "no more register spill slots should be allocated!");
372 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
373 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
376 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
378 int offset = growUp? firstOffset + getRegSpillsSize()
379 : firstOffset - (getRegSpillsSize() + size);
381 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
382 size += abs(aligned - offset); // include alignment padding in size
384 incrementRegSpillsSize(size); // update size of reg. spills area
390 MachineFunctionInfo::pushTempValue(unsigned size)
392 unsigned align = SizeToAlignment(size, MF.getTarget());
395 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
397 int offset = growUp? firstOffset + currentTmpValuesSize
398 : firstOffset - (currentTmpValuesSize + size);
400 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
402 size += abs(aligned - offset); // include alignment padding in size
404 incrementTmpAreaSize(size); // update "current" size of tmp area
409 void MachineFunctionInfo::popAllTempValues() {
410 resetTmpAreaSize(); // clear tmp area to reuse
414 MachineFunctionInfo::getOffset(const Value* val) const
416 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
417 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
420 } // End llvm namespace