aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-05-01 06:25:24 +0000
committerChris Lattner <sabre@nondot.org>2008-05-01 06:25:24 +0000
commitfd1cbbe9cfeddab8ec99a9325c1e87311609c0a3 (patch)
tree97c06df8a7a6e2792fdf3f9523324af2afc839d3
parent2facbddb76e68fcc4d842174859506ca1584604c (diff)
Delete the IPO simplify-libcalls and completely reimplement it as
a FunctionPass. This makes it simpler, fixes dozens of bugs, adds a couple of minor features, and shrinks is considerably: from 2214 to 1437 lines. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50520 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Transforms/IPO.h5
-rw-r--r--include/llvm/Transforms/Scalar.h6
-rw-r--r--lib/Transforms/IPO/SimplifyLibCalls.cpp2214
-rw-r--r--lib/Transforms/Scalar/SimplifyLibCalls.cpp1437
4 files changed, 1443 insertions, 2219 deletions
diff --git a/include/llvm/Transforms/IPO.h b/include/llvm/Transforms/IPO.h
index 3c5353a9a3..7d98dfe604 100644
--- a/include/llvm/Transforms/IPO.h
+++ b/include/llvm/Transforms/IPO.h
@@ -159,11 +159,6 @@ FunctionPass *createSingleLoopExtractorPass();
///
ModulePass *createBlockExtractorPass(const std::vector<BasicBlock*> &BTNE);
-/// createOptimizeWellKnownCallsPass - This pass optimizes specific calls to
-/// specific well-known (library) functions.
-ModulePass *createSimplifyLibCallsPass();
-
-
/// createIndMemRemPass - This pass removes potential indirect calls of
/// malloc and free
ModulePass *createIndMemRemPass();
diff --git a/include/llvm/Transforms/Scalar.h b/include/llvm/Transforms/Scalar.h
index 1098405014..f350a513e5 100644
--- a/include/llvm/Transforms/Scalar.h
+++ b/include/llvm/Transforms/Scalar.h
@@ -323,6 +323,12 @@ FunctionPass *createMemCpyOptPass();
// can prove are dead.
//
LoopPass *createLoopDeletionPass();
+
+//===----------------------------------------------------------------------===//
+//
+/// createSimplifyLibCallsPass - This pass optimizes specific calls to
+/// specific well-known (library) functions.
+FunctionPass *createSimplifyLibCallsPass();
//===----------------------------------------------------------------------===//
//
diff --git a/lib/Transforms/IPO/SimplifyLibCalls.cpp b/lib/Transforms/IPO/SimplifyLibCalls.cpp
deleted file mode 100644
index cac25c33df..0000000000
--- a/lib/Transforms/IPO/SimplifyLibCalls.cpp
+++ /dev/null
@@ -1,2214 +0,0 @@
-//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements a module pass that applies a variety of small
-// optimizations for calls to specific well-known function calls (e.g. runtime
-// library functions). For example, a call to the function "exit(3)" that
-// occurs within the main() function can be transformed into a simple "return 3"
-// instruction. Any optimization that takes this form (replace call to library
-// function with simpler code that provides the same result) belongs in this
-// file.
-//
-//===----------------------------------------------------------------------===//
-
-#define DEBUG_TYPE "simplify-libcalls"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/Instructions.h"
-#include "llvm/Intrinsics.h"
-#include "llvm/Module.h"
-#include "llvm/Pass.h"
-#include "llvm/ADT/SmallPtrSet.h"
-#include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/Statistic.h"
-#include "llvm/Config/config.h"
-#include "llvm/Support/Compiler.h"
-#include "llvm/Support/Debug.h"
-#include "llvm/Target/TargetData.h"
-#include "llvm/Transforms/IPO.h"
-#include <cstring>
-using namespace llvm;
-
-/// This statistic keeps track of the total number of library calls that have
-/// been simplified regardless of which call it is.
-STATISTIC(SimplifiedLibCalls, "Number of library calls simplified");
-
-namespace {
- // Forward declarations
- class LibCallOptimization;
- class SimplifyLibCalls;
-
-/// This list is populated by the constructor for LibCallOptimization class.
-/// Therefore all subclasses are registered here at static initialization time
-/// and this list is what the SimplifyLibCalls pass uses to apply the individual
-/// optimizations to the call sites.
-/// @brief The list of optimizations deriving from LibCallOptimization
-static LibCallOptimization *OptList = 0;
-
-/// This class is the abstract base class for the set of optimizations that
-/// corresponds to one library call. The SimplifyLibCalls pass will call the
-/// ValidateCalledFunction method to ask the optimization if a given Function
-/// is the kind that the optimization can handle. If the subclass returns true,
-/// then SImplifyLibCalls will also call the OptimizeCall method to perform,
-/// or attempt to perform, the optimization(s) for the library call. Otherwise,
-/// OptimizeCall won't be called. Subclasses are responsible for providing the
-/// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization
-/// constructor. This is used to efficiently select which call instructions to
-/// optimize. The criteria for a "lib call" is "anything with well known
-/// semantics", typically a library function that is defined by an international
-/// standard. Because the semantics are well known, the optimizations can
-/// generally short-circuit actually calling the function if there's a simpler
-/// way (e.g. strlen(X) can be reduced to a constant if X is a constant global).
-/// @brief Base class for library call optimizations
-class VISIBILITY_HIDDEN LibCallOptimization {
- LibCallOptimization **Prev, *Next;
- const char *FunctionName; ///< Name of the library call we optimize
-#ifndef NDEBUG
- Statistic occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
-#endif
-public:
- /// The \p fname argument must be the name of the library function being
- /// optimized by the subclass.
- /// @brief Constructor that registers the optimization.
- LibCallOptimization(const char *FName, const char *Description)
- : FunctionName(FName) {
-
-#ifndef NDEBUG
- occurrences.construct("simplify-libcalls", Description);
-#endif
- // Register this optimizer in the list of optimizations.
- Next = OptList;
- OptList = this;
- Prev = &OptList;
- if (Next) Next->Prev = &Next;
- }
-
- /// getNext - All libcall optimizations are chained together into a list,
- /// return the next one in the list.
- LibCallOptimization *getNext() { return Next; }
-
- /// @brief Deregister from the optlist
- virtual ~LibCallOptimization() {
- *Prev = Next;
- if (Next) Next->Prev = Prev;
- }
-
- /// The implementation of this function in subclasses should determine if
- /// \p F is suitable for the optimization. This method is called by
- /// SimplifyLibCalls::runOnModule to short circuit visiting all the call
- /// sites of such a function if that function is not suitable in the first
- /// place. If the called function is suitabe, this method should return true;
- /// false, otherwise. This function should also perform any lazy
- /// initialization that the LibCallOptimization needs to do, if its to return
- /// true. This avoids doing initialization until the optimizer is actually
- /// going to be called upon to do some optimization.
- /// @brief Determine if the function is suitable for optimization
- virtual bool ValidateCalledFunction(
- const Function* F, ///< The function that is the target of call sites
- SimplifyLibCalls& SLC ///< The pass object invoking us
- ) = 0;
-
- /// The implementations of this function in subclasses is the heart of the
- /// SimplifyLibCalls algorithm. Sublcasses of this class implement
- /// OptimizeCall to determine if (a) the conditions are right for optimizing
- /// the call and (b) to perform the optimization. If an action is taken
- /// against ci, the subclass is responsible for returning true and ensuring
- /// that ci is erased from its parent.
- /// @brief Optimize a call, if possible.
- virtual bool OptimizeCall(
- CallInst* ci, ///< The call instruction that should be optimized.
- SimplifyLibCalls& SLC ///< The pass object invoking us
- ) = 0;
-
- /// @brief Get the name of the library call being optimized
- const char *getFunctionName() const { return FunctionName; }
-
- bool ReplaceCallWith(CallInst *CI, Value *V) {
- if (!CI->use_empty())
- CI->replaceAllUsesWith(V);
- CI->eraseFromParent();
- return true;
- }
-
- /// @brief Called by SimplifyLibCalls to update the occurrences statistic.
- void succeeded() {
-#ifndef NDEBUG
- DEBUG(++occurrences);
-#endif
- }
-};
-
-/// This class is an LLVM Pass that applies each of the LibCallOptimization
-/// instances to all the call sites in a module, relatively efficiently. The
-/// purpose of this pass is to provide optimizations for calls to well-known
-/// functions with well-known semantics, such as those in the c library. The
-/// class provides the basic infrastructure for handling runOnModule. Whenever
-/// this pass finds a function call, it asks the appropriate optimizer to
-/// validate the call (ValidateLibraryCall). If it is validated, then
-/// the OptimizeCall method is also called.
-/// @brief A ModulePass for optimizing well-known function calls.
-class VISIBILITY_HIDDEN SimplifyLibCalls : public ModulePass {
-public:
- static char ID; // Pass identification, replacement for typeid
- SimplifyLibCalls() : ModulePass((intptr_t)&ID) {}
-
- /// We need some target data for accurate signature details that are
- /// target dependent. So we require target data in our AnalysisUsage.
- /// @brief Require TargetData from AnalysisUsage.
- virtual void getAnalysisUsage(AnalysisUsage& Info) const {
- // Ask that the TargetData analysis be performed before us so we can use
- // the target data.
- Info.addRequired<TargetData>();
- }
-
- /// For this pass, process all of the function calls in the module, calling
- /// ValidateLibraryCall and OptimizeCall as appropriate.
- /// @brief Run all the lib call optimizations on a Module.
- virtual bool runOnModule(Module &M) {
- reset(M);
-
- bool result = false;
- StringMap<LibCallOptimization*> OptznMap;
- for (LibCallOptimization *Optzn = OptList; Optzn; Optzn = Optzn->getNext())
- OptznMap[Optzn->getFunctionName()] = Optzn;
-
- // The call optimizations can be recursive. That is, the optimization might
- // generate a call to another function which can also be optimized. This way
- // we make the LibCallOptimization instances very specific to the case they
- // handle. It also means we need to keep running over the function calls in
- // the module until we don't get any more optimizations possible.
- bool found_optimization = false;
- do {
- found_optimization = false;
- for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
- // All the "well-known" functions are external and have external linkage
- // because they live in a runtime library somewhere and were (probably)
- // not compiled by LLVM. So, we only act on external functions that
- // have external or dllimport linkage and non-empty uses.
- if (!FI->isDeclaration() ||
- !(FI->hasExternalLinkage() || FI->hasDLLImportLinkage()) ||
- FI->use_empty())
- continue;
-
- // Get the optimization class that pertains to this function
- StringMap<LibCallOptimization*>::iterator OMI =
- OptznMap.find(FI->getName());
- if (OMI == OptznMap.end()) continue;
-
- LibCallOptimization *CO = OMI->second;
-
- // Make sure the called function is suitable for the optimization
- if (!CO->ValidateCalledFunction(FI, *this))
- continue;
-
- // Loop over each of the uses of the function
- for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
- UI != UE ; ) {
- // If the use of the function is a call instruction
- if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
- // Do the optimization on the LibCallOptimization.
- if (CO->OptimizeCall(CI, *this)) {
- ++SimplifiedLibCalls;
- found_optimization = result = true;
- CO->succeeded();
- }
- }
- }
- }
- } while (found_optimization);
-
- return result;
- }
-
- /// @brief Return the *current* module we're working on.
- Module* getModule() const { return M; }
-
- /// @brief Return the *current* target data for the module we're working on.
- TargetData* getTargetData() const { return TD; }
-
- /// @brief Return the size_t type -- syntactic shortcut
- const Type* getIntPtrType() const { return TD->getIntPtrType(); }
-
- /// @brief Return a Function* for the putchar libcall
- Constant *get_putchar() {
- if (!putchar_func)
- putchar_func =
- M->getOrInsertFunction("putchar", Type::Int32Ty, Type::Int32Ty, NULL);
- return putchar_func;
- }
-
- /// @brief Return a Function* for the puts libcall
- Constant *get_puts() {
- if (!puts_func)
- puts_func = M->getOrInsertFunction("puts", Type::Int32Ty,
- PointerType::getUnqual(Type::Int8Ty),
- NULL);
- return puts_func;
- }
-
- /// @brief Return a Function* for the fputc libcall
- Constant *get_fputc(const Type* FILEptr_type) {
- if (!fputc_func)
- fputc_func = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
- FILEptr_type, NULL);
- return fputc_func;
- }
-
- /// @brief Return a Function* for the fputs libcall
- Constant *get_fputs(const Type* FILEptr_type) {
- if (!fputs_func)
- fputs_func = M->getOrInsertFunction("fputs", Type::Int32Ty,
- PointerType::getUnqual(Type::Int8Ty),
- FILEptr_type, NULL);
- return fputs_func;
- }
-
- /// @brief Return a Function* for the fwrite libcall
- Constant *get_fwrite(const Type* FILEptr_type) {
- if (!fwrite_func)
- fwrite_func = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
- PointerType::getUnqual(Type::Int8Ty),
- TD->getIntPtrType(),
- TD->getIntPtrType(),
- FILEptr_type, NULL);
- return fwrite_func;
- }
-
- /// @brief Return a Function* for the sqrt libcall
- Constant *get_sqrt() {
- if (!sqrt_func)
- sqrt_func = M->getOrInsertFunction("sqrt", Type::DoubleTy,
- Type::DoubleTy, NULL);
- return sqrt_func;
- }
-
- /// @brief Return a Function* for the strcpy libcall
- Constant *get_strcpy() {
- if (!strcpy_func)
- strcpy_func = M->getOrInsertFunction("strcpy",
- PointerType::getUnqual(Type::Int8Ty),
- PointerType::getUnqual(Type::Int8Ty),
- PointerType::getUnqual(Type::Int8Ty),
- NULL);
- return strcpy_func;
- }
-
- /// @brief Return a Function* for the strlen libcall
- Constant *get_strlen() {
- if (!strlen_func)
- strlen_func = M->getOrInsertFunction("strlen", TD->getIntPtrType(),
- PointerType::getUnqual(Type::Int8Ty),
- NULL);
- return strlen_func;
- }
-
- /// @brief Return a Function* for the memchr libcall
- Constant *get_memchr() {
- if (!memchr_func)
- memchr_func = M->getOrInsertFunction("memchr",
- PointerType::getUnqual(Type::Int8Ty),
- PointerType::getUnqual(Type::Int8Ty),
- Type::Int32Ty, TD->getIntPtrType(),
- NULL);
- return memchr_func;
- }
-
- /// @brief Return a Function* for the memcpy libcall
- Constant *get_memcpy() {
- if (!memcpy_func) {
- Intrinsic::ID IID = (TD->getIntPtrType() == Type::Int32Ty) ?
- Intrinsic::memcpy_i32 : Intrinsic::memcpy_i64;
- memcpy_func = Intrinsic::getDeclaration(M, IID);
- }
- return memcpy_func;
- }
-
- Constant *getUnaryFloatFunction(const char *BaseName, const Type *T = 0) {
- if (T == 0) T = Type::FloatTy;
-
- char NameBuffer[20];
- const char *Name;
- if (T == Type::DoubleTy)
- Name = BaseName; // floor
- else {
- Name = NameBuffer;
- unsigned NameLen = strlen(BaseName);
- assert(NameLen < sizeof(NameBuffer)-2 && "Buffer too small");
- memcpy(NameBuffer, BaseName, NameLen);
- if (T == Type::FloatTy)
- NameBuffer[NameLen] = 'f'; // floorf
- else
- NameBuffer[NameLen] = 'l'; // floorl
- NameBuffer[NameLen+1] = 0;
- }
-
- return M->getOrInsertFunction(Name, T, T, NULL);
- }
-
- Constant *get_floorf() { return getUnaryFloatFunction("floor"); }
- Constant *get_ceilf() { return getUnaryFloatFunction( "ceil"); }
- Constant *get_roundf() { return getUnaryFloatFunction("round"); }
- Constant *get_rintf() { return getUnaryFloatFunction( "rint"); }
- Constant *get_nearbyintf() { return getUnaryFloatFunction("nearbyint"); }
-
-
-
-private:
- /// @brief Reset our cached data for a new Module
- void reset(Module& mod) {
- M = &mod;
- TD = &getAnalysis<TargetData>();
- putchar_func = 0;
- puts_func = 0;
- fputc_func = 0;
- fputs_func = 0;
- fwrite_func = 0;
- memcpy_func = 0;
- memchr_func = 0;
- sqrt_func = 0;
- strcpy_func = 0;
- strlen_func = 0;
- }
-
-private:
- /// Caches for function pointers.
- Constant *putchar_func, *puts_func;
- Constant *fputc_func, *fputs_func, *fwrite_func;
- Constant *memcpy_func, *memchr_func;
- Constant *sqrt_func;
- Constant *strcpy_func, *strlen_func;
- Module *M; ///< Cached Module
- TargetData *TD; ///< Cached TargetData
-};
-
-char SimplifyLibCalls::ID = 0;
-// Register the pass
-RegisterPass<SimplifyLibCalls>
-X("simplify-libcalls", "Simplify well-known library calls");
-
-} // anonymous namespace
-
-// The only public symbol in this file which just instantiates the pass object
-ModulePass *llvm::createSimplifyLibCallsPass() {
- return new SimplifyLibCalls();
-}
-
-// Forward declare utility functions.
-static bool GetConstantStringInfo(Value *V, std::string &Str);
-static Value *CastToCStr(Value *V, Instruction *IP);
-static uint64_t GetStringLength(Value *V);
-
-
-// Classes below here, in the anonymous namespace, are all subclasses of the
-// LibCallOptimization class, each implementing all optimizations possible for a
-// single well-known library call. Each has a static singleton instance that
-// auto registers it into the "optlist" global above.
-namespace {
-
-/// This LibCallOptimization will find instances of a call to "exit" that occurs
-/// within the "main" function and change it to a simple "ret" instruction with
-/// the same value passed to the exit function. When this is done, it splits the
-/// basic block at the exit(3) call and deletes the call instruction.
-/// @brief Replace calls to exit in main with a simple return
-struct VISIBILITY_HIDDEN ExitInMainOptimization : public LibCallOptimization {
- ExitInMainOptimization() : LibCallOptimization("exit",
- "Number of 'exit' calls simplified") {}
-
- // Make sure the called function looks like exit (int argument, int return
- // type, external linkage, not varargs).
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- return F->arg_size() >= 1 && F->arg_begin()->getType()->isInteger();
- }
-
- virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) {
- // To be careful, we check that the call to exit is coming from "main", that
- // main has external linkage, and the return type of main and the argument
- // to exit have the same type.
- Function *from = ci->getParent()->getParent();
- if (from->hasExternalLinkage())
- if (from->getReturnType() == ci->getOperand(1)->getType()
- && !isa<StructType>(from->getReturnType()))
- if (from->getName() == "main") {
- // Okay, time to actually do the optimization. First, get the basic
- // block of the call instruction
- BasicBlock* bb = ci->getParent();
-
- // Create a return instruction that we'll replace the call with.
- // Note that the argument of the return is the argument of the call
- // instruction.
- ReturnInst::Create(ci->getOperand(1), ci);
-
- // Split the block at the call instruction which places it in a new
- // basic block.
- bb->splitBasicBlock(ci);
-
- // The block split caused a branch instruction to be inserted into
- // the end of the original block, right after the return instruction
- // that we put there. That's not a valid block, so delete the branch
- // instruction.
- bb->getInstList().pop_back();
-
- // Now we can finally get rid of the call instruction which now lives
- // in the new basic block.
- ci->eraseFromParent();
-
- // Optimization succeeded, return true.
- return true;
- }
- // We didn't pass the criteria for this optimization so return false
- return false;
- }
-} ExitInMainOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strcat library
-/// function. The simplification is possible only if the string being
-/// concatenated is a constant array or a constant expression that results in
-/// a constant string. In this case we can replace it with strlen + llvm.memcpy
-/// of the constant string. Both of these calls are further reduced, if possible
-/// on subsequent passes.
-/// @brief Simplify the strcat library function.
-struct VISIBILITY_HIDDEN StrCatOptimization : public LibCallOptimization {
-public:
- /// @brief Default constructor
- StrCatOptimization() : LibCallOptimization("strcat",
- "Number of 'strcat' calls simplified") {}
-
-public:
-
- /// @brief Make sure that the "strcat" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getNumParams() == 2 &&
- FT->getReturnType() == PointerType::getUnqual(Type::Int8Ty) &&
- FT->getParamType(0) == FT->getReturnType() &&
- FT->getParamType(1) == FT->getReturnType();
- }
-
- /// @brief Optimize the strcat library function
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- // Extract some information from the instruction
- Value *Dst = CI->getOperand(1);
- Value *Src = CI->getOperand(2);
-
- // See if we can get the length of the input string.
- uint64_t Len = GetStringLength(Src);
- if (Len == 0) return false;
- --Len; // Unbias length.
-
- // Handle the simple, do-nothing case
- if (Len == 0)
- return ReplaceCallWith(CI, Dst);
-
- // We need to find the end of the destination string. That's where the
- // memory is to be moved to. We just generate a call to strlen.
- CallInst *DstLen = CallInst::Create(SLC.get_strlen(), Dst,
- Dst->getName()+".len", CI);
-
- // Now that we have the destination's length, we must index into the
- // destination's pointer to get the actual memcpy destination (end of
- // the string .. we're concatenating).
- Dst = GetElementPtrInst::Create(Dst, DstLen, Dst->getName()+".indexed", CI);
-
- // We have enough information to now generate the memcpy call to
- // do the concatenation for us.
- Value *Vals[] = {
- Dst, Src,
- ConstantInt::get(SLC.getIntPtrType(), Len+1), // copy nul byte.
- ConstantInt::get(Type::Int32Ty, 1) // alignment
- };
- CallInst::Create(SLC.get_memcpy(), Vals, Vals + 4, "", CI);
-
- return ReplaceCallWith(CI, Dst);
- }
-} StrCatOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strchr library
-/// function. It optimizes out cases where the arguments are both constant
-/// and the result can be determined statically.
-/// @brief Simplify the strcmp library function.
-struct VISIBILITY_HIDDEN StrChrOptimization : public LibCallOptimization {
-public:
- StrChrOptimization() : LibCallOptimization("strchr",
- "Number of 'strchr' calls simplified") {}
-
- /// @brief Make sure that the "strchr" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getNumParams() == 2 &&
- FT->getReturnType() == PointerType::getUnqual(Type::Int8Ty) &&
- FT->getParamType(0) == FT->getReturnType() &&
- isa<IntegerType>(FT->getParamType(1));
- }
-
- /// @brief Perform the strchr optimizations
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- Value *SrcStr = CI->getOperand(1);
- // If the second operand is not constant, see if we can compute the length
- // and turn this into memchr.
- ConstantInt *CSI = dyn_cast<ConstantInt>(CI->getOperand(2));
- if (CSI == 0) {
- uint64_t Len = GetStringLength(SrcStr);
- if (Len == 0) return false;
-
- Value *Args[3] = {
- CI->getOperand(1),
- CI->getOperand(2),
- ConstantInt::get(SLC.getIntPtrType(), Len) // include nul.
- };
- return ReplaceCallWith(CI, CallInst::Create(SLC.get_memchr(),
- Args, Args + 3,
- CI->getName(), CI));
- }
-
- // Otherwise, the character is a constant, see if the first argument is
- // a string literal. If so, we can constant fold.
- std::string Str;
- if (!GetConstantStringInfo(SrcStr, Str))
- return false;
-
- // strchr can find the nul character.
- Str += '\0';
-
- // Get the character we're looking for
- char CharValue = CSI->getSExtValue();
-
- // Compute the offset
- uint64_t i = 0;
- while (1) {
- if (i == Str.size()) // Didn't find the char. strchr returns null.
- return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
- // Did we find our match?
- if (Str[i] == CharValue)
- break;
- ++i;
- }
-
- // strchr(s+n,c) -> gep(s+n+i,c)
- // (if c is a constant integer and s is a constant string)
- Value *Idx = ConstantInt::get(Type::Int64Ty, i);
- Value *GEP = GetElementPtrInst::Create(CI->getOperand(1), Idx,
- CI->getOperand(1)->getName() +
- ".strchr", CI);
- return ReplaceCallWith(CI, GEP);
- }
-} StrChrOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strcmp library
-/// function. It optimizes out cases where one or both arguments are constant
-/// and the result can be determined statically.
-/// @brief Simplify the strcmp library function.
-struct VISIBILITY_HIDDEN StrCmpOptimization : public LibCallOptimization {
-public:
- StrCmpOptimization() : LibCallOptimization("strcmp",
- "Number of 'strcmp' calls simplified") {}
-
- /// @brief Make sure that the "strcmp" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getReturnType() == Type::Int32Ty && FT->getNumParams() == 2 &&
- FT->getParamType(0) == FT->getParamType(1) &&
- FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty);
- }
-
- /// @brief Perform the strcmp optimization
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- // First, check to see if src and destination are the same. If they are,
- // then the optimization is to replace the CallInst with a constant 0
- // because the call is a no-op.
- Value *Str1P = CI->getOperand(1);
- Value *Str2P = CI->getOperand(2);
- if (Str1P == Str2P) // strcmp(x,x) -> 0
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
-
- std::string Str1;
- if (!GetConstantStringInfo(Str1P, Str1))
- return false;
- if (Str1.empty()) {
- // strcmp("", x) -> *x
- Value *V = new LoadInst(Str2P, CI->getName()+".load", CI);
- V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
- return ReplaceCallWith(CI, V);
- }
-
- std::string Str2;
- if (!GetConstantStringInfo(Str2P, Str2))
- return false;
- if (Str2.empty()) {
- // strcmp(x,"") -> *x
- Value *V = new LoadInst(Str1P, CI->getName()+".load", CI);
- V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
- return ReplaceCallWith(CI, V);
- }
-
- // strcmp(x, y) -> cnst (if both x and y are constant strings)
- int R = strcmp(Str1.c_str(), Str2.c_str());
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), R));
- }
-} StrCmpOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strncmp library
-/// function. It optimizes out cases where one or both arguments are constant
-/// and the result can be determined statically.
-/// @brief Simplify the strncmp library function.
-struct VISIBILITY_HIDDEN StrNCmpOptimization : public LibCallOptimization {
-public:
- StrNCmpOptimization() : LibCallOptimization("strncmp",
- "Number of 'strncmp' calls simplified") {}
-
- /// @brief Make sure that the "strncmp" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getReturnType() == Type::Int32Ty && FT->getNumParams() == 3 &&
- FT->getParamType(0) == FT->getParamType(1) &&
- FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
- isa<IntegerType>(FT->getParamType(2));
- return false;
- }
-
- /// @brief Perform the strncmp optimization
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- // First, check to see if src and destination are the same. If they are,
- // then the optimization is to replace the CallInst with a constant 0
- // because the call is a no-op.
- Value *Str1P = CI->getOperand(1);
- Value *Str2P = CI->getOperand(2);
- if (Str1P == Str2P) // strncmp(x,x, n) -> 0
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
-
- // Check the length argument, if it is Constant zero then the strings are
- // considered equal.
- uint64_t Length;
- if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
- Length = LengthArg->getZExtValue();
- else
- return false;
-
- if (Length == 0) // strncmp(x,y,0) -> 0
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
-
- std::string Str1;
- if (!GetConstantStringInfo(Str1P, Str1))
- return false;
- if (Str1.empty()) {
- // strncmp("", x, n) -> *x
- Value *V = new LoadInst(Str2P, CI->getName()+".load", CI);
- V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
- return ReplaceCallWith(CI, V);
- }
-
- std::string Str2;
- if (!GetConstantStringInfo(Str2P, Str2))
- return false;
- if (Str2.empty()) {
- // strncmp(x, "", n) -> *x
- Value *V = new LoadInst(Str1P, CI->getName()+".load", CI);
- V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
- return ReplaceCallWith(CI, V);
- }
-
- // strncmp(x, y, n) -> cnst (if both x and y are constant strings)
- int R = strncmp(Str1.c_str(), Str2.c_str(), Length);
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), R));
- }
-} StrNCmpOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strcpy library
-/// function. Two optimizations are possible:
-/// (1) If src and dest are the same and not volatile, just return dest
-/// (2) If the src is a constant then we can convert to llvm.memmove
-/// @brief Simplify the strcpy library function.
-struct VISIBILITY_HIDDEN StrCpyOptimization : public LibCallOptimization {
-public:
- StrCpyOptimization() : LibCallOptimization("strcpy",
- "Number of 'strcpy' calls simplified") {}
-
- /// @brief Make sure that the "strcpy" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getNumParams() == 2 &&
- FT->getParamType(0) == FT->getParamType(1) &&
- FT->getReturnType() == FT->getParamType(0) &&
- FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty);
- }
-
- /// @brief Perform the strcpy optimization
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- // First, check to see if src and destination are the same. If they are,
- // then the optimization is to replace the CallInst with the destination
- // because the call is a no-op. Note that this corresponds to the
- // degenerate strcpy(X,X) case which should have "undefined" results
- // according to the C specification. However, it occurs sometimes and
- // we optimize it as a no-op.
- Value *Dst = CI->getOperand(1);
- Value *Src = CI->getOperand(2);
- if (Dst == Src) {
- // strcpy(x, x) -> x
- return ReplaceCallWith(CI, Dst);
- }
-
- // See if we can get the length of the input string.
- uint64_t Len = GetStringLength(Src);
- if (Len == 0) return false;
- --Len; // Unbias length.
-
- // If the constant string's length is zero we can optimize this by just
- // doing a store of 0 at the first byte of the destination.
- if (Len == 0) {
- new StoreInst(ConstantInt::get(Type::Int8Ty, 0), Dst, CI);
- return ReplaceCallWith(CI, Dst);
- }
-
- // We have enough information to now generate the memcpy call to
- // do the concatenation for us.
- Value *MemcpyOps[] = {
- Dst, Src,
- ConstantInt::get(SLC.getIntPtrType(), Len+1),// Length including nul byte.
- ConstantInt::get(Type::Int32Ty, 1) // alignment
- };
- CallInst::Create(SLC.get_memcpy(), MemcpyOps, MemcpyOps + 4, "", CI);
-
- return ReplaceCallWith(CI, Dst);
- }
-} StrCpyOptimizer;
-
-/// This LibCallOptimization will simplify a call to the strlen library
-/// function by replacing it with a constant value if the string provided to
-/// it is a constant array.
-/// @brief Simplify the strlen library function.
-struct VISIBILITY_HIDDEN StrLenOptimization : public LibCallOptimization {
- StrLenOptimization() : LibCallOptimization("strlen",
- "Number of 'strlen' calls simplified") {}
-
- /// @brief Make sure that the "strlen" function has the right prototype
- virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
- const FunctionType *FT = F->getFunctionType();
- return FT->getNumParams() == 1 &&
- FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
- isa<IntegerType>(FT->getReturnType());
- }
-
- /// @brief Perform the strlen optimization
- virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
- // Make sure we're dealing with an sbyte* here.
- Value *Src = CI->getOperand(1);
-
- // Does the call to strlen have exactly one use?
- if (CI->hasOneUse()) {
- // Is that single use a icmp operator?
- if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CI->use_back()))
- // Is it compared against a constant integer?
- if (ConstantInt *Cst = dyn_cast<ConstantInt>(Cmp->getOperand(1))) {
- // If its compared against length 0 with == or !=
- if (Cst->getZExtValue() == 0 && Cmp->isEquality()) {
- // strlen(x) != 0 -> *x != 0
- // strlen(x) == 0 -> *x == 0
- Value *V = new LoadInst(Src, Src->getName()+".first", CI);
- V = new ICmpInst(Cmp->getPredicate(), V,
- ConstantInt::get(Type::Int8Ty, 0),
- Cmp->getName()+".strlen", CI);
- Cmp->replaceAllUsesWith(V);
- Cmp->eraseFromParent();
- return ReplaceCallWith(CI, 0); // no uses.
- }
- }
- }
-
- // Get the length of the constant string operand
- // strlen("xyz") -> 3 (for example)
- if (uint64_t Len = GetStringLength(Src))
- return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), Len-1));
- return false;
- }
-} StrLenOptimizer;
-
-/// IsOnlyUsedInEqualsComparison - Return true if it only matters that the value
-/// is equal or not-equal to zero.
-static bool IsOnlyUsedInEqualsZeroComparison(Instruction *I) {
- for (Value::use_iterator UI = I->use_begin(), E = I->u