summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen
diff options
context:
space:
mode:
authorAkira Hatanaka <ahatanaka@apple.com>2017-04-15 06:42:00 +0000
committerAkira Hatanaka <ahatanaka@apple.com>2017-04-15 06:42:00 +0000
commit4d53a1cb31c4950b2a30af3f4f6148f7216ffd92 (patch)
tree7fe753daacdc165e2f75093e8126f1fe6d619dc0 /clang/lib/CodeGen
parentd26d8839d8dd4b0513ca6251ab8f341149446c69 (diff)
downloadbcm5719-llvm-4d53a1cb31c4950b2a30af3f4f6148f7216ffd92.tar.gz
bcm5719-llvm-4d53a1cb31c4950b2a30af3f4f6148f7216ffd92.zip
[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
Diffstat (limited to 'clang/lib/CodeGen')
-rw-r--r--clang/lib/CodeGen/CGObjC.cpp16
1 files changed, 14 insertions, 2 deletions
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<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);
+ 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();
OpenPOWER on IntegriCloud