CGExprAgg.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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_FlatConversion: {
  609. QualType Ty = E->getSubExpr()->getType();
  610. QualType PtrTy = CGF.getContext().getPointerType(Ty);
  611. llvm::Value *DestPtr = Dest.getAddr();
  612. if (IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E->getSubExpr())) {
  613. llvm::Value *SrcVal = llvm::ConstantInt::get(CGF.getLLVMContext(), IL->getValue());
  614. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionToAggregate(
  615. CGF, SrcVal, DestPtr, E->getType(), Ty);
  616. } else if (FloatingLiteral *FL =
  617. dyn_cast<FloatingLiteral>(E->getSubExpr())) {
  618. llvm::Value *SrcVal = llvm::ConstantFP::get(CGF.getLLVMContext(), FL->getValue());
  619. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionToAggregate(
  620. CGF, SrcVal, DestPtr, E->getType(), Ty);
  621. } else {
  622. Expr *Src = E->getSubExpr();
  623. switch (CGF.getEvaluationKind(Ty)) {
  624. case TEK_Aggregate: {
  625. if (CastExpr *SrcCast = dyn_cast<CastExpr>(Src)) {
  626. if (SrcCast->getCastKind() == CK_LValueToRValue) {
  627. // Skip the lval to rval cast to reach decl.
  628. Src = SrcCast->getSubExpr();
  629. }
  630. }
  631. // Just use decl if possible to skip useless copy.
  632. if (DeclRefExpr *SrcDecl = dyn_cast<DeclRefExpr>(Src)) {
  633. LValue LV = CGF.EmitLValue(SrcDecl);
  634. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionAggregateCopy(
  635. CGF, LV.getAddress(), Src->getType(), DestPtr, E->getType());
  636. } else {
  637. LValue LV = CGF.EmitAggExprToLValue(Src);
  638. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionAggregateCopy(
  639. CGF, LV.getAddress(), Src->getType(), DestPtr, E->getType());
  640. }
  641. } break;
  642. case TEK_Scalar: {
  643. llvm::Value *SrcVal = CGF.EmitScalarExpr(Src);
  644. CGF.CGM.getHLSLRuntime().EmitHLSLFlatConversionToAggregate(
  645. CGF, SrcVal, DestPtr, E->getType(), Ty);
  646. } break;
  647. default:
  648. assert(0 && "invalid type for flat cast");
  649. break;
  650. }
  651. }
  652. } break;
  653. // HLSL Change Ends.
  654. case CK_LValueBitCast:
  655. llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
  656. case CK_Dependent:
  657. case CK_BitCast:
  658. case CK_ArrayToPointerDecay:
  659. case CK_FunctionToPointerDecay:
  660. case CK_NullToPointer:
  661. case CK_NullToMemberPointer:
  662. case CK_BaseToDerivedMemberPointer:
  663. case CK_DerivedToBaseMemberPointer:
  664. case CK_MemberPointerToBoolean:
  665. case CK_ReinterpretMemberPointer:
  666. case CK_IntegralToPointer:
  667. case CK_PointerToIntegral:
  668. case CK_PointerToBoolean:
  669. case CK_ToVoid:
  670. case CK_VectorSplat:
  671. case CK_IntegralCast:
  672. case CK_IntegralToBoolean:
  673. case CK_IntegralToFloating:
  674. case CK_FloatingToIntegral:
  675. case CK_FloatingToBoolean:
  676. case CK_FloatingCast:
  677. case CK_CPointerToObjCPointerCast:
  678. case CK_BlockPointerToObjCPointerCast:
  679. case CK_AnyPointerToBlockPointerCast:
  680. case CK_ObjCObjectLValueCast:
  681. case CK_FloatingRealToComplex:
  682. case CK_FloatingComplexToReal:
  683. case CK_FloatingComplexToBoolean:
  684. case CK_FloatingComplexCast:
  685. case CK_FloatingComplexToIntegralComplex:
  686. case CK_IntegralRealToComplex:
  687. case CK_IntegralComplexToReal:
  688. case CK_IntegralComplexToBoolean:
  689. case CK_IntegralComplexCast:
  690. case CK_IntegralComplexToFloatingComplex:
  691. case CK_ARCProduceObject:
  692. case CK_ARCConsumeObject:
  693. case CK_ARCReclaimReturnedObject:
  694. case CK_ARCExtendBlockObject:
  695. case CK_CopyAndAutoreleaseBlockObject:
  696. case CK_BuiltinFnToFnPtr:
  697. case CK_ZeroToOCLEvent:
  698. case CK_AddressSpaceConversion:
  699. llvm_unreachable("cast kind invalid for aggregate types");
  700. }
  701. }
  702. void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
  703. if (E->getCallReturnType(CGF.getContext())->isReferenceType()) {
  704. EmitAggLoadOfLValue(E);
  705. return;
  706. }
  707. RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
  708. EmitMoveFromReturnSlot(E, RV);
  709. }
  710. void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
  711. RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
  712. EmitMoveFromReturnSlot(E, RV);
  713. }
  714. void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
  715. CGF.EmitIgnoredExpr(E->getLHS());
  716. Visit(E->getRHS());
  717. }
  718. void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
  719. CodeGenFunction::StmtExprEvaluation eval(CGF);
  720. CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
  721. }
  722. void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
  723. if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
  724. VisitPointerToDataMemberBinaryOperator(E);
  725. else
  726. CGF.ErrorUnsupported(E, "aggregate binary expression");
  727. }
  728. void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
  729. const BinaryOperator *E) {
  730. LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
  731. EmitFinalDestCopy(E->getType(), LV);
  732. }
  733. /// Is the value of the given expression possibly a reference to or
  734. /// into a __block variable?
  735. static bool isBlockVarRef(const Expr *E) {
  736. // Make sure we look through parens.
  737. E = E->IgnoreParens();
  738. // Check for a direct reference to a __block variable.
  739. if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
  740. const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
  741. return (var && var->hasAttr<BlocksAttr>());
  742. }
  743. // More complicated stuff.
  744. // Binary operators.
  745. if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
  746. // For an assignment or pointer-to-member operation, just care
  747. // about the LHS.
  748. if (op->isAssignmentOp() || op->isPtrMemOp())
  749. return isBlockVarRef(op->getLHS());
  750. // For a comma, just care about the RHS.
  751. if (op->getOpcode() == BO_Comma)
  752. return isBlockVarRef(op->getRHS());
  753. // FIXME: pointer arithmetic?
  754. return false;
  755. // Check both sides of a conditional operator.
  756. } else if (const AbstractConditionalOperator *op
  757. = dyn_cast<AbstractConditionalOperator>(E)) {
  758. return isBlockVarRef(op->getTrueExpr())
  759. || isBlockVarRef(op->getFalseExpr());
  760. // OVEs are required to support BinaryConditionalOperators.
  761. } else if (const OpaqueValueExpr *op
  762. = dyn_cast<OpaqueValueExpr>(E)) {
  763. if (const Expr *src = op->getSourceExpr())
  764. return isBlockVarRef(src);
  765. // Casts are necessary to get things like (*(int*)&var) = foo().
  766. // We don't really care about the kind of cast here, except
  767. // we don't want to look through l2r casts, because it's okay
  768. // to get the *value* in a __block variable.
  769. } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
  770. if (cast->getCastKind() == CK_LValueToRValue)
  771. return false;
  772. return isBlockVarRef(cast->getSubExpr());
  773. // Handle unary operators. Again, just aggressively look through
  774. // it, ignoring the operation.
  775. } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
  776. return isBlockVarRef(uop->getSubExpr());
  777. // Look into the base of a field access.
  778. } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
  779. return isBlockVarRef(mem->getBase());
  780. // Look into the base of a subscript.
  781. } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
  782. return isBlockVarRef(sub->getBase());
  783. }
  784. return false;
  785. }
  786. void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
  787. // For an assignment to work, the value on the right has
  788. // to be compatible with the value on the left.
  789. assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
  790. E->getRHS()->getType())
  791. && "Invalid assignment");
  792. // If the LHS might be a __block variable, and the RHS can
  793. // potentially cause a block copy, we need to evaluate the RHS first
  794. // so that the assignment goes the right place.
  795. // This is pretty semantically fragile.
  796. if (isBlockVarRef(E->getLHS()) &&
  797. E->getRHS()->HasSideEffects(CGF.getContext())) {
  798. // Ensure that we have a destination, and evaluate the RHS into that.
  799. EnsureDest(E->getRHS()->getType());
  800. Visit(E->getRHS());
  801. // Now emit the LHS and copy into it.
  802. LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
  803. // That copy is an atomic copy if the LHS is atomic.
  804. if (LHS.getType()->isAtomicType() ||
  805. CGF.LValueIsSuitableForInlineAtomic(LHS)) {
  806. CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
  807. return;
  808. }
  809. EmitCopy(E->getLHS()->getType(),
  810. AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
  811. needsGC(E->getLHS()->getType()),
  812. AggValueSlot::IsAliased),
  813. Dest);
  814. return;
  815. }
  816. LValue LHS = CGF.EmitLValue(E->getLHS());
  817. // If we have an atomic type, evaluate into the destination and then
  818. // do an atomic copy.
  819. if (LHS.getType()->isAtomicType() ||
  820. CGF.LValueIsSuitableForInlineAtomic(LHS)) {
  821. EnsureDest(E->getRHS()->getType());
  822. Visit(E->getRHS());
  823. CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
  824. return;
  825. }
  826. // Codegen the RHS so that it stores directly into the LHS.
  827. AggValueSlot LHSSlot =
  828. AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
  829. needsGC(E->getLHS()->getType()),
  830. AggValueSlot::IsAliased);
  831. // A non-volatile aggregate destination might have volatile member.
  832. if (!LHSSlot.isVolatile() &&
  833. CGF.hasVolatileMember(E->getLHS()->getType()))
  834. LHSSlot.setVolatile(true);
  835. CGF.EmitAggExpr(E->getRHS(), LHSSlot);
  836. // Copy into the destination if the assignment isn't ignored.
  837. EmitFinalDestCopy(E->getType(), LHS);
  838. }
  839. void AggExprEmitter::
  840. VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
  841. llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
  842. llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
  843. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
  844. // Bind the common expression if necessary.
  845. CodeGenFunction::OpaqueValueMapping binding(CGF, E);
  846. CodeGenFunction::ConditionalEvaluation eval(CGF);
  847. CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
  848. CGF.getProfileCount(E));
  849. // Save whether the destination's lifetime is externally managed.
  850. bool isExternallyDestructed = Dest.isExternallyDestructed();
  851. eval.begin(CGF);
  852. CGF.EmitBlock(LHSBlock);
  853. CGF.incrementProfileCounter(E);
  854. Visit(E->getTrueExpr());
  855. eval.end(CGF);
  856. assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
  857. CGF.Builder.CreateBr(ContBlock);
  858. // If the result of an agg expression is unused, then the emission
  859. // of the LHS might need to create a destination slot. That's fine
  860. // with us, and we can safely emit the RHS into the same slot, but
  861. // we shouldn't claim that it's already being destructed.
  862. Dest.setExternallyDestructed(isExternallyDestructed);
  863. eval.begin(CGF);
  864. CGF.EmitBlock(RHSBlock);
  865. Visit(E->getFalseExpr());
  866. eval.end(CGF);
  867. CGF.EmitBlock(ContBlock);
  868. }
  869. void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
  870. Visit(CE->getChosenSubExpr());
  871. }
  872. void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
  873. llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
  874. llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
  875. if (!ArgPtr) {
  876. // If EmitVAArg fails, we fall back to the LLVM instruction.
  877. llvm::Value *Val =
  878. Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
  879. if (!Dest.isIgnored())
  880. Builder.CreateStore(Val, Dest.getAddr());
  881. return;
  882. }
  883. EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
  884. }
  885. void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
  886. // Ensure that we have a slot, but if we already do, remember
  887. // whether it was externally destructed.
  888. bool wasExternallyDestructed = Dest.isExternallyDestructed();
  889. EnsureDest(E->getType());
  890. // We're going to push a destructor if there isn't already one.
  891. Dest.setExternallyDestructed();
  892. Visit(E->getSubExpr());
  893. // Push that destructor we promised.
  894. if (!wasExternallyDestructed)
  895. CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr());
  896. }
  897. void
  898. AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
  899. AggValueSlot Slot = EnsureSlot(E->getType());
  900. CGF.EmitCXXConstructExpr(E, Slot);
  901. }
  902. void
  903. AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
  904. AggValueSlot Slot = EnsureSlot(E->getType());
  905. CGF.EmitLambdaExpr(E, Slot);
  906. }
  907. void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
  908. CGF.enterFullExpression(E);
  909. CodeGenFunction::RunCleanupsScope cleanups(CGF);
  910. Visit(E->getSubExpr());
  911. }
  912. void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
  913. QualType T = E->getType();
  914. AggValueSlot Slot = EnsureSlot(T);
  915. EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
  916. }
  917. void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
  918. QualType T = E->getType();
  919. AggValueSlot Slot = EnsureSlot(T);
  920. EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
  921. }
  922. /// isSimpleZero - If emitting this value will obviously just cause a store of
  923. /// zero to memory, return true. This can return false if uncertain, so it just
  924. /// handles simple cases.
  925. static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
  926. E = E->IgnoreParens();
  927. // 0
  928. if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
  929. return IL->getValue() == 0;
  930. // +0.0
  931. if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
  932. return FL->getValue().isPosZero();
  933. // int()
  934. if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
  935. CGF.getTypes().isZeroInitializable(E->getType()))
  936. return true;
  937. // (int*)0 - Null pointer expressions.
  938. if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
  939. return ICE->getCastKind() == CK_NullToPointer;
  940. // '\0'
  941. if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
  942. return CL->getValue() == 0;
  943. // Otherwise, hard case: conservatively return false.
  944. return false;
  945. }
  946. void
  947. AggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) {
  948. QualType type = LV.getType();
  949. // FIXME: Ignore result?
  950. // FIXME: Are initializers affected by volatile?
  951. if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
  952. // Storing "i32 0" to a zero'd memory location is a noop.
  953. return;
  954. } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) {
  955. return EmitNullInitializationToLValue(LV);
  956. } else if (isa<NoInitExpr>(E)) {
  957. // Do nothing.
  958. return;
  959. } else if (type->isReferenceType()) {
  960. RValue RV = CGF.EmitReferenceBindingToExpr(E);
  961. return CGF.EmitStoreThroughLValue(RV, LV);
  962. }
  963. switch (CGF.getEvaluationKind(type)) {
  964. case TEK_Complex:
  965. CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
  966. return;
  967. case TEK_Aggregate:
  968. CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
  969. AggValueSlot::IsDestructed,
  970. AggValueSlot::DoesNotNeedGCBarriers,
  971. AggValueSlot::IsNotAliased,
  972. Dest.isZeroed()));
  973. return;
  974. case TEK_Scalar:
  975. // HLSL Change Begins.
  976. if (hlsl::IsHLSLMatType(LV.getType())) {
  977. llvm::Value *V = CGF.EmitScalarExpr(E);
  978. llvm::Value *Ptr = LV.getAddress();
  979. CGF.CGM.getHLSLRuntime().EmitHLSLMatrixStore(CGF, V, Ptr, LV.getType());
  980. } else
  981. // HLSL Change Ends.
  982. if (LV.isSimple()) {
  983. CGF.EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false);
  984. } else {
  985. CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
  986. }
  987. return;
  988. }
  989. llvm_unreachable("bad evaluation kind");
  990. }
  991. void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
  992. QualType type = lv.getType();
  993. // If the destination slot is already zeroed out before the aggregate is
  994. // copied into it, we don't have to emit any zeros here.
  995. if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
  996. return;
  997. if (CGF.hasScalarEvaluationKind(type)) {
  998. // For non-aggregates, we can store the appropriate null constant.
  999. llvm::Value *null = CGF.CGM.EmitNullConstant(type);
  1000. // Note that the following is not equivalent to
  1001. // EmitStoreThroughBitfieldLValue for ARC types.
  1002. if (lv.isBitField()) {
  1003. CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
  1004. } else {
  1005. assert(lv.isSimple());
  1006. CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
  1007. }
  1008. } else {
  1009. // There's a potential optimization opportunity in combining
  1010. // memsets; that would be easy for arrays, but relatively
  1011. // difficult for structures with the current code.
  1012. CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
  1013. }
  1014. }
  1015. void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
  1016. #if 0
  1017. // FIXME: Assess perf here? Figure out what cases are worth optimizing here
  1018. // (Length of globals? Chunks of zeroed-out space?).
  1019. //
  1020. // If we can, prefer a copy from a global; this is a lot less code for long
  1021. // globals, and it's easier for the current optimizers to analyze.
  1022. if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
  1023. llvm::GlobalVariable* GV =
  1024. new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
  1025. llvm::GlobalValue::InternalLinkage, C, "");
  1026. EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
  1027. return;
  1028. }
  1029. #endif
  1030. if (E->hadArrayRangeDesignator())
  1031. CGF.ErrorUnsupported(E, "GNU array range designator extension");
  1032. AggValueSlot Dest = EnsureSlot(E->getType());
  1033. LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
  1034. Dest.getAlignment());
  1035. // Handle initialization of an array.
  1036. if (E->getType()->isArrayType()) {
  1037. if (E->isStringLiteralInit())
  1038. return Visit(E->getInit(0));
  1039. QualType elementType =
  1040. CGF.getContext().getAsArrayType(E->getType())->getElementType();
  1041. llvm::PointerType *APType =
  1042. cast<llvm::PointerType>(Dest.getAddr()->getType());
  1043. llvm::ArrayType *AType =
  1044. cast<llvm::ArrayType>(APType->getElementType());
  1045. EmitArrayInit(Dest.getAddr(), AType, elementType, E);
  1046. return;
  1047. }
  1048. if (E->getType()->isAtomicType()) {
  1049. // An _Atomic(T) object can be list-initialized from an expression
  1050. // of the same type.
  1051. assert(E->getNumInits() == 1 &&
  1052. CGF.getContext().hasSameUnqualifiedType(E->getInit(0)->getType(),
  1053. E->getType()) &&
  1054. "unexpected list initialization for atomic object");
  1055. return Visit(E->getInit(0));
  1056. }
  1057. assert(E->getType()->isRecordType() && "Only support structs/unions here!");
  1058. // Do struct initialization; this code just sets each individual member
  1059. // to the approprate value. This makes bitfield support automatic;
  1060. // the disadvantage is that the generated code is more difficult for
  1061. // the optimizer, especially with bitfields.
  1062. unsigned NumInitElements = E->getNumInits();
  1063. RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
  1064. // Prepare a 'this' for CXXDefaultInitExprs.
  1065. CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr());
  1066. if (record->isUnion()) {
  1067. // Only initialize one field of a union. The field itself is
  1068. // specified by the initializer list.
  1069. if (!E->getInitializedFieldInUnion()) {
  1070. // Empty union; we have nothing to do.
  1071. #ifndef NDEBUG
  1072. // Make sure that it's really an empty and not a failure of
  1073. // semantic analysis.
  1074. for (const auto *Field : record->fields())
  1075. assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
  1076. #endif
  1077. return;
  1078. }
  1079. // FIXME: volatility
  1080. FieldDecl *Field = E->getInitializedFieldInUnion();
  1081. LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
  1082. if (NumInitElements) {
  1083. // Store the initializer into the field
  1084. EmitInitializationToLValue(E->getInit(0), FieldLoc);
  1085. } else {
  1086. // Default-initialize to null.
  1087. EmitNullInitializationToLValue(FieldLoc);
  1088. }
  1089. return;
  1090. }
  1091. // HLSL Change Begins
  1092. if (CGF.getLangOpts().HLSL &&
  1093. !CGF.CGM.getHLSLRuntime().IsTrivalInitListExpr(CGF, E)) {
  1094. CGF.CGM.getHLSLRuntime().EmitHLSLInitListExpr(CGF, E, Dest.getAddr());
  1095. return;
  1096. }
  1097. // HLSL Change Ends
  1098. // We'll need to enter cleanup scopes in case any of the member
  1099. // initializers throw an exception.
  1100. SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
  1101. llvm::Instruction *cleanupDominator = nullptr;
  1102. // Here we iterate over the fields; this makes it simpler to both
  1103. // default-initialize fields and skip over unnamed fields.
  1104. unsigned curInitIndex = 0;
  1105. for (const auto *field : record->fields()) {
  1106. // We're done once we hit the flexible array member.
  1107. if (field->getType()->isIncompleteArrayType())
  1108. break;
  1109. // Always skip anonymous bitfields.
  1110. if (field->isUnnamedBitfield())
  1111. continue;
  1112. // We're done if we reach the end of the explicit initializers, we
  1113. // have a zeroed object, and the rest of the fields are
  1114. // zero-initializable.
  1115. if (curInitIndex == NumInitElements && Dest.isZeroed() &&
  1116. CGF.getTypes().isZeroInitializable(E->getType()))
  1117. break;
  1118. LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, field);
  1119. // We never generate write-barries for initialized fields.
  1120. LV.setNonGC(true);
  1121. if (curInitIndex < NumInitElements) {
  1122. // Store the initializer into the field.
  1123. EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
  1124. } else {
  1125. // We're out of initalizers; default-initialize to null
  1126. EmitNullInitializationToLValue(LV);
  1127. }
  1128. // Push a destructor if necessary.
  1129. // FIXME: if we have an array of structures, all explicitly
  1130. // initialized, we can end up pushing a linear number of cleanups.
  1131. bool pushedCleanup = false;
  1132. if (QualType::DestructionKind dtorKind
  1133. = field->getType().isDestructedType()) {
  1134. assert(LV.isSimple());
  1135. if (CGF.needsEHCleanup(dtorKind)) {
  1136. if (!cleanupDominator)
  1137. cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder
  1138. CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
  1139. CGF.getDestroyer(dtorKind), false);
  1140. cleanups.push_back(CGF.EHStack.stable_begin());
  1141. pushedCleanup = true;
  1142. }
  1143. }
  1144. // If the GEP didn't get used because of a dead zero init or something
  1145. // else, clean it up for -O0 builds and general tidiness.
  1146. if (!pushedCleanup && LV.isSimple())
  1147. if (llvm::GetElementPtrInst *GEP =
  1148. dyn_cast<llvm::GetElementPtrInst>(LV.getAddress()))
  1149. if (GEP->use_empty())
  1150. GEP->eraseFromParent();
  1151. }
  1152. // Deactivate all the partial cleanups in reverse order, which
  1153. // generally means popping them.
  1154. for (unsigned i = cleanups.size(); i != 0; --i)
  1155. CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
  1156. // Destroy the placeholder if we made one.
  1157. if (cleanupDominator)
  1158. cleanupDominator->eraseFromParent();
  1159. }
  1160. void AggExprEmitter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
  1161. AggValueSlot Dest = EnsureSlot(E->getType());
  1162. LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
  1163. Dest.getAlignment());
  1164. EmitInitializationToLValue(E->getBase(), DestLV);
  1165. VisitInitListExpr(E->getUpdater());
  1166. }
  1167. //===----------------------------------------------------------------------===//
  1168. // Entry Points into this File
  1169. //===----------------------------------------------------------------------===//
  1170. /// GetNumNonZeroBytesInInit - Get an approximate count of the number of
  1171. /// non-zero bytes that will be stored when outputting the initializer for the
  1172. /// specified initializer expression.
  1173. static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
  1174. E = E->IgnoreParens();
  1175. // 0 and 0.0 won't require any non-zero stores!
  1176. if (isSimpleZero(E, CGF)) return CharUnits::Zero();
  1177. // If this is an initlist expr, sum up the size of sizes of the (present)
  1178. // elements. If this is something weird, assume the whole thing is non-zero.
  1179. const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
  1180. if (!ILE || !CGF.getTypes().isZeroInitializable(ILE->getType()))
  1181. return CGF.getContext().getTypeSizeInChars(E->getType());
  1182. // InitListExprs for structs have to be handled carefully. If there are
  1183. // reference members, we need to consider the size of the reference, not the
  1184. // referencee. InitListExprs for unions and arrays can't have references.
  1185. if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
  1186. if (!RT->isUnionType()) {
  1187. RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
  1188. CharUnits NumNonZeroBytes = CharUnits::Zero();
  1189. unsigned ILEElement = 0;
  1190. for (const auto *Field : SD->fields()) {
  1191. // We're done once we hit the flexible array member or run out of
  1192. // InitListExpr elements.
  1193. if (Field->getType()->isIncompleteArrayType() ||
  1194. ILEElement == ILE->getNumInits())
  1195. break;
  1196. if (Field->isUnnamedBitfield())
  1197. continue;
  1198. const Expr *E = ILE->getInit(ILEElement++);
  1199. // Reference values are always non-null and have the width of a pointer.
  1200. if (Field->getType()->isReferenceType())
  1201. NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
  1202. CGF.getTarget().getPointerWidth(0));
  1203. else
  1204. NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
  1205. }
  1206. return NumNonZeroBytes;
  1207. }
  1208. }
  1209. CharUnits NumNonZeroBytes = CharUnits::Zero();
  1210. for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
  1211. NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
  1212. return NumNonZeroBytes;
  1213. }
  1214. /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
  1215. /// zeros in it, emit a memset and avoid storing the individual zeros.
  1216. ///
  1217. static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
  1218. CodeGenFunction &CGF) {
  1219. // If the slot is already known to be zeroed, nothing to do. Don't mess with
  1220. // volatile stores.
  1221. if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == nullptr)
  1222. return;
  1223. // C++ objects with a user-declared constructor don't need zero'ing.
  1224. if (CGF.getLangOpts().CPlusPlus)
  1225. if (const RecordType *RT = CGF.getContext()
  1226. .getBaseElementType(E->getType())->getAs<RecordType>()) {
  1227. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  1228. if (RD->hasUserDeclaredConstructor())
  1229. return;
  1230. }
  1231. // HLSL Change Begins
  1232. // Don't generate memset for hlsl.
  1233. if (CGF.getLangOpts().HLSL)
  1234. return;
  1235. // HLSL Change Ends
  1236. // If the type is 16-bytes or smaller, prefer individual stores over memset.
  1237. std::pair<CharUnits, CharUnits> TypeInfo =
  1238. CGF.getContext().getTypeInfoInChars(E->getType());
  1239. if (TypeInfo.first <= CharUnits::fromQuantity(16))
  1240. return;
  1241. // Check to see if over 3/4 of the initializer are known to be zero. If so,
  1242. // we prefer to emit memset + individual stores for the rest.
  1243. CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
  1244. if (NumNonZeroBytes*4 > TypeInfo.first)
  1245. return;
  1246. // Okay, it seems like a good idea to use an initial memset, emit the call.
  1247. llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
  1248. CharUnits Align = TypeInfo.second;
  1249. llvm::Value *Loc = Slot.getAddr();
  1250. Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy);
  1251. CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
  1252. Align.getQuantity(), false);
  1253. // Tell the AggExprEmitter that the slot is known zero.
  1254. Slot.setZeroed();
  1255. }
  1256. /// EmitAggExpr - Emit the computation of the specified expression of aggregate
  1257. /// type. The result is computed into DestPtr. Note that if DestPtr is null,
  1258. /// the value of the aggregate expression is not needed. If VolatileDest is
  1259. /// true, DestPtr cannot be 0.
  1260. void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
  1261. assert(E && hasAggregateEvaluationKind(E->getType()) &&
  1262. "Invalid aggregate expression to emit");
  1263. assert((Slot.getAddr() != nullptr || Slot.isIgnored()) &&
  1264. "slot has bits but no address");
  1265. // Optimize the slot if possible.
  1266. CheckAggExprForMemSetUse(Slot, E, *this);
  1267. AggExprEmitter(*this, Slot, Slot.isIgnored()).Visit(const_cast<Expr*>(E));
  1268. }
  1269. LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
  1270. assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
  1271. llvm::Value *Temp = CreateMemTemp(E->getType());
  1272. LValue LV = MakeAddrLValue(Temp, E->getType());
  1273. EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
  1274. AggValueSlot::DoesNotNeedGCBarriers,
  1275. AggValueSlot::IsNotAliased));
  1276. return LV;
  1277. }
  1278. void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
  1279. llvm::Value *SrcPtr, QualType Ty,
  1280. bool isVolatile,
  1281. CharUnits alignment,
  1282. bool isAssignment) {
  1283. assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
  1284. if (getLangOpts().CPlusPlus) {
  1285. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  1286. CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
  1287. assert((Record->hasTrivialCopyConstructor() ||
  1288. Record->hasTrivialCopyAssignment() ||
  1289. Record->hasTrivialMoveConstructor() ||
  1290. Record->hasTrivialMoveAssignment() ||
  1291. Record->isUnion()) &&
  1292. "Trying to aggregate-copy a type without a trivial copy/move "
  1293. "constructor or assignment operator");
  1294. // Ignore empty classes in C++.
  1295. if (Record->isEmpty())
  1296. return;
  1297. }
  1298. }
  1299. // HLSL Change Begins
  1300. if (getLangOpts().HLSL) {
  1301. // Don't generate memcpy for hlsl.
  1302. CGM.getHLSLRuntime().EmitHLSLAggregateCopy(*this, SrcPtr, DestPtr, Ty);
  1303. return;
  1304. }
  1305. // HLSL Change Ends
  1306. // Aggregate assignment turns into llvm.memcpy. This is almost valid per
  1307. // C99 6.5.16.1p3, which states "If the value being stored in an object is
  1308. // read from another object that overlaps in anyway the storage of the first
  1309. // object, then the overlap shall be exact and the two objects shall have
  1310. // qualified or unqualified versions of a compatible type."
  1311. //
  1312. // memcpy is not defined if the source and destination pointers are exactly
  1313. // equal, but other compilers do this optimization, and almost every memcpy
  1314. // implementation handles this case safely. If there is a libc that does not
  1315. // safely handle this, we can add a target hook.
  1316. // Get data size and alignment info for this aggregate. If this is an
  1317. // assignment don't copy the tail padding. Otherwise copying it is fine.
  1318. std::pair<CharUnits, CharUnits> TypeInfo;
  1319. if (isAssignment)
  1320. TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
  1321. else
  1322. TypeInfo = getContext().getTypeInfoInChars(Ty);
  1323. if (alignment.isZero())
  1324. alignment = TypeInfo.second;
  1325. llvm::Value *SizeVal = nullptr;
  1326. if (TypeInfo.first.isZero()) {
  1327. // But note that getTypeInfo returns 0 for a VLA.
  1328. if (auto *VAT = dyn_cast_or_null<VariableArrayType>(
  1329. getContext().getAsArrayType(Ty))) {
  1330. QualType BaseEltTy;
  1331. SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
  1332. TypeInfo = getContext().getTypeInfoDataSizeInChars(BaseEltTy);
  1333. std::pair<CharUnits, CharUnits> LastElementTypeInfo;
  1334. if (!isAssignment)
  1335. LastElementTypeInfo = getContext().getTypeInfoInChars(BaseEltTy);
  1336. assert(!TypeInfo.first.isZero());
  1337. SizeVal = Builder.CreateNUWMul(
  1338. SizeVal,
  1339. llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
  1340. if (!isAssignment) {
  1341. SizeVal = Builder.CreateNUWSub(
  1342. SizeVal,
  1343. llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
  1344. SizeVal = Builder.CreateNUWAdd(
  1345. SizeVal, llvm::ConstantInt::get(
  1346. SizeTy, LastElementTypeInfo.first.getQuantity()));
  1347. }
  1348. }
  1349. }
  1350. if (!SizeVal) {
  1351. SizeVal = llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity());
  1352. }
  1353. // FIXME: If we have a volatile struct, the optimizer can remove what might
  1354. // appear to be `extra' memory ops:
  1355. //
  1356. // volatile struct { int i; } a, b;
  1357. //
  1358. // int main() {
  1359. // a = b;
  1360. // a = b;
  1361. // }
  1362. //
  1363. // we need to use a different call here. We use isVolatile to indicate when
  1364. // either the source or the destination is volatile.
  1365. llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
  1366. llvm::Type *DBP =
  1367. llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
  1368. DestPtr = Builder.CreateBitCast(DestPtr, DBP);
  1369. llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
  1370. llvm::Type *SBP =
  1371. llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
  1372. SrcPtr = Builder.CreateBitCast(SrcPtr, SBP);
  1373. // Don't do any of the memmove_collectable tests if GC isn't set.
  1374. if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
  1375. // fall through
  1376. } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
  1377. RecordDecl *Record = RecordTy->getDecl();
  1378. if (Record->hasObjectMember()) {
  1379. CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
  1380. SizeVal);
  1381. return;
  1382. }
  1383. } else if (Ty->isArrayType()) {
  1384. QualType BaseType = getContext().getBaseElementType(Ty);
  1385. if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
  1386. if (RecordTy->getDecl()->hasObjectMember()) {
  1387. CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
  1388. SizeVal);
  1389. return;
  1390. }
  1391. }
  1392. }
  1393. // Determine the metadata to describe the position of any padding in this
  1394. // memcpy, as well as the TBAA tags for the members of the struct, in case
  1395. // the optimizer wishes to expand it in to scalar memory operations.
  1396. llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty);
  1397. Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, alignment.getQuantity(),
  1398. isVolatile, /*TBAATag=*/nullptr, TBAAStructTag);
  1399. }