Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1 | //===- InstCombineMulDivRem.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv, |
| 11 | // srem, urem, frem. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "InstCombine.h" |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IntrinsicInst.h" |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/PatternMatch.h" |
| 19 | using namespace llvm; |
| 20 | using namespace PatternMatch; |
| 21 | |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 22 | |
| 23 | /// simplifyValueKnownNonZero - The specific integer value is used in a context |
| 24 | /// where it is known to be non-zero. If this allows us to simplify the |
| 25 | /// computation, do so and return the new operand, otherwise return null. |
| 26 | static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC) { |
| 27 | // If V has multiple uses, then we would have to do more analysis to determine |
| 28 | // if this is safe. For example, the use could be in dynamically unreached |
| 29 | // code. |
| 30 | if (!V->hasOneUse()) return 0; |
| 31 | |
Chris Lattner | 613f1a3 | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 32 | bool MadeChange = false; |
| 33 | |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 34 | // ((1 << A) >>u B) --> (1 << (A-B)) |
| 35 | // Because V cannot be zero, we know that B is less than A. |
Chris Lattner | 6083bb9 | 2011-05-23 00:09:55 +0000 | [diff] [blame] | 36 | Value *A = 0, *B = 0, *PowerOf2 = 0; |
| 37 | if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))), |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 38 | m_Value(B))) && |
| 39 | // The "1" can be any value known to be a power of 2. |
Rafael Espindola | dbaa237 | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 40 | isKnownToBeAPowerOfTwo(PowerOf2)) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 41 | A = IC.Builder->CreateSub(A, B); |
Chris Lattner | 6083bb9 | 2011-05-23 00:09:55 +0000 | [diff] [blame] | 42 | return IC.Builder->CreateShl(PowerOf2, A); |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chris Lattner | 613f1a3 | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 45 | // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it |
| 46 | // inexact. Similarly for <<. |
| 47 | if (BinaryOperator *I = dyn_cast<BinaryOperator>(V)) |
Rafael Espindola | dbaa237 | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 48 | if (I->isLogicalShift() && isKnownToBeAPowerOfTwo(I->getOperand(0))) { |
Chris Lattner | 613f1a3 | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 49 | // We know that this is an exact/nuw shift and that the input is a |
| 50 | // non-zero context as well. |
| 51 | if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC)) { |
| 52 | I->setOperand(0, V2); |
| 53 | MadeChange = true; |
| 54 | } |
| 55 | |
| 56 | if (I->getOpcode() == Instruction::LShr && !I->isExact()) { |
| 57 | I->setIsExact(); |
| 58 | MadeChange = true; |
| 59 | } |
| 60 | |
| 61 | if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { |
| 62 | I->setHasNoUnsignedWrap(); |
| 63 | MadeChange = true; |
| 64 | } |
| 65 | } |
| 66 | |
Chris Lattner | 6c9b8d3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 67 | // TODO: Lots more we could do here: |
Chris Lattner | 6c9b8d3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 68 | // If V is a phi node, we can call this on each of its operands. |
| 69 | // "select cond, X, 0" can simplify to "X". |
| 70 | |
Chris Lattner | 613f1a3 | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 71 | return MadeChange ? V : 0; |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 75 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 76 | /// this size. |
| 77 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 78 | uint32_t W = C1->getBitWidth(); |
| 79 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 80 | if (sign) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 81 | LHSExt = LHSExt.sext(W * 2); |
| 82 | RHSExt = RHSExt.sext(W * 2); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 83 | } else { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 84 | LHSExt = LHSExt.zext(W * 2); |
| 85 | RHSExt = RHSExt.zext(W * 2); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | APInt MulExt = LHSExt * RHSExt; |
| 89 | |
| 90 | if (!sign) |
| 91 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 92 | |
| 93 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 94 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 95 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 96 | } |
| 97 | |
| 98 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 99 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 100 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 101 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 102 | if (Value *V = SimplifyMulInst(Op0, Op1, TD)) |
| 103 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 104 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 105 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 106 | return ReplaceInstUsesWith(I, V); |
| 107 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 108 | if (match(Op1, m_AllOnes())) // X * -1 == 0 - X |
| 109 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
| 110 | |
| 111 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 112 | |
| 113 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 114 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
| 115 | if (SI->getOpcode() == Instruction::Shl) |
| 116 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
| 117 | return BinaryOperator::CreateMul(SI->getOperand(0), |
| 118 | ConstantExpr::getShl(CI, ShOp)); |
| 119 | |
| 120 | const APInt &Val = CI->getValue(); |
| 121 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
| 122 | Constant *NewCst = ConstantInt::get(Op0->getType(), Val.logBase2()); |
| 123 | BinaryOperator *Shl = BinaryOperator::CreateShl(Op0, NewCst); |
| 124 | if (I.hasNoSignedWrap()) Shl->setHasNoSignedWrap(); |
| 125 | if (I.hasNoUnsignedWrap()) Shl->setHasNoUnsignedWrap(); |
| 126 | return Shl; |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 129 | // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |
| 130 | { Value *X; ConstantInt *C1; |
| 131 | if (Op0->hasOneUse() && |
| 132 | match(Op0, m_Add(m_Value(X), m_ConstantInt(C1)))) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 133 | Value *Add = Builder->CreateMul(X, CI); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 134 | return BinaryOperator::CreateAdd(Add, Builder->CreateMul(C1, CI)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 136 | } |
Stuart Hastings | acbf107 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 137 | |
Stuart Hastings | f100282 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 138 | // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n |
| 139 | // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n |
| 140 | // The "* (2**n)" thus becomes a potential shifting opportunity. |
Stuart Hastings | acbf107 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 141 | { |
| 142 | const APInt & Val = CI->getValue(); |
| 143 | const APInt &PosVal = Val.abs(); |
| 144 | if (Val.isNegative() && PosVal.isPowerOf2()) { |
Stuart Hastings | f100282 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 145 | Value *X = 0, *Y = 0; |
| 146 | if (Op0->hasOneUse()) { |
| 147 | ConstantInt *C1; |
| 148 | Value *Sub = 0; |
| 149 | if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) |
| 150 | Sub = Builder->CreateSub(X, Y, "suba"); |
| 151 | else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) |
| 152 | Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc"); |
| 153 | if (Sub) |
| 154 | return |
| 155 | BinaryOperator::CreateMul(Sub, |
| 156 | ConstantInt::get(Y->getType(), PosVal)); |
Stuart Hastings | acbf107 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Simplify mul instructions with a constant RHS. |
| 163 | if (isa<Constant>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 164 | // Try to fold constant mul into select arguments. |
| 165 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 166 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 167 | return R; |
| 168 | |
| 169 | if (isa<PHINode>(Op0)) |
| 170 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 171 | return NV; |
| 172 | } |
| 173 | |
| 174 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 175 | if (Value *Op1v = dyn_castNegVal(Op1)) |
| 176 | return BinaryOperator::CreateMul(Op0v, Op1v); |
| 177 | |
| 178 | // (X / Y) * Y = X - (X % Y) |
| 179 | // (X / Y) * -Y = (X % Y) - X |
| 180 | { |
| 181 | Value *Op1C = Op1; |
| 182 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); |
| 183 | if (!BO || |
| 184 | (BO->getOpcode() != Instruction::UDiv && |
| 185 | BO->getOpcode() != Instruction::SDiv)) { |
| 186 | Op1C = Op0; |
| 187 | BO = dyn_cast<BinaryOperator>(Op1); |
| 188 | } |
| 189 | Value *Neg = dyn_castNegVal(Op1C); |
| 190 | if (BO && BO->hasOneUse() && |
| 191 | (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) && |
| 192 | (BO->getOpcode() == Instruction::UDiv || |
| 193 | BO->getOpcode() == Instruction::SDiv)) { |
| 194 | Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); |
| 195 | |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 196 | // If the division is exact, X % Y is zero, so we end up with X or -X. |
| 197 | if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO)) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 198 | if (SDiv->isExact()) { |
| 199 | if (Op1BO == Op1C) |
| 200 | return ReplaceInstUsesWith(I, Op0BO); |
| 201 | return BinaryOperator::CreateNeg(Op0BO); |
| 202 | } |
| 203 | |
| 204 | Value *Rem; |
| 205 | if (BO->getOpcode() == Instruction::UDiv) |
| 206 | Rem = Builder->CreateURem(Op0BO, Op1BO); |
| 207 | else |
| 208 | Rem = Builder->CreateSRem(Op0BO, Op1BO); |
| 209 | Rem->takeName(BO); |
| 210 | |
| 211 | if (Op1BO == Op1C) |
| 212 | return BinaryOperator::CreateSub(Op0BO, Rem); |
| 213 | return BinaryOperator::CreateSub(Rem, Op0BO); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /// i1 mul -> i1 and. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 218 | if (I.getType()->isIntegerTy(1)) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 219 | return BinaryOperator::CreateAnd(Op0, Op1); |
| 220 | |
| 221 | // X*(1 << Y) --> X << Y |
| 222 | // (1 << Y)*X --> X << Y |
| 223 | { |
| 224 | Value *Y; |
| 225 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) |
| 226 | return BinaryOperator::CreateShl(Op1, Y); |
| 227 | if (match(Op1, m_Shl(m_One(), m_Value(Y)))) |
| 228 | return BinaryOperator::CreateShl(Op0, Y); |
| 229 | } |
| 230 | |
| 231 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 232 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 233 | // X * Y (where Y is 0 or 1) -> X & (0-Y) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 234 | if (!I.getType()->isVectorTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 235 | // -2 is "-1 << 1" so it is all bits set except the low one. |
| 236 | APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); |
| 237 | |
| 238 | Value *BoolCast = 0, *OtherOp = 0; |
| 239 | if (MaskedValueIsZero(Op0, Negative2)) |
| 240 | BoolCast = Op0, OtherOp = Op1; |
| 241 | else if (MaskedValueIsZero(Op1, Negative2)) |
| 242 | BoolCast = Op1, OtherOp = Op0; |
| 243 | |
| 244 | if (BoolCast) { |
| 245 | Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 246 | BoolCast); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 247 | return BinaryOperator::CreateAnd(V, OtherOp); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return Changed ? &I : 0; |
| 252 | } |
| 253 | |
Pedro Artigas | c2a08d2 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 254 | // |
| 255 | // Detect pattern: |
| 256 | // |
| 257 | // log2(Y*0.5) |
| 258 | // |
| 259 | // And check for corresponding fast math flags |
| 260 | // |
| 261 | |
| 262 | static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) { |
Pedro Artigas | ef2ef3e | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 263 | |
| 264 | if (!Op->hasOneUse()) |
| 265 | return; |
| 266 | |
| 267 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op); |
| 268 | if (!II) |
| 269 | return; |
| 270 | if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra()) |
| 271 | return; |
| 272 | Log2 = II; |
| 273 | |
| 274 | Value *OpLog2Of = II->getArgOperand(0); |
| 275 | if (!OpLog2Of->hasOneUse()) |
| 276 | return; |
| 277 | |
| 278 | Instruction *I = dyn_cast<Instruction>(OpLog2Of); |
| 279 | if (!I) |
| 280 | return; |
| 281 | if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra()) |
| 282 | return; |
| 283 | |
| 284 | ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(0)); |
| 285 | if (CFP && CFP->isExactlyValue(0.5)) { |
| 286 | Y = I->getOperand(1); |
| 287 | return; |
| 288 | } |
| 289 | CFP = dyn_cast<ConstantFP>(I->getOperand(1)); |
| 290 | if (CFP && CFP->isExactlyValue(0.5)) |
| 291 | Y = I->getOperand(0); |
Pedro Artigas | c2a08d2 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 294 | /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns |
| 295 | /// true iff the given value is FMul or FDiv with one and only one operand |
| 296 | /// being a normal constant (i.e. not Zero/NaN/Infinity). |
| 297 | static bool isFMulOrFDivWithConstant(Value *V) { |
| 298 | Instruction *I = dyn_cast<Instruction>(V); |
| 299 | if (!I || (I->getOpcode() != Instruction::FMul && |
Shuxin Yang | f279731 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 300 | I->getOpcode() != Instruction::FDiv)) |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 301 | return false; |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 302 | |
| 303 | ConstantFP *C0 = dyn_cast<ConstantFP>(I->getOperand(0)); |
| 304 | ConstantFP *C1 = dyn_cast<ConstantFP>(I->getOperand(1)); |
| 305 | |
| 306 | if (C0 && C1) |
| 307 | return false; |
| 308 | |
| 309 | return (C0 && C0->getValueAPF().isNormal()) || |
| 310 | (C1 && C1->getValueAPF().isNormal()); |
| 311 | } |
| 312 | |
| 313 | static bool isNormalFp(const ConstantFP *C) { |
| 314 | const APFloat &Flt = C->getValueAPF(); |
| 315 | return Flt.isNormal() && !Flt.isDenormal(); |
| 316 | } |
| 317 | |
| 318 | /// foldFMulConst() is a helper routine of InstCombiner::visitFMul(). |
| 319 | /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand |
| 320 | /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). |
| 321 | /// This function is to simplify "FMulOrDiv * C" and returns the |
| 322 | /// resulting expression. Note that this function could return NULL in |
| 323 | /// case the constants cannot be folded into a normal floating-point. |
| 324 | /// |
Shuxin Yang | f279731 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 325 | Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, ConstantFP *C, |
| 326 | Instruction *InsertBefore) { |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 327 | assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid"); |
| 328 | |
| 329 | Value *Opnd0 = FMulOrDiv->getOperand(0); |
| 330 | Value *Opnd1 = FMulOrDiv->getOperand(1); |
| 331 | |
| 332 | ConstantFP *C0 = dyn_cast<ConstantFP>(Opnd0); |
| 333 | ConstantFP *C1 = dyn_cast<ConstantFP>(Opnd1); |
| 334 | |
| 335 | BinaryOperator *R = 0; |
| 336 | |
| 337 | // (X * C0) * C => X * (C0*C) |
| 338 | if (FMulOrDiv->getOpcode() == Instruction::FMul) { |
| 339 | Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C); |
| 340 | if (isNormalFp(cast<ConstantFP>(F))) |
| 341 | R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F); |
| 342 | } else { |
| 343 | if (C0) { |
| 344 | // (C0 / X) * C => (C0 * C) / X |
| 345 | ConstantFP *F = cast<ConstantFP>(ConstantExpr::getFMul(C0, C)); |
| 346 | if (isNormalFp(F)) |
| 347 | R = BinaryOperator::CreateFDiv(F, Opnd1); |
| 348 | } else { |
| 349 | // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal |
| 350 | ConstantFP *F = cast<ConstantFP>(ConstantExpr::getFDiv(C, C1)); |
| 351 | if (isNormalFp(F)) { |
| 352 | R = BinaryOperator::CreateFMul(Opnd0, F); |
| 353 | } else { |
| 354 | // (X / C1) * C => X / (C1/C) |
| 355 | Constant *F = ConstantExpr::getFDiv(C1, C); |
| 356 | if (isNormalFp(cast<ConstantFP>(F))) |
| 357 | R = BinaryOperator::CreateFDiv(Opnd0, F); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (R) { |
| 363 | R->setHasUnsafeAlgebra(true); |
| 364 | InsertNewInstWith(R, *InsertBefore); |
| 365 | } |
| 366 | |
| 367 | return R; |
| 368 | } |
| 369 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 370 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 371 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 372 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 373 | |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 374 | if (isa<Constant>(Op0)) |
| 375 | std::swap(Op0, Op1); |
| 376 | |
Michael Ilseman | c244f38 | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 377 | if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), TD)) |
| 378 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 379 | |
Michael Ilseman | c244f38 | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 380 | // Simplify mul instructions with a constant RHS. |
| 381 | if (isa<Constant>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 382 | // Try to fold constant mul into select arguments. |
| 383 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 384 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 385 | return R; |
| 386 | |
| 387 | if (isa<PHINode>(Op0)) |
| 388 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 389 | return NV; |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 390 | |
| 391 | ConstantFP *C = dyn_cast<ConstantFP>(Op1); |
| 392 | if (C && I.hasUnsafeAlgebra() && C->getValueAPF().isNormal()) { |
| 393 | // Let MDC denote an expression in one of these forms: |
| 394 | // X * C, C/X, X/C, where C is a constant. |
| 395 | // |
| 396 | // Try to simplify "MDC * Constant" |
| 397 | if (isFMulOrFDivWithConstant(Op0)) { |
| 398 | Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I); |
| 399 | if (V) |
| 400 | return ReplaceInstUsesWith(I, V); |
| 401 | } |
| 402 | |
| 403 | // (MDC +/- C1) * C2 => (MDC * C2) +/- (C1 * C2) |
| 404 | Instruction *FAddSub = dyn_cast<Instruction>(Op0); |
| 405 | if (FAddSub && |
| 406 | (FAddSub->getOpcode() == Instruction::FAdd || |
| 407 | FAddSub->getOpcode() == Instruction::FSub)) { |
| 408 | Value *Opnd0 = FAddSub->getOperand(0); |
| 409 | Value *Opnd1 = FAddSub->getOperand(1); |
| 410 | ConstantFP *C0 = dyn_cast<ConstantFP>(Opnd0); |
| 411 | ConstantFP *C1 = dyn_cast<ConstantFP>(Opnd1); |
| 412 | bool Swap = false; |
| 413 | if (C0) { |
Shuxin Yang | f279731 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 414 | std::swap(C0, C1); |
| 415 | std::swap(Opnd0, Opnd1); |
| 416 | Swap = true; |
Shuxin Yang | d3ae286 | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (C1 && C1->getValueAPF().isNormal() && |
| 420 | isFMulOrFDivWithConstant(Opnd0)) { |
| 421 | Value *M0 = ConstantExpr::getFMul(C1, C); |
| 422 | Value *M1 = isNormalFp(cast<ConstantFP>(M0)) ? |
| 423 | foldFMulConst(cast<Instruction>(Opnd0), C, &I) : |
| 424 | 0; |
| 425 | if (M0 && M1) { |
| 426 | if (Swap && FAddSub->getOpcode() == Instruction::FSub) |
| 427 | std::swap(M0, M1); |
| 428 | |
| 429 | Value *R = (FAddSub->getOpcode() == Instruction::FAdd) ? |
| 430 | BinaryOperator::CreateFAdd(M0, M1) : |
| 431 | BinaryOperator::CreateFSub(M0, M1); |
| 432 | Instruction *RI = cast<Instruction>(R); |
| 433 | RI->setHasUnsafeAlgebra(true); |
| 434 | return RI; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (Value *Op0v = dyn_castFNegVal(Op0)) // -X * -Y = X*Y |
| 442 | if (Value *Op1v = dyn_castFNegVal(Op1)) |
| 443 | return BinaryOperator::CreateFMul(Op0v, Op1v); |
| 444 | |
Pedro Artigas | 84030dc | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 445 | // Under unsafe algebra do: |
| 446 | // X * log2(0.5*Y) = X*log2(Y) - X |
| 447 | if (I.hasUnsafeAlgebra()) { |
| 448 | Value *OpX = NULL; |
| 449 | Value *OpY = NULL; |
| 450 | IntrinsicInst *Log2; |
Pedro Artigas | c2a08d2 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 451 | detectLog2OfHalf(Op0, OpY, Log2); |
| 452 | if (OpY) { |
| 453 | OpX = Op1; |
| 454 | } else { |
| 455 | detectLog2OfHalf(Op1, OpY, Log2); |
| 456 | if (OpY) { |
| 457 | OpX = Op0; |
Pedro Artigas | 84030dc | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | // if pattern detected emit alternate sequence |
| 461 | if (OpX && OpY) { |
| 462 | Log2->setArgOperand(0, OpY); |
| 463 | Value *FMulVal = Builder->CreateFMul(OpX, Log2); |
Pedro Artigas | c2a08d2 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 464 | Instruction *FMul = cast<Instruction>(FMulVal); |
Pedro Artigas | 84030dc | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 465 | FMul->copyFastMathFlags(Log2); |
| 466 | Instruction *FSub = BinaryOperator::CreateFSub(FMulVal, OpX); |
| 467 | FSub->copyFastMathFlags(Log2); |
| 468 | return FSub; |
| 469 | } |
| 470 | } |
| 471 | |
Shuxin Yang | a5ed031 | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 472 | // X * cond ? 1.0 : 0.0 => cond ? X : 0.0 |
| 473 | if (I.hasNoNaNs() && I.hasNoSignedZeros()) { |
| 474 | Value *V0 = I.getOperand(0); |
| 475 | Value *V1 = I.getOperand(1); |
| 476 | Value *Cond, *SLHS, *SRHS; |
| 477 | bool Match = false; |
| 478 | |
| 479 | if (match(V0, m_Select(m_Value(Cond), m_Value(SLHS), m_Value(SRHS)))) { |
| 480 | Match = true; |
| 481 | } else if (match(V1, m_Select(m_Value(Cond), m_Value(SLHS), |
| 482 | m_Value(SRHS)))) { |
| 483 | Match = true; |
| 484 | std::swap(V0, V1); |
| 485 | } |
| 486 | |
| 487 | if (Match) { |
| 488 | ConstantFP *C0 = dyn_cast<ConstantFP>(SLHS); |
| 489 | ConstantFP *C1 = dyn_cast<ConstantFP>(SRHS); |
| 490 | |
| 491 | if (C0 && C1 && |
| 492 | ((C0->isZero() && C1->isExactlyValue(1.0)) || |
| 493 | (C1->isZero() && C0->isExactlyValue(1.0)))) { |
| 494 | Value *T; |
| 495 | if (C0->isZero()) |
| 496 | T = Builder->CreateSelect(Cond, SLHS, V1); |
| 497 | else |
| 498 | T = Builder->CreateSelect(Cond, V1, SRHS); |
| 499 | return ReplaceInstUsesWith(I, T); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 504 | return Changed ? &I : 0; |
| 505 | } |
| 506 | |
| 507 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
| 508 | /// instruction. |
| 509 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 510 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
| 511 | |
| 512 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 513 | int NonNullOperand = -1; |
| 514 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 515 | if (ST->isNullValue()) |
| 516 | NonNullOperand = 2; |
| 517 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 518 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 519 | if (ST->isNullValue()) |
| 520 | NonNullOperand = 1; |
| 521 | |
| 522 | if (NonNullOperand == -1) |
| 523 | return false; |
| 524 | |
| 525 | Value *SelectCond = SI->getOperand(0); |
| 526 | |
| 527 | // Change the div/rem to use 'Y' instead of the select. |
| 528 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| 529 | |
| 530 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 531 | // problem. However, the select, or the condition of the select may have |
| 532 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 533 | // propagate the known value for the select into other uses of it, and |
| 534 | // propagate a known value of the condition into its other users. |
| 535 | |
| 536 | // If the select and condition only have a single use, don't bother with this, |
| 537 | // early exit. |
| 538 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 539 | return true; |
| 540 | |
| 541 | // Scan the current block backward, looking for other uses of SI. |
| 542 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); |
| 543 | |
| 544 | while (BBI != BBFront) { |
| 545 | --BBI; |
| 546 | // If we found a call to a function, we can't assume it will return, so |
| 547 | // information from below it cannot be propagated above it. |
| 548 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 549 | break; |
| 550 | |
| 551 | // Replace uses of the select or its condition with the known values. |
| 552 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 553 | I != E; ++I) { |
| 554 | if (*I == SI) { |
| 555 | *I = SI->getOperand(NonNullOperand); |
| 556 | Worklist.Add(BBI); |
| 557 | } else if (*I == SelectCond) { |
| 558 | *I = NonNullOperand == 1 ? ConstantInt::getTrue(BBI->getContext()) : |
| 559 | ConstantInt::getFalse(BBI->getContext()); |
| 560 | Worklist.Add(BBI); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // If we past the instruction, quit looking for it. |
| 565 | if (&*BBI == SI) |
| 566 | SI = 0; |
| 567 | if (&*BBI == SelectCond) |
| 568 | SelectCond = 0; |
| 569 | |
| 570 | // If we ran out of things to eliminate, break out of the loop. |
| 571 | if (SelectCond == 0 && SI == 0) |
| 572 | break; |
| 573 | |
| 574 | } |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 579 | /// This function implements the transforms common to both integer division |
| 580 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 581 | /// division instructions. |
| 582 | /// @brief Common integer divide transforms |
| 583 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 584 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 585 | |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 586 | // The RHS is known non-zero. |
| 587 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) { |
| 588 | I.setOperand(1, V); |
| 589 | return &I; |
| 590 | } |
| 591 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 592 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 593 | // This does not apply for fdiv. |
| 594 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 595 | return &I; |
| 596 | |
| 597 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 598 | // (X / C1) / C2 -> X / (C1*C2) |
| 599 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 600 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 601 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 602 | if (MultiplyOverflows(RHS, LHSRHS, |
| 603 | I.getOpcode()==Instruction::SDiv)) |
| 604 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 605 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
| 606 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | if (!RHS->isZero()) { // avoid X udiv 0 |
| 610 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 611 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 612 | return R; |
| 613 | if (isa<PHINode>(Op0)) |
| 614 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 615 | return NV; |
| 616 | } |
| 617 | } |
| 618 | |
Benjamin Kramer | 23b02cd | 2011-04-30 18:16:00 +0000 | [diff] [blame] | 619 | // See if we can fold away this div instruction. |
| 620 | if (SimplifyDemandedInstructionBits(I)) |
| 621 | return &I; |
| 622 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 623 | // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y |
| 624 | Value *X = 0, *Z = 0; |
| 625 | if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 |
| 626 | bool isSigned = I.getOpcode() == Instruction::SDiv; |
| 627 | if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || |
| 628 | (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) |
| 629 | return BinaryOperator::Create(I.getOpcode(), X, Op1); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 635 | /// dyn_castZExtVal - Checks if V is a zext or constant that can |
| 636 | /// be truncated to Ty without losing bits. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 637 | static Value *dyn_castZExtVal(Value *V, Type *Ty) { |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 638 | if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) { |
| 639 | if (Z->getSrcTy() == Ty) |
| 640 | return Z->getOperand(0); |
| 641 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { |
| 642 | if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth()) |
| 643 | return ConstantExpr::getTrunc(C, Ty); |
| 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 648 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 649 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 650 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 651 | if (Value *V = SimplifyUDivInst(Op0, Op1, TD)) |
| 652 | return ReplaceInstUsesWith(I, V); |
| 653 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 654 | // Handle the integer div common cases |
| 655 | if (Instruction *Common = commonIDivTransforms(I)) |
| 656 | return Common; |
Pete Cooper | a29fc80 | 2011-11-07 23:04:49 +0000 | [diff] [blame] | 657 | |
| 658 | { |
Owen Anderson | 5b39620 | 2010-01-17 06:49:03 +0000 | [diff] [blame] | 659 | // X udiv 2^C -> X >> C |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 660 | // Check to see if this is an unsigned division with an exact power of 2, |
| 661 | // if so, convert to a right shift. |
Pete Cooper | a29fc80 | 2011-11-07 23:04:49 +0000 | [diff] [blame] | 662 | const APInt *C; |
| 663 | if (match(Op1, m_Power2(C))) { |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 664 | BinaryOperator *LShr = |
Pete Cooper | a29fc80 | 2011-11-07 23:04:49 +0000 | [diff] [blame] | 665 | BinaryOperator::CreateLShr(Op0, |
| 666 | ConstantInt::get(Op0->getType(), |
| 667 | C->logBase2())); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 668 | if (I.isExact()) LShr->setIsExact(); |
| 669 | return LShr; |
| 670 | } |
Pete Cooper | a29fc80 | 2011-11-07 23:04:49 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 672 | |
Pete Cooper | a29fc80 | 2011-11-07 23:04:49 +0000 | [diff] [blame] | 673 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 674 | // X udiv C, where C >= signbit |
| 675 | if (C->getValue().isNegative()) { |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 676 | Value *IC = Builder->CreateICmpULT(Op0, C); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 677 | return SelectInst::Create(IC, Constant::getNullValue(I.getType()), |
| 678 | ConstantInt::get(I.getType(), 1)); |
| 679 | } |
| 680 | } |
| 681 | |
Benjamin Kramer | c81fe9c | 2012-08-30 15:07:40 +0000 | [diff] [blame] | 682 | // (x lshr C1) udiv C2 --> x udiv (C2 << C1) |
Nadav Rotem | a694e2a | 2012-08-28 12:23:22 +0000 | [diff] [blame] | 683 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(Op1)) { |
Benjamin Kramer | aac7c65 | 2012-08-28 13:08:13 +0000 | [diff] [blame] | 684 | Value *X; |
| 685 | ConstantInt *C1; |
| 686 | if (match(Op0, m_LShr(m_Value(X), m_ConstantInt(C1)))) { |
Benjamin Kramer | 37dca63 | 2012-08-28 13:59:23 +0000 | [diff] [blame] | 687 | APInt NC = C2->getValue().shl(C1->getLimitedValue(C1->getBitWidth()-1)); |
Benjamin Kramer | aac7c65 | 2012-08-28 13:08:13 +0000 | [diff] [blame] | 688 | return BinaryOperator::CreateUDiv(X, Builder->getInt(NC)); |
Nadav Rotem | 9753f0b | 2012-08-28 10:01:43 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 692 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 693 | { const APInt *CI; Value *N; |
Evan Cheng | 2a5422b | 2012-06-21 22:52:49 +0000 | [diff] [blame] | 694 | if (match(Op1, m_Shl(m_Power2(CI), m_Value(N))) || |
| 695 | match(Op1, m_ZExt(m_Shl(m_Power2(CI), m_Value(N))))) { |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 696 | if (*CI != 1) |
Benjamin Kramer | e5bd3cf | 2012-09-21 16:26:41 +0000 | [diff] [blame] | 697 | N = Builder->CreateAdd(N, |
| 698 | ConstantInt::get(N->getType(), CI->logBase2())); |
Evan Cheng | 2a5422b | 2012-06-21 22:52:49 +0000 | [diff] [blame] | 699 | if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1)) |
| 700 | N = Builder->CreateZExt(N, Z->getDestTy()); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 701 | if (I.isExact()) |
| 702 | return BinaryOperator::CreateExactLShr(Op0, N); |
| 703 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 708 | // where C1&C2 are powers of two. |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 709 | { Value *Cond; const APInt *C1, *C2; |
| 710 | if (match(Op1, m_Select(m_Value(Cond), m_Power2(C1), m_Power2(C2)))) { |
| 711 | // Construct the "on true" case of the select |
| 712 | Value *TSI = Builder->CreateLShr(Op0, C1->logBase2(), Op1->getName()+".t", |
| 713 | I.isExact()); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 715 | // Construct the "on false" case of the select |
| 716 | Value *FSI = Builder->CreateLShr(Op0, C2->logBase2(), Op1->getName()+".f", |
| 717 | I.isExact()); |
| 718 | |
| 719 | // construct the select instruction and return it. |
| 720 | return SelectInst::Create(Cond, TSI, FSI); |
| 721 | } |
| 722 | } |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 723 | |
| 724 | // (zext A) udiv (zext B) --> zext (A udiv B) |
| 725 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 726 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
| 727 | return new ZExtInst(Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", |
| 728 | I.isExact()), |
| 729 | I.getType()); |
| 730 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 735 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 736 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 737 | if (Value *V = SimplifySDivInst(Op0, Op1, TD)) |
| 738 | return ReplaceInstUsesWith(I, V); |
| 739 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 740 | // Handle the integer div common cases |
| 741 | if (Instruction *Common = commonIDivTransforms(I)) |
| 742 | return Common; |
| 743 | |
| 744 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 745 | // sdiv X, -1 == -X |
| 746 | if (RHS->isAllOnesValue()) |
| 747 | return BinaryOperator::CreateNeg(Op0); |
| 748 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 749 | // sdiv X, C --> ashr exact X, log2(C) |
| 750 | if (I.isExact() && RHS->getValue().isNonNegative() && |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 751 | RHS->getValue().isPowerOf2()) { |
| 752 | Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), |
| 753 | RHS->getValue().exactLogBase2()); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 754 | return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // -X/C --> X/-C provided the negation doesn't overflow. |
| 758 | if (SubOperator *Sub = dyn_cast<SubOperator>(Op0)) |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 759 | if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap()) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 760 | return BinaryOperator::CreateSDiv(Sub->getOperand(1), |
| 761 | ConstantExpr::getNeg(RHS)); |
| 762 | } |
| 763 | |
| 764 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 765 | // unsigned inputs), turn this into a udiv. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 766 | if (I.getType()->isIntegerTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 767 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 768 | if (MaskedValueIsZero(Op0, Mask)) { |
| 769 | if (MaskedValueIsZero(Op1, Mask)) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 770 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 771 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 772 | } |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 773 | |
| 774 | if (match(Op1, m_Shl(m_Power2(), m_Value()))) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 775 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 776 | // Safe because the only negative value (1 << Y) can take on is |
| 777 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 778 | // the sign bit set. |
| 779 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
Shuxin Yang | 7d72cf8 | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 787 | /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special |
| 788 | /// FP value and: |
| 789 | /// 1) 1/C is exact, or |
| 790 | /// 2) reciprocal is allowed. |
| 791 | /// If the convertion was successful, the simplified expression "X * 1/C" is |
| 792 | /// returned; otherwise, NULL is returned. |
| 793 | /// |
| 794 | static Instruction *CvtFDivConstToReciprocal(Value *Dividend, |
| 795 | ConstantFP *Divisor, |
| 796 | bool AllowReciprocal) { |
| 797 | const APFloat &FpVal = Divisor->getValueAPF(); |
| 798 | APFloat Reciprocal(FpVal.getSemantics()); |
| 799 | bool Cvt = FpVal.getExactInverse(&Reciprocal); |
| 800 | |
| 801 | if (!Cvt && AllowReciprocal && FpVal.isNormal()) { |
| 802 | Reciprocal = APFloat(FpVal.getSemantics(), 1.0f); |
| 803 | (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven); |
| 804 | Cvt = !Reciprocal.isDenormal(); |
| 805 | } |
| 806 | |
| 807 | if (!Cvt) |
| 808 | return 0; |
| 809 | |
| 810 | ConstantFP *R; |
| 811 | R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal); |
| 812 | return BinaryOperator::CreateFMul(Dividend, R); |
| 813 | } |
| 814 | |
Frits van Bommel | 31726c1 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 815 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 816 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 817 | |
| 818 | if (Value *V = SimplifyFDivInst(Op0, Op1, TD)) |
| 819 | return ReplaceInstUsesWith(I, V); |
| 820 | |
Shuxin Yang | 7d72cf8 | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 821 | bool AllowReassociate = I.hasUnsafeAlgebra(); |
| 822 | bool AllowReciprocal = I.hasAllowReciprocal(); |
Benjamin Kramer | 5467396 | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 823 | |
Shuxin Yang | 7d72cf8 | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 824 | if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { |
| 825 | if (AllowReassociate) { |
| 826 | ConstantFP *C1 = 0; |
| 827 | ConstantFP *C2 = Op1C; |
| 828 | Value *X; |
| 829 | Instruction *Res = 0; |
| 830 | |
| 831 | if (match(Op0, m_FMul(m_Value(X), m_ConstantFP(C1)))) { |
| 832 | // (X*C1)/C2 => X * (C1/C2) |
| 833 | // |
| 834 | Constant *C = ConstantExpr::getFDiv(C1, C2); |
| 835 | const APFloat &F = cast<ConstantFP>(C)->getValueAPF(); |
| 836 | if (F.isNormal() && !F.isDenormal()) |
| 837 | Res = BinaryOperator::CreateFMul(X, C); |
| 838 | } else if (match(Op0, m_FDiv(m_Value(X), m_ConstantFP(C1)))) { |
| 839 | // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed] |
| 840 | // |
| 841 | Constant *C = ConstantExpr::getFMul(C1, C2); |
| 842 | const APFloat &F = cast<ConstantFP>(C)->getValueAPF(); |
| 843 | if (F.isNormal() && !F.isDenormal()) { |
| 844 | Res = CvtFDivConstToReciprocal(X, cast<ConstantFP>(C), |
| 845 | AllowReciprocal); |
| 846 | if (!Res) |
| 847 | Res = BinaryOperator::CreateFDiv(X, C); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (Res) { |
| 852 | Res->setFastMathFlags(I.getFastMathFlags()); |
| 853 | return Res; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // X / C => X * 1/C |
| 858 | if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) |
| 859 | return T; |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | if (AllowReassociate && isa<ConstantFP>(Op0)) { |
| 865 | ConstantFP *C1 = cast<ConstantFP>(Op0), *C2; |
| 866 | Constant *Fold = 0; |
| 867 | Value *X; |
| 868 | bool CreateDiv = true; |
| 869 | |
| 870 | // C1 / (X*C2) => (C1/C2) / X |
| 871 | if (match(Op1, m_FMul(m_Value(X), m_ConstantFP(C2)))) |
| 872 | Fold = ConstantExpr::getFDiv(C1, C2); |
| 873 | else if (match(Op1, m_FDiv(m_Value(X), m_ConstantFP(C2)))) { |
| 874 | // C1 / (X/C2) => (C1*C2) / X |
| 875 | Fold = ConstantExpr::getFMul(C1, C2); |
| 876 | } else if (match(Op1, m_FDiv(m_ConstantFP(C2), m_Value(X)))) { |
| 877 | // C1 / (C2/X) => (C1/C2) * X |
| 878 | Fold = ConstantExpr::getFDiv(C1, C2); |
| 879 | CreateDiv = false; |
| 880 | } |
| 881 | |
| 882 | if (Fold) { |
| 883 | const APFloat &FoldC = cast<ConstantFP>(Fold)->getValueAPF(); |
| 884 | if (FoldC.isNormal() && !FoldC.isDenormal()) { |
| 885 | Instruction *R = CreateDiv ? |
| 886 | BinaryOperator::CreateFDiv(Fold, X) : |
| 887 | BinaryOperator::CreateFMul(X, Fold); |
| 888 | R->setFastMathFlags(I.getFastMathFlags()); |
| 889 | return R; |
| 890 | } |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | if (AllowReassociate) { |
| 896 | Value *X, *Y; |
| 897 | Value *NewInst = 0; |
| 898 | Instruction *SimpR = 0; |
| 899 | |
| 900 | if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 901 | // (X/Y) / Z => X / (Y*Z) |
| 902 | // |
| 903 | if (!isa<ConstantFP>(Y) || !isa<ConstantFP>(Op1)) { |
| 904 | NewInst = Builder->CreateFMul(Y, Op1); |
| 905 | SimpR = BinaryOperator::CreateFDiv(X, NewInst); |
| 906 | } |
| 907 | } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 908 | // Z / (X/Y) => Z*Y / X |
| 909 | // |
| 910 | if (!isa<ConstantFP>(Y) || !isa<ConstantFP>(Op0)) { |
| 911 | NewInst = Builder->CreateFMul(Op0, Y); |
| 912 | SimpR = BinaryOperator::CreateFDiv(NewInst, X); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (NewInst) { |
| 917 | if (Instruction *T = dyn_cast<Instruction>(NewInst)) |
| 918 | T->setDebugLoc(I.getDebugLoc()); |
| 919 | SimpR->setFastMathFlags(I.getFastMathFlags()); |
| 920 | return SimpR; |
Benjamin Kramer | 5467396 | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Frits van Bommel | 31726c1 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 927 | /// This function implements the transforms common to both integer remainder |
| 928 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 929 | /// remainder instructions. |
| 930 | /// @brief Common integer remainder transforms |
| 931 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 932 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 933 | |
Chris Lattner | 1add46d | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 934 | // The RHS is known non-zero. |
| 935 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) { |
| 936 | I.setOperand(1, V); |
| 937 | return &I; |
| 938 | } |
| 939 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 940 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 941 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 942 | return &I; |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 943 | |
Duncan Sands | 00676a6 | 2011-05-02 18:41:29 +0000 | [diff] [blame] | 944 | if (isa<ConstantInt>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 945 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 946 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 947 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 948 | return R; |
| 949 | } else if (isa<PHINode>(Op0I)) { |
| 950 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 951 | return NV; |
| 952 | } |
| 953 | |
| 954 | // See if we can fold away this rem instruction. |
| 955 | if (SimplifyDemandedInstructionBits(I)) |
| 956 | return &I; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 964 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 965 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 966 | if (Value *V = SimplifyURemInst(Op0, Op1, TD)) |
| 967 | return ReplaceInstUsesWith(I, V); |
| 968 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 969 | if (Instruction *common = commonIRemTransforms(I)) |
| 970 | return common; |
| 971 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 972 | // X urem C^2 -> X and C-1 |
| 973 | { const APInt *C; |
| 974 | if (match(Op1, m_Power2(C))) |
| 975 | return BinaryOperator::CreateAnd(Op0, |
| 976 | ConstantInt::get(I.getType(), *C-1)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 979 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 980 | if (match(Op1, m_Shl(m_Power2(), m_Value()))) { |
| 981 | Constant *N1 = Constant::getAllOnesValue(I.getType()); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 982 | Value *Add = Builder->CreateAdd(Op1, N1); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 983 | return BinaryOperator::CreateAnd(Op0, Add); |
| 984 | } |
| 985 | |
| 986 | // urem X, (select Cond, 2^C1, 2^C2) --> |
| 987 | // select Cond, (and X, C1-1), (and X, C2-1) |
| 988 | // when C1&C2 are powers of two. |
| 989 | { Value *Cond; const APInt *C1, *C2; |
| 990 | if (match(Op1, m_Select(m_Value(Cond), m_Power2(C1), m_Power2(C2)))) { |
| 991 | Value *TrueAnd = Builder->CreateAnd(Op0, *C1-1, Op1->getName()+".t"); |
| 992 | Value *FalseAnd = Builder->CreateAnd(Op0, *C2-1, Op1->getName()+".f"); |
| 993 | return SelectInst::Create(Cond, TrueAnd, FalseAnd); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 996 | |
| 997 | // (zext A) urem (zext B) --> zext (A urem B) |
| 998 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 999 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
| 1000 | return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1), |
| 1001 | I.getType()); |
| 1002 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 1007 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1008 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1009 | if (Value *V = SimplifySRemInst(Op0, Op1, TD)) |
| 1010 | return ReplaceInstUsesWith(I, V); |
| 1011 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1012 | // Handle the integer rem common cases |
| 1013 | if (Instruction *Common = commonIRemTransforms(I)) |
| 1014 | return Common; |
| 1015 | |
| 1016 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 1017 | if (!isa<Constant>(RHSNeg) || |
| 1018 | (isa<ConstantInt>(RHSNeg) && |
| 1019 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { |
| 1020 | // X % -Y -> X % Y |
| 1021 | Worklist.AddValue(I.getOperand(1)); |
| 1022 | I.setOperand(1, RHSNeg); |
| 1023 | return &I; |
| 1024 | } |
| 1025 | |
| 1026 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1027 | // unsigned inputs), turn this into a urem. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1028 | if (I.getType()->isIntegerTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1029 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 1030 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1031 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1032 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // If it's a constant vector, flip any negative values positive. |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1037 | if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { |
| 1038 | Constant *C = cast<Constant>(Op1); |
| 1039 | unsigned VWidth = C->getType()->getVectorNumElements(); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1040 | |
| 1041 | bool hasNegative = false; |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1042 | bool hasMissing = false; |
| 1043 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1044 | Constant *Elt = C->getAggregateElement(i); |
| 1045 | if (Elt == 0) { |
| 1046 | hasMissing = true; |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1051 | if (RHS->isNegative()) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1052 | hasNegative = true; |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1053 | } |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1055 | if (hasNegative && !hasMissing) { |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1056 | SmallVector<Constant *, 16> Elts(VWidth); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1057 | for (unsigned i = 0; i != VWidth; ++i) { |
Chris Lattner | 7302d80 | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1058 | Elts[i] = C->getAggregateElement(i); // Handle undef, etc. |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1059 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1060 | if (RHS->isNegative()) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1061 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | Constant *NewRHSV = ConstantVector::get(Elts); |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1066 | if (NewRHSV != C) { // Don't loop on -MININT |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1067 | Worklist.AddValue(I.getOperand(1)); |
| 1068 | I.setOperand(1, NewRHSV); |
| 1069 | return &I; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1078 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1079 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1080 | if (Value *V = SimplifyFRemInst(Op0, Op1, TD)) |
| 1081 | return ReplaceInstUsesWith(I, V); |
| 1082 | |
| 1083 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 1084 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 1085 | return &I; |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |