Coverage Report

Created: 2024-10-22 16:19

/home/runner/work/DirectXShaderCompiler/DirectXShaderCompiler/lib/Transforms/Utils/SimplifyLibCalls.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
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.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/StringMap.h"
20
#include "llvm/ADT/Triple.h"
21
#include "llvm/Analysis/ValueTracking.h"
22
#include "llvm/IR/DataLayout.h"
23
#include "llvm/IR/DiagnosticInfo.h"
24
#include "llvm/IR/Function.h"
25
#include "llvm/IR/IRBuilder.h"
26
#include "llvm/IR/IntrinsicInst.h"
27
#include "llvm/IR/Intrinsics.h"
28
#include "llvm/IR/LLVMContext.h"
29
#include "llvm/IR/Module.h"
30
#include "llvm/IR/PatternMatch.h"
31
#include "llvm/Support/Allocator.h"
32
#include "llvm/Support/CommandLine.h"
33
#include "llvm/Analysis/TargetLibraryInfo.h"
34
#include "llvm/Transforms/Utils/BuildLibCalls.h"
35
36
using namespace llvm;
37
using namespace PatternMatch;
38
39
#if 0 // HLSL Change Starts - option pending
40
static cl::opt<bool>
41
    ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden,
42
                   cl::desc("Treat error-reporting calls as cold"));
43
44
static cl::opt<bool>
45
    EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
46
                         cl::init(false),
47
                         cl::desc("Enable unsafe double to float "
48
                                  "shrinking for math lib calls"));
49
#else
50
static const bool ColdErrorCalls = true;
51
static const bool EnableUnsafeFPShrink = false;
52
#endif // HLSL Change Ends
53
54
//===----------------------------------------------------------------------===//
55
// Helper Functions
56
//===----------------------------------------------------------------------===//
57
58
0
static bool ignoreCallingConv(LibFunc::Func Func) {
59
0
  switch (Func) {
60
0
  case LibFunc::abs:
61
0
  case LibFunc::labs:
62
0
  case LibFunc::llabs:
63
0
  case LibFunc::strlen:
64
0
    return true;
65
0
  default:
66
0
    return false;
67
0
  }
68
0
  llvm_unreachable("All cases should be covered in the switch.");
69
0
}
70
71
/// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
72
/// value is equal or not-equal to zero.
73
0
static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
74
0
  for (User *U : V->users()) {
75
0
    if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
76
0
      if (IC->isEquality())
77
0
        if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
78
0
          if (C->isNullValue())
79
0
            continue;
80
    // Unknown instruction.
81
0
    return false;
82
0
  }
83
0
  return true;
84
0
}
85
86
/// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
87
/// comparisons with With.
88
0
static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
89
0
  for (User *U : V->users()) {
90
0
    if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
91
0
      if (IC->isEquality() && IC->getOperand(1) == With)
92
0
        continue;
93
    // Unknown instruction.
94
0
    return false;
95
0
  }
96
0
  return true;
97
0
}
98
99
0
static bool callHasFloatingPointArgument(const CallInst *CI) {
100
0
  for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
101
0
       it != e; ++it) {
102
0
    if ((*it)->getType()->isFloatingPointTy())
103
0
      return true;
104
0
  }
105
0
  return false;
106
0
}
107
108
/// \brief Check whether the overloaded unary floating point function
109
/// corresponing to \a Ty is available.
110
static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
111
                            LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
112
0
                            LibFunc::Func LongDoubleFn) {
113
0
  switch (Ty->getTypeID()) {
114
0
  case Type::FloatTyID:
115
0
    return TLI->has(FloatFn);
116
0
  case Type::DoubleTyID:
117
0
    return TLI->has(DoubleFn);
118
0
  default:
119
0
    return TLI->has(LongDoubleFn);
120
0
  }
121
0
}
122
123
/// \brief Returns whether \p F matches the signature expected for the
124
/// string/memory copying library function \p Func.
125
/// Acceptable functions are st[rp][n]?cpy, memove, memcpy, and memset.
126
/// Their fortified (_chk) counterparts are also accepted.
127
0
static bool checkStringCopyLibFuncSignature(Function *F, LibFunc::Func Func) {
128
0
  const DataLayout &DL = F->getParent()->getDataLayout();
129
0
  FunctionType *FT = F->getFunctionType();
130
0
  LLVMContext &Context = F->getContext();
131
0
  Type *PCharTy = Type::getInt8PtrTy(Context);
132
0
  Type *SizeTTy = DL.getIntPtrType(Context);
133
0
  unsigned NumParams = FT->getNumParams();
134
135
  // All string libfuncs return the same type as the first parameter.
136
0
  if (FT->getReturnType() != FT->getParamType(0))
137
0
    return false;
138
139
0
  switch (Func) {
140
0
  default:
141
0
    llvm_unreachable("Can't check signature for non-string-copy libfunc.");
142
0
  case LibFunc::stpncpy_chk:
143
0
  case LibFunc::strncpy_chk:
144
0
    --NumParams; LLVM_FALLTHROUGH; // HLSL Change
145
0
  case LibFunc::stpncpy:
146
0
  case LibFunc::strncpy: {
147
0
    if (NumParams != 3 || FT->getParamType(0) != FT->getParamType(1) ||
148
0
        FT->getParamType(0) != PCharTy || !FT->getParamType(2)->isIntegerTy())
149
0
      return false;
150
0
    break;
151
0
  }
152
0
  case LibFunc::strcpy_chk:
153
0
  case LibFunc::stpcpy_chk:
154
0
    --NumParams; LLVM_FALLTHROUGH; // HLSL Change
155
0
  case LibFunc::stpcpy:
156
0
  case LibFunc::strcpy: {
157
0
    if (NumParams != 2 || FT->getParamType(0) != FT->getParamType(1) ||
158
0
        FT->getParamType(0) != PCharTy)
159
0
      return false;
160
0
    break;
161
0
  }
162
0
  case LibFunc::memmove_chk:
163
0
  case LibFunc::memcpy_chk:
164
0
    --NumParams; LLVM_FALLTHROUGH; // HLSL Change
165
0
  case LibFunc::memmove:
166
0
  case LibFunc::memcpy: {
167
0
    if (NumParams != 3 || !FT->getParamType(0)->isPointerTy() ||
168
0
        !FT->getParamType(1)->isPointerTy() || FT->getParamType(2) != SizeTTy)
169
0
      return false;
170
0
    break;
171
0
  }
172
0
  case LibFunc::memset_chk:
173
0
    --NumParams; LLVM_FALLTHROUGH; // HLSL Change
174
0
  case LibFunc::memset: {
175
0
    if (NumParams != 3 || !FT->getParamType(0)->isPointerTy() ||
176
0
        !FT->getParamType(1)->isIntegerTy() || FT->getParamType(2) != SizeTTy)
177
0
      return false;
178
0
    break;
179
0
  }
180
0
  }
181
  // If this is a fortified libcall, the last parameter is a size_t.
182
0
  if (NumParams == FT->getNumParams() - 1)
183
0
    return FT->getParamType(FT->getNumParams() - 1) == SizeTTy;
184
0
  return true;
185
0
}
186
187
//===----------------------------------------------------------------------===//
188
// String and Memory Library Call Optimizations
189
//===----------------------------------------------------------------------===//
190
191
0
Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {
192
0
  Function *Callee = CI->getCalledFunction();
193
  // Verify the "strcat" function prototype.
194
0
  FunctionType *FT = Callee->getFunctionType();
195
0
  if (FT->getNumParams() != 2||
196
0
      FT->getReturnType() != B.getInt8PtrTy() ||
197
0
      FT->getParamType(0) != FT->getReturnType() ||
198
0
      FT->getParamType(1) != FT->getReturnType())
199
0
    return nullptr;
200
201
  // Extract some information from the instruction
202
0
  Value *Dst = CI->getArgOperand(0);
203
0
  Value *Src = CI->getArgOperand(1);
204
205
  // See if we can get the length of the input string.
206
0
  uint64_t Len = GetStringLength(Src);
207
0
  if (Len == 0)
208
0
    return nullptr;
209
0
  --Len; // Unbias length.
210
211
  // Handle the simple, do-nothing case: strcat(x, "") -> x
212
0
  if (Len == 0)
213
0
    return Dst;
214
215
0
  return emitStrLenMemCpy(Src, Dst, Len, B);
216
0
}
217
218
Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
219
0
                                           IRBuilder<> &B) {
220
  // We need to find the end of the destination string.  That's where the
221
  // memory is to be moved to. We just generate a call to strlen.
222
0
  Value *DstLen = EmitStrLen(Dst, B, DL, TLI);
223
0
  if (!DstLen)
224
0
    return nullptr;
225
226
  // Now that we have the destination's length, we must index into the
227
  // destination's pointer to get the actual memcpy destination (end of
228
  // the string .. we're concatenating).
229
0
  Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
230
231
  // We have enough information to now generate the memcpy call to do the
232
  // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
233
0
  B.CreateMemCpy(CpyDst, Src,
234
0
                 ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1),
235
0
                 1);
236
0
  return Dst;
237
0
}
238
239
0
Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {
240
0
  Function *Callee = CI->getCalledFunction();
241
  // Verify the "strncat" function prototype.
242
0
  FunctionType *FT = Callee->getFunctionType();
243
0
  if (FT->getNumParams() != 3 || FT->getReturnType() != B.getInt8PtrTy() ||
244
0
      FT->getParamType(0) != FT->getReturnType() ||
245
0
      FT->getParamType(1) != FT->getReturnType() ||
246
0
      !FT->getParamType(2)->isIntegerTy())
247
0
    return nullptr;
248
249
  // Extract some information from the instruction
250
0
  Value *Dst = CI->getArgOperand(0);
251
0
  Value *Src = CI->getArgOperand(1);
252
0
  uint64_t Len;
253
254
  // We don't do anything if length is not constant
255
0
  if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
256
0
    Len = LengthArg->getZExtValue();
257
0
  else
258
0
    return nullptr;
259
260
  // See if we can get the length of the input string.
261
0
  uint64_t SrcLen = GetStringLength(Src);
262
0
  if (SrcLen == 0)
263
0
    return nullptr;
264
0
  --SrcLen; // Unbias length.
265
266
  // Handle the simple, do-nothing cases:
267
  // strncat(x, "", c) -> x
268
  // strncat(x,  c, 0) -> x
269
0
  if (SrcLen == 0 || Len == 0)
270
0
    return Dst;
271
272
  // We don't optimize this case
273
0
  if (Len < SrcLen)
274
0
    return nullptr;
275
276
  // strncat(x, s, c) -> strcat(x, s)
277
  // s is constant so the strcat can be optimized further
278
0
  return emitStrLenMemCpy(Src, Dst, SrcLen, B);
279
0
}
280
281
0
Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
282
0
  Function *Callee = CI->getCalledFunction();
283
  // Verify the "strchr" function prototype.
284
0
  FunctionType *FT = Callee->getFunctionType();
285
0
  if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
286
0
      FT->getParamType(0) != FT->getReturnType() ||
287
0
      !FT->getParamType(1)->isIntegerTy(32))
288
0
    return nullptr;
289
290
0
  Value *SrcStr = CI->getArgOperand(0);
291
292
  // If the second operand is non-constant, see if we can compute the length
293
  // of the input string and turn this into memchr.
294
0
  ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
295
0
  if (!CharC) {
296
0
    uint64_t Len = GetStringLength(SrcStr);
297
0
    if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
298
0
      return nullptr;
299
300
0
    return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
301
0
                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len),
302
0
                      B, DL, TLI);
303
0
  }
304
305
  // Otherwise, the character is a constant, see if the first argument is
306
  // a string literal.  If so, we can constant fold.
