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/Target/TargetCacheInfo.h"
26 #include "llvm/Function.h"
27 #include "llvm/iOther.h"
30 static AnnotationID MF_AID(
31 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
35 struct Printer : public MachineFunctionPass {
37 const std::string Banner;
39 Printer (std::ostream *_OS, const std::string &_Banner) :
40 OS (_OS), Banner (_Banner) { }
42 const char *getPassName() const { return "MachineFunction Printer"; }
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 bool runOnMachineFunction(MachineFunction &MF) {
56 /// Returns a newly-created MachineFunction Printer pass. The default output
57 /// stream is std::cerr; the default banner is empty.
59 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
60 const std::string &Banner) {
61 return new Printer(OS, Banner);
65 struct Deleter : public MachineFunctionPass {
66 const char *getPassName() const { return "Machine Code Deleter"; }
68 bool runOnMachineFunction(MachineFunction &MF) {
69 // Delete the annotation from the function now.
70 MachineFunction::destruct(MF.getFunction());
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() {
85 //===---------------------------------------------------------------------===//
86 // MachineFunction implementation
87 //===---------------------------------------------------------------------===//
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();
98 MachineFunction::~MachineFunction() {
105 void MachineFunction::dump() const { print(std::cerr); }
107 void MachineFunction::print(std::ostream &OS) const {
108 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
111 // Print Frame Information
112 getFrameInfo()->print(*this, OS);
114 // Print Constant Pool
115 getConstantPool()->print(OS);
117 for (const_iterator BB = begin(); BB != end(); ++BB)
119 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
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.
130 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
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);
139 void MachineFunction::destruct(const Function *Fn) {
140 bool Deleted = Fn->deleteAnnotation(MF_AID);
141 assert(Deleted && "Machine code did not exist for function!");
144 MachineFunction& MachineFunction::get(const Function *F)
146 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
147 assert(mc && "Call construct() method first to allocate the object");
151 void MachineFunction::clearSSARegMap() {
152 delete SSARegMapping;
156 //===----------------------------------------------------------------------===//
157 // MachineFrameInfo implementation
158 //===----------------------------------------------------------------------===//
160 /// CreateStackObject - Create a stack object for a value of the specified type.
162 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
163 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
166 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
167 return CreateStackObject(RC->getSize(), RC->getAlignment());
171 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
172 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
174 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
175 const StackObject &SO = Objects[i];
176 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
178 OS << "variable sized";
180 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
182 if (i < NumFixedObjects)
184 if (i < NumFixedObjects || SO.SPOffset != -1) {
185 int Off = SO.SPOffset + ValOffset;
186 OS << " at location [SP";
196 if (HasVarSizedObjects)
197 OS << " Stack frame contains variable sized objects\n";
200 void MachineFrameInfo::dump(const MachineFunction &MF) const {
201 print(MF, std::cerr);
205 //===----------------------------------------------------------------------===//
206 // MachineConstantPool implementation
207 //===----------------------------------------------------------------------===//
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";
214 void MachineConstantPool::dump() const { print(std::cerr); }
216 //===----------------------------------------------------------------------===//
217 // MachineFunctionInfo implementation
218 //===----------------------------------------------------------------------===//
221 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
222 unsigned &maxOptionalNumArgs)
224 const TargetFrameInfo &frameInfo = target.getFrameInfo();
226 unsigned maxSize = 0;
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))
232 unsigned numOperands = callInst->getNumOperands() - 1;
233 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
237 unsigned sizeForThisCall;
238 if (frameInfo.argsOnStackHaveFixedSize())
240 int argSize = frameInfo.getSizeOfEachArgOnStack();
241 sizeForThisCall = numExtra * (unsigned) argSize;
245 assert(0 && "UNTESTED CODE: Size per stack argument is not "
246 "fixed on this architecture: use actual arg sizes to "
247 "compute MaxOptionalArgsSize");
249 for (unsigned i = 0; i < numOperands; ++i)
250 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
251 getOperand(i)->getType());
254 if (maxSize < sizeForThisCall)
255 maxSize = sizeForThisCall;
257 if ((int)maxOptionalNumArgs < numExtra)
258 maxOptionalNumArgs = (unsigned) numExtra;
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).
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.
274 SizeToAlignment(unsigned size, const TargetMachine& target)
276 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
277 if (size > (unsigned) cacheLineSize / 2)
278 return cacheLineSize;
280 for (unsigned sz=1; /*no condition*/; sz *= 2)
281 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
286 void MachineFunctionInfo::CalculateArgSize() {
287 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
290 staticStackSize = maxOptionalArgsSize
291 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
295 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
296 unsigned &getPaddedSize,
300 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
301 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
304 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
306 int offset = growUp? firstOffset + getAutomaticVarsSize()
307 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
309 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
310 getPaddedSize = sizeToUse + abs(aligned - offset);
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!");
322 // Check if we've allocated a stack slot for this value already
324 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
325 if (pair != offsets.end())
328 unsigned getPaddedSize;
329 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
330 offsets[val] = offset;
331 incrementAutomaticVarsSize(getPaddedSize);
336 MachineFunctionInfo::allocateSpilledValue(const Type* type)
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!");
342 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
343 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
346 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
348 int offset = growUp? firstOffset + getRegSpillsSize()
349 : firstOffset - (getRegSpillsSize() + size);
351 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
352 size += abs(aligned - offset); // include alignment padding in size
354 incrementRegSpillsSize(size); // update size of reg. spills area
360 MachineFunctionInfo::pushTempValue(unsigned size)
362 unsigned align = SizeToAlignment(size, MF.getTarget());
365 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
367 int offset = growUp? firstOffset + currentTmpValuesSize
368 : firstOffset - (currentTmpValuesSize + size);
370 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
372 size += abs(aligned - offset); // include alignment padding in size
374 incrementTmpAreaSize(size); // update "current" size of tmp area
379 void MachineFunctionInfo::popAllTempValues() {
380 resetTmpAreaSize(); // clear tmp area to reuse