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