LLVM  7.0.0
Functions | Variables
SimplifyLibCalls.cpp File Reference
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Transforms/Utils/BuildLibCalls.h"
Include dependency graph for SimplifyLibCalls.cpp:

Go to the source code of this file.

Functions

static bool ignoreCallingConv (LibFunc Func)
 
static bool isCallingConvCCompatible (CallInst *CI)
 
static bool isOnlyUsedInEqualityComparison (Value *V, Value *With)
 Return true if it is only used in equality comparisons with With. More...
 
static bool callHasFloatingPointArgument (const CallInst *CI)
 
static Value * convertStrToNumber (CallInst *CI, StringRef &Str, int64_t Base)
 
static bool isLocallyOpenedFile (Value *File, CallInst *CI, IRBuilder<> &B, const TargetLibraryInfo *TLI)
 
static Value * foldMallocMemset (CallInst *Memset, IRBuilder<> &B, const TargetLibraryInfo &TLI)
 Fold memset[_chk](malloc(n), 0, n) –> calloc(1, n). More...
 
static Value * valueHasFloatPrecision (Value *Val)
 Return a variant of Val with float type. More...
 
static Value * optimizeUnaryDoubleFP (CallInst *CI, IRBuilder<> &B, bool CheckRetType)
 Shrink double -> float for unary functions like 'floor'. More...
 
static Value * replaceUnaryCall (CallInst *CI, IRBuilder<> &B, Intrinsic::ID IID)
 
static Value * optimizeBinaryDoubleFP (CallInst *CI, IRBuilder<> &B)
 Shrink double -> float for binary functions like 'fmin/fmax'. More...
 
static Value * getPow (Value *InnerChain[33], unsigned Exp, IRBuilder<> &B)
 
static bool isTrigLibCall (CallInst *CI)
 
static void insertSinCosCall (IRBuilder<> &B, Function *OrigCallee, Value *Arg, bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos)
 
static bool isReportingError (Function *Callee, CallInst *CI, int StreamArg)
 

Variables

static cl::opt< bool > EnableUnsafeFPShrink ("enable-double-float-shrink", cl::Hidden, cl::init(false), cl::desc("Enable unsafe double to float " "shrinking for math lib calls"))
 

Function Documentation

◆ callHasFloatingPointArgument()

static bool callHasFloatingPointArgument ( const CallInst *  CI)
static

Definition at line 100 of file SimplifyLibCalls.cpp.

References llvm::any_of(), and llvm::User::operands().

Referenced by isReportingError().

◆ convertStrToNumber()

static Value* convertStrToNumber ( CallInst *  CI,
StringRef &  Str,
int64_t  Base 
)
static

Definition at line 106 of file SimplifyLibCalls.cpp.

Referenced by insertSinCosCall().

◆ foldMallocMemset()

static Value* foldMallocMemset ( CallInst *  Memset,
IRBuilder<> &  B,
const TargetLibraryInfo &  TLI 
)
static

◆ getPow()

static Value* getPow ( Value *  InnerChain[33],
unsigned  Exp,
IRBuilder<> &  B 
)
static

Definition at line 1097 of file SimplifyLibCalls.cpp.

