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