307
0
  StringRef Str;
308
0
  if (!getConstantStringInfo(SrcStr, Str)) {
309
0
    if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
310
0
      return B.CreateGEP(B.getInt8Ty(), SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");
311
0
    return nullptr;
312
0
  }
313
314
  // Compute the offset, make sure to handle the case when we're searching for
315
  // zero (a weird way to spell strlen).
316
0
  size_t I = (0xFF & CharC->getSExtValue()) == 0
317
0
                 ? Str.size()
318
0
                 : Str.find(CharC->getSExtValue());
319
0
  if (I == StringRef::npos) // Didn't find the char.  strchr returns null.
320
0
    return Constant::getNullValue(CI->getType());
321
322
  // strchr(s+n,c)  -> gep(s+n+i,c)
323
0
  return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
324
0
}
325
326
0
Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
327
0
  Function *Callee = CI->getCalledFunction();
328
  // Verify the "strrchr" function prototype.
329
0
  FunctionType *FT = Callee->getFunctionType();
330
0
  if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
331
0
      FT->getParamType(0) != FT->getReturnType() ||
332
0
      !FT->getParamType(1)->isIntegerTy(32))
333
0
    return nullptr;
334
335
0
  Value *SrcStr = CI->getArgOperand(0);
336
0
  ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
337
338
  // Cannot fold anything if we're not looking for a constant.
339
0
  if (!CharC)
340
0
    return nullptr;
341
342
0
  StringRef Str;
343
0
  if (!getConstantStringInfo(SrcStr, Str)) {
344
    // strrchr(s, 0) -> strchr(s, 0)
345
0
    if (CharC->isZero())
346
0
      return EmitStrChr(SrcStr, '\0', B, TLI);
347
0
    return nullptr;
348
0
  }
349
350
  // Compute the offset.
351
0
  size_t I = (0xFF & CharC->getSExtValue()) == 0
352
0
                 ? Str.size()
353
0
                 : Str.rfind(CharC->getSExtValue());
354
0
  if (I == StringRef::npos) // Didn't find the char. Return null.
355
0
    return Constant::getNullValue(CI->getType());
356
357
  // strrchr(s+n,c) -> gep(s+n+i,c)
358
0
  return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr");
359
0
}
360
361
0
Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
362
0
  Function *Callee = CI->getCalledFunction();
363
  // Verify the "strcmp" function prototype.
364
0
  FunctionType *FT = Callee->getFunctionType();
365
0
  if (FT->getNumParams() != 2 || !FT->getReturnType()->isIntegerTy(32) ||
366
0
      FT->getParamType(0) != FT->getParamType(1) ||
367
0
      FT->getParamType(0) != B.getInt8PtrTy())
368
0
    return nullptr;
369
370
0
  Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
371
0
  if (Str1P == Str2P) // strcmp(x,x)  -> 0
372
0
    return ConstantInt::get(CI->getType(), 0);
373
374
0
  StringRef Str1, Str2;
375
0
  bool HasStr1 = getConstantStringInfo(Str1P, Str1);
376
0
  bool HasStr2 = getConstantStringInfo(Str2P, Str2);
377
378
  // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
379
0
  if (HasStr1 && HasStr2)
380
0
    return ConstantInt::get(CI->getType(), Str1.compare(Str2));
381
382
0
  if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
383
0
    return B.CreateNeg(
384
0
        B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
385
386
0
  if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
387
0
    return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
388
389
  // strcmp(P, "x") -> memcmp(P, "x", 2)
390
0
  uint64_t Len1 = GetStringLength(Str1P);
391
0
  uint64_t Len2 = GetStringLength(Str2P);
392
0
  if (Len1 && Len2) {
393
0
    return EmitMemCmp(Str1P, Str2P,
394
0
                      ConstantInt::get(DL.getIntPtrType(CI->getContext()),
395
0
                                       std::min(Len1, Len2)),
396
0
                      B, DL, TLI);
397
0
  }
398
399
0
  return nullptr;
400
0
}
401
402
0
Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
403
0
  Function *Callee = CI->getCalledFunction();
404
  // Verify the "strncmp" function prototype.
405
0
  FunctionType *FT = Callee->getFunctionType();
406
0
  if (FT->getNumParams() != 3 || !FT->getReturnType()->isIntegerTy(32) ||
407
0
      FT->getParamType(0) != FT->getParamType(1) ||
408
0
      FT->getParamType(0) != B.getInt8PtrTy() ||
409
0
      !FT->getParamType(2)->isIntegerTy())
410
0
    return nullptr;
411
412
0
  Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
413
0
  if (Str1P == Str2P) // strncmp(x,x,n)  -> 0
414
0
    return ConstantInt::get(CI->getType(), 0);
415
416
  // Get the length argument if it is constant.
417
0
  uint64_t Length;
418
0
  if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
419
0
    Length = LengthArg->getZExtValue();
420
0
  else
421
0
    return nullptr;
422
423
0
  if (Length == 0) // strncmp(x,y,0)   -> 0
424
0
    return ConstantInt::get(CI->getType(), 0);
425
426
0
  if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
427
0
    return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
428
429
0
  StringRef Str1, Str2;
430
0
  bool HasStr1 = getConstantStringInfo(Str1P, Str1);
431
0
  bool HasStr2 = getConstantStringInfo(Str2P, Str2);
432
433
  // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
434
0
  if (HasStr1 && HasStr2) {
435
0
    StringRef SubStr1 = Str1.substr(0, Length);
436
0
    StringRef SubStr2 = Str2.substr(0, Length);
437
0
    return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
438
0
  }
439
440
0
  if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
441
0
    return B.CreateNeg(
442
0
        B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
443
444
0
  if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
445
0
    return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
446
447
0
  return nullptr;
448
0
}
449
450
0
Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {
451
0
  Function *Callee = CI->getCalledFunction();
452
453
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::strcpy))
454
0
    return nullptr;
455
456
0
  Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
457
0
  if (Dst == Src) // strcpy(x,x)  -> x
458
0
    return Src;
459
460
  // See if we can get the length of the input string.
461
0
  uint64_t Len = GetStringLength(Src);
462
0
  if (Len == 0)
463
0
    return nullptr;
464
465
  // We have enough information to now generate the memcpy call to do the
466
  // copy for us.  Make a memcpy to copy the nul byte with align = 1.
467
0
  B.CreateMemCpy(Dst, Src,
468
0
                 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), 1);
469
0
  return Dst;
470
0
}
471
472
0
Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
473
0
  Function *Callee = CI->getCalledFunction();
474
  // Verify the "stpcpy" function prototype.
475
0
  FunctionType *FT = Callee->getFunctionType();
476
477
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::stpcpy))
478
0
    return nullptr;
479
480
0
  Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
481
0
  if (Dst == Src) { // stpcpy(x,x)  -> x+strlen(x)
482
0
    Value *StrLen = EmitStrLen(Src, B, DL, TLI);
483
0
    return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
484
0
  }
485
486
  // See if we can get the length of the input string.
487
0
  uint64_t Len = GetStringLength(Src);
488
0
  if (Len == 0)
489
0
    return nullptr;
490
491
0
  Type *PT = FT->getParamType(0);
492
0
  Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
493
0
  Value *DstEnd =
494
0
      B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
495
496
  // We have enough information to now generate the memcpy call to do the
497
  // copy for us.  Make a memcpy to copy the nul byte with align = 1.
498
0
  B.CreateMemCpy(Dst, Src, LenV, 1);
499
0
  return DstEnd;
500
0
}
501
502
0
Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
503
0
  Function *Callee = CI->getCalledFunction();
504
0
  FunctionType *FT = Callee->getFunctionType();
505
506
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::strncpy))
507
0
    return nullptr;
508
509
0
  Value *Dst = CI->getArgOperand(0);
510
0
  Value *Src = CI->getArgOperand(1);
511
0
  Value *LenOp = CI->getArgOperand(2);
512
513
  // See if we can get the length of the input string.
514
0
  uint64_t SrcLen = GetStringLength(Src);
515
0
  if (SrcLen == 0)
516
0
    return nullptr;
517
0
  --SrcLen;
518
519
0
  if (SrcLen == 0) {
520
    // strncpy(x, "", y) -> memset(x, '\0', y, 1)
521
0
    B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
522
0
    return Dst;
523
0
  }
524
525
0
  uint64_t Len;
526
0
  if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
527
0
    Len = LengthArg->getZExtValue();
528
0
  else
529
0
    return nullptr;
530
531
0
  if (Len == 0)
532
0
    return Dst; // strncpy(x, y, 0) -> x
533
534
  // Let strncpy handle the zero padding
535
0
  if (Len > SrcLen + 1)
536
0
    return nullptr;
537
538
0
  Type *PT = FT->getParamType(0);
539
  // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
540
0
  B.CreateMemCpy(Dst, Src, ConstantInt::get(DL.getIntPtrType(PT), Len), 1);
541
542
0
  return Dst;
543
0
}
544
545
0
Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {
546
0
  Function *Callee = CI->getCalledFunction();
547
0
  FunctionType *FT = Callee->getFunctionType();
548
0
  if (FT->getNumParams() != 1 || FT->getParamType(0) != B.getInt8PtrTy() ||
549
0
      !FT->getReturnType()->isIntegerTy())
550
0
    return nullptr;
551
552
0
  Value *Src = CI->getArgOperand(0);
553
554
  // Constant folding: strlen("xyz") -> 3
555
0
  if (uint64_t Len = GetStringLength(Src))
556
0
    return ConstantInt::get(CI->getType(), Len - 1);
557
558
  // strlen(x?"foo":"bars") --> x ? 3 : 4
559
0
  if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
560
0
    uint64_t LenTrue = GetStringLength(SI->getTrueValue());
561
0
    uint64_t LenFalse = GetStringLength(SI->getFalseValue());
562
0
    if (LenTrue && LenFalse) {
563
0
      Function *Caller = CI->getParent()->getParent();
564
0
      emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,
565
0
                             SI->getDebugLoc(),
566
0
                             "folded strlen(select) to select of constants");
567
0
      return B.CreateSelect(SI->getCondition(),
568
0
                            ConstantInt::get(CI->getType(), LenTrue - 1),
569
0
                            ConstantInt::get(CI->getType(), LenFalse - 1));
570
0
    }
571
0
  }
572
573
  // strlen(x) != 0 --> *x != 0
574
  // strlen(x) == 0 --> *x == 0
575
0
  if (isOnlyUsedInZeroEqualityComparison(CI))
576
0
    return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
577
578
0
  return nullptr;
579
0
}
580
581
0
Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
582
0
  Function *Callee = CI->getCalledFunction();
583
0
  FunctionType *FT = Callee->getFunctionType();
584
0
  if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
585
0
      FT->getParamType(1) != FT->getParamType(0) ||
586
0
      FT->getReturnType() != FT->getParamType(0))
587
0
    return nullptr;
588
589
0
  StringRef S1, S2;
590
0
  bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
591
0
  bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
592
593
  // strpbrk(s, "") -> nullptr
594
  // strpbrk("", s) -> nullptr
595
0
  if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
596
0
    return Constant::getNullValue(CI->getType());
597
598
  // Constant folding.
599
0
  if (HasS1 && HasS2) {
600
0
    size_t I = S1.find_first_of(S2);
601
0
    if (I == StringRef::npos) // No match.
602
0
      return Constant::getNullValue(CI->getType());
603
604
0
    return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I), "strpbrk");
605
0
  }
606
607
  // strpbrk(s, "a") -> strchr(s, 'a')
608
0
  if (HasS2 && S2.size() == 1)
609
0
    return EmitStrChr(CI->getArgOperand(0), S2[0], B, TLI);
610
611
0
  return nullptr;
