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