mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-13 04:30:23 +00:00
IndVarSimplify: do not recompute an IV value outside of the loop if :
- it is trivially known to be used inside the loop in a way that can not be optimized away - there is no use outside of the loop which can take advantage of the computation hoisting git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@177432 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
dff4d1522a
commit
eb9a42e8ab
@ -535,6 +535,45 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) {
|
||||
if (!SE->isLoopInvariant(ExitValue, L))
|
||||
continue;
|
||||
|
||||
// Computing the value outside of the loop brings no benefit if :
|
||||
// - it is definitely used inside the loop in a way which can not be
|
||||
// optimized away.
|
||||
// - no use outside of the loop can take advantage of hoisting the
|
||||
// computation out of the loop
|
||||
if (ExitValue->getSCEVType()>=scMulExpr) {
|
||||
unsigned NumHardInternalUses = 0;
|
||||
unsigned NumSoftExternalUses = 0;
|
||||
unsigned NumUses = 0;
|
||||
for (Value::use_iterator IB=Inst->use_begin(), IE=Inst->use_end();
|
||||
IB!=IE && NumUses<=6 ; ++IB) {
|
||||
Instruction *UseInstr = cast<Instruction>(*IB);
|
||||
unsigned Opc = UseInstr->getOpcode();
|
||||
NumUses++;
|
||||
if (L->contains(UseInstr)) {
|
||||
if (Opc == Instruction::Call || Opc == Instruction::Ret)
|
||||
NumHardInternalUses++;
|
||||
} else {
|
||||
if (Opc == Instruction::PHI) {
|
||||
// Do not count the Phi as a use. LCSSA may have inserted
|
||||
// plenty of trivial ones.
|
||||
NumUses--;
|
||||
for (Value::use_iterator PB=UseInstr->use_begin(),
|
||||
PE=UseInstr->use_end();
|
||||
PB!=PE && NumUses<=6 ; ++PB, ++NumUses) {
|
||||
unsigned PhiOpc = cast<Instruction>(*PB)->getOpcode();
|
||||
if (PhiOpc != Instruction::Call && PhiOpc != Instruction::Ret)
|
||||
NumSoftExternalUses++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (Opc != Instruction::Call && Opc != Instruction::Ret)
|
||||
NumSoftExternalUses++;
|
||||
}
|
||||
}
|
||||
if (NumUses <= 6 && NumHardInternalUses && !NumSoftExternalUses)
|
||||
continue;
|
||||
}
|
||||
|
||||
Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
|
||||
|
||||
DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
|
||||
|
69
test/Transforms/IndVarSimplify/dont-recompute.ll
Normal file
69
test/Transforms/IndVarSimplify/dont-recompute.ll
Normal file
@ -0,0 +1,69 @@
|
||||
; RUN: opt < %s -indvars -S | FileCheck %s
|
||||
|
||||
; This tests that the IV is not recomputed outside of the loop when it is known
|
||||
; to be computed by the loop and used in the loop any way. In the example below
|
||||
; although a's value can be computed outside of the loop, there is no benefit
|
||||
; in doing so as it has to be computed by the loop anyway.
|
||||
;
|
||||
; extern void func(unsigned val);
|
||||
;
|
||||
; void test(unsigned m)
|
||||
; {
|
||||
; unsigned a = 0;
|
||||
;
|
||||
; for (int i=0; i<186; i++) {
|
||||
; a += m;
|
||||
; func(a);
|
||||
; }
|
||||
;
|
||||
; func(a);
|
||||
; }
|
||||
|
||||
declare void @func(i32)
|
||||
|
||||
; CHECK: @test
|
||||
define void @test(i32 %m) nounwind uwtable {
|
||||
entry:
|
||||
br label %for.body
|
||||
|
||||
for.body: ; preds = %for.body, %entry
|
||||
%i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
|
||||
%a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
|
||||
%add = add i32 %a.05, %m
|
||||
; CHECK: tail call void @func(i32 %add)
|
||||
tail call void @func(i32 %add)
|
||||
%inc = add nsw i32 %i.06, 1
|
||||
%exitcond = icmp eq i32 %inc, 186
|
||||
br i1 %exitcond, label %for.end, label %for.body
|
||||
|
||||
for.end: ; preds = %for.body
|
||||
; CHECK: for.end:
|
||||
; CHECK-NOT: mul i32 %m, 186
|
||||
; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
|
||||
; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
|
||||
tail call void @func(i32 %add)
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: @test2
|
||||
define i32 @test2(i32 %m) nounwind uwtable {
|
||||
entry:
|
||||
br label %for.body
|
||||
|
||||
for.body: ; preds = %for.body, %entry
|
||||
%i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
|
||||
%a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
|
||||
%add = add i32 %a.05, %m
|
||||
; CHECK: tail call void @func(i32 %add)
|
||||
tail call void @func(i32 %add)
|
||||
%inc = add nsw i32 %i.06, 1
|
||||
%exitcond = icmp eq i32 %inc, 186
|
||||
br i1 %exitcond, label %for.end, label %for.body
|
||||
|
||||
for.end: ; preds = %for.body
|
||||
; CHECK: for.end:
|
||||
; CHECK-NOT: mul i32 %m, 186
|
||||
; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
|
||||
; CHECK-NEXT: ret i32 %add.lcssa
|
||||
ret i32 %add
|
||||
}
|
Loading…
Reference in New Issue
Block a user