[PATCH] D144184: [InstSimplify] fold LoadInst for uniformaly initialized constant global variables
Kohei Asano via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 16 13:56:15 PST 2023
khei4 updated this revision to Diff 498137.
khei4 added a comment.
Fix comments and add test cases.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D144184/new/
https://reviews.llvm.org/D144184
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/load.ll
Index: llvm/test/Transforms/InstSimplify/load.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/load.ll
+++ llvm/test/Transforms/InstSimplify/load.ll
@@ -3,8 +3,13 @@
@zeroinit = constant {} zeroinitializer
@poison = constant {} poison
+
+
@constzeroarray = internal constant [4 x i32] zeroinitializer
+ at constzeroarrayi8 = internal constant [2 x i8] zeroinitializer
+
@constarray = internal constant [4 x i32] [i32 1, i32 1, i32 1, i32 1]
+ at constarrayi8 = internal constant [2 x i8] [i8 1, i8 1]
define i32 @crash_on_zeroinit() {
; CHECK-LABEL: @crash_on_zeroinit(
@@ -47,18 +52,40 @@
; CHECK-LABEL: @load_gep_const_zero_array(
; CHECK-NEXT: ret i32 0
;
- %gep = getelementptr inbounds [4 x i32], ptr @constzeroarray , i64 0, i64 %idx
+ %gep = getelementptr inbounds [4 x i32], ptr @constzeroarray, i64 0, i64 %idx
%load = load i32, ptr %gep
ret i32 %load
}
+define i8 @load_gep_const_zero_array_i8(i64 %idx) {
+; CHECK-LABEL: @load_gep_const_zero_array_i8(
+; CHECK-NEXT: ret i8 0
+;
+ %gep1 = getelementptr inbounds i8, ptr @constzeroarrayi8, i64 0
+ %gep = getelementptr inbounds i8, ptr %gep1, i64 %idx
+ %load = load i8, ptr %gep
+ ret i8 %load
+}
+
define i32 @load_gep_const_array(i64 %idx) {
; CHECK-LABEL: @load_gep_const_array(
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @constarray, i64 0, i64 [[IDX:%.*]]
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
;
- %gep = getelementptr inbounds [4 x i32], ptr @constarray , i64 0, i64 %idx
+ %gep = getelementptr inbounds [4 x i32], ptr @constarray, i64 0, i64 %idx
%load = load i32, ptr %gep
ret i32 %load
}
+
+define i8 @load_gep_const_array_i8(i64 %idx) {
+; CHECK-LABEL: @load_gep_const_array_i8(
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr @constarrayi8, i64 [[IDX:%.*]]
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %gep1 = getelementptr inbounds i8, ptr @constarrayi8, i64 0
+ %gep = getelementptr inbounds i8, ptr %gep1, i64 %idx
+ %load = load i8, ptr %gep
+ ret i8 %load
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6586,6 +6586,11 @@
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
return nullptr;
+ // If GlobalVariable's initializer is uniform, then return the constant
+ // regardless its offset.
+ if (Constant *C =
+ ConstantFoldLoadFromUniformValue(GV->getInitializer(), LI->getType()))
+ return C;
// Try to convert operand into a constant by stripping offsets while looking
// through invariant.group intrinsics.
APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144184.498137.patch
Type: text/x-patch
Size: 2934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230216/9070d37d/attachment.bin>
More information about the llvm-commits
mailing list