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