Revert r166407 because it caused analyzer tests to crash and broke self-host bots.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166424 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Argyrios Kyrtzidis 2012-10-22 18:16:14 +00:00
parent e29c19091c
commit 0b06e2331e
2 changed files with 58 additions and 84 deletions

View File

@ -69,7 +69,6 @@
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ValueHandle.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
using namespace llvm; using namespace llvm;
@ -118,45 +117,34 @@ FunctionPass *llvm::createTailCallEliminationPass() {
return new TailCallElim(); return new TailCallElim();
} }
/// CanTRE - Scan the specified basic block for alloca instructions. /// AllocaMightEscapeToCalls - Return true if this alloca may be accessed by
/// If it contains any that are variable-sized or not in the entry block, /// callees of this function. We only do very simple analysis right now, this
/// returns false. /// could be expanded in the future to use mod/ref information for particular
static bool CanTRE(AllocaInst *AI) { /// call sites if desired.
// Because of PR962, we don't TRE allocas outside the entry block. static bool AllocaMightEscapeToCalls(AllocaInst *AI) {
// FIXME: do simple 'address taken' analysis.
// If this alloca is in the body of the function, or if it is a variable return true;
// sized allocation, we cannot tail call eliminate calls marked 'tail'
// with this mechanism.
BasicBlock *BB = AI->getParent();
return BB == &BB->getParent()->getEntryBlock() &&
isa<ConstantInt>(AI->getArraySize());
} }
struct AllocaCaptureTracker : public CaptureTracker { /// CheckForEscapingAllocas - Scan the specified basic block for alloca
AllocaCaptureTracker() : Captured(false) {} /// instructions. If it contains any that might be accessed by calls, return
/// true.
static bool CheckForEscapingAllocas(BasicBlock *BB,
bool &CannotTCETailMarkedCall) {
bool RetVal = false;
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
RetVal |= AllocaMightEscapeToCalls(AI);
void tooManyUses() { Captured = true; } // If this alloca is in the body of the function, or if it is a variable
// sized allocation, we cannot tail call eliminate calls marked 'tail'
bool shouldExplore(Use *U) { // with this mechanism.
Value *V = U->getUser(); if (BB != &BB->getParent()->getEntryBlock() ||
if (isa<CallInst>(V) || isa<InvokeInst>(V)) !isa<ConstantInt>(AI->getArraySize()))
UsesAlloca.push_back(V); CannotTCETailMarkedCall = true;
}
return true; return RetVal;
} }
bool captured(Use *U) {
if (isa<ReturnInst>(U->getUser()))
return false;
Captured = true;
return true;
}
SmallVector<WeakVH, 64> UsesAlloca;
bool Captured;
};
bool TailCallElim::runOnFunction(Function &F) { bool TailCallElim::runOnFunction(Function &F) {
// If this function is a varargs function, we won't be able to PHI the args // If this function is a varargs function, we won't be able to PHI the args
@ -169,34 +157,38 @@ bool TailCallElim::runOnFunction(Function &F) {
bool MadeChange = false; bool MadeChange = false;
bool FunctionContainsEscapingAllocas = false; bool FunctionContainsEscapingAllocas = false;
// CanTRETailMarkedCall - If false, we cannot perform TRE on tail calls // CannotTCETailMarkedCall - If true, we cannot perform TCE on tail calls
// marked with the 'tail' attribute, because doing so would cause the stack // marked with the 'tail' attribute, because doing so would cause the stack
// size to increase (real TRE would deallocate variable sized allocas, TRE // size to increase (real TCE would deallocate variable sized allocas, TCE
// doesn't). // doesn't).
bool CanTRETailMarkedCall = true; bool CannotTCETailMarkedCall = false;
// Find calls that can be marked tail. // Loop over the function, looking for any returning blocks, and keeping track
AllocaCaptureTracker ACT; // of whether this function has any non-trivially used allocas.
for (Function::iterator BB = F.begin(), EE = F.end(); BB != EE; ++BB) { for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { if (FunctionContainsEscapingAllocas && CannotTCETailMarkedCall)
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { break;
CanTRETailMarkedCall &= CanTRE(AI);
PointerMayBeCaptured(AI, &ACT); FunctionContainsEscapingAllocas |=
if (ACT.Captured) CheckForEscapingAllocas(BB, CannotTCETailMarkedCall);
return false;
}
}
} }
// Second pass, change any tail recursive calls to loops. /// FIXME: The code generator produces really bad code when an 'escaping
/// alloca' is changed from being a static alloca to being a dynamic alloca.
/// Until this is resolved, disable this transformation if that would ever
/// happen. This bug is PR962.
if (FunctionContainsEscapingAllocas)
return false;
// Second pass, change any tail calls to loops.
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) { if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) {
bool Change = ProcessReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail, bool Change = ProcessReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
ArgumentPHIs, !CanTRETailMarkedCall); ArgumentPHIs,CannotTCETailMarkedCall);
if (!Change && BB->getFirstNonPHIOrDbg() == Ret) if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
Change = FoldReturnAndProcessPred(BB, Ret, OldEntry, Change = FoldReturnAndProcessPred(BB, Ret, OldEntry,
TailCallsAreMarkedTail, ArgumentPHIs, TailCallsAreMarkedTail, ArgumentPHIs,
!CanTRETailMarkedCall); CannotTCETailMarkedCall);
MadeChange |= Change; MadeChange |= Change;
} }
} }
@ -218,24 +210,21 @@ bool TailCallElim::runOnFunction(Function &F) {
} }
} }
// Finally, if this function contains no non-escaping allocas and doesn't // Finally, if this function contains no non-escaping allocas, or calls
// call setjmp, mark all calls in the function as eligible for tail calls // setjmp, mark all calls in the function as eligible for tail calls
// (there is no stack memory for them to access). //(there is no stack memory for them to access).
std::sort(ACT.UsesAlloca.begin(), ACT.UsesAlloca.end());
if (!FunctionContainsEscapingAllocas && !F.callsFunctionThatReturnsTwice()) if (!FunctionContainsEscapingAllocas && !F.callsFunctionThatReturnsTwice())
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
if (CallInst *CI = dyn_cast<CallInst>(I)) if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (!std::binary_search(ACT.UsesAlloca.begin(), ACT.UsesAlloca.end(), CI->setTailCall();
CI)) { MadeChange = true;
CI->setTailCall(); }
MadeChange = true;
}
return MadeChange; return MadeChange;
} }
/// CanMoveAboveCall - Return true if it is safe to move the specified /// CanMoveAboveCall - Return true if it is safe to move the specified
/// instruction from after the call to before the call, assuming that all /// instruction from after the call to before the call, assuming that all
/// instructions between the call and this instruction are movable. /// instructions between the call and this instruction are movable.

