blob: cc638be9a9d578d357110bacd966a5cf9186286e [file] [log] [blame]
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
Reid Spencer39a762d2005-04-25 02:53:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00005// 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 Spencer39a762d2005-04-25 02:53:12 +00007//
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 Spencer9bbaa2a2005-04-25 03:59:26 +000013// 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 Spencer39a762d2005-04-25 02:53:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
Reid Spencerf2534c72005-04-25 21:11:48 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Constants.h"
Reid Spencer39a762d2005-04-25 02:53:12 +000024#include "llvm/Instructions.h"
25#include "llvm/ADT/Statistic.h"
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000026#include "llvm/ADT/hash_map"
Reid Spencerbb92b4f2005-04-26 19:13:17 +000027#include "llvm/Target/TargetData.h"
Reid Spencerf2534c72005-04-25 21:11:48 +000028#include <iostream>
Reid Spencer39a762d2005-04-25 02:53:12 +000029using namespace llvm;
30
31namespace {
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 Spencerbb92b4f2005-04-26 19:13:17 +000048 /// 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 Spencer39a762d2005-04-25 02:53:12 +000051
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 Spencer9bbaa2a2005-04-25 03:59:26 +000064 CallOptimizer(const char * fname );
Reid Spencer39a762d2005-04-25 02:53:12 +000065
66 virtual ~CallOptimizer();
67
Reid Spencerf2534c72005-04-25 21:11:48 +000068 /// 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 Spencerbb92b4f2005-04-26 19:13:17 +000078 const Function* F, ///< The function that is the target of call sites
79 const TargetData& TD ///< Information about the target
Reid Spencer8ee5aac2005-04-26 03:26:15 +000080 ) = 0;
Reid Spencerf2534c72005-04-25 21:11:48 +000081
Reid Spencer39a762d2005-04-25 02:53:12 +000082 /// 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 Spencerf2534c72005-04-25 21:11:48 +000091 virtual bool OptimizeCall(
Reid Spencerbb92b4f2005-04-26 19:13:17 +000092 CallInst* ci, ///< The call instruction that should be optimized.
93 const TargetData& TD ///< Information about the target
Reid Spencer8ee5aac2005-04-26 03:26:15 +000094 ) = 0;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000095
Reid Spencerf2534c72005-04-25 21:11:48 +000096 const char * getFunctionName() const { return func_name; }
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000097 private:
Reid Spencerf2534c72005-04-25 21:11:48 +000098 const char* func_name;
Reid Spencer39a762d2005-04-25 02:53:12 +000099 };
100
101 /// @brief The list of optimizations deriving from CallOptimizer
Reid Spencerf2534c72005-04-25 21:11:48 +0000102
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000103 hash_map<std::string,CallOptimizer*> optlist;
Reid Spencer39a762d2005-04-25 02:53:12 +0000104
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000105 CallOptimizer::CallOptimizer(const char* fname)
106 : func_name(fname)
Reid Spencer39a762d2005-04-25 02:53:12 +0000107 {
108 // Register this call optimizer
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000109 optlist[func_name] = this;
Reid Spencer39a762d2005-04-25 02:53:12 +0000110 }
111
112 /// Make sure we get our virtual table in this file.
Reid Spencerfe91dfe2005-04-25 21:20:38 +0000113 CallOptimizer::~CallOptimizer() { }
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000114
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000115}
116
117ModulePass *llvm::createSimplifyLibCallsPass()
118{
119 return new SimplifyLibCalls();
120}
121
122void 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
129bool 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
181namespace {
182
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000183 /// Provide some functions for accessing standard library prototypes and
184 /// caching them so we don't have to keep recomputing them
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000185 FunctionType* get_strlen(const Type* IntPtrTy)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000186 {
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 Spencerbb92b4f2005-04-26 19:13:17 +0000192 strlen_type = FunctionType::get(IntPtrTy, args, false);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000193 }
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 Spencer76dab9a2005-04-26 05:24:00 +0000213
Reid Spencerb4f7b832005-04-26 07:45:18 +0000214 /// 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 Spencer76dab9a2005-04-26 05:24:00 +0000220 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000221 assert(V != 0 && "Invalid args to getConstantStringLength");
Reid Spencerb4f7b832005-04-26 07:45:18 +0000222 len = 0; // make sure we initialize this
Reid Spencer76dab9a2005-04-26 05:24:00 +0000223 User* GEP = 0;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000224 // 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 Spencer76dab9a2005-04-26 05:24:00 +0000227 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 Spencerb4f7b832005-04-26 07:45:18 +0000233 return false;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000234 else
Reid Spencerb4f7b832005-04-26 07:45:18 +0000235 return false;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000236
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000237 // Make sure the GEP has exactly three arguments.
238 if (GEP->getNumOperands() != 3)
239 return false;
240
Reid Spencer76dab9a2005-04-26 05:24:00 +0000241 // 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 Spencerb4f7b832005-04-26 07:45:18 +0000244 {
245 if (!op1->isNullValue())
Reid Spencer76dab9a2005-04-26 05:24:00 +0000246 return false;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000247 }
Reid Spencer76dab9a2005-04-26 05:24:00 +0000248 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 Spencerb4f7b832005-04-26 07:45:18 +0000253 // 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 Spencer76dab9a2005-04-26 05:24:00 +0000260
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 Spencerb4f7b832005-04-26 07:45:18 +0000266 return false;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000267
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000268 // Get the initializer.
Reid Spencerb4f7b832005-04-26 07:45:18 +0000269 Constant* INTLZR = GV->getInitializer();
Reid Spencer76dab9a2005-04-26 05:24:00 +0000270
Reid Spencerb4f7b832005-04-26 07:45:18 +0000271 // 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 Spencer76dab9a2005-04-26 05:24:00 +0000291 {
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 Spencerb4f7b832005-04-26 07:45:18 +0000303
304 // Subtract out the initial value from the length
305 len -= start_idx;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000306 return true; // success!
307 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000308
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
315struct ExitInMainOptimization : public CallOptimizer
316{
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000317 ExitInMainOptimization() : CallOptimizer("exit") {}
318 virtual ~ExitInMainOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000319
320 // Make sure the called function looks like exit (int argument, int return
321 // type, external linkage, not varargs).
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000322 virtual bool ValidateCalledFunction(const Function* f, const TargetData& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000323 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000324 if (f->arg_size() >= 1)
325 if (f->arg_begin()->getType()->isInteger())
326 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000327 return false;
328 }
329
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000330 virtual bool OptimizeCall(CallInst* ci, const TargetData& TD)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000331 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000332 // 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 Spencer39a762d2005-04-25 02:53:12 +0000343
Reid Spencerf2534c72005-04-25 21:11:48 +0000344 // 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 Spencer39a762d2005-04-25 02:53:12 +0000348
Reid Spencerf2534c72005-04-25 21:11:48 +0000349 // Split the block at the call instruction which places it in a new
350 // basic block.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000351 bb->splitBasicBlock(ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000352
Reid Spencerf2534c72005-04-25 21:11:48 +0000353 // 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 Spencer8ee5aac2005-04-26 03:26:15 +0000357 bb->getInstList().pop_back();
Reid Spencer39a762d2005-04-25 02:53:12 +0000358
Reid Spencerf2534c72005-04-25 21:11:48 +0000359 // 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 Spencer9bbaa2a2005-04-25 03:59:26 +0000368 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000369} ExitInMainOptimizer;
370
Reid Spencerf2534c72005-04-25 21:11:48 +0000371/// 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 Spencer9bbaa2a2005-04-25 03:59:26 +0000377struct StrCatOptimization : public CallOptimizer
378{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000379private:
380 Function* strlen_func;
381 Function* memcpy_func;
382public:
383 StrCatOptimization()
384 : CallOptimizer("strcat")
385 , strlen_func(0)
386 , memcpy_func(0)
387 {}
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000388 virtual ~StrCatOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000389
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000390 inline Function* get_strlen_func(Module*M,const Type* IntPtrTy)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000391 {
392 if (strlen_func)
393 return strlen_func;
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000394 return strlen_func = M->getOrInsertFunction("strlen",get_strlen(IntPtrTy));
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000395 }
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 Spencerf2534c72005-04-25 21:11:48 +0000404 /// @brief Make sure that the "strcat" function has the right prototype
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000405 virtual bool ValidateCalledFunction(const Function* f, const TargetData& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000406 {
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 Spencer8ee5aac2005-04-26 03:26:15 +0000413 {
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 Spencerf2534c72005-04-25 21:11:48 +0000422 return true;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000423 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000424 }
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 Spencerbb92b4f2005-04-26 19:13:17 +0000430 virtual bool OptimizeCall(CallInst* ci, const TargetData& TD)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000431 {
Reid Spencer76dab9a2005-04-26 05:24:00 +0000432 // 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 Spencerb4f7b832005-04-26 07:45:18 +0000435 uint64_t len = 0;
436 if (!getConstantStringLength(ci->getOperand(2),len))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000437 return false;
438
Reid Spencerb4f7b832005-04-26 07:45:18 +0000439 // Handle the simple, do-nothing case
440 if (len == 0)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000441 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000442 ci->replaceAllUsesWith(ci->getOperand(1));
443 ci->eraseFromParent();
444 return true;
445 }
446
Reid Spencerb4f7b832005-04-26 07:45:18 +0000447 // Increment the length because we actually want to memcpy the null
448 // terminator as well.
449 len++;
Reid Spencerf2534c72005-04-25 21:11:48 +0000450
Reid Spencerb4f7b832005-04-26 07:45:18 +0000451 // 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 Spencerbb92b4f2005-04-26 19:13:17 +0000459 new CallInst(get_strlen_func(M,TD.getIntPtrType()),
460 ci->getOperand(1),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000461
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 Spencer9bbaa2a2005-04-25 03:59:26 +0000485 }
486} StrCatOptimizer;
487
Reid Spencer76dab9a2005-04-26 05:24:00 +0000488/// 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.
492struct 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 Spencerbb92b4f2005-04-26 19:13:17 +0000498 virtual bool ValidateCalledFunction(const Function* f, const TargetData& TD)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000499 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000500 if (f->getReturnType() == TD.getIntPtrType())
Reid Spencer76dab9a2005-04-26 05:24:00 +0000501 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 Spencerbb92b4f2005-04-26 19:13:17 +0000509 virtual bool OptimizeCall(CallInst* ci, const TargetData& TD)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000510 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000511 // Get the length of the string
512 uint64_t len = 0;
513 if (!getConstantStringLength(ci->getOperand(1),len))
Reid Spencer76dab9a2005-04-26 05:24:00 +0000514 return false;
515
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000516 ci->replaceAllUsesWith(ConstantInt::get(TD.getIntPtrType(),len));
Reid Spencerb4f7b832005-04-26 07:45:18 +0000517 ci->eraseFromParent();
518 return true;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000519 }
520} StrLenOptimizer;
521
Reid Spencerf2534c72005-04-25 21:11:48 +0000522/// 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.
526struct MemCpyOptimization : public CallOptimizer
527{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000528 MemCpyOptimization() : CallOptimizer("llvm.memcpy") {}
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000529protected:
530 MemCpyOptimization(const char* fname) : CallOptimizer(fname) {}
531public:
Reid Spencerf2534c72005-04-25 21:11:48 +0000532 virtual ~MemCpyOptimization() {}
533
534 /// @brief Make sure that the "memcpy" function has the right prototype
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000535 virtual bool ValidateCalledFunction(const Function* f, const TargetData& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000536 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000537 // Just make sure this has 4 arguments per LLVM spec.
Reid Spencer4855ebf2005-04-26 19:55:57 +0000538 return (f->arg_size() == 4) &&
539 (f->getReturnType() == PointerType::get(Type::VoidTy));
Reid Spencerf2534c72005-04-25 21:11:48 +0000540 }
541
Reid Spencerb4f7b832005-04-26 07:45:18 +0000542 /// 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 Spencerbb92b4f2005-04-26 19:13:17 +0000548 virtual bool OptimizeCall(CallInst* ci, const TargetData& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000549 {
Reid Spencer4855ebf2005-04-26 19:55:57 +0000550 // 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 Spencerbb92b4f2005-04-26 19:13:17 +0000561 if (len > alignment)
Reid Spencerb4f7b832005-04-26 07:45:18 +0000562 return false;
563
564 Value* dest = ci->getOperand(1);
565 Value* src = ci->getOperand(2);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000566 CastInst* SrcCast = 0;
567 CastInst* DestCast = 0;
568 switch (len)
569 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000570 case 0:
571 // Just replace with the destination parameter since a zero length
572 // memcpy is a no-op.
Reid Spencer4855ebf2005-04-26 19:55:57 +0000573 ci->replaceAllUsesWith(
574 new CastInst(dest,PointerType::get(Type::VoidTy),"",ci));
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000575 ci->eraseFromParent();
576 return true;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000577 case 1:
578 SrcCast = new CastInst(src,PointerType::get(Type::SByteTy),"",ci);
579 DestCast = new CastInst(dest,PointerType::get(Type::SByteTy),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000580 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 Spencerb4f7b832005-04-26 07:45:18 +0000584 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 Spencerb4f7b832005-04-26 07:45:18 +0000588 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 Spencerb4f7b832005-04-26 07:45:18 +0000592 break;
593 default:
594 return false;
595 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000596 LoadInst* LI = new LoadInst(SrcCast,"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000597 StoreInst* SI = new StoreInst(LI, DestCast, ci);
Reid Spencer4855ebf2005-04-26 19:55:57 +0000598 ci->replaceAllUsesWith(
599 new CastInst(dest,PointerType::get(Type::VoidTy),"",ci));
Reid Spencerb4f7b832005-04-26 07:45:18 +0000600 ci->eraseFromParent();
601 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000602 }
603} MemCpyOptimizer;
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000604
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.
608struct MemMoveOptimization : public MemCpyOptimization
609{
610 MemMoveOptimization() : MemCpyOptimization("llvm.memmove") {}
611
612} MemMoveOptimizer;
613
Reid Spencer39a762d2005-04-25 02:53:12 +0000614}