1 //===- InstCombineMulDivRem.cpp -------------------------------------------===// 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 visit functions for mul, fmul, sdiv, udiv, fdiv, 10 // srem, urem, frem. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InstCombineInternal.h" 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/Analysis/InstructionSimplify.h" 18 #include "llvm/Analysis/ValueTracking.h" 19 #include "llvm/IR/BasicBlock.h" 20 #include "llvm/IR/Constant.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/InstrTypes.h" 23 #include "llvm/IR/Instruction.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/IntrinsicInst.h" 26 #include "llvm/IR/Intrinsics.h" 27 #include "llvm/IR/Operator.h" 28 #include "llvm/IR/PatternMatch.h" 29 #include "llvm/IR/Type.h" 30 #include "llvm/IR/Value.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Transforms/InstCombine/InstCombiner.h" 34 #include "llvm/Transforms/Utils/BuildLibCalls.h" 35 #include <cassert> 36 37 #define DEBUG_TYPE "instcombine" 38 #include "llvm/Transforms/Utils/InstructionWorklist.h" 39 40 using namespace llvm; 41 using namespace PatternMatch; 42 43 /// The specific integer value is used in a context where it is known to be 44 /// non-zero. If this allows us to simplify the computation, do so and return 45 /// the new operand, otherwise return null. 46 static Value *simplifyValueKnownNonZero(Value *V, InstCombinerImpl &IC, 47 Instruction &CxtI) { 48 // If V has multiple uses, then we would have to do more analysis to determine 49 // if this is safe. For example, the use could be in dynamically unreached 50 // code. 51 if (!V->hasOneUse()) return nullptr; 52 53 bool MadeChange = false; 54 55 // ((1 << A) >>u B) --> (1 << (A-B)) 56 // Because V cannot be zero, we know that B is less than A. 57 Value *A = nullptr, *B = nullptr, *One = nullptr; 58 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) && 59 match(One, m_One())) { 60 A = IC.Builder.CreateSub(A, B); 61 return IC.Builder.CreateShl(One, A); 62 } 63 64 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it 65 // inexact. Similarly for <<. 66 BinaryOperator *I = dyn_cast<BinaryOperator>(V); 67 if (I && I->isLogicalShift() && 68 IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) { 69 // We know that this is an exact/nuw shift and that the input is a 70 // non-zero context as well. 71 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) { 72 IC.replaceOperand(*I, 0, V2); 73 MadeChange = true; 74 } 75 76 if (I->getOpcode() == Instruction::LShr && !I->isExact()) { 77 I->setIsExact(); 78 MadeChange = true; 79 } 80 81 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { 82 I->setHasNoUnsignedWrap(); 83 MadeChange = true; 84 } 85 } 86 87 // TODO: Lots more we could do here: 88 // If V is a phi node, we can call this on each of its operands. 89 // "select cond, X, 0" can simplify to "X". 90 91 return MadeChange ? V : nullptr; 92 } 93 94 // TODO: This is a specific form of a much more general pattern. 95 // We could detect a select with any binop identity constant, or we 96 // could use SimplifyBinOp to see if either arm of the select reduces. 97 // But that needs to be done carefully and/or while removing potential 98 // reverse canonicalizations as in InstCombiner::foldSelectIntoOp(). 99 static Value *foldMulSelectToNegate(BinaryOperator &I, 100 InstCombiner::BuilderTy &Builder) { 101 Value *Cond, *OtherOp; 102 103 // mul (select Cond, 1, -1), OtherOp --> select Cond, OtherOp, -OtherOp 104 // mul OtherOp, (select Cond, 1, -1) --> select Cond, OtherOp, -OtherOp 105 if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())), 106 m_Value(OtherOp)))) { 107 bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap(); 108 Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap); 109 return Builder.CreateSelect(Cond, OtherOp, Neg); 110 } 111 // mul (select Cond, -1, 1), OtherOp --> select Cond, -OtherOp, OtherOp 112 // mul OtherOp, (select Cond, -1, 1) --> select Cond, -OtherOp, OtherOp 113 if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())), 114 m_Value(OtherOp)))) { 115 bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap(); 116 Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap); 117 return Builder.CreateSelect(Cond, Neg, OtherOp); 118 } 119 120 // fmul (select Cond, 1.0, -1.0), OtherOp --> select Cond, OtherOp, -OtherOp 121 // fmul OtherOp, (select Cond, 1.0, -1.0) --> select Cond, OtherOp, -OtherOp 122 if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(1.0), 123 m_SpecificFP(-1.0))), 124 m_Value(OtherOp)))) { 125 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 126 Builder.setFastMathFlags(I.getFastMathFlags()); 127 return Builder.CreateSelect(Cond, OtherOp, Builder.CreateFNeg(OtherOp)); 128 } 129 130 // fmul (select Cond, -1.0, 1.0), OtherOp --> select Cond, -OtherOp, OtherOp 131 // fmul OtherOp, (select Cond, -1.0, 1.0) --> select Cond, -OtherOp, OtherOp 132 if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(-1.0), 133 m_SpecificFP(1.0))), 134 m_Value(OtherOp)))) { 135 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 136 Builder.setFastMathFlags(I.getFastMathFlags()); 137 return Builder.CreateSelect(Cond, Builder.CreateFNeg(OtherOp), OtherOp); 138 } 139 140 return nullptr; 141 } 142 143 /// Reduce integer multiplication patterns that contain a (+/-1 << Z) factor. 144 /// Callers are expected to call this twice to handle commuted patterns. 145 static Value *foldMulShl1(BinaryOperator &Mul, bool CommuteOperands, 146 InstCombiner::BuilderTy &Builder) { 147 Value *X = Mul.getOperand(0), *Y = Mul.getOperand(1); 148 if (CommuteOperands) 149 std::swap(X, Y); 150 151 const bool HasNSW = Mul.hasNoSignedWrap(); 152 const bool HasNUW = Mul.hasNoUnsignedWrap(); 153 154 // X * (1 << Z) --> X << Z 155 Value *Z; 156 if (match(Y, m_Shl(m_One(), m_Value(Z)))) { 157 bool PropagateNSW = HasNSW && cast<ShlOperator>(Y)->hasNoSignedWrap(); 158 return Builder.CreateShl(X, Z, Mul.getName(), HasNUW, PropagateNSW); 159 } 160 161 // Similar to above, but an increment of the shifted value becomes an add: 162 // X * ((1 << Z) + 1) --> (X * (1 << Z)) + X --> (X << Z) + X 163 // This increases uses of X, so it may require a freeze, but that is still 164 // expected to be an improvement because it removes the multiply. 165 BinaryOperator *Shift; 166 if (match(Y, m_OneUse(m_Add(m_BinOp(Shift), m_One()))) && 167 match(Shift, m_OneUse(m_Shl(m_One(), m_Value(Z))))) { 168 bool PropagateNSW = HasNSW && Shift->hasNoSignedWrap(); 169 Value *FrX = Builder.CreateFreeze(X, X->getName() + ".fr"); 170 Value *Shl = Builder.CreateShl(FrX, Z, "mulshl", HasNUW, PropagateNSW); 171 return Builder.CreateAdd(Shl, FrX, Mul.getName(), HasNUW, PropagateNSW); 172 } 173 174 // Similar to above, but a decrement of the shifted value is disguised as 175 // 'not' and becomes a sub: 176 // X * (~(-1 << Z)) --> X * ((1 << Z) - 1) --> (X << Z) - X 177 // This increases uses of X, so it may require a freeze, but that is still 178 // expected to be an improvement because it removes the multiply. 179 if (match(Y, m_OneUse(m_Not(m_OneUse(m_Shl(m_AllOnes(), m_Value(Z))))))) { 180 Value *FrX = Builder.CreateFreeze(X, X->getName() + ".fr"); 181 Value *Shl = Builder.CreateShl(FrX, Z, "mulshl"); 182 return Builder.CreateSub(Shl, FrX, Mul.getName()); 183 } 184 185 return nullptr; 186 } 187 188 Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) { 189 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 190 if (Value *V = 191 simplifyMulInst(Op0, Op1, I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), 192 SQ.getWithInstruction(&I))) 193 return replaceInstUsesWith(I, V); 194 195 if (SimplifyAssociativeOrCommutative(I)) 196 return &I; 197 198 if (Instruction *X = foldVectorBinop(I)) 199 return X; 200 201 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 202 return Phi; 203 204 if (Value *V = foldUsingDistributiveLaws(I)) 205 return replaceInstUsesWith(I, V); 206 207 Type *Ty = I.getType(); 208 const unsigned BitWidth = Ty->getScalarSizeInBits(); 209 const bool HasNSW = I.hasNoSignedWrap(); 210 const bool HasNUW = I.hasNoUnsignedWrap(); 211 212 // X * -1 --> 0 - X 213 if (match(Op1, m_AllOnes())) { 214 return HasNSW ? BinaryOperator::CreateNSWNeg(Op0) 215 : BinaryOperator::CreateNeg(Op0); 216 } 217 218 // Also allow combining multiply instructions on vectors. 219 { 220 Value *NewOp; 221 Constant *C1, *C2; 222 const APInt *IVal; 223 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)), 224 m_Constant(C1))) && 225 match(C1, m_APInt(IVal))) { 226 // ((X << C2)*C1) == (X * (C1 << C2)) 227 Constant *Shl = ConstantExpr::getShl(C1, C2); 228 BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0)); 229 BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl); 230 if (HasNUW && Mul->hasNoUnsignedWrap()) 231 BO->setHasNoUnsignedWrap(); 232 if (HasNSW && Mul->hasNoSignedWrap() && Shl->isNotMinSignedValue()) 233 BO->setHasNoSignedWrap(); 234 return BO; 235 } 236 237 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { 238 // Replace X*(2^C) with X << C, where C is either a scalar or a vector. 239 if (Constant *NewCst = ConstantExpr::getExactLogBase2(C1)) { 240 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); 241 242 if (HasNUW) 243 Shl->setHasNoUnsignedWrap(); 244 if (HasNSW) { 245 const APInt *V; 246 if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1) 247 Shl->setHasNoSignedWrap(); 248 } 249 250 return Shl; 251 } 252 } 253 } 254 255 if (Op0->hasOneUse() && match(Op1, m_NegatedPower2())) { 256 // Interpret X * (-1<<C) as (-X) * (1<<C) and try to sink the negation. 257 // The "* (1<<C)" thus becomes a potential shifting opportunity. 258 if (Value *NegOp0 = Negator::Negate(/*IsNegation*/ true, Op0, *this)) 259 return BinaryOperator::CreateMul( 260 NegOp0, ConstantExpr::getNeg(cast<Constant>(Op1)), I.getName()); 261 262 // Try to convert multiply of extended operand to narrow negate and shift 263 // for better analysis. 264 // This is valid if the shift amount (trailing zeros in the multiplier 265 // constant) clears more high bits than the bitwidth difference between 266 // source and destination types: 267 // ({z/s}ext X) * (-1<<C) --> (zext (-X)) << C 268 const APInt *NegPow2C; 269 Value *X; 270 if (match(Op0, m_ZExtOrSExt(m_Value(X))) && 271 match(Op1, m_APIntAllowUndef(NegPow2C))) { 272 unsigned SrcWidth = X->getType()->getScalarSizeInBits(); 273 unsigned ShiftAmt = NegPow2C->countTrailingZeros(); 274 if (ShiftAmt >= BitWidth - SrcWidth) { 275 Value *N = Builder.CreateNeg(X, X->getName() + ".neg"); 276 Value *Z = Builder.CreateZExt(N, Ty, N->getName() + ".z"); 277 return BinaryOperator::CreateShl(Z, ConstantInt::get(Ty, ShiftAmt)); 278 } 279 } 280 } 281 282 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) 283 return FoldedMul; 284 285 if (Value *FoldedMul = foldMulSelectToNegate(I, Builder)) 286 return replaceInstUsesWith(I, FoldedMul); 287 288 // Simplify mul instructions with a constant RHS. 289 Constant *MulC; 290 if (match(Op1, m_ImmConstant(MulC))) { 291 // Canonicalize (X+C1)*MulC -> X*MulC+C1*MulC. 292 // Canonicalize (X|C1)*MulC -> X*MulC+C1*MulC. 293 Value *X; 294 Constant *C1; 295 if ((match(Op0, m_OneUse(m_Add(m_Value(X), m_ImmConstant(C1))))) || 296 (match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C1)))) && 297 haveNoCommonBitsSet(X, C1, DL, &AC, &I, &DT))) { 298 // C1*MulC simplifies to a tidier constant. 299 Value *NewC = Builder.CreateMul(C1, MulC); 300 auto *BOp0 = cast<BinaryOperator>(Op0); 301 bool Op0NUW = 302 (BOp0->getOpcode() == Instruction::Or || BOp0->hasNoUnsignedWrap()); 303 Value *NewMul = Builder.CreateMul(X, MulC); 304 auto *BO = BinaryOperator::CreateAdd(NewMul, NewC); 305 if (HasNUW && Op0NUW) { 306 // If NewMulBO is constant we also can set BO to nuw. 307 if (auto *NewMulBO = dyn_cast<BinaryOperator>(NewMul)) 308 NewMulBO->setHasNoUnsignedWrap(); 309 BO->setHasNoUnsignedWrap(); 310 } 311 return BO; 312 } 313 } 314 315 // abs(X) * abs(X) -> X * X 316 // nabs(X) * nabs(X) -> X * X 317 if (Op0 == Op1) { 318 Value *X, *Y; 319 SelectPatternFlavor SPF = matchSelectPattern(Op0, X, Y).Flavor; 320 if (SPF == SPF_ABS || SPF == SPF_NABS) 321 return BinaryOperator::CreateMul(X, X); 322 323 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) 324 return BinaryOperator::CreateMul(X, X); 325 } 326 327 // -X * C --> X * -C 328 Value *X, *Y; 329 Constant *Op1C; 330 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C))) 331 return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C)); 332 333 // -X * -Y --> X * Y 334 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) { 335 auto *NewMul = BinaryOperator::CreateMul(X, Y); 336 if (HasNSW && cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() && 337 cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap()) 338 NewMul->setHasNoSignedWrap(); 339 return NewMul; 340 } 341 342 // -X * Y --> -(X * Y) 343 // X * -Y --> -(X * Y) 344 if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y)))) 345 return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y)); 346 347 // (X / Y) * Y = X - (X % Y) 348 // (X / Y) * -Y = (X % Y) - X 349 { 350 Value *Y = Op1; 351 BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0); 352 if (!Div || (Div->getOpcode() != Instruction::UDiv && 353 Div->getOpcode() != Instruction::SDiv)) { 354 Y = Op0; 355 Div = dyn_cast<BinaryOperator>(Op1); 356 } 357 Value *Neg = dyn_castNegVal(Y); 358 if (Div && Div->hasOneUse() && 359 (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) && 360 (Div->getOpcode() == Instruction::UDiv || 361 Div->getOpcode() == Instruction::SDiv)) { 362 Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1); 363 364 // If the division is exact, X % Y is zero, so we end up with X or -X. 365 if (Div->isExact()) { 366 if (DivOp1 == Y) 367 return replaceInstUsesWith(I, X); 368 return BinaryOperator::CreateNeg(X); 369 } 370 371 auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem 372 : Instruction::SRem; 373 // X must be frozen because we are increasing its number of uses. 374 Value *XFreeze = Builder.CreateFreeze(X, X->getName() + ".fr"); 375 Value *Rem = Builder.CreateBinOp(RemOpc, XFreeze, DivOp1); 376 if (DivOp1 == Y) 377 return BinaryOperator::CreateSub(XFreeze, Rem); 378 return BinaryOperator::CreateSub(Rem, XFreeze); 379 } 380 } 381 382 // Fold the following two scenarios: 383 // 1) i1 mul -> i1 and. 384 // 2) X * Y --> X & Y, iff X, Y can be only {0,1}. 385 // Note: We could use known bits to generalize this and related patterns with 386 // shifts/truncs 387 if (Ty->isIntOrIntVectorTy(1) || 388 (match(Op0, m_And(m_Value(), m_One())) && 389 match(Op1, m_And(m_Value(), m_One())))) 390 return BinaryOperator::CreateAnd(Op0, Op1); 391 392 if (Value *R = foldMulShl1(I, /* CommuteOperands */ false, Builder)) 393 return replaceInstUsesWith(I, R); 394 if (Value *R = foldMulShl1(I, /* CommuteOperands */ true, Builder)) 395 return replaceInstUsesWith(I, R); 396 397 // (zext bool X) * (zext bool Y) --> zext (and X, Y) 398 // (sext bool X) * (sext bool Y) --> zext (and X, Y) 399 // Note: -1 * -1 == 1 * 1 == 1 (if the extends match, the result is the same) 400 if (((match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) || 401 (match(Op0, m_SExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) && 402 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() && 403 (Op0->hasOneUse() || Op1->hasOneUse() || X == Y)) { 404 Value *And = Builder.CreateAnd(X, Y, "mulbool"); 405 return CastInst::Create(Instruction::ZExt, And, Ty); 406 } 407 // (sext bool X) * (zext bool Y) --> sext (and X, Y) 408 // (zext bool X) * (sext bool Y) --> sext (and X, Y) 409 // Note: -1 * 1 == 1 * -1 == -1 410 if (((match(Op0, m_SExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) || 411 (match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) && 412 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() && 413 (Op0->hasOneUse() || Op1->hasOneUse())) { 414 Value *And = Builder.CreateAnd(X, Y, "mulbool"); 415 return CastInst::Create(Instruction::SExt, And, Ty); 416 } 417 418 // (zext bool X) * Y --> X ? Y : 0 419 // Y * (zext bool X) --> X ? Y : 0 420 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) 421 return SelectInst::Create(X, Op1, ConstantInt::getNullValue(Ty)); 422 if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) 423 return SelectInst::Create(X, Op0, ConstantInt::getNullValue(Ty)); 424 425 Constant *ImmC; 426 if (match(Op1, m_ImmConstant(ImmC))) { 427 // (sext bool X) * C --> X ? -C : 0 428 if (match(Op0, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { 429 Constant *NegC = ConstantExpr::getNeg(ImmC); 430 return SelectInst::Create(X, NegC, ConstantInt::getNullValue(Ty)); 431 } 432 433 // (ashr i32 X, 31) * C --> (X < 0) ? -C : 0 434 const APInt *C; 435 if (match(Op0, m_OneUse(m_AShr(m_Value(X), m_APInt(C)))) && 436 *C == C->getBitWidth() - 1) { 437 Constant *NegC = ConstantExpr::getNeg(ImmC); 438 Value *IsNeg = Builder.CreateIsNeg(X, "isneg"); 439 return SelectInst::Create(IsNeg, NegC, ConstantInt::getNullValue(Ty)); 440 } 441 } 442 443 // (lshr X, 31) * Y --> (X < 0) ? Y : 0 444 // TODO: We are not checking one-use because the elimination of the multiply 445 // is better for analysis? 446 const APInt *C; 447 if (match(&I, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C)), m_Value(Y))) && 448 *C == C->getBitWidth() - 1) { 449 Value *IsNeg = Builder.CreateIsNeg(X, "isneg"); 450 return SelectInst::Create(IsNeg, Y, ConstantInt::getNullValue(Ty)); 451 } 452 453 // (and X, 1) * Y --> (trunc X) ? Y : 0 454 if (match(&I, m_c_BinOp(m_OneUse(m_And(m_Value(X), m_One())), m_Value(Y)))) { 455 Value *Tr = Builder.CreateTrunc(X, CmpInst::makeCmpResultType(Ty)); 456 return SelectInst::Create(Tr, Y, ConstantInt::getNullValue(Ty)); 457 } 458 459 // ((ashr X, 31) | 1) * X --> abs(X) 460 // X * ((ashr X, 31) | 1) --> abs(X) 461 if (match(&I, m_c_BinOp(m_Or(m_AShr(m_Value(X), 462 m_SpecificIntAllowUndef(BitWidth - 1)), 463 m_One()), 464 m_Deferred(X)))) { 465 Value *Abs = Builder.CreateBinaryIntrinsic( 466 Intrinsic::abs, X, ConstantInt::getBool(I.getContext(), HasNSW)); 467 Abs->takeName(&I); 468 return replaceInstUsesWith(I, Abs); 469 } 470 471 if (Instruction *Ext = narrowMathIfNoOverflow(I)) 472 return Ext; 473 474 bool Changed = false; 475 if (!HasNSW && willNotOverflowSignedMul(Op0, Op1, I)) { 476 Changed = true; 477 I.setHasNoSignedWrap(true); 478 } 479 480 if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1, I)) { 481 Changed = true; 482 I.setHasNoUnsignedWrap(true); 483 } 484 485 return Changed ? &I : nullptr; 486 } 487 488 Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) { 489 BinaryOperator::BinaryOps Opcode = I.getOpcode(); 490 assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) && 491 "Expected fmul or fdiv"); 492 493 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 494 Value *X, *Y; 495 496 // -X * -Y --> X * Y 497 // -X / -Y --> X / Y 498 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) 499 return BinaryOperator::CreateWithCopiedFlags(Opcode, X, Y, &I); 500 501 // fabs(X) * fabs(X) -> X * X 502 // fabs(X) / fabs(X) -> X / X 503 if (Op0 == Op1 && match(Op0, m_FAbs(m_Value(X)))) 504 return BinaryOperator::CreateWithCopiedFlags(Opcode, X, X, &I); 505 506 // fabs(X) * fabs(Y) --> fabs(X * Y) 507 // fabs(X) / fabs(Y) --> fabs(X / Y) 508 if (match(Op0, m_FAbs(m_Value(X))) && match(Op1, m_FAbs(m_Value(Y))) && 509 (Op0->hasOneUse() || Op1->hasOneUse())) { 510 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 511 Builder.setFastMathFlags(I.getFastMathFlags()); 512 Value *XY = Builder.CreateBinOp(Opcode, X, Y); 513 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, XY); 514 Fabs->takeName(&I); 515 return replaceInstUsesWith(I, Fabs); 516 } 517 518 return nullptr; 519 } 520 521 Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) { 522 if (Value *V = simplifyFMulInst(I.getOperand(0), I.getOperand(1), 523 I.getFastMathFlags(), 524 SQ.getWithInstruction(&I))) 525 return replaceInstUsesWith(I, V); 526 527 if (SimplifyAssociativeOrCommutative(I)) 528 return &I; 529 530 if (Instruction *X = foldVectorBinop(I)) 531 return X; 532 533 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 534 return Phi; 535 536 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) 537 return FoldedMul; 538 539 if (Value *FoldedMul = foldMulSelectToNegate(I, Builder)) 540 return replaceInstUsesWith(I, FoldedMul); 541 542 if (Instruction *R = foldFPSignBitOps(I)) 543 return R; 544 545 // X * -1.0 --> -X 546 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 547 if (match(Op1, m_SpecificFP(-1.0))) 548 return UnaryOperator::CreateFNegFMF(Op0, &I); 549 550 // With no-nans: X * 0.0 --> copysign(0.0, X) 551 if (I.hasNoNaNs() && match(Op1, m_PosZeroFP())) { 552 CallInst *CopySign = Builder.CreateIntrinsic(Intrinsic::copysign, 553 {I.getType()}, {Op1, Op0}, &I); 554 return replaceInstUsesWith(I, CopySign); 555 } 556 557 // -X * C --> X * -C 558 Value *X, *Y; 559 Constant *C; 560 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C))) 561 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) 562 return BinaryOperator::CreateFMulFMF(X, NegC, &I); 563 564 // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E) 565 if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1)) 566 return replaceInstUsesWith(I, V); 567 568 if (I.hasAllowReassoc()) { 569 // Reassociate constant RHS with another constant to form constant 570 // expression. 571 if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) { 572 Constant *C1; 573 if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) { 574 // (C1 / X) * C --> (C * C1) / X 575 Constant *CC1 = 576 ConstantFoldBinaryOpOperands(Instruction::FMul, C, C1, DL); 577 if (CC1 && CC1->isNormalFP()) 578 return BinaryOperator::CreateFDivFMF(CC1, X, &I); 579 } 580 if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { 581 // (X / C1) * C --> X * (C / C1) 582 Constant *CDivC1 = 583 ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C1, DL); 584 if (CDivC1 && CDivC1->isNormalFP()) 585 return BinaryOperator::CreateFMulFMF(X, CDivC1, &I); 586 587 // If the constant was a denormal, try reassociating differently. 588 // (X / C1) * C --> X / (C1 / C) 589 Constant *C1DivC = 590 ConstantFoldBinaryOpOperands(Instruction::FDiv, C1, C, DL); 591 if (C1DivC && Op0->hasOneUse() && C1DivC->isNormalFP()) 592 return BinaryOperator::CreateFDivFMF(X, C1DivC, &I); 593 } 594 595 // We do not need to match 'fadd C, X' and 'fsub X, C' because they are 596 // canonicalized to 'fadd X, C'. Distributing the multiply may allow 597 // further folds and (X * C) + C2 is 'fma'. 598 if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) { 599 // (X + C1) * C --> (X * C) + (C * C1) 600 if (Constant *CC1 = ConstantFoldBinaryOpOperands( 601 Instruction::FMul, C, C1, DL)) { 602 Value *XC = Builder.CreateFMulFMF(X, C, &I); 603 return BinaryOperator::CreateFAddFMF(XC, CC1, &I); 604 } 605 } 606 if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) { 607 // (C1 - X) * C --> (C * C1) - (X * C) 608 if (Constant *CC1 = ConstantFoldBinaryOpOperands( 609 Instruction::FMul, C, C1, DL)) { 610 Value *XC = Builder.CreateFMulFMF(X, C, &I); 611 return BinaryOperator::CreateFSubFMF(CC1, XC, &I); 612 } 613 } 614 } 615 616 Value *Z; 617 if (match(&I, m_c_FMul(m_OneUse(m_FDiv(m_Value(X), m_Value(Y))), 618 m_Value(Z)))) { 619 // Sink division: (X / Y) * Z --> (X * Z) / Y 620 Value *NewFMul = Builder.CreateFMulFMF(X, Z, &I); 621 return BinaryOperator::CreateFDivFMF(NewFMul, Y, &I); 622 } 623 624 // sqrt(X) * sqrt(Y) -> sqrt(X * Y) 625 // nnan disallows the possibility of returning a number if both operands are 626 // negative (in that case, we should return NaN). 627 if (I.hasNoNaNs() && match(Op0, m_OneUse(m_Sqrt(m_Value(X)))) && 628 match(Op1, m_OneUse(m_Sqrt(m_Value(Y))))) { 629 Value *XY = Builder.CreateFMulFMF(X, Y, &I); 630 Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I); 631 return replaceInstUsesWith(I, Sqrt); 632 } 633 634 // The following transforms are done irrespective of the number of uses 635 // for the expression "1.0/sqrt(X)". 636 // 1) 1.0/sqrt(X) * X -> X/sqrt(X) 637 // 2) X * 1.0/sqrt(X) -> X/sqrt(X) 638 // We always expect the backend to reduce X/sqrt(X) to sqrt(X), if it 639 // has the necessary (reassoc) fast-math-flags. 640 if (I.hasNoSignedZeros() && 641 match(Op0, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) && 642 match(Y, m_Sqrt(m_Value(X))) && Op1 == X) 643 return BinaryOperator::CreateFDivFMF(X, Y, &I); 644 if (I.hasNoSignedZeros() && 645 match(Op1, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) && 646 match(Y, m_Sqrt(m_Value(X))) && Op0 == X) 647 return BinaryOperator::CreateFDivFMF(X, Y, &I); 648 649 // Like the similar transform in instsimplify, this requires 'nsz' because 650 // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0. 651 if (I.hasNoNaNs() && I.hasNoSignedZeros() && Op0 == Op1 && 652 Op0->hasNUses(2)) { 653 // Peek through fdiv to find squaring of square root: 654 // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y 655 if (match(Op0, m_FDiv(m_Value(X), m_Sqrt(m_Value(Y))))) { 656 Value *XX = Builder.CreateFMulFMF(X, X, &I); 657 return BinaryOperator::CreateFDivFMF(XX, Y, &I); 658 } 659 // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X) 660 if (match(Op0, m_FDiv(m_Sqrt(m_Value(Y)), m_Value(X)))) { 661 Value *XX = Builder.CreateFMulFMF(X, X, &I); 662 return BinaryOperator::CreateFDivFMF(Y, XX, &I); 663 } 664 } 665 666 // pow(X, Y) * X --> pow(X, Y+1) 667 // X * pow(X, Y) --> pow(X, Y+1) 668 if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Value(X), 669 m_Value(Y))), 670 m_Deferred(X)))) { 671 Value *Y1 = 672 Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), 1.0), &I); 673 Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, Y1, &I); 674 return replaceInstUsesWith(I, Pow); 675 } 676 677 if (I.isOnlyUserOfAnyOperand()) { 678 // pow(X, Y) * pow(X, Z) -> pow(X, Y + Z) 679 if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) && 680 match(Op1, m_Intrinsic<Intrinsic::pow>(m_Specific(X), m_Value(Z)))) { 681 auto *YZ = Builder.CreateFAddFMF(Y, Z, &I); 682 auto *NewPow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, YZ, &I); 683 return replaceInstUsesWith(I, NewPow); 684 } 685 // pow(X, Y) * pow(Z, Y) -> pow(X * Z, Y) 686 if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) && 687 match(Op1, m_Intrinsic<Intrinsic::pow>(m_Value(Z), m_Specific(Y)))) { 688 auto *XZ = Builder.CreateFMulFMF(X, Z, &I); 689 auto *NewPow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, XZ, Y, &I); 690 return replaceInstUsesWith(I, NewPow); 691 } 692 693 // powi(x, y) * powi(x, z) -> powi(x, y + z) 694 if (match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) && 695 match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) && 696 Y->getType() == Z->getType()) { 697 auto *YZ = Builder.CreateAdd(Y, Z); 698 auto *NewPow = Builder.CreateIntrinsic( 699 Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I); 700 return replaceInstUsesWith(I, NewPow); 701 } 702 703 // exp(X) * exp(Y) -> exp(X + Y) 704 if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) && 705 match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y)))) { 706 Value *XY = Builder.CreateFAddFMF(X, Y, &I); 707 Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I); 708 return replaceInstUsesWith(I, Exp); 709 } 710 711 // exp2(X) * exp2(Y) -> exp2(X + Y) 712 if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) && 713 match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y)))) { 714 Value *XY = Builder.CreateFAddFMF(X, Y, &I); 715 Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I); 716 return replaceInstUsesWith(I, Exp2); 717 } 718 } 719 720 // (X*Y) * X => (X*X) * Y where Y != X 721 // The purpose is two-fold: 722 // 1) to form a power expression (of X). 723 // 2) potentially shorten the critical path: After transformation, the 724 // latency of the instruction Y is amortized by the expression of X*X, 725 // and therefore Y is in a "less critical" position compared to what it 726 // was before the transformation. 727 if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) && 728 Op1 != Y) { 729 Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I); 730 return BinaryOperator::CreateFMulFMF(XX, Y, &I); 731 } 732 if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) && 733 Op0 != Y) { 734 Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I); 735 return BinaryOperator::CreateFMulFMF(XX, Y, &I); 736 } 737 } 738 739 // log2(X * 0.5) * Y = log2(X) * Y - Y 740 if (I.isFast()) { 741 IntrinsicInst *Log2 = nullptr; 742 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>( 743 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { 744 Log2 = cast<IntrinsicInst>(Op0); 745 Y = Op1; 746 } 747 if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>( 748 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { 749 Log2 = cast<IntrinsicInst>(Op1); 750 Y = Op0; 751 } 752 if (Log2) { 753 Value *Log2 = Builder.CreateUnaryIntrinsic(Intrinsic::log2, X, &I); 754 Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I); 755 return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I); 756 } 757 } 758 759 // Simplify FMUL recurrences starting with 0.0 to 0.0 if nnan and nsz are set. 760 // Given a phi node with entry value as 0 and it used in fmul operation, 761 // we can replace fmul with 0 safely and eleminate loop operation. 762 PHINode *PN = nullptr; 763 Value *Start = nullptr, *Step = nullptr; 764 if (matchSimpleRecurrence(&I, PN, Start, Step) && I.hasNoNaNs() && 765 I.hasNoSignedZeros() && match(Start, m_Zero())) 766 return replaceInstUsesWith(I, Start); 767 768 return nullptr; 769 } 770 771 /// Fold a divide or remainder with a select instruction divisor when one of the 772 /// select operands is zero. In that case, we can use the other select operand 773 /// because div/rem by zero is undefined. 774 bool InstCombinerImpl::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) { 775 SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1)); 776 if (!SI) 777 return false; 778 779 int NonNullOperand; 780 if (match(SI->getTrueValue(), m_Zero())) 781 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y 782 NonNullOperand = 2; 783 else if (match(SI->getFalseValue(), m_Zero())) 784 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y 785 NonNullOperand = 1; 786 else 787 return false; 788 789 // Change the div/rem to use 'Y' instead of the select. 790 replaceOperand(I, 1, SI->getOperand(NonNullOperand)); 791 792 // Okay, we know we replace the operand of the div/rem with 'Y' with no 793 // problem. However, the select, or the condition of the select may have 794 // multiple uses. Based on our knowledge that the operand must be non-zero, 795 // propagate the known value for the select into other uses of it, and 796 // propagate a known value of the condition into its other users. 797 798 // If the select and condition only have a single use, don't bother with this, 799 // early exit. 800 Value *SelectCond = SI->getCondition(); 801 if (SI->use_empty() && SelectCond->hasOneUse()) 802 return true; 803 804 // Scan the current block backward, looking for other uses of SI. 805 BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin(); 806 Type *CondTy = SelectCond->getType(); 807 while (BBI != BBFront) { 808 --BBI; 809 // If we found an instruction that we can't assume will return, so 810 // information from below it cannot be propagated above it. 811 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI)) 812 break; 813 814 // Replace uses of the select or its condition with the known values. 815 for (Use &Op : BBI->operands()) { 816 if (Op == SI) { 817 replaceUse(Op, SI->getOperand(NonNullOperand)); 818 Worklist.push(&*BBI); 819 } else if (Op == SelectCond) { 820 replaceUse(Op, NonNullOperand == 1 ? ConstantInt::getTrue(CondTy) 821 : ConstantInt::getFalse(CondTy)); 822 Worklist.push(&*BBI); 823 } 824 } 825 826 // If we past the instruction, quit looking for it. 827 if (&*BBI == SI) 828 SI = nullptr; 829 if (&*BBI == SelectCond) 830 SelectCond = nullptr; 831 832 // If we ran out of things to eliminate, break out of the loop. 833 if (!SelectCond && !SI) 834 break; 835 836 } 837 return true; 838 } 839 840 /// True if the multiply can not be expressed in an int this size. 841 static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, 842 bool IsSigned) { 843 bool Overflow; 844 Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow); 845 return Overflow; 846 } 847 848 /// True if C1 is a multiple of C2. Quotient contains C1/C2. 849 static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, 850 bool IsSigned) { 851 assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal"); 852 853 // Bail if we will divide by zero. 854 if (C2.isZero()) 855 return false; 856 857 // Bail if we would divide INT_MIN by -1. 858 if (IsSigned && C1.isMinSignedValue() && C2.isAllOnes()) 859 return false; 860 861 APInt Remainder(C1.getBitWidth(), /*val=*/0ULL, IsSigned); 862 if (IsSigned) 863 APInt::sdivrem(C1, C2, Quotient, Remainder); 864 else 865 APInt::udivrem(C1, C2, Quotient, Remainder); 866 867 return Remainder.isMinValue(); 868 } 869 870 static Instruction *foldIDivShl(BinaryOperator &I, 871 InstCombiner::BuilderTy &Builder) { 872 assert((I.getOpcode() == Instruction::SDiv || 873 I.getOpcode() == Instruction::UDiv) && 874 "Expected integer divide"); 875 876 bool IsSigned = I.getOpcode() == Instruction::SDiv; 877 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 878 Type *Ty = I.getType(); 879 880 Instruction *Ret = nullptr; 881 Value *X, *Y, *Z; 882 883 // With appropriate no-wrap constraints, remove a common factor in the 884 // dividend and divisor that is disguised as a left-shifted value. 885 if (match(Op1, m_Shl(m_Value(X), m_Value(Z))) && 886 match(Op0, m_c_Mul(m_Specific(X), m_Value(Y)))) { 887 // Both operands must have the matching no-wrap for this kind of division. 888 auto *Mul = cast<OverflowingBinaryOperator>(Op0); 889 auto *Shl = cast<OverflowingBinaryOperator>(Op1); 890 bool HasNUW = Mul->hasNoUnsignedWrap() && Shl->hasNoUnsignedWrap(); 891 bool HasNSW = Mul->hasNoSignedWrap() && Shl->hasNoSignedWrap(); 892 893 // (X * Y) u/ (X << Z) --> Y u>> Z 894 if (!IsSigned && HasNUW) 895 Ret = BinaryOperator::CreateLShr(Y, Z); 896 897 // (X * Y) s/ (X << Z) --> Y s/ (1 << Z) 898 if (IsSigned && HasNSW && (Op0->hasOneUse() || Op1->hasOneUse())) { 899 Value *Shl = Builder.CreateShl(ConstantInt::get(Ty, 1), Z); 900 Ret = BinaryOperator::CreateSDiv(Y, Shl); 901 } 902 } 903 904 // With appropriate no-wrap constraints, remove a common factor in the 905 // dividend and divisor that is disguised as a left-shift amount. 906 if (match(Op0, m_Shl(m_Value(X), m_Value(Z))) && 907 match(Op1, m_Shl(m_Value(Y), m_Specific(Z)))) { 908 auto *Shl0 = cast<OverflowingBinaryOperator>(Op0); 909 auto *Shl1 = cast<OverflowingBinaryOperator>(Op1); 910 911 // For unsigned div, we need 'nuw' on both shifts or 912 // 'nsw' on both shifts + 'nuw' on the dividend. 913 // (X << Z) / (Y << Z) --> X / Y 914 if (!IsSigned && 915 ((Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap()) || 916 (Shl0->hasNoUnsignedWrap() && Shl0->hasNoSignedWrap() && 917 Shl1->hasNoSignedWrap()))) 918 Ret = BinaryOperator::CreateUDiv(X, Y); 919 920 // For signed div, we need 'nsw' on both shifts + 'nuw' on the divisor. 921 // (X << Z) / (Y << Z) --> X / Y 922 if (IsSigned && Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap() && 923 Shl1->hasNoUnsignedWrap()) 924 Ret = BinaryOperator::CreateSDiv(X, Y); 925 } 926 927 if (!Ret) 928 return nullptr; 929 930 Ret->setIsExact(I.isExact()); 931 return Ret; 932 } 933 934 /// This function implements the transforms common to both integer division 935 /// instructions (udiv and sdiv). It is called by the visitors to those integer 936 /// division instructions. 937 /// Common integer divide transforms 938 Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) { 939 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 940 return Phi; 941 942 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 943 bool IsSigned = I.getOpcode() == Instruction::SDiv; 944 Type *Ty = I.getType(); 945 946 // The RHS is known non-zero. 947 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) 948 return replaceOperand(I, 1, V); 949 950 // Handle cases involving: [su]div X, (select Cond, Y, Z) 951 // This does not apply for fdiv. 952 if (simplifyDivRemOfSelectWithZeroOp(I)) 953 return &I; 954 955 // If the divisor is a select-of-constants, try to constant fold all div ops: 956 // C / (select Cond, TrueC, FalseC) --> select Cond, (C / TrueC), (C / FalseC) 957 // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds. 958 if (match(Op0, m_ImmConstant()) && 959 match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) { 960 if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1), 961 /*FoldWithMultiUse*/ true)) 962 return R; 963 } 964 965 const APInt *C2; 966 if (match(Op1, m_APInt(C2))) { 967 Value *X; 968 const APInt *C1; 969 970 // (X / C1) / C2 -> X / (C1*C2) 971 if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) || 972 (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) { 973 APInt Product(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 974 if (!multiplyOverflows(*C1, *C2, Product, IsSigned)) 975 return BinaryOperator::Create(I.getOpcode(), X, 976 ConstantInt::get(Ty, Product)); 977 } 978 979 if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) || 980 (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) { 981 APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 982 983 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. 984 if (isMultiple(*C2, *C1, Quotient, IsSigned)) { 985 auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X, 986 ConstantInt::get(Ty, Quotient)); 987 NewDiv->setIsExact(I.isExact()); 988 return NewDiv; 989 } 990 991 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. 992 if (isMultiple(*C1, *C2, Quotient, IsSigned)) { 993 auto *Mul = BinaryOperator::Create(Instruction::Mul, X, 994 ConstantInt::get(Ty, Quotient)); 995 auto *OBO = cast<OverflowingBinaryOperator>(Op0); 996 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); 997 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); 998 return Mul; 999 } 1000 } 1001 1002 if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) && 1003 C1->ult(C1->getBitWidth() - 1)) || 1004 (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))) && 1005 C1->ult(C1->getBitWidth()))) { 1006 APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 1007 APInt C1Shifted = APInt::getOneBitSet( 1008 C1->getBitWidth(), static_cast<unsigned>(C1->getZExtValue())); 1009 1010 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1. 1011 if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) { 1012 auto *BO = BinaryOperator::Create(I.getOpcode(), X, 1013 ConstantInt::get(Ty, Quotient)); 1014 BO->setIsExact(I.isExact()); 1015 return BO; 1016 } 1017 1018 // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2. 1019 if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) { 1020 auto *Mul = BinaryOperator::Create(Instruction::Mul, X, 1021 ConstantInt::get(Ty, Quotient)); 1022 auto *OBO = cast<OverflowingBinaryOperator>(Op0); 1023 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); 1024 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); 1025 return Mul; 1026 } 1027 } 1028 1029 if (!C2->isZero()) // avoid X udiv 0 1030 if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I)) 1031 return FoldedDiv; 1032 } 1033 1034 if (match(Op0, m_One())) { 1035 assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?"); 1036 if (IsSigned) { 1037 // 1 / 0 --> undef ; 1 / 1 --> 1 ; 1 / -1 --> -1 ; 1 / anything else --> 0 1038 // (Op1 + 1) u< 3 ? Op1 : 0 1039 // Op1 must be frozen because we are increasing its number of uses. 1040 Value *F1 = Builder.CreateFreeze(Op1, Op1->getName() + ".fr"); 1041 Value *Inc = Builder.CreateAdd(F1, Op0); 1042 Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3)); 1043 return SelectInst::Create(Cmp, F1, ConstantInt::get(Ty, 0)); 1044 } else { 1045 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the 1046 // result is one, otherwise it's zero. 1047 return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty); 1048 } 1049 } 1050 1051 // See if we can fold away this div instruction. 1052 if (SimplifyDemandedInstructionBits(I)) 1053 return &I; 1054 1055 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y 1056 Value *X, *Z; 1057 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1 1058 if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || 1059 (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) 1060 return BinaryOperator::Create(I.getOpcode(), X, Op1); 1061 1062 // (X << Y) / X -> 1 << Y 1063 Value *Y; 1064 if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y)))) 1065 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y); 1066 if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y)))) 1067 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y); 1068 1069 // X / (X * Y) -> 1 / Y if the multiplication does not overflow. 1070 if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) { 1071 bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap(); 1072 bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap(); 1073 if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) { 1074 replaceOperand(I, 0, ConstantInt::get(Ty, 1)); 1075 replaceOperand(I, 1, Y); 1076 return &I; 1077 } 1078 } 1079 1080 // (X << Z) / (X * Y) -> (1 << Z) / Y 1081 // TODO: Handle sdiv. 1082 if (!IsSigned && Op1->hasOneUse() && 1083 match(Op0, m_NUWShl(m_Value(X), m_Value(Z))) && 1084 match(Op1, m_c_Mul(m_Specific(X), m_Value(Y)))) 1085 if (cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap()) { 1086 Instruction *NewDiv = BinaryOperator::CreateUDiv( 1087 Builder.CreateShl(ConstantInt::get(Ty, 1), Z, "", /*NUW*/ true), Y); 1088 NewDiv->setIsExact(I.isExact()); 1089 return NewDiv; 1090 } 1091 1092 if (Instruction *R = foldIDivShl(I, Builder)) 1093 return R; 1094 1095 // With the appropriate no-wrap constraint, remove a multiply by the divisor 1096 // after peeking through another divide: 1097 // ((Op1 * X) / Y) / Op1 --> X / Y 1098 if (match(Op0, m_BinOp(I.getOpcode(), m_c_Mul(m_Specific(Op1), m_Value(X)), 1099 m_Value(Y)))) { 1100 auto *InnerDiv = cast<PossiblyExactOperator>(Op0); 1101 auto *Mul = cast<OverflowingBinaryOperator>(InnerDiv->getOperand(0)); 1102 Instruction *NewDiv = nullptr; 1103 if (!IsSigned && Mul->hasNoUnsignedWrap()) 1104 NewDiv = BinaryOperator::CreateUDiv(X, Y); 1105 else if (IsSigned && Mul->hasNoSignedWrap()) 1106 NewDiv = BinaryOperator::CreateSDiv(X, Y); 1107 1108 // Exact propagates only if both of the original divides are exact. 1109 if (NewDiv) { 1110 NewDiv->setIsExact(I.isExact() && InnerDiv->isExact()); 1111 return NewDiv; 1112 } 1113 } 1114 1115 return nullptr; 1116 } 1117 1118 static const unsigned MaxDepth = 6; 1119 1120 // Take the exact integer log2 of the value. If DoFold is true, create the 1121 // actual instructions, otherwise return a non-null dummy value. Return nullptr 1122 // on failure. 1123 static Value *takeLog2(IRBuilderBase &Builder, Value *Op, unsigned Depth, 1124 bool DoFold) { 1125 auto IfFold = [DoFold](function_ref<Value *()> Fn) { 1126 if (!DoFold) 1127 return reinterpret_cast<Value *>(-1); 1128 return Fn(); 1129 }; 1130 1131 // FIXME: assert that Op1 isn't/doesn't contain undef. 1132 1133 // log2(2^C) -> C 1134 if (match(Op, m_Power2())) 1135 return IfFold([&]() { 1136 Constant *C = ConstantExpr::getExactLogBase2(cast<Constant>(Op)); 1137 if (!C) 1138 llvm_unreachable("Failed to constant fold udiv -> logbase2"); 1139 return C; 1140 }); 1141 1142 // The remaining tests are all recursive, so bail out if we hit the limit. 1143 if (Depth++ == MaxDepth) 1144 return nullptr; 1145 1146 // log2(zext X) -> zext log2(X) 1147 // FIXME: Require one use? 1148 Value *X, *Y; 1149 if (match(Op, m_ZExt(m_Value(X)))) 1150 if (Value *LogX = takeLog2(Builder, X, Depth, DoFold)) 1151 return IfFold([&]() { return Builder.CreateZExt(LogX, Op->getType()); }); 1152 1153 // log2(X << Y) -> log2(X) + Y 1154 // FIXME: Require one use unless X is 1? 1155 if (match(Op, m_Shl(m_Value(X), m_Value(Y)))) 1156 if (Value *LogX = takeLog2(Builder, X, Depth, DoFold)) 1157 return IfFold([&]() { return Builder.CreateAdd(LogX, Y); }); 1158 1159 // log2(Cond ? X : Y) -> Cond ? log2(X) : log2(Y) 1160 // FIXME: missed optimization: if one of the hands of select is/contains 1161 // undef, just directly pick the other one. 1162 // FIXME: can both hands contain undef? 1163 // FIXME: Require one use? 1164 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) 1165 if (Value *LogX = takeLog2(Builder, SI->getOperand(1), Depth, DoFold)) 1166 if (Value *LogY = takeLog2(Builder, SI->getOperand(2), Depth, DoFold)) 1167 return IfFold([&]() { 1168 return Builder.CreateSelect(SI->getOperand(0), LogX, LogY); 1169 }); 1170 1171 // log2(umin(X, Y)) -> umin(log2(X), log2(Y)) 1172 // log2(umax(X, Y)) -> umax(log2(X), log2(Y)) 1173 auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op); 1174 if (MinMax && MinMax->hasOneUse() && !MinMax->isSigned()) 1175 if (Value *LogX = takeLog2(Builder, MinMax->getLHS(), Depth, DoFold)) 1176 if (Value *LogY = takeLog2(Builder, MinMax->getRHS(), Depth, DoFold)) 1177 return IfFold([&]() { 1178 return Builder.CreateBinaryIntrinsic( 1179 MinMax->getIntrinsicID(), LogX, LogY); 1180 }); 1181 1182 return nullptr; 1183 } 1184 1185 /// If we have zero-extended operands of an unsigned div or rem, we may be able 1186 /// to narrow the operation (sink the zext below the math). 1187 static Instruction *narrowUDivURem(BinaryOperator &I, 1188 InstCombiner::BuilderTy &Builder) { 1189 Instruction::BinaryOps Opcode = I.getOpcode(); 1190 Value *N = I.getOperand(0); 1191 Value *D = I.getOperand(1); 1192 Type *Ty = I.getType(); 1193 Value *X, *Y; 1194 if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) && 1195 X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) { 1196 // udiv (zext X), (zext Y) --> zext (udiv X, Y) 1197 // urem (zext X), (zext Y) --> zext (urem X, Y) 1198 Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y); 1199 return new ZExtInst(NarrowOp, Ty); 1200 } 1201 1202 Constant *C; 1203 if (isa<Instruction>(N) && match(N, m_OneUse(m_ZExt(m_Value(X)))) && 1204 match(D, m_Constant(C))) { 1205 // If the constant is the same in the smaller type, use the narrow version. 1206 Constant *TruncC = ConstantExpr::getTrunc(C, X->getType()); 1207 if (ConstantExpr::getZExt(TruncC, Ty) != C) 1208 return nullptr; 1209 1210 // udiv (zext X), C --> zext (udiv X, C') 1211 // urem (zext X), C --> zext (urem X, C') 1212 return new ZExtInst(Builder.CreateBinOp(Opcode, X, TruncC), Ty); 1213 } 1214 if (isa<Instruction>(D) && match(D, m_OneUse(m_ZExt(m_Value(X)))) && 1215 match(N, m_Constant(C))) { 1216 // If the constant is the same in the smaller type, use the narrow version. 1217 Constant *TruncC = ConstantExpr::getTrunc(C, X->getType()); 1218 if (ConstantExpr::getZExt(TruncC, Ty) != C) 1219 return nullptr; 1220 1221 // udiv C, (zext X) --> zext (udiv C', X) 1222 // urem C, (zext X) --> zext (urem C', X) 1223 return new ZExtInst(Builder.CreateBinOp(Opcode, TruncC, X), Ty); 1224 } 1225 1226 return nullptr; 1227 } 1228 1229 Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) { 1230 if (Value *V = simplifyUDivInst(I.getOperand(0), I.getOperand(1), I.isExact(), 1231 SQ.getWithInstruction(&I))) 1232 return replaceInstUsesWith(I, V); 1233 1234 if (Instruction *X = foldVectorBinop(I)) 1235 return X; 1236 1237 // Handle the integer div common cases 1238 if (Instruction *Common = commonIDivTransforms(I)) 1239 return Common; 1240 1241 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1242 Value *X; 1243 const APInt *C1, *C2; 1244 if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) { 1245 // (X lshr C1) udiv C2 --> X udiv (C2 << C1) 1246 bool Overflow; 1247 APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); 1248 if (!Overflow) { 1249 bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); 1250 BinaryOperator *BO = BinaryOperator::CreateUDiv( 1251 X, ConstantInt::get(X->getType(), C2ShlC1)); 1252 if (IsExact) 1253 BO->setIsExact(); 1254 return BO; 1255 } 1256 } 1257 1258 // Op0 / C where C is large (negative) --> zext (Op0 >= C) 1259 // TODO: Could use isKnownNegative() to handle non-constant values. 1260 Type *Ty = I.getType(); 1261 if (match(Op1, m_Negative())) { 1262 Value *Cmp = Builder.CreateICmpUGE(Op0, Op1); 1263 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1264 } 1265 // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined) 1266 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { 1267 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); 1268 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1269 } 1270 1271 if (Instruction *NarrowDiv = narrowUDivURem(I, Builder)) 1272 return NarrowDiv; 1273 1274 // If the udiv operands are non-overflowing multiplies with a common operand, 1275 // then eliminate the common factor: 1276 // (A * B) / (A * X) --> B / X (and commuted variants) 1277 // TODO: The code would be reduced if we had m_c_NUWMul pattern matching. 1278 // TODO: If -reassociation handled this generally, we could remove this. 1279 Value *A, *B; 1280 if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) { 1281 if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) || 1282 match(Op1, m_NUWMul(m_Value(X), m_Specific(A)))) 1283 return BinaryOperator::CreateUDiv(B, X); 1284 if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) || 1285 match(Op1, m_NUWMul(m_Value(X), m_Specific(B)))) 1286 return BinaryOperator::CreateUDiv(A, X); 1287 } 1288 1289 // Look through a right-shift to find the common factor: 1290 // ((Op1 *nuw A) >> B) / Op1 --> A >> B 1291 if (match(Op0, m_LShr(m_NUWMul(m_Specific(Op1), m_Value(A)), m_Value(B))) || 1292 match(Op0, m_LShr(m_NUWMul(m_Value(A), m_Specific(Op1)), m_Value(B)))) { 1293 Instruction *Lshr = BinaryOperator::CreateLShr(A, B); 1294 if (I.isExact() && cast<PossiblyExactOperator>(Op0)->isExact()) 1295 Lshr->setIsExact(); 1296 return Lshr; 1297 } 1298 1299 // Op1 udiv Op2 -> Op1 lshr log2(Op2), if log2() folds away. 1300 if (takeLog2(Builder, Op1, /*Depth*/0, /*DoFold*/false)) { 1301 Value *Res = takeLog2(Builder, Op1, /*Depth*/0, /*DoFold*/true); 1302 return replaceInstUsesWith( 1303 I, Builder.CreateLShr(Op0, Res, I.getName(), I.isExact())); 1304 } 1305 1306 return nullptr; 1307 } 1308 1309 Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) { 1310 if (Value *V = simplifySDivInst(I.getOperand(0), I.getOperand(1), I.isExact(), 1311 SQ.getWithInstruction(&I))) 1312 return replaceInstUsesWith(I, V); 1313 1314 if (Instruction *X = foldVectorBinop(I)) 1315 return X; 1316 1317 // Handle the integer div common cases 1318 if (Instruction *Common = commonIDivTransforms(I)) 1319 return Common; 1320 1321 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1322 Type *Ty = I.getType(); 1323 Value *X; 1324 // sdiv Op0, -1 --> -Op0 1325 // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined) 1326 if (match(Op1, m_AllOnes()) || 1327 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))) 1328 return BinaryOperator::CreateNeg(Op0); 1329 1330 // X / INT_MIN --> X == INT_MIN 1331 if (match(Op1, m_SignMask())) 1332 return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), Ty); 1333 1334 if (I.isExact()) { 1335 // sdiv exact X, 1<<C --> ashr exact X, C iff 1<<C is non-negative 1336 if (match(Op1, m_Power2()) && match(Op1, m_NonNegative())) { 1337 Constant *C = ConstantExpr::getExactLogBase2(cast<Constant>(Op1)); 1338 return BinaryOperator::CreateExactAShr(Op0, C); 1339 } 1340 1341 // sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative) 1342 Value *ShAmt; 1343 if (match(Op1, m_NSWShl(m_One(), m_Value(ShAmt)))) 1344 return BinaryOperator::CreateExactAShr(Op0, ShAmt); 1345 1346 // sdiv exact X, -1<<C --> -(ashr exact X, C) 1347 if (match(Op1, m_NegatedPower2())) { 1348 Constant *NegPow2C = ConstantExpr::getNeg(cast<Constant>(Op1)); 1349 Constant *C = ConstantExpr::getExactLogBase2(NegPow2C); 1350 Value *Ashr = Builder.CreateAShr(Op0, C, I.getName() + ".neg", true); 1351 return BinaryOperator::CreateNeg(Ashr); 1352 } 1353 } 1354 1355 const APInt *Op1C; 1356 if (match(Op1, m_APInt(Op1C))) { 1357 // If the dividend is sign-extended and the constant divisor is small enough 1358 // to fit in the source type, shrink the division to the narrower type: 1359 // (sext X) sdiv C --> sext (X sdiv C) 1360 Value *Op0Src; 1361 if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && 1362 Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { 1363 1364 // In the general case, we need to make sure that the dividend is not the 1365 // minimum signed value because dividing that by -1 is UB. But here, we 1366 // know that the -1 divisor case is already handled above. 1367 1368 Constant *NarrowDivisor = 1369 ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); 1370 Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor); 1371 return new SExtInst(NarrowOp, Ty); 1372 } 1373 1374 // -X / C --> X / -C (if the negation doesn't overflow). 1375 // TODO: This could be enhanced to handle arbitrary vector constants by 1376 // checking if all elements are not the min-signed-val. 1377 if (!Op1C->isMinSignedValue() && 1378 match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) { 1379 Constant *NegC = ConstantInt::get(Ty, -(*Op1C)); 1380 Instruction *BO = BinaryOperator::CreateSDiv(X, NegC); 1381 BO->setIsExact(I.isExact()); 1382 return BO; 1383 } 1384 } 1385 1386 // -X / Y --> -(X / Y) 1387 Value *Y; 1388 if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y)))) 1389 return BinaryOperator::CreateNSWNeg( 1390 Builder.CreateSDiv(X, Y, I.getName(), I.isExact())); 1391 1392 // abs(X) / X --> X > -1 ? 1 : -1 1393 // X / abs(X) --> X > -1 ? 1 : -1 1394 if (match(&I, m_c_BinOp( 1395 m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X), m_One())), 1396 m_Deferred(X)))) { 1397 Value *Cond = Builder.CreateIsNotNeg(X); 1398 return SelectInst::Create(Cond, ConstantInt::get(Ty, 1), 1399 ConstantInt::getAllOnesValue(Ty)); 1400 } 1401 1402 KnownBits KnownDividend = computeKnownBits(Op0, 0, &I); 1403 if (!I.isExact() && 1404 (match(Op1, m_Power2(Op1C)) || match(Op1, m_NegatedPower2(Op1C))) && 1405 KnownDividend.countMinTrailingZeros() >= Op1C->countTrailingZeros()) { 1406 I.setIsExact(); 1407 return &I; 1408 } 1409 1410 if (KnownDividend.isNonNegative()) { 1411 // If both operands are unsigned, turn this into a udiv. 1412 if (isKnownNonNegative(Op1, DL, 0, &AC, &I, &DT)) { 1413 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1414 BO->setIsExact(I.isExact()); 1415 return BO; 1416 } 1417 1418 if (match(Op1, m_NegatedPower2())) { 1419 // X sdiv (-(1 << C)) -> -(X sdiv (1 << C)) -> 1420 // -> -(X udiv (1 << C)) -> -(X u>> C) 1421 Constant *CNegLog2 = ConstantExpr::getExactLogBase2( 1422 ConstantExpr::getNeg(cast<Constant>(Op1))); 1423 Value *Shr = Builder.CreateLShr(Op0, CNegLog2, I.getName(), I.isExact()); 1424 return BinaryOperator::CreateNeg(Shr); 1425 } 1426 1427 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { 1428 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) 1429 // Safe because the only negative value (1 << Y) can take on is 1430 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have 1431 // the sign bit set. 1432 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1433 BO->setIsExact(I.isExact()); 1434 return BO; 1435 } 1436 } 1437 1438 return nullptr; 1439 } 1440 1441 /// Remove negation and try to convert division into multiplication. 1442 Instruction *InstCombinerImpl::foldFDivConstantDivisor(BinaryOperator &I) { 1443 Constant *C; 1444 if (!match(I.getOperand(1), m_Constant(C))) 1445 return nullptr; 1446 1447 // -X / C --> X / -C 1448 Value *X; 1449 const DataLayout &DL = I.getModule()->getDataLayout(); 1450 if (match(I.getOperand(0), m_FNeg(m_Value(X)))) 1451 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) 1452 return BinaryOperator::CreateFDivFMF(X, NegC, &I); 1453 1454 // nnan X / +0.0 -> copysign(inf, X) 1455 if (I.hasNoNaNs() && match(I.getOperand(1), m_Zero())) { 1456 IRBuilder<> B(&I); 1457 // TODO: nnan nsz X / -0.0 -> copysign(inf, X) 1458 CallInst *CopySign = B.CreateIntrinsic( 1459 Intrinsic::copysign, {C->getType()}, 1460 {ConstantFP::getInfinity(I.getType()), I.getOperand(0)}, &I); 1461 CopySign->takeName(&I); 1462 return replaceInstUsesWith(I, CopySign); 1463 } 1464 1465 // If the constant divisor has an exact inverse, this is always safe. If not, 1466 // then we can still create a reciprocal if fast-math-flags allow it and the 1467 // constant is a regular number (not zero, infinite, or denormal). 1468 if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP()))) 1469 return nullptr; 1470 1471 // Disallow denormal constants because we don't know what would happen 1472 // on all targets. 1473 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that 1474 // denorms are flushed? 1475 auto *RecipC = ConstantFoldBinaryOpOperands( 1476 Instruction::FDiv, ConstantFP::get(I.getType(), 1.0), C, DL); 1477 if (!RecipC || !RecipC->isNormalFP()) 1478 return nullptr; 1479 1480 // X / C --> X * (1 / C) 1481 return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I); 1482 } 1483 1484 /// Remove negation and try to reassociate constant math. 1485 static Instruction *foldFDivConstantDividend(BinaryOperator &I) { 1486 Constant *C; 1487 if (!match(I.getOperand(0), m_Constant(C))) 1488 return nullptr; 1489 1490 // C / -X --> -C / X 1491 Value *X; 1492 const DataLayout &DL = I.getModule()->getDataLayout(); 1493 if (match(I.getOperand(1), m_FNeg(m_Value(X)))) 1494 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) 1495 return BinaryOperator::CreateFDivFMF(NegC, X, &I); 1496 1497 if (!I.hasAllowReassoc() || !I.hasAllowReciprocal()) 1498 return nullptr; 1499 1500 // Try to reassociate C / X expressions where X includes another constant. 1501 Constant *C2, *NewC = nullptr; 1502 if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) { 1503 // C / (X * C2) --> (C / C2) / X 1504 NewC = ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C2, DL); 1505 } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) { 1506 // C / (X / C2) --> (C * C2) / X 1507 NewC = ConstantFoldBinaryOpOperands(Instruction::FMul, C, C2, DL); 1508 } 1509 // Disallow denormal constants because we don't know what would happen 1510 // on all targets. 1511 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that 1512 // denorms are flushed? 1513 if (!NewC || !NewC->isNormalFP()) 1514 return nullptr; 1515 1516 return BinaryOperator::CreateFDivFMF(NewC, X, &I); 1517 } 1518 1519 /// Negate the exponent of pow/exp to fold division-by-pow() into multiply. 1520 static Instruction *foldFDivPowDivisor(BinaryOperator &I, 1521 InstCombiner::BuilderTy &Builder) { 1522 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1523 auto *II = dyn_cast<IntrinsicInst>(Op1); 1524 if (!II || !II->hasOneUse() || !I.hasAllowReassoc() || 1525 !I.hasAllowReciprocal()) 1526 return nullptr; 1527 1528 // Z / pow(X, Y) --> Z * pow(X, -Y) 1529 // Z / exp{2}(Y) --> Z * exp{2}(-Y) 1530 // In the general case, this creates an extra instruction, but fmul allows 1531 // for better canonicalization and optimization than fdiv. 1532 Intrinsic::ID IID = II->getIntrinsicID(); 1533 SmallVector<Value *> Args; 1534 switch (IID) { 1535 case Intrinsic::pow: 1536 Args.push_back(II->getArgOperand(0)); 1537 Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(1), &I)); 1538 break; 1539 case Intrinsic::powi: { 1540 // Require 'ninf' assuming that makes powi(X, -INT_MIN) acceptable. 1541 // That is, X ** (huge negative number) is 0.0, ~1.0, or INF and so 1542 // dividing by that is INF, ~1.0, or 0.0. Code that uses powi allows 1543 // non-standard results, so this corner case should be acceptable if the 1544 // code rules out INF values. 1545 if (!I.hasNoInfs()) 1546 return nullptr; 1547 Args.push_back(II->getArgOperand(0)); 1548 Args.push_back(Builder.CreateNeg(II->getArgOperand(1))); 1549 Type *Tys[] = {I.getType(), II->getArgOperand(1)->getType()}; 1550 Value *Pow = Builder.CreateIntrinsic(IID, Tys, Args, &I); 1551 return BinaryOperator::CreateFMulFMF(Op0, Pow, &I); 1552 } 1553 case Intrinsic::exp: 1554 case Intrinsic::exp2: 1555 Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(0), &I)); 1556 break; 1557 default: 1558 return nullptr; 1559 } 1560 Value *Pow = Builder.CreateIntrinsic(IID, I.getType(), Args, &I); 1561 return BinaryOperator::CreateFMulFMF(Op0, Pow, &I); 1562 } 1563 1564 Instruction *InstCombinerImpl::visitFDiv(BinaryOperator &I) { 1565 Module *M = I.getModule(); 1566 1567 if (Value *V = simplifyFDivInst(I.getOperand(0), I.getOperand(1), 1568 I.getFastMathFlags(), 1569 SQ.getWithInstruction(&I))) 1570 return replaceInstUsesWith(I, V); 1571 1572 if (Instruction *X = foldVectorBinop(I)) 1573 return X; 1574 1575 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 1576 return Phi; 1577 1578 if (Instruction *R = foldFDivConstantDivisor(I)) 1579 return R; 1580 1581 if (Instruction *R = foldFDivConstantDividend(I)) 1582 return R; 1583 1584 if (Instruction *R = foldFPSignBitOps(I)) 1585 return R; 1586 1587 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1588 if (isa<Constant>(Op0)) 1589 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 1590 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1591 return R; 1592 1593 if (isa<Constant>(Op1)) 1594 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 1595 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1596 return R; 1597 1598 if (I.hasAllowReassoc() && I.hasAllowReciprocal()) { 1599 Value *X, *Y; 1600 if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && 1601 (!isa<Constant>(Y) || !isa<Constant>(Op1))) { 1602 // (X / Y) / Z => X / (Y * Z) 1603 Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I); 1604 return BinaryOperator::CreateFDivFMF(X, YZ, &I); 1605 } 1606 if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && 1607 (!isa<Constant>(Y) || !isa<Constant>(Op0))) { 1608 // Z / (X / Y) => (Y * Z) / X 1609 Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I); 1610 return BinaryOperator::CreateFDivFMF(YZ, X, &I); 1611 } 1612 // Z / (1.0 / Y) => (Y * Z) 1613 // 1614 // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The 1615 // m_OneUse check is avoided because even in the case of the multiple uses 1616 // for 1.0/Y, the number of instructions remain the same and a division is 1617 // replaced by a multiplication. 1618 if (match(Op1, m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) 1619 return BinaryOperator::CreateFMulFMF(Y, Op0, &I); 1620 } 1621 1622 if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) { 1623 // sin(X) / cos(X) -> tan(X) 1624 // cos(X) / sin(X) -> 1/tan(X) (cotangent) 1625 Value *X; 1626 bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) && 1627 match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X))); 1628 bool IsCot = 1629 !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) && 1630 match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X))); 1631 1632 if ((IsTan || IsCot) && hasFloatFn(M, &TLI, I.getType(), LibFunc_tan, 1633 LibFunc_tanf, LibFunc_tanl)) { 1634 IRBuilder<> B(&I); 1635 IRBuilder<>::FastMathFlagGuard FMFGuard(B); 1636 B.setFastMathFlags(I.getFastMathFlags()); 1637 AttributeList Attrs = 1638 cast<CallBase>(Op0)->getCalledFunction()->getAttributes(); 1639 Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf, 1640 LibFunc_tanl, B, Attrs); 1641 if (IsCot) 1642 Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res); 1643 return replaceInstUsesWith(I, Res); 1644 } 1645 } 1646 1647 // X / (X * Y) --> 1.0 / Y 1648 // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed. 1649 // We can ignore the possibility that X is infinity because INF/INF is NaN. 1650 Value *X, *Y; 1651 if (I.hasNoNaNs() && I.hasAllowReassoc() && 1652 match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) { 1653 replaceOperand(I, 0, ConstantFP::get(I.getType(), 1.0)); 1654 replaceOperand(I, 1, Y); 1655 return &I; 1656 } 1657 1658 // X / fabs(X) -> copysign(1.0, X) 1659 // fabs(X) / X -> copysign(1.0, X) 1660 if (I.hasNoNaNs() && I.hasNoInfs() && 1661 (match(&I, m_FDiv(m_Value(X), m_FAbs(m_Deferred(X)))) || 1662 match(&I, m_FDiv(m_FAbs(m_Value(X)), m_Deferred(X))))) { 1663 Value *V = Builder.CreateBinaryIntrinsic( 1664 Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), X, &I); 1665 return replaceInstUsesWith(I, V); 1666 } 1667 1668 if (Instruction *Mul = foldFDivPowDivisor(I, Builder)) 1669 return Mul; 1670 1671 // pow(X, Y) / X --> pow(X, Y-1) 1672 if (I.hasAllowReassoc() && 1673 match(Op0, m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Specific(Op1), 1674 m_Value(Y))))) { 1675 Value *Y1 = 1676 Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), -1.0), &I); 1677 Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, Op1, Y1, &I); 1678 return replaceInstUsesWith(I, Pow); 1679 } 1680 1681 return nullptr; 1682 } 1683 1684 /// This function implements the transforms common to both integer remainder 1685 /// instructions (urem and srem). It is called by the visitors to those integer 1686 /// remainder instructions. 1687 /// Common integer remainder transforms 1688 Instruction *InstCombinerImpl::commonIRemTransforms(BinaryOperator &I) { 1689 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 1690 return Phi; 1691 1692 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1693 1694 // The RHS is known non-zero. 1695 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) 1696 return replaceOperand(I, 1, V); 1697 1698 // Handle cases involving: rem X, (select Cond, Y, Z) 1699 if (simplifyDivRemOfSelectWithZeroOp(I)) 1700 return &I; 1701 1702 // If the divisor is a select-of-constants, try to constant fold all rem ops: 1703 // C % (select Cond, TrueC, FalseC) --> select Cond, (C % TrueC), (C % FalseC) 1704 // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds. 1705 if (match(Op0, m_ImmConstant()) && 1706 match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) { 1707 if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1), 1708 /*FoldWithMultiUse*/ true)) 1709 return R; 1710 } 1711 1712 if (isa<Constant>(Op1)) { 1713 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { 1714 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { 1715 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1716 return R; 1717 } else if (auto *PN = dyn_cast<PHINode>(Op0I)) { 1718 const APInt *Op1Int; 1719 if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() && 1720 (I.getOpcode() == Instruction::URem || 1721 !Op1Int->isMinSignedValue())) { 1722 // foldOpIntoPhi will speculate instructions to the end of the PHI's 1723 // predecessor blocks, so do this only if we know the srem or urem 1724 // will not fault. 1725 if (Instruction *NV = foldOpIntoPhi(I, PN)) 1726 return NV; 1727 } 1728 } 1729 1730 // See if we can fold away this rem instruction. 1731 if (SimplifyDemandedInstructionBits(I)) 1732 return &I; 1733 } 1734 } 1735 1736 return nullptr; 1737 } 1738 1739 Instruction *InstCombinerImpl::visitURem(BinaryOperator &I) { 1740 if (Value *V = simplifyURemInst(I.getOperand(0), I.getOperand(1), 1741 SQ.getWithInstruction(&I))) 1742 return replaceInstUsesWith(I, V); 1743 1744 if (Instruction *X = foldVectorBinop(I)) 1745 return X; 1746 1747 if (Instruction *common = commonIRemTransforms(I)) 1748 return common; 1749 1750 if (Instruction *NarrowRem = narrowUDivURem(I, Builder)) 1751 return NarrowRem; 1752 1753 // X urem Y -> X and Y-1, where Y is a power of 2, 1754 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1755 Type *Ty = I.getType(); 1756 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { 1757 // This may increase instruction count, we don't enforce that Y is a 1758 // constant. 1759 Constant *N1 = Constant::getAllOnesValue(Ty); 1760 Value *Add = Builder.CreateAdd(Op1, N1); 1761 return BinaryOperator::CreateAnd(Op0, Add); 1762 } 1763 1764 // 1 urem X -> zext(X != 1) 1765 if (match(Op0, m_One())) { 1766 Value *Cmp = Builder.CreateICmpNE(Op1, ConstantInt::get(Ty, 1)); 1767 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1768 } 1769 1770 // Op0 urem C -> Op0 < C ? Op0 : Op0 - C, where C >= signbit. 1771 // Op0 must be frozen because we are increasing its number of uses. 1772 if (match(Op1, m_Negative())) { 1773 Value *F0 = Builder.CreateFreeze(Op0, Op0->getName() + ".fr"); 1774 Value *Cmp = Builder.CreateICmpULT(F0, Op1); 1775 Value *Sub = Builder.CreateSub(F0, Op1); 1776 return SelectInst::Create(Cmp, F0, Sub); 1777 } 1778 1779 // If the divisor is a sext of a boolean, then the divisor must be max 1780 // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also 1781 // max unsigned value. In that case, the remainder is 0: 1782 // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0 1783 Value *X; 1784 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { 1785 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); 1786 return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0); 1787 } 1788 1789 return nullptr; 1790 } 1791 1792 Instruction *InstCombinerImpl::visitSRem(BinaryOperator &I) { 1793 if (Value *V = simplifySRemInst(I.getOperand(0), I.getOperand(1), 1794 SQ.getWithInstruction(&I))) 1795 return replaceInstUsesWith(I, V); 1796 1797 if (Instruction *X = foldVectorBinop(I)) 1798 return X; 1799 1800 // Handle the integer rem common cases 1801 if (Instruction *Common = commonIRemTransforms(I)) 1802 return Common; 1803 1804 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1805 { 1806 const APInt *Y; 1807 // X % -Y -> X % Y 1808 if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) 1809 return replaceOperand(I, 1, ConstantInt::get(I.getType(), -*Y)); 1810 } 1811 1812 // -X srem Y --> -(X srem Y) 1813 Value *X, *Y; 1814 if (match(&I, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y)))) 1815 return BinaryOperator::CreateNSWNeg(Builder.CreateSRem(X, Y)); 1816 1817 // If the sign bits of both operands are zero (i.e. we can prove they are 1818 // unsigned inputs), turn this into a urem. 1819 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); 1820 if (MaskedValueIsZero(Op1, Mask, 0, &I) && 1821 MaskedValueIsZero(Op0, Mask, 0, &I)) { 1822 // X srem Y -> X urem Y, iff X and Y don't have sign bit set 1823 return BinaryOperator::CreateURem(Op0, Op1, I.getName()); 1824 } 1825 1826 // If it's a constant vector, flip any negative values positive. 1827 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { 1828 Constant *C = cast<Constant>(Op1); 1829 unsigned VWidth = cast<FixedVectorType>(C->getType())->getNumElements(); 1830 1831 bool hasNegative = false; 1832 bool hasMissing = false; 1833 for (unsigned i = 0; i != VWidth; ++i) { 1834 Constant *Elt = C->getAggregateElement(i); 1835 if (!Elt) { 1836 hasMissing = true; 1837 break; 1838 } 1839 1840 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) 1841 if (RHS->isNegative()) 1842 hasNegative = true; 1843 } 1844 1845 if (hasNegative && !hasMissing) { 1846 SmallVector<Constant *, 16> Elts(VWidth); 1847 for (unsigned i = 0; i != VWidth; ++i) { 1848 Elts[i] = C->getAggregateElement(i); // Handle undef, etc. 1849 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { 1850 if (RHS->isNegative()) 1851 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); 1852 } 1853 } 1854 1855 Constant *NewRHSV = ConstantVector::get(Elts); 1856 if (NewRHSV != C) // Don't loop on -MININT 1857 return replaceOperand(I, 1, NewRHSV); 1858 } 1859 } 1860 1861 return nullptr; 1862 } 1863 1864 Instruction *InstCombinerImpl::visitFRem(BinaryOperator &I) { 1865 if (Value *V = simplifyFRemInst(I.getOperand(0), I.getOperand(1), 1866 I.getFastMathFlags(), 1867 SQ.getWithInstruction(&I))) 1868 return replaceInstUsesWith(I, V); 1869 1870 if (Instruction *X = foldVectorBinop(I)) 1871 return X; 1872 1873 if (Instruction *Phi = foldBinopWithPhiOperands(I)) 1874 return Phi; 1875 1876 return nullptr; 1877 } 1878