Merge constant array and structures. This will create a global variables for arrays and structs that are constant and their initializer is constant. It is on by default but can be disable with the flag -fno-merge-all-constants.

llvm-svn: 85991
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 4f8aef4..b1ceb46 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -19,6 +19,7 @@
 #include "clang/AST/DeclObjC.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Frontend/CompileOptions.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Intrinsics.h"
 #include "llvm/Target/TargetData.h"
@@ -316,6 +317,20 @@
   llvm::Value *DeclPtr;
   if (Ty->isConstantSizeType()) {
     if (!Target.useGlobalsForAutomaticVariables()) {
+      
+      // All constant structs and arrays should be global if
+      // their initializer is constant and if the element type is POD.
+      if (CGM.getCompileOpts().MergeAllConstants) {
+        if (Ty.isConstant(getContext())
+            && (Ty->isArrayType() || Ty->isRecordType())
+            && (D.getInit() 
+                && D.getInit()->isConstantInitializer(getContext()))
+            && Ty->isPODType()) {
+          EmitStaticBlockVarDecl(D);
+          return;
+        }
+      }
+      
       // A normal fixed sized variable becomes an alloca in the entry block.
       const llvm::Type *LTy = ConvertTypeForMem(Ty);
       Align = getContext().getDeclAlignInBytes(&D);