1 //===-- MachineFunction.cpp -----------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 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. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Collect native machine code information for a function. This allows 11 // target-specific information about the generated code to be stored with each 12 // function. 13 // 14 //===----------------------------------------------------------------------===// 15 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 using namespace llvm; 30 31 static AnnotationID MF_AID( 32 AnnotationManager::getID("CodeGen::MachineCodeForFunction")); 33 34 35 namespace { 36 struct Printer : public FunctionPass { 37 const char *getPassName() const { return "MachineFunction Printer"; } 38 39 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 40 AU.setPreservesAll(); 41 } 42 43 bool runOnFunction(Function &F) { 44 MachineFunction::get(&F).dump(); 45 return false; 46 } 47 }; 48 } 49 50 FunctionPass *llvm::createMachineFunctionPrinterPass() { 51 return new Printer(); 52 } 53 54 55 //===---------------------------------------------------------------------===// 56 // MachineFunction implementation 57 //===---------------------------------------------------------------------===// 58 59 MachineFunction::MachineFunction(const Function *F, 60 const TargetMachine &TM) 61 : Annotation(MF_AID), Fn(F), Target(TM) { 62 SSARegMapping = new SSARegMap(); 63 MFInfo = new MachineFunctionInfo(*this); 64 FrameInfo = new MachineFrameInfo(); 65 ConstantPool = new MachineConstantPool(); 66 } 67 68 MachineFunction::~MachineFunction() { 69 delete SSARegMapping; 70 delete MFInfo; 71 delete FrameInfo; 72 delete ConstantPool; 73 } 74 75 void MachineFunction::dump() const { print(std::cerr); } 76 77 void MachineFunction::print(std::ostream &OS) const { 78 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName() 79 << "\"\n"; 80 81 // Print Frame Information 82 getFrameInfo()->print(*this, OS); 83 84 // Print Constant Pool 85 getConstantPool()->print(OS); 86 87 for (const_iterator BB = begin(); BB != end(); ++BB) { 88 const BasicBlock *LBB = BB->getBasicBlock(); 89 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n"; 90 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){ 91 OS << "\t"; 92 (*I)->print(OS, Target); 93 } 94 } 95 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n"; 96 } 97 98 99 // The next two methods are used to construct and to retrieve 100 // the MachineCodeForFunction object for the given function. 101 // construct() -- Allocates and initializes for a given function and target 102 // get() -- Returns a handle to the object. 103 // This should not be called before "construct()" 104 // for a given Function. 105 // 106 MachineFunction& 107 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar) 108 { 109 assert(Fn->getAnnotation(MF_AID) == 0 && 110 "Object already exists for this function!"); 111 MachineFunction* mcInfo = new MachineFunction(Fn, Tar); 112 Fn->addAnnotation(mcInfo); 113 return *mcInfo; 114 } 115 116 void 117 MachineFunction::destruct(const Function *Fn) 118 { 119 bool Deleted = Fn->deleteAnnotation(MF_AID); 120 assert(Deleted && "Machine code did not exist for function!"); 121 } 122 123 MachineFunction& MachineFunction::get(const Function *F) 124 { 125 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID); 126 assert(mc && "Call construct() method first to allocate the object"); 127 return *mc; 128 } 129 130 void MachineFunction::clearSSARegMap() { 131 delete SSARegMapping; 132 SSARegMapping = 0; 133 } 134 135 //===----------------------------------------------------------------------===// 136 // MachineFrameInfo implementation 137 //===----------------------------------------------------------------------===// 138 139 /// CreateStackObject - Create a stack object for a value of the specified type. 140 /// 141 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) { 142 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty)); 143 } 144 145 int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) { 146 return CreateStackObject(RC->getSize(), RC->getAlignment()); 147 } 148 149 150 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{ 151 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea(); 152 153 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 154 const StackObject &SO = Objects[i]; 155 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is "; 156 if (SO.Size == 0) 157 OS << "variable sized"; 158 else 159 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " "); 160 161 if (i < NumFixedObjects) 162 OS << " fixed"; 163 if (i < NumFixedObjects || SO.SPOffset != -1) { 164 int Off = SO.SPOffset + ValOffset; 165 OS << " at location [SP"; 166 if (Off > 0) 167 OS << "+" << Off; 168 else if (Off < 0) 169 OS << Off; 170 OS << "]"; 171 } 172 OS << "\n"; 173 } 174 175 if (HasVarSizedObjects) 176 OS << " Stack frame contains variable sized objects\n"; 177 } 178 179 void MachineFrameInfo::dump(const MachineFunction &MF) const { 180 print(MF, std::cerr); 181 } 182 183 184 //===----------------------------------------------------------------------===// 185 // MachineConstantPool implementation 186 //===----------------------------------------------------------------------===// 187 188 void MachineConstantPool::print(std::ostream &OS) const { 189 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 190 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n"; 191 } 192 193 void MachineConstantPool::dump() const { print(std::cerr); } 194 195 //===----------------------------------------------------------------------===// 196 // MachineFunctionInfo implementation 197 //===----------------------------------------------------------------------===// 198 199 static unsigned 200 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F, 201 unsigned &maxOptionalNumArgs) 202 { 203 const TargetFrameInfo &frameInfo = target.getFrameInfo(); 204 205 unsigned maxSize = 0; 206 207 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB) 208 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) 209 if (const CallInst *callInst = dyn_cast<CallInst>(I)) 210 { 211 unsigned numOperands = callInst->getNumOperands() - 1; 212 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs(); 213 if (numExtra <= 0) 214 continue; 215 216 unsigned sizeForThisCall; 217 if (frameInfo.argsOnStackHaveFixedSize()) 218 { 219 int argSize = frameInfo.getSizeOfEachArgOnStack(); 220 sizeForThisCall = numExtra * (unsigned) argSize; 221 } 222 else 223 { 224 assert(0 && "UNTESTED CODE: Size per stack argument is not " 225 "fixed on this architecture: use actual arg sizes to " 226 "compute MaxOptionalArgsSize"); 227 sizeForThisCall = 0; 228 for (unsigned i = 0; i < numOperands; ++i) 229 sizeForThisCall += target.getTargetData().getTypeSize(callInst-> 230 getOperand(i)->getType()); 231 } 232 233 if (maxSize < sizeForThisCall) 234 maxSize = sizeForThisCall; 235 236 if ((int)maxOptionalNumArgs < numExtra) 237 maxOptionalNumArgs = (unsigned) numExtra; 238 } 239 240 return maxSize; 241 } 242 243 // Align data larger than one L1 cache line on L1 cache line boundaries. 244 // Align all smaller data on the next higher 2^x boundary (4, 8, ...), 245 // but not higher than the alignment of the largest type we support 246 // (currently a double word). -- see class TargetData). 247 // 248 // This function is similar to the corresponding function in EmitAssembly.cpp 249 // but they are unrelated. This one does not align at more than a 250 // double-word boundary whereas that one might. 251 // 252 inline unsigned 253 SizeToAlignment(unsigned size, const TargetMachine& target) 254 { 255 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); 256 if (size > (unsigned) cacheLineSize / 2) 257 return cacheLineSize; 258 else 259 for (unsigned sz=1; /*no condition*/; sz *= 2) 260 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment()) 261 return sz; 262 } 263 264 265 void MachineFunctionInfo::CalculateArgSize() { 266 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(), 267 MF.getFunction(), 268 maxOptionalNumArgs); 269 staticStackSize = maxOptionalArgsSize 270 + MF.getTarget().getFrameInfo().getMinStackFrameSize(); 271 } 272 273 int 274 MachineFunctionInfo::computeOffsetforLocalVar(const Value* val, 275 unsigned &getPaddedSize, 276 unsigned sizeToUse) 277 { 278 if (sizeToUse == 0) 279 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType()); 280 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget()); 281 282 bool growUp; 283 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF, 284 growUp); 285 int offset = growUp? firstOffset + getAutomaticVarsSize() 286 : firstOffset - (getAutomaticVarsSize() + sizeToUse); 287 288 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align); 289 getPaddedSize = sizeToUse + abs(aligned - offset); 290 291 return aligned; 292 } 293 294 295 int MachineFunctionInfo::allocateLocalVar(const Value* val, 296 unsigned sizeToUse) { 297 assert(! automaticVarsAreaFrozen && 298 "Size of auto vars area has been used to compute an offset so " 299 "no more automatic vars should be allocated!"); 300 301 // Check if we've allocated a stack slot for this value already 302 // 303 hash_map<const Value*, int>::const_iterator pair = offsets.find(val); 304 if (pair != offsets.end()) 305 return pair->second; 306 307 unsigned getPaddedSize; 308 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse); 309 offsets[val] = offset; 310 incrementAutomaticVarsSize(getPaddedSize); 311 return offset; 312 } 313 314 int 315 MachineFunctionInfo::allocateSpilledValue(const Type* type) 316 { 317 assert(! spillsAreaFrozen && 318 "Size of reg spills area has been used to compute an offset so " 319 "no more register spill slots should be allocated!"); 320 321 unsigned size = MF.getTarget().getTargetData().getTypeSize(type); 322 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type); 323 324 bool growUp; 325 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp); 326 327 int offset = growUp? firstOffset + getRegSpillsSize() 328 : firstOffset - (getRegSpillsSize() + size); 329 330 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align); 331 size += abs(aligned - offset); // include alignment padding in size 332 333 incrementRegSpillsSize(size); // update size of reg. spills area 334 335 return aligned; 336 } 337 338 int 339 MachineFunctionInfo::pushTempValue(unsigned size) 340 { 341 unsigned align = SizeToAlignment(size, MF.getTarget()); 342 343 bool growUp; 344 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp); 345 346 int offset = growUp? firstOffset + currentTmpValuesSize 347 : firstOffset - (currentTmpValuesSize + size); 348 349 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, 350 align); 351 size += abs(aligned - offset); // include alignment padding in size 352 353 incrementTmpAreaSize(size); // update "current" size of tmp area 354 355 return aligned; 356 } 357 358 void MachineFunctionInfo::popAllTempValues() { 359 resetTmpAreaSize(); // clear tmp area to reuse 360 } 361 362