InstructionSimplify.cpp revision 88cd0aadb2849a259e8656b0ff8439ef660db7c5
1//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
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 routines for folding instructions into simpler forms
11// that do not require creating new instructions.  This does constant folding
12// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
13// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
14// ("and i32 %x, %x" -> "%x").  All operands are assumed to have already been
15// simplified: This is usually true and assuming it simplifies the logic (if
16// they have not been simplified then results are correct but maybe suboptimal).
17//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "instsimplify"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/InstructionSimplify.h"
23#include "llvm/Analysis/ConstantFolding.h"
24#include "llvm/Analysis/Dominators.h"
25#include "llvm/Analysis/ValueTracking.h"
26#include "llvm/Support/PatternMatch.h"
27#include "llvm/Support/ValueHandle.h"
28#include "llvm/Target/TargetData.h"
29using namespace llvm;
30using namespace llvm::PatternMatch;
31
32enum { RecursionLimit = 3 };
33
34STATISTIC(NumExpand,  "Number of expansions");
35STATISTIC(NumFactor , "Number of factorizations");
36STATISTIC(NumReassoc, "Number of reassociations");
37
38static Value *SimplifyAndInst(Value *, Value *, const TargetData *,
39                              const DominatorTree *, unsigned);
40static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *,
41                            const DominatorTree *, unsigned);
42static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *,
43                              const DominatorTree *, unsigned);
44static Value *SimplifyOrInst(Value *, Value *, const TargetData *,
45                             const DominatorTree *, unsigned);
46static Value *SimplifyXorInst(Value *, Value *, const TargetData *,
47                              const DominatorTree *, unsigned);
48
49/// ValueDominatesPHI - Does the given value dominate the specified phi node?
50static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
51  Instruction *I = dyn_cast<Instruction>(V);
52  if (!I)
53    // Arguments and constants dominate all instructions.
54    return true;
55
56  // If we have a DominatorTree then do a precise test.
57  if (DT)
58    return DT->dominates(I, P);
59
60  // Otherwise, if the instruction is in the entry block, and is not an invoke,
61  // then it obviously dominates all phi nodes.
62  if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
63      !isa<InvokeInst>(I))
64    return true;
65
66  return false;
67}
68
69/// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning
70/// it into "(A op B) op' (A op C)".  Here "op" is given by Opcode and "op'" is
71/// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
72/// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
73/// Returns the simplified value, or null if no simplification was performed.
74static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
75                          unsigned OpcToExpand, const TargetData *TD,
76                          const DominatorTree *DT, unsigned MaxRecurse) {
77  Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
78  // Recursion is always used, so bail out at once if we already hit the limit.
79  if (!MaxRecurse--)
80    return 0;
81
82  // Check whether the expression has the form "(A op' B) op C".
83  if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
84    if (Op0->getOpcode() == OpcodeToExpand) {
85      // It does!  Try turning it into "(A op C) op' (B op C)".
86      Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
87      // Do "A op C" and "B op C" both simplify?
88      if (Value *L = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse))
89        if (Value *R = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
90          // They do! Return "L op' R" if it simplifies or is already available.
91          // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
92          if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand)
93                                     && L == B && R == A)) {
94            ++NumExpand;
95            return LHS;
96          }
97          // Otherwise return "L op' R" if it simplifies.
98          if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
99                                       MaxRecurse)) {
100            ++NumExpand;
101            return V;
102          }
103        }
104    }
105
106  // Check whether the expression has the form "A op (B op' C)".
107  if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
108    if (Op1->getOpcode() == OpcodeToExpand) {
109      // It does!  Try turning it into "(A op B) op' (A op C)".
110      Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
111      // Do "A op B" and "A op C" both simplify?
112      if (Value *L = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse))
113        if (Value *R = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) {
114          // They do! Return "L op' R" if it simplifies or is already available.
115          // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
116          if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand)
117                                     && L == C && R == B)) {
118            ++NumExpand;
119            return RHS;
120          }
121          // Otherwise return "L op' R" if it simplifies.
122          if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
123                                       MaxRecurse)) {
124            ++NumExpand;
125            return V;
126          }
127        }
128    }
129
130  return 0;
131}
132
133/// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term
134/// using the operation OpCodeToExtract.  For example, when Opcode is Add and
135/// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)".
136/// Returns the simplified value, or null if no simplification was performed.
137static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS,
138                             unsigned OpcToExtract, const TargetData *TD,
139                             const DominatorTree *DT, unsigned MaxRecurse) {
140  Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract;
141  // Recursion is always used, so bail out at once if we already hit the limit.
142  if (!MaxRecurse--)
143    return 0;
144
145  BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
146  BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
147
148  if (!Op0 || Op0->getOpcode() != OpcodeToExtract ||
149      !Op1 || Op1->getOpcode() != OpcodeToExtract)
150    return 0;
151
152  // The expression has the form "(A op' B) op (C op' D)".
153  Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
154  Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
155
156  // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)".
157  // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
158  // commutative case, "(A op' B) op (C op' A)"?
159  if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) {
160    Value *DD = A == C ? D : C;
161    // Form "A op' (B op DD)" if it simplifies completely.
162    // Does "B op DD" simplify?
163    if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, DT, MaxRecurse)) {
164      // It does!  Return "A op' V" if it simplifies or is already available.
165      // If V equals B then "A op' V" is just the LHS.  If V equals DD then
166      // "A op' V" is just the RHS.
167      if (V == B || V == DD) {
168        ++NumFactor;
169        return V == B ? LHS : RHS;
170      }
171      // Otherwise return "A op' V" if it simplifies.
172      if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, DT, MaxRecurse)) {
173        ++NumFactor;
174        return W;
175      }
176    }
177  }
178
179  // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)".
180  // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
181  // commutative case, "(A op' B) op (B op' D)"?
182  if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) {
183    Value *CC = B == D ? C : D;
184    // Form "(A op CC) op' B" if it simplifies completely..
185    // Does "A op CC" simplify?
186    if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, DT, MaxRecurse)) {
187      // It does!  Return "V op' B" if it simplifies or is already available.
188      // If V equals A then "V op' B" is just the LHS.  If V equals CC then
189      // "V op' B" is just the RHS.
190      if (V == A || V == CC) {
191        ++NumFactor;
192        return V == A ? LHS : RHS;
193      }
194      // Otherwise return "V op' B" if it simplifies.
195      if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, DT, MaxRecurse)) {
196        ++NumFactor;
197        return W;
198      }
199    }
200  }
201
202  return 0;
203}
204
205/// SimplifyAssociativeBinOp - Generic simplifications for associative binary
206/// operations.  Returns the simpler value, or null if none was found.
207static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
208                                       const TargetData *TD,
209                                       const DominatorTree *DT,
210                                       unsigned MaxRecurse) {
211  Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
212  assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
213
214  // Recursion is always used, so bail out at once if we already hit the limit.
215  if (!MaxRecurse--)
216    return 0;
217
218  BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
219  BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
220
221  // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
222  if (Op0 && Op0->getOpcode() == Opcode) {
223    Value *A = Op0->getOperand(0);
224    Value *B = Op0->getOperand(1);
225    Value *C = RHS;
226
227    // Does "B op C" simplify?
228    if (Value *V = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
229      // It does!  Return "A op V" if it simplifies or is already available.
230      // If V equals B then "A op V" is just the LHS.
231      if (V == B) return LHS;
232      // Otherwise return "A op V" if it simplifies.
233      if (Value *W = SimplifyBinOp(Opcode, A, V, TD, DT, MaxRecurse)) {
234        ++NumReassoc;
235        return W;
236      }
237    }
238  }
239
240  // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
241  if (Op1 && Op1->getOpcode() == Opcode) {
242    Value *A = LHS;
243    Value *B = Op1->getOperand(0);
244    Value *C = Op1->getOperand(1);
245
246    // Does "A op B" simplify?
247    if (Value *V = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) {
248      // It does!  Return "V op C" if it simplifies or is already available.
249      // If V equals B then "V op C" is just the RHS.
250      if (V == B) return RHS;
251      // Otherwise return "V op C" if it simplifies.
252      if (Value *W = SimplifyBinOp(Opcode, V, C, TD, DT, MaxRecurse)) {
253        ++NumReassoc;
254        return W;
255      }
256    }
257  }
258
259  // The remaining transforms require commutativity as well as associativity.
260  if (!Instruction::isCommutative(Opcode))
261    return 0;
262
263  // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
264  if (Op0 && Op0->getOpcode() == Opcode) {
265    Value *A = Op0->getOperand(0);
266    Value *B = Op0->getOperand(1);
267    Value *C = RHS;
268
269    // Does "C op A" simplify?
270    if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
271      // It does!  Return "V op B" if it simplifies or is already available.
272      // If V equals A then "V op B" is just the LHS.
273      if (V == A) return LHS;
274      // Otherwise return "V op B" if it simplifies.
275      if (Value *W = SimplifyBinOp(Opcode, V, B, TD, DT, MaxRecurse)) {
276        ++NumReassoc;
277        return W;
278      }
279    }
280  }
281
282  // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
283  if (Op1 && Op1->getOpcode() == Opcode) {
284    Value *A = LHS;
285    Value *B = Op1->getOperand(0);
286    Value *C = Op1->getOperand(1);
287
288    // Does "C op A" simplify?
289    if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
290      // It does!  Return "B op V" if it simplifies or is already available.
291      // If V equals C then "B op V" is just the RHS.
292      if (V == C) return RHS;
293      // Otherwise return "B op V" if it simplifies.
294      if (Value *W = SimplifyBinOp(Opcode, B, V, TD, DT, MaxRecurse)) {
295        ++NumReassoc;
296        return W;
297      }
298    }
299  }
300
301  return 0;
302}
303
304/// ThreadBinOpOverSelect - In the case of a binary operation with a select
305/// instruction as an operand, try to simplify the binop by seeing whether
306/// evaluating it on both branches of the select results in the same value.
307/// Returns the common value if so, otherwise returns null.
308static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
309                                    const TargetData *TD,
310                                    const DominatorTree *DT,
311                                    unsigned MaxRecurse) {
312  // Recursion is always used, so bail out at once if we already hit the limit.
313  if (!MaxRecurse--)
314    return 0;
315
316  SelectInst *SI;
317  if (isa<SelectInst>(LHS)) {
318    SI = cast<SelectInst>(LHS);
319  } else {
320    assert(isa<SelectInst>(RHS) && "No select instruction operand!");
321    SI = cast<SelectInst>(RHS);
322  }
323
324  // Evaluate the BinOp on the true and false branches of the select.
325  Value *TV;
326  Value *FV;
327  if (SI == LHS) {
328    TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, DT, MaxRecurse);
329    FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, DT, MaxRecurse);
330  } else {
331    TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, DT, MaxRecurse);
332    FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, DT, MaxRecurse);
333  }
334
335  // If they simplified to the same value, then return the common value.
336  // If they both failed to simplify then return null.
337  if (TV == FV)
338    return TV;
339
340  // If one branch simplified to undef, return the other one.
341  if (TV && isa<UndefValue>(TV))
342    return FV;
343  if (FV && isa<UndefValue>(FV))
344    return TV;
345
346  // If applying the operation did not change the true and false select values,
347  // then the result of the binop is the select itself.
348  if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
349    return SI;
350
351  // If one branch simplified and the other did not, and the simplified
352  // value is equal to the unsimplified one, return the simplified value.
353  // For example, select (cond, X, X & Z) & Z -> X & Z.
354  if ((FV && !TV) || (TV && !FV)) {
355    // Check that the simplified value has the form "X op Y" where "op" is the
356    // same as the original operation.
357    Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
358    if (Simplified && Simplified->getOpcode() == Opcode) {
359      // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
360      // We already know that "op" is the same as for the simplified value.  See
361      // if the operands match too.  If so, return the simplified value.
362      Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
363      Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
364      Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
365      if (Simplified->getOperand(0) == UnsimplifiedLHS &&
366          Simplified->getOperand(1) == UnsimplifiedRHS)
367        return Simplified;
368      if (Simplified->isCommutative() &&
369          Simplified->getOperand(1) == UnsimplifiedLHS &&
370          Simplified->getOperand(0) == UnsimplifiedRHS)
371        return Simplified;
372    }
373  }
374
375  return 0;
376}
377
378/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
379/// try to simplify the comparison by seeing whether both branches of the select
380/// result in the same value.  Returns the common value if so, otherwise returns
381/// null.
382static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
383                                  Value *RHS, const TargetData *TD,
384                                  const DominatorTree *DT,
385                                  unsigned MaxRecurse) {
386  // Recursion is always used, so bail out at once if we already hit the limit.
387  if (!MaxRecurse--)
388    return 0;
389
390  // Make sure the select is on the LHS.
391  if (!isa<SelectInst>(LHS)) {
392    std::swap(LHS, RHS);
393    Pred = CmpInst::getSwappedPredicate(Pred);
394  }
395  assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
396  SelectInst *SI = cast<SelectInst>(LHS);
397
398  // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
399  // Does "cmp TV, RHS" simplify?
400  if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT,
401                                    MaxRecurse)) {
402    // It does!  Does "cmp FV, RHS" simplify?
403    if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT,
404                                      MaxRecurse)) {
405      // It does!  If they simplified to the same value, then use it as the
406      // result of the original comparison.
407      if (TCmp == FCmp)
408        return TCmp;
409      Value *Cond = SI->getCondition();
410      // If the false value simplified to false, then the result of the compare
411      // is equal to "Cond && TCmp".  This also catches the case when the false
412      // value simplified to false and the true value to true, returning "Cond".
413      if (match(FCmp, m_Zero()))
414        if (Value *V = SimplifyAndInst(Cond, TCmp, TD, DT, MaxRecurse))
415          return V;
416      // If the true value simplified to true, then the result of the compare
417      // is equal to "Cond || FCmp".
418      if (match(TCmp, m_One()))
419        if (Value *V = SimplifyOrInst(Cond, FCmp, TD, DT, MaxRecurse))
420          return V;
421      // Finally, if the false value simplified to true and the true value to
422      // false, then the result of the compare is equal to "!Cond".
423      if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
424        if (Value *V =
425            SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
426                            TD, DT, MaxRecurse))
427          return V;
428    }
429  }
430
431  return 0;
432}
433
434/// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
435/// is a PHI instruction, try to simplify the binop by seeing whether evaluating
436/// it on the incoming phi values yields the same result for every value.  If so
437/// returns the common value, otherwise returns null.
438static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
439                                 const TargetData *TD, const DominatorTree *DT,
440                                 unsigned MaxRecurse) {
441  // Recursion is always used, so bail out at once if we already hit the limit.
442  if (!MaxRecurse--)
443    return 0;
444
445  PHINode *PI;
446  if (isa<PHINode>(LHS)) {
447    PI = cast<PHINode>(LHS);
448    // Bail out if RHS and the phi may be mutually interdependent due to a loop.
449    if (!ValueDominatesPHI(RHS, PI, DT))
450      return 0;
451  } else {
452    assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
453    PI = cast<PHINode>(RHS);
454    // Bail out if LHS and the phi may be mutually interdependent due to a loop.
455    if (!ValueDominatesPHI(LHS, PI, DT))
456      return 0;
457  }
458
459  // Evaluate the BinOp on the incoming phi values.
460  Value *CommonValue = 0;
461  for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
462    Value *Incoming = PI->getIncomingValue(i);
463    // If the incoming value is the phi node itself, it can safely be skipped.
464    if (Incoming == PI) continue;
465    Value *V = PI == LHS ?
466      SimplifyBinOp(Opcode, Incoming, RHS, TD, DT, MaxRecurse) :
467      SimplifyBinOp(Opcode, LHS, Incoming, TD, DT, MaxRecurse);
468    // If the operation failed to simplify, or simplified to a different value
469    // to previously, then give up.
470    if (!V || (CommonValue && V != CommonValue))
471      return 0;
472    CommonValue = V;
473  }
474
475  return CommonValue;
476}
477
478/// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
479/// try to simplify the comparison by seeing whether comparing with all of the
480/// incoming phi values yields the same result every time.  If so returns the
481/// common result, otherwise returns null.
482static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
483                               const TargetData *TD, const DominatorTree *DT,
484                               unsigned MaxRecurse) {
485  // Recursion is always used, so bail out at once if we already hit the limit.
486  if (!MaxRecurse--)
487    return 0;
488
489  // Make sure the phi is on the LHS.
490  if (!isa<PHINode>(LHS)) {
491    std::swap(LHS, RHS);
492    Pred = CmpInst::getSwappedPredicate(Pred);
493  }
494  assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
495  PHINode *PI = cast<PHINode>(LHS);
496
497  // Bail out if RHS and the phi may be mutually interdependent due to a loop.
498  if (!ValueDominatesPHI(RHS, PI, DT))
499    return 0;
500
501  // Evaluate the BinOp on the incoming phi values.
502  Value *CommonValue = 0;
503  for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
504    Value *Incoming = PI->getIncomingValue(i);
505    // If the incoming value is the phi node itself, it can safely be skipped.
506    if (Incoming == PI) continue;
507    Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, DT, MaxRecurse);
508    // If the operation failed to simplify, or simplified to a different value
509    // to previously, then give up.
510    if (!V || (CommonValue && V != CommonValue))
511      return 0;
512    CommonValue = V;
513  }
514
515  return CommonValue;
516}
517
518/// SimplifyAddInst - Given operands for an Add, see if we can
519/// fold the result.  If not, this returns null.
520static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
521                              const TargetData *TD, const DominatorTree *DT,
522                              unsigned MaxRecurse) {
523  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
524    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
525      Constant *Ops[] = { CLHS, CRHS };
526      return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
527                                      Ops, 2, TD);
528    }
529
530    // Canonicalize the constant to the RHS.
531    std::swap(Op0, Op1);
532  }
533
534  // X + undef -> undef
535  if (match(Op1, m_Undef()))
536    return Op1;
537
538  // X + 0 -> X
539  if (match(Op1, m_Zero()))
540    return Op0;
541
542  // X + (Y - X) -> Y
543  // (Y - X) + X -> Y
544  // Eg: X + -X -> 0
545  Value *Y = 0;
546  if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
547      match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
548    return Y;
549
550  // X + ~X -> -1   since   ~X = -X-1
551  if (match(Op0, m_Not(m_Specific(Op1))) ||
552      match(Op1, m_Not(m_Specific(Op0))))
553    return Constant::getAllOnesValue(Op0->getType());
554
555  /// i1 add -> xor.
556  if (MaxRecurse && Op0->getType()->isIntegerTy(1))
557    if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
558      return V;
559
560  // Try some generic simplifications for associative operations.
561  if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, DT,
562                                          MaxRecurse))
563    return V;
564
565  // Mul distributes over Add.  Try some generic simplifications based on this.
566  if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul,
567                                TD, DT, MaxRecurse))
568    return V;
569
570  // Threading Add over selects and phi nodes is pointless, so don't bother.
571  // Threading over the select in "A + select(cond, B, C)" means evaluating
572  // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
573  // only if B and C are equal.  If B and C are equal then (since we assume
574  // that operands have already been simplified) "select(cond, B, C)" should
575  // have been simplified to the common value of B and C already.  Analysing
576  // "A+B" and "A+C" thus gains nothing, but costs compile time.  Similarly
577  // for threading over phi nodes.
578
579  return 0;
580}
581
582Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
583                             const TargetData *TD, const DominatorTree *DT) {
584  return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
585}
586
587/// SimplifySubInst - Given operands for a Sub, see if we can
588/// fold the result.  If not, this returns null.
589static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
590                              const TargetData *TD, const DominatorTree *DT,
591                              unsigned MaxRecurse) {
592  if (Constant *CLHS = dyn_cast<Constant>(Op0))
593    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
594      Constant *Ops[] = { CLHS, CRHS };
595      return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(),
596                                      Ops, 2, TD);
597    }
598
599  // X - undef -> undef
600  // undef - X -> undef
601  if (match(Op0, m_Undef()) || match(Op1, m_Undef()))
602    return UndefValue::get(Op0->getType());
603
604  // X - 0 -> X
605  if (match(Op1, m_Zero()))
606    return Op0;
607
608  // X - X -> 0
609  if (Op0 == Op1)
610    return Constant::getNullValue(Op0->getType());
611
612  // (X*2) - X -> X
613  // (X<<1) - X -> X
614  Value *X = 0;
615  if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
616      match(Op0, m_Shl(m_Specific(Op1), m_One())))
617    return Op1;
618
619  // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
620  // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
621  Value *Y = 0, *Z = Op1;
622  if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
623    // See if "V === Y - Z" simplifies.
624    if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, DT, MaxRecurse-1))
625      // It does!  Now see if "X + V" simplifies.
626      if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, DT,
627                                   MaxRecurse-1)) {
628        // It does, we successfully reassociated!
629        ++NumReassoc;
630        return W;
631      }
632    // See if "V === X - Z" simplifies.
633    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
634      // It does!  Now see if "Y + V" simplifies.
635      if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, DT,
636                                   MaxRecurse-1)) {
637        // It does, we successfully reassociated!
638        ++NumReassoc;
639        return W;
640      }
641  }
642
643  // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
644  // For example, X - (X + 1) -> -1
645  X = Op0;
646  if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
647    // See if "V === X - Y" simplifies.
648    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, DT, MaxRecurse-1))
649      // It does!  Now see if "V - Z" simplifies.
650      if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, DT,
651                                   MaxRecurse-1)) {
652        // It does, we successfully reassociated!
653        ++NumReassoc;
654        return W;
655      }
656    // See if "V === X - Z" simplifies.
657    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
658      // It does!  Now see if "V - Y" simplifies.
659      if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, DT,
660                                   MaxRecurse-1)) {
661        // It does, we successfully reassociated!
662        ++NumReassoc;
663        return W;
664      }
665  }
666
667  // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
668  // For example, X - (X - Y) -> Y.
669  Z = Op0;
670  if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
671    // See if "V === Z - X" simplifies.
672    if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
673      // It does!  Now see if "V + Y" simplifies.
674      if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
675                                   MaxRecurse-1)) {
676        // It does, we successfully reassociated!
677        ++NumReassoc;
678        return W;
679      }
680
681  // Mul distributes over Sub.  Try some generic simplifications based on this.
682  if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
683                                TD, DT, MaxRecurse))
684    return V;
685
686  // i1 sub -> xor.
687  if (MaxRecurse && Op0->getType()->isIntegerTy(1))
688    if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
689      return V;
690
691  // Threading Sub over selects and phi nodes is pointless, so don't bother.
692  // Threading over the select in "A - select(cond, B, C)" means evaluating
693  // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
694  // only if B and C are equal.  If B and C are equal then (since we assume
695  // that operands have already been simplified) "select(cond, B, C)" should
696  // have been simplified to the common value of B and C already.  Analysing
697  // "A-B" and "A-C" thus gains nothing, but costs compile time.  Similarly
698  // for threading over phi nodes.
699
700  return 0;
701}
702
703Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
704                             const TargetData *TD, const DominatorTree *DT) {
705  return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
706}
707
708/// SimplifyMulInst - Given operands for a Mul, see if we can
709/// fold the result.  If not, this returns null.
710static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
711                              const DominatorTree *DT, unsigned MaxRecurse) {
712  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
713    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
714      Constant *Ops[] = { CLHS, CRHS };
715      return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(),
716                                      Ops, 2, TD);
717    }
718
719    // Canonicalize the constant to the RHS.
720    std::swap(Op0, Op1);
721  }
722
723  // X * undef -> 0
724  if (match(Op1, m_Undef()))
725    return Constant::getNullValue(Op0->getType());
726
727  // X * 0 -> 0
728  if (match(Op1, m_Zero()))
729    return Op1;
730
731  // X * 1 -> X
732  if (match(Op1, m_One()))
733    return Op0;
734
735  // (X / Y) * Y -> X if the division is exact.
736  Value *X = 0, *Y = 0;
737  if ((match(Op0, m_IDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
738      (match(Op1, m_IDiv(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y)
739    BinaryOperator *Div = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1);
740    if (Div->isExact())
741      return X;
742  }
743
744  // i1 mul -> and.
745  if (MaxRecurse && Op0->getType()->isIntegerTy(1))
746    if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1))
747      return V;
748
749  // Try some generic simplifications for associative operations.
750  if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, DT,
751                                          MaxRecurse))
752    return V;
753
754  // Mul distributes over Add.  Try some generic simplifications based on this.
755  if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
756                             TD, DT, MaxRecurse))
757    return V;
758
759  // If the operation is with the result of a select instruction, check whether
760  // operating on either branch of the select always yields the same value.
761  if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
762    if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, DT,
763                                         MaxRecurse))
764      return V;
765
766  // If the operation is with the result of a phi instruction, check whether
767  // operating on all incoming values of the phi always yields the same value.
768  if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
769    if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, DT,
770                                      MaxRecurse))
771      return V;
772
773  return 0;
774}
775
776Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
777                             const DominatorTree *DT) {
778  return ::SimplifyMulInst(Op0, Op1, TD, DT, RecursionLimit);
779}
780
781/// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can
782/// fold the result.  If not, this returns null.
783static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
784                          const TargetData *TD, const DominatorTree *DT,
785                          unsigned MaxRecurse) {
786  if (Constant *C0 = dyn_cast<Constant>(Op0)) {
787    if (Constant *C1 = dyn_cast<Constant>(Op1)) {
788      Constant *Ops[] = { C0, C1 };
789      return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD);
790    }
791  }
792
793  bool isSigned = Opcode == Instruction::SDiv;
794
795  // X / undef -> undef
796  if (match(Op1, m_Undef()))
797    return Op1;
798
799  // undef / X -> 0
800  if (match(Op0, m_Undef()))
801    return Constant::getNullValue(Op0->getType());
802
803  // 0 / X -> 0, we don't need to preserve faults!
804  if (match(Op0, m_Zero()))
805    return Op0;
806
807  // X / 1 -> X
808  if (match(Op1, m_One()))
809    return Op0;
810
811  if (Op0->getType()->isIntegerTy(1))
812    // It can't be division by zero, hence it must be division by one.
813    return Op0;
814
815  // X / X -> 1
816  if (Op0 == Op1)
817    return ConstantInt::get(Op0->getType(), 1);
818
819  // (X * Y) / Y -> X if the multiplication does not overflow.
820  Value *X = 0, *Y = 0;
821  if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) {
822    if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
823    BinaryOperator *Mul = cast<BinaryOperator>(Op0);
824    // If the Mul knows it does not overflow, then we are good to go.
825    if ((isSigned && Mul->hasNoSignedWrap()) ||
826        (!isSigned && Mul->hasNoUnsignedWrap()))
827      return X;
828    // If X has the form X = A / Y then X * Y cannot overflow.
829    if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
830      if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y)
831        return X;
832  }
833
834  // (X rem Y) / Y -> 0
835  if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
836      (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
837    return Constant::getNullValue(Op0->getType());
838
839  // If the operation is with the result of a select instruction, check whether
840  // operating on either branch of the select always yields the same value.
841  if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
842    if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse))
843      return V;
844
845  // If the operation is with the result of a phi instruction, check whether
846  // operating on all incoming values of the phi always yields the same value.
847  if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
848    if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse))
849      return V;
850
851  return 0;
852}
853
854/// SimplifySDivInst - Given operands for an SDiv, see if we can
855/// fold the result.  If not, this returns null.
856static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
857                               const DominatorTree *DT, unsigned MaxRecurse) {
858  if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, DT, MaxRecurse))
859    return V;
860
861  return 0;
862}
863
864Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
865                              const DominatorTree *DT) {
866  return ::SimplifySDivInst(Op0, Op1, TD, DT, RecursionLimit);
867}
868
869/// SimplifyUDivInst - Given operands for a UDiv, see if we can
870/// fold the result.  If not, this returns null.
871static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
872                               const DominatorTree *DT, unsigned MaxRecurse) {
873  if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, DT, MaxRecurse))
874    return V;
875
876  return 0;
877}
878
879Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
880                              const DominatorTree *DT) {
881  return ::SimplifyUDivInst(Op0, Op1, TD, DT, RecursionLimit);
882}
883
884static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *,
885                               const DominatorTree *, unsigned) {
886  // undef / X -> undef    (the undef could be a snan).
887  if (match(Op0, m_Undef()))
888    return Op0;
889
890  // X / undef -> undef
891  if (match(Op1, m_Undef()))
892    return Op1;
893
894  return 0;
895}
896
897Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD,
898                              const DominatorTree *DT) {
899  return ::SimplifyFDivInst(Op0, Op1, TD, DT, RecursionLimit);
900}
901
902/// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can
903/// fold the result.  If not, this returns null.
904static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1,
905                            const TargetData *TD, const DominatorTree *DT,
906                            unsigned MaxRecurse) {
907  if (Constant *C0 = dyn_cast<Constant>(Op0)) {
908    if (Constant *C1 = dyn_cast<Constant>(Op1)) {
909      Constant *Ops[] = { C0, C1 };
910      return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD);
911    }
912  }
913
914  // 0 shift by X -> 0
915  if (match(Op0, m_Zero()))
916    return Op0;
917
918  // X shift by 0 -> X
919  if (match(Op1, m_Zero()))
920    return Op0;
921
922  // X shift by undef -> undef because it may shift by the bitwidth.
923  if (match(Op1, m_Undef()))
924    return Op1;
925
926  // Shifting by the bitwidth or more is undefined.
927  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1))
928    if (CI->getValue().getLimitedValue() >=
929        Op0->getType()->getScalarSizeInBits())
930      return UndefValue::get(Op0->getType());
931
932  // If the operation is with the result of a select instruction, check whether
933  // operating on either branch of the select always yields the same value.
934  if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
935    if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse))
936      return V;
937
938  // If the operation is with the result of a phi instruction, check whether
939  // operating on all incoming values of the phi always yields the same value.
940  if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
941    if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse))
942      return V;
943
944  return 0;
945}
946
947/// SimplifyShlInst - Given operands for an Shl, see if we can
948/// fold the result.  If not, this returns null.
949static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
950                              const TargetData *TD, const DominatorTree *DT,
951                              unsigned MaxRecurse) {
952  if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, DT, MaxRecurse))
953    return V;
954
955  // undef << X -> 0
956  if (match(Op0, m_Undef()))
957    return Constant::getNullValue(Op0->getType());
958
959  // (X >> A) << A -> X
960  Value *X;
961  if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1))) &&
962      cast<PossiblyExactOperator>(Op0)->isExact())
963    return X;
964  return 0;
965}
966
967Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
968                             const TargetData *TD, const DominatorTree *DT) {
969  return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
970}
971
972/// SimplifyLShrInst - Given operands for an LShr, see if we can
973/// fold the result.  If not, this returns null.
974static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
975                               const TargetData *TD, const DominatorTree *DT,
976                               unsigned MaxRecurse) {
977  if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, DT, MaxRecurse))
978    return V;
979
980  // undef >>l X -> 0
981  if (match(Op0, m_Undef()))
982    return Constant::getNullValue(Op0->getType());
983
984  // (X << A) >> A -> X
985  Value *X;
986  if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
987      cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap())
988    return X;
989
990  return 0;
991}
992
993Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
994                              const TargetData *TD, const DominatorTree *DT) {
995  return ::SimplifyLShrInst(Op0, Op1, isExact, TD, DT, RecursionLimit);
996}
997
998/// SimplifyAShrInst - Given operands for an AShr, see if we can
999/// fold the result.  If not, this returns null.
1000static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1001                               const TargetData *TD, const DominatorTree *DT,
1002                               unsigned MaxRecurse) {
1003  if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, DT, MaxRecurse))
1004    return V;
1005
1006  // all ones >>a X -> all ones
1007  if (match(Op0, m_AllOnes()))
1008    return Op0;
1009
1010  // undef >>a X -> all ones
1011  if (match(Op0, m_Undef()))
1012    return Constant::getAllOnesValue(Op0->getType());
1013
1014  // (X << A) >> A -> X
1015  Value *X;
1016  if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
1017      cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
1018    return X;
1019
1020  return 0;
1021}
1022
1023Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1024                              const TargetData *TD, const DominatorTree *DT) {
1025  return ::SimplifyAShrInst(Op0, Op1, isExact, TD, DT, RecursionLimit);
1026}
1027
1028/// SimplifyAndInst - Given operands for an And, see if we can
1029/// fold the result.  If not, this returns null.
1030static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
1031                              const DominatorTree *DT, unsigned MaxRecurse) {
1032  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1033    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1034      Constant *Ops[] = { CLHS, CRHS };
1035      return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
1036                                      Ops, 2, TD);
1037    }
1038
1039    // Canonicalize the constant to the RHS.
1040    std::swap(Op0, Op1);
1041  }
1042
1043  // X & undef -> 0
1044  if (match(Op1, m_Undef()))
1045    return Constant::getNullValue(Op0->getType());
1046
1047  // X & X = X
1048  if (Op0 == Op1)
1049    return Op0;
1050
1051  // X & 0 = 0
1052  if (match(Op1, m_Zero()))
1053    return Op1;
1054
1055  // X & -1 = X
1056  if (match(Op1, m_AllOnes()))
1057    return Op0;
1058
1059  // A & ~A  =  ~A & A  =  0
1060  if (match(Op0, m_Not(m_Specific(Op1))) ||
1061      match(Op1, m_Not(m_Specific(Op0))))
1062    return Constant::getNullValue(Op0->getType());
1063
1064  // (A | ?) & A = A
1065  Value *A = 0, *B = 0;
1066  if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1067      (A == Op1 || B == Op1))
1068    return Op1;
1069
1070  // A & (A | ?) = A
1071  if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1072      (A == Op0 || B == Op0))
1073    return Op0;
1074
1075  // Try some generic simplifications for associative operations.
1076  if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, DT,
1077                                          MaxRecurse))
1078    return V;
1079
1080  // And distributes over Or.  Try some generic simplifications based on this.
1081  if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1082                             TD, DT, MaxRecurse))
1083    return V;
1084
1085  // And distributes over Xor.  Try some generic simplifications based on this.
1086  if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
1087                             TD, DT, MaxRecurse))
1088    return V;
1089
1090  // Or distributes over And.  Try some generic simplifications based on this.
1091  if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1092                                TD, DT, MaxRecurse))
1093    return V;
1094
1095  // If the operation is with the result of a select instruction, check whether
1096  // operating on either branch of the select always yields the same value.
1097  if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1098    if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, DT,
1099                                         MaxRecurse))
1100      return V;
1101
1102  // If the operation is with the result of a phi instruction, check whether
1103  // operating on all incoming values of the phi always yields the same value.
1104  if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1105    if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, DT,
1106                                      MaxRecurse))
1107      return V;
1108
1109  return 0;
1110}
1111
1112Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
1113                             const DominatorTree *DT) {
1114  return ::SimplifyAndInst(Op0, Op1, TD, DT, RecursionLimit);
1115}
1116
1117/// SimplifyOrInst - Given operands for an Or, see if we can
1118/// fold the result.  If not, this returns null.
1119static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
1120                             const DominatorTree *DT, unsigned MaxRecurse) {
1121  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1122    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1123      Constant *Ops[] = { CLHS, CRHS };
1124      return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
1125                                      Ops, 2, TD);
1126    }
1127
1128    // Canonicalize the constant to the RHS.
1129    std::swap(Op0, Op1);
1130  }
1131
1132  // X | undef -> -1
1133  if (match(Op1, m_Undef()))
1134    return Constant::getAllOnesValue(Op0->getType());
1135
1136  // X | X = X
1137  if (Op0 == Op1)
1138    return Op0;
1139
1140  // X | 0 = X
1141  if (match(Op1, m_Zero()))
1142    return Op0;
1143
1144  // X | -1 = -1
1145  if (match(Op1, m_AllOnes()))
1146    return Op1;
1147
1148  // A | ~A  =  ~A | A  =  -1
1149  if (match(Op0, m_Not(m_Specific(Op1))) ||
1150      match(Op1, m_Not(m_Specific(Op0))))
1151    return Constant::getAllOnesValue(Op0->getType());
1152
1153  // (A & ?) | A = A
1154  Value *A = 0, *B = 0;
1155  if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1156      (A == Op1 || B == Op1))
1157    return Op1;
1158
1159  // A | (A & ?) = A
1160  if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
1161      (A == Op0 || B == Op0))
1162    return Op0;
1163
1164  // ~(A & ?) | A = -1
1165  if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1166      (A == Op1 || B == Op1))
1167    return Constant::getAllOnesValue(Op1->getType());
1168
1169  // A | ~(A & ?) = -1
1170  if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1171      (A == Op0 || B == Op0))
1172    return Constant::getAllOnesValue(Op0->getType());
1173
1174  // Try some generic simplifications for associative operations.
1175  if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, DT,
1176                                          MaxRecurse))
1177    return V;
1178
1179  // Or distributes over And.  Try some generic simplifications based on this.
1180  if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And,
1181                             TD, DT, MaxRecurse))
1182    return V;
1183
1184  // And distributes over Or.  Try some generic simplifications based on this.
1185  if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And,
1186                                TD, DT, MaxRecurse))
1187    return V;
1188
1189  // If the operation is with the result of a select instruction, check whether
1190  // operating on either branch of the select always yields the same value.
1191  if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1192    if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, DT,
1193                                         MaxRecurse))
1194      return V;
1195
1196  // If the operation is with the result of a phi instruction, check whether
1197  // operating on all incoming values of the phi always yields the same value.
1198  if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1199    if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, DT,
1200                                      MaxRecurse))
1201      return V;
1202
1203  return 0;
1204}
1205
1206Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
1207                            const DominatorTree *DT) {
1208  return ::SimplifyOrInst(Op0, Op1, TD, DT, RecursionLimit);
1209}
1210
1211/// SimplifyXorInst - Given operands for a Xor, see if we can
1212/// fold the result.  If not, this returns null.
1213static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
1214                              const DominatorTree *DT, unsigned MaxRecurse) {
1215  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1216    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1217      Constant *Ops[] = { CLHS, CRHS };
1218      return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(),
1219                                      Ops, 2, TD);
1220    }
1221
1222    // Canonicalize the constant to the RHS.
1223    std::swap(Op0, Op1);
1224  }
1225
1226  // A ^ undef -> undef
1227  if (match(Op1, m_Undef()))
1228    return Op1;
1229
1230  // A ^ 0 = A
1231  if (match(Op1, m_Zero()))
1232    return Op0;
1233
1234  // A ^ A = 0
1235  if (Op0 == Op1)
1236    return Constant::getNullValue(Op0->getType());
1237
1238  // A ^ ~A  =  ~A ^ A  =  -1
1239  if (match(Op0, m_Not(m_Specific(Op1))) ||
1240      match(Op1, m_Not(m_Specific(Op0))))
1241    return Constant::getAllOnesValue(Op0->getType());
1242
1243  // Try some generic simplifications for associative operations.
1244  if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, DT,
1245                                          MaxRecurse))
1246    return V;
1247
1248  // And distributes over Xor.  Try some generic simplifications based on this.
1249  if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And,
1250                                TD, DT, MaxRecurse))
1251    return V;
1252
1253  // Threading Xor over selects and phi nodes is pointless, so don't bother.
1254  // Threading over the select in "A ^ select(cond, B, C)" means evaluating
1255  // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
1256  // only if B and C are equal.  If B and C are equal then (since we assume
1257  // that operands have already been simplified) "select(cond, B, C)" should
1258  // have been simplified to the common value of B and C already.  Analysing
1259  // "A^B" and "A^C" thus gains nothing, but costs compile time.  Similarly
1260  // for threading over phi nodes.
1261
1262  return 0;
1263}
1264
1265Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
1266                             const DominatorTree *DT) {
1267  return ::SimplifyXorInst(Op0, Op1, TD, DT, RecursionLimit);
1268}
1269
1270static const Type *GetCompareTy(Value *Op) {
1271  return CmpInst::makeCmpResultType(Op->getType());
1272}
1273
1274/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
1275/// fold the result.  If not, this returns null.
1276static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1277                               const TargetData *TD, const DominatorTree *DT,
1278                               unsigned MaxRecurse) {
1279  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
1280  assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
1281
1282  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
1283    if (Constant *CRHS = dyn_cast<Constant>(RHS))
1284      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
1285
1286    // If we have a constant, make sure it is on the RHS.
1287    std::swap(LHS, RHS);
1288    Pred = CmpInst::getSwappedPredicate(Pred);
1289  }
1290
1291  const Type *ITy = GetCompareTy(LHS); // The return type.
1292  const Type *OpTy = LHS->getType();   // The operand type.
1293
1294  // icmp X, X -> true/false
1295  // X icmp undef -> true/false.  For example, icmp ugt %X, undef -> false
1296  // because X could be 0.
1297  if (LHS == RHS || isa<UndefValue>(RHS))
1298    return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
1299
1300  // Special case logic when the operands have i1 type.
1301  if (OpTy->isIntegerTy(1) || (OpTy->isVectorTy() &&
1302       cast<VectorType>(OpTy)->getElementType()->isIntegerTy(1))) {
1303    switch (Pred) {
1304    default: break;
1305    case ICmpInst::ICMP_EQ:
1306      // X == 1 -> X
1307      if (match(RHS, m_One()))
1308        return LHS;
1309      break;
1310    case ICmpInst::ICMP_NE:
1311      // X != 0 -> X
1312      if (match(RHS, m_Zero()))
1313        return LHS;
1314      break;
1315    case ICmpInst::ICMP_UGT:
1316      // X >u 0 -> X
1317      if (match(RHS, m_Zero()))
1318        return LHS;
1319      break;
1320    case ICmpInst::ICMP_UGE:
1321      // X >=u 1 -> X
1322      if (match(RHS, m_One()))
1323        return LHS;
1324      break;
1325    case ICmpInst::ICMP_SLT:
1326      // X <s 0 -> X
1327      if (match(RHS, m_Zero()))
1328        return LHS;
1329      break;
1330    case ICmpInst::ICMP_SLE:
1331      // X <=s -1 -> X
1332      if (match(RHS, m_One()))
1333        return LHS;
1334      break;
1335    }
1336  }
1337
1338  // icmp <alloca*>, <global/alloca*/null> - Different stack variables have
1339  // different addresses, and what's more the address of a stack variable is
1340  // never null or equal to the address of a global.  Note that generalizing
1341  // to the case where LHS is a global variable address or null is pointless,
1342  // since if both LHS and RHS are constants then we already constant folded
1343  // the compare, and if only one of them is then we moved it to RHS already.
1344  if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
1345                               isa<ConstantPointerNull>(RHS)))
1346    // We already know that LHS != LHS.
1347    return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1348
1349  // If we are comparing with zero then try hard since this is a common case.
1350  if (match(RHS, m_Zero())) {
1351    bool LHSKnownNonNegative, LHSKnownNegative;
1352    switch (Pred) {
1353    default:
1354      assert(false && "Unknown ICmp predicate!");
1355    case ICmpInst::ICMP_ULT:
1356      return ConstantInt::getFalse(LHS->getContext());
1357    case ICmpInst::ICMP_UGE:
1358      return ConstantInt::getTrue(LHS->getContext());
1359    case ICmpInst::ICMP_EQ:
1360    case ICmpInst::ICMP_ULE:
1361      if (isKnownNonZero(LHS, TD))
1362        return ConstantInt::getFalse(LHS->getContext());
1363      break;
1364    case ICmpInst::ICMP_NE:
1365    case ICmpInst::ICMP_UGT:
1366      if (isKnownNonZero(LHS, TD))
1367        return ConstantInt::getTrue(LHS->getContext());
1368      break;
1369    case ICmpInst::ICMP_SLT:
1370      ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1371      if (LHSKnownNegative)
1372        return ConstantInt::getTrue(LHS->getContext());
1373      if (LHSKnownNonNegative)
1374        return ConstantInt::getFalse(LHS->getContext());
1375      break;
1376    case ICmpInst::ICMP_SLE:
1377      ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1378      if (LHSKnownNegative)
1379        return ConstantInt::getTrue(LHS->getContext());
1380      if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
1381        return ConstantInt::getFalse(LHS->getContext());
1382      break;
1383    case ICmpInst::ICMP_SGE:
1384      ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1385      if (LHSKnownNegative)
1386        return ConstantInt::getFalse(LHS->getContext());
1387      if (LHSKnownNonNegative)
1388        return ConstantInt::getTrue(LHS->getContext());
1389      break;
1390    case ICmpInst::ICMP_SGT:
1391      ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1392      if (LHSKnownNegative)
1393        return ConstantInt::getFalse(LHS->getContext());
1394      if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
1395        return ConstantInt::getTrue(LHS->getContext());
1396      break;
1397    }
1398  }
1399
1400  // See if we are doing a comparison with a constant integer.
1401  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1402    switch (Pred) {
1403    default: break;
1404    case ICmpInst::ICMP_UGT:
1405      if (CI->isMaxValue(false))                 // A >u MAX -> FALSE
1406        return ConstantInt::getFalse(CI->getContext());
1407      break;
1408    case ICmpInst::ICMP_UGE:
1409      if (CI->isMinValue(false))                 // A >=u MIN -> TRUE
1410        return ConstantInt::getTrue(CI->getContext());
1411      break;
1412    case ICmpInst::ICMP_ULT:
1413      if (CI->isMinValue(false))                 // A <u MIN -> FALSE
1414        return ConstantInt::getFalse(CI->getContext());
1415      break;
1416    case ICmpInst::ICMP_ULE:
1417      if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE
1418        return ConstantInt::getTrue(CI->getContext());
1419      break;
1420    case ICmpInst::ICMP_SGT:
1421      if (CI->isMaxValue(true))                  // A >s MAX -> FALSE
1422        return ConstantInt::getFalse(CI->getContext());
1423      break;
1424    case ICmpInst::ICMP_SGE:
1425      if (CI->isMinValue(true))                  // A >=s MIN -> TRUE
1426        return ConstantInt::getTrue(CI->getContext());
1427      break;
1428    case ICmpInst::ICMP_SLT:
1429      if (CI->isMinValue(true))                  // A <s MIN -> FALSE
1430        return ConstantInt::getFalse(CI->getContext());
1431      break;
1432    case ICmpInst::ICMP_SLE:
1433      if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE
1434        return ConstantInt::getTrue(CI->getContext());
1435      break;
1436    }
1437
1438    // TODO: integer div and rem with constant RHS all produce values in a
1439    // constant range. We should check whether the comparison is a tautology.
1440  }
1441
1442  // Compare of cast, for example (zext X) != 0 -> X != 0
1443  if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
1444    Instruction *LI = cast<CastInst>(LHS);
1445    Value *SrcOp = LI->getOperand(0);
1446    const Type *SrcTy = SrcOp->getType();
1447    const Type *DstTy = LI->getType();
1448
1449    // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
1450    // if the integer type is the same size as the pointer type.
1451    if (MaxRecurse && TD && isa<PtrToIntInst>(LI) &&
1452        TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) {
1453      if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1454        // Transfer the cast to the constant.
1455        if (Value *V = SimplifyICmpInst(Pred, SrcOp,
1456                                        ConstantExpr::getIntToPtr(RHSC, SrcTy),
1457                                        TD, DT, MaxRecurse-1))
1458          return V;
1459      } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
1460        if (RI->getOperand(0)->getType() == SrcTy)
1461          // Compare without the cast.
1462          if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
1463                                          TD, DT, MaxRecurse-1))
1464            return V;
1465      }
1466    }
1467
1468    if (isa<ZExtInst>(LHS)) {
1469      // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
1470      // same type.
1471      if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
1472        if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1473          // Compare X and Y.  Note that signed predicates become unsigned.
1474          if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
1475                                          SrcOp, RI->getOperand(0), TD, DT,
1476                                          MaxRecurse-1))
1477            return V;
1478      }
1479      // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
1480      // too.  If not, then try to deduce the result of the comparison.
1481      else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1482        // Compute the constant that would happen if we truncated to SrcTy then
1483        // reextended to DstTy.
1484        Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1485        Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
1486
1487        // If the re-extended constant didn't change then this is effectively
1488        // also a case of comparing two zero-extended values.
1489        if (RExt == CI && MaxRecurse)
1490          if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
1491                                          SrcOp, Trunc, TD, DT, MaxRecurse-1))
1492            return V;
1493
1494        // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
1495        // there.  Use this to work out the result of the comparison.
1496        if (RExt != CI) {
1497          switch (Pred) {
1498          default:
1499            assert(false && "Unknown ICmp predicate!");
1500          // LHS <u RHS.
1501          case ICmpInst::ICMP_EQ:
1502          case ICmpInst::ICMP_UGT:
1503          case ICmpInst::ICMP_UGE:
1504            return ConstantInt::getFalse(CI->getContext());
1505
1506          case ICmpInst::ICMP_NE:
1507          case ICmpInst::ICMP_ULT:
1508          case ICmpInst::ICMP_ULE:
1509            return ConstantInt::getTrue(CI->getContext());
1510
1511          // LHS is non-negative.  If RHS is negative then LHS >s LHS.  If RHS
1512          // is non-negative then LHS <s RHS.
1513          case ICmpInst::ICMP_SGT:
1514          case ICmpInst::ICMP_SGE:
1515            return CI->getValue().isNegative() ?
1516              ConstantInt::getTrue(CI->getContext()) :
1517              ConstantInt::getFalse(CI->getContext());
1518
1519          case ICmpInst::ICMP_SLT:
1520          case ICmpInst::ICMP_SLE:
1521            return CI->getValue().isNegative() ?
1522              ConstantInt::getFalse(CI->getContext()) :
1523              ConstantInt::getTrue(CI->getContext());
1524          }
1525        }
1526      }
1527    }
1528
1529    if (isa<SExtInst>(LHS)) {
1530      // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
1531      // same type.
1532      if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
1533        if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1534          // Compare X and Y.  Note that the predicate does not change.
1535          if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
1536                                          TD, DT, MaxRecurse-1))
1537            return V;
1538      }
1539      // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
1540      // too.  If not, then try to deduce the result of the comparison.
1541      else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1542        // Compute the constant that would happen if we truncated to SrcTy then
1543        // reextended to DstTy.
1544        Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1545        Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
1546
1547        // If the re-extended constant didn't change then this is effectively
1548        // also a case of comparing two sign-extended values.
1549        if (RExt == CI && MaxRecurse)
1550          if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, DT,
1551                                          MaxRecurse-1))
1552            return V;
1553
1554        // Otherwise the upper bits of LHS are all equal, while RHS has varying
1555        // bits there.  Use this to work out the result of the comparison.
1556        if (RExt != CI) {
1557          switch (Pred) {
1558          default:
1559            assert(false && "Unknown ICmp predicate!");
1560          case ICmpInst::ICMP_EQ:
1561            return ConstantInt::getFalse(CI->getContext());
1562          case ICmpInst::ICMP_NE:
1563            return ConstantInt::getTrue(CI->getContext());
1564
1565          // If RHS is non-negative then LHS <s RHS.  If RHS is negative then
1566          // LHS >s RHS.
1567          case ICmpInst::ICMP_SGT:
1568          case ICmpInst::ICMP_SGE:
1569            return CI->getValue().isNegative() ?
1570              ConstantInt::getTrue(CI->getContext()) :
1571              ConstantInt::getFalse(CI->getContext());
1572          case ICmpInst::ICMP_SLT:
1573          case ICmpInst::ICMP_SLE:
1574            return CI->getValue().isNegative() ?
1575              ConstantInt::getFalse(CI->getContext()) :
1576              ConstantInt::getTrue(CI->getContext());
1577
1578          // If LHS is non-negative then LHS <u RHS.  If LHS is negative then
1579          // LHS >u RHS.
1580          case ICmpInst::ICMP_UGT:
1581          case ICmpInst::ICMP_UGE:
1582            // Comparison is true iff the LHS <s 0.
1583            if (MaxRecurse)
1584              if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
1585                                              Constant::getNullValue(SrcTy),
1586                                              TD, DT, MaxRecurse-1))
1587                return V;
1588            break;
1589          case ICmpInst::ICMP_ULT:
1590          case ICmpInst::ICMP_ULE:
1591            // Comparison is true iff the LHS >=s 0.
1592            if (MaxRecurse)
1593              if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
1594                                              Constant::getNullValue(SrcTy),
1595                                              TD, DT, MaxRecurse-1))
1596                return V;
1597            break;
1598          }
1599        }
1600      }
1601    }
1602  }
1603
1604  // Special logic for binary operators.
1605  BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
1606  BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
1607  if (MaxRecurse && (LBO || RBO)) {
1608    // Analyze the case when either LHS or RHS is an add instruction.
1609    Value *A = 0, *B = 0, *C = 0, *D = 0;
1610    // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
1611    bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
1612    if (LBO && LBO->getOpcode() == Instruction::Add) {
1613      A = LBO->getOperand(0); B = LBO->getOperand(1);
1614      NoLHSWrapProblem = ICmpInst::isEquality(Pred) ||
1615        (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) ||
1616        (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap());
1617    }
1618    if (RBO && RBO->getOpcode() == Instruction::Add) {
1619      C = RBO->getOperand(0); D = RBO->getOperand(1);
1620      NoRHSWrapProblem = ICmpInst::isEquality(Pred) ||
1621        (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) ||
1622        (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap());
1623    }
1624
1625    // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
1626    if ((A == RHS || B == RHS) && NoLHSWrapProblem)
1627      if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
1628                                      Constant::getNullValue(RHS->getType()),
1629                                      TD, DT, MaxRecurse-1))
1630        return V;
1631
1632    // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
1633    if ((C == LHS || D == LHS) && NoRHSWrapProblem)
1634      if (Value *V = SimplifyICmpInst(Pred,
1635                                      Constant::getNullValue(LHS->getType()),
1636                                      C == LHS ? D : C, TD, DT, MaxRecurse-1))
1637        return V;
1638
1639    // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
1640    if (A && C && (A == C || A == D || B == C || B == D) &&
1641        NoLHSWrapProblem && NoRHSWrapProblem) {
1642      // Determine Y and Z in the form icmp (X+Y), (X+Z).
1643      Value *Y = (A == C || A == D) ? B : A;
1644      Value *Z = (C == A || C == B) ? D : C;
1645      if (Value *V = SimplifyICmpInst(Pred, Y, Z, TD, DT, MaxRecurse-1))
1646        return V;
1647    }
1648  }
1649
1650  if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
1651    switch (Pred) {
1652    default:
1653      break;
1654    case ICmpInst::ICMP_EQ:
1655    case ICmpInst::ICMP_UGT:
1656    case ICmpInst::ICMP_UGE:
1657      return ConstantInt::getFalse(RHS->getContext());
1658    case ICmpInst::ICMP_NE:
1659    case ICmpInst::ICMP_ULT:
1660    case ICmpInst::ICMP_ULE:
1661      return ConstantInt::getTrue(RHS->getContext());
1662    }
1663  }
1664
1665  // If the comparison is with the result of a select instruction, check whether
1666  // comparing with either branch of the select always yields the same value.
1667  if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1668    if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
1669      return V;
1670
1671  // If the comparison is with the result of a phi instruction, check whether
1672  // doing the compare with each incoming phi value yields a common result.
1673  if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1674    if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
1675      return V;
1676
1677  return 0;
1678}
1679
1680Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1681                              const TargetData *TD, const DominatorTree *DT) {
1682  return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
1683}
1684
1685/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
1686/// fold the result.  If not, this returns null.
1687static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1688                               const TargetData *TD, const DominatorTree *DT,
1689                               unsigned MaxRecurse) {
1690  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
1691  assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
1692
1693  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
1694    if (Constant *CRHS = dyn_cast<Constant>(RHS))
1695      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
1696
1697    // If we have a constant, make sure it is on the RHS.
1698    std::swap(LHS, RHS);
1699    Pred = CmpInst::getSwappedPredicate(Pred);
1700  }
1701
1702  // Fold trivial predicates.
1703  if (Pred == FCmpInst::FCMP_FALSE)
1704    return ConstantInt::get(GetCompareTy(LHS), 0);
1705  if (Pred == FCmpInst::FCMP_TRUE)
1706    return ConstantInt::get(GetCompareTy(LHS), 1);
1707
1708  if (isa<UndefValue>(RHS))                  // fcmp pred X, undef -> undef
1709    return UndefValue::get(GetCompareTy(LHS));
1710
1711  // fcmp x,x -> true/false.  Not all compares are foldable.
1712  if (LHS == RHS) {
1713    if (CmpInst::isTrueWhenEqual(Pred))
1714      return ConstantInt::get(GetCompareTy(LHS), 1);
1715    if (CmpInst::isFalseWhenEqual(Pred))
1716      return ConstantInt::get(GetCompareTy(LHS), 0);
1717  }
1718
1719  // Handle fcmp with constant RHS
1720  if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1721    // If the constant is a nan, see if we can fold the comparison based on it.
1722    if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
1723      if (CFP->getValueAPF().isNaN()) {
1724        if (FCmpInst::isOrdered(Pred))   // True "if ordered and foo"
1725          return ConstantInt::getFalse(CFP->getContext());
1726        assert(FCmpInst::isUnordered(Pred) &&
1727               "Comparison must be either ordered or unordered!");
1728        // True if unordered.
1729        return ConstantInt::getTrue(CFP->getContext());
1730      }
1731      // Check whether the constant is an infinity.
1732      if (CFP->getValueAPF().isInfinity()) {
1733        if (CFP->getValueAPF().isNegative()) {
1734          switch (Pred) {
1735          case FCmpInst::FCMP_OLT:
1736            // No value is ordered and less than negative infinity.
1737            return ConstantInt::getFalse(CFP->getContext());
1738          case FCmpInst::FCMP_UGE:
1739            // All values are unordered with or at least negative infinity.
1740            return ConstantInt::getTrue(CFP->getContext());
1741          default:
1742            break;
1743          }
1744        } else {
1745          switch (Pred) {
1746          case FCmpInst::FCMP_OGT:
1747            // No value is ordered and greater than infinity.
1748            return ConstantInt::getFalse(CFP->getContext());
1749          case FCmpInst::FCMP_ULE:
1750            // All values are unordered with and at most infinity.
1751            return ConstantInt::getTrue(CFP->getContext());
1752          default:
1753            break;
1754          }
1755        }
1756      }
1757    }
1758  }
1759
1760  // If the comparison is with the result of a select instruction, check whether
1761  // comparing with either branch of the select always yields the same value.
1762  if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1763    if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
1764      return V;
1765
1766  // If the comparison is with the result of a phi instruction, check whether
1767  // doing the compare with each incoming phi value yields a common result.
1768  if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1769    if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
1770      return V;
1771
1772  return 0;
1773}
1774
1775Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1776                              const TargetData *TD, const DominatorTree *DT) {
1777  return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
1778}
1779
1780/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
1781/// the result.  If not, this returns null.
1782Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
1783                                const TargetData *TD, const DominatorTree *) {
1784  // select true, X, Y  -> X
1785  // select false, X, Y -> Y
1786  if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
1787    return CB->getZExtValue() ? TrueVal : FalseVal;
1788
1789  // select C, X, X -> X
1790  if (TrueVal == FalseVal)
1791    return TrueVal;
1792
1793  if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
1794    return FalseVal;
1795  if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
1796    return TrueVal;
1797  if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
1798    if (isa<Constant>(TrueVal))
1799      return TrueVal;
1800    return FalseVal;
1801  }
1802
1803  return 0;
1804}
1805
1806/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
1807/// fold the result.  If not, this returns null.
1808Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
1809                             const TargetData *TD, const DominatorTree *) {
1810  // The type of the GEP pointer operand.
1811  const PointerType *PtrTy = cast<PointerType>(Ops[0]->getType());
1812
1813  // getelementptr P -> P.
1814  if (NumOps == 1)
1815    return Ops[0];
1816
1817  if (isa<UndefValue>(Ops[0])) {
1818    // Compute the (pointer) type returned by the GEP instruction.
1819    const Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, &Ops[1],
1820                                                             NumOps-1);
1821    const Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
1822    return UndefValue::get(GEPTy);
1823  }
1824
1825  if (NumOps == 2) {
1826    // getelementptr P, 0 -> P.
1827    if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
1828      if (C->isZero())
1829        return Ops[0];
1830    // getelementptr P, N -> P if P points to a type of zero size.
1831    if (TD) {
1832      const Type *Ty = PtrTy->getElementType();
1833      if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0)
1834        return Ops[0];
1835    }
1836  }
1837
1838  // Check to see if this is constant foldable.
1839  for (unsigned i = 0; i != NumOps; ++i)
1840    if (!isa<Constant>(Ops[i]))
1841      return 0;
1842
1843  return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
1844                                        (Constant *const*)Ops+1, NumOps-1);
1845}
1846
1847/// SimplifyPHINode - See if we can fold the given phi.  If not, returns null.
1848static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) {
1849  // If all of the PHI's incoming values are the same then replace the PHI node
1850  // with the common value.
1851  Value *CommonValue = 0;
1852  bool HasUndefInput = false;
1853  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1854    Value *Incoming = PN->getIncomingValue(i);
1855    // If the incoming value is the phi node itself, it can safely be skipped.
1856    if (Incoming == PN) continue;
1857    if (isa<UndefValue>(Incoming)) {
1858      // Remember that we saw an undef value, but otherwise ignore them.
1859      HasUndefInput = true;
1860      continue;
1861    }
1862    if (CommonValue && Incoming != CommonValue)
1863      return 0;  // Not the same, bail out.
1864    CommonValue = Incoming;
1865  }
1866
1867  // If CommonValue is null then all of the incoming values were either undef or
1868  // equal to the phi node itself.
1869  if (!CommonValue)
1870    return UndefValue::get(PN->getType());
1871
1872  // If we have a PHI node like phi(X, undef, X), where X is defined by some
1873  // instruction, we cannot return X as the result of the PHI node unless it
1874  // dominates the PHI block.
1875  if (HasUndefInput)
1876    return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0;
1877
1878  return CommonValue;
1879}
1880
1881
1882//=== Helper functions for higher up the class hierarchy.
1883
1884/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
1885/// fold the result.  If not, this returns null.
1886static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
1887                            const TargetData *TD, const DominatorTree *DT,
1888                            unsigned MaxRecurse) {
1889  switch (Opcode) {
1890  case Instruction::Add:
1891    return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
1892                           TD, DT, MaxRecurse);
1893  case Instruction::Sub:
1894    return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
1895                           TD, DT, MaxRecurse);
1896  case Instruction::Mul:  return SimplifyMulInst (LHS, RHS, TD, DT, MaxRecurse);
1897  case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, DT, MaxRecurse);
1898  case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, DT, MaxRecurse);
1899  case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, TD, DT, MaxRecurse);
1900  case Instruction::Shl:
1901    return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
1902                           TD, DT, MaxRecurse);
1903  case Instruction::LShr:
1904    return SimplifyLShrInst(LHS, RHS, /*isExact*/false, TD, DT, MaxRecurse);
1905  case Instruction::AShr:
1906    return SimplifyAShrInst(LHS, RHS, /*isExact*/false, TD, DT, MaxRecurse);
1907  case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, DT, MaxRecurse);
1908  case Instruction::Or:  return SimplifyOrInst (LHS, RHS, TD, DT, MaxRecurse);
1909  case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, DT, MaxRecurse);
1910  default:
1911    if (Constant *CLHS = dyn_cast<Constant>(LHS))
1912      if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
1913        Constant *COps[] = {CLHS, CRHS};
1914        return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
1915      }
1916
1917    // If the operation is associative, try some generic simplifications.
1918    if (Instruction::isAssociative(Opcode))
1919      if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, DT,
1920                                              MaxRecurse))
1921        return V;
1922
1923    // If the operation is with the result of a select instruction, check whether
1924    // operating on either branch of the select always yields the same value.
1925    if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1926      if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, DT,
1927                                           MaxRecurse))
1928        return V;
1929
1930    // If the operation is with the result of a phi instruction, check whether
1931    // operating on all incoming values of the phi always yields the same value.
1932    if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1933      if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, DT, MaxRecurse))
1934        return V;
1935
1936    return 0;
1937  }
1938}
1939
1940Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
1941                           const TargetData *TD, const DominatorTree *DT) {
1942  return ::SimplifyBinOp(Opcode, LHS, RHS, TD, DT, RecursionLimit);
1943}
1944
1945/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
1946/// fold the result.
1947static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1948                              const TargetData *TD, const DominatorTree *DT,
1949                              unsigned MaxRecurse) {
1950  if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
1951    return SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
1952  return SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
1953}
1954
1955Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
1956                             const TargetData *TD, const DominatorTree *DT) {
1957  return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
1958}
1959
1960/// SimplifyInstruction - See if we can compute a simplified version of this
1961/// instruction.  If not, this returns null.
1962Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD,
1963                                 const DominatorTree *DT) {
1964  Value *Result;
1965
1966  switch (I->getOpcode()) {
1967  default:
1968    Result = ConstantFoldInstruction(I, TD);
1969    break;
1970  case Instruction::Add:
1971    Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
1972                             cast<BinaryOperator>(I)->hasNoSignedWrap(),
1973                             cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1974                             TD, DT);
1975    break;
1976  case Instruction::Sub:
1977    Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
1978                             cast<BinaryOperator>(I)->hasNoSignedWrap(),
1979                             cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1980                             TD, DT);
1981    break;
1982  case Instruction::Mul:
1983    Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, DT);
1984    break;
1985  case Instruction::SDiv:
1986    Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, DT);
1987    break;
1988  case Instruction::UDiv:
1989    Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, DT);
1990    break;
1991  case Instruction::FDiv:
1992    Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, DT);
1993    break;
1994  case Instruction::Shl:
1995    Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
1996                             cast<BinaryOperator>(I)->hasNoSignedWrap(),
1997                             cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1998                             TD, DT);
1999    break;
2000  case Instruction::LShr:
2001    Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1),
2002                              cast<BinaryOperator>(I)->isExact(),
2003                              TD, DT);
2004    break;
2005  case Instruction::AShr:
2006    Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1),
2007                              cast<BinaryOperator>(I)->isExact(),
2008                              TD, DT);
2009    break;
2010  case Instruction::And:
2011    Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, DT);
2012    break;
2013  case Instruction::Or:
2014    Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, DT);
2015    break;
2016  case Instruction::Xor:
2017    Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, DT);
2018    break;
2019  case Instruction::ICmp:
2020    Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
2021                              I->getOperand(0), I->getOperand(1), TD, DT);
2022    break;
2023  case Instruction::FCmp:
2024    Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
2025                              I->getOperand(0), I->getOperand(1), TD, DT);
2026    break;
2027  case Instruction::Select:
2028    Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
2029                                I->getOperand(2), TD, DT);
2030    break;
2031  case Instruction::GetElementPtr: {
2032    SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
2033    Result = SimplifyGEPInst(&Ops[0], Ops.size(), TD, DT);
2034    break;
2035  }
2036  case Instruction::PHI:
2037    Result = SimplifyPHINode(cast<PHINode>(I), DT);
2038    break;
2039  }
2040
2041  /// If called on unreachable code, the above logic may report that the
2042  /// instruction simplified to itself.  Make life easier for users by
2043  /// detecting that case here, returning a safe value instead.
2044  return Result == I ? UndefValue::get(I->getType()) : Result;
2045}
2046
2047/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
2048/// delete the From instruction.  In addition to a basic RAUW, this does a
2049/// recursive simplification of the newly formed instructions.  This catches
2050/// things where one simplification exposes other opportunities.  This only
2051/// simplifies and deletes scalar operations, it does not change the CFG.
2052///
2053void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
2054                                     const TargetData *TD,
2055                                     const DominatorTree *DT) {
2056  assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
2057
2058  // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
2059  // we can know if it gets deleted out from under us or replaced in a
2060  // recursive simplification.
2061  WeakVH FromHandle(From);
2062  WeakVH ToHandle(To);
2063
2064  while (!From->use_empty()) {
2065    // Update the instruction to use the new value.
2066    Use &TheUse = From->use_begin().getUse();
2067    Instruction *User = cast<Instruction>(TheUse.getUser());
2068    TheUse = To;
2069
2070    // Check to see if the instruction can be folded due to the operand
2071    // replacement.  For example changing (or X, Y) into (or X, -1) can replace
2072    // the 'or' with -1.
2073    Value *SimplifiedVal;
2074    {
2075      // Sanity check to make sure 'User' doesn't dangle across
2076      // SimplifyInstruction.
2077      AssertingVH<> UserHandle(User);
2078
2079      SimplifiedVal = SimplifyInstruction(User, TD, DT);
2080      if (SimplifiedVal == 0) continue;
2081    }
2082
2083    // Recursively simplify this user to the new value.
2084    ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT);
2085    From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
2086    To = ToHandle;
2087
2088    assert(ToHandle && "To value deleted by recursive simplification?");
2089
2090    // If the recursive simplification ended up revisiting and deleting
2091    // 'From' then we're done.
2092    if (From == 0)
2093      return;
2094  }
2095
2096  // If 'From' has value handles referring to it, do a real RAUW to update them.
2097  From->replaceAllUsesWith(To);
2098
2099  From->eraseFromParent();
2100}
2101