[InstCombine] Call simplifyLoadInst()
authorNikita Popov <npopov@redhat.com>
Wed, 15 Feb 2023 11:14:55 +0000 (12:14 +0100)
committerNikita Popov <npopov@redhat.com>
Mon, 20 Feb 2023 09:49:44 +0000 (10:49 +0100)
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.

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

index d75e041..861fa3b 100644 (file)
@@ -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,
index a20aeaa..afeb80b 100644 (file)
@@ -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;
 
index 33ef15d..0f650c7 100644 (file)
@@ -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))
index acfac71..0eee309 100644 (file)
@@ -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
index b230c14..3496739 100644 (file)
@@ -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: