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"
33 static AnnotationID MF_AID(
34 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
38 struct Printer : public MachineFunctionPass {
40 const std::string Banner;
42 Printer (std::ostream *_OS, const std::string &_Banner) :
43 OS (_OS), Banner (_Banner) { }
45 const char *getPassName() const { return "MachineFunction Printer"; }
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 bool runOnMachineFunction(MachineFunction &MF) {
59 /// Returns a newly-created MachineFunction Printer pass. The default output
60 /// stream is std::cerr; the default banner is empty.
62 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
63 const std::string &Banner) {
64 return new Printer(OS, Banner);
68 struct Deleter : public MachineFunctionPass {
69 const char *getPassName() const { return "Machine Code Deleter"; }
71 bool runOnMachineFunction(MachineFunction &MF) {
72 // Delete the annotation from the function now.
73 MachineFunction::destruct(MF.getFunction());
79 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
80 /// the current function, which should happen after the function has been
81 /// emitted to a .s file or to memory.
82 FunctionPass *llvm::createMachineCodeDeleter() {
88 //===---------------------------------------------------------------------===//
89 // MachineFunction implementation
90 //===---------------------------------------------------------------------===//
91 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode()
93 MachineBasicBlock* dummy = new MachineBasicBlock();
94 LeakDetector::removeGarbageObject(dummy);
98 void ilist_traits<MachineBasicBlock>::transferNodesFromList(
99 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
100 ilist_iterator<MachineBasicBlock> first,
101 ilist_iterator<MachineBasicBlock> last)
103 if (Parent != toList.Parent)
104 for (; first != last; ++first)
105 first->Parent = toList.Parent;
108 MachineFunction::MachineFunction(const Function *F,
109 const TargetMachine &TM)
110 : Annotation(MF_AID), Fn(F), Target(TM) {
111 SSARegMapping = new SSARegMap();
112 MFInfo = new MachineFunctionInfo(*this);
113 FrameInfo = new MachineFrameInfo();
114 ConstantPool = new MachineConstantPool();
115 BasicBlocks.Parent = this;
118 MachineFunction::~MachineFunction() {
120 delete SSARegMapping;
126 void MachineFunction::dump() const { print(std::cerr); }
128 void MachineFunction::print(std::ostream &OS) const {
129 OS << "# Machine code for " << Fn->getName () << "():\n";
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)
140 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
143 // The next two methods are used to construct and to retrieve
144 // the MachineCodeForFunction object for the given function.
145 // construct() -- Allocates and initializes for a given function and target
146 // get() -- Returns a handle to the object.
147 // This should not be called before "construct()"
148 // for a given Function.
151 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
153 assert(Fn->getAnnotation(MF_AID) == 0 &&
154 "Object already exists for this function!");
155 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
156 Fn->addAnnotation(mcInfo);
160 void MachineFunction::destruct(const Function *Fn) {
161 bool Deleted = Fn->deleteAnnotation(MF_AID);
162 assert(Deleted && "Machine code did not exist for function!");
165 MachineFunction& MachineFunction::get(const Function *F)
167 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
168 assert(mc && "Call construct() method first to allocate the object");
172 void MachineFunction::clearSSARegMap() {
173 delete SSARegMapping;
177 //===----------------------------------------------------------------------===//
178 // MachineFrameInfo implementation
179 //===----------------------------------------------------------------------===//
181 /// CreateStackObject - Create a stack object for a value of the specified type.
183 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
184 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
187 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
188 return CreateStackObject(RC->getSize(), RC->getAlignment());
192 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
193 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
195 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
196 const StackObject &SO = Objects[i];
197 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
199 OS << "variable sized";
201 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
203 if (i < NumFixedObjects)
205 if (i < NumFixedObjects || SO.SPOffset != -1) {
206 int Off = SO.SPOffset - ValOffset;
207 OS << " at location [SP";
217 if (HasVarSizedObjects)
218 OS << " Stack frame contains variable sized objects\n";
221 void MachineFrameInfo::dump(const MachineFunction &MF) const {
222 print(MF, std::cerr);
226 //===----------------------------------------------------------------------===//
227 // MachineConstantPool implementation
228 //===----------------------------------------------------------------------===//
230 void MachineConstantPool::print(std::ostream &OS) const {
231 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
232 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
235 void MachineConstantPool::dump() const { print(std::cerr); }
237 //===----------------------------------------------------------------------===//
238 // MachineFunctionInfo implementation
239 //===----------------------------------------------------------------------===//
242 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
243 unsigned &maxOptionalNumArgs)
245 const TargetFrameInfo &frameInfo = *target.getFrameInfo();
247 unsigned maxSize = 0;
249 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
250 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
251 if (const CallInst *callInst = dyn_cast<CallInst>(I))
253 unsigned numOperands = callInst->getNumOperands() - 1;
254 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
258 unsigned sizeForThisCall;
259 if (frameInfo.argsOnStackHaveFixedSize())
261 int argSize = frameInfo.getSizeOfEachArgOnStack();
262 sizeForThisCall = numExtra * (unsigned) argSize;
266 assert(0 && "UNTESTED CODE: Size per stack argument is not "
267 "fixed on this architecture: use actual arg sizes to "
268 "compute MaxOptionalArgsSize");
270 for (unsigned i = 0; i < numOperands; ++i)
271 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
272 getOperand(i)->getType());
275 if (maxSize < sizeForThisCall)
276 maxSize = sizeForThisCall;
278 if ((int)maxOptionalNumArgs < numExtra)
279 maxOptionalNumArgs = (unsigned) numExtra;
285 // Align data larger than one L1 cache line on L1 cache line boundaries.
286 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
287 // but not higher than the alignment of the largest type we support
288 // (currently a double word). -- see class TargetData).
290 // This function is similar to the corresponding function in EmitAssembly.cpp
291 // but they are unrelated. This one does not align at more than a
292 // double-word boundary whereas that one might.
295 SizeToAlignment(unsigned size, const TargetMachine& target)
297 const unsigned short cacheLineSize = 16;
298 if (size > (unsigned) cacheLineSize / 2)
299 return cacheLineSize;
301 for (unsigned sz=1; /*no condition*/; sz *= 2)
302 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
307 void MachineFunctionInfo::CalculateArgSize() {
308 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
311 staticStackSize = maxOptionalArgsSize
312 + MF.getTarget().getFrameInfo()->getMinStackFrameSize();
316 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
317 unsigned &getPaddedSize,
320 if (sizeToUse == 0) {
321 // All integer types smaller than ints promote to 4 byte integers.
322 if (val->getType()->isIntegral() && val->getType()->getPrimitiveSize() < 4)
325 sizeToUse = MF.getTarget().getTargetData().getTypeSize(val->getType());
327 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
330 int firstOffset = MF.getTarget().getFrameInfo()->getFirstAutomaticVarOffset(MF,
332 int offset = growUp? firstOffset + getAutomaticVarsSize()
333 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
335 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
336 getPaddedSize = sizeToUse + abs(aligned - offset);
342 int MachineFunctionInfo::allocateLocalVar(const Value* val,
343 unsigned sizeToUse) {
344 assert(! automaticVarsAreaFrozen &&
345 "Size of auto vars area has been used to compute an offset so "
346 "no more automatic vars should be allocated!");
348 // Check if we've allocated a stack slot for this value already
350 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
351 if (pair != offsets.end())
354 unsigned getPaddedSize;
355 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
356 offsets[val] = offset;
357 incrementAutomaticVarsSize(getPaddedSize);
362 MachineFunctionInfo::allocateSpilledValue(const Type* type)
364 assert(! spillsAreaFrozen &&
365 "Size of reg spills area has been used to compute an offset so "
366 "no more register spill slots should be allocated!");
368 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
369 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
372 int firstOffset = MF.getTarget().getFrameInfo()->getRegSpillAreaOffset(MF, growUp);
374 int offset = growUp? firstOffset + getRegSpillsSize()
375 : firstOffset - (getRegSpillsSize() + size);
377 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
378 size += abs(aligned - offset); // include alignment padding in size
380 incrementRegSpillsSize(size); // update size of reg. spills area
386 MachineFunctionInfo::pushTempValue(unsigned size)
388 unsigned align = SizeToAlignment(size, MF.getTarget());
391 int firstOffset = MF.getTarget().getFrameInfo()->getTmpAreaOffset(MF, growUp);
393 int offset = growUp? firstOffset + currentTmpValuesSize
394 : firstOffset - (currentTmpValuesSize + size);
396 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp,
398 size += abs(aligned - offset); // include alignment padding in size
400 incrementTmpAreaSize(size); // update "current" size of tmp area
405 void MachineFunctionInfo::popAllTempValues() {
406 resetTmpAreaSize(); // clear tmp area to reuse