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", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 38 | "Total number of 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 |
Jeff Cohen | 4bc952f | 2005-04-29 03:05:44 +0000 | [diff] [blame] | 62 | class LibCallOptimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 63 | { |
Jeff Cohen | 4bc952f | 2005-04-29 03:05:44 +0000 | [diff] [blame] | 64 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 65 | /// The \p fname argument must be the name of the library function being |
| 66 | /// optimized by the subclass. |
| 67 | /// @brief Constructor that registers the optimization. |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 68 | LibCallOptimization(const char* fname, const char* description ) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 69 | : func_name(fname) |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 70 | #ifndef NDEBUG |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 71 | , occurrences("simplify-libcalls",description) |
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 | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 75 | optlist[fname] = 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. |
Reid Spencer | 4f01a82 | 2005-05-07 04:59:45 +0000 | [diff] [blame] | 113 | void succeeded() { DEBUG(++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 |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 119 | Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls) |
| 120 | #endif |
| 121 | }; |
| 122 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 123 | /// This class is an LLVM Pass that applies each of the LibCallOptimization |
| 124 | /// instances to all the call sites in a module, relatively efficiently. The |
| 125 | /// purpose of this pass is to provide optimizations for calls to well-known |
| 126 | /// 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] | 127 | /// class provides the basic infrastructure for handling runOnModule. Whenever /// this pass finds a function call, it asks the appropriate optimizer to |
| 128 | /// validate the call (ValidateLibraryCall). If it is validated, then |
| 129 | /// the OptimizeCall method is also called. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 130 | /// @brief A ModulePass for optimizing well-known function calls. |
Jeff Cohen | 4bc952f | 2005-04-29 03:05:44 +0000 | [diff] [blame] | 131 | class SimplifyLibCalls : public ModulePass |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 132 | { |
Jeff Cohen | 4bc952f | 2005-04-29 03:05:44 +0000 | [diff] [blame] | 133 | public: |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 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) |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 166 | // not compiled by LLVM. So, we only act on external functions that |
| 167 | // have external linkage and non-empty uses. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 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. |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 204 | Module* getModule() const { 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. |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 207 | TargetData* getTargetData() const { return TD; } |
| 208 | |
| 209 | /// @brief Return the size_t type -- syntactic shortcut |
| 210 | const Type* getIntPtrType() const { return TD->getIntPtrType(); } |
| 211 | |
| 212 | /// @brief Return a Function* for the fputc libcall |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 213 | Function* get_fputc(const Type* FILEptr_type) |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 214 | { |
| 215 | if (!fputc_func) |
| 216 | { |
| 217 | std::vector<const Type*> args; |
| 218 | args.push_back(Type::IntTy); |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 219 | args.push_back(FILEptr_type); |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 220 | FunctionType* fputc_type = |
| 221 | FunctionType::get(Type::IntTy, args, false); |
| 222 | fputc_func = M->getOrInsertFunction("fputc",fputc_type); |
| 223 | } |
| 224 | return fputc_func; |
| 225 | } |
| 226 | |
| 227 | /// @brief Return a Function* for the fwrite libcall |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 228 | Function* get_fwrite(const Type* FILEptr_type) |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 229 | { |
| 230 | if (!fwrite_func) |
| 231 | { |
| 232 | std::vector<const Type*> args; |
| 233 | args.push_back(PointerType::get(Type::SByteTy)); |
| 234 | args.push_back(TD->getIntPtrType()); |
| 235 | args.push_back(TD->getIntPtrType()); |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 236 | args.push_back(FILEptr_type); |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 237 | FunctionType* fwrite_type = |
| 238 | FunctionType::get(TD->getIntPtrType(), args, false); |
| 239 | fwrite_func = M->getOrInsertFunction("fwrite",fwrite_type); |
| 240 | } |
| 241 | return fwrite_func; |
| 242 | } |
| 243 | |
| 244 | /// @brief Return a Function* for the sqrt libcall |
| 245 | Function* get_sqrt() |
| 246 | { |
| 247 | if (!sqrt_func) |
| 248 | { |
| 249 | std::vector<const Type*> args; |
| 250 | args.push_back(Type::DoubleTy); |
| 251 | FunctionType* sqrt_type = |
| 252 | FunctionType::get(Type::DoubleTy, args, false); |
| 253 | sqrt_func = M->getOrInsertFunction("sqrt",sqrt_type); |
| 254 | } |
| 255 | return sqrt_func; |
| 256 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 257 | |
| 258 | /// @brief Return a Function* for the strlen libcall |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 259 | Function* get_strcpy() |
| 260 | { |
| 261 | if (!strcpy_func) |
| 262 | { |
| 263 | std::vector<const Type*> args; |
| 264 | args.push_back(PointerType::get(Type::SByteTy)); |
| 265 | args.push_back(PointerType::get(Type::SByteTy)); |
| 266 | FunctionType* strcpy_type = |
| 267 | FunctionType::get(PointerType::get(Type::SByteTy), args, false); |
| 268 | strcpy_func = M->getOrInsertFunction("strcpy",strcpy_type); |
| 269 | } |
| 270 | return strcpy_func; |
| 271 | } |
| 272 | |
| 273 | /// @brief Return a Function* for the strlen libcall |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 274 | Function* get_strlen() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 275 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 276 | if (!strlen_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 277 | { |
| 278 | std::vector<const Type*> args; |
| 279 | args.push_back(PointerType::get(Type::SByteTy)); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 280 | FunctionType* strlen_type = |
| 281 | FunctionType::get(TD->getIntPtrType(), args, false); |
| 282 | strlen_func = M->getOrInsertFunction("strlen",strlen_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 283 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 284 | return strlen_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 287 | /// @brief Return a Function* for the memchr libcall |
| 288 | Function* get_memchr() |
| 289 | { |
| 290 | if (!memchr_func) |
| 291 | { |
| 292 | std::vector<const Type*> args; |
| 293 | args.push_back(PointerType::get(Type::SByteTy)); |
| 294 | args.push_back(Type::IntTy); |
| 295 | args.push_back(TD->getIntPtrType()); |
| 296 | FunctionType* memchr_type = FunctionType::get( |
| 297 | PointerType::get(Type::SByteTy), args, false); |
| 298 | memchr_func = M->getOrInsertFunction("memchr",memchr_type); |
| 299 | } |
| 300 | return memchr_func; |
| 301 | } |
| 302 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 303 | /// @brief Return a Function* for the memcpy libcall |
| 304 | Function* get_memcpy() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 305 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 306 | if (!memcpy_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 307 | { |
| 308 | // Note: this is for llvm.memcpy intrinsic |
| 309 | std::vector<const Type*> args; |
| 310 | args.push_back(PointerType::get(Type::SByteTy)); |
| 311 | args.push_back(PointerType::get(Type::SByteTy)); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 312 | args.push_back(Type::UIntTy); |
| 313 | args.push_back(Type::UIntTy); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 314 | FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false); |
| 315 | memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 316 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 317 | return memcpy_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 318 | } |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 319 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 320 | private: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 321 | /// @brief Reset our cached data for a new Module |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 322 | void reset(Module& mod) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 323 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 324 | M = &mod; |
| 325 | TD = &getAnalysis<TargetData>(); |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 326 | fputc_func = 0; |
| 327 | fwrite_func = 0; |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 328 | memcpy_func = 0; |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 329 | memchr_func = 0; |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 330 | sqrt_func = 0; |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 331 | strcpy_func = 0; |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 332 | strlen_func = 0; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 333 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 334 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 335 | private: |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 336 | Function* fputc_func; ///< Cached fputc function |
| 337 | Function* fwrite_func; ///< Cached fwrite function |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 338 | Function* memcpy_func; ///< Cached llvm.memcpy function |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 339 | Function* memchr_func; ///< Cached memchr function |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 340 | Function* sqrt_func; ///< Cached sqrt function |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 341 | Function* strcpy_func; ///< Cached strcpy function |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 342 | Function* strlen_func; ///< Cached strlen function |
| 343 | Module* M; ///< Cached Module |
| 344 | TargetData* TD; ///< Cached TargetData |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | // Register the pass |
| 348 | RegisterOpt<SimplifyLibCalls> |
| 349 | X("simplify-libcalls","Simplify well-known library calls"); |
| 350 | |
| 351 | } // anonymous namespace |
| 352 | |
| 353 | // The only public symbol in this file which just instantiates the pass object |
| 354 | ModulePass *llvm::createSimplifyLibCallsPass() |
| 355 | { |
| 356 | return new SimplifyLibCalls(); |
| 357 | } |
| 358 | |
| 359 | // Classes below here, in the anonymous namespace, are all subclasses of the |
| 360 | // LibCallOptimization class, each implementing all optimizations possible for a |
| 361 | // single well-known library call. Each has a static singleton instance that |
| 362 | // auto registers it into the "optlist" global above. |
| 363 | namespace { |
| 364 | |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 365 | // Forward declare a utility function. |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 366 | bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** A = 0 ); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 367 | |
| 368 | /// This LibCallOptimization will find instances of a call to "exit" that occurs |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 369 | /// 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] | 370 | /// the same value passed to the exit function. When this is done, it splits the |
| 371 | /// basic block at the exit(3) call and deletes the call instruction. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 372 | /// @brief Replace calls to exit in main with a simple return |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 373 | struct ExitInMainOptimization : public LibCallOptimization |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 374 | { |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 375 | ExitInMainOptimization() : LibCallOptimization("exit", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 376 | "Number of 'exit' calls simplified") {} |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 377 | virtual ~ExitInMainOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 378 | |
| 379 | // Make sure the called function looks like exit (int argument, int return |
| 380 | // type, external linkage, not varargs). |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 381 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 382 | { |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 383 | if (f->arg_size() >= 1) |
| 384 | if (f->arg_begin()->getType()->isInteger()) |
| 385 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 389 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 390 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 391 | // To be careful, we check that the call to exit is coming from "main", that |
| 392 | // main has external linkage, and the return type of main and the argument |
| 393 | // to exit have the same type. |
| 394 | Function *from = ci->getParent()->getParent(); |
| 395 | if (from->hasExternalLinkage()) |
| 396 | if (from->getReturnType() == ci->getOperand(1)->getType()) |
| 397 | if (from->getName() == "main") |
| 398 | { |
| 399 | // Okay, time to actually do the optimization. First, get the basic |
| 400 | // block of the call instruction |
| 401 | BasicBlock* bb = ci->getParent(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 402 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 403 | // Create a return instruction that we'll replace the call with. |
| 404 | // Note that the argument of the return is the argument of the call |
| 405 | // instruction. |
| 406 | ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 407 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 408 | // Split the block at the call instruction which places it in a new |
| 409 | // basic block. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 410 | bb->splitBasicBlock(ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 411 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 412 | // The block split caused a branch instruction to be inserted into |
| 413 | // the end of the original block, right after the return instruction |
| 414 | // that we put there. That's not a valid block, so delete the branch |
| 415 | // instruction. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 416 | bb->getInstList().pop_back(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 417 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 418 | // Now we can finally get rid of the call instruction which now lives |
| 419 | // in the new basic block. |
| 420 | ci->eraseFromParent(); |
| 421 | |
| 422 | // Optimization succeeded, return true. |
| 423 | return true; |
| 424 | } |
| 425 | // We didn't pass the criteria for this optimization so return false |
| 426 | return false; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 427 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 428 | } ExitInMainOptimizer; |
| 429 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 430 | /// This LibCallOptimization will simplify a call to the strcat library |
| 431 | /// function. The simplification is possible only if the string being |
| 432 | /// concatenated is a constant array or a constant expression that results in |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 433 | /// a constant string. In this case we can replace it with strlen + llvm.memcpy |
| 434 | /// of the constant string. Both of these calls are further reduced, if possible |
| 435 | /// on subsequent passes. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 436 | /// @brief Simplify the strcat library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 437 | struct StrCatOptimization : public LibCallOptimization |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 438 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 439 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 440 | /// @brief Default constructor |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 441 | StrCatOptimization() : LibCallOptimization("strcat", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 442 | "Number of 'strcat' calls simplified") {} |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 443 | |
| 444 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 445 | /// @breif Destructor |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 446 | virtual ~StrCatOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 447 | |
| 448 | /// @brief Make sure that the "strcat" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 449 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 450 | { |
| 451 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 452 | if (f->arg_size() == 2) |
| 453 | { |
| 454 | Function::const_arg_iterator AI = f->arg_begin(); |
| 455 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 456 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 457 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 458 | // Indicate this is a suitable call type. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 459 | return true; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 460 | } |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 461 | } |
| 462 | return false; |
| 463 | } |
| 464 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 465 | /// @brief Optimize the strcat library function |
| 466 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 467 | { |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 468 | // Extract some information from the instruction |
| 469 | Module* M = ci->getParent()->getParent()->getParent(); |
| 470 | Value* dest = ci->getOperand(1); |
| 471 | Value* src = ci->getOperand(2); |
| 472 | |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 473 | // Extract the initializer (while making numerous checks) from the |
| 474 | // source operand of the call to strcat. If we get null back, one of |
| 475 | // a variety of checks in get_GVInitializer failed |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 476 | uint64_t len = 0; |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 477 | if (!getConstantStringLength(src,len)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 478 | return false; |
| 479 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 480 | // Handle the simple, do-nothing case |
| 481 | if (len == 0) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 482 | { |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 483 | ci->replaceAllUsesWith(dest); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 484 | ci->eraseFromParent(); |
| 485 | return true; |
| 486 | } |
| 487 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 488 | // Increment the length because we actually want to memcpy the null |
| 489 | // terminator as well. |
| 490 | len++; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 491 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 492 | // We need to find the end of the destination string. That's where the |
| 493 | // 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] | 494 | // optimized in another pass). Note that the SLC.get_strlen() call |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 495 | // caches the Function* for us. |
| 496 | CallInst* strlen_inst = |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 497 | new CallInst(SLC.get_strlen(), dest, dest->getName()+".len",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 498 | |
| 499 | // Now that we have the destination's length, we must index into the |
| 500 | // destination's pointer to get the actual memcpy destination (end of |
| 501 | // the string .. we're concatenating). |
| 502 | std::vector<Value*> idx; |
| 503 | idx.push_back(strlen_inst); |
| 504 | GetElementPtrInst* gep = |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 505 | new GetElementPtrInst(dest,idx,dest->getName()+".indexed",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 506 | |
| 507 | // We have enough information to now generate the memcpy call to |
| 508 | // do the concatenation for us. |
| 509 | std::vector<Value*> vals; |
| 510 | vals.push_back(gep); // destination |
| 511 | vals.push_back(ci->getOperand(2)); // source |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 512 | vals.push_back(ConstantUInt::get(Type::UIntTy,len)); // length |
| 513 | vals.push_back(ConstantUInt::get(Type::UIntTy,1)); // alignment |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 514 | new CallInst(SLC.get_memcpy(), vals, "", ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 515 | |
| 516 | // Finally, substitute the first operand of the strcat call for the |
| 517 | // strcat call itself since strcat returns its first operand; and, |
| 518 | // kill the strcat CallInst. |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 519 | ci->replaceAllUsesWith(dest); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 520 | ci->eraseFromParent(); |
| 521 | return true; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 522 | } |
| 523 | } StrCatOptimizer; |
| 524 | |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 525 | /// This LibCallOptimization will simplify a call to the strchr library |
| 526 | /// function. It optimizes out cases where the arguments are both constant |
| 527 | /// and the result can be determined statically. |
| 528 | /// @brief Simplify the strcmp library function. |
| 529 | struct StrChrOptimization : public LibCallOptimization |
| 530 | { |
| 531 | public: |
| 532 | StrChrOptimization() : LibCallOptimization("strchr", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 533 | "Number of 'strchr' calls simplified") {} |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 534 | virtual ~StrChrOptimization() {} |
| 535 | |
| 536 | /// @brief Make sure that the "strchr" function has the right prototype |
| 537 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 538 | { |
| 539 | if (f->getReturnType() == PointerType::get(Type::SByteTy) && |
| 540 | f->arg_size() == 2) |
| 541 | return true; |
| 542 | return false; |
| 543 | } |
| 544 | |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 545 | /// @brief Perform the strchr optimizations |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 546 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 547 | { |
| 548 | // If there aren't three operands, bail |
| 549 | if (ci->getNumOperands() != 3) |
| 550 | return false; |
| 551 | |
| 552 | // Check that the first argument to strchr is a constant array of sbyte. |
| 553 | // If it is, get the length and data, otherwise return false. |
| 554 | uint64_t len = 0; |
| 555 | ConstantArray* CA; |
| 556 | if (!getConstantStringLength(ci->getOperand(1),len,&CA)) |
| 557 | return false; |
| 558 | |
| 559 | // Check that the second argument to strchr is a constant int, return false |
| 560 | // if it isn't |
| 561 | ConstantSInt* CSI = dyn_cast<ConstantSInt>(ci->getOperand(2)); |
| 562 | if (!CSI) |
| 563 | { |
| 564 | // Just lower this to memchr since we know the length of the string as |
| 565 | // it is constant. |
| 566 | Function* f = SLC.get_memchr(); |
| 567 | std::vector<Value*> args; |
| 568 | args.push_back(ci->getOperand(1)); |
| 569 | args.push_back(ci->getOperand(2)); |
| 570 | args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); |
| 571 | ci->replaceAllUsesWith( new CallInst(f,args,ci->getName(),ci)); |
| 572 | ci->eraseFromParent(); |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | // Get the character we're looking for |
| 577 | int64_t chr = CSI->getValue(); |
| 578 | |
| 579 | // Compute the offset |
| 580 | uint64_t offset = 0; |
| 581 | bool char_found = false; |
| 582 | for (uint64_t i = 0; i < len; ++i) |
| 583 | { |
| 584 | if (ConstantSInt* CI = dyn_cast<ConstantSInt>(CA->getOperand(i))) |
| 585 | { |
| 586 | // Check for the null terminator |
| 587 | if (CI->isNullValue()) |
| 588 | break; // we found end of string |
| 589 | else if (CI->getValue() == chr) |
| 590 | { |
| 591 | char_found = true; |
| 592 | offset = i; |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // strchr(s,c) -> offset_of_in(c,s) |
| 599 | // (if c is a constant integer and s is a constant string) |
| 600 | if (char_found) |
| 601 | { |
| 602 | std::vector<Value*> indices; |
| 603 | indices.push_back(ConstantUInt::get(Type::ULongTy,offset)); |
| 604 | GetElementPtrInst* GEP = new GetElementPtrInst(ci->getOperand(1),indices, |
| 605 | ci->getOperand(1)->getName()+".strchr",ci); |
| 606 | ci->replaceAllUsesWith(GEP); |
| 607 | } |
| 608 | else |
| 609 | ci->replaceAllUsesWith( |
| 610 | ConstantPointerNull::get(PointerType::get(Type::SByteTy))); |
| 611 | |
| 612 | ci->eraseFromParent(); |
| 613 | return true; |
| 614 | } |
| 615 | } StrChrOptimizer; |
| 616 | |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 617 | /// This LibCallOptimization will simplify a call to the strcmp library |
| 618 | /// function. It optimizes out cases where one or both arguments are constant |
| 619 | /// and the result can be determined statically. |
| 620 | /// @brief Simplify the strcmp library function. |
| 621 | struct StrCmpOptimization : public LibCallOptimization |
| 622 | { |
| 623 | public: |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 624 | StrCmpOptimization() : LibCallOptimization("strcmp", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 625 | "Number of 'strcmp' calls simplified") {} |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 626 | virtual ~StrCmpOptimization() {} |
| 627 | |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 628 | /// @brief Make sure that the "strcmp" function has the right prototype |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 629 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 630 | { |
| 631 | if (f->getReturnType() == Type::IntTy && f->arg_size() == 2) |
| 632 | return true; |
| 633 | return false; |
| 634 | } |
| 635 | |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 636 | /// @brief Perform the strcmp optimization |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 637 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 638 | { |
| 639 | // First, check to see if src and destination are the same. If they are, |
Reid Spencer | 16449a9 | 2005-04-30 06:45:47 +0000 | [diff] [blame] | 640 | // then the optimization is to replace the CallInst with a constant 0 |
| 641 | // because the call is a no-op. |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 642 | Value* s1 = ci->getOperand(1); |
| 643 | Value* s2 = ci->getOperand(2); |
| 644 | if (s1 == s2) |
| 645 | { |
| 646 | // strcmp(x,x) -> 0 |
| 647 | ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0)); |
| 648 | ci->eraseFromParent(); |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | bool isstr_1 = false; |
| 653 | uint64_t len_1 = 0; |
| 654 | ConstantArray* A1; |
| 655 | if (getConstantStringLength(s1,len_1,&A1)) |
| 656 | { |
| 657 | isstr_1 = true; |
| 658 | if (len_1 == 0) |
| 659 | { |
| 660 | // strcmp("",x) -> *x |
| 661 | LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci); |
| 662 | CastInst* cast = |
| 663 | new CastInst(load,Type::IntTy,ci->getName()+".int",ci); |
| 664 | ci->replaceAllUsesWith(cast); |
| 665 | ci->eraseFromParent(); |
| 666 | return true; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | bool isstr_2 = false; |
| 671 | uint64_t len_2 = 0; |
| 672 | ConstantArray* A2; |
| 673 | if (getConstantStringLength(s2,len_2,&A2)) |
| 674 | { |
| 675 | isstr_2 = true; |
| 676 | if (len_2 == 0) |
| 677 | { |
| 678 | // strcmp(x,"") -> *x |
| 679 | LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci); |
| 680 | CastInst* cast = |
| 681 | new CastInst(load,Type::IntTy,ci->getName()+".int",ci); |
| 682 | ci->replaceAllUsesWith(cast); |
| 683 | ci->eraseFromParent(); |
| 684 | return true; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (isstr_1 && isstr_2) |
| 689 | { |
| 690 | // strcmp(x,y) -> cnst (if both x and y are constant strings) |
| 691 | std::string str1 = A1->getAsString(); |
| 692 | std::string str2 = A2->getAsString(); |
| 693 | int result = strcmp(str1.c_str(), str2.c_str()); |
| 694 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,result)); |
| 695 | ci->eraseFromParent(); |
| 696 | return true; |
| 697 | } |
| 698 | return false; |
| 699 | } |
| 700 | } StrCmpOptimizer; |
| 701 | |
Reid Spencer | 49fa0704 | 2005-05-03 01:43:45 +0000 | [diff] [blame] | 702 | /// This LibCallOptimization will simplify a call to the strncmp library |
| 703 | /// function. It optimizes out cases where one or both arguments are constant |
| 704 | /// and the result can be determined statically. |
| 705 | /// @brief Simplify the strncmp library function. |
| 706 | struct StrNCmpOptimization : public LibCallOptimization |
| 707 | { |
| 708 | public: |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 709 | StrNCmpOptimization() : LibCallOptimization("strncmp", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 710 | "Number of 'strncmp' calls simplified") {} |
Reid Spencer | 49fa0704 | 2005-05-03 01:43:45 +0000 | [diff] [blame] | 711 | virtual ~StrNCmpOptimization() {} |
| 712 | |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 713 | /// @brief Make sure that the "strncmp" function has the right prototype |
Reid Spencer | 49fa0704 | 2005-05-03 01:43:45 +0000 | [diff] [blame] | 714 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 715 | { |
| 716 | if (f->getReturnType() == Type::IntTy && f->arg_size() == 3) |
| 717 | return true; |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | /// @brief Perform the strncpy optimization |
| 722 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 723 | { |
| 724 | // First, check to see if src and destination are the same. If they are, |
| 725 | // then the optimization is to replace the CallInst with a constant 0 |
| 726 | // because the call is a no-op. |
| 727 | Value* s1 = ci->getOperand(1); |
| 728 | Value* s2 = ci->getOperand(2); |
| 729 | if (s1 == s2) |
| 730 | { |
| 731 | // strncmp(x,x,l) -> 0 |
| 732 | ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0)); |
| 733 | ci->eraseFromParent(); |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | // Check the length argument, if it is Constant zero then the strings are |
| 738 | // considered equal. |
| 739 | uint64_t len_arg = 0; |
| 740 | bool len_arg_is_const = false; |
| 741 | if (ConstantInt* len_CI = dyn_cast<ConstantInt>(ci->getOperand(3))) |
| 742 | { |
| 743 | len_arg_is_const = true; |
| 744 | len_arg = len_CI->getRawValue(); |
| 745 | if (len_arg == 0) |
| 746 | { |
| 747 | // strncmp(x,y,0) -> 0 |
| 748 | ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0)); |
| 749 | ci->eraseFromParent(); |
| 750 | return true; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | bool isstr_1 = false; |
| 755 | uint64_t len_1 = 0; |
| 756 | ConstantArray* A1; |
| 757 | if (getConstantStringLength(s1,len_1,&A1)) |
| 758 | { |
| 759 | isstr_1 = true; |
| 760 | if (len_1 == 0) |
| 761 | { |
| 762 | // strncmp("",x) -> *x |
| 763 | LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci); |
| 764 | CastInst* cast = |
| 765 | new CastInst(load,Type::IntTy,ci->getName()+".int",ci); |
| 766 | ci->replaceAllUsesWith(cast); |
| 767 | ci->eraseFromParent(); |
| 768 | return true; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | bool isstr_2 = false; |
| 773 | uint64_t len_2 = 0; |
| 774 | ConstantArray* A2; |
| 775 | if (getConstantStringLength(s2,len_2,&A2)) |
| 776 | { |
| 777 | isstr_2 = true; |
| 778 | if (len_2 == 0) |
| 779 | { |
| 780 | // strncmp(x,"") -> *x |
| 781 | LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci); |
| 782 | CastInst* cast = |
| 783 | new CastInst(load,Type::IntTy,ci->getName()+".int",ci); |
| 784 | ci->replaceAllUsesWith(cast); |
| 785 | ci->eraseFromParent(); |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if (isstr_1 && isstr_2 && len_arg_is_const) |
| 791 | { |
| 792 | // strncmp(x,y,const) -> constant |
| 793 | std::string str1 = A1->getAsString(); |
| 794 | std::string str2 = A2->getAsString(); |
| 795 | int result = strncmp(str1.c_str(), str2.c_str(), len_arg); |
| 796 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,result)); |
| 797 | ci->eraseFromParent(); |
| 798 | return true; |
| 799 | } |
| 800 | return false; |
| 801 | } |
| 802 | } StrNCmpOptimizer; |
| 803 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 804 | /// This LibCallOptimization will simplify a call to the strcpy library |
| 805 | /// function. Two optimizations are possible: |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 806 | /// (1) If src and dest are the same and not volatile, just return dest |
| 807 | /// (2) If the src is a constant then we can convert to llvm.memmove |
| 808 | /// @brief Simplify the strcpy library function. |
| 809 | struct StrCpyOptimization : public LibCallOptimization |
| 810 | { |
| 811 | public: |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 812 | StrCpyOptimization() : LibCallOptimization("strcpy", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 813 | "Number of 'strcpy' calls simplified") {} |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 814 | virtual ~StrCpyOptimization() {} |
| 815 | |
| 816 | /// @brief Make sure that the "strcpy" function has the right prototype |
| 817 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 818 | { |
| 819 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 820 | if (f->arg_size() == 2) |
| 821 | { |
| 822 | Function::const_arg_iterator AI = f->arg_begin(); |
| 823 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 824 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 825 | { |
| 826 | // Indicate this is a suitable call type. |
| 827 | return true; |
| 828 | } |
| 829 | } |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | /// @brief Perform the strcpy optimization |
| 834 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 835 | { |
| 836 | // First, check to see if src and destination are the same. If they are, |
| 837 | // then the optimization is to replace the CallInst with the destination |
| 838 | // because the call is a no-op. Note that this corresponds to the |
| 839 | // degenerate strcpy(X,X) case which should have "undefined" results |
| 840 | // according to the C specification. However, it occurs sometimes and |
| 841 | // we optimize it as a no-op. |
| 842 | Value* dest = ci->getOperand(1); |
| 843 | Value* src = ci->getOperand(2); |
| 844 | if (dest == src) |
| 845 | { |
| 846 | ci->replaceAllUsesWith(dest); |
| 847 | ci->eraseFromParent(); |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | // Get the length of the constant string referenced by the second operand, |
| 852 | // the "src" parameter. Fail the optimization if we can't get the length |
| 853 | // (note that getConstantStringLength does lots of checks to make sure this |
| 854 | // is valid). |
| 855 | uint64_t len = 0; |
| 856 | if (!getConstantStringLength(ci->getOperand(2),len)) |
| 857 | return false; |
| 858 | |
| 859 | // If the constant string's length is zero we can optimize this by just |
| 860 | // doing a store of 0 at the first byte of the destination |
| 861 | if (len == 0) |
| 862 | { |
| 863 | new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci); |
| 864 | ci->replaceAllUsesWith(dest); |
| 865 | ci->eraseFromParent(); |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | // Increment the length because we actually want to memcpy the null |
| 870 | // terminator as well. |
| 871 | len++; |
| 872 | |
| 873 | // Extract some information from the instruction |
| 874 | Module* M = ci->getParent()->getParent()->getParent(); |
| 875 | |
| 876 | // We have enough information to now generate the memcpy call to |
| 877 | // do the concatenation for us. |
| 878 | std::vector<Value*> vals; |
| 879 | vals.push_back(dest); // destination |
| 880 | vals.push_back(src); // source |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 881 | vals.push_back(ConstantUInt::get(Type::UIntTy,len)); // length |
| 882 | vals.push_back(ConstantUInt::get(Type::UIntTy,1)); // alignment |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 883 | new CallInst(SLC.get_memcpy(), vals, "", ci); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 884 | |
| 885 | // Finally, substitute the first operand of the strcat call for the |
| 886 | // strcat call itself since strcat returns its first operand; and, |
| 887 | // kill the strcat CallInst. |
| 888 | ci->replaceAllUsesWith(dest); |
| 889 | ci->eraseFromParent(); |
| 890 | return true; |
| 891 | } |
| 892 | } StrCpyOptimizer; |
| 893 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 894 | /// This LibCallOptimization will simplify a call to the strlen library |
| 895 | /// function by replacing it with a constant value if the string provided to |
| 896 | /// it is a constant array. |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 897 | /// @brief Simplify the strlen library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 898 | struct StrLenOptimization : public LibCallOptimization |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 899 | { |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 900 | StrLenOptimization() : LibCallOptimization("strlen", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 901 | "Number of 'strlen' calls simplified") {} |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 902 | virtual ~StrLenOptimization() {} |
| 903 | |
| 904 | /// @brief Make sure that the "strlen" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 905 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 906 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 907 | if (f->getReturnType() == SLC.getTargetData()->getIntPtrType()) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 908 | if (f->arg_size() == 1) |
| 909 | if (Function::const_arg_iterator AI = f->arg_begin()) |
| 910 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 911 | return true; |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | /// @brief Perform the strlen optimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 916 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 917 | { |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 918 | // Make sure we're dealing with an sbyte* here. |
| 919 | Value* str = ci->getOperand(1); |
| 920 | if (str->getType() != PointerType::get(Type::SByteTy)) |
| 921 | return false; |
| 922 | |
| 923 | // Does the call to strlen have exactly one use? |
| 924 | if (ci->hasOneUse()) |
| 925 | // Is that single use a binary operator? |
| 926 | if (BinaryOperator* bop = dyn_cast<BinaryOperator>(ci->use_back())) |
| 927 | // Is it compared against a constant integer? |
| 928 | if (ConstantInt* CI = dyn_cast<ConstantInt>(bop->getOperand(1))) |
| 929 | { |
| 930 | // Get the value the strlen result is compared to |
| 931 | uint64_t val = CI->getRawValue(); |
| 932 | |
| 933 | // If its compared against length 0 with == or != |
| 934 | if (val == 0 && |
| 935 | (bop->getOpcode() == Instruction::SetEQ || |
| 936 | bop->getOpcode() == Instruction::SetNE)) |
| 937 | { |
| 938 | // strlen(x) != 0 -> *x != 0 |
| 939 | // strlen(x) == 0 -> *x == 0 |
| 940 | LoadInst* load = new LoadInst(str,str->getName()+".first",ci); |
| 941 | BinaryOperator* rbop = BinaryOperator::create(bop->getOpcode(), |
| 942 | load, ConstantSInt::get(Type::SByteTy,0), |
| 943 | bop->getName()+".strlen", ci); |
| 944 | bop->replaceAllUsesWith(rbop); |
| 945 | bop->eraseFromParent(); |
| 946 | ci->eraseFromParent(); |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // Get the length of the constant string operand |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 952 | uint64_t len = 0; |
| 953 | if (!getConstantStringLength(ci->getOperand(1),len)) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 954 | return false; |
| 955 | |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 956 | // strlen("xyz") -> 3 (for example) |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 957 | ci->replaceAllUsesWith( |
| 958 | ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len)); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 959 | ci->eraseFromParent(); |
| 960 | return true; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 961 | } |
| 962 | } StrLenOptimizer; |
| 963 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 964 | /// This LibCallOptimization will simplify a call to the memcpy library |
| 965 | /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8 |
| 966 | /// bytes depending on the length of the string and the alignment. Additional |
| 967 | /// optimizations are possible in code generation (sequence of immediate store) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 968 | /// @brief Simplify the memcpy library function. |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 969 | struct LLVMMemCpyOptimization : public LibCallOptimization |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 970 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 971 | /// @brief Default Constructor |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 972 | LLVMMemCpyOptimization() : LibCallOptimization("llvm.memcpy", |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 973 | "Number of 'llvm.memcpy' calls simplified") {} |
| 974 | |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 975 | protected: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 976 | /// @brief Subclass Constructor |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 977 | LLVMMemCpyOptimization(const char* fname, const char* desc) |
| 978 | : LibCallOptimization(fname, desc) {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 979 | public: |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 980 | /// @brief Destructor |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 981 | virtual ~LLVMMemCpyOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 982 | |
| 983 | /// @brief Make sure that the "memcpy" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 984 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 985 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 986 | // Just make sure this has 4 arguments per LLVM spec. |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 987 | return (f->arg_size() == 4); |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 990 | /// Because of alignment and instruction information that we don't have, we |
| 991 | /// leave the bulk of this to the code generators. The optimization here just |
| 992 | /// deals with a few degenerate cases where the length of the string and the |
| 993 | /// alignment match the sizes of our intrinsic types so we can do a load and |
| 994 | /// store instead of the memcpy call. |
| 995 | /// @brief Perform the memcpy optimization. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 996 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 997 | { |
Reid Spencer | 4855ebf | 2005-04-26 19:55:57 +0000 | [diff] [blame] | 998 | // Make sure we have constant int values to work with |
| 999 | ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3)); |
| 1000 | if (!LEN) |
| 1001 | return false; |
| 1002 | ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4)); |
| 1003 | if (!ALIGN) |
| 1004 | return false; |
| 1005 | |
| 1006 | // If the length is larger than the alignment, we can't optimize |
| 1007 | uint64_t len = LEN->getRawValue(); |
| 1008 | uint64_t alignment = ALIGN->getRawValue(); |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1009 | if (alignment == 0) |
| 1010 | alignment = 1; // Alignment 0 is identity for alignment 1 |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1011 | if (len > alignment) |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 1014 | // 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] | 1015 | Value* dest = ci->getOperand(1); |
| 1016 | Value* src = ci->getOperand(2); |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 1017 | Type* castType = 0; |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 1018 | switch (len) |
| 1019 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1020 | case 0: |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1021 | // memcpy(d,s,0,a) -> noop |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1022 | ci->eraseFromParent(); |
| 1023 | return true; |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 1024 | case 1: castType = Type::SByteTy; break; |
| 1025 | case 2: castType = Type::ShortTy; break; |
| 1026 | case 4: castType = Type::IntTy; break; |
| 1027 | case 8: castType = Type::LongTy; break; |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 1028 | default: |
| 1029 | return false; |
| 1030 | } |
Reid Spencer | 08b4940 | 2005-04-27 17:46:54 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Cast source and dest to the right sized primitive and then load/store |
| 1033 | CastInst* SrcCast = |
| 1034 | new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci); |
| 1035 | CastInst* DestCast = |
| 1036 | new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci); |
| 1037 | LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 1038 | StoreInst* SI = new StoreInst(LI, DestCast, ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 1039 | ci->eraseFromParent(); |
| 1040 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 1041 | } |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1042 | } LLVMMemCpyOptimizer; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1043 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 1044 | /// This LibCallOptimization will simplify a call to the memmove library |
| 1045 | /// function. It is identical to MemCopyOptimization except for the name of |
| 1046 | /// the intrinsic. |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1047 | /// @brief Simplify the memmove library function. |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1048 | struct LLVMMemMoveOptimization : public LLVMMemCpyOptimization |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1049 | { |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 1050 | /// @brief Default Constructor |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1051 | LLVMMemMoveOptimization() : LLVMMemCpyOptimization("llvm.memmove", |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1052 | "Number of 'llvm.memmove' calls simplified") {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1053 | |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1054 | } LLVMMemMoveOptimizer; |
| 1055 | |
| 1056 | /// This LibCallOptimization will simplify a call to the memset library |
| 1057 | /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8 |
| 1058 | /// bytes depending on the length argument. |
| 1059 | struct LLVMMemSetOptimization : public LibCallOptimization |
| 1060 | { |
| 1061 | /// @brief Default Constructor |
| 1062 | LLVMMemSetOptimization() : LibCallOptimization("llvm.memset", |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1063 | "Number of 'llvm.memset' calls simplified") {} |
| 1064 | |
| 1065 | public: |
| 1066 | /// @brief Destructor |
| 1067 | virtual ~LLVMMemSetOptimization() {} |
| 1068 | |
| 1069 | /// @brief Make sure that the "memset" function has the right prototype |
| 1070 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) |
| 1071 | { |
| 1072 | // Just make sure this has 3 arguments per LLVM spec. |
| 1073 | return (f->arg_size() == 4); |
| 1074 | } |
| 1075 | |
| 1076 | /// Because of alignment and instruction information that we don't have, we |
| 1077 | /// leave the bulk of this to the code generators. The optimization here just |
| 1078 | /// deals with a few degenerate cases where the length parameter is constant |
| 1079 | /// and the alignment matches the sizes of our intrinsic types so we can do |
| 1080 | /// store instead of the memcpy call. Other calls are transformed into the |
| 1081 | /// llvm.memset intrinsic. |
| 1082 | /// @brief Perform the memset optimization. |
| 1083 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) |
| 1084 | { |
| 1085 | // Make sure we have constant int values to work with |
| 1086 | ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3)); |
| 1087 | if (!LEN) |
| 1088 | return false; |
| 1089 | ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4)); |
| 1090 | if (!ALIGN) |
| 1091 | return false; |
| 1092 | |
| 1093 | // Extract the length and alignment |
| 1094 | uint64_t len = LEN->getRawValue(); |
| 1095 | uint64_t alignment = ALIGN->getRawValue(); |
| 1096 | |
| 1097 | // Alignment 0 is identity for alignment 1 |
| 1098 | if (alignment == 0) |
| 1099 | alignment = 1; |
| 1100 | |
| 1101 | // If the length is zero, this is a no-op |
| 1102 | if (len == 0) |
| 1103 | { |
| 1104 | // memset(d,c,0,a) -> noop |
| 1105 | ci->eraseFromParent(); |
| 1106 | return true; |
| 1107 | } |
| 1108 | |
| 1109 | // If the length is larger than the alignment, we can't optimize |
| 1110 | if (len > alignment) |
| 1111 | return false; |
| 1112 | |
| 1113 | // Make sure we have a constant ubyte to work with so we can extract |
| 1114 | // the value to be filled. |
| 1115 | ConstantUInt* FILL = dyn_cast<ConstantUInt>(ci->getOperand(2)); |
| 1116 | if (!FILL) |
| 1117 | return false; |
| 1118 | if (FILL->getType() != Type::UByteTy) |
| 1119 | return false; |
| 1120 | |
| 1121 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 1122 | |
| 1123 | // Extract the fill character |
| 1124 | uint64_t fill_char = FILL->getValue(); |
| 1125 | uint64_t fill_value = fill_char; |
| 1126 | |
| 1127 | // Get the type we will cast to, based on size of memory area to fill, and |
| 1128 | // and the value we will store there. |
| 1129 | Value* dest = ci->getOperand(1); |
| 1130 | Type* castType = 0; |
| 1131 | switch (len) |
| 1132 | { |
| 1133 | case 1: |
| 1134 | castType = Type::UByteTy; |
| 1135 | break; |
| 1136 | case 2: |
| 1137 | castType = Type::UShortTy; |
| 1138 | fill_value |= fill_char << 8; |
| 1139 | break; |
| 1140 | case 4: |
| 1141 | castType = Type::UIntTy; |
| 1142 | fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24; |
| 1143 | break; |
| 1144 | case 8: |
| 1145 | castType = Type::ULongTy; |
| 1146 | fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24; |
| 1147 | fill_value |= fill_char << 32 | fill_char << 40 | fill_char << 48; |
| 1148 | fill_value |= fill_char << 56; |
| 1149 | break; |
| 1150 | default: |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | // Cast dest to the right sized primitive and then load/store |
| 1155 | CastInst* DestCast = |
| 1156 | new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci); |
| 1157 | new StoreInst(ConstantUInt::get(castType,fill_value),DestCast, ci); |
| 1158 | ci->eraseFromParent(); |
| 1159 | return true; |
| 1160 | } |
| 1161 | } LLVMMemSetOptimizer; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 1162 | |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1163 | /// This LibCallOptimization will simplify calls to the "pow" library |
| 1164 | /// function. It looks for cases where the result of pow is well known and |
| 1165 | /// substitutes the appropriate value. |
| 1166 | /// @brief Simplify the pow library function. |
| 1167 | struct PowOptimization : public LibCallOptimization |
| 1168 | { |
| 1169 | public: |
| 1170 | /// @brief Default Constructor |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1171 | PowOptimization() : LibCallOptimization("pow", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1172 | "Number of 'pow' calls simplified") {} |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1173 | |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1174 | /// @brief Destructor |
| 1175 | virtual ~PowOptimization() {} |
| 1176 | |
| 1177 | /// @brief Make sure that the "pow" function has the right prototype |
| 1178 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1179 | { |
| 1180 | // Just make sure this has 2 arguments |
| 1181 | return (f->arg_size() == 2); |
| 1182 | } |
| 1183 | |
| 1184 | /// @brief Perform the pow optimization. |
| 1185 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1186 | { |
| 1187 | const Type *Ty = cast<Function>(ci->getOperand(0))->getReturnType(); |
| 1188 | Value* base = ci->getOperand(1); |
| 1189 | Value* expn = ci->getOperand(2); |
| 1190 | if (ConstantFP *Op1 = dyn_cast<ConstantFP>(base)) { |
| 1191 | double Op1V = Op1->getValue(); |
| 1192 | if (Op1V == 1.0) |
| 1193 | { |
| 1194 | // pow(1.0,x) -> 1.0 |
| 1195 | ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0)); |
| 1196 | ci->eraseFromParent(); |
| 1197 | return true; |
| 1198 | } |
| 1199 | } |
| 1200 | else if (ConstantFP* Op2 = dyn_cast<ConstantFP>(expn)) |
| 1201 | { |
| 1202 | double Op2V = Op2->getValue(); |
| 1203 | if (Op2V == 0.0) |
| 1204 | { |
| 1205 | // pow(x,0.0) -> 1.0 |
| 1206 | ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0)); |
| 1207 | ci->eraseFromParent(); |
| 1208 | return true; |
| 1209 | } |
| 1210 | else if (Op2V == 0.5) |
| 1211 | { |
| 1212 | // pow(x,0.5) -> sqrt(x) |
| 1213 | CallInst* sqrt_inst = new CallInst(SLC.get_sqrt(), base, |
| 1214 | ci->getName()+".pow",ci); |
| 1215 | ci->replaceAllUsesWith(sqrt_inst); |
| 1216 | ci->eraseFromParent(); |
| 1217 | return true; |
| 1218 | } |
| 1219 | else if (Op2V == 1.0) |
| 1220 | { |
| 1221 | // pow(x,1.0) -> x |
| 1222 | ci->replaceAllUsesWith(base); |
| 1223 | ci->eraseFromParent(); |
| 1224 | return true; |
| 1225 | } |
| 1226 | else if (Op2V == -1.0) |
| 1227 | { |
| 1228 | // pow(x,-1.0) -> 1.0/x |
| 1229 | BinaryOperator* div_inst= BinaryOperator::create(Instruction::Div, |
| 1230 | ConstantFP::get(Ty,1.0), base, ci->getName()+".pow", ci); |
| 1231 | ci->replaceAllUsesWith(div_inst); |
| 1232 | ci->eraseFromParent(); |
| 1233 | return true; |
| 1234 | } |
| 1235 | } |
| 1236 | return false; // opt failed |
| 1237 | } |
| 1238 | } PowOptimizer; |
| 1239 | |
Reid Spencer | 45bb4af | 2005-05-21 00:39:30 +0000 | [diff] [blame^] | 1240 | /// CastToCStr - Return V if it is an sbyte*, otherwise cast it to sbyte*, |
| 1241 | /// inserting the cast before IP, and return the cast. |
| 1242 | /// @brief Cast a value to a "C" string. |
| 1243 | static Value *CastToCStr(Value *V, Instruction &IP) { |
| 1244 | const Type *SBPTy = PointerType::get(Type::SByteTy); |
| 1245 | if (V->getType() != SBPTy) |
| 1246 | return new CastInst(V, SBPTy, V->getName(), &IP); |
| 1247 | return V; |
| 1248 | } |
| 1249 | |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1250 | /// This LibCallOptimization will simplify calls to the "fprintf" library |
| 1251 | /// function. It looks for cases where the result of fprintf is not used and the |
| 1252 | /// operation can be reduced to something simpler. |
| 1253 | /// @brief Simplify the pow library function. |
| 1254 | struct FPrintFOptimization : public LibCallOptimization |
| 1255 | { |
| 1256 | public: |
| 1257 | /// @brief Default Constructor |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1258 | FPrintFOptimization() : LibCallOptimization("fprintf", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1259 | "Number of 'fprintf' calls simplified") {} |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1260 | |
| 1261 | /// @brief Destructor |
| 1262 | virtual ~FPrintFOptimization() {} |
| 1263 | |
| 1264 | /// @brief Make sure that the "fprintf" function has the right prototype |
| 1265 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1266 | { |
| 1267 | // Just make sure this has at least 2 arguments |
| 1268 | return (f->arg_size() >= 2); |
| 1269 | } |
| 1270 | |
| 1271 | /// @brief Perform the fprintf optimization. |
| 1272 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1273 | { |
| 1274 | // If the call has more than 3 operands, we can't optimize it |
| 1275 | if (ci->getNumOperands() > 4 || ci->getNumOperands() <= 2) |
| 1276 | return false; |
| 1277 | |
| 1278 | // If the result of the fprintf call is used, none of these optimizations |
| 1279 | // can be made. |
| 1280 | if (!ci->hasNUses(0)) |
| 1281 | return false; |
| 1282 | |
| 1283 | // All the optimizations depend on the length of the second argument and the |
| 1284 | // fact that it is a constant string array. Check that now |
| 1285 | uint64_t len = 0; |
| 1286 | ConstantArray* CA = 0; |
| 1287 | if (!getConstantStringLength(ci->getOperand(2), len, &CA)) |
| 1288 | return false; |
| 1289 | |
| 1290 | if (ci->getNumOperands() == 3) |
| 1291 | { |
| 1292 | // Make sure there's no % in the constant array |
| 1293 | for (unsigned i = 0; i < len; ++i) |
| 1294 | { |
| 1295 | if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i))) |
| 1296 | { |
| 1297 | // Check for the null terminator |
| 1298 | if (CI->getRawValue() == '%') |
| 1299 | return false; // we found end of string |
| 1300 | } |
| 1301 | else |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
Reid Spencer | 45bb4af | 2005-05-21 00:39:30 +0000 | [diff] [blame^] | 1305 | // fprintf(file,fmt) -> fwrite(fmt,strlen(fmt),file) |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1306 | const Type* FILEptr_type = ci->getOperand(1)->getType(); |
| 1307 | Function* fwrite_func = SLC.get_fwrite(FILEptr_type); |
| 1308 | if (!fwrite_func) |
| 1309 | return false; |
| 1310 | std::vector<Value*> args; |
| 1311 | args.push_back(ci->getOperand(2)); |
| 1312 | args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); |
| 1313 | args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1)); |
| 1314 | args.push_back(ci->getOperand(1)); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1315 | new CallInst(fwrite_func,args,ci->getName(),ci); |
| 1316 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1317 | ci->eraseFromParent(); |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
| 1321 | // The remaining optimizations require the format string to be length 2 |
| 1322 | // "%s" or "%c". |
| 1323 | if (len != 2) |
| 1324 | return false; |
| 1325 | |
| 1326 | // The first character has to be a % |
| 1327 | if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0))) |
| 1328 | if (CI->getRawValue() != '%') |
| 1329 | return false; |
| 1330 | |
| 1331 | // Get the second character and switch on its value |
| 1332 | ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1)); |
| 1333 | switch (CI->getRawValue()) |
| 1334 | { |
| 1335 | case 's': |
| 1336 | { |
| 1337 | uint64_t len = 0; |
| 1338 | ConstantArray* CA = 0; |
| 1339 | if (!getConstantStringLength(ci->getOperand(3), len, &CA)) |
| 1340 | return false; |
| 1341 | |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1342 | // fprintf(file,"%s",str) -> fwrite(fmt,strlen(fmt),1,file) |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1343 | const Type* FILEptr_type = ci->getOperand(1)->getType(); |
| 1344 | Function* fwrite_func = SLC.get_fwrite(FILEptr_type); |
| 1345 | if (!fwrite_func) |
| 1346 | return false; |
| 1347 | std::vector<Value*> args; |
Reid Spencer | 45bb4af | 2005-05-21 00:39:30 +0000 | [diff] [blame^] | 1348 | args.push_back(CastToCStr(ci->getOperand(3), *ci)); |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1349 | args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); |
| 1350 | args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1)); |
| 1351 | args.push_back(ci->getOperand(1)); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1352 | new CallInst(fwrite_func,args,ci->getName(),ci); |
| 1353 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1354 | break; |
| 1355 | } |
| 1356 | case 'c': |
| 1357 | { |
| 1358 | ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(3)); |
| 1359 | if (!CI) |
| 1360 | return false; |
| 1361 | |
| 1362 | const Type* FILEptr_type = ci->getOperand(1)->getType(); |
| 1363 | Function* fputc_func = SLC.get_fputc(FILEptr_type); |
| 1364 | if (!fputc_func) |
| 1365 | return false; |
| 1366 | CastInst* cast = new CastInst(CI,Type::IntTy,CI->getName()+".int",ci); |
| 1367 | new CallInst(fputc_func,cast,ci->getOperand(1),"",ci); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1368 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1)); |
Reid Spencer | 2d5c7be | 2005-05-02 23:59:26 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | default: |
| 1372 | return false; |
| 1373 | } |
| 1374 | ci->eraseFromParent(); |
| 1375 | return true; |
| 1376 | } |
| 1377 | } FPrintFOptimizer; |
| 1378 | |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1379 | /// This LibCallOptimization will simplify calls to the "sprintf" library |
| 1380 | /// function. It looks for cases where the result of sprintf is not used and the |
| 1381 | /// operation can be reduced to something simpler. |
| 1382 | /// @brief Simplify the pow library function. |
| 1383 | struct SPrintFOptimization : public LibCallOptimization |
| 1384 | { |
| 1385 | public: |
| 1386 | /// @brief Default Constructor |
| 1387 | SPrintFOptimization() : LibCallOptimization("sprintf", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1388 | "Number of 'sprintf' calls simplified") {} |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1389 | |
| 1390 | /// @brief Destructor |
| 1391 | virtual ~SPrintFOptimization() {} |
| 1392 | |
| 1393 | /// @brief Make sure that the "fprintf" function has the right prototype |
| 1394 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1395 | { |
| 1396 | // Just make sure this has at least 2 arguments |
| 1397 | return (f->getReturnType() == Type::IntTy && f->arg_size() >= 2); |
| 1398 | } |
| 1399 | |
| 1400 | /// @brief Perform the sprintf optimization. |
| 1401 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1402 | { |
| 1403 | // If the call has more than 3 operands, we can't optimize it |
| 1404 | if (ci->getNumOperands() > 4 || ci->getNumOperands() < 3) |
| 1405 | return false; |
| 1406 | |
| 1407 | // All the optimizations depend on the length of the second argument and the |
| 1408 | // fact that it is a constant string array. Check that now |
| 1409 | uint64_t len = 0; |
| 1410 | ConstantArray* CA = 0; |
| 1411 | if (!getConstantStringLength(ci->getOperand(2), len, &CA)) |
| 1412 | return false; |
| 1413 | |
| 1414 | if (ci->getNumOperands() == 3) |
| 1415 | { |
| 1416 | if (len == 0) |
| 1417 | { |
| 1418 | // If the length is 0, we just need to store a null byte |
| 1419 | new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci); |
| 1420 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0)); |
| 1421 | ci->eraseFromParent(); |
| 1422 | return true; |
| 1423 | } |
| 1424 | |
| 1425 | // Make sure there's no % in the constant array |
| 1426 | for (unsigned i = 0; i < len; ++i) |
| 1427 | { |
| 1428 | if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i))) |
| 1429 | { |
| 1430 | // Check for the null terminator |
| 1431 | if (CI->getRawValue() == '%') |
| 1432 | return false; // we found a %, can't optimize |
| 1433 | } |
| 1434 | else |
| 1435 | return false; // initializer is not constant int, can't optimize |
| 1436 | } |
| 1437 | |
| 1438 | // Increment length because we want to copy the null byte too |
| 1439 | len++; |
| 1440 | |
| 1441 | // sprintf(str,fmt) -> llvm.memcpy(str,fmt,strlen(fmt),1) |
| 1442 | Function* memcpy_func = SLC.get_memcpy(); |
| 1443 | if (!memcpy_func) |
| 1444 | return false; |
| 1445 | std::vector<Value*> args; |
| 1446 | args.push_back(ci->getOperand(1)); |
| 1447 | args.push_back(ci->getOperand(2)); |
| 1448 | args.push_back(ConstantUInt::get(Type::UIntTy,len)); |
| 1449 | args.push_back(ConstantUInt::get(Type::UIntTy,1)); |
| 1450 | new CallInst(memcpy_func,args,"",ci); |
| 1451 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); |
| 1452 | ci->eraseFromParent(); |
| 1453 | return true; |
| 1454 | } |
| 1455 | |
| 1456 | // The remaining optimizations require the format string to be length 2 |
| 1457 | // "%s" or "%c". |
| 1458 | if (len != 2) |
| 1459 | return false; |
| 1460 | |
| 1461 | // The first character has to be a % |
| 1462 | if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0))) |
| 1463 | if (CI->getRawValue() != '%') |
| 1464 | return false; |
| 1465 | |
| 1466 | // Get the second character and switch on its value |
| 1467 | ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1)); |
| 1468 | switch (CI->getRawValue()) |
| 1469 | { |
| 1470 | case 's': |
| 1471 | { |
| 1472 | uint64_t len = 0; |
| 1473 | if (ci->hasNUses(0)) |
| 1474 | { |
| 1475 | // sprintf(dest,"%s",str) -> strcpy(dest,str) |
| 1476 | Function* strcpy_func = SLC.get_strcpy(); |
| 1477 | if (!strcpy_func) |
| 1478 | return false; |
| 1479 | std::vector<Value*> args; |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 1480 | args.push_back(CastToCStr(ci->getOperand(1), *ci)); |
| 1481 | args.push_back(CastToCStr(ci->getOperand(3), *ci)); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1482 | new CallInst(strcpy_func,args,"",ci); |
| 1483 | } |
| 1484 | else if (getConstantStringLength(ci->getOperand(3),len)) |
| 1485 | { |
| 1486 | // sprintf(dest,"%s",cstr) -> llvm.memcpy(dest,str,strlen(str),1) |
| 1487 | len++; // get the null-terminator |
| 1488 | Function* memcpy_func = SLC.get_memcpy(); |
| 1489 | if (!memcpy_func) |
| 1490 | return false; |
| 1491 | std::vector<Value*> args; |
Chris Lattner | f8053ce | 2005-05-20 22:22:25 +0000 | [diff] [blame] | 1492 | args.push_back(CastToCStr(ci->getOperand(1), *ci)); |
| 1493 | args.push_back(CastToCStr(ci->getOperand(3), *ci)); |
Reid Spencer | 1e520fd | 2005-05-04 03:20:21 +0000 | [diff] [blame] | 1494 | args.push_back(ConstantUInt::get(Type::UIntTy,len)); |
| 1495 | args.push_back(ConstantUInt::get(Type::UIntTy,1)); |
| 1496 | new CallInst(memcpy_func,args,"",ci); |
| 1497 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len)); |
| 1498 | } |
| 1499 | break; |
| 1500 | } |
| 1501 | case 'c': |
| 1502 | { |
| 1503 | // sprintf(dest,"%c",chr) -> store chr, dest |
| 1504 | CastInst* cast = |
| 1505 | new CastInst(ci->getOperand(3),Type::SByteTy,"char",ci); |
| 1506 | new StoreInst(cast, ci->getOperand(1), ci); |
| 1507 | GetElementPtrInst* gep = new GetElementPtrInst(ci->getOperand(1), |
| 1508 | ConstantUInt::get(Type::UIntTy,1),ci->getOperand(1)->getName()+".end", |
| 1509 | ci); |
| 1510 | new StoreInst(ConstantInt::get(Type::SByteTy,0),gep,ci); |
| 1511 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1)); |
| 1512 | break; |
| 1513 | } |
| 1514 | default: |
| 1515 | return false; |
| 1516 | } |
| 1517 | ci->eraseFromParent(); |
| 1518 | return true; |
| 1519 | } |
| 1520 | } SPrintFOptimizer; |
| 1521 | |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1522 | /// This LibCallOptimization will simplify calls to the "fputs" library |
| 1523 | /// function. It looks for cases where the result of fputs is not used and the |
| 1524 | /// operation can be reduced to something simpler. |
| 1525 | /// @brief Simplify the pow library function. |
| 1526 | struct PutsOptimization : public LibCallOptimization |
| 1527 | { |
| 1528 | public: |
| 1529 | /// @brief Default Constructor |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1530 | PutsOptimization() : LibCallOptimization("fputs", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1531 | "Number of 'fputs' calls simplified") {} |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1532 | |
| 1533 | /// @brief Destructor |
| 1534 | virtual ~PutsOptimization() {} |
| 1535 | |
| 1536 | /// @brief Make sure that the "fputs" function has the right prototype |
| 1537 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1538 | { |
| 1539 | // Just make sure this has 2 arguments |
| 1540 | return (f->arg_size() == 2); |
| 1541 | } |
| 1542 | |
| 1543 | /// @brief Perform the fputs optimization. |
| 1544 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1545 | { |
| 1546 | // If the result is used, none of these optimizations work |
| 1547 | if (!ci->hasNUses(0)) |
| 1548 | return false; |
| 1549 | |
| 1550 | // All the optimizations depend on the length of the first argument and the |
| 1551 | // fact that it is a constant string array. Check that now |
| 1552 | uint64_t len = 0; |
| 1553 | if (!getConstantStringLength(ci->getOperand(1), len)) |
| 1554 | return false; |
| 1555 | |
| 1556 | switch (len) |
| 1557 | { |
| 1558 | case 0: |
| 1559 | // fputs("",F) -> noop |
| 1560 | break; |
| 1561 | case 1: |
| 1562 | { |
| 1563 | // fputs(s,F) -> fputc(s[0],F) (if s is constant and strlen(s) == 1) |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1564 | const Type* FILEptr_type = ci->getOperand(2)->getType(); |
| 1565 | Function* fputc_func = SLC.get_fputc(FILEptr_type); |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1566 | if (!fputc_func) |
| 1567 | return false; |
| 1568 | LoadInst* loadi = new LoadInst(ci->getOperand(1), |
| 1569 | ci->getOperand(1)->getName()+".byte",ci); |
| 1570 | CastInst* casti = new CastInst(loadi,Type::IntTy, |
| 1571 | loadi->getName()+".int",ci); |
| 1572 | new CallInst(fputc_func,casti,ci->getOperand(2),"",ci); |
| 1573 | break; |
| 1574 | } |
| 1575 | default: |
| 1576 | { |
| 1577 | // fputs(s,F) -> fwrite(s,1,len,F) (if s is constant and strlen(s) > 1) |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1578 | const Type* FILEptr_type = ci->getOperand(2)->getType(); |
| 1579 | Function* fwrite_func = SLC.get_fwrite(FILEptr_type); |
Reid Spencer | 9361697 | 2005-04-29 09:39:47 +0000 | [diff] [blame] | 1580 | if (!fwrite_func) |
| 1581 | return false; |
| 1582 | std::vector<Value*> parms; |
| 1583 | parms.push_back(ci->getOperand(1)); |
| 1584 | parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),len)); |
| 1585 | parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),1)); |
| 1586 | parms.push_back(ci->getOperand(2)); |
| 1587 | new CallInst(fwrite_func,parms,"",ci); |
| 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | ci->eraseFromParent(); |
| 1592 | return true; // success |
| 1593 | } |
| 1594 | } PutsOptimizer; |
| 1595 | |
Reid Spencer | 282d057 | 2005-05-04 18:58:28 +0000 | [diff] [blame] | 1596 | /// This LibCallOptimization will simplify calls to the "isdigit" library |
| 1597 | /// function. It simply does range checks the parameter explicitly. |
| 1598 | /// @brief Simplify the isdigit library function. |
| 1599 | struct IsDigitOptimization : public LibCallOptimization |
| 1600 | { |
| 1601 | public: |
| 1602 | /// @brief Default Constructor |
| 1603 | IsDigitOptimization() : LibCallOptimization("isdigit", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1604 | "Number of 'isdigit' calls simplified") {} |
Reid Spencer | 282d057 | 2005-05-04 18:58:28 +0000 | [diff] [blame] | 1605 | |
| 1606 | /// @brief Destructor |
| 1607 | virtual ~IsDigitOptimization() {} |
| 1608 | |
| 1609 | /// @brief Make sure that the "fputs" function has the right prototype |
| 1610 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1611 | { |
| 1612 | // Just make sure this has 1 argument |
| 1613 | return (f->arg_size() == 1); |
| 1614 | } |
| 1615 | |
| 1616 | /// @brief Perform the toascii optimization. |
| 1617 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1618 | { |
| 1619 | if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1))) |
| 1620 | { |
| 1621 | // isdigit(c) -> 0 or 1, if 'c' is constant |
| 1622 | uint64_t val = CI->getRawValue(); |
| 1623 | if (val >= '0' && val <='9') |
| 1624 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1)); |
| 1625 | else |
| 1626 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0)); |
| 1627 | ci->eraseFromParent(); |
| 1628 | return true; |
| 1629 | } |
| 1630 | |
| 1631 | // isdigit(c) -> (unsigned)c - '0' <= 9 |
| 1632 | CastInst* cast = |
| 1633 | new CastInst(ci->getOperand(1),Type::UIntTy, |
| 1634 | ci->getOperand(1)->getName()+".uint",ci); |
| 1635 | BinaryOperator* sub_inst = BinaryOperator::create(Instruction::Sub,cast, |
| 1636 | ConstantUInt::get(Type::UIntTy,0x30), |
| 1637 | ci->getOperand(1)->getName()+".sub",ci); |
| 1638 | SetCondInst* setcond_inst = new SetCondInst(Instruction::SetLE,sub_inst, |
| 1639 | ConstantUInt::get(Type::UIntTy,9), |
| 1640 | ci->getOperand(1)->getName()+".cmp",ci); |
| 1641 | CastInst* c2 = |
| 1642 | new CastInst(setcond_inst,Type::IntTy, |
| 1643 | ci->getOperand(1)->getName()+".isdigit",ci); |
| 1644 | ci->replaceAllUsesWith(c2); |
| 1645 | ci->eraseFromParent(); |
| 1646 | return true; |
| 1647 | } |
| 1648 | } IsDigitOptimizer; |
| 1649 | |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1650 | /// This LibCallOptimization will simplify calls to the "toascii" library |
| 1651 | /// function. It simply does the corresponding and operation to restrict the |
| 1652 | /// range of values to the ASCII character set (0-127). |
| 1653 | /// @brief Simplify the toascii library function. |
| 1654 | struct ToAsciiOptimization : public LibCallOptimization |
| 1655 | { |
| 1656 | public: |
| 1657 | /// @brief Default Constructor |
Reid Spencer | 95d8efd | 2005-05-03 02:54:54 +0000 | [diff] [blame] | 1658 | ToAsciiOptimization() : LibCallOptimization("toascii", |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1659 | "Number of 'toascii' calls simplified") {} |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1660 | |
| 1661 | /// @brief Destructor |
| 1662 | virtual ~ToAsciiOptimization() {} |
| 1663 | |
| 1664 | /// @brief Make sure that the "fputs" function has the right prototype |
| 1665 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1666 | { |
| 1667 | // Just make sure this has 2 arguments |
| 1668 | return (f->arg_size() == 1); |
| 1669 | } |
| 1670 | |
| 1671 | /// @brief Perform the toascii optimization. |
| 1672 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1673 | { |
| 1674 | // toascii(c) -> (c & 0x7f) |
| 1675 | Value* chr = ci->getOperand(1); |
| 1676 | BinaryOperator* and_inst = BinaryOperator::create(Instruction::And,chr, |
| 1677 | ConstantInt::get(chr->getType(),0x7F),ci->getName()+".toascii",ci); |
| 1678 | ci->replaceAllUsesWith(and_inst); |
| 1679 | ci->eraseFromParent(); |
| 1680 | return true; |
| 1681 | } |
| 1682 | } ToAsciiOptimizer; |
| 1683 | |
Reid Spencer | b195fcd | 2005-05-14 16:42:52 +0000 | [diff] [blame] | 1684 | /// This LibCallOptimization will simplify calls to the "ffs" library |
| 1685 | /// calls which find the first set bit in an int, long, or long long. The |
| 1686 | /// optimization is to compute the result at compile time if the argument is |
| 1687 | /// a constant. |
| 1688 | /// @brief Simplify the ffs library function. |
| 1689 | struct FFSOptimization : public LibCallOptimization |
| 1690 | { |
| 1691 | protected: |
| 1692 | /// @brief Subclass Constructor |
| 1693 | FFSOptimization(const char* funcName, const char* description) |
| 1694 | : LibCallOptimization(funcName, description) |
| 1695 | {} |
| 1696 | |
| 1697 | public: |
| 1698 | /// @brief Default Constructor |
| 1699 | FFSOptimization() : LibCallOptimization("ffs", |
| 1700 | "Number of 'ffs' calls simplified") {} |
| 1701 | |
| 1702 | /// @brief Destructor |
| 1703 | virtual ~FFSOptimization() {} |
| 1704 | |
| 1705 | /// @brief Make sure that the "fputs" function has the right prototype |
| 1706 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 1707 | { |
| 1708 | // Just make sure this has 2 arguments |
| 1709 | return (f->arg_size() == 1 && f->getReturnType() == Type::IntTy); |
| 1710 | } |
| 1711 | |
| 1712 | /// @brief Perform the ffs optimization. |
| 1713 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 1714 | { |
| 1715 | if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1))) |
| 1716 | { |
| 1717 | // ffs(cnst) -> bit# |
| 1718 | // ffsl(cnst) -> bit# |
Reid Spencer | 17f7784 | 2005-05-15 21:19:45 +0000 | [diff] [blame] | 1719 | // ffsll(cnst) -> bit# |
Reid Spencer | b195fcd | 2005-05-14 16:42:52 +0000 | [diff] [blame] | 1720 | uint64_t val = CI->getRawValue(); |
Reid Spencer | 17f7784 | 2005-05-15 21:19:45 +0000 | [diff] [blame] | 1721 | int result = 0; |
| 1722 | while (val != 0) { |
| 1723 | result +=1; |
| 1724 | if (val&1) |
| 1725 | break; |
| 1726 | val >>= 1; |
| 1727 | } |
Reid Spencer | b195fcd | 2005-05-14 16:42:52 +0000 | [diff] [blame] | 1728 | ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy, result)); |
| 1729 | ci->eraseFromParent(); |
| 1730 | return true; |
| 1731 | } |
Reid Spencer | 17f7784 | 2005-05-15 21:19:45 +0000 | [diff] [blame] | 1732 | |
| 1733 | // ffs(x) -> ( x == 0 ? 0 : llvm.cttz(x)+1) |
| 1734 | // ffsl(x) -> ( x == 0 ? 0 : llvm.cttz(x)+1) |
| 1735 | // ffsll(x) -> ( x == 0 ? 0 : llvm.cttz(x)+1) |
| 1736 | const Type* arg_type = ci->getOperand(1)->getType(); |
| 1737 | std::vector<const Type*> args; |
| 1738 | args.push_back(arg_type); |
| 1739 | FunctionType* llvm_cttz_type = FunctionType::get(arg_type,args,false); |
| 1740 | Function* F = |
| 1741 | SLC.getModule()->getOrInsertFunction("llvm.cttz",llvm_cttz_type); |
| 1742 | std::string inst_name(ci->getName()+".ffs"); |
| 1743 | Instruction* call = |
| 1744 | new CallInst(F, ci->getOperand(1), inst_name, ci); |
| 1745 | if (arg_type != Type::IntTy) |
| 1746 | call = new CastInst(call, Type::IntTy, inst_name, ci); |
| 1747 | BinaryOperator* add = BinaryOperator::create(Instruction::Add, call, |
| 1748 | ConstantSInt::get(Type::IntTy,1), inst_name, ci); |
| 1749 | SetCondInst* eq = new SetCondInst(Instruction::SetEQ,ci->getOperand(1), |
| 1750 | ConstantSInt::get(ci->getOperand(1)->getType(),0),inst_name,ci); |
| 1751 | SelectInst* select = new SelectInst(eq,ConstantSInt::get(Type::IntTy,0),add, |
| 1752 | inst_name,ci); |
| 1753 | ci->replaceAllUsesWith(select); |
| 1754 | ci->eraseFromParent(); |
| 1755 | return true; |
Reid Spencer | b195fcd | 2005-05-14 16:42:52 +0000 | [diff] [blame] | 1756 | } |
| 1757 | } FFSOptimizer; |
| 1758 | |
| 1759 | /// This LibCallOptimization will simplify calls to the "ffsl" library |
| 1760 | /// calls. It simply uses FFSOptimization for which the transformation is |
| 1761 | /// identical. |
| 1762 | /// @brief Simplify the ffsl library function. |
| 1763 | struct FFSLOptimization : public FFSOptimization |
| 1764 | { |
| 1765 | public: |
| 1766 | /// @brief Default Constructor |
| 1767 | FFSLOptimization() : FFSOptimization("ffsl", |
| 1768 | "Number of 'ffsl' calls simplified") {} |
| 1769 | |
| 1770 | } FFSLOptimizer; |
| 1771 | |
| 1772 | /// This LibCallOptimization will simplify calls to the "ffsll" library |
| 1773 | /// calls. It simply uses FFSOptimization for which the transformation is |
| 1774 | /// identical. |
| 1775 | /// @brief Simplify the ffsl library function. |
| 1776 | struct FFSLLOptimization : public FFSOptimization |
| 1777 | { |
| 1778 | public: |
| 1779 | /// @brief Default Constructor |
| 1780 | FFSLLOptimization() : FFSOptimization("ffsll", |
| 1781 | "Number of 'ffsll' calls simplified") {} |
| 1782 | |
| 1783 | } FFSLLOptimizer; |
| 1784 | |
Reid Spencer | 7ddcfb3 | 2005-04-27 21:29:20 +0000 | [diff] [blame] | 1785 | /// A function to compute the length of a null-terminated constant array of |
| 1786 | /// integers. This function can't rely on the size of the constant array |
| 1787 | /// because there could be a null terminator in the middle of the array. |
| 1788 | /// We also have to bail out if we find a non-integer constant initializer |
| 1789 | /// of one of the elements or if there is no null-terminator. The logic |
| 1790 | /// below checks each of these conditions and will return true only if all |
| 1791 | /// conditions are met. In that case, the \p len parameter is set to the length |
| 1792 | /// of the null-terminated string. If false is returned, the conditions were |
| 1793 | /// not met and len is set to 0. |
| 1794 | /// @brief Get the length of a constant string (null-terminated array). |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1795 | bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** CA ) |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 1796 | { |
| 1797 | assert(V != 0 && "Invalid args to getConstantStringLength"); |
| 1798 | len = 0; // make sure we initialize this |
| 1799 | User* GEP = 0; |
| 1800 | // If the value is not a GEP instruction nor a constant expression with a |
| 1801 | // GEP instruction, then return false because ConstantArray can't occur |
| 1802 | // any other way |
| 1803 | if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V)) |
| 1804 | GEP = GEPI; |
| 1805 | else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V)) |
| 1806 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 1807 | GEP = CE; |
| 1808 | else |
| 1809 | return false; |
| 1810 | else |
| 1811 | return false; |
| 1812 | |
| 1813 | // Make sure the GEP has exactly three arguments. |
| 1814 | if (GEP->getNumOperands() != 3) |
| 1815 | return false; |
| 1816 | |
| 1817 | // Check to make sure that the first operand of the GEP is an integer and |
| 1818 | // has value 0 so that we are sure we're indexing into the initializer. |
| 1819 | if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1))) |
| 1820 | { |
| 1821 | if (!op1->isNullValue()) |
| 1822 | return false; |
| 1823 | } |
| 1824 | else |
| 1825 | return false; |
| 1826 | |
| 1827 | // Ensure that the second operand is a ConstantInt. If it isn't then this |
| 1828 | // GEP is wonky and we're not really sure what were referencing into and |
| 1829 | // better of not optimizing it. While we're at it, get the second index |
| 1830 | // value. We'll need this later for indexing the ConstantArray. |
| 1831 | uint64_t start_idx = 0; |
| 1832 | if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
| 1833 | start_idx = CI->getRawValue(); |
| 1834 | else |
| 1835 | return false; |
| 1836 | |
| 1837 | // The GEP instruction, constant or instruction, must reference a global |
| 1838 | // variable that is a constant and is initialized. The referenced constant |
| 1839 | // initializer is the array that we'll use for optimization. |
| 1840 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 1841 | if (!GV || !GV->isConstant() || !GV->hasInitializer()) |
| 1842 | return false; |
| 1843 | |
| 1844 | // Get the initializer. |
| 1845 | Constant* INTLZR = GV->getInitializer(); |
| 1846 | |
| 1847 | // Handle the ConstantAggregateZero case |
| 1848 | if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR)) |
| 1849 | { |
| 1850 | // This is a degenerate case. The initializer is constant zero so the |
| 1851 | // length of the string must be zero. |
| 1852 | len = 0; |
| 1853 | return true; |
| 1854 | } |
| 1855 | |
| 1856 | // Must be a Constant Array |
| 1857 | ConstantArray* A = dyn_cast<ConstantArray>(INTLZR); |
| 1858 | if (!A) |
| 1859 | return false; |
| 1860 | |
| 1861 | // Get the number of elements in the array |
| 1862 | uint64_t max_elems = A->getType()->getNumElements(); |
| 1863 | |
| 1864 | // Traverse the constant array from start_idx (derived above) which is |
| 1865 | // the place the GEP refers to in the array. |
| 1866 | for ( len = start_idx; len < max_elems; len++) |
| 1867 | { |
| 1868 | if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len))) |
| 1869 | { |
| 1870 | // Check for the null terminator |
| 1871 | if (CI->isNullValue()) |
| 1872 | break; // we found end of string |
| 1873 | } |
| 1874 | else |
| 1875 | return false; // This array isn't suitable, non-int initializer |
| 1876 | } |
| 1877 | if (len >= max_elems) |
| 1878 | return false; // This array isn't null terminated |
| 1879 | |
| 1880 | // Subtract out the initial value from the length |
| 1881 | len -= start_idx; |
Reid Spencer | 4c444fe | 2005-04-30 03:17:54 +0000 | [diff] [blame] | 1882 | if (CA) |
| 1883 | *CA = A; |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 1884 | return true; // success! |
| 1885 | } |
| 1886 | |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1887 | // TODO: |
| 1888 | // Additional cases that we need to add to this file: |
| 1889 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1890 | // cbrt: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1891 | // * cbrt(expN(X)) -> expN(x/3) |
| 1892 | // * cbrt(sqrt(x)) -> pow(x,1/6) |
| 1893 | // * cbrt(sqrt(x)) -> pow(x,1/9) |
| 1894 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1895 | // cos, cosf, cosl: |
Reid Spencer | 16983ca | 2005-04-28 18:05:16 +0000 | [diff] [blame] | 1896 | // * cos(-x) -> cos(x) |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1897 | // |
| 1898 | // exp, expf, expl: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1899 | // * exp(log(x)) -> x |
| 1900 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1901 | // isascii: |
| 1902 | // * isascii(c) -> ((c & ~0x7f) == 0) |
| 1903 | // |
| 1904 | // isdigit: |
| 1905 | // * isdigit(c) -> (unsigned)(c) - '0' <= 9 |
| 1906 | // |
| 1907 | // log, logf, logl: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1908 | // * log(exp(x)) -> x |
| 1909 | // * log(x**y) -> y*log(x) |
| 1910 | // * log(exp(y)) -> y*log(e) |
| 1911 | // * log(exp2(y)) -> y*log(2) |
| 1912 | // * log(exp10(y)) -> y*log(10) |
| 1913 | // * log(sqrt(x)) -> 0.5*log(x) |
| 1914 | // * log(pow(x,y)) -> y*log(x) |
| 1915 | // |
| 1916 | // lround, lroundf, lroundl: |
| 1917 | // * lround(cnst) -> cnst' |
| 1918 | // |
| 1919 | // memcmp: |
| 1920 | // * memcmp(s1,s2,0) -> 0 |
| 1921 | // * memcmp(x,x,l) -> 0 |
| 1922 | // * memcmp(x,y,l) -> cnst |
| 1923 | // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l) |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1924 | // * memcmp(x,y,1) -> *x - *y |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1925 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1926 | // memmove: |
| 1927 | // * memmove(d,s,l,a) -> memcpy(d,s,l,a) |
| 1928 | // (if s is a global constant array) |
| 1929 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1930 | // pow, powf, powl: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1931 | // * pow(exp(x),y) -> exp(x*y) |
| 1932 | // * pow(sqrt(x),y) -> pow(x,y*0.5) |
| 1933 | // * pow(pow(x,y),z)-> pow(x,y*z) |
| 1934 | // |
| 1935 | // puts: |
| 1936 | // * puts("") -> fputc("\n",stdout) (how do we get "stdout"?) |
| 1937 | // |
| 1938 | // round, roundf, roundl: |
| 1939 | // * round(cnst) -> cnst' |
| 1940 | // |
| 1941 | // signbit: |
| 1942 | // * signbit(cnst) -> cnst' |
| 1943 | // * signbit(nncst) -> 0 (if pstv is a non-negative constant) |
| 1944 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1945 | // sqrt, sqrtf, sqrtl: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1946 | // * sqrt(expN(x)) -> expN(x*0.5) |
| 1947 | // * sqrt(Nroot(x)) -> pow(x,1/(2*N)) |
| 1948 | // * sqrt(pow(x,y)) -> pow(|x|,y*0.5) |
| 1949 | // |
Reid Spencer | 170ae7f | 2005-05-07 20:15:59 +0000 | [diff] [blame] | 1950 | // stpcpy: |
| 1951 | // * stpcpy(str, "literal") -> |
| 1952 | // llvm.memcpy(str,"literal",strlen("literal")+1,1) |
Reid Spencer | 38cabd7 | 2005-05-03 07:23:44 +0000 | [diff] [blame] | 1953 | // strrchr: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1954 | // * strrchr(s,c) -> reverse_offset_of_in(c,s) |
| 1955 | // (if c is a constant integer and s is a constant string) |
| 1956 | // * strrchr(s1,0) -> strchr(s1,0) |
| 1957 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1958 | // strncat: |
| 1959 | // * strncat(x,y,0) -> x |
| 1960 | // * strncat(x,y,0) -> x (if strlen(y) = 0) |
| 1961 | // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y)) |
| 1962 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1963 | // strncpy: |
| 1964 | // * strncpy(d,s,0) -> d |
| 1965 | // * strncpy(d,s,l) -> memcpy(d,s,l,1) |
| 1966 | // (if s and l are constants) |
| 1967 | // |
| 1968 | // strpbrk: |
| 1969 | // * strpbrk(s,a) -> offset_in_for(s,a) |
| 1970 | // (if s and a are both constant strings) |
| 1971 | // * strpbrk(s,"") -> 0 |
| 1972 | // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1) |
| 1973 | // |
| 1974 | // strspn, strcspn: |
| 1975 | // * strspn(s,a) -> const_int (if both args are constant) |
| 1976 | // * strspn("",a) -> 0 |
| 1977 | // * strspn(s,"") -> 0 |
| 1978 | // * strcspn(s,a) -> const_int (if both args are constant) |
| 1979 | // * strcspn("",a) -> 0 |
| 1980 | // * strcspn(s,"") -> strlen(a) |
| 1981 | // |
| 1982 | // strstr: |
| 1983 | // * strstr(x,x) -> x |
| 1984 | // * strstr(s1,s2) -> offset_of_s2_in(s1) |
| 1985 | // (if s1 and s2 are constant strings) |
| 1986 | // |
| 1987 | // tan, tanf, tanl: |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1988 | // * tan(atan(x)) -> x |
| 1989 | // |
Reid Spencer | 649ac28 | 2005-04-28 04:40:06 +0000 | [diff] [blame] | 1990 | // trunc, truncf, truncl: |
| 1991 | // * trunc(cnst) -> cnst' |
| 1992 | // |
| 1993 | // |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 1994 | } |