From 4d53a1cb31c4950b2a30af3f4f6148f7216ffd92 Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Sat, 15 Apr 2017 06:42:00 +0000 Subject: [ObjC] Use empty Objective-C collection literal constants when available. Original patch by Douglas Gregor with minor modifications. This recommits r300389, which broke bots because there have been API changes since the original patch was written. rdar://problem/20689633 llvm-svn: 300396 --- clang/lib/CodeGen/CGObjC.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'clang/lib/CodeGen') diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp index c011ab4dfc9..3a09a15dbc1 100644 --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -117,10 +117,22 @@ llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, const ObjCArrayLiteral *ALE = dyn_cast(E); if (!ALE) DLE = cast(E); - - // Compute the type of the array we're initializing. + + // Optimize empty collections by referencing constants, when available. uint64_t NumElements = ALE ? ALE->getNumElements() : DLE->getNumElements(); + if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) { + StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__"; + QualType IdTy(CGM.getContext().getObjCIdType()); + llvm::Constant *Constant = + CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName); + Address Addr(Constant, Context.getTypeAlignInChars(IdTy)); + LValue LV = MakeAddrLValue(Addr, IdTy); + return Builder.CreateBitCast(EmitLoadOfScalar(LV, E->getLocStart()), + ConvertType(E->getType())); + } + + // Compute the type of the array we're initializing. llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), NumElements); QualType ElementType = Context.getObjCIdType().withConst(); -- cgit v1.2.3