612
0
}
613
614
0
Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {
615
0
  Function *Callee = CI->getCalledFunction();
616
0
  FunctionType *FT = Callee->getFunctionType();
617
0
  if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
618
0
      !FT->getParamType(0)->isPointerTy() ||
619
0
      !FT->getParamType(1)->isPointerTy())
620
0
    return nullptr;
621
622
0
  Value *EndPtr = CI->getArgOperand(1);
623
0
  if (isa<ConstantPointerNull>(EndPtr)) {
624
    // With a null EndPtr, this function won't capture the main argument.
625
    // It would be readonly too, except that it still may write to errno.
626
0
    CI->addAttribute(1, Attribute::NoCapture);
627
0
  }
628
629
0
  return nullptr;
630
0
}
631
632
0
Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {
633
0
  Function *Callee = CI->getCalledFunction();
634
0
  FunctionType *FT = Callee->getFunctionType();
635
0
  if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
636
0
      FT->getParamType(1) != FT->getParamType(0) ||
637
0
      !FT->getReturnType()->isIntegerTy())
638
0
    return nullptr;
639
640
0
  StringRef S1, S2;
641
0
  bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
642
0
  bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
643
644
  // strspn(s, "") -> 0
645
  // strspn("", s) -> 0
646
0
  if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
647
0
    return Constant::getNullValue(CI->getType());
648
649
  // Constant folding.
650
0
  if (HasS1 && HasS2) {
651
0
    size_t Pos = S1.find_first_not_of(S2);
652
0
    if (Pos == StringRef::npos)
653
0
      Pos = S1.size();
654
0
    return ConstantInt::get(CI->getType(), Pos);
655
0
  }
656
657
0
  return nullptr;
658
0
}
659
660
0
Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
661
0
  Function *Callee = CI->getCalledFunction();
662
0
  FunctionType *FT = Callee->getFunctionType();
663
0
  if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
664
0
      FT->getParamType(1) != FT->getParamType(0) ||
665
0
      !FT->getReturnType()->isIntegerTy())
666
0
    return nullptr;
667
668
0
  StringRef S1, S2;
669
0
  bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
670
0
  bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
671
672
  // strcspn("", s) -> 0
673
0
  if (HasS1 && S1.empty())
674
0
    return Constant::getNullValue(CI->getType());
675
676
  // Constant folding.
677
0
  if (HasS1 && HasS2) {
678
0
    size_t Pos = S1.find_first_of(S2);
679
0
    if (Pos == StringRef::npos)
680
0
      Pos = S1.size();
681
0
    return ConstantInt::get(CI->getType(), Pos);
682
0
  }
683
684
  // strcspn(s, "") -> strlen(s)
685
0
  if (HasS2 && S2.empty())
686
0
    return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);
687
688
0
  return nullptr;
689
0
}
690
691
0
Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
692
0
  Function *Callee = CI->getCalledFunction();
693
0
  FunctionType *FT = Callee->getFunctionType();
694
0
  if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
695
0
      !FT->getParamType(1)->isPointerTy() ||
696
0
      !FT->getReturnType()->isPointerTy())
697
0
    return nullptr;
698
699
  // fold strstr(x, x) -> x.
700
0
  if (CI->getArgOperand(0) == CI->getArgOperand(1))
701
0
    return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
702
703
  // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
704
0
  if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
705
0
    Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);
706
0
    if (!StrLen)
707
0
      return nullptr;
708
0
    Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
709
0
                                 StrLen, B, DL, TLI);
710
0
    if (!StrNCmp)
711
0
      return nullptr;
712
0
    for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
713
0
      ICmpInst *Old = cast<ICmpInst>(*UI++);
714
0
      Value *Cmp =
715
0
          B.CreateICmp(Old->getPredicate(), StrNCmp,
716
0
                       ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
717
0
      replaceAllUsesWith(Old, Cmp);
718
0
    }
719
0
    return CI;
720
0
  }
721
722
  // See if either input string is a constant string.
723
0
  StringRef SearchStr, ToFindStr;
724
0
  bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
725
0
  bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
726
727
  // fold strstr(x, "") -> x.
728
0
  if (HasStr2 && ToFindStr.empty())
729
0
    return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
730
731
  // If both strings are known, constant fold it.
732
0
  if (HasStr1 && HasStr2) {
733
0
    size_t Offset = SearchStr.find(ToFindStr);
734
735
0
    if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
736
0
      return Constant::getNullValue(CI->getType());
737
738
    // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
739
0
    Value *Result = CastToCStr(CI->getArgOperand(0), B);
740
0
    Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
741
0
    return B.CreateBitCast(Result, CI->getType());
742
0
  }
743
744
  // fold strstr(x, "y") -> strchr(x, 'y').
745
0
  if (HasStr2 && ToFindStr.size() == 1) {
746
0
    Value *StrChr = EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
747
0
    return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
748
0
  }
749
0
  return nullptr;
750
0
}
751
752
0
Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
753
0
  Function *Callee = CI->getCalledFunction();
754
0
  FunctionType *FT = Callee->getFunctionType();
755
0
  if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
756
0
      !FT->getParamType(1)->isIntegerTy(32) ||
757
0
      !FT->getParamType(2)->isIntegerTy() ||
758
0
      !FT->getReturnType()->isPointerTy())
759
0
    return nullptr;
760
761
0
  Value *SrcStr = CI->getArgOperand(0);
762
0
  ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
763
0
  ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
764
765
  // memchr(x, y, 0) -> null
766
0
  if (LenC && LenC->isNullValue())
767
0
    return Constant::getNullValue(CI->getType());
768
769
  // From now on we need at least constant length and string.
770
0
  StringRef Str;
771
0
  if (!LenC || !getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
772
0
    return nullptr;
773
774
  // Truncate the string to LenC. If Str is smaller than LenC we will still only
775
  // scan the string, as reading past the end of it is undefined and we can just
776
  // return null if we don't find the char.
777
0
  Str = Str.substr(0, LenC->getZExtValue());
778
779
  // If the char is variable but the input str and length are not we can turn
780
  // this memchr call into a simple bit field test. Of course this only works
781
  // when the return value is only checked against null.
782
  //
783
  // It would be really nice to reuse switch lowering here but we can't change
784
  // the CFG at this point.
785
  //
786
  // memchr("\r\n", C, 2) != nullptr -> (C & ((1 << '\r') | (1 << '\n'))) != 0
787
  //   after bounds check.
788
0
  if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) {
789
0
    unsigned char Max =
790
0
        *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
791
0
                          reinterpret_cast<const unsigned char *>(Str.end()));
792
793
    // Make sure the bit field we're about to create fits in a register on the
794
    // target.
795
    // FIXME: On a 64 bit architecture this prevents us from using the
796
    // interesting range of alpha ascii chars. We could do better by emitting
797
    // two bitfields or shifting the range by 64 if no lower chars are used.
798
0
    if (!DL.fitsInLegalInteger(Max + 1))
799
0
      return nullptr;
800
801
    // For the bit field use a power-of-2 type with at least 8 bits to avoid
802
    // creating unnecessary illegal types.
803
0
    unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
804
805
    // Now build the bit field.
806
0
    APInt Bitfield(Width, 0);
807
0
    for (char C : Str)
808
0
      Bitfield.setBit((unsigned char)C);
809
0
    Value *BitfieldC = B.getInt(Bitfield);
810
811
    // First check that the bit field access is within bounds.
812
0
    Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
813
0
    Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
814
0
                                 "memchr.bounds");
815
816
    // Create code that checks if the given bit is set in the field.
817
0
    Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
818
0
    Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
819
820
    // Finally merge both checks and cast to pointer type. The inttoptr
821
    // implicitly zexts the i1 to intptr type.
822
0
    return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType());
823
0
  }
824
825
  // Check if all arguments are constants.  If so, we can constant fold.
826
0
  if (!CharC)
827
0
    return nullptr;
828
829
  // Compute the offset.
830
0
  size_t I = Str.find(CharC->getSExtValue() & 0xFF);
831
0
  if (I == StringRef::npos) // Didn't find the char.  memchr returns null.
832
0
    return Constant::getNullValue(CI->getType());
833
834
  // memchr(s+n,c,l) -> gep(s+n+i,c)
835
0
  return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "memchr");
836
0
}
837
838
0
Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
839
0
  Function *Callee = CI->getCalledFunction();
840
0
  FunctionType *FT = Callee->getFunctionType();
841
0
  if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
842
0
      !FT->getParamType(1)->isPointerTy() ||
843
0
      !FT->getReturnType()->isIntegerTy(32))
844
0
    return nullptr;
845
846
0
  Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
847
848
0
  if (LHS == RHS) // memcmp(s,s,x) -> 0
849
0
    return Constant::getNullValue(CI->getType());
850
851
  // Make sure we have a constant length.
852
0
  ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
853
0
  if (!LenC)
854
0
    return nullptr;
855
0
  uint64_t Len = LenC->getZExtValue();
856
857
0
  if (Len == 0) // memcmp(s1,s2,0) -> 0
858
0
    return Constant::getNullValue(CI->getType());
859
860
  // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
861
0
  if (Len == 1) {
862
0
    Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
863
0
                               CI->getType(), "lhsv");
864
0
    Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
865
0
                               CI->getType(), "rhsv");
866
0
    return B.CreateSub(LHSV, RHSV, "chardiff");
867
0
  }
868
869
  // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
870
0
  StringRef LHSStr, RHSStr;
871
0
  if (getConstantStringInfo(LHS, LHSStr) &&
872
0
      getConstantStringInfo(RHS, RHSStr)) {
873
    // Make sure we're not reading out-of-bounds memory.
874
0
    if (Len > LHSStr.size() || Len > RHSStr.size())
875
0
      return nullptr;
876
    // Fold the memcmp and normalize the result.  This way we get consistent
877
    // results across multiple platforms.
878
0
    uint64_t Ret = 0;
879
0
    int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
880
0
    if (Cmp < 0)
881
0
      Ret = -1;
882
0
    else if (Cmp > 0)
883
0
      Ret = 1;
884
0
    return ConstantInt::get(CI->getType(), Ret);
885
0
  }
886
887
0
  return nullptr;
888
0
}
889
890
0
Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
891
0
  Function *Callee = CI->getCalledFunction();
892
893
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memcpy))
894
0
    return nullptr;
895
896
  // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
897
0
  B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
898
0
                 CI->getArgOperand(2), 1);
899
0
  return CI->getArgOperand(0);
900
0
}
901
902
0
Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {
903
0
  Function *Callee = CI->getCalledFunction();
904
905
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memmove))
906
0
    return nullptr;
907
908
  // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
909
0
  B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
910
0
                  CI->getArgOperand(2), 1);
911
0
  return CI->getArgOperand(0);
912
0
}
913
914
0
Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {
915
0
  Function *Callee = CI->getCalledFunction();
916
917
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memset))
918
0
    return nullptr;
919
920
  // memset(p, v, n) -> llvm.memset(p, v, n, 1)
921
0
  Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
922
0
  B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
923
0
  return CI->getArgOperand(0);
924
0
}
925
926
//===----------------------------------------------------------------------===//
927
// Math Library Optimizations
928
//===----------------------------------------------------------------------===//
929
930
/// Return a variant of Val with float type.
931
/// Currently this works in two cases: If Val is an FPExtension of a float
932
/// value to something bigger, simply return the operand.
933
/// If Val is a ConstantFP but can be converted to a float ConstantFP without
934
/// loss of precision do so.
935
0
static Value *valueHasFloatPrecision(Value *Val) {
936
0
  if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
937
0
    Value *Op = Cast->getOperand(0);
938
0
    if (Op->getType()->isFloatTy())
939
0
      return Op;
940
0
  }
941
0
  if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
