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