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