942
0
    APFloat F = Const->getValueAPF();
943
0
    bool losesInfo;
944
0
    (void)F.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
945
0
                    &losesInfo);
946
0
    if (!losesInfo)
947
0
      return ConstantFP::get(Const->getContext(), F);
948
0
  }
949
0
  return nullptr;
950
0
}
951
952
//===----------------------------------------------------------------------===//
953
// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
954
955
Value *LibCallSimplifier::optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
956
0
                                                bool CheckRetType) {
957
0
  Function *Callee = CI->getCalledFunction();
958
0
  FunctionType *FT = Callee->getFunctionType();
959
0
  if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
960
0
      !FT->getParamType(0)->isDoubleTy())
961
0
    return nullptr;
962
963
0
  if (CheckRetType) {
964
    // Check if all the uses for function like 'sin' are converted to float.
965
0
    for (User *U : CI->users()) {
966
0
      FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
967
0
      if (!Cast || !Cast->getType()->isFloatTy())
968
0
        return nullptr;
969
0
    }
970
0
  }
971
972
  // If this is something like 'floor((double)floatval)', convert to floorf.
973
0
  Value *V = valueHasFloatPrecision(CI->getArgOperand(0));
974
0
  if (V == nullptr)
975
0
    return nullptr;
976
977
  // floor((double)floatval) -> (double)floorf(floatval)
978
0
  if (Callee->isIntrinsic()) {
979
0
    Module *M = CI->getParent()->getParent()->getParent();
980
0
    Intrinsic::ID IID = Callee->getIntrinsicID();
981
0
    Function *F = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
982
0
    V = B.CreateCall(F, V);
983
0
  } else {
984
    // The call is a library call rather than an intrinsic.
985
0
    V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
986
0
  }
987
988
0
  return B.CreateFPExt(V, B.getDoubleTy());
989
0
}
990
991
// Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'
992
0
Value *LibCallSimplifier::optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
993
0
  Function *Callee = CI->getCalledFunction();
994
0
  FunctionType *FT = Callee->getFunctionType();
995
  // Just make sure this has 2 arguments of the same FP type, which match the
996
  // result type.
997
0
  if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
998
0
      FT->getParamType(0) != FT->getParamType(1) ||
999
0
      !FT->getParamType(0)->isFloatingPointTy())
1000
0
    return nullptr;
1001
1002
  // If this is something like 'fmin((double)floatval1, (double)floatval2)',
1003
  // or fmin(1.0, (double)floatval), then we convert it to fminf.
1004
0
  Value *V1 = valueHasFloatPrecision(CI->getArgOperand(0));
1005
0
  if (V1 == nullptr)
1006
0
    return nullptr;
1007
0
  Value *V2 = valueHasFloatPrecision(CI->getArgOperand(1));
1008
0
  if (V2 == nullptr)
1009
0
    return nullptr;
1010
1011
  // fmin((double)floatval1, (double)floatval2)
1012
  //                      -> (double)fminf(floatval1, floatval2)
1013
  // TODO: Handle intrinsics in the same way as in optimizeUnaryDoubleFP().
1014
0
  Value *V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
1015
0
                                   Callee->getAttributes());
1016
0
  return B.CreateFPExt(V, B.getDoubleTy());
1017
0
}
1018
1019
0
Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {
1020
0
  Function *Callee = CI->getCalledFunction();
1021
0
  Value *Ret = nullptr;
1022
0
  if (UnsafeFPShrink && Callee->getName() == "cos" && TLI->has(LibFunc::cosf)) {
1023
0
    Ret = optimizeUnaryDoubleFP(CI, B, true);
1024
0
  }
1025
1026
0
  FunctionType *FT = Callee->getFunctionType();
1027
  // Just make sure this has 1 argument of FP type, which matches the
1028
  // result type.
1029
0
  if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1030
0
      !FT->getParamType(0)->isFloatingPointTy())
1031
0
    return Ret;
1032
1033
  // cos(-x) -> cos(x)
1034
0
  Value *Op1 = CI->getArgOperand(0);
1035
0
  if (BinaryOperator::isFNeg(Op1)) {
1036
0
    BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1037
0
    return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1038
0
  }
1039
0
  return Ret;
1040
0
}
1041
1042
0
Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
1043
0
  Function *Callee = CI->getCalledFunction();
1044
1045
0
  Value *Ret = nullptr;
1046
0
  if (UnsafeFPShrink && Callee->getName() == "pow" && TLI->has(LibFunc::powf)) {
1047
0
    Ret = optimizeUnaryDoubleFP(CI, B, true);
1048
0
  }
1049
1050
0
  FunctionType *FT = Callee->getFunctionType();
1051
  // Just make sure this has 2 arguments of the same FP type, which match the
1052
  // result type.
1053
0
  if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1054
0
      FT->getParamType(0) != FT->getParamType(1) ||
1055
0
      !FT->getParamType(0)->isFloatingPointTy())
1056
0
    return Ret;
1057
1058
0
  Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1059
0
  if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1060
    // pow(1.0, x) -> 1.0
1061
0
    if (Op1C->isExactlyValue(1.0))
1062
0
      return Op1C;
1063
    // pow(2.0, x) -> exp2(x)
1064
0
    if (Op1C->isExactlyValue(2.0) &&
1065
0
        hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
1066
0
                        LibFunc::exp2l))
1067
0
      return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1068
    // pow(10.0, x) -> exp10(x)
1069
0
    if (Op1C->isExactlyValue(10.0) &&
1070
0
        hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1071
0
                        LibFunc::exp10l))
1072
0
      return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
1073
0
                                  Callee->getAttributes());
1074
0
  }
1075
1076
0
  ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1077
0
  if (!Op2C)
1078
0
    return Ret;
1079
1080
0
  if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1081
0
    return ConstantFP::get(CI->getType(), 1.0);
1082
1083
0
  if (Op2C->isExactlyValue(0.5) &&
1084
0
      hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1085
0
                      LibFunc::sqrtl) &&
1086
0
      hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1087
0
                      LibFunc::fabsl)) {
1088
    // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1089
    // This is faster than calling pow, and still handles negative zero
1090
    // and negative infinity correctly.
1091
    // TODO: In fast-math mode, this could be just sqrt(x).
1092
    // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1093
0
    Value *Inf = ConstantFP::getInfinity(CI->getType());
1094
0
    Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1095
0
    Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());
1096
0
    Value *FAbs =
1097
0
        EmitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());
1098
0
    Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1099
0
    Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1100
0
    return Sel;
1101
0
  }
1102
1103
0
  if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1104
0
    return Op1;
1105
0
  if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1106
0
    return B.CreateFMul(Op1, Op1, "pow2");
1107
0
  if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1108
0
    return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
1109
0
  return nullptr;
1110
0
}
1111
1112
0
Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
1113
0
  Function *Callee = CI->getCalledFunction();
1114
0
  Function *Caller = CI->getParent()->getParent();
1115
1116
0
  Value *Ret = nullptr;
1117
0
  if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1118
0
      TLI->has(LibFunc::exp2f)) {
1119
0
    Ret = optimizeUnaryDoubleFP(CI, B, true);
1120
0
  }
1121
1122
0
  FunctionType *FT = Callee->getFunctionType();
1123
  // Just make sure this has 1 argument of FP type, which matches the
1124
  // result type.
1125
0
  if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1126
0
      !FT->getParamType(0)->isFloatingPointTy())
1127
0
    return Ret;
1128
1129
0
  Value *Op = CI->getArgOperand(0);
1130
  // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= 32
1131
  // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < 32
1132
0
  LibFunc::Func LdExp = LibFunc::ldexpl;
1133
0
  if (Op->getType()->isFloatTy())
1134
0
    LdExp = LibFunc::ldexpf;
1135
0
  else if (Op->getType()->isDoubleTy())
1136
0
    LdExp = LibFunc::ldexp;
1137
1138
0
  if (TLI->has(LdExp)) {
1139
0
    Value *LdExpArg = nullptr;
1140
0
    if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1141
0
      if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1142
0
        LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1143
0
    } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1144
0
      if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1145
0
        LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1146
0
    }
1147
1148
0
    if (LdExpArg) {
1149
0
      Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));
1150
0
      if (!Op->getType()->isFloatTy())
1151
0
        One = ConstantExpr::getFPExtend(One, Op->getType());
1152
1153
0
      Module *M = Caller->getParent();
1154
0
      Value *Callee =
1155
0
          M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1156
0
                                 Op->getType(), B.getInt32Ty(), nullptr);
1157
0
      CallInst *CI = B.CreateCall(Callee, {One, LdExpArg});
1158
0
      if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1159
0
        CI->setCallingConv(F->getCallingConv());
1160
1161
0
      return CI;
1162
0
    }
1163
0
  }
1164
0
  return Ret;
1165
0
}
1166
1167
0
Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) {
1168
0
  Function *Callee = CI->getCalledFunction();
1169
1170
0
  Value *Ret = nullptr;
1171
0
  if (Callee->getName() == "fabs" && TLI->has(LibFunc::fabsf)) {
1172
0
    Ret = optimizeUnaryDoubleFP(CI, B, false);
1173
0
  }
1174
1175
0
  FunctionType *FT = Callee->getFunctionType();
1176
  // Make sure this has 1 argument of FP type which matches the result type.
1177
0
  if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1178
0
      !FT->getParamType(0)->isFloatingPointTy())
1179
0
    return Ret;
1180
1181
0
  Value *Op = CI->getArgOperand(0);
1182
0
  if (Instruction *I = dyn_cast<Instruction>(Op)) {
1183
    // Fold fabs(x * x) -> x * x; any squared FP value must already be positive.
1184
0
    if (I->getOpcode() == Instruction::FMul)
1185
0
      if (I->getOperand(0) == I->getOperand(1))
1186
0
        return Op;
1187
0
  }
1188
0
  return Ret;
1189
0
}
1190
1191
0
Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
1192
0
  Function *Callee = CI->getCalledFunction();
1193
  
1194
0
  Value *Ret = nullptr;
1195
0
  if (TLI->has(LibFunc::sqrtf) && (Callee->getName() == "sqrt" ||
1196
0
                                   Callee->getIntrinsicID() == Intrinsic::sqrt))
1197
0
    Ret = optimizeUnaryDoubleFP(CI, B, true);
1198
1199
  // FIXME: For finer-grain optimization, we need intrinsics to have the same
1200
  // fast-math flag decorations that are applied to FP instructions. For now,
1201
  // we have to rely on the function-level unsafe-fp-math attribute to do this
1202
  // optimization because there's no other way to express that the sqrt can be
1203
  // reassociated.
1204
0
  Function *F = CI->getParent()->getParent();
1205
0
  if (F->hasFnAttribute("unsafe-fp-math")) {
1206
    // Check for unsafe-fp-math = true.
1207
0
    Attribute Attr = F->getFnAttribute("unsafe-fp-math");
1208
0
    if (Attr.getValueAsString() != "true")
1209
0
      return Ret;
1210
0
  }
1211
0
  Value *Op = CI->getArgOperand(0);
