]> granicus.if.org Git - clang/commitdiff
Merge constant array and structures. This will create a global variables for arrays...
authorTanya Lattner <tonic@nondot.org>
Wed, 4 Nov 2009 01:18:09 +0000 (01:18 +0000)
committerTanya Lattner <tonic@nondot.org>
Wed, 4 Nov 2009 01:18:09 +0000 (01:18 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85991 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Driver/Options.def
include/clang/Frontend/CompileOptions.h
lib/CodeGen/CGDecl.cpp
lib/Driver/Tools.cpp
tools/clang-cc/clang-cc.cpp

index c3345ec427341b04c01bc71280a2fd64d260f6cb..5370114ee82430eaa292fa42abfa513b080d73f9 100644 (file)
@@ -404,6 +404,7 @@ OPTION("-flax-vector-conversions", flax_vector_conversions, Flag, f_Group, INVAL
 OPTION("-flimited-precision=", flimited_precision_EQ, Joined, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-flto", flto, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fmath-errno", fmath_errno, Flag, f_Group, INVALID, "", 0, 0, 0)
+OPTION("-fmerge-all-constants", fmerge_all_constants, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fmessage-length=", fmessage_length_EQ, Joined, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fms-extensions", fms_extensions, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fmudflapth", fmudflapth, Flag, f_Group, INVALID, "", 0, 0, 0)
@@ -428,6 +429,7 @@ OPTION("-fno-inline-functions", fno_inline_functions, Flag, clang_ignored_f_Grou
 OPTION("-fno-inline", fno_inline, Flag, clang_ignored_f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fno-keep-inline-functions", fno_keep_inline_functions, Flag, clang_ignored_f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fno-math-errno", fno_math_errno, Flag, f_Group, INVALID, "", 0, 0, 0)
+OPTION("-fno-merge-all-constants", fno_merge_all_constants, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fno-omit-frame-pointer", fno_omit_frame_pointer, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fno-pascal-strings", fno_pascal_strings, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fno-rtti", fno_rtti, Flag, f_Group, INVALID, "", 0, 0, 0)
index 508af537b1fa1b96ed316ceea182c8391d11ad3e..ad53a8d592ba62a4971134b7f2bf02f7210fad7c 100644 (file)
@@ -43,7 +43,8 @@ public:
   unsigned NoCommon          : 1; /// Set when -fno-common or C++ is enabled.
   unsigned DisableRedZone    : 1; /// Set when -mno-red-zone is enabled.
   unsigned NoImplicitFloat   : 1; /// Set when -mno-implicit-float is enabled.
-
+  unsigned MergeAllConstants : 1; // Merge identical constants.
+  
   /// Inlining - The kind of inlining to perform.
   InliningMethod Inlining;
 
@@ -67,6 +68,7 @@ public:
     Inlining = NoInlining;
     DisableRedZone = 0;
     NoImplicitFloat = 0;
+    MergeAllConstants = 1;
   }
 };
 
index 4f8aef420d4e604e9f8c8feb16d0d8d0ff09f751..b1ceb4627712738bf9a65dbf27f2c6fbbafb7b05 100644 (file)
@@ -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 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
   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);
index 521c90dc31e5c152d2c6f12dcfe46d12594cc24e..b4c27a24e3a36a4dd5aa2eb1b57f44874fbfa3b9 100644 (file)
@@ -703,6 +703,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back("--debug-pass=Structure");
   if (Args.hasArg(options::OPT_fdebug_pass_arguments))
     CmdArgs.push_back("--debug-pass=Arguments");
+  if (!Args.hasFlag(options::OPT_fmerge_all_constants,
+                    options::OPT_fno_merge_all_constants))
+    CmdArgs.push_back("--no-merge-all-constants");
 
   // This is a coarse approximation of what llvm-gcc actually does, both
   // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
index 71e546980007fec4b38117f0a4d50c54441f3c72..1a3b830ad507e28146f51d9ecac829fcf55ebae7 100644 (file)
@@ -653,6 +653,10 @@ static llvm::cl::opt<bool>
 NoElideConstructors("fno-elide-constructors",
                     llvm::cl::desc("Disable C++ copy constructor elision"));
 
+static llvm::cl::opt<bool>
+NoMergeConstants("fno-merge-all-constants",
+                       llvm::cl::desc("Disallow merging of constants."));
+
 static llvm::cl::opt<std::string>
 TargetABI("target-abi",
           llvm::cl::desc("Target a particular ABI type"));
@@ -1372,6 +1376,8 @@ static void InitializeCompileOptions(CompileOptions &Opts,
 
   Opts.DisableRedZone = DisableRedZone;
   Opts.NoImplicitFloat = NoImplicitFloat;
+  
+  Opts.MergeAllConstants = !NoMergeConstants;
 }
 
 //===----------------------------------------------------------------------===//