[llvm] a4d6c7d - [InstSimplify] Fold LoadInst for uniform constant global variables

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 20 00:44:01 PST 2023


Author: Kohei Asano
Date: 2023-02-20T09:43:52+01:00
New Revision: a4d6c7dd99cbf1c802c633dc9f73a69131b03fe6

URL: https://github.com/llvm/llvm-project/commit/a4d6c7dd99cbf1c802c633dc9f73a69131b03fe6
DIFF: https://github.com/llvm/llvm-project/commit/a4d6c7dd99cbf1c802c633dc9f73a69131b03fe6.diff

LOG: [InstSimplify] Fold LoadInst for uniform constant global variables

Fold LoadInst for uniformly initialized constants, even if there
are non-constant GEP indices.

Goal proof: https://alive2.llvm.org/ce/z/oZtVby

Motivated by https://github.com/rust-lang/rust/issues/107208

Differential Revision: https://reviews.llvm.org/D144184

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/load.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 38dc5fb3ee0e..9f294dfb4f20 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6586,6 +6586,12 @@ static Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp,
   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
     return nullptr;
 
+  // If GlobalVariable's initializer is uniform, then return the constant
+  // regardless of 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);

diff  --git a/llvm/test/Transforms/InstSimplify/load.ll b/llvm/test/Transforms/InstSimplify/load.ll
index 40ce6f7d4dfc..21a6452960ad 100644
--- a/llvm/test/Transforms/InstSimplify/load.ll
+++ b/llvm/test/Transforms/InstSimplify/load.ll
@@ -48,9 +48,7 @@ define <3 x float> @load_vec3() {
 
 define i32 @load_gep_const_zero_array(i64 %idx) {
 ; CHECK-LABEL: @load_gep_const_zero_array(
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @constzeroarray, i64 0, i64 [[IDX:%.*]]
-; CHECK-NEXT:    [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
-; CHECK-NEXT:    ret i32 [[LOAD]]
+; CHECK-NEXT:    ret i32 0
 ;
   %gep = getelementptr inbounds [4 x i32], ptr @constzeroarray, i64 0, i64 %idx
   %load = load i32, ptr %gep
@@ -59,10 +57,7 @@ define i32 @load_gep_const_zero_array(i64 %idx) {
 
 define i8 @load_i8_multi_gep_const_zero_array(i64 %idx1, i64 %idx2) {
 ; CHECK-LABEL: @load_i8_multi_gep_const_zero_array(
-; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds i8, ptr @constzeroarray, i64 [[IDX1:%.*]]
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i8, ptr [[GEP1]], i64 [[IDX2:%.*]]
-; CHECK-NEXT:    [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
-; CHECK-NEXT:    ret i8 [[LOAD]]
+; CHECK-NEXT:    ret i8 0
 ;
   %gep1 = getelementptr inbounds i8, ptr @constzeroarray, i64 %idx1
   %gep = getelementptr inbounds i8, ptr %gep1, i64 %idx2


        


More information about the llvm-commits mailing list