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