1 //===- InstCombineMulDivRem.cpp -------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv, 11 // srem, urem, frem. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "InstCombine.h" 16 #include "llvm/Analysis/InstructionSimplify.h" 17 #include "llvm/IR/IntrinsicInst.h" 18 #include "llvm/IR/PatternMatch.h" 19 using namespace llvm; 20 using namespace PatternMatch; 21 22 #define DEBUG_TYPE "instcombine" 23 24 25 /// simplifyValueKnownNonZero - The specific integer value is used in a context 26 /// where it is known to be non-zero. If this allows us to simplify the 27 /// computation, do so and return the new operand, otherwise return null. 28 static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC, 29 Instruction *CxtI) { 30 // If V has multiple uses, then we would have to do more analysis to determine 31 // if this is safe. For example, the use could be in dynamically unreached 32 // code. 33 if (!V->hasOneUse()) return nullptr; 34 35 bool MadeChange = false; 36 37 // ((1 << A) >>u B) --> (1 << (A-B)) 38 // Because V cannot be zero, we know that B is less than A. 39 Value *A = nullptr, *B = nullptr, *PowerOf2 = nullptr; 40 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))), 41 m_Value(B))) && 42 // The "1" can be any value known to be a power of 2. 43 isKnownToBeAPowerOfTwo(PowerOf2, false, 0, IC.getAssumptionTracker(), 44 CxtI, IC.getDominatorTree())) { 45 A = IC.Builder->CreateSub(A, B); 46 return IC.Builder->CreateShl(PowerOf2, A); 47 } 48 49 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it 50 // inexact. Similarly for <<. 51 if (BinaryOperator *I = dyn_cast<BinaryOperator>(V)) 52 if (I->isLogicalShift() && isKnownToBeAPowerOfTwo(I->getOperand(0), false, 53 0, IC.getAssumptionTracker(), 54 CxtI, 55 IC.getDominatorTree())) { 56 // We know that this is an exact/nuw shift and that the input is a 57 // non-zero context as well. 58 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) { 59 I->setOperand(0, V2); 60 MadeChange = true; 61 } 62 63 if (I->getOpcode() == Instruction::LShr && !I->isExact()) { 64 I->setIsExact(); 65 MadeChange = true; 66 } 67 68 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { 69 I->setHasNoUnsignedWrap(); 70 MadeChange = true; 71 } 72 } 73 74 // TODO: Lots more we could do here: 75 // If V is a phi node, we can call this on each of its operands. 76 // "select cond, X, 0" can simplify to "X". 77 78 return MadeChange ? V : nullptr; 79 } 80 81 82 /// MultiplyOverflows - True if the multiply can not be expressed in an int 83 /// this size. 84 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { 85 uint32_t W = C1->getBitWidth(); 86 APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); 87 if (sign) { 88 LHSExt = LHSExt.sext(W * 2); 89 RHSExt = RHSExt.sext(W * 2); 90 } else { 91 LHSExt = LHSExt.zext(W * 2); 92 RHSExt = RHSExt.zext(W * 2); 93 } 94 95 APInt MulExt = LHSExt * RHSExt; 96 97 if (!sign) 98 return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); 99 100 APInt Min = APInt::getSignedMinValue(W).sext(W * 2); 101 APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); 102 return MulExt.slt(Min) || MulExt.sgt(Max); 103 } 104 105 /// \brief True if C2 is a multiple of C1. Quotient contains C2/C1. 106 static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, 107 bool IsSigned) { 108 assert(C1.getBitWidth() == C2.getBitWidth() && 109 "Inconsistent width of constants!"); 110 111 APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned); 112 if (IsSigned) 113 APInt::sdivrem(C1, C2, Quotient, Remainder); 114 else 115 APInt::udivrem(C1, C2, Quotient, Remainder); 116 117 return Remainder.isMinValue(); 118 } 119 120 /// \brief A helper routine of InstCombiner::visitMul(). 121 /// 122 /// If C is a vector of known powers of 2, then this function returns 123 /// a new vector obtained from C replacing each element with its logBase2. 124 /// Return a null pointer otherwise. 125 static Constant *getLogBase2Vector(ConstantDataVector *CV) { 126 const APInt *IVal; 127 SmallVector<Constant *, 4> Elts; 128 129 for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) { 130 Constant *Elt = CV->getElementAsConstant(I); 131 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2()) 132 return nullptr; 133 Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2())); 134 } 135 136 return ConstantVector::get(Elts); 137 } 138 139 Instruction *InstCombiner::visitMul(BinaryOperator &I) { 140 bool Changed = SimplifyAssociativeOrCommutative(I); 141 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 142 143 if (Value *V = SimplifyVectorOp(I)) 144 return ReplaceInstUsesWith(I, V); 145 146 if (Value *V = SimplifyMulInst(Op0, Op1, DL, TLI, DT, AT)) 147 return ReplaceInstUsesWith(I, V); 148 149 if (Value *V = SimplifyUsingDistributiveLaws(I)) 150 return ReplaceInstUsesWith(I, V); 151 152 if (match(Op1, m_AllOnes())) // X * -1 == 0 - X 153 return BinaryOperator::CreateNeg(Op0, I.getName()); 154 155 // Also allow combining multiply instructions on vectors. 156 { 157 Value *NewOp; 158 Constant *C1, *C2; 159 const APInt *IVal; 160 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)), 161 m_Constant(C1))) && 162 match(C1, m_APInt(IVal))) 163 // ((X << C1)*C2) == (X * (C2 << C1)) 164 return BinaryOperator::CreateMul(NewOp, ConstantExpr::getShl(C1, C2)); 165 166 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { 167 Constant *NewCst = nullptr; 168 if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2()) 169 // Replace X*(2^C) with X << C, where C is either a scalar or a splat. 170 NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2()); 171 else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1)) 172 // Replace X*(2^C) with X << C, where C is a vector of known 173 // constant powers of 2. 174 NewCst = getLogBase2Vector(CV); 175 176 if (NewCst) { 177 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); 178 179 if (I.hasNoSignedWrap()) 180 Shl->setHasNoSignedWrap(); 181 if (I.hasNoUnsignedWrap()) 182 Shl->setHasNoUnsignedWrap(); 183 184 return Shl; 185 } 186 } 187 } 188 189 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { 190 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n 191 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n 192 // The "* (2**n)" thus becomes a potential shifting opportunity. 193 { 194 const APInt & Val = CI->getValue(); 195 const APInt &PosVal = Val.abs(); 196 if (Val.isNegative() && PosVal.isPowerOf2()) { 197 Value *X = nullptr, *Y = nullptr; 198 if (Op0->hasOneUse()) { 199 ConstantInt *C1; 200 Value *Sub = nullptr; 201 if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) 202 Sub = Builder->CreateSub(X, Y, "suba"); 203 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) 204 Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc"); 205 if (Sub) 206 return 207 BinaryOperator::CreateMul(Sub, 208 ConstantInt::get(Y->getType(), PosVal)); 209 } 210 } 211 } 212 } 213 214 // Simplify mul instructions with a constant RHS. 215 if (isa<Constant>(Op1)) { 216 // Try to fold constant mul into select arguments. 217 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 218 if (Instruction *R = FoldOpIntoSelect(I, SI)) 219 return R; 220 221 if (isa<PHINode>(Op0)) 222 if (Instruction *NV = FoldOpIntoPhi(I)) 223 return NV; 224 225 // Canonicalize (X+C1)*CI -> X*CI+C1*CI. 226 { 227 Value *X; 228 Constant *C1; 229 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { 230 Value *Mul = Builder->CreateMul(C1, Op1); 231 // Only go forward with the transform if C1*CI simplifies to a tidier 232 // constant. 233 if (!match(Mul, m_Mul(m_Value(), m_Value()))) 234 return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul); 235 } 236 } 237 } 238 239 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y 240 if (Value *Op1v = dyn_castNegVal(Op1)) 241 return BinaryOperator::CreateMul(Op0v, Op1v); 242 243 // (X / Y) * Y = X - (X % Y) 244 // (X / Y) * -Y = (X % Y) - X 245 { 246 Value *Op1C = Op1; 247 BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); 248 if (!BO || 249 (BO->getOpcode() != Instruction::UDiv && 250 BO->getOpcode() != Instruction::SDiv)) { 251 Op1C = Op0; 252 BO = dyn_cast<BinaryOperator>(Op1); 253 } 254 Value *Neg = dyn_castNegVal(Op1C); 255 if (BO && BO->hasOneUse() && 256 (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) && 257 (BO->getOpcode() == Instruction::UDiv || 258 BO->getOpcode() == Instruction::SDiv)) { 259 Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); 260 261 // If the division is exact, X % Y is zero, so we end up with X or -X. 262 if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO)) 263 if (SDiv->isExact()) { 264 if (Op1BO == Op1C) 265 return ReplaceInstUsesWith(I, Op0BO); 266 return BinaryOperator::CreateNeg(Op0BO); 267 } 268 269 Value *Rem; 270 if (BO->getOpcode() == Instruction::UDiv) 271 Rem = Builder->CreateURem(Op0BO, Op1BO); 272 else 273 Rem = Builder->CreateSRem(Op0BO, Op1BO); 274 Rem->takeName(BO); 275 276 if (Op1BO == Op1C) 277 return BinaryOperator::CreateSub(Op0BO, Rem); 278 return BinaryOperator::CreateSub(Rem, Op0BO); 279 } 280 } 281 282 /// i1 mul -> i1 and. 283 if (I.getType()->getScalarType()->isIntegerTy(1)) 284 return BinaryOperator::CreateAnd(Op0, Op1); 285 286 // X*(1 << Y) --> X << Y 287 // (1 << Y)*X --> X << Y 288 { 289 Value *Y; 290 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) 291 return BinaryOperator::CreateShl(Op1, Y); 292 if (match(Op1, m_Shl(m_One(), m_Value(Y)))) 293 return BinaryOperator::CreateShl(Op0, Y); 294 } 295 296 // If one of the operands of the multiply is a cast from a boolean value, then 297 // we know the bool is either zero or one, so this is a 'masking' multiply. 298 // X * Y (where Y is 0 or 1) -> X & (0-Y) 299 if (!I.getType()->isVectorTy()) { 300 // -2 is "-1 << 1" so it is all bits set except the low one. 301 APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); 302 303 Value *BoolCast = nullptr, *OtherOp = nullptr; 304 if (MaskedValueIsZero(Op0, Negative2, 0, &I)) 305 BoolCast = Op0, OtherOp = Op1; 306 else if (MaskedValueIsZero(Op1, Negative2, 0, &I)) 307 BoolCast = Op1, OtherOp = Op0; 308 309 if (BoolCast) { 310 Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), 311 BoolCast); 312 return BinaryOperator::CreateAnd(V, OtherOp); 313 } 314 } 315 316 return Changed ? &I : nullptr; 317 } 318 319 // 320 // Detect pattern: 321 // 322 // log2(Y*0.5) 323 // 324 // And check for corresponding fast math flags 325 // 326 327 static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) { 328 329 if (!Op->hasOneUse()) 330 return; 331 332 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op); 333 if (!II) 334 return; 335 if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra()) 336 return; 337 Log2 = II; 338 339 Value *OpLog2Of = II->getArgOperand(0); 340 if (!OpLog2Of->hasOneUse()) 341 return; 342 343 Instruction *I = dyn_cast<Instruction>(OpLog2Of); 344 if (!I) 345 return; 346 if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra()) 347 return; 348 349 if (match(I->getOperand(0), m_SpecificFP(0.5))) 350 Y = I->getOperand(1); 351 else if (match(I->getOperand(1), m_SpecificFP(0.5))) 352 Y = I->getOperand(0); 353 } 354 355 static bool isFiniteNonZeroFp(Constant *C) { 356 if (C->getType()->isVectorTy()) { 357 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; 358 ++I) { 359 ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I)); 360 if (!CFP || !CFP->getValueAPF().isFiniteNonZero()) 361 return false; 362 } 363 return true; 364 } 365 366 return isa<ConstantFP>(C) && 367 cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero(); 368 } 369 370 static bool isNormalFp(Constant *C) { 371 if (C->getType()->isVectorTy()) { 372 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; 373 ++I) { 374 ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I)); 375 if (!CFP || !CFP->getValueAPF().isNormal()) 376 return false; 377 } 378 return true; 379 } 380 381 return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal(); 382 } 383 384 /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns 385 /// true iff the given value is FMul or FDiv with one and only one operand 386 /// being a normal constant (i.e. not Zero/NaN/Infinity). 387 static bool isFMulOrFDivWithConstant(Value *V) { 388 Instruction *I = dyn_cast<Instruction>(V); 389 if (!I || (I->getOpcode() != Instruction::FMul && 390 I->getOpcode() != Instruction::FDiv)) 391 return false; 392 393 Constant *C0 = dyn_cast<Constant>(I->getOperand(0)); 394 Constant *C1 = dyn_cast<Constant>(I->getOperand(1)); 395 396 if (C0 && C1) 397 return false; 398 399 return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1)); 400 } 401 402 /// foldFMulConst() is a helper routine of InstCombiner::visitFMul(). 403 /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand 404 /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). 405 /// This function is to simplify "FMulOrDiv * C" and returns the 406 /// resulting expression. Note that this function could return NULL in 407 /// case the constants cannot be folded into a normal floating-point. 408 /// 409 Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C, 410 Instruction *InsertBefore) { 411 assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid"); 412 413 Value *Opnd0 = FMulOrDiv->getOperand(0); 414 Value *Opnd1 = FMulOrDiv->getOperand(1); 415 416 Constant *C0 = dyn_cast<Constant>(Opnd0); 417 Constant *C1 = dyn_cast<Constant>(Opnd1); 418 419 BinaryOperator *R = nullptr; 420 421 // (X * C0) * C => X * (C0*C) 422 if (FMulOrDiv->getOpcode() == Instruction::FMul) { 423 Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C); 424 if (isNormalFp(F)) 425 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F); 426 } else { 427 if (C0) { 428 // (C0 / X) * C => (C0 * C) / X 429 if (FMulOrDiv->hasOneUse()) { 430 // It would otherwise introduce another div. 431 Constant *F = ConstantExpr::getFMul(C0, C); 432 if (isNormalFp(F)) 433 R = BinaryOperator::CreateFDiv(F, Opnd1); 434 } 435 } else { 436 // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal 437 Constant *F = ConstantExpr::getFDiv(C, C1); 438 if (isNormalFp(F)) { 439 R = BinaryOperator::CreateFMul(Opnd0, F); 440 } else { 441 // (X / C1) * C => X / (C1/C) 442 Constant *F = ConstantExpr::getFDiv(C1, C); 443 if (isNormalFp(F)) 444 R = BinaryOperator::CreateFDiv(Opnd0, F); 445 } 446 } 447 } 448 449 if (R) { 450 R->setHasUnsafeAlgebra(true); 451 InsertNewInstWith(R, *InsertBefore); 452 } 453 454 return R; 455 } 456 457 Instruction *InstCombiner::visitFMul(BinaryOperator &I) { 458 bool Changed = SimplifyAssociativeOrCommutative(I); 459 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 460 461 if (Value *V = SimplifyVectorOp(I)) 462 return ReplaceInstUsesWith(I, V); 463 464 if (isa<Constant>(Op0)) 465 std::swap(Op0, Op1); 466 467 if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL, TLI, 468 DT, AT)) 469 return ReplaceInstUsesWith(I, V); 470 471 bool AllowReassociate = I.hasUnsafeAlgebra(); 472 473 // Simplify mul instructions with a constant RHS. 474 if (isa<Constant>(Op1)) { 475 // Try to fold constant mul into select arguments. 476 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 477 if (Instruction *R = FoldOpIntoSelect(I, SI)) 478 return R; 479 480 if (isa<PHINode>(Op0)) 481 if (Instruction *NV = FoldOpIntoPhi(I)) 482 return NV; 483 484 // (fmul X, -1.0) --> (fsub -0.0, X) 485 if (match(Op1, m_SpecificFP(-1.0))) { 486 Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType()); 487 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0); 488 RI->copyFastMathFlags(&I); 489 return RI; 490 } 491 492 Constant *C = cast<Constant>(Op1); 493 if (AllowReassociate && isFiniteNonZeroFp(C)) { 494 // Let MDC denote an expression in one of these forms: 495 // X * C, C/X, X/C, where C is a constant. 496 // 497 // Try to simplify "MDC * Constant" 498 if (isFMulOrFDivWithConstant(Op0)) 499 if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I)) 500 return ReplaceInstUsesWith(I, V); 501 502 // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C) 503 Instruction *FAddSub = dyn_cast<Instruction>(Op0); 504 if (FAddSub && 505 (FAddSub->getOpcode() == Instruction::FAdd || 506 FAddSub->getOpcode() == Instruction::FSub)) { 507 Value *Opnd0 = FAddSub->getOperand(0); 508 Value *Opnd1 = FAddSub->getOperand(1); 509 Constant *C0 = dyn_cast<Constant>(Opnd0); 510 Constant *C1 = dyn_cast<Constant>(Opnd1); 511 bool Swap = false; 512 if (C0) { 513 std::swap(C0, C1); 514 std::swap(Opnd0, Opnd1); 515 Swap = true; 516 } 517 518 if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) { 519 Value *M1 = ConstantExpr::getFMul(C1, C); 520 Value *M0 = isNormalFp(cast<Constant>(M1)) ? 521 foldFMulConst(cast<Instruction>(Opnd0), C, &I) : 522 nullptr; 523 if (M0 && M1) { 524 if (Swap && FAddSub->getOpcode() == Instruction::FSub) 525 std::swap(M0, M1); 526 527 Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd) 528 ? BinaryOperator::CreateFAdd(M0, M1) 529 : BinaryOperator::CreateFSub(M0, M1); 530 RI->copyFastMathFlags(&I); 531 return RI; 532 } 533 } 534 } 535 } 536 } 537 538 // sqrt(X) * sqrt(X) -> X 539 if (AllowReassociate && (Op0 == Op1)) 540 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) 541 if (II->getIntrinsicID() == Intrinsic::sqrt) 542 return ReplaceInstUsesWith(I, II->getOperand(0)); 543 544 // Under unsafe algebra do: 545 // X * log2(0.5*Y) = X*log2(Y) - X 546 if (AllowReassociate) { 547 Value *OpX = nullptr; 548 Value *OpY = nullptr; 549 IntrinsicInst *Log2; 550 detectLog2OfHalf(Op0, OpY, Log2); 551 if (OpY) { 552 OpX = Op1; 553 } else { 554 detectLog2OfHalf(Op1, OpY, Log2); 555 if (OpY) { 556 OpX = Op0; 557 } 558 } 559 // if pattern detected emit alternate sequence 560 if (OpX && OpY) { 561 BuilderTy::FastMathFlagGuard Guard(*Builder); 562 Builder->SetFastMathFlags(Log2->getFastMathFlags()); 563 Log2->setArgOperand(0, OpY); 564 Value *FMulVal = Builder->CreateFMul(OpX, Log2); 565 Value *FSub = Builder->CreateFSub(FMulVal, OpX); 566 FSub->takeName(&I); 567 return ReplaceInstUsesWith(I, FSub); 568 } 569 } 570 571 // Handle symmetric situation in a 2-iteration loop 572 Value *Opnd0 = Op0; 573 Value *Opnd1 = Op1; 574 for (int i = 0; i < 2; i++) { 575 bool IgnoreZeroSign = I.hasNoSignedZeros(); 576 if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) { 577 BuilderTy::FastMathFlagGuard Guard(*Builder); 578 Builder->SetFastMathFlags(I.getFastMathFlags()); 579 580 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign); 581 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign); 582 583 // -X * -Y => X*Y 584 if (N1) { 585 Value *FMul = Builder->CreateFMul(N0, N1); 586 FMul->takeName(&I); 587 return ReplaceInstUsesWith(I, FMul); 588 } 589 590 if (Opnd0->hasOneUse()) { 591 // -X * Y => -(X*Y) (Promote negation as high as possible) 592 Value *T = Builder->CreateFMul(N0, Opnd1); 593 Value *Neg = Builder->CreateFNeg(T); 594 Neg->takeName(&I); 595 return ReplaceInstUsesWith(I, Neg); 596 } 597 } 598 599 // (X*Y) * X => (X*X) * Y where Y != X 600 // The purpose is two-fold: 601 // 1) to form a power expression (of X). 602 // 2) potentially shorten the critical path: After transformation, the 603 // latency of the instruction Y is amortized by the expression of X*X, 604 // and therefore Y is in a "less critical" position compared to what it 605 // was before the transformation. 606 // 607 if (AllowReassociate) { 608 Value *Opnd0_0, *Opnd0_1; 609 if (Opnd0->hasOneUse() && 610 match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) { 611 Value *Y = nullptr; 612 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1) 613 Y = Opnd0_1; 614 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1) 615 Y = Opnd0_0; 616 617 if (Y) { 618 BuilderTy::FastMathFlagGuard Guard(*Builder); 619 Builder->SetFastMathFlags(I.getFastMathFlags()); 620 Value *T = Builder->CreateFMul(Opnd1, Opnd1); 621 622 Value *R = Builder->CreateFMul(T, Y); 623 R->takeName(&I); 624 return ReplaceInstUsesWith(I, R); 625 } 626 } 627 } 628 629 if (!isa<Constant>(Op1)) 630 std::swap(Opnd0, Opnd1); 631 else 632 break; 633 } 634 635 return Changed ? &I : nullptr; 636 } 637 638 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select 639 /// instruction. 640 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { 641 SelectInst *SI = cast<SelectInst>(I.getOperand(1)); 642 643 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y 644 int NonNullOperand = -1; 645 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) 646 if (ST->isNullValue()) 647 NonNullOperand = 2; 648 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y 649 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) 650 if (ST->isNullValue()) 651 NonNullOperand = 1; 652 653 if (NonNullOperand == -1) 654 return false; 655 656 Value *SelectCond = SI->getOperand(0); 657 658 // Change the div/rem to use 'Y' instead of the select. 659 I.setOperand(1, SI->getOperand(NonNullOperand)); 660 661 // Okay, we know we replace the operand of the div/rem with 'Y' with no 662 // problem. However, the select, or the condition of the select may have 663 // multiple uses. Based on our knowledge that the operand must be non-zero, 664 // propagate the known value for the select into other uses of it, and 665 // propagate a known value of the condition into its other users. 666 667 // If the select and condition only have a single use, don't bother with this, 668 // early exit. 669 if (SI->use_empty() && SelectCond->hasOneUse()) 670 return true; 671 672 // Scan the current block backward, looking for other uses of SI. 673 BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); 674 675 while (BBI != BBFront) { 676 --BBI; 677 // If we found a call to a function, we can't assume it will return, so 678 // information from below it cannot be propagated above it. 679 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) 680 break; 681 682 // Replace uses of the select or its condition with the known values. 683 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); 684 I != E; ++I) { 685 if (*I == SI) { 686 *I = SI->getOperand(NonNullOperand); 687 Worklist.Add(BBI); 688 } else if (*I == SelectCond) { 689 *I = Builder->getInt1(NonNullOperand == 1); 690 Worklist.Add(BBI); 691 } 692 } 693 694 // If we past the instruction, quit looking for it. 695 if (&*BBI == SI) 696 SI = nullptr; 697 if (&*BBI == SelectCond) 698 SelectCond = nullptr; 699 700 // If we ran out of things to eliminate, break out of the loop. 701 if (!SelectCond && !SI) 702 break; 703 704 } 705 return true; 706 } 707 708 709 /// This function implements the transforms common to both integer division 710 /// instructions (udiv and sdiv). It is called by the visitors to those integer 711 /// division instructions. 712 /// @brief Common integer divide transforms 713 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { 714 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 715 716 // The RHS is known non-zero. 717 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) { 718 I.setOperand(1, V); 719 return &I; 720 } 721 722 // Handle cases involving: [su]div X, (select Cond, Y, Z) 723 // This does not apply for fdiv. 724 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 725 return &I; 726 727 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 728 if (Instruction *LHS = dyn_cast<Instruction>(Op0)) { 729 // (X / C1) / C2 -> X / (C1*C2) 730 if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) 731 if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { 732 if (MultiplyOverflows(RHS, LHSRHS, 733 I.getOpcode() == Instruction::SDiv)) 734 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); 735 return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), 736 ConstantExpr::getMul(RHS, LHSRHS)); 737 } 738 739 Value *X; 740 const APInt *C1, *C2; 741 if (match(RHS, m_APInt(C2))) { 742 bool IsSigned = I.getOpcode() == Instruction::SDiv; 743 if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) || 744 (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) { 745 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); 746 747 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. 748 if (IsMultiple(*C2, *C1, Quotient, IsSigned)) { 749 BinaryOperator *BO = BinaryOperator::Create( 750 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient)); 751 BO->setIsExact(I.isExact()); 752 return BO; 753 } 754 755 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. 756 if (IsMultiple(*C1, *C2, Quotient, IsSigned)) { 757 BinaryOperator *BO = BinaryOperator::Create( 758 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient)); 759 BO->setHasNoUnsignedWrap( 760 !IsSigned && 761 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap()); 762 BO->setHasNoSignedWrap( 763 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap()); 764 return BO; 765 } 766 } 767 768 if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1)))) || 769 (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) { 770 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); 771 APInt C1Shifted = APInt::getOneBitSet( 772 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue())); 773 774 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1. 775 if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) { 776 BinaryOperator *BO = BinaryOperator::Create( 777 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient)); 778 BO->setIsExact(I.isExact()); 779 return BO; 780 } 781 782 // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2. 783 if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) { 784 BinaryOperator *BO = BinaryOperator::Create( 785 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient)); 786 BO->setHasNoUnsignedWrap( 787 !IsSigned && 788 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap()); 789 BO->setHasNoSignedWrap( 790 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap()); 791 return BO; 792 } 793 } 794 } 795 } 796 797 if (!RHS->isZero()) { // avoid X udiv 0 798 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 799 if (Instruction *R = FoldOpIntoSelect(I, SI)) 800 return R; 801 if (isa<PHINode>(Op0)) 802 if (Instruction *NV = FoldOpIntoPhi(I)) 803 return NV; 804 } 805 } 806 807 if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) { 808 if (One->isOne() && !I.getType()->isIntegerTy(1)) { 809 bool isSigned = I.getOpcode() == Instruction::SDiv; 810 if (isSigned) { 811 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the 812 // result is one, if Op1 is -1 then the result is minus one, otherwise 813 // it's zero. 814 Value *Inc = Builder->CreateAdd(Op1, One); 815 Value *Cmp = Builder->CreateICmpULT( 816 Inc, ConstantInt::get(I.getType(), 3)); 817 return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0)); 818 } else { 819 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the 820 // result is one, otherwise it's zero. 821 return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType()); 822 } 823 } 824 } 825 826 // See if we can fold away this div instruction. 827 if (SimplifyDemandedInstructionBits(I)) 828 return &I; 829 830 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y 831 Value *X = nullptr, *Z = nullptr; 832 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 833 bool isSigned = I.getOpcode() == Instruction::SDiv; 834 if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || 835 (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) 836 return BinaryOperator::Create(I.getOpcode(), X, Op1); 837 } 838 839 return nullptr; 840 } 841 842 /// dyn_castZExtVal - Checks if V is a zext or constant that can 843 /// be truncated to Ty without losing bits. 844 static Value *dyn_castZExtVal(Value *V, Type *Ty) { 845 if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) { 846 if (Z->getSrcTy() == Ty) 847 return Z->getOperand(0); 848 } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { 849 if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth()) 850 return ConstantExpr::getTrunc(C, Ty); 851 } 852 return nullptr; 853 } 854 855 namespace { 856 const unsigned MaxDepth = 6; 857 typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1, 858 const BinaryOperator &I, 859 InstCombiner &IC); 860 861 /// \brief Used to maintain state for visitUDivOperand(). 862 struct UDivFoldAction { 863 FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this 864 ///< operand. This can be zero if this action 865 ///< joins two actions together. 866 867 Value *OperandToFold; ///< Which operand to fold. 868 union { 869 Instruction *FoldResult; ///< The instruction returned when FoldAction is 870 ///< invoked. 871 872 size_t SelectLHSIdx; ///< Stores the LHS action index if this action 873 ///< joins two actions together. 874 }; 875 876 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand) 877 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {} 878 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS) 879 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {} 880 }; 881 } 882 883 // X udiv 2^C -> X >> C 884 static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1, 885 const BinaryOperator &I, InstCombiner &IC) { 886 const APInt &C = cast<Constant>(Op1)->getUniqueInteger(); 887 BinaryOperator *LShr = BinaryOperator::CreateLShr( 888 Op0, ConstantInt::get(Op0->getType(), C.logBase2())); 889 if (I.isExact()) 890 LShr->setIsExact(); 891 return LShr; 892 } 893 894 // X udiv C, where C >= signbit 895 static Instruction *foldUDivNegCst(Value *Op0, Value *Op1, 896 const BinaryOperator &I, InstCombiner &IC) { 897 Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1)); 898 899 return SelectInst::Create(ICI, Constant::getNullValue(I.getType()), 900 ConstantInt::get(I.getType(), 1)); 901 } 902 903 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) 904 static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, 905 InstCombiner &IC) { 906 Instruction *ShiftLeft = cast<Instruction>(Op1); 907 if (isa<ZExtInst>(ShiftLeft)) 908 ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0)); 909 910 const APInt &CI = 911 cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger(); 912 Value *N = ShiftLeft->getOperand(1); 913 if (CI != 1) 914 N = IC.Builder->CreateAdd(N, ConstantInt::get(N->getType(), CI.logBase2())); 915 if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1)) 916 N = IC.Builder->CreateZExt(N, Z->getDestTy()); 917 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N); 918 if (I.isExact()) 919 LShr->setIsExact(); 920 return LShr; 921 } 922 923 // \brief Recursively visits the possible right hand operands of a udiv 924 // instruction, seeing through select instructions, to determine if we can 925 // replace the udiv with something simpler. If we find that an operand is not 926 // able to simplify the udiv, we abort the entire transformation. 927 static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, 928 SmallVectorImpl<UDivFoldAction> &Actions, 929 unsigned Depth = 0) { 930 // Check to see if this is an unsigned division with an exact power of 2, 931 // if so, convert to a right shift. 932 if (match(Op1, m_Power2())) { 933 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1)); 934 return Actions.size(); 935 } 936 937 if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) 938 // X udiv C, where C >= signbit 939 if (C->getValue().isNegative()) { 940 Actions.push_back(UDivFoldAction(foldUDivNegCst, C)); 941 return Actions.size(); 942 } 943 944 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) 945 if (match(Op1, m_Shl(m_Power2(), m_Value())) || 946 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) { 947 Actions.push_back(UDivFoldAction(foldUDivShl, Op1)); 948 return Actions.size(); 949 } 950 951 // The remaining tests are all recursive, so bail out if we hit the limit. 952 if (Depth++ == MaxDepth) 953 return 0; 954 955 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 956 if (size_t LHSIdx = 957 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth)) 958 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) { 959 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1)); 960 return Actions.size(); 961 } 962 963 return 0; 964 } 965 966 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { 967 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 968 969 if (Value *V = SimplifyVectorOp(I)) 970 return ReplaceInstUsesWith(I, V); 971 972 if (Value *V = SimplifyUDivInst(Op0, Op1, DL, TLI, DT, AT)) 973 return ReplaceInstUsesWith(I, V); 974 975 // Handle the integer div common cases 976 if (Instruction *Common = commonIDivTransforms(I)) 977 return Common; 978 979 // (x lshr C1) udiv C2 --> x udiv (C2 << C1) 980 if (Constant *C2 = dyn_cast<Constant>(Op1)) { 981 Value *X; 982 Constant *C1; 983 if (match(Op0, m_LShr(m_Value(X), m_Constant(C1)))) 984 return BinaryOperator::CreateUDiv(X, ConstantExpr::getShl(C2, C1)); 985 } 986 987 // (zext A) udiv (zext B) --> zext (A udiv B) 988 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) 989 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) 990 return new ZExtInst( 991 Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", I.isExact()), 992 I.getType()); 993 994 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...)))) 995 SmallVector<UDivFoldAction, 6> UDivActions; 996 if (visitUDivOperand(Op0, Op1, I, UDivActions)) 997 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) { 998 FoldUDivOperandCb Action = UDivActions[i].FoldAction; 999 Value *ActionOp1 = UDivActions[i].OperandToFold; 1000 Instruction *Inst; 1001 if (Action) 1002 Inst = Action(Op0, ActionOp1, I, *this); 1003 else { 1004 // This action joins two actions together. The RHS of this action is 1005 // simply the last action we processed, we saved the LHS action index in 1006 // the joining action. 1007 size_t SelectRHSIdx = i - 1; 1008 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult; 1009 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx; 1010 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult; 1011 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(), 1012 SelectLHS, SelectRHS); 1013 } 1014 1015 // If this is the last action to process, return it to the InstCombiner. 1016 // Otherwise, we insert it before the UDiv and record it so that we may 1017 // use it as part of a joining action (i.e., a SelectInst). 1018 if (e - i != 1) { 1019 Inst->insertBefore(&I); 1020 UDivActions[i].FoldResult = Inst; 1021 } else 1022 return Inst; 1023 } 1024 1025 return nullptr; 1026 } 1027 1028 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { 1029 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1030 1031 if (Value *V = SimplifyVectorOp(I)) 1032 return ReplaceInstUsesWith(I, V); 1033 1034 if (Value *V = SimplifySDivInst(Op0, Op1, DL, TLI, DT, AT)) 1035 return ReplaceInstUsesWith(I, V); 1036 1037 // Handle the integer div common cases 1038 if (Instruction *Common = commonIDivTransforms(I)) 1039 return Common; 1040 1041 // sdiv X, -1 == -X 1042 if (match(Op1, m_AllOnes())) 1043 return BinaryOperator::CreateNeg(Op0); 1044 1045 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 1046 // sdiv X, C --> ashr exact X, log2(C) 1047 if (I.isExact() && RHS->getValue().isNonNegative() && 1048 RHS->getValue().isPowerOf2()) { 1049 Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), 1050 RHS->getValue().exactLogBase2()); 1051 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); 1052 } 1053 } 1054 1055 if (Constant *RHS = dyn_cast<Constant>(Op1)) { 1056 // X/INT_MIN -> X == INT_MIN 1057 if (RHS->isMinSignedValue()) 1058 return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType()); 1059 1060 // -X/C --> X/-C provided the negation doesn't overflow. 1061 if (SubOperator *Sub = dyn_cast<SubOperator>(Op0)) 1062 if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap()) 1063 return BinaryOperator::CreateSDiv(Sub->getOperand(1), 1064 ConstantExpr::getNeg(RHS)); 1065 } 1066 1067 // If the sign bits of both operands are zero (i.e. we can prove they are 1068 // unsigned inputs), turn this into a udiv. 1069 if (I.getType()->isIntegerTy()) { 1070 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); 1071 if (MaskedValueIsZero(Op0, Mask, 0, &I)) { 1072 if (MaskedValueIsZero(Op1, Mask, 0, &I)) { 1073 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set 1074 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1075 } 1076 1077 if (match(Op1, m_Shl(m_Power2(), m_Value()))) { 1078 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) 1079 // Safe because the only negative value (1 << Y) can take on is 1080 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have 1081 // the sign bit set. 1082 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1083 } 1084 } 1085 } 1086 1087 return nullptr; 1088 } 1089 1090 /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special 1091 /// FP value and: 1092 /// 1) 1/C is exact, or 1093 /// 2) reciprocal is allowed. 1094 /// If the conversion was successful, the simplified expression "X * 1/C" is 1095 /// returned; otherwise, NULL is returned. 1096 /// 1097 static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor, 1098 bool AllowReciprocal) { 1099 if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors. 1100 return nullptr; 1101 1102 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF(); 1103 APFloat Reciprocal(FpVal.getSemantics()); 1104 bool Cvt = FpVal.getExactInverse(&Reciprocal); 1105 1106 if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) { 1107 Reciprocal = APFloat(FpVal.getSemantics(), 1.0f); 1108 (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven); 1109 Cvt = !Reciprocal.isDenormal(); 1110 } 1111 1112 if (!Cvt) 1113 return nullptr; 1114 1115 ConstantFP *R; 1116 R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal); 1117 return BinaryOperator::CreateFMul(Dividend, R); 1118 } 1119 1120 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { 1121 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1122 1123 if (Value *V = SimplifyVectorOp(I)) 1124 return ReplaceInstUsesWith(I, V); 1125 1126 if (Value *V = SimplifyFDivInst(Op0, Op1, DL, TLI, DT, AT)) 1127 return ReplaceInstUsesWith(I, V); 1128 1129 if (isa<Constant>(Op0)) 1130 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 1131 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1132 return R; 1133 1134 bool AllowReassociate = I.hasUnsafeAlgebra(); 1135 bool AllowReciprocal = I.hasAllowReciprocal(); 1136 1137 if (Constant *Op1C = dyn_cast<Constant>(Op1)) { 1138 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 1139 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1140 return R; 1141 1142 if (AllowReassociate) { 1143 Constant *C1 = nullptr; 1144 Constant *C2 = Op1C; 1145 Value *X; 1146 Instruction *Res = nullptr; 1147 1148 if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) { 1149 // (X*C1)/C2 => X * (C1/C2) 1150 // 1151 Constant *C = ConstantExpr::getFDiv(C1, C2); 1152 if (isNormalFp(C)) 1153 Res = BinaryOperator::CreateFMul(X, C); 1154 } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { 1155 // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed] 1156 // 1157 Constant *C = ConstantExpr::getFMul(C1, C2); 1158 if (isNormalFp(C)) { 1159 Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal); 1160 if (!Res) 1161 Res = BinaryOperator::CreateFDiv(X, C); 1162 } 1163 } 1164 1165 if (Res) { 1166 Res->setFastMathFlags(I.getFastMathFlags()); 1167 return Res; 1168 } 1169 } 1170 1171 // X / C => X * 1/C 1172 if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) { 1173 T->copyFastMathFlags(&I); 1174 return T; 1175 } 1176 1177 return nullptr; 1178 } 1179 1180 if (AllowReassociate && isa<Constant>(Op0)) { 1181 Constant *C1 = cast<Constant>(Op0), *C2; 1182 Constant *Fold = nullptr; 1183 Value *X; 1184 bool CreateDiv = true; 1185 1186 // C1 / (X*C2) => (C1/C2) / X 1187 if (match(Op1, m_FMul(m_Value(X), m_Constant(C2)))) 1188 Fold = ConstantExpr::getFDiv(C1, C2); 1189 else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) { 1190 // C1 / (X/C2) => (C1*C2) / X 1191 Fold = ConstantExpr::getFMul(C1, C2); 1192 } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) { 1193 // C1 / (C2/X) => (C1/C2) * X 1194 Fold = ConstantExpr::getFDiv(C1, C2); 1195 CreateDiv = false; 1196 } 1197 1198 if (Fold && isNormalFp(Fold)) { 1199 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X) 1200 : BinaryOperator::CreateFMul(X, Fold); 1201 R->setFastMathFlags(I.getFastMathFlags()); 1202 return R; 1203 } 1204 return nullptr; 1205 } 1206 1207 if (AllowReassociate) { 1208 Value *X, *Y; 1209 Value *NewInst = nullptr; 1210 Instruction *SimpR = nullptr; 1211 1212 if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) { 1213 // (X/Y) / Z => X / (Y*Z) 1214 // 1215 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) { 1216 NewInst = Builder->CreateFMul(Y, Op1); 1217 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { 1218 FastMathFlags Flags = I.getFastMathFlags(); 1219 Flags &= cast<Instruction>(Op0)->getFastMathFlags(); 1220 RI->setFastMathFlags(Flags); 1221 } 1222 SimpR = BinaryOperator::CreateFDiv(X, NewInst); 1223 } 1224 } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) { 1225 // Z / (X/Y) => Z*Y / X 1226 // 1227 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) { 1228 NewInst = Builder->CreateFMul(Op0, Y); 1229 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { 1230 FastMathFlags Flags = I.getFastMathFlags(); 1231 Flags &= cast<Instruction>(Op1)->getFastMathFlags(); 1232 RI->setFastMathFlags(Flags); 1233 } 1234 SimpR = BinaryOperator::CreateFDiv(NewInst, X); 1235 } 1236 } 1237 1238 if (NewInst) { 1239 if (Instruction *T = dyn_cast<Instruction>(NewInst)) 1240 T->setDebugLoc(I.getDebugLoc()); 1241 SimpR->setFastMathFlags(I.getFastMathFlags()); 1242 return SimpR; 1243 } 1244 } 1245 1246 return nullptr; 1247 } 1248 1249 /// This function implements the transforms common to both integer remainder 1250 /// instructions (urem and srem). It is called by the visitors to those integer 1251 /// remainder instructions. 1252 /// @brief Common integer remainder transforms 1253 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { 1254 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1255 1256 // The RHS is known non-zero. 1257 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) { 1258 I.setOperand(1, V); 1259 return &I; 1260 } 1261 1262 // Handle cases involving: rem X, (select Cond, Y, Z) 1263 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 1264 return &I; 1265 1266 if (isa<Constant>(Op1)) { 1267 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { 1268 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { 1269 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1270 return R; 1271 } else if (isa<PHINode>(Op0I)) { 1272 if (Instruction *NV = FoldOpIntoPhi(I)) 1273 return NV; 1274 } 1275 1276 // See if we can fold away this rem instruction. 1277 if (SimplifyDemandedInstructionBits(I)) 1278 return &I; 1279 } 1280 } 1281 1282 return nullptr; 1283 } 1284 1285 Instruction *InstCombiner::visitURem(BinaryOperator &I) { 1286 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1287 1288 if (Value *V = SimplifyVectorOp(I)) 1289 return ReplaceInstUsesWith(I, V); 1290 1291 if (Value *V = SimplifyURemInst(Op0, Op1, DL, TLI, DT, AT)) 1292 return ReplaceInstUsesWith(I, V); 1293 1294 if (Instruction *common = commonIRemTransforms(I)) 1295 return common; 1296 1297 // (zext A) urem (zext B) --> zext (A urem B) 1298 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) 1299 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) 1300 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1), 1301 I.getType()); 1302 1303 // X urem Y -> X and Y-1, where Y is a power of 2, 1304 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true, 0, AT, &I, DT)) { 1305 Constant *N1 = Constant::getAllOnesValue(I.getType()); 1306 Value *Add = Builder->CreateAdd(Op1, N1); 1307 return BinaryOperator::CreateAnd(Op0, Add); 1308 } 1309 1310 // 1 urem X -> zext(X != 1) 1311 if (match(Op0, m_One())) { 1312 Value *Cmp = Builder->CreateICmpNE(Op1, Op0); 1313 Value *Ext = Builder->CreateZExt(Cmp, I.getType()); 1314 return ReplaceInstUsesWith(I, Ext); 1315 } 1316 1317 return nullptr; 1318 } 1319 1320 Instruction *InstCombiner::visitSRem(BinaryOperator &I) { 1321 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1322 1323 if (Value *V = SimplifyVectorOp(I)) 1324 return ReplaceInstUsesWith(I, V); 1325 1326 if (Value *V = SimplifySRemInst(Op0, Op1, DL, TLI, DT, AT)) 1327 return ReplaceInstUsesWith(I, V); 1328 1329 // Handle the integer rem common cases 1330 if (Instruction *Common = commonIRemTransforms(I)) 1331 return Common; 1332 1333 if (Value *RHSNeg = dyn_castNegVal(Op1)) 1334 if (!isa<Constant>(RHSNeg) || 1335 (isa<ConstantInt>(RHSNeg) && 1336 cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { 1337 // X % -Y -> X % Y 1338 Worklist.AddValue(I.getOperand(1)); 1339 I.setOperand(1, RHSNeg); 1340 return &I; 1341 } 1342 1343 // If the sign bits of both operands are zero (i.e. we can prove they are 1344 // unsigned inputs), turn this into a urem. 1345 if (I.getType()->isIntegerTy()) { 1346 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); 1347 if (MaskedValueIsZero(Op1, Mask, 0, &I) && 1348 MaskedValueIsZero(Op0, Mask, 0, &I)) { 1349 // X srem Y -> X urem Y, iff X and Y don't have sign bit set 1350 return BinaryOperator::CreateURem(Op0, Op1, I.getName()); 1351 } 1352 } 1353 1354 // If it's a constant vector, flip any negative values positive. 1355 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { 1356 Constant *C = cast<Constant>(Op1); 1357 unsigned VWidth = C->getType()->getVectorNumElements(); 1358 1359 bool hasNegative = false; 1360 bool hasMissing = false; 1361 for (unsigned i = 0; i != VWidth; ++i) { 1362 Constant *Elt = C->getAggregateElement(i); 1363 if (!Elt) { 1364 hasMissing = true; 1365 break; 1366 } 1367 1368 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) 1369 if (RHS->isNegative()) 1370 hasNegative = true; 1371 } 1372 1373 if (hasNegative && !hasMissing) { 1374 SmallVector<Constant *, 16> Elts(VWidth); 1375 for (unsigned i = 0; i != VWidth; ++i) { 1376 Elts[i] = C->getAggregateElement(i); // Handle undef, etc. 1377 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { 1378 if (RHS->isNegative()) 1379 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); 1380 } 1381 } 1382 1383 Constant *NewRHSV = ConstantVector::get(Elts); 1384 if (NewRHSV != C) { // Don't loop on -MININT 1385 Worklist.AddValue(I.getOperand(1)); 1386 I.setOperand(1, NewRHSV); 1387 return &I; 1388 } 1389 } 1390 } 1391 1392 return nullptr; 1393 } 1394 1395 Instruction *InstCombiner::visitFRem(BinaryOperator &I) { 1396 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1397 1398 if (Value *V = SimplifyVectorOp(I)) 1399 return ReplaceInstUsesWith(I, V); 1400 1401 if (Value *V = SimplifyFRemInst(Op0, Op1, DL, TLI, DT, AT)) 1402 return ReplaceInstUsesWith(I, V); 1403 1404 // Handle cases involving: rem X, (select Cond, Y, Z) 1405 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 1406 return &I; 1407 1408 return nullptr; 1409 } 1410