[llvm-commits] [llvm] r112669 - in /llvm/trunk: lib/Transforms/Scalar/LICM.cpp test/Transforms/LICM/hoisting.ll test/Transforms/LICM/scalar_promote.ll

Chris Lattner sabre at nondot.org
Tue Aug 31 16:00:16 PDT 2010


Author: lattner
Date: Tue Aug 31 18:00:16 2010
New Revision: 112669

URL: http://llvm.org/viewvc/llvm-project?rev=112669&view=rev
Log:
licm is wasting time hoisting constant foldable operations,
instead of hoisting them, just fold them away.  This occurs in the
testcase for PR8041, for example.

Modified:
    llvm/trunk/lib/Transforms/Scalar/LICM.cpp
    llvm/trunk/test/Transforms/LICM/hoisting.ll
    llvm/trunk/test/Transforms/LICM/scalar_promote.ll

Modified: llvm/trunk/lib/Transforms/Scalar/LICM.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LICM.cpp?rev=112669&r1=112668&r2=112669&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LICM.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LICM.cpp Tue Aug 31 18:00:16 2010
@@ -36,11 +36,11 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/Instructions.h"
-#include "llvm/Target/TargetData.h"
-#include "llvm/Analysis/LoopInfo.h"
-#include "llvm/Analysis/LoopPass.h"
 #include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Analysis/AliasSetTracker.h"
+#include "llvm/Analysis/ConstantFolding.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/LoopPass.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Transforms/Utils/Local.h"
@@ -353,6 +353,18 @@
     for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
       Instruction &I = *II++;
 
+      // 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)) {
+        DEBUG(dbgs() << "LICM folding inst: " << I << "  --> " << *C << '\n');
+        CurAST->copyValue(&I, C);
+        CurAST->deleteValue(&I);
+        I.replaceAllUsesWith(C);
+        I.eraseFromParent();
+        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.
@@ -360,7 +372,7 @@
       if (isLoopInvariantInst(I) && canSinkOrHoistInst(I) &&
           isSafeToExecuteUnconditionally(I))
         hoist(I);
-      }
+    }
 
   const std::vector<DomTreeNode*> &Children = N->getChildren();
   for (unsigned i = 0, e = Children.size(); i != e; ++i)

Modified: llvm/trunk/test/Transforms/LICM/hoisting.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LICM/hoisting.ll?rev=112669&r1=112668&r2=112669&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LICM/hoisting.ll (original)
+++ llvm/trunk/test/Transforms/LICM/hoisting.ll Tue Aug 31 18:00:16 2010
@@ -48,3 +48,19 @@
 	%C = sub i32 %A, %B		; <i32> [#uses=1]
 	ret i32 %C
 }
+
+
+; This loop invariant instruction should be constant folded, not hoisted.
+define i32 @test3(i1 %c) {
+; CHECK: define i32 @test3
+; CHECK: call void @foo2(i32 6)
+	%A = load i32* @X		; <i32> [#uses=2]
+	br label %Loop
+Loop:
+	%B = add i32 4, 2		; <i32> [#uses=2]
+	call void @foo2( i32 %B )
+	br i1 %c, label %Loop, label %Out
+Out:		; preds = %Loop
+	%C = sub i32 %A, %B		; <i32> [#uses=1]
+	ret i32 %C
+}

Modified: llvm/trunk/test/Transforms/LICM/scalar_promote.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LICM/scalar_promote.ll?rev=112669&r1=112668&r2=112669&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LICM/scalar_promote.ll (original)
+++ llvm/trunk/test/Transforms/LICM/scalar_promote.ll Tue Aug 31 18:00:16 2010
@@ -1,4 +1,6 @@
 ; RUN: opt < %s  -licm -S | FileCheck %s
+target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
+
 @X = global i32 7		; <i32*> [#uses=4]
 
 define void @test1(i32 %i) {
@@ -32,23 +34,21 @@
 	br label %Loop
 ; CHECK: @test2
 ; CHECK: Entry:
-; CHECK-NEXT:  %X1 = getelementptr i32* @X, i64 0 
-; CHECK-NEXT:    %X2 = getelementptr i32* @X, i64 0
-; CHECK-NEXT:    %X1.promoted = load i32* %X1 
+; CHECK-NEXT:    %.promoted = load i32* getelementptr inbounds (i32* @X, i64 1)
 ; CHECK-NEXT:    br label %Loop
 
 Loop:		; preds = %Loop, %0
-	%X1 = getelementptr i32* @X, i64 0		; <i32*> [#uses=1]
+	%X1 = getelementptr i32* @X, i64 1		; <i32*> [#uses=1]
 	%A = load i32* %X1		; <i32> [#uses=1]
 	%V = add i32 %A, 1		; <i32> [#uses=1]
-	%X2 = getelementptr i32* @X, i64 0		; <i32*> [#uses=1]
+	%X2 = getelementptr i32* @X, i64 1		; <i32*> [#uses=1]
 	store i32 %V, i32* %X2
 	br i1 false, label %Loop, label %Exit
 
 Exit:		; preds = %Loop
 	ret void
 ; CHECK: Exit:
-; CHECK-NEXT:   store i32 %V, i32* %X1
+; CHECK-NEXT:   store i32 %V, i32* getelementptr inbounds (i32* @X, i64 1)
 ; CHECK-NEXT:   ret void
 }
 





More information about the llvm-commits mailing list