LLVM  8.0.1
Functions | Variables
SimplifyLibCalls.cpp File Reference
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
#include "llvm/ADT/APSInt.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/Analysis/Loads.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 ValueconvertStrToNumber (CallInst *CI, StringRef &Str, int64_t Base)
 
static bool isLocallyOpenedFile (Value *File, CallInst *CI, IRBuilder<> &B, const TargetLibraryInfo *TLI)
 
static bool isOnlyUsedInComparisonWithZero (Value *V)
 
static bool canTransformToMemCmp (CallInst *CI, Value *Str, uint64_t Len, const DataLayout &DL)
 
static ValuereplaceUnaryCall (CallInst *CI, IRBuilder<> &B, Intrinsic::ID IID)
 
static ValuevalueHasFloatPrecision (Value *Val)
 Return a variant of Val with float type. More...
 
static ValueoptimizeDoubleFP (CallInst *CI, IRBuilder<> &B, bool isBinary, bool isPrecise=false)
 Shrink double -> float functions. More...
 
static ValueoptimizeUnaryDoubleFP (CallInst *CI, IRBuilder<> &B, bool isPrecise=false)
 Shrink double -> float for unary functions. More...
 
static ValueoptimizeBinaryDoubleFP (CallInst *CI, IRBuilder<> &B, bool isPrecise=false)
 Shrink double -> float for binary functions. More...
 
static ValueoptimizeTrigReflections (CallInst *Call, LibFunc Func, IRBuilder<> &B)
 
static ValuegetPow (Value *InnerChain[33], unsigned Exp, IRBuilder<> &B)
 
