Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 1 | //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===// |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a variety of small optimizations for calls to specific |
| 11 | // well-known (e.g. runtime library) function calls. For example, a call to the |
| 12 | // function "exit(3)" that occurs within the main() function can be transformed |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 13 | // into a simple "return 3" instruction. Any optimization that takes this form |
| 14 | // (replace call to library function with simpler code that provides same |
| 15 | // result) belongs in this file. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/Transforms/IPO.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/hash_map" |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | Statistic<> SimplifiedLibCalls("simplified-lib-calls", |
| 29 | "Number of well-known library calls simplified"); |
| 30 | |
| 31 | /// This class is the base class for a set of small but important |
| 32 | /// optimizations of calls to well-known functions, such as those in the c |
| 33 | /// library. This class provides the basic infrastructure for handling |
| 34 | /// runOnModule. Subclasses register themselves and provide two methods: |
| 35 | /// RecognizeCall and OptimizeCall. Whenever this class finds a function call, |
| 36 | /// it asks the subclasses to recognize the call. If it is recognized, then |
| 37 | /// the OptimizeCall method is called on that subclass instance. In this way |
| 38 | /// the subclasses implement the calling conditions on which they trigger and |
| 39 | /// the action to perform, making it easy to add new optimizations of this |
| 40 | /// form. |
| 41 | /// @brief A ModulePass for optimizing well-known function calls |
| 42 | struct SimplifyLibCalls : public ModulePass { |
| 43 | |
| 44 | |
| 45 | /// For this pass, process all of the function calls in the module, calling |
| 46 | /// RecognizeCall and OptimizeCall as appropriate. |
| 47 | virtual bool runOnModule(Module &M); |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | RegisterOpt<SimplifyLibCalls> |
| 52 | X("simplify-libcalls","Simplify well-known library calls"); |
| 53 | |
| 54 | struct CallOptimizer |
| 55 | { |
| 56 | /// @brief Constructor that registers the optimization |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 57 | CallOptimizer(const char * fname ); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 58 | |
| 59 | virtual ~CallOptimizer(); |
| 60 | |
| 61 | /// The implementations of this function in subclasses is the heart of the |
| 62 | /// SimplifyLibCalls algorithm. Sublcasses of this class implement |
| 63 | /// OptimizeCall to determine if (a) the conditions are right for optimizing |
| 64 | /// the call and (b) to perform the optimization. If an action is taken |
| 65 | /// against ci, the subclass is responsible for returning true and ensuring |
| 66 | /// that ci is erased from its parent. |
| 67 | /// @param ci the call instruction under consideration |
| 68 | /// @param f the function that ci calls. |
| 69 | /// @brief Optimize a call, if possible. |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 70 | virtual bool OptimizeCall(CallInst* ci) const = 0; |
| 71 | |
| 72 | const std::string& getFunctionName() const { return func_name; } |
| 73 | private: |
| 74 | std::string func_name; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /// @brief The list of optimizations deriving from CallOptimizer |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 78 | hash_map<std::string,CallOptimizer*> optlist; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 80 | CallOptimizer::CallOptimizer(const char* fname) |
| 81 | : func_name(fname) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 82 | { |
| 83 | // Register this call optimizer |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 84 | optlist[func_name] = this; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /// Make sure we get our virtual table in this file. |
| 88 | CallOptimizer::~CallOptimizer() {} |
| 89 | } |
| 90 | |
| 91 | ModulePass *llvm::createSimplifyLibCallsPass() |
| 92 | { |
| 93 | return new SimplifyLibCalls(); |
| 94 | } |
| 95 | |
| 96 | bool SimplifyLibCalls::runOnModule(Module &M) |
| 97 | { |
| 98 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 99 | { |
| 100 | // All the "well-known" functions are external because they live in a |
| 101 | // runtime library somewhere and were (probably) not compiled by LLVM. |
| 102 | // So, we only act on external functions that have non-empty uses. |
| 103 | if (FI->isExternal() && !FI->use_empty()) |
| 104 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 105 | // Get the optimization class that pertains to this function |
| 106 | if (CallOptimizer* CO = optlist[FI->getName()] ) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 107 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 108 | // Loop over each of the uses of the function |
| 109 | for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end(); |
| 110 | UI != UE ; ) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 111 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 112 | // If the use of the function is a call instruction |
| 113 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 114 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 115 | // Do the optimization on the CallOptimizer we found earlier. |
| 116 | if (CO->OptimizeCall(CI)) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 117 | { |
| 118 | ++SimplifiedLibCalls; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | namespace { |
| 130 | |
| 131 | /// This CallOptimizer will find instances of a call to "exit" that occurs |
| 132 | /// within the "main" function and change it to a simple "ret" instruction with |
| 133 | /// the same value as passed to the exit function. It assumes that the |
| 134 | /// instructions after the call to exit(3) can be deleted since they are |
| 135 | /// unreachable anyway. |
| 136 | /// @brief Replace calls to exit in main with a simple return |
| 137 | struct ExitInMainOptimization : public CallOptimizer |
| 138 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 139 | ExitInMainOptimization() : CallOptimizer("exit") {} |
| 140 | virtual ~ExitInMainOptimization() {} |
| 141 | virtual bool OptimizeCall(CallInst* ci) const |
| 142 | { |
| 143 | // If the call isn't coming from main or main doesn't have external linkage |
| 144 | // or the return type of main is not the same as the type of the exit(3) |
| 145 | // argument then we don't act |
| 146 | if (const Function* f = ci->getParent()->getParent()) |
| 147 | if (!(f->hasExternalLinkage() && |
| 148 | (f->getReturnType() == ci->getOperand(1)->getType()) && |
| 149 | (f->getName() == "main"))) |
| 150 | return false; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 151 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 152 | // Okay, time to replace it. Get the basic block of the call instruction |
| 153 | BasicBlock* bb = ci->getParent(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 154 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 155 | // Create a return instruction that we'll replace the call with. Note that |
| 156 | // the argument of the return is the argument of the call instruction. |
| 157 | ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 158 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 159 | // Erase everything from the call instruction to the end of the block. There |
| 160 | // really shouldn't be anything other than the call instruction, but just in |
| 161 | // case there is we delete it all because its now dead. |
| 162 | bb->getInstList().erase(ci, bb->end()); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 163 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 166 | } ExitInMainOptimizer; |
| 167 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 168 | /// This CallOptimizer will find instances of a call to "exit" that occurs |
| 169 | /// within the "main" function and change it to a simple "ret" instruction with |
| 170 | /// the same value as passed to the exit function. It assumes that the |
| 171 | /// instructions after the call to exit(3) can be deleted since they are |
| 172 | /// unreachable anyway. |
| 173 | /// @brief Replace calls to exit in main with a simple return |
| 174 | struct StrCatOptimization : public CallOptimizer |
| 175 | { |
| 176 | StrCatOptimization() : CallOptimizer("strcat") {} |
| 177 | virtual ~StrCatOptimization() {} |
| 178 | virtual bool OptimizeCall(CallInst* ci) const |
| 179 | { |
| 180 | return false; |
| 181 | } |
| 182 | } StrCatOptimizer; |
| 183 | |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 184 | } |