CGExprAgg.cpp 60 KB

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