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