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