1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the library calls simplifier. It does not implement 10 // any pass, but can't be used by other passes to do simplifications. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/SimplifyLibCalls.h" 15 #include "llvm/ADT/APSInt.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringMap.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Analysis/BlockFrequencyInfo.h" 20 #include "llvm/Analysis/ConstantFolding.h" 21 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 22 #include "llvm/Analysis/ProfileSummaryInfo.h" 23 #include "llvm/Transforms/Utils/Local.h" 24 #include "llvm/Analysis/ValueTracking.h" 25 #include "llvm/Analysis/CaptureTracking.h" 26 #include "llvm/Analysis/Loads.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/IRBuilder.h" 30 #include "llvm/IR/IntrinsicInst.h" 31 #include "llvm/IR/Intrinsics.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/PatternMatch.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/KnownBits.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Transforms/Utils/BuildLibCalls.h" 39 #include "llvm/Transforms/Utils/SizeOpts.h" 40 41 using namespace llvm; 42 using namespace PatternMatch; 43 44 static cl::opt<bool> 45 EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden, 46 cl::init(false), 47 cl::desc("Enable unsafe double to float " 48 "shrinking for math lib calls")); 49 50 //===----------------------------------------------------------------------===// 51 // Helper Functions 52 //===----------------------------------------------------------------------===// 53 54 static bool ignoreCallingConv(LibFunc Func) { 55 return Func == LibFunc_abs || Func == LibFunc_labs || 56 Func == LibFunc_llabs || Func == LibFunc_strlen; 57 } 58 59 /// Return true if it is only used in equality comparisons with With. 60 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) { 61 for (User *U : V->users()) { 62 if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) 63 if (IC->isEquality() && IC->getOperand(1) == With) 64 continue; 65 // Unknown instruction. 66 return false; 67 } 68 return true; 69 } 70 71 static bool callHasFloatingPointArgument(const CallInst *CI) { 72 return any_of(CI->operands(), [](const Use &OI) { 73 return OI->getType()->isFloatingPointTy(); 74 }); 75 } 76 77 static bool callHasFP128Argument(const CallInst *CI) { 78 return any_of(CI->operands(), [](const Use &OI) { 79 return OI->getType()->isFP128Ty(); 80 }); 81 } 82 83 static Value *convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base) { 84 if (Base < 2 || Base > 36) 85 // handle special zero base 86 if (Base != 0) 87 return nullptr; 88 89 char *End; 90 std::string nptr = Str.str(); 91 errno = 0; 92 long long int Result = strtoll(nptr.c_str(), &End, Base); 93 if (errno) 94 return nullptr; 95 96 // if we assume all possible target locales are ASCII supersets, 97 // then if strtoll successfully parses a number on the host, 98 // it will also successfully parse the same way on the target 99 if (*End != '\0') 100 return nullptr; 101 102 if (!isIntN(CI->getType()->getPrimitiveSizeInBits(), Result)) 103 return nullptr; 104 105 return ConstantInt::get(CI->getType(), Result); 106 } 107 108 static bool isOnlyUsedInComparisonWithZero(Value *V) { 109 for (User *U : V->users()) { 110 if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) 111 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) 112 if (C->isNullValue()) 113 continue; 114 // Unknown instruction. 115 return false; 116 } 117 return true; 118 } 119 120 static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len, 121 const DataLayout &DL) { 122 if (!isOnlyUsedInComparisonWithZero(CI)) 123 return false; 124 125 if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL)) 126 return false; 127 128 if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory)) 129 return false; 130 131 return true; 132 } 133 134 static void annotateDereferenceableBytes(CallInst *CI, 135 ArrayRef<unsigned> ArgNos, 136 uint64_t DereferenceableBytes) { 137 const Function *F = CI->getCaller(); 138 if (!F) 139 return; 140 for (unsigned ArgNo : ArgNos) { 141 uint64_t DerefBytes = DereferenceableBytes; 142 unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace(); 143 if (!llvm::NullPointerIsDefined(F, AS) || 144 CI->paramHasAttr(ArgNo, Attribute::NonNull)) 145 DerefBytes = std::max(CI->getDereferenceableOrNullBytes( 146 ArgNo + AttributeList::FirstArgIndex), 147 DereferenceableBytes); 148 149 if (CI->getDereferenceableBytes(ArgNo + AttributeList::FirstArgIndex) < 150 DerefBytes) { 151 CI->removeParamAttr(ArgNo, Attribute::Dereferenceable); 152 if (!llvm::NullPointerIsDefined(F, AS) || 153 CI->paramHasAttr(ArgNo, Attribute::NonNull)) 154 CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull); 155 CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes( 156 CI->getContext(), DerefBytes)); 157 } 158 } 159 } 160 161 static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI, 162 ArrayRef<unsigned> ArgNos) { 163 Function *F = CI->getCaller(); 164 if (!F) 165 return; 166 167 for (unsigned ArgNo : ArgNos) { 168 if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef)) 169 CI->addParamAttr(ArgNo, Attribute::NoUndef); 170 171 if (CI->paramHasAttr(ArgNo, Attribute::NonNull)) 172 continue; 173 unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace(); 174 if (llvm::NullPointerIsDefined(F, AS)) 175 continue; 176 177 CI->addParamAttr(ArgNo, Attribute::NonNull); 178 annotateDereferenceableBytes(CI, ArgNo, 1); 179 } 180 } 181 182 static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos, 183 Value *Size, const DataLayout &DL) { 184 if (ConstantInt *LenC = dyn_cast<ConstantInt>(Size)) { 185 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos); 186 annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue()); 187 } else if (isKnownNonZero(Size, DL)) { 188 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos); 189 const APInt *X, *Y; 190 uint64_t DerefMin = 1; 191 if (match(Size, m_Select(m_Value(), m_APInt(X), m_APInt(Y)))) { 192 DerefMin = std::min(X->getZExtValue(), Y->getZExtValue()); 193 annotateDereferenceableBytes(CI, ArgNos, DerefMin); 194 } 195 } 196 } 197 198 //===----------------------------------------------------------------------===// 199 // String and Memory Library Call Optimizations 200 //===----------------------------------------------------------------------===// 201 202 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) { 203 // Extract some information from the instruction 204 Value *Dst = CI->getArgOperand(0); 205 Value *Src = CI->getArgOperand(1); 206 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 207 208 // See if we can get the length of the input string. 209 uint64_t Len = GetStringLength(Src); 210 if (Len) 211 annotateDereferenceableBytes(CI, 1, Len); 212 else 213 return nullptr; 214 --Len; // Unbias length. 215 216 // Handle the simple, do-nothing case: strcat(x, "") -> x 217 if (Len == 0) 218 return Dst; 219 220 return emitStrLenMemCpy(Src, Dst, Len, B); 221 } 222 223 Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, 224 IRBuilderBase &B) { 225 // We need to find the end of the destination string. That's where the 226 // memory is to be moved to. We just generate a call to strlen. 227 Value *DstLen = emitStrLen(Dst, B, DL, TLI); 228 if (!DstLen) 229 return nullptr; 230 231 // Now that we have the destination's length, we must index into the 232 // destination's pointer to get the actual memcpy destination (end of 233 // the string .. we're concatenating). 234 Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr"); 235 236 // We have enough information to now generate the memcpy call to do the 237 // concatenation for us. Make a memcpy to copy the nul byte with align = 1. 238 B.CreateMemCpy( 239 CpyDst, Align(1), Src, Align(1), 240 ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1)); 241 return Dst; 242 } 243 244 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) { 245 // Extract some information from the instruction. 246 Value *Dst = CI->getArgOperand(0); 247 Value *Src = CI->getArgOperand(1); 248 Value *Size = CI->getArgOperand(2); 249 uint64_t Len; 250 annotateNonNullNoUndefBasedOnAccess(CI, 0); 251 if (isKnownNonZero(Size, DL)) 252 annotateNonNullNoUndefBasedOnAccess(CI, 1); 253 254 // We don't do anything if length is not constant. 255 ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size); 256 if (LengthArg) { 257 Len = LengthArg->getZExtValue(); 258 // strncat(x, c, 0) -> x 259 if (!Len) 260 return Dst; 261 } else { 262 return nullptr; 263 } 264 265 // See if we can get the length of the input string. 266 uint64_t SrcLen = GetStringLength(Src); 267 if (SrcLen) { 268 annotateDereferenceableBytes(CI, 1, SrcLen); 269 --SrcLen; // Unbias length. 270 } else { 271 return nullptr; 272 } 273 274 // strncat(x, "", c) -> x 275 if (SrcLen == 0) 276 return Dst; 277 278 // We don't optimize this case. 279 if (Len < SrcLen) 280 return nullptr; 281 282 // strncat(x, s, c) -> strcat(x, s) 283 // s is constant so the strcat can be optimized further. 284 return emitStrLenMemCpy(Src, Dst, SrcLen, B); 285 } 286 287 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) { 288 Function *Callee = CI->getCalledFunction(); 289 FunctionType *FT = Callee->getFunctionType(); 290 Value *SrcStr = CI->getArgOperand(0); 291 annotateNonNullNoUndefBasedOnAccess(CI, 0); 292 293 // If the second operand is non-constant, see if we can compute the length 294 // of the input string and turn this into memchr. 295 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 296 if (!CharC) { 297 uint64_t Len = GetStringLength(SrcStr); 298 if (Len) 299 annotateDereferenceableBytes(CI, 0, Len); 300 else 301 return nullptr; 302 if (!FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32. 303 return nullptr; 304 305 return emitMemChr(SrcStr, CI->getArgOperand(1), // include nul. 306 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), 307 B, DL, TLI); 308 } 309 310 // Otherwise, the character is a constant, see if the first argument is 311 // a string literal. If so, we can constant fold. 312 StringRef Str; 313 if (!getConstantStringInfo(SrcStr, Str)) { 314 if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p) 315 if (Value *StrLen = emitStrLen(SrcStr, B, DL, TLI)) 316 return B.CreateGEP(B.getInt8Ty(), SrcStr, StrLen, "strchr"); 317 return nullptr; 318 } 319 320 // Compute the offset, make sure to handle the case when we're searching for 321 // zero (a weird way to spell strlen). 322 size_t I = (0xFF & CharC->getSExtValue()) == 0 323 ? Str.size() 324 : Str.find(CharC->getSExtValue()); 325 if (I == StringRef::npos) // Didn't find the char. strchr returns null. 326 return Constant::getNullValue(CI->getType()); 327 328 // strchr(s+n,c) -> gep(s+n+i,c) 329 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr"); 330 } 331 332 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) { 333 Value *SrcStr = CI->getArgOperand(0); 334 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 335 annotateNonNullNoUndefBasedOnAccess(CI, 0); 336 337 // Cannot fold anything if we're not looking for a constant. 338 if (!CharC) 339 return nullptr; 340 341 StringRef Str; 342 if (!getConstantStringInfo(SrcStr, Str)) { 343 // strrchr(s, 0) -> strchr(s, 0) 344 if (CharC->isZero()) 345 return emitStrChr(SrcStr, '\0', B, TLI); 346 return nullptr; 347 } 348 349 // Compute the offset. 350 size_t I = (0xFF & CharC->getSExtValue()) == 0 351 ? Str.size() 352 : Str.rfind(CharC->getSExtValue()); 353 if (I == StringRef::npos) // Didn't find the char. Return null. 354 return Constant::getNullValue(CI->getType()); 355 356 // strrchr(s+n,c) -> gep(s+n+i,c) 357 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr"); 358 } 359 360 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) { 361 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); 362 if (Str1P == Str2P) // strcmp(x,x) -> 0 363 return ConstantInt::get(CI->getType(), 0); 364 365 StringRef Str1, Str2; 366 bool HasStr1 = getConstantStringInfo(Str1P, Str1); 367 bool HasStr2 = getConstantStringInfo(Str2P, Str2); 368 369 // strcmp(x, y) -> cnst (if both x and y are constant strings) 370 if (HasStr1 && HasStr2) 371 return ConstantInt::get(CI->getType(), Str1.compare(Str2)); 372 373 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x 374 return B.CreateNeg(B.CreateZExt( 375 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType())); 376 377 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x 378 return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"), 379 CI->getType()); 380 381 // strcmp(P, "x") -> memcmp(P, "x", 2) 382 uint64_t Len1 = GetStringLength(Str1P); 383 if (Len1) 384 annotateDereferenceableBytes(CI, 0, Len1); 385 uint64_t Len2 = GetStringLength(Str2P); 386 if (Len2) 387 annotateDereferenceableBytes(CI, 1, Len2); 388 389 if (Len1 && Len2) { 390 return emitMemCmp(Str1P, Str2P, 391 ConstantInt::get(DL.getIntPtrType(CI->getContext()), 392 std::min(Len1, Len2)), 393 B, DL, TLI); 394 } 395 396 // strcmp to memcmp 397 if (!HasStr1 && HasStr2) { 398 if (canTransformToMemCmp(CI, Str1P, Len2, DL)) 399 return emitMemCmp( 400 Str1P, Str2P, 401 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2), B, DL, 402 TLI); 403 } else if (HasStr1 && !HasStr2) { 404 if (canTransformToMemCmp(CI, Str2P, Len1, DL)) 405 return emitMemCmp( 406 Str1P, Str2P, 407 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1), B, DL, 408 TLI); 409 } 410 411 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 412 return nullptr; 413 } 414 415 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) { 416 Value *Str1P = CI->getArgOperand(0); 417 Value *Str2P = CI->getArgOperand(1); 418 Value *Size = CI->getArgOperand(2); 419 if (Str1P == Str2P) // strncmp(x,x,n) -> 0 420 return ConstantInt::get(CI->getType(), 0); 421 422 if (isKnownNonZero(Size, DL)) 423 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 424 // Get the length argument if it is constant. 425 uint64_t Length; 426 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size)) 427 Length = LengthArg->getZExtValue(); 428 else 429 return nullptr; 430 431 if (Length == 0) // strncmp(x,y,0) -> 0 432 return ConstantInt::get(CI->getType(), 0); 433 434 if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1) 435 return emitMemCmp(Str1P, Str2P, Size, B, DL, TLI); 436 437 StringRef Str1, Str2; 438 bool HasStr1 = getConstantStringInfo(Str1P, Str1); 439 bool HasStr2 = getConstantStringInfo(Str2P, Str2); 440 441 // strncmp(x, y) -> cnst (if both x and y are constant strings) 442 if (HasStr1 && HasStr2) { 443 StringRef SubStr1 = Str1.substr(0, Length); 444 StringRef SubStr2 = Str2.substr(0, Length); 445 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2)); 446 } 447 448 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x 449 return B.CreateNeg(B.CreateZExt( 450 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType())); 451 452 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x 453 return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"), 454 CI->getType()); 455 456 uint64_t Len1 = GetStringLength(Str1P); 457 if (Len1) 458 annotateDereferenceableBytes(CI, 0, Len1); 459 uint64_t Len2 = GetStringLength(Str2P); 460 if (Len2) 461 annotateDereferenceableBytes(CI, 1, Len2); 462 463 // strncmp to memcmp 464 if (!HasStr1 && HasStr2) { 465 Len2 = std::min(Len2, Length); 466 if (canTransformToMemCmp(CI, Str1P, Len2, DL)) 467 return emitMemCmp( 468 Str1P, Str2P, 469 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2), B, DL, 470 TLI); 471 } else if (HasStr1 && !HasStr2) { 472 Len1 = std::min(Len1, Length); 473 if (canTransformToMemCmp(CI, Str2P, Len1, DL)) 474 return emitMemCmp( 475 Str1P, Str2P, 476 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1), B, DL, 477 TLI); 478 } 479 480 return nullptr; 481 } 482 483 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) { 484 Value *Src = CI->getArgOperand(0); 485 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 486 uint64_t SrcLen = GetStringLength(Src); 487 if (SrcLen && Size) { 488 annotateDereferenceableBytes(CI, 0, SrcLen); 489 if (SrcLen <= Size->getZExtValue() + 1) 490 return emitStrDup(Src, B, TLI); 491 } 492 493 return nullptr; 494 } 495 496 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) { 497 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); 498 if (Dst == Src) // strcpy(x,x) -> x 499 return Src; 500 501 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 502 // See if we can get the length of the input string. 503 uint64_t Len = GetStringLength(Src); 504 if (Len) 505 annotateDereferenceableBytes(CI, 1, Len); 506 else 507 return nullptr; 508 509 // We have enough information to now generate the memcpy call to do the 510 // copy for us. Make a memcpy to copy the nul byte with align = 1. 511 CallInst *NewCI = 512 B.CreateMemCpy(Dst, Align(1), Src, Align(1), 513 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len)); 514 NewCI->setAttributes(CI->getAttributes()); 515 NewCI->removeAttributes(AttributeList::ReturnIndex, 516 AttributeFuncs::typeIncompatible(NewCI->getType())); 517 return Dst; 518 } 519 520 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) { 521 Function *Callee = CI->getCalledFunction(); 522 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); 523 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x) 524 Value *StrLen = emitStrLen(Src, B, DL, TLI); 525 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr; 526 } 527 528 // See if we can get the length of the input string. 529 uint64_t Len = GetStringLength(Src); 530 if (Len) 531 annotateDereferenceableBytes(CI, 1, Len); 532 else 533 return nullptr; 534 535 Type *PT = Callee->getFunctionType()->getParamType(0); 536 Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len); 537 Value *DstEnd = B.CreateGEP(B.getInt8Ty(), Dst, 538 ConstantInt::get(DL.getIntPtrType(PT), Len - 1)); 539 540 // We have enough information to now generate the memcpy call to do the 541 // copy for us. Make a memcpy to copy the nul byte with align = 1. 542 CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV); 543 NewCI->setAttributes(CI->getAttributes()); 544 NewCI->removeAttributes(AttributeList::ReturnIndex, 545 AttributeFuncs::typeIncompatible(NewCI->getType())); 546 return DstEnd; 547 } 548 549 Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilderBase &B) { 550 Function *Callee = CI->getCalledFunction(); 551 Value *Dst = CI->getArgOperand(0); 552 Value *Src = CI->getArgOperand(1); 553 Value *Size = CI->getArgOperand(2); 554 annotateNonNullNoUndefBasedOnAccess(CI, 0); 555 if (isKnownNonZero(Size, DL)) 556 annotateNonNullNoUndefBasedOnAccess(CI, 1); 557 558 uint64_t Len; 559 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size)) 560 Len = LengthArg->getZExtValue(); 561 else 562 return nullptr; 563 564 // strncpy(x, y, 0) -> x 565 if (Len == 0) 566 return Dst; 567 568 // See if we can get the length of the input string. 569 uint64_t SrcLen = GetStringLength(Src); 570 if (SrcLen) { 571 annotateDereferenceableBytes(CI, 1, SrcLen); 572 --SrcLen; // Unbias length. 573 } else { 574 return nullptr; 575 } 576 577 if (SrcLen == 0) { 578 // strncpy(x, "", y) -> memset(x, '\0', y) 579 Align MemSetAlign = 580 CI->getAttributes().getParamAttributes(0).getAlignment().valueOrOne(); 581 CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign); 582 AttrBuilder ArgAttrs(CI->getAttributes().getParamAttributes(0)); 583 NewCI->setAttributes(NewCI->getAttributes().addParamAttributes( 584 CI->getContext(), 0, ArgAttrs)); 585 return Dst; 586 } 587 588 // strncpy(a, "a", 4) - > memcpy(a, "a\0\0\0", 4) 589 if (Len > SrcLen + 1) { 590 if (Len <= 128) { 591 StringRef Str; 592 if (!getConstantStringInfo(Src, Str)) 593 return nullptr; 594 std::string SrcStr = Str.str(); 595 SrcStr.resize(Len, '\0'); 596 Src = B.CreateGlobalString(SrcStr, "str"); 597 } else { 598 return nullptr; 599 } 600 } 601 602 Type *PT = Callee->getFunctionType()->getParamType(0); 603 // strncpy(x, s, c) -> memcpy(align 1 x, align 1 s, c) [s and c are constant] 604 CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), 605 ConstantInt::get(DL.getIntPtrType(PT), Len)); 606 NewCI->setAttributes(CI->getAttributes()); 607 NewCI->removeAttributes(AttributeList::ReturnIndex, 608 AttributeFuncs::typeIncompatible(NewCI->getType())); 609 return Dst; 610 } 611 612 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B, 613 unsigned CharSize) { 614 Value *Src = CI->getArgOperand(0); 615 616 // Constant folding: strlen("xyz") -> 3 617 if (uint64_t Len = GetStringLength(Src, CharSize)) 618 return ConstantInt::get(CI->getType(), Len - 1); 619 620 // If s is a constant pointer pointing to a string literal, we can fold 621 // strlen(s + x) to strlen(s) - x, when x is known to be in the range 622 // [0, strlen(s)] or the string has a single null terminator '\0' at the end. 623 // We only try to simplify strlen when the pointer s points to an array 624 // of i8. Otherwise, we would need to scale the offset x before doing the 625 // subtraction. This will make the optimization more complex, and it's not 626 // very useful because calling strlen for a pointer of other types is 627 // very uncommon. 628 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) { 629 if (!isGEPBasedOnPointerToString(GEP, CharSize)) 630 return nullptr; 631 632 ConstantDataArraySlice Slice; 633 if (getConstantDataArrayInfo(GEP->getOperand(0), Slice, CharSize)) { 634 uint64_t NullTermIdx; 635 if (Slice.Array == nullptr) { 636 NullTermIdx = 0; 637 } else { 638 NullTermIdx = ~((uint64_t)0); 639 for (uint64_t I = 0, E = Slice.Length; I < E; ++I) { 640 if (Slice.Array->getElementAsInteger(I + Slice.Offset) == 0) { 641 NullTermIdx = I; 642 break; 643 } 644 } 645 // If the string does not have '\0', leave it to strlen to compute 646 // its length. 647 if (NullTermIdx == ~((uint64_t)0)) 648 return nullptr; 649 } 650 651 Value *Offset = GEP->getOperand(2); 652 KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr); 653 Known.Zero.flipAllBits(); 654 uint64_t ArrSize = 655 cast<ArrayType>(GEP->getSourceElementType())->getNumElements(); 656 657 // KnownZero's bits are flipped, so zeros in KnownZero now represent 658 // bits known to be zeros in Offset, and ones in KnowZero represent 659 // bits unknown in Offset. Therefore, Offset is known to be in range 660 // [0, NullTermIdx] when the flipped KnownZero is non-negative and 661 // unsigned-less-than NullTermIdx. 662 // 663 // If Offset is not provably in the range [0, NullTermIdx], we can still 664 // optimize if we can prove that the program has undefined behavior when 665 // Offset is outside that range. That is the case when GEP->getOperand(0) 666 // is a pointer to an object whose memory extent is NullTermIdx+1. 667 if ((Known.Zero.isNonNegative() && Known.Zero.ule(NullTermIdx)) || 668 (GEP->isInBounds() && isa<GlobalVariable>(GEP->getOperand(0)) && 669 NullTermIdx == ArrSize - 1)) { 670 Offset = B.CreateSExtOrTrunc(Offset, CI->getType()); 671 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx), 672 Offset); 673 } 674 } 675 } 676 677 // strlen(x?"foo":"bars") --> x ? 3 : 4 678 if (SelectInst *SI = dyn_cast<SelectInst>(Src)) { 679 uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize); 680 uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize); 681 if (LenTrue && LenFalse) { 682 ORE.emit([&]() { 683 return OptimizationRemark("instcombine", "simplify-libcalls", CI) 684 << "folded strlen(select) to select of constants"; 685 }); 686 return B.CreateSelect(SI->getCondition(), 687 ConstantInt::get(CI->getType(), LenTrue - 1), 688 ConstantInt::get(CI->getType(), LenFalse - 1)); 689 } 690 } 691 692 // strlen(x) != 0 --> *x != 0 693 // strlen(x) == 0 --> *x == 0 694 if (isOnlyUsedInZeroEqualityComparison(CI)) 695 return B.CreateZExt(B.CreateLoad(B.getIntNTy(CharSize), Src, "strlenfirst"), 696 CI->getType()); 697 698 return nullptr; 699 } 700 701 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) { 702 if (Value *V = optimizeStringLength(CI, B, 8)) 703 return V; 704 annotateNonNullNoUndefBasedOnAccess(CI, 0); 705 return nullptr; 706 } 707 708 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) { 709 Module &M = *CI->getModule(); 710 unsigned WCharSize = TLI->getWCharSize(M) * 8; 711 // We cannot perform this optimization without wchar_size metadata. 712 if (WCharSize == 0) 713 return nullptr; 714 715 return optimizeStringLength(CI, B, WCharSize); 716 } 717 718 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) { 719 StringRef S1, S2; 720 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); 721 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); 722 723 // strpbrk(s, "") -> nullptr 724 // strpbrk("", s) -> nullptr 725 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) 726 return Constant::getNullValue(CI->getType()); 727 728 // Constant folding. 729 if (HasS1 && HasS2) { 730 size_t I = S1.find_first_of(S2); 731 if (I == StringRef::npos) // No match. 732 return Constant::getNullValue(CI->getType()); 733 734 return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I), 735 "strpbrk"); 736 } 737 738 // strpbrk(s, "a") -> strchr(s, 'a') 739 if (HasS2 && S2.size() == 1) 740 return emitStrChr(CI->getArgOperand(0), S2[0], B, TLI); 741 742 return nullptr; 743 } 744 745 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) { 746 Value *EndPtr = CI->getArgOperand(1); 747 if (isa<ConstantPointerNull>(EndPtr)) { 748 // With a null EndPtr, this function won't capture the main argument. 749 // It would be readonly too, except that it still may write to errno. 750 CI->addParamAttr(0, Attribute::NoCapture); 751 } 752 753 return nullptr; 754 } 755 756 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) { 757 StringRef S1, S2; 758 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); 759 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); 760 761 // strspn(s, "") -> 0 762 // strspn("", s) -> 0 763 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) 764 return Constant::getNullValue(CI->getType()); 765 766 // Constant folding. 767 if (HasS1 && HasS2) { 768 size_t Pos = S1.find_first_not_of(S2); 769 if (Pos == StringRef::npos) 770 Pos = S1.size(); 771 return ConstantInt::get(CI->getType(), Pos); 772 } 773 774 return nullptr; 775 } 776 777 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) { 778 StringRef S1, S2; 779 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); 780 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); 781 782 // strcspn("", s) -> 0 783 if (HasS1 && S1.empty()) 784 return Constant::getNullValue(CI->getType()); 785 786 // Constant folding. 787 if (HasS1 && HasS2) { 788 size_t Pos = S1.find_first_of(S2); 789 if (Pos == StringRef::npos) 790 Pos = S1.size(); 791 return ConstantInt::get(CI->getType(), Pos); 792 } 793 794 // strcspn(s, "") -> strlen(s) 795 if (HasS2 && S2.empty()) 796 return emitStrLen(CI->getArgOperand(0), B, DL, TLI); 797 798 return nullptr; 799 } 800 801 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) { 802 // fold strstr(x, x) -> x. 803 if (CI->getArgOperand(0) == CI->getArgOperand(1)) 804 return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); 805 806 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0 807 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) { 808 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI); 809 if (!StrLen) 810 return nullptr; 811 Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1), 812 StrLen, B, DL, TLI); 813 if (!StrNCmp) 814 return nullptr; 815 for (User *U : llvm::make_early_inc_range(CI->users())) { 816 ICmpInst *Old = cast<ICmpInst>(U); 817 Value *Cmp = 818 B.CreateICmp(Old->getPredicate(), StrNCmp, 819 ConstantInt::getNullValue(StrNCmp->getType()), "cmp"); 820 replaceAllUsesWith(Old, Cmp); 821 } 822 return CI; 823 } 824 825 // See if either input string is a constant string. 826 StringRef SearchStr, ToFindStr; 827 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr); 828 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr); 829 830 // fold strstr(x, "") -> x. 831 if (HasStr2 && ToFindStr.empty()) 832 return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); 833 834 // If both strings are known, constant fold it. 835 if (HasStr1 && HasStr2) { 836 size_t Offset = SearchStr.find(ToFindStr); 837 838 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null 839 return Constant::getNullValue(CI->getType()); 840 841 // strstr("abcd", "bc") -> gep((char*)"abcd", 1) 842 Value *Result = castToCStr(CI->getArgOperand(0), B); 843 Result = 844 B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), Result, Offset, "strstr"); 845 return B.CreateBitCast(Result, CI->getType()); 846 } 847 848 // fold strstr(x, "y") -> strchr(x, 'y'). 849 if (HasStr2 && ToFindStr.size() == 1) { 850 Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI); 851 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr; 852 } 853 854 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 855 return nullptr; 856 } 857 858 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) { 859 if (isKnownNonZero(CI->getOperand(2), DL)) 860 annotateNonNullNoUndefBasedOnAccess(CI, 0); 861 return nullptr; 862 } 863 864 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) { 865 Value *SrcStr = CI->getArgOperand(0); 866 Value *Size = CI->getArgOperand(2); 867 annotateNonNullAndDereferenceable(CI, 0, Size, DL); 868 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 869 ConstantInt *LenC = dyn_cast<ConstantInt>(Size); 870 871 // memchr(x, y, 0) -> null 872 if (LenC) { 873 if (LenC->isZero()) 874 return Constant::getNullValue(CI->getType()); 875 } else { 876 // From now on we need at least constant length and string. 877 return nullptr; 878 } 879 880 StringRef Str; 881 if (!getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false)) 882 return nullptr; 883 884 // Truncate the string to LenC. If Str is smaller than LenC we will still only 885 // scan the string, as reading past the end of it is undefined and we can just 886 // return null if we don't find the char. 887 Str = Str.substr(0, LenC->getZExtValue()); 888 889 // If the char is variable but the input str and length are not we can turn 890 // this memchr call into a simple bit field test. Of course this only works 891 // when the return value is only checked against null. 892 // 893 // It would be really nice to reuse switch lowering here but we can't change 894 // the CFG at this point. 895 // 896 // memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n'))) 897 // != 0 898 // after bounds check. 899 if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) { 900 unsigned char Max = 901 *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()), 902 reinterpret_cast<const unsigned char *>(Str.end())); 903 904 // Make sure the bit field we're about to create fits in a register on the 905 // target. 906 // FIXME: On a 64 bit architecture this prevents us from using the 907 // interesting range of alpha ascii chars. We could do better by emitting 908 // two bitfields or shifting the range by 64 if no lower chars are used. 909 if (!DL.fitsInLegalInteger(Max + 1)) 910 return nullptr; 911 912 // For the bit field use a power-of-2 type with at least 8 bits to avoid 913 // creating unnecessary illegal types. 914 unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max)); 915 916 // Now build the bit field. 917 APInt Bitfield(Width, 0); 918 for (char C : Str) 919 Bitfield.setBit((unsigned char)C); 920 Value *BitfieldC = B.getInt(Bitfield); 921 922 // Adjust width of "C" to the bitfield width, then mask off the high bits. 923 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType()); 924 C = B.CreateAnd(C, B.getIntN(Width, 0xFF)); 925 926 // First check that the bit field access is within bounds. 927 Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width), 928 "memchr.bounds"); 929 930 // Create code that checks if the given bit is set in the field. 931 Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C); 932 Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits"); 933 934 // Finally merge both checks and cast to pointer type. The inttoptr 935 // implicitly zexts the i1 to intptr type. 936 return B.CreateIntToPtr(B.CreateLogicalAnd(Bounds, Bits, "memchr"), 937 CI->getType()); 938 } 939 940 // Check if all arguments are constants. If so, we can constant fold. 941 if (!CharC) 942 return nullptr; 943 944 // Compute the offset. 945 size_t I = Str.find(CharC->getSExtValue() & 0xFF); 946 if (I == StringRef::npos) // Didn't find the char. memchr returns null. 947 return Constant::getNullValue(CI->getType()); 948 949 // memchr(s+n,c,l) -> gep(s+n+i,c) 950 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "memchr"); 951 } 952 953 static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS, 954 uint64_t Len, IRBuilderBase &B, 955 const DataLayout &DL) { 956 if (Len == 0) // memcmp(s1,s2,0) -> 0 957 return Constant::getNullValue(CI->getType()); 958 959 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS 960 if (Len == 1) { 961 Value *LHSV = 962 B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(LHS, B), "lhsc"), 963 CI->getType(), "lhsv"); 964 Value *RHSV = 965 B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(RHS, B), "rhsc"), 966 CI->getType(), "rhsv"); 967 return B.CreateSub(LHSV, RHSV, "chardiff"); 968 } 969 970 // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0 971 // TODO: The case where both inputs are constants does not need to be limited 972 // to legal integers or equality comparison. See block below this. 973 if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) { 974 IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8); 975 unsigned PrefAlignment = DL.getPrefTypeAlignment(IntType); 976 977 // First, see if we can fold either argument to a constant. 978 Value *LHSV = nullptr; 979 if (auto *LHSC = dyn_cast<Constant>(LHS)) { 980 LHSC = ConstantExpr::getBitCast(LHSC, IntType->getPointerTo()); 981 LHSV = ConstantFoldLoadFromConstPtr(LHSC, IntType, DL); 982 } 983 Value *RHSV = nullptr; 984 if (auto *RHSC = dyn_cast<Constant>(RHS)) { 985 RHSC = ConstantExpr::getBitCast(RHSC, IntType->getPointerTo()); 986 RHSV = ConstantFoldLoadFromConstPtr(RHSC, IntType, DL); 987 } 988 989 // Don't generate unaligned loads. If either source is constant data, 990 // alignment doesn't matter for that source because there is no load. 991 if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) && 992 (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) { 993 if (!LHSV) { 994 Type *LHSPtrTy = 995 IntType->getPointerTo(LHS->getType()->getPointerAddressSpace()); 996 LHSV = B.CreateLoad(IntType, B.CreateBitCast(LHS, LHSPtrTy), "lhsv"); 997 } 998 if (!RHSV) { 999 Type *RHSPtrTy = 1000 IntType->getPointerTo(RHS->getType()->getPointerAddressSpace()); 1001 RHSV = B.CreateLoad(IntType, B.CreateBitCast(RHS, RHSPtrTy), "rhsv"); 1002 } 1003 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp"); 1004 } 1005 } 1006 1007 // Constant folding: memcmp(x, y, Len) -> constant (all arguments are const). 1008 // TODO: This is limited to i8 arrays. 1009 StringRef LHSStr, RHSStr; 1010 if (getConstantStringInfo(LHS, LHSStr) && 1011 getConstantStringInfo(RHS, RHSStr)) { 1012 // Make sure we're not reading out-of-bounds memory. 1013 if (Len > LHSStr.size() || Len > RHSStr.size()) 1014 return nullptr; 1015 // Fold the memcmp and normalize the result. This way we get consistent 1016 // results across multiple platforms. 1017 uint64_t Ret = 0; 1018 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len); 1019 if (Cmp < 0) 1020 Ret = -1; 1021 else if (Cmp > 0) 1022 Ret = 1; 1023 return ConstantInt::get(CI->getType(), Ret); 1024 } 1025 1026 return nullptr; 1027 } 1028 1029 // Most simplifications for memcmp also apply to bcmp. 1030 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI, 1031 IRBuilderBase &B) { 1032 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1); 1033 Value *Size = CI->getArgOperand(2); 1034 1035 if (LHS == RHS) // memcmp(s,s,x) -> 0 1036 return Constant::getNullValue(CI->getType()); 1037 1038 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL); 1039 // Handle constant lengths. 1040 ConstantInt *LenC = dyn_cast<ConstantInt>(Size); 1041 if (!LenC) 1042 return nullptr; 1043 1044 // memcmp(d,s,0) -> 0 1045 if (LenC->getZExtValue() == 0) 1046 return Constant::getNullValue(CI->getType()); 1047 1048 if (Value *Res = 1049 optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL)) 1050 return Res; 1051 return nullptr; 1052 } 1053 1054 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) { 1055 if (Value *V = optimizeMemCmpBCmpCommon(CI, B)) 1056 return V; 1057 1058 // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0 1059 // bcmp can be more efficient than memcmp because it only has to know that 1060 // there is a difference, not how different one is to the other. 1061 if (TLI->has(LibFunc_bcmp) && isOnlyUsedInZeroEqualityComparison(CI)) { 1062 Value *LHS = CI->getArgOperand(0); 1063 Value *RHS = CI->getArgOperand(1); 1064 Value *Size = CI->getArgOperand(2); 1065 return emitBCmp(LHS, RHS, Size, B, DL, TLI); 1066 } 1067 1068 return nullptr; 1069 } 1070 1071 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) { 1072 return optimizeMemCmpBCmpCommon(CI, B); 1073 } 1074 1075 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) { 1076 Value *Size = CI->getArgOperand(2); 1077 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL); 1078 if (isa<IntrinsicInst>(CI)) 1079 return nullptr; 1080 1081 // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n) 1082 CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1), 1083 CI->getArgOperand(1), Align(1), Size); 1084 NewCI->setAttributes(CI->getAttributes()); 1085 NewCI->removeAttributes(AttributeList::ReturnIndex, 1086 AttributeFuncs::typeIncompatible(NewCI->getType())); 1087 return CI->getArgOperand(0); 1088 } 1089 1090 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) { 1091 Value *Dst = CI->getArgOperand(0); 1092 Value *Src = CI->getArgOperand(1); 1093 ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2)); 1094 ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3)); 1095 StringRef SrcStr; 1096 if (CI->use_empty() && Dst == Src) 1097 return Dst; 1098 // memccpy(d, s, c, 0) -> nullptr 1099 if (N) { 1100 if (N->isNullValue()) 1101 return Constant::getNullValue(CI->getType()); 1102 if (!getConstantStringInfo(Src, SrcStr, /*Offset=*/0, 1103 /*TrimAtNul=*/false) || 1104 !StopChar) 1105 return nullptr; 1106 } else { 1107 return nullptr; 1108 } 1109 1110 // Wrap arg 'c' of type int to char 1111 size_t Pos = SrcStr.find(StopChar->getSExtValue() & 0xFF); 1112 if (Pos == StringRef::npos) { 1113 if (N->getZExtValue() <= SrcStr.size()) { 1114 B.CreateMemCpy(Dst, Align(1), Src, Align(1), CI->getArgOperand(3)); 1115 return Constant::getNullValue(CI->getType()); 1116 } 1117 return nullptr; 1118 } 1119 1120 Value *NewN = 1121 ConstantInt::get(N->getType(), std::min(uint64_t(Pos + 1), N->getZExtValue())); 1122 // memccpy -> llvm.memcpy 1123 B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN); 1124 return Pos + 1 <= N->getZExtValue() 1125 ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN) 1126 : Constant::getNullValue(CI->getType()); 1127 } 1128 1129 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) { 1130 Value *Dst = CI->getArgOperand(0); 1131 Value *N = CI->getArgOperand(2); 1132 // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n 1133 CallInst *NewCI = 1134 B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N); 1135 // Propagate attributes, but memcpy has no return value, so make sure that 1136 // any return attributes are compliant. 1137 // TODO: Attach return value attributes to the 1st operand to preserve them? 1138 NewCI->setAttributes(CI->getAttributes()); 1139 NewCI->removeAttributes(AttributeList::ReturnIndex, 1140 AttributeFuncs::typeIncompatible(NewCI->getType())); 1141 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N); 1142 } 1143 1144 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) { 1145 Value *Size = CI->getArgOperand(2); 1146 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL); 1147 if (isa<IntrinsicInst>(CI)) 1148 return nullptr; 1149 1150 // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n) 1151 CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1), 1152 CI->getArgOperand(1), Align(1), Size); 1153 NewCI->setAttributes(CI->getAttributes()); 1154 NewCI->removeAttributes(AttributeList::ReturnIndex, 1155 AttributeFuncs::typeIncompatible(NewCI->getType())); 1156 return CI->getArgOperand(0); 1157 } 1158 1159 /// Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n). 1160 Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilderBase &B) { 1161 // This has to be a memset of zeros (bzero). 1162 auto *FillValue = dyn_cast<ConstantInt>(Memset->getArgOperand(1)); 1163 if (!FillValue || FillValue->getZExtValue() != 0) 1164 return nullptr; 1165 1166 // TODO: We should handle the case where the malloc has more than one use. 1167 // This is necessary to optimize common patterns such as when the result of 1168 // the malloc is checked against null or when a memset intrinsic is used in 1169 // place of a memset library call. 1170 auto *Malloc = dyn_cast<CallInst>(Memset->getArgOperand(0)); 1171 if (!Malloc || !Malloc->hasOneUse()) 1172 return nullptr; 1173 1174 // Is the inner call really malloc()? 1175 Function *InnerCallee = Malloc->getCalledFunction(); 1176 if (!InnerCallee) 1177 return nullptr; 1178 1179 LibFunc Func; 1180 if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) || 1181 Func != LibFunc_malloc) 1182 return nullptr; 1183 1184 // The memset must cover the same number of bytes that are malloc'd. 1185 if (Memset->getArgOperand(2) != Malloc->getArgOperand(0)) 1186 return nullptr; 1187 1188 // Replace the malloc with a calloc. We need the data layout to know what the 1189 // actual size of a 'size_t' parameter is. 1190 B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator()); 1191 const DataLayout &DL = Malloc->getModule()->getDataLayout(); 1192 IntegerType *SizeType = DL.getIntPtrType(B.GetInsertBlock()->getContext()); 1193 if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1), 1194 Malloc->getArgOperand(0), 1195 Malloc->getAttributes(), B, *TLI)) { 1196 substituteInParent(Malloc, Calloc); 1197 return Calloc; 1198 } 1199 1200 return nullptr; 1201 } 1202 1203 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) { 1204 Value *Size = CI->getArgOperand(2); 1205 annotateNonNullAndDereferenceable(CI, 0, Size, DL); 1206 if (isa<IntrinsicInst>(CI)) 1207 return nullptr; 1208 1209 if (auto *Calloc = foldMallocMemset(CI, B)) 1210 return Calloc; 1211 1212 // memset(p, v, n) -> llvm.memset(align 1 p, v, n) 1213 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); 1214 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1)); 1215 NewCI->setAttributes(CI->getAttributes()); 1216 NewCI->removeAttributes(AttributeList::ReturnIndex, 1217 AttributeFuncs::typeIncompatible(NewCI->getType())); 1218 return CI->getArgOperand(0); 1219 } 1220 1221 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) { 1222 if (isa<ConstantPointerNull>(CI->getArgOperand(0))) 1223 return emitMalloc(CI->getArgOperand(1), B, DL, TLI); 1224 1225 return nullptr; 1226 } 1227 1228 //===----------------------------------------------------------------------===// 1229 // Math Library Optimizations 1230 //===----------------------------------------------------------------------===// 1231 1232 // Replace a libcall \p CI with a call to intrinsic \p IID 1233 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B, 1234 Intrinsic::ID IID) { 1235 // Propagate fast-math flags from the existing call to the new call. 1236 IRBuilderBase::FastMathFlagGuard Guard(B); 1237 B.setFastMathFlags(CI->getFastMathFlags()); 1238 1239 Module *M = CI->getModule(); 1240 Value *V = CI->getArgOperand(0); 1241 Function *F = Intrinsic::getDeclaration(M, IID, CI->getType()); 1242 CallInst *NewCall = B.CreateCall(F, V); 1243 NewCall->takeName(CI); 1244 return NewCall; 1245 } 1246 1247 /// Return a variant of Val with float type. 1248 /// Currently this works in two cases: If Val is an FPExtension of a float 1249 /// value to something bigger, simply return the operand. 1250 /// If Val is a ConstantFP but can be converted to a float ConstantFP without 1251 /// loss of precision do so. 1252 static Value *valueHasFloatPrecision(Value *Val) { 1253 if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) { 1254 Value *Op = Cast->getOperand(0); 1255 if (Op->getType()->isFloatTy()) 1256 return Op; 1257 } 1258 if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) { 1259 APFloat F = Const->getValueAPF(); 1260 bool losesInfo; 1261 (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 1262 &losesInfo); 1263 if (!losesInfo) 1264 return ConstantFP::get(Const->getContext(), F); 1265 } 1266 return nullptr; 1267 } 1268 1269 /// Shrink double -> float functions. 1270 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B, 1271 bool isBinary, bool isPrecise = false) { 1272 Function *CalleeFn = CI->getCalledFunction(); 1273 if (!CI->getType()->isDoubleTy() || !CalleeFn) 1274 return nullptr; 1275 1276 // If not all the uses of the function are converted to float, then bail out. 1277 // This matters if the precision of the result is more important than the 1278 // precision of the arguments. 1279 if (isPrecise) 1280 for (User *U : CI->users()) { 1281 FPTruncInst *Cast = dyn_cast<FPTruncInst>(U); 1282 if (!Cast || !Cast->getType()->isFloatTy()) 1283 return nullptr; 1284 } 1285 1286 // If this is something like 'g((double) float)', convert to 'gf(float)'. 1287 Value *V[2]; 1288 V[0] = valueHasFloatPrecision(CI->getArgOperand(0)); 1289 V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr; 1290 if (!V[0] || (isBinary && !V[1])) 1291 return nullptr; 1292 1293 // If call isn't an intrinsic, check that it isn't within a function with the 1294 // same name as the float version of this call, otherwise the result is an 1295 // infinite loop. For example, from MinGW-w64: 1296 // 1297 // float expf(float val) { return (float) exp((double) val); } 1298 StringRef CalleeName = CalleeFn->getName(); 1299 bool IsIntrinsic = CalleeFn->isIntrinsic(); 1300 if (!IsIntrinsic) { 1301 StringRef CallerName = CI->getFunction()->getName(); 1302 if (!CallerName.empty() && CallerName.back() == 'f' && 1303 CallerName.size() == (CalleeName.size() + 1) && 1304 CallerName.startswith(CalleeName)) 1305 return nullptr; 1306 } 1307 1308 // Propagate the math semantics from the current function to the new function. 1309 IRBuilderBase::FastMathFlagGuard Guard(B); 1310 B.setFastMathFlags(CI->getFastMathFlags()); 1311 1312 // g((double) float) -> (double) gf(float) 1313 Value *R; 1314 if (IsIntrinsic) { 1315 Module *M = CI->getModule(); 1316 Intrinsic::ID IID = CalleeFn->getIntrinsicID(); 1317 Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy()); 1318 R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]); 1319 } else { 1320 AttributeList CalleeAttrs = CalleeFn->getAttributes(); 1321 R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeName, B, CalleeAttrs) 1322 : emitUnaryFloatFnCall(V[0], CalleeName, B, CalleeAttrs); 1323 } 1324 return B.CreateFPExt(R, B.getDoubleTy()); 1325 } 1326 1327 /// Shrink double -> float for unary functions. 1328 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B, 1329 bool isPrecise = false) { 1330 return optimizeDoubleFP(CI, B, false, isPrecise); 1331 } 1332 1333 /// Shrink double -> float for binary functions. 1334 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B, 1335 bool isPrecise = false) { 1336 return optimizeDoubleFP(CI, B, true, isPrecise); 1337 } 1338 1339 // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z))) 1340 Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) { 1341 if (!CI->isFast()) 1342 return nullptr; 1343 1344 // Propagate fast-math flags from the existing call to new instructions. 1345 IRBuilderBase::FastMathFlagGuard Guard(B); 1346 B.setFastMathFlags(CI->getFastMathFlags()); 1347 1348 Value *Real, *Imag; 1349 if (CI->getNumArgOperands() == 1) { 1350 Value *Op = CI->getArgOperand(0); 1351 assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!"); 1352 Real = B.CreateExtractValue(Op, 0, "real"); 1353 Imag = B.CreateExtractValue(Op, 1, "imag"); 1354 } else { 1355 assert(CI->getNumArgOperands() == 2 && "Unexpected signature for cabs!"); 1356 Real = CI->getArgOperand(0); 1357 Imag = CI->getArgOperand(1); 1358 } 1359 1360 Value *RealReal = B.CreateFMul(Real, Real); 1361 Value *ImagImag = B.CreateFMul(Imag, Imag); 1362 1363 Function *FSqrt = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::sqrt, 1364 CI->getType()); 1365 return B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs"); 1366 } 1367 1368 static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func, 1369 IRBuilderBase &B) { 1370 if (!isa<FPMathOperator>(Call)) 1371 return nullptr; 1372 1373 IRBuilderBase::FastMathFlagGuard Guard(B); 1374 B.setFastMathFlags(Call->getFastMathFlags()); 1375 1376 // TODO: Can this be shared to also handle LLVM intrinsics? 1377 Value *X; 1378 switch (Func) { 1379 case LibFunc_sin: 1380 case LibFunc_sinf: 1381 case LibFunc_sinl: 1382 case LibFunc_tan: 1383 case LibFunc_tanf: 1384 case LibFunc_tanl: 1385 // sin(-X) --> -sin(X) 1386 // tan(-X) --> -tan(X) 1387 if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) 1388 return B.CreateFNeg(B.CreateCall(Call->getCalledFunction(), X)); 1389 break; 1390 case LibFunc_cos: 1391 case LibFunc_cosf: 1392 case LibFunc_cosl: 1393 // cos(-X) --> cos(X) 1394 if (match(Call->getArgOperand(0), m_FNeg(m_Value(X)))) 1395 return B.CreateCall(Call->getCalledFunction(), X, "cos"); 1396 break; 1397 default: 1398 break; 1399 } 1400 return nullptr; 1401 } 1402 1403 static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilderBase &B) { 1404 // Multiplications calculated using Addition Chains. 1405 // Refer: http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html 1406 1407 assert(Exp != 0 && "Incorrect exponent 0 not handled"); 1408 1409 if (InnerChain[Exp]) 1410 return InnerChain[Exp]; 1411 1412 static const unsigned AddChain[33][2] = { 1413 {0, 0}, // Unused. 1414 {0, 0}, // Unused (base case = pow1). 1415 {1, 1}, // Unused (pre-computed). 1416 {1, 2}, {2, 2}, {2, 3}, {3, 3}, {2, 5}, {4, 4}, 1417 {1, 8}, {5, 5}, {1, 10}, {6, 6}, {4, 9}, {7, 7}, 1418 {3, 12}, {8, 8}, {8, 9}, {2, 16}, {1, 18}, {10, 10}, 1419 {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13}, 1420 {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16}, 1421 }; 1422 1423 InnerChain[Exp] = B.CreateFMul(getPow(InnerChain, AddChain[Exp][0], B), 1424 getPow(InnerChain, AddChain[Exp][1], B)); 1425 return InnerChain[Exp]; 1426 } 1427 1428 // Return a properly extended integer (DstWidth bits wide) if the operation is 1429 // an itofp. 1430 static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) { 1431 if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) { 1432 Value *Op = cast<Instruction>(I2F)->getOperand(0); 1433 // Make sure that the exponent fits inside an "int" of size DstWidth, 1434 // thus avoiding any range issues that FP has not. 1435 unsigned BitWidth = Op->getType()->getPrimitiveSizeInBits(); 1436 if (BitWidth < DstWidth || 1437 (BitWidth == DstWidth && isa<SIToFPInst>(I2F))) 1438 return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, B.getIntNTy(DstWidth)) 1439 : B.CreateZExt(Op, B.getIntNTy(DstWidth)); 1440 } 1441 1442 return nullptr; 1443 } 1444 1445 /// Use exp{,2}(x * y) for pow(exp{,2}(x), y); 1446 /// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x); 1447 /// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x). 1448 Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) { 1449 Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1); 1450 AttributeList Attrs; // Attributes are only meaningful on the original call 1451 Module *Mod = Pow->getModule(); 1452 Type *Ty = Pow->getType(); 1453 bool Ignored; 1454 1455 // Evaluate special cases related to a nested function as the base. 1456 1457 // pow(exp(x), y) -> exp(x * y) 1458 // pow(exp2(x), y) -> exp2(x * y) 1459 // If exp{,2}() is used only once, it is better to fold two transcendental 1460 // math functions into one. If used again, exp{,2}() would still have to be 1461 // called with the original argument, then keep both original transcendental 1462 // functions. However, this transformation is only safe with fully relaxed 1463 // math semantics, since, besides rounding differences, it changes overflow 1464 // and underflow behavior quite dramatically. For example: 1465 // pow(exp(1000), 0.001) = pow(inf, 0.001) = inf 1466 // Whereas: 1467 // exp(1000 * 0.001) = exp(1) 1468 // TODO: Loosen the requirement for fully relaxed math semantics. 1469 // TODO: Handle exp10() when more targets have it available. 1470 CallInst *BaseFn = dyn_cast<CallInst>(Base); 1471 if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) { 1472 LibFunc LibFn; 1473 1474 Function *CalleeFn = BaseFn->getCalledFunction(); 1475 if (CalleeFn && 1476 TLI->getLibFunc(CalleeFn->getName(), LibFn) && TLI->has(LibFn)) { 1477 StringRef ExpName; 1478 Intrinsic::ID ID; 1479 Value *ExpFn; 1480 LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble; 1481 1482 switch (LibFn) { 1483 default: 1484 return nullptr; 1485 case LibFunc_expf: case LibFunc_exp: case LibFunc_expl: 1486 ExpName = TLI->getName(LibFunc_exp); 1487 ID = Intrinsic::exp; 1488 LibFnFloat = LibFunc_expf; 1489 LibFnDouble = LibFunc_exp; 1490 LibFnLongDouble = LibFunc_expl; 1491 break; 1492 case LibFunc_exp2f: case LibFunc_exp2: case LibFunc_exp2l: 1493 ExpName = TLI->getName(LibFunc_exp2); 1494 ID = Intrinsic::exp2; 1495 LibFnFloat = LibFunc_exp2f; 1496 LibFnDouble = LibFunc_exp2; 1497 LibFnLongDouble = LibFunc_exp2l; 1498 break; 1499 } 1500 1501 // Create new exp{,2}() with the product as its argument. 1502 Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul"); 1503 ExpFn = BaseFn->doesNotAccessMemory() 1504 ? B.CreateCall(Intrinsic::getDeclaration(Mod, ID, Ty), 1505 FMul, ExpName) 1506 : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat, 1507 LibFnLongDouble, B, 1508 BaseFn->getAttributes()); 1509 1510 // Since the new exp{,2}() is different from the original one, dead code 1511 // elimination cannot be trusted to remove it, since it may have side 1512 // effects (e.g., errno). When the only consumer for the original 1513 // exp{,2}() is pow(), then it has to be explicitly erased. 1514 substituteInParent(BaseFn, ExpFn); 1515 return ExpFn; 1516 } 1517 } 1518 1519 // Evaluate special cases related to a constant base. 1520 1521 const APFloat *BaseF; 1522 if (!match(Pow->getArgOperand(0), m_APFloat(BaseF))) 1523 return nullptr; 1524 1525 // pow(2.0, itofp(x)) -> ldexp(1.0, x) 1526 if (match(Base, m_SpecificFP(2.0)) && 1527 (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) && 1528 hasFloatFn(TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) { 1529 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize())) 1530 return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), ExpoI, TLI, 1531 LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl, 1532 B, Attrs); 1533 } 1534 1535 // pow(2.0 ** n, x) -> exp2(n * x) 1536 if (hasFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) { 1537 APFloat BaseR = APFloat(1.0); 1538 BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored); 1539 BaseR = BaseR / *BaseF; 1540 bool IsInteger = BaseF->isInteger(), IsReciprocal = BaseR.isInteger(); 1541 const APFloat *NF = IsReciprocal ? &BaseR : BaseF; 1542 APSInt NI(64, false); 1543 if ((IsInteger || IsReciprocal) && 1544 NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) == 1545 APFloat::opOK && 1546 NI > 1 && NI.isPowerOf2()) { 1547 double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0); 1548 Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul"); 1549 if (Pow->doesNotAccessMemory()) 1550 return B.CreateCall(Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty), 1551 FMul, "exp2"); 1552 else 1553 return emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2, LibFunc_exp2f, 1554 LibFunc_exp2l, B, Attrs); 1555 } 1556 } 1557 1558 // pow(10.0, x) -> exp10(x) 1559 // TODO: There is no exp10() intrinsic yet, but some day there shall be one. 1560 if (match(Base, m_SpecificFP(10.0)) && 1561 hasFloatFn(TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l)) 1562 return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f, 1563 LibFunc_exp10l, B, Attrs); 1564 1565 // pow(x, y) -> exp2(log2(x) * y) 1566 if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() && 1567 !BaseF->isNegative()) { 1568 // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN. 1569 // Luckily optimizePow has already handled the x == 1 case. 1570 assert(!match(Base, m_FPOne()) && 1571 "pow(1.0, y) should have been simplified earlier!"); 1572 1573 Value *Log = nullptr; 1574 if (Ty->isFloatTy()) 1575 Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat())); 1576 else if (Ty->isDoubleTy()) 1577 Log = ConstantFP::get(Ty, std::log2(BaseF->convertToDouble())); 1578 1579 if (Log) { 1580 Value *FMul = B.CreateFMul(Log, Expo, "mul"); 1581 if (Pow->doesNotAccessMemory()) 1582 return B.CreateCall(Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty), 1583 FMul, "exp2"); 1584 else if (hasFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) 1585 return emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2, LibFunc_exp2f, 1586 LibFunc_exp2l, B, Attrs); 1587 } 1588 } 1589 1590 return nullptr; 1591 } 1592 1593 static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno, 1594 Module *M, IRBuilderBase &B, 1595 const TargetLibraryInfo *TLI) { 1596 // If errno is never set, then use the intrinsic for sqrt(). 1597 if (NoErrno) { 1598 Function *SqrtFn = 1599 Intrinsic::getDeclaration(M, Intrinsic::sqrt, V->getType()); 1600 return B.CreateCall(SqrtFn, V, "sqrt"); 1601 } 1602 1603 // Otherwise, use the libcall for sqrt(). 1604 if (hasFloatFn(TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf, LibFunc_sqrtl)) 1605 // TODO: We also should check that the target can in fact lower the sqrt() 1606 // libcall. We currently have no way to ask this question, so we ask if 1607 // the target has a sqrt() libcall, which is not exactly the same. 1608 return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf, 1609 LibFunc_sqrtl, B, Attrs); 1610 1611 return nullptr; 1612 } 1613 1614 /// Use square root in place of pow(x, +/-0.5). 1615 Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) { 1616 Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1); 1617 AttributeList Attrs; // Attributes are only meaningful on the original call 1618 Module *Mod = Pow->getModule(); 1619 Type *Ty = Pow->getType(); 1620 1621 const APFloat *ExpoF; 1622 if (!match(Expo, m_APFloat(ExpoF)) || 1623 (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5))) 1624 return nullptr; 1625 1626 // Converting pow(X, -0.5) to 1/sqrt(X) may introduce an extra rounding step, 1627 // so that requires fast-math-flags (afn or reassoc). 1628 if (ExpoF->isNegative() && (!Pow->hasApproxFunc() && !Pow->hasAllowReassoc())) 1629 return nullptr; 1630 1631 // If we have a pow() library call (accesses memory) and we can't guarantee 1632 // that the base is not an infinity, give up: 1633 // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting 1634 // errno), but sqrt(-Inf) is required by various standards to set errno. 1635 if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() && 1636 !isKnownNeverInfinity(Base, TLI)) 1637 return nullptr; 1638 1639 Sqrt = getSqrtCall(Base, Attrs, Pow->doesNotAccessMemory(), Mod, B, TLI); 1640 if (!Sqrt) 1641 return nullptr; 1642 1643 // Handle signed zero base by expanding to fabs(sqrt(x)). 1644 if (!Pow->hasNoSignedZeros()) { 1645 Function *FAbsFn = Intrinsic::getDeclaration(Mod, Intrinsic::fabs, Ty); 1646 Sqrt = B.CreateCall(FAbsFn, Sqrt, "abs"); 1647 } 1648 1649 // Handle non finite base by expanding to 1650 // (x == -infinity ? +infinity : sqrt(x)). 1651 if (!Pow->hasNoInfs()) { 1652 Value *PosInf = ConstantFP::getInfinity(Ty), 1653 *NegInf = ConstantFP::getInfinity(Ty, true); 1654 Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf"); 1655 Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt); 1656 } 1657 1658 // If the exponent is negative, then get the reciprocal. 1659 if (ExpoF->isNegative()) 1660 Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal"); 1661 1662 return Sqrt; 1663 } 1664 1665 static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M, 1666 IRBuilderBase &B) { 1667 Value *Args[] = {Base, Expo}; 1668 Type *Types[] = {Base->getType(), Expo->getType()}; 1669 Function *F = Intrinsic::getDeclaration(M, Intrinsic::powi, Types); 1670 return B.CreateCall(F, Args); 1671 } 1672 1673 Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) { 1674 Value *Base = Pow->getArgOperand(0); 1675 Value *Expo = Pow->getArgOperand(1); 1676 Function *Callee = Pow->getCalledFunction(); 1677 StringRef Name = Callee->getName(); 1678 Type *Ty = Pow->getType(); 1679 Module *M = Pow->getModule(); 1680 bool AllowApprox = Pow->hasApproxFunc(); 1681 bool Ignored; 1682 1683 // Propagate the math semantics from the call to any created instructions. 1684 IRBuilderBase::FastMathFlagGuard Guard(B); 1685 B.setFastMathFlags(Pow->getFastMathFlags()); 1686 // Evaluate special cases related to the base. 1687 1688 // pow(1.0, x) -> 1.0 1689 if (match(Base, m_FPOne())) 1690 return Base; 1691 1692 if (Value *Exp = replacePowWithExp(Pow, B)) 1693 return Exp; 1694 1695 // Evaluate special cases related to the exponent. 1696 1697 // pow(x, -1.0) -> 1.0 / x 1698 if (match(Expo, m_SpecificFP(-1.0))) 1699 return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal"); 1700 1701 // pow(x, +/-0.0) -> 1.0 1702 if (match(Expo, m_AnyZeroFP())) 1703 return ConstantFP::get(Ty, 1.0); 1704 1705 // pow(x, 1.0) -> x 1706 if (match(Expo, m_FPOne())) 1707 return Base; 1708 1709 // pow(x, 2.0) -> x * x 1710 if (match(Expo, m_SpecificFP(2.0))) 1711 return B.CreateFMul(Base, Base, "square"); 1712 1713 if (Value *Sqrt = replacePowWithSqrt(Pow, B)) 1714 return Sqrt; 1715 1716 // pow(x, n) -> x * x * x * ... 1717 const APFloat *ExpoF; 1718 if (AllowApprox && match(Expo, m_APFloat(ExpoF)) && 1719 !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) { 1720 // We limit to a max of 7 multiplications, thus the maximum exponent is 32. 1721 // If the exponent is an integer+0.5 we generate a call to sqrt and an 1722 // additional fmul. 1723 // TODO: This whole transformation should be backend specific (e.g. some 1724 // backends might prefer libcalls or the limit for the exponent might 1725 // be different) and it should also consider optimizing for size. 1726 APFloat LimF(ExpoF->getSemantics(), 33), 1727 ExpoA(abs(*ExpoF)); 1728 if (ExpoA < LimF) { 1729 // This transformation applies to integer or integer+0.5 exponents only. 1730 // For integer+0.5, we create a sqrt(Base) call. 1731 Value *Sqrt = nullptr; 1732 if (!ExpoA.isInteger()) { 1733 APFloat Expo2 = ExpoA; 1734 // To check if ExpoA is an integer + 0.5, we add it to itself. If there 1735 // is no floating point exception and the result is an integer, then 1736 // ExpoA == integer + 0.5 1737 if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK) 1738 return nullptr; 1739 1740 if (!Expo2.isInteger()) 1741 return nullptr; 1742 1743 Sqrt = getSqrtCall(Base, Pow->getCalledFunction()->getAttributes(), 1744 Pow->doesNotAccessMemory(), M, B, TLI); 1745 if (!Sqrt) 1746 return nullptr; 1747 } 1748 1749 // We will memoize intermediate products of the Addition Chain. 1750 Value *InnerChain[33] = {nullptr}; 1751 InnerChain[1] = Base; 1752 InnerChain[2] = B.CreateFMul(Base, Base, "square"); 1753 1754 // We cannot readily convert a non-double type (like float) to a double. 1755 // So we first convert it to something which could be converted to double. 1756 ExpoA.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &Ignored); 1757 Value *FMul = getPow(InnerChain, ExpoA.convertToDouble(), B); 1758 1759 // Expand pow(x, y+0.5) to pow(x, y) * sqrt(x). 1760 if (Sqrt) 1761 FMul = B.CreateFMul(FMul, Sqrt); 1762 1763 // If the exponent is negative, then get the reciprocal. 1764 if (ExpoF->isNegative()) 1765 FMul = B.CreateFDiv(ConstantFP::get(Ty, 1.0), FMul, "reciprocal"); 1766 1767 return FMul; 1768 } 1769 1770 APSInt IntExpo(TLI->getIntSize(), /*isUnsigned=*/false); 1771 // powf(x, n) -> powi(x, n) if n is a constant signed integer value 1772 if (ExpoF->isInteger() && 1773 ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) == 1774 APFloat::opOK) { 1775 return createPowWithIntegerExponent( 1776 Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo), M, B); 1777 } 1778 } 1779 1780 // powf(x, itofp(y)) -> powi(x, y) 1781 if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) { 1782 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize())) 1783 return createPowWithIntegerExponent(Base, ExpoI, M, B); 1784 } 1785 1786 // Shrink pow() to powf() if the arguments are single precision, 1787 // unless the result is expected to be double precision. 1788 if (UnsafeFPShrink && Name == TLI->getName(LibFunc_pow) && 1789 hasFloatVersion(Name)) { 1790 if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, true)) 1791 return Shrunk; 1792 } 1793 1794 return nullptr; 1795 } 1796 1797 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) { 1798 Function *Callee = CI->getCalledFunction(); 1799 AttributeList Attrs; // Attributes are only meaningful on the original call 1800 StringRef Name = Callee->getName(); 1801 Value *Ret = nullptr; 1802 if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) && 1803 hasFloatVersion(Name)) 1804 Ret = optimizeUnaryDoubleFP(CI, B, true); 1805 1806 Type *Ty = CI->getType(); 1807 Value *Op = CI->getArgOperand(0); 1808 1809 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= IntSize 1810 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < IntSize 1811 if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) && 1812 hasFloatFn(TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) { 1813 if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize())) 1814 return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), Exp, TLI, 1815 LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl, 1816 B, Attrs); 1817 } 1818 1819 return Ret; 1820 } 1821 1822 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) { 1823 // If we can shrink the call to a float function rather than a double 1824 // function, do that first. 1825 Function *Callee = CI->getCalledFunction(); 1826 StringRef Name = Callee->getName(); 1827 if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(Name)) 1828 if (Value *Ret = optimizeBinaryDoubleFP(CI, B)) 1829 return Ret; 1830 1831 // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to 1832 // the intrinsics for improved optimization (for example, vectorization). 1833 // No-signed-zeros is implied by the definitions of fmax/fmin themselves. 1834 // From the C standard draft WG14/N1256: 1835 // "Ideally, fmax would be sensitive to the sign of zero, for example 1836 // fmax(-0.0, +0.0) would return +0; however, implementation in software 1837 // might be impractical." 1838 IRBuilderBase::FastMathFlagGuard Guard(B); 1839 FastMathFlags FMF = CI->getFastMathFlags(); 1840 FMF.setNoSignedZeros(); 1841 B.setFastMathFlags(FMF); 1842 1843 Intrinsic::ID IID = Callee->getName().startswith("fmin") ? Intrinsic::minnum 1844 : Intrinsic::maxnum; 1845 Function *F = Intrinsic::getDeclaration(CI->getModule(), IID, CI->getType()); 1846 return B.CreateCall(F, { CI->getArgOperand(0), CI->getArgOperand(1) }); 1847 } 1848 1849 Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) { 1850 Function *LogFn = Log->getCalledFunction(); 1851 AttributeList Attrs; // Attributes are only meaningful on the original call 1852 StringRef LogNm = LogFn->getName(); 1853 Intrinsic::ID LogID = LogFn->getIntrinsicID(); 1854 Module *Mod = Log->getModule(); 1855 Type *Ty = Log->getType(); 1856 Value *Ret = nullptr; 1857 1858 if (UnsafeFPShrink && hasFloatVersion(LogNm)) 1859 Ret = optimizeUnaryDoubleFP(Log, B, true); 1860 1861 // The earlier call must also be 'fast' in order to do these transforms. 1862 CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0)); 1863 if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse()) 1864 return Ret; 1865 1866 LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb; 1867 1868 // This is only applicable to log(), log2(), log10(). 1869 if (TLI->getLibFunc(LogNm, LogLb)) 1870 switch (LogLb) { 1871 case LibFunc_logf: 1872 LogID = Intrinsic::log; 1873 ExpLb = LibFunc_expf; 1874 Exp2Lb = LibFunc_exp2f; 1875 Exp10Lb = LibFunc_exp10f; 1876 PowLb = LibFunc_powf; 1877 break; 1878 case LibFunc_log: 1879 LogID = Intrinsic::log; 1880 ExpLb = LibFunc_exp; 1881 Exp2Lb = LibFunc_exp2; 1882 Exp10Lb = LibFunc_exp10; 1883 PowLb = LibFunc_pow; 1884 break; 1885 case LibFunc_logl: 1886 LogID = Intrinsic::log; 1887 ExpLb = LibFunc_expl; 1888 Exp2Lb = LibFunc_exp2l; 1889 Exp10Lb = LibFunc_exp10l; 1890 PowLb = LibFunc_powl; 1891 break; 1892 case LibFunc_log2f: 1893 LogID = Intrinsic::log2; 1894 ExpLb = LibFunc_expf; 1895 Exp2Lb = LibFunc_exp2f; 1896 Exp10Lb = LibFunc_exp10f; 1897 PowLb = LibFunc_powf; 1898 break; 1899 case LibFunc_log2: 1900 LogID = Intrinsic::log2; 1901 ExpLb = LibFunc_exp; 1902 Exp2Lb = LibFunc_exp2; 1903 Exp10Lb = LibFunc_exp10; 1904 PowLb = LibFunc_pow; 1905 break; 1906 case LibFunc_log2l: 1907 LogID = Intrinsic::log2; 1908 ExpLb = LibFunc_expl; 1909 Exp2Lb = LibFunc_exp2l; 1910 Exp10Lb = LibFunc_exp10l; 1911 PowLb = LibFunc_powl; 1912 break; 1913 case LibFunc_log10f: 1914 LogID = Intrinsic::log10; 1915 ExpLb = LibFunc_expf; 1916 Exp2Lb = LibFunc_exp2f; 1917 Exp10Lb = LibFunc_exp10f; 1918 PowLb = LibFunc_powf; 1919 break; 1920 case LibFunc_log10: 1921 LogID = Intrinsic::log10; 1922 ExpLb = LibFunc_exp; 1923 Exp2Lb = LibFunc_exp2; 1924 Exp10Lb = LibFunc_exp10; 1925 PowLb = LibFunc_pow; 1926 break; 1927 case LibFunc_log10l: 1928 LogID = Intrinsic::log10; 1929 ExpLb = LibFunc_expl; 1930 Exp2Lb = LibFunc_exp2l; 1931 Exp10Lb = LibFunc_exp10l; 1932 PowLb = LibFunc_powl; 1933 break; 1934 default: 1935 return Ret; 1936 } 1937 else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 || 1938 LogID == Intrinsic::log10) { 1939 if (Ty->getScalarType()->isFloatTy()) { 1940 ExpLb = LibFunc_expf; 1941 Exp2Lb = LibFunc_exp2f; 1942 Exp10Lb = LibFunc_exp10f; 1943 PowLb = LibFunc_powf; 1944 } else if (Ty->getScalarType()->isDoubleTy()) { 1945 ExpLb = LibFunc_exp; 1946 Exp2Lb = LibFunc_exp2; 1947 Exp10Lb = LibFunc_exp10; 1948 PowLb = LibFunc_pow; 1949 } else 1950 return Ret; 1951 } else 1952 return Ret; 1953 1954 IRBuilderBase::FastMathFlagGuard Guard(B); 1955 B.setFastMathFlags(FastMathFlags::getFast()); 1956 1957 Intrinsic::ID ArgID = Arg->getIntrinsicID(); 1958 LibFunc ArgLb = NotLibFunc; 1959 TLI->getLibFunc(*Arg, ArgLb); 1960 1961 // log(pow(x,y)) -> y*log(x) 1962 if (ArgLb == PowLb || ArgID == Intrinsic::pow) { 1963 Value *LogX = 1964 Log->doesNotAccessMemory() 1965 ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty), 1966 Arg->getOperand(0), "log") 1967 : emitUnaryFloatFnCall(Arg->getOperand(0), LogNm, B, Attrs); 1968 Value *MulY = B.CreateFMul(Arg->getArgOperand(1), LogX, "mul"); 1969 // Since pow() may have side effects, e.g. errno, 1970 // dead code elimination may not be trusted to remove it. 1971 substituteInParent(Arg, MulY); 1972 return MulY; 1973 } 1974 1975 // log(exp{,2,10}(y)) -> y*log({e,2,10}) 1976 // TODO: There is no exp10() intrinsic yet. 1977 if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb || 1978 ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) { 1979 Constant *Eul; 1980 if (ArgLb == ExpLb || ArgID == Intrinsic::exp) 1981 // FIXME: Add more precise value of e for long double. 1982 Eul = ConstantFP::get(Log->getType(), numbers::e); 1983 else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2) 1984 Eul = ConstantFP::get(Log->getType(), 2.0); 1985 else 1986 Eul = ConstantFP::get(Log->getType(), 10.0); 1987 Value *LogE = Log->doesNotAccessMemory() 1988 ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty), 1989 Eul, "log") 1990 : emitUnaryFloatFnCall(Eul, LogNm, B, Attrs); 1991 Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul"); 1992 // Since exp() may have side effects, e.g. errno, 1993 // dead code elimination may not be trusted to remove it. 1994 substituteInParent(Arg, MulY); 1995 return MulY; 1996 } 1997 1998 return Ret; 1999 } 2000 2001 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) { 2002 Function *Callee = CI->getCalledFunction(); 2003 Value *Ret = nullptr; 2004 // TODO: Once we have a way (other than checking for the existince of the 2005 // libcall) to tell whether our target can lower @llvm.sqrt, relax the 2006 // condition below. 2007 if (TLI->has(LibFunc_sqrtf) && (Callee->getName() == "sqrt" || 2008 Callee->getIntrinsicID() == Intrinsic::sqrt)) 2009 Ret = optimizeUnaryDoubleFP(CI, B, true); 2010 2011 if (!CI->isFast()) 2012 return Ret; 2013 2014 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0)); 2015 if (!I || I->getOpcode() != Instruction::FMul || !I->isFast()) 2016 return Ret; 2017 2018 // We're looking for a repeated factor in a multiplication tree, 2019 // so we can do this fold: sqrt(x * x) -> fabs(x); 2020 // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y). 2021 Value *Op0 = I->getOperand(0); 2022 Value *Op1 = I->getOperand(1); 2023 Value *RepeatOp = nullptr; 2024 Value *OtherOp = nullptr; 2025 if (Op0 == Op1) { 2026 // Simple match: the operands of the multiply are identical. 2027 RepeatOp = Op0; 2028 } else { 2029 // Look for a more complicated pattern: one of the operands is itself 2030 // a multiply, so search for a common factor in that multiply. 2031 // Note: We don't bother looking any deeper than this first level or for 2032 // variations of this pattern because instcombine's visitFMUL and/or the 2033 // reassociation pass should give us this form. 2034 Value *OtherMul0, *OtherMul1; 2035 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) { 2036 // Pattern: sqrt((x * y) * z) 2037 if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) { 2038 // Matched: sqrt((x * x) * z) 2039 RepeatOp = OtherMul0; 2040 OtherOp = Op1; 2041 } 2042 } 2043 } 2044 if (!RepeatOp) 2045 return Ret; 2046 2047 // Fast math flags for any created instructions should match the sqrt 2048 // and multiply. 2049 IRBuilderBase::FastMathFlagGuard Guard(B); 2050 B.setFastMathFlags(I->getFastMathFlags()); 2051 2052 // If we found a repeated factor, hoist it out of the square root and 2053 // replace it with the fabs of that factor. 2054 Module *M = Callee->getParent(); 2055 Type *ArgType = I->getType(); 2056 Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType); 2057 Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs"); 2058 if (OtherOp) { 2059 // If we found a non-repeated factor, we still need to get its square 2060 // root. We then multiply that by the value that was simplified out 2061 // of the square root calculation. 2062 Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType); 2063 Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt"); 2064 return B.CreateFMul(FabsCall, SqrtCall); 2065 } 2066 return FabsCall; 2067 } 2068 2069 // TODO: Generalize to handle any trig function and its inverse. 2070 Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) { 2071 Function *Callee = CI->getCalledFunction(); 2072 Value *Ret = nullptr; 2073 StringRef Name = Callee->getName(); 2074 if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(Name)) 2075 Ret = optimizeUnaryDoubleFP(CI, B, true); 2076 2077 Value *Op1 = CI->getArgOperand(0); 2078 auto *OpC = dyn_cast<CallInst>(Op1); 2079 if (!OpC) 2080 return Ret; 2081 2082 // Both calls must be 'fast' in order to remove them. 2083 if (!CI->isFast() || !OpC->isFast()) 2084 return Ret; 2085 2086 // tan(atan(x)) -> x 2087 // tanf(atanf(x)) -> x 2088 // tanl(atanl(x)) -> x 2089 LibFunc Func; 2090 Function *F = OpC->getCalledFunction(); 2091 if (F && TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) && 2092 ((Func == LibFunc_atan && Callee->getName() == "tan") || 2093 (Func == LibFunc_atanf && Callee->getName() == "tanf") || 2094 (Func == LibFunc_atanl && Callee->getName() == "tanl"))) 2095 Ret = OpC->getArgOperand(0); 2096 return Ret; 2097 } 2098 2099 static bool isTrigLibCall(CallInst *CI) { 2100 // We can only hope to do anything useful if we can ignore things like errno 2101 // and floating-point exceptions. 2102 // We already checked the prototype. 2103 return CI->hasFnAttr(Attribute::NoUnwind) && 2104 CI->hasFnAttr(Attribute::ReadNone); 2105 } 2106 2107 static void insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg, 2108 bool UseFloat, Value *&Sin, Value *&Cos, 2109 Value *&SinCos) { 2110 Type *ArgTy = Arg->getType(); 2111 Type *ResTy; 2112 StringRef Name; 2113 2114 Triple T(OrigCallee->getParent()->getTargetTriple()); 2115 if (UseFloat) { 2116 Name = "__sincospif_stret"; 2117 2118 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now"); 2119 // x86_64 can't use {float, float} since that would be returned in both 2120 // xmm0 and xmm1, which isn't what a real struct would do. 2121 ResTy = T.getArch() == Triple::x86_64 2122 ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2)) 2123 : static_cast<Type *>(StructType::get(ArgTy, ArgTy)); 2124 } else { 2125 Name = "__sincospi_stret"; 2126 ResTy = StructType::get(ArgTy, ArgTy); 2127 } 2128 2129 Module *M = OrigCallee->getParent(); 2130 FunctionCallee Callee = 2131 M->getOrInsertFunction(Name, OrigCallee->getAttributes(), ResTy, ArgTy); 2132 2133 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) { 2134 // If the argument is an instruction, it must dominate all uses so put our 2135 // sincos call there. 2136 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator()); 2137 } else { 2138 // Otherwise (e.g. for a constant) the beginning of the function is as 2139 // good a place as any. 2140 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock(); 2141 B.SetInsertPoint(&EntryBB, EntryBB.begin()); 2142 } 2143 2144 SinCos = B.CreateCall(Callee, Arg, "sincospi"); 2145 2146 if (SinCos->getType()->isStructTy()) { 2147 Sin = B.CreateExtractValue(SinCos, 0, "sinpi"); 2148 Cos = B.CreateExtractValue(SinCos, 1, "cospi"); 2149 } else { 2150 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0), 2151 "sinpi"); 2152 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1), 2153 "cospi"); 2154 } 2155 } 2156 2157 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilderBase &B) { 2158 // Make sure the prototype is as expected, otherwise the rest of the 2159 // function is probably invalid and likely to abort. 2160 if (!isTrigLibCall(CI)) 2161 return nullptr; 2162 2163 Value *Arg = CI->getArgOperand(0); 2164 SmallVector<CallInst *, 1> SinCalls; 2165 SmallVector<CallInst *, 1> CosCalls; 2166 SmallVector<CallInst *, 1> SinCosCalls; 2167 2168 bool IsFloat = Arg->getType()->isFloatTy(); 2169 2170 // Look for all compatible sinpi, cospi and sincospi calls with the same 2171 // argument. If there are enough (in some sense) we can make the 2172 // substitution. 2173 Function *F = CI->getFunction(); 2174 for (User *U : Arg->users()) 2175 classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls); 2176 2177 // It's only worthwhile if both sinpi and cospi are actually used. 2178 if (SinCalls.empty() || CosCalls.empty()) 2179 return nullptr; 2180 2181 Value *Sin, *Cos, *SinCos; 2182 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos); 2183 2184 auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls, 2185 Value *Res) { 2186 for (CallInst *C : Calls) 2187 replaceAllUsesWith(C, Res); 2188 }; 2189 2190 replaceTrigInsts(SinCalls, Sin); 2191 replaceTrigInsts(CosCalls, Cos); 2192 replaceTrigInsts(SinCosCalls, SinCos); 2193 2194 return nullptr; 2195 } 2196 2197 void LibCallSimplifier::classifyArgUse( 2198 Value *Val, Function *F, bool IsFloat, 2199 SmallVectorImpl<CallInst *> &SinCalls, 2200 SmallVectorImpl<CallInst *> &CosCalls, 2201 SmallVectorImpl<CallInst *> &SinCosCalls) { 2202 CallInst *CI = dyn_cast<CallInst>(Val); 2203 2204 if (!CI || CI->use_empty()) 2205 return; 2206 2207 // Don't consider calls in other functions. 2208 if (CI->getFunction() != F) 2209 return; 2210 2211 Function *Callee = CI->getCalledFunction(); 2212 LibFunc Func; 2213 if (!Callee || !TLI->getLibFunc(*Callee, Func) || !TLI->has(Func) || 2214 !isTrigLibCall(CI)) 2215 return; 2216 2217 if (IsFloat) { 2218 if (Func == LibFunc_sinpif) 2219 SinCalls.push_back(CI); 2220 else if (Func == LibFunc_cospif) 2221 CosCalls.push_back(CI); 2222 else if (Func == LibFunc_sincospif_stret) 2223 SinCosCalls.push_back(CI); 2224 } else { 2225 if (Func == LibFunc_sinpi) 2226 SinCalls.push_back(CI); 2227 else if (Func == LibFunc_cospi) 2228 CosCalls.push_back(CI); 2229 else if (Func == LibFunc_sincospi_stret) 2230 SinCosCalls.push_back(CI); 2231 } 2232 } 2233 2234 //===----------------------------------------------------------------------===// 2235 // Integer Library Call Optimizations 2236 //===----------------------------------------------------------------------===// 2237 2238 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) { 2239 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0 2240 Value *Op = CI->getArgOperand(0); 2241 Type *ArgType = Op->getType(); 2242 Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(), 2243 Intrinsic::cttz, ArgType); 2244 Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz"); 2245 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1)); 2246 V = B.CreateIntCast(V, B.getInt32Ty(), false); 2247 2248 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType)); 2249 return B.CreateSelect(Cond, V, B.getInt32(0)); 2250 } 2251 2252 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) { 2253 // fls(x) -> (i32)(sizeInBits(x) - llvm.ctlz(x, false)) 2254 Value *Op = CI->getArgOperand(0); 2255 Type *ArgType = Op->getType(); 2256 Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(), 2257 Intrinsic::ctlz, ArgType); 2258 Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz"); 2259 V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()), 2260 V); 2261 return B.CreateIntCast(V, CI->getType(), false); 2262 } 2263 2264 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) { 2265 // abs(x) -> x <s 0 ? -x : x 2266 // The negation has 'nsw' because abs of INT_MIN is undefined. 2267 Value *X = CI->getArgOperand(0); 2268 Value *IsNeg = B.CreateICmpSLT(X, Constant::getNullValue(X->getType())); 2269 Value *NegX = B.CreateNSWNeg(X, "neg"); 2270 return B.CreateSelect(IsNeg, NegX, X); 2271 } 2272 2273 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) { 2274 // isdigit(c) -> (c-'0') <u 10 2275 Value *Op = CI->getArgOperand(0); 2276 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp"); 2277 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit"); 2278 return B.CreateZExt(Op, CI->getType()); 2279 } 2280 2281 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) { 2282 // isascii(c) -> c <u 128 2283 Value *Op = CI->getArgOperand(0); 2284 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii"); 2285 return B.CreateZExt(Op, CI->getType()); 2286 } 2287 2288 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) { 2289 // toascii(c) -> c & 0x7f 2290 return B.CreateAnd(CI->getArgOperand(0), 2291 ConstantInt::get(CI->getType(), 0x7F)); 2292 } 2293 2294 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) { 2295 StringRef Str; 2296 if (!getConstantStringInfo(CI->getArgOperand(0), Str)) 2297 return nullptr; 2298 2299 return convertStrToNumber(CI, Str, 10); 2300 } 2301 2302 Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilderBase &B) { 2303 StringRef Str; 2304 if (!getConstantStringInfo(CI->getArgOperand(0), Str)) 2305 return nullptr; 2306 2307 if (!isa<ConstantPointerNull>(CI->getArgOperand(1))) 2308 return nullptr; 2309 2310 if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) { 2311 return convertStrToNumber(CI, Str, CInt->getSExtValue()); 2312 } 2313 2314 return nullptr; 2315 } 2316 2317 //===----------------------------------------------------------------------===// 2318 // Formatting and IO Library Call Optimizations 2319 //===----------------------------------------------------------------------===// 2320 2321 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg); 2322 2323 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B, 2324 int StreamArg) { 2325 Function *Callee = CI->getCalledFunction(); 2326 // Error reporting calls should be cold, mark them as such. 2327 // This applies even to non-builtin calls: it is only a hint and applies to 2328 // functions that the frontend might not understand as builtins. 2329 2330 // This heuristic was suggested in: 2331 // Improving Static Branch Prediction in a Compiler 2332 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu 2333 // Proceedings of PACT'98, Oct. 1998, IEEE 2334 if (!CI->hasFnAttr(Attribute::Cold) && 2335 isReportingError(Callee, CI, StreamArg)) { 2336 CI->addAttribute(AttributeList::FunctionIndex, Attribute::Cold); 2337 } 2338 2339 return nullptr; 2340 } 2341 2342 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) { 2343 if (!Callee || !Callee->isDeclaration()) 2344 return false; 2345 2346 if (StreamArg < 0) 2347 return true; 2348 2349 // These functions might be considered cold, but only if their stream 2350 // argument is stderr. 2351 2352 if (StreamArg >= (int)CI->getNumArgOperands()) 2353 return false; 2354 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg)); 2355 if (!LI) 2356 return false; 2357 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()); 2358 if (!GV || !GV->isDeclaration()) 2359 return false; 2360 return GV->getName() == "stderr"; 2361 } 2362 2363 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) { 2364 // Check for a fixed format string. 2365 StringRef FormatStr; 2366 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) 2367 return nullptr; 2368 2369 // Empty format string -> noop. 2370 if (FormatStr.empty()) // Tolerate printf's declared void. 2371 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0); 2372 2373 // Do not do any of the following transformations if the printf return value 2374 // is used, in general the printf return value is not compatible with either 2375 // putchar() or puts(). 2376 if (!CI->use_empty()) 2377 return nullptr; 2378 2379 // printf("x") -> putchar('x'), even for "%" and "%%". 2380 if (FormatStr.size() == 1 || FormatStr == "%%") 2381 return emitPutChar(B.getInt32(FormatStr[0]), B, TLI); 2382 2383 // Try to remove call or emit putchar/puts. 2384 if (FormatStr == "%s" && CI->getNumArgOperands() > 1) { 2385 StringRef OperandStr; 2386 if (!getConstantStringInfo(CI->getOperand(1), OperandStr)) 2387 return nullptr; 2388 // printf("%s", "") --> NOP 2389 if (OperandStr.empty()) 2390 return (Value *)CI; 2391 // printf("%s", "a") --> putchar('a') 2392 if (OperandStr.size() == 1) 2393 return emitPutChar(B.getInt32(OperandStr[0]), B, TLI); 2394 // printf("%s", str"\n") --> puts(str) 2395 if (OperandStr.back() == '\n') { 2396 OperandStr = OperandStr.drop_back(); 2397 Value *GV = B.CreateGlobalString(OperandStr, "str"); 2398 return emitPutS(GV, B, TLI); 2399 } 2400 return nullptr; 2401 } 2402 2403 // printf("foo\n") --> puts("foo") 2404 if (FormatStr.back() == '\n' && 2405 FormatStr.find('%') == StringRef::npos) { // No format characters. 2406 // Create a string literal with no \n on it. We expect the constant merge 2407 // pass to be run after this pass, to merge duplicate strings. 2408 FormatStr = FormatStr.drop_back(); 2409 Value *GV = B.CreateGlobalString(FormatStr, "str"); 2410 return emitPutS(GV, B, TLI); 2411 } 2412 2413 // Optimize specific format strings. 2414 // printf("%c", chr) --> putchar(chr) 2415 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && 2416 CI->getArgOperand(1)->getType()->isIntegerTy()) 2417 return emitPutChar(CI->getArgOperand(1), B, TLI); 2418 2419 // printf("%s\n", str) --> puts(str) 2420 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && 2421 CI->getArgOperand(1)->getType()->isPointerTy()) 2422 return emitPutS(CI->getArgOperand(1), B, TLI); 2423 return nullptr; 2424 } 2425 2426 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) { 2427 2428 Function *Callee = CI->getCalledFunction(); 2429 FunctionType *FT = Callee->getFunctionType(); 2430 if (Value *V = optimizePrintFString(CI, B)) { 2431 return V; 2432 } 2433 2434 // printf(format, ...) -> iprintf(format, ...) if no floating point 2435 // arguments. 2436 if (TLI->has(LibFunc_iprintf) && !callHasFloatingPointArgument(CI)) { 2437 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2438 FunctionCallee IPrintFFn = 2439 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); 2440 CallInst *New = cast<CallInst>(CI->clone()); 2441 New->setCalledFunction(IPrintFFn); 2442 B.Insert(New); 2443 return New; 2444 } 2445 2446 // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point 2447 // arguments. 2448 if (TLI->has(LibFunc_small_printf) && !callHasFP128Argument(CI)) { 2449 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2450 auto SmallPrintFFn = 2451 M->getOrInsertFunction(TLI->getName(LibFunc_small_printf), 2452 FT, Callee->getAttributes()); 2453 CallInst *New = cast<CallInst>(CI->clone()); 2454 New->setCalledFunction(SmallPrintFFn); 2455 B.Insert(New); 2456 return New; 2457 } 2458 2459 annotateNonNullNoUndefBasedOnAccess(CI, 0); 2460 return nullptr; 2461 } 2462 2463 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, 2464 IRBuilderBase &B) { 2465 // Check for a fixed format string. 2466 StringRef FormatStr; 2467 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) 2468 return nullptr; 2469 2470 // If we just have a format string (nothing else crazy) transform it. 2471 Value *Dest = CI->getArgOperand(0); 2472 if (CI->getNumArgOperands() == 2) { 2473 // Make sure there's no % in the constant array. We could try to handle 2474 // %% -> % in the future if we cared. 2475 if (FormatStr.find('%') != StringRef::npos) 2476 return nullptr; // we found a format specifier, bail out. 2477 2478 // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1) 2479 B.CreateMemCpy( 2480 Dest, Align(1), CI->getArgOperand(1), Align(1), 2481 ConstantInt::get(DL.getIntPtrType(CI->getContext()), 2482 FormatStr.size() + 1)); // Copy the null byte. 2483 return ConstantInt::get(CI->getType(), FormatStr.size()); 2484 } 2485 2486 // The remaining optimizations require the format string to be "%s" or "%c" 2487 // and have an extra operand. 2488 if (FormatStr.size() != 2 || FormatStr[0] != '%' || 2489 CI->getNumArgOperands() < 3) 2490 return nullptr; 2491 2492 // Decode the second character of the format string. 2493 if (FormatStr[1] == 'c') { 2494 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0 2495 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) 2496 return nullptr; 2497 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char"); 2498 Value *Ptr = castToCStr(Dest, B); 2499 B.CreateStore(V, Ptr); 2500 Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul"); 2501 B.CreateStore(B.getInt8(0), Ptr); 2502 2503 return ConstantInt::get(CI->getType(), 1); 2504 } 2505 2506 if (FormatStr[1] == 's') { 2507 // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str, 2508 // strlen(str)+1) 2509 if (!CI->getArgOperand(2)->getType()->isPointerTy()) 2510 return nullptr; 2511 2512 if (CI->use_empty()) 2513 // sprintf(dest, "%s", str) -> strcpy(dest, str) 2514 return emitStrCpy(Dest, CI->getArgOperand(2), B, TLI); 2515 2516 uint64_t SrcLen = GetStringLength(CI->getArgOperand(2)); 2517 if (SrcLen) { 2518 B.CreateMemCpy( 2519 Dest, Align(1), CI->getArgOperand(2), Align(1), 2520 ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen)); 2521 // Returns total number of characters written without null-character. 2522 return ConstantInt::get(CI->getType(), SrcLen - 1); 2523 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) { 2524 // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest 2525 // Handle mismatched pointer types (goes away with typeless pointers?). 2526 V = B.CreatePointerCast(V, Dest->getType()); 2527 Value *PtrDiff = B.CreatePtrDiff(V, Dest); 2528 return B.CreateIntCast(PtrDiff, CI->getType(), false); 2529 } 2530 2531 bool OptForSize = CI->getFunction()->hasOptSize() || 2532 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI, 2533 PGSOQueryType::IRPass); 2534 if (OptForSize) 2535 return nullptr; 2536 2537 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI); 2538 if (!Len) 2539 return nullptr; 2540 Value *IncLen = 2541 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc"); 2542 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen); 2543 2544 // The sprintf result is the unincremented number of bytes in the string. 2545 return B.CreateIntCast(Len, CI->getType(), false); 2546 } 2547 return nullptr; 2548 } 2549 2550 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) { 2551 Function *Callee = CI->getCalledFunction(); 2552 FunctionType *FT = Callee->getFunctionType(); 2553 if (Value *V = optimizeSPrintFString(CI, B)) { 2554 return V; 2555 } 2556 2557 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating 2558 // point arguments. 2559 if (TLI->has(LibFunc_siprintf) && !callHasFloatingPointArgument(CI)) { 2560 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2561 FunctionCallee SIPrintFFn = 2562 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes()); 2563 CallInst *New = cast<CallInst>(CI->clone()); 2564 New->setCalledFunction(SIPrintFFn); 2565 B.Insert(New); 2566 return New; 2567 } 2568 2569 // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit 2570 // floating point arguments. 2571 if (TLI->has(LibFunc_small_sprintf) && !callHasFP128Argument(CI)) { 2572 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2573 auto SmallSPrintFFn = 2574 M->getOrInsertFunction(TLI->getName(LibFunc_small_sprintf), 2575 FT, Callee->getAttributes()); 2576 CallInst *New = cast<CallInst>(CI->clone()); 2577 New->setCalledFunction(SmallSPrintFFn); 2578 B.Insert(New); 2579 return New; 2580 } 2581 2582 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); 2583 return nullptr; 2584 } 2585 2586 Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, 2587 IRBuilderBase &B) { 2588 // Check for size 2589 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 2590 if (!Size) 2591 return nullptr; 2592 2593 uint64_t N = Size->getZExtValue(); 2594 // Check for a fixed format string. 2595 StringRef FormatStr; 2596 if (!getConstantStringInfo(CI->getArgOperand(2), FormatStr)) 2597 return nullptr; 2598 2599 // If we just have a format string (nothing else crazy) transform it. 2600 if (CI->getNumArgOperands() == 3) { 2601 // Make sure there's no % in the constant array. We could try to handle 2602 // %% -> % in the future if we cared. 2603 if (FormatStr.find('%') != StringRef::npos) 2604 return nullptr; // we found a format specifier, bail out. 2605 2606 if (N == 0) 2607 return ConstantInt::get(CI->getType(), FormatStr.size()); 2608 else if (N < FormatStr.size() + 1) 2609 return nullptr; 2610 2611 // snprintf(dst, size, fmt) -> llvm.memcpy(align 1 dst, align 1 fmt, 2612 // strlen(fmt)+1) 2613 B.CreateMemCpy( 2614 CI->getArgOperand(0), Align(1), CI->getArgOperand(2), Align(1), 2615 ConstantInt::get(DL.getIntPtrType(CI->getContext()), 2616 FormatStr.size() + 1)); // Copy the null byte. 2617 return ConstantInt::get(CI->getType(), FormatStr.size()); 2618 } 2619 2620 // The remaining optimizations require the format string to be "%s" or "%c" 2621 // and have an extra operand. 2622 if (FormatStr.size() == 2 && FormatStr[0] == '%' && 2623 CI->getNumArgOperands() == 4) { 2624 2625 // Decode the second character of the format string. 2626 if (FormatStr[1] == 'c') { 2627 if (N == 0) 2628 return ConstantInt::get(CI->getType(), 1); 2629 else if (N == 1) 2630 return nullptr; 2631 2632 // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0 2633 if (!CI->getArgOperand(3)->getType()->isIntegerTy()) 2634 return nullptr; 2635 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char"); 2636 Value *Ptr = castToCStr(CI->getArgOperand(0), B); 2637 B.CreateStore(V, Ptr); 2638 Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul"); 2639 B.CreateStore(B.getInt8(0), Ptr); 2640 2641 return ConstantInt::get(CI->getType(), 1); 2642 } 2643 2644 if (FormatStr[1] == 's') { 2645 // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1) 2646 StringRef Str; 2647 if (!getConstantStringInfo(CI->getArgOperand(3), Str)) 2648 return nullptr; 2649 2650 if (N == 0) 2651 return ConstantInt::get(CI->getType(), Str.size()); 2652 else if (N < Str.size() + 1) 2653 return nullptr; 2654 2655 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(3), 2656 Align(1), ConstantInt::get(CI->getType(), Str.size() + 1)); 2657 2658 // The snprintf result is the unincremented number of bytes in the string. 2659 return ConstantInt::get(CI->getType(), Str.size()); 2660 } 2661 } 2662 return nullptr; 2663 } 2664 2665 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) { 2666 if (Value *V = optimizeSnPrintFString(CI, B)) { 2667 return V; 2668 } 2669 2670 if (isKnownNonZero(CI->getOperand(1), DL)) 2671 annotateNonNullNoUndefBasedOnAccess(CI, 0); 2672 return nullptr; 2673 } 2674 2675 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, 2676 IRBuilderBase &B) { 2677 optimizeErrorReporting(CI, B, 0); 2678 2679 // All the optimizations depend on the format string. 2680 StringRef FormatStr; 2681 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) 2682 return nullptr; 2683 2684 // Do not do any of the following transformations if the fprintf return 2685 // value is used, in general the fprintf return value is not compatible 2686 // with fwrite(), fputc() or fputs(). 2687 if (!CI->use_empty()) 2688 return nullptr; 2689 2690 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F) 2691 if (CI->getNumArgOperands() == 2) { 2692 // Could handle %% -> % if we cared. 2693 if (FormatStr.find('%') != StringRef::npos) 2694 return nullptr; // We found a format specifier. 2695 2696 return emitFWrite( 2697 CI->getArgOperand(1), 2698 ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()), 2699 CI->getArgOperand(0), B, DL, TLI); 2700 } 2701 2702 // The remaining optimizations require the format string to be "%s" or "%c" 2703 // and have an extra operand. 2704 if (FormatStr.size() != 2 || FormatStr[0] != '%' || 2705 CI->getNumArgOperands() < 3) 2706 return nullptr; 2707 2708 // Decode the second character of the format string. 2709 if (FormatStr[1] == 'c') { 2710 // fprintf(F, "%c", chr) --> fputc(chr, F) 2711 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) 2712 return nullptr; 2713 return emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); 2714 } 2715 2716 if (FormatStr[1] == 's') { 2717 // fprintf(F, "%s", str) --> fputs(str, F) 2718 if (!CI->getArgOperand(2)->getType()->isPointerTy()) 2719 return nullptr; 2720 return emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); 2721 } 2722 return nullptr; 2723 } 2724 2725 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) { 2726 Function *Callee = CI->getCalledFunction(); 2727 FunctionType *FT = Callee->getFunctionType(); 2728 if (Value *V = optimizeFPrintFString(CI, B)) { 2729 return V; 2730 } 2731 2732 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no 2733 // floating point arguments. 2734 if (TLI->has(LibFunc_fiprintf) && !callHasFloatingPointArgument(CI)) { 2735 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2736 FunctionCallee FIPrintFFn = 2737 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes()); 2738 CallInst *New = cast<CallInst>(CI->clone()); 2739 New->setCalledFunction(FIPrintFFn); 2740 B.Insert(New); 2741 return New; 2742 } 2743 2744 // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no 2745 // 128-bit floating point arguments. 2746 if (TLI->has(LibFunc_small_fprintf) && !callHasFP128Argument(CI)) { 2747 Module *M = B.GetInsertBlock()->getParent()->getParent(); 2748 auto SmallFPrintFFn = 2749 M->getOrInsertFunction(TLI->getName(LibFunc_small_fprintf), 2750 FT, Callee->getAttributes()); 2751 CallInst *New = cast<CallInst>(CI->clone()); 2752 New->setCalledFunction(SmallFPrintFFn); 2753 B.Insert(New); 2754 return New; 2755 } 2756 2757 return nullptr; 2758 } 2759 2760 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) { 2761 optimizeErrorReporting(CI, B, 3); 2762 2763 // Get the element size and count. 2764 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 2765 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); 2766 if (SizeC && CountC) { 2767 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue(); 2768 2769 // If this is writing zero records, remove the call (it's a noop). 2770 if (Bytes == 0) 2771 return ConstantInt::get(CI->getType(), 0); 2772 2773 // If this is writing one byte, turn it into fputc. 2774 // This optimisation is only valid, if the return value is unused. 2775 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F) 2776 Value *Char = B.CreateLoad(B.getInt8Ty(), 2777 castToCStr(CI->getArgOperand(0), B), "char"); 2778 Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI); 2779 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr; 2780 } 2781 } 2782 2783 return nullptr; 2784 } 2785 2786 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) { 2787 optimizeErrorReporting(CI, B, 1); 2788 2789 // Don't rewrite fputs to fwrite when optimising for size because fwrite 2790 // requires more arguments and thus extra MOVs are required. 2791 bool OptForSize = CI->getFunction()->hasOptSize() || 2792 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI, 2793 PGSOQueryType::IRPass); 2794 if (OptForSize) 2795 return nullptr; 2796 2797 // We can't optimize if return value is used. 2798 if (!CI->use_empty()) 2799 return nullptr; 2800 2801 // fputs(s,F) --> fwrite(s,strlen(s),1,F) 2802 uint64_t Len = GetStringLength(CI->getArgOperand(0)); 2803 if (!Len) 2804 return nullptr; 2805 2806 // Known to have no uses (see above). 2807 return emitFWrite( 2808 CI->getArgOperand(0), 2809 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1), 2810 CI->getArgOperand(1), B, DL, TLI); 2811 } 2812 2813 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) { 2814 annotateNonNullNoUndefBasedOnAccess(CI, 0); 2815 if (!CI->use_empty()) 2816 return nullptr; 2817 2818 // Check for a constant string. 2819 // puts("") -> putchar('\n') 2820 StringRef Str; 2821 if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) 2822 return emitPutChar(B.getInt32('\n'), B, TLI); 2823 2824 return nullptr; 2825 } 2826 2827 Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) { 2828 // bcopy(src, dst, n) -> llvm.memmove(dst, src, n) 2829 return B.CreateMemMove(CI->getArgOperand(1), Align(1), CI->getArgOperand(0), 2830 Align(1), CI->getArgOperand(2)); 2831 } 2832 2833 bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) { 2834 LibFunc Func; 2835 SmallString<20> FloatFuncName = FuncName; 2836 FloatFuncName += 'f'; 2837 if (TLI->getLibFunc(FloatFuncName, Func)) 2838 return TLI->has(Func); 2839 return false; 2840 } 2841 2842 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI, 2843 IRBuilderBase &Builder) { 2844 LibFunc Func; 2845 Function *Callee = CI->getCalledFunction(); 2846 // Check for string/memory library functions. 2847 if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) { 2848 // Make sure we never change the calling convention. 2849 assert( 2850 (ignoreCallingConv(Func) || 2851 TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) && 2852 "Optimizing string/memory libcall would change the calling convention"); 2853 switch (Func) { 2854 case LibFunc_strcat: 2855 return optimizeStrCat(CI, Builder); 2856 case LibFunc_strncat: 2857 return optimizeStrNCat(CI, Builder); 2858 case LibFunc_strchr: 2859 return optimizeStrChr(CI, Builder); 2860 case LibFunc_strrchr: 2861 return optimizeStrRChr(CI, Builder); 2862 case LibFunc_strcmp: 2863 return optimizeStrCmp(CI, Builder); 2864 case LibFunc_strncmp: 2865 return optimizeStrNCmp(CI, Builder); 2866 case LibFunc_strcpy: 2867 return optimizeStrCpy(CI, Builder); 2868 case LibFunc_stpcpy: 2869 return optimizeStpCpy(CI, Builder); 2870 case LibFunc_strncpy: 2871 return optimizeStrNCpy(CI, Builder); 2872 case LibFunc_strlen: 2873 return optimizeStrLen(CI, Builder); 2874 case LibFunc_strpbrk: 2875 return optimizeStrPBrk(CI, Builder); 2876 case LibFunc_strndup: 2877 return optimizeStrNDup(CI, Builder); 2878 case LibFunc_strtol: 2879 case LibFunc_strtod: 2880 case LibFunc_strtof: 2881 case LibFunc_strtoul: 2882 case LibFunc_strtoll: 2883 case LibFunc_strtold: 2884 case LibFunc_strtoull: 2885 return optimizeStrTo(CI, Builder); 2886 case LibFunc_strspn: 2887 return optimizeStrSpn(CI, Builder); 2888 case LibFunc_strcspn: 2889 return optimizeStrCSpn(CI, Builder); 2890 case LibFunc_strstr: 2891 return optimizeStrStr(CI, Builder); 2892 case LibFunc_memchr: 2893 return optimizeMemChr(CI, Builder); 2894 case LibFunc_memrchr: 2895 return optimizeMemRChr(CI, Builder); 2896 case LibFunc_bcmp: 2897 return optimizeBCmp(CI, Builder); 2898 case LibFunc_memcmp: 2899 return optimizeMemCmp(CI, Builder); 2900 case LibFunc_memcpy: 2901 return optimizeMemCpy(CI, Builder); 2902 case LibFunc_memccpy: 2903 return optimizeMemCCpy(CI, Builder); 2904 case LibFunc_mempcpy: 2905 return optimizeMemPCpy(CI, Builder); 2906 case LibFunc_memmove: 2907 return optimizeMemMove(CI, Builder); 2908 case LibFunc_memset: 2909 return optimizeMemSet(CI, Builder); 2910 case LibFunc_realloc: 2911 return optimizeRealloc(CI, Builder); 2912 case LibFunc_wcslen: 2913 return optimizeWcslen(CI, Builder); 2914 case LibFunc_bcopy: 2915 return optimizeBCopy(CI, Builder); 2916 default: 2917 break; 2918 } 2919 } 2920 return nullptr; 2921 } 2922 2923 Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, 2924 LibFunc Func, 2925 IRBuilderBase &Builder) { 2926 // Don't optimize calls that require strict floating point semantics. 2927 if (CI->isStrictFP()) 2928 return nullptr; 2929 2930 if (Value *V = optimizeTrigReflections(CI, Func, Builder)) 2931 return V; 2932 2933 switch (Func) { 2934 case LibFunc_sinpif: 2935 case LibFunc_sinpi: 2936 case LibFunc_cospif: 2937 case LibFunc_cospi: 2938 return optimizeSinCosPi(CI, Builder); 2939 case LibFunc_powf: 2940 case LibFunc_pow: 2941 case LibFunc_powl: 2942 return optimizePow(CI, Builder); 2943 case LibFunc_exp2l: 2944 case LibFunc_exp2: 2945 case LibFunc_exp2f: 2946 return optimizeExp2(CI, Builder); 2947 case LibFunc_fabsf: 2948 case LibFunc_fabs: 2949 case LibFunc_fabsl: 2950 return replaceUnaryCall(CI, Builder, Intrinsic::fabs); 2951 case LibFunc_sqrtf: 2952 case LibFunc_sqrt: 2953 case LibFunc_sqrtl: 2954 return optimizeSqrt(CI, Builder); 2955 case LibFunc_logf: 2956 case LibFunc_log: 2957 case LibFunc_logl: 2958 case LibFunc_log10f: 2959 case LibFunc_log10: 2960 case LibFunc_log10l: 2961 case LibFunc_log1pf: 2962 case LibFunc_log1p: 2963 case LibFunc_log1pl: 2964 case LibFunc_log2f: 2965 case LibFunc_log2: 2966 case LibFunc_log2l: 2967 case LibFunc_logbf: 2968 case LibFunc_logb: 2969 case LibFunc_logbl: 2970 return optimizeLog(CI, Builder); 2971 case LibFunc_tan: 2972 case LibFunc_tanf: 2973 case LibFunc_tanl: 2974 return optimizeTan(CI, Builder); 2975 case LibFunc_ceil: 2976 return replaceUnaryCall(CI, Builder, Intrinsic::ceil); 2977 case LibFunc_floor: 2978 return replaceUnaryCall(CI, Builder, Intrinsic::floor); 2979 case LibFunc_round: 2980 return replaceUnaryCall(CI, Builder, Intrinsic::round); 2981 case LibFunc_roundeven: 2982 return replaceUnaryCall(CI, Builder, Intrinsic::roundeven); 2983 case LibFunc_nearbyint: 2984 return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint); 2985 case LibFunc_rint: 2986 return replaceUnaryCall(CI, Builder, Intrinsic::rint); 2987 case LibFunc_trunc: 2988 return replaceUnaryCall(CI, Builder, Intrinsic::trunc); 2989 case LibFunc_acos: 2990 case LibFunc_acosh: 2991 case LibFunc_asin: 2992 case LibFunc_asinh: 2993 case LibFunc_atan: 2994 case LibFunc_atanh: 2995 case LibFunc_cbrt: 2996 case LibFunc_cosh: 2997 case LibFunc_exp: 2998 case LibFunc_exp10: 2999 case LibFunc_expm1: 3000 case LibFunc_cos: 3001 case LibFunc_sin: 3002 case LibFunc_sinh: 3003 case LibFunc_tanh: 3004 if (UnsafeFPShrink && hasFloatVersion(CI->getCalledFunction()->getName())) 3005 return optimizeUnaryDoubleFP(CI, Builder, true); 3006 return nullptr; 3007 case LibFunc_copysign: 3008 if (hasFloatVersion(CI->getCalledFunction()->getName())) 3009 return optimizeBinaryDoubleFP(CI, Builder); 3010 return nullptr; 3011 case LibFunc_fminf: 3012 case LibFunc_fmin: 3013 case LibFunc_fminl: 3014 case LibFunc_fmaxf: 3015 case LibFunc_fmax: 3016 case LibFunc_fmaxl: 3017 return optimizeFMinFMax(CI, Builder); 3018 case LibFunc_cabs: 3019 case LibFunc_cabsf: 3020 case LibFunc_cabsl: 3021 return optimizeCAbs(CI, Builder); 3022 default: 3023 return nullptr; 3024 } 3025 } 3026 3027 Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) { 3028 // TODO: Split out the code below that operates on FP calls so that 3029 // we can all non-FP calls with the StrictFP attribute to be 3030 // optimized. 3031 if (CI->isNoBuiltin()) 3032 return nullptr; 3033 3034 LibFunc Func; 3035 Function *Callee = CI->getCalledFunction(); 3036 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI); 3037 3038 SmallVector<OperandBundleDef, 2> OpBundles; 3039 CI->getOperandBundlesAsDefs(OpBundles); 3040 3041 IRBuilderBase::OperandBundlesGuard Guard(Builder); 3042 Builder.setDefaultOperandBundles(OpBundles); 3043 3044 // Command-line parameter overrides instruction attribute. 3045 // This can't be moved to optimizeFloatingPointLibCall() because it may be 3046 // used by the intrinsic optimizations. 3047 if (EnableUnsafeFPShrink.getNumOccurrences() > 0) 3048 UnsafeFPShrink = EnableUnsafeFPShrink; 3049 else if (isa<FPMathOperator>(CI) && CI->isFast()) 3050 UnsafeFPShrink = true; 3051 3052 // First, check for intrinsics. 3053 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) { 3054 if (!IsCallingConvC) 3055 return nullptr; 3056 // The FP intrinsics have corresponding constrained versions so we don't 3057 // need to check for the StrictFP attribute here. 3058 switch (II->getIntrinsicID()) { 3059 case Intrinsic::pow: 3060 return optimizePow(CI, Builder); 3061 case Intrinsic::exp2: 3062 return optimizeExp2(CI, Builder); 3063 case Intrinsic::log: 3064 case Intrinsic::log2: 3065 case Intrinsic::log10: 3066 return optimizeLog(CI, Builder); 3067 case Intrinsic::sqrt: 3068 return optimizeSqrt(CI, Builder); 3069 // TODO: Use foldMallocMemset() with memset intrinsic. 3070 case Intrinsic::memset: 3071 return optimizeMemSet(CI, Builder); 3072 case Intrinsic::memcpy: 3073 return optimizeMemCpy(CI, Builder); 3074 case Intrinsic::memmove: 3075 return optimizeMemMove(CI, Builder); 3076 default: 3077 return nullptr; 3078 } 3079 } 3080 3081 // Also try to simplify calls to fortified library functions. 3082 if (Value *SimplifiedFortifiedCI = 3083 FortifiedSimplifier.optimizeCall(CI, Builder)) { 3084 // Try to further simplify the result. 3085 CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI); 3086 if (SimplifiedCI && SimplifiedCI->getCalledFunction()) { 3087 // Ensure that SimplifiedCI's uses are complete, since some calls have 3088 // their uses analyzed. 3089 replaceAllUsesWith(CI, SimplifiedCI); 3090 3091 // Set insertion point to SimplifiedCI to guarantee we reach all uses 3092 // we might replace later on. 3093 IRBuilderBase::InsertPointGuard Guard(Builder); 3094 Builder.SetInsertPoint(SimplifiedCI); 3095 if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) { 3096 // If we were able to further simplify, remove the now redundant call. 3097 substituteInParent(SimplifiedCI, V); 3098 return V; 3099 } 3100 } 3101 return SimplifiedFortifiedCI; 3102 } 3103 3104 // Then check for known library functions. 3105 if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) { 3106 // We never change the calling convention. 3107 if (!ignoreCallingConv(Func) && !IsCallingConvC) 3108 return nullptr; 3109 if (Value *V = optimizeStringMemoryLibCall(CI, Builder)) 3110 return V; 3111 if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder)) 3112 return V; 3113 switch (Func) { 3114 case LibFunc_ffs: 3115 case LibFunc_ffsl: 3116 case LibFunc_ffsll: 3117 return optimizeFFS(CI, Builder); 3118 case LibFunc_fls: 3119 case LibFunc_flsl: 3120 case LibFunc_flsll: 3121 return optimizeFls(CI, Builder); 3122 case LibFunc_abs: 3123 case LibFunc_labs: 3124 case LibFunc_llabs: 3125 return optimizeAbs(CI, Builder); 3126 case LibFunc_isdigit: 3127 return optimizeIsDigit(CI, Builder); 3128 case LibFunc_isascii: 3129 return optimizeIsAscii(CI, Builder); 3130 case LibFunc_toascii: 3131 return optimizeToAscii(CI, Builder); 3132 case LibFunc_atoi: 3133 case LibFunc_atol: 3134 case LibFunc_atoll: 3135 return optimizeAtoi(CI, Builder); 3136 case LibFunc_strtol: 3137 case LibFunc_strtoll: 3138 return optimizeStrtol(CI, Builder); 3139 case LibFunc_printf: 3140 return optimizePrintF(CI, Builder); 3141 case LibFunc_sprintf: 3142 return optimizeSPrintF(CI, Builder); 3143 case LibFunc_snprintf: 3144 return optimizeSnPrintF(CI, Builder); 3145 case LibFunc_fprintf: 3146 return optimizeFPrintF(CI, Builder); 3147 case LibFunc_fwrite: 3148 return optimizeFWrite(CI, Builder); 3149 case LibFunc_fputs: 3150 return optimizeFPuts(CI, Builder); 3151 case LibFunc_puts: 3152 return optimizePuts(CI, Builder); 3153 case LibFunc_perror: 3154 return optimizeErrorReporting(CI, Builder); 3155 case LibFunc_vfprintf: 3156 case LibFunc_fiprintf: 3157 return optimizeErrorReporting(CI, Builder, 0); 3158 default: 3159 return nullptr; 3160 } 3161 } 3162 return nullptr; 3163 } 3164 3165 LibCallSimplifier::LibCallSimplifier( 3166 const DataLayout &DL, const TargetLibraryInfo *TLI, 3167 OptimizationRemarkEmitter &ORE, 3168 BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, 3169 function_ref<void(Instruction *, Value *)> Replacer, 3170 function_ref<void(Instruction *)> Eraser) 3171 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE), BFI(BFI), PSI(PSI), 3172 UnsafeFPShrink(false), Replacer(Replacer), Eraser(Eraser) {} 3173 3174 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) { 3175 // Indirect through the replacer used in this instance. 3176 Replacer(I, With); 3177 } 3178 3179 void LibCallSimplifier::eraseFromParent(Instruction *I) { 3180 Eraser(I); 3181 } 3182 3183 // TODO: 3184 // Additional cases that we need to add to this file: 3185 // 3186 // cbrt: 3187 // * cbrt(expN(X)) -> expN(x/3) 3188 // * cbrt(sqrt(x)) -> pow(x,1/6) 3189 // * cbrt(cbrt(x)) -> pow(x,1/9) 3190 // 3191 // exp, expf, expl: 3192 // * exp(log(x)) -> x 3193 // 3194 // log, logf, logl: 3195 // * log(exp(x)) -> x 3196 // * log(exp(y)) -> y*log(e) 3197 // * log(exp10(y)) -> y*log(10) 3198 // * log(sqrt(x)) -> 0.5*log(x) 3199 // 3200 // pow, powf, powl: 3201 // * pow(sqrt(x),y) -> pow(x,y*0.5) 3202 // * pow(pow(x,y),z)-> pow(x,y*z) 3203 // 3204 // signbit: 3205 // * signbit(cnst) -> cnst' 3206 // * signbit(nncst) -> 0 (if pstv is a non-negative constant) 3207 // 3208 // sqrt, sqrtf, sqrtl: 3209 // * sqrt(expN(x)) -> expN(x*0.5) 3210 // * sqrt(Nroot(x)) -> pow(x,1/(2*N)) 3211 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5) 3212 // 3213 3214 //===----------------------------------------------------------------------===// 3215 // Fortified Library Call Optimizations 3216 //===----------------------------------------------------------------------===// 3217 3218 bool 3219 FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI, 3220 unsigned ObjSizeOp, 3221 Optional<unsigned> SizeOp, 3222 Optional<unsigned> StrOp, 3223 Optional<unsigned> FlagOp) { 3224 // If this function takes a flag argument, the implementation may use it to 3225 // perform extra checks. Don't fold into the non-checking variant. 3226 if (FlagOp) { 3227 ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp)); 3228 if (!Flag || !Flag->isZero()) 3229 return false; 3230 } 3231 3232 if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp)) 3233 return true; 3234 3235 if (ConstantInt *ObjSizeCI = 3236 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) { 3237 if (ObjSizeCI->isMinusOne()) 3238 return true; 3239 // If the object size wasn't -1 (unknown), bail out if we were asked to. 3240 if (OnlyLowerUnknownSize) 3241 return false; 3242 if (StrOp) { 3243 uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp)); 3244 // If the length is 0 we don't know how long it is and so we can't 3245 // remove the check. 3246 if (Len) 3247 annotateDereferenceableBytes(CI, *StrOp, Len); 3248 else 3249 return false; 3250 return ObjSizeCI->getZExtValue() >= Len; 3251 } 3252 3253 if (SizeOp) { 3254 if (ConstantInt *SizeCI = 3255 dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp))) 3256 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue(); 3257 } 3258 } 3259 return false; 3260 } 3261 3262 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, 3263 IRBuilderBase &B) { 3264 if (isFortifiedCallFoldable(CI, 3, 2)) { 3265 CallInst *NewCI = 3266 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), 3267 Align(1), CI->getArgOperand(2)); 3268 NewCI->setAttributes(CI->getAttributes()); 3269 NewCI->removeAttributes(AttributeList::ReturnIndex, 3270 AttributeFuncs::typeIncompatible(NewCI->getType())); 3271 return CI->getArgOperand(0); 3272 } 3273 return nullptr; 3274 } 3275 3276 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, 3277 IRBuilderBase &B) { 3278 if (isFortifiedCallFoldable(CI, 3, 2)) { 3279 CallInst *NewCI = 3280 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), 3281 Align(1), CI->getArgOperand(2)); 3282 NewCI->setAttributes(CI->getAttributes()); 3283 NewCI->removeAttributes(AttributeList::ReturnIndex, 3284 AttributeFuncs::typeIncompatible(NewCI->getType())); 3285 return CI->getArgOperand(0); 3286 } 3287 return nullptr; 3288 } 3289 3290 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, 3291 IRBuilderBase &B) { 3292 // TODO: Try foldMallocMemset() here. 3293 3294 if (isFortifiedCallFoldable(CI, 3, 2)) { 3295 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); 3296 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, 3297 CI->getArgOperand(2), Align(1)); 3298 NewCI->setAttributes(CI->getAttributes()); 3299 NewCI->removeAttributes(AttributeList::ReturnIndex, 3300 AttributeFuncs::typeIncompatible(NewCI->getType())); 3301 return CI->getArgOperand(0); 3302 } 3303 return nullptr; 3304 } 3305 3306 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI, 3307 IRBuilderBase &B) { 3308 const DataLayout &DL = CI->getModule()->getDataLayout(); 3309 if (isFortifiedCallFoldable(CI, 3, 2)) 3310 if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1), 3311 CI->getArgOperand(2), B, DL, TLI)) { 3312 CallInst *NewCI = cast<CallInst>(Call); 3313 NewCI->setAttributes(CI->getAttributes()); 3314 NewCI->removeAttributes( 3315 AttributeList::ReturnIndex, 3316 AttributeFuncs::typeIncompatible(NewCI->getType())); 3317 return NewCI; 3318 } 3319 return nullptr; 3320 } 3321 3322 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI, 3323 IRBuilderBase &B, 3324 LibFunc Func) { 3325 const DataLayout &DL = CI->getModule()->getDataLayout(); 3326 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1), 3327 *ObjSize = CI->getArgOperand(2); 3328 3329 // __stpcpy_chk(x,x,...) -> x+strlen(x) 3330 if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) { 3331 Value *StrLen = emitStrLen(Src, B, DL, TLI); 3332 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr; 3333 } 3334 3335 // If a) we don't have any length information, or b) we know this will 3336 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our 3337 // st[rp]cpy_chk call which may fail at runtime if the size is too long. 3338 // TODO: It might be nice to get a maximum length out of the possible 3339 // string lengths for varying. 3340 if (isFortifiedCallFoldable(CI, 2, None, 1)) { 3341 if (Func == LibFunc_strcpy_chk) 3342 return emitStrCpy(Dst, Src, B, TLI); 3343 else 3344 return emitStpCpy(Dst, Src, B, TLI); 3345 } 3346 3347 if (OnlyLowerUnknownSize) 3348 return nullptr; 3349 3350 // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk. 3351 uint64_t Len = GetStringLength(Src); 3352 if (Len) 3353 annotateDereferenceableBytes(CI, 1, Len); 3354 else 3355 return nullptr; 3356 3357 Type *SizeTTy = DL.getIntPtrType(CI->getContext()); 3358 Value *LenV = ConstantInt::get(SizeTTy, Len); 3359 Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI); 3360 // If the function was an __stpcpy_chk, and we were able to fold it into 3361 // a __memcpy_chk, we still need to return the correct end pointer. 3362 if (Ret && Func == LibFunc_stpcpy_chk) 3363 return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1)); 3364 return Ret; 3365 } 3366 3367 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI, 3368 IRBuilderBase &B) { 3369 if (isFortifiedCallFoldable(CI, 1, None, 0)) 3370 return emitStrLen(CI->getArgOperand(0), B, CI->getModule()->getDataLayout(), 3371 TLI); 3372 return nullptr; 3373 } 3374 3375 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI, 3376 IRBuilderBase &B, 3377 LibFunc Func) { 3378 if (isFortifiedCallFoldable(CI, 3, 2)) { 3379 if (Func == LibFunc_strncpy_chk) 3380 return emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), 3381 CI->getArgOperand(2), B, TLI); 3382 else 3383 return emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1), 3384 CI->getArgOperand(2), B, TLI); 3385 } 3386 3387 return nullptr; 3388 } 3389 3390 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI, 3391 IRBuilderBase &B) { 3392 if (isFortifiedCallFoldable(CI, 4, 3)) 3393 return emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1), 3394 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI); 3395 3396 return nullptr; 3397 } 3398 3399 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI, 3400 IRBuilderBase &B) { 3401 if (isFortifiedCallFoldable(CI, 3, 1, None, 2)) { 3402 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5)); 3403 return emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1), 3404 CI->getArgOperand(4), VariadicArgs, B, TLI); 3405 } 3406 3407 return nullptr; 3408 } 3409 3410 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI, 3411 IRBuilderBase &B) { 3412 if (isFortifiedCallFoldable(CI, 2, None, None, 1)) { 3413 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4)); 3414 return emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3), VariadicArgs, 3415 B, TLI); 3416 } 3417 3418 return nullptr; 3419 } 3420 3421 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI, 3422 IRBuilderBase &B) { 3423 if (isFortifiedCallFoldable(CI, 2)) 3424 return emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI); 3425 3426 return nullptr; 3427 } 3428 3429 Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI, 3430 IRBuilderBase &B) { 3431 if (isFortifiedCallFoldable(CI, 3)) 3432 return emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1), 3433 CI->getArgOperand(2), B, TLI); 3434 3435 return nullptr; 3436 } 3437 3438 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI, 3439 IRBuilderBase &B) { 3440 if (isFortifiedCallFoldable(CI, 3)) 3441 return emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1), 3442 CI->getArgOperand(2), B, TLI); 3443 3444 return nullptr; 3445 } 3446 3447 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI, 3448 IRBuilderBase &B) { 3449 if (isFortifiedCallFoldable(CI, 3)) 3450 return emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1), 3451 CI->getArgOperand(2), B, TLI); 3452 3453 return nullptr; 3454 } 3455 3456 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI, 3457 IRBuilderBase &B) { 3458 if (isFortifiedCallFoldable(CI, 3, 1, None, 2)) 3459 return emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1), 3460 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI); 3461 3462 return nullptr; 3463 } 3464 3465 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI, 3466 IRBuilderBase &B) { 3467 if (isFortifiedCallFoldable(CI, 2, None, None, 1)) 3468 return emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3), 3469 CI->getArgOperand(4), B, TLI); 3470 3471 return nullptr; 3472 } 3473 3474 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI, 3475 IRBuilderBase &Builder) { 3476 // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here. 3477 // Some clang users checked for _chk libcall availability using: 3478 // __has_builtin(__builtin___memcpy_chk) 3479 // When compiling with -fno-builtin, this is always true. 3480 // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we 3481 // end up with fortified libcalls, which isn't acceptable in a freestanding 3482 // environment which only provides their non-fortified counterparts. 3483 // 3484 // Until we change clang and/or teach external users to check for availability 3485 // differently, disregard the "nobuiltin" attribute and TLI::has. 3486 // 3487 // PR23093. 3488 3489 LibFunc Func; 3490 Function *Callee = CI->getCalledFunction(); 3491 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI); 3492 3493 SmallVector<OperandBundleDef, 2> OpBundles; 3494 CI->getOperandBundlesAsDefs(OpBundles); 3495 3496 IRBuilderBase::OperandBundlesGuard Guard(Builder); 3497 Builder.setDefaultOperandBundles(OpBundles); 3498 3499 // First, check that this is a known library functions and that the prototype 3500 // is correct. 3501 if (!TLI->getLibFunc(*Callee, Func)) 3502 return nullptr; 3503 3504 // We never change the calling convention. 3505 if (!ignoreCallingConv(Func) && !IsCallingConvC) 3506 return nullptr; 3507 3508 switch (Func) { 3509 case LibFunc_memcpy_chk: 3510 return optimizeMemCpyChk(CI, Builder); 3511 case LibFunc_mempcpy_chk: 3512 return optimizeMemPCpyChk(CI, Builder); 3513 case LibFunc_memmove_chk: 3514 return optimizeMemMoveChk(CI, Builder); 3515 case LibFunc_memset_chk: 3516 return optimizeMemSetChk(CI, Builder); 3517 case LibFunc_stpcpy_chk: 3518 case LibFunc_strcpy_chk: 3519 return optimizeStrpCpyChk(CI, Builder, Func); 3520 case LibFunc_strlen_chk: 3521 return optimizeStrLenChk(CI, Builder); 3522 case LibFunc_stpncpy_chk: 3523 case LibFunc_strncpy_chk: 3524 return optimizeStrpNCpyChk(CI, Builder, Func); 3525 case LibFunc_memccpy_chk: 3526 return optimizeMemCCpyChk(CI, Builder); 3527 case LibFunc_snprintf_chk: 3528 return optimizeSNPrintfChk(CI, Builder); 3529 case LibFunc_sprintf_chk: 3530 return optimizeSPrintfChk(CI, Builder); 3531 case LibFunc_strcat_chk: 3532 return optimizeStrCatChk(CI, Builder); 3533 case LibFunc_strlcat_chk: 3534 return optimizeStrLCat(CI, Builder); 3535 case LibFunc_strncat_chk: 3536 return optimizeStrNCatChk(CI, Builder); 3537 case LibFunc_strlcpy_chk: 3538 return optimizeStrLCpyChk(CI, Builder); 3539 case LibFunc_vsnprintf_chk: 3540 return optimizeVSNPrintfChk(CI, Builder); 3541 case LibFunc_vsprintf_chk: 3542 return optimizeVSPrintfChk(CI, Builder); 3543 default: 3544 break; 3545 } 3546 return nullptr; 3547 } 3548 3549 FortifiedLibCallSimplifier::FortifiedLibCallSimplifier( 3550 const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize) 3551 : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {} 3552