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