Index: lib/CodeGen/GlobalMerge.cpp =================================================================== --- lib/CodeGen/GlobalMerge.cpp +++ lib/CodeGen/GlobalMerge.cpp @@ -73,10 +73,10 @@ #define DEBUG_TYPE "global-merge" -static cl::opt +static cl::opt EnableGlobalMerge("enable-global-merge", cl::Hidden, cl::desc("Enable global merge pass"), - cl::init(true)); + cl::init(cl::BOU_UNSET)); static cl::opt EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, @@ -278,10 +278,35 @@ } } -bool GlobalMerge::doInitialization(Module &M) { - if (!EnableGlobalMerge) +static bool moduleHasEnableGlobalMergeFlag(const Module &M) { + Metadata *FlagMD = M.getModuleFlag("Enable Global Merge"); + + if (!FlagMD) return false; + MDString *Flag = dyn_cast(M.getModuleFlag("Enable Global Merge")); + if (!Flag) + report_fatal_error("Unexpected \"Enable Global Merge\" metadata type, " + "should be a string!"); + + if (Flag->getString() == "true") + return true; + if (Flag->getString() != "false") + report_fatal_error("Unexpected \"Enable Global Merge\" metadata value, " + "should be true|false!"); + return false; +} + +bool GlobalMerge::doInitialization(Module &M) { + switch(EnableGlobalMerge) { + case cl::BOU_UNSET: + if (!moduleHasEnableGlobalMergeFlag(M)) + return false; + break; + case cl::BOU_TRUE: break; + case cl::BOU_FALSE: return false; + }; + DenseMap > Globals, ConstGlobals, BSSGlobals; bool Changed = false; Index: test/CodeGen/AArch64/global-merge-moduleflag-disabled.ll =================================================================== --- /dev/null +++ test/CodeGen/AArch64/global-merge-moduleflag-disabled.ll @@ -0,0 +1,21 @@ +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -o - | FileCheck %s --check-prefix=CHECK-NGM +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -enable-global-merge -o - | FileCheck %s --check-prefix=CHECK-GM +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -enable-global-merge=false -o - | FileCheck %s --check-prefix=CHECK-NGM + +@m = internal global i32 0, align 4 +@n = internal global i32 0, align 4 + +define void @f1(i32 %a1, i32 %a2) { + store i32 %a1, i32* @m, align 4 + store i32 %a2, i32* @n, align 4 + ret void +} + +!0 = !{ i32 1, !"Enable Global Merge", !"false" } +!llvm.module.flags = !{ !0 } + +;CHECK-GM: .type _MergedGlobals,@object // @_MergedGlobals +;CHECK-GM: .local _MergedGlobals +;CHECK-GM: .comm _MergedGlobals,8,8 + +;CHECK-NGM-NOT: MergedGlobals Index: test/CodeGen/AArch64/global-merge-moduleflag-enabled.ll =================================================================== --- /dev/null +++ test/CodeGen/AArch64/global-merge-moduleflag-enabled.ll @@ -0,0 +1,21 @@ +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -o - | FileCheck %s --check-prefix=CHECK-GM +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -enable-global-merge -o - | FileCheck %s --check-prefix=CHECK-GM +; RUN: llc %s -mtriple=aarch64-none-linux-gnu -enable-global-merge=false -o - | FileCheck %s --check-prefix=CHECK-NGM + +@m = internal global i32 0, align 4 +@n = internal global i32 0, align 4 + +define void @f1(i32 %a1, i32 %a2) { + store i32 %a1, i32* @m, align 4 + store i32 %a2, i32* @n, align 4 + ret void +} + +!0 = !{ i32 1, !"Enable Global Merge", !"true" } +!llvm.module.flags = !{ !0 } + +;CHECK-GM: .type _MergedGlobals,@object // @_MergedGlobals +;CHECK-GM: .local _MergedGlobals +;CHECK-GM: .comm _MergedGlobals,8,8 + +;CHECK-NGM-NOT: MergedGlobals Index: test/CodeGen/ARM/2010-12-15-elf-lcomm.ll =================================================================== --- test/CodeGen/ARM/2010-12-15-elf-lcomm.ll +++ test/CodeGen/ARM/2010-12-15-elf-lcomm.ll @@ -12,7 +12,7 @@ ; ASM: .type array00,%object @ @array00 ; ASM-NEXT: .local array00 ; ASM-NEXT: .comm array00,80,1 -; ASM-NEXT: .type _MergedGlobals,%object @ @_MergedGlobals +; ASM-NEXT: .type sum,%object @ @sum ; OBJ: Sections [ Index: test/CodeGen/ARM/2011-01-19-MergedGlobalDbg.ll =================================================================== --- test/CodeGen/ARM/2011-01-19-MergedGlobalDbg.ll +++ test/CodeGen/ARM/2011-01-19-MergedGlobalDbg.ll @@ -1,4 +1,4 @@ -; RUN: llc -filetype=obj < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s +; RUN: llc -filetype=obj -enable-global-merge < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32" target triple = "thumbv7-apple-darwin10" Index: test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll =================================================================== --- test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll +++ test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll @@ -1,4 +1,4 @@ -; RUN: llc < %s -mtriple=thumbv7-apple-darwin10 | FileCheck %s +; RUN: llc < %s -mtriple=thumbv7-apple-darwin10 -enable-global-merge | FileCheck %s ; CHECK: .zerofill __DATA,__bss,__MergedGlobals,16,2 @prev = external global [0 x i16] Index: test/CodeGen/ARM/2011-08-02-MergedGlobalDbg.ll =================================================================== --- test/CodeGen/ARM/2011-08-02-MergedGlobalDbg.ll +++ test/CodeGen/ARM/2011-08-02-MergedGlobalDbg.ll @@ -1,4 +1,4 @@ -; RUN: llc -filetype=obj < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s +; RUN: llc -filetype=obj -enable-global-merge < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s ; Check debug info output for merged global. ; DW_AT_location Index: test/CodeGen/ARM/global-merge-1.ll =================================================================== --- test/CodeGen/ARM/global-merge-1.ll +++ test/CodeGen/ARM/global-merge-1.ll @@ -1,7 +1,7 @@ -; RUN: llc %s -O0 -o - | FileCheck -check-prefix=NO-MERGE %s +; RUN: llc %s -O0 -o - -enable-global-merge=true | FileCheck -check-prefix=NO-MERGE %s ; RUN: llc %s -O0 -o - -enable-global-merge=false | FileCheck -check-prefix=NO-MERGE %s ; RUN: llc %s -O0 -o - -enable-global-merge=true | FileCheck -check-prefix=NO-MERGE %s -; RUN: llc %s -O1 -o - | FileCheck -check-prefix=MERGE %s +; RUN: llc %s -O1 -o - -enable-global-merge=true | FileCheck -check-prefix=MERGE %s ; RUN: llc %s -O1 -o - -enable-global-merge=false | FileCheck -check-prefix=NO-MERGE %s ; RUN: llc %s -O1 -o - -enable-global-merge=true | FileCheck -check-prefix=MERGE %s Index: test/CodeGen/ARM/global-merge-addrspace.ll =================================================================== --- test/CodeGen/ARM/global-merge-addrspace.ll +++ test/CodeGen/ARM/global-merge-addrspace.ll @@ -1,4 +1,4 @@ -; RUN: llc < %s -mtriple=thumb-apple-darwin | FileCheck %s +; RUN: llc < %s -mtriple=thumb-apple-darwin -enable-global-merge | FileCheck %s ; Test the GlobalMerge pass. Check that the pass does not crash when using ; multiple address spaces. Index: test/CodeGen/ARM/global-merge.ll =================================================================== --- test/CodeGen/ARM/global-merge.ll +++ test/CodeGen/ARM/global-merge.ll @@ -1,4 +1,4 @@ -; RUN: llc < %s -mtriple=thumb-apple-darwin -global-merge-on-const=true | FileCheck %s +; RUN: llc < %s -mtriple=thumb-apple-darwin -enable-global-merge -global-merge-on-const=true | FileCheck %s ; Test the ARMGlobalMerge pass. Use -march=thumb because it has a small ; value for the maximum offset (127). Index: test/MC/ARM/elf-reloc-01.ll =================================================================== --- test/MC/ARM/elf-reloc-01.ll +++ test/MC/ARM/elf-reloc-01.ll @@ -60,6 +60,9 @@ declare void @exit(i32) noreturn nounwind +!0 = !{ i32 1, !"Enable Global Merge", !"true" } +!llvm.module.flags = !{ !0 } + ; OBJ: Relocations [ ; OBJ: Section (2) .rel.text { ; OBJ: 0x{{[0-9,A-F]+}} R_ARM_MOVW_ABS_NC _MergedGlobals