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