LLVM  9.0.0svn
SimplifyLibCalls.h
Go to the documentation of this file.
1 //===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file exposes an interface to build some C language libcalls for
10 // optimization passes that need to call the various functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
15 #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
16 
17 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/IR/IRBuilder.h"
20 
21 namespace llvm {
22 class StringRef;
23 class Value;
24 class CallInst;
25 class DataLayout;
26 class Instruction;
27 class TargetLibraryInfo;
28 class BasicBlock;
29 class Function;
30 class OptimizationRemarkEmitter;
31 class BlockFrequencyInfo;
32 class ProfileSummaryInfo;
33 
34 /// This class implements simplifications for calls to fortified library
35 /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
36 /// when possible, replace them with their non-checking counterparts.
37 /// Other optimizations can also be done, but it's possible to disable them and
38 /// only simplify needless use of the checking versions (when the object size
39 /// is unknown) by passing true for OnlyLowerUnknownSize.
41 private:
42  const TargetLibraryInfo *TLI;
43  bool OnlyLowerUnknownSize;
44 
45 public:
47  bool OnlyLowerUnknownSize = false);
48 
49  /// Take the given call instruction and return a more
50  /// optimal value to replace the instruction with or 0 if a more
51  /// optimal form can't be found.
52  /// The call must not be an indirect call.
54 
55 private:
56  Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
57  Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
58  Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
59 
60  /// Str/Stp cpy are similar enough to be handled in the same functions.
61  Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func);
62  Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func);
63  Value *optimizeMemCCpyChk(CallInst *CI, IRBuilder<> &B);
64  Value *optimizeSNPrintfChk(CallInst *CI, IRBuilder<> &B);
65  Value *optimizeSPrintfChk(CallInst *CI,IRBuilder<> &B);
66  Value *optimizeStrCatChk(CallInst *CI, IRBuilder<> &B);
67  Value *optimizeStrLCat(CallInst *CI, IRBuilder<> &B);
68  Value *optimizeStrNCatChk(CallInst *CI, IRBuilder<> &B);
69  Value *optimizeStrLCpyChk(CallInst *CI, IRBuilder<> &B);
70  Value *optimizeVSNPrintfChk(CallInst *CI, IRBuilder<> &B);
71  Value *optimizeVSPrintfChk(CallInst *CI, IRBuilder<> &B);
72 
73  /// Checks whether the call \p CI to a fortified libcall is foldable
74  /// to the non-fortified version.
75  ///
76  /// \param CI the call to the fortified libcall.
77  ///
78  /// \param ObjSizeOp the index of the object size parameter of this chk
79  /// function. Not optional since this is mandatory.
80  ///
81  /// \param SizeOp optionally set to the parameter index of an explicit buffer
82  /// size argument. For instance, set to '2' for __strncpy_chk.
83  ///
84  /// \param StrOp optionally set to the parameter index of the source string
85  /// parameter to strcpy-like functions, where only the strlen of the source
86  /// will be writtin into the destination.
87  ///
88  /// \param FlagsOp optionally set to the parameter index of a 'flags'
89  /// parameter. These are used by an implementation to opt-into stricter
90  /// checking.
91  bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
92  Optional<unsigned> SizeOp = None,
93  Optional<unsigned> StrOp = None,
95 };
96 
97 /// LibCallSimplifier - This class implements a collection of optimizations
98 /// that replace well formed calls to library functions with a more optimal
99 /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
101 private:
102  FortifiedLibCallSimplifier FortifiedSimplifier;
103  const DataLayout &DL;
104  const TargetLibraryInfo *TLI;
107  ProfileSummaryInfo *PSI;
108  bool UnsafeFPShrink;
111 
112  /// Internal wrapper for RAUW that is the default implementation.
113  ///
114  /// Other users may provide an alternate function with this signature instead
115  /// of this one.
116  static void replaceAllUsesWithDefault(Instruction *I, Value *With) {
117  I->replaceAllUsesWith(With);
118  }
119 
120  /// Internal wrapper for eraseFromParent that is the default implementation.
121  static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); }
122 
123  /// Replace an instruction's uses with a value using our replacer.
124  void replaceAllUsesWith(Instruction *I, Value *With);
125 
126  /// Erase an instruction from its parent with our eraser.
127  void eraseFromParent(Instruction *I);
128 
129  Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B);
130 
131 public:
133  const DataLayout &DL, const TargetLibraryInfo *TLI,
136  function_ref<void(Instruction *, Value *)> Replacer =
137  &replaceAllUsesWithDefault,
138  function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault);
139 
140  /// optimizeCall - Take the given call instruction and return a more
141  /// optimal value to replace the instruction with or 0 if a more
142  /// optimal form can't be found. Note that the returned value may
143  /// be equal to the instruction being optimized. In this case all
144  /// other instructions that use the given instruction were modified
145  /// and the given instruction is dead.
146  /// The call must not be an indirect call.
148 
149 private:
150  // String and Memory Library Call Optimizations
151  Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
152  Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
153  Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
154  Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
155  Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
156  Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
157  Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
158  Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
159  Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
160  Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
161  Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
162  Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
163  Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
164  Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
165  Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
166  Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B);
167  Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
168  Value *optimizeBCmp(CallInst *CI, IRBuilder<> &B);
169  Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilder<> &B);
170  Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
171  Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
172  Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
173  Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B);
174  Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B);
175  // Wrapper for all String/Memory Library Call Optimizations
176  Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
177 
178  // Math Library Optimizations
179  Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B);
180  Value *optimizePow(CallInst *CI, IRBuilder<> &B);
181  Value *replacePowWithExp(CallInst *Pow, IRBuilder<> &B);
182  Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B);
183  Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
184  Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B);
185  Value *optimizeLog(CallInst *CI, IRBuilder<> &B);
186  Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
187  Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
188  Value *optimizeTan(CallInst *CI, IRBuilder<> &B);
189  // Wrapper for all floating point library call optimizations
190  Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
191  IRBuilder<> &B);
192 
193  // Integer Library Call Optimizations
194  Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
195  Value *optimizeFls(CallInst *CI, IRBuilder<> &B);
196  Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
197  Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
198  Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
199  Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
200  Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B);
201  Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B);
202 
203  // Formatting and IO Library Call Optimizations
204  Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
205  int StreamArg = -1);
206  Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
207  Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
208  Value *optimizeSnPrintF(CallInst *CI, IRBuilder<> &B);
209  Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
210  Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
211  Value *optimizeFRead(CallInst *CI, IRBuilder<> &B);
212  Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
213  Value *optimizeFGets(CallInst *CI, IRBuilder<> &B);
214  Value *optimizeFPutc(CallInst *CI, IRBuilder<> &B);
215  Value *optimizeFGetc(CallInst *CI, IRBuilder<> &B);
216  Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
217 
218  // Helper methods
219  Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
220  void classifyArgUse(Value *Val, Function *F, bool IsFloat,
221  SmallVectorImpl<CallInst *> &SinCalls,
222  SmallVectorImpl<CallInst *> &CosCalls,
223  SmallVectorImpl<CallInst *> &SinCosCalls);
224  Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
225  Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
226  Value *optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B);
227  Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
228 
229  /// hasFloatVersion - Checks if there is a float version of the specified
230  /// function by checking for an existing function with name FuncName + f
231  bool hasFloatVersion(StringRef FuncName);
232 
233  /// Shared code to optimize strlen+wcslen.
234  Value *optimizeStringLength(CallInst *CI, IRBuilder<> &B, unsigned CharSize);
235 };
236 } // End llvm namespace
237 
238 #endif
LibCallSimplifier - This class implements a collection of optimizations that replace well formed call...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
Value * optimizeCall(CallInst *CI)
Take the given call instruction and return a more optimal value to replace the instruction with or 0 ...
Analysis providing profile information.
This class represents a function call, abstracting a target machine&#39;s calling convention.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
F(f)
FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize=false)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
This class implements simplifications for calls to fortified library functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to, when possible, replace them with their non-checking counterparts.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides information about what library functions are available for the current target.
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM Value Representation.
Definition: Value.h:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
The optimization diagnostic interface.