12240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//===- TailRecursionElimination.cpp - Eliminate Tail Calls ----------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
92240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//
107152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner// This file transforms calls of the current function (self recursion) followed
117152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner// by a return instruction with a branch to the entry of the function, creating
127152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner// a loop.  This pass also implements the following extensions to the basic
137152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner// algorithm:
142240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//
157152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//  1. Trivial instructions between the call and return do not prevent the
167152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     transformation from taking place, though currently the analysis cannot
177152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     support moving any really useful instructions (only dead ones).
18543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner//  2. This pass transforms functions that are prevented from being tail
1924080a98786cf393740cd035e27dd16c12827892Duncan Sands//     recursive by an associative and commutative expression to use an
2024080a98786cf393740cd035e27dd16c12827892Duncan Sands//     accumulator variable, thus compiling the typical naive factorial or
2124080a98786cf393740cd035e27dd16c12827892Duncan Sands//     'fib' implementation into efficient code.
22d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//  3. TRE is performed if the function returns void, if the return
23d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//     returns the result returned by the call, or if the function returns a
24d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//     run-time constant on all exits from the function.  It is possible, though
25d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//     unlikely, that the return returns something else (like constant 0), and
26d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//     can still be TRE'd.  It can be TRE'd if ALL OTHER return instructions in
27d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//     the function return the exact same value.
280cade261322242132905914f613d25d16e1a9ff6Nick Lewycky//  4. If it can prove that callees do not access their caller stack frame,
297f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner//     they are marked as eligible for tail call elimination (by the code
307f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner//     generator).
312240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//
327152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner// There are several improvements that could be made:
337152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//
347152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//  1. If the function has any alloca instructions, these instructions will be
357152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     moved out of the entry block of the function, causing them to be
367152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     evaluated each time through the tail recursion.  Safely keeping allocas
377152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     in the entry block requires analysis to proves that the tail-called
387152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     function does not read or write the stack object.
397a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner//  2. Tail recursion is only performed if the call immediately precedes the
407152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     return instruction.  It's possible that there could be a jump between
417152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     the call and the return.
42d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//  3. There can be intervening operations between the call and the return that
437152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     prevent the TRE from occurring.  For example, there could be GEP's and
447152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     stores to memory that will not be read or written by the call.  This
457152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     requires some substantial analysis (such as with DSA) to prove safe to
467152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     move ahead of the call, but doing so could allow many more TREs to be
477152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner//     performed, for example in TreeAdd/TreeAlloc from the treeadd benchmark.
487f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner//  4. The algorithm we use to detect if callees access their caller stack
497f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner//     frames is very primitive.
502240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//
512240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner//===----------------------------------------------------------------------===//
522240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner
530e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattner#define DEBUG_TYPE "tailcallelim"
543fc6ef1bb96d9a3194cef667a2d3cbc94e3fb189Chris Lattner#include "llvm/Transforms/Scalar.h"
55c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng#include "llvm/Transforms/Utils/BasicBlockUtils.h"
566a35b40250735a50efe66c88414cdd3b79185019Chris Lattner#include "llvm/Transforms/Utils/Local.h"
57ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner#include "llvm/Constants.h"
582240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner#include "llvm/DerivedTypes.h"
592240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner#include "llvm/Function.h"
602240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner#include "llvm/Instructions.h"
61c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng#include "llvm/IntrinsicInst.h"
620e00c6c561181be7e9cc75ad2fee9cd4cbbfca1eRafael Espindola#include "llvm/Module.h"
632240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner#include "llvm/Pass.h"
640cade261322242132905914f613d25d16e1a9ff6Nick Lewycky#include "llvm/Analysis/CaptureTracking.h"
65ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman#include "llvm/Analysis/InlineCost.h"
66cdbd99262286e96729007ac535cd430ecb3d38acDuncan Sands#include "llvm/Analysis/InstructionSimplify.h"
67dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/Analysis/Loads.h"
68ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman#include "llvm/Support/CallSite.h"
69543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner#include "llvm/Support/CFG.h"
70c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng#include "llvm/Support/Debug.h"
71337c0811381a48b75aec204d96090779fec6606eFrancois Pichet#include "llvm/Support/raw_ostream.h"
72551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
73c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng#include "llvm/ADT/STLExtras.h"
74f8485c643412dbff46fe87ea2867445169a5c28eChris Lattnerusing namespace llvm;
75d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
760e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumEliminated, "Number of tail calls removed");
7760f5ad46c2147af79035a43e5745e46376124780Evan ChengSTATISTIC(NumRetDuped,   "Number of return duplicated");
780e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumAccumAdded, "Number of accumulators introduced");
792240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner
800e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattnernamespace {
813e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  struct TailCallElim : public FunctionPass {
82ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
83081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    TailCallElim() : FunctionPass(ID) {
84081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeTailCallElimPass(*PassRegistry::getPassRegistry());
85081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
86794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
872240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner    virtual bool runOnFunction(Function &F);
887152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
897152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  private:
90c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    CallInst *FindTRECandidate(Instruction *I,
91c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                               bool CannotTailCallElimCallsMarkedTail);
92c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    bool EliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
93c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    BasicBlock *&OldEntry,
94c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    bool &TailCallsAreMarkedTail,
95c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    SmallVector<PHINode*, 8> &ArgumentPHIs,
96c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    bool CannotTailCallElimCallsMarkedTail);
97c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    bool FoldReturnAndProcessPred(BasicBlock *BB,
98c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                  ReturnInst *Ret, BasicBlock *&OldEntry,
99c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                  bool &TailCallsAreMarkedTail,
100c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                  SmallVector<PHINode*, 8> &ArgumentPHIs,
101c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                  bool CannotTailCallElimCallsMarkedTail);
1027152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    bool ProcessReturningBlock(ReturnInst *RI, BasicBlock *&OldEntry,
103ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner                               bool &TailCallsAreMarkedTail,
1040cade261322242132905914f613d25d16e1a9ff6Nick Lewycky                               SmallVector<PHINode*, 8> &ArgumentPHIs,
105ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner                               bool CannotTailCallElimCallsMarkedTail);
1067152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    bool CanMoveAboveCall(Instruction *I, CallInst *CI);
107543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    Value *CanTransformAccumulatorRecursion(Instruction *I, CallInst *CI);
1082240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  };
1092240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner}
1102240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner
111844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar TailCallElim::ID = 0;
112d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(TailCallElim, "tailcallelim",
113ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                "Tail Call Elimination", false, false)
114844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
115d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// Public interface to the TailCallElimination pass
116f8485c643412dbff46fe87ea2867445169a5c28eChris LattnerFunctionPass *llvm::createTailCallEliminationPass() {
117f8485c643412dbff46fe87ea2867445169a5c28eChris Lattner  return new TailCallElim();
118f8485c643412dbff46fe87ea2867445169a5c28eChris Lattner}
1193fc6ef1bb96d9a3194cef667a2d3cbc94e3fb189Chris Lattner
120cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky/// AllocaMightEscapeToCalls - Return true if this alloca may be accessed by
121cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky/// callees of this function.  We only do very simple analysis right now, this
122cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky/// could be expanded in the future to use mod/ref information for particular
123cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky/// call sites if desired.
124cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewyckystatic bool AllocaMightEscapeToCalls(AllocaInst *AI) {
125cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky  // FIXME: do simple 'address taken' analysis.
126cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky  return true;
127cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky}
128cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky
1290cade261322242132905914f613d25d16e1a9ff6Nick Lewycky/// CheckForEscapingAllocas - Scan the specified basic block for alloca
1307f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner/// instructions.  If it contains any that might be accessed by calls, return
1317f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner/// true.
132ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattnerstatic bool CheckForEscapingAllocas(BasicBlock *BB,
133ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner                                    bool &CannotTCETailMarkedCall) {
134ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  bool RetVal = false;
1357f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
136ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
137cb19438f63019eb557c1fbaeae8a6de78b03c584Nick Lewycky      RetVal |= AllocaMightEscapeToCalls(AI);
138ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
139ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      // If this alloca is in the body of the function, or if it is a variable
140ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      // sized allocation, we cannot tail call eliminate calls marked 'tail'
141ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      // with this mechanism.
142ecb7a77885b174cf4d001a9b48533b3979e7810dDan Gohman      if (BB != &BB->getParent()->getEntryBlock() ||
143ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner          !isa<ConstantInt>(AI->getArraySize()))
144ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner        CannotTCETailMarkedCall = true;
145ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    }
146ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  return RetVal;
1477f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner}
1487f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner
1492240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattnerbool TailCallElim::runOnFunction(Function &F) {
1502240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  // If this function is a varargs function, we won't be able to PHI the args
1512240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  // right, so don't even try to convert it...
1522240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  if (F.getFunctionType()->isVarArg()) return false;
1532240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner
1542240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  BasicBlock *OldEntry = 0;
155ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  bool TailCallsAreMarkedTail = false;
1560cade261322242132905914f613d25d16e1a9ff6Nick Lewycky  SmallVector<PHINode*, 8> ArgumentPHIs;
1572240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  bool MadeChange = false;
1587f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  bool FunctionContainsEscapingAllocas = false;
1597f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner
160ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // CannotTCETailMarkedCall - If true, we cannot perform TCE on tail calls
161ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // marked with the 'tail' attribute, because doing so would cause the stack
162ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // size to increase (real TCE would deallocate variable sized allocas, TCE
163ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // doesn't).
164ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  bool CannotTCETailMarkedCall = false;
165ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
1667f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  // Loop over the function, looking for any returning blocks, and keeping track
1677f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  // of whether this function has any non-trivially used allocas.
1687f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
169ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    if (FunctionContainsEscapingAllocas && CannotTCETailMarkedCall)
170ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      break;
17100b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
1724c0e4cdc40bbf94766242294fe386f4eb2d32d2eChris Lattner    FunctionContainsEscapingAllocas |=
173ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      CheckForEscapingAllocas(BB, CannotTCETailMarkedCall);
1747f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner  }
17532b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner
17632b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner  /// FIXME: The code generator produces really bad code when an 'escaping
17732b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner  /// alloca' is changed from being a static alloca to being a dynamic alloca.
17832b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner  /// Until this is resolved, disable this transformation if that would ever
17932b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner  /// happen.  This bug is PR962.
18032b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner  if (FunctionContainsEscapingAllocas)
18132b1e87f111d79f3f5b0e86c8f138c02bef8aa18Chris Lattner    return false;
182fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
183ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // Second pass, change any tail calls to loops.
184c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
185c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) {
186c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      bool Change = ProcessReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
187ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner                                          ArgumentPHIs,CannotTCETailMarkedCall);
188c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
189c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng        Change = FoldReturnAndProcessPred(BB, Ret, OldEntry,
190c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                          TailCallsAreMarkedTail, ArgumentPHIs,
191c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                          CannotTCETailMarkedCall);
192c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      MadeChange |= Change;
193c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    }
194c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  }
195ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
196cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  // If we eliminated any tail recursions, it's possible that we inserted some
197cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  // silly PHI nodes which just merge an initial value (the incoming operand)
198cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  // with themselves.  Check to see if we did and clean up our mess if so.  This
199cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  // occurs when a function passes an argument straight through to its tail
200cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  // call.
201cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  if (!ArgumentPHIs.empty()) {
202cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner    for (unsigned i = 0, e = ArgumentPHIs.size(); i != e; ++i) {
203cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner      PHINode *PN = ArgumentPHIs[i];
204cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner
205cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner      // If the PHI Node is a dynamic constant, replace it with the value it is.
206cdbd99262286e96729007ac535cd430ecb3d38acDuncan Sands      if (Value *PNV = SimplifyInstruction(PN)) {
207ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner        PN->replaceAllUsesWith(PNV);
208ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner        PN->eraseFromParent();
209cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner      }
210cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner    }
211cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner  }
212cf2f89251d45bc9783917874adb9029f71d50068Chris Lattner
2130e00c6c561181be7e9cc75ad2fee9cd4cbbfca1eRafael Espindola  // Finally, if this function contains no non-escaping allocas, or calls
2140e00c6c561181be7e9cc75ad2fee9cd4cbbfca1eRafael Espindola  // setjmp, mark all calls in the function as eligible for tail calls
2150e00c6c561181be7e9cc75ad2fee9cd4cbbfca1eRafael Espindola  //(there is no stack memory for them to access).
2160e00c6c561181be7e9cc75ad2fee9cd4cbbfca1eRafael Espindola  if (!FunctionContainsEscapingAllocas && !F.callsFunctionThatReturnsTwice())
2177f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner    for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2187f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
219febc81680c80a53f15f85b1812cba07fc179b9fdEvan Cheng        if (CallInst *CI = dyn_cast<CallInst>(I)) {
220febc81680c80a53f15f85b1812cba07fc179b9fdEvan Cheng          CI->setTailCall();
221febc81680c80a53f15f85b1812cba07fc179b9fdEvan Cheng          MadeChange = true;
222febc81680c80a53f15f85b1812cba07fc179b9fdEvan Cheng        }
2237f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner
2242240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner  return MadeChange;
2252240d2b3f7b9f5835868c83ce78d125d1b65212bChris Lattner}
2267152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
2277152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
228543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// CanMoveAboveCall - Return true if it is safe to move the specified
229543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// instruction from after the call to before the call, assuming that all
230543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// instructions between the call and this instruction are movable.
231543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner///
2327152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattnerbool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) {
2337152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // FIXME: We can move load/store/call/free instructions above the call if the
2347152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // call does not mod/ref the memory location being processed.
2356a35b40250735a50efe66c88414cdd3b79185019Chris Lattner  if (I->mayHaveSideEffects())  // This also handles volatile loads.
2367152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    return false;
2376a35b40250735a50efe66c88414cdd3b79185019Chris Lattner
2380cade261322242132905914f613d25d16e1a9ff6Nick Lewycky  if (LoadInst *L = dyn_cast<LoadInst>(I)) {
2396a35b40250735a50efe66c88414cdd3b79185019Chris Lattner    // Loads may always be moved above calls without side effects.
2406a35b40250735a50efe66c88414cdd3b79185019Chris Lattner    if (CI->mayHaveSideEffects()) {
2416a35b40250735a50efe66c88414cdd3b79185019Chris Lattner      // Non-volatile loads may be moved above a call with side effects if it
2426a35b40250735a50efe66c88414cdd3b79185019Chris Lattner      // does not write to memory and the load provably won't trap.
2436a35b40250735a50efe66c88414cdd3b79185019Chris Lattner      // FIXME: Writes to memory only matter if they may alias the pointer
2446a35b40250735a50efe66c88414cdd3b79185019Chris Lattner      // being loaded from.
2456a35b40250735a50efe66c88414cdd3b79185019Chris Lattner      if (CI->mayWriteToMemory() ||
24649db68fba01722ca032dc5170f8248a9d25f0199Bob Wilson          !isSafeToLoadUnconditionally(L->getPointerOperand(), L,
24749db68fba01722ca032dc5170f8248a9d25f0199Bob Wilson                                       L->getAlignment()))
2486a35b40250735a50efe66c88414cdd3b79185019Chris Lattner        return false;
2496a35b40250735a50efe66c88414cdd3b79185019Chris Lattner    }
2506a35b40250735a50efe66c88414cdd3b79185019Chris Lattner  }
2517152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
2527152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Otherwise, if this is a side-effect free instruction, check to make sure
2537152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // that it does not use the return value of the call.  If it doesn't use the
2547152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // return value of the call, it must only use things that are defined before
2557152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // the call, or movable instructions between the call and the instruction
2567152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // itself.
2577152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
2587152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    if (I->getOperand(i) == CI)
2597152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner      return false;
2607152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  return true;
2617152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner}
2627152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
263d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// isDynamicConstant - Return true if the specified value is the same when the
264d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// return would exit as it was when the initial iteration of the recursive
265d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// function was executed.
266d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//
267d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// We currently handle static constants and arguments that are not modified as
268d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// part of the recursion.
269d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//
270f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewyckystatic bool isDynamicConstant(Value *V, CallInst *CI, ReturnInst *RI) {
271d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  if (isa<Constant>(V)) return true; // Static constants are always dyn consts
272d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner
273d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // Check to see if this is an immutable argument, if so, the value
274d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // will be available to initialize the accumulator.
275d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  if (Argument *Arg = dyn_cast<Argument>(V)) {
276d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    // Figure out which argument number this is...
277d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    unsigned ArgNo = 0;
278d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    Function *F = CI->getParent()->getParent();
279e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::arg_iterator AI = F->arg_begin(); &*AI != Arg; ++AI)
280d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner      ++ArgNo;
281fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
282d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    // If we are passing this argument into call as the corresponding
283d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    // argument operand, then the argument is dynamically constant.
284d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner    // Otherwise, we cannot transform this function safely.
285de9f5452d3ae894bb7fdd455cec5af50e2560aa5Gabor Greif    if (CI->getArgOperand(ArgNo) == Arg)
286d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner      return true;
287d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  }
288f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky
289f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky  // Switch cases are always constant integers. If the value is being switched
290f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky  // on and the return is only reachable from one of its cases, it's
291f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky  // effectively constant.
292f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky  if (BasicBlock *UniquePred = RI->getParent()->getUniquePredecessor())
293f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky    if (SwitchInst *SI = dyn_cast<SwitchInst>(UniquePred->getTerminator()))
294f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky      if (SI->getCondition() == V)
295f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky        return SI->getDefaultDest() != RI->getParent();
296f80fcd00b3590626a6110fe5fbe9b5fd2a8be1aeNick Lewycky
297d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // Not a constant or immutable argument, we can't safely transform.
298d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  return false;
299d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner}
300d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner
301d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner// getCommonReturnValue - Check to see if the function containing the specified
302d50e9e2566479301e687fc81e16dab7e3c2b50eaDuncan Sands// tail call consistently returns the same runtime-constant value at all exit
303d50e9e2566479301e687fc81e16dab7e3c2b50eaDuncan Sands// points except for IgnoreRI.  If so, return the returned value.
304d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner//
305d50e9e2566479301e687fc81e16dab7e3c2b50eaDuncan Sandsstatic Value *getCommonReturnValue(ReturnInst *IgnoreRI, CallInst *CI) {
306d50e9e2566479301e687fc81e16dab7e3c2b50eaDuncan Sands  Function *F = CI->getParent()->getParent();
307d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  Value *ReturnedValue = 0;
308d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner
309b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner  for (Function::iterator BBI = F->begin(), E = F->end(); BBI != E; ++BBI) {
310b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    ReturnInst *RI = dyn_cast<ReturnInst>(BBI->getTerminator());
311b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    if (RI == 0 || RI == IgnoreRI) continue;
312b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner
313b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // We can only perform this transformation if the value returned is
314b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // evaluatable at the start of the initial invocation of the function,
315b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // instead of at the end of the evaluation.
316b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    //
317b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    Value *RetOp = RI->getOperand(0);
318b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    if (!isDynamicConstant(RetOp, CI, RI))
319b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      return 0;
320b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner
321b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    if (ReturnedValue && RetOp != ReturnedValue)
322b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      return 0;     // Cannot transform if differing values are returned.
323b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    ReturnedValue = RetOp;
324b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner  }
325d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  return ReturnedValue;
326d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner}
3277152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
328543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// CanTransformAccumulatorRecursion - If the specified instruction can be
329543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// transformed using accumulator recursion elimination, return the constant
330543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner/// which is the start of the accumulator value.  Otherwise return null.
331543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner///
332543d622ef7505910c1cdc09ada0ab797c3437590Chris LattnerValue *TailCallElim::CanTransformAccumulatorRecursion(Instruction *I,
333543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner                                                      CallInst *CI) {
33424080a98786cf393740cd035e27dd16c12827892Duncan Sands  if (!I->isAssociative() || !I->isCommutative()) return 0;
335543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  assert(I->getNumOperands() == 2 &&
33624080a98786cf393740cd035e27dd16c12827892Duncan Sands         "Associative/commutative operations should have 2 args!");
337543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
338b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner  // Exactly one operand should be the result of the call instruction.
33907e6e56f57e8781a8d7bc601cc9034a3741d84c2Anton Korobeynikov  if ((I->getOperand(0) == CI && I->getOperand(1) == CI) ||
34007e6e56f57e8781a8d7bc601cc9034a3741d84c2Anton Korobeynikov      (I->getOperand(0) != CI && I->getOperand(1) != CI))
341543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    return 0;
342543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
343543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // The only user of this instruction we allow is a single return instruction.
344543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  if (!I->hasOneUse() || !isa<ReturnInst>(I->use_back()))
345543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    return 0;
346543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
347543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // Ok, now we have to check all of the other return instructions in this
348543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // function.  If they return non-constants or differing values, then we cannot
349543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // transform the function safely.
350d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  return getCommonReturnValue(cast<ReturnInst>(I->use_back()), CI);
351543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner}
352543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
353c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Chengstatic Instruction *FirstNonDbg(BasicBlock::iterator I) {
354c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  while (isa<DbgInfoIntrinsic>(I))
355c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    ++I;
356c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  return &*I;
357c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng}
358c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
359c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan ChengCallInst*
360c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan ChengTailCallElim::FindTRECandidate(Instruction *TI,
361c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                               bool CannotTailCallElimCallsMarkedTail) {
362c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  BasicBlock *BB = TI->getParent();
3637152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  Function *F = BB->getParent();
3647152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
365c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  if (&BB->front() == TI) // Make sure there is something before the terminator.
366c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    return 0;
3678d9455d4e4f63c368f17b74a36b472fc61685310Chris Lattner
3687152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Scan backwards from the return, checking to see if there is a tail call in
3697152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // this block.  If so, set CI to it.
370c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  CallInst *CI = 0;
371c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  BasicBlock::iterator BBI = TI;
372c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  while (true) {
3737152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    CI = dyn_cast<CallInst>(BBI);
3747152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    if (CI && CI->getCalledFunction() == F)
3757152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner      break;
3767152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
3777152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    if (BBI == BB->begin())
378c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      return 0;          // Didn't find a potential tail call.
3797152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    --BBI;
3807152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  }
3817152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
382ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // If this call is marked as a tail call, and if there are dynamic allocas in
383ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // the function, we cannot perform this optimization.
384ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  if (CI->isTailCall() && CannotTailCallElimCallsMarkedTail)
385c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    return 0;
386ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
387ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  // As a special case, detect code like this:
388ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  //   double fabs(double f) { return __builtin_fabs(f); } // a 'fabs' call
389ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  // and disable this xform in this case, because the code generator will
390ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  // lower the call to fabs into inline code.
391ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  if (BB == &F->getEntryBlock() &&
392c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      FirstNonDbg(BB->front()) == CI &&
393c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      FirstNonDbg(llvm::next(BB->begin())) == TI &&
394ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman      callIsSmall(F)) {
395ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    // A single-block function with just a call and a return. Check that
396ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    // the arguments match.
397ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    CallSite::arg_iterator I = CallSite(CI).arg_begin(),
398ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman                           E = CallSite(CI).arg_end();
399ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    Function::arg_iterator FI = F->arg_begin(),
400ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman                           FE = F->arg_end();
401ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    for (; I != E && FI != FE; ++I, ++FI)
402ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman      if (*I != &*FI) break;
403ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman    if (I == E && FI == FE)
404c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      return 0;
405ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman  }
406ea25b48af33be42e19236d8eac26bd42b45bcc1bDan Gohman
407c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  return CI;
408c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng}
409c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
410c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Chengbool TailCallElim::EliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
411c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       BasicBlock *&OldEntry,
412c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       bool &TailCallsAreMarkedTail,
413c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       SmallVector<PHINode*, 8> &ArgumentPHIs,
414c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       bool CannotTailCallElimCallsMarkedTail) {
41524080a98786cf393740cd035e27dd16c12827892Duncan Sands  // If we are introducing accumulator recursion to eliminate operations after
41624080a98786cf393740cd035e27dd16c12827892Duncan Sands  // the call instruction that are both associative and commutative, the initial
41724080a98786cf393740cd035e27dd16c12827892Duncan Sands  // value for the accumulator is placed in this variable.  If this value is set
41824080a98786cf393740cd035e27dd16c12827892Duncan Sands  // then we actually perform accumulator recursion elimination instead of
419d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // simple tail recursion elimination.  If the operation is an LLVM instruction
420d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // (eg: "add") then it is recorded in AccumulatorRecursionInstr.  If not, then
421d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // we are handling the case when the return instruction returns a constant C
422d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // which is different to the constant returned by other return instructions
423d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // (which is recorded in AccumulatorRecursionEliminationInitVal).  This is a
424d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  // special case of accumulator recursion, the operation being "return C".
425543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  Value *AccumulatorRecursionEliminationInitVal = 0;
426543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  Instruction *AccumulatorRecursionInstr = 0;
427543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
4287152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Ok, we found a potential tail call.  We can currently only transform the
4297152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // tail call if all of the instructions between the call and the return are
4307152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // movable to above the call itself, leaving the call next to the return.
4317152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Check that this is the case now.
432c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  BasicBlock::iterator BBI = CI;
433c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  for (++BBI; &*BBI != Ret; ++BBI) {
434b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    if (CanMoveAboveCall(BBI, CI)) continue;
435b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner
436b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // If we can't move the instruction above the call, it might be because it
4377a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner    // is an associative and commutative operation that could be transformed
438b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // using accumulator recursion elimination.  Check to see if this is the
439b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    // case, and if so, remember the initial accumulator value for later.
440b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    if ((AccumulatorRecursionEliminationInitVal =
441b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner                           CanTransformAccumulatorRecursion(BBI, CI))) {
442b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      // Yes, this is accumulator recursion.  Remember which instruction
443b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      // accumulates.
444b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      AccumulatorRecursionInstr = BBI;
445b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner    } else {
446b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner      return false;   // Otherwise, we cannot eliminate the tail recursion!
447543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    }
448b5d84d13bc338efc4eeed87d19c49dfaed38e036Chris Lattner  }
4497152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
4507152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // We can only transform call/return pairs that either ignore the return value
451d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // of the call and return void, ignore the value of the call and return a
452d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // constant, return the value returned by the tail call, or that are being
453d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner  // accumulator recursion variable eliminated.
454826c49132a9cba1e08a5fce78d05561d9a3746ceDevang Patel  if (Ret->getNumOperands() == 1 && Ret->getReturnValue() != CI &&
4553b5f45042b17ad52815e3dd6c0c1df99f196dd04Chris Lattner      !isa<UndefValue>(Ret->getReturnValue()) &&
456d64152a70842b2f4186aa912938e69ca09c1434cChris Lattner      AccumulatorRecursionEliminationInitVal == 0 &&
457d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      !getCommonReturnValue(0, CI)) {
458d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    // One case remains that we are able to handle: the current return
459d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    // instruction returns a constant, and all other return instructions
460d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    // return a different constant.
461d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    if (!isDynamicConstant(Ret->getReturnValue(), CI, Ret))
462d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      return false; // Current return instruction does not return a constant.
463d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    // Check that all other return instructions return a common constant.  If
464d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    // so, record it in AccumulatorRecursionEliminationInitVal.
465d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    AccumulatorRecursionEliminationInitVal = getCommonReturnValue(Ret, CI);
466d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    if (!AccumulatorRecursionEliminationInitVal)
467d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      return false;
468d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands  }
4697152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner
470c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  BasicBlock *BB = Ret->getParent();
471c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  Function *F = BB->getParent();
472c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
4737152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // OK! We can transform this tail call.  If this is the first one found,
4747152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // create the new entry block, allowing us to branch back to the old entry.
4757152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  if (OldEntry == 0) {
4767152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    OldEntry = &F->getEntryBlock();
4771d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    BasicBlock *NewEntry = BasicBlock::Create(F->getContext(), "", F, OldEntry);
4786934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner    NewEntry->takeName(OldEntry);
4796934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner    OldEntry->setName("tailrecurse");
480051a950000e21935165db56695e35bade668193bGabor Greif    BranchInst::Create(OldEntry, NewEntry);
481fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
482ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    // If this tail call is marked 'tail' and if there are any allocas in the
483ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    // entry block, move them up to the new entry block.
484ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    TailCallsAreMarkedTail = CI->isTailCall();
485ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    if (TailCallsAreMarkedTail)
486ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      // Move all fixed sized allocas from OldEntry to NewEntry.
487ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner      for (BasicBlock::iterator OEBI = OldEntry->begin(), E = OldEntry->end(),
488ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner             NEBI = NewEntry->begin(); OEBI != E; )
489ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner        if (AllocaInst *AI = dyn_cast<AllocaInst>(OEBI++))
490ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner          if (isa<ConstantInt>(AI->getArraySize()))
4914bc5f8071a28b6fc4f4c2207dd03a5f747d0d84bChris Lattner            AI->moveBefore(NEBI);
492ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
4937152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    // Now that we have created a new block, which jumps to the entry
4947152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    // block, insert a PHI node for each argument of the function.
4957152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    // For now, we initialize each PHI to only have the real arguments
4967152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    // which are passed in.
4977152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    Instruction *InsertPos = OldEntry->begin();
4987f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner    for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
4997f78f218fdb3515a61b995ac64f909bfd8ee84a7Chris Lattner         I != E; ++I) {
5003ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad      PHINode *PN = PHINode::Create(I->getType(), 2,
501b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif                                    I->getName() + ".tr", InsertPos);
5027152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner      I->replaceAllUsesWith(PN); // Everyone use the PHI node now!
5037152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner      PN->addIncoming(I, NewEntry);
5047152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner      ArgumentPHIs.push_back(PN);
5057152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner    }
5067152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  }
507fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
508ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // If this function has self recursive calls in the tail position where some
509ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // are marked tail and some are not, only transform one flavor or another.  We
510ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // have to choose whether we move allocas in the entry block to the new entry
511ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // block or not, so we can't make a good choice for both.  NOTE: We could do
512ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // slightly better here in the case that the function has no entry block
513ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  // allocas.
514ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner  if (TailCallsAreMarkedTail && !CI->isTailCall())
515ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner    return false;
516ce869ee05bfcb9d4750a3d0919a8260a727841c3Chris Lattner
5177152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Ok, now that we know we have a pseudo-entry block WITH all of the
5187152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // required PHI nodes, add entries into the PHI node for the actual
5197152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // parameters passed into the tail-recursive call.
520407014f9a5f05f5a5867e5992a036358acc4a441Gabor Greif  for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i)
521de9f5452d3ae894bb7fdd455cec5af50e2560aa5Gabor Greif    ArgumentPHIs[i]->addIncoming(CI->getArgOperand(i), BB);
522fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
523543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // If we are introducing an accumulator variable to eliminate the recursion,
524543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // do so now.  Note that we _know_ that no subsequent tail recursion
525543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // eliminations will happen on this function because of the way the
526543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  // accumulator recursion predicate is set up.
527543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  //
528543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  if (AccumulatorRecursionEliminationInitVal) {
529543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    Instruction *AccRecInstr = AccumulatorRecursionInstr;
530543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // Start by inserting a new PHI node for the accumulator.
531d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad    pred_iterator PB = pred_begin(OldEntry), PE = pred_end(OldEntry);
532d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    PHINode *AccPN =
533d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      PHINode::Create(AccumulatorRecursionEliminationInitVal->getType(),
5343ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad                      std::distance(PB, PE) + 1,
535d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands                      "accumulator.tr", OldEntry->begin());
536543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
537543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // Loop over all of the predecessors of the tail recursion block.  For the
538543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // real entry into the function we seed the PHI with the initial value,
539543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // computed earlier.  For any other existing branches to this block (due to
540543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // other tail recursions eliminated) the accumulator is not modified.
541543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // Because we haven't added the branch in the current block to OldEntry yet,
542543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // it will not show up as a predecessor.
543d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad    for (pred_iterator PI = PB; PI != PE; ++PI) {
544a8b9df7bd96e0b0bc6dec448d30b7d72180b6595Gabor Greif      BasicBlock *P = *PI;
545a8b9df7bd96e0b0bc6dec448d30b7d72180b6595Gabor Greif      if (P == &F->getEntryBlock())
546a8b9df7bd96e0b0bc6dec448d30b7d72180b6595Gabor Greif        AccPN->addIncoming(AccumulatorRecursionEliminationInitVal, P);
547543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner      else
548a8b9df7bd96e0b0bc6dec448d30b7d72180b6595Gabor Greif        AccPN->addIncoming(AccPN, P);
549543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    }
550543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
551d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    if (AccRecInstr) {
552d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // Add an incoming argument for the current block, which is computed by
553d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // our associative and commutative accumulator instruction.
554d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      AccPN->addIncoming(AccRecInstr, BB);
555d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands
556d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // Next, rewrite the accumulator recursion instruction so that it does not
557d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // use the result of the call anymore, instead, use the PHI node we just
558d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // inserted.
559d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      AccRecInstr->setOperand(AccRecInstr->getOperand(0) != CI, AccPN);
560d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    } else {
561d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // Add an incoming argument for the current block, which is just the
562d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      // constant returned by the current return instruction.
563d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands      AccPN->addIncoming(Ret->getReturnValue(), BB);
564d0d3ccc827de69de3d9f666aa922f5f191219a06Duncan Sands    }
565543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
566543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // Finally, rewrite any return instructions in the program to return the PHI
567543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // node instead of the "initval" that they do currently.  This loop will
568543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    // actually rewrite the return value we are destroying, but that's ok.
569543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    for (Function::iterator BBI = F->begin(), E = F->end(); BBI != E; ++BBI)
570543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner      if (ReturnInst *RI = dyn_cast<ReturnInst>(BBI->getTerminator()))
571543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner        RI->setOperand(0, AccPN);
572543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner    ++NumAccumAdded;
573543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  }
574543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner
5757152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // Now that all of the PHI nodes are in place, remove the call and
5767152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  // ret instructions, replacing them with an unconditional branch.
57781199d2545112fcff5a6e055eb71e2a4950192a7Devang Patel  BranchInst *NewBI = BranchInst::Create(OldEntry, Ret);
57881199d2545112fcff5a6e055eb71e2a4950192a7Devang Patel  NewBI->setDebugLoc(CI->getDebugLoc());
57981199d2545112fcff5a6e055eb71e2a4950192a7Devang Patel
5807152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  BB->getInstList().erase(Ret);  // Remove return.
5817152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  BB->getInstList().erase(CI);   // Remove call.
582543d622ef7505910c1cdc09ada0ab797c3437590Chris Lattner  ++NumEliminated;
5837152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner  return true;
5847152da38ef2c1f9f8ee407dc7740dfd43cbc41a6Chris Lattner}
585c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
586c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Chengbool TailCallElim::FoldReturnAndProcessPred(BasicBlock *BB,
587c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       ReturnInst *Ret, BasicBlock *&OldEntry,
588c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       bool &TailCallsAreMarkedTail,
589c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       SmallVector<PHINode*, 8> &ArgumentPHIs,
590c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       bool CannotTailCallElimCallsMarkedTail) {
591c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  bool Change = false;
592c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
593c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  // If the return block contains nothing but the return and PHI's,
594c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  // there might be an opportunity to duplicate the return in its
595c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  // predecessors and perform TRC there. Look for predecessors that end
596c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  // in unconditional branch and recursive call(s).
597c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  SmallVector<BranchInst*, 8> UncondBranchPreds;
598c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
599c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    BasicBlock *Pred = *PI;
600c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    TerminatorInst *PTI = Pred->getTerminator();
601c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
602c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      if (BI->isUnconditional())
603c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng        UncondBranchPreds.push_back(BI);
604c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  }
605c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
606c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  while (!UncondBranchPreds.empty()) {
607c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    BranchInst *BI = UncondBranchPreds.pop_back_val();
608c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    BasicBlock *Pred = BI->getParent();
609c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    if (CallInst *CI = FindTRECandidate(BI, CannotTailCallElimCallsMarkedTail)){
610c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      DEBUG(dbgs() << "FOLDING: " << *BB
611c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng            << "INTO UNCOND BRANCH PRED: " << *Pred);
612c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      EliminateRecursiveTailCall(CI, FoldReturnIntoUncondBranch(Ret, BB, Pred),
613c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                 OldEntry, TailCallsAreMarkedTail, ArgumentPHIs,
614c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                 CannotTailCallElimCallsMarkedTail);
61560f5ad46c2147af79035a43e5745e46376124780Evan Cheng      ++NumRetDuped;
616c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng      Change = true;
617c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    }
618c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  }
619c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
620c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  return Change;
621c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng}
622c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
623c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Chengbool TailCallElim::ProcessReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry,
624c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                         bool &TailCallsAreMarkedTail,
625c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                         SmallVector<PHINode*, 8> &ArgumentPHIs,
626c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                       bool CannotTailCallElimCallsMarkedTail) {
627c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  CallInst *CI = FindTRECandidate(Ret, CannotTailCallElimCallsMarkedTail);
628c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  if (!CI)
629c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng    return false;
630c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng
631c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng  return EliminateRecursiveTailCall(CI, Ret, OldEntry, TailCallsAreMarkedTail,
632c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    ArgumentPHIs,
633c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng                                    CannotTailCallElimCallsMarkedTail);
634c3f507f98a0747bd256e1c13536060b6fc5c4b62Evan Cheng}
635