Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 1 | //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===// |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a variety of small optimizations for calls to specific |
| 11 | // well-known (e.g. runtime library) function calls. For example, a call to the |
| 12 | // function "exit(3)" that occurs within the main() function can be transformed |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 13 | // into a simple "return 3" instruction. Any optimization that takes this form |
| 14 | // (replace call to library function with simpler code that provides same |
| 15 | // result) belongs in this file. |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | 18b9981 | 2005-04-26 23:05:17 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "simplify-libcalls" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Instructions.h" |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/hash_map" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Support/Debug.h" |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/IPO.h" |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 30 | #include <iostream> |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 34 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 35 | /// This statistic keeps track of the total number of library calls that have |
| 36 | /// been simplified regardless of which call it is. |
| 37 | Statistic<> SimplifiedLibCalls("simplify-libcalls", |
| 38 | "Number of well-known library calls simplified"); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 39 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 40 | /// @brief The list of optimizations deriving from LibCallOptimization |
| 41 | class LibCallOptimization; |
| 42 | class SimplifyLibCalls; |
| 43 | hash_map<std::string,LibCallOptimization*> optlist; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 44 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 45 | /// This class is the abstract base class for the set of optimizations that |
| 46 | /// corresponds to one library call. The SimplifyLibCall pass will call the |
| 47 | /// ValidateCalledFunction method to ask the optimization if a given Function |
| 48 | /// is the kind that the optimization can handle. It will also call the |
| 49 | /// OptimizeCall method to perform, or attempt to perform, the optimization(s) |
| 50 | /// for the library call. Subclasses of this class are located toward the |
| 51 | /// end of this file. |
| 52 | /// @brief Base class for library call optimizations |
| 53 | struct LibCallOptimization |
| 54 | { |
| 55 | /// @brief Constructor that registers the optimization. The \p fname argument |
| 56 | /// must be the name of the library function being optimized by the subclass. |
| 57 | LibCallOptimization(const char * fname ) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 58 | : func_name(fname) |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 59 | #ifndef NDEBUG |
Reid Spencer | dc11db6 | 2005-04-27 00:20:23 +0000 | [diff] [blame] | 60 | , stat_name(std::string("simplify-libcalls:")+fname) |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 61 | , occurrences(stat_name.c_str(),"Number of calls simplified") |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 62 | #endif |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 63 | { |
| 64 | // Register this call optimizer |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 65 | optlist[func_name] = this; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 68 | /// @brief Destructor |
| 69 | virtual ~LibCallOptimization() {} |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 70 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 71 | /// The implementation of this function in subclasses should determine if |
| 72 | /// \p F is suitable for the optimization. This method is called by |
| 73 | /// runOnModule to short circuit visiting all the call sites of such a |
| 74 | /// function if that function is not suitable in the first place. |
| 75 | /// If the called function is suitabe, this method should return true; |
| 76 | /// false, otherwise. This function should also perform any lazy |
| 77 | /// initialization that the LibCallOptimization needs to do, if its to return |
| 78 | /// true. This avoids doing initialization until the optimizer is actually |
| 79 | /// going to be called upon to do some optimization. |
| 80 | virtual bool ValidateCalledFunction( |
| 81 | const Function* F, ///< The function that is the target of call sites |
| 82 | SimplifyLibCalls& SLC ///< The pass object invoking us |
| 83 | ) = 0; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 84 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 85 | /// The implementations of this function in subclasses is the heart of the |
| 86 | /// SimplifyLibCalls algorithm. Sublcasses of this class implement |
| 87 | /// OptimizeCall to determine if (a) the conditions are right for optimizing |
| 88 | /// the call and (b) to perform the optimization. If an action is taken |
| 89 | /// against ci, the subclass is responsible for returning true and ensuring |
| 90 | /// that ci is erased from its parent. |
| 91 | /// @param ci the call instruction under consideration |
| 92 | /// @param f the function that ci calls. |
| 93 | /// @brief Optimize a call, if possible. |
| 94 | virtual bool OptimizeCall( |
| 95 | CallInst* ci, ///< The call instruction that should be optimized. |
| 96 | SimplifyLibCalls& SLC ///< The pass object invoking us |
| 97 | ) = 0; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 98 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 99 | /// @brief Get the name of the library call being optimized |
| 100 | const char * getFunctionName() const { return func_name; } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 101 | |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 102 | #ifndef NDEBUG |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 103 | void occurred() { ++occurrences; } |
Reid Spencer | e95a647 | 2005-04-27 00:05:45 +0000 | [diff] [blame] | 104 | #endif |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | const char* func_name; ///< Name of the library call we optimize |
| 108 | #ifndef NDEBUG |
| 109 | std::string stat_name; ///< Holder for debug statistic name |
| 110 | Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls) |
| 111 | #endif |
| 112 | }; |
| 113 | |
| 114 | /// This class is the base class for a set of small but important |
| 115 | /// optimizations of calls to well-known functions, such as those in the c |
| 116 | /// library. |
| 117 | |
| 118 | /// This class is an LLVM Pass that applies each of the LibCallOptimization |
| 119 | /// instances to all the call sites in a module, relatively efficiently. The |
| 120 | /// purpose of this pass is to provide optimizations for calls to well-known |
| 121 | /// functions with well-known semantics, such as those in the c library. The |
| 122 | /// class provides the basic infrastructure for handling runOnModule. |
| 123 | /// Whenever this pass finds a function call, it asks the subclasses to |
| 124 | /// validate the call by calling ValidateLibraryCall. If it is validated, then |
| 125 | /// the OptimizeCall method is called. |
| 126 | /// @brief A ModulePass for optimizing well-known function calls. |
| 127 | struct SimplifyLibCalls : public ModulePass |
| 128 | { |
| 129 | /// We need some target data for accurate signature details that are |
| 130 | /// target dependent. So we require target data in our AnalysisUsage. |
| 131 | virtual void getAnalysisUsage(AnalysisUsage& Info) const |
| 132 | { |
| 133 | // Ask that the TargetData analysis be performed before us so we can use |
| 134 | // the target data. |
| 135 | Info.addRequired<TargetData>(); |
| 136 | } |
| 137 | |
| 138 | /// For this pass, process all of the function calls in the module, calling |
| 139 | /// ValidateLibraryCall and OptimizeCall as appropriate. |
| 140 | virtual bool runOnModule(Module &M) |
| 141 | { |
| 142 | reset(M); |
| 143 | |
| 144 | bool result = false; |
| 145 | |
| 146 | // The call optimizations can be recursive. That is, the optimization might |
| 147 | // generate a call to another function which can also be optimized. This way |
| 148 | // we make the LibCallOptimization instances very specific to the case they |
| 149 | // handle. It also means we need to keep running over the function calls in |
| 150 | // the module until we don't get any more optimizations possible. |
| 151 | bool found_optimization = false; |
| 152 | do |
| 153 | { |
| 154 | found_optimization = false; |
| 155 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 156 | { |
| 157 | // All the "well-known" functions are external and have external linkage |
| 158 | // because they live in a runtime library somewhere and were (probably) |
| 159 | // not compiled by LLVM. So, we only act on external functions that have |
| 160 | // external linkage and non-empty uses. |
| 161 | if (!FI->isExternal() || !FI->hasExternalLinkage() || FI->use_empty()) |
| 162 | continue; |
| 163 | |
| 164 | // Get the optimization class that pertains to this function |
| 165 | LibCallOptimization* CO = optlist[FI->getName().c_str()]; |
| 166 | if (!CO) |
| 167 | continue; |
| 168 | |
| 169 | // Make sure the called function is suitable for the optimization |
| 170 | if (!CO->ValidateCalledFunction(FI,*this)) |
| 171 | continue; |
| 172 | |
| 173 | // Loop over each of the uses of the function |
| 174 | for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end(); |
| 175 | UI != UE ; ) |
| 176 | { |
| 177 | // If the use of the function is a call instruction |
| 178 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 179 | { |
| 180 | // Do the optimization on the LibCallOptimization. |
| 181 | if (CO->OptimizeCall(CI,*this)) |
| 182 | { |
| 183 | ++SimplifiedLibCalls; |
| 184 | found_optimization = result = true; |
| 185 | #ifndef NDEBUG |
| 186 | CO->occurred(); |
| 187 | #endif |
| 188 | } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 192 | } while (found_optimization); |
| 193 | return result; |
| 194 | } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 195 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 196 | /// @brief Return the *current* module we're working on. |
| 197 | Module* getModule() { return M; } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 198 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 199 | /// @brief Return the *current* target data for the module we're working on. |
| 200 | TargetData* getTargetData() { return TD; } |
| 201 | |
| 202 | /// @brief Return a Function* for the strlen libcall |
| 203 | Function* get_strlen() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 204 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 205 | if (!strlen_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 206 | { |
| 207 | std::vector<const Type*> args; |
| 208 | args.push_back(PointerType::get(Type::SByteTy)); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 209 | FunctionType* strlen_type = |
| 210 | FunctionType::get(TD->getIntPtrType(), args, false); |
| 211 | strlen_func = M->getOrInsertFunction("strlen",strlen_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 212 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 213 | return strlen_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 216 | /// @brief Return a Function* for the memcpy libcall |
| 217 | Function* get_memcpy() |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 218 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 219 | if (!memcpy_func) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 220 | { |
| 221 | // Note: this is for llvm.memcpy intrinsic |
| 222 | std::vector<const Type*> args; |
| 223 | args.push_back(PointerType::get(Type::SByteTy)); |
| 224 | args.push_back(PointerType::get(Type::SByteTy)); |
| 225 | args.push_back(Type::IntTy); |
| 226 | args.push_back(Type::IntTy); |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 227 | FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false); |
| 228 | memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type); |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 229 | } |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 230 | return memcpy_func; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 231 | } |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 232 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 233 | /// @brief Compute length of constant string |
| 234 | bool getConstantStringLength(Value* V, uint64_t& len ); |
| 235 | |
| 236 | private: |
| 237 | void reset(Module& mod) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 238 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 239 | M = &mod; |
| 240 | TD = &getAnalysis<TargetData>(); |
| 241 | memcpy_func = 0; |
| 242 | strlen_func = 0; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 243 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 244 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 245 | private: |
| 246 | Function* memcpy_func; |
| 247 | Function* strlen_func; |
| 248 | Module* M; |
| 249 | TargetData* TD; |
| 250 | }; |
| 251 | |
| 252 | // Register the pass |
| 253 | RegisterOpt<SimplifyLibCalls> |
| 254 | X("simplify-libcalls","Simplify well-known library calls"); |
| 255 | |
| 256 | } // anonymous namespace |
| 257 | |
| 258 | // The only public symbol in this file which just instantiates the pass object |
| 259 | ModulePass *llvm::createSimplifyLibCallsPass() |
| 260 | { |
| 261 | return new SimplifyLibCalls(); |
| 262 | } |
| 263 | |
| 264 | // Classes below here, in the anonymous namespace, are all subclasses of the |
| 265 | // LibCallOptimization class, each implementing all optimizations possible for a |
| 266 | // single well-known library call. Each has a static singleton instance that |
| 267 | // auto registers it into the "optlist" global above. |
| 268 | namespace { |
| 269 | |
| 270 | bool getConstantStringLength(Value* V, uint64_t& len ); |
| 271 | |
| 272 | /// This LibCallOptimization will find instances of a call to "exit" that occurs |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 273 | /// within the "main" function and change it to a simple "ret" instruction with |
| 274 | /// the same value as passed to the exit function. It assumes that the |
| 275 | /// instructions after the call to exit(3) can be deleted since they are |
| 276 | /// unreachable anyway. |
| 277 | /// @brief Replace calls to exit in main with a simple return |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 278 | struct ExitInMainOptimization : public LibCallOptimization |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 279 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 280 | ExitInMainOptimization() : LibCallOptimization("exit") {} |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 281 | virtual ~ExitInMainOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 282 | |
| 283 | // Make sure the called function looks like exit (int argument, int return |
| 284 | // type, external linkage, not varargs). |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 285 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 286 | { |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 287 | if (f->arg_size() >= 1) |
| 288 | if (f->arg_begin()->getType()->isInteger()) |
| 289 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 293 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 294 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 295 | // To be careful, we check that the call to exit is coming from "main", that |
| 296 | // main has external linkage, and the return type of main and the argument |
| 297 | // to exit have the same type. |
| 298 | Function *from = ci->getParent()->getParent(); |
| 299 | if (from->hasExternalLinkage()) |
| 300 | if (from->getReturnType() == ci->getOperand(1)->getType()) |
| 301 | if (from->getName() == "main") |
| 302 | { |
| 303 | // Okay, time to actually do the optimization. First, get the basic |
| 304 | // block of the call instruction |
| 305 | BasicBlock* bb = ci->getParent(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 306 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 307 | // Create a return instruction that we'll replace the call with. |
| 308 | // Note that the argument of the return is the argument of the call |
| 309 | // instruction. |
| 310 | ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 311 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 312 | // Split the block at the call instruction which places it in a new |
| 313 | // basic block. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 314 | bb->splitBasicBlock(ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 315 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 316 | // The block split caused a branch instruction to be inserted into |
| 317 | // the end of the original block, right after the return instruction |
| 318 | // that we put there. That's not a valid block, so delete the branch |
| 319 | // instruction. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 320 | bb->getInstList().pop_back(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 321 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 322 | // Now we can finally get rid of the call instruction which now lives |
| 323 | // in the new basic block. |
| 324 | ci->eraseFromParent(); |
| 325 | |
| 326 | // Optimization succeeded, return true. |
| 327 | return true; |
| 328 | } |
| 329 | // We didn't pass the criteria for this optimization so return false |
| 330 | return false; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 331 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 332 | } ExitInMainOptimizer; |
| 333 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 334 | /// This LibCallOptimization will simplify a call to the strcat library |
| 335 | /// function. The simplification is possible only if the string being |
| 336 | /// concatenated is a constant array or a constant expression that results in |
| 337 | /// a constant array. In this case, if the array is small, we can generate a |
| 338 | /// series of inline store instructions to effect the concatenation without |
| 339 | /// calling strcat. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 340 | /// @brief Simplify the strcat library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 341 | struct StrCatOptimization : public LibCallOptimization |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 342 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 343 | public: |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 344 | StrCatOptimization() : LibCallOptimization("strcat") {} |
| 345 | |
| 346 | public: |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 347 | virtual ~StrCatOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 348 | |
| 349 | /// @brief Make sure that the "strcat" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 350 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 351 | { |
| 352 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 353 | if (f->arg_size() == 2) |
| 354 | { |
| 355 | Function::const_arg_iterator AI = f->arg_begin(); |
| 356 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 357 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 358 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 359 | // Indicate this is a suitable call type. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 360 | return true; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 361 | } |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 366 | /// @brief Optimize the strcat library function |
| 367 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 368 | { |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 369 | // Extract the initializer (while making numerous checks) from the |
| 370 | // source operand of the call to strcat. If we get null back, one of |
| 371 | // a variety of checks in get_GVInitializer failed |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 372 | uint64_t len = 0; |
| 373 | if (!getConstantStringLength(ci->getOperand(2),len)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 374 | return false; |
| 375 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 376 | // Handle the simple, do-nothing case |
| 377 | if (len == 0) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 378 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 379 | ci->replaceAllUsesWith(ci->getOperand(1)); |
| 380 | ci->eraseFromParent(); |
| 381 | return true; |
| 382 | } |
| 383 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 384 | // Increment the length because we actually want to memcpy the null |
| 385 | // terminator as well. |
| 386 | len++; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 387 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 388 | // Extract some information from the instruction |
| 389 | Module* M = ci->getParent()->getParent()->getParent(); |
| 390 | |
| 391 | // We need to find the end of the destination string. That's where the |
| 392 | // memory is to be moved to. We just generate a call to strlen (further |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 393 | // optimized in another pass). Note that the SLC.get_strlen() call |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 394 | // caches the Function* for us. |
| 395 | CallInst* strlen_inst = |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 396 | new CallInst(SLC.get_strlen(), ci->getOperand(1),"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 397 | |
| 398 | // Now that we have the destination's length, we must index into the |
| 399 | // destination's pointer to get the actual memcpy destination (end of |
| 400 | // the string .. we're concatenating). |
| 401 | std::vector<Value*> idx; |
| 402 | idx.push_back(strlen_inst); |
| 403 | GetElementPtrInst* gep = |
| 404 | new GetElementPtrInst(ci->getOperand(1),idx,"",ci); |
| 405 | |
| 406 | // We have enough information to now generate the memcpy call to |
| 407 | // do the concatenation for us. |
| 408 | std::vector<Value*> vals; |
| 409 | vals.push_back(gep); // destination |
| 410 | vals.push_back(ci->getOperand(2)); // source |
| 411 | vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length |
| 412 | vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 413 | CallInst* memcpy_inst = new CallInst(SLC.get_memcpy(), vals, "", ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 414 | |
| 415 | // Finally, substitute the first operand of the strcat call for the |
| 416 | // strcat call itself since strcat returns its first operand; and, |
| 417 | // kill the strcat CallInst. |
| 418 | ci->replaceAllUsesWith(ci->getOperand(1)); |
| 419 | ci->eraseFromParent(); |
| 420 | return true; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 421 | } |
| 422 | } StrCatOptimizer; |
| 423 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 424 | /// This LibCallOptimization will simplify a call to the strcpy library function. |
| 425 | /// Several optimizations are possible: |
| 426 | /// (1) If src and dest are the same and not volatile, just return dest |
| 427 | /// (2) If the src is a constant then we can convert to llvm.memmove |
| 428 | /// @brief Simplify the strcpy library function. |
| 429 | struct StrCpyOptimization : public LibCallOptimization |
| 430 | { |
| 431 | public: |
| 432 | StrCpyOptimization() : LibCallOptimization("strcpy") {} |
| 433 | virtual ~StrCpyOptimization() {} |
| 434 | |
| 435 | /// @brief Make sure that the "strcpy" function has the right prototype |
| 436 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
| 437 | { |
| 438 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 439 | if (f->arg_size() == 2) |
| 440 | { |
| 441 | Function::const_arg_iterator AI = f->arg_begin(); |
| 442 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 443 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 444 | { |
| 445 | // Indicate this is a suitable call type. |
| 446 | return true; |
| 447 | } |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | /// @brief Perform the strcpy optimization |
| 453 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
| 454 | { |
| 455 | // First, check to see if src and destination are the same. If they are, |
| 456 | // then the optimization is to replace the CallInst with the destination |
| 457 | // because the call is a no-op. Note that this corresponds to the |
| 458 | // degenerate strcpy(X,X) case which should have "undefined" results |
| 459 | // according to the C specification. However, it occurs sometimes and |
| 460 | // we optimize it as a no-op. |
| 461 | Value* dest = ci->getOperand(1); |
| 462 | Value* src = ci->getOperand(2); |
| 463 | if (dest == src) |
| 464 | { |
| 465 | ci->replaceAllUsesWith(dest); |
| 466 | ci->eraseFromParent(); |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | // Get the length of the constant string referenced by the second operand, |
| 471 | // the "src" parameter. Fail the optimization if we can't get the length |
| 472 | // (note that getConstantStringLength does lots of checks to make sure this |
| 473 | // is valid). |
| 474 | uint64_t len = 0; |
| 475 | if (!getConstantStringLength(ci->getOperand(2),len)) |
| 476 | return false; |
| 477 | |
| 478 | // If the constant string's length is zero we can optimize this by just |
| 479 | // doing a store of 0 at the first byte of the destination |
| 480 | if (len == 0) |
| 481 | { |
| 482 | new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci); |
| 483 | ci->replaceAllUsesWith(dest); |
| 484 | ci->eraseFromParent(); |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | // Increment the length because we actually want to memcpy the null |
| 489 | // terminator as well. |
| 490 | len++; |
| 491 | |
| 492 | // Extract some information from the instruction |
| 493 | Module* M = ci->getParent()->getParent()->getParent(); |
| 494 | |
| 495 | // We have enough information to now generate the memcpy call to |
| 496 | // do the concatenation for us. |
| 497 | std::vector<Value*> vals; |
| 498 | vals.push_back(dest); // destination |
| 499 | vals.push_back(src); // source |
| 500 | vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length |
| 501 | vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment |
| 502 | CallInst* memcpy_inst = new CallInst(SLC.get_memcpy(), vals, "", ci); |
| 503 | |
| 504 | // Finally, substitute the first operand of the strcat call for the |
| 505 | // strcat call itself since strcat returns its first operand; and, |
| 506 | // kill the strcat CallInst. |
| 507 | ci->replaceAllUsesWith(dest); |
| 508 | ci->eraseFromParent(); |
| 509 | return true; |
| 510 | } |
| 511 | } StrCpyOptimizer; |
| 512 | |
| 513 | /// This LibCallOptimization will simplify a call to the strlen library function by |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 514 | /// replacing it with a constant value if the string provided to it is a |
| 515 | /// constant array. |
| 516 | /// @brief Simplify the strlen library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 517 | struct StrLenOptimization : public LibCallOptimization |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 518 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 519 | StrLenOptimization() : LibCallOptimization("strlen") {} |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 520 | virtual ~StrLenOptimization() {} |
| 521 | |
| 522 | /// @brief Make sure that the "strlen" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 523 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 524 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 525 | if (f->getReturnType() == SLC.getTargetData()->getIntPtrType()) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 526 | if (f->arg_size() == 1) |
| 527 | if (Function::const_arg_iterator AI = f->arg_begin()) |
| 528 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
| 529 | return true; |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | /// @brief Perform the strlen optimization |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 534 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 535 | { |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 536 | // Get the length of the string |
| 537 | uint64_t len = 0; |
| 538 | if (!getConstantStringLength(ci->getOperand(1),len)) |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 539 | return false; |
| 540 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 541 | ci->replaceAllUsesWith( |
| 542 | ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len)); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 543 | ci->eraseFromParent(); |
| 544 | return true; |
Reid Spencer | 76dab9a | 2005-04-26 05:24:00 +0000 | [diff] [blame] | 545 | } |
| 546 | } StrLenOptimizer; |
| 547 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 548 | /// This LibCallOptimization will simplify a call to the memcpy library function by |
| 549 | /// expanding it out to a single store of size 0, 1, 2, 4, or 8 bytes depending |
| 550 | /// on the length of the string and the alignment. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 551 | /// @brief Simplify the memcpy library function. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 552 | struct MemCpyOptimization : public LibCallOptimization |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 553 | { |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 554 | MemCpyOptimization() : LibCallOptimization("llvm.memcpy") {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 555 | protected: |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 556 | MemCpyOptimization(const char* fname) : LibCallOptimization(fname) {} |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 557 | public: |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 558 | virtual ~MemCpyOptimization() {} |
| 559 | |
| 560 | /// @brief Make sure that the "memcpy" function has the right prototype |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 561 | virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 562 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 563 | // Just make sure this has 4 arguments per LLVM spec. |
Reid Spencer | 2bc7a4f | 2005-04-26 23:02:16 +0000 | [diff] [blame] | 564 | return (f->arg_size() == 4); |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 567 | /// Because of alignment and instruction information that we don't have, we |
| 568 | /// leave the bulk of this to the code generators. The optimization here just |
| 569 | /// deals with a few degenerate cases where the length of the string and the |
| 570 | /// alignment match the sizes of our intrinsic types so we can do a load and |
| 571 | /// store instead of the memcpy call. |
| 572 | /// @brief Perform the memcpy optimization. |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 573 | virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 574 | { |
Reid Spencer | 4855ebf | 2005-04-26 19:55:57 +0000 | [diff] [blame] | 575 | // Make sure we have constant int values to work with |
| 576 | ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3)); |
| 577 | if (!LEN) |
| 578 | return false; |
| 579 | ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4)); |
| 580 | if (!ALIGN) |
| 581 | return false; |
| 582 | |
| 583 | // If the length is larger than the alignment, we can't optimize |
| 584 | uint64_t len = LEN->getRawValue(); |
| 585 | uint64_t alignment = ALIGN->getRawValue(); |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 586 | if (len > alignment) |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 587 | return false; |
| 588 | |
| 589 | Value* dest = ci->getOperand(1); |
| 590 | Value* src = ci->getOperand(2); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 591 | CastInst* SrcCast = 0; |
| 592 | CastInst* DestCast = 0; |
| 593 | switch (len) |
| 594 | { |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 595 | case 0: |
Reid Spencer | aaca170 | 2005-04-26 22:46:23 +0000 | [diff] [blame] | 596 | // The memcpy is a no-op so just dump its call. |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 597 | ci->eraseFromParent(); |
| 598 | return true; |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 599 | case 1: |
| 600 | SrcCast = new CastInst(src,PointerType::get(Type::SByteTy),"",ci); |
| 601 | DestCast = new CastInst(dest,PointerType::get(Type::SByteTy),"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 602 | break; |
| 603 | case 2: |
| 604 | SrcCast = new CastInst(src,PointerType::get(Type::ShortTy),"",ci); |
| 605 | DestCast = new CastInst(dest,PointerType::get(Type::ShortTy),"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 606 | break; |
| 607 | case 4: |
| 608 | SrcCast = new CastInst(src,PointerType::get(Type::IntTy),"",ci); |
| 609 | DestCast = new CastInst(dest,PointerType::get(Type::IntTy),"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 610 | break; |
| 611 | case 8: |
| 612 | SrcCast = new CastInst(src,PointerType::get(Type::LongTy),"",ci); |
| 613 | DestCast = new CastInst(dest,PointerType::get(Type::LongTy),"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 614 | break; |
| 615 | default: |
| 616 | return false; |
| 617 | } |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 618 | LoadInst* LI = new LoadInst(SrcCast,"",ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 619 | StoreInst* SI = new StoreInst(LI, DestCast, ci); |
Reid Spencer | b4f7b83 | 2005-04-26 07:45:18 +0000 | [diff] [blame] | 620 | ci->eraseFromParent(); |
| 621 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 622 | } |
| 623 | } MemCpyOptimizer; |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 624 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 625 | /// This LibCallOptimization will simplify a call to the memmove library function. /// It is identical to MemCopyOptimization except for the name of the intrinsic. |
Reid Spencer | bb92b4f | 2005-04-26 19:13:17 +0000 | [diff] [blame] | 626 | /// @brief Simplify the memmove library function. |
| 627 | struct MemMoveOptimization : public MemCpyOptimization |
| 628 | { |
| 629 | MemMoveOptimization() : MemCpyOptimization("llvm.memmove") {} |
| 630 | |
| 631 | } MemMoveOptimizer; |
| 632 | |
Reid Spencer | e249a82 | 2005-04-27 07:54:40 +0000 | [diff] [blame] | 633 | /// A function to compute the length of a null-terminated string of integers. |
| 634 | /// This function can't rely on the size of the constant array because there |
| 635 | /// could be a null terminator in the middle of the array. We also have to |
| 636 | /// bail out if we find a non-integer constant initializer of one of the |
| 637 | /// elements or if there is no null-terminator. The logic below checks |
| 638 | bool getConstantStringLength(Value* V, uint64_t& len ) |
| 639 | { |
| 640 | assert(V != 0 && "Invalid args to getConstantStringLength"); |
| 641 | len = 0; // make sure we initialize this |
| 642 | User* GEP = 0; |
| 643 | // If the value is not a GEP instruction nor a constant expression with a |
| 644 | // GEP instruction, then return false because ConstantArray can't occur |
| 645 | // any other way |
| 646 | if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V)) |
| 647 | GEP = GEPI; |
| 648 | else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V)) |
| 649 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 650 | GEP = CE; |
| 651 | else |
| 652 | return false; |
| 653 | else |
| 654 | return false; |
| 655 | |
| 656 | // Make sure the GEP has exactly three arguments. |
| 657 | if (GEP->getNumOperands() != 3) |
| 658 | return false; |
| 659 | |
| 660 | // Check to make sure that the first operand of the GEP is an integer and |
| 661 | // has value 0 so that we are sure we're indexing into the initializer. |
| 662 | if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1))) |
| 663 | { |
| 664 | if (!op1->isNullValue()) |
| 665 | return false; |
| 666 | } |
| 667 | else |
| 668 | return false; |
| 669 | |
| 670 | // Ensure that the second operand is a ConstantInt. If it isn't then this |
| 671 | // GEP is wonky and we're not really sure what were referencing into and |
| 672 | // better of not optimizing it. While we're at it, get the second index |
| 673 | // value. We'll need this later for indexing the ConstantArray. |
| 674 | uint64_t start_idx = 0; |
| 675 | if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
| 676 | start_idx = CI->getRawValue(); |
| 677 | else |
| 678 | return false; |
| 679 | |
| 680 | // The GEP instruction, constant or instruction, must reference a global |
| 681 | // variable that is a constant and is initialized. The referenced constant |
| 682 | // initializer is the array that we'll use for optimization. |
| 683 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 684 | if (!GV || !GV->isConstant() || !GV->hasInitializer()) |
| 685 | return false; |
| 686 | |
| 687 | // Get the initializer. |
| 688 | Constant* INTLZR = GV->getInitializer(); |
| 689 | |
| 690 | // Handle the ConstantAggregateZero case |
| 691 | if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR)) |
| 692 | { |
| 693 | // This is a degenerate case. The initializer is constant zero so the |
| 694 | // length of the string must be zero. |
| 695 | len = 0; |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | // Must be a Constant Array |
| 700 | ConstantArray* A = dyn_cast<ConstantArray>(INTLZR); |
| 701 | if (!A) |
| 702 | return false; |
| 703 | |
| 704 | // Get the number of elements in the array |
| 705 | uint64_t max_elems = A->getType()->getNumElements(); |
| 706 | |
| 707 | // Traverse the constant array from start_idx (derived above) which is |
| 708 | // the place the GEP refers to in the array. |
| 709 | for ( len = start_idx; len < max_elems; len++) |
| 710 | { |
| 711 | if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len))) |
| 712 | { |
| 713 | // Check for the null terminator |
| 714 | if (CI->isNullValue()) |
| 715 | break; // we found end of string |
| 716 | } |
| 717 | else |
| 718 | return false; // This array isn't suitable, non-int initializer |
| 719 | } |
| 720 | if (len >= max_elems) |
| 721 | return false; // This array isn't null terminated |
| 722 | |
| 723 | // Subtract out the initial value from the length |
| 724 | len -= start_idx; |
| 725 | return true; // success! |
| 726 | } |
| 727 | |
| 728 | |
| 729 | // TODO: Additional cases that we need to add to this file: |
| 730 | // 1. memmove -> memcpy if src is a global constant array |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 731 | } |