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/APFloat.h" 16 #include "llvm/ADT/APInt.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Analysis/InstructionSimplify.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/Support/KnownBits.h" 34 #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" 35 #include "llvm/Transforms/Utils/BuildLibCalls.h" 36 #include <cassert> 37 #include <cstddef> 38 #include <cstdint> 39 #include <utility> 40 41 using namespace llvm; 42 using namespace PatternMatch; 43 44 #define DEBUG_TYPE "instcombine" 45 46 /// The specific integer value is used in a context where it is known to be 47 /// non-zero. If this allows us to simplify the computation, do so and return 48 /// the new operand, otherwise return null. 49 static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC, 50 Instruction &CxtI) { 51 // If V has multiple uses, then we would have to do more analysis to determine 52 // if this is safe. For example, the use could be in dynamically unreached 53 // code. 54 if (!V->hasOneUse()) return nullptr; 55 56 bool MadeChange = false; 57 58 // ((1 << A) >>u B) --> (1 << (A-B)) 59 // Because V cannot be zero, we know that B is less than A. 60 Value *A = nullptr, *B = nullptr, *One = nullptr; 61 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) && 62 match(One, m_One())) { 63 A = IC.Builder.CreateSub(A, B); 64 return IC.Builder.CreateShl(One, A); 65 } 66 67 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it 68 // inexact. Similarly for <<. 69 BinaryOperator *I = dyn_cast<BinaryOperator>(V); 70 if (I && I->isLogicalShift() && 71 IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) { 72 // We know that this is an exact/nuw shift and that the input is a 73 // non-zero context as well. 74 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) { 75 IC.replaceOperand(*I, 0, V2); 76 MadeChange = true; 77 } 78 79 if (I->getOpcode() == Instruction::LShr && !I->isExact()) { 80 I->setIsExact(); 81 MadeChange = true; 82 } 83 84 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { 85 I->setHasNoUnsignedWrap(); 86 MadeChange = true; 87 } 88 } 89 90 // TODO: Lots more we could do here: 91 // If V is a phi node, we can call this on each of its operands. 92 // "select cond, X, 0" can simplify to "X". 93 94 return MadeChange ? V : nullptr; 95 } 96 97 /// A helper routine of InstCombiner::visitMul(). 98 /// 99 /// If C is a scalar/fixed width vector of known powers of 2, then this 100 /// function returns a new scalar/fixed width vector obtained from logBase2 101 /// of C. 102 /// Return a null pointer otherwise. 103 static Constant *getLogBase2(Type *Ty, Constant *C) { 104 const APInt *IVal; 105 if (match(C, m_APInt(IVal)) && IVal->isPowerOf2()) 106 return ConstantInt::get(Ty, IVal->logBase2()); 107 108 // FIXME: We can extract pow of 2 of splat constant for scalable vectors. 109 if (!isa<FixedVectorType>(Ty)) 110 return nullptr; 111 112 SmallVector<Constant *, 4> Elts; 113 for (unsigned I = 0, E = cast<FixedVectorType>(Ty)->getNumElements(); I != E; 114 ++I) { 115 Constant *Elt = C->getAggregateElement(I); 116 if (!Elt) 117 return nullptr; 118 if (isa<UndefValue>(Elt)) { 119 Elts.push_back(UndefValue::get(Ty->getScalarType())); 120 continue; 121 } 122 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2()) 123 return nullptr; 124 Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2())); 125 } 126 127 return ConstantVector::get(Elts); 128 } 129 130 // TODO: This is a specific form of a much more general pattern. 131 // We could detect a select with any binop identity constant, or we 132 // could use SimplifyBinOp to see if either arm of the select reduces. 133 // But that needs to be done carefully and/or while removing potential 134 // reverse canonicalizations as in InstCombiner::foldSelectIntoOp(). 135 static Value *foldMulSelectToNegate(BinaryOperator &I, 136 InstCombiner::BuilderTy &Builder) { 137 Value *Cond, *OtherOp; 138 139 // mul (select Cond, 1, -1), OtherOp --> select Cond, OtherOp, -OtherOp 140 // mul OtherOp, (select Cond, 1, -1) --> select Cond, OtherOp, -OtherOp 141 if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())), 142 m_Value(OtherOp)))) 143 return Builder.CreateSelect(Cond, OtherOp, Builder.CreateNeg(OtherOp)); 144 145 // mul (select Cond, -1, 1), OtherOp --> select Cond, -OtherOp, OtherOp 146 // mul OtherOp, (select Cond, -1, 1) --> select Cond, -OtherOp, OtherOp 147 if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())), 148 m_Value(OtherOp)))) 149 return Builder.CreateSelect(Cond, Builder.CreateNeg(OtherOp), OtherOp); 150 151 // fmul (select Cond, 1.0, -1.0), OtherOp --> select Cond, OtherOp, -OtherOp 152 // fmul OtherOp, (select Cond, 1.0, -1.0) --> select Cond, OtherOp, -OtherOp 153 if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(1.0), 154 m_SpecificFP(-1.0))), 155 m_Value(OtherOp)))) { 156 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 157 Builder.setFastMathFlags(I.getFastMathFlags()); 158 return Builder.CreateSelect(Cond, OtherOp, Builder.CreateFNeg(OtherOp)); 159 } 160 161 // fmul (select Cond, -1.0, 1.0), OtherOp --> select Cond, -OtherOp, OtherOp 162 // fmul OtherOp, (select Cond, -1.0, 1.0) --> select Cond, -OtherOp, OtherOp 163 if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(-1.0), 164 m_SpecificFP(1.0))), 165 m_Value(OtherOp)))) { 166 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 167 Builder.setFastMathFlags(I.getFastMathFlags()); 168 return Builder.CreateSelect(Cond, Builder.CreateFNeg(OtherOp), OtherOp); 169 } 170 171 return nullptr; 172 } 173 174 Instruction *InstCombiner::visitMul(BinaryOperator &I) { 175 if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1), 176 SQ.getWithInstruction(&I))) 177 return replaceInstUsesWith(I, V); 178 179 if (SimplifyAssociativeOrCommutative(I)) 180 return &I; 181 182 if (Instruction *X = foldVectorBinop(I)) 183 return X; 184 185 if (Value *V = SimplifyUsingDistributiveLaws(I)) 186 return replaceInstUsesWith(I, V); 187 188 // X * -1 == 0 - X 189 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 190 if (match(Op1, m_AllOnes())) { 191 BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName()); 192 if (I.hasNoSignedWrap()) 193 BO->setHasNoSignedWrap(); 194 return BO; 195 } 196 197 // Also allow combining multiply instructions on vectors. 198 { 199 Value *NewOp; 200 Constant *C1, *C2; 201 const APInt *IVal; 202 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)), 203 m_Constant(C1))) && 204 match(C1, m_APInt(IVal))) { 205 // ((X << C2)*C1) == (X * (C1 << C2)) 206 Constant *Shl = ConstantExpr::getShl(C1, C2); 207 BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0)); 208 BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl); 209 if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap()) 210 BO->setHasNoUnsignedWrap(); 211 if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() && 212 Shl->isNotMinSignedValue()) 213 BO->setHasNoSignedWrap(); 214 return BO; 215 } 216 217 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { 218 // Replace X*(2^C) with X << C, where C is either a scalar or a vector. 219 if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) { 220 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); 221 222 if (I.hasNoUnsignedWrap()) 223 Shl->setHasNoUnsignedWrap(); 224 if (I.hasNoSignedWrap()) { 225 const APInt *V; 226 if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1) 227 Shl->setHasNoSignedWrap(); 228 } 229 230 return Shl; 231 } 232 } 233 } 234 235 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { 236 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n 237 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n 238 // The "* (2**n)" thus becomes a potential shifting opportunity. 239 { 240 const APInt & Val = CI->getValue(); 241 const APInt &PosVal = Val.abs(); 242 if (Val.isNegative() && PosVal.isPowerOf2()) { 243 Value *X = nullptr, *Y = nullptr; 244 if (Op0->hasOneUse()) { 245 ConstantInt *C1; 246 Value *Sub = nullptr; 247 if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) 248 Sub = Builder.CreateSub(X, Y, "suba"); 249 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) 250 Sub = Builder.CreateSub(Builder.CreateNeg(C1), Y, "subc"); 251 if (Sub) 252 return 253 BinaryOperator::CreateMul(Sub, 254 ConstantInt::get(Y->getType(), PosVal)); 255 } 256 } 257 } 258 } 259 260 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) 261 return FoldedMul; 262 263 if (Value *FoldedMul = foldMulSelectToNegate(I, Builder)) 264 return replaceInstUsesWith(I, FoldedMul); 265 266 // Simplify mul instructions with a constant RHS. 267 if (isa<Constant>(Op1)) { 268 // Canonicalize (X+C1)*CI -> X*CI+C1*CI. 269 Value *X; 270 Constant *C1; 271 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { 272 Value *Mul = Builder.CreateMul(C1, Op1); 273 // Only go forward with the transform if C1*CI simplifies to a tidier 274 // constant. 275 if (!match(Mul, m_Mul(m_Value(), m_Value()))) 276 return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul); 277 } 278 } 279 280 // abs(X) * abs(X) -> X * X 281 // nabs(X) * nabs(X) -> X * X 282 if (Op0 == Op1) { 283 Value *X, *Y; 284 SelectPatternFlavor SPF = matchSelectPattern(Op0, X, Y).Flavor; 285 if (SPF == SPF_ABS || SPF == SPF_NABS) 286 return BinaryOperator::CreateMul(X, X); 287 } 288 289 // -X * C --> X * -C 290 Value *X, *Y; 291 Constant *Op1C; 292 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C))) 293 return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C)); 294 295 // -X * -Y --> X * Y 296 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) { 297 auto *NewMul = BinaryOperator::CreateMul(X, Y); 298 if (I.hasNoSignedWrap() && 299 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() && 300 cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap()) 301 NewMul->setHasNoSignedWrap(); 302 return NewMul; 303 } 304 305 // -X * Y --> -(X * Y) 306 // X * -Y --> -(X * Y) 307 if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y)))) 308 return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y)); 309 310 // (X / Y) * Y = X - (X % Y) 311 // (X / Y) * -Y = (X % Y) - X 312 { 313 Value *Y = Op1; 314 BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0); 315 if (!Div || (Div->getOpcode() != Instruction::UDiv && 316 Div->getOpcode() != Instruction::SDiv)) { 317 Y = Op0; 318 Div = dyn_cast<BinaryOperator>(Op1); 319 } 320 Value *Neg = dyn_castNegVal(Y); 321 if (Div && Div->hasOneUse() && 322 (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) && 323 (Div->getOpcode() == Instruction::UDiv || 324 Div->getOpcode() == Instruction::SDiv)) { 325 Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1); 326 327 // If the division is exact, X % Y is zero, so we end up with X or -X. 328 if (Div->isExact()) { 329 if (DivOp1 == Y) 330 return replaceInstUsesWith(I, X); 331 return BinaryOperator::CreateNeg(X); 332 } 333 334 auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem 335 : Instruction::SRem; 336 Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1); 337 if (DivOp1 == Y) 338 return BinaryOperator::CreateSub(X, Rem); 339 return BinaryOperator::CreateSub(Rem, X); 340 } 341 } 342 343 /// i1 mul -> i1 and. 344 if (I.getType()->isIntOrIntVectorTy(1)) 345 return BinaryOperator::CreateAnd(Op0, Op1); 346 347 // X*(1 << Y) --> X << Y 348 // (1 << Y)*X --> X << Y 349 { 350 Value *Y; 351 BinaryOperator *BO = nullptr; 352 bool ShlNSW = false; 353 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) { 354 BO = BinaryOperator::CreateShl(Op1, Y); 355 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap(); 356 } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) { 357 BO = BinaryOperator::CreateShl(Op0, Y); 358 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap(); 359 } 360 if (BO) { 361 if (I.hasNoUnsignedWrap()) 362 BO->setHasNoUnsignedWrap(); 363 if (I.hasNoSignedWrap() && ShlNSW) 364 BO->setHasNoSignedWrap(); 365 return BO; 366 } 367 } 368 369 // (bool X) * Y --> X ? Y : 0 370 // Y * (bool X) --> X ? Y : 0 371 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) 372 return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0)); 373 if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) 374 return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0)); 375 376 // (lshr X, 31) * Y --> (ashr X, 31) & Y 377 // Y * (lshr X, 31) --> (ashr X, 31) & Y 378 // TODO: We are not checking one-use because the elimination of the multiply 379 // is better for analysis? 380 // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be 381 // more similar to what we're doing above. 382 const APInt *C; 383 if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) 384 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1); 385 if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) 386 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0); 387 388 if (Instruction *Ext = narrowMathIfNoOverflow(I)) 389 return Ext; 390 391 bool Changed = false; 392 if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) { 393 Changed = true; 394 I.setHasNoSignedWrap(true); 395 } 396 397 if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) { 398 Changed = true; 399 I.setHasNoUnsignedWrap(true); 400 } 401 402 return Changed ? &I : nullptr; 403 } 404 405 Instruction *InstCombiner::foldFPSignBitOps(BinaryOperator &I) { 406 BinaryOperator::BinaryOps Opcode = I.getOpcode(); 407 assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) && 408 "Expected fmul or fdiv"); 409 410 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 411 Value *X, *Y; 412 413 // -X * -Y --> X * Y 414 // -X / -Y --> X / Y 415 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) 416 return BinaryOperator::CreateWithCopiedFlags(Opcode, X, Y, &I); 417 418 // fabs(X) * fabs(X) -> X * X 419 // fabs(X) / fabs(X) -> X / X 420 if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X)))) 421 return BinaryOperator::CreateWithCopiedFlags(Opcode, X, X, &I); 422 423 // fabs(X) * fabs(Y) --> fabs(X * Y) 424 // fabs(X) / fabs(Y) --> fabs(X / Y) 425 if (match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X))) && 426 match(Op1, m_Intrinsic<Intrinsic::fabs>(m_Value(Y))) && 427 (Op0->hasOneUse() || Op1->hasOneUse())) { 428 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 429 Builder.setFastMathFlags(I.getFastMathFlags()); 430 Value *XY = Builder.CreateBinOp(Opcode, X, Y); 431 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, XY); 432 Fabs->takeName(&I); 433 return replaceInstUsesWith(I, Fabs); 434 } 435 436 return nullptr; 437 } 438 439 Instruction *InstCombiner::visitFMul(BinaryOperator &I) { 440 if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1), 441 I.getFastMathFlags(), 442 SQ.getWithInstruction(&I))) 443 return replaceInstUsesWith(I, V); 444 445 if (SimplifyAssociativeOrCommutative(I)) 446 return &I; 447 448 if (Instruction *X = foldVectorBinop(I)) 449 return X; 450 451 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) 452 return FoldedMul; 453 454 if (Value *FoldedMul = foldMulSelectToNegate(I, Builder)) 455 return replaceInstUsesWith(I, FoldedMul); 456 457 if (Instruction *R = foldFPSignBitOps(I)) 458 return R; 459 460 // X * -1.0 --> -X 461 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 462 if (match(Op1, m_SpecificFP(-1.0))) 463 return UnaryOperator::CreateFNegFMF(Op0, &I); 464 465 // -X * C --> X * -C 466 Value *X, *Y; 467 Constant *C; 468 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C))) 469 return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I); 470 471 // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E) 472 if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1)) 473 return replaceInstUsesWith(I, V); 474 475 if (I.hasAllowReassoc()) { 476 // Reassociate constant RHS with another constant to form constant 477 // expression. 478 if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) { 479 Constant *C1; 480 if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) { 481 // (C1 / X) * C --> (C * C1) / X 482 Constant *CC1 = ConstantExpr::getFMul(C, C1); 483 if (CC1->isNormalFP()) 484 return BinaryOperator::CreateFDivFMF(CC1, X, &I); 485 } 486 if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { 487 // (X / C1) * C --> X * (C / C1) 488 Constant *CDivC1 = ConstantExpr::getFDiv(C, C1); 489 if (CDivC1->isNormalFP()) 490 return BinaryOperator::CreateFMulFMF(X, CDivC1, &I); 491 492 // If the constant was a denormal, try reassociating differently. 493 // (X / C1) * C --> X / (C1 / C) 494 Constant *C1DivC = ConstantExpr::getFDiv(C1, C); 495 if (Op0->hasOneUse() && C1DivC->isNormalFP()) 496 return BinaryOperator::CreateFDivFMF(X, C1DivC, &I); 497 } 498 499 // We do not need to match 'fadd C, X' and 'fsub X, C' because they are 500 // canonicalized to 'fadd X, C'. Distributing the multiply may allow 501 // further folds and (X * C) + C2 is 'fma'. 502 if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) { 503 // (X + C1) * C --> (X * C) + (C * C1) 504 Constant *CC1 = ConstantExpr::getFMul(C, C1); 505 Value *XC = Builder.CreateFMulFMF(X, C, &I); 506 return BinaryOperator::CreateFAddFMF(XC, CC1, &I); 507 } 508 if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) { 509 // (C1 - X) * C --> (C * C1) - (X * C) 510 Constant *CC1 = ConstantExpr::getFMul(C, C1); 511 Value *XC = Builder.CreateFMulFMF(X, C, &I); 512 return BinaryOperator::CreateFSubFMF(CC1, XC, &I); 513 } 514 } 515 516 Value *Z; 517 if (match(&I, m_c_FMul(m_OneUse(m_FDiv(m_Value(X), m_Value(Y))), 518 m_Value(Z)))) { 519 // Sink division: (X / Y) * Z --> (X * Z) / Y 520 Value *NewFMul = Builder.CreateFMulFMF(X, Z, &I); 521 return BinaryOperator::CreateFDivFMF(NewFMul, Y, &I); 522 } 523 524 // sqrt(X) * sqrt(Y) -> sqrt(X * Y) 525 // nnan disallows the possibility of returning a number if both operands are 526 // negative (in that case, we should return NaN). 527 if (I.hasNoNaNs() && 528 match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) && 529 match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) { 530 Value *XY = Builder.CreateFMulFMF(X, Y, &I); 531 Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I); 532 return replaceInstUsesWith(I, Sqrt); 533 } 534 535 // Like the similar transform in instsimplify, this requires 'nsz' because 536 // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0. 537 if (I.hasNoNaNs() && I.hasNoSignedZeros() && Op0 == Op1 && 538 Op0->hasNUses(2)) { 539 // Peek through fdiv to find squaring of square root: 540 // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y 541 if (match(Op0, m_FDiv(m_Value(X), 542 m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) { 543 Value *XX = Builder.CreateFMulFMF(X, X, &I); 544 return BinaryOperator::CreateFDivFMF(XX, Y, &I); 545 } 546 // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X) 547 if (match(Op0, m_FDiv(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)), 548 m_Value(X)))) { 549 Value *XX = Builder.CreateFMulFMF(X, X, &I); 550 return BinaryOperator::CreateFDivFMF(Y, XX, &I); 551 } 552 } 553 554 // exp(X) * exp(Y) -> exp(X + Y) 555 // Match as long as at least one of exp has only one use. 556 if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) && 557 match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y))) && 558 (Op0->hasOneUse() || Op1->hasOneUse())) { 559 Value *XY = Builder.CreateFAddFMF(X, Y, &I); 560 Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I); 561 return replaceInstUsesWith(I, Exp); 562 } 563 564 // exp2(X) * exp2(Y) -> exp2(X + Y) 565 // Match as long as at least one of exp2 has only one use. 566 if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) && 567 match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y))) && 568 (Op0->hasOneUse() || Op1->hasOneUse())) { 569 Value *XY = Builder.CreateFAddFMF(X, Y, &I); 570 Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I); 571 return replaceInstUsesWith(I, Exp2); 572 } 573 574 // (X*Y) * X => (X*X) * Y where Y != X 575 // The purpose is two-fold: 576 // 1) to form a power expression (of X). 577 // 2) potentially shorten the critical path: After transformation, the 578 // latency of the instruction Y is amortized by the expression of X*X, 579 // and therefore Y is in a "less critical" position compared to what it 580 // was before the transformation. 581 if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) && 582 Op1 != Y) { 583 Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I); 584 return BinaryOperator::CreateFMulFMF(XX, Y, &I); 585 } 586 if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) && 587 Op0 != Y) { 588 Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I); 589 return BinaryOperator::CreateFMulFMF(XX, Y, &I); 590 } 591 } 592 593 // log2(X * 0.5) * Y = log2(X) * Y - Y 594 if (I.isFast()) { 595 IntrinsicInst *Log2 = nullptr; 596 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>( 597 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { 598 Log2 = cast<IntrinsicInst>(Op0); 599 Y = Op1; 600 } 601 if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>( 602 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { 603 Log2 = cast<IntrinsicInst>(Op1); 604 Y = Op0; 605 } 606 if (Log2) { 607 Value *Log2 = Builder.CreateUnaryIntrinsic(Intrinsic::log2, X, &I); 608 Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I); 609 return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I); 610 } 611 } 612 613 return nullptr; 614 } 615 616 /// Fold a divide or remainder with a select instruction divisor when one of the 617 /// select operands is zero. In that case, we can use the other select operand 618 /// because div/rem by zero is undefined. 619 bool InstCombiner::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) { 620 SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1)); 621 if (!SI) 622 return false; 623 624 int NonNullOperand; 625 if (match(SI->getTrueValue(), m_Zero())) 626 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y 627 NonNullOperand = 2; 628 else if (match(SI->getFalseValue(), m_Zero())) 629 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y 630 NonNullOperand = 1; 631 else 632 return false; 633 634 // Change the div/rem to use 'Y' instead of the select. 635 replaceOperand(I, 1, SI->getOperand(NonNullOperand)); 636 637 // Okay, we know we replace the operand of the div/rem with 'Y' with no 638 // problem. However, the select, or the condition of the select may have 639 // multiple uses. Based on our knowledge that the operand must be non-zero, 640 // propagate the known value for the select into other uses of it, and 641 // propagate a known value of the condition into its other users. 642 643 // If the select and condition only have a single use, don't bother with this, 644 // early exit. 645 Value *SelectCond = SI->getCondition(); 646 if (SI->use_empty() && SelectCond->hasOneUse()) 647 return true; 648 649 // Scan the current block backward, looking for other uses of SI. 650 BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin(); 651 Type *CondTy = SelectCond->getType(); 652 while (BBI != BBFront) { 653 --BBI; 654 // If we found an instruction that we can't assume will return, so 655 // information from below it cannot be propagated above it. 656 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI)) 657 break; 658 659 // Replace uses of the select or its condition with the known values. 660 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); 661 I != E; ++I) { 662 if (*I == SI) { 663 replaceUse(*I, SI->getOperand(NonNullOperand)); 664 Worklist.push(&*BBI); 665 } else if (*I == SelectCond) { 666 replaceUse(*I, NonNullOperand == 1 ? ConstantInt::getTrue(CondTy) 667 : ConstantInt::getFalse(CondTy)); 668 Worklist.push(&*BBI); 669 } 670 } 671 672 // If we past the instruction, quit looking for it. 673 if (&*BBI == SI) 674 SI = nullptr; 675 if (&*BBI == SelectCond) 676 SelectCond = nullptr; 677 678 // If we ran out of things to eliminate, break out of the loop. 679 if (!SelectCond && !SI) 680 break; 681 682 } 683 return true; 684 } 685 686 /// True if the multiply can not be expressed in an int this size. 687 static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, 688 bool IsSigned) { 689 bool Overflow; 690 Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow); 691 return Overflow; 692 } 693 694 /// True if C1 is a multiple of C2. Quotient contains C1/C2. 695 static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, 696 bool IsSigned) { 697 assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal"); 698 699 // Bail if we will divide by zero. 700 if (C2.isNullValue()) 701 return false; 702 703 // Bail if we would divide INT_MIN by -1. 704 if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue()) 705 return false; 706 707 APInt Remainder(C1.getBitWidth(), /*val=*/0ULL, IsSigned); 708 if (IsSigned) 709 APInt::sdivrem(C1, C2, Quotient, Remainder); 710 else 711 APInt::udivrem(C1, C2, Quotient, Remainder); 712 713 return Remainder.isMinValue(); 714 } 715 716 /// This function implements the transforms common to both integer division 717 /// instructions (udiv and sdiv). It is called by the visitors to those integer 718 /// division instructions. 719 /// Common integer divide transforms 720 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { 721 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 722 bool IsSigned = I.getOpcode() == Instruction::SDiv; 723 Type *Ty = I.getType(); 724 725 // The RHS is known non-zero. 726 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) 727 return replaceOperand(I, 1, V); 728 729 // Handle cases involving: [su]div X, (select Cond, Y, Z) 730 // This does not apply for fdiv. 731 if (simplifyDivRemOfSelectWithZeroOp(I)) 732 return &I; 733 734 const APInt *C2; 735 if (match(Op1, m_APInt(C2))) { 736 Value *X; 737 const APInt *C1; 738 739 // (X / C1) / C2 -> X / (C1*C2) 740 if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) || 741 (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) { 742 APInt Product(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 743 if (!multiplyOverflows(*C1, *C2, Product, IsSigned)) 744 return BinaryOperator::Create(I.getOpcode(), X, 745 ConstantInt::get(Ty, Product)); 746 } 747 748 if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) || 749 (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) { 750 APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 751 752 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. 753 if (isMultiple(*C2, *C1, Quotient, IsSigned)) { 754 auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X, 755 ConstantInt::get(Ty, Quotient)); 756 NewDiv->setIsExact(I.isExact()); 757 return NewDiv; 758 } 759 760 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. 761 if (isMultiple(*C1, *C2, Quotient, IsSigned)) { 762 auto *Mul = BinaryOperator::Create(Instruction::Mul, X, 763 ConstantInt::get(Ty, Quotient)); 764 auto *OBO = cast<OverflowingBinaryOperator>(Op0); 765 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); 766 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); 767 return Mul; 768 } 769 } 770 771 if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) && 772 *C1 != C1->getBitWidth() - 1) || 773 (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))))) { 774 APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned); 775 APInt C1Shifted = APInt::getOneBitSet( 776 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue())); 777 778 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1. 779 if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) { 780 auto *BO = BinaryOperator::Create(I.getOpcode(), X, 781 ConstantInt::get(Ty, Quotient)); 782 BO->setIsExact(I.isExact()); 783 return BO; 784 } 785 786 // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2. 787 if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) { 788 auto *Mul = BinaryOperator::Create(Instruction::Mul, X, 789 ConstantInt::get(Ty, Quotient)); 790 auto *OBO = cast<OverflowingBinaryOperator>(Op0); 791 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); 792 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); 793 return Mul; 794 } 795 } 796 797 if (!C2->isNullValue()) // avoid X udiv 0 798 if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I)) 799 return FoldedDiv; 800 } 801 802 if (match(Op0, m_One())) { 803 assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?"); 804 if (IsSigned) { 805 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the 806 // result is one, if Op1 is -1 then the result is minus one, otherwise 807 // it's zero. 808 Value *Inc = Builder.CreateAdd(Op1, Op0); 809 Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3)); 810 return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0)); 811 } else { 812 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the 813 // result is one, otherwise it's zero. 814 return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty); 815 } 816 } 817 818 // See if we can fold away this div instruction. 819 if (SimplifyDemandedInstructionBits(I)) 820 return &I; 821 822 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y 823 Value *X, *Z; 824 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1 825 if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || 826 (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) 827 return BinaryOperator::Create(I.getOpcode(), X, Op1); 828 829 // (X << Y) / X -> 1 << Y 830 Value *Y; 831 if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y)))) 832 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y); 833 if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y)))) 834 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y); 835 836 // X / (X * Y) -> 1 / Y if the multiplication does not overflow. 837 if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) { 838 bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap(); 839 bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap(); 840 if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) { 841 replaceOperand(I, 0, ConstantInt::get(Ty, 1)); 842 replaceOperand(I, 1, Y); 843 return &I; 844 } 845 } 846 847 return nullptr; 848 } 849 850 static const unsigned MaxDepth = 6; 851 852 namespace { 853 854 using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1, 855 const BinaryOperator &I, 856 InstCombiner &IC); 857 858 /// Used to maintain state for visitUDivOperand(). 859 struct UDivFoldAction { 860 /// Informs visitUDiv() how to fold this operand. This can be zero if this 861 /// action joins two actions together. 862 FoldUDivOperandCb FoldAction; 863 864 /// Which operand to fold. 865 Value *OperandToFold; 866 867 union { 868 /// The instruction returned when FoldAction is invoked. 869 Instruction *FoldResult; 870 871 /// Stores the LHS action index if this action joins two actions together. 872 size_t SelectLHSIdx; 873 }; 874 875 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand) 876 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {} 877 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS) 878 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {} 879 }; 880 881 } // end anonymous namespace 882 883 // X udiv 2^C -> X >> C 884 static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1, 885 const BinaryOperator &I, InstCombiner &IC) { 886 Constant *C1 = getLogBase2(Op0->getType(), cast<Constant>(Op1)); 887 if (!C1) 888 llvm_unreachable("Failed to constant fold udiv -> logbase2"); 889 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1); 890 if (I.isExact()) 891 LShr->setIsExact(); 892 return LShr; 893 } 894 895 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) 896 // X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2) 897 static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, 898 InstCombiner &IC) { 899 Value *ShiftLeft; 900 if (!match(Op1, m_ZExt(m_Value(ShiftLeft)))) 901 ShiftLeft = Op1; 902 903 Constant *CI; 904 Value *N; 905 if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N)))) 906 llvm_unreachable("match should never fail here!"); 907 Constant *Log2Base = getLogBase2(N->getType(), CI); 908 if (!Log2Base) 909 llvm_unreachable("getLogBase2 should never fail here!"); 910 N = IC.Builder.CreateAdd(N, Log2Base); 911 if (Op1 != ShiftLeft) 912 N = IC.Builder.CreateZExt(N, Op1->getType()); 913 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N); 914 if (I.isExact()) 915 LShr->setIsExact(); 916 return LShr; 917 } 918 919 // Recursively visits the possible right hand operands of a udiv 920 // instruction, seeing through select instructions, to determine if we can 921 // replace the udiv with something simpler. If we find that an operand is not 922 // able to simplify the udiv, we abort the entire transformation. 923 static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, 924 SmallVectorImpl<UDivFoldAction> &Actions, 925 unsigned Depth = 0) { 926 // Check to see if this is an unsigned division with an exact power of 2, 927 // if so, convert to a right shift. 928 if (match(Op1, m_Power2())) { 929 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1)); 930 return Actions.size(); 931 } 932 933 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) 934 if (match(Op1, m_Shl(m_Power2(), m_Value())) || 935 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) { 936 Actions.push_back(UDivFoldAction(foldUDivShl, Op1)); 937 return Actions.size(); 938 } 939 940 // The remaining tests are all recursive, so bail out if we hit the limit. 941 if (Depth++ == MaxDepth) 942 return 0; 943 944 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 945 if (size_t LHSIdx = 946 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth)) 947 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) { 948 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1)); 949 return Actions.size(); 950 } 951 952 return 0; 953 } 954 955 /// If we have zero-extended operands of an unsigned div or rem, we may be able 956 /// to narrow the operation (sink the zext below the math). 957 static Instruction *narrowUDivURem(BinaryOperator &I, 958 InstCombiner::BuilderTy &Builder) { 959 Instruction::BinaryOps Opcode = I.getOpcode(); 960 Value *N = I.getOperand(0); 961 Value *D = I.getOperand(1); 962 Type *Ty = I.getType(); 963 Value *X, *Y; 964 if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) && 965 X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) { 966 // udiv (zext X), (zext Y) --> zext (udiv X, Y) 967 // urem (zext X), (zext Y) --> zext (urem X, Y) 968 Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y); 969 return new ZExtInst(NarrowOp, Ty); 970 } 971 972 Constant *C; 973 if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) || 974 (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) { 975 // If the constant is the same in the smaller type, use the narrow version. 976 Constant *TruncC = ConstantExpr::getTrunc(C, X->getType()); 977 if (ConstantExpr::getZExt(TruncC, Ty) != C) 978 return nullptr; 979 980 // udiv (zext X), C --> zext (udiv X, C') 981 // urem (zext X), C --> zext (urem X, C') 982 // udiv C, (zext X) --> zext (udiv C', X) 983 // urem C, (zext X) --> zext (urem C', X) 984 Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC) 985 : Builder.CreateBinOp(Opcode, TruncC, X); 986 return new ZExtInst(NarrowOp, Ty); 987 } 988 989 return nullptr; 990 } 991 992 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { 993 if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1), 994 SQ.getWithInstruction(&I))) 995 return replaceInstUsesWith(I, V); 996 997 if (Instruction *X = foldVectorBinop(I)) 998 return X; 999 1000 // Handle the integer div common cases 1001 if (Instruction *Common = commonIDivTransforms(I)) 1002 return Common; 1003 1004 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1005 Value *X; 1006 const APInt *C1, *C2; 1007 if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) { 1008 // (X lshr C1) udiv C2 --> X udiv (C2 << C1) 1009 bool Overflow; 1010 APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); 1011 if (!Overflow) { 1012 bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); 1013 BinaryOperator *BO = BinaryOperator::CreateUDiv( 1014 X, ConstantInt::get(X->getType(), C2ShlC1)); 1015 if (IsExact) 1016 BO->setIsExact(); 1017 return BO; 1018 } 1019 } 1020 1021 // Op0 / C where C is large (negative) --> zext (Op0 >= C) 1022 // TODO: Could use isKnownNegative() to handle non-constant values. 1023 Type *Ty = I.getType(); 1024 if (match(Op1, m_Negative())) { 1025 Value *Cmp = Builder.CreateICmpUGE(Op0, Op1); 1026 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1027 } 1028 // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined) 1029 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { 1030 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); 1031 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1032 } 1033 1034 if (Instruction *NarrowDiv = narrowUDivURem(I, Builder)) 1035 return NarrowDiv; 1036 1037 // If the udiv operands are non-overflowing multiplies with a common operand, 1038 // then eliminate the common factor: 1039 // (A * B) / (A * X) --> B / X (and commuted variants) 1040 // TODO: The code would be reduced if we had m_c_NUWMul pattern matching. 1041 // TODO: If -reassociation handled this generally, we could remove this. 1042 Value *A, *B; 1043 if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) { 1044 if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) || 1045 match(Op1, m_NUWMul(m_Value(X), m_Specific(A)))) 1046 return BinaryOperator::CreateUDiv(B, X); 1047 if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) || 1048 match(Op1, m_NUWMul(m_Value(X), m_Specific(B)))) 1049 return BinaryOperator::CreateUDiv(A, X); 1050 } 1051 1052 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...)))) 1053 SmallVector<UDivFoldAction, 6> UDivActions; 1054 if (visitUDivOperand(Op0, Op1, I, UDivActions)) 1055 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) { 1056 FoldUDivOperandCb Action = UDivActions[i].FoldAction; 1057 Value *ActionOp1 = UDivActions[i].OperandToFold; 1058 Instruction *Inst; 1059 if (Action) 1060 Inst = Action(Op0, ActionOp1, I, *this); 1061 else { 1062 // This action joins two actions together. The RHS of this action is 1063 // simply the last action we processed, we saved the LHS action index in 1064 // the joining action. 1065 size_t SelectRHSIdx = i - 1; 1066 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult; 1067 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx; 1068 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult; 1069 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(), 1070 SelectLHS, SelectRHS); 1071 } 1072 1073 // If this is the last action to process, return it to the InstCombiner. 1074 // Otherwise, we insert it before the UDiv and record it so that we may 1075 // use it as part of a joining action (i.e., a SelectInst). 1076 if (e - i != 1) { 1077 Inst->insertBefore(&I); 1078 UDivActions[i].FoldResult = Inst; 1079 } else 1080 return Inst; 1081 } 1082 1083 return nullptr; 1084 } 1085 1086 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { 1087 if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1), 1088 SQ.getWithInstruction(&I))) 1089 return replaceInstUsesWith(I, V); 1090 1091 if (Instruction *X = foldVectorBinop(I)) 1092 return X; 1093 1094 // Handle the integer div common cases 1095 if (Instruction *Common = commonIDivTransforms(I)) 1096 return Common; 1097 1098 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1099 Value *X; 1100 // sdiv Op0, -1 --> -Op0 1101 // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined) 1102 if (match(Op1, m_AllOnes()) || 1103 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))) 1104 return BinaryOperator::CreateNeg(Op0); 1105 1106 // X / INT_MIN --> X == INT_MIN 1107 if (match(Op1, m_SignMask())) 1108 return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType()); 1109 1110 const APInt *Op1C; 1111 if (match(Op1, m_APInt(Op1C))) { 1112 // sdiv exact X, C --> ashr exact X, log2(C) 1113 if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) { 1114 Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); 1115 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); 1116 } 1117 1118 // If the dividend is sign-extended and the constant divisor is small enough 1119 // to fit in the source type, shrink the division to the narrower type: 1120 // (sext X) sdiv C --> sext (X sdiv C) 1121 Value *Op0Src; 1122 if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && 1123 Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { 1124 1125 // In the general case, we need to make sure that the dividend is not the 1126 // minimum signed value because dividing that by -1 is UB. But here, we 1127 // know that the -1 divisor case is already handled above. 1128 1129 Constant *NarrowDivisor = 1130 ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); 1131 Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor); 1132 return new SExtInst(NarrowOp, Op0->getType()); 1133 } 1134 1135 // -X / C --> X / -C (if the negation doesn't overflow). 1136 // TODO: This could be enhanced to handle arbitrary vector constants by 1137 // checking if all elements are not the min-signed-val. 1138 if (!Op1C->isMinSignedValue() && 1139 match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) { 1140 Constant *NegC = ConstantInt::get(I.getType(), -(*Op1C)); 1141 Instruction *BO = BinaryOperator::CreateSDiv(X, NegC); 1142 BO->setIsExact(I.isExact()); 1143 return BO; 1144 } 1145 } 1146 1147 // -X / Y --> -(X / Y) 1148 Value *Y; 1149 if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y)))) 1150 return BinaryOperator::CreateNSWNeg( 1151 Builder.CreateSDiv(X, Y, I.getName(), I.isExact())); 1152 1153 // If the sign bits of both operands are zero (i.e. we can prove they are 1154 // unsigned inputs), turn this into a udiv. 1155 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); 1156 if (MaskedValueIsZero(Op0, Mask, 0, &I)) { 1157 if (MaskedValueIsZero(Op1, Mask, 0, &I)) { 1158 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set 1159 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1160 BO->setIsExact(I.isExact()); 1161 return BO; 1162 } 1163 1164 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { 1165 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) 1166 // Safe because the only negative value (1 << Y) can take on is 1167 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have 1168 // the sign bit set. 1169 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 1170 BO->setIsExact(I.isExact()); 1171 return BO; 1172 } 1173 } 1174 1175 return nullptr; 1176 } 1177 1178 /// Remove negation and try to convert division into multiplication. 1179 static Instruction *foldFDivConstantDivisor(BinaryOperator &I) { 1180 Constant *C; 1181 if (!match(I.getOperand(1), m_Constant(C))) 1182 return nullptr; 1183 1184 // -X / C --> X / -C 1185 Value *X; 1186 if (match(I.getOperand(0), m_FNeg(m_Value(X)))) 1187 return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I); 1188 1189 // If the constant divisor has an exact inverse, this is always safe. If not, 1190 // then we can still create a reciprocal if fast-math-flags allow it and the 1191 // constant is a regular number (not zero, infinite, or denormal). 1192 if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP()))) 1193 return nullptr; 1194 1195 // Disallow denormal constants because we don't know what would happen 1196 // on all targets. 1197 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that 1198 // denorms are flushed? 1199 auto *RecipC = ConstantExpr::getFDiv(ConstantFP::get(I.getType(), 1.0), C); 1200 if (!RecipC->isNormalFP()) 1201 return nullptr; 1202 1203 // X / C --> X * (1 / C) 1204 return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I); 1205 } 1206 1207 /// Remove negation and try to reassociate constant math. 1208 static Instruction *foldFDivConstantDividend(BinaryOperator &I) { 1209 Constant *C; 1210 if (!match(I.getOperand(0), m_Constant(C))) 1211 return nullptr; 1212 1213 // C / -X --> -C / X 1214 Value *X; 1215 if (match(I.getOperand(1), m_FNeg(m_Value(X)))) 1216 return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I); 1217 1218 if (!I.hasAllowReassoc() || !I.hasAllowReciprocal()) 1219 return nullptr; 1220 1221 // Try to reassociate C / X expressions where X includes another constant. 1222 Constant *C2, *NewC = nullptr; 1223 if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) { 1224 // C / (X * C2) --> (C / C2) / X 1225 NewC = ConstantExpr::getFDiv(C, C2); 1226 } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) { 1227 // C / (X / C2) --> (C * C2) / X 1228 NewC = ConstantExpr::getFMul(C, C2); 1229 } 1230 // Disallow denormal constants because we don't know what would happen 1231 // on all targets. 1232 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that 1233 // denorms are flushed? 1234 if (!NewC || !NewC->isNormalFP()) 1235 return nullptr; 1236 1237 return BinaryOperator::CreateFDivFMF(NewC, X, &I); 1238 } 1239 1240 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { 1241 if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1), 1242 I.getFastMathFlags(), 1243 SQ.getWithInstruction(&I))) 1244 return replaceInstUsesWith(I, V); 1245 1246 if (Instruction *X = foldVectorBinop(I)) 1247 return X; 1248 1249 if (Instruction *R = foldFDivConstantDivisor(I)) 1250 return R; 1251 1252 if (Instruction *R = foldFDivConstantDividend(I)) 1253 return R; 1254 1255 if (Instruction *R = foldFPSignBitOps(I)) 1256 return R; 1257 1258 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1259 if (isa<Constant>(Op0)) 1260 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 1261 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1262 return R; 1263 1264 if (isa<Constant>(Op1)) 1265 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 1266 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1267 return R; 1268 1269 if (I.hasAllowReassoc() && I.hasAllowReciprocal()) { 1270 Value *X, *Y; 1271 if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && 1272 (!isa<Constant>(Y) || !isa<Constant>(Op1))) { 1273 // (X / Y) / Z => X / (Y * Z) 1274 Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I); 1275 return BinaryOperator::CreateFDivFMF(X, YZ, &I); 1276 } 1277 if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && 1278 (!isa<Constant>(Y) || !isa<Constant>(Op0))) { 1279 // Z / (X / Y) => (Y * Z) / X 1280 Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I); 1281 return BinaryOperator::CreateFDivFMF(YZ, X, &I); 1282 } 1283 // Z / (1.0 / Y) => (Y * Z) 1284 // 1285 // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The 1286 // m_OneUse check is avoided because even in the case of the multiple uses 1287 // for 1.0/Y, the number of instructions remain the same and a division is 1288 // replaced by a multiplication. 1289 if (match(Op1, m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) 1290 return BinaryOperator::CreateFMulFMF(Y, Op0, &I); 1291 } 1292 1293 if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) { 1294 // sin(X) / cos(X) -> tan(X) 1295 // cos(X) / sin(X) -> 1/tan(X) (cotangent) 1296 Value *X; 1297 bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) && 1298 match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X))); 1299 bool IsCot = 1300 !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) && 1301 match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X))); 1302 1303 if ((IsTan || IsCot) && 1304 hasFloatFn(&TLI, I.getType(), LibFunc_tan, LibFunc_tanf, LibFunc_tanl)) { 1305 IRBuilder<> B(&I); 1306 IRBuilder<>::FastMathFlagGuard FMFGuard(B); 1307 B.setFastMathFlags(I.getFastMathFlags()); 1308 AttributeList Attrs = 1309 cast<CallBase>(Op0)->getCalledFunction()->getAttributes(); 1310 Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf, 1311 LibFunc_tanl, B, Attrs); 1312 if (IsCot) 1313 Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res); 1314 return replaceInstUsesWith(I, Res); 1315 } 1316 } 1317 1318 // X / (X * Y) --> 1.0 / Y 1319 // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed. 1320 // We can ignore the possibility that X is infinity because INF/INF is NaN. 1321 Value *X, *Y; 1322 if (I.hasNoNaNs() && I.hasAllowReassoc() && 1323 match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) { 1324 replaceOperand(I, 0, ConstantFP::get(I.getType(), 1.0)); 1325 replaceOperand(I, 1, Y); 1326 return &I; 1327 } 1328 1329 // X / fabs(X) -> copysign(1.0, X) 1330 // fabs(X) / X -> copysign(1.0, X) 1331 if (I.hasNoNaNs() && I.hasNoInfs() && 1332 (match(&I, 1333 m_FDiv(m_Value(X), m_Intrinsic<Intrinsic::fabs>(m_Deferred(X)))) || 1334 match(&I, m_FDiv(m_Intrinsic<Intrinsic::fabs>(m_Value(X)), 1335 m_Deferred(X))))) { 1336 Value *V = Builder.CreateBinaryIntrinsic( 1337 Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), X, &I); 1338 return replaceInstUsesWith(I, V); 1339 } 1340 return nullptr; 1341 } 1342 1343 /// This function implements the transforms common to both integer remainder 1344 /// instructions (urem and srem). It is called by the visitors to those integer 1345 /// remainder instructions. 1346 /// Common integer remainder transforms 1347 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { 1348 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1349 1350 // The RHS is known non-zero. 1351 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) 1352 return replaceOperand(I, 1, V); 1353 1354 // Handle cases involving: rem X, (select Cond, Y, Z) 1355 if (simplifyDivRemOfSelectWithZeroOp(I)) 1356 return &I; 1357 1358 if (isa<Constant>(Op1)) { 1359 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { 1360 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { 1361 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1362 return R; 1363 } else if (auto *PN = dyn_cast<PHINode>(Op0I)) { 1364 const APInt *Op1Int; 1365 if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() && 1366 (I.getOpcode() == Instruction::URem || 1367 !Op1Int->isMinSignedValue())) { 1368 // foldOpIntoPhi will speculate instructions to the end of the PHI's 1369 // predecessor blocks, so do this only if we know the srem or urem 1370 // will not fault. 1371 if (Instruction *NV = foldOpIntoPhi(I, PN)) 1372 return NV; 1373 } 1374 } 1375 1376 // See if we can fold away this rem instruction. 1377 if (SimplifyDemandedInstructionBits(I)) 1378 return &I; 1379 } 1380 } 1381 1382 return nullptr; 1383 } 1384 1385 Instruction *InstCombiner::visitURem(BinaryOperator &I) { 1386 if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1), 1387 SQ.getWithInstruction(&I))) 1388 return replaceInstUsesWith(I, V); 1389 1390 if (Instruction *X = foldVectorBinop(I)) 1391 return X; 1392 1393 if (Instruction *common = commonIRemTransforms(I)) 1394 return common; 1395 1396 if (Instruction *NarrowRem = narrowUDivURem(I, Builder)) 1397 return NarrowRem; 1398 1399 // X urem Y -> X and Y-1, where Y is a power of 2, 1400 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1401 Type *Ty = I.getType(); 1402 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { 1403 // This may increase instruction count, we don't enforce that Y is a 1404 // constant. 1405 Constant *N1 = Constant::getAllOnesValue(Ty); 1406 Value *Add = Builder.CreateAdd(Op1, N1); 1407 return BinaryOperator::CreateAnd(Op0, Add); 1408 } 1409 1410 // 1 urem X -> zext(X != 1) 1411 if (match(Op0, m_One())) { 1412 Value *Cmp = Builder.CreateICmpNE(Op1, ConstantInt::get(Ty, 1)); 1413 return CastInst::CreateZExtOrBitCast(Cmp, Ty); 1414 } 1415 1416 // X urem C -> X < C ? X : X - C, where C >= signbit. 1417 if (match(Op1, m_Negative())) { 1418 Value *Cmp = Builder.CreateICmpULT(Op0, Op1); 1419 Value *Sub = Builder.CreateSub(Op0, Op1); 1420 return SelectInst::Create(Cmp, Op0, Sub); 1421 } 1422 1423 // If the divisor is a sext of a boolean, then the divisor must be max 1424 // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also 1425 // max unsigned value. In that case, the remainder is 0: 1426 // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0 1427 Value *X; 1428 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { 1429 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); 1430 return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0); 1431 } 1432 1433 return nullptr; 1434 } 1435 1436 Instruction *InstCombiner::visitSRem(BinaryOperator &I) { 1437 if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1), 1438 SQ.getWithInstruction(&I))) 1439 return replaceInstUsesWith(I, V); 1440 1441 if (Instruction *X = foldVectorBinop(I)) 1442 return X; 1443 1444 // Handle the integer rem common cases 1445 if (Instruction *Common = commonIRemTransforms(I)) 1446 return Common; 1447 1448 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1449 { 1450 const APInt *Y; 1451 // X % -Y -> X % Y 1452 if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) 1453 return replaceOperand(I, 1, ConstantInt::get(I.getType(), -*Y)); 1454 } 1455 1456 // -X srem Y --> -(X srem Y) 1457 Value *X, *Y; 1458 if (match(&I, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y)))) 1459 return BinaryOperator::CreateNSWNeg(Builder.CreateSRem(X, Y)); 1460 1461 // If the sign bits of both operands are zero (i.e. we can prove they are 1462 // unsigned inputs), turn this into a urem. 1463 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); 1464 if (MaskedValueIsZero(Op1, Mask, 0, &I) && 1465 MaskedValueIsZero(Op0, Mask, 0, &I)) { 1466 // X srem Y -> X urem Y, iff X and Y don't have sign bit set 1467 return BinaryOperator::CreateURem(Op0, Op1, I.getName()); 1468 } 1469 1470 // If it's a constant vector, flip any negative values positive. 1471 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { 1472 Constant *C = cast<Constant>(Op1); 1473 unsigned VWidth = cast<VectorType>(C->getType())->getNumElements(); 1474 1475 bool hasNegative = false; 1476 bool hasMissing = false; 1477 for (unsigned i = 0; i != VWidth; ++i) { 1478 Constant *Elt = C->getAggregateElement(i); 1479 if (!Elt) { 1480 hasMissing = true; 1481 break; 1482 } 1483 1484 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) 1485 if (RHS->isNegative()) 1486 hasNegative = true; 1487 } 1488 1489 if (hasNegative && !hasMissing) { 1490 SmallVector<Constant *, 16> Elts(VWidth); 1491 for (unsigned i = 0; i != VWidth; ++i) { 1492 Elts[i] = C->getAggregateElement(i); // Handle undef, etc. 1493 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { 1494 if (RHS->isNegative()) 1495 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); 1496 } 1497 } 1498 1499 Constant *NewRHSV = ConstantVector::get(Elts); 1500 if (NewRHSV != C) // Don't loop on -MININT 1501 return replaceOperand(I, 1, NewRHSV); 1502 } 1503 } 1504 1505 return nullptr; 1506 } 1507 1508 Instruction *InstCombiner::visitFRem(BinaryOperator &I) { 1509 if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1), 1510 I.getFastMathFlags(), 1511 SQ.getWithInstruction(&I))) 1512 return replaceInstUsesWith(I, V); 1513 1514 if (Instruction *X = foldVectorBinop(I)) 1515 return X; 1516 1517 return nullptr; 1518 } 1519