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