static ValuegetSqrtCall (Value *V, AttributeList Attrs, bool NoErrno, Module *M, IRBuilder<> &B, const TargetLibraryInfo *TLI)
 
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< boolEnableUnsafeFPShrink ("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 102 of file SimplifyLibCalls.cpp.

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

Referenced by isReportingError().

◆ canTransformToMemCmp()

static bool canTransformToMemCmp ( CallInst CI,
Value Str,
uint64_t  Len,
const DataLayout DL 
)
static

Definition at line 167 of file SimplifyLibCalls.cpp.

References llvm::CallBase::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 >::CreateIntCast(), 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::emitCalloc(), llvm::emitMalloc(), llvm::emitMemChr(), llvm::emitMemCmp(), llvm::emitStrChr(), llvm::emitStrLen(), llvm::emitStrNCmp(), llvm::StringRef::empty(), llvm::StringRef::end(), 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::getArgOperand(), llvm::ConstantExpr::getBitCast(), llvm::CallBase::getCalledFunction(), llvm::getConstantDataArrayInfo(), llvm::getConstantStringInfo(), llvm::BasicBlock::getContext(), llvm::Value::getContext(), llvm::ConstantDataSequential::getElementAsInteger(), llvm::Instruction::getFunction(), llvm::Function::getFunctionType(), llvm::IRBuilderBase::GetInsertBlock(), llvm::IRBuilderBase::getInt(), llvm::IRBuilderBase::getInt64(), llvm::IRBuilderBase::getInt8(), llvm::IRBuilderBase::getInt8Ty(), llvm::IRBuilderBase::getIntN(), llvm::DataLayout::getIntPtrType(), llvm::getKnownAlignment(), 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::Function::hasFnAttribute(), I, llvm::CmpInst::ICMP_ULT, llvm::isDereferenceableAndAlignedPointer(), llvm::isGEPBasedOnPointerToString(), llvm::Type::isIntegerTy(), llvm::APInt::isNonNegative(), isOnlyUsedInComparisonWithZero(), isOnlyUsedInEqualityComparison(), llvm::isOnlyUsedInZeroEqualityComparison(), llvm::ConstantInt::isZero(), llvm::ConstantDataArraySlice::Length, llvm::max(), memcmp, llvm::NextPowerOf2(), llvm::Attribute::NoCapture, llvm::StringRef::npos, llvm::ConstantDataArraySlice::Offset, llvm::Value::replaceAllUsesWith(), llvm::MipsISD::Ret, llvm::StringRef::rfind(), llvm::Attribute::SanitizeMemory, llvm::APInt::setBit(), llvm::IRBuilderBase::SetInsertPoint(), SI, llvm::StringRef::size(), llvm::StringRef::substr(), llvm::APInt::ule(), llvm::Value::user_begin(), llvm::Value::user_end(), and llvm::KnownBits::Zero.

◆ convertStrToNumber()

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

Definition at line 108 of file SimplifyLibCalls.cpp.

Referenced by insertSinCosCall().

◆ getPow()

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

◆ getSqrtCall()

static Value* getSqrtCall ( Value V,
AttributeList  Attrs,
bool  NoErrno,
Module M,
IRBuilder<> &  B,
const TargetLibraryInfo TLI 
)
static

Definition at line 1307 of file SimplifyLibCalls.cpp.

References llvm::abs(), llvm::APFloat::add(), llvm::lltok::APFloat, llvm::AMDGPU::HSAMD::Kernel::Key::Attrs, B, Callee, llvm::APFloatBase::cmpLessThan, llvm::APFloat::compare(), llvm::APFloat::convert(), llvm::APFloat::convertToDouble(), llvm::IRBuilder< T, Inserter >::CreateCall(), 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::CallBase::doesNotAccessMemory(), llvm::dyn_cast(), llvm::emitUnaryFloatFnCall(), F(), llvm::Intrinsic::fabs, llvm::ConstantFP::get(), llvm::CallBase::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase::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(), getPow(), llvm::APFloat::getSemantics(), llvm::Value::getType(), llvm::Instruction::hasNoInfs(), llvm::Instruction::hasNoNaNs(), llvm::Instruction::hasNoSignedZeros(), llvm::hasUnaryFloatFn(), I, llvm::APFloatBase::IEEEdouble(), llvm::Type::isDoubleTy(), llvm::APFloat::isExactlyValue(), llvm::Instruction::isFast(), llvm::Type::isFloatTy(), llvm::APFloat::isInteger(), llvm::APFloat::isNegative(), llvm::PatternMatch::m_APFloat(), llvm::PatternMatch::m_FMul(), llvm::PatternMatch::m_FPOne(), llvm::PatternMatch::m_SpecificFP(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::Mod, Name, llvm::APFloatBase::opOK, optimizeBinaryDoubleFP(), optimizeUnaryDoubleFP(), llvm::Intrinsic::pow, llvm::MipsISD::Ret, llvm::APFloatBase::rmNearestTiesToEven, llvm::APFloatBase::rmTowardZero, llvm::CallBase::setCallingConv(), llvm::FastMathFlags::setFast(), llvm::IRBuilderBase::setFastMathFlags(), llvm::FastMathFlags::setNoNaNs(), llvm::FastMathFlags::setNoSignedZeros(), llvm::Intrinsic::sqrt, 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 1708 of file SimplifyLibCalls.cpp.

References llvm::CallBase::addAttribute(), Arg, assert(), llvm::BasicBlock::begin(), C, Callee, llvm::Attribute::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::Intrinsic::ctlz, llvm::Intrinsic::cttz, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), F(), llvm::AttributeList::FunctionIndex, llvm::ConstantInt::get(), llvm::StructType::get(), llvm::VectorType::get(), llvm::CallBase::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase::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::hasFnAttr(), llvm::Type::isFloatTy(), isReportingError(), llvm::Type::isStructTy(), isTrigLibCall(), Name, llvm::SmallVectorTemplateBase< T, bool >::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

◆ isOnlyUsedInComparisonWithZero()

static bool isOnlyUsedInComparisonWithZero ( Value V)
static

Definition at line 155 of file SimplifyLibCalls.cpp.

References C, and llvm::Value::users().

Referenced by canTransformToMemCmp().

◆ isOnlyUsedInEqualityComparison()

static bool isOnlyUsedInEqualityComparison ( Value V,
Value With 
)
static

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

Definition at line 91 of file SimplifyLibCalls.cpp.

References llvm::Value::users().

Referenced by canTransformToMemCmp().

◆ isReportingError()

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

Definition at line 1943 of file SimplifyLibCalls.cpp.

References assert(), B, callHasFloatingPointArgument(), llvm::castToCStr(), llvm::Intrinsic::ceil, 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::Intrinsic::fabs, llvm::StringRef::find(), llvm::Intrinsic::floor, llvm::ConstantInt::get(), llvm::CallBase::getArgOperand(), llvm::Function::getAttributes(), llvm::CallBase::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::getNumArgOperands(), llvm::User::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::isStrictFP(), N, llvm::Intrinsic::nearbyint, llvm::StringRef::npos, llvm::Function::optForSize(), optimizeBinaryDoubleFP(), optimizeTrigReflections(), optimizeUnaryDoubleFP(), replaceUnaryCall(), llvm::Intrinsic::rint, llvm::Intrinsic::round, llvm::CallBase::setCalledFunction(), Size, llvm::StringRef::size(), llvm::Intrinsic::trunc, and llvm::Value::use_empty().

Referenced by insertSinCosCall().

◆ isTrigLibCall()

static bool isTrigLibCall ( CallInst CI)
static

◆ optimizeBinaryDoubleFP()

static Value* optimizeBinaryDoubleFP ( CallInst CI,
IRBuilder<> &  B,
bool  isPrecise = false 
)
static

◆ optimizeDoubleFP()

static Value* optimizeDoubleFP ( CallInst CI,
IRBuilder<> &  B,
bool  isBinary,
bool  isPrecise = false 
)
static

◆ optimizeTrigReflections()

static Value* optimizeTrigReflections ( CallInst Call,
LibFunc  Func,
IRBuilder<> &  B 
)
static

◆ optimizeUnaryDoubleFP()

static Value* optimizeUnaryDoubleFP ( CallInst CI,
IRBuilder<> &  B,
bool  isPrecise = false 
)
static

Shrink double -> float for unary functions.

Definition at line 1091 of file SimplifyLibCalls.cpp.

References optimizeDoubleFP().

Referenced by getSqrtCall(), and isReportingError().

◆ 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 1014 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 optimizeDoubleFP().

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