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