1212
0
  if (Instruction *I = dyn_cast<Instruction>(Op)) {
1213
0
    if (I->getOpcode() == Instruction::FMul && I->hasUnsafeAlgebra()) {
1214
      // We're looking for a repeated factor in a multiplication tree,
1215
      // so we can do this fold: sqrt(x * x) -> fabs(x);
1216
      // or this fold: sqrt(x * x * y) -> fabs(x) * sqrt(y).
1217
0
      Value *Op0 = I->getOperand(0);
1218
0
      Value *Op1 = I->getOperand(1);
1219
0
      Value *RepeatOp = nullptr;
1220
0
      Value *OtherOp = nullptr;
1221
0
      if (Op0 == Op1) {
1222
        // Simple match: the operands of the multiply are identical.
1223
0
        RepeatOp = Op0;
1224
0
      } else {
1225
        // Look for a more complicated pattern: one of the operands is itself
1226
        // a multiply, so search for a common factor in that multiply.
1227
        // Note: We don't bother looking any deeper than this first level or for
1228
        // variations of this pattern because instcombine's visitFMUL and/or the
1229
        // reassociation pass should give us this form.
1230
0
        Value *OtherMul0, *OtherMul1;
1231
0
        if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
1232
          // Pattern: sqrt((x * y) * z)
1233
0
          if (OtherMul0 == OtherMul1) {
1234
            // Matched: sqrt((x * x) * z)
1235
0
            RepeatOp = OtherMul0;
1236
0
            OtherOp = Op1;
1237
0
          }
1238
0
        }
1239
0
      }
1240
0
      if (RepeatOp) {
1241
        // Fast math flags for any created instructions should match the sqrt
1242
        // and multiply.
1243
        // FIXME: We're not checking the sqrt because it doesn't have
1244
        // fast-math-flags (see earlier comment).
1245
0
        IRBuilder<true, ConstantFolder,
1246
0
          IRBuilderDefaultInserter<true> >::FastMathFlagGuard Guard(B);
1247
0
        B.SetFastMathFlags(I->getFastMathFlags());
1248
        // If we found a repeated factor, hoist it out of the square root and
1249
        // replace it with the fabs of that factor.
1250
0
        Module *M = Callee->getParent();
1251
0
        Type *ArgType = Op->getType();
1252
0
        Value *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
1253
0
        Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
1254
0
        if (OtherOp) {
1255
          // If we found a non-repeated factor, we still need to get its square
1256
          // root. We then multiply that by the value that was simplified out
1257
          // of the square root calculation.
1258
0
          Value *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
1259
0
          Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
1260
0
          return B.CreateFMul(FabsCall, SqrtCall);
1261
0
        }
1262
0
        return FabsCall;
1263
0
      }
1264
0
    }
1265
0
  }
1266
0
  return Ret;
1267
0
}
1268
1269
static bool isTrigLibCall(CallInst *CI);
1270
static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1271
                             bool UseFloat, Value *&Sin, Value *&Cos,
1272
                             Value *&SinCos);
1273
1274
0
Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {
1275
1276
  // Make sure the prototype is as expected, otherwise the rest of the
1277
  // function is probably invalid and likely to abort.
1278
0
  if (!isTrigLibCall(CI))
1279
0
    return nullptr;
1280
1281
0
  Value *Arg = CI->getArgOperand(0);
1282
0
  SmallVector<CallInst *, 1> SinCalls;
1283
0
  SmallVector<CallInst *, 1> CosCalls;
1284
0
  SmallVector<CallInst *, 1> SinCosCalls;
1285
1286
0
  bool IsFloat = Arg->getType()->isFloatTy();
1287
1288
  // Look for all compatible sinpi, cospi and sincospi calls with the same
1289
  // argument. If there are enough (in some sense) we can make the
1290
  // substitution.
1291
0
  for (User *U : Arg->users())
1292
0
    classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,
1293
0
                   SinCosCalls);
1294
1295
  // It's only worthwhile if both sinpi and cospi are actually used.
1296
0
  if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1297
0
    return nullptr;
1298
1299
0
  Value *Sin, *Cos, *SinCos;
1300
0
  insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);
1301
1302
0
  replaceTrigInsts(SinCalls, Sin);
1303
0
  replaceTrigInsts(CosCalls, Cos);
1304
0
  replaceTrigInsts(SinCosCalls, SinCos);
1305
1306
0
  return nullptr;
1307
0
}
1308
1309
0
static bool isTrigLibCall(CallInst *CI) {
1310
0
  Function *Callee = CI->getCalledFunction();
1311
0
  FunctionType *FT = Callee->getFunctionType();
1312
1313
  // We can only hope to do anything useful if we can ignore things like errno
1314
  // and floating-point exceptions.
1315
0
  bool AttributesSafe =
1316
0
      CI->hasFnAttr(Attribute::NoUnwind) && CI->hasFnAttr(Attribute::ReadNone);
1317
1318
  // Other than that we need float(float) or double(double)
1319
0
  return AttributesSafe && FT->getNumParams() == 1 &&
1320
0
         FT->getReturnType() == FT->getParamType(0) &&
1321
0
         (FT->getParamType(0)->isFloatTy() ||
1322
0
          FT->getParamType(0)->isDoubleTy());
1323
0
}
1324
1325
void
1326
LibCallSimplifier::classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
1327
                                  SmallVectorImpl<CallInst *> &SinCalls,
1328
                                  SmallVectorImpl<CallInst *> &CosCalls,
1329
0
                                  SmallVectorImpl<CallInst *> &SinCosCalls) {
1330
0
  CallInst *CI = dyn_cast<CallInst>(Val);
1331
1332
0
  if (!CI)
1333
0
    return;
1334
1335
0
  Function *Callee = CI->getCalledFunction();
1336
0
  StringRef FuncName = Callee->getName();
1337
0
  LibFunc::Func Func;
1338
0
  if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) || !isTrigLibCall(CI))
1339
0
    return;
1340
1341
0
  if (IsFloat) {
1342
0
    if (Func == LibFunc::sinpif)
1343
0
      SinCalls.push_back(CI);
1344
0
    else if (Func == LibFunc::cospif)
1345
0
      CosCalls.push_back(CI);
1346
0
    else if (Func == LibFunc::sincospif_stret)
1347
0
      SinCosCalls.push_back(CI);
1348
0
  } else {
1349
0
    if (Func == LibFunc::sinpi)
1350
0
      SinCalls.push_back(CI);
1351
0
    else if (Func == LibFunc::cospi)
1352
0
      CosCalls.push_back(CI);
1353
0
    else if (Func == LibFunc::sincospi_stret)
1354
0
      SinCosCalls.push_back(CI);
1355
0
  }
1356
0
}
1357
1358
void LibCallSimplifier::replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls,
1359
0
                                         Value *Res) {
1360
0
  for (SmallVectorImpl<CallInst *>::iterator I = Calls.begin(), E = Calls.end();
1361
0
       I != E; ++I) {
1362
0
    replaceAllUsesWith(*I, Res);
1363
0
  }
1364
0
}
1365
1366
void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1367
0
                      bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos) {
1368
0
  Type *ArgTy = Arg->getType();
1369
0
  Type *ResTy;
1370
0
  StringRef Name;
1371
1372
0
  Triple T(OrigCallee->getParent()->getTargetTriple());
1373
0
  if (UseFloat) {
1374
0
    Name = "__sincospif_stret";
1375
1376
0
    assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1377
    // x86_64 can't use {float, float} since that would be returned in both
1378
    // xmm0 and xmm1, which isn't what a real struct would do.
1379
0
    ResTy = T.getArch() == Triple::x86_64
1380
0
                ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1381
0
                : static_cast<Type *>(StructType::get(ArgTy, ArgTy, nullptr));
1382
0
  } else {
1383
0
    Name = "__sincospi_stret";
1384
0
    ResTy = StructType::get(ArgTy, ArgTy, nullptr);
1385
0
  }
1386
1387
0
  Module *M = OrigCallee->getParent();
1388
0
  Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1389
0
                                         ResTy, ArgTy, nullptr);
1390
1391
0
  if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1392
    // If the argument is an instruction, it must dominate all uses so put our
1393
    // sincos call there.
1394
0
    BasicBlock::iterator Loc = ArgInst;
1395
0
    B.SetInsertPoint(ArgInst->getParent(), ++Loc);
1396
0
  } else {
1397
    // Otherwise (e.g. for a constant) the beginning of the function is as
1398
    // good a place as any.
1399
0
    BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1400
0
    B.SetInsertPoint(&EntryBB, EntryBB.begin());
1401
0
  }
1402
1403
0
  SinCos = B.CreateCall(Callee, Arg, "sincospi");
1404
1405
0
  if (SinCos->getType()->isStructTy()) {
1406
0
    Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1407
0
    Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1408
0
  } else {
1409
0
    Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1410
0
                                 "sinpi");
1411
0
    Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1412
0
                                 "cospi");
1413
0
  }
1414
0
}
1415
1416
//===----------------------------------------------------------------------===//
1417
// Integer Library Call Optimizations
1418
//===----------------------------------------------------------------------===//
1419
1420
0
Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {
1421
0
  Function *Callee = CI->getCalledFunction();
1422
0
  FunctionType *FT = Callee->getFunctionType();
1423
  // Just make sure this has 2 arguments of the same FP type, which match the
1424
  // result type.
1425
0
  if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy(32) ||
1426
0
      !FT->getParamType(0)->isIntegerTy())
1427
0
    return nullptr;
1428
1429
0
  Value *Op = CI->getArgOperand(0);
1430
1431
  // Constant fold.
1432
0
  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1433
0
    if (CI->isZero()) // ffs(0) -> 0.
1434
0
      return B.getInt32(0);
1435
    // ffs(c) -> cttz(c)+1
1436
0
    return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1437
0
  }
1438
1439
  // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1440
0
  Type *ArgType = Op->getType();
1441
0
  Value *F =
1442
0
      Intrinsic::getDeclaration(Callee->getParent(), Intrinsic::cttz, ArgType);
1443
0
  Value *V = B.CreateCall(F, {Op, B.getFalse()}, "cttz");
1444
0
  V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1445
0
  V = B.CreateIntCast(V, B.getInt32Ty(), false);
1446
1447
0
  Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1448
0
  return B.CreateSelect(Cond, V, B.getInt32(0));
1449
0
}
1450
1451
0
Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
1452
0
  Function *Callee = CI->getCalledFunction();
1453
0
  FunctionType *FT = Callee->getFunctionType();
1454
  // We require integer(integer) where the types agree.
1455
0
  if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1456
0
      FT->getParamType(0) != FT->getReturnType())
1457
0
    return nullptr;
1458
1459
  // abs(x) -> x >s -1 ? x : -x
1460
0
  Value *Op = CI->getArgOperand(0);
1461
0
  Value *Pos =
1462
0
      B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");
1463
0
  Value *Neg = B.CreateNeg(Op, "neg");
1464
0
  return B.CreateSelect(Pos, Op, Neg);
1465
0
}
1466
1467
0
Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
1468
0
  Function *Callee = CI->getCalledFunction();
1469
0
  FunctionType *FT = Callee->getFunctionType();
1470
  // We require integer(i32)
1471
0
  if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1472
0
      !FT->getParamType(0)->isIntegerTy(32))
1473
0
    return nullptr;
1474
1475
  // isdigit(c) -> (c-'0') <u 10
1476
0
  Value *Op = CI->getArgOperand(0);
1477
0
  Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1478
0
  Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1479
0
  return B.CreateZExt(Op, CI->getType());
1480
0
}
1481
1482
0
Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {
1483
0
  Function *Callee = CI->getCalledFunction();
1484
0
  FunctionType *FT = Callee->getFunctionType();
1485
  // We require integer(i32)
1486
0
  if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1487
0
      !FT->getParamType(0)->isIntegerTy(32))
1488
0
    return nullptr;
1489
1490
  // isascii(c) -> c <u 128
1491
0
  Value *Op = CI->getArgOperand(0);
1492
0
  Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1493
0
  return B.CreateZExt(Op, CI->getType());
1494
0
}
1495
1496
0
Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
1497
0
  Function *Callee = CI->getCalledFunction();
1498
0
  FunctionType *FT = Callee->getFunctionType();
1499
  // We require i32(i32)
1500
0
  if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1501
0
      !FT->getParamType(0)->isIntegerTy(32))
