blob: be2fe8eb89a7d6507aec2c0b8ccd1fd7f7682c2b [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
Reid Spencer18b99812005-04-26 23:05:17 +000019#define DEBUG_TYPE "simplify-libcalls"
Reid Spencer2bc7a4f2005-04-26 23:02:16 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Instructions.h"
Reid Spencer39a762d2005-04-25 02:53:12 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000025#include "llvm/ADT/hash_map"
Reid Spencer2bc7a4f2005-04-26 23:02:16 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/Support/Debug.h"
Reid Spencerbb92b4f2005-04-26 19:13:17 +000028#include "llvm/Target/TargetData.h"
Reid Spencer2bc7a4f2005-04-26 23:02:16 +000029#include "llvm/Transforms/IPO.h"
Reid Spencerf2534c72005-04-25 21:11:48 +000030#include <iostream>
Reid Spencer39a762d2005-04-25 02:53:12 +000031using namespace llvm;
32
33namespace {
Reid Spencer39a762d2005-04-25 02:53:12 +000034
Reid Spencere249a822005-04-27 07:54:40 +000035/// This statistic keeps track of the total number of library calls that have
36/// been simplified regardless of which call it is.
37Statistic<> SimplifiedLibCalls("simplify-libcalls",
38 "Number of well-known library calls simplified");
Reid Spencer39a762d2005-04-25 02:53:12 +000039
Reid Spencere249a822005-04-27 07:54:40 +000040/// @brief The list of optimizations deriving from LibCallOptimization
41class LibCallOptimization;
42class SimplifyLibCalls;
43hash_map<std::string,LibCallOptimization*> optlist;
Reid Spencer39a762d2005-04-25 02:53:12 +000044
Reid Spencere249a822005-04-27 07:54:40 +000045/// This class is the abstract base class for the set of optimizations that
46/// corresponds to one library call. The SimplifyLibCall pass will call the
47/// ValidateCalledFunction method to ask the optimization if a given Function
48/// is the kind that the optimization can handle. It will also call the
49/// OptimizeCall method to perform, or attempt to perform, the optimization(s)
50/// for the library call. Subclasses of this class are located toward the
51/// end of this file.
52/// @brief Base class for library call optimizations
53struct LibCallOptimization
54{
55 /// @brief Constructor that registers the optimization. The \p fname argument
56 /// must be the name of the library function being optimized by the subclass.
57 LibCallOptimization(const char * fname )
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000058 : func_name(fname)
Reid Spencere95a6472005-04-27 00:05:45 +000059#ifndef NDEBUG
Reid Spencerdc11db62005-04-27 00:20:23 +000060 , stat_name(std::string("simplify-libcalls:")+fname)
Reid Spencere249a822005-04-27 07:54:40 +000061 , occurrences(stat_name.c_str(),"Number of calls simplified")
Reid Spencere95a6472005-04-27 00:05:45 +000062#endif
Reid Spencer39a762d2005-04-25 02:53:12 +000063 {
64 // Register this call optimizer
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000065 optlist[func_name] = this;
Reid Spencer39a762d2005-04-25 02:53:12 +000066 }
67
Reid Spencere249a822005-04-27 07:54:40 +000068 /// @brief Destructor
69 virtual ~LibCallOptimization() {}
Reid Spencer8ee5aac2005-04-26 03:26:15 +000070
Reid Spencere249a822005-04-27 07:54:40 +000071 /// The implementation of this function in subclasses should determine if
72 /// \p F is suitable for the optimization. This method is called by
73 /// runOnModule to short circuit visiting all the call sites of such a
74 /// function if that function is not suitable in the first place.
75 /// If the called function is suitabe, this method should return true;
76 /// false, otherwise. This function should also perform any lazy
77 /// initialization that the LibCallOptimization needs to do, if its to return
78 /// true. This avoids doing initialization until the optimizer is actually
79 /// going to be called upon to do some optimization.
80 virtual bool ValidateCalledFunction(
81 const Function* F, ///< The function that is the target of call sites
82 SimplifyLibCalls& SLC ///< The pass object invoking us
83 ) = 0;
Reid Spencerbb92b4f2005-04-26 19:13:17 +000084
Reid Spencere249a822005-04-27 07:54:40 +000085 /// The implementations of this function in subclasses is the heart of the
86 /// SimplifyLibCalls algorithm. Sublcasses of this class implement
87 /// OptimizeCall to determine if (a) the conditions are right for optimizing
88 /// the call and (b) to perform the optimization. If an action is taken
89 /// against ci, the subclass is responsible for returning true and ensuring
90 /// that ci is erased from its parent.
91 /// @param ci the call instruction under consideration
92 /// @param f the function that ci calls.
93 /// @brief Optimize a call, if possible.
94 virtual bool OptimizeCall(
95 CallInst* ci, ///< The call instruction that should be optimized.
96 SimplifyLibCalls& SLC ///< The pass object invoking us
97 ) = 0;
Reid Spencerbb92b4f2005-04-26 19:13:17 +000098
Reid Spencere249a822005-04-27 07:54:40 +000099 /// @brief Get the name of the library call being optimized
100 const char * getFunctionName() const { return func_name; }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000101
Reid Spencere95a6472005-04-27 00:05:45 +0000102#ifndef NDEBUG
Reid Spencere249a822005-04-27 07:54:40 +0000103 void occurred() { ++occurrences; }
Reid Spencere95a6472005-04-27 00:05:45 +0000104#endif
Reid Spencere249a822005-04-27 07:54:40 +0000105
106private:
107 const char* func_name; ///< Name of the library call we optimize
108#ifndef NDEBUG
109 std::string stat_name; ///< Holder for debug statistic name
110 Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
111#endif
112};
113
114/// This class is the base class for a set of small but important
115/// optimizations of calls to well-known functions, such as those in the c
116/// library.
117
118/// This class is an LLVM Pass that applies each of the LibCallOptimization
119/// instances to all the call sites in a module, relatively efficiently. The
120/// purpose of this pass is to provide optimizations for calls to well-known
121/// functions with well-known semantics, such as those in the c library. The
122/// class provides the basic infrastructure for handling runOnModule.
123/// Whenever this pass finds a function call, it asks the subclasses to
124/// validate the call by calling ValidateLibraryCall. If it is validated, then
125/// the OptimizeCall method is called.
126/// @brief A ModulePass for optimizing well-known function calls.
127struct SimplifyLibCalls : public ModulePass
128{
129 /// We need some target data for accurate signature details that are
130 /// target dependent. So we require target data in our AnalysisUsage.
131 virtual void getAnalysisUsage(AnalysisUsage& Info) const
132 {
133 // Ask that the TargetData analysis be performed before us so we can use
134 // the target data.
135 Info.addRequired<TargetData>();
136 }
137
138 /// For this pass, process all of the function calls in the module, calling
139 /// ValidateLibraryCall and OptimizeCall as appropriate.
140 virtual bool runOnModule(Module &M)
141 {
142 reset(M);
143
144 bool result = false;
145
146 // The call optimizations can be recursive. That is, the optimization might
147 // generate a call to another function which can also be optimized. This way
148 // we make the LibCallOptimization instances very specific to the case they
149 // handle. It also means we need to keep running over the function calls in
150 // the module until we don't get any more optimizations possible.
151 bool found_optimization = false;
152 do
153 {
154 found_optimization = false;
155 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
156 {
157 // All the "well-known" functions are external and have external linkage
158 // because they live in a runtime library somewhere and were (probably)
159 // not compiled by LLVM. So, we only act on external functions that have
160 // external linkage and non-empty uses.
161 if (!FI->isExternal() || !FI->hasExternalLinkage() || FI->use_empty())
162 continue;
163
164 // Get the optimization class that pertains to this function
165 LibCallOptimization* CO = optlist[FI->getName().c_str()];
166 if (!CO)
167 continue;
168
169 // Make sure the called function is suitable for the optimization
170 if (!CO->ValidateCalledFunction(FI,*this))
171 continue;
172
173 // Loop over each of the uses of the function
174 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
175 UI != UE ; )
176 {
177 // If the use of the function is a call instruction
178 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
179 {
180 // Do the optimization on the LibCallOptimization.
181 if (CO->OptimizeCall(CI,*this))
182 {
183 ++SimplifiedLibCalls;
184 found_optimization = result = true;
185#ifndef NDEBUG
186 CO->occurred();
187#endif
188 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000189 }
190 }
191 }
Reid Spencere249a822005-04-27 07:54:40 +0000192 } while (found_optimization);
193 return result;
194 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000195
Reid Spencere249a822005-04-27 07:54:40 +0000196 /// @brief Return the *current* module we're working on.
197 Module* getModule() { return M; }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000198
Reid Spencere249a822005-04-27 07:54:40 +0000199 /// @brief Return the *current* target data for the module we're working on.
200 TargetData* getTargetData() { return TD; }
201
202 /// @brief Return a Function* for the strlen libcall
203 Function* get_strlen()
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000204 {
Reid Spencere249a822005-04-27 07:54:40 +0000205 if (!strlen_func)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000206 {
207 std::vector<const Type*> args;
208 args.push_back(PointerType::get(Type::SByteTy));
Reid Spencere249a822005-04-27 07:54:40 +0000209 FunctionType* strlen_type =
210 FunctionType::get(TD->getIntPtrType(), args, false);
211 strlen_func = M->getOrInsertFunction("strlen",strlen_type);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000212 }
Reid Spencere249a822005-04-27 07:54:40 +0000213 return strlen_func;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000214 }
215
Reid Spencere249a822005-04-27 07:54:40 +0000216 /// @brief Return a Function* for the memcpy libcall
217 Function* get_memcpy()
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000218 {
Reid Spencere249a822005-04-27 07:54:40 +0000219 if (!memcpy_func)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000220 {
221 // Note: this is for llvm.memcpy intrinsic
222 std::vector<const Type*> args;
223 args.push_back(PointerType::get(Type::SByteTy));
224 args.push_back(PointerType::get(Type::SByteTy));
225 args.push_back(Type::IntTy);
226 args.push_back(Type::IntTy);
Reid Spencere249a822005-04-27 07:54:40 +0000227 FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false);
228 memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000229 }
Reid Spencere249a822005-04-27 07:54:40 +0000230 return memcpy_func;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000231 }
Reid Spencer76dab9a2005-04-26 05:24:00 +0000232
Reid Spencere249a822005-04-27 07:54:40 +0000233 /// @brief Compute length of constant string
234 bool getConstantStringLength(Value* V, uint64_t& len );
235
236private:
237 void reset(Module& mod)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000238 {
Reid Spencere249a822005-04-27 07:54:40 +0000239 M = &mod;
240 TD = &getAnalysis<TargetData>();
241 memcpy_func = 0;
242 strlen_func = 0;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000243 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000244
Reid Spencere249a822005-04-27 07:54:40 +0000245private:
246 Function* memcpy_func;
247 Function* strlen_func;
248 Module* M;
249 TargetData* TD;
250};
251
252// Register the pass
253RegisterOpt<SimplifyLibCalls>
254X("simplify-libcalls","Simplify well-known library calls");
255
256} // anonymous namespace
257
258// The only public symbol in this file which just instantiates the pass object
259ModulePass *llvm::createSimplifyLibCallsPass()
260{
261 return new SimplifyLibCalls();
262}
263
264// Classes below here, in the anonymous namespace, are all subclasses of the
265// LibCallOptimization class, each implementing all optimizations possible for a
266// single well-known library call. Each has a static singleton instance that
267// auto registers it into the "optlist" global above.
268namespace {
269
270bool getConstantStringLength(Value* V, uint64_t& len );
271
272/// This LibCallOptimization will find instances of a call to "exit" that occurs
Reid Spencer39a762d2005-04-25 02:53:12 +0000273/// within the "main" function and change it to a simple "ret" instruction with
274/// the same value as passed to the exit function. It assumes that the
275/// instructions after the call to exit(3) can be deleted since they are
276/// unreachable anyway.
277/// @brief Replace calls to exit in main with a simple return
Reid Spencere249a822005-04-27 07:54:40 +0000278struct ExitInMainOptimization : public LibCallOptimization
Reid Spencer39a762d2005-04-25 02:53:12 +0000279{
Reid Spencere249a822005-04-27 07:54:40 +0000280 ExitInMainOptimization() : LibCallOptimization("exit") {}
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000281 virtual ~ExitInMainOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000282
283 // Make sure the called function looks like exit (int argument, int return
284 // type, external linkage, not varargs).
Reid Spencere249a822005-04-27 07:54:40 +0000285 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencerf2534c72005-04-25 21:11:48 +0000286 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000287 if (f->arg_size() >= 1)
288 if (f->arg_begin()->getType()->isInteger())
289 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000290 return false;
291 }
292
Reid Spencere249a822005-04-27 07:54:40 +0000293 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000294 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000295 // To be careful, we check that the call to exit is coming from "main", that
296 // main has external linkage, and the return type of main and the argument
297 // to exit have the same type.
298 Function *from = ci->getParent()->getParent();
299 if (from->hasExternalLinkage())
300 if (from->getReturnType() == ci->getOperand(1)->getType())
301 if (from->getName() == "main")
302 {
303 // Okay, time to actually do the optimization. First, get the basic
304 // block of the call instruction
305 BasicBlock* bb = ci->getParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000306
Reid Spencerf2534c72005-04-25 21:11:48 +0000307 // Create a return instruction that we'll replace the call with.
308 // Note that the argument of the return is the argument of the call
309 // instruction.
310 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000311
Reid Spencerf2534c72005-04-25 21:11:48 +0000312 // Split the block at the call instruction which places it in a new
313 // basic block.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000314 bb->splitBasicBlock(ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000315
Reid Spencerf2534c72005-04-25 21:11:48 +0000316 // The block split caused a branch instruction to be inserted into
317 // the end of the original block, right after the return instruction
318 // that we put there. That's not a valid block, so delete the branch
319 // instruction.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000320 bb->getInstList().pop_back();
Reid Spencer39a762d2005-04-25 02:53:12 +0000321
Reid Spencerf2534c72005-04-25 21:11:48 +0000322 // Now we can finally get rid of the call instruction which now lives
323 // in the new basic block.
324 ci->eraseFromParent();
325
326 // Optimization succeeded, return true.
327 return true;
328 }
329 // We didn't pass the criteria for this optimization so return false
330 return false;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000331 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000332} ExitInMainOptimizer;
333
Reid Spencere249a822005-04-27 07:54:40 +0000334/// This LibCallOptimization will simplify a call to the strcat library
335/// function. The simplification is possible only if the string being
336/// concatenated is a constant array or a constant expression that results in
337/// a constant array. In this case, if the array is small, we can generate a
338/// series of inline store instructions to effect the concatenation without
339/// calling strcat.
Reid Spencerf2534c72005-04-25 21:11:48 +0000340/// @brief Simplify the strcat library function.
Reid Spencere249a822005-04-27 07:54:40 +0000341struct StrCatOptimization : public LibCallOptimization
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000342{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000343public:
Reid Spencere249a822005-04-27 07:54:40 +0000344 StrCatOptimization() : LibCallOptimization("strcat") {}
345
346public:
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000347 virtual ~StrCatOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000348
349 /// @brief Make sure that the "strcat" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000350 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencerf2534c72005-04-25 21:11:48 +0000351 {
352 if (f->getReturnType() == PointerType::get(Type::SByteTy))
353 if (f->arg_size() == 2)
354 {
355 Function::const_arg_iterator AI = f->arg_begin();
356 if (AI++->getType() == PointerType::get(Type::SByteTy))
357 if (AI->getType() == PointerType::get(Type::SByteTy))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000358 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000359 // Indicate this is a suitable call type.
Reid Spencerf2534c72005-04-25 21:11:48 +0000360 return true;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000361 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000362 }
363 return false;
364 }
365
Reid Spencere249a822005-04-27 07:54:40 +0000366 /// @brief Optimize the strcat library function
367 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000368 {
Reid Spencer76dab9a2005-04-26 05:24:00 +0000369 // Extract the initializer (while making numerous checks) from the
370 // source operand of the call to strcat. If we get null back, one of
371 // a variety of checks in get_GVInitializer failed
Reid Spencerb4f7b832005-04-26 07:45:18 +0000372 uint64_t len = 0;
373 if (!getConstantStringLength(ci->getOperand(2),len))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000374 return false;
375
Reid Spencerb4f7b832005-04-26 07:45:18 +0000376 // Handle the simple, do-nothing case
377 if (len == 0)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000378 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000379 ci->replaceAllUsesWith(ci->getOperand(1));
380 ci->eraseFromParent();
381 return true;
382 }
383
Reid Spencerb4f7b832005-04-26 07:45:18 +0000384 // Increment the length because we actually want to memcpy the null
385 // terminator as well.
386 len++;
Reid Spencerf2534c72005-04-25 21:11:48 +0000387
Reid Spencerb4f7b832005-04-26 07:45:18 +0000388 // Extract some information from the instruction
389 Module* M = ci->getParent()->getParent()->getParent();
390
391 // We need to find the end of the destination string. That's where the
392 // memory is to be moved to. We just generate a call to strlen (further
Reid Spencere249a822005-04-27 07:54:40 +0000393 // optimized in another pass). Note that the SLC.get_strlen() call
Reid Spencerb4f7b832005-04-26 07:45:18 +0000394 // caches the Function* for us.
395 CallInst* strlen_inst =
Reid Spencere249a822005-04-27 07:54:40 +0000396 new CallInst(SLC.get_strlen(), ci->getOperand(1),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000397
398 // Now that we have the destination's length, we must index into the
399 // destination's pointer to get the actual memcpy destination (end of
400 // the string .. we're concatenating).
401 std::vector<Value*> idx;
402 idx.push_back(strlen_inst);
403 GetElementPtrInst* gep =
404 new GetElementPtrInst(ci->getOperand(1),idx,"",ci);
405
406 // We have enough information to now generate the memcpy call to
407 // do the concatenation for us.
408 std::vector<Value*> vals;
409 vals.push_back(gep); // destination
410 vals.push_back(ci->getOperand(2)); // source
411 vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length
412 vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment
Reid Spencere249a822005-04-27 07:54:40 +0000413 CallInst* memcpy_inst = new CallInst(SLC.get_memcpy(), vals, "", ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000414
415 // Finally, substitute the first operand of the strcat call for the
416 // strcat call itself since strcat returns its first operand; and,
417 // kill the strcat CallInst.
418 ci->replaceAllUsesWith(ci->getOperand(1));
419 ci->eraseFromParent();
420 return true;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000421 }
422} StrCatOptimizer;
423
Reid Spencere249a822005-04-27 07:54:40 +0000424/// This LibCallOptimization will simplify a call to the strcpy library function.
425/// Several optimizations are possible:
426/// (1) If src and dest are the same and not volatile, just return dest
427/// (2) If the src is a constant then we can convert to llvm.memmove
428/// @brief Simplify the strcpy library function.
429struct StrCpyOptimization : public LibCallOptimization
430{
431public:
432 StrCpyOptimization() : LibCallOptimization("strcpy") {}
433 virtual ~StrCpyOptimization() {}
434
435 /// @brief Make sure that the "strcpy" function has the right prototype
436 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
437 {
438 if (f->getReturnType() == PointerType::get(Type::SByteTy))
439 if (f->arg_size() == 2)
440 {
441 Function::const_arg_iterator AI = f->arg_begin();
442 if (AI++->getType() == PointerType::get(Type::SByteTy))
443 if (AI->getType() == PointerType::get(Type::SByteTy))
444 {
445 // Indicate this is a suitable call type.
446 return true;
447 }
448 }
449 return false;
450 }
451
452 /// @brief Perform the strcpy optimization
453 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
454 {
455 // First, check to see if src and destination are the same. If they are,
456 // then the optimization is to replace the CallInst with the destination
457 // because the call is a no-op. Note that this corresponds to the
458 // degenerate strcpy(X,X) case which should have "undefined" results
459 // according to the C specification. However, it occurs sometimes and
460 // we optimize it as a no-op.
461 Value* dest = ci->getOperand(1);
462 Value* src = ci->getOperand(2);
463 if (dest == src)
464 {
465 ci->replaceAllUsesWith(dest);
466 ci->eraseFromParent();
467 return true;
468 }
469
470 // Get the length of the constant string referenced by the second operand,
471 // the "src" parameter. Fail the optimization if we can't get the length
472 // (note that getConstantStringLength does lots of checks to make sure this
473 // is valid).
474 uint64_t len = 0;
475 if (!getConstantStringLength(ci->getOperand(2),len))
476 return false;
477
478 // If the constant string's length is zero we can optimize this by just
479 // doing a store of 0 at the first byte of the destination
480 if (len == 0)
481 {
482 new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
483 ci->replaceAllUsesWith(dest);
484 ci->eraseFromParent();
485 return true;
486 }
487
488 // Increment the length because we actually want to memcpy the null
489 // terminator as well.
490 len++;
491
492 // Extract some information from the instruction
493 Module* M = ci->getParent()->getParent()->getParent();
494
495 // We have enough information to now generate the memcpy call to
496 // do the concatenation for us.
497 std::vector<Value*> vals;
498 vals.push_back(dest); // destination
499 vals.push_back(src); // source
500 vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length
501 vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment
502 CallInst* memcpy_inst = new CallInst(SLC.get_memcpy(), vals, "", ci);
503
504 // Finally, substitute the first operand of the strcat call for the
505 // strcat call itself since strcat returns its first operand; and,
506 // kill the strcat CallInst.
507 ci->replaceAllUsesWith(dest);
508 ci->eraseFromParent();
509 return true;
510 }
511} StrCpyOptimizer;
512
513/// This LibCallOptimization will simplify a call to the strlen library function by
Reid Spencer76dab9a2005-04-26 05:24:00 +0000514/// replacing it with a constant value if the string provided to it is a
515/// constant array.
516/// @brief Simplify the strlen library function.
Reid Spencere249a822005-04-27 07:54:40 +0000517struct StrLenOptimization : public LibCallOptimization
Reid Spencer76dab9a2005-04-26 05:24:00 +0000518{
Reid Spencere249a822005-04-27 07:54:40 +0000519 StrLenOptimization() : LibCallOptimization("strlen") {}
Reid Spencer76dab9a2005-04-26 05:24:00 +0000520 virtual ~StrLenOptimization() {}
521
522 /// @brief Make sure that the "strlen" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000523 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000524 {
Reid Spencere249a822005-04-27 07:54:40 +0000525 if (f->getReturnType() == SLC.getTargetData()->getIntPtrType())
Reid Spencer76dab9a2005-04-26 05:24:00 +0000526 if (f->arg_size() == 1)
527 if (Function::const_arg_iterator AI = f->arg_begin())
528 if (AI->getType() == PointerType::get(Type::SByteTy))
529 return true;
530 return false;
531 }
532
533 /// @brief Perform the strlen optimization
Reid Spencere249a822005-04-27 07:54:40 +0000534 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000535 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000536 // Get the length of the string
537 uint64_t len = 0;
538 if (!getConstantStringLength(ci->getOperand(1),len))
Reid Spencer76dab9a2005-04-26 05:24:00 +0000539 return false;
540
Reid Spencere249a822005-04-27 07:54:40 +0000541 ci->replaceAllUsesWith(
542 ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len));
Reid Spencerb4f7b832005-04-26 07:45:18 +0000543 ci->eraseFromParent();
544 return true;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000545 }
546} StrLenOptimizer;
547
Reid Spencere249a822005-04-27 07:54:40 +0000548/// This LibCallOptimization will simplify a call to the memcpy library function by
549/// expanding it out to a single store of size 0, 1, 2, 4, or 8 bytes depending
550/// on the length of the string and the alignment.
Reid Spencerf2534c72005-04-25 21:11:48 +0000551/// @brief Simplify the memcpy library function.
Reid Spencere249a822005-04-27 07:54:40 +0000552struct MemCpyOptimization : public LibCallOptimization
Reid Spencerf2534c72005-04-25 21:11:48 +0000553{
Reid Spencere249a822005-04-27 07:54:40 +0000554 MemCpyOptimization() : LibCallOptimization("llvm.memcpy") {}
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000555protected:
Reid Spencere249a822005-04-27 07:54:40 +0000556 MemCpyOptimization(const char* fname) : LibCallOptimization(fname) {}
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000557public:
Reid Spencerf2534c72005-04-25 21:11:48 +0000558 virtual ~MemCpyOptimization() {}
559
560 /// @brief Make sure that the "memcpy" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000561 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000562 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000563 // Just make sure this has 4 arguments per LLVM spec.
Reid Spencer2bc7a4f2005-04-26 23:02:16 +0000564 return (f->arg_size() == 4);
Reid Spencerf2534c72005-04-25 21:11:48 +0000565 }
566
Reid Spencerb4f7b832005-04-26 07:45:18 +0000567 /// Because of alignment and instruction information that we don't have, we
568 /// leave the bulk of this to the code generators. The optimization here just
569 /// deals with a few degenerate cases where the length of the string and the
570 /// alignment match the sizes of our intrinsic types so we can do a load and
571 /// store instead of the memcpy call.
572 /// @brief Perform the memcpy optimization.
Reid Spencere249a822005-04-27 07:54:40 +0000573 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000574 {
Reid Spencer4855ebf2005-04-26 19:55:57 +0000575 // Make sure we have constant int values to work with
576 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
577 if (!LEN)
578 return false;
579 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
580 if (!ALIGN)
581 return false;
582
583 // If the length is larger than the alignment, we can't optimize
584 uint64_t len = LEN->getRawValue();
585 uint64_t alignment = ALIGN->getRawValue();
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000586 if (len > alignment)
Reid Spencerb4f7b832005-04-26 07:45:18 +0000587 return false;
588
589 Value* dest = ci->getOperand(1);
590 Value* src = ci->getOperand(2);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000591 CastInst* SrcCast = 0;
592 CastInst* DestCast = 0;
593 switch (len)
594 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000595 case 0:
Reid Spenceraaca1702005-04-26 22:46:23 +0000596 // The memcpy is a no-op so just dump its call.
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000597 ci->eraseFromParent();
598 return true;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000599 case 1:
600 SrcCast = new CastInst(src,PointerType::get(Type::SByteTy),"",ci);
601 DestCast = new CastInst(dest,PointerType::get(Type::SByteTy),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000602 break;
603 case 2:
604 SrcCast = new CastInst(src,PointerType::get(Type::ShortTy),"",ci);
605 DestCast = new CastInst(dest,PointerType::get(Type::ShortTy),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000606 break;
607 case 4:
608 SrcCast = new CastInst(src,PointerType::get(Type::IntTy),"",ci);
609 DestCast = new CastInst(dest,PointerType::get(Type::IntTy),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000610 break;
611 case 8:
612 SrcCast = new CastInst(src,PointerType::get(Type::LongTy),"",ci);
613 DestCast = new CastInst(dest,PointerType::get(Type::LongTy),"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000614 break;
615 default:
616 return false;
617 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000618 LoadInst* LI = new LoadInst(SrcCast,"",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000619 StoreInst* SI = new StoreInst(LI, DestCast, ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000620 ci->eraseFromParent();
621 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000622 }
623} MemCpyOptimizer;
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000624
Reid Spencere249a822005-04-27 07:54:40 +0000625/// This LibCallOptimization will simplify a call to the memmove library function. /// It is identical to MemCopyOptimization except for the name of the intrinsic.
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000626/// @brief Simplify the memmove library function.
627struct MemMoveOptimization : public MemCpyOptimization
628{
629 MemMoveOptimization() : MemCpyOptimization("llvm.memmove") {}
630
631} MemMoveOptimizer;
632
Reid Spencere249a822005-04-27 07:54:40 +0000633/// A function to compute the length of a null-terminated string of integers.
634/// This function can't rely on the size of the constant array because there
635/// could be a null terminator in the middle of the array. We also have to
636/// bail out if we find a non-integer constant initializer of one of the
637/// elements or if there is no null-terminator. The logic below checks
638bool getConstantStringLength(Value* V, uint64_t& len )
639{
640 assert(V != 0 && "Invalid args to getConstantStringLength");
641 len = 0; // make sure we initialize this
642 User* GEP = 0;
643 // If the value is not a GEP instruction nor a constant expression with a
644 // GEP instruction, then return false because ConstantArray can't occur
645 // any other way
646 if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V))
647 GEP = GEPI;
648 else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
649 if (CE->getOpcode() == Instruction::GetElementPtr)
650 GEP = CE;
651 else
652 return false;
653 else
654 return false;
655
656 // Make sure the GEP has exactly three arguments.
657 if (GEP->getNumOperands() != 3)
658 return false;
659
660 // Check to make sure that the first operand of the GEP is an integer and
661 // has value 0 so that we are sure we're indexing into the initializer.
662 if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1)))
663 {
664 if (!op1->isNullValue())
665 return false;
666 }
667 else
668 return false;
669
670 // Ensure that the second operand is a ConstantInt. If it isn't then this
671 // GEP is wonky and we're not really sure what were referencing into and
672 // better of not optimizing it. While we're at it, get the second index
673 // value. We'll need this later for indexing the ConstantArray.
674 uint64_t start_idx = 0;
675 if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
676 start_idx = CI->getRawValue();
677 else
678 return false;
679
680 // The GEP instruction, constant or instruction, must reference a global
681 // variable that is a constant and is initialized. The referenced constant
682 // initializer is the array that we'll use for optimization.
683 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
684 if (!GV || !GV->isConstant() || !GV->hasInitializer())
685 return false;
686
687 // Get the initializer.
688 Constant* INTLZR = GV->getInitializer();
689
690 // Handle the ConstantAggregateZero case
691 if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR))
692 {
693 // This is a degenerate case. The initializer is constant zero so the
694 // length of the string must be zero.
695 len = 0;
696 return true;
697 }
698
699 // Must be a Constant Array
700 ConstantArray* A = dyn_cast<ConstantArray>(INTLZR);
701 if (!A)
702 return false;
703
704 // Get the number of elements in the array
705 uint64_t max_elems = A->getType()->getNumElements();
706
707 // Traverse the constant array from start_idx (derived above) which is
708 // the place the GEP refers to in the array.
709 for ( len = start_idx; len < max_elems; len++)
710 {
711 if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
712 {
713 // Check for the null terminator
714 if (CI->isNullValue())
715 break; // we found end of string
716 }
717 else
718 return false; // This array isn't suitable, non-int initializer
719 }
720 if (len >= max_elems)
721 return false; // This array isn't null terminated
722
723 // Subtract out the initial value from the length
724 len -= start_idx;
725 return true; // success!
726}
727
728
729// TODO: Additional cases that we need to add to this file:
730// 1. memmove -> memcpy if src is a global constant array
Reid Spencer39a762d2005-04-25 02:53:12 +0000731}