References llvm::abs(), llvm::lltok::APFloat, assert(), llvm::AMDGPU::HSAMD::Kernel::Key::Attrs, B, Callee, llvm::APFloatBase::cmpGreaterThan, llvm::APFloat::compare(), llvm::APFloat::convert(), llvm::APFloat::convertToDouble(), llvm::IRBuilder< T, Inserter >::CreateCall(), llvm::IRBuilder< T, Inserter >::CreateFCmpOEQ(), llvm::IRBuilder< T, Inserter >::CreateFCmpOGT(), llvm::IRBuilder< T, Inserter >::CreateFCmpOLT(), llvm::IRBuilder< T, Inserter >::CreateFDiv(), llvm::IRBuilder< T, Inserter >::CreateFMul(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::IRBuilder< T, Inserter >::CreateSExt(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::dyn_cast(), llvm::emitUnaryFloatFnCall(), F(), llvm::ConstantFP::get(), llvm::CallBase< InstTy >::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase< InstTy >::getCalledFunction(), llvm::Value::getContext(), llvm::Intrinsic::getDeclaration(), llvm::Instruction::getFastMathFlags(), llvm::ConstantExpr::getFPExtend(), llvm::ConstantFP::getInfinity(), llvm::IRBuilderBase::getInt32Ty(), llvm::Function::getIntrinsicID(), llvm::Instruction::getModule(), llvm::Value::getName(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Module::getOrInsertFunction(), llvm::GlobalValue::getParent(), llvm::APFloat::getSemantics(), llvm::Value::getType(), llvm::ConstantFP::getValueAPF(), llvm::CallBase< InstTy >::hasFnAttr(), llvm::Instruction::hasNoNaNs(), llvm::hasUnaryFloatFn(), I, llvm::APFloatBase::IEEEdouble(), llvm::Type::isDoubleTy(), llvm::ConstantFP::isExactlyValue(), llvm::APFloat::isExactlyValue(), llvm::Instruction::isFast(), llvm::Type::isFloatTy(), llvm::APFloat::isInteger(), llvm::ConstantFP::isNegative(), llvm::APFloat::isNegative(), llvm::APFloat::isZero(), llvm::PatternMatch::m_APFloat(), llvm::PatternMatch::m_FMul(), llvm::PatternMatch::m_SpecificFP(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), optimizeBinaryDoubleFP(), optimizeUnaryDoubleFP(), llvm::MipsISD::Ret, llvm::APFloatBase::rmTowardZero, llvm::CallBase< InstTy >::setCallingConv(), llvm::FastMathFlags::setFast(), llvm::IRBuilderBase::setFastMathFlags(), llvm::FastMathFlags::setNoNaNs(), llvm::FastMathFlags::setNoSignedZeros(), llvm::StringRef::startswith(), and llvm::Constant::stripPointerCasts().

◆ ignoreCallingConv()

static bool ignoreCallingConv ( LibFunc  Func)
static

◆ insertSinCosCall()

static void insertSinCosCall ( IRBuilder<> &  B,
Function *  OrigCallee,
Value *  Arg,
bool  UseFloat,
Value *&  Sin,
Value *&  Cos,
Value *&  SinCos 
)
static

Definition at line 1526 of file SimplifyLibCalls.cpp.

References llvm::CallBase< InstTy >::addAttribute(), Arg, assert(), llvm::BasicBlock::begin(), C, Callee, llvm::CallingConv::Cold, convertStrToNumber(), llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::IRBuilder< T, Inserter >::CreateCall(), llvm::IRBuilder< T, Inserter >::CreateExtractElement(), llvm::IRBuilder< T, Inserter >::CreateExtractValue(), llvm::IRBuilder< T, Inserter >::CreateICmpNE(), llvm::IRBuilder< T, Inserter >::CreateICmpSLT(), llvm::IRBuilder< T, Inserter >::CreateICmpULT(), llvm::IRBuilder< T, Inserter >::CreateIntCast(), llvm::IRBuilder< T, Inserter >::CreateNSWNeg(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::IRBuilder< T, Inserter >::CreateSub(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::dyn_cast(), llvm::SmallVectorBase::empty(), F(), llvm::AttributeList::FunctionIndex, llvm::ConstantInt::get(), llvm::StructType::get(), llvm::VectorType::get(), llvm::CallBase< InstTy >::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase< InstTy >::getCalledFunction(), llvm::getConstantStringInfo(), llvm::Intrinsic::getDeclaration(), llvm::Function::getEntryBlock(), llvm::IRBuilderBase::getFalse(), llvm::Instruction::getFunction(), llvm::IRBuilderBase::GetInsertBlock(), llvm::IRBuilderBase::getInt32(), llvm::IRBuilderBase::getInt32Ty(), llvm::Type::getIntegerBitWidth(), llvm::Constant::getNullValue(), llvm::Module::getOrInsertFunction(), llvm::BasicBlock::getParent(), llvm::GlobalValue::getParent(), llvm::Module::getTargetTriple(), llvm::IRBuilderBase::getTrue(), llvm::Value::getType(), llvm::CallBase< InstTy >::hasFnAttr(), llvm::Type::isFloatTy(), isReportingError(), llvm::Type::isStructTy(), isTrigLibCall(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), llvm::IRBuilderBase::SetInsertPoint(), T, llvm::Value::users(), X, llvm::Triple::x86, and llvm::Triple::x86_64.

◆ isCallingConvCCompatible()

static bool isCallingConvCCompatible ( CallInst *  CI)
static

◆ isLocallyOpenedFile()

static bool isLocallyOpenedFile ( Value *  File,
CallInst *  CI,
IRBuilder<> &  B,
const TargetLibraryInfo *  TLI 
)
static

Definition at line 131 of file SimplifyLibCalls.cpp.

References llvm::CallBase< InstTy >::addParamAttr(), llvm::ConstantDataArraySlice::Array, B, llvm::StringRef::begin(), llvm::tgtok::Bits, C, Callee, llvm::castToCStr(), llvm::StringRef::compare(), llvm::computeKnownBits(), llvm::ConstantFoldLoadFromConstPtr(), llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::IRBuilder< T, Inserter >::CreateConstInBoundsGEP1_64(), llvm::IRBuilder< T, Inserter >::CreateGEP(), llvm::IRBuilder< T, Inserter >::CreateICmp(), llvm::IRBuilder< T, Inserter >::CreateICmpNE(), llvm::IRBuilder< T, Inserter >::CreateInBoundsGEP(), llvm::IRBuilder< T, Inserter >::CreateIntToPtr(), llvm::IRBuilder< T, Inserter >::CreateIsNotNull(), llvm::IRBuilder< T, Inserter >::CreateLoad(), llvm::IRBuilderBase::CreateMemCpy(), llvm::IRBuilderBase::CreateMemMove(), llvm::IRBuilderBase::CreateMemSet(), llvm::IRBuilder< T, Inserter >::CreateNeg(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::IRBuilder< T, Inserter >::CreateSExtOrTrunc(), llvm::IRBuilder< T, Inserter >::CreateShl(), llvm::IRBuilder< T, Inserter >::CreateSub(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::IRBuilder< T, Inserter >::CreateZExtOrTrunc(), llvm::StringRef::data(), llvm::dyn_cast(), E, llvm::emitMemChr(), llvm::emitMemCmp(), llvm::emitStrChr(), llvm::emitStrLen(), llvm::emitStrNCmp(), llvm::StringRef::empty(), llvm::StringRef::end(), llvm::ARMBuildAttrs::File, llvm::StringRef::find(), llvm::StringRef::find_first_not_of(), llvm::StringRef::find_first_of(), llvm::APInt::flipAllBits(), GEP, llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::CallBase< InstTy >::getArgOperand(), llvm::ConstantExpr::getBitCast(), llvm::CallBase< InstTy >::getCalledFunction(), llvm::getConstantDataArrayInfo(), llvm::getConstantStringInfo(), llvm::Value::getContext(), llvm::ConstantDataSequential::getElementAsInteger(), llvm::Function::getFunctionType(), llvm::IRBuilderBase::getInt(), llvm::IRBuilderBase::getInt64(), llvm::IRBuilderBase::getInt8(), llvm::IRBuilderBase::getInt8Ty(), llvm::IRBuilderBase::getIntN(), llvm::getKnownAlignment(), llvm::TargetLibraryInfo::getLibFunc(), llvm::Instruction::getModule(), llvm::Constant::getNullValue(), llvm::FunctionType::getParamType(), llvm::Type::getPointerAddressSpace(), llvm::Type::getPointerTo(), llvm::CmpInst::getPredicate(), llvm::ConstantInt::getSExtValue(), llvm::GetStringLength(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), llvm::TargetLibraryInfo::has(), I, llvm::CmpInst::ICMP_ULT, llvm::inferLibFuncAttributes(), llvm::isGEPBasedOnPointerToString(), llvm::Type::isIntegerTy(), llvm::APInt::isNonNegative(), isOnlyUsedInEqualityComparison(), llvm::isOnlyUsedInZeroEqualityComparison(), llvm::ConstantInt::isZero(), llvm::ConstantDataArraySlice::Length, llvm::max(), memcmp, llvm::NextPowerOf2(), llvm::StringRef::npos, llvm::ConstantDataArraySlice::Offset, llvm::PointerMayBeCaptured(), Result, llvm::MipsISD::Ret, llvm::StringRef::rfind(), llvm::APInt::setBit(), SI, llvm::StringRef::size(), llvm::StringRef::substr(), llvm::APInt::ule(), llvm::Value::user_begin(), llvm::Value::user_end(), and llvm::KnownBits::Zero.

Referenced by isReportingError().

◆ isOnlyUsedInEqualityComparison()

static bool isOnlyUsedInEqualityComparison ( Value *  V,
Value *  With 
)
static

Return true if it is only used in equality comparisons with With.

Definition at line 89 of file SimplifyLibCalls.cpp.

References llvm::Value::users().

Referenced by isLocallyOpenedFile().

◆ isReportingError()

static bool isReportingError ( Function *  Callee,
CallInst *  CI,
int  StreamArg 
)
static

Definition at line 1761 of file SimplifyLibCalls.cpp.

References assert(), B, callHasFloatingPointArgument(), llvm::castToCStr(), llvm::Instruction::clone(), llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateGEP(), llvm::IRBuilderBase::CreateGlobalString(), llvm::IRBuilder< T, Inserter >::CreateIntCast(), llvm::IRBuilder< T, Inserter >::CreateLoad(), llvm::IRBuilderBase::CreateMemCpy(), llvm::IRBuilder< T, Inserter >::CreateStore(), llvm::IRBuilder< T, Inserter >::CreateTrunc(), llvm::StringRef::drop_back(), llvm::dyn_cast(), llvm::emitFGetCUnlocked(), llvm::emitFGetSUnlocked(), llvm::emitFPutC(), llvm::emitFPutCUnlocked(), llvm::emitFPutS(), llvm::emitFPutSUnlocked(), llvm::emitFReadUnlocked(), llvm::emitFWrite(), llvm::emitFWriteUnlocked(), llvm::emitPutChar(), llvm::emitPutS(), llvm::emitStrLen(), llvm::StringRef::empty(), llvm::StringRef::find(), llvm::ConstantInt::get(), llvm::CallBase< InstTy >::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase< InstTy >::getCalledFunction(), llvm::getConstantStringInfo(), llvm::Value::getContext(), llvm::Instruction::getFunction(), llvm::Function::getFunctionType(), llvm::IRBuilderBase::GetInsertBlock(), llvm::IRBuilderBase::getInt32(), llvm::IRBuilderBase::getInt8(), llvm::IRBuilderBase::getInt8Ty(), llvm::Value::getName(), llvm::CallBase< InstTy >::getNumArgOperands(), llvm::CallBase< InstTy >::getOperand(), llvm::Module::getOrInsertFunction(), llvm::BasicBlock::getParent(), llvm::GlobalValue::getParent(), llvm::LoadInst::getPointerOperand(), llvm::GetStringLength(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), ignoreCallingConv(), llvm::IRBuilder< T, Inserter >::Insert(), isCallingConvCCompatible(), llvm::GlobalValue::isDeclaration(), llvm::Type::isIntegerTy(), isLocallyOpenedFile(), llvm::Type::isPointerTy(), llvm::CallBase< InstTy >::isStrictFP(), N, llvm::StringRef::npos, llvm::Function::optForSize(), optimizeBinaryDoubleFP(), optimizeUnaryDoubleFP(), Ptr, replaceUnaryCall(), round(), llvm::CallBase< InstTy >::setCalledFunction(), llvm::StringRef::size(), llvm::AMDGPU::HSAMD::Kernel::Arg::Key::Size, and llvm::Value::use_empty().

Referenced by insertSinCosCall().

◆ isTrigLibCall()

static bool isTrigLibCall ( CallInst *  CI)
static

Definition at line 1518 of file SimplifyLibCalls.cpp.

References llvm::CallBase< InstTy >::hasFnAttr().

Referenced by insertSinCosCall().

◆ optimizeBinaryDoubleFP()

static Value* optimizeBinaryDoubleFP ( CallInst *  CI,
IRBuilder<> &  B 
)
static

◆ optimizeUnaryDoubleFP()

static Value* optimizeUnaryDoubleFP ( CallInst *  CI,
IRBuilder<> &  B,
bool  CheckRetType 
)
static

◆ replaceUnaryCall()

static Value* replaceUnaryCall ( CallInst *  CI,
IRBuilder<> &  B,
Intrinsic::ID  IID 
)
static

◆ valueHasFloatPrecision()

static Value* valueHasFloatPrecision ( Value *  Val)
static

Return a variant of Val with float type.

Currently this works in two cases: If Val is an FPExtension of a float value to something bigger, simply return the operand. If Val is a ConstantFP but can be converted to a float ConstantFP without loss of precision do so.

Definition at line 935 of file SimplifyLibCalls.cpp.

References llvm::APFloat::convert(), F(), llvm::ConstantFP::get(), llvm::Value::getType(), llvm::APFloatBase::IEEEsingle(), llvm::Type::isFloatTy(), and llvm::APFloatBase::rmNearestTiesToEven.

Referenced by optimizeBinaryDoubleFP(), and optimizeUnaryDoubleFP().

Variable Documentation

◆ EnableUnsafeFPShrink

cl::opt<bool> EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden, cl::init(false), cl::desc("Enable unsafe double to float " "shrinking for math lib calls"))
static