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