Skip to content

Commit e578b3b

Browse files
committed
[InstSimplify] Fold all global variables with initializers
Allow computing size of interposable or externally initializable global variables. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D152145
1 parent ebd2ed2 commit e578b3b

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
830830
}
831831

832832
SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
833-
if (!GV.hasDefinitiveInitializer())
833+
if (!GV.getValueType()->isSized() || GV.hasExternalWeakLinkage() ||
834+
((!GV.hasInitializer() || GV.isInterposable()) &&
835+
Options.EvalMode != ObjectSizeOpts::Mode::Min))
834836
return unknown();
835837

836838
APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2+
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3+
4+
@gv = global i64 zeroinitializer, align 16
5+
6+
define i1 @cmp_gv_alloca() {
7+
; CHECK-LABEL: define i1 @cmp_gv_alloca() {
8+
; CHECK-NEXT: ret i1 false
9+
;
10+
%alloca = alloca i64, align 8
11+
%cmp = icmp eq ptr %alloca, @gv
12+
ret i1 %cmp
13+
}
14+
15+
@gv_externally_init = externally_initialized global i64 zeroinitializer, align 16
16+
17+
define i1 @cmp_gv_alloca_extern_init() {
18+
; CHECK-LABEL: define i1 @cmp_gv_alloca_extern_init() {
19+
; CHECK-NEXT: ret i1 false
20+
;
21+
%alloca = alloca i64, align 8
22+
%cmp = icmp eq ptr %alloca, @gv_externally_init
23+
ret i1 %cmp
24+
}
25+
26+
@const_gv = protected addrspace(4) externally_initialized global [4096 x i64] zeroinitializer, align 16
27+
define i1 @cmp_gv_alloca_cast() {
28+
; CHECK-LABEL: define i1 @cmp_gv_alloca_cast() {
29+
; CHECK-NEXT: ret i1 false
30+
;
31+
%alloca = alloca i64, align 8, addrspace(5)
32+
%cast.alloca = addrspacecast ptr addrspace(5) %alloca to ptr
33+
%cmp = icmp eq ptr %cast.alloca, addrspacecast (ptr addrspace(4) @const_gv to ptr)
34+
ret i1 %cmp
35+
}
36+
37+
@gv_weak = weak global i64 zeroinitializer, align 16
38+
39+
define i1 @cmp_gv_weak_alloca() {
40+
; CHECK-LABEL: define i1 @cmp_gv_weak_alloca() {
41+
; CHECK-NEXT: ret i1 false
42+
;
43+
%alloca = alloca i64, align 8
44+
%cmp = icmp eq ptr %alloca, @gv_weak
45+
ret i1 %cmp
46+
}
47+
48+
%opaque = type opaque
49+
@gv_unsized = weak global %opaque zeroinitializer, align 16
50+
51+
define i1 @cmp_gv_unsized_alloca() {
52+
; CHECK-LABEL: define i1 @cmp_gv_unsized_alloca() {
53+
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i64, align 8
54+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[ALLOCA]], @gv_unsized
55+
; CHECK-NEXT: ret i1 [[CMP]]
56+
;
57+
%alloca = alloca i64, align 8
58+
%cmp = icmp eq ptr %alloca, @gv_unsized
59+
ret i1 %cmp
60+
}

llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,41 @@ define i64 @test_objectsize_malloc() {
146146
%objsize = call i64 @llvm.objectsize.i64(ptr %ptr, i1 false, i1 true, i1 true)
147147
ret i64 %objsize
148148
}
149+
150+
@gv_weak = weak global i64 zeroinitializer, align 16
151+
152+
define i32 @promote_with_objectsize_min_false() {
153+
; CHECK-LABEL: @promote_with_objectsize_min_false(
154+
; CHECK-NEXT: ret i32 -1
155+
;
156+
%size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 false, i1 false, i1 false)
157+
ret i32 %size
158+
}
159+
160+
define i32 @promote_with_objectsize_min_true() {
161+
; CHECK-LABEL: @promote_with_objectsize_min_true(
162+
; CHECK-NEXT: ret i32 8
163+
;
164+
%size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 true, i1 false, i1 false)
165+
ret i32 %size
166+
}
167+
168+
@gv_extern = extern_weak global i64, align 16
169+
170+
define i32 @promote_with_objectsize_nullunknown_false() {
171+
; CHECK-LABEL: @promote_with_objectsize_nullunknown_false(
172+
; CHECK-NEXT: ret i32 0
173+
;
174+
%size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 false, i1 false)
175+
ret i32 %size
176+
}
177+
178+
define i32 @promote_with_objectsize_nullunknown_true() {
179+
; CHECK-LABEL: @promote_with_objectsize_nullunknown_true(
180+
; CHECK-NEXT: ret i32 0
181+
;
182+
%size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 true, i1 false)
183+
ret i32 %size
184+
}
185+
186+
declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1)

0 commit comments

Comments
 (0)