mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-06 23:32:27 +00:00
Improve tail call elimination to handle the switch statement.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86403 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
bf992817f2
commit
f80fcd00b3
@ -406,22 +406,6 @@ return: ; preds = %then.1, %else.0, %then.0
|
||||
|
||||
//===---------------------------------------------------------------------===//
|
||||
|
||||
Tail recursion elimination is not transforming this function, because it is
|
||||
returning n, which fails the isDynamicConstant check in the accumulator
|
||||
recursion checks.
|
||||
|
||||
long long fib(const long long n) {
|
||||
switch(n) {
|
||||
case 0:
|
||||
case 1:
|
||||
return n;
|
||||
default:
|
||||
return fib(n-1) + fib(n-2);
|
||||
}
|
||||
}
|
||||
|
||||
//===---------------------------------------------------------------------===//
|
||||
|
||||
Tail recursion elimination should handle:
|
||||
|
||||
int pow2m1(int n) {
|
||||
|
@ -234,7 +234,7 @@ bool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) {
|
||||
// We currently handle static constants and arguments that are not modified as
|
||||
// part of the recursion.
|
||||
//
|
||||
static bool isDynamicConstant(Value *V, CallInst *CI) {
|
||||
static bool isDynamicConstant(Value *V, CallInst *CI, ReturnInst *RI) {
|
||||
if (isa<Constant>(V)) return true; // Static constants are always dyn consts
|
||||
|
||||
// Check to see if this is an immutable argument, if so, the value
|
||||
@ -252,6 +252,15 @@ static bool isDynamicConstant(Value *V, CallInst *CI) {
|
||||
if (CI->getOperand(ArgNo+1) == Arg)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Switch cases are always constant integers. If the value is being switched
|
||||
// on and the return is only reachable from one of its cases, it's
|
||||
// effectively constant.
|
||||
if (BasicBlock *UniquePred = RI->getParent()->getUniquePredecessor())
|
||||
if (SwitchInst *SI = dyn_cast<SwitchInst>(UniquePred->getTerminator()))
|
||||
if (SI->getCondition() == V)
|
||||
return SI->getDefaultDest() != RI->getParent();
|
||||
|
||||
// Not a constant or immutable argument, we can't safely transform.
|
||||
return false;
|
||||
}
|
||||
@ -273,7 +282,7 @@ static Value *getCommonReturnValue(ReturnInst *TheRI, CallInst *CI) {
|
||||
// evaluatable at the start of the initial invocation of the function,
|
||||
// instead of at the end of the evaluation.
|
||||
//
|
||||
if (!isDynamicConstant(RetOp, CI))
|
||||
if (!isDynamicConstant(RetOp, CI, RI))
|
||||
return 0;
|
||||
|
||||
if (ReturnedValue && RetOp != ReturnedValue)
|
||||
|
34
test/Transforms/TailCallElim/switch.ll
Normal file
34
test/Transforms/TailCallElim/switch.ll
Normal file
@ -0,0 +1,34 @@
|
||||
; RUN: opt %s -tailcallelim -S | FileCheck %s
|
||||
|
||||
define i64 @fib(i64 %n) nounwind readnone {
|
||||
; CHECK: @fib
|
||||
entry:
|
||||
; CHECK: tailrecurse:
|
||||
; CHECK: %accumulator.tr = phi i64 [ %n, %entry ], [ %3, %bb1 ]
|
||||
; CHECK: %n.tr = phi i64 [ %n, %entry ], [ %2, %bb1 ]
|
||||
switch i64 %n, label %bb1 [
|
||||
; CHECK: switch i64 %n.tr, label %bb1 [
|
||||
i64 0, label %bb2
|
||||
i64 1, label %bb2
|
||||
]
|
||||
|
||||
bb1:
|
||||
; CHECK: bb1:
|
||||
%0 = add i64 %n, -1
|
||||
; CHECK: %0 = add i64 %n.tr, -1
|
||||
%1 = tail call i64 @fib(i64 %0) nounwind
|
||||
; CHECK: %1 = tail call i64 @fib(i64 %0)
|
||||
%2 = add i64 %n, -2
|
||||
; CHECK: %2 = add i64 %n.tr, -2
|
||||
%3 = tail call i64 @fib(i64 %2) nounwind
|
||||
; CHECK-NOT: tail call i64 @fib
|
||||
%4 = add nsw i64 %3, %1
|
||||
; CHECK: add nsw i64 %accumulator.tr, %1
|
||||
ret i64 %4
|
||||
; CHECK: br label %tailrecurse
|
||||
|
||||
bb2:
|
||||
; CHECK: bb2:
|
||||
ret i64 %n
|
||||
; CHECK: ret i64 %accumulator.tr
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user