CGExprAgg.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
  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 Aggregate Expr nodes as LLVM code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "CGObjCRuntime.h"
  15. #include "CodeGenModule.h"
  16. #include "CGHLSLRuntime.h" // HLSL Change
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/DeclCXX.h"
  19. #include "clang/AST/DeclTemplate.h"
  20. #include "clang/AST/StmtVisitor.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/GlobalVariable.h"
  24. #include "llvm/IR/Intrinsics.h"
  25. using namespace clang;
  26. using namespace CodeGen;
  27. //===----------------------------------------------------------------------===//
  28. // Aggregate Expression Emitter
  29. //===----------------------------------------------------------------------===//
  30. namespace {
  31. class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
  32. CodeGenFunction &CGF;
  33. CGBuilderTy &Builder;
  34. AggValueSlot Dest;
  35. bool IsResultUnused;
  36. /// We want to use 'dest' as the return slot except under two
  37. /// conditions:
  38. /// - The destination slot requires garbage collection, so we
  39. /// need to use the GC API.
  40. /// - The destination slot is potentially aliased.
  41. bool shouldUseDestForReturnSlot() const {
  42. return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased());
  43. }
  44. ReturnValueSlot getReturnValueSlot() const {
  45. if (!shouldUseDestForReturnSlot())
  46. return ReturnValueSlot();
  47. return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile(), IsResultUnused);
  48. }
  49. AggValueSlot EnsureSlot(QualType T) {
  50. if (!Dest.isIgnored()) return Dest;
  51. return CGF.CreateAggTemp(T, "agg.tmp.ensured");
  52. }
  53. void EnsureDest(QualType T) {
  54. if (!Dest.isIgnored()) return;
  55. Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured");
  56. }
  57. public:
  58. AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, bool IsResultUnused)
  59. : CGF(cgf), Builder(CGF.Builder), Dest(Dest),
  60. IsResultUnused(IsResultUnused) { }
  61. //===--------------------------------------------------------------------===//
  62. // Utilities
  63. //===--------------------------------------------------------------------===//
  64. /// EmitAggLoadOfLValue - Given an expression with aggregate type that
  65. /// represents a value lvalue, this method emits the address of the lvalue,
  66. /// then loads the result into DestPtr.
  67. void EmitAggLoadOfLValue(const Expr *E);
  68. /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
  69. void EmitFinalDestCopy(QualType type, const LValue &src);
  70. void EmitFinalDestCopy(QualType type, RValue src,
  71. CharUnits srcAlignment = CharUnits::Zero());
  72. void EmitCopy(QualType type, const AggValueSlot &dest,
  73. const AggValueSlot &src);
  74. void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
  75. void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
  76. QualType elementType, InitListExpr *E);
  77. AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
  78. if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T))
  79. return AggValueSlot::NeedsGCBarriers;
  80. return AggValueSlot::DoesNotNeedGCBarriers;
  81. }
  82. bool TypeRequiresGCollection(QualType T);
  83. //===--------------------------------------------------------------------===//
  84. // Visitor Methods
  85. //===--------------------------------------------------------------------===//
  86. void Visit(Expr *E) {
  87. ApplyDebugLocation DL(CGF, E);
  88. StmtVisitor<AggExprEmitter>::Visit(E);
  89. }
  90. void VisitStmt(Stmt *S) {
  91. CGF.ErrorUnsupported(S, "aggregate expression");
  92. }
  93. void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
  94. void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
  95. Visit(GE->getResultExpr());
  96. }
  97. void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
  98. void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
  99. return Visit(E->getReplacement());
  100. }
  101. // l-values.
  102. void VisitDeclRefExpr(DeclRefExpr *E) {
  103. // For aggregates, we should always be able to emit the variable
  104. // as an l-value unless it's a reference. This is due to the fact
  105. // that we can't actually ever see a normal l2r conversion on an
  106. // aggregate in C++, and in C there's no language standard
  107. // actively preventing us from listing variables in the captures
  108. // list of a block.
  109. if (E->getDecl()->getType()->isReferenceType()) {
  110. if (CodeGenFunction::ConstantEmission result
  111. = CGF.tryEmitAsConstant(E)) {
  112. EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E));
  113. return;
  114. }
  115. }
  116. EmitAggLoadOfLValue(E);
  117. }
  118. void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
  119. void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
  120. void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
  121. void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
  122. void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
  123. EmitAggLoadOfLValue(E);
  124. }
  125. void VisitPredefinedExpr(const PredefinedExpr *E) {
  126. EmitAggLoadOfLValue(E);
  127. }
  128. // Operators.
  129. void VisitCastExpr(CastExpr *E);
  130. void VisitCallExpr(const CallExpr *E);
  131. void VisitStmtExpr(const StmtExpr *E);
  132. void VisitBinaryOperator(const BinaryOperator *BO);
  133. void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
  134. void VisitBinAssign(const BinaryOperator *E);
  135. void VisitBinComma(const BinaryOperator *E);
  136. void VisitObjCMessageExpr(ObjCMessageExpr *E);
  137. void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
  138. EmitAggLoadOfLValue(E);
  139. }
  140. void VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E);
  141. void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
  142. void VisitChooseExpr(const ChooseExpr *CE);
  143. void VisitInitListExpr(InitListExpr *E);
  144. void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
  145. void VisitNoInitExpr(NoInitExpr *E) { } // Do nothing.
  146. void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
  147. Visit(DAE->getExpr());
  148. }
  149. void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
  150. CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
  151. Visit(DIE->getExpr());
  152. }
  153. void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
  154. void VisitCXXConstructExpr(const CXXConstructExpr *E);
  155. void VisitLambdaExpr(LambdaExpr *E);
  156. void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
  157. void VisitExprWithCleanups(ExprWithCleanups *E);
  158. void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
  159. void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
  160. void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
  161. void VisitOpaqueValueExpr(OpaqueValueExpr *E);
  162. void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
  163. if (E->isGLValue()) {
  164. LValue LV = CGF.EmitPseudoObjectLValue(E);
  165. return EmitFinalDestCopy(E->getType(), LV);
  166. }
  167. CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
  168. }
  169. void VisitVAArgExpr(VAArgExpr *E);
  170. void EmitInitializationToLValue(Expr *E, LValue Address);
  171. void EmitNullInitializationToLValue(LValue Address);
  172. // case Expr::ChooseExprClass:
  173. void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
  174. void VisitAtomicExpr(AtomicExpr *E) {
  175. CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr());
  176. }
  177. };
  178. } // end anonymous namespace.
  179. //===----------------------------------------------------------------------===//
  180. // Utilities
  181. //===----------------------------------------------------------------------===//
  182. /// EmitAggLoadOfLValue - Given an expression with aggregate type that
  183. /// represents a value lvalue, this method emits the address of the lvalue,
  184. /// then loads the result into DestPtr.
  185. void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
  186. LValue LV = CGF.EmitLValue(E);
  187. // If the type of the l-value is atomic, then do an atomic load.
  188. if (LV.getType()->isAtomicType() || CGF.LValueIsSuitableForInlineAtomic(LV)) {
  189. CGF.EmitAtomicLoad(LV, E->getExprLoc(), Dest);
  190. return;
  191. }
  192. EmitFinalDestCopy(E->getType(), LV);
  193. }
  194. /// \brief True if the given aggregate type requires special GC API calls.
  195. bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
  196. // Only record types have members that might require garbage collection.
  197. const RecordType *RecordTy = T->getAs<RecordType>();
  198. if (!RecordTy) return false;
  199. // Don't mess with non-trivial C++ types.
  200. RecordDecl *Record = RecordTy->getDecl();
  201. if (isa<CXXRecordDecl>(Record) &&
  202. (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() ||
  203. !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
  204. return false;
  205. // Check whether the type has an object member.
  206. return Record->hasObjectMember();
  207. }
  208. /// \brief Perform the final move to DestPtr if for some reason
  209. /// getReturnValueSlot() didn't use it directly.
  210. ///
  211. /// The idea is that you do something like this:
  212. /// RValue Result = EmitSomething(..., getReturnValueSlot());
  213. /// EmitMoveFromReturnSlot(E, Result);
  214. ///
  215. /// If nothing interferes, this will cause the result to be emitted
  216. /// directly into the return value slot. Otherwise, a final move
  217. /// will be performed.
  218. void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) {
  219. if (shouldUseDestForReturnSlot()) {
  220. // Logically, Dest.getAddr() should equal Src.getAggregateAddr().
  221. // The possibility of undef rvalues complicates that a lot,
  222. // though, so we can't really assert.
  223. return;
  224. }
  225. // Otherwise, copy from there to the destination.
  226. assert(Dest.getAddr() != src.getAggregateAddr());
  227. std::pair<CharUnits, CharUnits> typeInfo =
  228. CGF.getContext().getTypeInfoInChars(E->getType());
  229. EmitFinalDestCopy(E->getType(), src, typeInfo.second);
  230. }
  231. /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
  232. void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src,
  233. CharUnits srcAlign) {
  234. assert(src.isAggregate() && "value must be aggregate value!");
  235. LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign);
  236. EmitFinalDestCopy(type, srcLV);
  237. }
  238. /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
  239. void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) {
  240. // If Dest is ignored, then we're evaluating an aggregate expression
  241. // in a context that doesn't care about the result. Note that loads
  242. // from volatile l-values force the existence of a non-ignored
  243. // destination.
  244. if (Dest.isIgnored())
  245. return;
  246. AggValueSlot srcAgg =
  247. AggValueSlot::forLValue(src, AggValueSlot::IsDestructed,
  248. needsGC(type), AggValueSlot::IsAliased);
  249. EmitCopy(type, Dest, srcAgg);
  250. }
  251. /// Perform a copy from the source into the destination.
  252. ///
  253. /// \param type - the type of the aggregate being copied; qualifiers are
  254. /// ignored
  255. void AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
  256. const AggValueSlot &src) {
  257. if (dest.requiresGCollection()) {
  258. CharUnits sz = CGF.getContext().getTypeSizeInChars(type);
  259. llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
  260. CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
  261. dest.getAddr(),
  262. src.getAddr(),
  263. size);
  264. return;
  265. }
  266. // If the result of the assignment is used, copy the LHS there also.
  267. // It's volatile if either side is. Use the minimum alignment of
  268. // the two sides.
  269. CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type,
  270. dest.isVolatile() || src.isVolatile(),
  271. std::min(dest.getAlignment(), src.getAlignment()));
  272. }
  273. /// \brief Emit the initializer for a std::initializer_list initialized with a
  274. /// real initializer list.
  275. void
  276. AggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
  277. // Emit an array containing the elements. The array is externally destructed
  278. // if the std::initializer_list object is.
  279. ASTContext &Ctx = CGF.getContext();
  280. LValue Array = CGF.EmitLValue(E->getSubExpr());
  281. assert(Array.isSimple() && "initializer_list array not a simple lvalue");
  282. llvm::Value *ArrayPtr = Array.getAddress();
  283. const ConstantArrayType *ArrayType =
  284. Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
  285. assert(ArrayType && "std::initializer_list constructed from non-array");
  286. // FIXME: Perform the checks on the field types in SemaInit.
  287. RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl();
  288. RecordDecl::field_iterator Field = Record->field_begin();
  289. if (Field == Record->field_end()) {
  290. CGF.ErrorUnsupported(E, "weird std::initializer_list");
  291. return;
  292. }
  293. // Start pointer.
  294. if (!Field->getType()->isPointerType() ||
  295. !Ctx.hasSameType(Field->getType()->getPointeeType(),
  296. ArrayType->getElementType())) {
  297. CGF.ErrorUnsupported(E, "weird std::initializer_list");
  298. return;
  299. }
  300. AggValueSlot Dest = EnsureSlot(E->getType());
  301. LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
  302. Dest.getAlignment());
  303. LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
  304. llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
  305. llvm::Value *IdxStart[] = { Zero, Zero };
  306. llvm::Value *ArrayStart =
  307. Builder.CreateInBoundsGEP(ArrayPtr, IdxStart, "arraystart");
  308. CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start);
  309. ++Field;
  310. if (Field == Record->field_end()) {
  311. CGF.ErrorUnsupported(E, "weird std::initializer_list");
  312. return;
  313. }
  314. llvm::Value *Size = Builder.getInt(ArrayType->getSize());
  315. LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
  316. if (Field->getType()->isPointerType() &&
  317. Ctx.hasSameType(Field->getType()->getPointeeType(),
  318. ArrayType->getElementType())) {
  319. // End pointer.
  320. llvm::Value *IdxEnd[] = { Zero, Size };
  321. llvm::Value *ArrayEnd =
  322. Builder.CreateInBoundsGEP(ArrayPtr, IdxEnd, "arrayend");
  323. CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength);
  324. } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) {
  325. // Length.
  326. CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength);
  327. } else {
  328. CGF.ErrorUnsupported(E, "weird std::initializer_list");
  329. return;
  330. }
  331. }
  332. /// \brief Determine if E is a trivial array filler, that is, one that is
  333. /// equivalent to zero-initialization.
  334. static bool isTrivialFiller(Expr *E) {
  335. if (!E)
  336. return true;
  337. if (isa<ImplicitValueInitExpr>(E))
  338. return true;
  339. if (auto *ILE = dyn_cast<InitListExpr>(E)) {
  340. if (ILE->getNumInits())
  341. return false;
  342. return isTrivialFiller(ILE->getArrayFiller());
  343. }
  344. if (auto *Cons = dyn_cast_or_null<CXXConstructExpr>(E))
  345. return Cons->getConstructor()->isDefaultConstructor() &&
  346. Cons->getConstructor()->isTrivial();
  347. // FIXME: Are there other cases where we can avoid emitting an initializer?
  348. return false;
  349. }
  350. /// \brief Emit initialization of an array from an initializer list.
  351. void AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
  352. QualType elementType, InitListExpr *E) {
  353. uint64_t NumInitElements = E->getNumInits();
  354. uint64_t NumArrayElements = AType->getNumElements();
  355. // HLSL Change Starts
  356. if (CGF.getLangOpts().HLSL &&
  357. !CGF.CGM.getHLSLRuntime().IsTrivalInitListExpr(CGF, E)) {
  358. // Generate dx.hl.init for every array, not only matrix array.
  359. CGF.CGM.getHLSLRuntime().EmitHLSLInitListExpr(CGF, E, DestPtr);
  360. return;
  361. }
  362. // HLSL Change Ends
  363. assert(NumInitElements <= NumArrayElements);
  364. // DestPtr is an array*. Construct an elementType* by drilling
  365. // down a level.
  366. llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
  367. llvm::Value *indices[] = { zero, zero };
  368. llvm::Value *begin =
  369. Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin");
  370. // Exception safety requires us to destroy all the
  371. // already-constructed members if an initializer throws.
  372. // For that, we'll need an EH cleanup.
  373. QualType::DestructionKind dtorKind = elementType.isDestructedType();
  374. llvm::AllocaInst *endOfInit = nullptr;
  375. EHScopeStack::stable_iterator cleanup;
  376. llvm::Instruction *cleanupDominator = nullptr;
  377. if (CGF.needsEHCleanup(dtorKind)) {
  378. // In principle we could tell the cleanup where we are more
  379. // directly, but the control flow can get so varied here that it
  380. // would actually be quite complex. Therefore we go through an
  381. // alloca.
  382. endOfInit = CGF.CreateTempAlloca(begin->getType(),
  383. "arrayinit.endOfInit");
  384. cleanupDominator = Builder.CreateStore(begin, endOfInit);
  385. CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
  386. CGF.getDestroyer(dtorKind));
  387. cleanup = CGF.EHStack.stable_begin();
  388. // Otherwise, remember that we didn't need a cleanup.
  389. } else {
  390. dtorKind = QualType::DK_none;
  391. }
  392. llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
  393. // The 'current element to initialize'. The invariants on this
  394. // variable are complicated. Essentially, after each iteration of
  395. // the loop, it points to the last initialized element, except
  396. // that it points to the beginning of the array before any
  397. // elements have been initialized.
  398. llvm::Value *element = begin;
  399. // Emit the explicit initializers.
  400. for (uint64_t i = 0; i != NumInitElements; ++i) {
  401. // Advance to the next element.
  402. if (i > 0) {
  403. element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
  404. // Tell the cleanup that it needs to destroy up to this
  405. // element. TODO: some of these stores can be trivially
  406. // observed to be unnecessary.
  407. if (endOfInit) Builder.CreateStore(element, endOfInit);
  408. }
  409. LValue elementLV = CGF.MakeAddrLValue(element, elementType);
  410. EmitInitializationToLValue(E->getInit(i), elementLV);
  411. }
  412. // Check whether there's a non-trivial array-fill expression.
  413. Expr *filler = E->getArrayFiller();
  414. bool hasTrivialFiller = isTrivialFiller(filler);
  415. // Any remaining elements need to be zero-initialized, possibly
  416. // using the filler expression. We can skip this if the we're
  417. // emitting to zeroed memory.
  418. if (NumInitElements != NumArrayElements &&
  419. !(Dest.isZeroed() && hasTrivialFiller &&
  420. CGF.getTypes().isZeroInitializable(elementType))) {
  421. // Use an actual loop. This is basically
  422. // do { *array++ = filler; } while (array != end);
  423. // Advance to the start of the rest of the array.
  424. if (NumInitElements) {
  425. element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
  426. if (endOfInit) Builder.CreateStore(element, endOfInit);
  427. }
  428. // Compute the end of the array.
  429. llvm::Value *end = Builder.CreateInBoundsGEP(begin,
  430. llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
  431. "arrayinit.end");
  432. llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
  433. llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
  434. // Jump into the body.
  435. CGF.EmitBlock(bodyBB);
  436. llvm::PHINode *currentElement =
  437. Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
  438. currentElement->addIncoming(element, entryBB);
  439. // Emit the actual filler expression.
  440. LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType);
  441. if (filler)
  442. EmitInitializationToLValue(filler, elementLV);
  443. else
  444. EmitNullInitializationToLValue(elementLV);
  445. // Move on to the next element.
  446. llvm::Value *nextElement =
  447. Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
  448. // Tell the EH cleanup that we finished with the last element.
  449. if (endOfInit) Builder.CreateStore(nextElement, endOfInit);
  450. // Leave the loop if we're done.
  451. llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
  452. "arrayinit.done");
  453. llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
  454. Builder.CreateCondBr(done, endBB, bodyBB);
  455. currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
  456. CGF.EmitBlock(endBB);
  457. }
  458. // Leave the partial-array cleanup if we entered one.
  459. if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator);
  460. }
  461. //===----------------------------------------------------------------------===//
  462. // Visitor Methods
  463. //===----------------------------------------------------------------------===//
  464. void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
  465. Visit(E->GetTemporaryExpr());
  466. }
  467. void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
  468. EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e));
  469. }
  470. void
  471. AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
  472. if (Dest.isPotentiallyAliased() &&
  473. E->getType().isPODType(CGF.getContext())) {
  474. // For a POD type, just emit a load of the lvalue + a copy, because our
  475. // compound literal might alias the destination.
  476. EmitAggLoadOfLValue(E);
  477. return;
  478. }
  479. AggValueSlot Slot = EnsureSlot(E->getType());
  480. CGF.EmitAggExpr(E->getInitializer(), Slot);
  481. }
  482. /// Attempt to look through various unimportant expressions to find a
  483. /// cast of the given kind.
  484. static Expr *findPeephole(Expr *op, CastKind kind) {
  485. while (true) {
  486. op = op->IgnoreParens();
  487. if (CastExpr *castE = dyn_cast<CastExpr>(op)) {
  488. if (castE->getCastKind() == kind)
  489. return castE->getSubExpr();
  490. if (castE->getCastKind() == CK_NoOp)
  491. continue;
  492. }
  493. return nullptr;
  494. }
  495. }
  496. void AggExprEmitter::VisitCastExpr(CastExpr *E) {
  497. switch (E->getCastKind()) {
  498. case CK_Dynamic: {
  499. // FIXME: Can this actually happen? We have no test coverage for it.
  500. assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
  501. LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
  502. CodeGenFunction::TCK_Load);
  503. // FIXME: Do we also need to handle property references here?
  504. if (LV.isSimple())
  505. CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
  506. else
  507. CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
  508. if (!Dest.isIgnored())
  509. CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
  510. break;
  511. }
  512. case CK_ToUnion: {
  513. // Evaluate even if the destination is ignored.
  514. if (Dest.isIgnored()) {
  515. CGF.EmitAnyExpr(E->getSubExpr(), AggValueSlot::ignored(),
  516. /*ignoreResult=*/true);
  517. break;
  518. }
  519. // GCC union extension
  520. QualType Ty = E->getSubExpr()->getType();
  521. QualType PtrTy = CGF.getContext().getPointerType(Ty);
  522. llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
  523. CGF.ConvertType(PtrTy));
  524. EmitInitializationToLValue(E->getSubExpr(),
  525. CGF.MakeAddrLValue(CastPtr, Ty));
  526. break;
  527. }
  528. case CK_DerivedToBase:
  529. case CK_BaseToDerived:
  530. case CK_UncheckedDerivedToBase: {
  531. llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
  532. "should have been unpacked before we got here");
  533. }
  534. case CK_NonAtomicToAtomic:
  535. case CK_AtomicToNonAtomic: {
  536. bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic);
  537. // Determine the atomic and value types.
  538. QualType atomicType = E->getSubExpr()->getType();
  539. QualType valueType = E->getType();
  540. if (isToAtomic) std::swap(atomicType, valueType);
  541. assert(atomicType->isAtomicType());
  542. assert(CGF.getContext().hasSameUnqualifiedType(valueType,
  543. atomicType->castAs<AtomicType>()->getValueType()));
  544. // Just recurse normally if we're ignoring the result or the
  545. // atomic type doesn't change representation.
  546. if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) {
  547. return Visit(E->getSubExpr());
  548. }
  549. CastKind peepholeTarget =
  550. (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic);
  551. // These two cases are reverses of each other; try to peephole them.
  552. if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) {
  553. assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
  554. E->getType()) &&
  555. "peephole significantly changed types?");
  556. return Visit(op);
  557. }
  558. // If we're converting an r-value of non-atomic type to an r-value
  559. // of atomic type, just emit directly into the relevant sub-object.
  560. if (isToAtomic) {
  561. AggValueSlot valueDest = Dest;
  562. if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) {
  563. // Zero-initialize. (Strictly speaking, we only need to intialize
  564. // the padding at the end, but this is simpler.)
  565. if (!Dest.isZeroed())
  566. CGF.EmitNullInitialization(Dest.getAddr(), atomicType);
  567. // Build a GEP to refer to the subobject.
  568. llvm::Value *valueAddr =
  569. CGF.Builder.CreateStructGEP(nullptr, valueDest.getAddr(), 0);
  570. valueDest = AggValueSlot::forAddr(valueAddr,
  571. valueDest.getAlignment(),
  572. valueDest.getQualifiers(),
  573. valueDest.isExternallyDestructed(),
  574. valueDest.requiresGCollection(),
  575. valueDest.isPotentiallyAliased(),
  576. AggValueSlot::IsZeroed);
  577. }
  578. CGF.EmitAggExpr(E->getSubExpr(), valueDest);
  579. return;
  580. }
  581. // Otherwise, we're converting an atomic type to a non-atomic type.
  582. // Make an atomic temporary, emit into that, and then copy the value out.
  583. AggValueSlot atomicSlot =
  584. CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp");
  585. CGF.EmitAggExpr(E->getSubExpr(), atomicSlot);
  586. llvm::Value *valueAddr =
  587. Builder.CreateStructGEP(nullptr, atomicSlot.getAddr(), 0);
  588. RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile());
  589. return EmitFinalDestCopy(valueType, rvalue);
  590. }
  591. case CK_LValueToRValue:
  592. // If we're loading from a volatile type, force the destination
  593. // into existence.
  594. if (E->getSubExpr()->getType().isVolatileQualified()) {
  595. EnsureDest(E->getType());
  596. return Visit(E->getSubExpr());
  597. }
  598. // fallthrough
  599. case CK_NoOp:
  600. case CK_UserDefinedConversion:
  601. case CK_ConstructorConversion:
  602. assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
  603. E->getType()) &&
  604. "Implicit cast types must be compatible");
  605. Visit(E->getSubExpr());
  606. break;
  607. // HLSL Change Begins.
  608. case CK_HLSLDerivedToBase:
  609. case CK_FlatConversion: {
  610. QualType Ty = E->getSubExpr()->getType();
  611. // We must emit the converted subexpression for any side-effects,
  612. // but the conversion itself doesn't have any, so we should not
  613. // emit it if we were not provided a destination aggregate value slot.
  614. if (IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E->getSubExpr())) {
  615. if (Dest.isIgnored()) return;
  616. llvm::Value *SrcVal = llvm::ConstantInt::get(CGF.getLLVMContext(), IL->getValue());
  617. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversion(
  618. CGF, SrcVal, Dest.getAddr(), E->getType(), Ty);
  619. } else if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E->getSubExpr())) {
  620. if (Dest.isIgnored()) return;
  621. llvm::Value *SrcVal = llvm::ConstantFP::get(CGF.getLLVMContext(), FL->getValue());
  622. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversion(
  623. CGF, SrcVal, Dest.getAddr(), E->getType(), Ty);
  624. } else {
  625. Expr *Src = E->getSubExpr();
  626. switch (CGF.getEvaluationKind(Ty)) {
  627. case TEK_Aggregate: {
  628. if (CastExpr *SrcCast = dyn_cast<CastExpr>(Src)) {
  629. if (SrcCast->getCastKind() == CK_LValueToRValue) {
  630. // Skip the lval to rval cast to reach decl.
  631. Src = SrcCast->getSubExpr();
  632. }
  633. }
  634. // Just use decl if possible to skip useless copy.
  635. LValue LV;
  636. if (DeclRefExpr *SrcDecl = dyn_cast<DeclRefExpr>(Src))
  637. LV = CGF.EmitLValue(SrcDecl);
  638. else
  639. LV = CGF.EmitAggExprToLValue(Src);
  640. if (Dest.isIgnored()) return;
  641. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionAggregateCopy(
  642. CGF, LV.getAddress(), Src->getType(), Dest.getAddr(), E->getType());
  643. } break;
  644. case TEK_Scalar: {
  645. llvm::Value *SrcVal = CGF.EmitScalarExpr(Src);
  646. if (Dest.isIgnored()) return;
  647. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversion(
  648. CGF, SrcVal, Dest.getAddr(), E->getType(), Ty);
  649. } break;
  650. default:
  651. assert(0 && "invalid type for flat cast");
  652. break;
  653. }
  654. }
  655. } break;
  656. // HLSL Change Ends.
  657. case CK_LValueBitCast:
  658. llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
  659. case CK_Dependent:
  660. case CK_BitCast:
  661. case CK_ArrayToPointerDecay:
  662. case CK_FunctionToPointerDecay:
  663. case CK_NullToPointer:
  664. case CK_NullToMemberPointer:
  665. case CK_BaseToDerivedMemberPointer:
  666. case CK_DerivedToBaseMemberPointer:
  667. case CK_MemberPointerToBoolean:
  668. case CK_ReinterpretMemberPointer:
  669. case CK_IntegralToPointer:
  670. case CK_PointerToIntegral:
  671. case CK_PointerToBoolean:
  672. case CK_ToVoid:
  673. case CK_VectorSplat:
  674. case CK_IntegralCast:
  675. case CK_IntegralToBoolean:
  676. case CK_IntegralToFloating:
  677. case CK_FloatingToIntegral:
  678. case CK_FloatingToBoolean:
  679. case CK_FloatingCast:
  680. case CK_CPointerToObjCPointerCast:
  681. case CK_BlockPointerToObjCPointerCast:
  682. case CK_AnyPointerToBlockPointerCast:
  683. case CK_ObjCObjectLValueCast:
  684. case CK_FloatingRealToComplex:
  685. case CK_FloatingComplexToReal:
  686. case CK_FloatingComplexToBoolean:
  687. case CK_FloatingComplexCast:
  688. case CK_FloatingComplexToIntegralComplex:
  689. case CK_IntegralRealToComplex:
  690. case CK_IntegralComplexToReal:
  691. case CK_IntegralComplexToBoolean:
  692. case CK_IntegralComplexCast:
  693. case CK_IntegralComplexToFloatingComplex:
  694. case CK_ARCProduceObject:
  695. case CK_ARCConsumeObject:
  696. case CK_ARCReclaimReturnedObject:
  697. case CK_ARCExtendBlockObject:
  698. case CK_CopyAndAutoreleaseBlockObject:
  699. case CK_BuiltinFnToFnPtr:
  700. case CK_ZeroToOCLEvent:
  701. case CK_AddressSpaceConversion:
  702. llvm_unreachable("cast kind invalid for aggregate types");
  703. }
  704. }
  705. void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
  706. if (E->getCallReturnType(CGF.getContext())->isReferenceType()) {
  707. EmitAggLoadOfLValue(E);
  708. return;
  709. }
  710. RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
  711. EmitMoveFromReturnSlot(E, RV);
  712. }
  713. void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
  714. RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
  715. EmitMoveFromReturnSlot(E, RV);
  716. }
  717. void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
  718. CGF.EmitIgnoredExpr(E->getLHS());
  719. Visit(E->getRHS());
  720. }
  721. void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
  722. CodeGenFunction::StmtExprEvaluation eval(CGF);
  723. CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
  724. }
  725. void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
  726. if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
  727. VisitPointerToDataMemberBinaryOperator(E);
  728. else
  729. CGF.ErrorUnsupported(E, "aggregate binary expression");
  730. }
  731. void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
  732. const BinaryOperator *E) {
  733. LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
  734. EmitFinalDestCopy(E->getType(), LV);
  735. }
  736. /// Is the value of the given expression possibly a reference to or
  737. /// into a __block variable?
  738. static bool isBlockVarRef(const Expr *E) {
  739. // Make sure we look through parens.
  740. E = E->IgnoreParens();
  741. // Check for a direct reference to a __block variable.
  742. if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
  743. const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
  744. return (var && var->hasAttr<BlocksAttr>());
  745. }
  746. // More complicated stuff.
  747. // Binary operators.
  748. if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
  749. // For an assignment or pointer-to-member operation, just care
  750. // about the LHS.
  751. if (op->isAssignmentOp() || op->isPtrMemOp())
  752. return isBlockVarRef(op->getLHS());
  753. // For a comma, just care about the RHS.
  754. if (op->getOpcode() == BO_Comma)
  755. return isBlockVarRef(op->getRHS());
  756. // FIXME: pointer arithmetic?
  757. return false;
  758. // Check both sides of a conditional operator.
  759. } else if (const AbstractConditionalOperator *op
  760. = dyn_cast<AbstractConditionalOperator>(E)) {
  761. return isBlockVarRef(op->getTrueExpr())
  762. || isBlockVarRef(op->getFalseExpr());
  763. // OVEs are required to support BinaryConditionalOperators.
  764. } else if (const OpaqueValueExpr *op
  765. = dyn_cast<OpaqueValueExpr>(E)) {
  766. if (const Expr *src = op->getSourceExpr())
  767. return isBlockVarRef(src);
  768. // Casts are necessary to get things like (*(int*)&var) = foo().
  769. // We don't really care about the kind of cast here, except
  770. // we don't want to look through l2r casts, because it's okay
  771. // to get the *value* in a __block variable.
  772. } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
  773. if (cast->getCastKind() == CK_LValueToRValue)
  774. return false;
  775. return isBlockVarRef(cast->getSubExpr());
  776. // Handle unary operators. Again, just aggressively look through
  777. // it, ignoring the operation.
  778. } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
  779. return isBlockVarRef(uop->getSubExpr());
  780. // Look into the base of a field access.
  781. } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
  782. return isBlockVarRef(mem->getBase());
  783. // Look into the base of a subscript.
  784. } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
  785. return isBlockVarRef(sub->getBase());
  786. }
  787. return false;
  788. }
  789. void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
  790. // For an assignment to work, the value on the right has
  791. // to be compatible with the value on the left.
  792. assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
  793. E->getRHS()->getType())
  794. && "Invalid assignment");
  795. // If the LHS might be a __block variable, and the RHS can
  796. // potentially cause a block copy, we need to evaluate the RHS first
  797. // so that the assignment goes the right place.
  798. // This is pretty semantically fragile.
  799. if (isBlockVarRef(E->getLHS()) &&
  800. E->getRHS()->HasSideEffects(CGF.getContext())) {
  801. // Ensure that we have a destination, and evaluate the RHS into that.
  802. EnsureDest(E->getRHS()->getType());
  803. Visit(E->getRHS());
  804. // Now emit the LHS and copy into it.
  805. LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
  806. // That copy is an atomic copy if the LHS is atomic.
  807. if (LHS.getType()->isAtomicType() ||
  808. CGF.LValueIsSuitableForInlineAtomic(LHS)) {
  809. CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
  810. return;
  811. }
  812. EmitCopy(E->getLHS()->getType(),
  813. AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
  814. needsGC(E->getLHS()->getType()),
  815. AggValueSlot::IsAliased),
  816. Dest);
  817. return;
  818. }
  819. LValue LHS = CGF.EmitLValue(E->getLHS());
  820. // If we have an atomic type, evaluate into the destination and then
  821. // do an atomic copy.
  822. if (LHS.getType()->isAtomicType() ||
  823. CGF.LValueIsSuitableForInlineAtomic(LHS)) {
  824. EnsureDest(E->getRHS()->getType());
  825. Visit(E->getRHS());
  826. CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
  827. return;
  828. }
  829. // Codegen the RHS so that it stores directly into the LHS.
  830. AggValueSlot LHSSlot =
  831. AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
  832. needsGC(E->getLHS()->getType()),
  833. AggValueSlot::IsAliased);
  834. // A non-volatile aggregate destination might have volatile member.
  835. if (!LHSSlot.isVolatile() &&
  836. CGF.hasVolatileMember(E->getLHS()->getType()))
  837. LHSSlot.setVolatile(true);
  838. CGF.EmitAggExpr(E->getRHS(), LHSSlot);
  839. // Copy into the destination if the assignment isn't ignored.
  840. EmitFinalDestCopy(E->getType(), LHS);
  841. }
  842. void AggExprEmitter::
  843. VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
  844. llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
  845. llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
  846. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
  847. // Bind the common expression if necessary.
  848. CodeGenFunction::OpaqueValueMapping binding(CGF, E);
  849. CodeGenFunction::ConditionalEvaluation eval(CGF);
  850. CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
  851. CGF.getProfileCount(E));
  852. // Save whether the destination's lifetime is externally managed.
  853. bool isExternallyDestructed = Dest.isExternallyDestructed();
  854. eval.begin(CGF);
  855. CGF.EmitBlock(LHSBlock);
  856. CGF.incrementProfileCounter(E);
  857. Visit(E->getTrueExpr());
  858. eval.end(CGF);
  859. assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
  860. CGF.Builder.CreateBr(ContBlock);
  861. // If the result of an agg expression is unused, then the emission
  862. // of the LHS might need to create a destination slot. That's fine
  863. // with us, and we can safely emit the RHS into the same slot, but
  864. // we shouldn't claim that it's already being destructed.
  865. Dest.setExternallyDestructed(isExternallyDestructed);
  866. eval.begin(CGF);
  867. CGF.EmitBlock(RHSBlock);
  868. Visit(E->getFalseExpr());
  869. eval.end(CGF);
  870. CGF.EmitBlock(ContBlock);
  871. }
  872. void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
  873. Visit(CE->getChosenSubExpr());
  874. }
  875. void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
  876. llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
  877. llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
  878. if (!ArgPtr) {
  879. // If EmitVAArg fails, we fall back to the LLVM instruction.
  880. llvm::Value *Val =
  881. Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
  882. if (!Dest.isIgnored())
  883. Builder.CreateStore(Val, Dest.getAddr());
  884. return;
  885. }
  886. EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
  887. }
  888. void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
  889. // Ensure that we have a slot, but if we already do, remember
  890. // whether it was externally destructed.
  891. bool wasExternallyDestructed = Dest.isExternallyDestructed();
  892. EnsureDest(E->getType());
  893. // We're going to push a destructor if there isn't already one.
  894. Dest.setExternallyDestructed();
  895. Visit(E->getSubExpr());
  896. // Push that destructor we promised.
  897. if (!wasExternallyDestructed)
  898. CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr());
  899. }
  900. void
  901. AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
  902. AggValueSlot Slot = EnsureSlot(E->getType());
  903. CGF.EmitCXXConstructExpr(E, Slot);
  904. }
  905. void
  906. AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
  907. AggValueSlot Slot = EnsureSlot(E->getType());
  908. CGF.EmitLambdaExpr(E, Slot);
  909. }
  910. void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
  911. CGF.enterFullExpression(E);
  912. CodeGenFunction::RunCleanupsScope cleanups(CGF);
  913. Visit(E->getSubExpr());
  914. }
  915. void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
  916. QualType T = E->getType();
  917. AggValueSlot Slot = EnsureSlot(T);
  918. EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
  919. }
  920. void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
  921. QualType T = E->getType();
  922. AggValueSlot Slot = EnsureSlot(T);
  923. EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
  924. }
  925. /// isSimpleZero - If emitting this value will obviously just cause a store of
  926. /// zero to memory, return true. This can return false if uncertain, so it just
  927. /// handles simple cases.
  928. static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
  929. E = E->IgnoreParens();
  930. // 0
  931. if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
  932. return IL->getValue() == 0;
  933. // +0.0
  934. if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
  935. return FL->getValue().isPosZero();
  936. // int()
  937. if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
  938. CGF.getTypes().isZeroInitializable(E->getType()))
  939. return true;
  940. // (int*)0 - Null pointer expressions.
  941. if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
  942. return ICE->getCastKind() == CK_NullToPointer;
  943. // '\0'
  944. if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
  945. return CL->getValue() == 0;
  946. // Otherwise, hard case: conservatively return false.
  947. return false;
  948. }
  949. void
  950. AggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) {
  951. QualType type = LV.getType();
  952. // FIXME: Ignore result?
  953. // FIXME: Are initializers affected by volatile?
  954. if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
  955. // Storing "i32 0" to a zero'd memory location is a noop.
  956. return;
  957. } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) {
  958. return EmitNullInitializationToLValue(LV);
  959. } else if (isa<NoInitExpr>(E)) {
  960. // Do nothing.
  961. return;
  962. } else if (type->isReferenceType()) {
  963. RValue RV = CGF.EmitReferenceBindingToExpr(E);
  964. return CGF.EmitStoreThroughLValue(RV, LV);
  965. }
  966. switch (CGF.getEvaluationKind(type)) {
  967. case TEK_Complex:
  968. CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
  969. return;
  970. case TEK_Aggregate:
  971. CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
  972. AggValueSlot::IsDestructed,
  973. AggValueSlot::DoesNotNeedGCBarriers,
  974. AggValueSlot::IsNotAliased,
  975. Dest.isZeroed()));
  976. return;
  977. case TEK_Scalar:
  978. // HLSL Change Begins.
  979. if (hlsl::IsHLSLMatType(LV.getType())) {
  980. llvm::Value *V = CGF.EmitScalarExpr(E);
  981. llvm::Value *Ptr = LV.getAddress();
  982. CGF.CGM.getHLSLRuntime().EmitHLSLMatrixStore(CGF, V, Ptr, LV.getType());
  983. } else
  984. // HLSL Change Ends.
  985. if (LV.isSimple()) {
  986. CGF.EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false);
  987. } else {
  988. CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
  989. }
  990. return;
  991. }
  992. llvm_unreachable("bad evaluation kind");
  993. }
  994. void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
  995. QualType type = lv.getType();
  996. // If the destination slot is already zeroed out before the aggregate is
  997. // copied into it, we don't have to emit any zeros here.
  998. if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
  999. return;
  1000. if (CGF.hasScalarEvaluationKind(type)) {
  1001. // For non-aggregates, we can store the appropriate null constant.
  1002. llvm::Value *null = CGF.CGM.EmitNullConstant(type);
  1003. // Note that the following is not equivalent to
  1004. // EmitStoreThroughBitfieldLValue for ARC types.
  1005. if (lv.isBitField()) {
  1006. CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
  1007. } else {
  1008. assert(lv.isSimple());
  1009. CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
  1010. }
  1011. } else {
  1012. // There's a potential optimization opportunity in combining
  1013. // memsets; that would be easy for arrays, but relatively
  1014. // difficult for structures with the current code.
  1015. CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
  1016. }
  1017. }
  1018. void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
  1019. #if 0
  1020. // FIXME: Assess perf here? Figure out what cases are worth optimizing here
  1021. // (Length of globals? Chunks of zeroed-out space?).
  1022. //
  1023. // If we can, prefer a copy from a global; this is a lot less code for long
  1024. // globals, and it's easier for the current optimizers to analyze.
  1025. if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
  1026. llvm::GlobalVariable* GV =
  1027. new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
  1028. llvm::GlobalValue::InternalLinkage, C, "");
  1029. EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
  1030. return;
  1031. }
  1032. #endif
  1033. if (E->hadArrayRangeDesignator())
  1034. CGF.ErrorUnsupported(E, "GNU array range designator extension");
  1035. AggValueSlot Dest = EnsureSlot(E->getType());
  1036. LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
  1037. Dest.getAlignment());
  1038. // Handle initialization of an array.
  1039. if (E->getType()->isArrayType()) {
  1040. if (E->isStringLiteralInit())
  1041. return Visit(E->getInit(0));
  1042. QualType elementType =
  1043. CGF.getContext().getAsArrayType(E->getType())->getElementType();
  1044. llvm::PointerType *APType =
  1045. cast<llvm::PointerType>(Dest.getAddr()->getType());
  1046. llvm::ArrayType *AType =
  1047. cast<llvm::ArrayType>(APType->getElementType());
  1048. EmitArrayInit(Dest.getAddr(), AType, elementType, E);
  1049. return;
  1050. }
  1051. if (E->getType()->isAtomicType()) {
  1052. // An _Atomic(T) object can be list-initialized from an expression
  1053. // of the same type.
  1054. assert(E->getNumInits() == 1 &&
  1055. CGF.getContext().hasSameUnqualifiedType(E->getInit(0)->getType(),
  1056. E->getType()) &&
  1057. "unexpected list initialization for atomic object");
  1058. return Visit(E->getInit(0));
  1059. }
  1060. assert(E->getType()->isRecordType() && "Only support structs/unions here!");
  1061. // Do struct initialization; this code just sets each individual member
  1062. // to the approprate value. This makes bitfield support automatic;
  1063. // the disadvantage is that the generated code is more difficult for
  1064. // the optimizer, especially with bitfields.
  1065. unsigned NumInitElements = E->getNumInits();
  1066. RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
  1067. // Prepare a 'this' for CXXDefaultInitExprs.
  1068. CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr());
  1069. if (record->isUnion()) {
  1070. // Only initialize one field of a union. The field itself is
  1071. // specified by the initializer list.
  1072. if (!E->getInitializedFieldInUnion()) {
  1073. // Empty union; we have nothing to do.
  1074. #ifndef NDEBUG
  1075. // Make sure that it's really an empty and not a failure of
  1076. // semantic analysis.
  1077. for (const auto *Field : record->fields())
  1078. assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
  1079. #endif
  1080. return;
  1081. }
  1082. // FIXME: volatility
  1083. FieldDecl *Field = E->getInitializedFieldInUnion();
  1084. LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
  1085. if (NumInitElements) {
  1086. // Store the initializer into the field
  1087. EmitInitializationToLValue(E->getInit(0), FieldLoc);
  1088. } else {
  1089. // Default-initialize to null.
  1090. EmitNullInitializationToLValue(FieldLoc);
  1091. }
  1092. return;
  1093. }
  1094. // HLSL Change Begins
  1095. if (CGF.getLangOpts().HLSL &&
  1096. !CGF.CGM.getHLSLRuntime().IsTrivalInitListExpr(CGF, E)) {
  1097. CGF.CGM.getHLSLRuntime().EmitHLSLInitListExpr(CGF, E, Dest.getAddr());
  1098. return;
  1099. }
  1100. // HLSL Change Ends
  1101. // We'll need to enter cleanup scopes in case any of the member
  1102. // initializers throw an exception.
  1103. SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
  1104. llvm::Instruction *cleanupDominator = nullptr;
  1105. // Here we iterate over the fields; this makes it simpler to both
  1106. // default-initialize fields and skip over unnamed fields.
  1107. unsigned curInitIndex = 0;
  1108. for (const auto *field : record->fields()) {
  1109. // We're done once we hit the flexible array member.
  1110. if (field->getType()->isIncompleteArrayType())
  1111. break;
  1112. // Always skip anonymous bitfields.
  1113. if (field->isUnnamedBitfield())
  1114. continue;
  1115. // We're done if we reach the end of the explicit initializers, we
  1116. // have a zeroed object, and the rest of the fields are
  1117. // zero-initializable.
  1118. if (curInitIndex == NumInitElements && Dest.isZeroed() &&
  1119. CGF.getTypes().isZeroInitializable(E->getType()))
  1120. break;
  1121. LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, field);
  1122. // We never generate write-barries for initialized fields.
  1123. LV.setNonGC(true);
  1124. if (curInitIndex < NumInitElements) {
  1125. // Store the initializer into the field.
  1126. EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
  1127. } else {
  1128. // We're out of initalizers; default-initialize to null
  1129. EmitNullInitializationToLValue(LV);
  1130. }
  1131. // Push a destructor if necessary.
  1132. // FIXME: if we have an array of structures, all explicitly
  1133. // initialized, we can end up pushing a linear number of cleanups.
  1134. bool pushedCleanup = false;
  1135. if (QualType::DestructionKind dtorKind
  1136. = field->getType().isDestructedType()) {
  1137. assert(LV.isSimple());
  1138. if (CGF.needsEHCleanup(dtorKind)) {
  1139. if (!cleanupDominator)
  1140. cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder
  1141. CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
  1142. CGF.getDestroyer(dtorKind), false);
  1143. cleanups.push_back(CGF.EHStack.stable_begin());
  1144. pushedCleanup = true;
  1145. }
  1146. }
  1147. // If the GEP didn't get used because of a dead zero init or something
  1148. // else, clean it up for -O0 builds and general tidiness.
  1149. if (!pushedCleanup && LV.isSimple())
  1150. if (llvm::GetElementPtrInst *GEP =
  1151. dyn_cast<llvm::GetElementPtrInst>(LV.getAddress()))
  1152. if (GEP->use_empty())
  1153. GEP->eraseFromParent();
  1154. }
  1155. // Deactivate all the partial cleanups in reverse order, which
  1156. // generally means popping them.
  1157. for (unsigned i = cleanups.size(); i != 0; --i)
  1158. CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
  1159. // Destroy the placeholder if we made one.
  1160. if (cleanupDominator)
  1161. cleanupDominator->eraseFromParent();
  1162. }
  1163. void AggExprEmitter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
  1164. AggValueSlot Dest = EnsureSlot(E->getType());
  1165. LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
  1166. Dest.getAlignment());
  1167. EmitInitializationToLValue(E->getBase(), DestLV);
  1168. VisitInitListExpr(E->getUpdater());
  1169. }
  1170. //===----------------------------------------------------------------------===//
  1171. // Entry Points into this File
  1172. //===----------------------------------------------------------------------===//
  1173. /// GetNumNonZeroBytesInInit - Get an approximate count of the number of
  1174. /// non-zero bytes that will be stored when outputting the initializer for the
  1175. /// specified initializer expression.
  1176. static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
  1177. E = E->IgnoreParens();
  1178. // 0 and 0.0 won't require any non-zero stores!
  1179. if (isSimpleZero(E, CGF)) return CharUnits::Zero();
  1180. // If this is an initlist expr, sum up the size of sizes of the (present)
  1181. // elements. If this is something weird, assume the whole thing is non-zero.
  1182. const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
  1183. if (!ILE || !CGF.getTypes().isZeroInitializable(ILE->getType()))
  1184. return CGF.getContext().getTypeSizeInChars(E->getType());
  1185. // InitListExprs for structs have to be handled carefully. If there are
  1186. // reference members, we need to consider the size of the reference, not the
  1187. // referencee. InitListExprs for unions and arrays can't have references.
  1188. if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
  1189. if (!RT->isUnionType()) {
  1190. RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
  1191. CharUnits NumNonZeroBytes = CharUnits::Zero();
  1192. unsigned ILEElement = 0;
  1193. for (const auto *Field : SD->fields()) {
  1194. // We're done once we hit the flexible array member or run out of
  1195. // InitListExpr elements.
  1196. if (Field->getType()->isIncompleteArrayType() ||
  1197. ILEElement == ILE->getNumInits())
  1198. break;
  1199. if (Field->isUnnamedBitfield())
  1200. continue;
  1201. const Expr *E = ILE->getInit(ILEElement++);
  1202. // Reference values are always non-null and have the width of a pointer.
  1203. if (Field->getType()->isReferenceType())
  1204. NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
  1205. CGF.getTarget().getPointerWidth(0));
  1206. else
  1207. NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
  1208. }
  1209. return NumNonZeroBytes;
  1210. }
  1211. }
  1212. CharUnits NumNonZeroBytes = CharUnits::Zero();
  1213. for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
  1214. NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
  1215. return NumNonZeroBytes;
  1216. }
  1217. /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
  1218. /// zeros in it, emit a memset and avoid storing the individual zeros.
  1219. ///
  1220. static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
  1221. CodeGenFunction &CGF) {
  1222. // If the slot is already known to be zeroed, nothing to do. Don't mess with
  1223. // volatile stores.
  1224. if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == nullptr)
  1225. return;
  1226. // C++ objects with a user-declared constructor don't need zero'ing.
  1227. if (CGF.getLangOpts().CPlusPlus)
  1228. if (const RecordType *RT = CGF.getContext()
  1229. .getBaseElementType(E->getType())->getAs<RecordType>()) {
  1230. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  1231. if (RD->hasUserDeclaredConstructor())
  1232. return;
  1233. }
  1234. // HLSL Change Begins
  1235. // Don't generate memset for hlsl.
  1236. if (CGF.getLangOpts().HLSL)
  1237. return;
  1238. // HLSL Change Ends
  1239. // If the type is 16-bytes or smaller, prefer individual stores over memset.
  1240. std::pair<CharUnits, CharUnits> TypeInfo =
  1241. CGF.getContext().getTypeInfoInChars(E->getType());
  1242. if (TypeInfo.first <= CharUnits::fromQuantity(16))
  1243. return;
  1244. // Check to see if over 3/4 of the initializer are known to be zero. If so,
  1245. // we prefer to emit memset + individual stores for the rest.
  1246. CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
  1247. if (NumNonZeroBytes*4 > TypeInfo.first)
  1248. return;
  1249. // Okay, it seems like a good idea to use an initial memset, emit the call.
  1250. llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
  1251. CharUnits Align = TypeInfo.second;
  1252. llvm::Value *Loc = Slot.getAddr();
  1253. Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy);
  1254. CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
  1255. Align.getQuantity(), false);
  1256. // Tell the AggExprEmitter that the slot is known zero.
  1257. Slot.setZeroed();
  1258. }
  1259. /// EmitAggExpr - Emit the computation of the specified expression of aggregate
  1260. /// type. The result is computed into DestPtr. Note that if DestPtr is null,
  1261. /// the value of the aggregate expression is not needed. If VolatileDest is
  1262. /// true, DestPtr cannot be 0.
  1263. void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
  1264. assert(E && hasAggregateEvaluationKind(E->getType()) &&
  1265. "Invalid aggregate expression to emit");
  1266. assert((Slot.getAddr() != nullptr || Slot.isIgnored()) &&
  1267. "slot has bits but no address");
  1268. // Optimize the slot if possible.
  1269. CheckAggExprForMemSetUse(Slot, E, *this);
  1270. AggExprEmitter(*this, Slot, Slot.isIgnored()).Visit(const_cast<Expr*>(E));
  1271. }
  1272. LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
  1273. assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
  1274. llvm::Value *Temp = CreateMemTemp(E->getType());
  1275. LValue LV = MakeAddrLValue(Temp, E->getType());
  1276. EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
  1277. AggValueSlot::DoesNotNeedGCBarriers,
  1278. AggValueSlot::IsNotAliased));
  1279. return LV;
  1280. }
  1281. void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
  1282. llvm::Value *SrcPtr, QualType Ty,
  1283. bool isVolatile,
  1284. CharUnits alignment,
  1285. bool isAssignment) {
  1286. assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
  1287. if (getLangOpts().CPlusPlus) {
  1288. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  1289. CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
  1290. assert((Record->hasTrivialCopyConstructor() ||
  1291. Record->hasTrivialCopyAssignment() ||
  1292. Record->hasTrivialMoveConstructor() ||
  1293. Record->hasTrivialMoveAssignment() ||
  1294. Record->isUnion()) &&
  1295. "Trying to aggregate-copy a type without a trivial copy/move "
  1296. "constructor or assignment operator");
  1297. // Ignore empty classes in C++.
  1298. if (Record->isEmpty())
  1299. return;
  1300. }
  1301. }
  1302. // HLSL Change Begins
  1303. if (getLangOpts().HLSL) {
  1304. // Don't generate memcpy for hlsl.
  1305. CGM.getHLSLRuntime().EmitHLSLAggregateCopy(*this, SrcPtr, DestPtr, Ty);
  1306. return;
  1307. }
  1308. // HLSL Change Ends
  1309. // Aggregate assignment turns into llvm.memcpy. This is almost valid per
  1310. // C99 6.5.16.1p3, which states "If the value being stored in an object is
  1311. // read from another object that overlaps in anyway the storage of the first
  1312. // object, then the overlap shall be exact and the two objects shall have
  1313. // qualified or unqualified versions of a compatible type."
  1314. //
  1315. // memcpy is not defined if the source and destination pointers are exactly
  1316. // equal, but other compilers do this optimization, and almost every memcpy
  1317. // implementation handles this case safely. If there is a libc that does not
  1318. // safely handle this, we can add a target hook.
  1319. // Get data size and alignment info for this aggregate. If this is an
  1320. // assignment don't copy the tail padding. Otherwise copying it is fine.
  1321. std::pair<CharUnits, CharUnits> TypeInfo;
  1322. if (isAssignment)
  1323. TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
  1324. else
  1325. TypeInfo = getContext().getTypeInfoInChars(Ty);
  1326. if (alignment.isZero())
  1327. alignment = TypeInfo.second;
  1328. llvm::Value *SizeVal = nullptr;
  1329. if (TypeInfo.first.isZero()) {
  1330. // But note that getTypeInfo returns 0 for a VLA.
  1331. if (auto *VAT = dyn_cast_or_null<VariableArrayType>(
  1332. getContext().getAsArrayType(Ty))) {
  1333. QualType BaseEltTy;
  1334. SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
  1335. TypeInfo = getContext().getTypeInfoDataSizeInChars(BaseEltTy);
  1336. std::pair<CharUnits, CharUnits> LastElementTypeInfo;
  1337. if (!isAssignment)
  1338. LastElementTypeInfo = getContext().getTypeInfoInChars(BaseEltTy);
  1339. assert(!TypeInfo.first.isZero());
  1340. SizeVal = Builder.CreateNUWMul(
  1341. SizeVal,
  1342. llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
  1343. if (!isAssignment) {
  1344. SizeVal = Builder.CreateNUWSub(
  1345. SizeVal,
  1346. llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
  1347. SizeVal = Builder.CreateNUWAdd(
  1348. SizeVal, llvm::ConstantInt::get(
  1349. SizeTy, LastElementTypeInfo.first.getQuantity()));
  1350. }
  1351. }
  1352. }
  1353. if (!SizeVal) {
  1354. SizeVal = llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity());
  1355. }
  1356. // FIXME: If we have a volatile struct, the optimizer can remove what might
  1357. // appear to be `extra' memory ops:
  1358. //
  1359. // volatile struct { int i; } a, b;
  1360. //
  1361. // int main() {
  1362. // a = b;
  1363. // a = b;
  1364. // }
  1365. //
  1366. // we need to use a different call here. We use isVolatile to indicate when
  1367. // either the source or the destination is volatile.
  1368. llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
  1369. llvm::Type *DBP =
  1370. llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
  1371. DestPtr = Builder.CreateBitCast(DestPtr, DBP);
  1372. llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
  1373. llvm::Type *SBP =
  1374. llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
  1375. SrcPtr = Builder.CreateBitCast(SrcPtr, SBP);
  1376. // Don't do any of the memmove_collectable tests if GC isn't set.
  1377. if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
  1378. // fall through
  1379. } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
  1380. RecordDecl *Record = RecordTy->getDecl();
  1381. if (Record->hasObjectMember()) {
  1382. CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
  1383. SizeVal);
  1384. return;
  1385. }
  1386. } else if (Ty->isArrayType()) {
  1387. QualType BaseType = getContext().getBaseElementType(Ty);
  1388. if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
  1389. if (RecordTy->getDecl()->hasObjectMember()) {
  1390. CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
  1391. SizeVal);
  1392. return;
  1393. }
  1394. }
  1395. }
  1396. // Determine the metadata to describe the position of any padding in this
  1397. // memcpy, as well as the TBAA tags for the members of the struct, in case
  1398. // the optimizer wishes to expand it in to scalar memory operations.
  1399. llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty);
  1400. Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, alignment.getQuantity(),
  1401. isVolatile, /*TBAATag=*/nullptr, TBAAStructTag);
  1402. }