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