Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Transforms / InstCombine / InstCombineAddSub.cpp
blobbf92b5dc5594b1a824c4401bb38dea5e3519e20d
1 //===- InstCombineAddSub.cpp ------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visit functions for add, fadd, sub, and fsub.
11 //===----------------------------------------------------------------------===//
13 #include "InstCombineInternal.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/ValueTracking.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"
25 #include "llvm/IR/Operator.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/IR/Value.h"
29 #include "llvm/Support/AlignOf.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/KnownBits.h"
32 #include <cassert>
33 #include <utility>
35 using namespace llvm;
36 using namespace PatternMatch;
38 #define DEBUG_TYPE "instcombine"
40 namespace {
42 /// Class representing coefficient of floating-point addend.
43 /// This class needs to be highly efficient, which is especially true for
44 /// the constructor. As of I write this comment, the cost of the default
45 /// constructor is merely 4-byte-store-zero (Assuming compiler is able to
46 /// perform write-merging).
47 ///
48 class FAddendCoef {
49 public:
50 // The constructor has to initialize a APFloat, which is unnecessary for
51 // most addends which have coefficient either 1 or -1. So, the constructor
52 // is expensive. In order to avoid the cost of the constructor, we should
53 // reuse some instances whenever possible. The pre-created instances
54 // FAddCombine::Add[0-5] embodies this idea.
55 FAddendCoef() = default;
56 ~FAddendCoef();
58 // If possible, don't define operator+/operator- etc because these
59 // operators inevitably call FAddendCoef's constructor which is not cheap.
60 void operator=(const FAddendCoef &A);
61 void operator+=(const FAddendCoef &A);
62 void operator*=(const FAddendCoef &S);
64 void set(short C) {
65 assert(!insaneIntVal(C) && "Insane coefficient");
66 IsFp = false; IntVal = C;
69 void set(const APFloat& C);
71 void negate();
73 bool isZero() const { return isInt() ? !IntVal : getFpVal().isZero(); }
74 Value *getValue(Type *) const;
76 bool isOne() const { return isInt() && IntVal == 1; }
77 bool isTwo() const { return isInt() && IntVal == 2; }
78 bool isMinusOne() const { return isInt() && IntVal == -1; }
79 bool isMinusTwo() const { return isInt() && IntVal == -2; }
81 private:
82 bool insaneIntVal(int V) { return V > 4 || V < -4; }
84 APFloat *getFpValPtr()
85 { return reinterpret_cast<APFloat *>(&FpValBuf.buffer[0]); }
87 const APFloat *getFpValPtr() const
88 { return reinterpret_cast<const APFloat *>(&FpValBuf.buffer[0]); }
90 const APFloat &getFpVal() const {
91 assert(IsFp && BufHasFpVal && "Incorret state");
92 return *getFpValPtr();
95 APFloat &getFpVal() {
96 assert(IsFp && BufHasFpVal && "Incorret state");
97 return *getFpValPtr();
100 bool isInt() const { return !IsFp; }
102 // If the coefficient is represented by an integer, promote it to a
103 // floating point.
104 void convertToFpType(const fltSemantics &Sem);
106 // Construct an APFloat from a signed integer.
107 // TODO: We should get rid of this function when APFloat can be constructed
108 // from an *SIGNED* integer.
109 APFloat createAPFloatFromInt(const fltSemantics &Sem, int Val);
111 bool IsFp = false;
113 // True iff FpValBuf contains an instance of APFloat.
114 bool BufHasFpVal = false;
116 // The integer coefficient of an individual addend is either 1 or -1,
117 // and we try to simplify at most 4 addends from neighboring at most
118 // two instructions. So the range of <IntVal> falls in [-4, 4]. APInt
119 // is overkill of this end.
120 short IntVal = 0;
122 AlignedCharArrayUnion<APFloat> FpValBuf;
125 /// FAddend is used to represent floating-point addend. An addend is
126 /// represented as <C, V>, where the V is a symbolic value, and C is a
127 /// constant coefficient. A constant addend is represented as <C, 0>.
128 class FAddend {
129 public:
130 FAddend() = default;
132 void operator+=(const FAddend &T) {
133 assert((Val == T.Val) && "Symbolic-values disagree");
134 Coeff += T.Coeff;
137 Value *getSymVal() const { return Val; }
138 const FAddendCoef &getCoef() const { return Coeff; }
140 bool isConstant() const { return Val == nullptr; }
141 bool isZero() const { return Coeff.isZero(); }
143 void set(short Coefficient, Value *V) {
144 Coeff.set(Coefficient);
145 Val = V;
147 void set(const APFloat &Coefficient, Value *V) {
148 Coeff.set(Coefficient);
149 Val = V;
151 void set(const ConstantFP *Coefficient, Value *V) {
152 Coeff.set(Coefficient->getValueAPF());
153 Val = V;
156 void negate() { Coeff.negate(); }
158 /// Drill down the U-D chain one step to find the definition of V, and
159 /// try to break the definition into one or two addends.
160 static unsigned drillValueDownOneStep(Value* V, FAddend &A0, FAddend &A1);
162 /// Similar to FAddend::drillDownOneStep() except that the value being
163 /// splitted is the addend itself.
164 unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1) const;
166 private:
167 void Scale(const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; }
169 // This addend has the value of "Coeff * Val".
170 Value *Val = nullptr;
171 FAddendCoef Coeff;
174 /// FAddCombine is the class for optimizing an unsafe fadd/fsub along
175 /// with its neighboring at most two instructions.
177 class FAddCombine {
178 public:
179 FAddCombine(InstCombiner::BuilderTy &B) : Builder(B) {}
181 Value *simplify(Instruction *FAdd);
183 private:
184 using AddendVect = SmallVector<const FAddend *, 4>;
186 Value *simplifyFAdd(AddendVect& V, unsigned InstrQuota);
188 /// Convert given addend to a Value
189 Value *createAddendVal(const FAddend &A, bool& NeedNeg);
191 /// Return the number of instructions needed to emit the N-ary addition.
192 unsigned calcInstrNumber(const AddendVect& Vect);
194 Value *createFSub(Value *Opnd0, Value *Opnd1);
195 Value *createFAdd(Value *Opnd0, Value *Opnd1);
196 Value *createFMul(Value *Opnd0, Value *Opnd1);
197 Value *createFNeg(Value *V);
198 Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota);
199 void createInstPostProc(Instruction *NewInst, bool NoNumber = false);
201 // Debugging stuff are clustered here.
202 #ifndef NDEBUG
203 unsigned CreateInstrNum;
204 void initCreateInstNum() { CreateInstrNum = 0; }
205 void incCreateInstNum() { CreateInstrNum++; }
206 #else
207 void initCreateInstNum() {}
208 void incCreateInstNum() {}
209 #endif
211 InstCombiner::BuilderTy &Builder;
212 Instruction *Instr = nullptr;
215 } // end anonymous namespace
217 //===----------------------------------------------------------------------===//
219 // Implementation of
220 // {FAddendCoef, FAddend, FAddition, FAddCombine}.
222 //===----------------------------------------------------------------------===//
223 FAddendCoef::~FAddendCoef() {
224 if (BufHasFpVal)
225 getFpValPtr()->~APFloat();
228 void FAddendCoef::set(const APFloat& C) {
229 APFloat *P = getFpValPtr();
231 if (isInt()) {
232 // As the buffer is meanless byte stream, we cannot call
233 // APFloat::operator=().
234 new(P) APFloat(C);
235 } else
236 *P = C;
238 IsFp = BufHasFpVal = true;
241 void FAddendCoef::convertToFpType(const fltSemantics &Sem) {
242 if (!isInt())
243 return;
245 APFloat *P = getFpValPtr();
246 if (IntVal > 0)
247 new(P) APFloat(Sem, IntVal);
248 else {
249 new(P) APFloat(Sem, 0 - IntVal);
250 P->changeSign();
252 IsFp = BufHasFpVal = true;
255 APFloat FAddendCoef::createAPFloatFromInt(const fltSemantics &Sem, int Val) {
256 if (Val >= 0)
257 return APFloat(Sem, Val);
259 APFloat T(Sem, 0 - Val);
260 T.changeSign();
262 return T;
265 void FAddendCoef::operator=(const FAddendCoef &That) {
266 if (That.isInt())
267 set(That.IntVal);
268 else
269 set(That.getFpVal());
272 void FAddendCoef::operator+=(const FAddendCoef &That) {
273 enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven;
274 if (isInt() == That.isInt()) {
275 if (isInt())
276 IntVal += That.IntVal;
277 else
278 getFpVal().add(That.getFpVal(), RndMode);
279 return;
282 if (isInt()) {
283 const APFloat &T = That.getFpVal();
284 convertToFpType(T.getSemantics());
285 getFpVal().add(T, RndMode);
286 return;
289 APFloat &T = getFpVal();
290 T.add(createAPFloatFromInt(T.getSemantics(), That.IntVal), RndMode);
293 void FAddendCoef::operator*=(const FAddendCoef &That) {
294 if (That.isOne())
295 return;
297 if (That.isMinusOne()) {
298 negate();
299 return;
302 if (isInt() && That.isInt()) {
303 int Res = IntVal * (int)That.IntVal;
304 assert(!insaneIntVal(Res) && "Insane int value");
305 IntVal = Res;
306 return;
309 const fltSemantics &Semantic =
310 isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics();
312 if (isInt())
313 convertToFpType(Semantic);
314 APFloat &F0 = getFpVal();
316 if (That.isInt())
317 F0.multiply(createAPFloatFromInt(Semantic, That.IntVal),
318 APFloat::rmNearestTiesToEven);
319 else
320 F0.multiply(That.getFpVal(), APFloat::rmNearestTiesToEven);
323 void FAddendCoef::negate() {
324 if (isInt())
325 IntVal = 0 - IntVal;
326 else
327 getFpVal().changeSign();
330 Value *FAddendCoef::getValue(Type *Ty) const {
331 return isInt() ?
332 ConstantFP::get(Ty, float(IntVal)) :
333 ConstantFP::get(Ty->getContext(), getFpVal());
336 // The definition of <Val> Addends
337 // =========================================
338 // A + B <1, A>, <1,B>
339 // A - B <1, A>, <1,B>
340 // 0 - B <-1, B>
341 // C * A, <C, A>
342 // A + C <1, A> <C, NULL>
343 // 0 +/- 0 <0, NULL> (corner case)
345 // Legend: A and B are not constant, C is constant
346 unsigned FAddend::drillValueDownOneStep
347 (Value *Val, FAddend &Addend0, FAddend &Addend1) {
348 Instruction *I = nullptr;
349 if (!Val || !(I = dyn_cast<Instruction>(Val)))
350 return 0;
352 unsigned Opcode = I->getOpcode();
354 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) {
355 ConstantFP *C0, *C1;
356 Value *Opnd0 = I->getOperand(0);
357 Value *Opnd1 = I->getOperand(1);
358 if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero())
359 Opnd0 = nullptr;
361 if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero())
362 Opnd1 = nullptr;
364 if (Opnd0) {
365 if (!C0)
366 Addend0.set(1, Opnd0);
367 else
368 Addend0.set(C0, nullptr);
371 if (Opnd1) {
372 FAddend &Addend = Opnd0 ? Addend1 : Addend0;
373 if (!C1)
374 Addend.set(1, Opnd1);
375 else
376 Addend.set(C1, nullptr);
377 if (Opcode == Instruction::FSub)
378 Addend.negate();
381 if (Opnd0 || Opnd1)
382 return Opnd0 && Opnd1 ? 2 : 1;
384 // Both operands are zero. Weird!
385 Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr);
386 return 1;
389 if (I->getOpcode() == Instruction::FMul) {
390 Value *V0 = I->getOperand(0);
391 Value *V1 = I->getOperand(1);
392 if (ConstantFP *C = dyn_cast<ConstantFP>(V0)) {
393 Addend0.set(C, V1);
394 return 1;
397 if (ConstantFP *C = dyn_cast<ConstantFP>(V1)) {
398 Addend0.set(C, V0);
399 return 1;
403 return 0;
406 // Try to break *this* addend into two addends. e.g. Suppose this addend is
407 // <2.3, V>, and V = X + Y, by calling this function, we obtain two addends,
408 // i.e. <2.3, X> and <2.3, Y>.
409 unsigned FAddend::drillAddendDownOneStep
410 (FAddend &Addend0, FAddend &Addend1) const {
411 if (isConstant())
412 return 0;
414 unsigned BreakNum = FAddend::drillValueDownOneStep(Val, Addend0, Addend1);
415 if (!BreakNum || Coeff.isOne())
416 return BreakNum;
418 Addend0.Scale(Coeff);
420 if (BreakNum == 2)
421 Addend1.Scale(Coeff);
423 return BreakNum;
426 Value *FAddCombine::simplify(Instruction *I) {
427 assert(I->hasAllowReassoc() && I->hasNoSignedZeros() &&
428 "Expected 'reassoc'+'nsz' instruction");
430 // Currently we are not able to handle vector type.
431 if (I->getType()->isVectorTy())
432 return nullptr;
434 assert((I->getOpcode() == Instruction::FAdd ||
435 I->getOpcode() == Instruction::FSub) && "Expect add/sub");
437 // Save the instruction before calling other member-functions.
438 Instr = I;
440 FAddend Opnd0, Opnd1, Opnd0_0, Opnd0_1, Opnd1_0, Opnd1_1;
442 unsigned OpndNum = FAddend::drillValueDownOneStep(I, Opnd0, Opnd1);
444 // Step 1: Expand the 1st addend into Opnd0_0 and Opnd0_1.
445 unsigned Opnd0_ExpNum = 0;
446 unsigned Opnd1_ExpNum = 0;
448 if (!Opnd0.isConstant())
449 Opnd0_ExpNum = Opnd0.drillAddendDownOneStep(Opnd0_0, Opnd0_1);
451 // Step 2: Expand the 2nd addend into Opnd1_0 and Opnd1_1.
452 if (OpndNum == 2 && !Opnd1.isConstant())
453 Opnd1_ExpNum = Opnd1.drillAddendDownOneStep(Opnd1_0, Opnd1_1);
455 // Step 3: Try to optimize Opnd0_0 + Opnd0_1 + Opnd1_0 + Opnd1_1
456 if (Opnd0_ExpNum && Opnd1_ExpNum) {
457 AddendVect AllOpnds;
458 AllOpnds.push_back(&Opnd0_0);
459 AllOpnds.push_back(&Opnd1_0);
460 if (Opnd0_ExpNum == 2)
461 AllOpnds.push_back(&Opnd0_1);
462 if (Opnd1_ExpNum == 2)
463 AllOpnds.push_back(&Opnd1_1);
465 // Compute instruction quota. We should save at least one instruction.
466 unsigned InstQuota = 0;
468 Value *V0 = I->getOperand(0);
469 Value *V1 = I->getOperand(1);
470 InstQuota = ((!isa<Constant>(V0) && V0->hasOneUse()) &&
471 (!isa<Constant>(V1) && V1->hasOneUse())) ? 2 : 1;
473 if (Value *R = simplifyFAdd(AllOpnds, InstQuota))
474 return R;
477 if (OpndNum != 2) {
478 // The input instruction is : "I=0.0 +/- V". If the "V" were able to be
479 // splitted into two addends, say "V = X - Y", the instruction would have
480 // been optimized into "I = Y - X" in the previous steps.
482 const FAddendCoef &CE = Opnd0.getCoef();
483 return CE.isOne() ? Opnd0.getSymVal() : nullptr;
486 // step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1]
487 if (Opnd1_ExpNum) {
488 AddendVect AllOpnds;
489 AllOpnds.push_back(&Opnd0);
490 AllOpnds.push_back(&Opnd1_0);
491 if (Opnd1_ExpNum == 2)
492 AllOpnds.push_back(&Opnd1_1);
494 if (Value *R = simplifyFAdd(AllOpnds, 1))
495 return R;
498 // step 5: Try to optimize Opnd1 + Opnd0_0 [+ Opnd0_1]
499 if (Opnd0_ExpNum) {
500 AddendVect AllOpnds;
501 AllOpnds.push_back(&Opnd1);
502 AllOpnds.push_back(&Opnd0_0);
503 if (Opnd0_ExpNum == 2)
504 AllOpnds.push_back(&Opnd0_1);
506 if (Value *R = simplifyFAdd(AllOpnds, 1))
507 return R;
510 return nullptr;
513 Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
514 unsigned AddendNum = Addends.size();
515 assert(AddendNum <= 4 && "Too many addends");
517 // For saving intermediate results;
518 unsigned NextTmpIdx = 0;
519 FAddend TmpResult[3];
521 // Points to the constant addend of the resulting simplified expression.
522 // If the resulting expr has constant-addend, this constant-addend is
523 // desirable to reside at the top of the resulting expression tree. Placing
524 // constant close to supper-expr(s) will potentially reveal some optimization
525 // opportunities in super-expr(s).
526 const FAddend *ConstAdd = nullptr;
528 // Simplified addends are placed <SimpVect>.
529 AddendVect SimpVect;
531 // The outer loop works on one symbolic-value at a time. Suppose the input
532 // addends are : <a1, x>, <b1, y>, <a2, x>, <c1, z>, <b2, y>, ...
533 // The symbolic-values will be processed in this order: x, y, z.
534 for (unsigned SymIdx = 0; SymIdx < AddendNum; SymIdx++) {
536 const FAddend *ThisAddend = Addends[SymIdx];
537 if (!ThisAddend) {
538 // This addend was processed before.
539 continue;
542 Value *Val = ThisAddend->getSymVal();
543 unsigned StartIdx = SimpVect.size();
544 SimpVect.push_back(ThisAddend);
546 // The inner loop collects addends sharing same symbolic-value, and these
547 // addends will be later on folded into a single addend. Following above
548 // example, if the symbolic value "y" is being processed, the inner loop
549 // will collect two addends "<b1,y>" and "<b2,Y>". These two addends will
550 // be later on folded into "<b1+b2, y>".
551 for (unsigned SameSymIdx = SymIdx + 1;
552 SameSymIdx < AddendNum; SameSymIdx++) {
553 const FAddend *T = Addends[SameSymIdx];
554 if (T && T->getSymVal() == Val) {
555 // Set null such that next iteration of the outer loop will not process
556 // this addend again.
557 Addends[SameSymIdx] = nullptr;
558 SimpVect.push_back(T);
562 // If multiple addends share same symbolic value, fold them together.
563 if (StartIdx + 1 != SimpVect.size()) {
564 FAddend &R = TmpResult[NextTmpIdx ++];
565 R = *SimpVect[StartIdx];
566 for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++)
567 R += *SimpVect[Idx];
569 // Pop all addends being folded and push the resulting folded addend.
570 SimpVect.resize(StartIdx);
571 if (Val) {
572 if (!R.isZero()) {
573 SimpVect.push_back(&R);
575 } else {
576 // Don't push constant addend at this time. It will be the last element
577 // of <SimpVect>.
578 ConstAdd = &R;
583 assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) &&
584 "out-of-bound access");
586 if (ConstAdd)
587 SimpVect.push_back(ConstAdd);
589 Value *Result;
590 if (!SimpVect.empty())
591 Result = createNaryFAdd(SimpVect, InstrQuota);
592 else {
593 // The addition is folded to 0.0.
594 Result = ConstantFP::get(Instr->getType(), 0.0);
597 return Result;
600 Value *FAddCombine::createNaryFAdd
601 (const AddendVect &Opnds, unsigned InstrQuota) {
602 assert(!Opnds.empty() && "Expect at least one addend");
604 // Step 1: Check if the # of instructions needed exceeds the quota.
606 unsigned InstrNeeded = calcInstrNumber(Opnds);
607 if (InstrNeeded > InstrQuota)
608 return nullptr;
610 initCreateInstNum();
612 // step 2: Emit the N-ary addition.
613 // Note that at most three instructions are involved in Fadd-InstCombine: the
614 // addition in question, and at most two neighboring instructions.
615 // The resulting optimized addition should have at least one less instruction
616 // than the original addition expression tree. This implies that the resulting
617 // N-ary addition has at most two instructions, and we don't need to worry
618 // about tree-height when constructing the N-ary addition.
620 Value *LastVal = nullptr;
621 bool LastValNeedNeg = false;
623 // Iterate the addends, creating fadd/fsub using adjacent two addends.
624 for (const FAddend *Opnd : Opnds) {
625 bool NeedNeg;
626 Value *V = createAddendVal(*Opnd, NeedNeg);
627 if (!LastVal) {
628 LastVal = V;
629 LastValNeedNeg = NeedNeg;
630 continue;
633 if (LastValNeedNeg == NeedNeg) {
634 LastVal = createFAdd(LastVal, V);
635 continue;
638 if (LastValNeedNeg)
639 LastVal = createFSub(V, LastVal);
640 else
641 LastVal = createFSub(LastVal, V);
643 LastValNeedNeg = false;
646 if (LastValNeedNeg) {
647 LastVal = createFNeg(LastVal);
650 #ifndef NDEBUG
651 assert(CreateInstrNum == InstrNeeded &&
652 "Inconsistent in instruction numbers");
653 #endif
655 return LastVal;
658 Value *FAddCombine::createFSub(Value *Opnd0, Value *Opnd1) {
659 Value *V = Builder.CreateFSub(Opnd0, Opnd1);
660 if (Instruction *I = dyn_cast<Instruction>(V))
661 createInstPostProc(I);
662 return V;
665 Value *FAddCombine::createFNeg(Value *V) {
666 Value *Zero = cast<Value>(ConstantFP::getZeroValueForNegation(V->getType()));
667 Value *NewV = createFSub(Zero, V);
668 if (Instruction *I = dyn_cast<Instruction>(NewV))
669 createInstPostProc(I, true); // fneg's don't receive instruction numbers.
670 return NewV;
673 Value *FAddCombine::createFAdd(Value *Opnd0, Value *Opnd1) {
674 Value *V = Builder.CreateFAdd(Opnd0, Opnd1);
675 if (Instruction *I = dyn_cast<Instruction>(V))
676 createInstPostProc(I);
677 return V;
680 Value *FAddCombine::createFMul(Value *Opnd0, Value *Opnd1) {
681 Value *V = Builder.CreateFMul(Opnd0, Opnd1);
682 if (Instruction *I = dyn_cast<Instruction>(V))
683 createInstPostProc(I);
684 return V;
687 void FAddCombine::createInstPostProc(Instruction *NewInstr, bool NoNumber) {
688 NewInstr->setDebugLoc(Instr->getDebugLoc());
690 // Keep track of the number of instruction created.
691 if (!NoNumber)
692 incCreateInstNum();
694 // Propagate fast-math flags
695 NewInstr->setFastMathFlags(Instr->getFastMathFlags());
698 // Return the number of instruction needed to emit the N-ary addition.
699 // NOTE: Keep this function in sync with createAddendVal().
700 unsigned FAddCombine::calcInstrNumber(const AddendVect &Opnds) {
701 unsigned OpndNum = Opnds.size();
702 unsigned InstrNeeded = OpndNum - 1;
704 // The number of addends in the form of "(-1)*x".
705 unsigned NegOpndNum = 0;
707 // Adjust the number of instructions needed to emit the N-ary add.
708 for (const FAddend *Opnd : Opnds) {
709 if (Opnd->isConstant())
710 continue;
712 // The constant check above is really for a few special constant
713 // coefficients.
714 if (isa<UndefValue>(Opnd->getSymVal()))
715 continue;
717 const FAddendCoef &CE = Opnd->getCoef();
718 if (CE.isMinusOne() || CE.isMinusTwo())
719 NegOpndNum++;
721 // Let the addend be "c * x". If "c == +/-1", the value of the addend
722 // is immediately available; otherwise, it needs exactly one instruction
723 // to evaluate the value.
724 if (!CE.isMinusOne() && !CE.isOne())
725 InstrNeeded++;
727 if (NegOpndNum == OpndNum)
728 InstrNeeded++;
729 return InstrNeeded;
732 // Input Addend Value NeedNeg(output)
733 // ================================================================
734 // Constant C C false
735 // <+/-1, V> V coefficient is -1
736 // <2/-2, V> "fadd V, V" coefficient is -2
737 // <C, V> "fmul V, C" false
739 // NOTE: Keep this function in sync with FAddCombine::calcInstrNumber.
740 Value *FAddCombine::createAddendVal(const FAddend &Opnd, bool &NeedNeg) {
741 const FAddendCoef &Coeff = Opnd.getCoef();
743 if (Opnd.isConstant()) {
744 NeedNeg = false;
745 return Coeff.getValue(Instr->getType());
748 Value *OpndVal = Opnd.getSymVal();
750 if (Coeff.isMinusOne() || Coeff.isOne()) {
751 NeedNeg = Coeff.isMinusOne();
752 return OpndVal;
755 if (Coeff.isTwo() || Coeff.isMinusTwo()) {
756 NeedNeg = Coeff.isMinusTwo();
757 return createFAdd(OpndVal, OpndVal);
760 NeedNeg = false;
761 return createFMul(OpndVal, Coeff.getValue(Instr->getType()));
764 // Checks if any operand is negative and we can convert add to sub.
765 // This function checks for following negative patterns
766 // ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C))
767 // ADD(XOR(AND(Z, C), C), 1) == NEG(OR(Z, ~C))
768 // XOR(AND(Z, C), (C + 1)) == NEG(OR(Z, ~C)) if C is even
769 static Value *checkForNegativeOperand(BinaryOperator &I,
770 InstCombiner::BuilderTy &Builder) {
771 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
773 // This function creates 2 instructions to replace ADD, we need at least one
774 // of LHS or RHS to have one use to ensure benefit in transform.
775 if (!LHS->hasOneUse() && !RHS->hasOneUse())
776 return nullptr;
778 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
779 const APInt *C1 = nullptr, *C2 = nullptr;
781 // if ONE is on other side, swap
782 if (match(RHS, m_Add(m_Value(X), m_One())))
783 std::swap(LHS, RHS);
785 if (match(LHS, m_Add(m_Value(X), m_One()))) {
786 // if XOR on other side, swap
787 if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1))))
788 std::swap(X, RHS);
790 if (match(X, m_Xor(m_Value(Y), m_APInt(C1)))) {
791 // X = XOR(Y, C1), Y = OR(Z, C2), C2 = NOT(C1) ==> X == NOT(AND(Z, C1))
792 // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, AND(Z, C1))
793 if (match(Y, m_Or(m_Value(Z), m_APInt(C2))) && (*C2 == ~(*C1))) {
794 Value *NewAnd = Builder.CreateAnd(Z, *C1);
795 return Builder.CreateSub(RHS, NewAnd, "sub");
796 } else if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && (*C1 == *C2)) {
797 // X = XOR(Y, C1), Y = AND(Z, C2), C2 == C1 ==> X == NOT(OR(Z, ~C1))
798 // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, OR(Z, ~C1))
799 Value *NewOr = Builder.CreateOr(Z, ~(*C1));
800 return Builder.CreateSub(RHS, NewOr, "sub");
805 // Restore LHS and RHS
806 LHS = I.getOperand(0);
807 RHS = I.getOperand(1);
809 // if XOR is on other side, swap
810 if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1))))
811 std::swap(LHS, RHS);
813 // C2 is ODD
814 // LHS = XOR(Y, C1), Y = AND(Z, C2), C1 == (C2 + 1) => LHS == NEG(OR(Z, ~C2))
815 // ADD(LHS, RHS) == SUB(RHS, OR(Z, ~C2))
816 if (match(LHS, m_Xor(m_Value(Y), m_APInt(C1))))
817 if (C1->countTrailingZeros() == 0)
818 if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && *C1 == (*C2 + 1)) {
819 Value *NewOr = Builder.CreateOr(Z, ~(*C2));
820 return Builder.CreateSub(RHS, NewOr, "sub");
822 return nullptr;
825 Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) {
826 Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1);
827 Constant *Op1C;
828 if (!match(Op1, m_Constant(Op1C)))
829 return nullptr;
831 if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add))
832 return NV;
834 Value *X, *Y;
836 // add (sub X, Y), -1 --> add (not Y), X
837 if (match(Op0, m_OneUse(m_Sub(m_Value(X), m_Value(Y)))) &&
838 match(Op1, m_AllOnes()))
839 return BinaryOperator::CreateAdd(Builder.CreateNot(Y), X);
841 // zext(bool) + C -> bool ? C + 1 : C
842 if (match(Op0, m_ZExt(m_Value(X))) &&
843 X->getType()->getScalarSizeInBits() == 1)
844 return SelectInst::Create(X, AddOne(Op1C), Op1);
846 // ~X + C --> (C-1) - X
847 if (match(Op0, m_Not(m_Value(X))))
848 return BinaryOperator::CreateSub(SubOne(Op1C), X);
850 const APInt *C;
851 if (!match(Op1, m_APInt(C)))
852 return nullptr;
854 if (C->isSignMask()) {
855 // If wrapping is not allowed, then the addition must set the sign bit:
856 // X + (signmask) --> X | signmask
857 if (Add.hasNoSignedWrap() || Add.hasNoUnsignedWrap())
858 return BinaryOperator::CreateOr(Op0, Op1);
860 // If wrapping is allowed, then the addition flips the sign bit of LHS:
861 // X + (signmask) --> X ^ signmask
862 return BinaryOperator::CreateXor(Op0, Op1);
865 // Is this add the last step in a convoluted sext?
866 // add(zext(xor i16 X, -32768), -32768) --> sext X
867 Type *Ty = Add.getType();
868 const APInt *C2;
869 if (match(Op0, m_ZExt(m_Xor(m_Value(X), m_APInt(C2)))) &&
870 C2->isMinSignedValue() && C2->sext(Ty->getScalarSizeInBits()) == *C)
871 return CastInst::Create(Instruction::SExt, X, Ty);
873 // (add (zext (add nuw X, C2)), C) --> (zext (add nuw X, C2 + C))
874 if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) &&
875 C->isNegative() && C->sge(-C2->sext(C->getBitWidth()))) {
876 Constant *NewC =
877 ConstantInt::get(X->getType(), *C2 + C->trunc(C2->getBitWidth()));
878 return new ZExtInst(Builder.CreateNUWAdd(X, NewC), Ty);
881 if (C->isOneValue() && Op0->hasOneUse()) {
882 // add (sext i1 X), 1 --> zext (not X)
883 // TODO: The smallest IR representation is (select X, 0, 1), and that would
884 // not require the one-use check. But we need to remove a transform in
885 // visitSelect and make sure that IR value tracking for select is equal or
886 // better than for these ops.
887 if (match(Op0, m_SExt(m_Value(X))) &&
888 X->getType()->getScalarSizeInBits() == 1)
889 return new ZExtInst(Builder.CreateNot(X), Ty);
891 // Shifts and add used to flip and mask off the low bit:
892 // add (ashr (shl i32 X, 31), 31), 1 --> and (not X), 1
893 const APInt *C3;
894 if (match(Op0, m_AShr(m_Shl(m_Value(X), m_APInt(C2)), m_APInt(C3))) &&
895 C2 == C3 && *C2 == Ty->getScalarSizeInBits() - 1) {
896 Value *NotX = Builder.CreateNot(X);
897 return BinaryOperator::CreateAnd(NotX, ConstantInt::get(Ty, 1));
901 return nullptr;
904 // Matches multiplication expression Op * C where C is a constant. Returns the
905 // constant value in C and the other operand in Op. Returns true if such a
906 // match is found.
907 static bool MatchMul(Value *E, Value *&Op, APInt &C) {
908 const APInt *AI;
909 if (match(E, m_Mul(m_Value(Op), m_APInt(AI)))) {
910 C = *AI;
911 return true;
913 if (match(E, m_Shl(m_Value(Op), m_APInt(AI)))) {
914 C = APInt(AI->getBitWidth(), 1);
915 C <<= *AI;
916 return true;
918 return false;
921 // Matches remainder expression Op % C where C is a constant. Returns the
922 // constant value in C and the other operand in Op. Returns the signedness of
923 // the remainder operation in IsSigned. Returns true if such a match is
924 // found.
925 static bool MatchRem(Value *E, Value *&Op, APInt &C, bool &IsSigned) {
926 const APInt *AI;
927 IsSigned = false;
928 if (match(E, m_SRem(m_Value(Op), m_APInt(AI)))) {
929 IsSigned = true;
930 C = *AI;
931 return true;
933 if (match(E, m_URem(m_Value(Op), m_APInt(AI)))) {
934 C = *AI;
935 return true;
937 if (match(E, m_And(m_Value(Op), m_APInt(AI))) && (*AI + 1).isPowerOf2()) {
938 C = *AI + 1;
939 return true;
941 return false;
944 // Matches division expression Op / C with the given signedness as indicated
945 // by IsSigned, where C is a constant. Returns the constant value in C and the
946 // other operand in Op. Returns true if such a match is found.
947 static bool MatchDiv(Value *E, Value *&Op, APInt &C, bool IsSigned) {
948 const APInt *AI;
949 if (IsSigned && match(E, m_SDiv(m_Value(Op), m_APInt(AI)))) {
950 C = *AI;
951 return true;
953 if (!IsSigned) {
954 if (match(E, m_UDiv(m_Value(Op), m_APInt(AI)))) {
955 C = *AI;
956 return true;
958 if (match(E, m_LShr(m_Value(Op), m_APInt(AI)))) {
959 C = APInt(AI->getBitWidth(), 1);
960 C <<= *AI;
961 return true;
964 return false;
967 // Returns whether C0 * C1 with the given signedness overflows.
968 static bool MulWillOverflow(APInt &C0, APInt &C1, bool IsSigned) {
969 bool overflow;
970 if (IsSigned)
971 (void)C0.smul_ov(C1, overflow);
972 else
973 (void)C0.umul_ov(C1, overflow);
974 return overflow;
977 // Simplifies X % C0 + (( X / C0 ) % C1) * C0 to X % (C0 * C1), where (C0 * C1)
978 // does not overflow.
979 Value *InstCombiner::SimplifyAddWithRemainder(BinaryOperator &I) {
980 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
981 Value *X, *MulOpV;
982 APInt C0, MulOpC;
983 bool IsSigned;
984 // Match I = X % C0 + MulOpV * C0
985 if (((MatchRem(LHS, X, C0, IsSigned) && MatchMul(RHS, MulOpV, MulOpC)) ||
986 (MatchRem(RHS, X, C0, IsSigned) && MatchMul(LHS, MulOpV, MulOpC))) &&
987 C0 == MulOpC) {
988 Value *RemOpV;
989 APInt C1;
990 bool Rem2IsSigned;
991 // Match MulOpC = RemOpV % C1
992 if (MatchRem(MulOpV, RemOpV, C1, Rem2IsSigned) &&
993 IsSigned == Rem2IsSigned) {
994 Value *DivOpV;
995 APInt DivOpC;
996 // Match RemOpV = X / C0
997 if (MatchDiv(RemOpV, DivOpV, DivOpC, IsSigned) && X == DivOpV &&
998 C0 == DivOpC && !MulWillOverflow(C0, C1, IsSigned)) {
999 Value *NewDivisor =
1000 ConstantInt::get(X->getType()->getContext(), C0 * C1);
1001 return IsSigned ? Builder.CreateSRem(X, NewDivisor, "srem")
1002 : Builder.CreateURem(X, NewDivisor, "urem");
1007 return nullptr;
1010 /// Fold
1011 /// (1 << NBits) - 1
1012 /// Into:
1013 /// ~(-(1 << NBits))
1014 /// Because a 'not' is better for bit-tracking analysis and other transforms
1015 /// than an 'add'. The new shl is always nsw, and is nuw if old `and` was.
1016 static Instruction *canonicalizeLowbitMask(BinaryOperator &I,
1017 InstCombiner::BuilderTy &Builder) {
1018 Value *NBits;
1019 if (!match(&I, m_Add(m_OneUse(m_Shl(m_One(), m_Value(NBits))), m_AllOnes())))
1020 return nullptr;
1022 Constant *MinusOne = Constant::getAllOnesValue(NBits->getType());
1023 Value *NotMask = Builder.CreateShl(MinusOne, NBits, "notmask");
1024 // Be wary of constant folding.
1025 if (auto *BOp = dyn_cast<BinaryOperator>(NotMask)) {
1026 // Always NSW. But NUW propagates from `add`.
1027 BOp->setHasNoSignedWrap();
1028 BOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
1031 return BinaryOperator::CreateNot(NotMask, I.getName());
1034 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
1035 if (Value *V = SimplifyAddInst(I.getOperand(0), I.getOperand(1),
1036 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
1037 SQ.getWithInstruction(&I)))
1038 return replaceInstUsesWith(I, V);
1040 if (SimplifyAssociativeOrCommutative(I))
1041 return &I;
1043 if (Instruction *X = foldVectorBinop(I))
1044 return X;
1046 // (A*B)+(A*C) -> A*(B+C) etc
1047 if (Value *V = SimplifyUsingDistributiveLaws(I))
1048 return replaceInstUsesWith(I, V);
1050 if (Instruction *X = foldAddWithConstant(I))
1051 return X;
1053 // FIXME: This should be moved into the above helper function to allow these
1054 // transforms for general constant or constant splat vectors.
1055 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1056 Type *Ty = I.getType();
1057 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1058 Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr;
1059 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
1060 unsigned TySizeBits = Ty->getScalarSizeInBits();
1061 const APInt &RHSVal = CI->getValue();
1062 unsigned ExtendAmt = 0;
1063 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
1064 // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
1065 if (XorRHS->getValue() == -RHSVal) {
1066 if (RHSVal.isPowerOf2())
1067 ExtendAmt = TySizeBits - RHSVal.logBase2() - 1;
1068 else if (XorRHS->getValue().isPowerOf2())
1069 ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1;
1072 if (ExtendAmt) {
1073 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
1074 if (!MaskedValueIsZero(XorLHS, Mask, 0, &I))
1075 ExtendAmt = 0;
1078 if (ExtendAmt) {
1079 Constant *ShAmt = ConstantInt::get(Ty, ExtendAmt);
1080 Value *NewShl = Builder.CreateShl(XorLHS, ShAmt, "sext");
1081 return BinaryOperator::CreateAShr(NewShl, ShAmt);
1084 // If this is a xor that was canonicalized from a sub, turn it back into
1085 // a sub and fuse this add with it.
1086 if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
1087 KnownBits LHSKnown = computeKnownBits(XorLHS, 0, &I);
1088 if ((XorRHS->getValue() | LHSKnown.Zero).isAllOnesValue())
1089 return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
1090 XorLHS);
1092 // (X + signmask) + C could have gotten canonicalized to (X^signmask) + C,
1093 // transform them into (X + (signmask ^ C))
1094 if (XorRHS->getValue().isSignMask())
1095 return BinaryOperator::CreateAdd(XorLHS,
1096 ConstantExpr::getXor(XorRHS, CI));
1100 if (Ty->isIntOrIntVectorTy(1))
1101 return BinaryOperator::CreateXor(LHS, RHS);
1103 // X + X --> X << 1
1104 if (LHS == RHS) {
1105 auto *Shl = BinaryOperator::CreateShl(LHS, ConstantInt::get(Ty, 1));
1106 Shl->setHasNoSignedWrap(I.hasNoSignedWrap());
1107 Shl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
1108 return Shl;
1111 Value *A, *B;
1112 if (match(LHS, m_Neg(m_Value(A)))) {
1113 // -A + -B --> -(A + B)
1114 if (match(RHS, m_Neg(m_Value(B))))
1115 return BinaryOperator::CreateNeg(Builder.CreateAdd(A, B));
1117 // -A + B --> B - A
1118 return BinaryOperator::CreateSub(RHS, A);
1121 // A + -B --> A - B
1122 if (match(RHS, m_Neg(m_Value(B))))
1123 return BinaryOperator::CreateSub(LHS, B);
1125 if (Value *V = checkForNegativeOperand(I, Builder))
1126 return replaceInstUsesWith(I, V);
1128 // (A + 1) + ~B --> A - B
1129 // ~B + (A + 1) --> A - B
1130 if (match(&I, m_c_BinOp(m_Add(m_Value(A), m_One()), m_Not(m_Value(B)))))
1131 return BinaryOperator::CreateSub(A, B);
1133 // X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1)
1134 if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V);
1136 // A+B --> A|B iff A and B have no bits set in common.
1137 if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
1138 return BinaryOperator::CreateOr(LHS, RHS);
1140 // FIXME: We already did a check for ConstantInt RHS above this.
1141 // FIXME: Is this pattern covered by another fold? No regression tests fail on
1142 // removal.
1143 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
1144 // (X & FF00) + xx00 -> (X+xx00) & FF00
1145 Value *X;
1146 ConstantInt *C2;
1147 if (LHS->hasOneUse() &&
1148 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
1149 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
1150 // See if all bits from the first bit set in the Add RHS up are included
1151 // in the mask. First, get the rightmost bit.
1152 const APInt &AddRHSV = CRHS->getValue();
1154 // Form a mask of all bits from the lowest bit added through the top.
1155 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
1157 // See if the and mask includes all of these bits.
1158 APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
1160 if (AddRHSHighBits == AddRHSHighBitsAnd) {
1161 // Okay, the xform is safe. Insert the new add pronto.
1162 Value *NewAdd = Builder.CreateAdd(X, CRHS, LHS->getName());
1163 return BinaryOperator::CreateAnd(NewAdd, C2);
1168 // add (select X 0 (sub n A)) A --> select X A n
1170 SelectInst *SI = dyn_cast<SelectInst>(LHS);
1171 Value *A = RHS;
1172 if (!SI) {
1173 SI = dyn_cast<SelectInst>(RHS);
1174 A = LHS;
1176 if (SI && SI->hasOneUse()) {
1177 Value *TV = SI->getTrueValue();
1178 Value *FV = SI->getFalseValue();
1179 Value *N;
1181 // Can we fold the add into the argument of the select?
1182 // We check both true and false select arguments for a matching subtract.
1183 if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
1184 // Fold the add into the true select value.
1185 return SelectInst::Create(SI->getCondition(), N, A);
1187 if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
1188 // Fold the add into the false select value.
1189 return SelectInst::Create(SI->getCondition(), A, N);
1193 if (Instruction *Ext = narrowMathIfNoOverflow(I))
1194 return Ext;
1196 // (add (xor A, B) (and A, B)) --> (or A, B)
1197 // (add (and A, B) (xor A, B)) --> (or A, B)
1198 if (match(&I, m_c_BinOp(m_Xor(m_Value(A), m_Value(B)),
1199 m_c_And(m_Deferred(A), m_Deferred(B)))))
1200 return BinaryOperator::CreateOr(A, B);
1202 // (add (or A, B) (and A, B)) --> (add A, B)
1203 // (add (and A, B) (or A, B)) --> (add A, B)
1204 if (match(&I, m_c_BinOp(m_Or(m_Value(A), m_Value(B)),
1205 m_c_And(m_Deferred(A), m_Deferred(B))))) {
1206 I.setOperand(0, A);
1207 I.setOperand(1, B);
1208 return &I;
1211 // TODO(jingyue): Consider willNotOverflowSignedAdd and
1212 // willNotOverflowUnsignedAdd to reduce the number of invocations of
1213 // computeKnownBits.
1214 bool Changed = false;
1215 if (!I.hasNoSignedWrap() && willNotOverflowSignedAdd(LHS, RHS, I)) {
1216 Changed = true;
1217 I.setHasNoSignedWrap(true);
1219 if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedAdd(LHS, RHS, I)) {
1220 Changed = true;
1221 I.setHasNoUnsignedWrap(true);
1224 if (Instruction *V = canonicalizeLowbitMask(I, Builder))
1225 return V;
1227 return Changed ? &I : nullptr;
1230 /// Factor a common operand out of fadd/fsub of fmul/fdiv.
1231 static Instruction *factorizeFAddFSub(BinaryOperator &I,
1232 InstCombiner::BuilderTy &Builder) {
1233 assert((I.getOpcode() == Instruction::FAdd ||
1234 I.getOpcode() == Instruction::FSub) && "Expecting fadd/fsub");
1235 assert(I.hasAllowReassoc() && I.hasNoSignedZeros() &&
1236 "FP factorization requires FMF");
1237 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1238 Value *X, *Y, *Z;
1239 bool IsFMul;
1240 if ((match(Op0, m_OneUse(m_FMul(m_Value(X), m_Value(Z)))) &&
1241 match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))) ||
1242 (match(Op0, m_OneUse(m_FMul(m_Value(Z), m_Value(X)))) &&
1243 match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))))
1244 IsFMul = true;
1245 else if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Z)))) &&
1246 match(Op1, m_OneUse(m_FDiv(m_Value(Y), m_Specific(Z)))))
1247 IsFMul = false;
1248 else
1249 return nullptr;
1251 // (X * Z) + (Y * Z) --> (X + Y) * Z
1252 // (X * Z) - (Y * Z) --> (X - Y) * Z
1253 // (X / Z) + (Y / Z) --> (X + Y) / Z
1254 // (X / Z) - (Y / Z) --> (X - Y) / Z
1255 bool IsFAdd = I.getOpcode() == Instruction::FAdd;
1256 Value *XY = IsFAdd ? Builder.CreateFAddFMF(X, Y, &I)
1257 : Builder.CreateFSubFMF(X, Y, &I);
1259 // Bail out if we just created a denormal constant.
1260 // TODO: This is copied from a previous implementation. Is it necessary?
1261 const APFloat *C;
1262 if (match(XY, m_APFloat(C)) && !C->isNormal())
1263 return nullptr;
1265 return IsFMul ? BinaryOperator::CreateFMulFMF(XY, Z, &I)
1266 : BinaryOperator::CreateFDivFMF(XY, Z, &I);
1269 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
1270 if (Value *V = SimplifyFAddInst(I.getOperand(0), I.getOperand(1),
1271 I.getFastMathFlags(),
1272 SQ.getWithInstruction(&I)))
1273 return replaceInstUsesWith(I, V);
1275 if (SimplifyAssociativeOrCommutative(I))
1276 return &I;
1278 if (Instruction *X = foldVectorBinop(I))
1279 return X;
1281 if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
1282 return FoldedFAdd;
1284 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1285 Value *X;
1286 // (-X) + Y --> Y - X
1287 if (match(LHS, m_FNeg(m_Value(X))))
1288 return BinaryOperator::CreateFSubFMF(RHS, X, &I);
1289 // Y + (-X) --> Y - X
1290 if (match(RHS, m_FNeg(m_Value(X))))
1291 return BinaryOperator::CreateFSubFMF(LHS, X, &I);
1293 // Check for (fadd double (sitofp x), y), see if we can merge this into an
1294 // integer add followed by a promotion.
1295 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
1296 Value *LHSIntVal = LHSConv->getOperand(0);
1297 Type *FPType = LHSConv->getType();
1299 // TODO: This check is overly conservative. In many cases known bits
1300 // analysis can tell us that the result of the addition has less significant
1301 // bits than the integer type can hold.
1302 auto IsValidPromotion = [](Type *FTy, Type *ITy) {
1303 Type *FScalarTy = FTy->getScalarType();
1304 Type *IScalarTy = ITy->getScalarType();
1306 // Do we have enough bits in the significand to represent the result of
1307 // the integer addition?
1308 unsigned MaxRepresentableBits =
1309 APFloat::semanticsPrecision(FScalarTy->getFltSemantics());
1310 return IScalarTy->getIntegerBitWidth() <= MaxRepresentableBits;
1313 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
1314 // ... if the constant fits in the integer value. This is useful for things
1315 // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
1316 // requires a constant pool load, and generally allows the add to be better
1317 // instcombined.
1318 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
1319 if (IsValidPromotion(FPType, LHSIntVal->getType())) {
1320 Constant *CI =
1321 ConstantExpr::getFPToSI(CFP, LHSIntVal->getType());
1322 if (LHSConv->hasOneUse() &&
1323 ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
1324 willNotOverflowSignedAdd(LHSIntVal, CI, I)) {
1325 // Insert the new integer add.
1326 Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, CI, "addconv");
1327 return new SIToFPInst(NewAdd, I.getType());
1331 // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
1332 if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
1333 Value *RHSIntVal = RHSConv->getOperand(0);
1334 // It's enough to check LHS types only because we require int types to
1335 // be the same for this transform.
1336 if (IsValidPromotion(FPType, LHSIntVal->getType())) {
1337 // Only do this if x/y have the same type, if at least one of them has a
1338 // single use (so we don't increase the number of int->fp conversions),
1339 // and if the integer add will not overflow.
1340 if (LHSIntVal->getType() == RHSIntVal->getType() &&
1341 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1342 willNotOverflowSignedAdd(LHSIntVal, RHSIntVal, I)) {
1343 // Insert the new integer add.
1344 Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, RHSIntVal, "addconv");
1345 return new SIToFPInst(NewAdd, I.getType());
1351 // Handle specials cases for FAdd with selects feeding the operation
1352 if (Value *V = SimplifySelectsFeedingBinaryOp(I, LHS, RHS))
1353 return replaceInstUsesWith(I, V);
1355 if (I.hasAllowReassoc() && I.hasNoSignedZeros()) {
1356 if (Instruction *F = factorizeFAddFSub(I, Builder))
1357 return F;
1358 if (Value *V = FAddCombine(Builder).simplify(&I))
1359 return replaceInstUsesWith(I, V);
1362 return nullptr;
1365 /// Optimize pointer differences into the same array into a size. Consider:
1366 /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer
1367 /// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
1368 Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
1369 Type *Ty) {
1370 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
1371 // this.
1372 bool Swapped = false;
1373 GEPOperator *GEP1 = nullptr, *GEP2 = nullptr;
1375 // For now we require one side to be the base pointer "A" or a constant
1376 // GEP derived from it.
1377 if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1378 // (gep X, ...) - X
1379 if (LHSGEP->getOperand(0) == RHS) {
1380 GEP1 = LHSGEP;
1381 Swapped = false;
1382 } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1383 // (gep X, ...) - (gep X, ...)
1384 if (LHSGEP->getOperand(0)->stripPointerCasts() ==
1385 RHSGEP->getOperand(0)->stripPointerCasts()) {
1386 GEP2 = RHSGEP;
1387 GEP1 = LHSGEP;
1388 Swapped = false;
1393 if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1394 // X - (gep X, ...)
1395 if (RHSGEP->getOperand(0) == LHS) {
1396 GEP1 = RHSGEP;
1397 Swapped = true;
1398 } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1399 // (gep X, ...) - (gep X, ...)
1400 if (RHSGEP->getOperand(0)->stripPointerCasts() ==
1401 LHSGEP->getOperand(0)->stripPointerCasts()) {
1402 GEP2 = LHSGEP;
1403 GEP1 = RHSGEP;
1404 Swapped = true;
1409 if (!GEP1)
1410 // No GEP found.
1411 return nullptr;
1413 if (GEP2) {
1414 // (gep X, ...) - (gep X, ...)
1416 // Avoid duplicating the arithmetic if there are more than one non-constant
1417 // indices between the two GEPs and either GEP has a non-constant index and
1418 // multiple users. If zero non-constant index, the result is a constant and
1419 // there is no duplication. If one non-constant index, the result is an add
1420 // or sub with a constant, which is no larger than the original code, and
1421 // there's no duplicated arithmetic, even if either GEP has multiple
1422 // users. If more than one non-constant indices combined, as long as the GEP
1423 // with at least one non-constant index doesn't have multiple users, there
1424 // is no duplication.
1425 unsigned NumNonConstantIndices1 = GEP1->countNonConstantIndices();
1426 unsigned NumNonConstantIndices2 = GEP2->countNonConstantIndices();
1427 if (NumNonConstantIndices1 + NumNonConstantIndices2 > 1 &&
1428 ((NumNonConstantIndices1 > 0 && !GEP1->hasOneUse()) ||
1429 (NumNonConstantIndices2 > 0 && !GEP2->hasOneUse()))) {
1430 return nullptr;
1434 // Emit the offset of the GEP and an intptr_t.
1435 Value *Result = EmitGEPOffset(GEP1);
1437 // If we had a constant expression GEP on the other side offsetting the
1438 // pointer, subtract it from the offset we have.
1439 if (GEP2) {
1440 Value *Offset = EmitGEPOffset(GEP2);
1441 Result = Builder.CreateSub(Result, Offset);
1444 // If we have p - gep(p, ...) then we have to negate the result.
1445 if (Swapped)
1446 Result = Builder.CreateNeg(Result, "diff.neg");
1448 return Builder.CreateIntCast(Result, Ty, true);
1451 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1452 if (Value *V = SimplifySubInst(I.getOperand(0), I.getOperand(1),
1453 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
1454 SQ.getWithInstruction(&I)))
1455 return replaceInstUsesWith(I, V);
1457 if (Instruction *X = foldVectorBinop(I))
1458 return X;
1460 // (A*B)-(A*C) -> A*(B-C) etc
1461 if (Value *V = SimplifyUsingDistributiveLaws(I))
1462 return replaceInstUsesWith(I, V);
1464 // If this is a 'B = x-(-A)', change to B = x+A.
1465 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1466 if (Value *V = dyn_castNegVal(Op1)) {
1467 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
1469 if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) {
1470 assert(BO->getOpcode() == Instruction::Sub &&
1471 "Expected a subtraction operator!");
1472 if (BO->hasNoSignedWrap() && I.hasNoSignedWrap())
1473 Res->setHasNoSignedWrap(true);
1474 } else {
1475 if (cast<Constant>(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap())
1476 Res->setHasNoSignedWrap(true);
1479 return Res;
1482 if (I.getType()->isIntOrIntVectorTy(1))
1483 return BinaryOperator::CreateXor(Op0, Op1);
1485 // Replace (-1 - A) with (~A).
1486 if (match(Op0, m_AllOnes()))
1487 return BinaryOperator::CreateNot(Op1);
1489 // (~X) - (~Y) --> Y - X
1490 Value *X, *Y;
1491 if (match(Op0, m_Not(m_Value(X))) && match(Op1, m_Not(m_Value(Y))))
1492 return BinaryOperator::CreateSub(Y, X);
1494 // (X + -1) - Y --> ~Y + X
1495 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes()))))
1496 return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X);
1498 // Y - (X + 1) --> ~X + Y
1499 if (match(Op1, m_OneUse(m_Add(m_Value(X), m_One()))))
1500 return BinaryOperator::CreateAdd(Builder.CreateNot(X), Op0);
1502 if (Constant *C = dyn_cast<Constant>(Op0)) {
1503 bool IsNegate = match(C, m_ZeroInt());
1504 Value *X;
1505 if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1506 // 0 - (zext bool) --> sext bool
1507 // C - (zext bool) --> bool ? C - 1 : C
1508 if (IsNegate)
1509 return CastInst::CreateSExtOrBitCast(X, I.getType());
1510 return SelectInst::Create(X, SubOne(C), C);
1512 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1513 // 0 - (sext bool) --> zext bool
1514 // C - (sext bool) --> bool ? C + 1 : C
1515 if (IsNegate)
1516 return CastInst::CreateZExtOrBitCast(X, I.getType());
1517 return SelectInst::Create(X, AddOne(C), C);
1520 // C - ~X == X + (1+C)
1521 if (match(Op1, m_Not(m_Value(X))))
1522 return BinaryOperator::CreateAdd(X, AddOne(C));
1524 // Try to fold constant sub into select arguments.
1525 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1526 if (Instruction *R = FoldOpIntoSelect(I, SI))
1527 return R;
1529 // Try to fold constant sub into PHI values.
1530 if (PHINode *PN = dyn_cast<PHINode>(Op1))
1531 if (Instruction *R = foldOpIntoPhi(I, PN))
1532 return R;
1534 // C-(X+C2) --> (C-C2)-X
1535 Constant *C2;
1536 if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
1537 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
1540 const APInt *Op0C;
1541 if (match(Op0, m_APInt(Op0C))) {
1542 unsigned BitWidth = I.getType()->getScalarSizeInBits();
1544 // -(X >>u 31) -> (X >>s 31)
1545 // -(X >>s 31) -> (X >>u 31)
1546 if (Op0C->isNullValue()) {
1547 Value *X;
1548 const APInt *ShAmt;
1549 if (match(Op1, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
1550 *ShAmt == BitWidth - 1) {
1551 Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
1552 return BinaryOperator::CreateAShr(X, ShAmtOp);
1554 if (match(Op1, m_AShr(m_Value(X), m_APInt(ShAmt))) &&
1555 *ShAmt == BitWidth - 1) {
1556 Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
1557 return BinaryOperator::CreateLShr(X, ShAmtOp);
1560 if (Op1->hasOneUse()) {
1561 Value *LHS, *RHS;
1562 SelectPatternFlavor SPF = matchSelectPattern(Op1, LHS, RHS).Flavor;
1563 if (SPF == SPF_ABS || SPF == SPF_NABS) {
1564 // This is a negate of an ABS/NABS pattern. Just swap the operands
1565 // of the select.
1566 SelectInst *SI = cast<SelectInst>(Op1);
1567 Value *TrueVal = SI->getTrueValue();
1568 Value *FalseVal = SI->getFalseValue();
1569 SI->setTrueValue(FalseVal);
1570 SI->setFalseValue(TrueVal);
1571 // Don't swap prof metadata, we didn't change the branch behavior.
1572 return replaceInstUsesWith(I, SI);
1577 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
1578 // zero.
1579 if (Op0C->isMask()) {
1580 KnownBits RHSKnown = computeKnownBits(Op1, 0, &I);
1581 if ((*Op0C | RHSKnown.Zero).isAllOnesValue())
1582 return BinaryOperator::CreateXor(Op1, Op0);
1587 Value *Y;
1588 // X-(X+Y) == -Y X-(Y+X) == -Y
1589 if (match(Op1, m_c_Add(m_Specific(Op0), m_Value(Y))))
1590 return BinaryOperator::CreateNeg(Y);
1592 // (X-Y)-X == -Y
1593 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
1594 return BinaryOperator::CreateNeg(Y);
1597 // (sub (or A, B), (xor A, B)) --> (and A, B)
1599 Value *A, *B;
1600 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
1601 match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
1602 return BinaryOperator::CreateAnd(A, B);
1606 Value *Y;
1607 // ((X | Y) - X) --> (~X & Y)
1608 if (match(Op0, m_OneUse(m_c_Or(m_Value(Y), m_Specific(Op1)))))
1609 return BinaryOperator::CreateAnd(
1610 Y, Builder.CreateNot(Op1, Op1->getName() + ".not"));
1613 if (Op1->hasOneUse()) {
1614 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
1615 Constant *C = nullptr;
1617 // (X - (Y - Z)) --> (X + (Z - Y)).
1618 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
1619 return BinaryOperator::CreateAdd(Op0,
1620 Builder.CreateSub(Z, Y, Op1->getName()));
1622 // (X - (X & Y)) --> (X & ~Y)
1623 if (match(Op1, m_c_And(m_Value(Y), m_Specific(Op0))))
1624 return BinaryOperator::CreateAnd(Op0,
1625 Builder.CreateNot(Y, Y->getName() + ".not"));
1627 // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
1628 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && match(Op0, m_Zero()) &&
1629 C->isNotMinSignedValue() && !C->isOneValue())
1630 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
1632 // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
1633 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
1634 if (Value *XNeg = dyn_castNegVal(X))
1635 return BinaryOperator::CreateShl(XNeg, Y);
1637 // Subtracting -1/0 is the same as adding 1/0:
1638 // sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y)
1639 // 'nuw' is dropped in favor of the canonical form.
1640 if (match(Op1, m_SExt(m_Value(Y))) &&
1641 Y->getType()->getScalarSizeInBits() == 1) {
1642 Value *Zext = Builder.CreateZExt(Y, I.getType());
1643 BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Zext);
1644 Add->setHasNoSignedWrap(I.hasNoSignedWrap());
1645 return Add;
1648 // X - A*-B -> X + A*B
1649 // X - -A*B -> X + A*B
1650 Value *A, *B;
1651 if (match(Op1, m_c_Mul(m_Value(A), m_Neg(m_Value(B)))))
1652 return BinaryOperator::CreateAdd(Op0, Builder.CreateMul(A, B));
1654 // X - A*C -> X + A*-C
1655 // No need to handle commuted multiply because multiply handling will
1656 // ensure constant will be move to the right hand side.
1657 if (match(Op1, m_Mul(m_Value(A), m_Constant(C))) && !isa<ConstantExpr>(C)) {
1658 Value *NewMul = Builder.CreateMul(A, ConstantExpr::getNeg(C));
1659 return BinaryOperator::CreateAdd(Op0, NewMul);
1664 // ~A - Min/Max(~A, O) -> Max/Min(A, ~O) - A
1665 // ~A - Min/Max(O, ~A) -> Max/Min(A, ~O) - A
1666 // Min/Max(~A, O) - ~A -> A - Max/Min(A, ~O)
1667 // Min/Max(O, ~A) - ~A -> A - Max/Min(A, ~O)
1668 // So long as O here is freely invertible, this will be neutral or a win.
1669 Value *LHS, *RHS, *A;
1670 Value *NotA = Op0, *MinMax = Op1;
1671 SelectPatternFlavor SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor;
1672 if (!SelectPatternResult::isMinOrMax(SPF)) {
1673 NotA = Op1;
1674 MinMax = Op0;
1675 SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor;
1677 if (SelectPatternResult::isMinOrMax(SPF) &&
1678 match(NotA, m_Not(m_Value(A))) && (NotA == LHS || NotA == RHS)) {
1679 if (NotA == LHS)
1680 std::swap(LHS, RHS);
1681 // LHS is now O above and expected to have at least 2 uses (the min/max)
1682 // NotA is epected to have 2 uses from the min/max and 1 from the sub.
1683 if (IsFreeToInvert(LHS, !LHS->hasNUsesOrMore(3)) &&
1684 !NotA->hasNUsesOrMore(4)) {
1685 // Note: We don't generate the inverse max/min, just create the not of
1686 // it and let other folds do the rest.
1687 Value *Not = Builder.CreateNot(MinMax);
1688 if (NotA == Op0)
1689 return BinaryOperator::CreateSub(Not, A);
1690 else
1691 return BinaryOperator::CreateSub(A, Not);
1696 // Optimize pointer differences into the same array into a size. Consider:
1697 // &A[10] - &A[0]: we should compile this to "10".
1698 Value *LHSOp, *RHSOp;
1699 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
1700 match(Op1, m_PtrToInt(m_Value(RHSOp))))
1701 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1702 return replaceInstUsesWith(I, Res);
1704 // trunc(p)-trunc(q) -> trunc(p-q)
1705 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
1706 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
1707 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1708 return replaceInstUsesWith(I, Res);
1710 // Canonicalize a shifty way to code absolute value to the common pattern.
1711 // There are 2 potential commuted variants.
1712 // We're relying on the fact that we only do this transform when the shift has
1713 // exactly 2 uses and the xor has exactly 1 use (otherwise, we might increase
1714 // instructions).
1715 Value *A;
1716 const APInt *ShAmt;
1717 Type *Ty = I.getType();
1718 if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
1719 Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
1720 match(Op0, m_OneUse(m_c_Xor(m_Specific(A), m_Specific(Op1))))) {
1721 // B = ashr i32 A, 31 ; smear the sign bit
1722 // sub (xor A, B), B ; flip bits if negative and subtract -1 (add 1)
1723 // --> (A < 0) ? -A : A
1724 Value *Cmp = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(Ty));
1725 // Copy the nuw/nsw flags from the sub to the negate.
1726 Value *Neg = Builder.CreateNeg(A, "", I.hasNoUnsignedWrap(),
1727 I.hasNoSignedWrap());
1728 return SelectInst::Create(Cmp, Neg, A);
1731 if (Instruction *Ext = narrowMathIfNoOverflow(I))
1732 return Ext;
1734 bool Changed = false;
1735 if (!I.hasNoSignedWrap() && willNotOverflowSignedSub(Op0, Op1, I)) {
1736 Changed = true;
1737 I.setHasNoSignedWrap(true);
1739 if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedSub(Op0, Op1, I)) {
1740 Changed = true;
1741 I.setHasNoUnsignedWrap(true);
1744 return Changed ? &I : nullptr;
1747 Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
1748 if (Value *V = SimplifyFSubInst(I.getOperand(0), I.getOperand(1),
1749 I.getFastMathFlags(),
1750 SQ.getWithInstruction(&I)))
1751 return replaceInstUsesWith(I, V);
1753 if (Instruction *X = foldVectorBinop(I))
1754 return X;
1756 // Subtraction from -0.0 is the canonical form of fneg.
1757 // fsub nsz 0, X ==> fsub nsz -0.0, X
1758 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1759 if (I.hasNoSignedZeros() && match(Op0, m_PosZeroFP()))
1760 return BinaryOperator::CreateFNegFMF(Op1, &I);
1762 Value *X, *Y;
1763 Constant *C;
1765 // Fold negation into constant operand. This is limited with one-use because
1766 // fneg is assumed better for analysis and cheaper in codegen than fmul/fdiv.
1767 // -(X * C) --> X * (-C)
1768 if (match(&I, m_FNeg(m_OneUse(m_FMul(m_Value(X), m_Constant(C))))))
1769 return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
1770 // -(X / C) --> X / (-C)
1771 if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Value(X), m_Constant(C))))))
1772 return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
1773 // -(C / X) --> (-C) / X
1774 if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Constant(C), m_Value(X))))))
1775 return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
1777 // If Op0 is not -0.0 or we can ignore -0.0: Z - (X - Y) --> Z + (Y - X)
1778 // Canonicalize to fadd to make analysis easier.
1779 // This can also help codegen because fadd is commutative.
1780 // Note that if this fsub was really an fneg, the fadd with -0.0 will get
1781 // killed later. We still limit that particular transform with 'hasOneUse'
1782 // because an fneg is assumed better/cheaper than a generic fsub.
1783 if (I.hasNoSignedZeros() || CannotBeNegativeZero(Op0, SQ.TLI)) {
1784 if (match(Op1, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) {
1785 Value *NewSub = Builder.CreateFSubFMF(Y, X, &I);
1786 return BinaryOperator::CreateFAddFMF(Op0, NewSub, &I);
1790 if (isa<Constant>(Op0))
1791 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1792 if (Instruction *NV = FoldOpIntoSelect(I, SI))
1793 return NV;
1795 // X - C --> X + (-C)
1796 // But don't transform constant expressions because there's an inverse fold
1797 // for X + (-Y) --> X - Y.
1798 if (match(Op1, m_Constant(C)) && !isa<ConstantExpr>(Op1))
1799 return BinaryOperator::CreateFAddFMF(Op0, ConstantExpr::getFNeg(C), &I);
1801 // X - (-Y) --> X + Y
1802 if (match(Op1, m_FNeg(m_Value(Y))))
1803 return BinaryOperator::CreateFAddFMF(Op0, Y, &I);
1805 // Similar to above, but look through a cast of the negated value:
1806 // X - (fptrunc(-Y)) --> X + fptrunc(Y)
1807 Type *Ty = I.getType();
1808 if (match(Op1, m_OneUse(m_FPTrunc(m_FNeg(m_Value(Y))))))
1809 return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPTrunc(Y, Ty), &I);
1811 // X - (fpext(-Y)) --> X + fpext(Y)
1812 if (match(Op1, m_OneUse(m_FPExt(m_FNeg(m_Value(Y))))))
1813 return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPExt(Y, Ty), &I);
1815 // Handle special cases for FSub with selects feeding the operation
1816 if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
1817 return replaceInstUsesWith(I, V);
1819 if (I.hasAllowReassoc() && I.hasNoSignedZeros()) {
1820 // (Y - X) - Y --> -X
1821 if (match(Op0, m_FSub(m_Specific(Op1), m_Value(X))))
1822 return BinaryOperator::CreateFNegFMF(X, &I);
1824 // Y - (X + Y) --> -X
1825 // Y - (Y + X) --> -X
1826 if (match(Op1, m_c_FAdd(m_Specific(Op0), m_Value(X))))
1827 return BinaryOperator::CreateFNegFMF(X, &I);
1829 // (X * C) - X --> X * (C - 1.0)
1830 if (match(Op0, m_FMul(m_Specific(Op1), m_Constant(C)))) {
1831 Constant *CSubOne = ConstantExpr::getFSub(C, ConstantFP::get(Ty, 1.0));
1832 return BinaryOperator::CreateFMulFMF(Op1, CSubOne, &I);
1834 // X - (X * C) --> X * (1.0 - C)
1835 if (match(Op1, m_FMul(m_Specific(Op0), m_Constant(C)))) {
1836 Constant *OneSubC = ConstantExpr::getFSub(ConstantFP::get(Ty, 1.0), C);
1837 return BinaryOperator::CreateFMulFMF(Op0, OneSubC, &I);
1840 if (Instruction *F = factorizeFAddFSub(I, Builder))
1841 return F;
1843 // TODO: This performs reassociative folds for FP ops. Some fraction of the
1844 // functionality has been subsumed by simple pattern matching here and in
1845 // InstSimplify. We should let a dedicated reassociation pass handle more
1846 // complex pattern matching and remove this from InstCombine.
1847 if (Value *V = FAddCombine(Builder).simplify(&I))
1848 return replaceInstUsesWith(I, V);
1851 return nullptr;