CGDecl.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This contains code to emit Decl nodes as LLVM code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CGDebugInfo.h"
  14. #include "CodeGenFunction.h"
  15. #include "CodeGenModule.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/AST/Decl.h"
  19. #include "clang/AST/DeclObjC.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "clang/Basic/TargetInfo.h"
  22. #include "clang/Frontend/CodeGenOptions.h"
  23. #include "llvm/GlobalVariable.h"
  24. #include "llvm/Intrinsics.h"
  25. #include "llvm/Target/TargetData.h"
  26. #include "llvm/Type.h"
  27. using namespace clang;
  28. using namespace CodeGen;
  29. void CodeGenFunction::EmitDecl(const Decl &D) {
  30. switch (D.getKind()) {
  31. case Decl::TranslationUnit:
  32. case Decl::Namespace:
  33. case Decl::UnresolvedUsingTypename:
  34. case Decl::ClassTemplateSpecialization:
  35. case Decl::ClassTemplatePartialSpecialization:
  36. case Decl::TemplateTypeParm:
  37. case Decl::UnresolvedUsingValue:
  38. case Decl::NonTypeTemplateParm:
  39. case Decl::CXXMethod:
  40. case Decl::CXXConstructor:
  41. case Decl::CXXDestructor:
  42. case Decl::CXXConversion:
  43. case Decl::Field:
  44. case Decl::IndirectField:
  45. case Decl::ObjCIvar:
  46. case Decl::ObjCAtDefsField:
  47. case Decl::ParmVar:
  48. case Decl::ImplicitParam:
  49. case Decl::ClassTemplate:
  50. case Decl::FunctionTemplate:
  51. case Decl::TypeAliasTemplate:
  52. case Decl::TemplateTemplateParm:
  53. case Decl::ObjCMethod:
  54. case Decl::ObjCCategory:
  55. case Decl::ObjCProtocol:
  56. case Decl::ObjCInterface:
  57. case Decl::ObjCCategoryImpl:
  58. case Decl::ObjCImplementation:
  59. case Decl::ObjCProperty:
  60. case Decl::ObjCCompatibleAlias:
  61. case Decl::AccessSpec:
  62. case Decl::LinkageSpec:
  63. case Decl::ObjCPropertyImpl:
  64. case Decl::ObjCClass:
  65. case Decl::ObjCForwardProtocol:
  66. case Decl::FileScopeAsm:
  67. case Decl::Friend:
  68. case Decl::FriendTemplate:
  69. case Decl::Block:
  70. case Decl::ClassScopeFunctionSpecialization:
  71. assert(0 && "Declaration should not be in declstmts!");
  72. case Decl::Function: // void X();
  73. case Decl::Record: // struct/union/class X;
  74. case Decl::Enum: // enum X;
  75. case Decl::EnumConstant: // enum ? { X = ? }
  76. case Decl::CXXRecord: // struct/union/class X; [C++]
  77. case Decl::Using: // using X; [C++]
  78. case Decl::UsingShadow:
  79. case Decl::UsingDirective: // using namespace X; [C++]
  80. case Decl::NamespaceAlias:
  81. case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
  82. case Decl::Label: // __label__ x;
  83. // None of these decls require codegen support.
  84. return;
  85. case Decl::Var: {
  86. const VarDecl &VD = cast<VarDecl>(D);
  87. assert(VD.isLocalVarDecl() &&
  88. "Should not see file-scope variables inside a function!");
  89. return EmitVarDecl(VD);
  90. }
  91. case Decl::Typedef: // typedef int X;
  92. case Decl::TypeAlias: { // using X = int; [C++0x]
  93. const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
  94. QualType Ty = TD.getUnderlyingType();
  95. if (Ty->isVariablyModifiedType())
  96. EmitVariablyModifiedType(Ty);
  97. }
  98. }
  99. }
  100. /// EmitVarDecl - This method handles emission of any variable declaration
  101. /// inside a function, including static vars etc.
  102. void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
  103. switch (D.getStorageClass()) {
  104. case SC_None:
  105. case SC_Auto:
  106. case SC_Register:
  107. return EmitAutoVarDecl(D);
  108. case SC_Static: {
  109. llvm::GlobalValue::LinkageTypes Linkage =
  110. llvm::GlobalValue::InternalLinkage;
  111. // If the function definition has some sort of weak linkage, its
  112. // static variables should also be weak so that they get properly
  113. // uniqued. We can't do this in C, though, because there's no
  114. // standard way to agree on which variables are the same (i.e.
  115. // there's no mangling).
  116. if (getContext().getLangOptions().CPlusPlus)
  117. if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
  118. Linkage = CurFn->getLinkage();
  119. return EmitStaticVarDecl(D, Linkage);
  120. }
  121. case SC_Extern:
  122. case SC_PrivateExtern:
  123. // Don't emit it now, allow it to be emitted lazily on its first use.
  124. return;
  125. }
  126. assert(0 && "Unknown storage class");
  127. }
  128. static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
  129. const char *Separator) {
  130. CodeGenModule &CGM = CGF.CGM;
  131. if (CGF.getContext().getLangOptions().CPlusPlus) {
  132. StringRef Name = CGM.getMangledName(&D);
  133. return Name.str();
  134. }
  135. std::string ContextName;
  136. if (!CGF.CurFuncDecl) {
  137. // Better be in a block declared in global scope.
  138. const NamedDecl *ND = cast<NamedDecl>(&D);
  139. const DeclContext *DC = ND->getDeclContext();
  140. if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
  141. MangleBuffer Name;
  142. CGM.getBlockMangledName(GlobalDecl(), Name, BD);
  143. ContextName = Name.getString();
  144. }
  145. else
  146. assert(0 && "Unknown context for block static var decl");
  147. } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
  148. StringRef Name = CGM.getMangledName(FD);
  149. ContextName = Name.str();
  150. } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
  151. ContextName = CGF.CurFn->getName();
  152. else
  153. assert(0 && "Unknown context for static var decl");
  154. return ContextName + Separator + D.getNameAsString();
  155. }
  156. llvm::GlobalVariable *
  157. CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
  158. const char *Separator,
  159. llvm::GlobalValue::LinkageTypes Linkage) {
  160. QualType Ty = D.getType();
  161. assert(Ty->isConstantSizeType() && "VLAs can't be static");
  162. std::string Name = GetStaticDeclName(*this, D, Separator);
  163. llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
  164. llvm::GlobalVariable *GV =
  165. new llvm::GlobalVariable(CGM.getModule(), LTy,
  166. Ty.isConstant(getContext()), Linkage,
  167. CGM.EmitNullConstant(D.getType()), Name, 0,
  168. D.isThreadSpecified(),
  169. CGM.getContext().getTargetAddressSpace(Ty));
  170. GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
  171. if (Linkage != llvm::GlobalValue::InternalLinkage)
  172. GV->setVisibility(CurFn->getVisibility());
  173. return GV;
  174. }
  175. /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
  176. /// global variable that has already been created for it. If the initializer
  177. /// has a different type than GV does, this may free GV and return a different
  178. /// one. Otherwise it just returns GV.
  179. llvm::GlobalVariable *
  180. CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
  181. llvm::GlobalVariable *GV) {
  182. llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
  183. // If constant emission failed, then this should be a C++ static
  184. // initializer.
  185. if (!Init) {
  186. if (!getContext().getLangOptions().CPlusPlus)
  187. CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
  188. else if (Builder.GetInsertBlock()) {
  189. // Since we have a static initializer, this global variable can't
  190. // be constant.
  191. GV->setConstant(false);
  192. EmitCXXGuardedInit(D, GV);
  193. }
  194. return GV;
  195. }
  196. // The initializer may differ in type from the global. Rewrite
  197. // the global to match the initializer. (We have to do this
  198. // because some types, like unions, can't be completely represented
  199. // in the LLVM type system.)
  200. if (GV->getType()->getElementType() != Init->getType()) {
  201. llvm::GlobalVariable *OldGV = GV;
  202. GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
  203. OldGV->isConstant(),
  204. OldGV->getLinkage(), Init, "",
  205. /*InsertBefore*/ OldGV,
  206. D.isThreadSpecified(),
  207. CGM.getContext().getTargetAddressSpace(D.getType()));
  208. GV->setVisibility(OldGV->getVisibility());
  209. // Steal the name of the old global
  210. GV->takeName(OldGV);
  211. // Replace all uses of the old global with the new global
  212. llvm::Constant *NewPtrForOldDecl =
  213. llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
  214. OldGV->replaceAllUsesWith(NewPtrForOldDecl);
  215. // Erase the old global, since it is no longer used.
  216. OldGV->eraseFromParent();
  217. }
  218. GV->setInitializer(Init);
  219. return GV;
  220. }
  221. void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
  222. llvm::GlobalValue::LinkageTypes Linkage) {
  223. llvm::Value *&DMEntry = LocalDeclMap[&D];
  224. assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
  225. llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
  226. // Store into LocalDeclMap before generating initializer to handle
  227. // circular references.
  228. DMEntry = GV;
  229. // We can't have a VLA here, but we can have a pointer to a VLA,
  230. // even though that doesn't really make any sense.
  231. // Make sure to evaluate VLA bounds now so that we have them for later.
  232. if (D.getType()->isVariablyModifiedType())
  233. EmitVariablyModifiedType(D.getType());
  234. // Local static block variables must be treated as globals as they may be
  235. // referenced in their RHS initializer block-literal expresion.
  236. CGM.setStaticLocalDeclAddress(&D, GV);
  237. // If this value has an initializer, emit it.
  238. if (D.getInit())
  239. GV = AddInitializerToStaticVarDecl(D, GV);
  240. GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
  241. // FIXME: Merge attribute handling.
  242. if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
  243. SourceManager &SM = CGM.getContext().getSourceManager();
  244. llvm::Constant *Ann =
  245. CGM.EmitAnnotateAttr(GV, AA, SM.getExpansionLineNumber(D.getLocation()));
  246. CGM.AddAnnotation(Ann);
  247. }
  248. if (const SectionAttr *SA = D.getAttr<SectionAttr>())
  249. GV->setSection(SA->getName());
  250. if (D.hasAttr<UsedAttr>())
  251. CGM.AddUsedGlobal(GV);
  252. // We may have to cast the constant because of the initializer
  253. // mismatch above.
  254. //
  255. // FIXME: It is really dangerous to store this in the map; if anyone
  256. // RAUW's the GV uses of this constant will be invalid.
  257. llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
  258. llvm::Type *LPtrTy =
  259. LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType()));
  260. DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
  261. // Emit global variable debug descriptor for static vars.
  262. CGDebugInfo *DI = getDebugInfo();
  263. if (DI) {
  264. DI->setLocation(D.getLocation());
  265. DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
  266. }
  267. }
  268. namespace {
  269. struct DestroyObject : EHScopeStack::Cleanup {
  270. DestroyObject(llvm::Value *addr, QualType type,
  271. CodeGenFunction::Destroyer *destroyer,
  272. bool useEHCleanupForArray)
  273. : addr(addr), type(type), destroyer(*destroyer),
  274. useEHCleanupForArray(useEHCleanupForArray) {}
  275. llvm::Value *addr;
  276. QualType type;
  277. CodeGenFunction::Destroyer &destroyer;
  278. bool useEHCleanupForArray;
  279. void Emit(CodeGenFunction &CGF, Flags flags) {
  280. // Don't use an EH cleanup recursively from an EH cleanup.
  281. bool useEHCleanupForArray =
  282. flags.isForNormalCleanup() && this->useEHCleanupForArray;
  283. CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
  284. }
  285. };
  286. struct DestroyNRVOVariable : EHScopeStack::Cleanup {
  287. DestroyNRVOVariable(llvm::Value *addr,
  288. const CXXDestructorDecl *Dtor,
  289. llvm::Value *NRVOFlag)
  290. : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
  291. const CXXDestructorDecl *Dtor;
  292. llvm::Value *NRVOFlag;
  293. llvm::Value *Loc;
  294. void Emit(CodeGenFunction &CGF, Flags flags) {
  295. // Along the exceptions path we always execute the dtor.
  296. bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
  297. llvm::BasicBlock *SkipDtorBB = 0;
  298. if (NRVO) {
  299. // If we exited via NRVO, we skip the destructor call.
  300. llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
  301. SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
  302. llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
  303. CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
  304. CGF.EmitBlock(RunDtorBB);
  305. }
  306. CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
  307. /*ForVirtualBase=*/false, Loc);
  308. if (NRVO) CGF.EmitBlock(SkipDtorBB);
  309. }
  310. };
  311. struct CallStackRestore : EHScopeStack::Cleanup {
  312. llvm::Value *Stack;
  313. CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
  314. void Emit(CodeGenFunction &CGF, Flags flags) {
  315. llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp");
  316. llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
  317. CGF.Builder.CreateCall(F, V);
  318. }
  319. };
  320. struct ExtendGCLifetime : EHScopeStack::Cleanup {
  321. const VarDecl &Var;
  322. ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
  323. void Emit(CodeGenFunction &CGF, Flags flags) {
  324. // Compute the address of the local variable, in case it's a
  325. // byref or something.
  326. DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
  327. SourceLocation());
  328. llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE));
  329. CGF.EmitExtendGCLifetime(value);
  330. }
  331. };
  332. struct CallCleanupFunction : EHScopeStack::Cleanup {
  333. llvm::Constant *CleanupFn;
  334. const CGFunctionInfo &FnInfo;
  335. const VarDecl &Var;
  336. CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
  337. const VarDecl *Var)
  338. : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
  339. void Emit(CodeGenFunction &CGF, Flags flags) {
  340. DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
  341. SourceLocation());
  342. // Compute the address of the local variable, in case it's a byref
  343. // or something.
  344. llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
  345. // In some cases, the type of the function argument will be different from
  346. // the type of the pointer. An example of this is
  347. // void f(void* arg);
  348. // __attribute__((cleanup(f))) void *g;
  349. //
  350. // To fix this we insert a bitcast here.
  351. QualType ArgTy = FnInfo.arg_begin()->type;
  352. llvm::Value *Arg =
  353. CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
  354. CallArgList Args;
  355. Args.add(RValue::get(Arg),
  356. CGF.getContext().getPointerType(Var.getType()));
  357. CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
  358. }
  359. };
  360. }
  361. /// EmitAutoVarWithLifetime - Does the setup required for an automatic
  362. /// variable with lifetime.
  363. static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
  364. llvm::Value *addr,
  365. Qualifiers::ObjCLifetime lifetime) {
  366. switch (lifetime) {
  367. case Qualifiers::OCL_None:
  368. llvm_unreachable("present but none");
  369. case Qualifiers::OCL_ExplicitNone:
  370. // nothing to do
  371. break;
  372. case Qualifiers::OCL_Strong: {
  373. CodeGenFunction::Destroyer &destroyer =
  374. (var.hasAttr<ObjCPreciseLifetimeAttr>()
  375. ? CodeGenFunction::destroyARCStrongPrecise
  376. : CodeGenFunction::destroyARCStrongImprecise);
  377. CleanupKind cleanupKind = CGF.getARCCleanupKind();
  378. CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
  379. cleanupKind & EHCleanup);
  380. break;
  381. }
  382. case Qualifiers::OCL_Autoreleasing:
  383. // nothing to do
  384. break;
  385. case Qualifiers::OCL_Weak:
  386. // __weak objects always get EH cleanups; otherwise, exceptions
  387. // could cause really nasty crashes instead of mere leaks.
  388. CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
  389. CodeGenFunction::destroyARCWeak,
  390. /*useEHCleanup*/ true);
  391. break;
  392. }
  393. }
  394. static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
  395. if (const Expr *e = dyn_cast<Expr>(s)) {
  396. // Skip the most common kinds of expressions that make
  397. // hierarchy-walking expensive.
  398. s = e = e->IgnoreParenCasts();
  399. if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
  400. return (ref->getDecl() == &var);
  401. }
  402. for (Stmt::const_child_range children = s->children(); children; ++children)
  403. // children might be null; as in missing decl or conditional of an if-stmt.
  404. if ((*children) && isAccessedBy(var, *children))
  405. return true;
  406. return false;
  407. }
  408. static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
  409. if (!decl) return false;
  410. if (!isa<VarDecl>(decl)) return false;
  411. const VarDecl *var = cast<VarDecl>(decl);
  412. return isAccessedBy(*var, e);
  413. }
  414. static void drillIntoBlockVariable(CodeGenFunction &CGF,
  415. LValue &lvalue,
  416. const VarDecl *var) {
  417. lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
  418. }
  419. void CodeGenFunction::EmitScalarInit(const Expr *init,
  420. const ValueDecl *D,
  421. LValue lvalue,
  422. bool capturedByInit) {
  423. Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
  424. if (!lifetime) {
  425. llvm::Value *value = EmitScalarExpr(init);
  426. if (capturedByInit)
  427. drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
  428. EmitStoreThroughLValue(RValue::get(value), lvalue);
  429. return;
  430. }
  431. // If we're emitting a value with lifetime, we have to do the
  432. // initialization *before* we leave the cleanup scopes.
  433. CodeGenFunction::RunCleanupsScope Scope(*this);
  434. if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init))
  435. init = ewc->getSubExpr();
  436. // We have to maintain the illusion that the variable is
  437. // zero-initialized. If the variable might be accessed in its
  438. // initializer, zero-initialize before running the initializer, then
  439. // actually perform the initialization with an assign.
  440. bool accessedByInit = false;
  441. if (lifetime != Qualifiers::OCL_ExplicitNone)
  442. accessedByInit = (capturedByInit || isAccessedBy(D, init));
  443. if (accessedByInit) {
  444. LValue tempLV = lvalue;
  445. // Drill down to the __block object if necessary.
  446. if (capturedByInit) {
  447. // We can use a simple GEP for this because it can't have been
  448. // moved yet.
  449. tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
  450. getByRefValueLLVMField(cast<VarDecl>(D))));
  451. }
  452. llvm::PointerType *ty
  453. = cast<llvm::PointerType>(tempLV.getAddress()->getType());
  454. ty = cast<llvm::PointerType>(ty->getElementType());
  455. llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
  456. // If __weak, we want to use a barrier under certain conditions.
  457. if (lifetime == Qualifiers::OCL_Weak)
  458. EmitARCInitWeak(tempLV.getAddress(), zero);
  459. // Otherwise just do a simple store.
  460. else
  461. EmitStoreOfScalar(zero, tempLV);
  462. }
  463. // Emit the initializer.
  464. llvm::Value *value = 0;
  465. switch (lifetime) {
  466. case Qualifiers::OCL_None:
  467. llvm_unreachable("present but none");
  468. case Qualifiers::OCL_ExplicitNone:
  469. // nothing to do
  470. value = EmitScalarExpr(init);
  471. break;
  472. case Qualifiers::OCL_Strong: {
  473. value = EmitARCRetainScalarExpr(init);
  474. break;
  475. }
  476. case Qualifiers::OCL_Weak: {
  477. // No way to optimize a producing initializer into this. It's not
  478. // worth optimizing for, because the value will immediately
  479. // disappear in the common case.
  480. value = EmitScalarExpr(init);
  481. if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
  482. if (accessedByInit)
  483. EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
  484. else
  485. EmitARCInitWeak(lvalue.getAddress(), value);
  486. return;
  487. }
  488. case Qualifiers::OCL_Autoreleasing:
  489. value = EmitARCRetainAutoreleaseScalarExpr(init);
  490. break;
  491. }
  492. if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
  493. // If the variable might have been accessed by its initializer, we
  494. // might have to initialize with a barrier. We have to do this for
  495. // both __weak and __strong, but __weak got filtered out above.
  496. if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
  497. llvm::Value *oldValue = EmitLoadOfScalar(lvalue);
  498. EmitStoreOfScalar(value, lvalue);
  499. EmitARCRelease(oldValue, /*precise*/ false);
  500. return;
  501. }
  502. EmitStoreOfScalar(value, lvalue);
  503. }
  504. /// EmitScalarInit - Initialize the given lvalue with the given object.
  505. void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
  506. Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
  507. if (!lifetime)
  508. return EmitStoreThroughLValue(RValue::get(init), lvalue);
  509. switch (lifetime) {
  510. case Qualifiers::OCL_None:
  511. llvm_unreachable("present but none");
  512. case Qualifiers::OCL_ExplicitNone:
  513. // nothing to do
  514. break;
  515. case Qualifiers::OCL_Strong:
  516. init = EmitARCRetain(lvalue.getType(), init);
  517. break;
  518. case Qualifiers::OCL_Weak:
  519. // Initialize and then skip the primitive store.
  520. EmitARCInitWeak(lvalue.getAddress(), init);
  521. return;
  522. case Qualifiers::OCL_Autoreleasing:
  523. init = EmitARCRetainAutorelease(lvalue.getType(), init);
  524. break;
  525. }
  526. EmitStoreOfScalar(init, lvalue);
  527. }
  528. /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
  529. /// non-zero parts of the specified initializer with equal or fewer than
  530. /// NumStores scalar stores.
  531. static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
  532. unsigned &NumStores) {
  533. // Zero and Undef never requires any extra stores.
  534. if (isa<llvm::ConstantAggregateZero>(Init) ||
  535. isa<llvm::ConstantPointerNull>(Init) ||
  536. isa<llvm::UndefValue>(Init))
  537. return true;
  538. if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
  539. isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
  540. isa<llvm::ConstantExpr>(Init))
  541. return Init->isNullValue() || NumStores--;
  542. // See if we can emit each element.
  543. if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
  544. for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
  545. llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
  546. if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
  547. return false;
  548. }
  549. return true;
  550. }
  551. // Anything else is hard and scary.
  552. return false;
  553. }
  554. /// emitStoresForInitAfterMemset - For inits that
  555. /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
  556. /// stores that would be required.
  557. static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
  558. bool isVolatile, CGBuilderTy &Builder) {
  559. // Zero doesn't require any stores.
  560. if (isa<llvm::ConstantAggregateZero>(Init) ||
  561. isa<llvm::ConstantPointerNull>(Init) ||
  562. isa<llvm::UndefValue>(Init))
  563. return;
  564. if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
  565. isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
  566. isa<llvm::ConstantExpr>(Init)) {
  567. if (!Init->isNullValue())
  568. Builder.CreateStore(Init, Loc, isVolatile);
  569. return;
  570. }
  571. assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
  572. "Unknown value type!");
  573. for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
  574. llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
  575. if (Elt->isNullValue()) continue;
  576. // Otherwise, get a pointer to the element and emit it.
  577. emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
  578. isVolatile, Builder);
  579. }
  580. }
  581. /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
  582. /// plus some stores to initialize a local variable instead of using a memcpy
  583. /// from a constant global. It is beneficial to use memset if the global is all
  584. /// zeros, or mostly zeros and large.
  585. static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
  586. uint64_t GlobalSize) {
  587. // If a global is all zeros, always use a memset.
  588. if (isa<llvm::ConstantAggregateZero>(Init)) return true;
  589. // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
  590. // do it if it will require 6 or fewer scalar stores.
  591. // TODO: Should budget depends on the size? Avoiding a large global warrants
  592. // plopping in more stores.
  593. unsigned StoreBudget = 6;
  594. uint64_t SizeLimit = 32;
  595. return GlobalSize > SizeLimit &&
  596. canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
  597. }
  598. /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
  599. /// variable declaration with auto, register, or no storage class specifier.
  600. /// These turn into simple stack objects, or GlobalValues depending on target.
  601. void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
  602. AutoVarEmission emission = EmitAutoVarAlloca(D);
  603. EmitAutoVarInit(emission);
  604. EmitAutoVarCleanups(emission);
  605. }
  606. /// EmitAutoVarAlloca - Emit the alloca and debug information for a
  607. /// local variable. Does not emit initalization or destruction.
  608. CodeGenFunction::AutoVarEmission
  609. CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
  610. QualType Ty = D.getType();
  611. AutoVarEmission emission(D);
  612. bool isByRef = D.hasAttr<BlocksAttr>();
  613. emission.IsByRef = isByRef;
  614. CharUnits alignment = getContext().getDeclAlign(&D);
  615. emission.Alignment = alignment;
  616. // If the type is variably-modified, emit all the VLA sizes for it.
  617. if (Ty->isVariablyModifiedType())
  618. EmitVariablyModifiedType(Ty);
  619. llvm::Value *DeclPtr;
  620. if (Ty->isConstantSizeType()) {
  621. if (!Target.useGlobalsForAutomaticVariables()) {
  622. bool NRVO = getContext().getLangOptions().ElideConstructors &&
  623. D.isNRVOVariable();
  624. // If this value is a POD array or struct with a statically
  625. // determinable constant initializer, there are optimizations we
  626. // can do.
  627. // TODO: we can potentially constant-evaluate non-POD structs and
  628. // arrays as long as the initialization is trivial (e.g. if they
  629. // have a non-trivial destructor, but not a non-trivial constructor).
  630. if (D.getInit() &&
  631. (Ty->isArrayType() || Ty->isRecordType()) &&
  632. (Ty.isPODType(getContext()) ||
  633. getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
  634. D.getInit()->isConstantInitializer(getContext(), false)) {
  635. // If the variable's a const type, and it's neither an NRVO
  636. // candidate nor a __block variable, emit it as a global instead.
  637. if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
  638. !NRVO && !isByRef) {
  639. EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
  640. emission.Address = 0; // signal this condition to later callbacks
  641. assert(emission.wasEmittedAsGlobal());
  642. return emission;
  643. }
  644. // Otherwise, tell the initialization code that we're in this case.
  645. emission.IsConstantAggregate = true;
  646. }
  647. // A normal fixed sized variable becomes an alloca in the entry block,
  648. // unless it's an NRVO variable.
  649. llvm::Type *LTy = ConvertTypeForMem(Ty);
  650. if (NRVO) {
  651. // The named return value optimization: allocate this variable in the
  652. // return slot, so that we can elide the copy when returning this
  653. // variable (C++0x [class.copy]p34).
  654. DeclPtr = ReturnValue;
  655. if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
  656. if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
  657. // Create a flag that is used to indicate when the NRVO was applied
  658. // to this variable. Set it to zero to indicate that NRVO was not
  659. // applied.
  660. llvm::Value *Zero = Builder.getFalse();
  661. llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
  662. EnsureInsertPoint();
  663. Builder.CreateStore(Zero, NRVOFlag);
  664. // Record the NRVO flag for this variable.
  665. NRVOFlags[&D] = NRVOFlag;
  666. emission.NRVOFlag = NRVOFlag;
  667. }
  668. }
  669. } else {
  670. if (isByRef)
  671. LTy = BuildByRefType(&D);
  672. llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
  673. Alloc->setName(D.getNameAsString());
  674. CharUnits allocaAlignment = alignment;
  675. if (isByRef)
  676. allocaAlignment = std::max(allocaAlignment,
  677. getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
  678. Alloc->setAlignment(allocaAlignment.getQuantity());
  679. DeclPtr = Alloc;
  680. }
  681. } else {
  682. // Targets that don't support recursion emit locals as globals.
  683. const char *Class =
  684. D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
  685. DeclPtr = CreateStaticVarDecl(D, Class,
  686. llvm::GlobalValue::InternalLinkage);
  687. }
  688. } else {
  689. EnsureInsertPoint();
  690. if (!DidCallStackSave) {
  691. // Save the stack.
  692. llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
  693. llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
  694. llvm::Value *V = Builder.CreateCall(F);
  695. Builder.CreateStore(V, Stack);
  696. DidCallStackSave = true;
  697. // Push a cleanup block and restore the stack there.
  698. // FIXME: in general circumstances, this should be an EH cleanup.
  699. EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
  700. }
  701. llvm::Value *elementCount;
  702. QualType elementType;
  703. llvm::tie(elementCount, elementType) = getVLASize(Ty);
  704. llvm::Type *llvmTy = ConvertTypeForMem(elementType);
  705. // Allocate memory for the array.
  706. llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
  707. vla->setAlignment(alignment.getQuantity());
  708. DeclPtr = vla;
  709. }
  710. llvm::Value *&DMEntry = LocalDeclMap[&D];
  711. assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
  712. DMEntry = DeclPtr;
  713. emission.Address = DeclPtr;
  714. // Emit debug info for local var declaration.
  715. if (HaveInsertPoint())
  716. if (CGDebugInfo *DI = getDebugInfo()) {
  717. DI->setLocation(D.getLocation());
  718. if (Target.useGlobalsForAutomaticVariables()) {
  719. DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
  720. } else
  721. DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
  722. }
  723. return emission;
  724. }
  725. /// Determines whether the given __block variable is potentially
  726. /// captured by the given expression.
  727. static bool isCapturedBy(const VarDecl &var, const Expr *e) {
  728. // Skip the most common kinds of expressions that make
  729. // hierarchy-walking expensive.
  730. e = e->IgnoreParenCasts();
  731. if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
  732. const BlockDecl *block = be->getBlockDecl();
  733. for (BlockDecl::capture_const_iterator i = block->capture_begin(),
  734. e = block->capture_end(); i != e; ++i) {
  735. if (i->getVariable() == &var)
  736. return true;
  737. }
  738. // No need to walk into the subexpressions.
  739. return false;
  740. }
  741. if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
  742. const CompoundStmt *CS = SE->getSubStmt();
  743. for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
  744. BE = CS->body_end(); BI != BE; ++BI)
  745. if (Expr *E = dyn_cast<Expr>((*BI))) {
  746. if (isCapturedBy(var, E))
  747. return true;
  748. }
  749. else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) {
  750. // special case declarations
  751. for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
  752. I != E; ++I) {
  753. if (VarDecl *VD = dyn_cast<VarDecl>((*I))) {
  754. Expr *Init = VD->getInit();
  755. if (Init && isCapturedBy(var, Init))
  756. return true;
  757. }
  758. }
  759. }
  760. else
  761. // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
  762. // Later, provide code to poke into statements for capture analysis.
  763. return true;
  764. return false;
  765. }
  766. for (Stmt::const_child_range children = e->children(); children; ++children)
  767. if (isCapturedBy(var, cast<Expr>(*children)))
  768. return true;
  769. return false;
  770. }
  771. /// \brief Determine whether the given initializer is trivial in the sense
  772. /// that it requires no code to be generated.
  773. static bool isTrivialInitializer(const Expr *Init) {
  774. if (!Init)
  775. return true;
  776. if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
  777. if (CXXConstructorDecl *Constructor = Construct->getConstructor())
  778. if (Constructor->isTrivial() &&
  779. Constructor->isDefaultConstructor() &&
  780. !Construct->requiresZeroInitialization())
  781. return true;
  782. return false;
  783. }
  784. void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
  785. assert(emission.Variable && "emission was not valid!");
  786. // If this was emitted as a global constant, we're done.
  787. if (emission.wasEmittedAsGlobal()) return;
  788. const VarDecl &D = *emission.Variable;
  789. QualType type = D.getType();
  790. // If this local has an initializer, emit it now.
  791. const Expr *Init = D.getInit();
  792. // If we are at an unreachable point, we don't need to emit the initializer
  793. // unless it contains a label.
  794. if (!HaveInsertPoint()) {
  795. if (!Init || !ContainsLabel(Init)) return;
  796. EnsureInsertPoint();
  797. }
  798. // Initialize the structure of a __block variable.
  799. if (emission.IsByRef)
  800. emitByrefStructureInit(emission);
  801. if (isTrivialInitializer(Init))
  802. return;
  803. CharUnits alignment = emission.Alignment;
  804. // Check whether this is a byref variable that's potentially
  805. // captured and moved by its own initializer. If so, we'll need to
  806. // emit the initializer first, then copy into the variable.
  807. bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
  808. llvm::Value *Loc =
  809. capturedByInit ? emission.Address : emission.getObjectAddress(*this);
  810. if (!emission.IsConstantAggregate) {
  811. LValue lv = MakeAddrLValue(Loc, type, alignment.getQuantity());
  812. lv.setNonGC(true);
  813. return EmitExprAsInit(Init, &D, lv, capturedByInit);
  814. }
  815. // If this is a simple aggregate initialization, we can optimize it
  816. // in various ways.
  817. assert(!capturedByInit && "constant init contains a capturing block?");
  818. bool isVolatile = type.isVolatileQualified();
  819. llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this);
  820. assert(constant != 0 && "Wasn't a simple constant init?");
  821. llvm::Value *SizeVal =
  822. llvm::ConstantInt::get(IntPtrTy,
  823. getContext().getTypeSizeInChars(type).getQuantity());
  824. llvm::Type *BP = Int8PtrTy;
  825. if (Loc->getType() != BP)
  826. Loc = Builder.CreateBitCast(Loc, BP, "tmp");
  827. // If the initializer is all or mostly zeros, codegen with memset then do
  828. // a few stores afterward.
  829. if (shouldUseMemSetPlusStoresToInitialize(constant,
  830. CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
  831. Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
  832. alignment.getQuantity(), isVolatile);
  833. if (!constant->isNullValue()) {
  834. Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
  835. emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
  836. }
  837. } else {
  838. // Otherwise, create a temporary global with the initializer then
  839. // memcpy from the global to the alloca.
  840. std::string Name = GetStaticDeclName(*this, D, ".");
  841. llvm::GlobalVariable *GV =
  842. new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
  843. llvm::GlobalValue::PrivateLinkage,
  844. constant, Name, 0, false, 0);
  845. GV->setAlignment(alignment.getQuantity());
  846. GV->setUnnamedAddr(true);
  847. llvm::Value *SrcPtr = GV;
  848. if (SrcPtr->getType() != BP)
  849. SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
  850. Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
  851. isVolatile);
  852. }
  853. }
  854. /// Emit an expression as an initializer for a variable at the given
  855. /// location. The expression is not necessarily the normal
  856. /// initializer for the variable, and the address is not necessarily
  857. /// its normal location.
  858. ///
  859. /// \param init the initializing expression
  860. /// \param var the variable to act as if we're initializing
  861. /// \param loc the address to initialize; its type is a pointer
  862. /// to the LLVM mapping of the variable's type
  863. /// \param alignment the alignment of the address
  864. /// \param capturedByInit true if the variable is a __block variable
  865. /// whose address is potentially changed by the initializer
  866. void CodeGenFunction::EmitExprAsInit(const Expr *init,
  867. const ValueDecl *D,
  868. LValue lvalue,
  869. bool capturedByInit) {
  870. QualType type = D->getType();
  871. if (type->isReferenceType()) {
  872. RValue rvalue = EmitReferenceBindingToExpr(init, D);
  873. if (capturedByInit)
  874. drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
  875. EmitStoreThroughLValue(rvalue, lvalue);
  876. } else if (!hasAggregateLLVMType(type)) {
  877. EmitScalarInit(init, D, lvalue, capturedByInit);
  878. } else if (type->isAnyComplexType()) {
  879. ComplexPairTy complex = EmitComplexExpr(init);
  880. if (capturedByInit)
  881. drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
  882. StoreComplexToAddr(complex, lvalue.getAddress(), lvalue.isVolatile());
  883. } else {
  884. // TODO: how can we delay here if D is captured by its initializer?
  885. EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
  886. AggValueSlot::IsDestructed,
  887. AggValueSlot::DoesNotNeedGCBarriers,
  888. AggValueSlot::IsNotAliased));
  889. }
  890. }
  891. /// Enter a destroy cleanup for the given local variable.
  892. void CodeGenFunction::emitAutoVarTypeCleanup(
  893. const CodeGenFunction::AutoVarEmission &emission,
  894. QualType::DestructionKind dtorKind) {
  895. assert(dtorKind != QualType::DK_none);
  896. // Note that for __block variables, we want to destroy the
  897. // original stack object, not the possibly forwarded object.
  898. llvm::Value *addr = emission.getObjectAddress(*this);
  899. const VarDecl *var = emission.Variable;
  900. QualType type = var->getType();
  901. CleanupKind cleanupKind = NormalAndEHCleanup;
  902. CodeGenFunction::Destroyer *destroyer = 0;
  903. switch (dtorKind) {
  904. case QualType::DK_none:
  905. llvm_unreachable("no cleanup for trivially-destructible variable");
  906. case QualType::DK_cxx_destructor:
  907. // If there's an NRVO flag on the emission, we need a different
  908. // cleanup.
  909. if (emission.NRVOFlag) {
  910. assert(!type->isArrayType());
  911. CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
  912. EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
  913. emission.NRVOFlag);
  914. return;
  915. }
  916. break;
  917. case QualType::DK_objc_strong_lifetime:
  918. // Suppress cleanups for pseudo-strong variables.
  919. if (var->isARCPseudoStrong()) return;
  920. // Otherwise, consider whether to use an EH cleanup or not.
  921. cleanupKind = getARCCleanupKind();
  922. // Use the imprecise destroyer by default.
  923. if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
  924. destroyer = CodeGenFunction::destroyARCStrongImprecise;
  925. break;
  926. case QualType::DK_objc_weak_lifetime:
  927. break;
  928. }
  929. // If we haven't chosen a more specific destroyer, use the default.
  930. if (!destroyer) destroyer = &getDestroyer(dtorKind);
  931. // Use an EH cleanup in array destructors iff the destructor itself
  932. // is being pushed as an EH cleanup.
  933. bool useEHCleanup = (cleanupKind & EHCleanup);
  934. EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
  935. useEHCleanup);
  936. }
  937. void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
  938. assert(emission.Variable && "emission was not valid!");
  939. // If this was emitted as a global constant, we're done.
  940. if (emission.wasEmittedAsGlobal()) return;
  941. const VarDecl &D = *emission.Variable;
  942. // Check the type for a cleanup.
  943. if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
  944. emitAutoVarTypeCleanup(emission, dtorKind);
  945. // In GC mode, honor objc_precise_lifetime.
  946. if (getLangOptions().getGCMode() != LangOptions::NonGC &&
  947. D.hasAttr<ObjCPreciseLifetimeAttr>()) {
  948. EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
  949. }
  950. // Handle the cleanup attribute.
  951. if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
  952. const FunctionDecl *FD = CA->getFunctionDecl();
  953. llvm::Constant *F = CGM.GetAddrOfFunction(FD);
  954. assert(F && "Could not find function!");
  955. const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
  956. EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
  957. }
  958. // If this is a block variable, call _Block_object_destroy
  959. // (on the unforwarded address).
  960. if (emission.IsByRef)
  961. enterByrefCleanup(emission);
  962. }
  963. CodeGenFunction::Destroyer &
  964. CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
  965. // This is surprisingly compiler-dependent. GCC 4.2 can't bind
  966. // references to functions directly in returns, and using '*&foo'
  967. // confuses MSVC. Luckily, the following code pattern works in both.
  968. Destroyer *destroyer = 0;
  969. switch (kind) {
  970. case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
  971. case QualType::DK_cxx_destructor:
  972. destroyer = &destroyCXXObject;
  973. break;
  974. case QualType::DK_objc_strong_lifetime:
  975. destroyer = &destroyARCStrongPrecise;
  976. break;
  977. case QualType::DK_objc_weak_lifetime:
  978. destroyer = &destroyARCWeak;
  979. break;
  980. }
  981. return *destroyer;
  982. }
  983. /// pushDestroy - Push the standard destructor for the given type.
  984. void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
  985. llvm::Value *addr, QualType type) {
  986. assert(dtorKind && "cannot push destructor for trivial type");
  987. CleanupKind cleanupKind = getCleanupKind(dtorKind);
  988. pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
  989. cleanupKind & EHCleanup);
  990. }
  991. void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
  992. QualType type, Destroyer &destroyer,
  993. bool useEHCleanupForArray) {
  994. pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
  995. destroyer, useEHCleanupForArray);
  996. }
  997. /// emitDestroy - Immediately perform the destruction of the given
  998. /// object.
  999. ///
  1000. /// \param addr - the address of the object; a type*
  1001. /// \param type - the type of the object; if an array type, all
  1002. /// objects are destroyed in reverse order
  1003. /// \param destroyer - the function to call to destroy individual
  1004. /// elements
  1005. /// \param useEHCleanupForArray - whether an EH cleanup should be
  1006. /// used when destroying array elements, in case one of the
  1007. /// destructions throws an exception
  1008. void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
  1009. Destroyer &destroyer,
  1010. bool useEHCleanupForArray) {
  1011. const ArrayType *arrayType = getContext().getAsArrayType(type);
  1012. if (!arrayType)
  1013. return destroyer(*this, addr, type);
  1014. llvm::Value *begin = addr;
  1015. llvm::Value *length = emitArrayLength(arrayType, type, begin);
  1016. // Normally we have to check whether the array is zero-length.
  1017. bool checkZeroLength = true;
  1018. // But if the array length is constant, we can suppress that.
  1019. if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
  1020. // ...and if it's constant zero, we can just skip the entire thing.
  1021. if (constLength->isZero()) return;
  1022. checkZeroLength = false;
  1023. }
  1024. llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
  1025. emitArrayDestroy(begin, end, type, destroyer,
  1026. checkZeroLength, useEHCleanupForArray);
  1027. }
  1028. /// emitArrayDestroy - Destroys all the elements of the given array,
  1029. /// beginning from last to first. The array cannot be zero-length.
  1030. ///
  1031. /// \param begin - a type* denoting the first element of the array
  1032. /// \param end - a type* denoting one past the end of the array
  1033. /// \param type - the element type of the array
  1034. /// \param destroyer - the function to call to destroy elements
  1035. /// \param useEHCleanup - whether to push an EH cleanup to destroy
  1036. /// the remaining elements in case the destruction of a single
  1037. /// element throws
  1038. void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
  1039. llvm::Value *end,
  1040. QualType type,
  1041. Destroyer &destroyer,
  1042. bool checkZeroLength,
  1043. bool useEHCleanup) {
  1044. assert(!type->isArrayType());
  1045. // The basic structure here is a do-while loop, because we don't
  1046. // need to check for the zero-element case.
  1047. llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
  1048. llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
  1049. if (checkZeroLength) {
  1050. llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
  1051. "arraydestroy.isempty");
  1052. Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
  1053. }
  1054. // Enter the loop body, making that address the current address.
  1055. llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
  1056. EmitBlock(bodyBB);
  1057. llvm::PHINode *elementPast =
  1058. Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
  1059. elementPast->addIncoming(end, entryBB);
  1060. // Shift the address back by one element.
  1061. llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
  1062. llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
  1063. "arraydestroy.element");
  1064. if (useEHCleanup)
  1065. pushRegularPartialArrayCleanup(begin, element, type, destroyer);
  1066. // Perform the actual destruction there.
  1067. destroyer(*this, element, type);
  1068. if (useEHCleanup)
  1069. PopCleanupBlock();
  1070. // Check whether we've reached the end.
  1071. llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
  1072. Builder.CreateCondBr(done, doneBB, bodyBB);
  1073. elementPast->addIncoming(element, Builder.GetInsertBlock());
  1074. // Done.
  1075. EmitBlock(doneBB);
  1076. }
  1077. /// Perform partial array destruction as if in an EH cleanup. Unlike
  1078. /// emitArrayDestroy, the element type here may still be an array type.
  1079. static void emitPartialArrayDestroy(CodeGenFunction &CGF,
  1080. llvm::Value *begin, llvm::Value *end,
  1081. QualType type,
  1082. CodeGenFunction::Destroyer &destroyer) {
  1083. // If the element type is itself an array, drill down.
  1084. unsigned arrayDepth = 0;
  1085. while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
  1086. // VLAs don't require a GEP index to walk into.
  1087. if (!isa<VariableArrayType>(arrayType))
  1088. arrayDepth++;
  1089. type = arrayType->getElementType();
  1090. }
  1091. if (arrayDepth) {
  1092. llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1);
  1093. SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero);
  1094. begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
  1095. end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
  1096. }
  1097. // Destroy the array. We don't ever need an EH cleanup because we
  1098. // assume that we're in an EH cleanup ourselves, so a throwing
  1099. // destructor causes an immediate terminate.
  1100. CGF.emitArrayDestroy(begin, end, type, destroyer,
  1101. /*checkZeroLength*/ true, /*useEHCleanup*/ false);
  1102. }
  1103. namespace {
  1104. /// RegularPartialArrayDestroy - a cleanup which performs a partial
  1105. /// array destroy where the end pointer is regularly determined and
  1106. /// does not need to be loaded from a local.
  1107. class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
  1108. llvm::Value *ArrayBegin;
  1109. llvm::Value *ArrayEnd;
  1110. QualType ElementType;
  1111. CodeGenFunction::Destroyer &Destroyer;
  1112. public:
  1113. RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
  1114. QualType elementType,
  1115. CodeGenFunction::Destroyer *destroyer)
  1116. : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
  1117. ElementType(elementType), Destroyer(*destroyer) {}
  1118. void Emit(CodeGenFunction &CGF, Flags flags) {
  1119. emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
  1120. ElementType, Destroyer);
  1121. }
  1122. };
  1123. /// IrregularPartialArrayDestroy - a cleanup which performs a
  1124. /// partial array destroy where the end pointer is irregularly
  1125. /// determined and must be loaded from a local.
  1126. class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
  1127. llvm::Value *ArrayBegin;
  1128. llvm::Value *ArrayEndPointer;
  1129. QualType ElementType;
  1130. CodeGenFunction::Destroyer &Destroyer;
  1131. public:
  1132. IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
  1133. llvm::Value *arrayEndPointer,
  1134. QualType elementType,
  1135. CodeGenFunction::Destroyer *destroyer)
  1136. : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
  1137. ElementType(elementType), Destroyer(*destroyer) {}
  1138. void Emit(CodeGenFunction &CGF, Flags flags) {
  1139. llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
  1140. emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
  1141. ElementType, Destroyer);
  1142. }
  1143. };
  1144. }
  1145. /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
  1146. /// already-constructed elements of the given array. The cleanup
  1147. /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
  1148. ///
  1149. /// \param elementType - the immediate element type of the array;
  1150. /// possibly still an array type
  1151. /// \param array - a value of type elementType*
  1152. /// \param destructionKind - the kind of destruction required
  1153. /// \param initializedElementCount - a value of type size_t* holding
  1154. /// the number of successfully-constructed elements
  1155. void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
  1156. llvm::Value *arrayEndPointer,
  1157. QualType elementType,
  1158. Destroyer &destroyer) {
  1159. pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
  1160. arrayBegin, arrayEndPointer,
  1161. elementType, &destroyer);
  1162. }
  1163. /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
  1164. /// already-constructed elements of the given array. The cleanup
  1165. /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
  1166. ///
  1167. /// \param elementType - the immediate element type of the array;
  1168. /// possibly still an array type
  1169. /// \param array - a value of type elementType*
  1170. /// \param destructionKind - the kind of destruction required
  1171. /// \param initializedElementCount - a value of type size_t* holding
  1172. /// the number of successfully-constructed elements
  1173. void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
  1174. llvm::Value *arrayEnd,
  1175. QualType elementType,
  1176. Destroyer &destroyer) {
  1177. pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
  1178. arrayBegin, arrayEnd,
  1179. elementType, &destroyer);
  1180. }
  1181. namespace {
  1182. /// A cleanup to perform a release of an object at the end of a
  1183. /// function. This is used to balance out the incoming +1 of a
  1184. /// ns_consumed argument when we can't reasonably do that just by
  1185. /// not doing the initial retain for a __block argument.
  1186. struct ConsumeARCParameter : EHScopeStack::Cleanup {
  1187. ConsumeARCParameter(llvm::Value *param) : Param(param) {}
  1188. llvm::Value *Param;
  1189. void Emit(CodeGenFunction &CGF, Flags flags) {
  1190. CGF.EmitARCRelease(Param, /*precise*/ false);
  1191. }
  1192. };
  1193. }
  1194. /// Emit an alloca (or GlobalValue depending on target)
  1195. /// for the specified parameter and set up LocalDeclMap.
  1196. void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
  1197. unsigned ArgNo) {
  1198. // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
  1199. assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
  1200. "Invalid argument to EmitParmDecl");
  1201. Arg->setName(D.getName());
  1202. // Use better IR generation for certain implicit parameters.
  1203. if (isa<ImplicitParamDecl>(D)) {
  1204. // The only implicit argument a block has is its literal.
  1205. if (BlockInfo) {
  1206. LocalDeclMap[&D] = Arg;
  1207. if (CGDebugInfo *DI = getDebugInfo()) {
  1208. DI->setLocation(D.getLocation());
  1209. DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
  1210. }
  1211. return;
  1212. }
  1213. }
  1214. QualType Ty = D.getType();
  1215. llvm::Value *DeclPtr;
  1216. // If this is an aggregate or variable sized value, reuse the input pointer.
  1217. if (!Ty->isConstantSizeType() ||
  1218. CodeGenFunction::hasAggregateLLVMType(Ty)) {
  1219. DeclPtr = Arg;
  1220. } else {
  1221. // Otherwise, create a temporary to hold the value.
  1222. DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
  1223. bool doStore = true;
  1224. Qualifiers qs = Ty.getQualifiers();
  1225. if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
  1226. // We honor __attribute__((ns_consumed)) for types with lifetime.
  1227. // For __strong, it's handled by just skipping the initial retain;
  1228. // otherwise we have to balance out the initial +1 with an extra
  1229. // cleanup to do the release at the end of the function.
  1230. bool isConsumed = D.hasAttr<NSConsumedAttr>();
  1231. // 'self' is always formally __strong, but if this is not an
  1232. // init method then we don't want to retain it.
  1233. if (D.isARCPseudoStrong()) {
  1234. const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
  1235. assert(&D == method->getSelfDecl());
  1236. assert(lt == Qualifiers::OCL_Strong);
  1237. assert(qs.hasConst());
  1238. assert(method->getMethodFamily() != OMF_init);
  1239. (void) method;
  1240. lt = Qualifiers::OCL_ExplicitNone;
  1241. }
  1242. if (lt == Qualifiers::OCL_Strong) {
  1243. if (!isConsumed)
  1244. // Don't use objc_retainBlock for block pointers, because we
  1245. // don't want to Block_copy something just because we got it
  1246. // as a parameter.
  1247. Arg = EmitARCRetainNonBlock(Arg);
  1248. } else {
  1249. // Push the cleanup for a consumed parameter.
  1250. if (isConsumed)
  1251. EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg);
  1252. if (lt == Qualifiers::OCL_Weak) {
  1253. EmitARCInitWeak(DeclPtr, Arg);
  1254. doStore = false; // The weak init is a store, no need to do two
  1255. }
  1256. }
  1257. // Enter the cleanup scope.
  1258. EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
  1259. }
  1260. // Store the initial value into the alloca.
  1261. if (doStore) {
  1262. LValue lv = MakeAddrLValue(DeclPtr, Ty,
  1263. getContext().getDeclAlign(&D).getQuantity());
  1264. EmitStoreOfScalar(Arg, lv);
  1265. }
  1266. }
  1267. llvm::Value *&DMEntry = LocalDeclMap[&D];
  1268. assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
  1269. DMEntry = DeclPtr;
  1270. // Emit debug info for param declaration.
  1271. if (CGDebugInfo *DI = getDebugInfo())
  1272. DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
  1273. }