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