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