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 | |
Reid Spencer | 18b9981 | 2005-04-26 23:05:17 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "simplify-libcalls" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Instructions.h" |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/hash_map" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Support/Debug.h" |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/IPO.h" |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 30 | #include <iostream> |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 34 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 35 | /// This statistic keeps track of the total number of library calls that have |
| 36 | /// been simplified regardless of which call it is. |
| 37 | Statistic<> SimplifiedLibCalls("simplify-libcalls", |
| 38 | "Number of well-known library calls simplified"); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 40 | // Forward declarations |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 41 | class LibCallOptimization; |
| 42 | class SimplifyLibCalls; |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 43 | |
| 44 | /// @brief The list of optimizations deriving from LibCallOptimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 45 | hash_map<std::string,LibCallOptimization*> optlist; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 46 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 47 | /// This class is the abstract base class for the set of optimizations that |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 48 | /// corresponds to one library call. The SimplifyLibCalls pass will call the |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 49 | /// ValidateCalledFunction method to ask the optimization if a given Function |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 50 | /// is the kind that the optimization can handle. If the subclass returns true, |
| 51 | /// then SImplifyLibCalls will also call the OptimizeCall method to perform, |
| 52 | /// or attempt to perform, the optimization(s) for the library call. Otherwise, |
| 53 | /// OptimizeCall won't be called. Subclasses are responsible for providing the |
| 54 | /// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization |
| 55 | /// constructor. This is used to efficiently select which call instructions to |
| 56 | /// optimize. The criteria for a "lib call" is "anything with well known |
| 57 | /// semantics", typically a library function that is defined by an international |
| 58 | /// standard. Because the semantics are well known, the optimizations can |
| 59 | /// generally short-circuit actually calling the function if there's a simpler |
| 60 | /// way (e.g. strlen(X) can be reduced to a constant if X is a constant global). |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 61 | /// @brief Base class for library call optimizations |
| 62 | struct LibCallOptimization |
| 63 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 64 | /// The \p fname argument must be the name of the library function being |
| 65 | /// optimized by the subclass. |
| 66 | /// @brief Constructor that registers the optimization. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 67 | LibCallOptimization(const char * fname ) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 68 | : func_name(fname) |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 69 | #ifndef NDEBUG |
Reid Spencer | dc11db6 | 2005-04-27 00:20:23 +0000 | [diff] [blame] | 70 | , stat_name(std::string("simplify-libcalls:")+fname) |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 71 | , occurrences(stat_name.c_str(),"Number of calls simplified") |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 72 | #endif |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 73 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 74 | // Register this call optimizer in the optlist (a hash_map) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 75 | optlist[func_name] = this; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 78 | /// @brief Deregister from the optlist |
| 79 | virtual ~LibCallOptimization() { optlist.erase(func_name); } |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 80 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 81 | /// The implementation of this function in subclasses should determine if |
| 82 | /// \p F is suitable for the optimization. This method is called by |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 83 | /// SimplifyLibCalls::runOnModule to short circuit visiting all the call |
| 84 | /// sites of such a function if that function is not suitable in the first |
| 85 | /// place. If the called function is suitabe, this method should return true; |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 86 | /// false, otherwise. This function should also perform any lazy |
| 87 | /// initialization that the LibCallOptimization needs to do, if its to return |
| 88 | /// true. This avoids doing initialization until the optimizer is actually |
| 89 | /// going to be called upon to do some optimization. |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 90 | /// @brief Determine if the function is suitable for optimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 91 | virtual bool ValidateCalledFunction( |
| 92 | const Function* F, ///< The function that is the target of call sites |
| 93 | SimplifyLibCalls& SLC ///< The pass object invoking us |
| 94 | ) = 0; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 95 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 96 | /// The implementations of this function in subclasses is the heart of the |
| 97 | /// SimplifyLibCalls algorithm. Sublcasses of this class implement |
| 98 | /// OptimizeCall to determine if (a) the conditions are right for optimizing |
| 99 | /// the call and (b) to perform the optimization. If an action is taken |
| 100 | /// against ci, the subclass is responsible for returning true and ensuring |
| 101 | /// that ci is erased from its parent. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 102 | /// @brief Optimize a call, if possible. |
| 103 | virtual bool OptimizeCall( |
| 104 | CallInst* ci, ///< The call instruction that should be optimized. |
| 105 | SimplifyLibCalls& SLC ///< The pass object invoking us |
| 106 | ) = 0; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 107 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 108 | /// @brief Get the name of the library call being optimized |
| 109 | const char * getFunctionName() const { return func_name; } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 110 | |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 111 | #ifndef NDEBUG |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 112 | /// @brief Called by SimplifyLibCalls to update the occurrences statistic. |
| 113 | void succeeded() { ++occurrences; } |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 114 | #endif |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | const char* func_name; ///< Name of the library call we optimize |
| 118 | #ifndef NDEBUG |
| 119 | std::string stat_name; ///< Holder for debug statistic name |
| 120 | Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls) |
| 121 | #endif |
| 122 | }; |
| 123 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 124 | /// This class is an LLVM Pass that applies each of the LibCallOptimization |
| 125 | /// instances to all the call sites in a module, relatively efficiently. The |
| 126 | /// purpose of this pass is to provide optimizations for calls to well-known |
| 127 | /// functions with well-known semantics, such as those in the c library. The |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 128 | /// class provides the basic infrastructure for handling runOnModule. Whenever /// this pass finds a function call, it asks the appropriate optimizer to |
| 129 | /// validate the call (ValidateLibraryCall). If it is validated, then |
| 130 | /// the OptimizeCall method is also called. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 131 | /// @brief A ModulePass for optimizing well-known function calls. |
| 132 | struct SimplifyLibCalls : public ModulePass |
| 133 | { |
| 134 | /// We need some target data for accurate signature details that are |
| 135 | /// target dependent. So we require target data in our AnalysisUsage. |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 136 | /// @brief Require TargetData from AnalysisUsage. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 137 | virtual void getAnalysisUsage(AnalysisUsage& Info) const |
| 138 | { |
| 139 | // Ask that the TargetData analysis be performed before us so we can use |
| 140 | // the target data. |
| 141 | Info.addRequired<TargetData>(); |
| 142 | } |
| 143 | |
| 144 | /// For this pass, process all of the function calls in the module, calling |
| 145 | /// ValidateLibraryCall and OptimizeCall as appropriate. |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 146 | /// @brief Run all the lib call optimizations on a Module. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 147 | virtual bool runOnModule(Module &M) |
| 148 | { |
| 149 | reset(M); |
| 150 | |
| 151 | bool result = false; |
| 152 | |
| 153 | // The call optimizations can be recursive. That is, the optimization might |
| 154 | // generate a call to another function which can also be optimized. This way |
| 155 | // we make the LibCallOptimization instances very specific to the case they |
| 156 | // handle. It also means we need to keep running over the function calls in |
| 157 | // the module until we don't get any more optimizations possible. |
| 158 | bool found_optimization = false; |
| 159 | do |
| 160 | { |
| 161 | found_optimization = false; |
| 162 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 163 | { |
| 164 | // All the "well-known" functions are external and have external linkage |
| 165 | // because they live in a runtime library somewhere and were (probably) |
| 166 | // not compiled by LLVM. So, we only act on external functions that have |
| 167 | // external linkage and non-empty uses. |
| 168 | if (!FI->isExternal() || !FI->hasExternalLinkage() || FI->use_empty()) |
| 169 | continue; |
| 170 | |
| 171 | // Get the optimization class that pertains to this function |
| 172 | LibCallOptimization* CO = optlist[FI->getName().c_str()]; |
| 173 | if (!CO) |
| 174 | continue; |
| 175 | |
| 176 | // Make sure the called function is suitable for the optimization |
| 177 | if (!CO->ValidateCalledFunction(FI,*this)) |
| 178 | continue; |
| 179 | |
| 180 | // Loop over each of the uses of the function |
| 181 | for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end(); |
| 182 | UI != UE ; ) |
| 183 | { |
| 184 | // If the use of the function is a call instruction |
| 185 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 186 | { |
| 187 | // Do the optimization on the LibCallOptimization. |
| 188 | if (CO->OptimizeCall(CI,*this)) |
| 189 | { |
| 190 | ++SimplifiedLibCalls; |
| 191 | found_optimization = result = true; |
| 192 | #ifndef NDEBUG |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 193 | CO->succeeded(); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 194 | #endif |
| 195 | } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 199 | } while (found_optimization); |
| 200 | return result; |
| 201 | } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 202 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 203 | /// @brief Return the *current* module we're working on. |
| 204 | Module* getModule() { return M; } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 205 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 206 | /// @brief Return the *current* target data for the module we're working on. |
| 207 | TargetData* getTargetData() { return TD; } |
| 208 | |
| 209 | /// @brief Return a Function* for the strlen libcall |
| 210 | Function* get_strlen() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 211 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 212 | if (!strlen_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 213 | { |
| 214 | std::vector<const Type*> args; |
| 215 | args.push_back(PointerType::get(Type::SByteTy)); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 216 | FunctionType* strlen_type = |
| 217 | FunctionType::get(TD->getIntPtrType(), args, false); |
| 218 | strlen_func = M->getOrInsertFunction("strlen",strlen_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 219 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 220 | return strlen_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 223 | /// @brief Return a Function* for the memcpy libcall |
| 224 | Function* get_memcpy() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 225 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 226 | if (!memcpy_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 227 | { |
| 228 | // Note: this is for llvm.memcpy intrinsic |
| 229 | std::vector<const Type*> args; |
| 230 | args.push_back(PointerType::get(Type::SByteTy)); |
| 231 | args.push_back(PointerType::get(Type::SByteTy)); |
| 232 | args.push_back(Type::IntTy); |
| 233 | args.push_back(Type::IntTy); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 234 | FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false); |
| 235 | memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 236 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 237 | return memcpy_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 238 | } |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 239 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 240 | private: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 241 | /// @brief Reset our cached data for a new Module |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 242 | void reset(Module& mod) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 243 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 244 | M = &mod; |
| 245 | TD = &getAnalysis<TargetData>(); |
| 246 | memcpy_func = 0; |
| 247 | strlen_func = 0; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 248 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 249 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 250 | private: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 251 | Function* memcpy_func; ///< Cached llvm.memcpy function |
| 252 | Function* strlen_func; ///< Cached strlen function |
| 253 | Module* M; ///< Cached Module |
| 254 | TargetData* TD; ///< Cached TargetData |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | // Register the pass |
| 258 | RegisterOpt<SimplifyLibCalls> |
| 259 | X("simplify-libcalls","Simplify well-known library calls"); |
| 260 | |
| 261 | } // anonymous namespace |
| 262 | |
| 263 | // The only public symbol in this file which just instantiates the pass object |
| 264 | ModulePass *llvm::createSimplifyLibCallsPass() |
| 265 | { |
| 266 | return new SimplifyLibCalls(); |
| 267 | } |
| 268 | |
| 269 | // Classes below here, in the anonymous namespace, are all subclasses of the |
| 270 | // LibCallOptimization class, each implementing all optimizations possible for a |
| 271 | // single well-known library call. Each has a static singleton instance that |
| 272 | // auto registers it into the "optlist" global above. |
| 273 | namespace { |
| 274 | |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 275 | // Forward declare a utility function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 276 | bool getConstantStringLength(Value* V, uint64_t& len ); |
| 277 | |
| 278 | /// This LibCallOptimization will find instances of a call to "exit" that occurs |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 279 | /// within the "main" function and change it to a simple "ret" instruction with |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 280 | /// the same value passed to the exit function. When this is done, it splits the |
| 281 | /// basic block at the exit(3) call and deletes the call instruction. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 282 | /// @brief Replace calls to exit in main with a simple return |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 283 | struct ExitInMainOptimization : public LibCallOptimization |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 284 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 285 | ExitInMainOptimization() : LibCallOptimization("exit") {} |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 286 | virtual ~ExitInMainOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 287 | |
| 288 | // Make sure the called function looks like exit (int argument, int return |
| 289 | // type, external linkage, not varargs). |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 290 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 291 | { |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 292 | if (f->arg_size() >= 1) |
| 293 | if (f->arg_begin()->getType()->isInteger()) |
| 294 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 298 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 299 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 300 | // To be careful, we check that the call to exit is coming from "main", that |
| 301 | // main has external linkage, and the return type of main and the argument |
| 302 | // to exit have the same type. |
| 303 | Function *from = ci->getParent()->getParent(); |
| 304 | if (from->hasExternalLinkage()) |
| 305 | if (from->getReturnType() == ci->getOperand(1)->getType()) |
| 306 | if (from->getName() == "main") |
| 307 | { |
| 308 | // Okay, time to actually do the optimization. First, get the basic |
| 309 | // block of the call instruction |
| 310 | BasicBlock* bb = ci->getParent(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 311 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 312 | // Create a return instruction that we'll replace the call with. |
| 313 | // Note that the argument of the return is the argument of the call |
| 314 | // instruction. |
| 315 | ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 316 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 317 | // Split the block at the call instruction which places it in a new |
| 318 | // basic block. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 319 | bb->splitBasicBlock(ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 320 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 321 | // The block split caused a branch instruction to be inserted into |
| 322 | // the end of the original block, right after the return instruction |
| 323 | // that we put there. That's not a valid block, so delete the branch |
| 324 | // instruction. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 325 | bb->getInstList().pop_back(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 326 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 327 | // Now we can finally get rid of the call instruction which now lives |
| 328 | // in the new basic block. |
| 329 | ci->eraseFromParent(); |
| 330 | |
| 331 | // Optimization succeeded, return true. |
| 332 | return true; |
| 333 | } |
| 334 | // We didn't pass the criteria for this optimization so return false |
| 335 | return false; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 336 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 337 | } ExitInMainOptimizer; |
| 338 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 339 | /// This LibCallOptimization will simplify a call to the strcat library |
| 340 | /// function. The simplification is possible only if the string being |
| 341 | /// concatenated is a constant array or a constant expression that results in |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 342 | /// a constant string. In this case we can replace it with strlen + llvm.memcpy |
| 343 | /// of the constant string. Both of these calls are further reduced, if possible |
| 344 | /// on subsequent passes. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 345 | /// @brief Simplify the strcat library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 346 | struct StrCatOptimization : public LibCallOptimization |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 347 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 348 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 349 | /// @brief Default constructor |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 350 | StrCatOptimization() : LibCallOptimization("strcat") {} |
| 351 | |
| 352 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 353 | /// @breif Destructor |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 354 | virtual ~StrCatOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 355 | |
| 356 | /// @brief Make sure that the "strcat" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 357 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 358 | { |
| 359 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 360 | if (f->arg_size() == 2) |
| 361 | { |
| 362 | Function::const_arg_iterator AI = f->arg_begin(); |
| 363 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 364 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 365 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 366 | // Indicate this is a suitable call type. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 367 | return true; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 368 | } |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 373 | /// @brief Optimize the strcat library function |
| 374 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 375 | { |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 376 | // Extract some information from the instruction |
| 377 | Module* M = ci->getParent()->getParent()->getParent(); |
| 378 | Value* dest = ci->getOperand(1); |
| 379 | Value* src = ci->getOperand(2); |
| 380 | |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 381 | // Extract the initializer (while making numerous checks) from the |
| 382 | // source operand of the call to strcat. If we get null back, one of |
| 383 | // a variety of checks in get_GVInitializer failed |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 384 | uint64_t len = 0; |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 385 | if (!getConstantStringLength(src,len)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 386 | return false; |
| 387 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 388 | // Handle the simple, do-nothing case |
| 389 | if (len == 0) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 390 | { |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 391 | ci->replaceAllUsesWith(dest); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 392 | ci->eraseFromParent(); |
| 393 | return true; |
| 394 | } |
| 395 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 396 | // Increment the length because we actually want to memcpy the null |
| 397 | // terminator as well. |
| 398 | len++; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 399 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 400 | |
| 401 | // We need to find the end of the destination string. That's where the |
| 402 | // memory is to be moved to. We just generate a call to strlen (further |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 403 | // optimized in another pass). Note that the SLC.get_strlen() call |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 404 | // caches the Function* for us. |
| 405 | CallInst* strlen_inst = |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 406 | new CallInst(SLC.get_strlen(), dest, dest->getName()+".len",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 407 | |
| 408 | // Now that we have the destination's length, we must index into the |
| 409 | // destination's pointer to get the actual memcpy destination (end of |
| 410 | // the string .. we're concatenating). |
| 411 | std::vector<Value*> idx; |
| 412 | idx.push_back(strlen_inst); |
| 413 | GetElementPtrInst* gep = |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 414 | new GetElementPtrInst(dest,idx,dest->getName()+".indexed",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 415 | |
| 416 | // We have enough information to now generate the memcpy call to |
| 417 | // do the concatenation for us. |
| 418 | std::vector<Value*> vals; |
| 419 | vals.push_back(gep); // destination |
| 420 | vals.push_back(ci->getOperand(2)); // source |
| 421 | vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length |
| 422 | vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 423 | new CallInst(SLC.get_memcpy(), vals, "", ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 424 | |
| 425 | // Finally, substitute the first operand of the strcat call for the |
| 426 | // strcat call itself since strcat returns its first operand; and, |
| 427 | // kill the strcat CallInst. |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 428 | ci->replaceAllUsesWith(dest); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 429 | ci->eraseFromParent(); |
| 430 | return true; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 431 | } |
| 432 | } StrCatOptimizer; |
| 433 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 434 | /// This LibCallOptimization will simplify a call to the strcpy library |
| 435 | /// function. Two optimizations are possible: |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 436 | /// (1) If src and dest are the same and not volatile, just return dest |
| 437 | /// (2) If the src is a constant then we can convert to llvm.memmove |
| 438 | /// @brief Simplify the strcpy library function. |
| 439 | struct StrCpyOptimization : public LibCallOptimization |
| 440 | { |
| 441 | public: |
| 442 | StrCpyOptimization() : LibCallOptimization("strcpy") {} |
| 443 | virtual ~StrCpyOptimization() {} |
| 444 | |
| 445 | /// @brief Make sure that the "strcpy" function has the right prototype |
| 446 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 447 | { |
| 448 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 449 | if (f->arg_size() == 2) |
| 450 | { |
| 451 | Function::const_arg_iterator AI = f->arg_begin(); |
| 452 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 453 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 454 | { |
| 455 | // Indicate this is a suitable call type. |
| 456 | return true; |
| 457 | } |
| 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | /// @brief Perform the strcpy optimization |
| 463 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 464 | { |
| 465 | // First, check to see if src and destination are the same. If they are, |
| 466 | // then the optimization is to replace the CallInst with the destination |
| 467 | // because the call is a no-op. Note that this corresponds to the |
| 468 | // degenerate strcpy(X,X) case which should have "undefined" results |
| 469 | // according to the C specification. However, it occurs sometimes and |
| 470 | // we optimize it as a no-op. |
| 471 | Value* dest = ci->getOperand(1); |
| 472 | Value* src = ci->getOperand(2); |
| 473 | if (dest == src) |
| 474 | { |
| 475 | ci->replaceAllUsesWith(dest); |
| 476 | ci->eraseFromParent(); |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | // Get the length of the constant string referenced by the second operand, |
| 481 | // the "src" parameter. Fail the optimization if we can't get the length |
| 482 | // (note that getConstantStringLength does lots of checks to make sure this |
| 483 | // is valid). |
| 484 | uint64_t len = 0; |
| 485 | if (!getConstantStringLength(ci->getOperand(2),len)) |
| 486 | return false; |
| 487 | |
| 488 | // If the constant string's length is zero we can optimize this by just |
| 489 | // doing a store of 0 at the first byte of the destination |
| 490 | if (len == 0) |
| 491 | { |
| 492 | new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci); |
| 493 | ci->replaceAllUsesWith(dest); |
| 494 | ci->eraseFromParent(); |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | // Increment the length because we actually want to memcpy the null |
| 499 | // terminator as well. |
| 500 | len++; |
| 501 | |
| 502 | // Extract some information from the instruction |
| 503 | Module* M = ci->getParent()->getParent()->getParent(); |
| 504 | |
| 505 | // We have enough information to now generate the memcpy call to |
| 506 | // do the concatenation for us. |
| 507 | std::vector<Value*> vals; |
| 508 | vals.push_back(dest); // destination |
| 509 | vals.push_back(src); // source |
| 510 | vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length |
| 511 | vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 512 | new CallInst(SLC.get_memcpy(), vals, "", ci); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 513 | |
| 514 | // Finally, substitute the first operand of the strcat call for the |
| 515 | // strcat call itself since strcat returns its first operand; and, |
| 516 | // kill the strcat CallInst. |
| 517 | ci->replaceAllUsesWith(dest); |
| 518 | ci->eraseFromParent(); |
| 519 | return true; |
| 520 | } |
| 521 | } StrCpyOptimizer; |
| 522 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 523 | /// This LibCallOptimization will simplify a call to the strlen library |
| 524 | /// function by replacing it with a constant value if the string provided to |
| 525 | /// it is a constant array. |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 526 | /// @brief Simplify the strlen library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 527 | struct StrLenOptimization : public LibCallOptimization |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 528 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 529 | StrLenOptimization() : LibCallOptimization("strlen") {} |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 530 | virtual ~StrLenOptimization() {} |
| 531 | |
| 532 | /// @brief Make sure that the "strlen" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 533 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 534 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 535 | if (f->getReturnType() == SLC.getTargetData()->getIntPtrType()) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 536 | if (f->arg_size() == 1) |
| 537 | if (Function::const_arg_iterator AI = f->arg_begin()) |
| 538 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 539 | return true; |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | /// @brief Perform the strlen optimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 544 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 545 | { |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 546 | // Get the length of the string |
| 547 | uint64_t len = 0; |
| 548 | if (!getConstantStringLength(ci->getOperand(1),len)) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 549 | return false; |
| 550 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 551 | ci->replaceAllUsesWith( |
| 552 | ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len)); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 553 | ci->eraseFromParent(); |
| 554 | return true; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 555 | } |
| 556 | } StrLenOptimizer; |
| 557 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 558 | /// This LibCallOptimization will simplify a call to the memcpy library |
| 559 | /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8 |
| 560 | /// bytes depending on the length of the string and the alignment. Additional |
| 561 | /// optimizations are possible in code generation (sequence of immediate store) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 562 | /// @brief Simplify the memcpy library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 563 | struct MemCpyOptimization : public LibCallOptimization |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 564 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 565 | /// @brief Default Constructor |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 566 | MemCpyOptimization() : LibCallOptimization("llvm.memcpy") {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 567 | protected: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 568 | /// @brief Subclass Constructor |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 569 | MemCpyOptimization(const char* fname) : LibCallOptimization(fname) {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 570 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 571 | /// @brief Destructor |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 572 | virtual ~MemCpyOptimization() {} |
| 573 | |
| 574 | /// @brief Make sure that the "memcpy" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 575 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 576 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 577 | // Just make sure this has 4 arguments per LLVM spec. |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 578 | return (f->arg_size() == 4); |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 581 | /// Because of alignment and instruction information that we don't have, we |
| 582 | /// leave the bulk of this to the code generators. The optimization here just |
| 583 | /// deals with a few degenerate cases where the length of the string and the |
| 584 | /// alignment match the sizes of our intrinsic types so we can do a load and |
| 585 | /// store instead of the memcpy call. |
| 586 | /// @brief Perform the memcpy optimization. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 587 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 588 | { |
Reid Spencer | 4855ebf | 2005-04-26 19:55:57 +0000 | [diff] [blame] | 589 | // Make sure we have constant int values to work with |
| 590 | ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3)); |
| 591 | if (!LEN) |
| 592 | return false; |
| 593 | ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4)); |
| 594 | if (!ALIGN) |
| 595 | return false; |
| 596 | |
| 597 | // If the length is larger than the alignment, we can't optimize |
| 598 | uint64_t len = LEN->getRawValue(); |
| 599 | uint64_t alignment = ALIGN->getRawValue(); |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 600 | if (len > alignment) |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 601 | return false; |
| 602 | |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 603 | // Get the type we will cast to, based on size of the string |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 604 | Value* dest = ci->getOperand(1); |
| 605 | Value* src = ci->getOperand(2); |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 606 | Type* castType = 0; |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 607 | switch (len) |
| 608 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 609 | case 0: |
Reid Spencer | aaca170 | 2005-04-26 22:46:23 +0000 | [diff] [blame] | 610 | // The memcpy is a no-op so just dump its call. |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 611 | ci->eraseFromParent(); |
| 612 | return true; |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 613 | case 1: castType = Type::SByteTy; break; |
| 614 | case 2: castType = Type::ShortTy; break; |
| 615 | case 4: castType = Type::IntTy; break; |
| 616 | case 8: castType = Type::LongTy; break; |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 617 | default: |
| 618 | return false; |
| 619 | } |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 620 | |
| 621 | // Cast source and dest to the right sized primitive and then load/store |
| 622 | CastInst* SrcCast = |
| 623 | new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci); |
| 624 | CastInst* DestCast = |
| 625 | new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci); |
| 626 | LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 627 | StoreInst* SI = new StoreInst(LI, DestCast, ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 628 | ci->eraseFromParent(); |
| 629 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 630 | } |
| 631 | } MemCpyOptimizer; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 632 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 633 | /// This LibCallOptimization will simplify a call to the memmove library |
| 634 | /// function. It is identical to MemCopyOptimization except for the name of |
| 635 | /// the intrinsic. |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 636 | /// @brief Simplify the memmove library function. |
| 637 | struct MemMoveOptimization : public MemCpyOptimization |
| 638 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 639 | /// @brief Default Constructor |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 640 | MemMoveOptimization() : MemCpyOptimization("llvm.memmove") {} |
| 641 | |
| 642 | } MemMoveOptimizer; |
| 643 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 644 | /// A function to compute the length of a null-terminated constant array of |
| 645 | /// integers. This function can't rely on the size of the constant array |
| 646 | /// because there could be a null terminator in the middle of the array. |
| 647 | /// We also have to bail out if we find a non-integer constant initializer |
| 648 | /// of one of the elements or if there is no null-terminator. The logic |
| 649 | /// below checks each of these conditions and will return true only if all |
| 650 | /// conditions are met. In that case, the \p len parameter is set to the length |
| 651 | /// of the null-terminated string. If false is returned, the conditions were |
| 652 | /// not met and len is set to 0. |
| 653 | /// @brief Get the length of a constant string (null-terminated array). |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 654 | bool getConstantStringLength(Value* V, uint64_t& len ) |
| 655 | { |
| 656 | assert(V != 0 && "Invalid args to getConstantStringLength"); |
| 657 | len = 0; // make sure we initialize this |
| 658 | User* GEP = 0; |
| 659 | // If the value is not a GEP instruction nor a constant expression with a |
| 660 | // GEP instruction, then return false because ConstantArray can't occur |
| 661 | // any other way |
| 662 | if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V)) |
| 663 | GEP = GEPI; |
| 664 | else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V)) |
| 665 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 666 | GEP = CE; |
| 667 | else |
| 668 | return false; |
| 669 | else |
| 670 | return false; |
| 671 | |
| 672 | // Make sure the GEP has exactly three arguments. |
| 673 | if (GEP->getNumOperands() != 3) |
| 674 | return false; |
| 675 | |
| 676 | // Check to make sure that the first operand of the GEP is an integer and |
| 677 | // has value 0 so that we are sure we're indexing into the initializer. |
| 678 | if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1))) |
| 679 | { |
| 680 | if (!op1->isNullValue()) |
| 681 | return false; |
| 682 | } |
| 683 | else |
| 684 | return false; |
| 685 | |
| 686 | // Ensure that the second operand is a ConstantInt. If it isn't then this |
| 687 | // GEP is wonky and we're not really sure what were referencing into and |
| 688 | // better of not optimizing it. While we're at it, get the second index |
| 689 | // value. We'll need this later for indexing the ConstantArray. |
| 690 | uint64_t start_idx = 0; |
| 691 | if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
| 692 | start_idx = CI->getRawValue(); |
| 693 | else |
| 694 | return false; |
| 695 | |
| 696 | // The GEP instruction, constant or instruction, must reference a global |
| 697 | // variable that is a constant and is initialized. The referenced constant |
| 698 | // initializer is the array that we'll use for optimization. |
| 699 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 700 | if (!GV || !GV->isConstant() || !GV->hasInitializer()) |
| 701 | return false; |
| 702 | |
| 703 | // Get the initializer. |
| 704 | Constant* INTLZR = GV->getInitializer(); |
| 705 | |
| 706 | // Handle the ConstantAggregateZero case |
| 707 | if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR)) |
| 708 | { |
| 709 | // This is a degenerate case. The initializer is constant zero so the |
| 710 | // length of the string must be zero. |
| 711 | len = 0; |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | // Must be a Constant Array |
| 716 | ConstantArray* A = dyn_cast<ConstantArray>(INTLZR); |
| 717 | if (!A) |
| 718 | return false; |
| 719 | |
| 720 | // Get the number of elements in the array |
| 721 | uint64_t max_elems = A->getType()->getNumElements(); |
| 722 | |
| 723 | // Traverse the constant array from start_idx (derived above) which is |
| 724 | // the place the GEP refers to in the array. |
| 725 | for ( len = start_idx; len < max_elems; len++) |
| 726 | { |
| 727 | if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len))) |
| 728 | { |
| 729 | // Check for the null terminator |
| 730 | if (CI->isNullValue()) |
| 731 | break; // we found end of string |
| 732 | } |
| 733 | else |
| 734 | return false; // This array isn't suitable, non-int initializer |
| 735 | } |
| 736 | if (len >= max_elems) |
| 737 | return false; // This array isn't null terminated |
| 738 | |
| 739 | // Subtract out the initial value from the length |
| 740 | len -= start_idx; |
| 741 | return true; // success! |
| 742 | } |
| 743 | |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame^] | 744 | // TODO: |
| 745 | // Additional cases that we need to add to this file: |
| 746 | // |
| 747 | // abs: |
| 748 | // * abs(cnst) -> cnst' |
| 749 | // |
| 750 | // atan: |
| 751 | // * atan(0.0) -> 0.0 |
| 752 | // * atan(1.0) -> pi/4 |
| 753 | // |
| 754 | // cbrt: |
| 755 | // * cbrt(constant) -> constant' |
| 756 | // * cbrt(expN(X)) -> expN(x/3) |
| 757 | // * cbrt(sqrt(x)) -> pow(x,1/6) |
| 758 | // * cbrt(sqrt(x)) -> pow(x,1/9) |
| 759 | // |
| 760 | // ceil, ceilf, ceill: |
| 761 | // * ceil(constant) -> constant' |
| 762 | // |
| 763 | // cos, cosf, cosl: |
| 764 | // * cos(0.0) -> 1.0 |
| 765 | // * cox(-x) -> cos(x) |
| 766 | // |
| 767 | // exp, expf, expl: |
| 768 | // * exp(0.0) -> 1.0 |
| 769 | // * exp(int) -> contant' |
| 770 | // * exp(log(x)) -> x |
| 771 | // |
| 772 | // fabs, fabsf, fabsl: |
| 773 | // * fabs(cnst) -> cnst' |
| 774 | // |
| 775 | // ffs, ffsl, ffsll: |
| 776 | // * ffs(cnst) -> cnst' |
| 777 | // |
| 778 | // floor, floorf, floorl: |
| 779 | // * floor(cnst) -> cnst' |
| 780 | // |
| 781 | // fprintf: |
| 782 | // * fprintf(file,fmt) -> fputs(fmt,file) |
| 783 | // (if fmt is constant and constains no % characters) |
| 784 | // * fprintf(file,"%s",str) -> fputs(orig,str) |
| 785 | // (only if the fprintf result is not used) |
| 786 | // * fprintf(file,"%c",chr) -> fputc(chr,file) |
| 787 | // |
| 788 | // fputs: (only if the result is not used) |
| 789 | // * fputs("",F) -> noop |
| 790 | // * fputs(s,F) -> fputc(s[0],F) (if s is constant and strlen(s) == 1) |
| 791 | // * fputs(s,F) -> fwrite(s, 1, len, F) (if s is constant and strlen(s) > 1) |
| 792 | // |
| 793 | // isascii: |
| 794 | // * isascii(c) -> ((c & ~0x7f) == 0) |
| 795 | // |
| 796 | // isdigit: |
| 797 | // * isdigit(c) -> (unsigned)(c) - '0' <= 9 |
| 798 | // |
| 799 | // log, logf, logl: |
| 800 | // * log(1.0) -> 0.0 |
| 801 | // * log(exp(x)) -> x |
| 802 | // * log(x**y) -> y*log(x) |
| 803 | // * log(exp(y)) -> y*log(e) |
| 804 | // * log(exp2(y)) -> y*log(2) |
| 805 | // * log(exp10(y)) -> y*log(10) |
| 806 | // * log(sqrt(x)) -> 0.5*log(x) |
| 807 | // * log(pow(x,y)) -> y*log(x) |
| 808 | // |
| 809 | // lround, lroundf, lroundl: |
| 810 | // * lround(cnst) -> cnst' |
| 811 | // |
| 812 | // memcmp: |
| 813 | // * memcmp(s1,s2,0) -> 0 |
| 814 | // * memcmp(x,x,l) -> 0 |
| 815 | // * memcmp(x,y,l) -> cnst |
| 816 | // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l) |
| 817 | // * memcpy(x,y,1) -> *x - *y |
| 818 | // |
| 819 | // memcpy: |
| 820 | // * memcpy(d,s,0,a) -> d |
| 821 | // |
| 822 | // memmove: |
| 823 | // * memmove(d,s,l,a) -> memcpy(d,s,l,a) |
| 824 | // (if s is a global constant array) |
| 825 | // |
| 826 | // memset: |
| 827 | // * memset(s,c,0) -> noop |
| 828 | // * memset(s,c,n) -> store s, c |
| 829 | // (for n=1,2,4,8) |
| 830 | // |
| 831 | // pow, powf, powl: |
| 832 | // * pow(1.0,y) -> 1.0 |
| 833 | // * pow(x,0.0) -> 1.0 |
| 834 | // * pow(x,1.0) -> x |
| 835 | // * pow(x,-1.0) -> 1.0/x |
| 836 | // * pow(x,0.5) -> sqrt(x) |
| 837 | // * pow(cst1,cst2) -> const1**const2 |
| 838 | // * pow(exp(x),y) -> exp(x*y) |
| 839 | // * pow(sqrt(x),y) -> pow(x,y*0.5) |
| 840 | // * pow(pow(x,y),z)-> pow(x,y*z) |
| 841 | // |
| 842 | // puts: |
| 843 | // * puts("") -> fputc("\n",stdout) (how do we get "stdout"?) |
| 844 | // |
| 845 | // round, roundf, roundl: |
| 846 | // * round(cnst) -> cnst' |
| 847 | // |
| 848 | // signbit: |
| 849 | // * signbit(cnst) -> cnst' |
| 850 | // * signbit(nncst) -> 0 (if pstv is a non-negative constant) |
| 851 | // |
| 852 | // sin, sinf, sinl: |
| 853 | // * sin(0.0) -> 0.0 |
| 854 | // |
| 855 | // sprintf: |
| 856 | // * sprintf(dest,fmt) -> strcpy(dest,fmt) |
| 857 | // (if fmt is constant and constains no % characters) |
| 858 | // * sprintf(dest,"%s",orig) -> strcpy(dest,orig) |
| 859 | // (only if the sprintf result is not used) |
| 860 | // |
| 861 | // sqrt, sqrtf, sqrtl: |
| 862 | // * sqrt(cnst) -> cnst' |
| 863 | // * sqrt(expN(x)) -> expN(x*0.5) |
| 864 | // * sqrt(Nroot(x)) -> pow(x,1/(2*N)) |
| 865 | // * sqrt(pow(x,y)) -> pow(|x|,y*0.5) |
| 866 | // |
| 867 | // strchr, strrchr: |
| 868 | // * strchr(s,c) -> offset_of_in(c,s) |
| 869 | // (if c is a constant integer and s is a constant string) |
| 870 | // * strrchr(s,c) -> reverse_offset_of_in(c,s) |
| 871 | // (if c is a constant integer and s is a constant string) |
| 872 | // * strrchr(s1,0) -> strchr(s1,0) |
| 873 | // |
| 874 | // strcmp: |
| 875 | // * strcmp(x,x) -> 0 |
| 876 | // * strcmp(x,"") -> *x |
| 877 | // * strcmp("",x) -> *x |
| 878 | // * strcmp(x,y) -> cnst (if both x and y are constant strings) |
| 879 | // |
| 880 | // strncat: |
| 881 | // * strncat(x,y,0) -> x |
| 882 | // * strncat(x,y,0) -> x (if strlen(y) = 0) |
| 883 | // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y)) |
| 884 | // |
| 885 | // strncmp: |
| 886 | // * strncmp(x,y,0) -> 0 |
| 887 | // * strncmp(x,x,l) -> 0 |
| 888 | // * strncmp(x,"",l) -> *x |
| 889 | // * strncmp("",x,l) -> *x |
| 890 | // * strncmp(x,y,1) -> *x - *y |
| 891 | // |
| 892 | // strncpy: |
| 893 | // * strncpy(d,s,0) -> d |
| 894 | // * strncpy(d,s,l) -> memcpy(d,s,l,1) |
| 895 | // (if s and l are constants) |
| 896 | // |
| 897 | // strpbrk: |
| 898 | // * strpbrk(s,a) -> offset_in_for(s,a) |
| 899 | // (if s and a are both constant strings) |
| 900 | // * strpbrk(s,"") -> 0 |
| 901 | // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1) |
| 902 | // |
| 903 | // strspn, strcspn: |
| 904 | // * strspn(s,a) -> const_int (if both args are constant) |
| 905 | // * strspn("",a) -> 0 |
| 906 | // * strspn(s,"") -> 0 |
| 907 | // * strcspn(s,a) -> const_int (if both args are constant) |
| 908 | // * strcspn("",a) -> 0 |
| 909 | // * strcspn(s,"") -> strlen(a) |
| 910 | // |
| 911 | // strstr: |
| 912 | // * strstr(x,x) -> x |
| 913 | // * strstr(s1,s2) -> offset_of_s2_in(s1) |
| 914 | // (if s1 and s2 are constant strings) |
| 915 | // |
| 916 | // tan, tanf, tanl: |
| 917 | // * tan(0.0) -> 0.0 |
| 918 | // * tan(atan(x)) -> x |
| 919 | // |
| 920 | // toascii: |
| 921 | // * toascii(c) -> (c & 0x7f) |
| 922 | // |
| 923 | // trunc, truncf, truncl: |
| 924 | // * trunc(cnst) -> cnst' |
| 925 | // |
| 926 | // |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 927 | } |