blob: c88ceede1f25fc1ba77965c685815c6a3f5f463f [file] [log] [blame]
Chris Lattner53a19b72010-01-05 07:18:46 +00001//===- 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.
11//
12//===----------------------------------------------------------------------===//
13
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"
19using namespace llvm;
20using namespace PatternMatch;
21
22/// AddOne - Add one to a ConstantInt.
23static Constant *AddOne(Constant *C) {
24 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
25}
26/// SubOne - Subtract one from a ConstantInt.
27static Constant *SubOne(ConstantInt *C) {
28 return ConstantInt::get(C->getContext(), C->getValue()-1);
29}
30
31
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.
36//
37static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000038 if (!V->hasOneUse() || !V->getType()->isIntegerTy())
Chris Lattner3168c7d2010-01-05 20:56:24 +000039 return 0;
40
41 Instruction *I = dyn_cast<Instruction>(V);
42 if (I == 0) return 0;
43
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);
Chris Lattner53a19b72010-01-05 07:18:46 +000055 }
56 return 0;
57}
58
59
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.
64bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
65 // There are different heuristics we can use for this. Here are some simple
66 // ones.
67
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;
74
75
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.
80
81 // TODO: Implement.
82
83 return false;
84}
85
86Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Duncan Sands096aa792010-11-13 15:10:37 +000087 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner53a19b72010-01-05 07:18:46 +000088 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
89
90 if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
91 I.hasNoUnsignedWrap(), TD))
92 return ReplaceInstUsesWith(I, V);
93
Duncan Sands37bf92b2010-12-22 13:36:08 +000094 // (A*B)+(A*C) -> A*(B+C) etc
95 if (Value *V = SimplifyUsingDistributiveLaws(I))
96 return ReplaceInstUsesWith(I, V);
97
Chris Lattnerb9b90442011-02-10 05:14:58 +000098 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);
Chris Lattner53a19b72010-01-05 07:18:46 +0000103
Chris Lattnerb9b90442011-02-10 05:14:58 +0000104 // See if SimplifyDemandedBits can simplify this. This handles stuff like
105 // (X & 254)+1 -> (X&254)|1
106 if (SimplifyDemandedInstructionBits(I))
107 return &I;
108
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);
113
114 Value *XorLHS = 0; ConstantInt *XorRHS = 0;
115 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000116 uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
Chris Lattnerb9b90442011-02-10 05:14:58 +0000117 const APInt &RHSVal = CI->getValue();
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000118 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;
Chris Lattner53a19b72010-01-05 07:18:46 +0000126 }
Chris Lattnerb9b90442011-02-10 05:14:58 +0000127
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000128 if (ExtendAmt) {
129 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
130 if (!MaskedValueIsZero(XorLHS, Mask))
131 ExtendAmt = 0;
132 }
Chris Lattnerb9b90442011-02-10 05:14:58 +0000133
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000134 if (ExtendAmt) {
135 Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt);
136 Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext");
137 return BinaryOperator::CreateAShr(NewShl, ShAmt);
Chris Lattner53a19b72010-01-05 07:18:46 +0000138 }
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000139
140 // If this is a xor that was canonicalized from a sub, turn it back into
141 // a sub and fuse this add with it.
142 if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
143 IntegerType *IT = cast<IntegerType>(I.getType());
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000144 APInt LHSKnownOne(IT->getBitWidth(), 0);
145 APInt LHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000146 ComputeMaskedBits(XorLHS, LHSKnownZero, LHSKnownOne);
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000147 if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue())
148 return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
149 XorLHS);
150 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000151 }
152 }
153
Chris Lattnerb9b90442011-02-10 05:14:58 +0000154 if (isa<Constant>(RHS) && isa<PHINode>(LHS))
155 if (Instruction *NV = FoldOpIntoPhi(I))
156 return NV;
157
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000158 if (I.getType()->isIntegerTy(1))
Chris Lattner53a19b72010-01-05 07:18:46 +0000159 return BinaryOperator::CreateXor(LHS, RHS);
160
Chris Lattnerb9b90442011-02-10 05:14:58 +0000161 // X + X --> X << 1
Chris Lattnerbd9f6bf2011-02-17 20:55:29 +0000162 if (LHS == RHS) {
Chris Lattner41429e32011-02-17 02:23:02 +0000163 BinaryOperator *New =
164 BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1));
165 New->setHasNoSignedWrap(I.hasNoSignedWrap());
166 New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
167 return New;
168 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000169
170 // -A + B --> B - A
171 // -A + -B --> -(A + B)
172 if (Value *LHSV = dyn_castNegVal(LHS)) {
Chris Lattnerb9b90442011-02-10 05:14:58 +0000173 if (Value *RHSV = dyn_castNegVal(RHS)) {
174 Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
175 return BinaryOperator::CreateNeg(NewAdd);
Chris Lattner53a19b72010-01-05 07:18:46 +0000176 }
177
178 return BinaryOperator::CreateSub(RHS, LHSV);
179 }
180
181 // A + -B --> A - B
182 if (!isa<Constant>(RHS))
183 if (Value *V = dyn_castNegVal(RHS))
184 return BinaryOperator::CreateSub(LHS, V);
185
186
187 ConstantInt *C2;
188 if (Value *X = dyn_castFoldableMul(LHS, C2)) {
189 if (X == RHS) // X*C + X --> X * (C+1)
190 return BinaryOperator::CreateMul(RHS, AddOne(C2));
191
192 // X*C1 + X*C2 --> X * (C1+C2)
193 ConstantInt *C1;
194 if (X == dyn_castFoldableMul(RHS, C1))
195 return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
196 }
197
198 // X + X*C --> X * (C+1)
199 if (dyn_castFoldableMul(RHS, C2) == LHS)
200 return BinaryOperator::CreateMul(LHS, AddOne(C2));
201
Chris Lattner53a19b72010-01-05 07:18:46 +0000202 // A+B --> A|B iff A and B have no bits set in common.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000203 if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000204 APInt LHSKnownOne(IT->getBitWidth(), 0);
205 APInt LHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000206 ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
Chris Lattner53a19b72010-01-05 07:18:46 +0000207 if (LHSKnownZero != 0) {
208 APInt RHSKnownOne(IT->getBitWidth(), 0);
209 APInt RHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000210 ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
Chris Lattner53a19b72010-01-05 07:18:46 +0000211
212 // No bits in common -> bitwise or.
213 if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
214 return BinaryOperator::CreateOr(LHS, RHS);
215 }
216 }
217
218 // W*X + Y*Z --> W * (X+Z) iff W == Y
Chris Lattnerb9b90442011-02-10 05:14:58 +0000219 {
Chris Lattner53a19b72010-01-05 07:18:46 +0000220 Value *W, *X, *Y, *Z;
221 if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
222 match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
223 if (W != Y) {
224 if (W == Z) {
225 std::swap(Y, Z);
226 } else if (Y == X) {
227 std::swap(W, X);
228 } else if (X == Z) {
229 std::swap(Y, Z);
230 std::swap(W, X);
231 }
232 }
233
234 if (W == Y) {
235 Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName());
236 return BinaryOperator::CreateMul(W, NewAdd);
237 }
238 }
239 }
240
241 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
242 Value *X = 0;
243 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
244 return BinaryOperator::CreateSub(SubOne(CRHS), X);
245
246 // (X & FF00) + xx00 -> (X+xx00) & FF00
247 if (LHS->hasOneUse() &&
Chris Lattnerb9b90442011-02-10 05:14:58 +0000248 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
249 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
250 // See if all bits from the first bit set in the Add RHS up are included
251 // in the mask. First, get the rightmost bit.
252 const APInt &AddRHSV = CRHS->getValue();
253
254 // Form a mask of all bits from the lowest bit added through the top.
255 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
Chris Lattner53a19b72010-01-05 07:18:46 +0000256
Chris Lattnerb9b90442011-02-10 05:14:58 +0000257 // See if the and mask includes all of these bits.
258 APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
Chris Lattner53a19b72010-01-05 07:18:46 +0000259
Chris Lattnerb9b90442011-02-10 05:14:58 +0000260 if (AddRHSHighBits == AddRHSHighBitsAnd) {
261 // Okay, the xform is safe. Insert the new add pronto.
262 Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
263 return BinaryOperator::CreateAnd(NewAdd, C2);
Chris Lattner53a19b72010-01-05 07:18:46 +0000264 }
265 }
266
267 // Try to fold constant add into select arguments.
268 if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
269 if (Instruction *R = FoldOpIntoSelect(I, SI))
270 return R;
271 }
272
273 // add (select X 0 (sub n A)) A --> select X A n
274 {
275 SelectInst *SI = dyn_cast<SelectInst>(LHS);
276 Value *A = RHS;
277 if (!SI) {
278 SI = dyn_cast<SelectInst>(RHS);
279 A = LHS;
280 }
281 if (SI && SI->hasOneUse()) {
282 Value *TV = SI->getTrueValue();
283 Value *FV = SI->getFalseValue();
284 Value *N;
285
286 // Can we fold the add into the argument of the select?
287 // We check both true and false select arguments for a matching subtract.
Chris Lattnerb9b90442011-02-10 05:14:58 +0000288 if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
Chris Lattner53a19b72010-01-05 07:18:46 +0000289 // Fold the add into the true select value.
290 return SelectInst::Create(SI->getCondition(), N, A);
Chris Lattnerb9b90442011-02-10 05:14:58 +0000291
292 if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
Chris Lattner53a19b72010-01-05 07:18:46 +0000293 // Fold the add into the false select value.
294 return SelectInst::Create(SI->getCondition(), A, N);
295 }
296 }
297
298 // Check for (add (sext x), y), see if we can merge this into an
299 // integer add followed by a sext.
300 if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
301 // (add (sext x), cst) --> (sext (add x, cst'))
302 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
303 Constant *CI =
304 ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
305 if (LHSConv->hasOneUse() &&
306 ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
307 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
308 // Insert the new, smaller add.
309 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
310 CI, "addconv");
311 return new SExtInst(NewAdd, I.getType());
312 }
313 }
314
315 // (add (sext x), (sext y)) --> (sext (add int x, y))
316 if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
317 // Only do this if x/y have the same type, if at last one of them has a
318 // single use (so we don't increase the number of sexts), and if the
319 // integer add will not overflow.
320 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
321 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
322 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
323 RHSConv->getOperand(0))) {
324 // Insert the new integer add.
325 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
Chris Lattner3168c7d2010-01-05 20:56:24 +0000326 RHSConv->getOperand(0), "addconv");
Chris Lattner53a19b72010-01-05 07:18:46 +0000327 return new SExtInst(NewAdd, I.getType());
328 }
329 }
330 }
331
Chad Rosierc1fc5e42012-04-26 23:29:14 +0000332 // Check for (x & y) + (x ^ y)
333 {
334 Value *A = 0, *B = 0;
335 if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
336 (match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
337 match(LHS, m_And(m_Specific(B), m_Specific(A)))))
338 return BinaryOperator::CreateOr(A, B);
339
340 if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
341 (match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
342 match(RHS, m_And(m_Specific(B), m_Specific(A)))))
343 return BinaryOperator::CreateOr(A, B);
344 }
345
Chris Lattner53a19b72010-01-05 07:18:46 +0000346 return Changed ? &I : 0;
347}
348
349Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
Duncan Sands096aa792010-11-13 15:10:37 +0000350 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner53a19b72010-01-05 07:18:46 +0000351 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
352
353 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
354 // X + 0 --> X
355 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
356 if (CFP->isExactlyValue(ConstantFP::getNegativeZero
357 (I.getType())->getValueAPF()))
358 return ReplaceInstUsesWith(I, LHS);
359 }
360
361 if (isa<PHINode>(LHS))
362 if (Instruction *NV = FoldOpIntoPhi(I))
363 return NV;
364 }
365
366 // -A + B --> B - A
367 // -A + -B --> -(A + B)
368 if (Value *LHSV = dyn_castFNegVal(LHS))
369 return BinaryOperator::CreateFSub(RHS, LHSV);
370
371 // A + -B --> A - B
372 if (!isa<Constant>(RHS))
373 if (Value *V = dyn_castFNegVal(RHS))
374 return BinaryOperator::CreateFSub(LHS, V);
375
376 // Check for X+0.0. Simplify it to X if we know X is not -0.0.
377 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
378 if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
379 return ReplaceInstUsesWith(I, LHS);
380
Dan Gohmana9445e12010-03-02 01:11:08 +0000381 // Check for (fadd double (sitofp x), y), see if we can merge this into an
Chris Lattner53a19b72010-01-05 07:18:46 +0000382 // integer add followed by a promotion.
383 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
Dan Gohmana9445e12010-03-02 01:11:08 +0000384 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
Chris Lattner53a19b72010-01-05 07:18:46 +0000385 // ... if the constant fits in the integer value. This is useful for things
386 // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
387 // requires a constant pool load, and generally allows the add to be better
388 // instcombined.
389 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
390 Constant *CI =
391 ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
392 if (LHSConv->hasOneUse() &&
393 ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
394 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
395 // Insert the new integer add.
396 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
397 CI, "addconv");
398 return new SIToFPInst(NewAdd, I.getType());
399 }
400 }
401
Dan Gohmana9445e12010-03-02 01:11:08 +0000402 // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
Chris Lattner53a19b72010-01-05 07:18:46 +0000403 if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
404 // Only do this if x/y have the same type, if at last one of them has a
405 // single use (so we don't increase the number of int->fp conversions),
406 // and if the integer add will not overflow.
407 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
408 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
409 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
410 RHSConv->getOperand(0))) {
411 // Insert the new integer add.
412 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
413 RHSConv->getOperand(0),"addconv");
414 return new SIToFPInst(NewAdd, I.getType());
415 }
416 }
417 }
418
419 return Changed ? &I : 0;
420}
421
422
Chris Lattner53a19b72010-01-05 07:18:46 +0000423/// Optimize pointer differences into the same array into a size. Consider:
424/// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer
425/// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
426///
427Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000428 Type *Ty) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000429 assert(TD && "Must have target data info for this");
430
431 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
432 // this.
433 bool Swapped = false;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000434 GEPOperator *GEP1 = 0, *GEP2 = 0;
435
Chris Lattner53a19b72010-01-05 07:18:46 +0000436 // For now we require one side to be the base pointer "A" or a constant
Benjamin Kramerd2348632012-02-20 14:34:57 +0000437 // GEP derived from it.
438 if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000439 // (gep X, ...) - X
440 if (LHSGEP->getOperand(0) == RHS) {
Benjamin Kramerd2348632012-02-20 14:34:57 +0000441 GEP1 = LHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000442 Swapped = false;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000443 } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
444 // (gep X, ...) - (gep X, ...)
445 if (LHSGEP->getOperand(0)->stripPointerCasts() ==
446 RHSGEP->getOperand(0)->stripPointerCasts()) {
447 GEP2 = RHSGEP;
448 GEP1 = LHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000449 Swapped = false;
450 }
451 }
452 }
453
Benjamin Kramerd2348632012-02-20 14:34:57 +0000454 if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000455 // X - (gep X, ...)
456 if (RHSGEP->getOperand(0) == LHS) {
Benjamin Kramerd2348632012-02-20 14:34:57 +0000457 GEP1 = RHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000458 Swapped = true;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000459 } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
460 // (gep X, ...) - (gep X, ...)
461 if (RHSGEP->getOperand(0)->stripPointerCasts() ==
462 LHSGEP->getOperand(0)->stripPointerCasts()) {
463 GEP2 = LHSGEP;
464 GEP1 = RHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000465 Swapped = true;
466 }
467 }
468 }
469
Benjamin Kramerd2348632012-02-20 14:34:57 +0000470 // Avoid duplicating the arithmetic if GEP2 has non-constant indices and
471 // multiple users.
472 if (GEP1 == 0 ||
473 (GEP2 != 0 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse()))
Chris Lattner53a19b72010-01-05 07:18:46 +0000474 return 0;
475
476 // Emit the offset of the GEP and an intptr_t.
Benjamin Kramerd2348632012-02-20 14:34:57 +0000477 Value *Result = EmitGEPOffset(GEP1);
Chris Lattner53a19b72010-01-05 07:18:46 +0000478
479 // If we had a constant expression GEP on the other side offsetting the
480 // pointer, subtract it from the offset we have.
Benjamin Kramerd2348632012-02-20 14:34:57 +0000481 if (GEP2) {
482 Value *Offset = EmitGEPOffset(GEP2);
483 Result = Builder->CreateSub(Result, Offset);
Chris Lattner53a19b72010-01-05 07:18:46 +0000484 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000485
486 // If we have p - gep(p, ...) then we have to negate the result.
487 if (Swapped)
488 Result = Builder->CreateNeg(Result, "diff.neg");
489
490 return Builder->CreateIntCast(Result, Ty, true);
491}
492
493
494Instruction *InstCombiner::visitSub(BinaryOperator &I) {
495 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
496
Duncan Sandsfea3b212010-12-15 14:07:39 +0000497 if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(),
498 I.hasNoUnsignedWrap(), TD))
499 return ReplaceInstUsesWith(I, V);
Chris Lattner53a19b72010-01-05 07:18:46 +0000500
Duncan Sands37bf92b2010-12-22 13:36:08 +0000501 // (A*B)-(A*C) -> A*(B-C) etc
502 if (Value *V = SimplifyUsingDistributiveLaws(I))
503 return ReplaceInstUsesWith(I, V);
504
Chris Lattner53a19b72010-01-05 07:18:46 +0000505 // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW.
506 if (Value *V = dyn_castNegVal(Op1)) {
507 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
508 Res->setHasNoSignedWrap(I.hasNoSignedWrap());
509 Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
510 return Res;
511 }
512
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000513 if (I.getType()->isIntegerTy(1))
Chris Lattner53a19b72010-01-05 07:18:46 +0000514 return BinaryOperator::CreateXor(Op0, Op1);
Chris Lattnerb9b90442011-02-10 05:14:58 +0000515
516 // Replace (-1 - A) with (~A).
517 if (match(Op0, m_AllOnes()))
518 return BinaryOperator::CreateNot(Op1);
Chris Lattner53a19b72010-01-05 07:18:46 +0000519
520 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000521 // C - ~X == X + (1+C)
522 Value *X = 0;
523 if (match(Op1, m_Not(m_Value(X))))
524 return BinaryOperator::CreateAdd(X, AddOne(C));
525
526 // -(X >>u 31) -> (X >>s 31)
527 // -(X >>s 31) -> (X >>u 31)
528 if (C->isZero()) {
Chris Lattnerb9b90442011-02-10 05:14:58 +0000529 Value *X; ConstantInt *CI;
530 if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) &&
531 // Verify we are shifting out everything but the sign bit.
532 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
533 return BinaryOperator::CreateAShr(X, CI);
534
535 if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) &&
536 // Verify we are shifting out everything but the sign bit.
537 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
538 return BinaryOperator::CreateLShr(X, CI);
Chris Lattner53a19b72010-01-05 07:18:46 +0000539 }
540
541 // Try to fold constant sub into select arguments.
542 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
543 if (Instruction *R = FoldOpIntoSelect(I, SI))
544 return R;
545
546 // C - zext(bool) -> bool ? C - 1 : C
547 if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
Chris Lattnerb9b90442011-02-10 05:14:58 +0000548 if (ZI->getSrcTy()->isIntegerTy(1))
Chris Lattner53a19b72010-01-05 07:18:46 +0000549 return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
Chris Lattnerb9b90442011-02-10 05:14:58 +0000550
551 // C-(X+C2) --> (C-C2)-X
552 ConstantInt *C2;
553 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(C2))))
554 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
Benjamin Kramer1fdfae02011-12-24 17:31:38 +0000555
556 if (SimplifyDemandedInstructionBits(I))
557 return &I;
Chris Lattner53a19b72010-01-05 07:18:46 +0000558 }
559
Chris Lattnerb9b90442011-02-10 05:14:58 +0000560
561 { Value *Y;
562 // X-(X+Y) == -Y X-(Y+X) == -Y
563 if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) ||
564 match(Op1, m_Add(m_Value(Y), m_Specific(Op0))))
565 return BinaryOperator::CreateNeg(Y);
566
567 // (X-Y)-X == -Y
568 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
569 return BinaryOperator::CreateNeg(Y);
570 }
571
572 if (Op1->hasOneUse()) {
573 Value *X = 0, *Y = 0, *Z = 0;
574 Constant *C = 0;
575 ConstantInt *CI = 0;
576
577 // (X - (Y - Z)) --> (X + (Z - Y)).
578 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
579 return BinaryOperator::CreateAdd(Op0,
580 Builder->CreateSub(Z, Y, Op1->getName()));
581
582 // (X - (X & Y)) --> (X & ~Y)
583 //
584 if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) ||
585 match(Op1, m_And(m_Specific(Op0), m_Value(Y))))
586 return BinaryOperator::CreateAnd(Op0,
587 Builder->CreateNot(Y, Y->getName() + ".not"));
588
589 // 0 - (X sdiv C) -> (X sdiv -C)
590 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) &&
591 match(Op0, m_Zero()))
592 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
593
594 // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
595 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
596 if (Value *XNeg = dyn_castNegVal(X))
597 return BinaryOperator::CreateShl(XNeg, Y);
598
599 // X - X*C --> X * (1-C)
600 if (match(Op1, m_Mul(m_Specific(Op0), m_ConstantInt(CI)))) {
601 Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(),1), CI);
602 return BinaryOperator::CreateMul(Op0, CP1);
Chris Lattner53a19b72010-01-05 07:18:46 +0000603 }
604
Chris Lattnerb9b90442011-02-10 05:14:58 +0000605 // X - X<<C --> X * (1-(1<<C))
606 if (match(Op1, m_Shl(m_Specific(Op0), m_ConstantInt(CI)))) {
607 Constant *One = ConstantInt::get(I.getType(), 1);
608 C = ConstantExpr::getSub(One, ConstantExpr::getShl(One, CI));
609 return BinaryOperator::CreateMul(Op0, C);
610 }
611
612 // X - A*-B -> X + A*B
613 // X - -A*B -> X + A*B
614 Value *A, *B;
615 if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
616 match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B))))
617 return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B));
Chris Lattner67920322011-01-15 05:50:18 +0000618
Chris Lattnerb9b90442011-02-10 05:14:58 +0000619 // X - A*CI -> X + A*-CI
620 // X - CI*A -> X + A*-CI
621 if (match(Op1, m_Mul(m_Value(A), m_ConstantInt(CI))) ||
622 match(Op1, m_Mul(m_ConstantInt(CI), m_Value(A)))) {
623 Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI));
624 return BinaryOperator::CreateAdd(Op0, NewMul);
Chris Lattner53a19b72010-01-05 07:18:46 +0000625 }
626 }
627
628 ConstantInt *C1;
629 if (Value *X = dyn_castFoldableMul(Op0, C1)) {
630 if (X == Op1) // X*C - X --> X * (C-1)
631 return BinaryOperator::CreateMul(Op1, SubOne(C1));
632
633 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
634 if (X == dyn_castFoldableMul(Op1, C2))
635 return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
636 }
637
638 // Optimize pointer differences into the same array into a size. Consider:
639 // &A[10] - &A[0]: we should compile this to "10".
640 if (TD) {
641 Value *LHSOp, *RHSOp;
642 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
643 match(Op1, m_PtrToInt(m_Value(RHSOp))))
644 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
645 return ReplaceInstUsesWith(I, Res);
646
647 // trunc(p)-trunc(q) -> trunc(p-q)
648 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
649 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
650 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
651 return ReplaceInstUsesWith(I, Res);
652 }
653
654 return 0;
655}
656
657Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
658 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
659
660 // If this is a 'B = x-(-A)', change to B = x+A...
661 if (Value *V = dyn_castFNegVal(Op1))
662 return BinaryOperator::CreateFAdd(Op0, V);
663
Chris Lattner53a19b72010-01-05 07:18:46 +0000664 return 0;
665}