1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
18 #include "llvm/DataLayout.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/Function.h"
22 #include "llvm/IRBuilder.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Target/TargetLibraryInfo.h"
25 #include "llvm/Transforms/Utils/BuildLibCalls.h"
29 /// This class is the abstract base class for the set of optimizations that
30 /// corresponds to one library call.
32 class LibCallOptimization {
36 const TargetLibraryInfo *TLI;
39 LibCallOptimization() { }
40 virtual ~LibCallOptimization() {}
42 /// callOptimizer - This pure virtual method is implemented by base classes to
43 /// do various optimizations. If this returns null then no transformation was
44 /// performed. If it returns CI, then it transformed the call and CI is to be
45 /// deleted. If it returns something else, replace CI with the new value and
47 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
50 Value *optimizeCall(CallInst *CI, const DataLayout *TD,
51 const TargetLibraryInfo *TLI, IRBuilder<> &B) {
52 Caller = CI->getParent()->getParent();
55 if (CI->getCalledFunction())
56 Context = &CI->getCalledFunction()->getContext();
58 // We never change the calling convention.
59 if (CI->getCallingConv() != llvm::CallingConv::C)
62 return callOptimizer(CI->getCalledFunction(), CI, B);
66 //===----------------------------------------------------------------------===//
67 // Fortified Library Call Optimizations
68 //===----------------------------------------------------------------------===//
70 struct FortifiedLibCallOptimization : public LibCallOptimization {
72 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
73 bool isString) const = 0;
76 struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
79 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
80 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
82 if (ConstantInt *SizeCI =
83 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
84 if (SizeCI->isAllOnesValue())
87 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
88 // If the length is 0 we don't know how long it is and so we can't
90 if (Len == 0) return false;
91 return SizeCI->getZExtValue() >= Len;
93 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
94 CI->getArgOperand(SizeArgOp)))
95 return SizeCI->getZExtValue() >= Arg->getZExtValue();
101 struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
102 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
104 FunctionType *FT = Callee->getFunctionType();
105 LLVMContext &Context = CI->getParent()->getContext();
107 // Check if this has the right signature.
108 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
109 !FT->getParamType(0)->isPointerTy() ||
110 !FT->getParamType(1)->isPointerTy() ||
111 FT->getParamType(2) != TD->getIntPtrType(Context) ||
112 FT->getParamType(3) != TD->getIntPtrType(Context))
115 if (isFoldable(3, 2, false)) {
116 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
117 CI->getArgOperand(2), 1);
118 return CI->getArgOperand(0);
124 struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
125 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
127 FunctionType *FT = Callee->getFunctionType();
128 LLVMContext &Context = CI->getParent()->getContext();
130 // Check if this has the right signature.
131 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
132 !FT->getParamType(0)->isPointerTy() ||
133 !FT->getParamType(1)->isPointerTy() ||
134 FT->getParamType(2) != TD->getIntPtrType(Context) ||
135 FT->getParamType(3) != TD->getIntPtrType(Context))
138 if (isFoldable(3, 2, false)) {
139 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
140 CI->getArgOperand(2), 1);
141 return CI->getArgOperand(0);
147 struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
148 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
150 FunctionType *FT = Callee->getFunctionType();
151 LLVMContext &Context = CI->getParent()->getContext();
153 // Check if this has the right signature.
154 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
155 !FT->getParamType(0)->isPointerTy() ||
156 !FT->getParamType(1)->isIntegerTy() ||
157 FT->getParamType(2) != TD->getIntPtrType(Context) ||
158 FT->getParamType(3) != TD->getIntPtrType(Context))
161 if (isFoldable(3, 2, false)) {
162 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
164 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
165 return CI->getArgOperand(0);
171 struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
172 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
174 StringRef Name = Callee->getName();
175 FunctionType *FT = Callee->getFunctionType();
176 LLVMContext &Context = CI->getParent()->getContext();
178 // Check if this has the right signature.
179 if (FT->getNumParams() != 3 ||
180 FT->getReturnType() != FT->getParamType(0) ||
181 FT->getParamType(0) != FT->getParamType(1) ||
182 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
183 FT->getParamType(2) != TD->getIntPtrType(Context))
186 // If a) we don't have any length information, or b) we know this will
187 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
188 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
189 // TODO: It might be nice to get a maximum length out of the possible
190 // string lengths for varying.
191 if (isFoldable(2, 1, true)) {
192 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
193 TLI, Name.substr(2, 6));
200 struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
201 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
203 StringRef Name = Callee->getName();
204 FunctionType *FT = Callee->getFunctionType();
205 LLVMContext &Context = CI->getParent()->getContext();
207 // Check if this has the right signature.
208 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
209 FT->getParamType(0) != FT->getParamType(1) ||
210 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
211 !FT->getParamType(2)->isIntegerTy() ||
212 FT->getParamType(3) != TD->getIntPtrType(Context))
215 if (isFoldable(3, 2, false)) {
216 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
217 CI->getArgOperand(2), B, TD, TLI,
225 //===----------------------------------------------------------------------===//
226 // String and Memory Library Call Optimizations
227 //===----------------------------------------------------------------------===//
229 struct StrCatOpt : public LibCallOptimization {
230 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
231 // Verify the "strcat" function prototype.
232 FunctionType *FT = Callee->getFunctionType();
233 if (FT->getNumParams() != 2 ||
234 FT->getReturnType() != B.getInt8PtrTy() ||
235 FT->getParamType(0) != FT->getReturnType() ||
236 FT->getParamType(1) != FT->getReturnType())
239 // Extract some information from the instruction
240 Value *Dst = CI->getArgOperand(0);
241 Value *Src = CI->getArgOperand(1);
243 // See if we can get the length of the input string.
244 uint64_t Len = GetStringLength(Src);
245 if (Len == 0) return 0;
246 --Len; // Unbias length.
248 // Handle the simple, do-nothing case: strcat(x, "") -> x
252 // These optimizations require DataLayout.
255 return emitStrLenMemCpy(Src, Dst, Len, B);
258 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
260 // We need to find the end of the destination string. That's where the
261 // memory is to be moved to. We just generate a call to strlen.
262 Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
266 // Now that we have the destination's length, we must index into the
267 // destination's pointer to get the actual memcpy destination (end of
268 // the string .. we're concatenating).
269 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
271 // We have enough information to now generate the memcpy call to do the
272 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
273 B.CreateMemCpy(CpyDst, Src,
274 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
279 struct StrNCatOpt : public StrCatOpt {
280 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
281 // Verify the "strncat" function prototype.
282 FunctionType *FT = Callee->getFunctionType();
283 if (FT->getNumParams() != 3 ||
284 FT->getReturnType() != B.getInt8PtrTy() ||
285 FT->getParamType(0) != FT->getReturnType() ||
286 FT->getParamType(1) != FT->getReturnType() ||
287 !FT->getParamType(2)->isIntegerTy())
290 // Extract some information from the instruction
291 Value *Dst = CI->getArgOperand(0);
292 Value *Src = CI->getArgOperand(1);
295 // We don't do anything if length is not constant
296 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
297 Len = LengthArg->getZExtValue();
301 // See if we can get the length of the input string.
302 uint64_t SrcLen = GetStringLength(Src);
303 if (SrcLen == 0) return 0;
304 --SrcLen; // Unbias length.
306 // Handle the simple, do-nothing cases:
307 // strncat(x, "", c) -> x
308 // strncat(x, c, 0) -> x
309 if (SrcLen == 0 || Len == 0) return Dst;
311 // These optimizations require DataLayout.
314 // We don't optimize this case
315 if (Len < SrcLen) return 0;
317 // strncat(x, s, c) -> strcat(x, s)
318 // s is constant so the strcat can be optimized further
319 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
323 } // End anonymous namespace.
327 class LibCallSimplifierImpl {
328 LibCallSimplifier *Simplifier;
329 const DataLayout *TD;
330 const TargetLibraryInfo *TLI;
331 StringMap<LibCallOptimization*> Optimizations;
333 // Fortified library call optimizations.
334 MemCpyChkOpt MemCpyChk;
335 MemMoveChkOpt MemMoveChk;
336 MemSetChkOpt MemSetChk;
337 StrCpyChkOpt StrCpyChk;
338 StrNCpyChkOpt StrNCpyChk;
340 // String and memory library call optimizations.
344 void initOptimizations();
346 LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI) {
351 Value *optimizeCall(CallInst *CI);
354 void LibCallSimplifierImpl::initOptimizations() {
355 // Fortified library call optimizations.
356 Optimizations["__memcpy_chk"] = &MemCpyChk;
357 Optimizations["__memmove_chk"] = &MemMoveChk;
358 Optimizations["__memset_chk"] = &MemSetChk;
359 Optimizations["__strcpy_chk"] = &StrCpyChk;
360 Optimizations["__stpcpy_chk"] = &StrCpyChk;
361 Optimizations["__strncpy_chk"] = &StrNCpyChk;
362 Optimizations["__stpncpy_chk"] = &StrNCpyChk;
364 // String and memory library call optimizations.
365 Optimizations["strcat"] = &StrCat;
366 Optimizations["strncat"] = &StrNCat;
369 Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
370 if (Optimizations.empty())
373 Function *Callee = CI->getCalledFunction();
374 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
376 IRBuilder<> Builder(CI);
377 return LCO->optimizeCall(CI, TD, TLI, Builder);
382 LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
383 const TargetLibraryInfo *TLI) {
384 Impl = new LibCallSimplifierImpl(TD, TLI);
387 LibCallSimplifier::~LibCallSimplifier() {
391 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
392 return Impl->optimizeCall(CI);