blob: ca67348d05c17e067bb71f1174be448b1dad9f24 [file] [log] [blame]
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001//===- InstCombineMulDivRem.cpp -------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerdc054bf2010-01-05 06:09:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
10// srem, urem, frem.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/SmallVector.h"
Duncan Sandsd0eb6d32010-12-21 14:00:22 +000018#include "llvm/Analysis/InstructionSimplify.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000019#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/Constant.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/Instruction.h"
24#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000026#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000028#include "llvm/IR/PatternMatch.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000029#include "llvm/IR/Type.h"
30#include "llvm/IR/Value.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/KnownBits.h"
34#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
Dmitry Venikove5fbf592018-01-11 06:33:00 +000035#include "llvm/Transforms/Utils/BuildLibCalls.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000036#include <cassert>
37#include <cstddef>
38#include <cstdint>
39#include <utility>
40
Chris Lattnerdc054bf2010-01-05 06:09:35 +000041using namespace llvm;
42using namespace PatternMatch;
43
Chandler Carruth964daaa2014-04-22 02:55:47 +000044#define DEBUG_TYPE "instcombine"
45
Sanjay Patel6eccf482015-09-09 15:24:36 +000046/// The specific integer value is used in a context where it is known to be
47/// non-zero. If this allows us to simplify the computation, do so and return
48/// the new operand, otherwise return null.
Hal Finkel60db0582014-09-07 18:57:58 +000049static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000050 Instruction &CxtI) {
Chris Lattner7c99f192011-05-22 18:18:41 +000051 // If V has multiple uses, then we would have to do more analysis to determine
52 // if this is safe. For example, the use could be in dynamically unreached
53 // code.
Craig Topperf40110f2014-04-25 05:29:35 +000054 if (!V->hasOneUse()) return nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +000055
Chris Lattner388cb8a2011-05-23 00:32:19 +000056 bool MadeChange = false;
57
Chris Lattner7c99f192011-05-22 18:18:41 +000058 // ((1 << A) >>u B) --> (1 << (A-B))
59 // Because V cannot be zero, we know that B is less than A.
David Majnemerdad21032014-10-14 20:28:40 +000060 Value *A = nullptr, *B = nullptr, *One = nullptr;
61 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) &&
62 match(One, m_One())) {
Craig Topperbb4069e2017-07-07 23:16:26 +000063 A = IC.Builder.CreateSub(A, B);
64 return IC.Builder.CreateShl(One, A);
Chris Lattner7c99f192011-05-22 18:18:41 +000065 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +000066
Chris Lattner388cb8a2011-05-23 00:32:19 +000067 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
68 // inexact. Similarly for <<.
Sanjay Patela8ef4a52016-05-22 17:08:52 +000069 BinaryOperator *I = dyn_cast<BinaryOperator>(V);
70 if (I && I->isLogicalShift() &&
Craig Topperd4039f72017-05-25 21:51:12 +000071 IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) {
Sanjay Patela8ef4a52016-05-22 17:08:52 +000072 // We know that this is an exact/nuw shift and that the input is a
73 // non-zero context as well.
74 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
75 I->setOperand(0, V2);
76 MadeChange = true;
Chris Lattner388cb8a2011-05-23 00:32:19 +000077 }
78
Sanjay Patela8ef4a52016-05-22 17:08:52 +000079 if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
80 I->setIsExact();
81 MadeChange = true;
82 }
83
84 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
85 I->setHasNoUnsignedWrap();
86 MadeChange = true;
87 }
88 }
89
Chris Lattner162dfc32011-05-22 18:26:48 +000090 // TODO: Lots more we could do here:
Chris Lattner162dfc32011-05-22 18:26:48 +000091 // If V is a phi node, we can call this on each of its operands.
92 // "select cond, X, 0" can simplify to "X".
Jim Grosbachbdbd7342013-04-05 21:20:12 +000093
Craig Topperf40110f2014-04-25 05:29:35 +000094 return MadeChange ? V : nullptr;
Chris Lattner7c99f192011-05-22 18:18:41 +000095}
96
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000097/// A helper routine of InstCombiner::visitMul().
Rafael Espindola65281bf2013-05-31 14:27:15 +000098///
Simon Pilgrim0b9f3912018-02-08 14:10:01 +000099/// If C is a scalar/vector of known powers of 2, then this function returns
100/// a new scalar/vector obtained from logBase2 of C.
Rafael Espindola65281bf2013-05-31 14:27:15 +0000101/// Return a null pointer otherwise.
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000102static Constant *getLogBase2(Type *Ty, Constant *C) {
Rafael Espindola65281bf2013-05-31 14:27:15 +0000103 const APInt *IVal;
Simon Pilgrimbe0dd722018-02-13 13:16:26 +0000104 if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
105 return ConstantInt::get(Ty, IVal->logBase2());
Rafael Espindola65281bf2013-05-31 14:27:15 +0000106
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000107 if (!Ty->isVectorTy())
108 return nullptr;
109
110 SmallVector<Constant *, 4> Elts;
111 for (unsigned I = 0, E = Ty->getVectorNumElements(); I != E; ++I) {
112 Constant *Elt = C->getAggregateElement(I);
113 if (!Elt)
114 return nullptr;
115 if (isa<UndefValue>(Elt)) {
116 Elts.push_back(UndefValue::get(Ty->getScalarType()));
117 continue;
118 }
Rafael Espindola65281bf2013-05-31 14:27:15 +0000119 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
Craig Topperf40110f2014-04-25 05:29:35 +0000120 return nullptr;
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000121 Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));
Rafael Espindola65281bf2013-05-31 14:27:15 +0000122 }
123
124 return ConstantVector::get(Elts);
125}
126
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000127Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000128 if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1),
129 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000130 return replaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000131
Sanjay Patel70043b72018-07-13 01:18:07 +0000132 if (SimplifyAssociativeOrCommutative(I))
133 return &I;
134
Sanjay Patel79dceb22018-10-03 15:20:58 +0000135 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000136 return X;
137
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000138 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000139 return replaceInstUsesWith(I, V);
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000140
David Majnemer027bc802014-11-22 04:52:38 +0000141 // X * -1 == 0 - X
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000142 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
David Majnemer027bc802014-11-22 04:52:38 +0000143 if (match(Op1, m_AllOnes())) {
144 BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
145 if (I.hasNoSignedWrap())
146 BO->setHasNoSignedWrap();
147 return BO;
148 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000149
Rafael Espindola65281bf2013-05-31 14:27:15 +0000150 // Also allow combining multiply instructions on vectors.
151 {
152 Value *NewOp;
153 Constant *C1, *C2;
154 const APInt *IVal;
155 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
156 m_Constant(C1))) &&
David Majnemerfd4a6d22014-11-22 04:52:52 +0000157 match(C1, m_APInt(IVal))) {
158 // ((X << C2)*C1) == (X * (C1 << C2))
159 Constant *Shl = ConstantExpr::getShl(C1, C2);
160 BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
161 BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
162 if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
163 BO->setHasNoUnsignedWrap();
164 if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
165 Shl->isNotMinSignedValue())
166 BO->setHasNoSignedWrap();
167 return BO;
168 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000169
Rafael Espindola65281bf2013-05-31 14:27:15 +0000170 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000171 // Replace X*(2^C) with X << C, where C is either a scalar or a vector.
172 if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) {
Rafael Espindola65281bf2013-05-31 14:27:15 +0000173 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000174
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000175 if (I.hasNoUnsignedWrap())
176 Shl->setHasNoUnsignedWrap();
David Majnemer45951a62015-04-18 04:41:30 +0000177 if (I.hasNoSignedWrap()) {
Craig Topper5fe01972017-06-27 19:57:53 +0000178 const APInt *V;
Craig Topper4e63db82018-09-11 17:57:20 +0000179 if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1)
David Majnemer45951a62015-04-18 04:41:30 +0000180 Shl->setHasNoSignedWrap();
181 }
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000182
Rafael Espindola65281bf2013-05-31 14:27:15 +0000183 return Shl;
184 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000185 }
Rafael Espindola65281bf2013-05-31 14:27:15 +0000186 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000187
Rafael Espindola65281bf2013-05-31 14:27:15 +0000188 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Stuart Hastings23804832011-06-01 16:42:47 +0000189 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
190 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
191 // The "* (2**n)" thus becomes a potential shifting opportunity.
Stuart Hastings82843742011-05-30 20:00:33 +0000192 {
193 const APInt & Val = CI->getValue();
194 const APInt &PosVal = Val.abs();
195 if (Val.isNegative() && PosVal.isPowerOf2()) {
Craig Topperf40110f2014-04-25 05:29:35 +0000196 Value *X = nullptr, *Y = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000197 if (Op0->hasOneUse()) {
198 ConstantInt *C1;
Craig Topperf40110f2014-04-25 05:29:35 +0000199 Value *Sub = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000200 if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
Craig Topperbb4069e2017-07-07 23:16:26 +0000201 Sub = Builder.CreateSub(X, Y, "suba");
Stuart Hastings23804832011-06-01 16:42:47 +0000202 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
Craig Topperbb4069e2017-07-07 23:16:26 +0000203 Sub = Builder.CreateSub(Builder.CreateNeg(C1), Y, "subc");
Stuart Hastings23804832011-06-01 16:42:47 +0000204 if (Sub)
205 return
206 BinaryOperator::CreateMul(Sub,
207 ConstantInt::get(Y->getType(), PosVal));
Stuart Hastings82843742011-05-30 20:00:33 +0000208 }
209 }
210 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000211 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000212
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000213 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
214 return FoldedMul;
215
Chris Lattner6b657ae2011-02-10 05:36:31 +0000216 // Simplify mul instructions with a constant RHS.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000217 if (isa<Constant>(Op1)) {
Benjamin Kramer72196f32014-01-19 15:24:22 +0000218 // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000219 Value *X;
220 Constant *C1;
221 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
222 Value *Mul = Builder.CreateMul(C1, Op1);
223 // Only go forward with the transform if C1*CI simplifies to a tidier
224 // constant.
225 if (!match(Mul, m_Mul(m_Value(), m_Value())))
226 return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul);
Benjamin Kramer72196f32014-01-19 15:24:22 +0000227 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000228 }
229
Sanjay Patel604cb9e2018-02-14 16:50:55 +0000230 // -X * C --> X * -C
231 Value *X, *Y;
232 Constant *Op1C;
233 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C)))
234 return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C));
235
236 // -X * -Y --> X * Y
237 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) {
238 auto *NewMul = BinaryOperator::CreateMul(X, Y);
239 if (I.hasNoSignedWrap() &&
240 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
241 cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap())
242 NewMul->setHasNoSignedWrap();
243 return NewMul;
David Majnemer8279a7502014-11-22 07:25:19 +0000244 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000245
Chen Zheng4952e662019-01-01 01:09:20 +0000246 // -X * Y --> -(X * Y)
247 // X * -Y --> -(X * Y)
248 if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
249 return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
250
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000251 // (X / Y) * Y = X - (X % Y)
252 // (X / Y) * -Y = (X % Y) - X
253 {
Sanjay Patela0a56822017-03-14 17:27:27 +0000254 Value *Y = Op1;
255 BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0);
256 if (!Div || (Div->getOpcode() != Instruction::UDiv &&
257 Div->getOpcode() != Instruction::SDiv)) {
258 Y = Op0;
259 Div = dyn_cast<BinaryOperator>(Op1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000260 }
Sanjay Patela0a56822017-03-14 17:27:27 +0000261 Value *Neg = dyn_castNegVal(Y);
262 if (Div && Div->hasOneUse() &&
263 (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) &&
264 (Div->getOpcode() == Instruction::UDiv ||
265 Div->getOpcode() == Instruction::SDiv)) {
266 Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000267
Chris Lattner35315d02011-02-06 21:44:57 +0000268 // If the division is exact, X % Y is zero, so we end up with X or -X.
Sanjay Patela0a56822017-03-14 17:27:27 +0000269 if (Div->isExact()) {
270 if (DivOp1 == Y)
271 return replaceInstUsesWith(I, X);
272 return BinaryOperator::CreateNeg(X);
273 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000274
Sanjay Patela0a56822017-03-14 17:27:27 +0000275 auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem
276 : Instruction::SRem;
Craig Topperbb4069e2017-07-07 23:16:26 +0000277 Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1);
Sanjay Patela0a56822017-03-14 17:27:27 +0000278 if (DivOp1 == Y)
279 return BinaryOperator::CreateSub(X, Rem);
280 return BinaryOperator::CreateSub(Rem, X);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000281 }
282 }
283
284 /// i1 mul -> i1 and.
Craig Topperfde47232017-07-09 07:04:03 +0000285 if (I.getType()->isIntOrIntVectorTy(1))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000286 return BinaryOperator::CreateAnd(Op0, Op1);
287
288 // X*(1 << Y) --> X << Y
289 // (1 << Y)*X --> X << Y
290 {
291 Value *Y;
David Majnemer546f8102014-11-22 08:57:02 +0000292 BinaryOperator *BO = nullptr;
293 bool ShlNSW = false;
294 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
295 BO = BinaryOperator::CreateShl(Op1, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000296 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
David Majnemer8e6f6a92014-11-24 16:41:13 +0000297 } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
David Majnemer546f8102014-11-22 08:57:02 +0000298 BO = BinaryOperator::CreateShl(Op0, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000299 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
David Majnemer546f8102014-11-22 08:57:02 +0000300 }
301 if (BO) {
302 if (I.hasNoUnsignedWrap())
303 BO->setHasNoUnsignedWrap();
304 if (I.hasNoSignedWrap() && ShlNSW)
305 BO->setHasNoSignedWrap();
306 return BO;
307 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000308 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000309
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000310 // (bool X) * Y --> X ? Y : 0
Sanjay Patel7558d862018-02-13 22:24:37 +0000311 // Y * (bool X) --> X ? Y : 0
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000312 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
313 return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0));
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000314 if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
315 return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0));
316
Sanjay Patel7558d862018-02-13 22:24:37 +0000317 // (lshr X, 31) * Y --> (ashr X, 31) & Y
318 // Y * (lshr X, 31) --> (ashr X, 31) & Y
319 // TODO: We are not checking one-use because the elimination of the multiply
320 // is better for analysis?
321 // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be
322 // more similar to what we're doing above.
323 const APInt *C;
324 if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
325 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1);
326 if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
327 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000328
Sanjay Patel90a36342018-09-14 22:23:35 +0000329 if (Instruction *Ext = narrowMathIfNoOverflow(I))
330 return Ext;
David Majnemera1cfd7c2016-12-30 00:28:58 +0000331
Sanjay Patel70043b72018-07-13 01:18:07 +0000332 bool Changed = false;
Craig Topper2b1fc322017-05-22 06:25:31 +0000333 if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) {
David Majnemer54c2ca22014-12-26 09:10:14 +0000334 Changed = true;
335 I.setHasNoSignedWrap(true);
336 }
337
Craig Topperbb973722017-05-15 02:44:08 +0000338 if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) {
David Majnemerb1296ec2014-12-26 09:50:35 +0000339 Changed = true;
340 I.setHasNoUnsignedWrap(true);
341 }
342
Craig Topperf40110f2014-04-25 05:29:35 +0000343 return Changed ? &I : nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000344}
345
346Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000347 if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1),
348 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +0000349 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000350 return replaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000351
Sanjay Patel70043b72018-07-13 01:18:07 +0000352 if (SimplifyAssociativeOrCommutative(I))
353 return &I;
354
Sanjay Patel79dceb22018-10-03 15:20:58 +0000355 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000356 return X;
357
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000358 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
359 return FoldedMul;
360
Sanjay Patele29375d2018-03-02 23:06:45 +0000361 // X * -1.0 --> -X
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000362 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patele29375d2018-03-02 23:06:45 +0000363 if (match(Op1, m_SpecificFP(-1.0)))
364 return BinaryOperator::CreateFNegFMF(Op0, &I);
Sanjay Patel6b9c7a92018-02-23 17:14:28 +0000365
Sanjay Patele29375d2018-03-02 23:06:45 +0000366 // -X * -Y --> X * Y
367 Value *X, *Y;
368 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
369 return BinaryOperator::CreateFMulFMF(X, Y, &I);
Owen Andersonf74cfe02014-01-16 20:36:42 +0000370
Sanjay Patele29375d2018-03-02 23:06:45 +0000371 // -X * C --> X * -C
372 Constant *C;
373 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C)))
374 return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000375
Sanjay Patele29375d2018-03-02 23:06:45 +0000376 // Sink negation: -X * Y --> -(X * Y)
377 if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))))
378 return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op1, &I), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000379
Sanjay Patele29375d2018-03-02 23:06:45 +0000380 // Sink negation: Y * -X --> -(X * Y)
381 if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))))
382 return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op0, &I), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000383
Sanjay Patele29375d2018-03-02 23:06:45 +0000384 // fabs(X) * fabs(X) -> X * X
385 if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X))))
386 return BinaryOperator::CreateFMulFMF(X, X, &I);
387
388 // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E)
389 if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
390 return replaceInstUsesWith(I, V);
391
Sanjay Patel81b3b102018-04-03 22:19:19 +0000392 if (I.hasAllowReassoc()) {
393 // Reassociate constant RHS with another constant to form constant
394 // expression.
395 if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) {
396 Constant *C1;
397 if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) {
398 // (C1 / X) * C --> (C * C1) / X
399 Constant *CC1 = ConstantExpr::getFMul(C, C1);
400 if (CC1->isNormalFP())
401 return BinaryOperator::CreateFDivFMF(CC1, X, &I);
402 }
403 if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
404 // (X / C1) * C --> X * (C / C1)
405 Constant *CDivC1 = ConstantExpr::getFDiv(C, C1);
406 if (CDivC1->isNormalFP())
407 return BinaryOperator::CreateFMulFMF(X, CDivC1, &I);
Sanjay Patel204edec2018-03-13 14:46:32 +0000408
Sanjay Patel81b3b102018-04-03 22:19:19 +0000409 // If the constant was a denormal, try reassociating differently.
410 // (X / C1) * C --> X / (C1 / C)
411 Constant *C1DivC = ConstantExpr::getFDiv(C1, C);
412 if (Op0->hasOneUse() && C1DivC->isNormalFP())
413 return BinaryOperator::CreateFDivFMF(X, C1DivC, &I);
414 }
415
416 // We do not need to match 'fadd C, X' and 'fsub X, C' because they are
417 // canonicalized to 'fadd X, C'. Distributing the multiply may allow
418 // further folds and (X * C) + C2 is 'fma'.
419 if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) {
420 // (X + C1) * C --> (X * C) + (C * C1)
421 Constant *CC1 = ConstantExpr::getFMul(C, C1);
422 Value *XC = Builder.CreateFMulFMF(X, C, &I);
423 return BinaryOperator::CreateFAddFMF(XC, CC1, &I);
424 }
425 if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) {
426 // (C1 - X) * C --> (C * C1) - (X * C)
427 Constant *CC1 = ConstantExpr::getFMul(C, C1);
428 Value *XC = Builder.CreateFMulFMF(X, C, &I);
429 return BinaryOperator::CreateFSubFMF(CC1, XC, &I);
430 }
Sanjay Patel204edec2018-03-13 14:46:32 +0000431 }
432
Sanjay Patel81b3b102018-04-03 22:19:19 +0000433 // sqrt(X) * sqrt(Y) -> sqrt(X * Y)
434 // nnan disallows the possibility of returning a number if both operands are
435 // negative (in that case, we should return NaN).
436 if (I.hasNoNaNs() &&
437 match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) &&
438 match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
439 Value *XY = Builder.CreateFMulFMF(X, Y, &I);
Neil Henning57f5d0a2018-10-08 10:32:33 +0000440 Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I);
Sanjay Patel81b3b102018-04-03 22:19:19 +0000441 return replaceInstUsesWith(I, Sqrt);
Sanjay Patel4fd4fd62018-03-26 15:03:57 +0000442 }
Sanjay Patel81b3b102018-04-03 22:19:19 +0000443
444 // (X*Y) * X => (X*X) * Y where Y != X
445 // The purpose is two-fold:
446 // 1) to form a power expression (of X).
447 // 2) potentially shorten the critical path: After transformation, the
448 // latency of the instruction Y is amortized by the expression of X*X,
449 // and therefore Y is in a "less critical" position compared to what it
450 // was before the transformation.
451 if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) &&
452 Op1 != Y) {
453 Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I);
454 return BinaryOperator::CreateFMulFMF(XX, Y, &I);
455 }
456 if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) &&
457 Op0 != Y) {
458 Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I);
459 return BinaryOperator::CreateFMulFMF(XX, Y, &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000460 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000461 }
462
Sanjay Patel2fd0acf2018-03-02 20:32:46 +0000463 // log2(X * 0.5) * Y = log2(X) * Y - Y
464 if (I.isFast()) {
465 IntrinsicInst *Log2 = nullptr;
466 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>(
467 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
468 Log2 = cast<IntrinsicInst>(Op0);
469 Y = Op1;
Pedro Artigasd8795042012-11-30 19:09:41 +0000470 }
Sanjay Patel2fd0acf2018-03-02 20:32:46 +0000471 if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>(
472 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
473 Log2 = cast<IntrinsicInst>(Op1);
474 Y = Op0;
475 }
476 if (Log2) {
477 Log2->setArgOperand(0, X);
478 Log2->copyFastMathFlags(&I);
479 Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I);
480 return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I);
Pedro Artigasd8795042012-11-30 19:09:41 +0000481 }
482 }
483
Sanjay Patel70043b72018-07-13 01:18:07 +0000484 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000485}
486
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000487/// Fold a divide or remainder with a select instruction divisor when one of the
488/// select operands is zero. In that case, we can use the other select operand
489/// because div/rem by zero is undefined.
490bool InstCombiner::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) {
491 SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1));
492 if (!SI)
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000493 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000494
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000495 int NonNullOperand;
496 if (match(SI->getTrueValue(), m_Zero()))
497 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
498 NonNullOperand = 2;
499 else if (match(SI->getFalseValue(), m_Zero()))
500 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
501 NonNullOperand = 1;
502 else
503 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000504
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000505 // Change the div/rem to use 'Y' instead of the select.
506 I.setOperand(1, SI->getOperand(NonNullOperand));
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000507
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000508 // Okay, we know we replace the operand of the div/rem with 'Y' with no
509 // problem. However, the select, or the condition of the select may have
510 // multiple uses. Based on our knowledge that the operand must be non-zero,
511 // propagate the known value for the select into other uses of it, and
512 // propagate a known value of the condition into its other users.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000513
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000514 // If the select and condition only have a single use, don't bother with this,
515 // early exit.
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000516 Value *SelectCond = SI->getCondition();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000517 if (SI->use_empty() && SelectCond->hasOneUse())
518 return true;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000519
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000520 // Scan the current block backward, looking for other uses of SI.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000521 BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin();
Sanjay Patel72d339a2017-10-06 23:43:06 +0000522 Type *CondTy = SelectCond->getType();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000523 while (BBI != BBFront) {
524 --BBI;
Serguei Katkovd894fb42018-06-04 02:52:36 +0000525 // If we found an instruction that we can't assume will return, so
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000526 // information from below it cannot be propagated above it.
Serguei Katkovd894fb42018-06-04 02:52:36 +0000527 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000528 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000529
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000530 // Replace uses of the select or its condition with the known values.
531 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
532 I != E; ++I) {
533 if (*I == SI) {
534 *I = SI->getOperand(NonNullOperand);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000535 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000536 } else if (*I == SelectCond) {
Sanjay Patel72d339a2017-10-06 23:43:06 +0000537 *I = NonNullOperand == 1 ? ConstantInt::getTrue(CondTy)
538 : ConstantInt::getFalse(CondTy);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000539 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000540 }
541 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000542
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000543 // If we past the instruction, quit looking for it.
544 if (&*BBI == SI)
Craig Topperf40110f2014-04-25 05:29:35 +0000545 SI = nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000546 if (&*BBI == SelectCond)
Craig Topperf40110f2014-04-25 05:29:35 +0000547 SelectCond = nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000548
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000549 // If we ran out of things to eliminate, break out of the loop.
Craig Topperf40110f2014-04-25 05:29:35 +0000550 if (!SelectCond && !SI)
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000551 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000552
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000553 }
554 return true;
555}
556
Sanjay Patel1998cc62018-02-12 18:38:35 +0000557/// True if the multiply can not be expressed in an int this size.
558static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
559 bool IsSigned) {
560 bool Overflow;
561 Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow);
562 return Overflow;
563}
564
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000565/// True if C1 is a multiple of C2. Quotient contains C1/C2.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000566static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
567 bool IsSigned) {
568 assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal");
569
570 // Bail if we will divide by zero.
571 if (C2.isNullValue())
572 return false;
573
574 // Bail if we would divide INT_MIN by -1.
575 if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue())
576 return false;
577
578 APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
579 if (IsSigned)
580 APInt::sdivrem(C1, C2, Quotient, Remainder);
581 else
582 APInt::udivrem(C1, C2, Quotient, Remainder);
583
584 return Remainder.isMinValue();
585}
586
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000587/// This function implements the transforms common to both integer division
588/// instructions (udiv and sdiv). It is called by the visitors to those integer
589/// division instructions.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +0000590/// Common integer divide transforms
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000591Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
592 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel9530f182018-01-21 16:14:51 +0000593 bool IsSigned = I.getOpcode() == Instruction::SDiv;
Sanjay Patel39059d22018-02-12 14:14:56 +0000594 Type *Ty = I.getType();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000595
Chris Lattner7c99f192011-05-22 18:18:41 +0000596 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000597 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +0000598 I.setOperand(1, V);
599 return &I;
600 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000601
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000602 // Handle cases involving: [su]div X, (select Cond, Y, Z)
603 // This does not apply for fdiv.
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000604 if (simplifyDivRemOfSelectWithZeroOp(I))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000605 return &I;
606
Sanjay Patel1998cc62018-02-12 18:38:35 +0000607 const APInt *C2;
608 if (match(Op1, m_APInt(C2))) {
609 Value *X;
610 const APInt *C1;
David Majnemerf9a095d2014-08-16 08:55:06 +0000611
Sanjay Patel1998cc62018-02-12 18:38:35 +0000612 // (X / C1) / C2 -> X / (C1*C2)
613 if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) ||
614 (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) {
615 APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
616 if (!multiplyOverflows(*C1, *C2, Product, IsSigned))
617 return BinaryOperator::Create(I.getOpcode(), X,
618 ConstantInt::get(Ty, Product));
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000619 }
Sanjay Patel1998cc62018-02-12 18:38:35 +0000620
621 if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
622 (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) {
623 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
624
625 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
626 if (isMultiple(*C2, *C1, Quotient, IsSigned)) {
627 auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X,
628 ConstantInt::get(Ty, Quotient));
629 NewDiv->setIsExact(I.isExact());
630 return NewDiv;
631 }
632
633 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
634 if (isMultiple(*C1, *C2, Quotient, IsSigned)) {
635 auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
636 ConstantInt::get(Ty, Quotient));
637 auto *OBO = cast<OverflowingBinaryOperator>(Op0);
638 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
639 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
640 return Mul;
641 }
642 }
643
644 if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) &&
645 *C1 != C1->getBitWidth() - 1) ||
646 (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))))) {
647 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
648 APInt C1Shifted = APInt::getOneBitSet(
649 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
650
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000651 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000652 if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
653 auto *BO = BinaryOperator::Create(I.getOpcode(), X,
654 ConstantInt::get(Ty, Quotient));
655 BO->setIsExact(I.isExact());
656 return BO;
657 }
658
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000659 // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000660 if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
661 auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
662 ConstantInt::get(Ty, Quotient));
663 auto *OBO = cast<OverflowingBinaryOperator>(Op0);
664 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
665 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
666 return Mul;
667 }
668 }
669
670 if (!C2->isNullValue()) // avoid X udiv 0
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000671 if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I))
Sanjay Patel1998cc62018-02-12 18:38:35 +0000672 return FoldedDiv;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000673 }
674
Craig Topper218a3592017-04-17 03:41:47 +0000675 if (match(Op0, m_One())) {
Sanjay Patel39059d22018-02-12 14:14:56 +0000676 assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?");
677 if (IsSigned) {
Craig Topper218a3592017-04-17 03:41:47 +0000678 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
679 // result is one, if Op1 is -1 then the result is minus one, otherwise
680 // it's zero.
Craig Topperbb4069e2017-07-07 23:16:26 +0000681 Value *Inc = Builder.CreateAdd(Op1, Op0);
Sanjay Patel39059d22018-02-12 14:14:56 +0000682 Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3));
683 return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0));
Craig Topper218a3592017-04-17 03:41:47 +0000684 } else {
685 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
686 // result is one, otherwise it's zero.
Sanjay Patel39059d22018-02-12 14:14:56 +0000687 return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty);
Nick Lewyckyf0cf8fa2014-05-14 03:03:05 +0000688 }
689 }
690
Benjamin Kramer57b3df52011-04-30 18:16:00 +0000691 // See if we can fold away this div instruction.
692 if (SimplifyDemandedInstructionBits(I))
693 return &I;
694
Duncan Sands771e82a2011-01-28 16:51:11 +0000695 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
Sanjay Patel9530f182018-01-21 16:14:51 +0000696 Value *X, *Z;
697 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1
698 if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
699 (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
Duncan Sands771e82a2011-01-28 16:51:11 +0000700 return BinaryOperator::Create(I.getOpcode(), X, Op1);
Sanjay Patel9530f182018-01-21 16:14:51 +0000701
702 // (X << Y) / X -> 1 << Y
703 Value *Y;
704 if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y))))
Sanjay Patel39059d22018-02-12 14:14:56 +0000705 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y);
Sanjay Patel9530f182018-01-21 16:14:51 +0000706 if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y))))
Sanjay Patel39059d22018-02-12 14:14:56 +0000707 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000708
Sanjay Patel510d6472018-02-11 17:20:32 +0000709 // X / (X * Y) -> 1 / Y if the multiplication does not overflow.
710 if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) {
711 bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
712 bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
713 if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) {
Sanjay Patel39059d22018-02-12 14:14:56 +0000714 I.setOperand(0, ConstantInt::get(Ty, 1));
Sanjay Patel510d6472018-02-11 17:20:32 +0000715 I.setOperand(1, Y);
716 return &I;
717 }
718 }
719
Craig Topperf40110f2014-04-25 05:29:35 +0000720 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000721}
722
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000723static const unsigned MaxDepth = 6;
724
David Majnemer37f8f442013-07-04 21:17:49 +0000725namespace {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000726
727using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1,
728 const BinaryOperator &I,
729 InstCombiner &IC);
David Majnemer37f8f442013-07-04 21:17:49 +0000730
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000731/// Used to maintain state for visitUDivOperand().
David Majnemer37f8f442013-07-04 21:17:49 +0000732struct UDivFoldAction {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000733 /// Informs visitUDiv() how to fold this operand. This can be zero if this
734 /// action joins two actions together.
735 FoldUDivOperandCb FoldAction;
David Majnemer37f8f442013-07-04 21:17:49 +0000736
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000737 /// Which operand to fold.
738 Value *OperandToFold;
739
David Majnemer37f8f442013-07-04 21:17:49 +0000740 union {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000741 /// The instruction returned when FoldAction is invoked.
742 Instruction *FoldResult;
David Majnemer37f8f442013-07-04 21:17:49 +0000743
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000744 /// Stores the LHS action index if this action joins two actions together.
745 size_t SelectLHSIdx;
David Majnemer37f8f442013-07-04 21:17:49 +0000746 };
747
748 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
Craig Topperf40110f2014-04-25 05:29:35 +0000749 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
David Majnemer37f8f442013-07-04 21:17:49 +0000750 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
751 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
752};
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000753
754} // end anonymous namespace
David Majnemer37f8f442013-07-04 21:17:49 +0000755
756// X udiv 2^C -> X >> C
757static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
758 const BinaryOperator &I, InstCombiner &IC) {
Simon Pilgrim94cc89d2018-02-08 14:46:10 +0000759 Constant *C1 = getLogBase2(Op0->getType(), cast<Constant>(Op1));
760 if (!C1)
761 llvm_unreachable("Failed to constant fold udiv -> logbase2");
762 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1);
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000763 if (I.isExact())
764 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000765 return LShr;
766}
767
David Majnemer37f8f442013-07-04 21:17:49 +0000768// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000769// X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2)
David Majnemer37f8f442013-07-04 21:17:49 +0000770static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
771 InstCombiner &IC) {
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000772 Value *ShiftLeft;
773 if (!match(Op1, m_ZExt(m_Value(ShiftLeft))))
774 ShiftLeft = Op1;
David Majnemer37f8f442013-07-04 21:17:49 +0000775
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000776 Constant *CI;
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000777 Value *N;
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000778 if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N))))
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000779 llvm_unreachable("match should never fail here!");
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000780 Constant *Log2Base = getLogBase2(N->getType(), CI);
781 if (!Log2Base)
782 llvm_unreachable("getLogBase2 should never fail here!");
783 N = IC.Builder.CreateAdd(N, Log2Base);
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000784 if (Op1 != ShiftLeft)
Craig Topperbb4069e2017-07-07 23:16:26 +0000785 N = IC.Builder.CreateZExt(N, Op1->getType());
David Majnemer37f8f442013-07-04 21:17:49 +0000786 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000787 if (I.isExact())
788 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000789 return LShr;
790}
791
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000792// Recursively visits the possible right hand operands of a udiv
David Majnemer37f8f442013-07-04 21:17:49 +0000793// instruction, seeing through select instructions, to determine if we can
794// replace the udiv with something simpler. If we find that an operand is not
795// able to simplify the udiv, we abort the entire transformation.
796static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
797 SmallVectorImpl<UDivFoldAction> &Actions,
798 unsigned Depth = 0) {
799 // Check to see if this is an unsigned division with an exact power of 2,
800 // if so, convert to a right shift.
801 if (match(Op1, m_Power2())) {
802 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
803 return Actions.size();
804 }
805
David Majnemer37f8f442013-07-04 21:17:49 +0000806 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
807 if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
808 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
809 Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
810 return Actions.size();
811 }
812
813 // The remaining tests are all recursive, so bail out if we hit the limit.
814 if (Depth++ == MaxDepth)
815 return 0;
816
817 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
David Majnemer492e6122014-08-30 09:19:05 +0000818 if (size_t LHSIdx =
819 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
820 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
821 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
David Majnemer37f8f442013-07-04 21:17:49 +0000822 return Actions.size();
823 }
824
825 return 0;
826}
827
Sanjay Patelbb789382017-08-24 22:54:01 +0000828/// If we have zero-extended operands of an unsigned div or rem, we may be able
829/// to narrow the operation (sink the zext below the math).
830static Instruction *narrowUDivURem(BinaryOperator &I,
831 InstCombiner::BuilderTy &Builder) {
832 Instruction::BinaryOps Opcode = I.getOpcode();
833 Value *N = I.getOperand(0);
834 Value *D = I.getOperand(1);
835 Type *Ty = I.getType();
836 Value *X, *Y;
837 if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) &&
838 X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) {
839 // udiv (zext X), (zext Y) --> zext (udiv X, Y)
840 // urem (zext X), (zext Y) --> zext (urem X, Y)
841 Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y);
842 return new ZExtInst(NarrowOp, Ty);
843 }
844
845 Constant *C;
846 if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) ||
847 (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) {
848 // If the constant is the same in the smaller type, use the narrow version.
849 Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
850 if (ConstantExpr::getZExt(TruncC, Ty) != C)
851 return nullptr;
852
853 // udiv (zext X), C --> zext (udiv X, C')
854 // urem (zext X), C --> zext (urem X, C')
855 // udiv C, (zext X) --> zext (udiv C', X)
856 // urem C, (zext X) --> zext (urem C', X)
857 Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC)
858 : Builder.CreateBinOp(Opcode, TruncC, X);
859 return new ZExtInst(NarrowOp, Ty);
860 }
861
862 return nullptr;
863}
864
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000865Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000866 if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1),
867 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000868 return replaceInstUsesWith(I, V);
Duncan Sands771e82a2011-01-28 16:51:11 +0000869
Sanjay Patel79dceb22018-10-03 15:20:58 +0000870 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000871 return X;
872
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000873 // Handle the integer div common cases
874 if (Instruction *Common = commonIDivTransforms(I))
875 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000876
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000877 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel38a86d32018-06-25 22:50:26 +0000878 Value *X;
879 const APInt *C1, *C2;
880 if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
881 // (X lshr C1) udiv C2 --> X udiv (C2 << C1)
882 bool Overflow;
883 APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
884 if (!Overflow) {
885 bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
886 BinaryOperator *BO = BinaryOperator::CreateUDiv(
887 X, ConstantInt::get(X->getType(), C2ShlC1));
888 if (IsExact)
889 BO->setIsExact();
890 return BO;
David Majnemera2521382014-10-13 21:48:30 +0000891 }
Nadav Rotem11935b22012-08-28 10:01:43 +0000892 }
893
Sanjay Patel38a86d32018-06-25 22:50:26 +0000894 // Op0 / C where C is large (negative) --> zext (Op0 >= C)
895 // TODO: Could use isKnownNegative() to handle non-constant values.
Sanjay Patel7c45deb2018-06-26 12:41:15 +0000896 Type *Ty = I.getType();
Sanjay Patel38a86d32018-06-25 22:50:26 +0000897 if (match(Op1, m_Negative())) {
898 Value *Cmp = Builder.CreateICmpUGE(Op0, Op1);
Sanjay Patel7c45deb2018-06-26 12:41:15 +0000899 return CastInst::CreateZExtOrBitCast(Cmp, Ty);
900 }
901 // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined)
902 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
903 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
904 return CastInst::CreateZExtOrBitCast(Cmp, Ty);
Sanjay Patel38a86d32018-06-25 22:50:26 +0000905 }
906
Sanjay Patelbb789382017-08-24 22:54:01 +0000907 if (Instruction *NarrowDiv = narrowUDivURem(I, Builder))
908 return NarrowDiv;
Benjamin Kramer9aa91b12011-04-30 18:16:07 +0000909
Sanjay Patel6d6eab62018-07-26 19:22:41 +0000910 // If the udiv operands are non-overflowing multiplies with a common operand,
911 // then eliminate the common factor:
912 // (A * B) / (A * X) --> B / X (and commuted variants)
913 // TODO: The code would be reduced if we had m_c_NUWMul pattern matching.
914 // TODO: If -reassociation handled this generally, we could remove this.
915 Value *A, *B;
916 if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) {
917 if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) ||
918 match(Op1, m_NUWMul(m_Value(X), m_Specific(A))))
919 return BinaryOperator::CreateUDiv(B, X);
920 if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) ||
921 match(Op1, m_NUWMul(m_Value(X), m_Specific(B))))
922 return BinaryOperator::CreateUDiv(A, X);
923 }
924
David Majnemer37f8f442013-07-04 21:17:49 +0000925 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
926 SmallVector<UDivFoldAction, 6> UDivActions;
927 if (visitUDivOperand(Op0, Op1, I, UDivActions))
928 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
929 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
930 Value *ActionOp1 = UDivActions[i].OperandToFold;
931 Instruction *Inst;
932 if (Action)
933 Inst = Action(Op0, ActionOp1, I, *this);
934 else {
935 // This action joins two actions together. The RHS of this action is
936 // simply the last action we processed, we saved the LHS action index in
937 // the joining action.
938 size_t SelectRHSIdx = i - 1;
939 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
940 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
941 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
942 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
943 SelectLHS, SelectRHS);
944 }
945
946 // If this is the last action to process, return it to the InstCombiner.
947 // Otherwise, we insert it before the UDiv and record it so that we may
948 // use it as part of a joining action (i.e., a SelectInst).
949 if (e - i != 1) {
950 Inst->insertBefore(&I);
951 UDivActions[i].FoldResult = Inst;
952 } else
953 return Inst;
954 }
955
Craig Topperf40110f2014-04-25 05:29:35 +0000956 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000957}
958
959Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000960 if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1),
961 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000962 return replaceInstUsesWith(I, V);
Duncan Sands771e82a2011-01-28 16:51:11 +0000963
Sanjay Patel79dceb22018-10-03 15:20:58 +0000964 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000965 return X;
966
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000967 // Handle the integer div common cases
968 if (Instruction *Common = commonIDivTransforms(I))
969 return Common;
970
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000971 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel6a96d902018-06-25 21:39:41 +0000972 Value *X;
973 // sdiv Op0, -1 --> -Op0
974 // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
975 if (match(Op1, m_AllOnes()) ||
976 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
977 return BinaryOperator::CreateNeg(Op0);
978
Sanjay Patelc6ada532016-06-27 17:25:57 +0000979 const APInt *Op1C;
Sanjay Patelbedd1f92016-06-27 18:38:40 +0000980 if (match(Op1, m_APInt(Op1C))) {
Sanjay Patelbedd1f92016-06-27 18:38:40 +0000981 // sdiv exact X, C --> ashr exact X, log2(C)
982 if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) {
983 Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2());
984 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
985 }
Sanjay Patel59ed2ff2016-06-27 22:27:11 +0000986
987 // If the dividend is sign-extended and the constant divisor is small enough
988 // to fit in the source type, shrink the division to the narrower type:
989 // (sext X) sdiv C --> sext (X sdiv C)
990 Value *Op0Src;
991 if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) &&
992 Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) {
993
994 // In the general case, we need to make sure that the dividend is not the
995 // minimum signed value because dividing that by -1 is UB. But here, we
996 // know that the -1 divisor case is already handled above.
997
998 Constant *NarrowDivisor =
999 ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType());
Craig Topperbb4069e2017-07-07 23:16:26 +00001000 Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor);
Sanjay Patel59ed2ff2016-06-27 22:27:11 +00001001 return new SExtInst(NarrowOp, Op0->getType());
1002 }
Benjamin Kramer72196f32014-01-19 15:24:22 +00001003 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001004
Benjamin Kramer72196f32014-01-19 15:24:22 +00001005 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
David Majnemerf28e2a42014-07-02 06:42:13 +00001006 // X/INT_MIN -> X == INT_MIN
1007 if (RHS->isMinSignedValue())
Craig Topperbb4069e2017-07-07 23:16:26 +00001008 return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType());
David Majnemerf28e2a42014-07-02 06:42:13 +00001009
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001010 // -X/C --> X/-C provided the negation doesn't overflow.
David Majnemerfa4699e2014-11-22 20:00:34 +00001011 Value *X;
1012 if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
1013 auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS));
1014 BO->setIsExact(I.isExact());
1015 return BO;
1016 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001017 }
1018
1019 // If the sign bits of both operands are zero (i.e. we can prove they are
1020 // unsigned inputs), turn this into a udiv.
Craig Topperbcfd2d12017-04-20 16:56:25 +00001021 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
Craig Topperf2484682017-04-17 01:51:19 +00001022 if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
1023 if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
1024 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
1025 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1026 BO->setIsExact(I.isExact());
1027 return BO;
1028 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001029
Craig Topperd4039f72017-05-25 21:51:12 +00001030 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
Craig Topperf2484682017-04-17 01:51:19 +00001031 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1032 // Safe because the only negative value (1 << Y) can take on is
1033 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1034 // the sign bit set.
1035 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1036 BO->setIsExact(I.isExact());
1037 return BO;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001038 }
1039 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001040
Craig Topperf40110f2014-04-25 05:29:35 +00001041 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001042}
1043
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001044/// Remove negation and try to convert division into multiplication.
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001045static Instruction *foldFDivConstantDivisor(BinaryOperator &I) {
1046 Constant *C;
1047 if (!match(I.getOperand(1), m_Constant(C)))
Craig Topperf40110f2014-04-25 05:29:35 +00001048 return nullptr;
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001049
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001050 // -X / C --> X / -C
1051 Value *X;
1052 if (match(I.getOperand(0), m_FNeg(m_Value(X))))
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001053 return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001054
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001055 // If the constant divisor has an exact inverse, this is always safe. If not,
1056 // then we can still create a reciprocal if fast-math-flags allow it and the
1057 // constant is a regular number (not zero, infinite, or denormal).
1058 if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP())))
Craig Topperf40110f2014-04-25 05:29:35 +00001059 return nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001060
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001061 // Disallow denormal constants because we don't know what would happen
1062 // on all targets.
1063 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1064 // denorms are flushed?
1065 auto *RecipC = ConstantExpr::getFDiv(ConstantFP::get(I.getType(), 1.0), C);
1066 if (!RecipC->isNormalFP())
1067 return nullptr;
1068
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001069 // X / C --> X * (1 / C)
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001070 return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001071}
1072
Sanjay Patel6f716a72018-02-21 00:01:45 +00001073/// Remove negation and try to reassociate constant math.
Sanjay Patele4129542018-02-19 21:17:58 +00001074static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001075 Constant *C;
1076 if (!match(I.getOperand(0), m_Constant(C)))
Sanjay Patele4129542018-02-19 21:17:58 +00001077 return nullptr;
1078
Sanjay Patel6f716a72018-02-21 00:01:45 +00001079 // C / -X --> -C / X
Sanjay Patele4129542018-02-19 21:17:58 +00001080 Value *X;
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001081 if (match(I.getOperand(1), m_FNeg(m_Value(X))))
1082 return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
Sanjay Patel6f716a72018-02-21 00:01:45 +00001083
1084 if (!I.hasAllowReassoc() || !I.hasAllowReciprocal())
1085 return nullptr;
1086
1087 // Try to reassociate C / X expressions where X includes another constant.
Sanjay Patele4129542018-02-19 21:17:58 +00001088 Constant *C2, *NewC = nullptr;
1089 if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001090 // C / (X * C2) --> (C / C2) / X
1091 NewC = ConstantExpr::getFDiv(C, C2);
Sanjay Patele4129542018-02-19 21:17:58 +00001092 } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001093 // C / (X / C2) --> (C * C2) / X
1094 NewC = ConstantExpr::getFMul(C, C2);
Sanjay Patele4129542018-02-19 21:17:58 +00001095 }
1096 // Disallow denormal constants because we don't know what would happen
1097 // on all targets.
1098 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1099 // denorms are flushed?
1100 if (!NewC || !NewC->isNormalFP())
1101 return nullptr;
1102
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001103 return BinaryOperator::CreateFDivFMF(NewC, X, &I);
Sanjay Patele4129542018-02-19 21:17:58 +00001104}
1105
Frits van Bommel2a559512011-01-29 17:50:27 +00001106Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001107 if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1),
1108 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +00001109 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001110 return replaceInstUsesWith(I, V);
Frits van Bommel2a559512011-01-29 17:50:27 +00001111
Sanjay Patel79dceb22018-10-03 15:20:58 +00001112 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001113 return X;
1114
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001115 if (Instruction *R = foldFDivConstantDivisor(I))
1116 return R;
Sanjay Patel28165602018-02-19 23:09:03 +00001117
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001118 if (Instruction *R = foldFDivConstantDividend(I))
1119 return R;
Sanjay Patelb39bcc02018-02-14 23:04:17 +00001120
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001121 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Stephen Lina9b57f62013-07-20 07:13:13 +00001122 if (isa<Constant>(Op0))
1123 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1124 if (Instruction *R = FoldOpIntoSelect(I, SI))
1125 return R;
1126
Sanjay Patel29b98ae2018-02-20 17:14:53 +00001127 if (isa<Constant>(Op1))
Stephen Lina9b57f62013-07-20 07:13:13 +00001128 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1129 if (Instruction *R = FoldOpIntoSelect(I, SI))
1130 return R;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001131
Sanjay Patel31a90462018-02-26 16:02:45 +00001132 if (I.hasAllowReassoc() && I.hasAllowReciprocal()) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001133 Value *X, *Y;
Sanjay Patel91bb7752018-02-16 17:52:32 +00001134 if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1135 (!isa<Constant>(Y) || !isa<Constant>(Op1))) {
1136 // (X / Y) / Z => X / (Y * Z)
Sanjay Patel31a90462018-02-26 16:02:45 +00001137 Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I);
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001138 return BinaryOperator::CreateFDivFMF(X, YZ, &I);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001139 }
Sanjay Patel91bb7752018-02-16 17:52:32 +00001140 if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1141 (!isa<Constant>(Y) || !isa<Constant>(Op0))) {
1142 // Z / (X / Y) => (Y * Z) / X
Sanjay Patel31a90462018-02-26 16:02:45 +00001143 Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001144 return BinaryOperator::CreateFDivFMF(YZ, X, &I);
Benjamin Kramer8564e0d2011-03-30 15:42:35 +00001145 }
1146 }
1147
Sanjay Patel339b4d32018-02-15 15:07:12 +00001148 if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
Sanjay Patel65da14d2018-02-16 16:13:20 +00001149 // sin(X) / cos(X) -> tan(X)
1150 // cos(X) / sin(X) -> 1/tan(X) (cotangent)
1151 Value *X;
1152 bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) &&
1153 match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)));
1154 bool IsCot =
1155 !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) &&
1156 match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X)));
Dmitry Venikove5fbf592018-01-11 06:33:00 +00001157
Sanjay Patel65da14d2018-02-16 16:13:20 +00001158 if ((IsTan || IsCot) && hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan,
1159 LibFunc_tanf, LibFunc_tanl)) {
1160 IRBuilder<> B(&I);
1161 IRBuilder<>::FastMathFlagGuard FMFGuard(B);
1162 B.setFastMathFlags(I.getFastMathFlags());
1163 AttributeList Attrs = CallSite(Op0).getCalledFunction()->getAttributes();
Mikael Holmene3605d02018-10-18 06:27:53 +00001164 Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf,
1165 LibFunc_tanl, B, Attrs);
Sanjay Patel65da14d2018-02-16 16:13:20 +00001166 if (IsCot)
1167 Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res);
1168 return replaceInstUsesWith(I, Res);
Dmitry Venikove5fbf592018-01-11 06:33:00 +00001169 }
1170 }
1171
Sanjay Patel1998cc62018-02-12 18:38:35 +00001172 // -X / -Y -> X / Y
1173 Value *X, *Y;
1174 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) {
1175 I.setOperand(0, X);
1176 I.setOperand(1, Y);
Matt Arsenaultfdb78f82017-01-10 23:08:54 +00001177 return &I;
1178 }
1179
Sanjay Patel4a4f35f2018-02-12 19:39:21 +00001180 // X / (X * Y) --> 1.0 / Y
1181 // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
1182 // We can ignore the possibility that X is infinity because INF/INF is NaN.
1183 if (I.hasNoNaNs() && I.hasAllowReassoc() &&
1184 match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) {
1185 I.setOperand(0, ConstantFP::get(I.getType(), 1.0));
1186 I.setOperand(1, Y);
1187 return &I;
1188 }
1189
Craig Topperf40110f2014-04-25 05:29:35 +00001190 return nullptr;
Frits van Bommel2a559512011-01-29 17:50:27 +00001191}
1192
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001193/// This function implements the transforms common to both integer remainder
1194/// instructions (urem and srem). It is called by the visitors to those integer
1195/// remainder instructions.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00001196/// Common integer remainder transforms
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001197Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
1198 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1199
Chris Lattner7c99f192011-05-22 18:18:41 +00001200 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001201 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +00001202 I.setOperand(1, V);
1203 return &I;
1204 }
1205
Duncan Sandsa3e36992011-05-02 16:27:02 +00001206 // Handle cases involving: rem X, (select Cond, Y, Z)
Sanjay Patelae2e3a42017-10-06 23:20:16 +00001207 if (simplifyDivRemOfSelectWithZeroOp(I))
Duncan Sandsa3e36992011-05-02 16:27:02 +00001208 return &I;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001209
Benjamin Kramer72196f32014-01-19 15:24:22 +00001210 if (isa<Constant>(Op1)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001211 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1212 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1213 if (Instruction *R = FoldOpIntoSelect(I, SI))
1214 return R;
Craig Topperfb71b7d2017-04-14 19:20:12 +00001215 } else if (auto *PN = dyn_cast<PHINode>(Op0I)) {
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001216 const APInt *Op1Int;
1217 if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
1218 (I.getOpcode() == Instruction::URem ||
1219 !Op1Int->isMinSignedValue())) {
Craig Topperfb71b7d2017-04-14 19:20:12 +00001220 // foldOpIntoPhi will speculate instructions to the end of the PHI's
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001221 // predecessor blocks, so do this only if we know the srem or urem
1222 // will not fault.
Craig Topperfb71b7d2017-04-14 19:20:12 +00001223 if (Instruction *NV = foldOpIntoPhi(I, PN))
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001224 return NV;
1225 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001226 }
1227
1228 // See if we can fold away this rem instruction.
1229 if (SimplifyDemandedInstructionBits(I))
1230 return &I;
1231 }
1232 }
1233
Craig Topperf40110f2014-04-25 05:29:35 +00001234 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001235}
1236
1237Instruction *InstCombiner::visitURem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001238 if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1),
1239 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001240 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001241
Sanjay Patel79dceb22018-10-03 15:20:58 +00001242 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001243 return X;
1244
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001245 if (Instruction *common = commonIRemTransforms(I))
1246 return common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001247
Sanjay Patelbb789382017-08-24 22:54:01 +00001248 if (Instruction *NarrowRem = narrowUDivURem(I, Builder))
1249 return NarrowRem;
David Majnemer6c30f492013-05-12 00:07:05 +00001250
David Majnemer470b0772013-05-11 09:01:28 +00001251 // X urem Y -> X and Y-1, where Y is a power of 2,
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001252 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001253 Type *Ty = I.getType();
Craig Topperd4039f72017-05-25 21:51:12 +00001254 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001255 Constant *N1 = Constant::getAllOnesValue(Ty);
Craig Topperbb4069e2017-07-07 23:16:26 +00001256 Value *Add = Builder.CreateAdd(Op1, N1);
Chris Lattner6b657ae2011-02-10 05:36:31 +00001257 return BinaryOperator::CreateAnd(Op0, Add);
1258 }
1259
Nick Lewycky7459be62013-07-13 01:16:47 +00001260 // 1 urem X -> zext(X != 1)
Sanjay Patel9adea012018-06-26 16:39:29 +00001261 if (match(Op0, m_One()))
1262 return CastInst::CreateZExtOrBitCast(Builder.CreateICmpNE(Op1, Op0), Ty);
Nick Lewycky7459be62013-07-13 01:16:47 +00001263
Sanjay Patel30ef70b2016-09-22 22:36:26 +00001264 // X urem C -> X < C ? X : X - C, where C >= signbit.
Simon Pilgrim1889f262018-02-08 18:36:01 +00001265 if (match(Op1, m_Negative())) {
Craig Topperbb4069e2017-07-07 23:16:26 +00001266 Value *Cmp = Builder.CreateICmpULT(Op0, Op1);
1267 Value *Sub = Builder.CreateSub(Op0, Op1);
Sanjay Patel30ef70b2016-09-22 22:36:26 +00001268 return SelectInst::Create(Cmp, Op0, Sub);
1269 }
1270
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001271 // If the divisor is a sext of a boolean, then the divisor must be max
1272 // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also
1273 // max unsigned value. In that case, the remainder is 0:
1274 // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0
1275 Value *X;
1276 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1277 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
1278 return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0);
1279 }
1280
Craig Topperf40110f2014-04-25 05:29:35 +00001281 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001282}
1283
1284Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001285 if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1),
1286 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001287 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001288
Sanjay Patel79dceb22018-10-03 15:20:58 +00001289 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001290 return X;
1291
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001292 // Handle the integer rem common cases
1293 if (Instruction *Common = commonIRemTransforms(I))
1294 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001295
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001296 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
David Majnemerdb077302014-10-13 22:37:51 +00001297 {
1298 const APInt *Y;
1299 // X % -Y -> X % Y
Simon Pilgrima54e8e42018-02-08 19:00:45 +00001300 if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001301 Worklist.AddValue(I.getOperand(1));
David Majnemerdb077302014-10-13 22:37:51 +00001302 I.setOperand(1, ConstantInt::get(I.getType(), -*Y));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001303 return &I;
1304 }
David Majnemerdb077302014-10-13 22:37:51 +00001305 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001306
1307 // If the sign bits of both operands are zero (i.e. we can prove they are
1308 // unsigned inputs), turn this into a urem.
Craig Topperbcfd2d12017-04-20 16:56:25 +00001309 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
Craig Topper1a18a7c2017-04-17 01:51:24 +00001310 if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
1311 MaskedValueIsZero(Op0, Mask, 0, &I)) {
1312 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
1313 return BinaryOperator::CreateURem(Op0, Op1, I.getName());
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001314 }
1315
1316 // If it's a constant vector, flip any negative values positive.
Chris Lattner0256be92012-01-27 03:08:05 +00001317 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1318 Constant *C = cast<Constant>(Op1);
1319 unsigned VWidth = C->getType()->getVectorNumElements();
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001320
1321 bool hasNegative = false;
Chris Lattner0256be92012-01-27 03:08:05 +00001322 bool hasMissing = false;
1323 for (unsigned i = 0; i != VWidth; ++i) {
1324 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001325 if (!Elt) {
Chris Lattner0256be92012-01-27 03:08:05 +00001326 hasMissing = true;
1327 break;
1328 }
1329
1330 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
Chris Lattnerb1a15122011-07-15 06:08:15 +00001331 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001332 hasNegative = true;
Chris Lattner0256be92012-01-27 03:08:05 +00001333 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001334
Chris Lattner0256be92012-01-27 03:08:05 +00001335 if (hasNegative && !hasMissing) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001336 SmallVector<Constant *, 16> Elts(VWidth);
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001337 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner8213c8a2012-02-06 21:56:39 +00001338 Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
Chris Lattner0256be92012-01-27 03:08:05 +00001339 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
Chris Lattnerb1a15122011-07-15 06:08:15 +00001340 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001341 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001342 }
1343 }
1344
1345 Constant *NewRHSV = ConstantVector::get(Elts);
Chris Lattner0256be92012-01-27 03:08:05 +00001346 if (NewRHSV != C) { // Don't loop on -MININT
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001347 Worklist.AddValue(I.getOperand(1));
1348 I.setOperand(1, NewRHSV);
1349 return &I;
1350 }
1351 }
1352 }
1353
Craig Topperf40110f2014-04-25 05:29:35 +00001354 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001355}
1356
1357Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001358 if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1),
1359 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +00001360 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001361 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001362
Sanjay Patel79dceb22018-10-03 15:20:58 +00001363 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001364 return X;
1365
Craig Topperf40110f2014-04-25 05:29:35 +00001366 return nullptr;
Duncan Sandsa3e36992011-05-02 16:27:02 +00001367}