1502
0
    return nullptr;
1503
1504
  // toascii(c) -> c & 0x7f
1505
0
  return B.CreateAnd(CI->getArgOperand(0),
1506
0
                     ConstantInt::get(CI->getType(), 0x7F));
1507
0
}
1508
1509
//===----------------------------------------------------------------------===//
1510
// Formatting and IO Library Call Optimizations
1511
//===----------------------------------------------------------------------===//
1512
1513
static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
1514
1515
Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
1516
0
                                                 int StreamArg) {
1517
  // Error reporting calls should be cold, mark them as such.
1518
  // This applies even to non-builtin calls: it is only a hint and applies to
1519
  // functions that the frontend might not understand as builtins.
1520
1521
  // This heuristic was suggested in:
1522
  // Improving Static Branch Prediction in a Compiler
1523
  // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1524
  // Proceedings of PACT'98, Oct. 1998, IEEE
1525
0
  Function *Callee = CI->getCalledFunction();
1526
1527
0
  if (!CI->hasFnAttr(Attribute::Cold) &&
1528
0
      isReportingError(Callee, CI, StreamArg)) {
1529
0
    CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1530
0
  }
1531
1532
0
  return nullptr;
1533
0
}
1534
1535
0
static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
1536
0
  if (!ColdErrorCalls)
1537
0
    return false;
1538
1539
0
  if (!Callee || !Callee->isDeclaration())
1540
0
    return false;
1541
1542
0
  if (StreamArg < 0)
1543
0
    return true;
1544
1545
  // These functions might be considered cold, but only if their stream
1546
  // argument is stderr.
1547
1548
0
  if (StreamArg >= (int)CI->getNumArgOperands())
1549
0
    return false;
1550
0
  LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1551
0
  if (!LI)
1552
0
    return false;
1553
0
  GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1554
0
  if (!GV || !GV->isDeclaration())
1555
0
    return false;
1556
0
  return GV->getName() == "stderr";
1557
0
}
1558
1559
0
Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
1560
  // Check for a fixed format string.
1561
0
  StringRef FormatStr;
1562
0
  if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1563
0
    return nullptr;
1564
1565
  // Empty format string -> noop.
1566
0
  if (FormatStr.empty()) // Tolerate printf's declared void.
1567
0
    return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
1568
1569
  // Do not do any of the following transformations if the printf return value
1570
  // is used, in general the printf return value is not compatible with either
1571
  // putchar() or puts().
1572
0
  if (!CI->use_empty())
1573
0
    return nullptr;
1574
1575
  // printf("x") -> putchar('x'), even for '%'.
1576
0
  if (FormatStr.size() == 1) {
1577
0
    Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TLI);
1578
0
    if (CI->use_empty() || !Res)
1579
0
      return Res;
1580
0
    return B.CreateIntCast(Res, CI->getType(), true);
1581
0
  }
1582
1583
  // printf("foo\n") --> puts("foo")
1584
0
  if (FormatStr[FormatStr.size() - 1] == '\n' &&
1585
0
      FormatStr.find('%') == StringRef::npos) { // No format characters.
1586
    // Create a string literal with no \n on it.  We expect the constant merge
1587
    // pass to be run after this pass, to merge duplicate strings.
1588
0
    FormatStr = FormatStr.drop_back();
1589
0
    Value *GV = B.CreateGlobalString(FormatStr, "str");
1590
0
    Value *NewCI = EmitPutS(GV, B, TLI);
1591
0
    return (CI->use_empty() || !NewCI)
1592
0
               ? NewCI
1593
0
               : ConstantInt::get(CI->getType(), FormatStr.size() + 1);
1594
0
  }
1595
1596
  // Optimize specific format strings.
1597
  // printf("%c", chr) --> putchar(chr)
1598
0
  if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1599
0
      CI->getArgOperand(1)->getType()->isIntegerTy()) {
1600
0
    Value *Res = EmitPutChar(CI->getArgOperand(1), B, TLI);
1601
1602
0
    if (CI->use_empty() || !Res)
1603
0
      return Res;
1604
0
    return B.CreateIntCast(Res, CI->getType(), true);
1605
0
  }
1606
1607
  // printf("%s\n", str) --> puts(str)
1608
0
  if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1609
0
      CI->getArgOperand(1)->getType()->isPointerTy()) {
1610
0
    return EmitPutS(CI->getArgOperand(1), B, TLI);
1611
0
  }
1612
0
  return nullptr;
1613
0
}
1614
1615
0
Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
1616
1617
0
  Function *Callee = CI->getCalledFunction();
1618
  // Require one fixed pointer argument and an integer/void result.
1619
0
  FunctionType *FT = Callee->getFunctionType();
1620
0
  if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1621
0
      !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1622
0
    return nullptr;
1623
1624
0
  if (Value *V = optimizePrintFString(CI, B)) {
1625
0
    return V;
1626
0
  }
1627
1628
  // printf(format, ...) -> iprintf(format, ...) if no floating point
1629
  // arguments.
1630
0
  if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1631
0
    Module *M = B.GetInsertBlock()->getParent()->getParent();
1632
0
    Constant *IPrintFFn =
1633
0
        M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1634
0
    CallInst *New = cast<CallInst>(CI->clone());
1635
0
    New->setCalledFunction(IPrintFFn);
1636
0
    B.Insert(New);
1637
0
    return New;
1638
0
  }
1639
0
  return nullptr;
1640
0
}
1641
1642
0
Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
1643
  // Check for a fixed format string.
1644
0
  StringRef FormatStr;
1645
0
  if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1646
0
    return nullptr;
1647
1648
  // If we just have a format string (nothing else crazy) transform it.
1649
0
  if (CI->getNumArgOperands() == 2) {
1650
    // Make sure there's no % in the constant array.  We could try to handle
1651
    // %% -> % in the future if we cared.
1652
0
    for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1653
0
      if (FormatStr[i] == '%')
1654
0
        return nullptr; // we found a format specifier, bail out.
1655
1656
    // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1657
0
    B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1658
0
                   ConstantInt::get(DL.getIntPtrType(CI->getContext()),
1659
0
                                    FormatStr.size() + 1),
1660
0
                   1); // Copy the null byte.
1661
0
    return ConstantInt::get(CI->getType(), FormatStr.size());
1662
0
  }
1663
1664
  // The remaining optimizations require the format string to be "%s" or "%c"
1665
  // and have an extra operand.
1666
0
  if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1667
0
      CI->getNumArgOperands() < 3)
1668
0
    return nullptr;
1669
1670
  // Decode the second character of the format string.
1671
0
  if (FormatStr[1] == 'c') {
1672
    // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1673
0
    if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1674
0
      return nullptr;
1675
0
    Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1676
0
    Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1677
0
    B.CreateStore(V, Ptr);
1678
0
    Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
1679
0
    B.CreateStore(B.getInt8(0), Ptr);
1680
1681
0
    return ConstantInt::get(CI->getType(), 1);
1682
0
  }
1683
1684
0
  if (FormatStr[1] == 's') {
1685
    // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1686
0
    if (!CI->getArgOperand(2)->getType()->isPointerTy())
1687
0
      return nullptr;
1688
1689
0
    Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);
1690
0
    if (!Len)
1691
0
      return nullptr;
1692
0
    Value *IncLen =
1693
0
        B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
1694
0
    B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1695
1696
    // The sprintf result is the unincremented number of bytes in the string.
1697
0
    return B.CreateIntCast(Len, CI->getType(), false);
1698
0
  }
1699
0
  return nullptr;
1700
0
}
1701
1702
0
Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
1703
0
  Function *Callee = CI->getCalledFunction();
1704
  // Require two fixed pointer arguments and an integer result.
1705
0
  FunctionType *FT = Callee->getFunctionType();
1706
0
  if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1707
0
      !FT->getParamType(1)->isPointerTy() ||
1708
0
      !FT->getReturnType()->isIntegerTy())
1709
0
    return nullptr;
1710
1711
0
  if (Value *V = optimizeSPrintFString(CI, B)) {
1712
0
    return V;
1713
0
  }
1714
1715
  // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1716
  // point arguments.
1717
0
  if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1718
0
    Module *M = B.GetInsertBlock()->getParent()->getParent();
1719
0
    Constant *SIPrintFFn =
1720
0
        M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1721
0
    CallInst *New = cast<CallInst>(CI->clone());
1722
0
    New->setCalledFunction(SIPrintFFn);
1723
0
    B.Insert(New);
1724
0
    return New;
1725
0
  }
1726
0
  return nullptr;
1727
0
}
1728
1729
0
Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
1730
0
  optimizeErrorReporting(CI, B, 0);
1731
1732
  // All the optimizations depend on the format string.
1733
0
  StringRef FormatStr;
1734
0
  if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1735
0
    return nullptr;
1736
1737
  // Do not do any of the following transformations if the fprintf return
1738
  // value is used, in general the fprintf return value is not compatible
1739
  // with fwrite(), fputc() or fputs().
1740
0
  if (!CI->use_empty())
1741
0
    return nullptr;
1742
1743
  // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1744
0
  if (CI->getNumArgOperands() == 2) {
1745
0
    for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1746
0
      if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1747
0
        return nullptr;        // We found a format specifier.
1748
1749
0
    return EmitFWrite(
1750
0
        CI->getArgOperand(1),
1751
0
        ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()),
1752
0
        CI->getArgOperand(0), B, DL, TLI);
1753
0
  }
1754
1755
  // The remaining optimizations require the format string to be "%s" or "%c"
1756
  // and have an extra operand.
1757
0
  if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1758
0
      CI->getNumArgOperands() < 3)
1759
0
    return nullptr;
1760
1761
  // Decode the second character of the format string.
1762
0
  if (FormatStr[1] == 'c') {
1763
    // fprintf(F, "%c", chr) --> fputc(chr, F)
1764
0
    if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1765
0
      return nullptr;
1766
0
    return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
1767
0
  }
1768
1769
0
  if (FormatStr[1] == 's') {
1770
    // fprintf(F, "%s", str) --> fputs(str, F)
1771
0
    if (!CI->getArgOperand(2)->getType()->isPointerTy())
1772
0
      return nullptr;
1773
0
    return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
1774
0
  }
1775
0
  return nullptr;
1776
0
}
1777
1778
0
Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
1779
0
  Function *Callee = CI->getCalledFunction();
1780
  // Require two fixed paramters as pointers and integer result.
1781
0
  FunctionType *FT = Callee->getFunctionType();
1782
0
  if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1783
0
      !FT->getParamType(1)->isPointerTy() ||
1784
0
      !FT->getReturnType()->isIntegerTy())
1785
0
    return nullptr;
1786
1787
0
  if (Value *V = optimizeFPrintFString(CI, B)) {
1788
0
    return V;
1789
0
  }
1790
1791
  // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1792
  // floating point arguments.
1793
0
  if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1794
0
    Module *M = B.GetInsertBlock()->getParent()->getParent();
1795
0
    Constant *FIPrintFFn =
1796
0
        M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1797
0
    CallInst *New = cast<CallInst>(CI->clone());
1798
0
    New->setCalledFunction(FIPrintFFn);
1799
0
    B.Insert(New);
1800
0
    return New;
1801
0
  }
1802
0
  return nullptr;
1803
0
}
1804
1805
0
Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
1806
0
  optimizeErrorReporting(CI, B, 3);
1807
1808
0
  Function *Callee = CI->getCalledFunction();
1809
  // Require a pointer, an integer, an integer, a pointer, returning integer.
1810
0
  FunctionType *FT = Callee->getFunctionType();
1811
0
  if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1812
0
      !FT->getParamType(1)->isIntegerTy() ||
1813
0
      !FT->getParamType(2)->isIntegerTy() ||
