[llvm] be88b58 - [InstCombine] Call simplifyLoadInst()

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


Author: Nikita Popov
Date: 2023-02-20T10:49:44+01:00
New Revision: be88b5814d9efce131dbc0c8e288907e2e6c89be

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

LOG: [InstCombine] Call simplifyLoadInst()

InstCombine is supposed to be a superset of InstSimplify, but
failed to invoke load simplification.

Unfortunately, this causes a minor compile-time regression, which
will be mitigated in a future commit.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/InstructionSimplify.h
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
    llvm/test/Transforms/InstCombine/load.ll
    llvm/test/Transforms/InstCombine/memcpy-from-global.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/InstructionSimplify.h b/llvm/include/llvm/Analysis/InstructionSimplify.h
index d75e04156794..861fa3b20a49 100644
--- a/llvm/include/llvm/Analysis/InstructionSimplify.h
+++ b/llvm/include/llvm/Analysis/InstructionSimplify.h
@@ -318,6 +318,10 @@ Value *simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q);
 /// If not, this returns null.
 Value *simplifyFreezeInst(Value *Op, const SimplifyQuery &Q);
 
+/// Given a load instruction and its pointer operand, fold the result or return
+/// null.
+Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q);
+
 /// See if we can compute a simplified version of this instruction. If not,
 /// return null.
 Value *simplifyInstruction(Instruction *I, const SimplifyQuery &Q,

diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index a20aeaa203f3..afeb80b5ff7a 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6590,8 +6590,8 @@ Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
   return ::simplifyFreezeInst(Op0, Q);
 }
 
-static Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp,
-                               const SimplifyQuery &Q) {
+Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp,
+                              const SimplifyQuery &Q) {
   if (LI->isVolatile())
     return nullptr;
 

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 33ef15d6784d..0f650c7385ad 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -1024,6 +1024,8 @@ static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
 
 Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
   Value *Op = LI.getOperand(0);
+  if (Value *Res = simplifyLoadInst(&LI, Op, SQ.getWithInstruction(&LI)))
+    return replaceInstUsesWith(LI, Res);
 
   // Try to canonicalize the loaded type.
   if (Instruction *Res = combineLoadToOperationType(*this, LI))

diff  --git a/llvm/test/Transforms/InstCombine/load.ll b/llvm/test/Transforms/InstCombine/load.ll
index acfac71007bd..0eee3095ee5b 100644
--- a/llvm/test/Transforms/InstCombine/load.ll
+++ b/llvm/test/Transforms/InstCombine/load.ll
@@ -395,9 +395,7 @@ define <2 x i64> @test24(ptr %P) {
 
 define i16 @load_from_zero_with_dynamic_offset(i64 %idx) {
 ; CHECK-LABEL: @load_from_zero_with_dynamic_offset(
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i16, ptr @GLOBAL, i64 [[IDX:%.*]]
-; CHECK-NEXT:    [[V:%.*]] = load i16, ptr [[GEP]], align 2
-; CHECK-NEXT:    ret i16 [[V]]
+; CHECK-NEXT:    ret i16 0
 ;
   %gep = getelementptr i16, ptr @GLOBAL, i64 %idx
   %v = load i16, ptr %gep
@@ -408,10 +406,7 @@ declare ptr @llvm.strip.invariant.group.p0(ptr %p)
 
 define i32 @load_via_strip_invariant_group() {
 ; CHECK-LABEL: @load_via_strip_invariant_group(
-; CHECK-NEXT:    [[A:%.*]] = call ptr @llvm.strip.invariant.group.p0(ptr nonnull @Y)
-; CHECK-NEXT:    [[B:%.*]] = getelementptr i8, ptr [[A]], i64 8
-; CHECK-NEXT:    [[D:%.*]] = load i32, ptr [[B]], align 4
-; CHECK-NEXT:    ret i32 [[D]]
+; CHECK-NEXT:    ret i32 37
 ;
   %a = call ptr @llvm.strip.invariant.group.p0(ptr @Y)
   %b = getelementptr i8, ptr %a, i64 8

diff  --git a/llvm/test/Transforms/InstCombine/memcpy-from-global.ll b/llvm/test/Transforms/InstCombine/memcpy-from-global.ll
index b230c1488f75..349673974b8f 100644
--- a/llvm/test/Transforms/InstCombine/memcpy-from-global.ll
+++ b/llvm/test/Transforms/InstCombine/memcpy-from-global.ll
@@ -303,9 +303,7 @@ entry:
 define float @test11(i64 %i) {
 ; CHECK-LABEL: @test11(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[G:%.*]] = getelementptr [4 x float], ptr addrspace(1) @I, i64 0, i64 [[I:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = load float, ptr addrspace(1) [[G]], align 4
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    ret float 0.000000e+00
 ;
 
 entry:


        


More information about the llvm-commits mailing list