Newer
Older
//===- 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.
//
//===----------------------------------------------------------------------===//
#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/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/IPO.h"
Anton Korobeynikov
committed
#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)
/// 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; }
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.
/// 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 {
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.
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.
virtual bool runOnModule(Module &M) {
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;
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();
// 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;
/// @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
Chris Lattner
committed
Constant *get_putchar() {
putchar_func =
M->getOrInsertFunction("putchar", Type::Int32Ty, Type::Int32Ty, NULL);
return putchar_func;
}
/// @brief Return a Function* for the puts libcall
Chris Lattner
committed
Constant *get_puts() {
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
/// @brief Return a Function* for the fputc libcall
Chris Lattner
committed
Constant *get_fputc(const Type* FILEptr_type) {
if (!fputc_func)
fputc_func = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
return fputc_func;
}
/// @brief Return a Function* for the fputs libcall
Chris Lattner
committed
Constant *get_fputs(const Type* FILEptr_type) {
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
FILEptr_type, NULL);
return fputs_func;
}
/// @brief Return a Function* for the fwrite libcall
Chris Lattner
committed
Constant *get_fwrite(const Type* FILEptr_type) {
if (!fwrite_func)
fwrite_func = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
TD->getIntPtrType(),
TD->getIntPtrType(),
FILEptr_type, NULL);
return fwrite_func;
}
/// @brief Return a Function* for the sqrt libcall
Chris Lattner
committed
Constant *get_sqrt() {
if (!sqrt_func)
sqrt_func = M->getOrInsertFunction("sqrt", Type::DoubleTy,
Type::DoubleTy, NULL);
return sqrt_func;
}
Chris Lattner
committed
Constant *get_strcpy() {
if (!strcpy_func)
strcpy_func = M->getOrInsertFunction("strcpy",
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
return strcpy_func;
}
/// @brief Return a Function* for the strlen libcall
Chris Lattner
committed
Constant *get_strlen() {
strlen_func = M->getOrInsertFunction("strlen", TD->getIntPtrType(),
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
/// @brief Return a Function* for the memchr libcall
Chris Lattner
committed
Constant *get_memchr() {
if (!memchr_func)
memchr_func = M->getOrInsertFunction("memchr",
Christopher Lamb
committed
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
return memchr_func;
}
/// @brief Return a Function* for the memcpy libcall
Chris Lattner
committed
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);
Chris Lattner
committed
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);
Chris Lattner
committed
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"); }
fputc_func = 0;
fwrite_func = 0;
memchr_func = 0;
sqrt_func = 0;
strcpy_func = 0;
/// Caches for function pointers.
Chris Lattner
committed
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
RegisterPass<SimplifyLibCalls>
X("simplify-libcalls", "Simplify well-known library calls");
// 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.
/// 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",
// 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){
Chris Lattner
committed
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
ReturnInst::Create(ci->getOperand(1), ci);
// Split the block at the call instruction which places it in a new
// basic block.
// 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.
// 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;
/// 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 {
StrCatOptimization() : LibCallOptimization("strcat",
"Number of 'strcat' calls simplified") {}
/// @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 &&
Christopher Lamb
committed
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) {
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);
/// 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 &&
Christopher Lamb
committed
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,
// 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;
}
// 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) &&
Christopher Lamb
committed
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 {
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) &&
Christopher Lamb
committed
FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
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 {
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) &&
Christopher Lamb
committed
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.
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);
/// This LibCallOptimization will simplify a call to the strlen library
/// function by replacing it with a constant value if the string provided to
/// @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 &&
Christopher Lamb
committed
FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
}
/// @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 (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;
/// 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->use_end();
UI != E; ++UI) {
if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
if (IC->isEquality())
if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
if (C->isNullValue())
continue;
// Unknown instruction.
return false;
}
return true;
}
/// This memcmpOptimization will simplify a call to the memcmp library
/// function.
struct VISIBILITY_HIDDEN memcmpOptimization : public LibCallOptimization {
/// @brief Default Constructor
memcmpOptimization()
: LibCallOptimization("memcmp", "Number of 'memcmp' calls simplified") {}
/// @brief Make sure that the "memcmp" function has the right prototype
virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &TD) {
Function::const_arg_iterator AI = F->arg_begin();
if (F->arg_size() != 3 || !isa<PointerType>(AI->getType())) return false;
if (!isa<PointerType>((++AI)->getType())) return false;
Chris Lattner
committed
if (!(++AI)->getType()->isInteger()) return false;
if (!F->getReturnType()->isInteger()) return false;
return true;
}
/// Because of alignment and instruction information that we don't have, we
/// leave the bulk of this to the code generators.
///
/// Note that we could do much more if we could force alignment on otherwise
/// small aligned allocas, or if we could indicate that loads have a small
/// alignment.
virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &TD) {
Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
// If the two operands are the same, return zero.
if (LHS == RHS) {
// memcmp(s,s,x) -> 0
return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
}
// Make sure we have a constant length.
ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
if (!LenC) return false;
// If the length is zero, this returns 0.
switch (Len) {
case 0:
// memcmp(s1,s2,0) -> 0
return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
case 1: {
// memcmp(S1,S2,1) -> *(ubyte*)S1 - *(ubyte*)S2
Christopher Lamb
committed
const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
CastInst *Op1Cast = CastInst::create(
Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
CastInst *Op2Cast = CastInst::create(
Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
Value *S1V = new LoadInst(Op1Cast, LHS->getName()+".val", CI);
Value *S2V = new LoadInst(Op2Cast, RHS->getName()+".val", CI);
Value *RV = BinaryOperator::createSub(S1V, S2V, CI->getName()+".diff",CI);
if (RV->getType() != CI->getType())
RV = CastInst::createIntegerCast(RV, CI->getType(), false,
RV->getName(), CI);
return ReplaceCallWith(CI, RV);
}
case 2:
if (IsOnlyUsedInEqualsZeroComparison(CI)) {
// TODO: IF both are aligned, use a short load/compare.
// memcmp(S1,S2,2) -> S1[0]-S2[0] | S1[1]-S2[1] iff only ==/!= 0 matters
Christopher Lamb
committed
const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
CastInst *Op1Cast = CastInst::create(
Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
CastInst *Op2Cast = CastInst::create(
Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
Value *S1V1 = new LoadInst(Op1Cast, LHS->getName()+".val1", CI);
Value *S2V1 = new LoadInst(Op2Cast, RHS->getName()+".val1", CI);
Value *D1 = BinaryOperator::createSub(S1V1, S2V1,
CI->getName()+".d1", CI);
Value *G1 = GetElementPtrInst::Create(Op1Cast, One, "next1v", CI);
Value *G2 = GetElementPtrInst::Create(Op2Cast, One, "next2v", CI);
Value *S1V2 = new LoadInst(G1, LHS->getName()+".val2", CI);
Value *S2V2 = new LoadInst(G2, RHS->getName()+".val2", CI);
Value *D2 = BinaryOperator::createSub(S1V2, S2V2,
CI->getName()+".d1", CI);
Value *Or = BinaryOperator::createOr(D1, D2, CI->getName()+".res", CI);
if (Or->getType() != CI->getType())
Or = CastInst::createIntegerCast(Or, CI->getType(), false /*ZExt*/,
Or->getName(), CI);
return ReplaceCallWith(CI, Or);
}
break;
default:
break;
}
return false;
}
} memcmpOptimizer;
/// This LibCallOptimization will simplify a call to the memcpy library
/// function. It simply converts them into calls to llvm.memcpy.*;
/// the resulting call should be optimized later.
/// @brief Simplify the memcpy library function.
struct VISIBILITY_HIDDEN MemCpyOptimization : public LibCallOptimization {
public:
MemCpyOptimization() : LibCallOptimization("memcpy",
"Number of 'memcpy' calls simplified") {}
/// @brief Make sure that the "memcpy" function has the right prototype
virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
const FunctionType *FT = F->getFunctionType();
const Type* voidPtr = PointerType::getUnqual(Type::Int8Ty);
return FT->getReturnType() == voidPtr && FT->getNumParams() == 3 &&
FT->getParamType(0) == voidPtr &&
FT->getParamType(1) == voidPtr &&
FT->getParamType(2) == SLC.getIntPtrType();
}
/// @brief Perform the memcpy optimization
virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
Value *MemcpyOps[] = {
CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
ConstantInt::get(Type::Int32Ty, 1) // align = 1 always.
};
CallInst::Create(SLC.get_memcpy(), MemcpyOps, MemcpyOps + 4, "", CI);
// memcpy always returns the destination
return ReplaceCallWith(CI, CI->getOperand(1));
}
} MemCpyOptimizer;
/// This LibCallOptimization will simplify a call to the memcpy library
/// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
/// bytes depending on the length of the string and the alignment. Additional
/// optimizations are possible in code generation (sequence of immediate store)
/// @brief Simplify the memcpy library function.
struct VISIBILITY_HIDDEN LLVMMemCpyMoveOptzn : public LibCallOptimization {
LLVMMemCpyMoveOptzn(const char* fname, const char* desc)
: LibCallOptimization(fname, desc) {}
/// @brief Make sure that the "memcpy" function has the right prototype
virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) {
// Just make sure this has 4 arguments per LLVM spec.
return (f->arg_size() == 4);
/// Because of alignment and instruction information that we don't have, we
/// leave the bulk of this to the code generators. The optimization here just
/// deals with a few degenerate cases where the length of the string and the
/// alignment match the sizes of our intrinsic types so we can do a load and
/// store instead of the memcpy call.
/// @brief Perform the memcpy optimization.
virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) {
// Make sure we have constant int values to work with
ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
if (!LEN)
return false;
ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
if (!ALIGN)
return false;
// If the length is larger than the alignment, we can't optimize