llvm.org GIT mirror llvm / beeec32
Fix tail recursion elimination When the BasicBlock containing the return instrution has a PHI with 2 incoming values, FoldReturnIntoUncondBranch will remove the no longer used incoming value and remove the no longer needed phi as well. This leaves us with a BB that no longer has a PHI, but the subsequent call to FoldReturnIntoUncondBranch from FoldReturnAndProcessPred will not remove the return instruction (which still uses the result of the call instruction). This prevents EliminateRecursiveTailCall to remove the value, as it is still being used in a basicblock which has no predecessors. The basicblock can not be erased on the spot, because its iterator is still being used in runTRE. This issue was exposed when removing the threshold on size for lifetime marker insertion for named temporaries in clang. The testcase is a much reduced version of peelOffOuterExpr(const Expr*, const ExplodedNode *) from clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222354 91177308-0d34-0410-b5e6-96231b3b80d8 Arnaud A. de Grandmaison 4 years ago
2 changed file(s) with 51 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
403403 // alloca' is changed from being a static alloca to being a dynamic alloca.
404404 // Until this is resolved, disable this transformation if that would ever
405405 // happen. This bug is PR962.
406 SmallVector BBToErase;
406407 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
407408 if (ReturnInst *Ret = dyn_cast(BB->getTerminator())) {
408409 bool Change = ProcessReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
409410 ArgumentPHIs, !CanTRETailMarkedCall);
410 if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
411 if (!Change && BB->getFirstNonPHIOrDbg() == Ret) {
411412 Change = FoldReturnAndProcessPred(BB, Ret, OldEntry,
412413 TailCallsAreMarkedTail, ArgumentPHIs,
413414 !CanTRETailMarkedCall);
415 // FoldReturnAndProcessPred may have emptied some BB. Remember to
416 // erase them.
417 if (Change && BB->empty())
418 BBToErase.push_back(BB);
419
420 }
414421 MadeChange |= Change;
415422 }
416423 }
424
425 for (auto BB: BBToErase)
426 BB->eraseFromParent();
417427
418428 // If we eliminated any tail recursions, it's possible that we inserted some
419429 // silly PHI nodes which just merge an initial value (the incoming operand)
822832 if (CallInst *CI = FindTRECandidate(BI, CannotTailCallElimCallsMarkedTail)){
823833 DEBUG(dbgs() << "FOLDING: " << *BB
824834 << "INTO UNCOND BRANCH PRED: " << *Pred);
825 EliminateRecursiveTailCall(CI, FoldReturnIntoUncondBranch(Ret, BB, Pred),
826 OldEntry, TailCallsAreMarkedTail, ArgumentPHIs,
835 ReturnInst *RI = FoldReturnIntoUncondBranch(Ret, BB, Pred);
836
837 // Cleanup: if all predecessors of BB have been eliminated by
838 // FoldReturnIntoUncondBranch, we would like to delete it, but we
839 // can not just nuke it as it is being used as an iterator by our caller.
840 // Just empty it, and the caller will erase it when it is safe to do so.
841 // It is important to empty it, because the ret instruction in there is
842 // still using a value which EliminateRecursiveTailCall will attempt
843 // to remove.
844 if (!BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB))
845 BB->getInstList().clear();
846
847 EliminateRecursiveTailCall(CI, RI, OldEntry, TailCallsAreMarkedTail,
848 ArgumentPHIs,
827849 CannotTailCallElimCallsMarkedTail);
828850 ++NumRetDuped;
829851 Change = true;
0 ; RUN: opt -tailcallelim -S < %s 2>&1 | FileCheck %s
1
2 ; CHECK: add nsw i32
3 ; CHECK-NEXT: br label
4 ; CHECK: add nsw i32
5 ; CHECK-NEXT: br label
6 ; CHECK-NOT: Uses remain when a value is destroyed
7 define i32 @test(i32 %n) {
8 entry:
9 %cmp = icmp slt i32 %n, 2
10 br i1 %cmp, label %if.then, label %if.else
11
12 if.then: ; preds = %entry
13 %v1 = add nsw i32 %n, -2
14 %call1 = tail call i32 @test(i32 %v1)
15 br label %return
16
17 if.else: ; preds = %entry
18 %v2 = add nsw i32 %n, 4
19 %call2 = tail call i32 @test(i32 %v2)
20 br label %return
21
22 return: ; preds = %if.end, %if.else
23 %retval = phi i32 [ %call1, %if.then ], [ %call2, %if.else ]
24 ret i32 %retval
25 }