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