Meador Inge | 5e89045 | 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 | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/IRBuilder.h" |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Intrinsics.h" |
| 27 | #include "llvm/IR/LLVMContext.h" |
| 28 | #include "llvm/IR/Module.h" |
Nadav Rotem | f26b4f0 | 2013-02-27 05:53:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Allocator.h" |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetLibraryInfo.h" |
| 32 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 36 | static cl::opt<bool> |
| 37 | ColdErrorCalls("error-reporting-is-cold", cl::init(true), |
| 38 | cl::Hidden, cl::desc("Treat error-reporting calls as cold")); |
| 39 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 40 | /// This class is the abstract base class for the set of optimizations that |
| 41 | /// corresponds to one library call. |
| 42 | namespace { |
| 43 | class LibCallOptimization { |
| 44 | protected: |
| 45 | Function *Caller; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 46 | const DataLayout *DL; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 47 | const TargetLibraryInfo *TLI; |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 48 | const LibCallSimplifier *LCS; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 49 | LLVMContext* Context; |
| 50 | public: |
| 51 | LibCallOptimization() { } |
| 52 | virtual ~LibCallOptimization() {} |
| 53 | |
| 54 | /// callOptimizer - This pure virtual method is implemented by base classes to |
| 55 | /// do various optimizations. If this returns null then no transformation was |
| 56 | /// performed. If it returns CI, then it transformed the call and CI is to be |
| 57 | /// deleted. If it returns something else, replace CI with the new value and |
| 58 | /// delete CI. |
| 59 | virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) |
| 60 | =0; |
| 61 | |
Chad Rosier | 33daeab | 2013-02-08 18:00:14 +0000 | [diff] [blame] | 62 | /// ignoreCallingConv - Returns false if this transformation could possibly |
| 63 | /// change the calling convention. |
| 64 | virtual bool ignoreCallingConv() { return false; } |
| 65 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 66 | Value *optimizeCall(CallInst *CI, const DataLayout *DL, |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 67 | const TargetLibraryInfo *TLI, |
| 68 | const LibCallSimplifier *LCS, IRBuilder<> &B) { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 69 | Caller = CI->getParent()->getParent(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 70 | this->DL = DL; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 71 | this->TLI = TLI; |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 72 | this->LCS = LCS; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 73 | if (CI->getCalledFunction()) |
| 74 | Context = &CI->getCalledFunction()->getContext(); |
| 75 | |
| 76 | // We never change the calling convention. |
Chad Rosier | 33daeab | 2013-02-08 18:00:14 +0000 | [diff] [blame] | 77 | if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 78 | return NULL; |
| 79 | |
| 80 | return callOptimizer(CI->getCalledFunction(), CI, B); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
Meador Inge | 57cfd71 | 2012-10-31 03:33:06 +0000 | [diff] [blame] | 85 | // Helper Functions |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 89 | /// value is equal or not-equal to zero. |
| 90 | static bool isOnlyUsedInZeroEqualityComparison(Value *V) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 91 | for (User *U : V->users()) { |
| 92 | if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) |
Meador Inge | 57cfd71 | 2012-10-31 03:33:06 +0000 | [diff] [blame] | 93 | if (IC->isEquality()) |
| 94 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 95 | if (C->isNullValue()) |
| 96 | continue; |
| 97 | // Unknown instruction. |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 103 | /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality |
| 104 | /// comparisons with With. |
| 105 | static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 106 | for (User *U : V->users()) { |
| 107 | if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 108 | if (IC->isEquality() && IC->getOperand(1) == With) |
| 109 | continue; |
| 110 | // Unknown instruction. |
| 111 | return false; |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 116 | static bool callHasFloatingPointArgument(const CallInst *CI) { |
| 117 | for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end(); |
| 118 | it != e; ++it) { |
| 119 | if ((*it)->getType()->isFloatingPointTy()) |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
Benjamin Kramer | bc87003 | 2013-08-31 18:19:35 +0000 | [diff] [blame] | 125 | /// \brief Check whether the overloaded unary floating point function |
| 126 | /// corresponing to \a Ty is available. |
| 127 | static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, |
| 128 | LibFunc::Func DoubleFn, LibFunc::Func FloatFn, |
| 129 | LibFunc::Func LongDoubleFn) { |
| 130 | switch (Ty->getTypeID()) { |
| 131 | case Type::FloatTyID: |
| 132 | return TLI->has(FloatFn); |
| 133 | case Type::DoubleTyID: |
| 134 | return TLI->has(DoubleFn); |
| 135 | default: |
| 136 | return TLI->has(LongDoubleFn); |
| 137 | } |
| 138 | } |
| 139 | |
Meador Inge | 57cfd71 | 2012-10-31 03:33:06 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 141 | // Fortified Library Call Optimizations |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
| 144 | struct FortifiedLibCallOptimization : public LibCallOptimization { |
| 145 | protected: |
| 146 | virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, |
| 147 | bool isString) const = 0; |
| 148 | }; |
| 149 | |
| 150 | struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization { |
| 151 | CallInst *CI; |
| 152 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 153 | bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, |
| 154 | bool isString) const override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 155 | if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp)) |
| 156 | return true; |
| 157 | if (ConstantInt *SizeCI = |
| 158 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) { |
| 159 | if (SizeCI->isAllOnesValue()) |
| 160 | return true; |
| 161 | if (isString) { |
| 162 | uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp)); |
| 163 | // If the length is 0 we don't know how long it is and so we can't |
| 164 | // remove the check. |
| 165 | if (Len == 0) return false; |
| 166 | return SizeCI->getZExtValue() >= Len; |
| 167 | } |
| 168 | if (ConstantInt *Arg = dyn_cast<ConstantInt>( |
| 169 | CI->getArgOperand(SizeArgOp))) |
| 170 | return SizeCI->getZExtValue() >= Arg->getZExtValue(); |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | struct MemCpyChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 177 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 178 | IRBuilder<> &B) override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 179 | this->CI = CI; |
| 180 | FunctionType *FT = Callee->getFunctionType(); |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 181 | LLVMContext &Context = CI->getParent()->getContext(); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 182 | |
| 183 | // Check if this has the right signature. |
| 184 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 185 | !FT->getParamType(0)->isPointerTy() || |
| 186 | !FT->getParamType(1)->isPointerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 187 | FT->getParamType(2) != DL->getIntPtrType(Context) || |
| 188 | FT->getParamType(3) != DL->getIntPtrType(Context)) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 189 | return 0; |
| 190 | |
| 191 | if (isFoldable(3, 2, false)) { |
| 192 | B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
| 193 | CI->getArgOperand(2), 1); |
| 194 | return CI->getArgOperand(0); |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | struct MemMoveChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 201 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 202 | IRBuilder<> &B) override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 203 | this->CI = CI; |
| 204 | FunctionType *FT = Callee->getFunctionType(); |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 205 | LLVMContext &Context = CI->getParent()->getContext(); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 206 | |
| 207 | // Check if this has the right signature. |
| 208 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 209 | !FT->getParamType(0)->isPointerTy() || |
| 210 | !FT->getParamType(1)->isPointerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 211 | FT->getParamType(2) != DL->getIntPtrType(Context) || |
| 212 | FT->getParamType(3) != DL->getIntPtrType(Context)) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 213 | return 0; |
| 214 | |
| 215 | if (isFoldable(3, 2, false)) { |
| 216 | B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), |
| 217 | CI->getArgOperand(2), 1); |
| 218 | return CI->getArgOperand(0); |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | struct MemSetChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 225 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 226 | IRBuilder<> &B) override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 227 | this->CI = CI; |
| 228 | FunctionType *FT = Callee->getFunctionType(); |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 229 | LLVMContext &Context = CI->getParent()->getContext(); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 230 | |
| 231 | // Check if this has the right signature. |
| 232 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 233 | !FT->getParamType(0)->isPointerTy() || |
| 234 | !FT->getParamType(1)->isIntegerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 235 | FT->getParamType(2) != DL->getIntPtrType(Context) || |
| 236 | FT->getParamType(3) != DL->getIntPtrType(Context)) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | |
| 239 | if (isFoldable(3, 2, false)) { |
| 240 | Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), |
| 241 | false); |
| 242 | B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); |
| 243 | return CI->getArgOperand(0); |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | struct StrCpyChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 250 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 251 | IRBuilder<> &B) override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 252 | this->CI = CI; |
| 253 | StringRef Name = Callee->getName(); |
| 254 | FunctionType *FT = Callee->getFunctionType(); |
| 255 | LLVMContext &Context = CI->getParent()->getContext(); |
| 256 | |
| 257 | // Check if this has the right signature. |
| 258 | if (FT->getNumParams() != 3 || |
| 259 | FT->getReturnType() != FT->getParamType(0) || |
| 260 | FT->getParamType(0) != FT->getParamType(1) || |
| 261 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 262 | FT->getParamType(2) != DL->getIntPtrType(Context)) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 263 | return 0; |
| 264 | |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 265 | Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); |
| 266 | if (Dst == Src) // __strcpy_chk(x,x) -> x |
| 267 | return Src; |
| 268 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 269 | // If a) we don't have any length information, or b) we know this will |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 270 | // fit then just lower to a plain strcpy. Otherwise we'll keep our |
| 271 | // strcpy_chk call which may fail at runtime if the size is too long. |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 272 | // TODO: It might be nice to get a maximum length out of the possible |
| 273 | // string lengths for varying. |
| 274 | if (isFoldable(2, 1, true)) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 275 | Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6)); |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 276 | return Ret; |
| 277 | } else { |
| 278 | // Maybe we can stil fold __strcpy_chk to __memcpy_chk. |
| 279 | uint64_t Len = GetStringLength(Src); |
| 280 | if (Len == 0) return 0; |
| 281 | |
| 282 | // This optimization require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 283 | if (!DL) return 0; |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 284 | |
| 285 | Value *Ret = |
| 286 | EmitMemCpyChk(Dst, Src, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 287 | ConstantInt::get(DL->getIntPtrType(Context), Len), |
| 288 | CI->getArgOperand(2), B, DL, TLI); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 289 | return Ret; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | }; |
| 294 | |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 295 | struct StpCpyChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 296 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 297 | IRBuilder<> &B) override { |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 298 | this->CI = CI; |
| 299 | StringRef Name = Callee->getName(); |
| 300 | FunctionType *FT = Callee->getFunctionType(); |
| 301 | LLVMContext &Context = CI->getParent()->getContext(); |
| 302 | |
| 303 | // Check if this has the right signature. |
| 304 | if (FT->getNumParams() != 3 || |
| 305 | FT->getReturnType() != FT->getParamType(0) || |
| 306 | FT->getParamType(0) != FT->getParamType(1) || |
| 307 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 308 | FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0))) |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 309 | return 0; |
| 310 | |
| 311 | Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); |
| 312 | if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 313 | Value *StrLen = EmitStrLen(Src, B, DL, TLI); |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 314 | return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0; |
| 315 | } |
| 316 | |
| 317 | // If a) we don't have any length information, or b) we know this will |
| 318 | // fit then just lower to a plain stpcpy. Otherwise we'll keep our |
| 319 | // stpcpy_chk call which may fail at runtime if the size is too long. |
| 320 | // TODO: It might be nice to get a maximum length out of the possible |
| 321 | // string lengths for varying. |
| 322 | if (isFoldable(2, 1, true)) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 323 | Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6)); |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 324 | return Ret; |
| 325 | } else { |
| 326 | // Maybe we can stil fold __stpcpy_chk to __memcpy_chk. |
| 327 | uint64_t Len = GetStringLength(Src); |
| 328 | if (Len == 0) return 0; |
| 329 | |
| 330 | // This optimization require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 331 | if (!DL) return 0; |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 332 | |
| 333 | Type *PT = FT->getParamType(0); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 334 | Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len); |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 335 | Value *DstEnd = B.CreateGEP(Dst, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 336 | ConstantInt::get(DL->getIntPtrType(PT), |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 337 | Len - 1)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 338 | if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI)) |
Meador Inge | fa9d137 | 2012-10-31 00:20:51 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | return DstEnd; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | }; |
| 345 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 346 | struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 347 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 348 | IRBuilder<> &B) override { |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 349 | this->CI = CI; |
| 350 | StringRef Name = Callee->getName(); |
| 351 | FunctionType *FT = Callee->getFunctionType(); |
| 352 | LLVMContext &Context = CI->getParent()->getContext(); |
| 353 | |
| 354 | // Check if this has the right signature. |
| 355 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 356 | FT->getParamType(0) != FT->getParamType(1) || |
| 357 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
| 358 | !FT->getParamType(2)->isIntegerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 359 | FT->getParamType(3) != DL->getIntPtrType(Context)) |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | |
| 362 | if (isFoldable(3, 2, false)) { |
| 363 | Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 364 | CI->getArgOperand(2), B, DL, TLI, |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 365 | Name.substr(2, 7)); |
| 366 | return Ret; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | }; |
| 371 | |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
| 373 | // String and Memory Library Call Optimizations |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | |
| 376 | struct StrCatOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 377 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 378 | IRBuilder<> &B) override { |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 379 | // Verify the "strcat" function prototype. |
| 380 | FunctionType *FT = Callee->getFunctionType(); |
| 381 | if (FT->getNumParams() != 2 || |
| 382 | FT->getReturnType() != B.getInt8PtrTy() || |
| 383 | FT->getParamType(0) != FT->getReturnType() || |
| 384 | FT->getParamType(1) != FT->getReturnType()) |
| 385 | return 0; |
| 386 | |
| 387 | // Extract some information from the instruction |
| 388 | Value *Dst = CI->getArgOperand(0); |
| 389 | Value *Src = CI->getArgOperand(1); |
| 390 | |
| 391 | // See if we can get the length of the input string. |
| 392 | uint64_t Len = GetStringLength(Src); |
| 393 | if (Len == 0) return 0; |
| 394 | --Len; // Unbias length. |
| 395 | |
| 396 | // Handle the simple, do-nothing case: strcat(x, "") -> x |
| 397 | if (Len == 0) |
| 398 | return Dst; |
| 399 | |
| 400 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 401 | if (!DL) return 0; |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 402 | |
| 403 | return emitStrLenMemCpy(Src, Dst, Len, B); |
| 404 | } |
| 405 | |
| 406 | Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, |
| 407 | IRBuilder<> &B) { |
| 408 | // We need to find the end of the destination string. That's where the |
| 409 | // memory is to be moved to. We just generate a call to strlen. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 410 | Value *DstLen = EmitStrLen(Dst, B, DL, TLI); |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 411 | if (!DstLen) |
| 412 | return 0; |
| 413 | |
| 414 | // Now that we have the destination's length, we must index into the |
| 415 | // destination's pointer to get the actual memcpy destination (end of |
| 416 | // the string .. we're concatenating). |
| 417 | Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr"); |
| 418 | |
| 419 | // We have enough information to now generate the memcpy call to do the |
| 420 | // concatenation for us. Make a memcpy to copy the nul byte with align = 1. |
| 421 | B.CreateMemCpy(CpyDst, Src, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 422 | ConstantInt::get(DL->getIntPtrType(*Context), Len + 1), 1); |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 423 | return Dst; |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | struct StrNCatOpt : public StrCatOpt { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 428 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 429 | IRBuilder<> &B) override { |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 430 | // Verify the "strncat" function prototype. |
| 431 | FunctionType *FT = Callee->getFunctionType(); |
| 432 | if (FT->getNumParams() != 3 || |
| 433 | FT->getReturnType() != B.getInt8PtrTy() || |
| 434 | FT->getParamType(0) != FT->getReturnType() || |
| 435 | FT->getParamType(1) != FT->getReturnType() || |
| 436 | !FT->getParamType(2)->isIntegerTy()) |
| 437 | return 0; |
| 438 | |
| 439 | // Extract some information from the instruction |
| 440 | Value *Dst = CI->getArgOperand(0); |
| 441 | Value *Src = CI->getArgOperand(1); |
| 442 | uint64_t Len; |
| 443 | |
| 444 | // We don't do anything if length is not constant |
| 445 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2))) |
| 446 | Len = LengthArg->getZExtValue(); |
| 447 | else |
| 448 | return 0; |
| 449 | |
| 450 | // See if we can get the length of the input string. |
| 451 | uint64_t SrcLen = GetStringLength(Src); |
| 452 | if (SrcLen == 0) return 0; |
| 453 | --SrcLen; // Unbias length. |
| 454 | |
| 455 | // Handle the simple, do-nothing cases: |
| 456 | // strncat(x, "", c) -> x |
| 457 | // strncat(x, c, 0) -> x |
| 458 | if (SrcLen == 0 || Len == 0) return Dst; |
| 459 | |
| 460 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 461 | if (!DL) return 0; |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 462 | |
| 463 | // We don't optimize this case |
| 464 | if (Len < SrcLen) return 0; |
| 465 | |
| 466 | // strncat(x, s, c) -> strcat(x, s) |
| 467 | // s is constant so the strcat can be optimized further |
| 468 | return emitStrLenMemCpy(Src, Dst, SrcLen, B); |
| 469 | } |
| 470 | }; |
| 471 | |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 472 | struct StrChrOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 473 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 474 | IRBuilder<> &B) override { |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 475 | // Verify the "strchr" function prototype. |
| 476 | FunctionType *FT = Callee->getFunctionType(); |
| 477 | if (FT->getNumParams() != 2 || |
| 478 | FT->getReturnType() != B.getInt8PtrTy() || |
| 479 | FT->getParamType(0) != FT->getReturnType() || |
| 480 | !FT->getParamType(1)->isIntegerTy(32)) |
| 481 | return 0; |
| 482 | |
| 483 | Value *SrcStr = CI->getArgOperand(0); |
| 484 | |
| 485 | // If the second operand is non-constant, see if we can compute the length |
| 486 | // of the input string and turn this into memchr. |
| 487 | ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 488 | if (CharC == 0) { |
| 489 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 490 | if (!DL) return 0; |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 491 | |
| 492 | uint64_t Len = GetStringLength(SrcStr); |
| 493 | if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32. |
| 494 | return 0; |
| 495 | |
| 496 | return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 497 | ConstantInt::get(DL->getIntPtrType(*Context), Len), |
| 498 | B, DL, TLI); |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Otherwise, the character is a constant, see if the first argument is |
| 502 | // a string literal. If so, we can constant fold. |
| 503 | StringRef Str; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 504 | if (!getConstantStringInfo(SrcStr, Str)) { |
| 505 | if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p) |
| 506 | return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr"); |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 507 | return 0; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 508 | } |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 509 | |
| 510 | // Compute the offset, make sure to handle the case when we're searching for |
| 511 | // zero (a weird way to spell strlen). |
Yunzhong Gao | dfdf7f4 | 2013-08-21 22:11:15 +0000 | [diff] [blame] | 512 | size_t I = (0xFF & CharC->getSExtValue()) == 0 ? |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 513 | Str.size() : Str.find(CharC->getSExtValue()); |
| 514 | if (I == StringRef::npos) // Didn't find the char. strchr returns null. |
| 515 | return Constant::getNullValue(CI->getType()); |
| 516 | |
| 517 | // strchr(s+n,c) -> gep(s+n+i,c) |
| 518 | return B.CreateGEP(SrcStr, B.getInt64(I), "strchr"); |
| 519 | } |
| 520 | }; |
| 521 | |
| 522 | struct StrRChrOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 523 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 524 | IRBuilder<> &B) override { |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 525 | // Verify the "strrchr" function prototype. |
| 526 | FunctionType *FT = Callee->getFunctionType(); |
| 527 | if (FT->getNumParams() != 2 || |
| 528 | FT->getReturnType() != B.getInt8PtrTy() || |
| 529 | FT->getParamType(0) != FT->getReturnType() || |
| 530 | !FT->getParamType(1)->isIntegerTy(32)) |
| 531 | return 0; |
| 532 | |
| 533 | Value *SrcStr = CI->getArgOperand(0); |
| 534 | ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 535 | |
| 536 | // Cannot fold anything if we're not looking for a constant. |
| 537 | if (!CharC) |
| 538 | return 0; |
| 539 | |
| 540 | StringRef Str; |
| 541 | if (!getConstantStringInfo(SrcStr, Str)) { |
| 542 | // strrchr(s, 0) -> strchr(s, 0) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 543 | if (DL && CharC->isZero()) |
| 544 | return EmitStrChr(SrcStr, '\0', B, DL, TLI); |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | // Compute the offset. |
Yunzhong Gao | dfdf7f4 | 2013-08-21 22:11:15 +0000 | [diff] [blame] | 549 | size_t I = (0xFF & CharC->getSExtValue()) == 0 ? |
Meador Inge | 186f8d9 | 2012-10-13 16:45:37 +0000 | [diff] [blame] | 550 | Str.size() : Str.rfind(CharC->getSExtValue()); |
| 551 | if (I == StringRef::npos) // Didn't find the char. Return null. |
| 552 | return Constant::getNullValue(CI->getType()); |
| 553 | |
| 554 | // strrchr(s+n,c) -> gep(s+n+i,c) |
| 555 | return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr"); |
| 556 | } |
| 557 | }; |
| 558 | |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 559 | struct StrCmpOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 560 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 561 | IRBuilder<> &B) override { |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 562 | // Verify the "strcmp" function prototype. |
| 563 | FunctionType *FT = Callee->getFunctionType(); |
| 564 | if (FT->getNumParams() != 2 || |
| 565 | !FT->getReturnType()->isIntegerTy(32) || |
| 566 | FT->getParamType(0) != FT->getParamType(1) || |
| 567 | FT->getParamType(0) != B.getInt8PtrTy()) |
| 568 | return 0; |
| 569 | |
| 570 | Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); |
| 571 | if (Str1P == Str2P) // strcmp(x,x) -> 0 |
| 572 | return ConstantInt::get(CI->getType(), 0); |
| 573 | |
| 574 | StringRef Str1, Str2; |
| 575 | bool HasStr1 = getConstantStringInfo(Str1P, Str1); |
| 576 | bool HasStr2 = getConstantStringInfo(Str2P, Str2); |
| 577 | |
| 578 | // strcmp(x, y) -> cnst (if both x and y are constant strings) |
| 579 | if (HasStr1 && HasStr2) |
| 580 | return ConstantInt::get(CI->getType(), Str1.compare(Str2)); |
| 581 | |
| 582 | if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x |
| 583 | return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), |
| 584 | CI->getType())); |
| 585 | |
| 586 | if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x |
| 587 | return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); |
| 588 | |
| 589 | // strcmp(P, "x") -> memcmp(P, "x", 2) |
| 590 | uint64_t Len1 = GetStringLength(Str1P); |
| 591 | uint64_t Len2 = GetStringLength(Str2P); |
| 592 | if (Len1 && Len2) { |
| 593 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 594 | if (!DL) return 0; |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 595 | |
| 596 | return EmitMemCmp(Str1P, Str2P, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 597 | ConstantInt::get(DL->getIntPtrType(*Context), |
| 598 | std::min(Len1, Len2)), B, DL, TLI); |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | struct StrNCmpOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 606 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 607 | IRBuilder<> &B) override { |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 608 | // Verify the "strncmp" function prototype. |
| 609 | FunctionType *FT = Callee->getFunctionType(); |
| 610 | if (FT->getNumParams() != 3 || |
| 611 | !FT->getReturnType()->isIntegerTy(32) || |
| 612 | FT->getParamType(0) != FT->getParamType(1) || |
| 613 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 614 | !FT->getParamType(2)->isIntegerTy()) |
| 615 | return 0; |
| 616 | |
| 617 | Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); |
| 618 | if (Str1P == Str2P) // strncmp(x,x,n) -> 0 |
| 619 | return ConstantInt::get(CI->getType(), 0); |
| 620 | |
| 621 | // Get the length argument if it is constant. |
| 622 | uint64_t Length; |
| 623 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2))) |
| 624 | Length = LengthArg->getZExtValue(); |
| 625 | else |
| 626 | return 0; |
| 627 | |
| 628 | if (Length == 0) // strncmp(x,y,0) -> 0 |
| 629 | return ConstantInt::get(CI->getType(), 0); |
| 630 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 631 | if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1) |
| 632 | return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI); |
Meador Inge | a239c2e | 2012-10-15 03:47:37 +0000 | [diff] [blame] | 633 | |
| 634 | StringRef Str1, Str2; |
| 635 | bool HasStr1 = getConstantStringInfo(Str1P, Str1); |
| 636 | bool HasStr2 = getConstantStringInfo(Str2P, Str2); |
| 637 | |
| 638 | // strncmp(x, y) -> cnst (if both x and y are constant strings) |
| 639 | if (HasStr1 && HasStr2) { |
| 640 | StringRef SubStr1 = Str1.substr(0, Length); |
| 641 | StringRef SubStr2 = Str2.substr(0, Length); |
| 642 | return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2)); |
| 643 | } |
| 644 | |
| 645 | if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x |
| 646 | return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), |
| 647 | CI->getType())); |
| 648 | |
| 649 | if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x |
| 650 | return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | }; |
| 655 | |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 656 | struct StrCpyOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 657 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 658 | IRBuilder<> &B) override { |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 659 | // Verify the "strcpy" function prototype. |
| 660 | FunctionType *FT = Callee->getFunctionType(); |
| 661 | if (FT->getNumParams() != 2 || |
| 662 | FT->getReturnType() != FT->getParamType(0) || |
| 663 | FT->getParamType(0) != FT->getParamType(1) || |
| 664 | FT->getParamType(0) != B.getInt8PtrTy()) |
| 665 | return 0; |
| 666 | |
| 667 | Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); |
| 668 | if (Dst == Src) // strcpy(x,x) -> x |
| 669 | return Src; |
| 670 | |
| 671 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 672 | if (!DL) return 0; |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 673 | |
| 674 | // See if we can get the length of the input string. |
| 675 | uint64_t Len = GetStringLength(Src); |
| 676 | if (Len == 0) return 0; |
| 677 | |
| 678 | // We have enough information to now generate the memcpy call to do the |
| 679 | // copy for us. Make a memcpy to copy the nul byte with align = 1. |
| 680 | B.CreateMemCpy(Dst, Src, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 681 | ConstantInt::get(DL->getIntPtrType(*Context), Len), 1); |
Meador Inge | 0c41d57 | 2012-10-18 18:12:40 +0000 | [diff] [blame] | 682 | return Dst; |
| 683 | } |
| 684 | }; |
| 685 | |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 686 | struct StpCpyOpt: public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 687 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 688 | IRBuilder<> &B) override { |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 689 | // Verify the "stpcpy" function prototype. |
| 690 | FunctionType *FT = Callee->getFunctionType(); |
| 691 | if (FT->getNumParams() != 2 || |
| 692 | FT->getReturnType() != FT->getParamType(0) || |
| 693 | FT->getParamType(0) != FT->getParamType(1) || |
| 694 | FT->getParamType(0) != B.getInt8PtrTy()) |
| 695 | return 0; |
| 696 | |
| 697 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 698 | if (!DL) return 0; |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 699 | |
| 700 | Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); |
| 701 | if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 702 | Value *StrLen = EmitStrLen(Src, B, DL, TLI); |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 703 | return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0; |
| 704 | } |
| 705 | |
| 706 | // See if we can get the length of the input string. |
| 707 | uint64_t Len = GetStringLength(Src); |
| 708 | if (Len == 0) return 0; |
| 709 | |
| 710 | Type *PT = FT->getParamType(0); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 711 | Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len); |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 712 | Value *DstEnd = B.CreateGEP(Dst, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 713 | ConstantInt::get(DL->getIntPtrType(PT), |
Meador Inge | e6d781f | 2012-10-31 00:20:56 +0000 | [diff] [blame] | 714 | Len - 1)); |
| 715 | |
| 716 | // We have enough information to now generate the memcpy call to do the |
| 717 | // copy for us. Make a memcpy to copy the nul byte with align = 1. |
| 718 | B.CreateMemCpy(Dst, Src, LenV, 1); |
| 719 | return DstEnd; |
| 720 | } |
| 721 | }; |
| 722 | |
Meador Inge | a0885fb | 2012-10-31 03:33:00 +0000 | [diff] [blame] | 723 | struct StrNCpyOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 724 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 725 | IRBuilder<> &B) override { |
Meador Inge | a0885fb | 2012-10-31 03:33:00 +0000 | [diff] [blame] | 726 | FunctionType *FT = Callee->getFunctionType(); |
| 727 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
| 728 | FT->getParamType(0) != FT->getParamType(1) || |
| 729 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 730 | !FT->getParamType(2)->isIntegerTy()) |
| 731 | return 0; |
| 732 | |
| 733 | Value *Dst = CI->getArgOperand(0); |
| 734 | Value *Src = CI->getArgOperand(1); |
| 735 | Value *LenOp = CI->getArgOperand(2); |
| 736 | |
| 737 | // See if we can get the length of the input string. |
| 738 | uint64_t SrcLen = GetStringLength(Src); |
| 739 | if (SrcLen == 0) return 0; |
| 740 | --SrcLen; |
| 741 | |
| 742 | if (SrcLen == 0) { |
| 743 | // strncpy(x, "", y) -> memset(x, '\0', y, 1) |
| 744 | B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1); |
| 745 | return Dst; |
| 746 | } |
| 747 | |
| 748 | uint64_t Len; |
| 749 | if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp)) |
| 750 | Len = LengthArg->getZExtValue(); |
| 751 | else |
| 752 | return 0; |
| 753 | |
| 754 | if (Len == 0) return Dst; // strncpy(x, y, 0) -> x |
| 755 | |
| 756 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 757 | if (!DL) return 0; |
Meador Inge | a0885fb | 2012-10-31 03:33:00 +0000 | [diff] [blame] | 758 | |
| 759 | // Let strncpy handle the zero padding |
| 760 | if (Len > SrcLen+1) return 0; |
| 761 | |
| 762 | Type *PT = FT->getParamType(0); |
| 763 | // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant] |
| 764 | B.CreateMemCpy(Dst, Src, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 765 | ConstantInt::get(DL->getIntPtrType(PT), Len), 1); |
Meador Inge | a0885fb | 2012-10-31 03:33:00 +0000 | [diff] [blame] | 766 | |
| 767 | return Dst; |
| 768 | } |
| 769 | }; |
| 770 | |
Meador Inge | 57cfd71 | 2012-10-31 03:33:06 +0000 | [diff] [blame] | 771 | struct StrLenOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 772 | bool ignoreCallingConv() override { return true; } |
| 773 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 774 | IRBuilder<> &B) override { |
Meador Inge | 57cfd71 | 2012-10-31 03:33:06 +0000 | [diff] [blame] | 775 | FunctionType *FT = Callee->getFunctionType(); |
| 776 | if (FT->getNumParams() != 1 || |
| 777 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 778 | !FT->getReturnType()->isIntegerTy()) |
| 779 | return 0; |
| 780 | |
| 781 | Value *Src = CI->getArgOperand(0); |
| 782 | |
| 783 | // Constant folding: strlen("xyz") -> 3 |
| 784 | if (uint64_t Len = GetStringLength(Src)) |
| 785 | return ConstantInt::get(CI->getType(), Len-1); |
| 786 | |
| 787 | // strlen(x) != 0 --> *x != 0 |
| 788 | // strlen(x) == 0 --> *x == 0 |
| 789 | if (isOnlyUsedInZeroEqualityComparison(CI)) |
| 790 | return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType()); |
| 791 | return 0; |
| 792 | } |
| 793 | }; |
| 794 | |
Meador Inge | 08684d1 | 2012-10-31 04:29:58 +0000 | [diff] [blame] | 795 | struct StrPBrkOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 796 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 797 | IRBuilder<> &B) override { |
Meador Inge | 08684d1 | 2012-10-31 04:29:58 +0000 | [diff] [blame] | 798 | FunctionType *FT = Callee->getFunctionType(); |
| 799 | if (FT->getNumParams() != 2 || |
| 800 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 801 | FT->getParamType(1) != FT->getParamType(0) || |
| 802 | FT->getReturnType() != FT->getParamType(0)) |
| 803 | return 0; |
| 804 | |
| 805 | StringRef S1, S2; |
| 806 | bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); |
| 807 | bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); |
| 808 | |
| 809 | // strpbrk(s, "") -> NULL |
| 810 | // strpbrk("", s) -> NULL |
| 811 | if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) |
| 812 | return Constant::getNullValue(CI->getType()); |
| 813 | |
| 814 | // Constant folding. |
| 815 | if (HasS1 && HasS2) { |
| 816 | size_t I = S1.find_first_of(S2); |
Matt Arsenault | ffdee89 | 2013-09-10 00:41:53 +0000 | [diff] [blame] | 817 | if (I == StringRef::npos) // No match. |
Meador Inge | 08684d1 | 2012-10-31 04:29:58 +0000 | [diff] [blame] | 818 | return Constant::getNullValue(CI->getType()); |
| 819 | |
| 820 | return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk"); |
| 821 | } |
| 822 | |
| 823 | // strpbrk(s, "a") -> strchr(s, 'a') |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 824 | if (DL && HasS2 && S2.size() == 1) |
| 825 | return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI); |
Meador Inge | 08684d1 | 2012-10-31 04:29:58 +0000 | [diff] [blame] | 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | }; |
| 830 | |
Meador Inge | e0f1dca | 2012-10-31 14:58:26 +0000 | [diff] [blame] | 831 | struct StrToOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 832 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 833 | IRBuilder<> &B) override { |
Meador Inge | e0f1dca | 2012-10-31 14:58:26 +0000 | [diff] [blame] | 834 | FunctionType *FT = Callee->getFunctionType(); |
| 835 | if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) || |
| 836 | !FT->getParamType(0)->isPointerTy() || |
| 837 | !FT->getParamType(1)->isPointerTy()) |
| 838 | return 0; |
| 839 | |
| 840 | Value *EndPtr = CI->getArgOperand(1); |
| 841 | if (isa<ConstantPointerNull>(EndPtr)) { |
| 842 | // With a null EndPtr, this function won't capture the main argument. |
| 843 | // It would be readonly too, except that it still may write to errno. |
Peter Collingbourne | 328d1b6 | 2013-03-02 01:20:18 +0000 | [diff] [blame] | 844 | CI->addAttribute(1, Attribute::NoCapture); |
Meador Inge | e0f1dca | 2012-10-31 14:58:26 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | }; |
| 850 | |
Meador Inge | 7629de3 | 2012-11-08 01:33:50 +0000 | [diff] [blame] | 851 | struct StrSpnOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 852 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 853 | IRBuilder<> &B) override { |
Meador Inge | 7629de3 | 2012-11-08 01:33:50 +0000 | [diff] [blame] | 854 | FunctionType *FT = Callee->getFunctionType(); |
| 855 | if (FT->getNumParams() != 2 || |
| 856 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 857 | FT->getParamType(1) != FT->getParamType(0) || |
| 858 | !FT->getReturnType()->isIntegerTy()) |
| 859 | return 0; |
| 860 | |
| 861 | StringRef S1, S2; |
| 862 | bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); |
| 863 | bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); |
| 864 | |
| 865 | // strspn(s, "") -> 0 |
| 866 | // strspn("", s) -> 0 |
| 867 | if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) |
| 868 | return Constant::getNullValue(CI->getType()); |
| 869 | |
| 870 | // Constant folding. |
| 871 | if (HasS1 && HasS2) { |
| 872 | size_t Pos = S1.find_first_not_of(S2); |
| 873 | if (Pos == StringRef::npos) Pos = S1.size(); |
| 874 | return ConstantInt::get(CI->getType(), Pos); |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | }; |
| 880 | |
Meador Inge | 5464ee7 | 2012-11-10 15:16:48 +0000 | [diff] [blame] | 881 | struct StrCSpnOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 882 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 883 | IRBuilder<> &B) override { |
Meador Inge | 5464ee7 | 2012-11-10 15:16:48 +0000 | [diff] [blame] | 884 | FunctionType *FT = Callee->getFunctionType(); |
| 885 | if (FT->getNumParams() != 2 || |
| 886 | FT->getParamType(0) != B.getInt8PtrTy() || |
| 887 | FT->getParamType(1) != FT->getParamType(0) || |
| 888 | !FT->getReturnType()->isIntegerTy()) |
| 889 | return 0; |
| 890 | |
| 891 | StringRef S1, S2; |
| 892 | bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); |
| 893 | bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); |
| 894 | |
| 895 | // strcspn("", s) -> 0 |
| 896 | if (HasS1 && S1.empty()) |
| 897 | return Constant::getNullValue(CI->getType()); |
| 898 | |
| 899 | // Constant folding. |
| 900 | if (HasS1 && HasS2) { |
| 901 | size_t Pos = S1.find_first_of(S2); |
| 902 | if (Pos == StringRef::npos) Pos = S1.size(); |
| 903 | return ConstantInt::get(CI->getType(), Pos); |
| 904 | } |
| 905 | |
| 906 | // strcspn(s, "") -> strlen(s) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 907 | if (DL && HasS2 && S2.empty()) |
| 908 | return EmitStrLen(CI->getArgOperand(0), B, DL, TLI); |
Meador Inge | 5464ee7 | 2012-11-10 15:16:48 +0000 | [diff] [blame] | 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | }; |
| 913 | |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 914 | struct StrStrOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 915 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 916 | IRBuilder<> &B) override { |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 917 | FunctionType *FT = Callee->getFunctionType(); |
| 918 | if (FT->getNumParams() != 2 || |
| 919 | !FT->getParamType(0)->isPointerTy() || |
| 920 | !FT->getParamType(1)->isPointerTy() || |
| 921 | !FT->getReturnType()->isPointerTy()) |
| 922 | return 0; |
| 923 | |
| 924 | // fold strstr(x, x) -> x. |
| 925 | if (CI->getArgOperand(0) == CI->getArgOperand(1)) |
| 926 | return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); |
| 927 | |
| 928 | // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0 |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 929 | if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) { |
| 930 | Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI); |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 931 | if (!StrLen) |
| 932 | return 0; |
| 933 | Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 934 | StrLen, B, DL, TLI); |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 935 | if (!StrNCmp) |
| 936 | return 0; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 937 | for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) { |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 938 | ICmpInst *Old = cast<ICmpInst>(*UI++); |
| 939 | Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp, |
| 940 | ConstantInt::getNullValue(StrNCmp->getType()), |
| 941 | "cmp"); |
| 942 | LCS->replaceAllUsesWith(Old, Cmp); |
| 943 | } |
| 944 | return CI; |
| 945 | } |
| 946 | |
| 947 | // See if either input string is a constant string. |
| 948 | StringRef SearchStr, ToFindStr; |
| 949 | bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr); |
| 950 | bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr); |
| 951 | |
| 952 | // fold strstr(x, "") -> x. |
| 953 | if (HasStr2 && ToFindStr.empty()) |
| 954 | return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); |
| 955 | |
| 956 | // If both strings are known, constant fold it. |
| 957 | if (HasStr1 && HasStr2) { |
Matt Arsenault | ffdee89 | 2013-09-10 00:41:53 +0000 | [diff] [blame] | 958 | size_t Offset = SearchStr.find(ToFindStr); |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 959 | |
| 960 | if (Offset == StringRef::npos) // strstr("foo", "bar") -> null |
| 961 | return Constant::getNullValue(CI->getType()); |
| 962 | |
| 963 | // strstr("abcd", "bc") -> gep((char*)"abcd", 1) |
| 964 | Value *Result = CastToCStr(CI->getArgOperand(0), B); |
| 965 | Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr"); |
| 966 | return B.CreateBitCast(Result, CI->getType()); |
| 967 | } |
| 968 | |
| 969 | // fold strstr(x, "y") -> strchr(x, 'y'). |
| 970 | if (HasStr2 && ToFindStr.size() == 1) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 971 | Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI); |
Meador Inge | 6e1591a | 2012-11-11 03:51:48 +0000 | [diff] [blame] | 972 | return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | }; |
| 977 | |
Meador Inge | bb51ec8 | 2012-11-11 05:11:20 +0000 | [diff] [blame] | 978 | struct MemCmpOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 979 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 980 | IRBuilder<> &B) override { |
Meador Inge | bb51ec8 | 2012-11-11 05:11:20 +0000 | [diff] [blame] | 981 | FunctionType *FT = Callee->getFunctionType(); |
| 982 | if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() || |
| 983 | !FT->getParamType(1)->isPointerTy() || |
| 984 | !FT->getReturnType()->isIntegerTy(32)) |
| 985 | return 0; |
| 986 | |
| 987 | Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1); |
| 988 | |
| 989 | if (LHS == RHS) // memcmp(s,s,x) -> 0 |
| 990 | return Constant::getNullValue(CI->getType()); |
| 991 | |
| 992 | // Make sure we have a constant length. |
| 993 | ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); |
| 994 | if (!LenC) return 0; |
| 995 | uint64_t Len = LenC->getZExtValue(); |
| 996 | |
| 997 | if (Len == 0) // memcmp(s1,s2,0) -> 0 |
| 998 | return Constant::getNullValue(CI->getType()); |
| 999 | |
| 1000 | // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS |
| 1001 | if (Len == 1) { |
| 1002 | Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"), |
| 1003 | CI->getType(), "lhsv"); |
| 1004 | Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"), |
| 1005 | CI->getType(), "rhsv"); |
| 1006 | return B.CreateSub(LHSV, RHSV, "chardiff"); |
| 1007 | } |
| 1008 | |
| 1009 | // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant) |
| 1010 | StringRef LHSStr, RHSStr; |
| 1011 | if (getConstantStringInfo(LHS, LHSStr) && |
| 1012 | getConstantStringInfo(RHS, RHSStr)) { |
| 1013 | // Make sure we're not reading out-of-bounds memory. |
| 1014 | if (Len > LHSStr.size() || Len > RHSStr.size()) |
| 1015 | return 0; |
Meador Inge | 30d8f0e | 2012-11-12 14:00:45 +0000 | [diff] [blame] | 1016 | // Fold the memcmp and normalize the result. This way we get consistent |
| 1017 | // results across multiple platforms. |
| 1018 | uint64_t Ret = 0; |
| 1019 | int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len); |
| 1020 | if (Cmp < 0) |
| 1021 | Ret = -1; |
| 1022 | else if (Cmp > 0) |
| 1023 | Ret = 1; |
Meador Inge | bb51ec8 | 2012-11-11 05:11:20 +0000 | [diff] [blame] | 1024 | return ConstantInt::get(CI->getType(), Ret); |
| 1025 | } |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | }; |
| 1030 | |
Meador Inge | 11b04b4 | 2012-11-11 05:54:34 +0000 | [diff] [blame] | 1031 | struct MemCpyOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1032 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1033 | IRBuilder<> &B) override { |
Meador Inge | 11b04b4 | 2012-11-11 05:54:34 +0000 | [diff] [blame] | 1034 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1035 | if (!DL) return 0; |
Meador Inge | 11b04b4 | 2012-11-11 05:54:34 +0000 | [diff] [blame] | 1036 | |
| 1037 | FunctionType *FT = Callee->getFunctionType(); |
| 1038 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
| 1039 | !FT->getParamType(0)->isPointerTy() || |
| 1040 | !FT->getParamType(1)->isPointerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1041 | FT->getParamType(2) != DL->getIntPtrType(*Context)) |
Meador Inge | 11b04b4 | 2012-11-11 05:54:34 +0000 | [diff] [blame] | 1042 | return 0; |
| 1043 | |
| 1044 | // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1) |
| 1045 | B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
| 1046 | CI->getArgOperand(2), 1); |
| 1047 | return CI->getArgOperand(0); |
| 1048 | } |
| 1049 | }; |
| 1050 | |
Meador Inge | d7cb600 | 2012-11-11 06:22:40 +0000 | [diff] [blame] | 1051 | struct MemMoveOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1052 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1053 | IRBuilder<> &B) override { |
Meador Inge | d7cb600 | 2012-11-11 06:22:40 +0000 | [diff] [blame] | 1054 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1055 | if (!DL) return 0; |
Meador Inge | d7cb600 | 2012-11-11 06:22:40 +0000 | [diff] [blame] | 1056 | |
| 1057 | FunctionType *FT = Callee->getFunctionType(); |
| 1058 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
| 1059 | !FT->getParamType(0)->isPointerTy() || |
| 1060 | !FT->getParamType(1)->isPointerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1061 | FT->getParamType(2) != DL->getIntPtrType(*Context)) |
Meador Inge | d7cb600 | 2012-11-11 06:22:40 +0000 | [diff] [blame] | 1062 | return 0; |
| 1063 | |
| 1064 | // memmove(x, y, n) -> llvm.memmove(x, y, n, 1) |
| 1065 | B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), |
| 1066 | CI->getArgOperand(2), 1); |
| 1067 | return CI->getArgOperand(0); |
| 1068 | } |
| 1069 | }; |
| 1070 | |
Meador Inge | 26ebe39 | 2012-11-11 06:49:03 +0000 | [diff] [blame] | 1071 | struct MemSetOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1072 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1073 | IRBuilder<> &B) override { |
Meador Inge | 26ebe39 | 2012-11-11 06:49:03 +0000 | [diff] [blame] | 1074 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1075 | if (!DL) return 0; |
Meador Inge | 26ebe39 | 2012-11-11 06:49:03 +0000 | [diff] [blame] | 1076 | |
| 1077 | FunctionType *FT = Callee->getFunctionType(); |
| 1078 | if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || |
| 1079 | !FT->getParamType(0)->isPointerTy() || |
| 1080 | !FT->getParamType(1)->isIntegerTy() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1081 | FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0))) |
Meador Inge | 26ebe39 | 2012-11-11 06:49:03 +0000 | [diff] [blame] | 1082 | return 0; |
| 1083 | |
| 1084 | // memset(p, v, n) -> llvm.memset(p, v, n, 1) |
| 1085 | Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); |
| 1086 | B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); |
| 1087 | return CI->getArgOperand(0); |
| 1088 | } |
| 1089 | }; |
| 1090 | |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1091 | //===----------------------------------------------------------------------===// |
| 1092 | // Math Library Optimizations |
| 1093 | //===----------------------------------------------------------------------===// |
| 1094 | |
| 1095 | //===----------------------------------------------------------------------===// |
| 1096 | // Double -> Float Shrinking Optimizations for Unary Functions like 'floor' |
| 1097 | |
| 1098 | struct UnaryDoubleFPOpt : public LibCallOptimization { |
| 1099 | bool CheckRetType; |
| 1100 | UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {} |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1101 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1102 | IRBuilder<> &B) override { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1103 | FunctionType *FT = Callee->getFunctionType(); |
| 1104 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() || |
| 1105 | !FT->getParamType(0)->isDoubleTy()) |
| 1106 | return 0; |
| 1107 | |
| 1108 | if (CheckRetType) { |
| 1109 | // Check if all the uses for function like 'sin' are converted to float. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1110 | for (User *U : CI->users()) { |
| 1111 | FPTruncInst *Cast = dyn_cast<FPTruncInst>(U); |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1112 | if (Cast == 0 || !Cast->getType()->isFloatTy()) |
| 1113 | return 0; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | // If this is something like 'floor((double)floatval)', convert to floorf. |
| 1118 | FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0)); |
| 1119 | if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy()) |
| 1120 | return 0; |
| 1121 | |
| 1122 | // floor((double)floatval) -> (double)floorf(floatval) |
| 1123 | Value *V = Cast->getOperand(0); |
| 1124 | V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes()); |
| 1125 | return B.CreateFPExt(V, B.getDoubleTy()); |
| 1126 | } |
| 1127 | }; |
| 1128 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1129 | // Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax' |
| 1130 | struct BinaryDoubleFPOpt : public LibCallOptimization { |
| 1131 | bool CheckRetType; |
| 1132 | BinaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {} |
| 1133 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1134 | IRBuilder<> &B) override { |
| 1135 | FunctionType *FT = Callee->getFunctionType(); |
| 1136 | // Just make sure this has 2 arguments of the same FP type, which match the |
| 1137 | // result type. |
| 1138 | if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) || |
| 1139 | FT->getParamType(0) != FT->getParamType(1) || |
| 1140 | !FT->getParamType(0)->isFloatingPointTy()) |
| 1141 | return 0; |
| 1142 | |
| 1143 | if (CheckRetType) { |
| 1144 | // Check if all the uses for function like 'fmin/fmax' are converted to |
| 1145 | // float. |
| 1146 | for (User *U : CI->users()) { |
| 1147 | FPTruncInst *Cast = dyn_cast<FPTruncInst>(U); |
| 1148 | if (Cast == 0 || !Cast->getType()->isFloatTy()) |
| 1149 | return 0; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // If this is something like 'fmin((double)floatval1, (double)floatval2)', |
| 1154 | // we convert it to fminf. |
| 1155 | FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0)); |
| 1156 | FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1)); |
| 1157 | if (Cast1 == 0 || !Cast1->getOperand(0)->getType()->isFloatTy() || |
| 1158 | Cast2 == 0 || !Cast2->getOperand(0)->getType()->isFloatTy()) |
| 1159 | return 0; |
| 1160 | |
| 1161 | // fmin((double)floatval1, (double)floatval2) |
| 1162 | // -> (double)fmin(floatval1, floatval2) |
| 1163 | Value *V = NULL; |
| 1164 | Value *V1 = Cast1->getOperand(0); |
| 1165 | Value *V2 = Cast2->getOperand(0); |
| 1166 | V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B, |
| 1167 | Callee->getAttributes()); |
| 1168 | return B.CreateFPExt(V, B.getDoubleTy()); |
| 1169 | } |
| 1170 | }; |
| 1171 | |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1172 | struct UnsafeFPLibCallOptimization : public LibCallOptimization { |
| 1173 | bool UnsafeFPShrink; |
| 1174 | UnsafeFPLibCallOptimization(bool UnsafeFPShrink) { |
| 1175 | this->UnsafeFPShrink = UnsafeFPShrink; |
| 1176 | } |
| 1177 | }; |
| 1178 | |
| 1179 | struct CosOpt : public UnsafeFPLibCallOptimization { |
| 1180 | CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {} |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1181 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1182 | IRBuilder<> &B) override { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1183 | Value *Ret = NULL; |
| 1184 | if (UnsafeFPShrink && Callee->getName() == "cos" && |
| 1185 | TLI->has(LibFunc::cosf)) { |
| 1186 | UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true); |
| 1187 | Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B); |
| 1188 | } |
| 1189 | |
| 1190 | FunctionType *FT = Callee->getFunctionType(); |
| 1191 | // Just make sure this has 1 argument of FP type, which matches the |
| 1192 | // result type. |
| 1193 | if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) || |
| 1194 | !FT->getParamType(0)->isFloatingPointTy()) |
| 1195 | return Ret; |
| 1196 | |
| 1197 | // cos(-x) -> cos(x) |
| 1198 | Value *Op1 = CI->getArgOperand(0); |
| 1199 | if (BinaryOperator::isFNeg(Op1)) { |
| 1200 | BinaryOperator *BinExpr = cast<BinaryOperator>(Op1); |
| 1201 | return B.CreateCall(Callee, BinExpr->getOperand(1), "cos"); |
| 1202 | } |
| 1203 | return Ret; |
| 1204 | } |
| 1205 | }; |
| 1206 | |
| 1207 | struct PowOpt : public UnsafeFPLibCallOptimization { |
| 1208 | PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {} |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1209 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1210 | IRBuilder<> &B) override { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1211 | Value *Ret = NULL; |
| 1212 | if (UnsafeFPShrink && Callee->getName() == "pow" && |
| 1213 | TLI->has(LibFunc::powf)) { |
| 1214 | UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true); |
| 1215 | Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B); |
| 1216 | } |
| 1217 | |
| 1218 | FunctionType *FT = Callee->getFunctionType(); |
| 1219 | // Just make sure this has 2 arguments of the same FP type, which match the |
| 1220 | // result type. |
| 1221 | if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) || |
| 1222 | FT->getParamType(0) != FT->getParamType(1) || |
| 1223 | !FT->getParamType(0)->isFloatingPointTy()) |
| 1224 | return Ret; |
| 1225 | |
| 1226 | Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1); |
| 1227 | if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { |
Michael Kuperstein | 2063637 | 2013-08-19 06:55:47 +0000 | [diff] [blame] | 1228 | // pow(1.0, x) -> 1.0 |
| 1229 | if (Op1C->isExactlyValue(1.0)) |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1230 | return Op1C; |
Michael Kuperstein | 2063637 | 2013-08-19 06:55:47 +0000 | [diff] [blame] | 1231 | // pow(2.0, x) -> exp2(x) |
Benjamin Kramer | bc87003 | 2013-08-31 18:19:35 +0000 | [diff] [blame] | 1232 | if (Op1C->isExactlyValue(2.0) && |
| 1233 | hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f, |
| 1234 | LibFunc::exp2l)) |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1235 | return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes()); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1236 | // pow(10.0, x) -> exp10(x) |
| 1237 | if (Op1C->isExactlyValue(10.0) && |
| 1238 | hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f, |
| 1239 | LibFunc::exp10l)) |
| 1240 | return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B, |
| 1241 | Callee->getAttributes()); |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2); |
| 1245 | if (Op2C == 0) return Ret; |
| 1246 | |
| 1247 | if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0 |
| 1248 | return ConstantFP::get(CI->getType(), 1.0); |
| 1249 | |
Michael Kuperstein | 2063637 | 2013-08-19 06:55:47 +0000 | [diff] [blame] | 1250 | if (Op2C->isExactlyValue(0.5) && |
Benjamin Kramer | bc87003 | 2013-08-31 18:19:35 +0000 | [diff] [blame] | 1251 | hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf, |
| 1252 | LibFunc::sqrtl) && |
| 1253 | hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf, |
| 1254 | LibFunc::fabsl)) { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1255 | // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))). |
| 1256 | // This is faster than calling pow, and still handles negative zero |
| 1257 | // and negative infinity correctly. |
| 1258 | // TODO: In fast-math mode, this could be just sqrt(x). |
| 1259 | // TODO: In finite-only mode, this could be just fabs(sqrt(x)). |
| 1260 | Value *Inf = ConstantFP::getInfinity(CI->getType()); |
| 1261 | Value *NegInf = ConstantFP::getInfinity(CI->getType(), true); |
| 1262 | Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, |
| 1263 | Callee->getAttributes()); |
| 1264 | Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B, |
| 1265 | Callee->getAttributes()); |
| 1266 | Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf); |
| 1267 | Value *Sel = B.CreateSelect(FCmp, Inf, FAbs); |
| 1268 | return Sel; |
| 1269 | } |
| 1270 | |
| 1271 | if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x |
| 1272 | return Op1; |
| 1273 | if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x |
| 1274 | return B.CreateFMul(Op1, Op1, "pow2"); |
| 1275 | if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x |
| 1276 | return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), |
| 1277 | Op1, "powrecip"); |
| 1278 | return 0; |
| 1279 | } |
| 1280 | }; |
| 1281 | |
| 1282 | struct Exp2Opt : public UnsafeFPLibCallOptimization { |
| 1283 | Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {} |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1284 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1285 | IRBuilder<> &B) override { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1286 | Value *Ret = NULL; |
| 1287 | if (UnsafeFPShrink && Callee->getName() == "exp2" && |
Benjamin Kramer | bc87003 | 2013-08-31 18:19:35 +0000 | [diff] [blame] | 1288 | TLI->has(LibFunc::exp2f)) { |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1289 | UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true); |
| 1290 | Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B); |
| 1291 | } |
| 1292 | |
| 1293 | FunctionType *FT = Callee->getFunctionType(); |
| 1294 | // Just make sure this has 1 argument of FP type, which matches the |
| 1295 | // result type. |
| 1296 | if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) || |
| 1297 | !FT->getParamType(0)->isFloatingPointTy()) |
| 1298 | return Ret; |
| 1299 | |
| 1300 | Value *Op = CI->getArgOperand(0); |
| 1301 | // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32 |
| 1302 | // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32 |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1303 | LibFunc::Func LdExp = LibFunc::ldexpl; |
| 1304 | if (Op->getType()->isFloatTy()) |
| 1305 | LdExp = LibFunc::ldexpf; |
| 1306 | else if (Op->getType()->isDoubleTy()) |
| 1307 | LdExp = LibFunc::ldexp; |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1308 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1309 | if (TLI->has(LdExp)) { |
| 1310 | Value *LdExpArg = 0; |
| 1311 | if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) { |
| 1312 | if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32) |
| 1313 | LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty()); |
| 1314 | } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) { |
| 1315 | if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32) |
| 1316 | LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty()); |
| 1317 | } |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1318 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1319 | if (LdExpArg) { |
| 1320 | Constant *One = ConstantFP::get(*Context, APFloat(1.0f)); |
| 1321 | if (!Op->getType()->isFloatTy()) |
| 1322 | One = ConstantExpr::getFPExtend(One, Op->getType()); |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1323 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1324 | Module *M = Caller->getParent(); |
| 1325 | Value *Callee = |
| 1326 | M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(), |
| 1327 | Op->getType(), B.getInt32Ty(), NULL); |
| 1328 | CallInst *CI = B.CreateCall2(Callee, One, LdExpArg); |
| 1329 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 1330 | CI->setCallingConv(F->getCallingConv()); |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1331 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1332 | return CI; |
| 1333 | } |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 1334 | } |
| 1335 | return Ret; |
| 1336 | } |
| 1337 | }; |
| 1338 | |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 1339 | struct SinCosPiOpt : public LibCallOptimization { |
| 1340 | SinCosPiOpt() {} |
| 1341 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1342 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1343 | IRBuilder<> &B) override { |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 1344 | // Make sure the prototype is as expected, otherwise the rest of the |
| 1345 | // function is probably invalid and likely to abort. |
| 1346 | if (!isTrigLibCall(CI)) |
| 1347 | return 0; |
| 1348 | |
| 1349 | Value *Arg = CI->getArgOperand(0); |
| 1350 | SmallVector<CallInst *, 1> SinCalls; |
| 1351 | SmallVector<CallInst *, 1> CosCalls; |
| 1352 | SmallVector<CallInst *, 1> SinCosCalls; |
| 1353 | |
| 1354 | bool IsFloat = Arg->getType()->isFloatTy(); |
| 1355 | |
| 1356 | // Look for all compatible sinpi, cospi and sincospi calls with the same |
| 1357 | // argument. If there are enough (in some sense) we can make the |
| 1358 | // substitution. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1359 | for (User *U : Arg->users()) |
| 1360 | classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls, |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 1361 | SinCosCalls); |
| 1362 | |
| 1363 | // It's only worthwhile if both sinpi and cospi are actually used. |
| 1364 | if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty())) |
| 1365 | return 0; |
| 1366 | |
| 1367 | Value *Sin, *Cos, *SinCos; |
| 1368 | insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, |
| 1369 | SinCos); |
| 1370 | |
| 1371 | replaceTrigInsts(SinCalls, Sin); |
| 1372 | replaceTrigInsts(CosCalls, Cos); |
| 1373 | replaceTrigInsts(SinCosCalls, SinCos); |
| 1374 | |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | bool isTrigLibCall(CallInst *CI) { |
| 1379 | Function *Callee = CI->getCalledFunction(); |
| 1380 | FunctionType *FT = Callee->getFunctionType(); |
| 1381 | |
| 1382 | // We can only hope to do anything useful if we can ignore things like errno |
| 1383 | // and floating-point exceptions. |
| 1384 | bool AttributesSafe = CI->hasFnAttr(Attribute::NoUnwind) && |
| 1385 | CI->hasFnAttr(Attribute::ReadNone); |
| 1386 | |
| 1387 | // Other than that we need float(float) or double(double) |
| 1388 | return AttributesSafe && FT->getNumParams() == 1 && |
| 1389 | FT->getReturnType() == FT->getParamType(0) && |
| 1390 | (FT->getParamType(0)->isFloatTy() || |
| 1391 | FT->getParamType(0)->isDoubleTy()); |
| 1392 | } |
| 1393 | |
| 1394 | void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat, |
| 1395 | SmallVectorImpl<CallInst *> &SinCalls, |
| 1396 | SmallVectorImpl<CallInst *> &CosCalls, |
| 1397 | SmallVectorImpl<CallInst *> &SinCosCalls) { |
| 1398 | CallInst *CI = dyn_cast<CallInst>(Val); |
| 1399 | |
| 1400 | if (!CI) |
| 1401 | return; |
| 1402 | |
| 1403 | Function *Callee = CI->getCalledFunction(); |
| 1404 | StringRef FuncName = Callee->getName(); |
| 1405 | LibFunc::Func Func; |
| 1406 | if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) || |
| 1407 | !isTrigLibCall(CI)) |
| 1408 | return; |
| 1409 | |
| 1410 | if (IsFloat) { |
| 1411 | if (Func == LibFunc::sinpif) |
| 1412 | SinCalls.push_back(CI); |
| 1413 | else if (Func == LibFunc::cospif) |
| 1414 | CosCalls.push_back(CI); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1415 | else if (Func == LibFunc::sincospif_stret) |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 1416 | SinCosCalls.push_back(CI); |
| 1417 | } else { |
| 1418 | if (Func == LibFunc::sinpi) |
| 1419 | SinCalls.push_back(CI); |
| 1420 | else if (Func == LibFunc::cospi) |
| 1421 | CosCalls.push_back(CI); |
| 1422 | else if (Func == LibFunc::sincospi_stret) |
| 1423 | SinCosCalls.push_back(CI); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | void replaceTrigInsts(SmallVectorImpl<CallInst*> &Calls, Value *Res) { |
| 1428 | for (SmallVectorImpl<CallInst*>::iterator I = Calls.begin(), |
| 1429 | E = Calls.end(); |
| 1430 | I != E; ++I) { |
| 1431 | LCS->replaceAllUsesWith(*I, Res); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg, |
| 1436 | bool UseFloat, Value *&Sin, Value *&Cos, |
| 1437 | Value *&SinCos) { |
| 1438 | Type *ArgTy = Arg->getType(); |
| 1439 | Type *ResTy; |
| 1440 | StringRef Name; |
| 1441 | |
| 1442 | Triple T(OrigCallee->getParent()->getTargetTriple()); |
| 1443 | if (UseFloat) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1444 | Name = "__sincospif_stret"; |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 1445 | |
| 1446 | assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now"); |
| 1447 | // x86_64 can't use {float, float} since that would be returned in both |
| 1448 | // xmm0 and xmm1, which isn't what a real struct would do. |
| 1449 | ResTy = T.getArch() == Triple::x86_64 |
| 1450 | ? static_cast<Type *>(VectorType::get(ArgTy, 2)) |
| 1451 | : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL)); |
| 1452 | } else { |
| 1453 | Name = "__sincospi_stret"; |
| 1454 | ResTy = StructType::get(ArgTy, ArgTy, NULL); |
| 1455 | } |
| 1456 | |
| 1457 | Module *M = OrigCallee->getParent(); |
| 1458 | Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(), |
| 1459 | ResTy, ArgTy, NULL); |
| 1460 | |
| 1461 | if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) { |
| 1462 | // If the argument is an instruction, it must dominate all uses so put our |
| 1463 | // sincos call there. |
| 1464 | BasicBlock::iterator Loc = ArgInst; |
| 1465 | B.SetInsertPoint(ArgInst->getParent(), ++Loc); |
| 1466 | } else { |
| 1467 | // Otherwise (e.g. for a constant) the beginning of the function is as |
| 1468 | // good a place as any. |
| 1469 | BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock(); |
| 1470 | B.SetInsertPoint(&EntryBB, EntryBB.begin()); |
| 1471 | } |
| 1472 | |
| 1473 | SinCos = B.CreateCall(Callee, Arg, "sincospi"); |
| 1474 | |
| 1475 | if (SinCos->getType()->isStructTy()) { |
| 1476 | Sin = B.CreateExtractValue(SinCos, 0, "sinpi"); |
| 1477 | Cos = B.CreateExtractValue(SinCos, 1, "cospi"); |
| 1478 | } else { |
| 1479 | Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0), |
| 1480 | "sinpi"); |
| 1481 | Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1), |
| 1482 | "cospi"); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | }; |
| 1487 | |
Meador Inge | 15d099a | 2012-11-25 20:45:27 +0000 | [diff] [blame] | 1488 | //===----------------------------------------------------------------------===// |
| 1489 | // Integer Library Call Optimizations |
| 1490 | //===----------------------------------------------------------------------===// |
| 1491 | |
| 1492 | struct FFSOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1493 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1494 | IRBuilder<> &B) override { |
Meador Inge | 15d099a | 2012-11-25 20:45:27 +0000 | [diff] [blame] | 1495 | FunctionType *FT = Callee->getFunctionType(); |
| 1496 | // Just make sure this has 2 arguments of the same FP type, which match the |
| 1497 | // result type. |
| 1498 | if (FT->getNumParams() != 1 || |
| 1499 | !FT->getReturnType()->isIntegerTy(32) || |
| 1500 | !FT->getParamType(0)->isIntegerTy()) |
| 1501 | return 0; |
| 1502 | |
| 1503 | Value *Op = CI->getArgOperand(0); |
| 1504 | |
| 1505 | // Constant fold. |
| 1506 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 1507 | if (CI->isZero()) // ffs(0) -> 0. |
| 1508 | return B.getInt32(0); |
| 1509 | // ffs(c) -> cttz(c)+1 |
| 1510 | return B.getInt32(CI->getValue().countTrailingZeros() + 1); |
| 1511 | } |
| 1512 | |
| 1513 | // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0 |
| 1514 | Type *ArgType = Op->getType(); |
| 1515 | Value *F = Intrinsic::getDeclaration(Callee->getParent(), |
| 1516 | Intrinsic::cttz, ArgType); |
| 1517 | Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz"); |
| 1518 | V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1)); |
| 1519 | V = B.CreateIntCast(V, B.getInt32Ty(), false); |
| 1520 | |
| 1521 | Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType)); |
| 1522 | return B.CreateSelect(Cond, V, B.getInt32(0)); |
| 1523 | } |
| 1524 | }; |
| 1525 | |
Meador Inge | dfb3b1a | 2012-11-26 00:24:07 +0000 | [diff] [blame] | 1526 | struct AbsOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1527 | bool ignoreCallingConv() override { return true; } |
| 1528 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1529 | IRBuilder<> &B) override { |
Meador Inge | dfb3b1a | 2012-11-26 00:24:07 +0000 | [diff] [blame] | 1530 | FunctionType *FT = Callee->getFunctionType(); |
| 1531 | // We require integer(integer) where the types agree. |
| 1532 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
| 1533 | FT->getParamType(0) != FT->getReturnType()) |
| 1534 | return 0; |
| 1535 | |
| 1536 | // abs(x) -> x >s -1 ? x : -x |
| 1537 | Value *Op = CI->getArgOperand(0); |
| 1538 | Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), |
| 1539 | "ispos"); |
| 1540 | Value *Neg = B.CreateNeg(Op, "neg"); |
| 1541 | return B.CreateSelect(Pos, Op, Neg); |
| 1542 | } |
| 1543 | }; |
| 1544 | |
Meador Inge | a0798ec | 2012-11-26 02:31:59 +0000 | [diff] [blame] | 1545 | struct IsDigitOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1546 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1547 | IRBuilder<> &B) override { |
Meador Inge | a0798ec | 2012-11-26 02:31:59 +0000 | [diff] [blame] | 1548 | FunctionType *FT = Callee->getFunctionType(); |
| 1549 | // We require integer(i32) |
| 1550 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
| 1551 | !FT->getParamType(0)->isIntegerTy(32)) |
| 1552 | return 0; |
| 1553 | |
| 1554 | // isdigit(c) -> (c-'0') <u 10 |
| 1555 | Value *Op = CI->getArgOperand(0); |
| 1556 | Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp"); |
| 1557 | Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit"); |
| 1558 | return B.CreateZExt(Op, CI->getType()); |
| 1559 | } |
| 1560 | }; |
| 1561 | |
Meador Inge | 017bb75 | 2012-11-26 03:10:07 +0000 | [diff] [blame] | 1562 | struct IsAsciiOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1563 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1564 | IRBuilder<> &B) override { |
Meador Inge | 017bb75 | 2012-11-26 03:10:07 +0000 | [diff] [blame] | 1565 | FunctionType *FT = Callee->getFunctionType(); |
| 1566 | // We require integer(i32) |
| 1567 | if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() || |
| 1568 | !FT->getParamType(0)->isIntegerTy(32)) |
| 1569 | return 0; |
| 1570 | |
| 1571 | // isascii(c) -> c <u 128 |
| 1572 | Value *Op = CI->getArgOperand(0); |
| 1573 | Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii"); |
| 1574 | return B.CreateZExt(Op, CI->getType()); |
| 1575 | } |
| 1576 | }; |
| 1577 | |
Meador Inge | 38c4441 | 2012-11-26 03:38:52 +0000 | [diff] [blame] | 1578 | struct ToAsciiOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1579 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1580 | IRBuilder<> &B) override { |
Meador Inge | 38c4441 | 2012-11-26 03:38:52 +0000 | [diff] [blame] | 1581 | FunctionType *FT = Callee->getFunctionType(); |
| 1582 | // We require i32(i32) |
| 1583 | if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) || |
| 1584 | !FT->getParamType(0)->isIntegerTy(32)) |
| 1585 | return 0; |
| 1586 | |
Meador Inge | d6d6859 | 2012-11-26 20:37:23 +0000 | [diff] [blame] | 1587 | // toascii(c) -> c & 0x7f |
Meador Inge | 38c4441 | 2012-11-26 03:38:52 +0000 | [diff] [blame] | 1588 | return B.CreateAnd(CI->getArgOperand(0), |
| 1589 | ConstantInt::get(CI->getType(),0x7F)); |
| 1590 | } |
| 1591 | }; |
| 1592 | |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1593 | //===----------------------------------------------------------------------===// |
| 1594 | // Formatting and IO Library Call Optimizations |
| 1595 | //===----------------------------------------------------------------------===// |
| 1596 | |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 1597 | struct ErrorReportingOpt : public LibCallOptimization { |
| 1598 | ErrorReportingOpt(int S = -1) : StreamArg(S) {} |
| 1599 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1600 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1601 | IRBuilder<> &) override { |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 1602 | // Error reporting calls should be cold, mark them as such. |
| 1603 | // This applies even to non-builtin calls: it is only a hint and applies to |
| 1604 | // functions that the frontend might not understand as builtins. |
| 1605 | |
| 1606 | // This heuristic was suggested in: |
| 1607 | // Improving Static Branch Prediction in a Compiler |
| 1608 | // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu |
| 1609 | // Proceedings of PACT'98, Oct. 1998, IEEE |
| 1610 | |
| 1611 | if (!CI->hasFnAttr(Attribute::Cold) && isReportingError(Callee, CI)) { |
| 1612 | CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold); |
| 1613 | } |
| 1614 | |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | protected: |
| 1619 | bool isReportingError(Function *Callee, CallInst *CI) { |
| 1620 | if (!ColdErrorCalls) |
| 1621 | return false; |
| 1622 | |
| 1623 | if (!Callee || !Callee->isDeclaration()) |
| 1624 | return false; |
| 1625 | |
| 1626 | if (StreamArg < 0) |
| 1627 | return true; |
| 1628 | |
| 1629 | // These functions might be considered cold, but only if their stream |
| 1630 | // argument is stderr. |
| 1631 | |
| 1632 | if (StreamArg >= (int) CI->getNumArgOperands()) |
| 1633 | return false; |
| 1634 | LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg)); |
| 1635 | if (!LI) |
| 1636 | return false; |
| 1637 | GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()); |
| 1638 | if (!GV || !GV->isDeclaration()) |
| 1639 | return false; |
| 1640 | return GV->getName() == "stderr"; |
| 1641 | } |
| 1642 | |
| 1643 | int StreamArg; |
| 1644 | }; |
| 1645 | |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1646 | struct PrintFOpt : public LibCallOptimization { |
| 1647 | Value *optimizeFixedFormatString(Function *Callee, CallInst *CI, |
| 1648 | IRBuilder<> &B) { |
| 1649 | // Check for a fixed format string. |
| 1650 | StringRef FormatStr; |
| 1651 | if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) |
| 1652 | return 0; |
| 1653 | |
| 1654 | // Empty format string -> noop. |
| 1655 | if (FormatStr.empty()) // Tolerate printf's declared void. |
| 1656 | return CI->use_empty() ? (Value*)CI : |
| 1657 | ConstantInt::get(CI->getType(), 0); |
| 1658 | |
| 1659 | // Do not do any of the following transformations if the printf return value |
| 1660 | // is used, in general the printf return value is not compatible with either |
| 1661 | // putchar() or puts(). |
| 1662 | if (!CI->use_empty()) |
| 1663 | return 0; |
| 1664 | |
| 1665 | // printf("x") -> putchar('x'), even for '%'. |
| 1666 | if (FormatStr.size() == 1) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1667 | Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI); |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1668 | if (CI->use_empty() || !Res) return Res; |
| 1669 | return B.CreateIntCast(Res, CI->getType(), true); |
| 1670 | } |
| 1671 | |
| 1672 | // printf("foo\n") --> puts("foo") |
| 1673 | if (FormatStr[FormatStr.size()-1] == '\n' && |
Matt Arsenault | ffdee89 | 2013-09-10 00:41:53 +0000 | [diff] [blame] | 1674 | FormatStr.find('%') == StringRef::npos) { // No format characters. |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1675 | // Create a string literal with no \n on it. We expect the constant merge |
| 1676 | // pass to be run after this pass, to merge duplicate strings. |
| 1677 | FormatStr = FormatStr.drop_back(); |
| 1678 | Value *GV = B.CreateGlobalString(FormatStr, "str"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1679 | Value *NewCI = EmitPutS(GV, B, DL, TLI); |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1680 | return (CI->use_empty() || !NewCI) ? |
| 1681 | NewCI : |
| 1682 | ConstantInt::get(CI->getType(), FormatStr.size()+1); |
| 1683 | } |
| 1684 | |
| 1685 | // Optimize specific format strings. |
| 1686 | // printf("%c", chr) --> putchar(chr) |
| 1687 | if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && |
| 1688 | CI->getArgOperand(1)->getType()->isIntegerTy()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1689 | Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI); |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1690 | |
| 1691 | if (CI->use_empty() || !Res) return Res; |
| 1692 | return B.CreateIntCast(Res, CI->getType(), true); |
| 1693 | } |
| 1694 | |
| 1695 | // printf("%s\n", str) --> puts(str) |
| 1696 | if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && |
| 1697 | CI->getArgOperand(1)->getType()->isPointerTy()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1698 | return EmitPutS(CI->getArgOperand(1), B, DL, TLI); |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1699 | } |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1703 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1704 | IRBuilder<> &B) override { |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 1705 | // Require one fixed pointer argument and an integer/void result. |
| 1706 | FunctionType *FT = Callee->getFunctionType(); |
| 1707 | if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || |
| 1708 | !(FT->getReturnType()->isIntegerTy() || |
| 1709 | FT->getReturnType()->isVoidTy())) |
| 1710 | return 0; |
| 1711 | |
| 1712 | if (Value *V = optimizeFixedFormatString(Callee, CI, B)) { |
| 1713 | return V; |
| 1714 | } |
| 1715 | |
| 1716 | // printf(format, ...) -> iprintf(format, ...) if no floating point |
| 1717 | // arguments. |
| 1718 | if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) { |
| 1719 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 1720 | Constant *IPrintFFn = |
| 1721 | M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); |
| 1722 | CallInst *New = cast<CallInst>(CI->clone()); |
| 1723 | New->setCalledFunction(IPrintFFn); |
| 1724 | B.Insert(New); |
| 1725 | return New; |
| 1726 | } |
| 1727 | return 0; |
| 1728 | } |
| 1729 | }; |
| 1730 | |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1731 | struct SPrintFOpt : public LibCallOptimization { |
| 1732 | Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI, |
| 1733 | IRBuilder<> &B) { |
| 1734 | // Check for a fixed format string. |
| 1735 | StringRef FormatStr; |
| 1736 | if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) |
| 1737 | return 0; |
| 1738 | |
| 1739 | // If we just have a format string (nothing else crazy) transform it. |
| 1740 | if (CI->getNumArgOperands() == 2) { |
| 1741 | // Make sure there's no % in the constant array. We could try to handle |
| 1742 | // %% -> % in the future if we cared. |
| 1743 | for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) |
| 1744 | if (FormatStr[i] == '%') |
| 1745 | return 0; // we found a format specifier, bail out. |
| 1746 | |
| 1747 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1748 | if (!DL) return 0; |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1749 | |
| 1750 | // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1) |
| 1751 | B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1752 | ConstantInt::get(DL->getIntPtrType(*Context), // Copy the |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1753 | FormatStr.size() + 1), 1); // nul byte. |
| 1754 | return ConstantInt::get(CI->getType(), FormatStr.size()); |
| 1755 | } |
| 1756 | |
| 1757 | // The remaining optimizations require the format string to be "%s" or "%c" |
| 1758 | // and have an extra operand. |
| 1759 | if (FormatStr.size() != 2 || FormatStr[0] != '%' || |
| 1760 | CI->getNumArgOperands() < 3) |
| 1761 | return 0; |
| 1762 | |
| 1763 | // Decode the second character of the format string. |
| 1764 | if (FormatStr[1] == 'c') { |
| 1765 | // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0 |
| 1766 | if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0; |
| 1767 | Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char"); |
| 1768 | Value *Ptr = CastToCStr(CI->getArgOperand(0), B); |
| 1769 | B.CreateStore(V, Ptr); |
| 1770 | Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul"); |
| 1771 | B.CreateStore(B.getInt8(0), Ptr); |
| 1772 | |
| 1773 | return ConstantInt::get(CI->getType(), 1); |
| 1774 | } |
| 1775 | |
| 1776 | if (FormatStr[1] == 's') { |
| 1777 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1778 | if (!DL) return 0; |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1779 | |
| 1780 | // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1) |
| 1781 | if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0; |
| 1782 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1783 | Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI); |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1784 | if (!Len) |
| 1785 | return 0; |
| 1786 | Value *IncLen = B.CreateAdd(Len, |
| 1787 | ConstantInt::get(Len->getType(), 1), |
| 1788 | "leninc"); |
| 1789 | B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1); |
| 1790 | |
| 1791 | // The sprintf result is the unincremented number of bytes in the string. |
| 1792 | return B.CreateIntCast(Len, CI->getType(), false); |
| 1793 | } |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1797 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1798 | IRBuilder<> &B) override { |
Meador Inge | 69ea027 | 2012-11-27 05:57:54 +0000 | [diff] [blame] | 1799 | // Require two fixed pointer arguments and an integer result. |
| 1800 | FunctionType *FT = Callee->getFunctionType(); |
| 1801 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1802 | !FT->getParamType(1)->isPointerTy() || |
| 1803 | !FT->getReturnType()->isIntegerTy()) |
| 1804 | return 0; |
| 1805 | |
| 1806 | if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) { |
| 1807 | return V; |
| 1808 | } |
| 1809 | |
| 1810 | // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating |
| 1811 | // point arguments. |
| 1812 | if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) { |
| 1813 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 1814 | Constant *SIPrintFFn = |
| 1815 | M->getOrInsertFunction("siprintf", FT, Callee->getAttributes()); |
| 1816 | CallInst *New = cast<CallInst>(CI->clone()); |
| 1817 | New->setCalledFunction(SIPrintFFn); |
| 1818 | B.Insert(New); |
| 1819 | return New; |
| 1820 | } |
| 1821 | return 0; |
| 1822 | } |
| 1823 | }; |
| 1824 | |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1825 | struct FPrintFOpt : public LibCallOptimization { |
| 1826 | Value *optimizeFixedFormatString(Function *Callee, CallInst *CI, |
| 1827 | IRBuilder<> &B) { |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 1828 | ErrorReportingOpt ER(/* StreamArg = */ 0); |
| 1829 | (void) ER.callOptimizer(Callee, CI, B); |
| 1830 | |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1831 | // All the optimizations depend on the format string. |
| 1832 | StringRef FormatStr; |
| 1833 | if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) |
| 1834 | return 0; |
| 1835 | |
Peter Collingbourne | c7ab4f9 | 2013-04-17 02:01:10 +0000 | [diff] [blame] | 1836 | // Do not do any of the following transformations if the fprintf return |
| 1837 | // value is used, in general the fprintf return value is not compatible |
| 1838 | // with fwrite(), fputc() or fputs(). |
| 1839 | if (!CI->use_empty()) |
| 1840 | return 0; |
| 1841 | |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1842 | // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) |
| 1843 | if (CI->getNumArgOperands() == 2) { |
| 1844 | for (unsigned i = 0, e = FormatStr.size(); i != e; ++i) |
| 1845 | if (FormatStr[i] == '%') // Could handle %% -> % if we cared. |
| 1846 | return 0; // We found a format specifier. |
| 1847 | |
| 1848 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1849 | if (!DL) return 0; |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1850 | |
Peter Collingbourne | c7ab4f9 | 2013-04-17 02:01:10 +0000 | [diff] [blame] | 1851 | return EmitFWrite(CI->getArgOperand(1), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1852 | ConstantInt::get(DL->getIntPtrType(*Context), |
Peter Collingbourne | c7ab4f9 | 2013-04-17 02:01:10 +0000 | [diff] [blame] | 1853 | FormatStr.size()), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1854 | CI->getArgOperand(0), B, DL, TLI); |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | // The remaining optimizations require the format string to be "%s" or "%c" |
| 1858 | // and have an extra operand. |
| 1859 | if (FormatStr.size() != 2 || FormatStr[0] != '%' || |
| 1860 | CI->getNumArgOperands() < 3) |
| 1861 | return 0; |
| 1862 | |
| 1863 | // Decode the second character of the format string. |
| 1864 | if (FormatStr[1] == 'c') { |
| 1865 | // fprintf(F, "%c", chr) --> fputc(chr, F) |
| 1866 | if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1867 | return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI); |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | if (FormatStr[1] == 's') { |
| 1871 | // fprintf(F, "%s", str) --> fputs(str, F) |
Peter Collingbourne | c7ab4f9 | 2013-04-17 02:01:10 +0000 | [diff] [blame] | 1872 | if (!CI->getArgOperand(2)->getType()->isPointerTy()) |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1873 | return 0; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1874 | return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI); |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1875 | } |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1879 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1880 | IRBuilder<> &B) override { |
Meador Inge | 28d5291 | 2012-11-29 15:45:33 +0000 | [diff] [blame] | 1881 | // Require two fixed paramters as pointers and integer result. |
| 1882 | FunctionType *FT = Callee->getFunctionType(); |
| 1883 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1884 | !FT->getParamType(1)->isPointerTy() || |
| 1885 | !FT->getReturnType()->isIntegerTy()) |
| 1886 | return 0; |
| 1887 | |
| 1888 | if (Value *V = optimizeFixedFormatString(Callee, CI, B)) { |
| 1889 | return V; |
| 1890 | } |
| 1891 | |
| 1892 | // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no |
| 1893 | // floating point arguments. |
| 1894 | if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) { |
| 1895 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 1896 | Constant *FIPrintFFn = |
| 1897 | M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes()); |
| 1898 | CallInst *New = cast<CallInst>(CI->clone()); |
| 1899 | New->setCalledFunction(FIPrintFFn); |
| 1900 | B.Insert(New); |
| 1901 | return New; |
| 1902 | } |
| 1903 | return 0; |
| 1904 | } |
| 1905 | }; |
| 1906 | |
Meador Inge | c2e3312 | 2012-11-29 15:45:39 +0000 | [diff] [blame] | 1907 | struct FWriteOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1908 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1909 | IRBuilder<> &B) override { |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 1910 | ErrorReportingOpt ER(/* StreamArg = */ 3); |
| 1911 | (void) ER.callOptimizer(Callee, CI, B); |
| 1912 | |
Meador Inge | c2e3312 | 2012-11-29 15:45:39 +0000 | [diff] [blame] | 1913 | // Require a pointer, an integer, an integer, a pointer, returning integer. |
| 1914 | FunctionType *FT = Callee->getFunctionType(); |
| 1915 | if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() || |
| 1916 | !FT->getParamType(1)->isIntegerTy() || |
| 1917 | !FT->getParamType(2)->isIntegerTy() || |
| 1918 | !FT->getParamType(3)->isPointerTy() || |
| 1919 | !FT->getReturnType()->isIntegerTy()) |
| 1920 | return 0; |
| 1921 | |
| 1922 | // Get the element size and count. |
| 1923 | ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 1924 | ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); |
| 1925 | if (!SizeC || !CountC) return 0; |
| 1926 | uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue(); |
| 1927 | |
| 1928 | // If this is writing zero records, remove the call (it's a noop). |
| 1929 | if (Bytes == 0) |
| 1930 | return ConstantInt::get(CI->getType(), 0); |
| 1931 | |
| 1932 | // If this is writing one byte, turn it into fputc. |
| 1933 | // This optimisation is only valid, if the return value is unused. |
| 1934 | if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F) |
| 1935 | Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1936 | Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI); |
Meador Inge | c2e3312 | 2012-11-29 15:45:39 +0000 | [diff] [blame] | 1937 | return NewCI ? ConstantInt::get(CI->getType(), 1) : 0; |
| 1938 | } |
| 1939 | |
| 1940 | return 0; |
| 1941 | } |
| 1942 | }; |
| 1943 | |
Meador Inge | 5c5e230 | 2012-11-29 15:45:43 +0000 | [diff] [blame] | 1944 | struct FPutsOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1945 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1946 | IRBuilder<> &B) override { |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 1947 | ErrorReportingOpt ER(/* StreamArg = */ 1); |
| 1948 | (void) ER.callOptimizer(Callee, CI, B); |
| 1949 | |
Meador Inge | 5c5e230 | 2012-11-29 15:45:43 +0000 | [diff] [blame] | 1950 | // These optimizations require DataLayout. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1951 | if (!DL) return 0; |
Meador Inge | 5c5e230 | 2012-11-29 15:45:43 +0000 | [diff] [blame] | 1952 | |
| 1953 | // Require two pointers. Also, we can't optimize if return value is used. |
| 1954 | FunctionType *FT = Callee->getFunctionType(); |
| 1955 | if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || |
| 1956 | !FT->getParamType(1)->isPointerTy() || |
| 1957 | !CI->use_empty()) |
| 1958 | return 0; |
| 1959 | |
| 1960 | // fputs(s,F) --> fwrite(s,1,strlen(s),F) |
| 1961 | uint64_t Len = GetStringLength(CI->getArgOperand(0)); |
| 1962 | if (!Len) return 0; |
| 1963 | // Known to have no uses (see above). |
| 1964 | return EmitFWrite(CI->getArgOperand(0), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1965 | ConstantInt::get(DL->getIntPtrType(*Context), Len-1), |
| 1966 | CI->getArgOperand(1), B, DL, TLI); |
Meador Inge | 5c5e230 | 2012-11-29 15:45:43 +0000 | [diff] [blame] | 1967 | } |
| 1968 | }; |
| 1969 | |
Meador Inge | aa8cccf | 2012-11-29 19:15:17 +0000 | [diff] [blame] | 1970 | struct PutsOpt : public LibCallOptimization { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1971 | Value *callOptimizer(Function *Callee, CallInst *CI, |
| 1972 | IRBuilder<> &B) override { |
Meador Inge | aa8cccf | 2012-11-29 19:15:17 +0000 | [diff] [blame] | 1973 | // Require one fixed pointer argument and an integer/void result. |
| 1974 | FunctionType *FT = Callee->getFunctionType(); |
| 1975 | if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() || |
| 1976 | !(FT->getReturnType()->isIntegerTy() || |
| 1977 | FT->getReturnType()->isVoidTy())) |
| 1978 | return 0; |
| 1979 | |
| 1980 | // Check for a constant string. |
| 1981 | StringRef Str; |
| 1982 | if (!getConstantStringInfo(CI->getArgOperand(0), Str)) |
| 1983 | return 0; |
| 1984 | |
| 1985 | if (Str.empty() && CI->use_empty()) { |
| 1986 | // puts("") -> putchar('\n') |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1987 | Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI); |
Meador Inge | aa8cccf | 2012-11-29 19:15:17 +0000 | [diff] [blame] | 1988 | if (CI->use_empty() || !Res) return Res; |
| 1989 | return B.CreateIntCast(Res, CI->getType(), true); |
| 1990 | } |
| 1991 | |
| 1992 | return 0; |
| 1993 | } |
| 1994 | }; |
| 1995 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 1996 | } // End anonymous namespace. |
| 1997 | |
| 1998 | namespace llvm { |
| 1999 | |
| 2000 | class LibCallSimplifierImpl { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2001 | const DataLayout *DL; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2002 | const TargetLibraryInfo *TLI; |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 2003 | const LibCallSimplifier *LCS; |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2004 | bool UnsafeFPShrink; |
Meador Inge | bb51ec8 | 2012-11-11 05:11:20 +0000 | [diff] [blame] | 2005 | |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2006 | // Math library call optimizations. |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2007 | CosOpt Cos; |
| 2008 | PowOpt Pow; |
| 2009 | Exp2Opt Exp2; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2010 | public: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2011 | LibCallSimplifierImpl(const DataLayout *DL, const TargetLibraryInfo *TLI, |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2012 | const LibCallSimplifier *LCS, |
| 2013 | bool UnsafeFPShrink = false) |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2014 | : Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2015 | this->DL = DL; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2016 | this->TLI = TLI; |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 2017 | this->LCS = LCS; |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2018 | this->UnsafeFPShrink = UnsafeFPShrink; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | Value *optimizeCall(CallInst *CI); |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2022 | LibCallOptimization *lookupOptimization(CallInst *CI); |
| 2023 | bool hasFloatVersion(StringRef FuncName); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2024 | }; |
| 2025 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2026 | bool LibCallSimplifierImpl::hasFloatVersion(StringRef FuncName) { |
| 2027 | LibFunc::Func Func; |
| 2028 | SmallString<20> FloatFuncName = FuncName; |
| 2029 | FloatFuncName += 'f'; |
| 2030 | if (TLI->getLibFunc(FloatFuncName, Func)) |
| 2031 | return TLI->has(Func); |
| 2032 | return false; |
| 2033 | } |
Meador Inge | 73d8a58 | 2012-10-13 16:45:32 +0000 | [diff] [blame] | 2034 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2035 | // Fortified library call optimizations. |
| 2036 | static MemCpyChkOpt MemCpyChk; |
| 2037 | static MemMoveChkOpt MemMoveChk; |
| 2038 | static MemSetChkOpt MemSetChk; |
| 2039 | static StrCpyChkOpt StrCpyChk; |
| 2040 | static StpCpyChkOpt StpCpyChk; |
| 2041 | static StrNCpyChkOpt StrNCpyChk; |
Meador Inge | bb51ec8 | 2012-11-11 05:11:20 +0000 | [diff] [blame] | 2042 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2043 | // String library call optimizations. |
| 2044 | static StrCatOpt StrCat; |
| 2045 | static StrNCatOpt StrNCat; |
| 2046 | static StrChrOpt StrChr; |
| 2047 | static StrRChrOpt StrRChr; |
| 2048 | static StrCmpOpt StrCmp; |
| 2049 | static StrNCmpOpt StrNCmp; |
| 2050 | static StrCpyOpt StrCpy; |
| 2051 | static StpCpyOpt StpCpy; |
| 2052 | static StrNCpyOpt StrNCpy; |
| 2053 | static StrLenOpt StrLen; |
| 2054 | static StrPBrkOpt StrPBrk; |
| 2055 | static StrToOpt StrTo; |
| 2056 | static StrSpnOpt StrSpn; |
| 2057 | static StrCSpnOpt StrCSpn; |
| 2058 | static StrStrOpt StrStr; |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2059 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2060 | // Memory library call optimizations. |
| 2061 | static MemCmpOpt MemCmp; |
| 2062 | static MemCpyOpt MemCpy; |
| 2063 | static MemMoveOpt MemMove; |
| 2064 | static MemSetOpt MemSet; |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2065 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2066 | // Math library call optimizations. |
| 2067 | static UnaryDoubleFPOpt UnaryDoubleFP(false); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2068 | static BinaryDoubleFPOpt BinaryDoubleFP(false); |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2069 | static UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true); |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 2070 | static SinCosPiOpt SinCosPi; |
Meador Inge | 15d099a | 2012-11-25 20:45:27 +0000 | [diff] [blame] | 2071 | |
| 2072 | // Integer library call optimizations. |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2073 | static FFSOpt FFS; |
| 2074 | static AbsOpt Abs; |
| 2075 | static IsDigitOpt IsDigit; |
| 2076 | static IsAsciiOpt IsAscii; |
| 2077 | static ToAsciiOpt ToAscii; |
Meador Inge | d7aa323 | 2012-11-26 20:37:20 +0000 | [diff] [blame] | 2078 | |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2079 | // Formatting and IO library call optimizations. |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 2080 | static ErrorReportingOpt ErrorReporting; |
| 2081 | static ErrorReportingOpt ErrorReporting0(0); |
| 2082 | static ErrorReportingOpt ErrorReporting1(1); |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2083 | static PrintFOpt PrintF; |
| 2084 | static SPrintFOpt SPrintF; |
| 2085 | static FPrintFOpt FPrintF; |
| 2086 | static FWriteOpt FWrite; |
| 2087 | static FPutsOpt FPuts; |
| 2088 | static PutsOpt Puts; |
| 2089 | |
| 2090 | LibCallOptimization *LibCallSimplifierImpl::lookupOptimization(CallInst *CI) { |
| 2091 | LibFunc::Func Func; |
| 2092 | Function *Callee = CI->getCalledFunction(); |
| 2093 | StringRef FuncName = Callee->getName(); |
| 2094 | |
| 2095 | // Next check for intrinsics. |
| 2096 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) { |
| 2097 | switch (II->getIntrinsicID()) { |
| 2098 | case Intrinsic::pow: |
| 2099 | return &Pow; |
| 2100 | case Intrinsic::exp2: |
| 2101 | return &Exp2; |
| 2102 | default: |
| 2103 | return 0; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | // Then check for known library functions. |
| 2108 | if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) { |
| 2109 | switch (Func) { |
| 2110 | case LibFunc::strcat: |
| 2111 | return &StrCat; |
| 2112 | case LibFunc::strncat: |
| 2113 | return &StrNCat; |
| 2114 | case LibFunc::strchr: |
| 2115 | return &StrChr; |
| 2116 | case LibFunc::strrchr: |
| 2117 | return &StrRChr; |
| 2118 | case LibFunc::strcmp: |
| 2119 | return &StrCmp; |
| 2120 | case LibFunc::strncmp: |
| 2121 | return &StrNCmp; |
| 2122 | case LibFunc::strcpy: |
| 2123 | return &StrCpy; |
| 2124 | case LibFunc::stpcpy: |
| 2125 | return &StpCpy; |
| 2126 | case LibFunc::strncpy: |
| 2127 | return &StrNCpy; |
| 2128 | case LibFunc::strlen: |
| 2129 | return &StrLen; |
| 2130 | case LibFunc::strpbrk: |
| 2131 | return &StrPBrk; |
| 2132 | case LibFunc::strtol: |
| 2133 | case LibFunc::strtod: |
| 2134 | case LibFunc::strtof: |
| 2135 | case LibFunc::strtoul: |
| 2136 | case LibFunc::strtoll: |
| 2137 | case LibFunc::strtold: |
| 2138 | case LibFunc::strtoull: |
| 2139 | return &StrTo; |
| 2140 | case LibFunc::strspn: |
| 2141 | return &StrSpn; |
| 2142 | case LibFunc::strcspn: |
| 2143 | return &StrCSpn; |
| 2144 | case LibFunc::strstr: |
| 2145 | return &StrStr; |
| 2146 | case LibFunc::memcmp: |
| 2147 | return &MemCmp; |
| 2148 | case LibFunc::memcpy: |
| 2149 | return &MemCpy; |
| 2150 | case LibFunc::memmove: |
| 2151 | return &MemMove; |
| 2152 | case LibFunc::memset: |
| 2153 | return &MemSet; |
| 2154 | case LibFunc::cosf: |
| 2155 | case LibFunc::cos: |
| 2156 | case LibFunc::cosl: |
| 2157 | return &Cos; |
Bob Wilson | 208130f | 2013-11-03 06:48:38 +0000 | [diff] [blame] | 2158 | case LibFunc::sinpif: |
| 2159 | case LibFunc::sinpi: |
| 2160 | case LibFunc::cospif: |
| 2161 | case LibFunc::cospi: |
| 2162 | return &SinCosPi; |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2163 | case LibFunc::powf: |
| 2164 | case LibFunc::pow: |
| 2165 | case LibFunc::powl: |
| 2166 | return &Pow; |
| 2167 | case LibFunc::exp2l: |
| 2168 | case LibFunc::exp2: |
| 2169 | case LibFunc::exp2f: |
| 2170 | return &Exp2; |
| 2171 | case LibFunc::ffs: |
| 2172 | case LibFunc::ffsl: |
| 2173 | case LibFunc::ffsll: |
| 2174 | return &FFS; |
| 2175 | case LibFunc::abs: |
| 2176 | case LibFunc::labs: |
| 2177 | case LibFunc::llabs: |
| 2178 | return &Abs; |
| 2179 | case LibFunc::isdigit: |
| 2180 | return &IsDigit; |
| 2181 | case LibFunc::isascii: |
| 2182 | return &IsAscii; |
| 2183 | case LibFunc::toascii: |
| 2184 | return &ToAscii; |
| 2185 | case LibFunc::printf: |
| 2186 | return &PrintF; |
| 2187 | case LibFunc::sprintf: |
| 2188 | return &SPrintF; |
| 2189 | case LibFunc::fprintf: |
| 2190 | return &FPrintF; |
| 2191 | case LibFunc::fwrite: |
| 2192 | return &FWrite; |
| 2193 | case LibFunc::fputs: |
| 2194 | return &FPuts; |
| 2195 | case LibFunc::puts: |
| 2196 | return &Puts; |
Hal Finkel | 3905642 | 2013-11-17 02:06:35 +0000 | [diff] [blame] | 2197 | case LibFunc::perror: |
| 2198 | return &ErrorReporting; |
| 2199 | case LibFunc::vfprintf: |
| 2200 | case LibFunc::fiprintf: |
| 2201 | return &ErrorReporting0; |
| 2202 | case LibFunc::fputc: |
| 2203 | return &ErrorReporting1; |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2204 | case LibFunc::ceil: |
| 2205 | case LibFunc::fabs: |
| 2206 | case LibFunc::floor: |
| 2207 | case LibFunc::rint: |
| 2208 | case LibFunc::round: |
| 2209 | case LibFunc::nearbyint: |
| 2210 | case LibFunc::trunc: |
| 2211 | if (hasFloatVersion(FuncName)) |
| 2212 | return &UnaryDoubleFP; |
| 2213 | return 0; |
| 2214 | case LibFunc::acos: |
| 2215 | case LibFunc::acosh: |
| 2216 | case LibFunc::asin: |
| 2217 | case LibFunc::asinh: |
| 2218 | case LibFunc::atan: |
| 2219 | case LibFunc::atanh: |
| 2220 | case LibFunc::cbrt: |
| 2221 | case LibFunc::cosh: |
| 2222 | case LibFunc::exp: |
| 2223 | case LibFunc::exp10: |
| 2224 | case LibFunc::expm1: |
| 2225 | case LibFunc::log: |
| 2226 | case LibFunc::log10: |
| 2227 | case LibFunc::log1p: |
| 2228 | case LibFunc::log2: |
| 2229 | case LibFunc::logb: |
| 2230 | case LibFunc::sin: |
| 2231 | case LibFunc::sinh: |
| 2232 | case LibFunc::sqrt: |
| 2233 | case LibFunc::tan: |
| 2234 | case LibFunc::tanh: |
| 2235 | if (UnsafeFPShrink && hasFloatVersion(FuncName)) |
| 2236 | return &UnsafeUnaryDoubleFP; |
| 2237 | return 0; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2238 | case LibFunc::fmin: |
| 2239 | case LibFunc::fmax: |
| 2240 | if (hasFloatVersion(FuncName)) |
| 2241 | return &BinaryDoubleFP; |
| 2242 | return 0; |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2243 | case LibFunc::memcpy_chk: |
| 2244 | return &MemCpyChk; |
| 2245 | default: |
| 2246 | return 0; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | // Finally check for fortified library calls. |
| 2251 | if (FuncName.endswith("_chk")) { |
| 2252 | if (FuncName == "__memmove_chk") |
| 2253 | return &MemMoveChk; |
| 2254 | else if (FuncName == "__memset_chk") |
| 2255 | return &MemSetChk; |
| 2256 | else if (FuncName == "__strcpy_chk") |
| 2257 | return &StrCpyChk; |
| 2258 | else if (FuncName == "__stpcpy_chk") |
| 2259 | return &StpCpyChk; |
| 2260 | else if (FuncName == "__strncpy_chk") |
| 2261 | return &StrNCpyChk; |
| 2262 | else if (FuncName == "__stpncpy_chk") |
| 2263 | return &StrNCpyChk; |
| 2264 | } |
| 2265 | |
| 2266 | return 0; |
| 2267 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { |
Meador Inge | 67cb638 | 2013-03-12 00:08:29 +0000 | [diff] [blame] | 2271 | LibCallOptimization *LCO = lookupOptimization(CI); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2272 | if (LCO) { |
| 2273 | IRBuilder<> Builder(CI); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2274 | return LCO->optimizeCall(CI, DL, TLI, LCS, Builder); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2275 | } |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2279 | LibCallSimplifier::LibCallSimplifier(const DataLayout *DL, |
Meador Inge | 2920a71 | 2012-11-13 04:16:17 +0000 | [diff] [blame] | 2280 | const TargetLibraryInfo *TLI, |
| 2281 | bool UnsafeFPShrink) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 2282 | Impl = new LibCallSimplifierImpl(DL, TLI, this, UnsafeFPShrink); |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | LibCallSimplifier::~LibCallSimplifier() { |
| 2286 | delete Impl; |
| 2287 | } |
| 2288 | |
| 2289 | Value *LibCallSimplifier::optimizeCall(CallInst *CI) { |
Michael Gottesman | 2253a2f | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 2290 | if (CI->isNoBuiltin()) return 0; |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2291 | return Impl->optimizeCall(CI); |
| 2292 | } |
| 2293 | |
Meador Inge | b69bf6b | 2012-11-11 03:51:43 +0000 | [diff] [blame] | 2294 | void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const { |
| 2295 | I->replaceAllUsesWith(With); |
| 2296 | I->eraseFromParent(); |
| 2297 | } |
| 2298 | |
Meador Inge | 5e89045 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2299 | } |
Meador Inge | be87bce | 2013-06-20 19:48:07 +0000 | [diff] [blame] | 2300 | |
| 2301 | // TODO: |
| 2302 | // Additional cases that we need to add to this file: |
| 2303 | // |
| 2304 | // cbrt: |
| 2305 | // * cbrt(expN(X)) -> expN(x/3) |
| 2306 | // * cbrt(sqrt(x)) -> pow(x,1/6) |
| 2307 | // * cbrt(sqrt(x)) -> pow(x,1/9) |
| 2308 | // |
| 2309 | // exp, expf, expl: |
| 2310 | // * exp(log(x)) -> x |
| 2311 | // |
| 2312 | // log, logf, logl: |
| 2313 | // * log(exp(x)) -> x |
| 2314 | // * log(x**y) -> y*log(x) |
| 2315 | // * log(exp(y)) -> y*log(e) |
| 2316 | // * log(exp2(y)) -> y*log(2) |
| 2317 | // * log(exp10(y)) -> y*log(10) |
| 2318 | // * log(sqrt(x)) -> 0.5*log(x) |
| 2319 | // * log(pow(x,y)) -> y*log(x) |
| 2320 | // |
| 2321 | // lround, lroundf, lroundl: |
| 2322 | // * lround(cnst) -> cnst' |
| 2323 | // |
| 2324 | // pow, powf, powl: |
| 2325 | // * pow(exp(x),y) -> exp(x*y) |
| 2326 | // * pow(sqrt(x),y) -> pow(x,y*0.5) |
| 2327 | // * pow(pow(x,y),z)-> pow(x,y*z) |
| 2328 | // |
| 2329 | // round, roundf, roundl: |
| 2330 | // * round(cnst) -> cnst' |
| 2331 | // |
| 2332 | // signbit: |
| 2333 | // * signbit(cnst) -> cnst' |
| 2334 | // * signbit(nncst) -> 0 (if pstv is a non-negative constant) |
| 2335 | // |
| 2336 | // sqrt, sqrtf, sqrtl: |
| 2337 | // * sqrt(expN(x)) -> expN(x*0.5) |
| 2338 | // * sqrt(Nroot(x)) -> pow(x,1/(2*N)) |
| 2339 | // * sqrt(pow(x,y)) -> pow(|x|,y*0.5) |
| 2340 | // |
Meador Inge | be87bce | 2013-06-20 19:48:07 +0000 | [diff] [blame] | 2341 | // tan, tanf, tanl: |
| 2342 | // * tan(atan(x)) -> x |
| 2343 | // |
| 2344 | // trunc, truncf, truncl: |
| 2345 | // * trunc(cnst) -> cnst' |
| 2346 | // |
| 2347 | // |