[PATCH] D149134: [LICM] Don't try to constant fold instructions

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 01:23:40 PDT 2023


nikic created this revision.
nikic added reviewers: fhahn, asbirlea, mkazantsev, efriedma.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This was introduced in https://github.com/llvm/llvm-project/commit/030f02021b6359ec5641622cf1aa63d873ecf55a as an alleged compile-time optimization. In reality, trying to constant fold instructions is more expensive than just hoisting them. In a standard pipeline, LICM tends to run either after a run of LoopInstSimplify or InstCombine, so LICM doesn't really see constant foldable instructions in the first place, and the attempted fold is futile.

This makes for a very minor compile-time improvement: http://llvm-compile-time-tracker.com/compare.php?from=41c201aabe4220d6d2ff0a07c20e20e932dbac7a&to=057b5f1f3573ddceb04d9eb6fb9973358d53fece&stat=instructions:u


https://reviews.llvm.org/D149134

Files:
  llvm/lib/Transforms/Scalar/LICM.cpp
  llvm/test/Transforms/LICM/hoisting.ll
  llvm/test/Transforms/LICM/pr32129.ll
  llvm/test/Transforms/LICM/scalar-promote.ll


Index: llvm/test/Transforms/LICM/scalar-promote.ll
===================================================================
--- llvm/test/Transforms/LICM/scalar-promote.ll
+++ llvm/test/Transforms/LICM/scalar-promote.ll
@@ -41,15 +41,17 @@
 define void @test2(i32 %i) {
 ; CHECK-LABEL: @test2(
 ; CHECK-NEXT:  Entry:
-; CHECK-NEXT:    [[DOTPROMOTED:%.*]] = load i32, ptr getelementptr inbounds (i32, ptr @X, i64 1), align 4
+; CHECK-NEXT:    [[X1:%.*]] = getelementptr i32, ptr @X, i64 1
+; CHECK-NEXT:    [[X2:%.*]] = getelementptr i32, ptr @X, i64 1
+; CHECK-NEXT:    [[X1_PROMOTED:%.*]] = load i32, ptr [[X1]], align 4
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       Loop:
-; CHECK-NEXT:    [[V1:%.*]] = phi i32 [ [[V:%.*]], [[LOOP]] ], [ [[DOTPROMOTED]], [[ENTRY:%.*]] ]
-; CHECK-NEXT:    [[V]] = add i32 [[V1]], 1
+; CHECK-NEXT:    [[A1:%.*]] = phi i32 [ [[V:%.*]], [[LOOP]] ], [ [[X1_PROMOTED]], [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[V]] = add i32 [[A1]], 1
 ; CHECK-NEXT:    br i1 false, label [[LOOP]], label [[EXIT:%.*]]
 ; CHECK:       Exit:
 ; CHECK-NEXT:    [[V_LCSSA:%.*]] = phi i32 [ [[V]], [[LOOP]] ]
-; CHECK-NEXT:    store i32 [[V_LCSSA]], ptr getelementptr inbounds (i32, ptr @X, i64 1), align 4
+; CHECK-NEXT:    store i32 [[V_LCSSA]], ptr [[X1]], align 4
 ; CHECK-NEXT:    ret void
 ;
 Entry:
Index: llvm/test/Transforms/LICM/pr32129.ll
===================================================================
--- llvm/test/Transforms/LICM/pr32129.ll
+++ llvm/test/Transforms/LICM/pr32129.ll
@@ -6,6 +6,8 @@
 define void @test() {
 ; CHECK-LABEL: define void @test() {
 ; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = icmp ult i32 0, 400
+; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[TMP0]], i32 9) [ "deopt"() ]
 ; CHECK-NEXT:    br label [[HEADER:%.*]]
 ; CHECK:       header.loopexit:
 ; CHECK-NEXT:    br label [[HEADER]]
Index: llvm/test/Transforms/LICM/hoisting.ll
===================================================================
--- llvm/test/Transforms/LICM/hoisting.ll
+++ llvm/test/Transforms/LICM/hoisting.ll
@@ -83,16 +83,17 @@
 }
 
 
-; This loop invariant instruction should be constant folded, not hoisted.
+; Don't bother constant folding the add, just hoist it.
 define i32 @test3(i1 %c) {
 ; CHECK-LABEL: @test3(
 ; CHECK-NEXT:    [[A:%.*]] = load i32, ptr @X, align 4
+; CHECK-NEXT:    [[B:%.*]] = add i32 4, 2
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       Loop:
-; CHECK-NEXT:    call void @foo2(i32 6)
+; CHECK-NEXT:    call void @foo2(i32 [[B]])
 ; CHECK-NEXT:    br i1 [[C:%.*]], label [[LOOP]], label [[OUT:%.*]]
 ; CHECK:       Out:
-; CHECK-NEXT:    [[B_LCSSA:%.*]] = phi i32 [ 6, [[LOOP]] ]
+; CHECK-NEXT:    [[B_LCSSA:%.*]] = phi i32 [ [[B]], [[LOOP]] ]
 ; CHECK-NEXT:    [[C:%.*]] = sub i32 [[A]], [[B_LCSSA]]
 ; CHECK-NEXT:    ret i32 [[C]]
 ;
Index: llvm/lib/Transforms/Scalar/LICM.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LICM.cpp
+++ llvm/lib/Transforms/Scalar/LICM.cpp
@@ -44,7 +44,6 @@
 #include "llvm/Analysis/AliasSetTracker.h"
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/CaptureTracking.h"
-#include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/GuardUtils.h"
 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
 #include "llvm/Analysis/Loads.h"
@@ -891,21 +890,6 @@
       continue;
 
     for (Instruction &I : llvm::make_early_inc_range(*BB)) {
-      // Try constant folding this instruction.  If all the operands are
-      // constants, it is technically hoistable, but it would be better to
-      // just fold it.
-      if (Constant *C = ConstantFoldInstruction(
-              &I, I.getModule()->getDataLayout(), TLI)) {
-        LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << "  --> " << *C
-                          << '\n');
-        // FIXME MSSA: Such replacements may make accesses unoptimized (D51960).
-        I.replaceAllUsesWith(C);
-        if (isInstructionTriviallyDead(&I, TLI))
-          eraseInstruction(I, *SafetyInfo, MSSAU);
-        Changed = true;
-        continue;
-      }
-
       // Try hoisting the instruction out to the preheader.  We can only do
       // this if all of the operands of the instruction are loop invariant and
       // if it is safe to hoist the instruction. We also check block frequency


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149134.516698.patch
Type: text/x-patch
Size: 4348 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/64db752b/attachment.bin>


More information about the llvm-commits mailing list