blob: 7e99f3e4e500fdf5513d847658e012b05e277f0b [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"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000016#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/SmallVector.h"
Duncan Sandsd0eb6d32010-12-21 14:00:22 +000019#include "llvm/Analysis/InstructionSimplify.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000020#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/Constant.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/InstrTypes.h"
24#include "llvm/IR/Instruction.h"
25#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000027#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000029#include "llvm/IR/PatternMatch.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000030#include "llvm/IR/Type.h"
31#include "llvm/IR/Value.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/KnownBits.h"
35#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
Dmitry Venikove5fbf592018-01-11 06:33:00 +000036#include "llvm/Transforms/Utils/BuildLibCalls.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000037#include <cassert>
38#include <cstddef>
39#include <cstdint>
40#include <utility>
41
Chris Lattnerdc054bf2010-01-05 06:09:35 +000042using namespace llvm;
43using namespace PatternMatch;
44
Chandler Carruth964daaa2014-04-22 02:55:47 +000045#define DEBUG_TYPE "instcombine"
46
Sanjay Patel6eccf482015-09-09 15:24:36 +000047/// The specific integer value is used in a context where it is known to be
48/// non-zero. If this allows us to simplify the computation, do so and return
49/// the new operand, otherwise return null.
Hal Finkel60db0582014-09-07 18:57:58 +000050static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000051 Instruction &CxtI) {
Chris Lattner7c99f192011-05-22 18:18:41 +000052 // If V has multiple uses, then we would have to do more analysis to determine
53 // if this is safe. For example, the use could be in dynamically unreached
54 // code.
Craig Topperf40110f2014-04-25 05:29:35 +000055 if (!V->hasOneUse()) return nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +000056
Chris Lattner388cb8a2011-05-23 00:32:19 +000057 bool MadeChange = false;
58
Chris Lattner7c99f192011-05-22 18:18:41 +000059 // ((1 << A) >>u B) --> (1 << (A-B))
60 // Because V cannot be zero, we know that B is less than A.
David Majnemerdad21032014-10-14 20:28:40 +000061 Value *A = nullptr, *B = nullptr, *One = nullptr;
62 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) &&
63 match(One, m_One())) {
Craig Topperbb4069e2017-07-07 23:16:26 +000064 A = IC.Builder.CreateSub(A, B);
65 return IC.Builder.CreateShl(One, A);
Chris Lattner7c99f192011-05-22 18:18:41 +000066 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +000067
Chris Lattner388cb8a2011-05-23 00:32:19 +000068 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
69 // inexact. Similarly for <<.
Sanjay Patela8ef4a52016-05-22 17:08:52 +000070 BinaryOperator *I = dyn_cast<BinaryOperator>(V);
71 if (I && I->isLogicalShift() &&
Craig Topperd4039f72017-05-25 21:51:12 +000072 IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) {
Sanjay Patela8ef4a52016-05-22 17:08:52 +000073 // We know that this is an exact/nuw shift and that the input is a
74 // non-zero context as well.
75 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
76 I->setOperand(0, V2);
77 MadeChange = true;
Chris Lattner388cb8a2011-05-23 00:32:19 +000078 }
79
Sanjay Patela8ef4a52016-05-22 17:08:52 +000080 if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
81 I->setIsExact();
82 MadeChange = true;
83 }
84
85 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
86 I->setHasNoUnsignedWrap();
87 MadeChange = true;
88 }
89 }
90
Chris Lattner162dfc32011-05-22 18:26:48 +000091 // TODO: Lots more we could do here:
Chris Lattner162dfc32011-05-22 18:26:48 +000092 // If V is a phi node, we can call this on each of its operands.
93 // "select cond, X, 0" can simplify to "X".
Jim Grosbachbdbd7342013-04-05 21:20:12 +000094
Craig Topperf40110f2014-04-25 05:29:35 +000095 return MadeChange ? V : nullptr;
Chris Lattner7c99f192011-05-22 18:18:41 +000096}
97
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000098/// A helper routine of InstCombiner::visitMul().
Rafael Espindola65281bf2013-05-31 14:27:15 +000099///
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000100/// If C is a scalar/vector of known powers of 2, then this function returns
101/// a new scalar/vector obtained from logBase2 of C.
Rafael Espindola65281bf2013-05-31 14:27:15 +0000102/// Return a null pointer otherwise.
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000103static Constant *getLogBase2(Type *Ty, Constant *C) {
Rafael Espindola65281bf2013-05-31 14:27:15 +0000104 const APInt *IVal;
Simon Pilgrimbe0dd722018-02-13 13:16:26 +0000105 if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
106 return ConstantInt::get(Ty, IVal->logBase2());
Rafael Espindola65281bf2013-05-31 14:27:15 +0000107
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000108 if (!Ty->isVectorTy())
109 return nullptr;
110
111 SmallVector<Constant *, 4> Elts;
112 for (unsigned I = 0, E = Ty->getVectorNumElements(); I != E; ++I) {
113 Constant *Elt = C->getAggregateElement(I);
114 if (!Elt)
115 return nullptr;
116 if (isa<UndefValue>(Elt)) {
117 Elts.push_back(UndefValue::get(Ty->getScalarType()));
118 continue;
119 }
Rafael Espindola65281bf2013-05-31 14:27:15 +0000120 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
Craig Topperf40110f2014-04-25 05:29:35 +0000121 return nullptr;
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000122 Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));
Rafael Espindola65281bf2013-05-31 14:27:15 +0000123 }
124
125 return ConstantVector::get(Elts);
126}
127
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000128Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000129 if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1),
130 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000131 return replaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000132
Sanjay Patel70043b72018-07-13 01:18:07 +0000133 if (SimplifyAssociativeOrCommutative(I))
134 return &I;
135
Sanjay Patel79dceb22018-10-03 15:20:58 +0000136 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000137 return X;
138
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000139 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000140 return replaceInstUsesWith(I, V);
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000141
David Majnemer027bc802014-11-22 04:52:38 +0000142 // X * -1 == 0 - X
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000143 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
David Majnemer027bc802014-11-22 04:52:38 +0000144 if (match(Op1, m_AllOnes())) {
145 BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
146 if (I.hasNoSignedWrap())
147 BO->setHasNoSignedWrap();
148 return BO;
149 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000150
Rafael Espindola65281bf2013-05-31 14:27:15 +0000151 // Also allow combining multiply instructions on vectors.
152 {
153 Value *NewOp;
154 Constant *C1, *C2;
155 const APInt *IVal;
156 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
157 m_Constant(C1))) &&
David Majnemerfd4a6d22014-11-22 04:52:52 +0000158 match(C1, m_APInt(IVal))) {
159 // ((X << C2)*C1) == (X * (C1 << C2))
160 Constant *Shl = ConstantExpr::getShl(C1, C2);
161 BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
162 BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
163 if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
164 BO->setHasNoUnsignedWrap();
165 if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
166 Shl->isNotMinSignedValue())
167 BO->setHasNoSignedWrap();
168 return BO;
169 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000170
Rafael Espindola65281bf2013-05-31 14:27:15 +0000171 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
Simon Pilgrim0b9f3912018-02-08 14:10:01 +0000172 // Replace X*(2^C) with X << C, where C is either a scalar or a vector.
173 if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) {
Rafael Espindola65281bf2013-05-31 14:27:15 +0000174 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000175
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000176 if (I.hasNoUnsignedWrap())
177 Shl->setHasNoUnsignedWrap();
David Majnemer45951a62015-04-18 04:41:30 +0000178 if (I.hasNoSignedWrap()) {
Craig Topper5fe01972017-06-27 19:57:53 +0000179 const APInt *V;
Craig Topper4e63db82018-09-11 17:57:20 +0000180 if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1)
David Majnemer45951a62015-04-18 04:41:30 +0000181 Shl->setHasNoSignedWrap();
182 }
Tilmann Scheller2bc5cb62014-10-07 10:19:34 +0000183
Rafael Espindola65281bf2013-05-31 14:27:15 +0000184 return Shl;
185 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000186 }
Rafael Espindola65281bf2013-05-31 14:27:15 +0000187 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000188
Rafael Espindola65281bf2013-05-31 14:27:15 +0000189 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Stuart Hastings23804832011-06-01 16:42:47 +0000190 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
191 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
192 // The "* (2**n)" thus becomes a potential shifting opportunity.
Stuart Hastings82843742011-05-30 20:00:33 +0000193 {
194 const APInt & Val = CI->getValue();
195 const APInt &PosVal = Val.abs();
196 if (Val.isNegative() && PosVal.isPowerOf2()) {
Craig Topperf40110f2014-04-25 05:29:35 +0000197 Value *X = nullptr, *Y = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000198 if (Op0->hasOneUse()) {
199 ConstantInt *C1;
Craig Topperf40110f2014-04-25 05:29:35 +0000200 Value *Sub = nullptr;
Stuart Hastings23804832011-06-01 16:42:47 +0000201 if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
Craig Topperbb4069e2017-07-07 23:16:26 +0000202 Sub = Builder.CreateSub(X, Y, "suba");
Stuart Hastings23804832011-06-01 16:42:47 +0000203 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
Craig Topperbb4069e2017-07-07 23:16:26 +0000204 Sub = Builder.CreateSub(Builder.CreateNeg(C1), Y, "subc");
Stuart Hastings23804832011-06-01 16:42:47 +0000205 if (Sub)
206 return
207 BinaryOperator::CreateMul(Sub,
208 ConstantInt::get(Y->getType(), PosVal));
Stuart Hastings82843742011-05-30 20:00:33 +0000209 }
210 }
211 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000212 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000213
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000214 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
215 return FoldedMul;
216
Chris Lattner6b657ae2011-02-10 05:36:31 +0000217 // Simplify mul instructions with a constant RHS.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000218 if (isa<Constant>(Op1)) {
Benjamin Kramer72196f32014-01-19 15:24:22 +0000219 // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000220 Value *X;
221 Constant *C1;
222 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
223 Value *Mul = Builder.CreateMul(C1, Op1);
224 // Only go forward with the transform if C1*CI simplifies to a tidier
225 // constant.
226 if (!match(Mul, m_Mul(m_Value(), m_Value())))
227 return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul);
Benjamin Kramer72196f32014-01-19 15:24:22 +0000228 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000229 }
230
Sanjay Patel604cb9e2018-02-14 16:50:55 +0000231 // -X * C --> X * -C
232 Value *X, *Y;
233 Constant *Op1C;
234 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C)))
235 return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C));
236
237 // -X * -Y --> X * Y
238 if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) {
239 auto *NewMul = BinaryOperator::CreateMul(X, Y);
240 if (I.hasNoSignedWrap() &&
241 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
242 cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap())
243 NewMul->setHasNoSignedWrap();
244 return NewMul;
David Majnemer8279a7502014-11-22 07:25:19 +0000245 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000246
Chen Zheng4952e662019-01-01 01:09:20 +0000247 // -X * Y --> -(X * Y)
248 // X * -Y --> -(X * Y)
249 if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
250 return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
251
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000252 // (X / Y) * Y = X - (X % Y)
253 // (X / Y) * -Y = (X % Y) - X
254 {
Sanjay Patela0a56822017-03-14 17:27:27 +0000255 Value *Y = Op1;
256 BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0);
257 if (!Div || (Div->getOpcode() != Instruction::UDiv &&
258 Div->getOpcode() != Instruction::SDiv)) {
259 Y = Op0;
260 Div = dyn_cast<BinaryOperator>(Op1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000261 }
Sanjay Patela0a56822017-03-14 17:27:27 +0000262 Value *Neg = dyn_castNegVal(Y);
263 if (Div && Div->hasOneUse() &&
264 (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) &&
265 (Div->getOpcode() == Instruction::UDiv ||
266 Div->getOpcode() == Instruction::SDiv)) {
267 Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000268
Chris Lattner35315d02011-02-06 21:44:57 +0000269 // If the division is exact, X % Y is zero, so we end up with X or -X.
Sanjay Patela0a56822017-03-14 17:27:27 +0000270 if (Div->isExact()) {
271 if (DivOp1 == Y)
272 return replaceInstUsesWith(I, X);
273 return BinaryOperator::CreateNeg(X);
274 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000275
Sanjay Patela0a56822017-03-14 17:27:27 +0000276 auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem
277 : Instruction::SRem;
Craig Topperbb4069e2017-07-07 23:16:26 +0000278 Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1);
Sanjay Patela0a56822017-03-14 17:27:27 +0000279 if (DivOp1 == Y)
280 return BinaryOperator::CreateSub(X, Rem);
281 return BinaryOperator::CreateSub(Rem, X);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000282 }
283 }
284
285 /// i1 mul -> i1 and.
Craig Topperfde47232017-07-09 07:04:03 +0000286 if (I.getType()->isIntOrIntVectorTy(1))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000287 return BinaryOperator::CreateAnd(Op0, Op1);
288
289 // X*(1 << Y) --> X << Y
290 // (1 << Y)*X --> X << Y
291 {
292 Value *Y;
David Majnemer546f8102014-11-22 08:57:02 +0000293 BinaryOperator *BO = nullptr;
294 bool ShlNSW = false;
295 if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
296 BO = BinaryOperator::CreateShl(Op1, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000297 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
David Majnemer8e6f6a92014-11-24 16:41:13 +0000298 } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
David Majnemer546f8102014-11-22 08:57:02 +0000299 BO = BinaryOperator::CreateShl(Op0, Y);
David Majnemer087dc8b2015-01-04 07:36:02 +0000300 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
David Majnemer546f8102014-11-22 08:57:02 +0000301 }
302 if (BO) {
303 if (I.hasNoUnsignedWrap())
304 BO->setHasNoUnsignedWrap();
305 if (I.hasNoSignedWrap() && ShlNSW)
306 BO->setHasNoSignedWrap();
307 return BO;
308 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000309 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000310
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000311 // (bool X) * Y --> X ? Y : 0
Sanjay Patel7558d862018-02-13 22:24:37 +0000312 // Y * (bool X) --> X ? Y : 0
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000313 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
314 return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0));
Sanjay Patelcb8ac002018-02-13 20:41:22 +0000315 if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
316 return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0));
317
Sanjay Patel7558d862018-02-13 22:24:37 +0000318 // (lshr X, 31) * Y --> (ashr X, 31) & Y
319 // Y * (lshr X, 31) --> (ashr X, 31) & Y
320 // TODO: We are not checking one-use because the elimination of the multiply
321 // is better for analysis?
322 // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be
323 // more similar to what we're doing above.
324 const APInt *C;
325 if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
326 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1);
327 if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
328 return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000329
Sanjay Patel90a36342018-09-14 22:23:35 +0000330 if (Instruction *Ext = narrowMathIfNoOverflow(I))
331 return Ext;
David Majnemera1cfd7c2016-12-30 00:28:58 +0000332
Sanjay Patel70043b72018-07-13 01:18:07 +0000333 bool Changed = false;
Craig Topper2b1fc322017-05-22 06:25:31 +0000334 if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) {
David Majnemer54c2ca22014-12-26 09:10:14 +0000335 Changed = true;
336 I.setHasNoSignedWrap(true);
337 }
338
Craig Topperbb973722017-05-15 02:44:08 +0000339 if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) {
David Majnemerb1296ec2014-12-26 09:50:35 +0000340 Changed = true;
341 I.setHasNoUnsignedWrap(true);
342 }
343
Craig Topperf40110f2014-04-25 05:29:35 +0000344 return Changed ? &I : nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000345}
346
347Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000348 if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1),
349 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +0000350 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000351 return replaceInstUsesWith(I, V);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000352
Sanjay Patel70043b72018-07-13 01:18:07 +0000353 if (SimplifyAssociativeOrCommutative(I))
354 return &I;
355
Sanjay Patel79dceb22018-10-03 15:20:58 +0000356 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000357 return X;
358
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000359 if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
360 return FoldedMul;
361
Sanjay Patele29375d2018-03-02 23:06:45 +0000362 // X * -1.0 --> -X
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000363 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patele29375d2018-03-02 23:06:45 +0000364 if (match(Op1, m_SpecificFP(-1.0)))
365 return BinaryOperator::CreateFNegFMF(Op0, &I);
Sanjay Patel6b9c7a92018-02-23 17:14:28 +0000366
Sanjay Patele29375d2018-03-02 23:06:45 +0000367 // -X * -Y --> X * Y
368 Value *X, *Y;
369 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
370 return BinaryOperator::CreateFMulFMF(X, Y, &I);
Owen Andersonf74cfe02014-01-16 20:36:42 +0000371
Sanjay Patele29375d2018-03-02 23:06:45 +0000372 // -X * C --> X * -C
373 Constant *C;
374 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C)))
375 return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000376
Sanjay Patele29375d2018-03-02 23:06:45 +0000377 // Sink negation: -X * Y --> -(X * Y)
378 if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))))
379 return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op1, &I), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000380
Sanjay Patele29375d2018-03-02 23:06:45 +0000381 // Sink negation: Y * -X --> -(X * Y)
382 if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))))
383 return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op0, &I), &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000384
Sanjay Patele29375d2018-03-02 23:06:45 +0000385 // fabs(X) * fabs(X) -> X * X
386 if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X))))
387 return BinaryOperator::CreateFMulFMF(X, X, &I);
388
389 // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E)
390 if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
391 return replaceInstUsesWith(I, V);
392
Sanjay Patel81b3b102018-04-03 22:19:19 +0000393 if (I.hasAllowReassoc()) {
394 // Reassociate constant RHS with another constant to form constant
395 // expression.
396 if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) {
397 Constant *C1;
398 if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) {
399 // (C1 / X) * C --> (C * C1) / X
400 Constant *CC1 = ConstantExpr::getFMul(C, C1);
401 if (CC1->isNormalFP())
402 return BinaryOperator::CreateFDivFMF(CC1, X, &I);
403 }
404 if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
405 // (X / C1) * C --> X * (C / C1)
406 Constant *CDivC1 = ConstantExpr::getFDiv(C, C1);
407 if (CDivC1->isNormalFP())
408 return BinaryOperator::CreateFMulFMF(X, CDivC1, &I);
Sanjay Patel204edec2018-03-13 14:46:32 +0000409
Sanjay Patel81b3b102018-04-03 22:19:19 +0000410 // If the constant was a denormal, try reassociating differently.
411 // (X / C1) * C --> X / (C1 / C)
412 Constant *C1DivC = ConstantExpr::getFDiv(C1, C);
413 if (Op0->hasOneUse() && C1DivC->isNormalFP())
414 return BinaryOperator::CreateFDivFMF(X, C1DivC, &I);
415 }
416
417 // We do not need to match 'fadd C, X' and 'fsub X, C' because they are
418 // canonicalized to 'fadd X, C'. Distributing the multiply may allow
419 // further folds and (X * C) + C2 is 'fma'.
420 if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) {
421 // (X + C1) * C --> (X * C) + (C * C1)
422 Constant *CC1 = ConstantExpr::getFMul(C, C1);
423 Value *XC = Builder.CreateFMulFMF(X, C, &I);
424 return BinaryOperator::CreateFAddFMF(XC, CC1, &I);
425 }
426 if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) {
427 // (C1 - X) * C --> (C * C1) - (X * C)
428 Constant *CC1 = ConstantExpr::getFMul(C, C1);
429 Value *XC = Builder.CreateFMulFMF(X, C, &I);
430 return BinaryOperator::CreateFSubFMF(CC1, XC, &I);
431 }
Sanjay Patel204edec2018-03-13 14:46:32 +0000432 }
433
Sanjay Patel81b3b102018-04-03 22:19:19 +0000434 // sqrt(X) * sqrt(Y) -> sqrt(X * Y)
435 // nnan disallows the possibility of returning a number if both operands are
436 // negative (in that case, we should return NaN).
437 if (I.hasNoNaNs() &&
438 match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) &&
439 match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
440 Value *XY = Builder.CreateFMulFMF(X, Y, &I);
Neil Henning57f5d0a2018-10-08 10:32:33 +0000441 Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I);
Sanjay Patel81b3b102018-04-03 22:19:19 +0000442 return replaceInstUsesWith(I, Sqrt);
Sanjay Patel4fd4fd62018-03-26 15:03:57 +0000443 }
Sanjay Patel81b3b102018-04-03 22:19:19 +0000444
445 // (X*Y) * X => (X*X) * Y where Y != X
446 // The purpose is two-fold:
447 // 1) to form a power expression (of X).
448 // 2) potentially shorten the critical path: After transformation, the
449 // latency of the instruction Y is amortized by the expression of X*X,
450 // and therefore Y is in a "less critical" position compared to what it
451 // was before the transformation.
452 if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) &&
453 Op1 != Y) {
454 Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I);
455 return BinaryOperator::CreateFMulFMF(XX, Y, &I);
456 }
457 if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) &&
458 Op0 != Y) {
459 Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I);
460 return BinaryOperator::CreateFMulFMF(XX, Y, &I);
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000461 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000462 }
463
Sanjay Patel2fd0acf2018-03-02 20:32:46 +0000464 // log2(X * 0.5) * Y = log2(X) * Y - Y
465 if (I.isFast()) {
466 IntrinsicInst *Log2 = nullptr;
467 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>(
468 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
469 Log2 = cast<IntrinsicInst>(Op0);
470 Y = Op1;
Pedro Artigasd8795042012-11-30 19:09:41 +0000471 }
Sanjay Patel2fd0acf2018-03-02 20:32:46 +0000472 if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>(
473 m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
474 Log2 = cast<IntrinsicInst>(Op1);
475 Y = Op0;
476 }
477 if (Log2) {
478 Log2->setArgOperand(0, X);
479 Log2->copyFastMathFlags(&I);
480 Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I);
481 return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I);
Pedro Artigasd8795042012-11-30 19:09:41 +0000482 }
483 }
484
Sanjay Patel70043b72018-07-13 01:18:07 +0000485 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000486}
487
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000488/// Fold a divide or remainder with a select instruction divisor when one of the
489/// select operands is zero. In that case, we can use the other select operand
490/// because div/rem by zero is undefined.
491bool InstCombiner::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) {
492 SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1));
493 if (!SI)
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000494 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000495
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000496 int NonNullOperand;
497 if (match(SI->getTrueValue(), m_Zero()))
498 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
499 NonNullOperand = 2;
500 else if (match(SI->getFalseValue(), m_Zero()))
501 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
502 NonNullOperand = 1;
503 else
504 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000505
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000506 // Change the div/rem to use 'Y' instead of the select.
507 I.setOperand(1, SI->getOperand(NonNullOperand));
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000508
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000509 // Okay, we know we replace the operand of the div/rem with 'Y' with no
510 // problem. However, the select, or the condition of the select may have
511 // multiple uses. Based on our knowledge that the operand must be non-zero,
512 // propagate the known value for the select into other uses of it, and
513 // propagate a known value of the condition into its other users.
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000514
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000515 // If the select and condition only have a single use, don't bother with this,
516 // early exit.
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000517 Value *SelectCond = SI->getCondition();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000518 if (SI->use_empty() && SelectCond->hasOneUse())
519 return true;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000520
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000521 // Scan the current block backward, looking for other uses of SI.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000522 BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin();
Sanjay Patel72d339a2017-10-06 23:43:06 +0000523 Type *CondTy = SelectCond->getType();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000524 while (BBI != BBFront) {
525 --BBI;
Serguei Katkovd894fb42018-06-04 02:52:36 +0000526 // If we found an instruction that we can't assume will return, so
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000527 // information from below it cannot be propagated above it.
Serguei Katkovd894fb42018-06-04 02:52:36 +0000528 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000529 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000530
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000531 // Replace uses of the select or its condition with the known values.
532 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
533 I != E; ++I) {
534 if (*I == SI) {
535 *I = SI->getOperand(NonNullOperand);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000536 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000537 } else if (*I == SelectCond) {
Sanjay Patel72d339a2017-10-06 23:43:06 +0000538 *I = NonNullOperand == 1 ? ConstantInt::getTrue(CondTy)
539 : ConstantInt::getFalse(CondTy);
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000540 Worklist.Add(&*BBI);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000541 }
542 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000543
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000544 // If we past the instruction, quit looking for it.
545 if (&*BBI == SI)
Craig Topperf40110f2014-04-25 05:29:35 +0000546 SI = nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000547 if (&*BBI == SelectCond)
Craig Topperf40110f2014-04-25 05:29:35 +0000548 SelectCond = nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000549
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000550 // If we ran out of things to eliminate, break out of the loop.
Craig Topperf40110f2014-04-25 05:29:35 +0000551 if (!SelectCond && !SI)
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000552 break;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000553
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000554 }
555 return true;
556}
557
Sanjay Patel1998cc62018-02-12 18:38:35 +0000558/// True if the multiply can not be expressed in an int this size.
559static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
560 bool IsSigned) {
561 bool Overflow;
562 Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow);
563 return Overflow;
564}
565
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000566/// True if C1 is a multiple of C2. Quotient contains C1/C2.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000567static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
568 bool IsSigned) {
569 assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal");
570
571 // Bail if we will divide by zero.
572 if (C2.isNullValue())
573 return false;
574
575 // Bail if we would divide INT_MIN by -1.
576 if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue())
577 return false;
578
579 APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
580 if (IsSigned)
581 APInt::sdivrem(C1, C2, Quotient, Remainder);
582 else
583 APInt::udivrem(C1, C2, Quotient, Remainder);
584
585 return Remainder.isMinValue();
586}
587
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000588/// This function implements the transforms common to both integer division
589/// instructions (udiv and sdiv). It is called by the visitors to those integer
590/// division instructions.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +0000591/// Common integer divide transforms
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000592Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
593 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel9530f182018-01-21 16:14:51 +0000594 bool IsSigned = I.getOpcode() == Instruction::SDiv;
Sanjay Patel39059d22018-02-12 14:14:56 +0000595 Type *Ty = I.getType();
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000596
Chris Lattner7c99f192011-05-22 18:18:41 +0000597 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000598 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +0000599 I.setOperand(1, V);
600 return &I;
601 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000602
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000603 // Handle cases involving: [su]div X, (select Cond, Y, Z)
604 // This does not apply for fdiv.
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000605 if (simplifyDivRemOfSelectWithZeroOp(I))
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000606 return &I;
607
Sanjay Patel1998cc62018-02-12 18:38:35 +0000608 const APInt *C2;
609 if (match(Op1, m_APInt(C2))) {
610 Value *X;
611 const APInt *C1;
David Majnemerf9a095d2014-08-16 08:55:06 +0000612
Sanjay Patel1998cc62018-02-12 18:38:35 +0000613 // (X / C1) / C2 -> X / (C1*C2)
614 if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) ||
615 (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) {
616 APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
617 if (!multiplyOverflows(*C1, *C2, Product, IsSigned))
618 return BinaryOperator::Create(I.getOpcode(), X,
619 ConstantInt::get(Ty, Product));
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000620 }
Sanjay Patel1998cc62018-02-12 18:38:35 +0000621
622 if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
623 (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) {
624 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
625
626 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
627 if (isMultiple(*C2, *C1, Quotient, IsSigned)) {
628 auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X,
629 ConstantInt::get(Ty, Quotient));
630 NewDiv->setIsExact(I.isExact());
631 return NewDiv;
632 }
633
634 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
635 if (isMultiple(*C1, *C2, Quotient, IsSigned)) {
636 auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
637 ConstantInt::get(Ty, Quotient));
638 auto *OBO = cast<OverflowingBinaryOperator>(Op0);
639 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
640 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
641 return Mul;
642 }
643 }
644
645 if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) &&
646 *C1 != C1->getBitWidth() - 1) ||
647 (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))))) {
648 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
649 APInt C1Shifted = APInt::getOneBitSet(
650 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
651
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000652 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000653 if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
654 auto *BO = BinaryOperator::Create(I.getOpcode(), X,
655 ConstantInt::get(Ty, Quotient));
656 BO->setIsExact(I.isExact());
657 return BO;
658 }
659
Sanjay Patel9d2099c2018-07-15 17:06:59 +0000660 // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2.
Sanjay Patel1998cc62018-02-12 18:38:35 +0000661 if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
662 auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
663 ConstantInt::get(Ty, Quotient));
664 auto *OBO = cast<OverflowingBinaryOperator>(Op0);
665 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
666 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
667 return Mul;
668 }
669 }
670
671 if (!C2->isNullValue()) // avoid X udiv 0
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000672 if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I))
Sanjay Patel1998cc62018-02-12 18:38:35 +0000673 return FoldedDiv;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000674 }
675
Craig Topper218a3592017-04-17 03:41:47 +0000676 if (match(Op0, m_One())) {
Sanjay Patel39059d22018-02-12 14:14:56 +0000677 assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?");
678 if (IsSigned) {
Craig Topper218a3592017-04-17 03:41:47 +0000679 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
680 // result is one, if Op1 is -1 then the result is minus one, otherwise
681 // it's zero.
Craig Topperbb4069e2017-07-07 23:16:26 +0000682 Value *Inc = Builder.CreateAdd(Op1, Op0);
Sanjay Patel39059d22018-02-12 14:14:56 +0000683 Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3));
684 return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0));
Craig Topper218a3592017-04-17 03:41:47 +0000685 } else {
686 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
687 // result is one, otherwise it's zero.
Sanjay Patel39059d22018-02-12 14:14:56 +0000688 return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty);
Nick Lewyckyf0cf8fa2014-05-14 03:03:05 +0000689 }
690 }
691
Benjamin Kramer57b3df52011-04-30 18:16:00 +0000692 // See if we can fold away this div instruction.
693 if (SimplifyDemandedInstructionBits(I))
694 return &I;
695
Duncan Sands771e82a2011-01-28 16:51:11 +0000696 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
Sanjay Patel9530f182018-01-21 16:14:51 +0000697 Value *X, *Z;
698 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1
699 if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
700 (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
Duncan Sands771e82a2011-01-28 16:51:11 +0000701 return BinaryOperator::Create(I.getOpcode(), X, Op1);
Sanjay Patel9530f182018-01-21 16:14:51 +0000702
703 // (X << Y) / X -> 1 << Y
704 Value *Y;
705 if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y))))
Sanjay Patel39059d22018-02-12 14:14:56 +0000706 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y);
Sanjay Patel9530f182018-01-21 16:14:51 +0000707 if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y))))
Sanjay Patel39059d22018-02-12 14:14:56 +0000708 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y);
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000709
Sanjay Patel510d6472018-02-11 17:20:32 +0000710 // X / (X * Y) -> 1 / Y if the multiplication does not overflow.
711 if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) {
712 bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
713 bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
714 if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) {
Sanjay Patel39059d22018-02-12 14:14:56 +0000715 I.setOperand(0, ConstantInt::get(Ty, 1));
Sanjay Patel510d6472018-02-11 17:20:32 +0000716 I.setOperand(1, Y);
717 return &I;
718 }
719 }
720
Craig Topperf40110f2014-04-25 05:29:35 +0000721 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000722}
723
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000724static const unsigned MaxDepth = 6;
725
David Majnemer37f8f442013-07-04 21:17:49 +0000726namespace {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000727
728using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1,
729 const BinaryOperator &I,
730 InstCombiner &IC);
David Majnemer37f8f442013-07-04 21:17:49 +0000731
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000732/// Used to maintain state for visitUDivOperand().
David Majnemer37f8f442013-07-04 21:17:49 +0000733struct UDivFoldAction {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000734 /// Informs visitUDiv() how to fold this operand. This can be zero if this
735 /// action joins two actions together.
736 FoldUDivOperandCb FoldAction;
David Majnemer37f8f442013-07-04 21:17:49 +0000737
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000738 /// Which operand to fold.
739 Value *OperandToFold;
740
David Majnemer37f8f442013-07-04 21:17:49 +0000741 union {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000742 /// The instruction returned when FoldAction is invoked.
743 Instruction *FoldResult;
David Majnemer37f8f442013-07-04 21:17:49 +0000744
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000745 /// Stores the LHS action index if this action joins two actions together.
746 size_t SelectLHSIdx;
David Majnemer37f8f442013-07-04 21:17:49 +0000747 };
748
749 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
Craig Topperf40110f2014-04-25 05:29:35 +0000750 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
David Majnemer37f8f442013-07-04 21:17:49 +0000751 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
752 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
753};
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000754
755} // end anonymous namespace
David Majnemer37f8f442013-07-04 21:17:49 +0000756
757// X udiv 2^C -> X >> C
758static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
759 const BinaryOperator &I, InstCombiner &IC) {
Simon Pilgrim94cc89d2018-02-08 14:46:10 +0000760 Constant *C1 = getLogBase2(Op0->getType(), cast<Constant>(Op1));
761 if (!C1)
762 llvm_unreachable("Failed to constant fold udiv -> logbase2");
763 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1);
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000764 if (I.isExact())
765 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000766 return LShr;
767}
768
David Majnemer37f8f442013-07-04 21:17:49 +0000769// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000770// X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2)
David Majnemer37f8f442013-07-04 21:17:49 +0000771static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
772 InstCombiner &IC) {
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000773 Value *ShiftLeft;
774 if (!match(Op1, m_ZExt(m_Value(ShiftLeft))))
775 ShiftLeft = Op1;
David Majnemer37f8f442013-07-04 21:17:49 +0000776
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000777 Constant *CI;
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000778 Value *N;
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000779 if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N))))
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000780 llvm_unreachable("match should never fail here!");
Simon Pilgrim2a90acd2018-02-08 15:19:38 +0000781 Constant *Log2Base = getLogBase2(N->getType(), CI);
782 if (!Log2Base)
783 llvm_unreachable("getLogBase2 should never fail here!");
784 N = IC.Builder.CreateAdd(N, Log2Base);
Andrea Di Biagioa82d52d2016-09-26 12:07:23 +0000785 if (Op1 != ShiftLeft)
Craig Topperbb4069e2017-07-07 23:16:26 +0000786 N = IC.Builder.CreateZExt(N, Op1->getType());
David Majnemer37f8f442013-07-04 21:17:49 +0000787 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
Suyog Sarda65f5ae92014-10-07 12:04:07 +0000788 if (I.isExact())
789 LShr->setIsExact();
David Majnemer37f8f442013-07-04 21:17:49 +0000790 return LShr;
791}
792
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000793// Recursively visits the possible right hand operands of a udiv
David Majnemer37f8f442013-07-04 21:17:49 +0000794// instruction, seeing through select instructions, to determine if we can
795// replace the udiv with something simpler. If we find that an operand is not
796// able to simplify the udiv, we abort the entire transformation.
797static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
798 SmallVectorImpl<UDivFoldAction> &Actions,
799 unsigned Depth = 0) {
800 // Check to see if this is an unsigned division with an exact power of 2,
801 // if so, convert to a right shift.
802 if (match(Op1, m_Power2())) {
803 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
804 return Actions.size();
805 }
806
David Majnemer37f8f442013-07-04 21:17:49 +0000807 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
808 if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
809 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
810 Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
811 return Actions.size();
812 }
813
814 // The remaining tests are all recursive, so bail out if we hit the limit.
815 if (Depth++ == MaxDepth)
816 return 0;
817
818 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
David Majnemer492e6122014-08-30 09:19:05 +0000819 if (size_t LHSIdx =
820 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
821 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
822 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
David Majnemer37f8f442013-07-04 21:17:49 +0000823 return Actions.size();
824 }
825
826 return 0;
827}
828
Sanjay Patelbb789382017-08-24 22:54:01 +0000829/// If we have zero-extended operands of an unsigned div or rem, we may be able
830/// to narrow the operation (sink the zext below the math).
831static Instruction *narrowUDivURem(BinaryOperator &I,
832 InstCombiner::BuilderTy &Builder) {
833 Instruction::BinaryOps Opcode = I.getOpcode();
834 Value *N = I.getOperand(0);
835 Value *D = I.getOperand(1);
836 Type *Ty = I.getType();
837 Value *X, *Y;
838 if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) &&
839 X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) {
840 // udiv (zext X), (zext Y) --> zext (udiv X, Y)
841 // urem (zext X), (zext Y) --> zext (urem X, Y)
842 Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y);
843 return new ZExtInst(NarrowOp, Ty);
844 }
845
846 Constant *C;
847 if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) ||
848 (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) {
849 // If the constant is the same in the smaller type, use the narrow version.
850 Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
851 if (ConstantExpr::getZExt(TruncC, Ty) != C)
852 return nullptr;
853
854 // udiv (zext X), C --> zext (udiv X, C')
855 // urem (zext X), C --> zext (urem X, C')
856 // udiv C, (zext X) --> zext (udiv C', X)
857 // urem C, (zext X) --> zext (urem C', X)
858 Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC)
859 : Builder.CreateBinOp(Opcode, TruncC, X);
860 return new ZExtInst(NarrowOp, Ty);
861 }
862
863 return nullptr;
864}
865
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000866Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000867 if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1),
868 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000869 return replaceInstUsesWith(I, V);
Duncan Sands771e82a2011-01-28 16:51:11 +0000870
Sanjay Patel79dceb22018-10-03 15:20:58 +0000871 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000872 return X;
873
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000874 // Handle the integer div common cases
875 if (Instruction *Common = commonIDivTransforms(I))
876 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000877
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000878 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel38a86d32018-06-25 22:50:26 +0000879 Value *X;
880 const APInt *C1, *C2;
881 if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
882 // (X lshr C1) udiv C2 --> X udiv (C2 << C1)
883 bool Overflow;
884 APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
885 if (!Overflow) {
886 bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
887 BinaryOperator *BO = BinaryOperator::CreateUDiv(
888 X, ConstantInt::get(X->getType(), C2ShlC1));
889 if (IsExact)
890 BO->setIsExact();
891 return BO;
David Majnemera2521382014-10-13 21:48:30 +0000892 }
Nadav Rotem11935b22012-08-28 10:01:43 +0000893 }
894
Sanjay Patel38a86d32018-06-25 22:50:26 +0000895 // Op0 / C where C is large (negative) --> zext (Op0 >= C)
896 // TODO: Could use isKnownNegative() to handle non-constant values.
Sanjay Patel7c45deb2018-06-26 12:41:15 +0000897 Type *Ty = I.getType();
Sanjay Patel38a86d32018-06-25 22:50:26 +0000898 if (match(Op1, m_Negative())) {
899 Value *Cmp = Builder.CreateICmpUGE(Op0, Op1);
Sanjay Patel7c45deb2018-06-26 12:41:15 +0000900 return CastInst::CreateZExtOrBitCast(Cmp, Ty);
901 }
902 // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined)
903 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
904 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
905 return CastInst::CreateZExtOrBitCast(Cmp, Ty);
Sanjay Patel38a86d32018-06-25 22:50:26 +0000906 }
907
Sanjay Patelbb789382017-08-24 22:54:01 +0000908 if (Instruction *NarrowDiv = narrowUDivURem(I, Builder))
909 return NarrowDiv;
Benjamin Kramer9aa91b12011-04-30 18:16:07 +0000910
Sanjay Patel6d6eab62018-07-26 19:22:41 +0000911 // If the udiv operands are non-overflowing multiplies with a common operand,
912 // then eliminate the common factor:
913 // (A * B) / (A * X) --> B / X (and commuted variants)
914 // TODO: The code would be reduced if we had m_c_NUWMul pattern matching.
915 // TODO: If -reassociation handled this generally, we could remove this.
916 Value *A, *B;
917 if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) {
918 if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) ||
919 match(Op1, m_NUWMul(m_Value(X), m_Specific(A))))
920 return BinaryOperator::CreateUDiv(B, X);
921 if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) ||
922 match(Op1, m_NUWMul(m_Value(X), m_Specific(B))))
923 return BinaryOperator::CreateUDiv(A, X);
924 }
925
David Majnemer37f8f442013-07-04 21:17:49 +0000926 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
927 SmallVector<UDivFoldAction, 6> UDivActions;
928 if (visitUDivOperand(Op0, Op1, I, UDivActions))
929 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
930 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
931 Value *ActionOp1 = UDivActions[i].OperandToFold;
932 Instruction *Inst;
933 if (Action)
934 Inst = Action(Op0, ActionOp1, I, *this);
935 else {
936 // This action joins two actions together. The RHS of this action is
937 // simply the last action we processed, we saved the LHS action index in
938 // the joining action.
939 size_t SelectRHSIdx = i - 1;
940 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
941 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
942 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
943 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
944 SelectLHS, SelectRHS);
945 }
946
947 // If this is the last action to process, return it to the InstCombiner.
948 // Otherwise, we insert it before the UDiv and record it so that we may
949 // use it as part of a joining action (i.e., a SelectInst).
950 if (e - i != 1) {
951 Inst->insertBefore(&I);
952 UDivActions[i].FoldResult = Inst;
953 } else
954 return Inst;
955 }
956
Craig Topperf40110f2014-04-25 05:29:35 +0000957 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000958}
959
960Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000961 if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1),
962 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000963 return replaceInstUsesWith(I, V);
Duncan Sands771e82a2011-01-28 16:51:11 +0000964
Sanjay Patel79dceb22018-10-03 15:20:58 +0000965 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000966 return X;
967
Chris Lattnerdc054bf2010-01-05 06:09:35 +0000968 // Handle the integer div common cases
969 if (Instruction *Common = commonIDivTransforms(I))
970 return Common;
971
Sanjay Patel7b0fc752018-06-21 17:06:36 +0000972 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel6a96d902018-06-25 21:39:41 +0000973 Value *X;
974 // sdiv Op0, -1 --> -Op0
975 // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
976 if (match(Op1, m_AllOnes()) ||
977 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
978 return BinaryOperator::CreateNeg(Op0);
979
Sanjay Patelc6ada532016-06-27 17:25:57 +0000980 const APInt *Op1C;
Sanjay Patelbedd1f92016-06-27 18:38:40 +0000981 if (match(Op1, m_APInt(Op1C))) {
Sanjay Patelbedd1f92016-06-27 18:38:40 +0000982 // sdiv exact X, C --> ashr exact X, log2(C)
983 if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) {
984 Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2());
985 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
986 }
Sanjay Patel59ed2ff2016-06-27 22:27:11 +0000987
988 // If the dividend is sign-extended and the constant divisor is small enough
989 // to fit in the source type, shrink the division to the narrower type:
990 // (sext X) sdiv C --> sext (X sdiv C)
991 Value *Op0Src;
992 if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) &&
993 Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) {
994
995 // In the general case, we need to make sure that the dividend is not the
996 // minimum signed value because dividing that by -1 is UB. But here, we
997 // know that the -1 divisor case is already handled above.
998
999 Constant *NarrowDivisor =
1000 ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType());
Craig Topperbb4069e2017-07-07 23:16:26 +00001001 Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor);
Sanjay Patel59ed2ff2016-06-27 22:27:11 +00001002 return new SExtInst(NarrowOp, Op0->getType());
1003 }
Benjamin Kramer72196f32014-01-19 15:24:22 +00001004 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001005
Benjamin Kramer72196f32014-01-19 15:24:22 +00001006 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
David Majnemerf28e2a42014-07-02 06:42:13 +00001007 // X/INT_MIN -> X == INT_MIN
1008 if (RHS->isMinSignedValue())
Craig Topperbb4069e2017-07-07 23:16:26 +00001009 return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType());
David Majnemerf28e2a42014-07-02 06:42:13 +00001010
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001011 // -X/C --> X/-C provided the negation doesn't overflow.
David Majnemerfa4699e2014-11-22 20:00:34 +00001012 Value *X;
1013 if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
1014 auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS));
1015 BO->setIsExact(I.isExact());
1016 return BO;
1017 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001018 }
1019
1020 // If the sign bits of both operands are zero (i.e. we can prove they are
1021 // unsigned inputs), turn this into a udiv.
Craig Topperbcfd2d12017-04-20 16:56:25 +00001022 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
Craig Topperf2484682017-04-17 01:51:19 +00001023 if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
1024 if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
1025 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
1026 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1027 BO->setIsExact(I.isExact());
1028 return BO;
1029 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001030
Craig Topperd4039f72017-05-25 21:51:12 +00001031 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
Craig Topperf2484682017-04-17 01:51:19 +00001032 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1033 // Safe because the only negative value (1 << Y) can take on is
1034 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1035 // the sign bit set.
1036 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1037 BO->setIsExact(I.isExact());
1038 return BO;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001039 }
1040 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001041
Craig Topperf40110f2014-04-25 05:29:35 +00001042 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001043}
1044
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001045/// Remove negation and try to convert division into multiplication.
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001046static Instruction *foldFDivConstantDivisor(BinaryOperator &I) {
1047 Constant *C;
1048 if (!match(I.getOperand(1), m_Constant(C)))
Craig Topperf40110f2014-04-25 05:29:35 +00001049 return nullptr;
Benjamin Kramer76b15d02014-01-19 13:36:27 +00001050
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001051 // -X / C --> X / -C
1052 Value *X;
1053 if (match(I.getOperand(0), m_FNeg(m_Value(X))))
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001054 return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001055
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001056 // If the constant divisor has an exact inverse, this is always safe. If not,
1057 // then we can still create a reciprocal if fast-math-flags allow it and the
1058 // constant is a regular number (not zero, infinite, or denormal).
1059 if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP())))
Craig Topperf40110f2014-04-25 05:29:35 +00001060 return nullptr;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001061
Sanjay Patel90f4c8e2018-02-20 16:08:15 +00001062 // Disallow denormal constants because we don't know what would happen
1063 // on all targets.
1064 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1065 // denorms are flushed?
1066 auto *RecipC = ConstantExpr::getFDiv(ConstantFP::get(I.getType(), 1.0), C);
1067 if (!RecipC->isNormalFP())
1068 return nullptr;
1069
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001070 // X / C --> X * (1 / C)
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001071 return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001072}
1073
Sanjay Patel6f716a72018-02-21 00:01:45 +00001074/// Remove negation and try to reassociate constant math.
Sanjay Patele4129542018-02-19 21:17:58 +00001075static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001076 Constant *C;
1077 if (!match(I.getOperand(0), m_Constant(C)))
Sanjay Patele4129542018-02-19 21:17:58 +00001078 return nullptr;
1079
Sanjay Patel6f716a72018-02-21 00:01:45 +00001080 // C / -X --> -C / X
Sanjay Patele4129542018-02-19 21:17:58 +00001081 Value *X;
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001082 if (match(I.getOperand(1), m_FNeg(m_Value(X))))
1083 return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
Sanjay Patel6f716a72018-02-21 00:01:45 +00001084
1085 if (!I.hasAllowReassoc() || !I.hasAllowReciprocal())
1086 return nullptr;
1087
1088 // Try to reassociate C / X expressions where X includes another constant.
Sanjay Patele4129542018-02-19 21:17:58 +00001089 Constant *C2, *NewC = nullptr;
1090 if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001091 // C / (X * C2) --> (C / C2) / X
1092 NewC = ConstantExpr::getFDiv(C, C2);
Sanjay Patele4129542018-02-19 21:17:58 +00001093 } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) {
Sanjay Patel6f716a72018-02-21 00:01:45 +00001094 // C / (X / C2) --> (C * C2) / X
1095 NewC = ConstantExpr::getFMul(C, C2);
Sanjay Patele4129542018-02-19 21:17:58 +00001096 }
1097 // Disallow denormal constants because we don't know what would happen
1098 // on all targets.
1099 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1100 // denorms are flushed?
1101 if (!NewC || !NewC->isNormalFP())
1102 return nullptr;
1103
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001104 return BinaryOperator::CreateFDivFMF(NewC, X, &I);
Sanjay Patele4129542018-02-19 21:17:58 +00001105}
1106
Frits van Bommel2a559512011-01-29 17:50:27 +00001107Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001108 if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1),
1109 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +00001110 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001111 return replaceInstUsesWith(I, V);
Frits van Bommel2a559512011-01-29 17:50:27 +00001112
Sanjay Patel79dceb22018-10-03 15:20:58 +00001113 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001114 return X;
1115
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001116 if (Instruction *R = foldFDivConstantDivisor(I))
1117 return R;
Sanjay Patel28165602018-02-19 23:09:03 +00001118
Sanjay Pateld8dd0152018-02-20 23:51:16 +00001119 if (Instruction *R = foldFDivConstantDividend(I))
1120 return R;
Sanjay Patelb39bcc02018-02-14 23:04:17 +00001121
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001122 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Stephen Lina9b57f62013-07-20 07:13:13 +00001123 if (isa<Constant>(Op0))
1124 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1125 if (Instruction *R = FoldOpIntoSelect(I, SI))
1126 return R;
1127
Sanjay Patel29b98ae2018-02-20 17:14:53 +00001128 if (isa<Constant>(Op1))
Stephen Lina9b57f62013-07-20 07:13:13 +00001129 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1130 if (Instruction *R = FoldOpIntoSelect(I, SI))
1131 return R;
Shuxin Yang320f52a2013-01-14 22:48:41 +00001132
Sanjay Patel31a90462018-02-26 16:02:45 +00001133 if (I.hasAllowReassoc() && I.hasAllowReciprocal()) {
Shuxin Yang320f52a2013-01-14 22:48:41 +00001134 Value *X, *Y;
Sanjay Patel91bb7752018-02-16 17:52:32 +00001135 if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1136 (!isa<Constant>(Y) || !isa<Constant>(Op1))) {
1137 // (X / Y) / Z => X / (Y * Z)
Sanjay Patel31a90462018-02-26 16:02:45 +00001138 Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I);
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001139 return BinaryOperator::CreateFDivFMF(X, YZ, &I);
Shuxin Yang320f52a2013-01-14 22:48:41 +00001140 }
Sanjay Patel91bb7752018-02-16 17:52:32 +00001141 if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1142 (!isa<Constant>(Y) || !isa<Constant>(Op0))) {
1143 // Z / (X / Y) => (Y * Z) / X
Sanjay Patel31a90462018-02-26 16:02:45 +00001144 Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
Sanjay Patel5a6f9042018-02-21 22:18:55 +00001145 return BinaryOperator::CreateFDivFMF(YZ, X, &I);
Benjamin Kramer8564e0d2011-03-30 15:42:35 +00001146 }
1147 }
1148
Sanjay Patel339b4d32018-02-15 15:07:12 +00001149 if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
Sanjay Patel65da14d2018-02-16 16:13:20 +00001150 // sin(X) / cos(X) -> tan(X)
1151 // cos(X) / sin(X) -> 1/tan(X) (cotangent)
1152 Value *X;
1153 bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) &&
1154 match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)));
1155 bool IsCot =
1156 !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) &&
1157 match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X)));
Dmitry Venikove5fbf592018-01-11 06:33:00 +00001158
Sanjay Patel65da14d2018-02-16 16:13:20 +00001159 if ((IsTan || IsCot) && hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan,
1160 LibFunc_tanf, LibFunc_tanl)) {
1161 IRBuilder<> B(&I);
1162 IRBuilder<>::FastMathFlagGuard FMFGuard(B);
1163 B.setFastMathFlags(I.getFastMathFlags());
1164 AttributeList Attrs = CallSite(Op0).getCalledFunction()->getAttributes();
Mikael Holmene3605d02018-10-18 06:27:53 +00001165 Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf,
1166 LibFunc_tanl, B, Attrs);
Sanjay Patel65da14d2018-02-16 16:13:20 +00001167 if (IsCot)
1168 Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res);
1169 return replaceInstUsesWith(I, Res);
Dmitry Venikove5fbf592018-01-11 06:33:00 +00001170 }
1171 }
1172
Sanjay Patel1998cc62018-02-12 18:38:35 +00001173 // -X / -Y -> X / Y
1174 Value *X, *Y;
1175 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) {
1176 I.setOperand(0, X);
1177 I.setOperand(1, Y);
Matt Arsenaultfdb78f82017-01-10 23:08:54 +00001178 return &I;
1179 }
1180
Sanjay Patel4a4f35f2018-02-12 19:39:21 +00001181 // X / (X * Y) --> 1.0 / Y
1182 // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
1183 // We can ignore the possibility that X is infinity because INF/INF is NaN.
1184 if (I.hasNoNaNs() && I.hasAllowReassoc() &&
1185 match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) {
1186 I.setOperand(0, ConstantFP::get(I.getType(), 1.0));
1187 I.setOperand(1, Y);
1188 return &I;
1189 }
1190
Craig Topperf40110f2014-04-25 05:29:35 +00001191 return nullptr;
Frits van Bommel2a559512011-01-29 17:50:27 +00001192}
1193
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001194/// This function implements the transforms common to both integer remainder
1195/// instructions (urem and srem). It is called by the visitors to those integer
1196/// remainder instructions.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00001197/// Common integer remainder transforms
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001198Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
1199 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1200
Chris Lattner7c99f192011-05-22 18:18:41 +00001201 // The RHS is known non-zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001202 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
Chris Lattner7c99f192011-05-22 18:18:41 +00001203 I.setOperand(1, V);
1204 return &I;
1205 }
1206
Duncan Sandsa3e36992011-05-02 16:27:02 +00001207 // Handle cases involving: rem X, (select Cond, Y, Z)
Sanjay Patelae2e3a42017-10-06 23:20:16 +00001208 if (simplifyDivRemOfSelectWithZeroOp(I))
Duncan Sandsa3e36992011-05-02 16:27:02 +00001209 return &I;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001210
Benjamin Kramer72196f32014-01-19 15:24:22 +00001211 if (isa<Constant>(Op1)) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001212 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1213 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1214 if (Instruction *R = FoldOpIntoSelect(I, SI))
1215 return R;
Craig Topperfb71b7d2017-04-14 19:20:12 +00001216 } else if (auto *PN = dyn_cast<PHINode>(Op0I)) {
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001217 const APInt *Op1Int;
1218 if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
1219 (I.getOpcode() == Instruction::URem ||
1220 !Op1Int->isMinSignedValue())) {
Craig Topperfb71b7d2017-04-14 19:20:12 +00001221 // foldOpIntoPhi will speculate instructions to the end of the PHI's
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001222 // predecessor blocks, so do this only if we know the srem or urem
1223 // will not fault.
Craig Topperfb71b7d2017-04-14 19:20:12 +00001224 if (Instruction *NV = foldOpIntoPhi(I, PN))
Sanjoy Dasb7e861a2016-06-05 21:17:04 +00001225 return NV;
1226 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001227 }
1228
1229 // See if we can fold away this rem instruction.
1230 if (SimplifyDemandedInstructionBits(I))
1231 return &I;
1232 }
1233 }
1234
Craig Topperf40110f2014-04-25 05:29:35 +00001235 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001236}
1237
1238Instruction *InstCombiner::visitURem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001239 if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1),
1240 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001241 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001242
Sanjay Patel79dceb22018-10-03 15:20:58 +00001243 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001244 return X;
1245
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001246 if (Instruction *common = commonIRemTransforms(I))
1247 return common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001248
Sanjay Patelbb789382017-08-24 22:54:01 +00001249 if (Instruction *NarrowRem = narrowUDivURem(I, Builder))
1250 return NarrowRem;
David Majnemer6c30f492013-05-12 00:07:05 +00001251
David Majnemer470b0772013-05-11 09:01:28 +00001252 // X urem Y -> X and Y-1, where Y is a power of 2,
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001253 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001254 Type *Ty = I.getType();
Craig Topperd4039f72017-05-25 21:51:12 +00001255 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001256 Constant *N1 = Constant::getAllOnesValue(Ty);
Craig Topperbb4069e2017-07-07 23:16:26 +00001257 Value *Add = Builder.CreateAdd(Op1, N1);
Chris Lattner6b657ae2011-02-10 05:36:31 +00001258 return BinaryOperator::CreateAnd(Op0, Add);
1259 }
1260
Nick Lewycky7459be62013-07-13 01:16:47 +00001261 // 1 urem X -> zext(X != 1)
Sanjay Patel9adea012018-06-26 16:39:29 +00001262 if (match(Op0, m_One()))
1263 return CastInst::CreateZExtOrBitCast(Builder.CreateICmpNE(Op1, Op0), Ty);
Nick Lewycky7459be62013-07-13 01:16:47 +00001264
Sanjay Patel30ef70b2016-09-22 22:36:26 +00001265 // X urem C -> X < C ? X : X - C, where C >= signbit.
Simon Pilgrim1889f262018-02-08 18:36:01 +00001266 if (match(Op1, m_Negative())) {
Craig Topperbb4069e2017-07-07 23:16:26 +00001267 Value *Cmp = Builder.CreateICmpULT(Op0, Op1);
1268 Value *Sub = Builder.CreateSub(Op0, Op1);
Sanjay Patel30ef70b2016-09-22 22:36:26 +00001269 return SelectInst::Create(Cmp, Op0, Sub);
1270 }
1271
Sanjay Patel3575f0c2018-06-26 16:30:00 +00001272 // If the divisor is a sext of a boolean, then the divisor must be max
1273 // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also
1274 // max unsigned value. In that case, the remainder is 0:
1275 // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0
1276 Value *X;
1277 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1278 Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
1279 return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0);
1280 }
1281
Craig Topperf40110f2014-04-25 05:29:35 +00001282 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001283}
1284
1285Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001286 if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1),
1287 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001288 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001289
Sanjay Patel79dceb22018-10-03 15:20:58 +00001290 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001291 return X;
1292
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001293 // Handle the integer rem common cases
1294 if (Instruction *Common = commonIRemTransforms(I))
1295 return Common;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001296
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001297 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
David Majnemerdb077302014-10-13 22:37:51 +00001298 {
1299 const APInt *Y;
1300 // X % -Y -> X % Y
Simon Pilgrima54e8e42018-02-08 19:00:45 +00001301 if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) {
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001302 Worklist.AddValue(I.getOperand(1));
David Majnemerdb077302014-10-13 22:37:51 +00001303 I.setOperand(1, ConstantInt::get(I.getType(), -*Y));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001304 return &I;
1305 }
David Majnemerdb077302014-10-13 22:37:51 +00001306 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001307
1308 // If the sign bits of both operands are zero (i.e. we can prove they are
1309 // unsigned inputs), turn this into a urem.
Craig Topperbcfd2d12017-04-20 16:56:25 +00001310 APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
Craig Topper1a18a7c2017-04-17 01:51:24 +00001311 if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
1312 MaskedValueIsZero(Op0, Mask, 0, &I)) {
1313 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
1314 return BinaryOperator::CreateURem(Op0, Op1, I.getName());
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001315 }
1316
1317 // If it's a constant vector, flip any negative values positive.
Chris Lattner0256be92012-01-27 03:08:05 +00001318 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1319 Constant *C = cast<Constant>(Op1);
1320 unsigned VWidth = C->getType()->getVectorNumElements();
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001321
1322 bool hasNegative = false;
Chris Lattner0256be92012-01-27 03:08:05 +00001323 bool hasMissing = false;
1324 for (unsigned i = 0; i != VWidth; ++i) {
1325 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001326 if (!Elt) {
Chris Lattner0256be92012-01-27 03:08:05 +00001327 hasMissing = true;
1328 break;
1329 }
1330
1331 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
Chris Lattnerb1a15122011-07-15 06:08:15 +00001332 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001333 hasNegative = true;
Chris Lattner0256be92012-01-27 03:08:05 +00001334 }
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001335
Chris Lattner0256be92012-01-27 03:08:05 +00001336 if (hasNegative && !hasMissing) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001337 SmallVector<Constant *, 16> Elts(VWidth);
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001338 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner8213c8a2012-02-06 21:56:39 +00001339 Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
Chris Lattner0256be92012-01-27 03:08:05 +00001340 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
Chris Lattnerb1a15122011-07-15 06:08:15 +00001341 if (RHS->isNegative())
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001342 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001343 }
1344 }
1345
1346 Constant *NewRHSV = ConstantVector::get(Elts);
Chris Lattner0256be92012-01-27 03:08:05 +00001347 if (NewRHSV != C) { // Don't loop on -MININT
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001348 Worklist.AddValue(I.getOperand(1));
1349 I.setOperand(1, NewRHSV);
1350 return &I;
1351 }
1352 }
1353 }
1354
Craig Topperf40110f2014-04-25 05:29:35 +00001355 return nullptr;
Chris Lattnerdc054bf2010-01-05 06:09:35 +00001356}
1357
1358Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
Sanjay Patel7b0fc752018-06-21 17:06:36 +00001359 if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1),
1360 I.getFastMathFlags(),
Craig Toppera4205622017-06-09 03:21:29 +00001361 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001362 return replaceInstUsesWith(I, V);
Duncan Sandsa3e36992011-05-02 16:27:02 +00001363
Sanjay Patel79dceb22018-10-03 15:20:58 +00001364 if (Instruction *X = foldVectorBinop(I))
Sanjay Patelbbc6d602018-06-02 16:27:44 +00001365 return X;
1366
Craig Topperf40110f2014-04-25 05:29:35 +00001367 return nullptr;
Duncan Sandsa3e36992011-05-02 16:27:02 +00001368}