View File

@ -1,9 +1,9 @@
; RUN: opt %s -tailcallelim -S | FileCheck %s ; RUN: opt %s -tailcallelim -S | FileCheck %s
; XFAIL: *
declare void @use(i8* nocapture, i8* nocapture) declare void @use(i8* nocapture, i8* nocapture)
declare void @boring()
define i8* @test1(i8* nocapture %A, i1 %cond) { define i8* @foo(i8* nocapture %A, i1 %cond) {
; CHECK: tailrecurse: ; CHECK: tailrecurse:
; CHECK: %A.tr = phi i8* [ %A, %0 ], [ %B, %cond_true ] ; CHECK: %A.tr = phi i8* [ %A, %0 ], [ %B, %cond_true ]
; CHECK: %cond.tr = phi i1 [ %cond, %0 ], [ false, %cond_true ] ; CHECK: %cond.tr = phi i1 [ %cond, %0 ], [ false, %cond_true ]
@ -14,27 +14,12 @@ define i8* @test1(i8* nocapture %A, i1 %cond) {
cond_true: cond_true:
; CHECK: cond_true: ; CHECK: cond_true:
; CHECK: br label %tailrecurse ; CHECK: br label %tailrecurse
call i8* @test1(i8* %B, i1 false) call i8* @foo(i8* %B, i1 false)
ret i8* null ret i8* null
cond_false: cond_false:
; CHECK: cond_false ; CHECK: cond_false
call void @use(i8* %A, i8* %B) call void @use(i8* %A, i8* %B)
; CHECK: call void @use(i8* %A.tr, i8* %B) ; CHECK: tail call void @use(i8* %A.tr, i8* %B)
call void @boring()
; CHECK: tail call void @boring()
ret i8* null ret i8* null
; CHECK: ret i8* null ; CHECK: ret i8* null
} }
; PR14143
define void @test2(i8* %a, i8* %b) {
; CHECK: @test2
; CHECK-NOT: tail call
; CHECK: ret void
%c = alloca [100 x i8], align 16
%tmp = bitcast [100 x i8]* %c to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %b, i8* %tmp, i64 100, i32 1, i1 false)
ret void
}
declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1)