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