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" |
| 16 | #include "llvm/IntrinsicInst.h" |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.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 | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 22 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 23 | /// this size. |
| 24 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 25 | uint32_t W = C1->getBitWidth(); |
| 26 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 27 | if (sign) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 28 | LHSExt = LHSExt.sext(W * 2); |
| 29 | RHSExt = RHSExt.sext(W * 2); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 30 | } else { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 31 | LHSExt = LHSExt.zext(W * 2); |
| 32 | RHSExt = RHSExt.zext(W * 2); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | APInt MulExt = LHSExt * RHSExt; |
| 36 | |
| 37 | if (!sign) |
| 38 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 39 | |
| 40 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 41 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 42 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 43 | } |
| 44 | |
| 45 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 46 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 47 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 48 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 49 | if (Value *V = SimplifyMulInst(Op0, Op1, TD)) |
| 50 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 51 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 52 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 53 | return ReplaceInstUsesWith(I, V); |
| 54 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 55 | if (match(Op1, m_AllOnes())) // X * -1 == 0 - X |
| 56 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
| 57 | |
| 58 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 59 | |
| 60 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 61 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
| 62 | if (SI->getOpcode() == Instruction::Shl) |
| 63 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
| 64 | return BinaryOperator::CreateMul(SI->getOperand(0), |
| 65 | ConstantExpr::getShl(CI, ShOp)); |
| 66 | |
| 67 | const APInt &Val = CI->getValue(); |
| 68 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
| 69 | Constant *NewCst = ConstantInt::get(Op0->getType(), Val.logBase2()); |
| 70 | BinaryOperator *Shl = BinaryOperator::CreateShl(Op0, NewCst); |
| 71 | if (I.hasNoSignedWrap()) Shl->setHasNoSignedWrap(); |
| 72 | if (I.hasNoUnsignedWrap()) Shl->setHasNoUnsignedWrap(); |
| 73 | return Shl; |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 76 | // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |
| 77 | { Value *X; ConstantInt *C1; |
| 78 | if (Op0->hasOneUse() && |
| 79 | match(Op0, m_Add(m_Value(X), m_ConstantInt(C1)))) { |
| 80 | Value *Add = Builder->CreateMul(X, CI, "tmp"); |
| 81 | return BinaryOperator::CreateAdd(Add, Builder->CreateMul(C1, CI)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | // Simplify mul instructions with a constant RHS. |
| 87 | if (isa<Constant>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 88 | // Try to fold constant mul into select arguments. |
| 89 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 90 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 91 | return R; |
| 92 | |
| 93 | if (isa<PHINode>(Op0)) |
| 94 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 95 | return NV; |
| 96 | } |
| 97 | |
| 98 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 99 | if (Value *Op1v = dyn_castNegVal(Op1)) |
| 100 | return BinaryOperator::CreateMul(Op0v, Op1v); |
| 101 | |
| 102 | // (X / Y) * Y = X - (X % Y) |
| 103 | // (X / Y) * -Y = (X % Y) - X |
| 104 | { |
| 105 | Value *Op1C = Op1; |
| 106 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); |
| 107 | if (!BO || |
| 108 | (BO->getOpcode() != Instruction::UDiv && |
| 109 | BO->getOpcode() != Instruction::SDiv)) { |
| 110 | Op1C = Op0; |
| 111 | BO = dyn_cast<BinaryOperator>(Op1); |
| 112 | } |
| 113 | Value *Neg = dyn_castNegVal(Op1C); |
| 114 | if (BO && BO->hasOneUse() && |
| 115 | (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) && |
| 116 | (BO->getOpcode() == Instruction::UDiv || |
| 117 | BO->getOpcode() == Instruction::SDiv)) { |
| 118 | Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); |
| 119 | |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 120 | // If the division is exact, X % Y is zero, so we end up with X or -X. |
| 121 | if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO)) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 122 | if (SDiv->isExact()) { |
| 123 | if (Op1BO == Op1C) |
| 124 | return ReplaceInstUsesWith(I, Op0BO); |
| 125 | return BinaryOperator::CreateNeg(Op0BO); |
| 126 | } |
| 127 | |
| 128 | Value *Rem; |
| 129 | if (BO->getOpcode() == Instruction::UDiv) |
| 130 | Rem = Builder->CreateURem(Op0BO, Op1BO); |
| 131 | else |
| 132 | Rem = Builder->CreateSRem(Op0BO, Op1BO); |
| 133 | Rem->takeName(BO); |
| 134 | |
| 135 | if (Op1BO == Op1C) |
| 136 | return BinaryOperator::CreateSub(Op0BO, Rem); |
| 137 | return BinaryOperator::CreateSub(Rem, Op0BO); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// i1 mul -> i1 and. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 142 | if (I.getType()->isIntegerTy(1)) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 143 | return BinaryOperator::CreateAnd(Op0, Op1); |
| 144 | |
| 145 | // X*(1 << Y) --> X << Y |
| 146 | // (1 << Y)*X --> X << Y |
| 147 | { |
| 148 | Value *Y; |
| 149 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) |
| 150 | return BinaryOperator::CreateShl(Op1, Y); |
| 151 | if (match(Op1, m_Shl(m_One(), m_Value(Y)))) |
| 152 | return BinaryOperator::CreateShl(Op0, Y); |
| 153 | } |
| 154 | |
| 155 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 156 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 157 | // X * Y (where Y is 0 or 1) -> X & (0-Y) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 158 | if (!I.getType()->isVectorTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 159 | // -2 is "-1 << 1" so it is all bits set except the low one. |
| 160 | APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); |
| 161 | |
| 162 | Value *BoolCast = 0, *OtherOp = 0; |
| 163 | if (MaskedValueIsZero(Op0, Negative2)) |
| 164 | BoolCast = Op0, OtherOp = Op1; |
| 165 | else if (MaskedValueIsZero(Op1, Negative2)) |
| 166 | BoolCast = Op1, OtherOp = Op0; |
| 167 | |
| 168 | if (BoolCast) { |
| 169 | Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), |
| 170 | BoolCast, "tmp"); |
| 171 | return BinaryOperator::CreateAnd(V, OtherOp); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return Changed ? &I : 0; |
| 176 | } |
| 177 | |
| 178 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 179 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 180 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 181 | |
| 182 | // Simplify mul instructions with a constant RHS... |
| 183 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 184 | if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1C)) { |
| 185 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 186 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 187 | if (Op1F->isExactlyValue(1.0)) |
Dan Gohman | a9445e1 | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 188 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'fmul double %X, 1.0' |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 189 | } else if (Op1C->getType()->isVectorTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 190 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1C)) { |
| 191 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 192 | if (Constant *Splat = Op1V->getSplatValue()) { |
| 193 | if (ConstantFP *F = dyn_cast<ConstantFP>(Splat)) |
| 194 | if (F->isExactlyValue(1.0)) |
| 195 | return ReplaceInstUsesWith(I, Op0); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Try to fold constant mul into select arguments. |
| 201 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 202 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 203 | return R; |
| 204 | |
| 205 | if (isa<PHINode>(Op0)) |
| 206 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 207 | return NV; |
| 208 | } |
| 209 | |
| 210 | if (Value *Op0v = dyn_castFNegVal(Op0)) // -X * -Y = X*Y |
| 211 | if (Value *Op1v = dyn_castFNegVal(Op1)) |
| 212 | return BinaryOperator::CreateFMul(Op0v, Op1v); |
| 213 | |
| 214 | return Changed ? &I : 0; |
| 215 | } |
| 216 | |
| 217 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
| 218 | /// instruction. |
| 219 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 220 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
| 221 | |
| 222 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 223 | int NonNullOperand = -1; |
| 224 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 225 | if (ST->isNullValue()) |
| 226 | NonNullOperand = 2; |
| 227 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 228 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 229 | if (ST->isNullValue()) |
| 230 | NonNullOperand = 1; |
| 231 | |
| 232 | if (NonNullOperand == -1) |
| 233 | return false; |
| 234 | |
| 235 | Value *SelectCond = SI->getOperand(0); |
| 236 | |
| 237 | // Change the div/rem to use 'Y' instead of the select. |
| 238 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| 239 | |
| 240 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 241 | // problem. However, the select, or the condition of the select may have |
| 242 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 243 | // propagate the known value for the select into other uses of it, and |
| 244 | // propagate a known value of the condition into its other users. |
| 245 | |
| 246 | // If the select and condition only have a single use, don't bother with this, |
| 247 | // early exit. |
| 248 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 249 | return true; |
| 250 | |
| 251 | // Scan the current block backward, looking for other uses of SI. |
| 252 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); |
| 253 | |
| 254 | while (BBI != BBFront) { |
| 255 | --BBI; |
| 256 | // If we found a call to a function, we can't assume it will return, so |
| 257 | // information from below it cannot be propagated above it. |
| 258 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 259 | break; |
| 260 | |
| 261 | // Replace uses of the select or its condition with the known values. |
| 262 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 263 | I != E; ++I) { |
| 264 | if (*I == SI) { |
| 265 | *I = SI->getOperand(NonNullOperand); |
| 266 | Worklist.Add(BBI); |
| 267 | } else if (*I == SelectCond) { |
| 268 | *I = NonNullOperand == 1 ? ConstantInt::getTrue(BBI->getContext()) : |
| 269 | ConstantInt::getFalse(BBI->getContext()); |
| 270 | Worklist.Add(BBI); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // If we past the instruction, quit looking for it. |
| 275 | if (&*BBI == SI) |
| 276 | SI = 0; |
| 277 | if (&*BBI == SelectCond) |
| 278 | SelectCond = 0; |
| 279 | |
| 280 | // If we ran out of things to eliminate, break out of the loop. |
| 281 | if (SelectCond == 0 && SI == 0) |
| 282 | break; |
| 283 | |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 289 | /// This function implements the transforms common to both integer division |
| 290 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 291 | /// division instructions. |
| 292 | /// @brief Common integer divide transforms |
| 293 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 294 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 295 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 296 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 297 | // This does not apply for fdiv. |
| 298 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 299 | return &I; |
| 300 | |
| 301 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 302 | // (X / C1) / C2 -> X / (C1*C2) |
| 303 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 304 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 305 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 306 | if (MultiplyOverflows(RHS, LHSRHS, |
| 307 | I.getOpcode()==Instruction::SDiv)) |
| 308 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 309 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
| 310 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (!RHS->isZero()) { // avoid X udiv 0 |
| 314 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 315 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 316 | return R; |
| 317 | if (isa<PHINode>(Op0)) |
| 318 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 319 | return NV; |
| 320 | } |
| 321 | } |
| 322 | |
Benjamin Kramer | 23b02cd | 2011-04-30 18:16:00 +0000 | [diff] [blame] | 323 | // See if we can fold away this div instruction. |
| 324 | if (SimplifyDemandedInstructionBits(I)) |
| 325 | return &I; |
| 326 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 327 | // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y |
| 328 | Value *X = 0, *Z = 0; |
| 329 | if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 |
| 330 | bool isSigned = I.getOpcode() == Instruction::SDiv; |
| 331 | if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || |
| 332 | (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) |
| 333 | return BinaryOperator::Create(I.getOpcode(), X, Op1); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 339 | /// dyn_castZExtVal - Checks if V is a zext or constant that can |
| 340 | /// be truncated to Ty without losing bits. |
| 341 | static Value *dyn_castZExtVal(Value *V, const Type *Ty) { |
| 342 | if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) { |
| 343 | if (Z->getSrcTy() == Ty) |
| 344 | return Z->getOperand(0); |
| 345 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { |
| 346 | if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth()) |
| 347 | return ConstantExpr::getTrunc(C, Ty); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 352 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 353 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 354 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 355 | if (Value *V = SimplifyUDivInst(Op0, Op1, TD)) |
| 356 | return ReplaceInstUsesWith(I, V); |
| 357 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 358 | // Handle the integer div common cases |
| 359 | if (Instruction *Common = commonIDivTransforms(I)) |
| 360 | return Common; |
| 361 | |
| 362 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Owen Anderson | 5b39620 | 2010-01-17 06:49:03 +0000 | [diff] [blame] | 363 | // X udiv 2^C -> X >> C |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 364 | // Check to see if this is an unsigned division with an exact power of 2, |
| 365 | // if so, convert to a right shift. |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 366 | if (C->getValue().isPowerOf2()) { // 0 not included in isPowerOf2 |
| 367 | BinaryOperator *LShr = |
| 368 | BinaryOperator::CreateLShr(Op0, |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 369 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 370 | if (I.isExact()) LShr->setIsExact(); |
| 371 | return LShr; |
| 372 | } |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 373 | |
| 374 | // X udiv C, where C >= signbit |
| 375 | if (C->getValue().isNegative()) { |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 376 | Value *IC = Builder->CreateICmpULT(Op0, C); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 377 | return SelectInst::Create(IC, Constant::getNullValue(I.getType()), |
| 378 | ConstantInt::get(I.getType(), 1)); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 383 | { const APInt *CI; Value *N; |
| 384 | if (match(Op1, m_Shl(m_Power2(CI), m_Value(N)))) { |
| 385 | if (*CI != 1) |
| 386 | N = Builder->CreateAdd(N, ConstantInt::get(I.getType(), CI->logBase2()), |
| 387 | "tmp"); |
| 388 | if (I.isExact()) |
| 389 | return BinaryOperator::CreateExactLShr(Op0, N); |
| 390 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 395 | // where C1&C2 are powers of two. |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 396 | { Value *Cond; const APInt *C1, *C2; |
| 397 | if (match(Op1, m_Select(m_Value(Cond), m_Power2(C1), m_Power2(C2)))) { |
| 398 | // Construct the "on true" case of the select |
| 399 | Value *TSI = Builder->CreateLShr(Op0, C1->logBase2(), Op1->getName()+".t", |
| 400 | I.isExact()); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 402 | // Construct the "on false" case of the select |
| 403 | Value *FSI = Builder->CreateLShr(Op0, C2->logBase2(), Op1->getName()+".f", |
| 404 | I.isExact()); |
| 405 | |
| 406 | // construct the select instruction and return it. |
| 407 | return SelectInst::Create(Cond, TSI, FSI); |
| 408 | } |
| 409 | } |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 410 | |
| 411 | // (zext A) udiv (zext B) --> zext (A udiv B) |
| 412 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 413 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
| 414 | return new ZExtInst(Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", |
| 415 | I.isExact()), |
| 416 | I.getType()); |
| 417 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 422 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 423 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 424 | if (Value *V = SimplifySDivInst(Op0, Op1, TD)) |
| 425 | return ReplaceInstUsesWith(I, V); |
| 426 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 427 | // Handle the integer div common cases |
| 428 | if (Instruction *Common = commonIDivTransforms(I)) |
| 429 | return Common; |
| 430 | |
| 431 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 432 | // sdiv X, -1 == -X |
| 433 | if (RHS->isAllOnesValue()) |
| 434 | return BinaryOperator::CreateNeg(Op0); |
| 435 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 436 | // sdiv X, C --> ashr exact X, log2(C) |
| 437 | if (I.isExact() && RHS->getValue().isNonNegative() && |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 438 | RHS->getValue().isPowerOf2()) { |
| 439 | Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), |
| 440 | RHS->getValue().exactLogBase2()); |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 441 | return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // -X/C --> X/-C provided the negation doesn't overflow. |
| 445 | if (SubOperator *Sub = dyn_cast<SubOperator>(Op0)) |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 446 | if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap()) |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 447 | return BinaryOperator::CreateSDiv(Sub->getOperand(1), |
| 448 | ConstantExpr::getNeg(RHS)); |
| 449 | } |
| 450 | |
| 451 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 452 | // unsigned inputs), turn this into a udiv. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 453 | if (I.getType()->isIntegerTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 454 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 455 | if (MaskedValueIsZero(Op0, Mask)) { |
| 456 | if (MaskedValueIsZero(Op1, Mask)) { |
| 457 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 458 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 459 | } |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 460 | |
| 461 | if (match(Op1, m_Shl(m_Power2(), m_Value()))) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 462 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 463 | // Safe because the only negative value (1 << Y) can take on is |
| 464 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 465 | // the sign bit set. |
| 466 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Frits van Bommel | 31726c1 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 474 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 475 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 476 | |
| 477 | if (Value *V = SimplifyFDivInst(Op0, Op1, TD)) |
| 478 | return ReplaceInstUsesWith(I, V); |
| 479 | |
Benjamin Kramer | 5467396 | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 480 | if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { |
| 481 | const APFloat &Op1F = Op1C->getValueAPF(); |
| 482 | |
| 483 | // If the divisor has an exact multiplicative inverse we can turn the fdiv |
| 484 | // into a cheaper fmul. |
| 485 | APFloat Reciprocal(Op1F.getSemantics()); |
| 486 | if (Op1F.getExactInverse(&Reciprocal)) { |
| 487 | ConstantFP *RFP = ConstantFP::get(Builder->getContext(), Reciprocal); |
| 488 | return BinaryOperator::CreateFMul(Op0, RFP); |
| 489 | } |
| 490 | } |
| 491 | |
Frits van Bommel | 31726c1 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 495 | /// This function implements the transforms common to both integer remainder |
| 496 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 497 | /// remainder instructions. |
| 498 | /// @brief Common integer remainder transforms |
| 499 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 500 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 501 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame^] | 502 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 503 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 504 | return &I; |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 505 | |
| 506 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 507 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 508 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 509 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 510 | return R; |
| 511 | } else if (isa<PHINode>(Op0I)) { |
| 512 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 513 | return NV; |
| 514 | } |
| 515 | |
| 516 | // See if we can fold away this rem instruction. |
| 517 | if (SimplifyDemandedInstructionBits(I)) |
| 518 | return &I; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 526 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 527 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame^] | 528 | if (Value *V = SimplifyURemInst(Op0, Op1, TD)) |
| 529 | return ReplaceInstUsesWith(I, V); |
| 530 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 531 | if (Instruction *common = commonIRemTransforms(I)) |
| 532 | return common; |
| 533 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 534 | // X urem C^2 -> X and C-1 |
| 535 | { const APInt *C; |
| 536 | if (match(Op1, m_Power2(C))) |
| 537 | return BinaryOperator::CreateAnd(Op0, |
| 538 | ConstantInt::get(I.getType(), *C-1)); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | 7a6aa1a | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 541 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 542 | if (match(Op1, m_Shl(m_Power2(), m_Value()))) { |
| 543 | Constant *N1 = Constant::getAllOnesValue(I.getType()); |
| 544 | Value *Add = Builder->CreateAdd(Op1, N1, "tmp"); |
| 545 | return BinaryOperator::CreateAnd(Op0, Add); |
| 546 | } |
| 547 | |
| 548 | // urem X, (select Cond, 2^C1, 2^C2) --> |
| 549 | // select Cond, (and X, C1-1), (and X, C2-1) |
| 550 | // when C1&C2 are powers of two. |
| 551 | { Value *Cond; const APInt *C1, *C2; |
| 552 | if (match(Op1, m_Select(m_Value(Cond), m_Power2(C1), m_Power2(C2)))) { |
| 553 | Value *TrueAnd = Builder->CreateAnd(Op0, *C1-1, Op1->getName()+".t"); |
| 554 | Value *FalseAnd = Builder->CreateAnd(Op0, *C2-1, Op1->getName()+".f"); |
| 555 | return SelectInst::Create(Cond, TrueAnd, FalseAnd); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Benjamin Kramer | 7d6eb5a | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 558 | |
| 559 | // (zext A) urem (zext B) --> zext (A urem B) |
| 560 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 561 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
| 562 | return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1), |
| 563 | I.getType()); |
| 564 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 569 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 570 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame^] | 571 | if (Value *V = SimplifySRemInst(Op0, Op1, TD)) |
| 572 | return ReplaceInstUsesWith(I, V); |
| 573 | |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 574 | // Handle the integer rem common cases |
| 575 | if (Instruction *Common = commonIRemTransforms(I)) |
| 576 | return Common; |
| 577 | |
| 578 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 579 | if (!isa<Constant>(RHSNeg) || |
| 580 | (isa<ConstantInt>(RHSNeg) && |
| 581 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { |
| 582 | // X % -Y -> X % Y |
| 583 | Worklist.AddValue(I.getOperand(1)); |
| 584 | I.setOperand(1, RHSNeg); |
| 585 | return &I; |
| 586 | } |
| 587 | |
| 588 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 589 | // unsigned inputs), turn this into a urem. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 590 | if (I.getType()->isIntegerTy()) { |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 591 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 592 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 593 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 594 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // If it's a constant vector, flip any negative values positive. |
| 599 | if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) { |
| 600 | unsigned VWidth = RHSV->getNumOperands(); |
| 601 | |
| 602 | bool hasNegative = false; |
| 603 | for (unsigned i = 0; !hasNegative && i != VWidth; ++i) |
| 604 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) |
| 605 | if (RHS->getValue().isNegative()) |
| 606 | hasNegative = true; |
| 607 | |
| 608 | if (hasNegative) { |
| 609 | std::vector<Constant *> Elts(VWidth); |
| 610 | for (unsigned i = 0; i != VWidth; ++i) { |
| 611 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) { |
| 612 | if (RHS->getValue().isNegative()) |
| 613 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
| 614 | else |
| 615 | Elts[i] = RHS; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | Constant *NewRHSV = ConstantVector::get(Elts); |
| 620 | if (NewRHSV != RHSV) { |
| 621 | Worklist.AddValue(I.getOperand(1)); |
| 622 | I.setOperand(1, NewRHSV); |
| 623 | return &I; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame^] | 632 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 633 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame^] | 634 | if (Value *V = SimplifyFRemInst(Op0, Op1, TD)) |
| 635 | return ReplaceInstUsesWith(I, V); |
| 636 | |
| 637 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 638 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 639 | return &I; |
| 640 | |
| 641 | return 0; |
| 642 | } |