[llvm] e578b3b - [InstSimplify] Fold all global variables with initializers
Anshil Gandhi via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 3 14:11:57 PDT 2023
Author: Anshil Gandhi
Date: 2023-07-03T15:11:41-06:00
New Revision: e578b3be24fc3986f46942aa1fdbf73ff8d34ace
URL: https://github.com/llvm/llvm-project/commit/e578b3be24fc3986f46942aa1fdbf73ff8d34ace
DIFF: https://github.com/llvm/llvm-project/commit/e578b3be24fc3986f46942aa1fdbf73ff8d34ace.diff
LOG: [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
Added:
llvm/test/Transforms/InstSimplify/gv-alloca-cmp.ll
Modified:
llvm/lib/Analysis/MemoryBuiltins.cpp
llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 6b2e4425ade0b7..53e089ba1feae5 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -830,7 +830,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
}
SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
- if (!GV.hasDefinitiveInitializer())
+ if (!GV.getValueType()->isSized() || GV.hasExternalWeakLinkage() ||
+ ((!GV.hasInitializer() || GV.isInterposable()) &&
+ Options.EvalMode != ObjectSizeOpts::Mode::Min))
return unknown();
APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
diff --git a/llvm/test/Transforms/InstSimplify/gv-alloca-cmp.ll b/llvm/test/Transforms/InstSimplify/gv-alloca-cmp.ll
new file mode 100644
index 00000000000000..4f2544052b2153
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/gv-alloca-cmp.ll
@@ -0,0 +1,60 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+ at gv = global i64 zeroinitializer, align 16
+
+define i1 @cmp_gv_alloca() {
+; CHECK-LABEL: define i1 @cmp_gv_alloca() {
+; CHECK-NEXT: ret i1 false
+;
+ %alloca = alloca i64, align 8
+ %cmp = icmp eq ptr %alloca, @gv
+ ret i1 %cmp
+}
+
+ at gv_externally_init = externally_initialized global i64 zeroinitializer, align 16
+
+define i1 @cmp_gv_alloca_extern_init() {
+; CHECK-LABEL: define i1 @cmp_gv_alloca_extern_init() {
+; CHECK-NEXT: ret i1 false
+;
+ %alloca = alloca i64, align 8
+ %cmp = icmp eq ptr %alloca, @gv_externally_init
+ ret i1 %cmp
+}
+
+ at const_gv = protected addrspace(4) externally_initialized global [4096 x i64] zeroinitializer, align 16
+define i1 @cmp_gv_alloca_cast() {
+; CHECK-LABEL: define i1 @cmp_gv_alloca_cast() {
+; CHECK-NEXT: ret i1 false
+;
+ %alloca = alloca i64, align 8, addrspace(5)
+ %cast.alloca = addrspacecast ptr addrspace(5) %alloca to ptr
+ %cmp = icmp eq ptr %cast.alloca, addrspacecast (ptr addrspace(4) @const_gv to ptr)
+ ret i1 %cmp
+}
+
+ at gv_weak = weak global i64 zeroinitializer, align 16
+
+define i1 @cmp_gv_weak_alloca() {
+; CHECK-LABEL: define i1 @cmp_gv_weak_alloca() {
+; CHECK-NEXT: ret i1 false
+;
+ %alloca = alloca i64, align 8
+ %cmp = icmp eq ptr %alloca, @gv_weak
+ ret i1 %cmp
+}
+
+%opaque = type opaque
+ at gv_unsized = weak global %opaque zeroinitializer, align 16
+
+define i1 @cmp_gv_unsized_alloca() {
+; CHECK-LABEL: define i1 @cmp_gv_unsized_alloca() {
+; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i64, align 8
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[ALLOCA]], @gv_unsized
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %alloca = alloca i64, align 8
+ %cmp = icmp eq ptr %alloca, @gv_unsized
+ ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll b/llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll
index 52f4cbb212c32e..c90d5152e1a096 100644
--- a/llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll
+++ b/llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll
@@ -146,3 +146,41 @@ define i64 @test_objectsize_malloc() {
%objsize = call i64 @llvm.objectsize.i64(ptr %ptr, i1 false, i1 true, i1 true)
ret i64 %objsize
}
+
+ at gv_weak = weak global i64 zeroinitializer, align 16
+
+define i32 @promote_with_objectsize_min_false() {
+; CHECK-LABEL: @promote_with_objectsize_min_false(
+; CHECK-NEXT: ret i32 -1
+;
+ %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 false, i1 false, i1 false)
+ ret i32 %size
+}
+
+define i32 @promote_with_objectsize_min_true() {
+; CHECK-LABEL: @promote_with_objectsize_min_true(
+; CHECK-NEXT: ret i32 8
+;
+ %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 true, i1 false, i1 false)
+ ret i32 %size
+}
+
+ at gv_extern = extern_weak global i64, align 16
+
+define i32 @promote_with_objectsize_nullunknown_false() {
+; CHECK-LABEL: @promote_with_objectsize_nullunknown_false(
+; CHECK-NEXT: ret i32 0
+;
+ %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 false, i1 false)
+ ret i32 %size
+}
+
+define i32 @promote_with_objectsize_nullunknown_true() {
+; CHECK-LABEL: @promote_with_objectsize_nullunknown_true(
+; CHECK-NEXT: ret i32 0
+;
+ %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 true, i1 false)
+ ret i32 %size
+}
+
+declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1)
More information about the llvm-commits
mailing list