LLVM API Documentation
00001 //===- InstCombineMulDivRem.cpp -------------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv, 00011 // srem, urem, frem. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "InstCombine.h" 00016 #include "llvm/Analysis/InstructionSimplify.h" 00017 #include "llvm/IR/IntrinsicInst.h" 00018 #include "llvm/Support/PatternMatch.h" 00019 using namespace llvm; 00020 using namespace PatternMatch; 00021 00022 00023 /// simplifyValueKnownNonZero - The specific integer value is used in a context 00024 /// where it is known to be non-zero. If this allows us to simplify the 00025 /// computation, do so and return the new operand, otherwise return null. 00026 static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC) { 00027 // If V has multiple uses, then we would have to do more analysis to determine 00028 // if this is safe. For example, the use could be in dynamically unreached 00029 // code. 00030 if (!V->hasOneUse()) return 0; 00031 00032 bool MadeChange = false; 00033 00034 // ((1 << A) >>u B) --> (1 << (A-B)) 00035 // Because V cannot be zero, we know that B is less than A. 00036 Value *A = 0, *B = 0, *PowerOf2 = 0; 00037 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))), 00038 m_Value(B))) && 00039 // The "1" can be any value known to be a power of 2. 00040 isKnownToBeAPowerOfTwo(PowerOf2)) { 00041 A = IC.Builder->CreateSub(A, B); 00042 return IC.Builder->CreateShl(PowerOf2, A); 00043 } 00044 00045 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it 00046 // inexact. Similarly for <<. 00047 if (BinaryOperator *I = dyn_cast<BinaryOperator>(V)) 00048 if (I->isLogicalShift() && isKnownToBeAPowerOfTwo(I->getOperand(0))) { 00049 // We know that this is an exact/nuw shift and that the input is a 00050 // non-zero context as well. 00051 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC)) { 00052 I->setOperand(0, V2); 00053 MadeChange = true; 00054 } 00055 00056 if (I->getOpcode() == Instruction::LShr && !I->isExact()) { 00057 I->setIsExact(); 00058 MadeChange = true; 00059 } 00060 00061 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { 00062 I->setHasNoUnsignedWrap(); 00063 MadeChange = true; 00064 } 00065 } 00066 00067 // TODO: Lots more we could do here: 00068 // If V is a phi node, we can call this on each of its operands. 00069 // "select cond, X, 0" can simplify to "X". 00070 00071 return MadeChange ? V : 0; 00072 } 00073 00074 00075 /// MultiplyOverflows - True if the multiply can not be expressed in an int 00076 /// this size. 00077 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { 00078 uint32_t W = C1->getBitWidth(); 00079 APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); 00080 if (sign) { 00081 LHSExt = LHSExt.sext(W * 2); 00082 RHSExt = RHSExt.sext(W * 2); 00083 } else { 00084 LHSExt = LHSExt.zext(W * 2); 00085 RHSExt = RHSExt.zext(W * 2); 00086 } 00087 00088 APInt MulExt = LHSExt * RHSExt; 00089 00090 if (!sign) 00091 return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); 00092 00093 APInt Min = APInt::getSignedMinValue(W).sext(W * 2); 00094 APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); 00095 return MulExt.slt(Min) || MulExt.sgt(Max); 00096 } 00097 00098 Instruction *InstCombiner::visitMul(BinaryOperator &I) { 00099 bool Changed = SimplifyAssociativeOrCommutative(I); 00100 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00101 00102 if (Value *V = SimplifyMulInst(Op0, Op1, TD)) 00103 return ReplaceInstUsesWith(I, V); 00104 00105 if (Value *V = SimplifyUsingDistributiveLaws(I)) 00106 return ReplaceInstUsesWith(I, V); 00107 00108 if (match(Op1, m_AllOnes())) // X * -1 == 0 - X 00109 return BinaryOperator::CreateNeg(Op0, I.getName()); 00110 00111 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { 00112 00113 // ((X << C1)*C2) == (X * (C2 << C1)) 00114 if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) 00115 if (SI->getOpcode() == Instruction::Shl) 00116 if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) 00117 return BinaryOperator::CreateMul(SI->getOperand(0), 00118 ConstantExpr::getShl(CI, ShOp)); 00119 00120 const APInt &Val = CI->getValue(); 00121 if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C 00122 Constant *NewCst = ConstantInt::get(Op0->getType(), Val.logBase2()); 00123 BinaryOperator *Shl = BinaryOperator::CreateShl(Op0, NewCst); 00124 if (I.hasNoSignedWrap()) Shl->setHasNoSignedWrap(); 00125 if (I.hasNoUnsignedWrap()) Shl->setHasNoUnsignedWrap(); 00126 return Shl; 00127 } 00128 00129 // Canonicalize (X+C1)*CI -> X*CI+C1*CI. 00130 { Value *X; ConstantInt *C1; 00131 if (Op0->hasOneUse() && 00132 match(Op0, m_Add(m_Value(X), m_ConstantInt(C1)))) { 00133 Value *Add = Builder->CreateMul(X, CI); 00134 return BinaryOperator::CreateAdd(Add, Builder->CreateMul(C1, CI)); 00135 } 00136 } 00137 00138 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n 00139 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n 00140 // The "* (2**n)" thus becomes a potential shifting opportunity. 00141 { 00142 const APInt & Val = CI->getValue(); 00143 const APInt &PosVal = Val.abs(); 00144 if (Val.isNegative() && PosVal.isPowerOf2()) { 00145 Value *X = 0, *Y = 0; 00146 if (Op0->hasOneUse()) { 00147 ConstantInt *C1; 00148 Value *Sub = 0; 00149 if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) 00150 Sub = Builder->CreateSub(X, Y, "suba"); 00151 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) 00152 Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc"); 00153 if (Sub) 00154 return 00155 BinaryOperator::CreateMul(Sub, 00156 ConstantInt::get(Y->getType(), PosVal)); 00157 } 00158 } 00159 } 00160 } 00161 00162 // Simplify mul instructions with a constant RHS. 00163 if (isa<Constant>(Op1)) { 00164 // Try to fold constant mul into select arguments. 00165 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 00166 if (Instruction *R = FoldOpIntoSelect(I, SI)) 00167 return R; 00168 00169 if (isa<PHINode>(Op0)) 00170 if (Instruction *NV = FoldOpIntoPhi(I)) 00171 return NV; 00172 } 00173 00174 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y 00175 if (Value *Op1v = dyn_castNegVal(Op1)) 00176 return BinaryOperator::CreateMul(Op0v, Op1v); 00177 00178 // (X / Y) * Y = X - (X % Y) 00179 // (X / Y) * -Y = (X % Y) - X 00180 { 00181 Value *Op1C = Op1; 00182 BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); 00183 if (!BO || 00184 (BO->getOpcode() != Instruction::UDiv && 00185 BO->getOpcode() != Instruction::SDiv)) { 00186 Op1C = Op0; 00187 BO = dyn_cast<BinaryOperator>(Op1); 00188 } 00189 Value *Neg = dyn_castNegVal(Op1C); 00190 if (BO && BO->hasOneUse() && 00191 (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) && 00192 (BO->getOpcode() == Instruction::UDiv || 00193 BO->getOpcode() == Instruction::SDiv)) { 00194 Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); 00195 00196 // If the division is exact, X % Y is zero, so we end up with X or -X. 00197 if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO)) 00198 if (SDiv->isExact()) { 00199 if (Op1BO == Op1C) 00200 return ReplaceInstUsesWith(I, Op0BO); 00201 return BinaryOperator::CreateNeg(Op0BO); 00202 } 00203 00204 Value *Rem; 00205 if (BO->getOpcode() == Instruction::UDiv) 00206 Rem = Builder->CreateURem(Op0BO, Op1BO); 00207 else 00208 Rem = Builder->CreateSRem(Op0BO, Op1BO); 00209 Rem->takeName(BO); 00210 00211 if (Op1BO == Op1C) 00212 return BinaryOperator::CreateSub(Op0BO, Rem); 00213 return BinaryOperator::CreateSub(Rem, Op0BO); 00214 } 00215 } 00216 00217 /// i1 mul -> i1 and. 00218 if (I.getType()->isIntegerTy(1)) 00219 return BinaryOperator::CreateAnd(Op0, Op1); 00220 00221 // X*(1 << Y) --> X << Y 00222 // (1 << Y)*X --> X << Y 00223 { 00224 Value *Y; 00225 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) 00226 return BinaryOperator::CreateShl(Op1, Y); 00227 if (match(Op1, m_Shl(m_One(), m_Value(Y)))) 00228 return BinaryOperator::CreateShl(Op0, Y); 00229 } 00230 00231 // If one of the operands of the multiply is a cast from a boolean value, then 00232 // we know the bool is either zero or one, so this is a 'masking' multiply. 00233 // X * Y (where Y is 0 or 1) -> X & (0-Y) 00234 if (!I.getType()->isVectorTy()) { 00235 // -2 is "-1 << 1" so it is all bits set except the low one. 00236 APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); 00237 00238 Value *BoolCast = 0, *OtherOp = 0; 00239 if (MaskedValueIsZero(Op0, Negative2)) 00240 BoolCast = Op0, OtherOp = Op1; 00241 else if (MaskedValueIsZero(Op1, Negative2)) 00242 BoolCast = Op1, OtherOp = Op0; 00243 00244 if (BoolCast) { 00245 Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), 00246 BoolCast); 00247 return BinaryOperator::CreateAnd(V, OtherOp); 00248 } 00249 } 00250 00251 return Changed ? &I : 0; 00252 } 00253 00254 // 00255 // Detect pattern: 00256 // 00257 // log2(Y*0.5) 00258 // 00259 // And check for corresponding fast math flags 00260 // 00261 00262 static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) { 00263 00264 if (!Op->hasOneUse()) 00265 return; 00266 00267 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op); 00268 if (!II) 00269 return; 00270 if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra()) 00271 return; 00272 Log2 = II; 00273 00274 Value *OpLog2Of = II->getArgOperand(0); 00275 if (!OpLog2Of->hasOneUse()) 00276 return; 00277 00278 Instruction *I = dyn_cast<Instruction>(OpLog2Of); 00279 if (!I) 00280 return; 00281 if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra()) 00282 return; 00283 00284 ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(0)); 00285 if (CFP && CFP->isExactlyValue(0.5)) { 00286 Y = I->getOperand(1); 00287 return; 00288 } 00289 CFP = dyn_cast<ConstantFP>(I->getOperand(1)); 00290 if (CFP && CFP->isExactlyValue(0.5)) 00291 Y = I->getOperand(0); 00292 } 00293 00294 /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns 00295 /// true iff the given value is FMul or FDiv with one and only one operand 00296 /// being a normal constant (i.e. not Zero/NaN/Infinity). 00297 static bool isFMulOrFDivWithConstant(Value *V) { 00298 Instruction *I = dyn_cast<Instruction>(V); 00299 if (!I || (I->getOpcode() != Instruction::FMul && 00300 I->getOpcode() != Instruction::FDiv)) 00301 return false; 00302 00303 ConstantFP *C0 = dyn_cast<ConstantFP>(I->getOperand(0)); 00304 ConstantFP *C1 = dyn_cast<ConstantFP>(I->getOperand(1)); 00305 00306 if (C0 && C1) 00307 return false; 00308 00309 return (C0 && C0->getValueAPF().isNormal()) || 00310 (C1 && C1->getValueAPF().isNormal()); 00311 } 00312 00313 static bool isNormalFp(const ConstantFP *C) { 00314 const APFloat &Flt = C->getValueAPF(); 00315 return Flt.isNormal() && !Flt.isDenormal(); 00316 } 00317 00318 /// foldFMulConst() is a helper routine of InstCombiner::visitFMul(). 00319 /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand 00320 /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). 00321 /// This function is to simplify "FMulOrDiv * C" and returns the 00322 /// resulting expression. Note that this function could return NULL in 00323 /// case the constants cannot be folded into a normal floating-point. 00324 /// 00325 Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, ConstantFP *C, 00326 Instruction *InsertBefore) { 00327 assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid"); 00328 00329 Value *Opnd0 = FMulOrDiv->getOperand(0); 00330 Value *Opnd1 = FMulOrDiv->getOperand(1); 00331 00332 ConstantFP *C0 = dyn_cast<ConstantFP>(Opnd0); 00333 ConstantFP *C1 = dyn_cast<ConstantFP>(Opnd1); 00334 00335 BinaryOperator *R = 0; 00336 00337 // (X * C0) * C => X * (C0*C) 00338 if (FMulOrDiv->getOpcode() == Instruction::FMul) { 00339 Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C); 00340 if (isNormalFp(cast<ConstantFP>(F))) 00341 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F); 00342 } else { 00343 if (C0) { 00344 // (C0 / X) * C => (C0 * C) / X 00345 ConstantFP *F = cast<ConstantFP>(ConstantExpr::getFMul(C0, C)); 00346 if (isNormalFp(F)) 00347 R = BinaryOperator::CreateFDiv(F, Opnd1); 00348 } else { 00349 // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal 00350 ConstantFP *F = cast<ConstantFP>(ConstantExpr::getFDiv(C, C1)); 00351 if (isNormalFp(F)) { 00352 R = BinaryOperator::CreateFMul(Opnd0, F); 00353 } else { 00354 // (X / C1) * C => X / (C1/C) 00355 Constant *F = ConstantExpr::getFDiv(C1, C); 00356 if (isNormalFp(cast<ConstantFP>(F))) 00357 R = BinaryOperator::CreateFDiv(Opnd0, F); 00358 } 00359 } 00360 } 00361 00362 if (R) { 00363 R->setHasUnsafeAlgebra(true); 00364 InsertNewInstWith(R, *InsertBefore); 00365 } 00366 00367 return R; 00368 } 00369 00370 Instruction *InstCombiner::visitFMul(BinaryOperator &I) { 00371 bool Changed = SimplifyAssociativeOrCommutative(I); 00372 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00373 00374 if (isa<Constant>(Op0)) 00375 std::swap(Op0, Op1); 00376 00377 if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), TD)) 00378 return ReplaceInstUsesWith(I, V); 00379 00380 bool AllowReassociate = I.hasUnsafeAlgebra(); 00381 00382 // Simplify mul instructions with a constant RHS. 00383 if (isa<Constant>(Op1)) { 00384 // Try to fold constant mul into select arguments. 00385 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 00386 if (Instruction *R = FoldOpIntoSelect(I, SI)) 00387 return R; 00388 00389 if (isa<PHINode>(Op0)) 00390 if (Instruction *NV = FoldOpIntoPhi(I)) 00391 return NV; 00392 00393 ConstantFP *C = dyn_cast<ConstantFP>(Op1); 00394 if (C && AllowReassociate && C->getValueAPF().isNormal()) { 00395 // Let MDC denote an expression in one of these forms: 00396 // X * C, C/X, X/C, where C is a constant. 00397 // 00398 // Try to simplify "MDC * Constant" 00399 if (isFMulOrFDivWithConstant(Op0)) { 00400 Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I); 00401 if (V) 00402 return ReplaceInstUsesWith(I, V); 00403 } 00404 00405 // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C) 00406 Instruction *FAddSub = dyn_cast<Instruction>(Op0); 00407 if (FAddSub && 00408 (FAddSub->getOpcode() == Instruction::FAdd || 00409 FAddSub->getOpcode() == Instruction::FSub)) { 00410 Value *Opnd0 = FAddSub->getOperand(0); 00411 Value *Opnd1 = FAddSub->getOperand(1); 00412 ConstantFP *C0 = dyn_cast<ConstantFP>(Opnd0); 00413 ConstantFP *C1 = dyn_cast<ConstantFP>(Opnd1); 00414 bool Swap = false; 00415 if (C0) { 00416 std::swap(C0, C1); 00417 std::swap(Opnd0, Opnd1); 00418 Swap = true; 00419 } 00420 00421 if (C1 && C1->getValueAPF().isNormal() && 00422 isFMulOrFDivWithConstant(Opnd0)) { 00423 Value *M1 = ConstantExpr::getFMul(C1, C); 00424 Value *M0 = isNormalFp(cast<ConstantFP>(M1)) ? 00425 foldFMulConst(cast<Instruction>(Opnd0), C, &I) : 00426 0; 00427 if (M0 && M1) { 00428 if (Swap && FAddSub->getOpcode() == Instruction::FSub) 00429 std::swap(M0, M1); 00430 00431 Value *R = (FAddSub->getOpcode() == Instruction::FAdd) ? 00432 BinaryOperator::CreateFAdd(M0, M1) : 00433 BinaryOperator::CreateFSub(M0, M1); 00434 Instruction *RI = cast<Instruction>(R); 00435 RI->copyFastMathFlags(&I); 00436 return RI; 00437 } 00438 } 00439 } 00440 } 00441 } 00442 00443 00444 // Under unsafe algebra do: 00445 // X * log2(0.5*Y) = X*log2(Y) - X 00446 if (I.hasUnsafeAlgebra()) { 00447 Value *OpX = NULL; 00448 Value *OpY = NULL; 00449 IntrinsicInst *Log2; 00450 detectLog2OfHalf(Op0, OpY, Log2); 00451 if (OpY) { 00452 OpX = Op1; 00453 } else { 00454 detectLog2OfHalf(Op1, OpY, Log2); 00455 if (OpY) { 00456 OpX = Op0; 00457 } 00458 } 00459 // if pattern detected emit alternate sequence 00460 if (OpX && OpY) { 00461 Log2->setArgOperand(0, OpY); 00462 Value *FMulVal = Builder->CreateFMul(OpX, Log2); 00463 Instruction *FMul = cast<Instruction>(FMulVal); 00464 FMul->copyFastMathFlags(Log2); 00465 Instruction *FSub = BinaryOperator::CreateFSub(FMulVal, OpX); 00466 FSub->copyFastMathFlags(Log2); 00467 return FSub; 00468 } 00469 } 00470 00471 // Handle symmetric situation in a 2-iteration loop 00472 Value *Opnd0 = Op0; 00473 Value *Opnd1 = Op1; 00474 for (int i = 0; i < 2; i++) { 00475 bool IgnoreZeroSign = I.hasNoSignedZeros(); 00476 if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) { 00477 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign); 00478 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign); 00479 00480 // -X * -Y => X*Y 00481 if (N1) 00482 return BinaryOperator::CreateFMul(N0, N1); 00483 00484 if (Opnd0->hasOneUse()) { 00485 // -X * Y => -(X*Y) (Promote negation as high as possible) 00486 Value *T = Builder->CreateFMul(N0, Opnd1); 00487 cast<Instruction>(T)->setDebugLoc(I.getDebugLoc()); 00488 Instruction *Neg = BinaryOperator::CreateFNeg(T); 00489 if (I.getFastMathFlags().any()) { 00490 cast<Instruction>(T)->copyFastMathFlags(&I); 00491 Neg->copyFastMathFlags(&I); 00492 } 00493 return Neg; 00494 } 00495 } 00496 00497 // (X*Y) * X => (X*X) * Y where Y != X 00498 // The purpose is two-fold: 00499 // 1) to form a power expression (of X). 00500 // 2) potentially shorten the critical path: After transformation, the 00501 // latency of the instruction Y is amortized by the expression of X*X, 00502 // and therefore Y is in a "less critical" position compared to what it 00503 // was before the transformation. 00504 // 00505 if (AllowReassociate) { 00506 Value *Opnd0_0, *Opnd0_1; 00507 if (Opnd0->hasOneUse() && 00508 match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) { 00509 Value *Y = 0; 00510 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1) 00511 Y = Opnd0_1; 00512 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1) 00513 Y = Opnd0_0; 00514 00515 if (Y) { 00516 Instruction *T = cast<Instruction>(Builder->CreateFMul(Opnd1, Opnd1)); 00517 T->copyFastMathFlags(&I); 00518 T->setDebugLoc(I.getDebugLoc()); 00519 00520 Instruction *R = BinaryOperator::CreateFMul(T, Y); 00521 R->copyFastMathFlags(&I); 00522 return R; 00523 } 00524 } 00525 } 00526 00527 if (!isa<Constant>(Op1)) 00528 std::swap(Opnd0, Opnd1); 00529 else 00530 break; 00531 } 00532 00533 return Changed ? &I : 0; 00534 } 00535 00536 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select 00537 /// instruction. 00538 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { 00539 SelectInst *SI = cast<SelectInst>(I.getOperand(1)); 00540 00541 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y 00542 int NonNullOperand = -1; 00543 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) 00544 if (ST->isNullValue()) 00545 NonNullOperand = 2; 00546 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y 00547 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) 00548 if (ST->isNullValue()) 00549 NonNullOperand = 1; 00550 00551 if (NonNullOperand == -1) 00552 return false; 00553 00554 Value *SelectCond = SI->getOperand(0); 00555 00556 // Change the div/rem to use 'Y' instead of the select. 00557 I.setOperand(1, SI->getOperand(NonNullOperand)); 00558 00559 // Okay, we know we replace the operand of the div/rem with 'Y' with no 00560 // problem. However, the select, or the condition of the select may have 00561 // multiple uses. Based on our knowledge that the operand must be non-zero, 00562 // propagate the known value for the select into other uses of it, and 00563 // propagate a known value of the condition into its other users. 00564 00565 // If the select and condition only have a single use, don't bother with this, 00566 // early exit. 00567 if (SI->use_empty() && SelectCond->hasOneUse()) 00568 return true; 00569 00570 // Scan the current block backward, looking for other uses of SI. 00571 BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); 00572 00573 while (BBI != BBFront) { 00574 --BBI; 00575 // If we found a call to a function, we can't assume it will return, so 00576 // information from below it cannot be propagated above it. 00577 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) 00578 break; 00579 00580 // Replace uses of the select or its condition with the known values. 00581 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); 00582 I != E; ++I) { 00583 if (*I == SI) { 00584 *I = SI->getOperand(NonNullOperand); 00585 Worklist.Add(BBI); 00586 } else if (*I == SelectCond) { 00587 *I = NonNullOperand == 1 ? ConstantInt::getTrue(BBI->getContext()) : 00588 ConstantInt::getFalse(BBI->getContext()); 00589 Worklist.Add(BBI); 00590 } 00591 } 00592 00593 // If we past the instruction, quit looking for it. 00594 if (&*BBI == SI) 00595 SI = 0; 00596 if (&*BBI == SelectCond) 00597 SelectCond = 0; 00598 00599 // If we ran out of things to eliminate, break out of the loop. 00600 if (SelectCond == 0 && SI == 0) 00601 break; 00602 00603 } 00604 return true; 00605 } 00606 00607 00608 /// This function implements the transforms common to both integer division 00609 /// instructions (udiv and sdiv). It is called by the visitors to those integer 00610 /// division instructions. 00611 /// @brief Common integer divide transforms 00612 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { 00613 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00614 00615 // The RHS is known non-zero. 00616 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) { 00617 I.setOperand(1, V); 00618 return &I; 00619 } 00620 00621 // Handle cases involving: [su]div X, (select Cond, Y, Z) 00622 // This does not apply for fdiv. 00623 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 00624 return &I; 00625 00626 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 00627 // (X / C1) / C2 -> X / (C1*C2) 00628 if (Instruction *LHS = dyn_cast<Instruction>(Op0)) 00629 if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) 00630 if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { 00631 if (MultiplyOverflows(RHS, LHSRHS, 00632 I.getOpcode()==Instruction::SDiv)) 00633 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); 00634 return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), 00635 ConstantExpr::getMul(RHS, LHSRHS)); 00636 } 00637 00638 if (!RHS->isZero()) { // avoid X udiv 0 00639 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 00640 if (Instruction *R = FoldOpIntoSelect(I, SI)) 00641 return R; 00642 if (isa<PHINode>(Op0)) 00643 if (Instruction *NV = FoldOpIntoPhi(I)) 00644 return NV; 00645 } 00646 } 00647 00648 // See if we can fold away this div instruction. 00649 if (SimplifyDemandedInstructionBits(I)) 00650 return &I; 00651 00652 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y 00653 Value *X = 0, *Z = 0; 00654 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 00655 bool isSigned = I.getOpcode() == Instruction::SDiv; 00656 if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || 00657 (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) 00658 return BinaryOperator::Create(I.getOpcode(), X, Op1); 00659 } 00660 00661 return 0; 00662 } 00663 00664 /// dyn_castZExtVal - Checks if V is a zext or constant that can 00665 /// be truncated to Ty without losing bits. 00666 static Value *dyn_castZExtVal(Value *V, Type *Ty) { 00667 if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) { 00668 if (Z->getSrcTy() == Ty) 00669 return Z->getOperand(0); 00670 } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { 00671 if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth()) 00672 return ConstantExpr::getTrunc(C, Ty); 00673 } 00674 return 0; 00675 } 00676 00677 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { 00678 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00679 00680 if (Value *V = SimplifyUDivInst(Op0, Op1, TD)) 00681 return ReplaceInstUsesWith(I, V); 00682 00683 // Handle the integer div common cases 00684 if (Instruction *Common = commonIDivTransforms(I)) 00685 return Common; 00686 00687 { 00688 // X udiv 2^C -> X >> C 00689 // Check to see if this is an unsigned division with an exact power of 2, 00690 // if so, convert to a right shift. 00691 const APInt *C; 00692 if (match(Op1, m_Power2(C))) { 00693 BinaryOperator *LShr = 00694 BinaryOperator::CreateLShr(Op0, 00695 ConstantInt::get(Op0->getType(), 00696 C->logBase2())); 00697 if (I.isExact()) LShr->setIsExact(); 00698 return LShr; 00699 } 00700 } 00701 00702 if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { 00703 // X udiv C, where C >= signbit 00704 if (C->getValue().isNegative()) { 00705 Value *IC = Builder->CreateICmpULT(Op0, C); 00706 return SelectInst::Create(IC, Constant::getNullValue(I.getType()), 00707 ConstantInt::get(I.getType(), 1)); 00708 } 00709 } 00710 00711 // (x lshr C1) udiv C2 --> x udiv (C2 << C1) 00712 if (ConstantInt *C2 = dyn_cast<ConstantInt>(Op1)) { 00713 Value *X; 00714 ConstantInt *C1; 00715 if (match(Op0, m_LShr(m_Value(X), m_ConstantInt(C1)))) { 00716 APInt NC = C2->getValue().shl(C1->getLimitedValue(C1->getBitWidth()-1)); 00717 return BinaryOperator::CreateUDiv(X, Builder->getInt(NC)); 00718 } 00719 } 00720 00721 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) 00722 { const APInt *CI; Value *N; 00723 if (match(Op1, m_Shl(m_Power2(CI), m_Value(N))) || 00724 match(Op1, m_ZExt(m_Shl(m_Power2(CI), m_Value(N))))) { 00725 if (*CI != 1) 00726 N = Builder->CreateAdd(N, 00727 ConstantInt::get(N->getType(), CI->logBase2())); 00728 if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1)) 00729 N = Builder->CreateZExt(N, Z->getDestTy()); 00730 if (I.isExact()) 00731 return BinaryOperator::CreateExactLShr(Op0, N); 00732 return BinaryOperator::CreateLShr(Op0, N); 00733 } 00734 } 00735 00736 // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) 00737 // where C1&C2 are powers of two. 00738 { Value *Cond; const APInt *C1, *C2; 00739 if (match(Op1, m_Select(m_Value(Cond), m_Power2(C1), m_Power2(C2)))) { 00740 // Construct the "on true" case of the select 00741 Value *TSI = Builder->CreateLShr(Op0, C1->logBase2(), Op1->getName()+".t", 00742 I.isExact()); 00743 00744 // Construct the "on false" case of the select 00745 Value *FSI = Builder->CreateLShr(Op0, C2->logBase2(), Op1->getName()+".f", 00746 I.isExact()); 00747 00748 // construct the select instruction and return it. 00749 return SelectInst::Create(Cond, TSI, FSI); 00750 } 00751 } 00752 00753 // (zext A) udiv (zext B) --> zext (A udiv B) 00754 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) 00755 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) 00756 return new ZExtInst(Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", 00757 I.isExact()), 00758 I.getType()); 00759 00760 return 0; 00761 } 00762 00763 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { 00764 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00765 00766 if (Value *V = SimplifySDivInst(Op0, Op1, TD)) 00767 return ReplaceInstUsesWith(I, V); 00768 00769 // Handle the integer div common cases 00770 if (Instruction *Common = commonIDivTransforms(I)) 00771 return Common; 00772 00773 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 00774 // sdiv X, -1 == -X 00775 if (RHS->isAllOnesValue()) 00776 return BinaryOperator::CreateNeg(Op0); 00777 00778 // sdiv X, C --> ashr exact X, log2(C) 00779 if (I.isExact() && RHS->getValue().isNonNegative() && 00780 RHS->getValue().isPowerOf2()) { 00781 Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), 00782 RHS->getValue().exactLogBase2()); 00783 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); 00784 } 00785 00786 // -X/C --> X/-C provided the negation doesn't overflow. 00787 if (SubOperator *Sub = dyn_cast<SubOperator>(Op0)) 00788 if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap()) 00789 return BinaryOperator::CreateSDiv(Sub->getOperand(1), 00790 ConstantExpr::getNeg(RHS)); 00791 } 00792 00793 // If the sign bits of both operands are zero (i.e. we can prove they are 00794 // unsigned inputs), turn this into a udiv. 00795 if (I.getType()->isIntegerTy()) { 00796 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); 00797 if (MaskedValueIsZero(Op0, Mask)) { 00798 if (MaskedValueIsZero(Op1, Mask)) { 00799 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set 00800 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 00801 } 00802 00803 if (match(Op1, m_Shl(m_Power2(), m_Value()))) { 00804 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) 00805 // Safe because the only negative value (1 << Y) can take on is 00806 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have 00807 // the sign bit set. 00808 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); 00809 } 00810 } 00811 } 00812 00813 return 0; 00814 } 00815 00816 /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special 00817 /// FP value and: 00818 /// 1) 1/C is exact, or 00819 /// 2) reciprocal is allowed. 00820 /// If the conversion was successful, the simplified expression "X * 1/C" is 00821 /// returned; otherwise, NULL is returned. 00822 /// 00823 static Instruction *CvtFDivConstToReciprocal(Value *Dividend, 00824 ConstantFP *Divisor, 00825 bool AllowReciprocal) { 00826 const APFloat &FpVal = Divisor->getValueAPF(); 00827 APFloat Reciprocal(FpVal.getSemantics()); 00828 bool Cvt = FpVal.getExactInverse(&Reciprocal); 00829 00830 if (!Cvt && AllowReciprocal && FpVal.isNormal()) { 00831 Reciprocal = APFloat(FpVal.getSemantics(), 1.0f); 00832 (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven); 00833 Cvt = !Reciprocal.isDenormal(); 00834 } 00835 00836 if (!Cvt) 00837 return 0; 00838 00839 ConstantFP *R; 00840 R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal); 00841 return BinaryOperator::CreateFMul(Dividend, R); 00842 } 00843 00844 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { 00845 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00846 00847 if (Value *V = SimplifyFDivInst(Op0, Op1, TD)) 00848 return ReplaceInstUsesWith(I, V); 00849 00850 bool AllowReassociate = I.hasUnsafeAlgebra(); 00851 bool AllowReciprocal = I.hasAllowReciprocal(); 00852 00853 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { 00854 if (AllowReassociate) { 00855 ConstantFP *C1 = 0; 00856 ConstantFP *C2 = Op1C; 00857 Value *X; 00858 Instruction *Res = 0; 00859 00860 if (match(Op0, m_FMul(m_Value(X), m_ConstantFP(C1)))) { 00861 // (X*C1)/C2 => X * (C1/C2) 00862 // 00863 Constant *C = ConstantExpr::getFDiv(C1, C2); 00864 const APFloat &F = cast<ConstantFP>(C)->getValueAPF(); 00865 if (F.isNormal() && !F.isDenormal()) 00866 Res = BinaryOperator::CreateFMul(X, C); 00867 } else if (match(Op0, m_FDiv(m_Value(X), m_ConstantFP(C1)))) { 00868 // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed] 00869 // 00870 Constant *C = ConstantExpr::getFMul(C1, C2); 00871 const APFloat &F = cast<ConstantFP>(C)->getValueAPF(); 00872 if (F.isNormal() && !F.isDenormal()) { 00873 Res = CvtFDivConstToReciprocal(X, cast<ConstantFP>(C), 00874 AllowReciprocal); 00875 if (!Res) 00876 Res = BinaryOperator::CreateFDiv(X, C); 00877 } 00878 } 00879 00880 if (Res) { 00881 Res->setFastMathFlags(I.getFastMathFlags()); 00882 return Res; 00883 } 00884 } 00885 00886 // X / C => X * 1/C 00887 if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) 00888 return T; 00889 00890 return 0; 00891 } 00892 00893 if (AllowReassociate && isa<ConstantFP>(Op0)) { 00894 ConstantFP *C1 = cast<ConstantFP>(Op0), *C2; 00895 Constant *Fold = 0; 00896 Value *X; 00897 bool CreateDiv = true; 00898 00899 // C1 / (X*C2) => (C1/C2) / X 00900 if (match(Op1, m_FMul(m_Value(X), m_ConstantFP(C2)))) 00901 Fold = ConstantExpr::getFDiv(C1, C2); 00902 else if (match(Op1, m_FDiv(m_Value(X), m_ConstantFP(C2)))) { 00903 // C1 / (X/C2) => (C1*C2) / X 00904 Fold = ConstantExpr::getFMul(C1, C2); 00905 } else if (match(Op1, m_FDiv(m_ConstantFP(C2), m_Value(X)))) { 00906 // C1 / (C2/X) => (C1/C2) * X 00907 Fold = ConstantExpr::getFDiv(C1, C2); 00908 CreateDiv = false; 00909 } 00910 00911 if (Fold) { 00912 const APFloat &FoldC = cast<ConstantFP>(Fold)->getValueAPF(); 00913 if (FoldC.isNormal() && !FoldC.isDenormal()) { 00914 Instruction *R = CreateDiv ? 00915 BinaryOperator::CreateFDiv(Fold, X) : 00916 BinaryOperator::CreateFMul(X, Fold); 00917 R->setFastMathFlags(I.getFastMathFlags()); 00918 return R; 00919 } 00920 } 00921 return 0; 00922 } 00923 00924 if (AllowReassociate) { 00925 Value *X, *Y; 00926 Value *NewInst = 0; 00927 Instruction *SimpR = 0; 00928 00929 if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) { 00930 // (X/Y) / Z => X / (Y*Z) 00931 // 00932 if (!isa<ConstantFP>(Y) || !isa<ConstantFP>(Op1)) { 00933 NewInst = Builder->CreateFMul(Y, Op1); 00934 SimpR = BinaryOperator::CreateFDiv(X, NewInst); 00935 } 00936 } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) { 00937 // Z / (X/Y) => Z*Y / X 00938 // 00939 if (!isa<ConstantFP>(Y) || !isa<ConstantFP>(Op0)) { 00940 NewInst = Builder->CreateFMul(Op0, Y); 00941 SimpR = BinaryOperator::CreateFDiv(NewInst, X); 00942 } 00943 } 00944 00945 if (NewInst) { 00946 if (Instruction *T = dyn_cast<Instruction>(NewInst)) 00947 T->setDebugLoc(I.getDebugLoc()); 00948 SimpR->setFastMathFlags(I.getFastMathFlags()); 00949 return SimpR; 00950 } 00951 } 00952 00953 return 0; 00954 } 00955 00956 /// This function implements the transforms common to both integer remainder 00957 /// instructions (urem and srem). It is called by the visitors to those integer 00958 /// remainder instructions. 00959 /// @brief Common integer remainder transforms 00960 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { 00961 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00962 00963 // The RHS is known non-zero. 00964 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) { 00965 I.setOperand(1, V); 00966 return &I; 00967 } 00968 00969 // Handle cases involving: rem X, (select Cond, Y, Z) 00970 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 00971 return &I; 00972 00973 if (isa<ConstantInt>(Op1)) { 00974 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { 00975 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { 00976 if (Instruction *R = FoldOpIntoSelect(I, SI)) 00977 return R; 00978 } else if (isa<PHINode>(Op0I)) { 00979 if (Instruction *NV = FoldOpIntoPhi(I)) 00980 return NV; 00981 } 00982 00983 // See if we can fold away this rem instruction. 00984 if (SimplifyDemandedInstructionBits(I)) 00985 return &I; 00986 } 00987 } 00988 00989 return 0; 00990 } 00991 00992 Instruction *InstCombiner::visitURem(BinaryOperator &I) { 00993 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 00994 00995 if (Value *V = SimplifyURemInst(Op0, Op1, TD)) 00996 return ReplaceInstUsesWith(I, V); 00997 00998 if (Instruction *common = commonIRemTransforms(I)) 00999 return common; 01000 01001 // (zext A) urem (zext B) --> zext (A urem B) 01002 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) 01003 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) 01004 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1), 01005 I.getType()); 01006 01007 // X urem Y -> X and Y-1, where Y is a power of 2, 01008 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true)) { 01009 Constant *N1 = Constant::getAllOnesValue(I.getType()); 01010 Value *Add = Builder->CreateAdd(Op1, N1); 01011 return BinaryOperator::CreateAnd(Op0, Add); 01012 } 01013 01014 return 0; 01015 } 01016 01017 Instruction *InstCombiner::visitSRem(BinaryOperator &I) { 01018 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 01019 01020 if (Value *V = SimplifySRemInst(Op0, Op1, TD)) 01021 return ReplaceInstUsesWith(I, V); 01022 01023 // Handle the integer rem common cases 01024 if (Instruction *Common = commonIRemTransforms(I)) 01025 return Common; 01026 01027 if (Value *RHSNeg = dyn_castNegVal(Op1)) 01028 if (!isa<Constant>(RHSNeg) || 01029 (isa<ConstantInt>(RHSNeg) && 01030 cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { 01031 // X % -Y -> X % Y 01032 Worklist.AddValue(I.getOperand(1)); 01033 I.setOperand(1, RHSNeg); 01034 return &I; 01035 } 01036 01037 // If the sign bits of both operands are zero (i.e. we can prove they are 01038 // unsigned inputs), turn this into a urem. 01039 if (I.getType()->isIntegerTy()) { 01040 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); 01041 if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { 01042 // X srem Y -> X urem Y, iff X and Y don't have sign bit set 01043 return BinaryOperator::CreateURem(Op0, Op1, I.getName()); 01044 } 01045 } 01046 01047 // If it's a constant vector, flip any negative values positive. 01048 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { 01049 Constant *C = cast<Constant>(Op1); 01050 unsigned VWidth = C->getType()->getVectorNumElements(); 01051 01052 bool hasNegative = false; 01053 bool hasMissing = false; 01054 for (unsigned i = 0; i != VWidth; ++i) { 01055 Constant *Elt = C->getAggregateElement(i); 01056 if (Elt == 0) { 01057 hasMissing = true; 01058 break; 01059 } 01060 01061 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) 01062 if (RHS->isNegative()) 01063 hasNegative = true; 01064 } 01065 01066 if (hasNegative && !hasMissing) { 01067 SmallVector<Constant *, 16> Elts(VWidth); 01068 for (unsigned i = 0; i != VWidth; ++i) { 01069 Elts[i] = C->getAggregateElement(i); // Handle undef, etc. 01070 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { 01071 if (RHS->isNegative()) 01072 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); 01073 } 01074 } 01075 01076 Constant *NewRHSV = ConstantVector::get(Elts); 01077 if (NewRHSV != C) { // Don't loop on -MININT 01078 Worklist.AddValue(I.getOperand(1)); 01079 I.setOperand(1, NewRHSV); 01080 return &I; 01081 } 01082 } 01083 } 01084 01085 return 0; 01086 } 01087 01088 Instruction *InstCombiner::visitFRem(BinaryOperator &I) { 01089 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 01090 01091 if (Value *V = SimplifyFRemInst(Op0, Op1, TD)) 01092 return ReplaceInstUsesWith(I, V); 01093 01094 // Handle cases involving: rem X, (select Cond, Y, Z) 01095 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) 01096 return &I; 01097 01098 return 0; 01099 }