Skip to content

Commit 69ba061

Browse files
author
Haicheng Wu
committed
[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
1 parent 9968e0d commit 69ba061

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
445445
LLVM_DEBUG(dbgs() << " Trying to merge set, starts with #"
446446
<< GlobalSet.find_first() << "\n");
447447

448+
bool Changed = false;
448449
ssize_t i = GlobalSet.find_first();
449450
while (i != -1) {
450451
ssize_t j = 0;
@@ -469,6 +470,12 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
469470
}
470471
}
471472

473+
// Exit early if there is only one global to merge.
474+
if (Tys.size() < 2) {
475+
i = j;
476+
continue;
477+
}
478+
472479
// If merged variables doesn't have external linkage, we needn't to expose
473480
// the symbol after merging.
474481
GlobalValue::LinkageTypes Linkage = HasExternal
@@ -526,10 +533,11 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
526533

527534
NumMerged++;
528535
}
536+
Changed = true;
529537
i = j;
530538
}
531539

532-
return true;
540+
return Changed;
533541
}
534542

535543
void GlobalMerge::collectUsedGlobalVariables(Module &M) {

llvm/test/CodeGen/AArch64/global-merge-3.ll

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ define void @f1(i32 %a1, i32 %a2, i32 %a3) {
1010
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE
1111
;CHECK-APPLE-IOS-NOT: adrp
1212
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF
13-
;CHECK-APPLE-IOS: adrp x9, __MergedGlobals_y@PAGE+12
14-
;CHECK-APPLE-IOS: str w1, [x9, __MergedGlobals_y@PAGEOFF+12]
13+
;CHECK-APPLE-IOS: adrp x9, _y@PAGE+12
14+
;CHECK-APPLE-IOS: str w1, [x9]
1515
%x3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @x, i32 0, i64 3
1616
%y3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @y, i32 0, i64 3
1717
store i32 %a1, i32* %x3, align 4
@@ -25,27 +25,19 @@ define void @f1(i32 %a1, i32 %a2, i32 %a3) {
2525
;CHECK: .L_MergedGlobals:
2626
;CHECK: .size .L_MergedGlobals, 4004
2727

28-
;CHECK: .type .L_MergedGlobals.1,@object // @_MergedGlobals.1
29-
;CHECK: .local .L_MergedGlobals.1
30-
;CHECK: .comm .L_MergedGlobals.1,4000,16
28+
;CHECK-APPLE-IOS: .zerofill __DATA,__common,_y,4000,2
3129

3230
;CHECK-APPLE-IOS: .p2align 4
3331
;CHECK-APPLE-IOS: __MergedGlobals_x:
3432
;CHECK-APPLE-IOS: .long 1
3533
;CHECK-APPLE-IOS: .space 4000
3634

37-
;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_y,4000,4
3835

3936
;CHECK: .set z, .L_MergedGlobals
4037
;CHECK: .globl x
4138
;CHECK: .set x, .L_MergedGlobals+4
4239
;CHECK: .size x, 4000
43-
;CHECK: .globl y
44-
;CHECK: .set y, .L_MergedGlobals.1
45-
;CHECK: .size y, 4000
4640

4741
;CHECK-APPLE-IOS-NOT: .set _z, __MergedGlobals_x
4842
;CHECK-APPLE-IOS:.globl _x
4943
;CHECK-APPLE-IOS:.set _x, __MergedGlobals_x+4
50-
;CHECK-APPLE-IOS:.globl _y
51-
;CHECK-APPLE-IOS:.set _y, __MergedGlobals_y

llvm/test/CodeGen/ARM/global-merge.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,23 @@ declare i8* @__cxa_begin_catch(i8*)
5252

5353
declare void @__cxa_end_catch()
5454

55+
; CHECK: g3:
5556
; CHECK: _MergedGlobals:
5657
@g1 = internal global i32 1
5758
@g2 = internal global i32 2
5859

5960
; Make sure that the complete variable fits within the range of the maximum
6061
; offset. Having the starting offset in range is not sufficient.
61-
; When this works properly, @g3 is placed in a separate chunk of merged globals.
62-
; CHECK: _MergedGlobals.1:
62+
; When this works properly, @g3 is not merged.
6363
@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
6464

6565
; Global variables that can be placed in BSS should be kept together in a
6666
; separate pool of merged globals.
67-
; CHECK: _MergedGlobals.2
67+
; CHECK: _MergedGlobals.1
6868
@g4 = internal global i32 0
6969
@g5 = internal global i32 0
7070

7171
; Global variables that are constant can be merged together
72-
; CHECK: _MergedGlobals.3
72+
; CHECK: _MergedGlobals.2
7373
@g6 = internal constant [12 x i32] zeroinitializer, align 4
7474
@g7 = internal constant [12 x i32] zeroinitializer, align 4

0 commit comments

Comments
 (0)