[GlobalMerge] Exit early if only one global is to be merged

To save some compilation time and prevent some unnecessary changes.

Differential Revision: https://reviews.llvm.org/D46640

llvm-svn: 332813
This commit is contained in:
Haicheng Wu 2018-05-19 18:00:02 +00:00
parent 9968e0dd49
commit 69ba0613f2
3 changed files with 16 additions and 16 deletions

View File

@ -445,6 +445,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
LLVM_DEBUG(dbgs() << " Trying to merge set, starts with #"
<< GlobalSet.find_first() << "\n");
bool Changed = false;
ssize_t i = GlobalSet.find_first();
while (i != -1) {
ssize_t j = 0;
@ -469,6 +470,12 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
}
}
// Exit early if there is only one global to merge.
if (Tys.size() < 2) {
i = j;
continue;
}
// If merged variables doesn't have external linkage, we needn't to expose
// the symbol after merging.
GlobalValue::LinkageTypes Linkage = HasExternal
@ -526,10 +533,11 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
NumMerged++;
}
Changed = true;
i = j;
}
return true;
return Changed;
}
void GlobalMerge::collectUsedGlobalVariables(Module &M) {

View File

@ -10,8 +10,8 @@ define void @f1(i32 %a1, i32 %a2, i32 %a3) {
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE
;CHECK-APPLE-IOS-NOT: adrp
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF
;CHECK-APPLE-IOS: adrp x9, __MergedGlobals_y@PAGE+12
;CHECK-APPLE-IOS: str w1, [x9, __MergedGlobals_y@PAGEOFF+12]
;CHECK-APPLE-IOS: adrp x9, _y@PAGE+12
;CHECK-APPLE-IOS: str w1, [x9]
%x3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @x, i32 0, i64 3
%y3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @y, i32 0, i64 3
store i32 %a1, i32* %x3, align 4
@ -25,27 +25,19 @@ define void @f1(i32 %a1, i32 %a2, i32 %a3) {
;CHECK: .L_MergedGlobals:
;CHECK: .size .L_MergedGlobals, 4004
;CHECK: .type .L_MergedGlobals.1,@object // @_MergedGlobals.1
;CHECK: .local .L_MergedGlobals.1
;CHECK: .comm .L_MergedGlobals.1,4000,16
;CHECK-APPLE-IOS: .zerofill __DATA,__common,_y,4000,2
;CHECK-APPLE-IOS: .p2align 4
;CHECK-APPLE-IOS: __MergedGlobals_x:
;CHECK-APPLE-IOS: .long 1
;CHECK-APPLE-IOS: .space 4000
;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_y,4000,4
;CHECK: .set z, .L_MergedGlobals
;CHECK: .globl x
;CHECK: .set x, .L_MergedGlobals+4
;CHECK: .size x, 4000
;CHECK: .globl y
;CHECK: .set y, .L_MergedGlobals.1
;CHECK: .size y, 4000
;CHECK-APPLE-IOS-NOT: .set _z, __MergedGlobals_x
;CHECK-APPLE-IOS:.globl _x
;CHECK-APPLE-IOS:.set _x, __MergedGlobals_x+4
;CHECK-APPLE-IOS:.globl _y
;CHECK-APPLE-IOS:.set _y, __MergedGlobals_y

View File

@ -52,23 +52,23 @@ declare i8* @__cxa_begin_catch(i8*)
declare void @__cxa_end_catch()
; CHECK: g3:
; CHECK: _MergedGlobals:
@g1 = internal global i32 1
@g2 = internal global i32 2
; Make sure that the complete variable fits within the range of the maximum
; offset. Having the starting offset in range is not sufficient.
; When this works properly, @g3 is placed in a separate chunk of merged globals.
; CHECK: _MergedGlobals.1:
; When this works properly, @g3 is not merged.
@g3 = internal global [30 x i32] [ i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10 ], align 4
; Global variables that can be placed in BSS should be kept together in a
; separate pool of merged globals.
; CHECK: _MergedGlobals.2
; CHECK: _MergedGlobals.1
@g4 = internal global i32 0
@g5 = internal global i32 0
; Global variables that are constant can be merged together
; CHECK: _MergedGlobals.3
; CHECK: _MergedGlobals.2
@g6 = internal constant [12 x i32] zeroinitializer, align 4
@g7 = internal constant [12 x i32] zeroinitializer, align 4