blob: 160792b0a0000cf1876394c9f7630ffc38e4c5a0 [file] [log] [blame]
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001//===- 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
Chandler Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
Duncan Sandsd0eb6d32010-12-21 14:00:22 +000016#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattnerdc054bf2010-01-05 06:09:35 +000019using namespace llvm;
20using namespace PatternMatch;
21
Chandler Carruth964daaa2014-04-22 02:55:47 +000022#define DEBUG_TYPE "instcombine"
23
Chris Lattner7c99f192011-05-22 18:18:41 +000024
Sanjay Patel6eccf482015-09-09 15:24:36 +000025/// The specific integer value is used in a context where it is known to be
26/// non-zero. If this allows us to simplify the computation, do so and return
27/// the new operand, otherwise return null.
Hal Finkel60db0582014-09-07 18:57:58 +000028static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000029 Instruction &CxtI) {
Chris Lattner7c99f192011-05-22 18:18:41 +000030 // If V has multiple uses, then we would have to do more analysis to determine
31 // if this is safe. For example, the use could be in dynamically unreached
32 // code.
Craig Topperf40110f2014-04-25 05:29:35 +000033 if (!V->hasOneUse()) return nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +000034
Chris Lattner388cb8a2011-05-23 00:32:19 +000035 bool MadeChange = false;
36
Chris Lattner7c99f192011-05-22 18:18:41 +000037 // ((1 << A) >>u B) --> (1 << (A-B))
38 // Because V cannot be zero, we know that B is less than A.
David Majnemerdad21032014-10-14 20:28:40 +000039 Value *A = nullptr, *B = nullptr, *One = nullptr;
40 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) &&
41 match(One, m_One())) {
Benjamin Kramer547b6c52011-09-27 20:39:19 +000042 A = IC.Builder->CreateSub(A, B);
David Majnemerdad21032014-10-14 20:28:40 +000043 return IC.Builder->CreateShl(One, A);
Chris Lattner7c99f192011-05-22 18:18:41 +000044 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +000045
Chris Lattner388cb8a2011-05-23 00:32:19 +000046 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
47 // inexact. Similarly for <<.
48 if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
Chandler Carruth66b31302015-01-04 12:03:27 +000049 if (I->isLogicalShift() &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +000050 isKnownToBeAPowerOfTwo(I->getOperand(0), IC.getDataLayout(), false, 0,
51 IC.getAssumptionCache(), &CxtI,
Chandler Carruth66b31302015-01-04 12:03:27 +000052 IC.getDominatorTree())) {
Chris Lattner388cb8a2011-05-23 00:32:19 +000053 // We know that this is an exact/nuw shift and that the input is a
54 // non-zero context as well.
Hal Finkel60db0582014-09-07 18:57:58 +000055 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
Chris Lattner388cb8a2011-05-23 00:32:19 +000056 I->setOperand(0, V2);
57 MadeChange = true;
58 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +000059
Chris Lattner388cb8a2011-05-23 00:32:19 +000060 if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
61 I->setIsExact();
62 MadeChange = true;
63 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +000064
Chris Lattner388cb8a2011-05-23 00:32:19 +000065 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
66 I->setHasNoUnsignedWrap();
67 MadeChange = true;
68 }
69 }
70
Chris Lattner162dfc32011-05-22 18:26:48 +000071 // TODO: Lots more we could do here:
Chris Lattner162dfc32011-05-22 18:26:48 +000072 // If V is a phi node, we can call this on each of its operands.
73 // "select cond, X, 0" can simplify to "X".
Jim Grosbachbdbd7342013-04-05 21:20:12 +000074
Craig Topperf40110f2014-04-25 05:29:35 +000075 return MadeChange ? V : nullptr;
Chris Lattner7c99f192011-05-22 18:18:41 +000076}
77
78
Sanjay Patel6eccf482015-09-09 15:24:36 +000079/// True if the multiply can not be expressed in an int this size.
David Majnemer27adb122014-10-12 08:34:24 +000080static bool MultiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
81 bool IsSigned) {
82 bool Overflow;
83 if (IsSigned)
84 Product = C1.smul_ov(C2, Overflow);
85 else
86 Product = C1.umul_ov(C2, Overflow);
Jim Grosbachbdbd7342013-04-05 21:20:12 +000087
David Majnemer27adb122014-10-12 08:34:24 +000088 return Overflow;
Chris Lattnerdc054bf2010-01-05 06:09:35 +000089}
90
David Majnemerf9a095d2014-08-16 08:55:06 +000091/// \brief True if C2 is a multiple of C1. Quotient contains C2/C1.
92static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
93 bool IsSigned) {
94 assert(C1.getBitWidth() == C2.getBitWidth() &&
95 "Inconsistent width of constants!");
96
David Majnemer135ca402015-09-06 06:49:59 +000097 // Bail if we will divide by zero.
98 if (C2.isMinValue())
99 return false;
100
101 // Bail if we would divide INT_MIN by -1.
102 if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue())
103 return false;
104
David Majnemerf9a095d2014-08-16 08:55:06 +0000105 APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
106 if (IsSigned)
107 APInt::sdivrem(C1, C2, Quotient, Remainder);
108 else
109 APInt::udivrem(C1, C2, Quotient, Remainder);
110
111 return Remainder.isMinValue();
112}
113
Rafael Espindola65281bf2013-05-31 14:27:15 +0000114/// \brief A helper routine of InstCombiner::visitMul().
115///
116/// If C is a vector of known powers of 2, then this function returns
117/// a new vector obtained from C replacing each element with its logBase2.
118/// Return a null pointer otherwise.
119static Constant *getLogBase2Vector(ConstantDataVector *CV) {
120 const APInt *IVal;
121 SmallVector<Constant *, 4> Elts;
122
123 for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
124 Constant *Elt = CV->getElementAsConstant(I);
125 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
Craig Topperf40110f2014-04-25 05:29:35 +0000126 return nullptr;
Rafael Espindola65281bf2013-05-31 14:27:15 +0000127 Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2()));
128 }
129
130 return ConstantVector::get(Elts);
131}
132
David Majnemer54c2ca22014-12-26 09:10:14 +0000133/// \brief Return true if we can prove that:
134/// (mul LHS, RHS) === (mul nsw LHS, RHS)
135bool InstCombiner::WillNotOverflowSignedMul(Value *LHS, Value *RHS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000136 Instruction &CxtI) {
David Majnemer54c2ca22014-12-26 09:10:14 +0000137 // Multiplying n * m significant bits yields a result of n + m significant
138 // bits. If the total number of significant bits does not exceed the
139 // result bit width (minus 1), there is no overflow.
140 // This means if we have enough leading sign bits in the operands
141 // we can guarantee that the result does not overflow.
142 // Ref: "Hacker's Delight" by Henry Warren
143 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
144
145 // Note that underestimating the number of sign bits gives a more
146 // conservative answer.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000147 unsigned SignBits =
148 ComputeNumSignBits(LHS, 0, &CxtI) + ComputeNumSignBits(RHS, 0, &CxtI);
David Majnemer54c2ca22014-12-26 09:10:14 +0000149
150 // First handle the easy case: if we have enough sign bits there's
151 // definitely no overflow.
152 if (SignBits > BitWidth + 1)
153 return true;
154
155 // There are two ambiguous cases where there can be no overflow:
156 // SignBits == BitWidth + 1 and
157 // SignBits == BitWidth
158 // The second case is difficult to check, therefore we only handle the
159 // first case.
160 if (SignBits == BitWidth + 1) {
161 // It overflows only when both arguments are negative and the true
162 // product is exactly the minimum negative number.
163 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
164 // For simplicity we just check if at least one side is not negative.
165 bool LHSNonNegative, LHSNegative;
166 bool RHSNonNegative, RHSNegative;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000167 ComputeSignBit(LHS, LHSNonNegative, LHSNegative, /*Depth=*/0, &CxtI);
168 ComputeSignBit(RHS, RHSNonNegative, RHSNegative, /*Depth=*/0, &CxtI);
David Majnemer54c2ca22014-12-26 09:10:14 +0000169 if (LHSNonNegative || RHSNonNegative)
170 return true;
171 }
172 return false;
173}
174
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000175Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +0000176 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000177 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
178
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000179 if (Value *V = SimplifyVectorOp(I))
180 return ReplaceInstUsesWith(I, V);
181
Chandler Carruth66b31302015-01-04 12:03:27 +0000182 if (Value *V = SimplifyMulInst(Op0, Op1, DL, TLI, DT, AC))
Duncan Sandsd0eb6d32010-12-21 14:00:22 +0000183 return ReplaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000184
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000185 if (Value *V = SimplifyUsingDistributiveLaws(I))
186 return ReplaceInstUsesWith(I, V);
187
David Majnemer027bc802014-11-22 04:52:38 +0000188 // X * -1 == 0 - X
189 if (match(Op1, m_AllOnes())) {
190 BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
191 if (I.hasNoSignedWrap())
192 BO->setHasNoSignedWrap();
193 return BO;
194 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000195
Rafael Espindola65281bf2013-05-31 14:27:15 +0000196 // Also allow combining multiply instructions on vectors.
197 {
198 Value *NewOp;
199 Constant *C1, *C2;
200 const APInt *IVal;
201 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
202 m_Constant(C1))) &&
David Majnemerfd4a6d22014-11-22 04:52:52 +0000203 match(C1, m_APInt(IVal))) {
204 // ((X << C2)*C1) == (X * (C1 << C2))
205 Constant *Shl = ConstantExpr::getShl(C1, C2);
206 BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
207 BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
208 if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
209 BO->setHasNoUnsignedWrap();
210 if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
211 Shl->isNotMinSignedValue())
212 BO->setHasNoSignedWrap();
213 return BO;
214 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000215
Rafael Espindola65281bf2013-05-31 14:27:15 +0000216 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
Craig Topperf40110f2014-04-25 05:29:35 +0000217 Constant *NewCst = nullptr;
Rafael Espindola65281bf2013-05-31 14:27:15 +0000218 if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2())
219 // Replace X*(2^C) with X << C, where C is either a scalar or a splat.
220 NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2());
221 else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1))
222 // Replace X*(2^C) with X << C, where C is a vector of known
223 // constant powers of 2.
224 NewCst = getLogBase2Vector(CV);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000225
Rafael Espindola65281bf2013-05-31 14:27:15 +0000226 if (NewCst) {
David Majnemer45951a62015-04-18 04:41:30 +0000227 unsigned Width = NewCst->getType()->getPrimitiveSizeInBits();
Rafael Espindola65281bf2013-05-31 14:27:15 +0000228 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000229
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000230 if (I.hasNoUnsignedWrap())
231 Shl->setHasNoUnsignedWrap();
David Majnemer45951a62015-04-18 04:41:30 +0000232 if (I.hasNoSignedWrap()) {
233 uint64_t V;
234 if (match(NewCst, m_ConstantInt(V)) && V != Width - 1)
235 Shl->setHasNoSignedWrap();
236 }
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000237
Rafael Espindola65281bf2013-05-31 14:27:15 +0000238 return Shl;
239 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000240 }
Rafael Espindola65281bf2013-05-31 14:27:15 +0000241 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000242
Rafael Espindola65281bf2013-05-31 14:27:15 +0000243 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Stuart Hastings23804832011-06-01 16:42:47 +0000244 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
245 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
246 // The "* (2**n)" thus becomes a potential shifting opportunity.
Stuart Hastings82843742011-05-30 20:00:33 +0000247 {
248 const APInt & Val = CI->getValue();
249 const APInt &PosVal = Val.abs();
250 if (Val.isNegative() && PosVal.isPowerOf2()) {
Craig Topperf40110f2014-04-25 05:29:35 +0000251 Value *X = nullptr, *Y = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000252 if (Op0->hasOneUse()) {
253 ConstantInt *C1;
Craig Topperf40110f2014-04-25 05:29:35 +0000254 Value *Sub = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000255 if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
256 Sub = Builder->CreateSub(X, Y, "suba");
257 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
258 Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc");
259 if (Sub)
260 return
261 BinaryOperator::CreateMul(Sub,
262 ConstantInt::get(Y->getType(), PosVal));
Stuart Hastings82843742011-05-30 20:00:33 +0000263 }
264 }
265 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000266 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000267
Chris Lattner6b657ae2011-02-10 05:36:31 +0000268 // Simplify mul instructions with a constant RHS.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000269 if (isa<Constant>(Op1)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000270 // Try to fold constant mul into select arguments.
271 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
272 if (Instruction *R = FoldOpIntoSelect(I, SI))
273 return R;
274
275 if (isa<PHINode>(Op0))
276 if (Instruction *NV = FoldOpIntoPhi(I))
277 return NV;
Benjamin Kramer72196f32014-01-19 15:24:22 +0000278
279 // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
280 {
281 Value *X;
282 Constant *C1;
283 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
David Majnemer6cf6c052014-06-19 07:14:33 +0000284 Value *Mul = Builder->CreateMul(C1, Op1);
285 // Only go forward with the transform if C1*CI simplifies to a tidier
286 // constant.
287 if (!match(Mul, m_Mul(m_Value(), m_Value())))
288 return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
Benjamin Kramer72196f32014-01-19 15:24:22 +0000289 }
290 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000291 }
292
David Majnemer8279a7502014-11-22 07:25:19 +0000293 if (Value *Op0v = dyn_castNegVal(Op0)) { // -X * -Y = X*Y
294 if (Value *Op1v = dyn_castNegVal(Op1)) {
295 BinaryOperator *BO = BinaryOperator::CreateMul(Op0v, Op1v);
296 if (I.hasNoSignedWrap() &&
297 match(Op0, m_NSWSub(m_Value(), m_Value())) &&
298 match(Op1, m_NSWSub(m_Value(), m_Value())))
299 BO->setHasNoSignedWrap();
300 return BO;
301 }
302 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000303
304 // (X / Y) * Y = X - (X % Y)
305 // (X / Y) * -Y = (X % Y) - X
306 {
307 Value *Op1C = Op1;
308 BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
309 if (!BO ||
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000310 (BO->getOpcode() != Instruction::UDiv &&
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000311 BO->getOpcode() != Instruction::SDiv)) {
312 Op1C = Op0;
313 BO = dyn_cast<BinaryOperator>(Op1);
314 }
315 Value *Neg = dyn_castNegVal(Op1C);
316 if (BO && BO->hasOneUse() &&
317 (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
318 (BO->getOpcode() == Instruction::UDiv ||
319 BO->getOpcode() == Instruction::SDiv)) {
320 Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
321
Chris Lattner35315d02011-02-06 21:44:57 +0000322 // If the division is exact, X % Y is zero, so we end up with X or -X.
323 if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000324 if (SDiv->isExact()) {
325 if (Op1BO == Op1C)
326 return ReplaceInstUsesWith(I, Op0BO);
327 return BinaryOperator::CreateNeg(Op0BO);
328 }
329
330 Value *Rem;
331 if (BO->getOpcode() == Instruction::UDiv)
332 Rem = Builder->CreateURem(Op0BO, Op1BO);
333 else
334 Rem = Builder->CreateSRem(Op0BO, Op1BO);
335 Rem->takeName(BO);
336
337 if (Op1BO == Op1C)
338 return BinaryOperator::CreateSub(Op0BO, Rem);
339 return BinaryOperator::CreateSub(Rem, Op0BO);
340 }
341 }
342
343 /// i1 mul -> i1 and.
Benjamin Kramer72196f32014-01-19 15:24:22 +0000344 if (I.getType()->getScalarType()->isIntegerTy(1))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000345 return BinaryOperator::CreateAnd(Op0, Op1);
346
347 // X*(1 << Y) --> X << Y
348 // (1 << Y)*X --> X << Y
349 {
350 Value *Y;
David Majnemer546f8102014-11-22 08:57:02 +0000351 BinaryOperator *BO = nullptr;
352 bool ShlNSW = false;
353 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
354 BO = BinaryOperator::CreateShl(Op1, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000355 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
David Majnemer8e6f6a92014-11-24 16:41:13 +0000356 } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
David Majnemer546f8102014-11-22 08:57:02 +0000357 BO = BinaryOperator::CreateShl(Op0, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000358 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
David Majnemer546f8102014-11-22 08:57:02 +0000359 }
360 if (BO) {
361 if (I.hasNoUnsignedWrap())
362 BO->setHasNoUnsignedWrap();
363 if (I.hasNoSignedWrap() && ShlNSW)
364 BO->setHasNoSignedWrap();
365 return BO;
366 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000367 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000368
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000369 // If one of the operands of the multiply is a cast from a boolean value, then
370 // we know the bool is either zero or one, so this is a 'masking' multiply.
371 // X * Y (where Y is 0 or 1) -> X & (0-Y)
Duncan Sands19d0b472010-02-16 11:11:14 +0000372 if (!I.getType()->isVectorTy()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000373 // -2 is "-1 << 1" so it is all bits set except the low one.
374 APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000375
Craig Topperf40110f2014-04-25 05:29:35 +0000376 Value *BoolCast = nullptr, *OtherOp = nullptr;
Hal Finkel60db0582014-09-07 18:57:58 +0000377 if (MaskedValueIsZero(Op0, Negative2, 0, &I))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000378 BoolCast = Op0, OtherOp = Op1;
Hal Finkel60db0582014-09-07 18:57:58 +0000379 else if (MaskedValueIsZero(Op1, Negative2, 0, &I))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000380 BoolCast = Op1, OtherOp = Op0;
381
382 if (BoolCast) {
383 Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()),
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000384 BoolCast);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000385 return BinaryOperator::CreateAnd(V, OtherOp);
386 }
387 }
388
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000389 if (!I.hasNoSignedWrap() && WillNotOverflowSignedMul(Op0, Op1, I)) {
David Majnemer54c2ca22014-12-26 09:10:14 +0000390 Changed = true;
391 I.setHasNoSignedWrap(true);
392 }
393
David Majnemer491331a2015-01-02 07:29:43 +0000394 if (!I.hasNoUnsignedWrap() &&
395 computeOverflowForUnsignedMul(Op0, Op1, &I) ==
396 OverflowResult::NeverOverflows) {
David Majnemerb1296ec2014-12-26 09:50:35 +0000397 Changed = true;
398 I.setHasNoUnsignedWrap(true);
399 }
400
Craig Topperf40110f2014-04-25 05:29:35 +0000401 return Changed ? &I : nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000402}
403
Sanjay Patel17045f72014-10-14 00:33:23 +0000404/// Detect pattern log2(Y * 0.5) with corresponding fast math flags.
Pedro Artigas993acd02012-11-30 22:07:05 +0000405static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) {
Sanjay Patel17045f72014-10-14 00:33:23 +0000406 if (!Op->hasOneUse())
407 return;
Pedro Artigas00b83c92012-11-30 22:47:15 +0000408
Sanjay Patel17045f72014-10-14 00:33:23 +0000409 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op);
410 if (!II)
411 return;
412 if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra())
413 return;
414 Log2 = II;
Pedro Artigas00b83c92012-11-30 22:47:15 +0000415
Sanjay Patel17045f72014-10-14 00:33:23 +0000416 Value *OpLog2Of = II->getArgOperand(0);
417 if (!OpLog2Of->hasOneUse())
418 return;
Pedro Artigas00b83c92012-11-30 22:47:15 +0000419
Sanjay Patel17045f72014-10-14 00:33:23 +0000420 Instruction *I = dyn_cast<Instruction>(OpLog2Of);
421 if (!I)
422 return;
423 if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
424 return;
Pedro Artigas00b83c92012-11-30 22:47:15 +0000425
Sanjay Patel17045f72014-10-14 00:33:23 +0000426 if (match(I->getOperand(0), m_SpecificFP(0.5)))
427 Y = I->getOperand(1);
428 else if (match(I->getOperand(1), m_SpecificFP(0.5)))
429 Y = I->getOperand(0);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000430}
Pedro Artigas993acd02012-11-30 22:07:05 +0000431
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000432static bool isFiniteNonZeroFp(Constant *C) {
433 if (C->getType()->isVectorTy()) {
434 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
435 ++I) {
Michael Kupersteinbcb26d62015-03-05 08:38:57 +0000436 ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I));
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000437 if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
438 return false;
439 }
440 return true;
441 }
442
443 return isa<ConstantFP>(C) &&
444 cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero();
445}
446
447static bool isNormalFp(Constant *C) {
448 if (C->getType()->isVectorTy()) {
449 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
450 ++I) {
Michael Kupersteinbcb26d62015-03-05 08:38:57 +0000451 ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I));
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000452 if (!CFP || !CFP->getValueAPF().isNormal())
453 return false;
454 }
455 return true;
456 }
457
458 return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal();
459}
460
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000461/// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns
462/// true iff the given value is FMul or FDiv with one and only one operand
463/// being a normal constant (i.e. not Zero/NaN/Infinity).
464static bool isFMulOrFDivWithConstant(Value *V) {
465 Instruction *I = dyn_cast<Instruction>(V);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000466 if (!I || (I->getOpcode() != Instruction::FMul &&
Shuxin Yang80138662013-01-07 22:41:28 +0000467 I->getOpcode() != Instruction::FDiv))
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000468 return false;
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000469
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000470 Constant *C0 = dyn_cast<Constant>(I->getOperand(0));
471 Constant *C1 = dyn_cast<Constant>(I->getOperand(1));
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000472
473 if (C0 && C1)
474 return false;
475
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000476 return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1));
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000477}
478
479/// foldFMulConst() is a helper routine of InstCombiner::visitFMul().
480/// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand
481/// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true).
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000482/// This function is to simplify "FMulOrDiv * C" and returns the
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000483/// resulting expression. Note that this function could return NULL in
484/// case the constants cannot be folded into a normal floating-point.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000485///
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000486Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yang80138662013-01-07 22:41:28 +0000487 Instruction *InsertBefore) {
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000488 assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid");
489
490 Value *Opnd0 = FMulOrDiv->getOperand(0);
491 Value *Opnd1 = FMulOrDiv->getOperand(1);
492
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000493 Constant *C0 = dyn_cast<Constant>(Opnd0);
494 Constant *C1 = dyn_cast<Constant>(Opnd1);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000495
Craig Topperf40110f2014-04-25 05:29:35 +0000496 BinaryOperator *R = nullptr;
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000497
498 // (X * C0) * C => X * (C0*C)
499 if (FMulOrDiv->getOpcode() == Instruction::FMul) {
500 Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000501 if (isNormalFp(F))
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000502 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
503 } else {
504 if (C0) {
505 // (C0 / X) * C => (C0 * C) / X
Shuxin Yang3a7ca6e2013-09-19 21:13:46 +0000506 if (FMulOrDiv->hasOneUse()) {
507 // It would otherwise introduce another div.
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000508 Constant *F = ConstantExpr::getFMul(C0, C);
Shuxin Yang3a7ca6e2013-09-19 21:13:46 +0000509 if (isNormalFp(F))
510 R = BinaryOperator::CreateFDiv(F, Opnd1);
511 }
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000512 } else {
513 // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000514 Constant *F = ConstantExpr::getFDiv(C, C1);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000515 if (isNormalFp(F)) {
516 R = BinaryOperator::CreateFMul(Opnd0, F);
517 } else {
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000518 // (X / C1) * C => X / (C1/C)
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000519 Constant *F = ConstantExpr::getFDiv(C1, C);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000520 if (isNormalFp(F))
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000521 R = BinaryOperator::CreateFDiv(Opnd0, F);
522 }
523 }
524 }
525
526 if (R) {
527 R->setHasUnsafeAlgebra(true);
528 InsertNewInstWith(R, *InsertBefore);
529 }
530
531 return R;
532}
533
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000534Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +0000535 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000536 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
537
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000538 if (Value *V = SimplifyVectorOp(I))
539 return ReplaceInstUsesWith(I, V);
540
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000541 if (isa<Constant>(Op0))
542 std::swap(Op0, Op1);
543
Chandler Carruth66b31302015-01-04 12:03:27 +0000544 if (Value *V =
545 SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL, TLI, DT, AC))
Michael Ilsemand5787be2012-12-12 00:28:32 +0000546 return ReplaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000547
Shuxin Yange8227452013-01-15 21:09:32 +0000548 bool AllowReassociate = I.hasUnsafeAlgebra();
549
Michael Ilsemand5787be2012-12-12 00:28:32 +0000550 // Simplify mul instructions with a constant RHS.
551 if (isa<Constant>(Op1)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000552 // Try to fold constant mul into select arguments.
553 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
554 if (Instruction *R = FoldOpIntoSelect(I, SI))
555 return R;
556
557 if (isa<PHINode>(Op0))
558 if (Instruction *NV = FoldOpIntoPhi(I))
559 return NV;
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000560
Owen Andersonf74cfe02014-01-16 20:36:42 +0000561 // (fmul X, -1.0) --> (fsub -0.0, X)
Benjamin Kramerfea9ac92014-01-18 16:43:14 +0000562 if (match(Op1, m_SpecificFP(-1.0))) {
563 Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType());
564 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
Owen Andersonf74cfe02014-01-16 20:36:42 +0000565 RI->copyFastMathFlags(&I);
566 return RI;
567 }
568
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000569 Constant *C = cast<Constant>(Op1);
570 if (AllowReassociate && isFiniteNonZeroFp(C)) {
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000571 // Let MDC denote an expression in one of these forms:
572 // X * C, C/X, X/C, where C is a constant.
573 //
574 // Try to simplify "MDC * Constant"
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000575 if (isFMulOrFDivWithConstant(Op0))
576 if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000577 return ReplaceInstUsesWith(I, V);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000578
Quentin Colombete684a6d2013-02-28 21:12:40 +0000579 // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C)
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000580 Instruction *FAddSub = dyn_cast<Instruction>(Op0);
581 if (FAddSub &&
582 (FAddSub->getOpcode() == Instruction::FAdd ||
583 FAddSub->getOpcode() == Instruction::FSub)) {
584 Value *Opnd0 = FAddSub->getOperand(0);
585 Value *Opnd1 = FAddSub->getOperand(1);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000586 Constant *C0 = dyn_cast<Constant>(Opnd0);
587 Constant *C1 = dyn_cast<Constant>(Opnd1);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000588 bool Swap = false;
589 if (C0) {
Shuxin Yang80138662013-01-07 22:41:28 +0000590 std::swap(C0, C1);
591 std::swap(Opnd0, Opnd1);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000592 Swap = true;
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000593 }
594
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000595 if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) {
Quentin Colombete684a6d2013-02-28 21:12:40 +0000596 Value *M1 = ConstantExpr::getFMul(C1, C);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000597 Value *M0 = isNormalFp(cast<Constant>(M1)) ?
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000598 foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
Craig Topperf40110f2014-04-25 05:29:35 +0000599 nullptr;
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000600 if (M0 && M1) {
601 if (Swap && FAddSub->getOpcode() == Instruction::FSub)
602 std::swap(M0, M1);
603
Benjamin Kramer67485762013-09-30 15:39:59 +0000604 Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd)
605 ? BinaryOperator::CreateFAdd(M0, M1)
606 : BinaryOperator::CreateFSub(M0, M1);
Shuxin Yange8227452013-01-15 21:09:32 +0000607 RI->copyFastMathFlags(&I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000608 return RI;
609 }
610 }
611 }
612 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000613 }
614
Sanjay Patel12d1ce52014-10-02 21:10:54 +0000615 // sqrt(X) * sqrt(X) -> X
616 if (AllowReassociate && (Op0 == Op1))
617 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0))
618 if (II->getIntrinsicID() == Intrinsic::sqrt)
619 return ReplaceInstUsesWith(I, II->getOperand(0));
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000620
Pedro Artigasd8795042012-11-30 19:09:41 +0000621 // Under unsafe algebra do:
622 // X * log2(0.5*Y) = X*log2(Y) - X
Sanjay Patelb41d4612014-10-02 15:20:45 +0000623 if (AllowReassociate) {
Craig Topperf40110f2014-04-25 05:29:35 +0000624 Value *OpX = nullptr;
625 Value *OpY = nullptr;
Pedro Artigasd8795042012-11-30 19:09:41 +0000626 IntrinsicInst *Log2;
Pedro Artigas993acd02012-11-30 22:07:05 +0000627 detectLog2OfHalf(Op0, OpY, Log2);
628 if (OpY) {
629 OpX = Op1;
630 } else {
631 detectLog2OfHalf(Op1, OpY, Log2);
632 if (OpY) {
633 OpX = Op0;
Pedro Artigasd8795042012-11-30 19:09:41 +0000634 }
635 }
636 // if pattern detected emit alternate sequence
637 if (OpX && OpY) {
Benjamin Kramer67485762013-09-30 15:39:59 +0000638 BuilderTy::FastMathFlagGuard Guard(*Builder);
Sanjay Patela2528152016-01-12 18:03:37 +0000639 Builder->setFastMathFlags(Log2->getFastMathFlags());
Pedro Artigasd8795042012-11-30 19:09:41 +0000640 Log2->setArgOperand(0, OpY);
641 Value *FMulVal = Builder->CreateFMul(OpX, Log2);
Benjamin Kramer67485762013-09-30 15:39:59 +0000642 Value *FSub = Builder->CreateFSub(FMulVal, OpX);
643 FSub->takeName(&I);
644 return ReplaceInstUsesWith(I, FSub);
Pedro Artigasd8795042012-11-30 19:09:41 +0000645 }
646 }
647
Shuxin Yange8227452013-01-15 21:09:32 +0000648 // Handle symmetric situation in a 2-iteration loop
649 Value *Opnd0 = Op0;
650 Value *Opnd1 = Op1;
651 for (int i = 0; i < 2; i++) {
652 bool IgnoreZeroSign = I.hasNoSignedZeros();
653 if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) {
Benjamin Kramer67485762013-09-30 15:39:59 +0000654 BuilderTy::FastMathFlagGuard Guard(*Builder);
Sanjay Patela2528152016-01-12 18:03:37 +0000655 Builder->setFastMathFlags(I.getFastMathFlags());
Benjamin Kramer67485762013-09-30 15:39:59 +0000656
Shuxin Yange8227452013-01-15 21:09:32 +0000657 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
658 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
Shuxin Yangf8e9a5a2012-12-14 18:46:06 +0000659
Shuxin Yange8227452013-01-15 21:09:32 +0000660 // -X * -Y => X*Y
Owen Andersone8537fc2014-01-16 20:59:41 +0000661 if (N1) {
662 Value *FMul = Builder->CreateFMul(N0, N1);
663 FMul->takeName(&I);
664 return ReplaceInstUsesWith(I, FMul);
665 }
Shuxin Yangf8e9a5a2012-12-14 18:46:06 +0000666
Shuxin Yange8227452013-01-15 21:09:32 +0000667 if (Opnd0->hasOneUse()) {
668 // -X * Y => -(X*Y) (Promote negation as high as possible)
669 Value *T = Builder->CreateFMul(N0, Opnd1);
Benjamin Kramer67485762013-09-30 15:39:59 +0000670 Value *Neg = Builder->CreateFNeg(T);
671 Neg->takeName(&I);
672 return ReplaceInstUsesWith(I, Neg);
Shuxin Yangf8e9a5a2012-12-14 18:46:06 +0000673 }
674 }
Shuxin Yange8227452013-01-15 21:09:32 +0000675
676 // (X*Y) * X => (X*X) * Y where Y != X
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000677 // The purpose is two-fold:
Shuxin Yange8227452013-01-15 21:09:32 +0000678 // 1) to form a power expression (of X).
679 // 2) potentially shorten the critical path: After transformation, the
680 // latency of the instruction Y is amortized by the expression of X*X,
681 // and therefore Y is in a "less critical" position compared to what it
682 // was before the transformation.
683 //
684 if (AllowReassociate) {
685 Value *Opnd0_0, *Opnd0_1;
686 if (Opnd0->hasOneUse() &&
687 match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) {
Craig Topperf40110f2014-04-25 05:29:35 +0000688 Value *Y = nullptr;
Shuxin Yange8227452013-01-15 21:09:32 +0000689 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
690 Y = Opnd0_1;
691 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
692 Y = Opnd0_0;
693
694 if (Y) {
Benjamin Kramer67485762013-09-30 15:39:59 +0000695 BuilderTy::FastMathFlagGuard Guard(*Builder);
Sanjay Patela2528152016-01-12 18:03:37 +0000696 Builder->setFastMathFlags(I.getFastMathFlags());
Benjamin Kramer67485762013-09-30 15:39:59 +0000697 Value *T = Builder->CreateFMul(Opnd1, Opnd1);
Shuxin Yange8227452013-01-15 21:09:32 +0000698
Benjamin Kramer67485762013-09-30 15:39:59 +0000699 Value *R = Builder->CreateFMul(T, Y);
700 R->takeName(&I);
701 return ReplaceInstUsesWith(I, R);
Shuxin Yange8227452013-01-15 21:09:32 +0000702 }
703 }
704 }
705
706 if (!isa<Constant>(Op1))
707 std::swap(Opnd0, Opnd1);
708 else
709 break;
Shuxin Yangf8e9a5a2012-12-14 18:46:06 +0000710 }
711
Craig Topperf40110f2014-04-25 05:29:35 +0000712 return Changed ? &I : nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000713}
714
Sanjay Patel6eccf482015-09-09 15:24:36 +0000715/// Try to fold a divide or remainder of a select instruction.
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000716bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
717 SelectInst *SI = cast<SelectInst>(I.getOperand(1));
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000718
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000719 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
720 int NonNullOperand = -1;
721 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
722 if (ST->isNullValue())
723 NonNullOperand = 2;
724 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
725 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
726 if (ST->isNullValue())
727 NonNullOperand = 1;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000728
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000729 if (NonNullOperand == -1)
730 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000731
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000732 Value *SelectCond = SI->getOperand(0);
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000733
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000734 // Change the div/rem to use 'Y' instead of the select.
735 I.setOperand(1, SI->getOperand(NonNullOperand));
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000736
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000737 // Okay, we know we replace the operand of the div/rem with 'Y' with no
738 // problem. However, the select, or the condition of the select may have
739 // multiple uses. Based on our knowledge that the operand must be non-zero,
740 // propagate the known value for the select into other uses of it, and
741 // propagate a known value of the condition into its other users.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000742
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000743 // If the select and condition only have a single use, don't bother with this,
744 // early exit.
745 if (SI->use_empty() && SelectCond->hasOneUse())
746 return true;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000747
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000748 // Scan the current block backward, looking for other uses of SI.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000749 BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin();
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000750
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000751 while (BBI != BBFront) {
752 --BBI;
753 // If we found a call to a function, we can't assume it will return, so
754 // information from below it cannot be propagated above it.
755 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
756 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000757
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000758 // Replace uses of the select or its condition with the known values.
759 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
760 I != E; ++I) {
761 if (*I == SI) {
762 *I = SI->getOperand(NonNullOperand);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000763 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000764 } else if (*I == SelectCond) {
Jakub Staszak96ff4d62013-06-06 23:34:59 +0000765 *I = Builder->getInt1(NonNullOperand == 1);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000766 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000767 }
768 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000769
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000770 // If we past the instruction, quit looking for it.
771 if (&*BBI == SI)
Craig Topperf40110f2014-04-25 05:29:35 +0000772 SI = nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000773 if (&*BBI == SelectCond)
Craig Topperf40110f2014-04-25 05:29:35 +0000774 SelectCond = nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000775
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000776 // If we ran out of things to eliminate, break out of the loop.
Craig Topperf40110f2014-04-25 05:29:35 +0000777 if (!SelectCond && !SI)
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000778 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000779
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000780 }
781 return true;
782}
783
784
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000785/// This function implements the transforms common to both integer division
786/// instructions (udiv and sdiv). It is called by the visitors to those integer
787/// division instructions.
788/// @brief Common integer divide transforms
789Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
790 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
791
Chris Lattner7c99f192011-05-22 18:18:41 +0000792 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000793 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +0000794 I.setOperand(1, V);
795 return &I;
796 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000797
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000798 // Handle cases involving: [su]div X, (select Cond, Y, Z)
799 // This does not apply for fdiv.
800 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
801 return &I;
802
David Majnemer27adb122014-10-12 08:34:24 +0000803 if (Instruction *LHS = dyn_cast<Instruction>(Op0)) {
804 const APInt *C2;
805 if (match(Op1, m_APInt(C2))) {
David Majnemerf9a095d2014-08-16 08:55:06 +0000806 Value *X;
David Majnemer27adb122014-10-12 08:34:24 +0000807 const APInt *C1;
808 bool IsSigned = I.getOpcode() == Instruction::SDiv;
David Majnemerf9a095d2014-08-16 08:55:06 +0000809
David Majnemer27adb122014-10-12 08:34:24 +0000810 // (X / C1) / C2 -> X / (C1*C2)
811 if ((IsSigned && match(LHS, m_SDiv(m_Value(X), m_APInt(C1)))) ||
812 (!IsSigned && match(LHS, m_UDiv(m_Value(X), m_APInt(C1))))) {
813 APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
814 if (!MultiplyOverflows(*C1, *C2, Product, IsSigned))
815 return BinaryOperator::Create(I.getOpcode(), X,
816 ConstantInt::get(I.getType(), Product));
817 }
David Majnemerf9a095d2014-08-16 08:55:06 +0000818
David Majnemer27adb122014-10-12 08:34:24 +0000819 if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
820 (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) {
821 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
822
823 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
824 if (IsMultiple(*C2, *C1, Quotient, IsSigned)) {
825 BinaryOperator *BO = BinaryOperator::Create(
826 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
827 BO->setIsExact(I.isExact());
828 return BO;
David Majnemerf9a095d2014-08-16 08:55:06 +0000829 }
830
David Majnemer27adb122014-10-12 08:34:24 +0000831 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
832 if (IsMultiple(*C1, *C2, Quotient, IsSigned)) {
833 BinaryOperator *BO = BinaryOperator::Create(
834 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
835 BO->setHasNoUnsignedWrap(
836 !IsSigned &&
837 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
838 BO->setHasNoSignedWrap(
839 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
840 return BO;
David Majnemerf9a095d2014-08-16 08:55:06 +0000841 }
842 }
David Majnemerf9a095d2014-08-16 08:55:06 +0000843
David Majnemer27adb122014-10-12 08:34:24 +0000844 if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1))) &&
845 *C1 != C1->getBitWidth() - 1) ||
846 (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) {
847 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
848 APInt C1Shifted = APInt::getOneBitSet(
849 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
850
851 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1.
852 if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
853 BinaryOperator *BO = BinaryOperator::Create(
854 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
855 BO->setIsExact(I.isExact());
856 return BO;
857 }
858
859 // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2.
860 if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
861 BinaryOperator *BO = BinaryOperator::Create(
862 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
863 BO->setHasNoUnsignedWrap(
864 !IsSigned &&
865 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
866 BO->setHasNoSignedWrap(
867 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
868 return BO;
869 }
870 }
871
872 if (*C2 != 0) { // avoid X udiv 0
873 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
874 if (Instruction *R = FoldOpIntoSelect(I, SI))
875 return R;
876 if (isa<PHINode>(Op0))
877 if (Instruction *NV = FoldOpIntoPhi(I))
878 return NV;
879 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000880 }
881 }
882
Nick Lewyckyf0cf8fa2014-05-14 03:03:05 +0000883 if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
884 if (One->isOne() && !I.getType()->isIntegerTy(1)) {
885 bool isSigned = I.getOpcode() == Instruction::SDiv;
886 if (isSigned) {
887 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
888 // result is one, if Op1 is -1 then the result is minus one, otherwise
889 // it's zero.
890 Value *Inc = Builder->CreateAdd(Op1, One);
891 Value *Cmp = Builder->CreateICmpULT(
892 Inc, ConstantInt::get(I.getType(), 3));
893 return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0));
894 } else {
895 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
896 // result is one, otherwise it's zero.
897 return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType());
898 }
899 }
900 }
901
Benjamin Kramer57b3df52011-04-30 18:16:00 +0000902 // See if we can fold away this div instruction.
903 if (SimplifyDemandedInstructionBits(I))
904 return &I;
905
Duncan Sands771e82a2011-01-28 16:51:11 +0000906 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
Craig Topperf40110f2014-04-25 05:29:35 +0000907 Value *X = nullptr, *Z = nullptr;
Duncan Sands771e82a2011-01-28 16:51:11 +0000908 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
909 bool isSigned = I.getOpcode() == Instruction::SDiv;
910 if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
911 (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
912 return BinaryOperator::Create(I.getOpcode(), X, Op1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000913 }
914
Craig Topperf40110f2014-04-25 05:29:35 +0000915 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000916}
917
Benjamin Kramer9aa91b12011-04-30 18:16:07 +0000918/// dyn_castZExtVal - Checks if V is a zext or constant that can
919/// be truncated to Ty without losing bits.
Chris Lattner229907c2011-07-18 04:54:35 +0000920static Value *dyn_castZExtVal(Value *V, Type *Ty) {
Benjamin Kramer9aa91b12011-04-30 18:16:07 +0000921 if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
922 if (Z->getSrcTy() == Ty)
923 return Z->getOperand(0);
924 } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) {
925 if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth())
926 return ConstantExpr::getTrunc(C, Ty);
927 }
Craig Topperf40110f2014-04-25 05:29:35 +0000928 return nullptr;
Benjamin Kramer9aa91b12011-04-30 18:16:07 +0000929}
930
David Majnemer37f8f442013-07-04 21:17:49 +0000931namespace {
932const unsigned MaxDepth = 6;
933typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1,
934 const BinaryOperator &I,
935 InstCombiner &IC);
936
937/// \brief Used to maintain state for visitUDivOperand().
938struct UDivFoldAction {
939 FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this
940 ///< operand. This can be zero if this action
941 ///< joins two actions together.
942
943 Value *OperandToFold; ///< Which operand to fold.
944 union {
945 Instruction *FoldResult; ///< The instruction returned when FoldAction is
946 ///< invoked.
947
948 size_t SelectLHSIdx; ///< Stores the LHS action index if this action
949 ///< joins two actions together.
950 };
951
952 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
Craig Topperf40110f2014-04-25 05:29:35 +0000953 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
David Majnemer37f8f442013-07-04 21:17:49 +0000954 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
955 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
956};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000957}
David Majnemer37f8f442013-07-04 21:17:49 +0000958
959// X udiv 2^C -> X >> C
960static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
961 const BinaryOperator &I, InstCombiner &IC) {
962 const APInt &C = cast<Constant>(Op1)->getUniqueInteger();
963 BinaryOperator *LShr = BinaryOperator::CreateLShr(
964 Op0, ConstantInt::get(Op0->getType(), C.logBase2()));
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000965 if (I.isExact())
966 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000967 return LShr;
968}
969
970// X udiv C, where C >= signbit
971static Instruction *foldUDivNegCst(Value *Op0, Value *Op1,
972 const BinaryOperator &I, InstCombiner &IC) {
973 Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1));
974
975 return SelectInst::Create(ICI, Constant::getNullValue(I.getType()),
976 ConstantInt::get(I.getType(), 1));
977}
978
979// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
980static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
981 InstCombiner &IC) {
982 Instruction *ShiftLeft = cast<Instruction>(Op1);
983 if (isa<ZExtInst>(ShiftLeft))
984 ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));
985
986 const APInt &CI =
987 cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger();
988 Value *N = ShiftLeft->getOperand(1);
989 if (CI != 1)
990 N = IC.Builder->CreateAdd(N, ConstantInt::get(N->getType(), CI.logBase2()));
991 if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1))
992 N = IC.Builder->CreateZExt(N, Z->getDestTy());
993 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000994 if (I.isExact())
995 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000996 return LShr;
997}
998
999// \brief Recursively visits the possible right hand operands of a udiv
1000// instruction, seeing through select instructions, to determine if we can
1001// replace the udiv with something simpler. If we find that an operand is not
1002// able to simplify the udiv, we abort the entire transformation.
1003static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
1004 SmallVectorImpl<UDivFoldAction> &Actions,
1005 unsigned Depth = 0) {
1006 // Check to see if this is an unsigned division with an exact power of 2,
1007 // if so, convert to a right shift.
1008 if (match(Op1, m_Power2())) {
1009 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
1010 return Actions.size();
1011 }
1012
1013 if (ConstantInt *C = dyn_cast<ConstantInt>(Op1))
1014 // X udiv C, where C >= signbit
1015 if (C->getValue().isNegative()) {
1016 Actions.push_back(UDivFoldAction(foldUDivNegCst, C));
1017 return Actions.size();
1018 }
1019
1020 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
1021 if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
1022 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
1023 Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
1024 return Actions.size();
1025 }
1026
1027 // The remaining tests are all recursive, so bail out if we hit the limit.
1028 if (Depth++ == MaxDepth)
1029 return 0;
1030
1031 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
David Majnemer492e6122014-08-30 09:19:05 +00001032 if (size_t LHSIdx =
1033 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
1034 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
1035 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
David Majnemer37f8f442013-07-04 21:17:49 +00001036 return Actions.size();
1037 }
1038
1039 return 0;
1040}
1041
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001042Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
1043 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1044
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001045 if (Value *V = SimplifyVectorOp(I))
1046 return ReplaceInstUsesWith(I, V);
1047
Chandler Carruth66b31302015-01-04 12:03:27 +00001048 if (Value *V = SimplifyUDivInst(Op0, Op1, DL, TLI, DT, AC))
Duncan Sands771e82a2011-01-28 16:51:11 +00001049 return ReplaceInstUsesWith(I, V);
1050
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001051 // Handle the integer div common cases
1052 if (Instruction *Common = commonIDivTransforms(I))
1053 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001054
Benjamin Kramerd4a64712012-08-30 15:07:40 +00001055 // (x lshr C1) udiv C2 --> x udiv (C2 << C1)
David Majnemera2521382014-10-13 21:48:30 +00001056 {
Benjamin Kramer9c0a8072012-08-28 13:08:13 +00001057 Value *X;
David Majnemera2521382014-10-13 21:48:30 +00001058 const APInt *C1, *C2;
1059 if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) &&
1060 match(Op1, m_APInt(C2))) {
1061 bool Overflow;
1062 APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
David Majnemera3aeb152014-11-22 18:16:54 +00001063 if (!Overflow) {
1064 bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
1065 BinaryOperator *BO = BinaryOperator::CreateUDiv(
David Majnemera2521382014-10-13 21:48:30 +00001066 X, ConstantInt::get(X->getType(), C2ShlC1));
David Majnemera3aeb152014-11-22 18:16:54 +00001067 if (IsExact)
1068 BO->setIsExact();
1069 return BO;
1070 }
David Majnemera2521382014-10-13 21:48:30 +00001071 }
Nadav Rotem11935b22012-08-28 10:01:43 +00001072 }
1073
Benjamin Kramer9aa91b12011-04-30 18:16:07 +00001074 // (zext A) udiv (zext B) --> zext (A udiv B)
1075 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1076 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
Suyog Sardaea205512014-10-07 11:56:06 +00001077 return new ZExtInst(
1078 Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", I.isExact()),
1079 I.getType());
Benjamin Kramer9aa91b12011-04-30 18:16:07 +00001080
David Majnemer37f8f442013-07-04 21:17:49 +00001081 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
1082 SmallVector<UDivFoldAction, 6> UDivActions;
1083 if (visitUDivOperand(Op0, Op1, I, UDivActions))
1084 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
1085 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
1086 Value *ActionOp1 = UDivActions[i].OperandToFold;
1087 Instruction *Inst;
1088 if (Action)
1089 Inst = Action(Op0, ActionOp1, I, *this);
1090 else {
1091 // This action joins two actions together. The RHS of this action is
1092 // simply the last action we processed, we saved the LHS action index in
1093 // the joining action.
1094 size_t SelectRHSIdx = i - 1;
1095 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
1096 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
1097 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
1098 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
1099 SelectLHS, SelectRHS);
1100 }
1101
1102 // If this is the last action to process, return it to the InstCombiner.
1103 // Otherwise, we insert it before the UDiv and record it so that we may
1104 // use it as part of a joining action (i.e., a SelectInst).
1105 if (e - i != 1) {
1106 Inst->insertBefore(&I);
1107 UDivActions[i].FoldResult = Inst;
1108 } else
1109 return Inst;
1110 }
1111
Craig Topperf40110f2014-04-25 05:29:35 +00001112 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001113}
1114
1115Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
1116 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1117
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001118 if (Value *V = SimplifyVectorOp(I))
1119 return ReplaceInstUsesWith(I, V);
1120
Chandler Carruth66b31302015-01-04 12:03:27 +00001121 if (Value *V = SimplifySDivInst(Op0, Op1, DL, TLI, DT, AC))
Duncan Sands771e82a2011-01-28 16:51:11 +00001122 return ReplaceInstUsesWith(I, V);
1123
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001124 // Handle the integer div common cases
1125 if (Instruction *Common = commonIDivTransforms(I))
1126 return Common;
1127
Benjamin Kramer72196f32014-01-19 15:24:22 +00001128 // sdiv X, -1 == -X
1129 if (match(Op1, m_AllOnes()))
1130 return BinaryOperator::CreateNeg(Op0);
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001131
Benjamin Kramer72196f32014-01-19 15:24:22 +00001132 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +00001133 // sdiv X, C --> ashr exact X, log2(C)
1134 if (I.isExact() && RHS->getValue().isNonNegative() &&
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001135 RHS->getValue().isPowerOf2()) {
1136 Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
1137 RHS->getValue().exactLogBase2());
Chris Lattner6b657ae2011-02-10 05:36:31 +00001138 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001139 }
Benjamin Kramer72196f32014-01-19 15:24:22 +00001140 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001141
Benjamin Kramer72196f32014-01-19 15:24:22 +00001142 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
David Majnemerf28e2a42014-07-02 06:42:13 +00001143 // X/INT_MIN -> X == INT_MIN
1144 if (RHS->isMinSignedValue())
1145 return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());
1146
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001147 // -X/C --> X/-C provided the negation doesn't overflow.
David Majnemerfa4699e2014-11-22 20:00:34 +00001148 Value *X;
1149 if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
1150 auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS));
1151 BO->setIsExact(I.isExact());
1152 return BO;
1153 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001154 }
1155
1156 // If the sign bits of both operands are zero (i.e. we can prove they are
1157 // unsigned inputs), turn this into a udiv.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001158 if (I.getType()->isIntegerTy()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001159 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
Hal Finkel60db0582014-09-07 18:57:58 +00001160 if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
1161 if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001162 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
David Majnemerec6e4812014-11-22 20:00:38 +00001163 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1164 BO->setIsExact(I.isExact());
1165 return BO;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001166 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001167
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001168 if (isKnownToBeAPowerOfTwo(Op1, DL, /*OrZero*/ true, 0, AC, &I, DT)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001169 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1170 // Safe because the only negative value (1 << Y) can take on is
1171 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1172 // the sign bit set.
David Majnemerfb380552014-11-22 20:00:41 +00001173 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1174 BO->setIsExact(I.isExact());
1175 return BO;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001176 }
1177 }
1178 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001179
Craig Topperf40110f2014-04-25 05:29:35 +00001180 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001181}
1182
Shuxin Yang320f52a2013-01-14 22:48:41 +00001183/// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special
1184/// FP value and:
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001185/// 1) 1/C is exact, or
Shuxin Yang320f52a2013-01-14 22:48:41 +00001186/// 2) reciprocal is allowed.
Sylvestre Ledru149e2812013-05-14 23:36:24 +00001187/// If the conversion was successful, the simplified expression "X * 1/C" is
Shuxin Yang320f52a2013-01-14 22:48:41 +00001188/// returned; otherwise, NULL is returned.
1189///
Suyog Sardaea205512014-10-07 11:56:06 +00001190static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor,
Shuxin Yang320f52a2013-01-14 22:48:41 +00001191 bool AllowReciprocal) {
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001192 if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors.
Craig Topperf40110f2014-04-25 05:29:35 +00001193 return nullptr;
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001194
1195 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
Shuxin Yang320f52a2013-01-14 22:48:41 +00001196 APFloat Reciprocal(FpVal.getSemantics());
1197 bool Cvt = FpVal.getExactInverse(&Reciprocal);
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001198
Michael Gottesman3cb77ab2013-06-19 21:23:18 +00001199 if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001200 Reciprocal = APFloat(FpVal.getSemantics(), 1.0f);
1201 (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven);
1202 Cvt = !Reciprocal.isDenormal();
1203 }
1204
1205 if (!Cvt)
Craig Topperf40110f2014-04-25 05:29:35 +00001206 return nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001207
1208 ConstantFP *R;
1209 R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal);
1210 return BinaryOperator::CreateFMul(Dividend, R);
1211}
1212
Frits van Bommel2a559512011-01-29 17:50:27 +00001213Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
1214 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1215
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001216 if (Value *V = SimplifyVectorOp(I))
1217 return ReplaceInstUsesWith(I, V);
1218
Mehdi Aminicd3ca6f2015-02-23 18:30:25 +00001219 if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(),
1220 DL, TLI, DT, AC))
Frits van Bommel2a559512011-01-29 17:50:27 +00001221 return ReplaceInstUsesWith(I, V);
1222
Stephen Lina9b57f62013-07-20 07:13:13 +00001223 if (isa<Constant>(Op0))
1224 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1225 if (Instruction *R = FoldOpIntoSelect(I, SI))
1226 return R;
1227
Shuxin Yang320f52a2013-01-14 22:48:41 +00001228 bool AllowReassociate = I.hasUnsafeAlgebra();
1229 bool AllowReciprocal = I.hasAllowReciprocal();
Benjamin Kramer8564e0d2011-03-30 15:42:35 +00001230
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001231 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
Stephen Lina9b57f62013-07-20 07:13:13 +00001232 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1233 if (Instruction *R = FoldOpIntoSelect(I, SI))
1234 return R;
1235
Shuxin Yang320f52a2013-01-14 22:48:41 +00001236 if (AllowReassociate) {
Craig Topperf40110f2014-04-25 05:29:35 +00001237 Constant *C1 = nullptr;
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001238 Constant *C2 = Op1C;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001239 Value *X;
Craig Topperf40110f2014-04-25 05:29:35 +00001240 Instruction *Res = nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001241
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001242 if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001243 // (X*C1)/C2 => X * (C1/C2)
1244 //
1245 Constant *C = ConstantExpr::getFDiv(C1, C2);
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001246 if (isNormalFp(C))
Shuxin Yang320f52a2013-01-14 22:48:41 +00001247 Res = BinaryOperator::CreateFMul(X, C);
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001248 } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001249 // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed]
1250 //
1251 Constant *C = ConstantExpr::getFMul(C1, C2);
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001252 if (isNormalFp(C)) {
1253 Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001254 if (!Res)
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001255 Res = BinaryOperator::CreateFDiv(X, C);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001256 }
1257 }
1258
1259 if (Res) {
1260 Res->setFastMathFlags(I.getFastMathFlags());
1261 return Res;
1262 }
1263 }
1264
1265 // X / C => X * 1/C
Owen Anderson4557a152014-01-16 21:07:52 +00001266 if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) {
1267 T->copyFastMathFlags(&I);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001268 return T;
Owen Anderson4557a152014-01-16 21:07:52 +00001269 }
Shuxin Yang320f52a2013-01-14 22:48:41 +00001270
Craig Topperf40110f2014-04-25 05:29:35 +00001271 return nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001272 }
1273
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001274 if (AllowReassociate && isa<Constant>(Op0)) {
1275 Constant *C1 = cast<Constant>(Op0), *C2;
Craig Topperf40110f2014-04-25 05:29:35 +00001276 Constant *Fold = nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001277 Value *X;
1278 bool CreateDiv = true;
1279
1280 // C1 / (X*C2) => (C1/C2) / X
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001281 if (match(Op1, m_FMul(m_Value(X), m_Constant(C2))))
Shuxin Yang320f52a2013-01-14 22:48:41 +00001282 Fold = ConstantExpr::getFDiv(C1, C2);
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001283 else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001284 // C1 / (X/C2) => (C1*C2) / X
1285 Fold = ConstantExpr::getFMul(C1, C2);
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001286 } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001287 // C1 / (C2/X) => (C1/C2) * X
1288 Fold = ConstantExpr::getFDiv(C1, C2);
1289 CreateDiv = false;
1290 }
1291
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001292 if (Fold && isNormalFp(Fold)) {
1293 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1294 : BinaryOperator::CreateFMul(X, Fold);
1295 R->setFastMathFlags(I.getFastMathFlags());
1296 return R;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001297 }
Craig Topperf40110f2014-04-25 05:29:35 +00001298 return nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001299 }
1300
1301 if (AllowReassociate) {
1302 Value *X, *Y;
Craig Topperf40110f2014-04-25 05:29:35 +00001303 Value *NewInst = nullptr;
1304 Instruction *SimpR = nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001305
1306 if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) {
1307 // (X/Y) / Z => X / (Y*Z)
1308 //
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001309 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001310 NewInst = Builder->CreateFMul(Y, Op1);
Owen Anderson1664dc82014-01-20 07:44:53 +00001311 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1312 FastMathFlags Flags = I.getFastMathFlags();
1313 Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1314 RI->setFastMathFlags(Flags);
1315 }
Shuxin Yang320f52a2013-01-14 22:48:41 +00001316 SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1317 }
1318 } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) {
1319 // Z / (X/Y) => Z*Y / X
1320 //
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001321 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001322 NewInst = Builder->CreateFMul(Op0, Y);
Owen Anderson1664dc82014-01-20 07:44:53 +00001323 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1324 FastMathFlags Flags = I.getFastMathFlags();
1325 Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1326 RI->setFastMathFlags(Flags);
1327 }
Shuxin Yang320f52a2013-01-14 22:48:41 +00001328 SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1329 }
1330 }
1331
1332 if (NewInst) {
1333 if (Instruction *T = dyn_cast<Instruction>(NewInst))
1334 T->setDebugLoc(I.getDebugLoc());
1335 SimpR->setFastMathFlags(I.getFastMathFlags());
1336 return SimpR;
Benjamin Kramer8564e0d2011-03-30 15:42:35 +00001337 }
1338 }
1339
Craig Topperf40110f2014-04-25 05:29:35 +00001340 return nullptr;
Frits van Bommel2a559512011-01-29 17:50:27 +00001341}
1342
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001343/// This function implements the transforms common to both integer remainder
1344/// instructions (urem and srem). It is called by the visitors to those integer
1345/// remainder instructions.
1346/// @brief Common integer remainder transforms
1347Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
1348 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1349
Chris Lattner7c99f192011-05-22 18:18:41 +00001350 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001351 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +00001352 I.setOperand(1, V);
1353 return &I;
1354 }
1355
Duncan Sandsa3e36992011-05-02 16:27:02 +00001356 // Handle cases involving: rem X, (select Cond, Y, Z)
1357 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1358 return &I;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001359
Benjamin Kramer72196f32014-01-19 15:24:22 +00001360 if (isa<Constant>(Op1)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001361 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1362 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1363 if (Instruction *R = FoldOpIntoSelect(I, SI))
1364 return R;
1365 } else if (isa<PHINode>(Op0I)) {
1366 if (Instruction *NV = FoldOpIntoPhi(I))
1367 return NV;
1368 }
1369
1370 // See if we can fold away this rem instruction.
1371 if (SimplifyDemandedInstructionBits(I))
1372 return &I;
1373 }
1374 }
1375
Craig Topperf40110f2014-04-25 05:29:35 +00001376 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001377}
1378
1379Instruction *InstCombiner::visitURem(BinaryOperator &I) {
1380 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1381
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001382 if (Value *V = SimplifyVectorOp(I))
1383 return ReplaceInstUsesWith(I, V);
1384
Chandler Carruth66b31302015-01-04 12:03:27 +00001385 if (Value *V = SimplifyURemInst(Op0, Op1, DL, TLI, DT, AC))
Duncan Sandsa3e36992011-05-02 16:27:02 +00001386 return ReplaceInstUsesWith(I, V);
1387
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001388 if (Instruction *common = commonIRemTransforms(I))
1389 return common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001390
David Majnemer6c30f492013-05-12 00:07:05 +00001391 // (zext A) urem (zext B) --> zext (A urem B)
1392 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1393 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
1394 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
1395 I.getType());
1396
David Majnemer470b0772013-05-11 09:01:28 +00001397 // X urem Y -> X and Y-1, where Y is a power of 2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001398 if (isKnownToBeAPowerOfTwo(Op1, DL, /*OrZero*/ true, 0, AC, &I, DT)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +00001399 Constant *N1 = Constant::getAllOnesValue(I.getType());
Benjamin Kramer547b6c52011-09-27 20:39:19 +00001400 Value *Add = Builder->CreateAdd(Op1, N1);
Chris Lattner6b657ae2011-02-10 05:36:31 +00001401 return BinaryOperator::CreateAnd(Op0, Add);
1402 }
1403
Nick Lewycky7459be62013-07-13 01:16:47 +00001404 // 1 urem X -> zext(X != 1)
1405 if (match(Op0, m_One())) {
1406 Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
1407 Value *Ext = Builder->CreateZExt(Cmp, I.getType());
1408 return ReplaceInstUsesWith(I, Ext);
1409 }
1410
Craig Topperf40110f2014-04-25 05:29:35 +00001411 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001412}
1413
1414Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
1415 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1416
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001417 if (Value *V = SimplifyVectorOp(I))
1418 return ReplaceInstUsesWith(I, V);
1419
Chandler Carruth66b31302015-01-04 12:03:27 +00001420 if (Value *V = SimplifySRemInst(Op0, Op1, DL, TLI, DT, AC))
Duncan Sandsa3e36992011-05-02 16:27:02 +00001421 return ReplaceInstUsesWith(I, V);
1422
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001423 // Handle the integer rem common cases
1424 if (Instruction *Common = commonIRemTransforms(I))
1425 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001426
David Majnemerdb077302014-10-13 22:37:51 +00001427 {
1428 const APInt *Y;
1429 // X % -Y -> X % Y
1430 if (match(Op1, m_APInt(Y)) && Y->isNegative() && !Y->isMinSignedValue()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001431 Worklist.AddValue(I.getOperand(1));
David Majnemerdb077302014-10-13 22:37:51 +00001432 I.setOperand(1, ConstantInt::get(I.getType(), -*Y));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001433 return &I;
1434 }
David Majnemerdb077302014-10-13 22:37:51 +00001435 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001436
1437 // If the sign bits of both operands are zero (i.e. we can prove they are
1438 // unsigned inputs), turn this into a urem.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001439 if (I.getType()->isIntegerTy()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001440 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
Hal Finkel60db0582014-09-07 18:57:58 +00001441 if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
1442 MaskedValueIsZero(Op0, Mask, 0, &I)) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001443 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001444 return BinaryOperator::CreateURem(Op0, Op1, I.getName());
1445 }
1446 }
1447
1448 // If it's a constant vector, flip any negative values positive.
Chris Lattner0256be92012-01-27 03:08:05 +00001449 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1450 Constant *C = cast<Constant>(Op1);
1451 unsigned VWidth = C->getType()->getVectorNumElements();
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001452
1453 bool hasNegative = false;
Chris Lattner0256be92012-01-27 03:08:05 +00001454 bool hasMissing = false;
1455 for (unsigned i = 0; i != VWidth; ++i) {
1456 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001457 if (!Elt) {
Chris Lattner0256be92012-01-27 03:08:05 +00001458 hasMissing = true;
1459 break;
1460 }
1461
1462 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
Chris Lattnerb1a15122011-07-15 06:08:15 +00001463 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001464 hasNegative = true;
Chris Lattner0256be92012-01-27 03:08:05 +00001465 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001466
Chris Lattner0256be92012-01-27 03:08:05 +00001467 if (hasNegative && !hasMissing) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001468 SmallVector<Constant *, 16> Elts(VWidth);
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001469 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner8213c8a2012-02-06 21:56:39 +00001470 Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
Chris Lattner0256be92012-01-27 03:08:05 +00001471 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
Chris Lattnerb1a15122011-07-15 06:08:15 +00001472 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001473 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001474 }
1475 }
1476
1477 Constant *NewRHSV = ConstantVector::get(Elts);
Chris Lattner0256be92012-01-27 03:08:05 +00001478 if (NewRHSV != C) { // Don't loop on -MININT
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001479 Worklist.AddValue(I.getOperand(1));
1480 I.setOperand(1, NewRHSV);
1481 return &I;
1482 }
1483 }
1484 }
1485
Craig Topperf40110f2014-04-25 05:29:35 +00001486 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001487}
1488
1489Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
Duncan Sandsa3e36992011-05-02 16:27:02 +00001490 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001491
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001492 if (Value *V = SimplifyVectorOp(I))
1493 return ReplaceInstUsesWith(I, V);
1494
Mehdi Aminicd3ca6f2015-02-23 18:30:25 +00001495 if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(),
1496 DL, TLI, DT, AC))
Duncan Sandsa3e36992011-05-02 16:27:02 +00001497 return ReplaceInstUsesWith(I, V);
1498
1499 // Handle cases involving: rem X, (select Cond, Y, Z)
1500 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1501 return &I;
1502
Craig Topperf40110f2014-04-25 05:29:35 +00001503 return nullptr;
Duncan Sandsa3e36992011-05-02 16:27:02 +00001504}