blob: 59e6382bf39a0428a7748b4425359d0528fbe9e0 [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 Spencer7ddcfb32005-04-27 21:29:20 +000040// Forward declarations
Reid Spencere249a822005-04-27 07:54:40 +000041class LibCallOptimization;
42class SimplifyLibCalls;
Reid Spencer7ddcfb32005-04-27 21:29:20 +000043
44/// @brief The list of optimizations deriving from LibCallOptimization
Reid Spencere249a822005-04-27 07:54:40 +000045hash_map<std::string,LibCallOptimization*> optlist;
Reid Spencer39a762d2005-04-25 02:53:12 +000046
Reid Spencere249a822005-04-27 07:54:40 +000047/// This class is the abstract base class for the set of optimizations that
Reid Spencer7ddcfb32005-04-27 21:29:20 +000048/// corresponds to one library call. The SimplifyLibCalls pass will call the
Reid Spencere249a822005-04-27 07:54:40 +000049/// ValidateCalledFunction method to ask the optimization if a given Function
Reid Spencer7ddcfb32005-04-27 21:29:20 +000050/// is the kind that the optimization can handle. If the subclass returns true,
51/// then SImplifyLibCalls will also call the OptimizeCall method to perform,
52/// or attempt to perform, the optimization(s) for the library call. Otherwise,
53/// OptimizeCall won't be called. Subclasses are responsible for providing the
54/// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization
55/// constructor. This is used to efficiently select which call instructions to
56/// optimize. The criteria for a "lib call" is "anything with well known
57/// semantics", typically a library function that is defined by an international
58/// standard. Because the semantics are well known, the optimizations can
59/// generally short-circuit actually calling the function if there's a simpler
60/// way (e.g. strlen(X) can be reduced to a constant if X is a constant global).
Reid Spencere249a822005-04-27 07:54:40 +000061/// @brief Base class for library call optimizations
62struct LibCallOptimization
63{
Reid Spencer7ddcfb32005-04-27 21:29:20 +000064 /// The \p fname argument must be the name of the library function being
65 /// optimized by the subclass.
66 /// @brief Constructor that registers the optimization.
Reid Spencere249a822005-04-27 07:54:40 +000067 LibCallOptimization(const char * fname )
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000068 : func_name(fname)
Reid Spencere95a6472005-04-27 00:05:45 +000069#ifndef NDEBUG
Reid Spencerdc11db62005-04-27 00:20:23 +000070 , stat_name(std::string("simplify-libcalls:")+fname)
Reid Spencere249a822005-04-27 07:54:40 +000071 , occurrences(stat_name.c_str(),"Number of calls simplified")
Reid Spencere95a6472005-04-27 00:05:45 +000072#endif
Reid Spencer39a762d2005-04-25 02:53:12 +000073 {
Reid Spencer7ddcfb32005-04-27 21:29:20 +000074 // Register this call optimizer in the optlist (a hash_map)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000075 optlist[func_name] = this;
Reid Spencer39a762d2005-04-25 02:53:12 +000076 }
77
Reid Spencer7ddcfb32005-04-27 21:29:20 +000078 /// @brief Deregister from the optlist
79 virtual ~LibCallOptimization() { optlist.erase(func_name); }
Reid Spencer8ee5aac2005-04-26 03:26:15 +000080
Reid Spencere249a822005-04-27 07:54:40 +000081 /// The implementation of this function in subclasses should determine if
82 /// \p F is suitable for the optimization. This method is called by
Reid Spencer7ddcfb32005-04-27 21:29:20 +000083 /// SimplifyLibCalls::runOnModule to short circuit visiting all the call
84 /// sites of such a function if that function is not suitable in the first
85 /// place. If the called function is suitabe, this method should return true;
Reid Spencere249a822005-04-27 07:54:40 +000086 /// false, otherwise. This function should also perform any lazy
87 /// initialization that the LibCallOptimization needs to do, if its to return
88 /// true. This avoids doing initialization until the optimizer is actually
89 /// going to be called upon to do some optimization.
Reid Spencer7ddcfb32005-04-27 21:29:20 +000090 /// @brief Determine if the function is suitable for optimization
Reid Spencere249a822005-04-27 07:54:40 +000091 virtual bool ValidateCalledFunction(
92 const Function* F, ///< The function that is the target of call sites
93 SimplifyLibCalls& SLC ///< The pass object invoking us
94 ) = 0;
Reid Spencerbb92b4f2005-04-26 19:13:17 +000095
Reid Spencere249a822005-04-27 07:54:40 +000096 /// The implementations of this function in subclasses is the heart of the
97 /// SimplifyLibCalls algorithm. Sublcasses of this class implement
98 /// OptimizeCall to determine if (a) the conditions are right for optimizing
99 /// the call and (b) to perform the optimization. If an action is taken
100 /// against ci, the subclass is responsible for returning true and ensuring
101 /// that ci is erased from its parent.
Reid Spencere249a822005-04-27 07:54:40 +0000102 /// @brief Optimize a call, if possible.
103 virtual bool OptimizeCall(
104 CallInst* ci, ///< The call instruction that should be optimized.
105 SimplifyLibCalls& SLC ///< The pass object invoking us
106 ) = 0;
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000107
Reid Spencere249a822005-04-27 07:54:40 +0000108 /// @brief Get the name of the library call being optimized
109 const char * getFunctionName() const { return func_name; }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000110
Reid Spencere95a6472005-04-27 00:05:45 +0000111#ifndef NDEBUG
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000112 /// @brief Called by SimplifyLibCalls to update the occurrences statistic.
113 void succeeded() { ++occurrences; }
Reid Spencere95a6472005-04-27 00:05:45 +0000114#endif
Reid Spencere249a822005-04-27 07:54:40 +0000115
116private:
117 const char* func_name; ///< Name of the library call we optimize
118#ifndef NDEBUG
119 std::string stat_name; ///< Holder for debug statistic name
120 Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
121#endif
122};
123
Reid Spencere249a822005-04-27 07:54:40 +0000124/// This class is an LLVM Pass that applies each of the LibCallOptimization
125/// instances to all the call sites in a module, relatively efficiently. The
126/// purpose of this pass is to provide optimizations for calls to well-known
127/// functions with well-known semantics, such as those in the c library. The
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000128/// class provides the basic infrastructure for handling runOnModule. Whenever /// this pass finds a function call, it asks the appropriate optimizer to
129/// validate the call (ValidateLibraryCall). If it is validated, then
130/// the OptimizeCall method is also called.
Reid Spencere249a822005-04-27 07:54:40 +0000131/// @brief A ModulePass for optimizing well-known function calls.
132struct SimplifyLibCalls : public ModulePass
133{
134 /// We need some target data for accurate signature details that are
135 /// target dependent. So we require target data in our AnalysisUsage.
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000136 /// @brief Require TargetData from AnalysisUsage.
Reid Spencere249a822005-04-27 07:54:40 +0000137 virtual void getAnalysisUsage(AnalysisUsage& Info) const
138 {
139 // Ask that the TargetData analysis be performed before us so we can use
140 // the target data.
141 Info.addRequired<TargetData>();
142 }
143
144 /// For this pass, process all of the function calls in the module, calling
145 /// ValidateLibraryCall and OptimizeCall as appropriate.
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000146 /// @brief Run all the lib call optimizations on a Module.
Reid Spencere249a822005-04-27 07:54:40 +0000147 virtual bool runOnModule(Module &M)
148 {
149 reset(M);
150
151 bool result = false;
152
153 // The call optimizations can be recursive. That is, the optimization might
154 // generate a call to another function which can also be optimized. This way
155 // we make the LibCallOptimization instances very specific to the case they
156 // handle. It also means we need to keep running over the function calls in
157 // the module until we don't get any more optimizations possible.
158 bool found_optimization = false;
159 do
160 {
161 found_optimization = false;
162 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
163 {
164 // All the "well-known" functions are external and have external linkage
165 // because they live in a runtime library somewhere and were (probably)
166 // not compiled by LLVM. So, we only act on external functions that have
167 // external linkage and non-empty uses.
168 if (!FI->isExternal() || !FI->hasExternalLinkage() || FI->use_empty())
169 continue;
170
171 // Get the optimization class that pertains to this function
172 LibCallOptimization* CO = optlist[FI->getName().c_str()];
173 if (!CO)
174 continue;
175
176 // Make sure the called function is suitable for the optimization
177 if (!CO->ValidateCalledFunction(FI,*this))
178 continue;
179
180 // Loop over each of the uses of the function
181 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
182 UI != UE ; )
183 {
184 // If the use of the function is a call instruction
185 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
186 {
187 // Do the optimization on the LibCallOptimization.
188 if (CO->OptimizeCall(CI,*this))
189 {
190 ++SimplifiedLibCalls;
191 found_optimization = result = true;
192#ifndef NDEBUG
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000193 CO->succeeded();
Reid Spencere249a822005-04-27 07:54:40 +0000194#endif
195 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000196 }
197 }
198 }
Reid Spencere249a822005-04-27 07:54:40 +0000199 } while (found_optimization);
200 return result;
201 }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000202
Reid Spencere249a822005-04-27 07:54:40 +0000203 /// @brief Return the *current* module we're working on.
204 Module* getModule() { return M; }
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000205
Reid Spencere249a822005-04-27 07:54:40 +0000206 /// @brief Return the *current* target data for the module we're working on.
207 TargetData* getTargetData() { return TD; }
208
209 /// @brief Return a Function* for the strlen libcall
210 Function* get_strlen()
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000211 {
Reid Spencere249a822005-04-27 07:54:40 +0000212 if (!strlen_func)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000213 {
214 std::vector<const Type*> args;
215 args.push_back(PointerType::get(Type::SByteTy));
Reid Spencere249a822005-04-27 07:54:40 +0000216 FunctionType* strlen_type =
217 FunctionType::get(TD->getIntPtrType(), args, false);
218 strlen_func = M->getOrInsertFunction("strlen",strlen_type);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000219 }
Reid Spencere249a822005-04-27 07:54:40 +0000220 return strlen_func;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000221 }
222
Reid Spencere249a822005-04-27 07:54:40 +0000223 /// @brief Return a Function* for the memcpy libcall
224 Function* get_memcpy()
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000225 {
Reid Spencere249a822005-04-27 07:54:40 +0000226 if (!memcpy_func)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000227 {
228 // Note: this is for llvm.memcpy intrinsic
229 std::vector<const Type*> args;
230 args.push_back(PointerType::get(Type::SByteTy));
231 args.push_back(PointerType::get(Type::SByteTy));
232 args.push_back(Type::IntTy);
233 args.push_back(Type::IntTy);
Reid Spencere249a822005-04-27 07:54:40 +0000234 FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false);
235 memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000236 }
Reid Spencere249a822005-04-27 07:54:40 +0000237 return memcpy_func;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000238 }
Reid Spencer76dab9a2005-04-26 05:24:00 +0000239
Reid Spencere249a822005-04-27 07:54:40 +0000240private:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000241 /// @brief Reset our cached data for a new Module
Reid Spencere249a822005-04-27 07:54:40 +0000242 void reset(Module& mod)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000243 {
Reid Spencere249a822005-04-27 07:54:40 +0000244 M = &mod;
245 TD = &getAnalysis<TargetData>();
246 memcpy_func = 0;
247 strlen_func = 0;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000248 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000249
Reid Spencere249a822005-04-27 07:54:40 +0000250private:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000251 Function* memcpy_func; ///< Cached llvm.memcpy function
252 Function* strlen_func; ///< Cached strlen function
253 Module* M; ///< Cached Module
254 TargetData* TD; ///< Cached TargetData
Reid Spencere249a822005-04-27 07:54:40 +0000255};
256
257// Register the pass
258RegisterOpt<SimplifyLibCalls>
259X("simplify-libcalls","Simplify well-known library calls");
260
261} // anonymous namespace
262
263// The only public symbol in this file which just instantiates the pass object
264ModulePass *llvm::createSimplifyLibCallsPass()
265{
266 return new SimplifyLibCalls();
267}
268
269// Classes below here, in the anonymous namespace, are all subclasses of the
270// LibCallOptimization class, each implementing all optimizations possible for a
271// single well-known library call. Each has a static singleton instance that
272// auto registers it into the "optlist" global above.
273namespace {
274
Reid Spencer08b49402005-04-27 17:46:54 +0000275// Forward declare a utility function.
Reid Spencere249a822005-04-27 07:54:40 +0000276bool getConstantStringLength(Value* V, uint64_t& len );
277
278/// This LibCallOptimization will find instances of a call to "exit" that occurs
Reid Spencer39a762d2005-04-25 02:53:12 +0000279/// within the "main" function and change it to a simple "ret" instruction with
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000280/// the same value passed to the exit function. When this is done, it splits the
281/// basic block at the exit(3) call and deletes the call instruction.
Reid Spencer39a762d2005-04-25 02:53:12 +0000282/// @brief Replace calls to exit in main with a simple return
Reid Spencere249a822005-04-27 07:54:40 +0000283struct ExitInMainOptimization : public LibCallOptimization
Reid Spencer39a762d2005-04-25 02:53:12 +0000284{
Reid Spencere249a822005-04-27 07:54:40 +0000285 ExitInMainOptimization() : LibCallOptimization("exit") {}
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000286 virtual ~ExitInMainOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000287
288 // Make sure the called function looks like exit (int argument, int return
289 // type, external linkage, not varargs).
Reid Spencere249a822005-04-27 07:54:40 +0000290 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencerf2534c72005-04-25 21:11:48 +0000291 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000292 if (f->arg_size() >= 1)
293 if (f->arg_begin()->getType()->isInteger())
294 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000295 return false;
296 }
297
Reid Spencere249a822005-04-27 07:54:40 +0000298 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000299 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000300 // To be careful, we check that the call to exit is coming from "main", that
301 // main has external linkage, and the return type of main and the argument
302 // to exit have the same type.
303 Function *from = ci->getParent()->getParent();
304 if (from->hasExternalLinkage())
305 if (from->getReturnType() == ci->getOperand(1)->getType())
306 if (from->getName() == "main")
307 {
308 // Okay, time to actually do the optimization. First, get the basic
309 // block of the call instruction
310 BasicBlock* bb = ci->getParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000311
Reid Spencerf2534c72005-04-25 21:11:48 +0000312 // Create a return instruction that we'll replace the call with.
313 // Note that the argument of the return is the argument of the call
314 // instruction.
315 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000316
Reid Spencerf2534c72005-04-25 21:11:48 +0000317 // Split the block at the call instruction which places it in a new
318 // basic block.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000319 bb->splitBasicBlock(ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000320
Reid Spencerf2534c72005-04-25 21:11:48 +0000321 // The block split caused a branch instruction to be inserted into
322 // the end of the original block, right after the return instruction
323 // that we put there. That's not a valid block, so delete the branch
324 // instruction.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000325 bb->getInstList().pop_back();
Reid Spencer39a762d2005-04-25 02:53:12 +0000326
Reid Spencerf2534c72005-04-25 21:11:48 +0000327 // Now we can finally get rid of the call instruction which now lives
328 // in the new basic block.
329 ci->eraseFromParent();
330
331 // Optimization succeeded, return true.
332 return true;
333 }
334 // We didn't pass the criteria for this optimization so return false
335 return false;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000336 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000337} ExitInMainOptimizer;
338
Reid Spencere249a822005-04-27 07:54:40 +0000339/// This LibCallOptimization will simplify a call to the strcat library
340/// function. The simplification is possible only if the string being
341/// concatenated is a constant array or a constant expression that results in
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000342/// a constant string. In this case we can replace it with strlen + llvm.memcpy
343/// of the constant string. Both of these calls are further reduced, if possible
344/// on subsequent passes.
Reid Spencerf2534c72005-04-25 21:11:48 +0000345/// @brief Simplify the strcat library function.
Reid Spencere249a822005-04-27 07:54:40 +0000346struct StrCatOptimization : public LibCallOptimization
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000347{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000348public:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000349 /// @brief Default constructor
Reid Spencere249a822005-04-27 07:54:40 +0000350 StrCatOptimization() : LibCallOptimization("strcat") {}
351
352public:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000353 /// @breif Destructor
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000354 virtual ~StrCatOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000355
356 /// @brief Make sure that the "strcat" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000357 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencerf2534c72005-04-25 21:11:48 +0000358 {
359 if (f->getReturnType() == PointerType::get(Type::SByteTy))
360 if (f->arg_size() == 2)
361 {
362 Function::const_arg_iterator AI = f->arg_begin();
363 if (AI++->getType() == PointerType::get(Type::SByteTy))
364 if (AI->getType() == PointerType::get(Type::SByteTy))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000365 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000366 // Indicate this is a suitable call type.
Reid Spencerf2534c72005-04-25 21:11:48 +0000367 return true;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000368 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000369 }
370 return false;
371 }
372
Reid Spencere249a822005-04-27 07:54:40 +0000373 /// @brief Optimize the strcat library function
374 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000375 {
Reid Spencer08b49402005-04-27 17:46:54 +0000376 // Extract some information from the instruction
377 Module* M = ci->getParent()->getParent()->getParent();
378 Value* dest = ci->getOperand(1);
379 Value* src = ci->getOperand(2);
380
Reid Spencer76dab9a2005-04-26 05:24:00 +0000381 // Extract the initializer (while making numerous checks) from the
382 // source operand of the call to strcat. If we get null back, one of
383 // a variety of checks in get_GVInitializer failed
Reid Spencerb4f7b832005-04-26 07:45:18 +0000384 uint64_t len = 0;
Reid Spencer08b49402005-04-27 17:46:54 +0000385 if (!getConstantStringLength(src,len))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000386 return false;
387
Reid Spencerb4f7b832005-04-26 07:45:18 +0000388 // Handle the simple, do-nothing case
389 if (len == 0)
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000390 {
Reid Spencer08b49402005-04-27 17:46:54 +0000391 ci->replaceAllUsesWith(dest);
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000392 ci->eraseFromParent();
393 return true;
394 }
395
Reid Spencerb4f7b832005-04-26 07:45:18 +0000396 // Increment the length because we actually want to memcpy the null
397 // terminator as well.
398 len++;
Reid Spencerf2534c72005-04-25 21:11:48 +0000399
Reid Spencerb4f7b832005-04-26 07:45:18 +0000400
401 // We need to find the end of the destination string. That's where the
402 // memory is to be moved to. We just generate a call to strlen (further
Reid Spencere249a822005-04-27 07:54:40 +0000403 // optimized in another pass). Note that the SLC.get_strlen() call
Reid Spencerb4f7b832005-04-26 07:45:18 +0000404 // caches the Function* for us.
405 CallInst* strlen_inst =
Reid Spencer08b49402005-04-27 17:46:54 +0000406 new CallInst(SLC.get_strlen(), dest, dest->getName()+".len",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000407
408 // Now that we have the destination's length, we must index into the
409 // destination's pointer to get the actual memcpy destination (end of
410 // the string .. we're concatenating).
411 std::vector<Value*> idx;
412 idx.push_back(strlen_inst);
413 GetElementPtrInst* gep =
Reid Spencer08b49402005-04-27 17:46:54 +0000414 new GetElementPtrInst(dest,idx,dest->getName()+".indexed",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000415
416 // We have enough information to now generate the memcpy call to
417 // do the concatenation for us.
418 std::vector<Value*> vals;
419 vals.push_back(gep); // destination
420 vals.push_back(ci->getOperand(2)); // source
421 vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length
422 vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment
Reid Spencer08b49402005-04-27 17:46:54 +0000423 new CallInst(SLC.get_memcpy(), vals, "", ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000424
425 // Finally, substitute the first operand of the strcat call for the
426 // strcat call itself since strcat returns its first operand; and,
427 // kill the strcat CallInst.
Reid Spencer08b49402005-04-27 17:46:54 +0000428 ci->replaceAllUsesWith(dest);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000429 ci->eraseFromParent();
430 return true;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000431 }
432} StrCatOptimizer;
433
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000434/// This LibCallOptimization will simplify a call to the strcpy library
435/// function. Two optimizations are possible:
Reid Spencere249a822005-04-27 07:54:40 +0000436/// (1) If src and dest are the same and not volatile, just return dest
437/// (2) If the src is a constant then we can convert to llvm.memmove
438/// @brief Simplify the strcpy library function.
439struct StrCpyOptimization : public LibCallOptimization
440{
441public:
442 StrCpyOptimization() : LibCallOptimization("strcpy") {}
443 virtual ~StrCpyOptimization() {}
444
445 /// @brief Make sure that the "strcpy" function has the right prototype
446 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
447 {
448 if (f->getReturnType() == PointerType::get(Type::SByteTy))
449 if (f->arg_size() == 2)
450 {
451 Function::const_arg_iterator AI = f->arg_begin();
452 if (AI++->getType() == PointerType::get(Type::SByteTy))
453 if (AI->getType() == PointerType::get(Type::SByteTy))
454 {
455 // Indicate this is a suitable call type.
456 return true;
457 }
458 }
459 return false;
460 }
461
462 /// @brief Perform the strcpy optimization
463 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
464 {
465 // First, check to see if src and destination are the same. If they are,
466 // then the optimization is to replace the CallInst with the destination
467 // because the call is a no-op. Note that this corresponds to the
468 // degenerate strcpy(X,X) case which should have "undefined" results
469 // according to the C specification. However, it occurs sometimes and
470 // we optimize it as a no-op.
471 Value* dest = ci->getOperand(1);
472 Value* src = ci->getOperand(2);
473 if (dest == src)
474 {
475 ci->replaceAllUsesWith(dest);
476 ci->eraseFromParent();
477 return true;
478 }
479
480 // Get the length of the constant string referenced by the second operand,
481 // the "src" parameter. Fail the optimization if we can't get the length
482 // (note that getConstantStringLength does lots of checks to make sure this
483 // is valid).
484 uint64_t len = 0;
485 if (!getConstantStringLength(ci->getOperand(2),len))
486 return false;
487
488 // If the constant string's length is zero we can optimize this by just
489 // doing a store of 0 at the first byte of the destination
490 if (len == 0)
491 {
492 new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
493 ci->replaceAllUsesWith(dest);
494 ci->eraseFromParent();
495 return true;
496 }
497
498 // Increment the length because we actually want to memcpy the null
499 // terminator as well.
500 len++;
501
502 // Extract some information from the instruction
503 Module* M = ci->getParent()->getParent()->getParent();
504
505 // We have enough information to now generate the memcpy call to
506 // do the concatenation for us.
507 std::vector<Value*> vals;
508 vals.push_back(dest); // destination
509 vals.push_back(src); // source
510 vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length
511 vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment
Reid Spencer08b49402005-04-27 17:46:54 +0000512 new CallInst(SLC.get_memcpy(), vals, "", ci);
Reid Spencere249a822005-04-27 07:54:40 +0000513
514 // Finally, substitute the first operand of the strcat call for the
515 // strcat call itself since strcat returns its first operand; and,
516 // kill the strcat CallInst.
517 ci->replaceAllUsesWith(dest);
518 ci->eraseFromParent();
519 return true;
520 }
521} StrCpyOptimizer;
522
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000523/// This LibCallOptimization will simplify a call to the strlen library
524/// function by replacing it with a constant value if the string provided to
525/// it is a constant array.
Reid Spencer76dab9a2005-04-26 05:24:00 +0000526/// @brief Simplify the strlen library function.
Reid Spencere249a822005-04-27 07:54:40 +0000527struct StrLenOptimization : public LibCallOptimization
Reid Spencer76dab9a2005-04-26 05:24:00 +0000528{
Reid Spencere249a822005-04-27 07:54:40 +0000529 StrLenOptimization() : LibCallOptimization("strlen") {}
Reid Spencer76dab9a2005-04-26 05:24:00 +0000530 virtual ~StrLenOptimization() {}
531
532 /// @brief Make sure that the "strlen" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000533 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000534 {
Reid Spencere249a822005-04-27 07:54:40 +0000535 if (f->getReturnType() == SLC.getTargetData()->getIntPtrType())
Reid Spencer76dab9a2005-04-26 05:24:00 +0000536 if (f->arg_size() == 1)
537 if (Function::const_arg_iterator AI = f->arg_begin())
538 if (AI->getType() == PointerType::get(Type::SByteTy))
539 return true;
540 return false;
541 }
542
543 /// @brief Perform the strlen optimization
Reid Spencere249a822005-04-27 07:54:40 +0000544 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
Reid Spencer76dab9a2005-04-26 05:24:00 +0000545 {
Reid Spencerb4f7b832005-04-26 07:45:18 +0000546 // Get the length of the string
547 uint64_t len = 0;
548 if (!getConstantStringLength(ci->getOperand(1),len))
Reid Spencer76dab9a2005-04-26 05:24:00 +0000549 return false;
550
Reid Spencere249a822005-04-27 07:54:40 +0000551 ci->replaceAllUsesWith(
552 ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len));
Reid Spencerb4f7b832005-04-26 07:45:18 +0000553 ci->eraseFromParent();
554 return true;
Reid Spencer76dab9a2005-04-26 05:24:00 +0000555 }
556} StrLenOptimizer;
557
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000558/// This LibCallOptimization will simplify a call to the memcpy library
559/// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
560/// bytes depending on the length of the string and the alignment. Additional
561/// optimizations are possible in code generation (sequence of immediate store)
Reid Spencerf2534c72005-04-25 21:11:48 +0000562/// @brief Simplify the memcpy library function.
Reid Spencere249a822005-04-27 07:54:40 +0000563struct MemCpyOptimization : public LibCallOptimization
Reid Spencerf2534c72005-04-25 21:11:48 +0000564{
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000565 /// @brief Default Constructor
Reid Spencere249a822005-04-27 07:54:40 +0000566 MemCpyOptimization() : LibCallOptimization("llvm.memcpy") {}
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000567protected:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000568 /// @brief Subclass Constructor
Reid Spencere249a822005-04-27 07:54:40 +0000569 MemCpyOptimization(const char* fname) : LibCallOptimization(fname) {}
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000570public:
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000571 /// @brief Destructor
Reid Spencerf2534c72005-04-25 21:11:48 +0000572 virtual ~MemCpyOptimization() {}
573
574 /// @brief Make sure that the "memcpy" function has the right prototype
Reid Spencere249a822005-04-27 07:54:40 +0000575 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000576 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000577 // Just make sure this has 4 arguments per LLVM spec.
Reid Spencer2bc7a4f2005-04-26 23:02:16 +0000578 return (f->arg_size() == 4);
Reid Spencerf2534c72005-04-25 21:11:48 +0000579 }
580
Reid Spencerb4f7b832005-04-26 07:45:18 +0000581 /// Because of alignment and instruction information that we don't have, we
582 /// leave the bulk of this to the code generators. The optimization here just
583 /// deals with a few degenerate cases where the length of the string and the
584 /// alignment match the sizes of our intrinsic types so we can do a load and
585 /// store instead of the memcpy call.
586 /// @brief Perform the memcpy optimization.
Reid Spencere249a822005-04-27 07:54:40 +0000587 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
Reid Spencerf2534c72005-04-25 21:11:48 +0000588 {
Reid Spencer4855ebf2005-04-26 19:55:57 +0000589 // Make sure we have constant int values to work with
590 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
591 if (!LEN)
592 return false;
593 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
594 if (!ALIGN)
595 return false;
596
597 // If the length is larger than the alignment, we can't optimize
598 uint64_t len = LEN->getRawValue();
599 uint64_t alignment = ALIGN->getRawValue();
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000600 if (len > alignment)
Reid Spencerb4f7b832005-04-26 07:45:18 +0000601 return false;
602
Reid Spencer08b49402005-04-27 17:46:54 +0000603 // Get the type we will cast to, based on size of the string
Reid Spencerb4f7b832005-04-26 07:45:18 +0000604 Value* dest = ci->getOperand(1);
605 Value* src = ci->getOperand(2);
Reid Spencer08b49402005-04-27 17:46:54 +0000606 Type* castType = 0;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000607 switch (len)
608 {
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000609 case 0:
Reid Spenceraaca1702005-04-26 22:46:23 +0000610 // The memcpy is a no-op so just dump its call.
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000611 ci->eraseFromParent();
612 return true;
Reid Spencer08b49402005-04-27 17:46:54 +0000613 case 1: castType = Type::SByteTy; break;
614 case 2: castType = Type::ShortTy; break;
615 case 4: castType = Type::IntTy; break;
616 case 8: castType = Type::LongTy; break;
Reid Spencerb4f7b832005-04-26 07:45:18 +0000617 default:
618 return false;
619 }
Reid Spencer08b49402005-04-27 17:46:54 +0000620
621 // Cast source and dest to the right sized primitive and then load/store
622 CastInst* SrcCast =
623 new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci);
624 CastInst* DestCast =
625 new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci);
626 LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000627 StoreInst* SI = new StoreInst(LI, DestCast, ci);
Reid Spencerb4f7b832005-04-26 07:45:18 +0000628 ci->eraseFromParent();
629 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000630 }
631} MemCpyOptimizer;
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000632
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000633/// This LibCallOptimization will simplify a call to the memmove library
634/// function. It is identical to MemCopyOptimization except for the name of
635/// the intrinsic.
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000636/// @brief Simplify the memmove library function.
637struct MemMoveOptimization : public MemCpyOptimization
638{
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000639 /// @brief Default Constructor
Reid Spencerbb92b4f2005-04-26 19:13:17 +0000640 MemMoveOptimization() : MemCpyOptimization("llvm.memmove") {}
641
642} MemMoveOptimizer;
643
Reid Spencer7ddcfb32005-04-27 21:29:20 +0000644/// A function to compute the length of a null-terminated constant array of
645/// integers. This function can't rely on the size of the constant array
646/// because there could be a null terminator in the middle of the array.
647/// We also have to bail out if we find a non-integer constant initializer
648/// of one of the elements or if there is no null-terminator. The logic
649/// below checks each of these conditions and will return true only if all
650/// conditions are met. In that case, the \p len parameter is set to the length
651/// of the null-terminated string. If false is returned, the conditions were
652/// not met and len is set to 0.
653/// @brief Get the length of a constant string (null-terminated array).
Reid Spencere249a822005-04-27 07:54:40 +0000654bool getConstantStringLength(Value* V, uint64_t& len )
655{
656 assert(V != 0 && "Invalid args to getConstantStringLength");
657 len = 0; // make sure we initialize this
658 User* GEP = 0;
659 // If the value is not a GEP instruction nor a constant expression with a
660 // GEP instruction, then return false because ConstantArray can't occur
661 // any other way
662 if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V))
663 GEP = GEPI;
664 else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
665 if (CE->getOpcode() == Instruction::GetElementPtr)
666 GEP = CE;
667 else
668 return false;
669 else
670 return false;
671
672 // Make sure the GEP has exactly three arguments.
673 if (GEP->getNumOperands() != 3)
674 return false;
675
676 // Check to make sure that the first operand of the GEP is an integer and
677 // has value 0 so that we are sure we're indexing into the initializer.
678 if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1)))
679 {
680 if (!op1->isNullValue())
681 return false;
682 }
683 else
684 return false;
685
686 // Ensure that the second operand is a ConstantInt. If it isn't then this
687 // GEP is wonky and we're not really sure what were referencing into and
688 // better of not optimizing it. While we're at it, get the second index
689 // value. We'll need this later for indexing the ConstantArray.
690 uint64_t start_idx = 0;
691 if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
692 start_idx = CI->getRawValue();
693 else
694 return false;
695
696 // The GEP instruction, constant or instruction, must reference a global
697 // variable that is a constant and is initialized. The referenced constant
698 // initializer is the array that we'll use for optimization.
699 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
700 if (!GV || !GV->isConstant() || !GV->hasInitializer())
701 return false;
702
703 // Get the initializer.
704 Constant* INTLZR = GV->getInitializer();
705
706 // Handle the ConstantAggregateZero case
707 if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR))
708 {
709 // This is a degenerate case. The initializer is constant zero so the
710 // length of the string must be zero.
711 len = 0;
712 return true;
713 }
714
715 // Must be a Constant Array
716 ConstantArray* A = dyn_cast<ConstantArray>(INTLZR);
717 if (!A)
718 return false;
719
720 // Get the number of elements in the array
721 uint64_t max_elems = A->getType()->getNumElements();
722
723 // Traverse the constant array from start_idx (derived above) which is
724 // the place the GEP refers to in the array.
725 for ( len = start_idx; len < max_elems; len++)
726 {
727 if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
728 {
729 // Check for the null terminator
730 if (CI->isNullValue())
731 break; // we found end of string
732 }
733 else
734 return false; // This array isn't suitable, non-int initializer
735 }
736 if (len >= max_elems)
737 return false; // This array isn't null terminated
738
739 // Subtract out the initial value from the length
740 len -= start_idx;
741 return true; // success!
742}
743
Reid Spencer649ac282005-04-28 04:40:06 +0000744// TODO:
745// Additional cases that we need to add to this file:
746//
747// abs:
748// * abs(cnst) -> cnst'
749//
750// atan:
751// * atan(0.0) -> 0.0
752// * atan(1.0) -> pi/4
753//
754// cbrt:
755// * cbrt(constant) -> constant'
756// * cbrt(expN(X)) -> expN(x/3)
757// * cbrt(sqrt(x)) -> pow(x,1/6)
758// * cbrt(sqrt(x)) -> pow(x,1/9)
759//
760// ceil, ceilf, ceill:
761// * ceil(constant) -> constant'
762//
763// cos, cosf, cosl:
764// * cos(0.0) -> 1.0
765// * cox(-x) -> cos(x)
766//
767// exp, expf, expl:
768// * exp(0.0) -> 1.0
769// * exp(int) -> contant'
770// * exp(log(x)) -> x
771//
772// fabs, fabsf, fabsl:
773// * fabs(cnst) -> cnst'
774//
775// ffs, ffsl, ffsll:
776// * ffs(cnst) -> cnst'
777//
778// floor, floorf, floorl:
779// * floor(cnst) -> cnst'
780//
781// fprintf:
782// * fprintf(file,fmt) -> fputs(fmt,file)
783// (if fmt is constant and constains no % characters)
784// * fprintf(file,"%s",str) -> fputs(orig,str)
785// (only if the fprintf result is not used)
786// * fprintf(file,"%c",chr) -> fputc(chr,file)
787//
788// fputs: (only if the result is not used)
789// * fputs("",F) -> noop
790// * fputs(s,F) -> fputc(s[0],F) (if s is constant and strlen(s) == 1)
791// * fputs(s,F) -> fwrite(s, 1, len, F) (if s is constant and strlen(s) > 1)
792//
793// isascii:
794// * isascii(c) -> ((c & ~0x7f) == 0)
795//
796// isdigit:
797// * isdigit(c) -> (unsigned)(c) - '0' <= 9
798//
799// log, logf, logl:
800// * log(1.0) -> 0.0
801// * log(exp(x)) -> x
802// * log(x**y) -> y*log(x)
803// * log(exp(y)) -> y*log(e)
804// * log(exp2(y)) -> y*log(2)
805// * log(exp10(y)) -> y*log(10)
806// * log(sqrt(x)) -> 0.5*log(x)
807// * log(pow(x,y)) -> y*log(x)
808//
809// lround, lroundf, lroundl:
810// * lround(cnst) -> cnst'
811//
812// memcmp:
813// * memcmp(s1,s2,0) -> 0
814// * memcmp(x,x,l) -> 0
815// * memcmp(x,y,l) -> cnst
816// (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
817// * memcpy(x,y,1) -> *x - *y
818//
819// memcpy:
820// * memcpy(d,s,0,a) -> d
821//
822// memmove:
823// * memmove(d,s,l,a) -> memcpy(d,s,l,a)
824// (if s is a global constant array)
825//
826// memset:
827// * memset(s,c,0) -> noop
828// * memset(s,c,n) -> store s, c
829// (for n=1,2,4,8)
830//
831// pow, powf, powl:
832// * pow(1.0,y) -> 1.0
833// * pow(x,0.0) -> 1.0
834// * pow(x,1.0) -> x
835// * pow(x,-1.0) -> 1.0/x
836// * pow(x,0.5) -> sqrt(x)
837// * pow(cst1,cst2) -> const1**const2
838// * pow(exp(x),y) -> exp(x*y)
839// * pow(sqrt(x),y) -> pow(x,y*0.5)
840// * pow(pow(x,y),z)-> pow(x,y*z)
841//
842// puts:
843// * puts("") -> fputc("\n",stdout) (how do we get "stdout"?)
844//
845// round, roundf, roundl:
846// * round(cnst) -> cnst'
847//
848// signbit:
849// * signbit(cnst) -> cnst'
850// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
851//
852// sin, sinf, sinl:
853// * sin(0.0) -> 0.0
854//
855// sprintf:
856// * sprintf(dest,fmt) -> strcpy(dest,fmt)
857// (if fmt is constant and constains no % characters)
858// * sprintf(dest,"%s",orig) -> strcpy(dest,orig)
859// (only if the sprintf result is not used)
860//
861// sqrt, sqrtf, sqrtl:
862// * sqrt(cnst) -> cnst'
863// * sqrt(expN(x)) -> expN(x*0.5)
864// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
865// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
866//
867// strchr, strrchr:
868// * strchr(s,c) -> offset_of_in(c,s)
869// (if c is a constant integer and s is a constant string)
870// * strrchr(s,c) -> reverse_offset_of_in(c,s)
871// (if c is a constant integer and s is a constant string)
872// * strrchr(s1,0) -> strchr(s1,0)
873//
874// strcmp:
875// * strcmp(x,x) -> 0
876// * strcmp(x,"") -> *x
877// * strcmp("",x) -> *x
878// * strcmp(x,y) -> cnst (if both x and y are constant strings)
879//
880// strncat:
881// * strncat(x,y,0) -> x
882// * strncat(x,y,0) -> x (if strlen(y) = 0)
883// * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
884//
885// strncmp:
886// * strncmp(x,y,0) -> 0
887// * strncmp(x,x,l) -> 0
888// * strncmp(x,"",l) -> *x
889// * strncmp("",x,l) -> *x
890// * strncmp(x,y,1) -> *x - *y
891//
892// strncpy:
893// * strncpy(d,s,0) -> d
894// * strncpy(d,s,l) -> memcpy(d,s,l,1)
895// (if s and l are constants)
896//
897// strpbrk:
898// * strpbrk(s,a) -> offset_in_for(s,a)
899// (if s and a are both constant strings)
900// * strpbrk(s,"") -> 0
901// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
902//
903// strspn, strcspn:
904// * strspn(s,a) -> const_int (if both args are constant)
905// * strspn("",a) -> 0
906// * strspn(s,"") -> 0
907// * strcspn(s,a) -> const_int (if both args are constant)
908// * strcspn("",a) -> 0
909// * strcspn(s,"") -> strlen(a)
910//
911// strstr:
912// * strstr(x,x) -> x
913// * strstr(s1,s2) -> offset_of_s2_in(s1)
914// (if s1 and s2 are constant strings)
915//
916// tan, tanf, tanl:
917// * tan(0.0) -> 0.0
918// * tan(atan(x)) -> x
919//
920// toascii:
921// * toascii(c) -> (c & 0x7f)
922//
923// trunc, truncf, truncl:
924// * trunc(cnst) -> cnst'
925//
926//
Reid Spencer39a762d2005-04-25 02:53:12 +0000927}