1 //===-- MachineFunctionInfo.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 // This implements the SparcV9 specific MachineFunctionInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "MachineFunctionInfo.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Function.h"
17 #include "llvm/Type.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetFrameInfo.h"
23 MachineFunctionInfo *MachineFunction::getInfo() const {
25 MFInfo = new MachineFunctionInfo(*const_cast<MachineFunction*>(this));
27 return static_cast<MachineFunctionInfo*>(MFInfo);
33 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
34 unsigned &maxOptionalNumArgs)
38 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
39 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
40 if (const CallInst *callInst = dyn_cast<CallInst>(I))
42 unsigned numOperands = callInst->getNumOperands() - 1;
43 int numExtra = numOperands-6;
47 unsigned sizeForThisCall = numExtra * 8;
49 if (maxSize < sizeForThisCall)
50 maxSize = sizeForThisCall;
52 if ((int)maxOptionalNumArgs < numExtra)
53 maxOptionalNumArgs = (unsigned) numExtra;
59 // Align data larger than one L1 cache line on L1 cache line boundaries.
60 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
61 // but not higher than the alignment of the largest type we support
62 // (currently a double word). -- see class TargetData).
64 // This function is similar to the corresponding function in EmitAssembly.cpp
65 // but they are unrelated. This one does not align at more than a
66 // double-word boundary whereas that one might.
69 SizeToAlignment(unsigned size, const TargetMachine& target)
71 const unsigned short cacheLineSize = 16;
72 if (size > (unsigned) cacheLineSize / 2)
75 for (unsigned sz=1; /*no condition*/; sz *= 2)
76 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
81 void MachineFunctionInfo::CalculateArgSize() {
82 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
85 staticStackSize = maxOptionalArgsSize + 176;
89 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
90 unsigned &getPaddedSize,
94 // All integer types smaller than ints promote to 4 byte integers.
95 if (val->getType()->isIntegral() && val->getType()->getPrimitiveSize() < 4)
98 sizeToUse = MF.getTarget().getTargetData().getTypeSize(val->getType());
100 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
103 int firstOffset = MF.getTarget().getFrameInfo()->getFirstAutomaticVarOffset(MF,
105 int offset = growUp? firstOffset + getAutomaticVarsSize()
106 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
108 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
109 getPaddedSize = sizeToUse + abs(aligned - offset);
115 int MachineFunctionInfo::allocateLocalVar(const Value* val,
116 unsigned sizeToUse) {
117 assert(! automaticVarsAreaFrozen &&
118 "Size of auto vars area has been used to compute an offset so "
119 "no more automatic vars should be allocated!");
121 // Check if we've allocated a stack slot for this value already
123 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
124 if (pair != offsets.end())
127 unsigned getPaddedSize;
128 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
129 offsets[val] = offset;
130 incrementAutomaticVarsSize(getPaddedSize);
135 MachineFunctionInfo::allocateSpilledValue(const Type* type)
137 assert(! spillsAreaFrozen &&
138 "Size of reg spills area has been used to compute an offset so "
139 "no more register spill slots should be allocated!");
141 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
142 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
145 int firstOffset = MF.getTarget().getFrameInfo()->getRegSpillAreaOffset(MF, growUp);
147 int offset = growUp? firstOffset + getRegSpillsSize()
148 : firstOffset - (getRegSpillsSize() + size);
150 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp, align);
151 size += abs(aligned - offset); // include alignment padding in size
153 incrementRegSpillsSize(size); // update size of reg. spills area
159 MachineFunctionInfo::pushTempValue(unsigned size)
161 unsigned align = SizeToAlignment(size, MF.getTarget());
164 int firstOffset = MF.getTarget().getFrameInfo()->getTmpAreaOffset(MF, growUp);
166 int offset = growUp? firstOffset + currentTmpValuesSize
167 : firstOffset - (currentTmpValuesSize + size);
169 int aligned = MF.getTarget().getFrameInfo()->adjustAlignment(offset, growUp,
171 size += abs(aligned - offset); // include alignment padding in size
173 incrementTmpAreaSize(size); // update "current" size of tmp area
178 void MachineFunctionInfo::popAllTempValues() {
179 resetTmpAreaSize(); // clear tmp area to reuse