1814
0
      !FT->getParamType(3)->isPointerTy() ||
1815
0
      !FT->getReturnType()->isIntegerTy())
1816
0
    return nullptr;
1817
1818
  // Get the element size and count.
1819
0
  ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1820
0
  ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1821
0
  if (!SizeC || !CountC)
1822
0
    return nullptr;
1823
0
  uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
1824
1825
  // If this is writing zero records, remove the call (it's a noop).
1826
0
  if (Bytes == 0)
1827
0
    return ConstantInt::get(CI->getType(), 0);
1828
1829
  // If this is writing one byte, turn it into fputc.
1830
  // This optimisation is only valid, if the return value is unused.
1831
0
  if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1832
0
    Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1833
0
    Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TLI);
1834
0
    return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
1835
0
  }
1836
1837
0
  return nullptr;
1838
0
}
1839
1840
0
Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
1841
0
  optimizeErrorReporting(CI, B, 1);
1842
1843
0
  Function *Callee = CI->getCalledFunction();
1844
1845
  // Require two pointers.  Also, we can't optimize if return value is used.
1846
0
  FunctionType *FT = Callee->getFunctionType();
1847
0
  if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1848
0
      !FT->getParamType(1)->isPointerTy() || !CI->use_empty())
1849
0
    return nullptr;
1850
1851
  // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1852
0
  uint64_t Len = GetStringLength(CI->getArgOperand(0));
1853
0
  if (!Len)
1854
0
    return nullptr;
1855
1856
  // Known to have no uses (see above).
1857
0
  return EmitFWrite(
1858
0
      CI->getArgOperand(0),
1859
0
      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
1860
0
      CI->getArgOperand(1), B, DL, TLI);
1861
0
}
1862
1863
0
Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
1864
0
  Function *Callee = CI->getCalledFunction();
1865
  // Require one fixed pointer argument and an integer/void result.
1866
0
  FunctionType *FT = Callee->getFunctionType();
1867
0
  if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1868
0
      !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1869
0
    return nullptr;
1870
1871
  // Check for a constant string.
1872
0
  StringRef Str;
1873
0
  if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1874
0
    return nullptr;
1875
1876
0
  if (Str.empty() && CI->use_empty()) {
1877
    // puts("") -> putchar('\n')
1878
0
    Value *Res = EmitPutChar(B.getInt32('\n'), B, TLI);
1879
0
    if (CI->use_empty() || !Res)
1880
0
      return Res;
1881
0
    return B.CreateIntCast(Res, CI->getType(), true);
1882
0
  }
1883
1884
0
  return nullptr;
1885
0
}
1886
1887
0
bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {
1888
0
  LibFunc::Func Func;
1889
0
  SmallString<20> FloatFuncName = FuncName;
1890
0
  FloatFuncName += 'f';
1891
0
  if (TLI->getLibFunc(FloatFuncName, Func))
1892
0
    return TLI->has(Func);
1893
0
  return false;
1894
0
}
1895
1896
Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
1897
0
                                                      IRBuilder<> &Builder) {
1898
0
  LibFunc::Func Func;
1899
0
  Function *Callee = CI->getCalledFunction();
1900
0
  StringRef FuncName = Callee->getName();
1901
1902
  // Check for string/memory library functions.
1903
0
  if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
1904
    // Make sure we never change the calling convention.
1905
0
    assert((ignoreCallingConv(Func) ||
1906
0
            CI->getCallingConv() == llvm::CallingConv::C) &&
1907
0
      "Optimizing string/memory libcall would change the calling convention");
1908
0
    switch (Func) {
1909
0
    case LibFunc::strcat:
1910
0
      return optimizeStrCat(CI, Builder);
1911
0
    case LibFunc::strncat:
1912
0
      return optimizeStrNCat(CI, Builder);
1913
0
    case LibFunc::strchr:
1914
0
      return optimizeStrChr(CI, Builder);
1915
0
    case LibFunc::strrchr:
1916
0
      return optimizeStrRChr(CI, Builder);
1917
0
    case LibFunc::strcmp:
1918
0
      return optimizeStrCmp(CI, Builder);
1919
0
    case LibFunc::strncmp:
1920
0
      return optimizeStrNCmp(CI, Builder);
1921
0
    case LibFunc::strcpy:
1922
0
      return optimizeStrCpy(CI, Builder);
1923
0
    case LibFunc::stpcpy:
1924
0
      return optimizeStpCpy(CI, Builder);
1925
0
    case LibFunc::strncpy:
1926
0
      return optimizeStrNCpy(CI, Builder);
1927
0
    case LibFunc::strlen:
1928
0
      return optimizeStrLen(CI, Builder);
1929
0
    case LibFunc::strpbrk:
1930
0
      return optimizeStrPBrk(CI, Builder);
1931
0
    case LibFunc::strtol:
1932
0
    case LibFunc::strtod:
1933
0
    case LibFunc::strtof:
1934
0
    case LibFunc::strtoul:
1935
0
    case LibFunc::strtoll:
1936
0
    case LibFunc::strtold:
1937
0
    case LibFunc::strtoull:
1938
0
      return optimizeStrTo(CI, Builder);
1939
0
    case LibFunc::strspn:
1940
0
      return optimizeStrSpn(CI, Builder);
1941
0
    case LibFunc::strcspn:
1942
0
      return optimizeStrCSpn(CI, Builder);
1943
0
    case LibFunc::strstr:
1944
0
      return optimizeStrStr(CI, Builder);
1945
0
    case LibFunc::memchr:
1946
0
      return optimizeMemChr(CI, Builder);
1947
0
    case LibFunc::memcmp:
1948
0
      return optimizeMemCmp(CI, Builder);
1949
0
    case LibFunc::memcpy:
1950
0
      return optimizeMemCpy(CI, Builder);
1951
0
    case LibFunc::memmove:
1952
0
      return optimizeMemMove(CI, Builder);
1953
0
    case LibFunc::memset:
1954
0
      return optimizeMemSet(CI, Builder);
1955
0
    default:
1956
0
      break;
1957
0
    }
1958
0
  }
1959
0
  return nullptr;
1960
0
}
1961
1962
3.61M
Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1963
3.61M
  if (CI->isNoBuiltin())
1964
0
    return nullptr;
1965
1966
3.61M
  LibFunc::Func Func;
1967
3.61M
  Function *Callee = CI->getCalledFunction();
1968
3.61M
  StringRef FuncName = Callee->getName();
1969
3.61M
  IRBuilder<> Builder(CI);
1970
3.61M
  bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
1971
1972
  // Command-line parameter overrides function attribute.
1973
3.61M
  if (false) // HLSL Change - EnableUnsafeFPShrink.getNumOccurrences() > 0)
1974
0
    UnsafeFPShrink = EnableUnsafeFPShrink;
1975
3.61M
  else if (Callee->hasFnAttribute("unsafe-fp-math")) {
1976
    // FIXME: This is the same problem as described in optimizeSqrt().
1977
    // If calls gain access to IR-level FMF, then use that instead of a
1978
    // function attribute.
1979
1980
    // Check for unsafe-fp-math = true.
1981
0
    Attribute Attr = Callee->getFnAttribute("unsafe-fp-math");
1982
0
    if (Attr.getValueAsString() == "true")
1983
0
      UnsafeFPShrink = true;
1984
0
  }
1985
1986
  // First, check for intrinsics.
1987
3.61M
  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
1988
1.00M
    if (!isCallingConvC)
1989
0
      return nullptr;
1990
1.00M
    switch (II->getIntrinsicID()) {
1991
0
    case Intrinsic::pow:
1992
0
      return optimizePow(CI, Builder);
1993
0
    case Intrinsic::exp2:
1994
0
      return optimizeExp2(CI, Builder);
1995
0
    case Intrinsic::fabs:
1996
0
      return optimizeFabs(CI, Builder);
1997
0
    case Intrinsic::sqrt:
1998
0
      return optimizeSqrt(CI, Builder);
1999
1.00M
    default:
2000
1.00M
      return nullptr;
2001
1.00M
    }
2002
1.00M
  }
2003
2004
  // Also try to simplify calls to fortified library functions.
2005
2.60M
  if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) {
2006
    // Try to further simplify the result.
2007
0
    CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
2008
0
    if (SimplifiedCI && SimplifiedCI->getCalledFunction())
2009
0
      if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) {
2010
        // If we were able to further simplify, remove the now redundant call.
2011
0
        SimplifiedCI->replaceAllUsesWith(V);
2012
0
        SimplifiedCI->eraseFromParent();
2013
0
        return V;
2014
0
      }
2015
0
    return SimplifiedFortifiedCI;
2016
0
  }
2017
2018
  // Then check for known library functions.
2019
2.60M
  if (TLI->getLibFunc(FuncName, Func) && 
TLI->has(Func)0
) {
2020
    // We never change the calling convention.
2021
0
    if (!ignoreCallingConv(Func) && !isCallingConvC)
2022
0
      return nullptr;
2023
0
    if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
2024
0
      return V;
2025
0
    switch (Func) {
2026
0
    case LibFunc::cosf:
2027
0
    case LibFunc::cos:
2028
0
    case LibFunc::cosl:
2029
0
      return optimizeCos(CI, Builder);
2030
0
    case LibFunc::sinpif:
2031
0
    case LibFunc::sinpi:
2032
0
    case LibFunc::cospif:
2033
0
    case LibFunc::cospi:
2034
0
      return optimizeSinCosPi(CI, Builder);
2035
0
    case LibFunc::powf:
2036
0
    case LibFunc::pow:
2037
0
    case LibFunc::powl:
2038
0
      return optimizePow(CI, Builder);
2039
0
    case LibFunc::exp2l:
2040
0
    case LibFunc::exp2:
2041
0
    case LibFunc::exp2f:
2042
0
      return optimizeExp2(CI, Builder);
2043
0
    case LibFunc::fabsf:
2044
0
    case LibFunc::fabs:
2045
0
    case LibFunc::fabsl:
2046
0
      return optimizeFabs(CI, Builder);
2047
0
    case LibFunc::sqrtf:
2048
0
    case LibFunc::sqrt:
2049
0
    case LibFunc::sqrtl:
2050
0
      return optimizeSqrt(CI, Builder);
2051
0
    case LibFunc::ffs:
2052
0
    case LibFunc::ffsl:
2053
0
    case LibFunc::ffsll:
2054
0
      return optimizeFFS(CI, Builder);
2055
0
    case LibFunc::abs:
2056
0
    case LibFunc::labs:
2057
0
    case LibFunc::llabs:
2058
0
      return optimizeAbs(CI, Builder);
2059
0
    case LibFunc::isdigit:
2060
0
      return optimizeIsDigit(CI, Builder);
2061
0
    case LibFunc::isascii:
2062
0
      return optimizeIsAscii(CI, Builder);
2063
0
    case LibFunc::toascii:
2064
0
      return optimizeToAscii(CI, Builder);
2065
0
    case LibFunc::printf:
2066
0
      return optimizePrintF(CI, Builder);
2067
0
    case LibFunc::sprintf:
2068
0
      return optimizeSPrintF(CI, Builder);
2069
0
    case LibFunc::fprintf:
2070
0
      return optimizeFPrintF(CI, Builder);
2071
0
    case LibFunc::fwrite:
2072
0
      return optimizeFWrite(CI, Builder);
2073
0
    case LibFunc::fputs:
2074
0
      return optimizeFPuts(CI, Builder);
2075
0
    case LibFunc::puts:
2076
0
      return optimizePuts(CI, Builder);
2077
0
    case LibFunc::perror:
2078
0
      return optimizeErrorReporting(CI, Builder);
2079
0
    case LibFunc::vfprintf:
2080
0
    case LibFunc::fiprintf:
2081
0
      return optimizeErrorReporting(CI, Builder, 0);
2082
0
    case LibFunc::fputc:
2083
0
      return optimizeErrorReporting(CI, Builder, 1);
2084
0
    case LibFunc::ceil:
2085
0
    case LibFunc::floor:
2086
0
    case LibFunc::rint:
2087
0
    case LibFunc::round:
2088
0
    case LibFunc::nearbyint:
2089
0
    case LibFunc::trunc:
2090
0
      if (hasFloatVersion(FuncName))
2091
0
        return optimizeUnaryDoubleFP(CI, Builder, false);
2092
0
      return nullptr;
2093
0
    case LibFunc::acos:
2094
0
    case LibFunc::acosh:
2095
0
    case LibFunc::asin:
2096
0
    case LibFunc::asinh:
2097
0
    case LibFunc::atan:
2098
0
    case LibFunc::atanh:
2099
0
    case LibFunc::cbrt:
2100
0
    case LibFunc::cosh:
2101
0
    case LibFunc::exp:
2102
0
    case LibFunc::exp10:
2103
0
    case LibFunc::expm1:
2104
0
    case LibFunc::log:
2105
0
    case LibFunc::log10:
2106
0
    case LibFunc::log1p:
2107
0
    case LibFunc::log2:
2108
0
    case LibFunc::logb:
2109
0
    case LibFunc::sin:
2110
0
    case LibFunc::sinh:
2111
0
    case LibFunc::tan:
2112
0
    case LibFunc::tanh:
2113
0
      if (UnsafeFPShrink && hasFloatVersion(FuncName))
2114
0
        return optimizeUnaryDoubleFP(CI, Builder, true);
2115
0
      return nullptr;
2116
0
    case LibFunc::copysign:
2117
0
    case LibFunc::fmin:
2118
0
    case LibFunc::fmax:
2119
0
      if (hasFloatVersion(FuncName))
2120
0
        return optimizeBinaryDoubleFP(CI, Builder);
2121
0
      return nullptr;
2122
0
    default:
2123
0
      return nullptr;
2124
0
    }
2125
0
  }
