Skip to content

Commit e22d260

Browse files
committed
GlobalOpt: Use the correct address space when creating a "*.init" global.
1 parent c8b7ec2 commit e22d260

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
950950
new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
951951
GlobalValue::InternalLinkage,
952952
ConstantInt::getFalse(GV->getContext()),
953-
GV->getName()+".init", GV->getThreadLocalMode());
953+
GV->getName()+".init", GV->getThreadLocalMode(),
954+
GV->getAddressSpace());
954955
bool InitBoolUsed = false;
955956

956957
// Loop over all instruction uses of GV, processing them in turn.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
2+
; RUN: opt -S -passes=globalopt -o - < %s | FileCheck %s
3+
4+
@g = internal addrspace(200) global ptr null, align 8
5+
6+
;.
7+
; CHECK: @g.init = internal unnamed_addr addrspace(200) global i1 false
8+
;.
9+
define internal i32 @f1() {
10+
; CHECK-LABEL: define {{[^@]+}}@f1() unnamed_addr {
11+
; CHECK-NEXT: bb:
12+
; CHECK-NEXT: [[G_INIT_VAL:%.*]] = load i1, ptr addrspace(200) @g.init, align 1
13+
; CHECK-NEXT: call fastcc void @f2()
14+
; CHECK-NEXT: [[NOTINIT:%.*]] = xor i1 [[G_INIT_VAL]], true
15+
; CHECK-NEXT: br i1 [[NOTINIT]], label [[BB2:%.*]], label [[BB3:%.*]]
16+
; CHECK: bb2:
17+
; CHECK-NEXT: br label [[BB4:%.*]]
18+
; CHECK: bb3:
19+
; CHECK-NEXT: br label [[BB4]]
20+
; CHECK: bb4:
21+
; CHECK-NEXT: [[I5:%.*]] = phi i32 [ -1, [[BB2]] ], [ 1, [[BB3]] ]
22+
; CHECK-NEXT: ret i32 [[I5]]
23+
;
24+
bb:
25+
%i = load ptr addrspace(200), ptr addrspace(200) @g, align 8
26+
call void @f2()
27+
%i1 = icmp eq ptr addrspace(200) %i, null
28+
br i1 %i1, label %bb2, label %bb3
29+
30+
bb2: ; preds = %bb
31+
br label %bb4
32+
33+
bb3: ; preds = %bb
34+
br label %bb4
35+
36+
bb4: ; preds = %bb3, %bb2
37+
%i5 = phi i32 [ -1, %bb2 ], [ 1, %bb3 ]
38+
ret i32 %i5
39+
}
40+
41+
define internal void @f2() {
42+
; CHECK-LABEL: define {{[^@]+}}@f2() unnamed_addr {
43+
; CHECK-NEXT: bb:
44+
; CHECK-NEXT: store i1 true, ptr addrspace(200) @g.init, align 1
45+
; CHECK-NEXT: ret void
46+
;
47+
bb:
48+
%i = call noalias ptr @malloc(i64 4)
49+
store ptr %i, ptr addrspace(200) @g, align 8
50+
ret void
51+
}
52+
53+
define dso_local i32 @main() {
54+
; CHECK-LABEL: define {{[^@]+}}@main() local_unnamed_addr {
55+
; CHECK-NEXT: bb:
56+
; CHECK-NEXT: store i1 false, ptr addrspace(200) @g.init, align 1
57+
; CHECK-NEXT: [[I:%.*]] = call fastcc i32 @f1()
58+
; CHECK-NEXT: ret i32 [[I]]
59+
;
60+
bb:
61+
store ptr null, ptr addrspace(200) @g, align 8
62+
%i = call i32 @f1()
63+
ret i32 %i
64+
}
65+
66+
; Function Attrs: allockind("alloc,uninitialized") allocsize(0)
67+
declare dso_local noalias ptr @malloc(i64) #0
68+
69+
attributes #0 = { allockind("alloc,uninitialized") allocsize(0) }

0 commit comments

Comments
 (0)