LLVM API Documentation
00001 //===-- FunctionLoweringInfo.cpp ------------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements routines for translating functions from LLVM IR into 00011 // Machine IR. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "function-lowering-info" 00016 #include "llvm/CodeGen/FunctionLoweringInfo.h" 00017 #include "llvm/ADT/PostOrderIterator.h" 00018 #include "llvm/CodeGen/Analysis.h" 00019 #include "llvm/CodeGen/MachineFrameInfo.h" 00020 #include "llvm/CodeGen/MachineFunction.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/CodeGen/MachineModuleInfo.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/DebugInfo.h" 00025 #include "llvm/IR/DataLayout.h" 00026 #include "llvm/IR/DerivedTypes.h" 00027 #include "llvm/IR/Function.h" 00028 #include "llvm/IR/Instructions.h" 00029 #include "llvm/IR/IntrinsicInst.h" 00030 #include "llvm/IR/LLVMContext.h" 00031 #include "llvm/IR/Module.h" 00032 #include "llvm/Support/Debug.h" 00033 #include "llvm/Support/ErrorHandling.h" 00034 #include "llvm/Support/MathExtras.h" 00035 #include "llvm/Target/TargetInstrInfo.h" 00036 #include "llvm/Target/TargetLowering.h" 00037 #include "llvm/Target/TargetOptions.h" 00038 #include "llvm/Target/TargetRegisterInfo.h" 00039 #include <algorithm> 00040 using namespace llvm; 00041 00042 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by 00043 /// PHI nodes or outside of the basic block that defines it, or used by a 00044 /// switch or atomic instruction, which may expand to multiple basic blocks. 00045 static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { 00046 if (I->use_empty()) return false; 00047 if (isa<PHINode>(I)) return true; 00048 const BasicBlock *BB = I->getParent(); 00049 for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end(); 00050 UI != E; ++UI) { 00051 const User *U = *UI; 00052 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) 00053 return true; 00054 } 00055 return false; 00056 } 00057 00058 FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli) 00059 : TLI(tli) { 00060 } 00061 00062 void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) { 00063 Fn = &fn; 00064 MF = &mf; 00065 RegInfo = &MF->getRegInfo(); 00066 00067 // Check whether the function can return without sret-demotion. 00068 SmallVector<ISD::OutputArg, 4> Outs; 00069 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, TLI); 00070 CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), *MF, 00071 Fn->isVarArg(), 00072 Outs, Fn->getContext()); 00073 00074 // Initialize the mapping of values to registers. This is only set up for 00075 // instruction values that are used outside of the block that defines 00076 // them. 00077 Function::const_iterator BB = Fn->begin(), EB = Fn->end(); 00078 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00079 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) 00080 if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { 00081 Type *Ty = AI->getAllocatedType(); 00082 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty); 00083 unsigned Align = 00084 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), 00085 AI->getAlignment()); 00086 00087 TySize *= CUI->getZExtValue(); // Get total allocated size. 00088 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. 00089 00090 // The object may need to be placed onto the stack near the stack 00091 // protector if one exists. Determine here if this object is a suitable 00092 // candidate. I.e., it would trigger the creation of a stack protector. 00093 bool MayNeedSP = 00094 (AI->isArrayAllocation() || 00095 (TySize >= 8 && isa<ArrayType>(Ty) && 00096 cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8))); 00097 StaticAllocaMap[AI] = 00098 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, 00099 MayNeedSP, AI); 00100 } 00101 00102 for (; BB != EB; ++BB) 00103 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 00104 I != E; ++I) { 00105 // Mark values used outside their block as exported, by allocating 00106 // a virtual register for them. 00107 if (isUsedOutsideOfDefiningBlock(I)) 00108 if (!isa<AllocaInst>(I) || 00109 !StaticAllocaMap.count(cast<AllocaInst>(I))) 00110 InitializeRegForValue(I); 00111 00112 // Collect llvm.dbg.declare information. This is done now instead of 00113 // during the initial isel pass through the IR so that it is done 00114 // in a predictable order. 00115 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { 00116 MachineModuleInfo &MMI = MF->getMMI(); 00117 if (MMI.hasDebugInfo() && 00118 DIVariable(DI->getVariable()).Verify() && 00119 !DI->getDebugLoc().isUnknown()) { 00120 // Don't handle byval struct arguments or VLAs, for example. 00121 // Non-byval arguments are handled here (they refer to the stack 00122 // temporary alloca at this point). 00123 const Value *Address = DI->getAddress(); 00124 if (Address) { 00125 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) 00126 Address = BCI->getOperand(0); 00127 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { 00128 DenseMap<const AllocaInst *, int>::iterator SI = 00129 StaticAllocaMap.find(AI); 00130 if (SI != StaticAllocaMap.end()) { // Check for VLAs. 00131 int FI = SI->second; 00132 MMI.setVariableDbgInfo(DI->getVariable(), 00133 FI, DI->getDebugLoc()); 00134 } 00135 } 00136 } 00137 } 00138 } 00139 } 00140 00141 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This 00142 // also creates the initial PHI MachineInstrs, though none of the input 00143 // operands are populated. 00144 for (BB = Fn->begin(); BB != EB; ++BB) { 00145 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); 00146 MBBMap[BB] = MBB; 00147 MF->push_back(MBB); 00148 00149 // Transfer the address-taken flag. This is necessary because there could 00150 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only 00151 // the first one should be marked. 00152 if (BB->hasAddressTaken()) 00153 MBB->setHasAddressTaken(); 00154 00155 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as 00156 // appropriate. 00157 for (BasicBlock::const_iterator I = BB->begin(); 00158 const PHINode *PN = dyn_cast<PHINode>(I); ++I) { 00159 if (PN->use_empty()) continue; 00160 00161 // Skip empty types 00162 if (PN->getType()->isEmptyTy()) 00163 continue; 00164 00165 DebugLoc DL = PN->getDebugLoc(); 00166 unsigned PHIReg = ValueMap[PN]; 00167 assert(PHIReg && "PHI node does not have an assigned virtual register!"); 00168 00169 SmallVector<EVT, 4> ValueVTs; 00170 ComputeValueVTs(TLI, PN->getType(), ValueVTs); 00171 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { 00172 EVT VT = ValueVTs[vti]; 00173 unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT); 00174 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00175 for (unsigned i = 0; i != NumRegisters; ++i) 00176 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); 00177 PHIReg += NumRegisters; 00178 } 00179 } 00180 } 00181 00182 // Mark landing pad blocks. 00183 for (BB = Fn->begin(); BB != EB; ++BB) 00184 if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) 00185 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); 00186 } 00187 00188 /// clear - Clear out all the function-specific state. This returns this 00189 /// FunctionLoweringInfo to an empty state, ready to be used for a 00190 /// different function. 00191 void FunctionLoweringInfo::clear() { 00192 assert(CatchInfoFound.size() == CatchInfoLost.size() && 00193 "Not all catch info was assigned to a landing pad!"); 00194 00195 MBBMap.clear(); 00196 ValueMap.clear(); 00197 StaticAllocaMap.clear(); 00198 #ifndef NDEBUG 00199 CatchInfoLost.clear(); 00200 CatchInfoFound.clear(); 00201 #endif 00202 LiveOutRegInfo.clear(); 00203 VisitedBBs.clear(); 00204 ArgDbgValues.clear(); 00205 ByValArgFrameIndexMap.clear(); 00206 RegFixups.clear(); 00207 } 00208 00209 /// CreateReg - Allocate a single virtual register for the given type. 00210 unsigned FunctionLoweringInfo::CreateReg(MVT VT) { 00211 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); 00212 } 00213 00214 /// CreateRegs - Allocate the appropriate number of virtual registers of 00215 /// the correctly promoted or expanded types. Assign these registers 00216 /// consecutive vreg numbers and return the first assigned number. 00217 /// 00218 /// In the case that the given value has struct or array type, this function 00219 /// will assign registers for each member or element. 00220 /// 00221 unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { 00222 SmallVector<EVT, 4> ValueVTs; 00223 ComputeValueVTs(TLI, Ty, ValueVTs); 00224 00225 unsigned FirstReg = 0; 00226 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { 00227 EVT ValueVT = ValueVTs[Value]; 00228 MVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT); 00229 00230 unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT); 00231 for (unsigned i = 0; i != NumRegs; ++i) { 00232 unsigned R = CreateReg(RegisterVT); 00233 if (!FirstReg) FirstReg = R; 00234 } 00235 } 00236 return FirstReg; 00237 } 00238 00239 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the 00240 /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If 00241 /// the register's LiveOutInfo is for a smaller bit width, it is extended to 00242 /// the larger bit width by zero extension. The bit width must be no smaller 00243 /// than the LiveOutInfo's existing bit width. 00244 const FunctionLoweringInfo::LiveOutInfo * 00245 FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { 00246 if (!LiveOutRegInfo.inBounds(Reg)) 00247 return NULL; 00248 00249 LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; 00250 if (!LOI->IsValid) 00251 return NULL; 00252 00253 if (BitWidth > LOI->KnownZero.getBitWidth()) { 00254 LOI->NumSignBits = 1; 00255 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth); 00256 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth); 00257 } 00258 00259 return LOI; 00260 } 00261 00262 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination 00263 /// register based on the LiveOutInfo of its operands. 00264 void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { 00265 Type *Ty = PN->getType(); 00266 if (!Ty->isIntegerTy() || Ty->isVectorTy()) 00267 return; 00268 00269 SmallVector<EVT, 1> ValueVTs; 00270 ComputeValueVTs(TLI, Ty, ValueVTs); 00271 assert(ValueVTs.size() == 1 && 00272 "PHIs with non-vector integer types should have a single VT."); 00273 EVT IntVT = ValueVTs[0]; 00274 00275 if (TLI.getNumRegisters(PN->getContext(), IntVT) != 1) 00276 return; 00277 IntVT = TLI.getTypeToTransformTo(PN->getContext(), IntVT); 00278 unsigned BitWidth = IntVT.getSizeInBits(); 00279 00280 unsigned DestReg = ValueMap[PN]; 00281 if (!TargetRegisterInfo::isVirtualRegister(DestReg)) 00282 return; 00283 LiveOutRegInfo.grow(DestReg); 00284 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; 00285 00286 Value *V = PN->getIncomingValue(0); 00287 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { 00288 DestLOI.NumSignBits = 1; 00289 APInt Zero(BitWidth, 0); 00290 DestLOI.KnownZero = Zero; 00291 DestLOI.KnownOne = Zero; 00292 return; 00293 } 00294 00295 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 00296 APInt Val = CI->getValue().zextOrTrunc(BitWidth); 00297 DestLOI.NumSignBits = Val.getNumSignBits(); 00298 DestLOI.KnownZero = ~Val; 00299 DestLOI.KnownOne = Val; 00300 } else { 00301 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" 00302 "CopyToReg node was created."); 00303 unsigned SrcReg = ValueMap[V]; 00304 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { 00305 DestLOI.IsValid = false; 00306 return; 00307 } 00308 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); 00309 if (!SrcLOI) { 00310 DestLOI.IsValid = false; 00311 return; 00312 } 00313 DestLOI = *SrcLOI; 00314 } 00315 00316 assert(DestLOI.KnownZero.getBitWidth() == BitWidth && 00317 DestLOI.KnownOne.getBitWidth() == BitWidth && 00318 "Masks should have the same bit width as the type."); 00319 00320 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { 00321 Value *V = PN->getIncomingValue(i); 00322 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { 00323 DestLOI.NumSignBits = 1; 00324 APInt Zero(BitWidth, 0); 00325 DestLOI.KnownZero = Zero; 00326 DestLOI.KnownOne = Zero; 00327 return; 00328 } 00329 00330 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 00331 APInt Val = CI->getValue().zextOrTrunc(BitWidth); 00332 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); 00333 DestLOI.KnownZero &= ~Val; 00334 DestLOI.KnownOne &= Val; 00335 continue; 00336 } 00337 00338 assert(ValueMap.count(V) && "V should have been placed in ValueMap when " 00339 "its CopyToReg node was created."); 00340 unsigned SrcReg = ValueMap[V]; 00341 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { 00342 DestLOI.IsValid = false; 00343 return; 00344 } 00345 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); 00346 if (!SrcLOI) { 00347 DestLOI.IsValid = false; 00348 return; 00349 } 00350 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); 00351 DestLOI.KnownZero &= SrcLOI->KnownZero; 00352 DestLOI.KnownOne &= SrcLOI->KnownOne; 00353 } 00354 } 00355 00356 /// setArgumentFrameIndex - Record frame index for the byval 00357 /// argument. This overrides previous frame index entry for this argument, 00358 /// if any. 00359 void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, 00360 int FI) { 00361 ByValArgFrameIndexMap[A] = FI; 00362 } 00363 00364 /// getArgumentFrameIndex - Get frame index for the byval argument. 00365 /// If the argument does not have any assigned frame index then 0 is 00366 /// returned. 00367 int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { 00368 DenseMap<const Argument *, int>::iterator I = 00369 ByValArgFrameIndexMap.find(A); 00370 if (I != ByValArgFrameIndexMap.end()) 00371 return I->second; 00372 DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); 00373 return 0; 00374 } 00375 00376 /// ComputeUsesVAFloatArgument - Determine if any floating-point values are 00377 /// being passed to this variadic function, and set the MachineModuleInfo's 00378 /// usesVAFloatArgument flag if so. This flag is used to emit an undefined 00379 /// reference to _fltused on Windows, which will link in MSVCRT's 00380 /// floating-point support. 00381 void llvm::ComputeUsesVAFloatArgument(const CallInst &I, 00382 MachineModuleInfo *MMI) 00383 { 00384 FunctionType *FT = cast<FunctionType>( 00385 I.getCalledValue()->getType()->getContainedType(0)); 00386 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { 00387 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { 00388 Type* T = I.getArgOperand(i)->getType(); 00389 for (po_iterator<Type*> i = po_begin(T), e = po_end(T); 00390 i != e; ++i) { 00391 if (i->isFloatingPointTy()) { 00392 MMI->setUsesVAFloatArgument(true); 00393 return; 00394 } 00395 } 00396 } 00397 } 00398 } 00399 00400 /// AddCatchInfo - Extract the personality and type infos from an eh.selector 00401 /// call, and add them to the specified machine basic block. 00402 void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI, 00403 MachineBasicBlock *MBB) { 00404 // Inform the MachineModuleInfo of the personality for this landing pad. 00405 const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1)); 00406 assert(CE->getOpcode() == Instruction::BitCast && 00407 isa<Function>(CE->getOperand(0)) && 00408 "Personality should be a function"); 00409 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); 00410 00411 // Gather all the type infos for this landing pad and pass them along to 00412 // MachineModuleInfo. 00413 std::vector<const GlobalVariable *> TyInfo; 00414 unsigned N = I.getNumArgOperands(); 00415 00416 for (unsigned i = N - 1; i > 1; --i) { 00417 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) { 00418 unsigned FilterLength = CI->getZExtValue(); 00419 unsigned FirstCatch = i + FilterLength + !FilterLength; 00420 assert(FirstCatch <= N && "Invalid filter length"); 00421 00422 if (FirstCatch < N) { 00423 TyInfo.reserve(N - FirstCatch); 00424 for (unsigned j = FirstCatch; j < N; ++j) 00425 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); 00426 MMI->addCatchTypeInfo(MBB, TyInfo); 00427 TyInfo.clear(); 00428 } 00429 00430 if (!FilterLength) { 00431 // Cleanup. 00432 MMI->addCleanup(MBB); 00433 } else { 00434 // Filter. 00435 TyInfo.reserve(FilterLength - 1); 00436 for (unsigned j = i + 1; j < FirstCatch; ++j) 00437 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); 00438 MMI->addFilterTypeInfo(MBB, TyInfo); 00439 TyInfo.clear(); 00440 } 00441 00442 N = i; 00443 } 00444 } 00445 00446 if (N > 2) { 00447 TyInfo.reserve(N - 2); 00448 for (unsigned j = 2; j < N; ++j) 00449 TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); 00450 MMI->addCatchTypeInfo(MBB, TyInfo); 00451 } 00452 } 00453 00454 /// AddLandingPadInfo - Extract the exception handling information from the 00455 /// landingpad instruction and add them to the specified machine module info. 00456 void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI, 00457 MachineBasicBlock *MBB) { 00458 MMI.addPersonality(MBB, 00459 cast<Function>(I.getPersonalityFn()->stripPointerCasts())); 00460 00461 if (I.isCleanup()) 00462 MMI.addCleanup(MBB); 00463 00464 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct, 00465 // but we need to do it this way because of how the DWARF EH emitter 00466 // processes the clauses. 00467 for (unsigned i = I.getNumClauses(); i != 0; --i) { 00468 Value *Val = I.getClause(i - 1); 00469 if (I.isCatch(i - 1)) { 00470 MMI.addCatchTypeInfo(MBB, 00471 dyn_cast<GlobalVariable>(Val->stripPointerCasts())); 00472 } else { 00473 // Add filters in a list. 00474 Constant *CVal = cast<Constant>(Val); 00475 SmallVector<const GlobalVariable*, 4> FilterList; 00476 for (User::op_iterator 00477 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) 00478 FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts())); 00479 00480 MMI.addFilterTypeInfo(MBB, FilterList); 00481 } 00482 } 00483 }