llvm.org GIT mirror llvm / 2ac6e23
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112669 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
3 changed file(s) with 38 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
3535 #include "llvm/DerivedTypes.h"
3636 #include "llvm/IntrinsicInst.h"
3737 #include "llvm/Instructions.h"
38 #include "llvm/Target/TargetData.h"
38 #include "llvm/Analysis/AliasAnalysis.h"
39 #include "llvm/Analysis/AliasSetTracker.h"
40 #include "llvm/Analysis/ConstantFolding.h"
3941 #include "llvm/Analysis/LoopInfo.h"
4042 #include "llvm/Analysis/LoopPass.h"
41 #include "llvm/Analysis/AliasAnalysis.h"
42 #include "llvm/Analysis/AliasSetTracker.h"
4343 #include "llvm/Analysis/Dominators.h"
4444 #include "llvm/Analysis/ScalarEvolution.h"
4545 #include "llvm/Transforms/Utils/Local.h"
352352 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
353353 Instruction &I = *II++;
354354
355 // Try constant folding this instruction. If all the operands are
356 // constants, it is technically hoistable, but it would be better to just
357 // fold it.
358 if (Constant *C = ConstantFoldInstruction(&I)) {
359 DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n');
360 CurAST->copyValue(&I, C);
361 CurAST->deleteValue(&I);
362 I.replaceAllUsesWith(C);
363 I.eraseFromParent();
364 continue;
365 }
366
355367 // Try hoisting the instruction out to the preheader. We can only do this
356368 // if all of the operands of the instruction are loop invariant and if it
357369 // is safe to hoist the instruction.
359371 if (isLoopInvariantInst(I) && canSinkOrHoistInst(I) &&
360372 isSafeToExecuteUnconditionally(I))
361373 hoist(I);
362 }
374 }
363375
364376 const std::vector &Children = N->getChildren();
365377 for (unsigned i = 0, e = Children.size(); i != e; ++i)
4747 %C = sub i32 %A, %B ; [#uses=1]
4848 ret i32 %C
4949 }
50
51
52 ; This loop invariant instruction should be constant folded, not hoisted.
53 define i32 @test3(i1 %c) {
54 ; CHECK: define i32 @test3
55 ; CHECK: call void @foo2(i32 6)
56 %A = load i32* @X ; [#uses=2]
57 br label %Loop
58 Loop:
59 %B = add i32 4, 2 ; [#uses=2]
60 call void @foo2( i32 %B )
61 br i1 %c, label %Loop, label %Out
62 Out: ; preds = %Loop
63 %C = sub i32 %A, %B ; [#uses=1]
64 ret i32 %C
65 }
0 ; RUN: opt < %s -licm -S | FileCheck %s
1 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"
2
13 @X = global i32 7 ; [#uses=4]
24
35 define void @test1(i32 %i) {
3133 br label %Loop
3234 ; CHECK: @test2
3335 ; CHECK: Entry:
34 ; CHECK-NEXT: %X1 = getelementptr i32* @X, i64 0
35 ; CHECK-NEXT: %X2 = getelementptr i32* @X, i64 0
36 ; CHECK-NEXT: %X1.promoted = load i32* %X1
36 ; CHECK-NEXT: %.promoted = load i32* getelementptr inbounds (i32* @X, i64 1)
3737 ; CHECK-NEXT: br label %Loop
3838
3939 Loop: ; preds = %Loop, %0
40 %X1 = getelementptr i32* @X, i64 0 ; [#uses=1]
40 %X1 = getelementptr i32* @X, i64 1 ; [#uses=1]
4141 %A = load i32* %X1 ; [#uses=1]
4242 %V = add i32 %A, 1 ; [#uses=1]
43 %X2 = getelementptr i32* @X, i64 0 ; [#uses=1]
43 %X2 = getelementptr i32* @X, i64 1 ; [#uses=1]
4444 store i32 %V, i32* %X2
4545 br i1 false, label %Loop, label %Exit
4646
4747 Exit: ; preds = %Loop
4848 ret void
4949 ; CHECK: Exit:
50 ; CHECK-NEXT: store i32 %V, i32* %X1
50 ; CHECK-NEXT: store i32 %V, i32* getelementptr inbounds (i32* @X, i64 1)
5151 ; CHECK-NEXT: ret void
5252 }
5353