From 9a22a287be11af048aa0140abad783a252678a85 Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanaka@apple.com>
Date: Mon, 17 Apr 2017 15:21:55 +0000
Subject: [PATCH] [ObjC] Mark loads from __NSArray0 and __NSDictionary0 as
 invariant.load.

Also, simplify code by calling MakeNaturalAlignAddrLValue.

This is a follow-up to r300396.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300454 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/CodeGen/CGObjC.cpp                       | 10 ++++++----
 test/CodeGenObjC/empty-collection-literals.m |  2 +-
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp
index 3a09a15dbc1..357d1ab818c 100644
--- a/lib/CodeGen/CGObjC.cpp
+++ b/lib/CodeGen/CGObjC.cpp
@@ -126,10 +126,12 @@ llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
     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()));
+    LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy);
+    llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getLocStart());
+    cast<llvm::LoadInst>(Ptr)->setMetadata(
+        CGM.getModule().getMDKindID("invariant.load"),
+        llvm::MDNode::get(getLLVMContext(), None));
+    return Builder.CreateBitCast(Ptr, ConvertType(E->getType()));
   }
 
   // Compute the type of the array we're initializing.
diff --git a/test/CodeGenObjC/empty-collection-literals.m b/test/CodeGenObjC/empty-collection-literals.m
index 0f9715f521f..4b1d7f6ebe4 100644
--- a/test/CodeGenObjC/empty-collection-literals.m
+++ b/test/CodeGenObjC/empty-collection-literals.m
@@ -41,7 +41,7 @@ void test_empty_dictionary() {
 
   // CHECK-WITH-EMPTY-COLLECTIONS-LABEL: define void @test_empty_dictionary
   // CHECK-WITH-EMPTY-COLLECTIONS-NOT: ret void
-  // CHECK-WITH-EMPTY-COLLECTIONS: load {{.*}} @__NSDictionary0__
+  // CHECK-WITH-EMPTY-COLLECTIONS: load {{.*}} @__NSDictionary0__{{.*}}!invariant.load
   // CHECK-WITH-EMPTY-COLLECTIONS-NOT: ret void
   // CHECK-WITH-EMPTY-COLLECTIONS: {{call.*objc_retain\(}}
   // CHECK-WITH-EMPTY-COLLECTIONS-NOT: ret void
-- 
GitLab