2126
2.60M
  return nullptr;
2127
2.60M
}
2128
2129
LibCallSimplifier::LibCallSimplifier(
2130
    const DataLayout &DL, const TargetLibraryInfo *TLI,
2131
    function_ref<void(Instruction *, Value *)> Replacer)
2132
    : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), UnsafeFPShrink(false),
2133
3.61M
      Replacer(Replacer) {}
2134
2135
0
void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
2136
  // Indirect through the replacer used in this instance.
2137
0
  Replacer(I, With);
2138
0
}
2139
2140
/*static*/ void LibCallSimplifier::replaceAllUsesWithDefault(Instruction *I,
2141
0
                                                             Value *With) {
2142
0
  I->replaceAllUsesWith(With);
2143
0
  I->eraseFromParent();
2144
0
}
2145
2146
// TODO:
2147
//   Additional cases that we need to add to this file:
2148
//
2149
// cbrt:
2150
//   * cbrt(expN(X))  -> expN(x/3)
2151
//   * cbrt(sqrt(x))  -> pow(x,1/6)
2152
//   * cbrt(sqrt(x))  -> pow(x,1/9)
2153
//
2154
// exp, expf, expl:
2155
//   * exp(log(x))  -> x
2156
//
2157
// log, logf, logl:
2158
//   * log(exp(x))   -> x
2159
//   * log(x**y)     -> y*log(x)
2160
//   * log(exp(y))   -> y*log(e)
2161
//   * log(exp2(y))  -> y*log(2)
2162
//   * log(exp10(y)) -> y*log(10)
2163
//   * log(sqrt(x))  -> 0.5*log(x)
2164
//   * log(pow(x,y)) -> y*log(x)
2165
//
2166
// lround, lroundf, lroundl:
2167
//   * lround(cnst) -> cnst'
2168
//
2169
// pow, powf, powl:
2170
//   * pow(exp(x),y)  -> exp(x*y)
2171
//   * pow(sqrt(x),y) -> pow(x,y*0.5)
2172
//   * pow(pow(x,y),z)-> pow(x,y*z)
2173
//
2174
// round, roundf, roundl:
2175
//   * round(cnst) -> cnst'
2176
//
2177
// signbit:
2178
//   * signbit(cnst) -> cnst'
2179
//   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2180
//
2181
// sqrt, sqrtf, sqrtl:
2182
//   * sqrt(expN(x))  -> expN(x*0.5)
2183
//   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2184
//   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2185
//
2186
// tan, tanf, tanl:
2187
//   * tan(atan(x)) -> x
2188
//
2189
// trunc, truncf, truncl:
2190
//   * trunc(cnst) -> cnst'
2191
//
2192
//
2193
2194
//===----------------------------------------------------------------------===//
2195
// Fortified Library Call Optimizations
2196
//===----------------------------------------------------------------------===//
2197
2198
bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI,
2199
                                                         unsigned ObjSizeOp,
2200
                                                         unsigned SizeOp,
2201
0
                                                         bool isString) {
2202
0
  if (CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(SizeOp))
2203
0
    return true;
2204
0
  if (ConstantInt *ObjSizeCI =
2205
0
          dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
2206
0
    if (ObjSizeCI->isAllOnesValue())
2207
0
      return true;
2208
    // If the object size wasn't -1 (unknown), bail out if we were asked to.
2209
0
    if (OnlyLowerUnknownSize)
2210
0
      return false;
2211
0
    if (isString) {
2212
0
      uint64_t Len = GetStringLength(CI->getArgOperand(SizeOp));
2213
      // If the length is 0 we don't know how long it is and so we can't
2214
      // remove the check.
2215
0
      if (Len == 0)
2216
0
        return false;
2217
0
      return ObjSizeCI->getZExtValue() >= Len;
2218
0
    }
2219
0
    if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getArgOperand(SizeOp)))
2220
0
      return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
2221
0
  }
2222
0
  return false;
2223
0
}
2224
2225
0
Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B) {
2226
0
  Function *Callee = CI->getCalledFunction();
2227
2228
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memcpy_chk))
2229
0
    return nullptr;
2230
2231
0
  if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2232
0
    B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2233
0
                   CI->getArgOperand(2), 1);
2234
0
    return CI->getArgOperand(0);
2235
0
  }
2236
0
  return nullptr;
2237
0
}
2238
2239
0
Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B) {
2240
0
  Function *Callee = CI->getCalledFunction();
2241
2242
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memmove_chk))
2243
0
    return nullptr;
2244
2245
0
  if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2246
0
    B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
2247
0
                    CI->getArgOperand(2), 1);
2248
0
    return CI->getArgOperand(0);
2249
0
  }
2250
0
  return nullptr;
2251
0
}
2252
2253
0
Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, IRBuilder<> &B) {
2254
0
  Function *Callee = CI->getCalledFunction();
2255
2256
0
  if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memset_chk))
2257
0
    return nullptr;
2258
2259
0
  if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2260
0
    Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
2261
0
    B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
2262
0
    return CI->getArgOperand(0);
2263
0
  }
2264
0
  return nullptr;
2265
0
}
2266
2267
Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
2268
                                                      IRBuilder<> &B,
2269
0
                                                      LibFunc::Func Func) {
2270
0
  Function *Callee = CI->getCalledFunction();
2271
0
  StringRef Name = Callee->getName();
2272
0
  const DataLayout &DL = CI->getModule()->getDataLayout();
2273
2274
0
  if (!checkStringCopyLibFuncSignature(Callee, Func))
2275
0
    return nullptr;
2276
2277
0
  Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
2278
0
        *ObjSize = CI->getArgOperand(2);
2279
2280
  // __stpcpy_chk(x,x,...)  -> x+strlen(x)
2281
0
  if (Func == LibFunc::stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
2282
0
    Value *StrLen = EmitStrLen(Src, B, DL, TLI);
2283
0
    return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
2284
0
  }
2285
2286
  // If a) we don't have any length information, or b) we know this will
2287
  // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
2288
  // st[rp]cpy_chk call which may fail at runtime if the size is too long.
2289
  // TODO: It might be nice to get a maximum length out of the possible
2290
  // string lengths for varying.
2291
0
  if (isFortifiedCallFoldable(CI, 2, 1, true))
2292
0
    return EmitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
2293
2294
0
  if (OnlyLowerUnknownSize)
2295
0
    return nullptr;
2296
2297
  // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
2298
0
  uint64_t Len = GetStringLength(Src);
2299
0
  if (Len == 0)
2300
0
    return nullptr;
2301
2302
0
  Type *SizeTTy = DL.getIntPtrType(CI->getContext());
2303
0
  Value *LenV = ConstantInt::get(SizeTTy, Len);
2304
0
  Value *Ret = EmitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
2305
  // If the function was an __stpcpy_chk, and we were able to fold it into
2306
  // a __memcpy_chk, we still need to return the correct end pointer.
2307
0
  if (Ret && Func == LibFunc::stpcpy_chk)
2308
0
    return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1));
2309
0
  return Ret;
2310
0
}
2311
2312
Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
2313
                                                       IRBuilder<> &B,
2314
0
                                                       LibFunc::Func Func) {
2315
0
  Function *Callee = CI->getCalledFunction();
2316
0
  StringRef Name = Callee->getName();
2317
2318
0
  if (!checkStringCopyLibFuncSignature(Callee, Func))
2319
0
    return nullptr;
2320
0
  if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2321
0
    Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2322
0
                             CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
2323
0
    return Ret;
2324
0
  }
2325
0
  return nullptr;
2326
0
}
2327
2328
2.60M
Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) {
2329
  // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
2330
  // Some clang users checked for _chk libcall availability using:
2331
  //   __has_builtin(__builtin___memcpy_chk)
2332
  // When compiling with -fno-builtin, this is always true.
2333
  // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
2334
  // end up with fortified libcalls, which isn't acceptable in a freestanding
2335
  // environment which only provides their non-fortified counterparts.
2336
  //
2337
  // Until we change clang and/or teach external users to check for availability
2338
  // differently, disregard the "nobuiltin" attribute and TLI::has.
2339
  //
2340
  // PR23093.
2341
2342
2.60M
  LibFunc::Func Func;
2343
2.60M
  Function *Callee = CI->getCalledFunction();
2344
2.60M
  StringRef FuncName = Callee->getName();
2345
2.60M
  IRBuilder<> Builder(CI);
2346
2.60M
  bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
2347
2348
  // First, check that this is a known library functions.
2349
2.60M
  if (!TLI->getLibFunc(FuncName, Func))
2350
2.60M
    return nullptr;
2351
2352
  // We never change the calling convention.
2353
0
  if (!ignoreCallingConv(Func) && !isCallingConvC)
2354
0
    return nullptr;
2355
2356
0
  switch (Func) {
2357
0
  case LibFunc::memcpy_chk:
2358
0
    return optimizeMemCpyChk(CI, Builder);
2359
0
  case LibFunc::memmove_chk:
2360
0
    return optimizeMemMoveChk(CI, Builder);
2361
0
  case LibFunc::memset_chk:
2362
0
    return optimizeMemSetChk(CI, Builder);
2363
0
  case LibFunc::stpcpy_chk:
2364
0
  case LibFunc::strcpy_chk:
2365
0
    return optimizeStrpCpyChk(CI, Builder, Func);
2366
0
  case LibFunc::stpncpy_chk:
2367
0
  case LibFunc::strncpy_chk:
2368
0
    return optimizeStrpNCpyChk(CI, Builder, Func);
2369
0
  default:
2370
0
    break;
2371
0
  }
2372
0
  return nullptr;
2373
0
}
2374
2375
FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
2376
    const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
2377
3.61M
    : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}