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