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 
64  /// Checks whether the call \p CI to a fortified libcall is foldable
65  /// to the non-fortified version.
66  bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
67  unsigned SizeOp, bool isString);
68 };
69 
70 /// LibCallSimplifier - This class implements a collection of optimizations
71 /// that replace well formed calls to library functions with a more optimal
72 /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
74 private:
75  FortifiedLibCallSimplifier FortifiedSimplifier;
76  const DataLayout &DL;
77  const TargetLibraryInfo *TLI;
80  ProfileSummaryInfo *PSI;
81  bool UnsafeFPShrink;
84 
85  /// Internal wrapper for RAUW that is the default implementation.
86  ///
87  /// Other users may provide an alternate function with this signature instead
88  /// of this one.
89  static void replaceAllUsesWithDefault(Instruction *I, Value *With) {
90  I->replaceAllUsesWith(With);
91  }
92 
93  /// Internal wrapper for eraseFromParent that is the default implementation.
94  static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); }
95 
96  /// Replace an instruction's uses with a value using our replacer.
97  void replaceAllUsesWith(Instruction *I, Value *With);
98 
99  /// Erase an instruction from its parent with our eraser.
100  void eraseFromParent(Instruction *I);
101 
102  Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B);
103 
104 public:
106  const DataLayout &DL, const TargetLibraryInfo *TLI,
109  function_ref<void(Instruction *, Value *)> Replacer =
110  &replaceAllUsesWithDefault,
111  function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault);
112 
113  /// optimizeCall - Take the given call instruction and return a more
114  /// optimal value to replace the instruction with or 0 if a more
115  /// optimal form can't be found. Note that the returned value may
116  /// be equal to the instruction being optimized. In this case all
117  /// other instructions that use the given instruction were modified
118  /// and the given instruction is dead.
119  /// The call must not be an indirect call.
121 
122 private:
123  // String and Memory Library Call Optimizations
124  Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
125  Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
126  Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
127  Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
128  Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
129  Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
130  Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
131  Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
132  Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
133  Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
134  Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
135  Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
136  Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
137  Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
138  Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
139  Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B);
140  Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
141  Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
142  Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
143  Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
144  Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B);
145  Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B);
146  // Wrapper for all String/Memory Library Call Optimizations
147  Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
148 
149  // Math Library Optimizations
150  Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B);
151  Value *optimizePow(CallInst *CI, IRBuilder<> &B);
152  Value *replacePowWithExp(CallInst *Pow, IRBuilder<> &B);
153  Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B);
154  Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
155  Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B);
156  Value *optimizeLog(CallInst *CI, IRBuilder<> &B);
157  Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
158  Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
159  Value *optimizeTan(CallInst *CI, IRBuilder<> &B);
160  // Wrapper for all floating point library call optimizations
161  Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
162  IRBuilder<> &B);
163 
164  // Integer Library Call Optimizations
165  Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
166  Value *optimizeFls(CallInst *CI, IRBuilder<> &B);
167  Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
168  Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
169  Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
170  Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
171  Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B);
172  Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B);
173 
174  // Formatting and IO Library Call Optimizations
175  Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
176  int StreamArg = -1);
177  Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
178  Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
179  Value *optimizeSnPrintF(CallInst *CI, IRBuilder<> &B);
180  Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
181  Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
182  Value *optimizeFRead(CallInst *CI, IRBuilder<> &B);
183  Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
184  Value *optimizeFGets(CallInst *CI, IRBuilder<> &B);
185  Value *optimizeFPutc(CallInst *CI, IRBuilder<> &B);
186  Value *optimizeFGetc(CallInst *CI, IRBuilder<> &B);
187  Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
188 
189  // Helper methods
190  Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
191  void classifyArgUse(Value *Val, Function *F, bool IsFloat,
192  SmallVectorImpl<CallInst *> &SinCalls,
193  SmallVectorImpl<CallInst *> &CosCalls,
194  SmallVectorImpl<CallInst *> &SinCosCalls);
195  Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
196  Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
197  Value *optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B);
198  Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
199 
200  /// hasFloatVersion - Checks if there is a float version of the specified
201  /// function by checking for an existing function with name FuncName + f
202  bool hasFloatVersion(StringRef FuncName);
203 
204  /// Shared code to optimize strlen+wcslen.
205  Value *optimizeStringLength(CallInst *CI, IRBuilder<> &B, unsigned CharSize);
206 };
207 } // End llvm namespace
208 
209 #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.