1 //===- TailRecursionElimination.cpp - Eliminate Tail Calls ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file transforms calls of the current function (self recursion) followed
11 // by a return instruction with a branch to the entry of the function, creating
12 // a loop. This pass also implements the following extensions to the basic
15 // 1. Trivial instructions between the call and return do not prevent the
16 // transformation from taking place, though currently the analysis cannot
17 // support moving any really useful instructions (only dead ones).
19 // There are several improvements that could be made:
21 // 1. If the function has any alloca instructions, these instructions will be
22 // moved out of the entry block of the function, causing them to be
23 // evaluated each time through the tail recursion. Safely keeping allocas
24 // in the entry block requires analysis to proves that the tail-called
25 // function does not read or write the stack object.
26 // 2. Tail recursion is only performed if the call immediately preceeds the
27 // return instruction. It's possible that there could be a jump between
28 // the call and the return.
29 // 3. TRE is only performed if the function returns void or if the return
30 // returns the result returned by the call. It is possible, but unlikely,
31 // that the return returns something else (like constant 0), and can still
32 // be TRE'd. It can be TRE'd if ALL OTHER return instructions in the
33 // function return the exact same value.
34 // 4. There can be intervening operations between the call and the return that
35 // prevent the TRE from occurring. For example, there could be GEP's and
36 // stores to memory that will not be read or written by the call. This
37 // requires some substantial analysis (such as with DSA) to prove safe to
38 // move ahead of the call, but doing so could allow many more TREs to be
39 // performed, for example in TreeAdd/TreeAlloc from the treeadd benchmark.
40 // 5. This pass could transform functions that are prevented from being tail
41 // recursive by a commutative expression to use an accumulator helper
42 // function, thus compiling the typical naive factorial or 'fib'
43 // implementation into efficient code.
45 //===----------------------------------------------------------------------===//
47 #include "llvm/Transforms/Scalar.h"
48 #include "llvm/DerivedTypes.h"
49 #include "llvm/Function.h"
50 #include "llvm/Instructions.h"
51 #include "llvm/Pass.h"
52 #include "Support/Statistic.h"
56 Statistic<> NumEliminated("tailcallelim", "Number of tail calls removed");
58 struct TailCallElim : public FunctionPass {
59 virtual bool runOnFunction(Function &F);
62 bool ProcessReturningBlock(ReturnInst *RI, BasicBlock *&OldEntry,
63 std::vector<PHINode*> &ArgumentPHIs);
64 bool CanMoveAboveCall(Instruction *I, CallInst *CI);
66 RegisterOpt<TailCallElim> X("tailcallelim", "Tail Call Elimination");
69 // Public interface to the TailCallElimination pass
70 FunctionPass *llvm::createTailCallEliminationPass() {
71 return new TailCallElim();
75 bool TailCallElim::runOnFunction(Function &F) {
76 // If this function is a varargs function, we won't be able to PHI the args
77 // right, so don't even try to convert it...
78 if (F.getFunctionType()->isVarArg()) return false;
80 BasicBlock *OldEntry = 0;
81 std::vector<PHINode*> ArgumentPHIs;
82 bool MadeChange = false;
84 // Loop over the function, looking for any returning blocks...
85 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
86 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator()))
87 MadeChange |= ProcessReturningBlock(Ret, OldEntry, ArgumentPHIs);
93 // CanMoveAboveCall - Return true if it is safe to move the specified
94 // instruction from after the call to before the call, assuming that all
95 // instructions between the call and this instruction are movable.
97 bool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) {
98 // FIXME: We can move load/store/call/free instructions above the call if the
99 // call does not mod/ref the memory location being processed.
100 if (I->mayWriteToMemory() || isa<LoadInst>(I))
103 // Otherwise, if this is a side-effect free instruction, check to make sure
104 // that it does not use the return value of the call. If it doesn't use the
105 // return value of the call, it must only use things that are defined before
106 // the call, or movable instructions between the call and the instruction
108 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
109 if (I->getOperand(i) == CI)
115 bool TailCallElim::ProcessReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry,
116 std::vector<PHINode*> &ArgumentPHIs) {
117 BasicBlock *BB = Ret->getParent();
118 Function *F = BB->getParent();
120 if (&BB->front() == Ret) // Make sure there is something before the ret...
123 // Scan backwards from the return, checking to see if there is a tail call in
124 // this block. If so, set CI to it.
126 BasicBlock::iterator BBI = Ret;
128 CI = dyn_cast<CallInst>(BBI);
129 if (CI && CI->getCalledFunction() == F)
132 if (BBI == BB->begin())
133 return false; // Didn't find a potential tail call.
137 // Ok, we found a potential tail call. We can currently only transform the
138 // tail call if all of the instructions between the call and the return are
139 // movable to above the call itself, leaving the call next to the return.
140 // Check that this is the case now.
141 for (BBI = CI, ++BBI; &*BBI != Ret; ++BBI)
142 if (!CanMoveAboveCall(BBI, CI))
143 return false; // Cannot move this instruction out of the way.
145 // We can only transform call/return pairs that either ignore the return value
146 // of the call and return void, or return the value returned by the tail call.
147 if (Ret->getNumOperands() != 0 && Ret->getReturnValue() != CI)
150 // OK! We can transform this tail call. If this is the first one found,
151 // create the new entry block, allowing us to branch back to the old entry.
153 OldEntry = &F->getEntryBlock();
154 std::string OldName = OldEntry->getName(); OldEntry->setName("tailrecurse");
155 BasicBlock *NewEntry = new BasicBlock(OldName, OldEntry);
156 new BranchInst(OldEntry, NewEntry);
158 // Now that we have created a new block, which jumps to the entry
159 // block, insert a PHI node for each argument of the function.
160 // For now, we initialize each PHI to only have the real arguments
161 // which are passed in.
162 Instruction *InsertPos = OldEntry->begin();
163 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
164 PHINode *PN = new PHINode(I->getType(), I->getName()+".tr", InsertPos);
165 I->replaceAllUsesWith(PN); // Everyone use the PHI node now!
166 PN->addIncoming(I, NewEntry);
167 ArgumentPHIs.push_back(PN);
171 // Ok, now that we know we have a pseudo-entry block WITH all of the
172 // required PHI nodes, add entries into the PHI node for the actual
173 // parameters passed into the tail-recursive call.
174 for (unsigned i = 0, e = CI->getNumOperands()-1; i != e; ++i)
175 ArgumentPHIs[i]->addIncoming(CI->getOperand(i+1), BB);
177 // Now that all of the PHI nodes are in place, remove the call and
178 // ret instructions, replacing them with an unconditional branch.
179 new BranchInst(OldEntry, Ret);
180 BB->getInstList().erase(Ret); // Remove return.
181 BB->getInstList().erase(CI); // Remove call.