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 | |
| 19 | #include "llvm/Transforms/IPO.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Constants.h" |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/hash_map" |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 27 | #include <iostream> |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | Statistic<> SimplifiedLibCalls("simplified-lib-calls", |
| 32 | "Number of well-known library calls simplified"); |
| 33 | |
| 34 | /// This class is the base class for a set of small but important |
| 35 | /// optimizations of calls to well-known functions, such as those in the c |
| 36 | /// library. This class provides the basic infrastructure for handling |
| 37 | /// runOnModule. Subclasses register themselves and provide two methods: |
| 38 | /// RecognizeCall and OptimizeCall. Whenever this class finds a function call, |
| 39 | /// it asks the subclasses to recognize the call. If it is recognized, then |
| 40 | /// the OptimizeCall method is called on that subclass instance. In this way |
| 41 | /// the subclasses implement the calling conditions on which they trigger and |
| 42 | /// the action to perform, making it easy to add new optimizations of this |
| 43 | /// form. |
| 44 | /// @brief A ModulePass for optimizing well-known function calls |
| 45 | struct SimplifyLibCalls : public ModulePass { |
| 46 | |
| 47 | |
| 48 | /// For this pass, process all of the function calls in the module, calling |
| 49 | /// RecognizeCall and OptimizeCall as appropriate. |
| 50 | virtual bool runOnModule(Module &M); |
| 51 | |
| 52 | }; |
| 53 | |
| 54 | RegisterOpt<SimplifyLibCalls> |
| 55 | X("simplify-libcalls","Simplify well-known library calls"); |
| 56 | |
| 57 | struct CallOptimizer |
| 58 | { |
| 59 | /// @brief Constructor that registers the optimization |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 60 | CallOptimizer(const char * fname ); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 61 | |
| 62 | virtual ~CallOptimizer(); |
| 63 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 64 | /// The implementation of this function in subclasses should determine if |
| 65 | /// \p F is suitable for the optimization. This method is called by |
| 66 | /// runOnModule to short circuit visiting all the call sites of such a |
| 67 | /// function if that function is not suitable in the first place. |
| 68 | /// If the called function is suitabe, this method should return true; |
| 69 | /// false, otherwise. This function should also perform any lazy |
| 70 | /// initialization that the CallOptimizer needs to do, if its to return |
| 71 | /// true. This avoids doing initialization until the optimizer is actually |
| 72 | /// going to be called upon to do some optimization. |
| 73 | virtual bool ValidateCalledFunction( |
| 74 | const Function* F ///< The function that is the target of call sites |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 75 | ) = 0; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 76 | |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 77 | /// The implementations of this function in subclasses is the heart of the |
| 78 | /// SimplifyLibCalls algorithm. Sublcasses of this class implement |
| 79 | /// OptimizeCall to determine if (a) the conditions are right for optimizing |
| 80 | /// the call and (b) to perform the optimization. If an action is taken |
| 81 | /// against ci, the subclass is responsible for returning true and ensuring |
| 82 | /// that ci is erased from its parent. |
| 83 | /// @param ci the call instruction under consideration |
| 84 | /// @param f the function that ci calls. |
| 85 | /// @brief Optimize a call, if possible. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 86 | virtual bool OptimizeCall( |
| 87 | CallInst* ci ///< The call instruction that should be optimized. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 88 | ) = 0; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 89 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 90 | const char * getFunctionName() const { return func_name; } |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 91 | private: |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 92 | const char* func_name; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /// @brief The list of optimizations deriving from CallOptimizer |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 96 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 97 | hash_map<std::string,CallOptimizer*> optlist; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 99 | CallOptimizer::CallOptimizer(const char* fname) |
| 100 | : func_name(fname) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 101 | { |
| 102 | // Register this call optimizer |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 103 | optlist[func_name] = this; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// Make sure we get our virtual table in this file. |
Reid Spencer | fe91dfe | 2005-04-25 21:20:38 +0000 | [diff] [blame] | 107 | CallOptimizer::~CallOptimizer() { } |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 108 | |
| 109 | /// Provide some functions for accessing standard library prototypes and |
| 110 | /// caching them so we don't have to keep recomputing them |
| 111 | FunctionType* get_strlen() |
| 112 | { |
| 113 | static FunctionType* strlen_type = 0; |
| 114 | if (!strlen_type) |
| 115 | { |
| 116 | std::vector<const Type*> args; |
| 117 | args.push_back(PointerType::get(Type::SByteTy)); |
| 118 | strlen_type = FunctionType::get(Type::IntTy, args, false); |
| 119 | } |
| 120 | return strlen_type; |
| 121 | } |
| 122 | |
| 123 | FunctionType* get_memcpy() |
| 124 | { |
| 125 | static FunctionType* memcpy_type = 0; |
| 126 | if (!memcpy_type) |
| 127 | { |
| 128 | // Note: this is for llvm.memcpy intrinsic |
| 129 | std::vector<const Type*> args; |
| 130 | args.push_back(PointerType::get(Type::SByteTy)); |
| 131 | args.push_back(PointerType::get(Type::SByteTy)); |
| 132 | args.push_back(Type::IntTy); |
| 133 | args.push_back(Type::IntTy); |
| 134 | memcpy_type = FunctionType::get( |
| 135 | PointerType::get(Type::SByteTy), args, false); |
| 136 | } |
| 137 | return memcpy_type; |
| 138 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | ModulePass *llvm::createSimplifyLibCallsPass() |
| 142 | { |
| 143 | return new SimplifyLibCalls(); |
| 144 | } |
| 145 | |
| 146 | bool SimplifyLibCalls::runOnModule(Module &M) |
| 147 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 148 | bool result = false; |
| 149 | |
| 150 | // The call optimizations can be recursive. That is, the optimization might |
| 151 | // generate a call to another function which can also be optimized. This way |
| 152 | // we make the CallOptimizer instances very specific to the case they handle. |
| 153 | // It also means we need to keep running over the function calls in the module |
| 154 | // until we don't get any more optimizations possible. |
| 155 | bool found_optimization = false; |
| 156 | do |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 157 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 158 | found_optimization = false; |
| 159 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 160 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 161 | // All the "well-known" functions are external and have external linkage |
| 162 | // because they live in a runtime library somewhere and were (probably) |
| 163 | // not compiled by LLVM. So, we only act on external functions that have |
| 164 | // external linkage and non-empty uses. |
| 165 | if (FI->isExternal() && FI->hasExternalLinkage() && !FI->use_empty()) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 166 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 167 | // Get the optimization class that pertains to this function |
| 168 | if (CallOptimizer* CO = optlist[FI->getName().c_str()] ) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 169 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 170 | // Make sure the called function is suitable for the optimization |
| 171 | if (CO->ValidateCalledFunction(FI)) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 172 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 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 ; ) |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 176 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 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 CallOptimizer. |
| 181 | if (CO->OptimizeCall(CI)) |
| 182 | { |
| 183 | ++SimplifiedLibCalls; |
| 184 | found_optimization = result = true; |
| 185 | } |
| 186 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 192 | } while (found_optimization); |
| 193 | return result; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | namespace { |
| 197 | |
| 198 | /// This CallOptimizer will find instances of a call to "exit" that occurs |
| 199 | /// within the "main" function and change it to a simple "ret" instruction with |
| 200 | /// the same value as passed to the exit function. It assumes that the |
| 201 | /// instructions after the call to exit(3) can be deleted since they are |
| 202 | /// unreachable anyway. |
| 203 | /// @brief Replace calls to exit in main with a simple return |
| 204 | struct ExitInMainOptimization : public CallOptimizer |
| 205 | { |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 206 | ExitInMainOptimization() : CallOptimizer("exit") {} |
| 207 | virtual ~ExitInMainOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 208 | |
| 209 | // Make sure the called function looks like exit (int argument, int return |
| 210 | // type, external linkage, not varargs). |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 211 | virtual bool ValidateCalledFunction(const Function* f) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 212 | { |
| 213 | if (f->getReturnType()->getTypeID() == Type::VoidTyID && !f->isVarArg()) |
| 214 | if (f->arg_size() == 1) |
| 215 | if (f->arg_begin()->getType()->isInteger()) |
| 216 | return true; |
| 217 | return false; |
| 218 | } |
| 219 | |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 220 | virtual bool OptimizeCall(CallInst* ci) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 221 | { |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 222 | // To be careful, we check that the call to exit is coming from "main", that |
| 223 | // main has external linkage, and the return type of main and the argument |
| 224 | // to exit have the same type. |
| 225 | Function *from = ci->getParent()->getParent(); |
| 226 | if (from->hasExternalLinkage()) |
| 227 | if (from->getReturnType() == ci->getOperand(1)->getType()) |
| 228 | if (from->getName() == "main") |
| 229 | { |
| 230 | // Okay, time to actually do the optimization. First, get the basic |
| 231 | // block of the call instruction |
| 232 | BasicBlock* bb = ci->getParent(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 233 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 234 | // Create a return instruction that we'll replace the call with. |
| 235 | // Note that the argument of the return is the argument of the call |
| 236 | // instruction. |
| 237 | ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 238 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 239 | // Split the block at the call instruction which places it in a new |
| 240 | // basic block. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 241 | bb->splitBasicBlock(ci); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 242 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 243 | // The block split caused a branch instruction to be inserted into |
| 244 | // the end of the original block, right after the return instruction |
| 245 | // that we put there. That's not a valid block, so delete the branch |
| 246 | // instruction. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 247 | bb->getInstList().pop_back(); |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 248 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 249 | // Now we can finally get rid of the call instruction which now lives |
| 250 | // in the new basic block. |
| 251 | ci->eraseFromParent(); |
| 252 | |
| 253 | // Optimization succeeded, return true. |
| 254 | return true; |
| 255 | } |
| 256 | // We didn't pass the criteria for this optimization so return false |
| 257 | return false; |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 258 | } |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 259 | } ExitInMainOptimizer; |
| 260 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 261 | /// This CallOptimizer will simplify a call to the strcat library function. The |
| 262 | /// simplification is possible only if the string being concatenated is a |
| 263 | /// constant array or a constant expression that results in a constant array. In |
| 264 | /// this case, if the array is small, we can generate a series of inline store |
| 265 | /// instructions to effect the concatenation without calling strcat. |
| 266 | /// @brief Simplify the strcat library function. |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 267 | struct StrCatOptimization : public CallOptimizer |
| 268 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 269 | private: |
| 270 | Function* strlen_func; |
| 271 | Function* memcpy_func; |
| 272 | public: |
| 273 | StrCatOptimization() |
| 274 | : CallOptimizer("strcat") |
| 275 | , strlen_func(0) |
| 276 | , memcpy_func(0) |
| 277 | {} |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 278 | virtual ~StrCatOptimization() {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 279 | |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 280 | inline Function* get_strlen_func(Module*M) |
| 281 | { |
| 282 | if (strlen_func) |
| 283 | return strlen_func; |
| 284 | return strlen_func = M->getOrInsertFunction("strlen",get_strlen()); |
| 285 | } |
| 286 | |
| 287 | inline Function* get_memcpy_func(Module* M) |
| 288 | { |
| 289 | if (memcpy_func) |
| 290 | return memcpy_func; |
| 291 | return memcpy_func = M->getOrInsertFunction("llvm.memcpy",get_memcpy()); |
| 292 | } |
| 293 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 294 | /// @brief Make sure that the "strcat" function has the right prototype |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 295 | virtual bool ValidateCalledFunction(const Function* f) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 296 | { |
| 297 | if (f->getReturnType() == PointerType::get(Type::SByteTy)) |
| 298 | if (f->arg_size() == 2) |
| 299 | { |
| 300 | Function::const_arg_iterator AI = f->arg_begin(); |
| 301 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 302 | if (AI->getType() == PointerType::get(Type::SByteTy)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 303 | { |
| 304 | // Invalidate the pre-computed strlen_func and memcpy_func Functions |
| 305 | // because, by definition, this method is only called when a new |
| 306 | // Module is being traversed. Invalidation causes re-computation for |
| 307 | // the new Module (if necessary). |
| 308 | strlen_func = 0; |
| 309 | memcpy_func = 0; |
| 310 | |
| 311 | // Indicate this is a suitable call type. |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 312 | return true; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 313 | } |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /// Perform the optimization if the length of the string concatenated |
| 319 | /// is reasonably short and it is a constant array. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 320 | virtual bool OptimizeCall(CallInst* ci) |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 321 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 322 | User* GEP = 0; |
| 323 | // If the thing being appended is not a GEP instruction nor a constant |
| 324 | // expression with a GEP instruction, then return false because this is |
| 325 | // not a situation we can optimize. |
| 326 | if (GetElementPtrInst* GEPI = |
| 327 | dyn_cast<GetElementPtrInst>(ci->getOperand(2))) |
| 328 | GEP = GEPI; |
| 329 | else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(ci->getOperand(2))) |
| 330 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 331 | GEP = CE; |
| 332 | else |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 333 | return false; |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 334 | else |
| 335 | return false; |
| 336 | |
| 337 | // Check to make sure that the first operand of the GEP is an integer and |
| 338 | // has value 0 so that we are sure we're indexing into the initializer. |
| 339 | if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1))) |
| 340 | if (op1->isNullValue()) |
| 341 | ; |
| 342 | else |
| 343 | return false; |
| 344 | else |
| 345 | return false; |
| 346 | |
| 347 | // Ensure that the second operand is a constant int. If it isn't then this |
| 348 | // GEP is wonky and we're not really sure what were referencing into and |
| 349 | // better of not optimizing it. |
| 350 | if (!dyn_cast<ConstantInt>(GEP->getOperand(2))) |
| 351 | return false; |
| 352 | |
| 353 | // The GEP instruction, constant or instruction, must reference a global |
| 354 | // variable that is a constant and is initialized. The referenced constant |
| 355 | // initializer is the array that we'll use for optimization. |
| 356 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 357 | if (!GV || !GV->isConstant() || !GV->hasInitializer()) |
| 358 | return false; |
| 359 | |
| 360 | // Get the initializer |
| 361 | Constant* INTLZR = GV->getInitializer(); |
| 362 | |
| 363 | // Handle the ConstantArray case. |
| 364 | if (ConstantArray* A = dyn_cast<ConstantArray>(INTLZR)) |
| 365 | { |
| 366 | // First off, we can't do this if the constant array isn't a string, |
| 367 | // meaning its base type is sbyte and its constant initializers for all |
| 368 | // the elements are constantInt or constantInt expressions. |
| 369 | if (!A->isString()) |
| 370 | return false; |
| 371 | |
| 372 | // Now we need to examine the source string to find its actual length. We |
| 373 | // can't rely on the size of the constant array becasue there could be a |
| 374 | // null terminator in the middle of the array. We also have to bail out if |
| 375 | // we find a non-integer constant initializer of one of the elements. |
| 376 | // Also, if we never find a terminator before the end of the array. |
| 377 | unsigned max_elems = A->getType()->getNumElements(); |
| 378 | unsigned len = 0; |
| 379 | for (; len < max_elems; len++) |
| 380 | { |
| 381 | if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len))) |
| 382 | { |
| 383 | if (CI->isNullValue()) |
| 384 | break; // we found end of string |
| 385 | } |
| 386 | else |
| 387 | return false; // This array isn't suitable, non-int initializer |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 388 | } |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 389 | if (len >= max_elems) |
| 390 | return false; // This array isn't null terminated |
| 391 | else |
| 392 | len++; // increment for null terminator |
| 393 | |
| 394 | // Extract some information from the instruction |
| 395 | Module* M = ci->getParent()->getParent()->getParent(); |
| 396 | |
| 397 | // We need to find the end of the destination string. That's where the |
| 398 | // memory is to be moved to. We just generate a call to strlen (further |
| 399 | // optimized in another pass). Note that the get_strlen_func() call |
| 400 | // caches the Function* for us. |
| 401 | CallInst* strlen_inst = |
| 402 | new CallInst(get_strlen_func(M),ci->getOperand(1),"",ci); |
| 403 | |
| 404 | // Now that we have the destination's length, we must index into the |
| 405 | // destination's pointer to get the actual memcpy destination (end of |
| 406 | // the string .. we're concatenating). |
| 407 | std::vector<Value*> idx; |
| 408 | idx.push_back(strlen_inst); |
| 409 | GetElementPtrInst* gep = |
| 410 | new GetElementPtrInst(ci->getOperand(1),idx,"",ci); |
| 411 | |
| 412 | // We have enough information to now generate the memcpy call to |
| 413 | // do the concatenation for us. |
| 414 | std::vector<Value*> vals; |
| 415 | vals.push_back(gep); // destination |
| 416 | vals.push_back(ci->getOperand(2)); // source |
| 417 | vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length |
| 418 | vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment |
| 419 | CallInst* memcpy_inst = |
| 420 | new CallInst(get_memcpy_func(M), vals, "", ci); |
| 421 | |
| 422 | // Finally, substitute the first operand of the strcat call for the |
| 423 | // strcat call itself since strcat returns its first operand; and, |
| 424 | // kill the strcat CallInst. |
| 425 | ci->replaceAllUsesWith(ci->getOperand(1)); |
| 426 | ci->eraseFromParent(); |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | // Handle the ConstantAggregateZero case |
| 431 | else if (ConstantAggregateZero* CAZ = |
| 432 | dyn_cast<ConstantAggregateZero>(INTLZR)) |
| 433 | { |
| 434 | // We know this is the zero length string case so we can just avoid |
| 435 | // the strcat altogether and replace the CallInst with its first operand |
| 436 | // (what strcat returns). |
| 437 | ci->replaceAllUsesWith(ci->getOperand(1)); |
| 438 | ci->eraseFromParent(); |
| 439 | return true; |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // We didn't pass the criteria for this optimization so return false. |
Reid Spencer | 9bbaa2a | 2005-04-25 03:59:26 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | } StrCatOptimizer; |
| 446 | |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 447 | /// This CallOptimizer will simplify a call to the memcpy library function by |
| 448 | /// expanding it out to a small set of stores if the copy source is a constant |
| 449 | /// array. |
| 450 | /// @brief Simplify the memcpy library function. |
| 451 | struct MemCpyOptimization : public CallOptimizer |
| 452 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 453 | MemCpyOptimization() : CallOptimizer("llvm.memcpy") {} |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 454 | virtual ~MemCpyOptimization() {} |
| 455 | |
| 456 | /// @brief Make sure that the "memcpy" function has the right prototype |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 457 | virtual bool ValidateCalledFunction(const Function* f) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 458 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 459 | if (f->getReturnType() == PointerType::get(Type::VoidTy)) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 460 | if (f->arg_size() == 2) |
| 461 | { |
| 462 | Function::const_arg_iterator AI = f->arg_begin(); |
| 463 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 464 | if (AI++->getType() == PointerType::get(Type::SByteTy)) |
| 465 | if (AI++->getType() == Type::IntTy) |
| 466 | if (AI->getType() == Type::IntTy) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | /// Perform the optimization if the length of the string concatenated |
| 473 | /// is reasonably short and it is a constant array. |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 474 | virtual bool OptimizeCall(CallInst* ci) |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 475 | { |
Reid Spencer | 8ee5aac | 2005-04-26 03:26:15 +0000 | [diff] [blame] | 476 | // |
Reid Spencer | f2534c7 | 2005-04-25 21:11:48 +0000 | [diff] [blame] | 477 | // We didn't pass the criteria for this optimization so return false. |
| 478 | return false; |
| 479 | } |
| 480 | } MemCpyOptimizer; |
Reid Spencer | 39a762d | 2005-04-25 02:53:12 +0000 | [diff] [blame] | 481 | } |