Revert r131155 for now. It makes VMCore depend on Analysis and Transforms
[llvm/stm8.git] / lib / Transforms / InstCombine / InstCombineAddSub.cpp
blobc36a9552e7a3aed6a339c5ffaf06ef8dfa01279f
1 //===- InstCombineAddSub.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 add, fadd, sub, and fsub.
12 //===----------------------------------------------------------------------===//
14 #include "InstCombine.h"
15 #include "llvm/Analysis/InstructionSimplify.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Support/GetElementPtrTypeIterator.h"
18 #include "llvm/Support/PatternMatch.h"
19 using namespace llvm;
20 using namespace PatternMatch;
22 /// AddOne - Add one to a ConstantInt.
23 static Constant *AddOne(Constant *C) {
24 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
26 /// SubOne - Subtract one from a ConstantInt.
27 static Constant *SubOne(ConstantInt *C) {
28 return ConstantInt::get(C->getContext(), C->getValue()-1);
32 // dyn_castFoldableMul - If this value is a multiply that can be folded into
33 // other computations (because it has a constant operand), return the
34 // non-constant operand of the multiply, and set CST to point to the multiplier.
35 // Otherwise, return null.
37 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
38 if (!V->hasOneUse() || !V->getType()->isIntegerTy())
39 return 0;
41 Instruction *I = dyn_cast<Instruction>(V);
42 if (I == 0) return 0;
44 if (I->getOpcode() == Instruction::Mul)
45 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
46 return I->getOperand(0);
47 if (I->getOpcode() == Instruction::Shl)
48 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
49 // The multiplier is really 1 << CST.
50 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
51 uint32_t CSTVal = CST->getLimitedValue(BitWidth);
52 CST = ConstantInt::get(V->getType()->getContext(),
53 APInt(BitWidth, 1).shl(CSTVal));
54 return I->getOperand(0);
56 return 0;
60 /// WillNotOverflowSignedAdd - Return true if we can prove that:
61 /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS))
62 /// This basically requires proving that the add in the original type would not
63 /// overflow to change the sign bit or have a carry out.
64 bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
65 // There are different heuristics we can use for this. Here are some simple
66 // ones.
68 // Add has the property that adding any two 2's complement numbers can only
69 // have one carry bit which can change a sign. As such, if LHS and RHS each
70 // have at least two sign bits, we know that the addition of the two values
71 // will sign extend fine.
72 if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
73 return true;
76 // If one of the operands only has one non-zero bit, and if the other operand
77 // has a known-zero bit in a more significant place than it (not including the
78 // sign bit) the ripple may go up to and fill the zero, but won't change the
79 // sign. For example, (X & ~4) + 1.
81 // TODO: Implement.
83 return false;
86 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
87 bool Changed = SimplifyAssociativeOrCommutative(I);
88 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
90 if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
91 I.hasNoUnsignedWrap(), TD))
92 return ReplaceInstUsesWith(I, V);
94 // (A*B)+(A*C) -> A*(B+C) etc
95 if (Value *V = SimplifyUsingDistributiveLaws(I))
96 return ReplaceInstUsesWith(I, V);
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
99 // X + (signbit) --> X ^ signbit
100 const APInt &Val = CI->getValue();
101 if (Val.isSignBit())
102 return BinaryOperator::CreateXor(LHS, RHS);
104 // See if SimplifyDemandedBits can simplify this. This handles stuff like
105 // (X & 254)+1 -> (X&254)|1
106 if (SimplifyDemandedInstructionBits(I))
107 return &I;
109 // zext(bool) + C -> bool ? C + 1 : C
110 if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
111 if (ZI->getSrcTy()->isIntegerTy(1))
112 return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
114 Value *XorLHS = 0; ConstantInt *XorRHS = 0;
115 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
116 uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
117 const APInt &RHSVal = CI->getValue();
118 unsigned ExtendAmt = 0;
119 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
120 // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
121 if (XorRHS->getValue() == -RHSVal) {
122 if (RHSVal.isPowerOf2())
123 ExtendAmt = TySizeBits - RHSVal.logBase2() - 1;
124 else if (XorRHS->getValue().isPowerOf2())
125 ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1;
128 if (ExtendAmt) {
129 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
130 if (!MaskedValueIsZero(XorLHS, Mask))
131 ExtendAmt = 0;
134 if (ExtendAmt) {
135 Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt);
136 Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext");
137 return BinaryOperator::CreateAShr(NewShl, ShAmt);
142 if (isa<Constant>(RHS) && isa<PHINode>(LHS))
143 if (Instruction *NV = FoldOpIntoPhi(I))
144 return NV;
146 if (I.getType()->isIntegerTy(1))
147 return BinaryOperator::CreateXor(LHS, RHS);
149 // X + X --> X << 1
150 if (LHS == RHS) {
151 BinaryOperator *New =
152 BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1));
153 New->setHasNoSignedWrap(I.hasNoSignedWrap());
154 New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
155 return New;
158 // -A + B --> B - A
159 // -A + -B --> -(A + B)
160 if (Value *LHSV = dyn_castNegVal(LHS)) {
161 if (Value *RHSV = dyn_castNegVal(RHS)) {
162 Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
163 return BinaryOperator::CreateNeg(NewAdd);
166 return BinaryOperator::CreateSub(RHS, LHSV);
169 // A + -B --> A - B
170 if (!isa<Constant>(RHS))
171 if (Value *V = dyn_castNegVal(RHS))
172 return BinaryOperator::CreateSub(LHS, V);
175 ConstantInt *C2;
176 if (Value *X = dyn_castFoldableMul(LHS, C2)) {
177 if (X == RHS) // X*C + X --> X * (C+1)
178 return BinaryOperator::CreateMul(RHS, AddOne(C2));
180 // X*C1 + X*C2 --> X * (C1+C2)
181 ConstantInt *C1;
182 if (X == dyn_castFoldableMul(RHS, C1))
183 return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
186 // X + X*C --> X * (C+1)
187 if (dyn_castFoldableMul(RHS, C2) == LHS)
188 return BinaryOperator::CreateMul(LHS, AddOne(C2));
190 // A+B --> A|B iff A and B have no bits set in common.
191 if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
192 APInt Mask = APInt::getAllOnesValue(IT->getBitWidth());
193 APInt LHSKnownOne(IT->getBitWidth(), 0);
194 APInt LHSKnownZero(IT->getBitWidth(), 0);
195 ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
196 if (LHSKnownZero != 0) {
197 APInt RHSKnownOne(IT->getBitWidth(), 0);
198 APInt RHSKnownZero(IT->getBitWidth(), 0);
199 ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
201 // No bits in common -> bitwise or.
202 if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
203 return BinaryOperator::CreateOr(LHS, RHS);
207 // W*X + Y*Z --> W * (X+Z) iff W == Y
209 Value *W, *X, *Y, *Z;
210 if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
211 match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
212 if (W != Y) {
213 if (W == Z) {
214 std::swap(Y, Z);
215 } else if (Y == X) {
216 std::swap(W, X);
217 } else if (X == Z) {
218 std::swap(Y, Z);
219 std::swap(W, X);
223 if (W == Y) {
224 Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName());
225 return BinaryOperator::CreateMul(W, NewAdd);
230 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
231 Value *X = 0;
232 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
233 return BinaryOperator::CreateSub(SubOne(CRHS), X);
235 // (X & FF00) + xx00 -> (X+xx00) & FF00
236 if (LHS->hasOneUse() &&
237 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
238 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
239 // See if all bits from the first bit set in the Add RHS up are included
240 // in the mask. First, get the rightmost bit.
241 const APInt &AddRHSV = CRHS->getValue();
243 // Form a mask of all bits from the lowest bit added through the top.
244 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
246 // See if the and mask includes all of these bits.
247 APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
249 if (AddRHSHighBits == AddRHSHighBitsAnd) {
250 // Okay, the xform is safe. Insert the new add pronto.
251 Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
252 return BinaryOperator::CreateAnd(NewAdd, C2);
256 // Try to fold constant add into select arguments.
257 if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
258 if (Instruction *R = FoldOpIntoSelect(I, SI))
259 return R;
262 // add (select X 0 (sub n A)) A --> select X A n
264 SelectInst *SI = dyn_cast<SelectInst>(LHS);
265 Value *A = RHS;
266 if (!SI) {
267 SI = dyn_cast<SelectInst>(RHS);
268 A = LHS;
270 if (SI && SI->hasOneUse()) {
271 Value *TV = SI->getTrueValue();
272 Value *FV = SI->getFalseValue();
273 Value *N;
275 // Can we fold the add into the argument of the select?
276 // We check both true and false select arguments for a matching subtract.
277 if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
278 // Fold the add into the true select value.
279 return SelectInst::Create(SI->getCondition(), N, A);
281 if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
282 // Fold the add into the false select value.
283 return SelectInst::Create(SI->getCondition(), A, N);
287 // Check for (add (sext x), y), see if we can merge this into an
288 // integer add followed by a sext.
289 if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
290 // (add (sext x), cst) --> (sext (add x, cst'))
291 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
292 Constant *CI =
293 ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
294 if (LHSConv->hasOneUse() &&
295 ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
296 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
297 // Insert the new, smaller add.
298 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
299 CI, "addconv");
300 return new SExtInst(NewAdd, I.getType());
304 // (add (sext x), (sext y)) --> (sext (add int x, y))
305 if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
306 // Only do this if x/y have the same type, if at last one of them has a
307 // single use (so we don't increase the number of sexts), and if the
308 // integer add will not overflow.
309 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
310 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
311 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
312 RHSConv->getOperand(0))) {
313 // Insert the new integer add.
314 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
315 RHSConv->getOperand(0), "addconv");
316 return new SExtInst(NewAdd, I.getType());
321 return Changed ? &I : 0;
324 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
325 bool Changed = SimplifyAssociativeOrCommutative(I);
326 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
328 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
329 // X + 0 --> X
330 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
331 if (CFP->isExactlyValue(ConstantFP::getNegativeZero
332 (I.getType())->getValueAPF()))
333 return ReplaceInstUsesWith(I, LHS);
336 if (isa<PHINode>(LHS))
337 if (Instruction *NV = FoldOpIntoPhi(I))
338 return NV;
341 // -A + B --> B - A
342 // -A + -B --> -(A + B)
343 if (Value *LHSV = dyn_castFNegVal(LHS))
344 return BinaryOperator::CreateFSub(RHS, LHSV);
346 // A + -B --> A - B
347 if (!isa<Constant>(RHS))
348 if (Value *V = dyn_castFNegVal(RHS))
349 return BinaryOperator::CreateFSub(LHS, V);
351 // Check for X+0.0. Simplify it to X if we know X is not -0.0.
352 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
353 if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
354 return ReplaceInstUsesWith(I, LHS);
356 // Check for (fadd double (sitofp x), y), see if we can merge this into an
357 // integer add followed by a promotion.
358 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
359 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
360 // ... if the constant fits in the integer value. This is useful for things
361 // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
362 // requires a constant pool load, and generally allows the add to be better
363 // instcombined.
364 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
365 Constant *CI =
366 ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
367 if (LHSConv->hasOneUse() &&
368 ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
369 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
370 // Insert the new integer add.
371 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
372 CI, "addconv");
373 return new SIToFPInst(NewAdd, I.getType());
377 // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
378 if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
379 // Only do this if x/y have the same type, if at last one of them has a
380 // single use (so we don't increase the number of int->fp conversions),
381 // and if the integer add will not overflow.
382 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
383 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
384 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
385 RHSConv->getOperand(0))) {
386 // Insert the new integer add.
387 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
388 RHSConv->getOperand(0),"addconv");
389 return new SIToFPInst(NewAdd, I.getType());
394 return Changed ? &I : 0;
398 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
399 /// code necessary to compute the offset from the base pointer (without adding
400 /// in the base pointer). Return the result as a signed integer of intptr size.
401 Value *InstCombiner::EmitGEPOffset(User *GEP) {
402 TargetData &TD = *getTargetData();
403 gep_type_iterator GTI = gep_type_begin(GEP);
404 const Type *IntPtrTy = TD.getIntPtrType(GEP->getContext());
405 Value *Result = Constant::getNullValue(IntPtrTy);
407 // If the GEP is inbounds, we know that none of the addressing operations will
408 // overflow in an unsigned sense.
409 bool isInBounds = cast<GEPOperator>(GEP)->isInBounds();
411 // Build a mask for high order bits.
412 unsigned IntPtrWidth = TD.getPointerSizeInBits();
413 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
415 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
416 ++i, ++GTI) {
417 Value *Op = *i;
418 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
419 if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
420 if (OpC->isZero()) continue;
422 // Handle a struct index, which adds its field offset to the pointer.
423 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
424 Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
426 if (Size)
427 Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
428 GEP->getName()+".offs");
429 continue;
432 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
433 Constant *OC =
434 ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
435 Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
436 // Emit an add instruction.
437 Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
438 continue;
440 // Convert to correct type.
441 if (Op->getType() != IntPtrTy)
442 Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
443 if (Size != 1) {
444 // We'll let instcombine(mul) convert this to a shl if possible.
445 Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
446 GEP->getName()+".idx", isInBounds /*NUW*/);
449 // Emit an add instruction.
450 Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
452 return Result;
458 /// Optimize pointer differences into the same array into a size. Consider:
459 /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer
460 /// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
462 Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
463 const Type *Ty) {
464 assert(TD && "Must have target data info for this");
466 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
467 // this.
468 bool Swapped = false;
469 GetElementPtrInst *GEP = 0;
470 ConstantExpr *CstGEP = 0;
472 // TODO: Could also optimize &A[i] - &A[j] -> "i-j", and "&A.foo[i] - &A.foo".
473 // For now we require one side to be the base pointer "A" or a constant
474 // expression derived from it.
475 if (GetElementPtrInst *LHSGEP = dyn_cast<GetElementPtrInst>(LHS)) {
476 // (gep X, ...) - X
477 if (LHSGEP->getOperand(0) == RHS) {
478 GEP = LHSGEP;
479 Swapped = false;
480 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(RHS)) {
481 // (gep X, ...) - (ce_gep X, ...)
482 if (CE->getOpcode() == Instruction::GetElementPtr &&
483 LHSGEP->getOperand(0) == CE->getOperand(0)) {
484 CstGEP = CE;
485 GEP = LHSGEP;
486 Swapped = false;
491 if (GetElementPtrInst *RHSGEP = dyn_cast<GetElementPtrInst>(RHS)) {
492 // X - (gep X, ...)
493 if (RHSGEP->getOperand(0) == LHS) {
494 GEP = RHSGEP;
495 Swapped = true;
496 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(LHS)) {
497 // (ce_gep X, ...) - (gep X, ...)
498 if (CE->getOpcode() == Instruction::GetElementPtr &&
499 RHSGEP->getOperand(0) == CE->getOperand(0)) {
500 CstGEP = CE;
501 GEP = RHSGEP;
502 Swapped = true;
507 if (GEP == 0)
508 return 0;
510 // Emit the offset of the GEP and an intptr_t.
511 Value *Result = EmitGEPOffset(GEP);
513 // If we had a constant expression GEP on the other side offsetting the
514 // pointer, subtract it from the offset we have.
515 if (CstGEP) {
516 Value *CstOffset = EmitGEPOffset(CstGEP);
517 Result = Builder->CreateSub(Result, CstOffset);
521 // If we have p - gep(p, ...) then we have to negate the result.
522 if (Swapped)
523 Result = Builder->CreateNeg(Result, "diff.neg");
525 return Builder->CreateIntCast(Result, Ty, true);
529 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
530 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
532 if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(),
533 I.hasNoUnsignedWrap(), TD))
534 return ReplaceInstUsesWith(I, V);
536 // (A*B)-(A*C) -> A*(B-C) etc
537 if (Value *V = SimplifyUsingDistributiveLaws(I))
538 return ReplaceInstUsesWith(I, V);
540 // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW.
541 if (Value *V = dyn_castNegVal(Op1)) {
542 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
543 Res->setHasNoSignedWrap(I.hasNoSignedWrap());
544 Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
545 return Res;
548 if (I.getType()->isIntegerTy(1))
549 return BinaryOperator::CreateXor(Op0, Op1);
551 // Replace (-1 - A) with (~A).
552 if (match(Op0, m_AllOnes()))
553 return BinaryOperator::CreateNot(Op1);
555 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
556 // C - ~X == X + (1+C)
557 Value *X = 0;
558 if (match(Op1, m_Not(m_Value(X))))
559 return BinaryOperator::CreateAdd(X, AddOne(C));
561 // -(X >>u 31) -> (X >>s 31)
562 // -(X >>s 31) -> (X >>u 31)
563 if (C->isZero()) {
564 Value *X; ConstantInt *CI;
565 if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) &&
566 // Verify we are shifting out everything but the sign bit.
567 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
568 return BinaryOperator::CreateAShr(X, CI);
570 if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) &&
571 // Verify we are shifting out everything but the sign bit.
572 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
573 return BinaryOperator::CreateLShr(X, CI);
576 // Try to fold constant sub into select arguments.
577 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
578 if (Instruction *R = FoldOpIntoSelect(I, SI))
579 return R;
581 // C - zext(bool) -> bool ? C - 1 : C
582 if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
583 if (ZI->getSrcTy()->isIntegerTy(1))
584 return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
586 // C-(X+C2) --> (C-C2)-X
587 ConstantInt *C2;
588 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(C2))))
589 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
593 { Value *Y;
594 // X-(X+Y) == -Y X-(Y+X) == -Y
595 if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) ||
596 match(Op1, m_Add(m_Value(Y), m_Specific(Op0))))
597 return BinaryOperator::CreateNeg(Y);
599 // (X-Y)-X == -Y
600 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
601 return BinaryOperator::CreateNeg(Y);
604 if (Op1->hasOneUse()) {
605 Value *X = 0, *Y = 0, *Z = 0;
606 Constant *C = 0;
607 ConstantInt *CI = 0;
609 // (X - (Y - Z)) --> (X + (Z - Y)).
610 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
611 return BinaryOperator::CreateAdd(Op0,
612 Builder->CreateSub(Z, Y, Op1->getName()));
614 // (X - (X & Y)) --> (X & ~Y)
616 if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) ||
617 match(Op1, m_And(m_Specific(Op0), m_Value(Y))))
618 return BinaryOperator::CreateAnd(Op0,
619 Builder->CreateNot(Y, Y->getName() + ".not"));
621 // 0 - (X sdiv C) -> (X sdiv -C)
622 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) &&
623 match(Op0, m_Zero()))
624 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
626 // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
627 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
628 if (Value *XNeg = dyn_castNegVal(X))
629 return BinaryOperator::CreateShl(XNeg, Y);
631 // X - X*C --> X * (1-C)
632 if (match(Op1, m_Mul(m_Specific(Op0), m_ConstantInt(CI)))) {
633 Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(),1), CI);
634 return BinaryOperator::CreateMul(Op0, CP1);
637 // X - X<<C --> X * (1-(1<<C))
638 if (match(Op1, m_Shl(m_Specific(Op0), m_ConstantInt(CI)))) {
639 Constant *One = ConstantInt::get(I.getType(), 1);
640 C = ConstantExpr::getSub(One, ConstantExpr::getShl(One, CI));
641 return BinaryOperator::CreateMul(Op0, C);
644 // X - A*-B -> X + A*B
645 // X - -A*B -> X + A*B
646 Value *A, *B;
647 if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
648 match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B))))
649 return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B));
651 // X - A*CI -> X + A*-CI
652 // X - CI*A -> X + A*-CI
653 if (match(Op1, m_Mul(m_Value(A), m_ConstantInt(CI))) ||
654 match(Op1, m_Mul(m_ConstantInt(CI), m_Value(A)))) {
655 Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI));
656 return BinaryOperator::CreateAdd(Op0, NewMul);
660 ConstantInt *C1;
661 if (Value *X = dyn_castFoldableMul(Op0, C1)) {
662 if (X == Op1) // X*C - X --> X * (C-1)
663 return BinaryOperator::CreateMul(Op1, SubOne(C1));
665 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
666 if (X == dyn_castFoldableMul(Op1, C2))
667 return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
670 // Optimize pointer differences into the same array into a size. Consider:
671 // &A[10] - &A[0]: we should compile this to "10".
672 if (TD) {
673 Value *LHSOp, *RHSOp;
674 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
675 match(Op1, m_PtrToInt(m_Value(RHSOp))))
676 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
677 return ReplaceInstUsesWith(I, Res);
679 // trunc(p)-trunc(q) -> trunc(p-q)
680 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
681 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
682 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
683 return ReplaceInstUsesWith(I, Res);
686 return 0;
689 Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
690 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
692 // If this is a 'B = x-(-A)', change to B = x+A...
693 if (Value *V = dyn_castFNegVal(Op1))
694 return BinaryOperator::CreateFAdd(Op0, V);
696 return 0;