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