summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen/CGObjC.cpp
diff options
context:
space:
mode:
authorAkira Hatanaka <ahatanaka@apple.com>2017-04-15 05:31:35 +0000
committerAkira Hatanaka <ahatanaka@apple.com>2017-04-15 05:31:35 +0000
commitdad52660449aca2ba77e61a06afcc9cb52fd44fd (patch)
treed0f5e5aa024219cab367e5f384b166cb985c7e72 /clang/lib/CodeGen/CGObjC.cpp
parent2561885f574ce7aedee4d30f3dbc6bfb09d64c06 (diff)
downloadbcm5719-llvm-dad52660449aca2ba77e61a06afcc9cb52fd44fd.tar.gz
bcm5719-llvm-dad52660449aca2ba77e61a06afcc9cb52fd44fd.zip
[ObjC] Use empty Objective-C collection literal constants when
available. Original patch by Douglas Gregor with minor modifications. rdar://problem/20689633 llvm-svn: 300389
Diffstat (limited to 'clang/lib/CodeGen/CGObjC.cpp')
-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..067718e2b8e 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);
+ 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();
OpenPOWER on IntegriCloud