diff options
author | Akira Hatanaka <ahatanaka@apple.com> | 2017-04-15 05:31:35 +0000 |
---|---|---|
committer | Akira Hatanaka <ahatanaka@apple.com> | 2017-04-15 05:31:35 +0000 |
commit | f79091e91f984b356f747954ef470951690fdab7 (patch) | |
tree | 902d50fecb072645c26cc0c1399d6121830a04bc /lib/CodeGen/CGObjC.cpp | |
parent | 918a3427117562dbe2d6f64d1be24ccb5b178a9e (diff) |
[ObjC] Use empty Objective-C collection literal constants when
available.
Original patch by Douglas Gregor with minor modifications.
rdar://problem/20689633
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300389 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGObjC.cpp')
-rw-r--r-- | lib/CodeGen/CGObjC.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index c011ab4dfc..067718e2b8 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -117,10 +117,22 @@ llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); if (!ALE) DLE = cast<ObjCDictionaryLiteral>(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); + LValue LV = LValue::MakeAddr(Constant, IdTy, + Context.getTypeAlignInChars(IdTy), Context); + 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(); |