Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:44

0001 //===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file exposes an interface to build some C language libcalls for
0010 // optimization passes that need to call the various functions.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
0015 #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
0016 
0017 #include "llvm/ADT/STLFunctionalExtras.h"
0018 #include "llvm/Analysis/TargetLibraryInfo.h"
0019 
0020 namespace llvm {
0021 class AssumptionCache;
0022 class StringRef;
0023 class Value;
0024 class CallInst;
0025 class DominatorTree;
0026 class DomConditionCache;
0027 class DataLayout;
0028 class Instruction;
0029 class IRBuilderBase;
0030 class Function;
0031 class OptimizationRemarkEmitter;
0032 class BlockFrequencyInfo;
0033 class ProfileSummaryInfo;
0034 
0035 /// This class implements simplifications for calls to fortified library
0036 /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
0037 /// when possible, replace them with their non-checking counterparts.
0038 /// Other optimizations can also be done, but it's possible to disable them and
0039 /// only simplify needless use of the checking versions (when the object size
0040 /// is unknown) by passing true for OnlyLowerUnknownSize.
0041 class FortifiedLibCallSimplifier {
0042 private:
0043   const TargetLibraryInfo *TLI;
0044   bool OnlyLowerUnknownSize;
0045 
0046 public:
0047   FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI,
0048                              bool OnlyLowerUnknownSize = false);
0049 
0050   /// Take the given call instruction and return a more
0051   /// optimal value to replace the instruction with or 0 if a more
0052   /// optimal form can't be found.
0053   /// The call must not be an indirect call.
0054   Value *optimizeCall(CallInst *CI, IRBuilderBase &B);
0055 
0056 private:
0057   Value *optimizeMemCpyChk(CallInst *CI, IRBuilderBase &B);
0058   Value *optimizeMemMoveChk(CallInst *CI, IRBuilderBase &B);
0059   Value *optimizeMemSetChk(CallInst *CI, IRBuilderBase &B);
0060 
0061   /// Str/Stp cpy are similar enough to be handled in the same functions.
0062   Value *optimizeStrpCpyChk(CallInst *CI, IRBuilderBase &B, LibFunc Func);
0063   Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilderBase &B, LibFunc Func);
0064   Value *optimizeStrLenChk(CallInst *CI, IRBuilderBase &B);
0065   Value *optimizeMemPCpyChk(CallInst *CI, IRBuilderBase &B);
0066   Value *optimizeMemCCpyChk(CallInst *CI, IRBuilderBase &B);
0067   Value *optimizeSNPrintfChk(CallInst *CI, IRBuilderBase &B);
0068   Value *optimizeSPrintfChk(CallInst *CI,IRBuilderBase &B);
0069   Value *optimizeStrCatChk(CallInst *CI, IRBuilderBase &B);
0070   Value *optimizeStrLCat(CallInst *CI, IRBuilderBase &B);
0071   Value *optimizeStrNCatChk(CallInst *CI, IRBuilderBase &B);
0072   Value *optimizeStrLCpyChk(CallInst *CI, IRBuilderBase &B);
0073   Value *optimizeVSNPrintfChk(CallInst *CI, IRBuilderBase &B);
0074   Value *optimizeVSPrintfChk(CallInst *CI, IRBuilderBase &B);
0075 
0076   /// Checks whether the call \p CI to a fortified libcall is foldable
0077   /// to the non-fortified version.
0078   ///
0079   /// \param CI the call to the fortified libcall.
0080   ///
0081   /// \param ObjSizeOp the index of the object size parameter of this chk
0082   /// function. Not optional since this is mandatory.
0083   ///
0084   /// \param SizeOp optionally set to the parameter index of an explicit buffer
0085   /// size argument. For instance, set to '2' for __strncpy_chk.
0086   ///
0087   /// \param StrOp optionally set to the parameter index of the source string
0088   /// parameter to strcpy-like functions, where only the strlen of the source
0089   /// will be writtin into the destination.
0090   ///
0091   /// \param FlagsOp optionally set to the parameter index of a 'flags'
0092   /// parameter. These are used by an implementation to opt-into stricter
0093   /// checking.
0094   bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
0095                                std::optional<unsigned> SizeOp = std::nullopt,
0096                                std::optional<unsigned> StrOp = std::nullopt,
0097                                std::optional<unsigned> FlagsOp = std::nullopt);
0098 };
0099 
0100 /// LibCallSimplifier - This class implements a collection of optimizations
0101 /// that replace well formed calls to library functions with a more optimal
0102 /// form.  For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
0103 class LibCallSimplifier {
0104 private:
0105   FortifiedLibCallSimplifier FortifiedSimplifier;
0106   const DataLayout &DL;
0107   const TargetLibraryInfo *TLI;
0108   DominatorTree *DT;
0109   DomConditionCache *DC;
0110   AssumptionCache *AC;
0111   OptimizationRemarkEmitter &ORE;
0112   BlockFrequencyInfo *BFI;
0113   ProfileSummaryInfo *PSI;
0114   bool UnsafeFPShrink = false;
0115   function_ref<void(Instruction *, Value *)> Replacer;
0116   function_ref<void(Instruction *)> Eraser;
0117 
0118   /// Internal wrapper for RAUW that is the default implementation.
0119   ///
0120   /// Other users may provide an alternate function with this signature instead
0121   /// of this one.
0122   static void replaceAllUsesWithDefault(Instruction *I, Value *With) {
0123     I->replaceAllUsesWith(With);
0124   }
0125 
0126   /// Internal wrapper for eraseFromParent that is the default implementation.
0127   static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); }
0128 
0129   /// Replace an instruction's uses with a value using our replacer.
0130   void replaceAllUsesWith(Instruction *I, Value *With);
0131 
0132   /// Erase an instruction from its parent with our eraser.
0133   void eraseFromParent(Instruction *I);
0134 
0135   /// Replace an instruction with a value and erase it from its parent.
0136   void substituteInParent(Instruction *I, Value *With) {
0137     replaceAllUsesWith(I, With);
0138     eraseFromParent(I);
0139   }
0140 
0141 public:
0142   LibCallSimplifier(
0143       const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
0144       DomConditionCache *DC, AssumptionCache *AC,
0145       OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
0146       ProfileSummaryInfo *PSI,
0147       function_ref<void(Instruction *, Value *)> Replacer =
0148           &replaceAllUsesWithDefault,
0149       function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault);
0150 
0151   /// optimizeCall - Take the given call instruction and return a more
0152   /// optimal value to replace the instruction with or 0 if a more
0153   /// optimal form can't be found.  Note that the returned value may
0154   /// be equal to the instruction being optimized.  In this case all
0155   /// other instructions that use the given instruction were modified
0156   /// and the given instruction is dead.
0157   /// The call must not be an indirect call.
0158   Value *optimizeCall(CallInst *CI, IRBuilderBase &B);
0159 
0160 private:
0161   // String and Memory Library Call Optimizations
0162   Value *optimizeStrCat(CallInst *CI, IRBuilderBase &B);
0163   Value *optimizeStrNCat(CallInst *CI, IRBuilderBase &B);
0164   Value *optimizeStrChr(CallInst *CI, IRBuilderBase &B);
0165   Value *optimizeStrRChr(CallInst *CI, IRBuilderBase &B);
0166   Value *optimizeStrCmp(CallInst *CI, IRBuilderBase &B);
0167   Value *optimizeStrNCmp(CallInst *CI, IRBuilderBase &B);
0168   Value *optimizeStrNDup(CallInst *CI, IRBuilderBase &B);
0169   Value *optimizeStrCpy(CallInst *CI, IRBuilderBase &B);
0170   Value *optimizeStpCpy(CallInst *CI, IRBuilderBase &B);
0171   Value *optimizeStrLCpy(CallInst *CI, IRBuilderBase &B);
0172   Value *optimizeStrNCpy(CallInst *CI, IRBuilderBase &B);
0173   Value *optimizeStrLen(CallInst *CI, IRBuilderBase &B);
0174   Value *optimizeStrNLen(CallInst *CI, IRBuilderBase &B);
0175   Value *optimizeStrPBrk(CallInst *CI, IRBuilderBase &B);
0176   Value *optimizeStrTo(CallInst *CI, IRBuilderBase &B);
0177   Value *optimizeStrSpn(CallInst *CI, IRBuilderBase &B);
0178   Value *optimizeStrCSpn(CallInst *CI, IRBuilderBase &B);
0179   Value *optimizeStrStr(CallInst *CI, IRBuilderBase &B);
0180   Value *optimizeMemChr(CallInst *CI, IRBuilderBase &B);
0181   Value *optimizeMemRChr(CallInst *CI, IRBuilderBase &B);
0182   Value *optimizeMemCmp(CallInst *CI, IRBuilderBase &B);
0183   Value *optimizeBCmp(CallInst *CI, IRBuilderBase &B);
0184   Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilderBase &B);
0185   Value *optimizeMemCCpy(CallInst *CI, IRBuilderBase &B);
0186   Value *optimizeMemPCpy(CallInst *CI, IRBuilderBase &B);
0187   Value *optimizeMemCpy(CallInst *CI, IRBuilderBase &B);
0188   Value *optimizeMemMove(CallInst *CI, IRBuilderBase &B);
0189   Value *optimizeMemSet(CallInst *CI, IRBuilderBase &B);
0190   Value *optimizeRealloc(CallInst *CI, IRBuilderBase &B);
0191   Value *optimizeNew(CallInst *CI, IRBuilderBase &B, LibFunc &Func);
0192   Value *optimizeWcslen(CallInst *CI, IRBuilderBase &B);
0193   Value *optimizeBCopy(CallInst *CI, IRBuilderBase &B);
0194 
0195   // Helper to optimize stpncpy and strncpy.
0196   Value *optimizeStringNCpy(CallInst *CI, bool RetEnd, IRBuilderBase &B);
0197   // Wrapper for all String/Memory Library Call Optimizations
0198   Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilderBase &B);
0199 
0200   // Math Library Optimizations
0201   Value *optimizeCAbs(CallInst *CI, IRBuilderBase &B);
0202   Value *optimizePow(CallInst *CI, IRBuilderBase &B);
0203   Value *replacePowWithExp(CallInst *Pow, IRBuilderBase &B);
0204   Value *replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B);
0205   Value *optimizeExp2(CallInst *CI, IRBuilderBase &B);
0206   Value *optimizeFMinFMax(CallInst *CI, IRBuilderBase &B);
0207   Value *optimizeLog(CallInst *CI, IRBuilderBase &B);
0208   Value *optimizeSqrt(CallInst *CI, IRBuilderBase &B);
0209   Value *optimizeFMod(CallInst *CI, IRBuilderBase &B);
0210   Value *mergeSqrtToExp(CallInst *CI, IRBuilderBase &B);
0211   Value *optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B);
0212   Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
0213   Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
0214   Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
0215   Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
0216   // Wrapper for all floating point library call optimizations
0217   Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
0218                                       IRBuilderBase &B);
0219 
0220   // Integer Library Call Optimizations
0221   Value *optimizeFFS(CallInst *CI, IRBuilderBase &B);
0222   Value *optimizeFls(CallInst *CI, IRBuilderBase &B);
0223   Value *optimizeAbs(CallInst *CI, IRBuilderBase &B);
0224   Value *optimizeIsDigit(CallInst *CI, IRBuilderBase &B);
0225   Value *optimizeIsAscii(CallInst *CI, IRBuilderBase &B);
0226   Value *optimizeToAscii(CallInst *CI, IRBuilderBase &B);
0227   Value *optimizeAtoi(CallInst *CI, IRBuilderBase &B);
0228   Value *optimizeStrToInt(CallInst *CI, IRBuilderBase &B, bool AsSigned);
0229 
0230   // Formatting and IO Library Call Optimizations
0231   Value *optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
0232                                 int StreamArg = -1);
0233   Value *optimizePrintF(CallInst *CI, IRBuilderBase &B);
0234   Value *optimizeSPrintF(CallInst *CI, IRBuilderBase &B);
0235   Value *optimizeSnPrintF(CallInst *CI, IRBuilderBase &B);
0236   Value *optimizeFPrintF(CallInst *CI, IRBuilderBase &B);
0237   Value *optimizeFWrite(CallInst *CI, IRBuilderBase &B);
0238   Value *optimizeFPuts(CallInst *CI, IRBuilderBase &B);
0239   Value *optimizePuts(CallInst *CI, IRBuilderBase &B);
0240 
0241   // Helper methods
0242   Value* emitSnPrintfMemCpy(CallInst *CI, Value *StrArg, StringRef Str,
0243                             uint64_t N, IRBuilderBase &B);
0244   Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
0245                           IRBuilderBase &B);
0246   void classifyArgUse(Value *Val, Function *F, bool IsFloat,
0247                       SmallVectorImpl<CallInst *> &SinCalls,
0248                       SmallVectorImpl<CallInst *> &CosCalls,
0249                       SmallVectorImpl<CallInst *> &SinCosCalls);
0250   Value *optimizePrintFString(CallInst *CI, IRBuilderBase &B);
0251   Value *optimizeSPrintFString(CallInst *CI, IRBuilderBase &B);
0252   Value *optimizeSnPrintFString(CallInst *CI, IRBuilderBase &B);
0253   Value *optimizeFPrintFString(CallInst *CI, IRBuilderBase &B);
0254 
0255   /// Exit functions
0256   Value *optimizeExit(CallInst *CI);
0257 
0258   /// hasFloatVersion - Checks if there is a float version of the specified
0259   /// function by checking for an existing function with name FuncName + f
0260   bool hasFloatVersion(const Module *M, StringRef FuncName);
0261 
0262   /// Shared code to optimize strlen+wcslen and strnlen+wcsnlen.
0263   Value *optimizeStringLength(CallInst *CI, IRBuilderBase &B, unsigned CharSize,
0264                               Value *Bound = nullptr);
0265 };
0266 } // End llvm namespace
0267 
0268 #endif