Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 1 | //===- TailRecursionElimination.cpp - Eliminate Tail Calls ----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 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 |
| 13 | // algorithm: |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 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). |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 18 | // |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 19 | // There are several improvements that could be made: |
| 20 | // |
| 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. |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 26 | // 2. Tail recursion is only performed if the call immediately preceeds the |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 27 | // return instruction. It's possible that there could be a jump between |
| 28 | // the call and the return. |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 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. |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 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. |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Chris Lattner | 0016085 | 2003-09-20 05:14:13 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 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" |
Chris Lattner | 2af5172 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | Statistic<> NumEliminated("tailcallelim", "Number of tail calls removed"); |
| 57 | |
| 58 | struct TailCallElim : public FunctionPass { |
| 59 | virtual bool runOnFunction(Function &F); |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | bool ProcessReturningBlock(ReturnInst *RI, BasicBlock *&OldEntry, |
| 63 | std::vector<PHINode*> &ArgumentPHIs); |
| 64 | bool CanMoveAboveCall(Instruction *I, CallInst *CI); |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 65 | }; |
| 66 | RegisterOpt<TailCallElim> X("tailcallelim", "Tail Call Elimination"); |
| 67 | } |
| 68 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 69 | // Public interface to the TailCallElimination pass |
Chris Lattner | 2af5172 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 70 | FunctionPass *llvm::createTailCallEliminationPass() { |
| 71 | return new TailCallElim(); |
| 72 | } |
Chris Lattner | 0016085 | 2003-09-20 05:14:13 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 74 | |
| 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; |
| 79 | |
| 80 | BasicBlock *OldEntry = 0; |
| 81 | std::vector<PHINode*> ArgumentPHIs; |
| 82 | bool MadeChange = false; |
| 83 | |
| 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())) |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 87 | MadeChange |= ProcessReturningBlock(Ret, OldEntry, ArgumentPHIs); |
Chris Lattner | 2e9014c | 2003-09-20 05:03:31 +0000 | [diff] [blame] | 88 | |
| 89 | return MadeChange; |
| 90 | } |
Chris Lattner | a7b6f3a | 2003-12-08 05:34:54 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 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. |
| 96 | // |
| 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)) |
| 101 | return false; |
| 102 | |
| 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 |
| 107 | // itself. |
| 108 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 109 | if (I->getOperand(i) == CI) |
| 110 | return false; |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | bool TailCallElim::ProcessReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry, |
| 116 | std::vector<PHINode*> &ArgumentPHIs) { |
| 117 | BasicBlock *BB = Ret->getParent(); |
| 118 | Function *F = BB->getParent(); |
| 119 | |
| 120 | if (&BB->front() == Ret) // Make sure there is something before the ret... |
| 121 | return false; |
| 122 | |
| 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. |
| 125 | CallInst *CI; |
| 126 | BasicBlock::iterator BBI = Ret; |
| 127 | while (1) { |
| 128 | CI = dyn_cast<CallInst>(BBI); |
| 129 | if (CI && CI->getCalledFunction() == F) |
| 130 | break; |
| 131 | |
| 132 | if (BBI == BB->begin()) |
| 133 | return false; // Didn't find a potential tail call. |
| 134 | --BBI; |
| 135 | } |
| 136 | |
| 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. |
| 144 | |
| 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) |
| 148 | return false; |
| 149 | |
| 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. |
| 152 | if (OldEntry == 0) { |
| 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); |
| 157 | |
| 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); |
| 168 | } |
| 169 | } |
| 170 | |
| 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); |
| 176 | |
| 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. |
| 182 | NumEliminated++; |
| 183 | return true; |
| 184 | } |