InstructionCombining.cpp revision 8d9b8d717e665945b31b0742b901561fb433cece
1//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
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// InstructionCombining - Combine instructions to form fewer, simple
11// instructions.  This pass does not modify the CFG.  This pass is where
12// algebraic simplification happens.
13//
14// This pass combines things like:
15//    %Y = add i32 %X, 1
16//    %Z = add i32 %Y, 1
17// into:
18//    %Z = add i32 %X, 2
19//
20// This is a simple worklist driven algorithm.
21//
22// This pass guarantees that the following canonicalizations are performed on
23// the program:
24//    1. If a binary operator has a constant operand, it is moved to the RHS
25//    2. Bitwise operators with constant operands are always grouped so that
26//       shifts are performed first, then or's, then and's, then xor's.
27//    3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28//    4. All cmp instructions on boolean values are replaced with logical ops
29//    5. add X, X is represented as (X*2) => (X << 1)
30//    6. Multiplies with a power-of-two constant argument are transformed into
31//       shifts.
32//   ... etc.
33//
34//===----------------------------------------------------------------------===//
35
36#define DEBUG_TYPE "instcombine"
37#include "llvm/Transforms/Scalar.h"
38#include "InstCombine.h"
39#include "llvm/IntrinsicInst.h"
40#include "llvm/LLVMContext.h"
41#include "llvm/DerivedTypes.h"
42#include "llvm/GlobalVariable.h"
43#include "llvm/Operator.h"
44#include "llvm/Analysis/ConstantFolding.h"
45#include "llvm/Analysis/InstructionSimplify.h"
46#include "llvm/Analysis/MemoryBuiltins.h"
47#include "llvm/Target/TargetData.h"
48#include "llvm/Transforms/Utils/BasicBlockUtils.h"
49#include "llvm/Transforms/Utils/Local.h"
50#include "llvm/Support/CallSite.h"
51#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
53#include "llvm/Support/GetElementPtrTypeIterator.h"
54#include "llvm/Support/MathExtras.h"
55#include "llvm/Support/PatternMatch.h"
56#include "llvm/ADT/SmallPtrSet.h"
57#include "llvm/ADT/Statistic.h"
58#include "llvm/ADT/STLExtras.h"
59#include <algorithm>
60#include <climits>
61using namespace llvm;
62using namespace llvm::PatternMatch;
63
64STATISTIC(NumCombined , "Number of insts combined");
65STATISTIC(NumConstProp, "Number of constant folds");
66STATISTIC(NumDeadInst , "Number of dead inst eliminated");
67STATISTIC(NumSunkInst , "Number of instructions sunk");
68
69
70char InstCombiner::ID = 0;
71static RegisterPass<InstCombiner>
72X("instcombine", "Combine redundant instructions");
73
74void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
75  AU.addPreservedID(LCSSAID);
76  AU.setPreservesCFG();
77}
78
79
80// isOnlyUse - Return true if this instruction will be deleted if we stop using
81// it.
82static bool isOnlyUse(Value *V) {
83  return V->hasOneUse() || isa<Constant>(V);
84}
85
86// getPromotedType - Return the specified type promoted as it would be to pass
87// though a va_arg area...
88static const Type *getPromotedType(const Type *Ty) {
89  if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
90    if (ITy->getBitWidth() < 32)
91      return Type::getInt32Ty(Ty->getContext());
92  }
93  return Ty;
94}
95
96/// ShouldChangeType - Return true if it is desirable to convert a computation
97/// from 'From' to 'To'.  We don't want to convert from a legal to an illegal
98/// type for example, or from a smaller to a larger illegal type.
99bool InstCombiner::ShouldChangeType(const Type *From, const Type *To) const {
100  assert(isa<IntegerType>(From) && isa<IntegerType>(To));
101
102  // If we don't have TD, we don't know if the source/dest are legal.
103  if (!TD) return false;
104
105  unsigned FromWidth = From->getPrimitiveSizeInBits();
106  unsigned ToWidth = To->getPrimitiveSizeInBits();
107  bool FromLegal = TD->isLegalInteger(FromWidth);
108  bool ToLegal = TD->isLegalInteger(ToWidth);
109
110  // If this is a legal integer from type, and the result would be an illegal
111  // type, don't do the transformation.
112  if (FromLegal && !ToLegal)
113    return false;
114
115  // Otherwise, if both are illegal, do not increase the size of the result. We
116  // do allow things like i160 -> i64, but not i64 -> i160.
117  if (!FromLegal && !ToLegal && ToWidth > FromWidth)
118    return false;
119
120  return true;
121}
122
123/// getBitCastOperand - If the specified operand is a CastInst, a constant
124/// expression bitcast, or a GetElementPtrInst with all zero indices, return the
125/// operand value, otherwise return null.
126static Value *getBitCastOperand(Value *V) {
127  if (Operator *O = dyn_cast<Operator>(V)) {
128    if (O->getOpcode() == Instruction::BitCast)
129      return O->getOperand(0);
130    if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
131      if (GEP->hasAllZeroIndices())
132        return GEP->getPointerOperand();
133  }
134  return 0;
135}
136
137
138
139// SimplifyCommutative - This performs a few simplifications for commutative
140// operators:
141//
142//  1. Order operands such that they are listed from right (least complex) to
143//     left (most complex).  This puts constants before unary operators before
144//     binary operators.
145//
146//  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
147//  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
148//
149bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
150  bool Changed = false;
151  if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
152    Changed = !I.swapOperands();
153
154  if (!I.isAssociative()) return Changed;
155  Instruction::BinaryOps Opcode = I.getOpcode();
156  if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
157    if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
158      if (isa<Constant>(I.getOperand(1))) {
159        Constant *Folded = ConstantExpr::get(I.getOpcode(),
160                                             cast<Constant>(I.getOperand(1)),
161                                             cast<Constant>(Op->getOperand(1)));
162        I.setOperand(0, Op->getOperand(0));
163        I.setOperand(1, Folded);
164        return true;
165      } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
166        if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
167            isOnlyUse(Op) && isOnlyUse(Op1)) {
168          Constant *C1 = cast<Constant>(Op->getOperand(1));
169          Constant *C2 = cast<Constant>(Op1->getOperand(1));
170
171          // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
172          Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
173          Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
174                                                    Op1->getOperand(0),
175                                                    Op1->getName(), &I);
176          Worklist.Add(New);
177          I.setOperand(0, New);
178          I.setOperand(1, Folded);
179          return true;
180        }
181    }
182  return Changed;
183}
184
185// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
186// if the LHS is a constant zero (which is the 'negate' form).
187//
188Value *InstCombiner::dyn_castNegVal(Value *V) const {
189  if (BinaryOperator::isNeg(V))
190    return BinaryOperator::getNegArgument(V);
191
192  // Constants can be considered to be negated values if they can be folded.
193  if (ConstantInt *C = dyn_cast<ConstantInt>(V))
194    return ConstantExpr::getNeg(C);
195
196  if (ConstantVector *C = dyn_cast<ConstantVector>(V))
197    if (C->getType()->getElementType()->isInteger())
198      return ConstantExpr::getNeg(C);
199
200  return 0;
201}
202
203// dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
204// instruction if the LHS is a constant negative zero (which is the 'negate'
205// form).
206//
207static inline Value *dyn_castFNegVal(Value *V) {
208  if (BinaryOperator::isFNeg(V))
209    return BinaryOperator::getFNegArgument(V);
210
211  // Constants can be considered to be negated values if they can be folded.
212  if (ConstantFP *C = dyn_cast<ConstantFP>(V))
213    return ConstantExpr::getFNeg(C);
214
215  if (ConstantVector *C = dyn_cast<ConstantVector>(V))
216    if (C->getType()->getElementType()->isFloatingPoint())
217      return ConstantExpr::getFNeg(C);
218
219  return 0;
220}
221
222/// MatchSelectPattern - Pattern match integer [SU]MIN, [SU]MAX, and ABS idioms,
223/// returning the kind and providing the out parameter results if we
224/// successfully match.
225static SelectPatternFlavor
226MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS) {
227  SelectInst *SI = dyn_cast<SelectInst>(V);
228  if (SI == 0) return SPF_UNKNOWN;
229
230  ICmpInst *ICI = dyn_cast<ICmpInst>(SI->getCondition());
231  if (ICI == 0) return SPF_UNKNOWN;
232
233  LHS = ICI->getOperand(0);
234  RHS = ICI->getOperand(1);
235
236  // (icmp X, Y) ? X : Y
237  if (SI->getTrueValue() == ICI->getOperand(0) &&
238      SI->getFalseValue() == ICI->getOperand(1)) {
239    switch (ICI->getPredicate()) {
240    default: return SPF_UNKNOWN; // Equality.
241    case ICmpInst::ICMP_UGT:
242    case ICmpInst::ICMP_UGE: return SPF_UMAX;
243    case ICmpInst::ICMP_SGT:
244    case ICmpInst::ICMP_SGE: return SPF_SMAX;
245    case ICmpInst::ICMP_ULT:
246    case ICmpInst::ICMP_ULE: return SPF_UMIN;
247    case ICmpInst::ICMP_SLT:
248    case ICmpInst::ICMP_SLE: return SPF_SMIN;
249    }
250  }
251
252  // (icmp X, Y) ? Y : X
253  if (SI->getTrueValue() == ICI->getOperand(1) &&
254      SI->getFalseValue() == ICI->getOperand(0)) {
255    switch (ICI->getPredicate()) {
256      default: return SPF_UNKNOWN; // Equality.
257      case ICmpInst::ICMP_UGT:
258      case ICmpInst::ICMP_UGE: return SPF_UMIN;
259      case ICmpInst::ICMP_SGT:
260      case ICmpInst::ICMP_SGE: return SPF_SMIN;
261      case ICmpInst::ICMP_ULT:
262      case ICmpInst::ICMP_ULE: return SPF_UMAX;
263      case ICmpInst::ICMP_SLT:
264      case ICmpInst::ICMP_SLE: return SPF_SMAX;
265    }
266  }
267
268  // TODO: (X > 4) ? X : 5   -->  (X >= 5) ? X : 5  -->  MAX(X, 5)
269
270  return SPF_UNKNOWN;
271}
272
273/// isFreeToInvert - Return true if the specified value is free to invert (apply
274/// ~ to).  This happens in cases where the ~ can be eliminated.
275static inline bool isFreeToInvert(Value *V) {
276  // ~(~(X)) -> X.
277  if (BinaryOperator::isNot(V))
278    return true;
279
280  // Constants can be considered to be not'ed values.
281  if (isa<ConstantInt>(V))
282    return true;
283
284  // Compares can be inverted if they have a single use.
285  if (CmpInst *CI = dyn_cast<CmpInst>(V))
286    return CI->hasOneUse();
287
288  return false;
289}
290
291static inline Value *dyn_castNotVal(Value *V) {
292  // If this is not(not(x)) don't return that this is a not: we want the two
293  // not's to be folded first.
294  if (BinaryOperator::isNot(V)) {
295    Value *Operand = BinaryOperator::getNotArgument(V);
296    if (!isFreeToInvert(Operand))
297      return Operand;
298  }
299
300  // Constants can be considered to be not'ed values...
301  if (ConstantInt *C = dyn_cast<ConstantInt>(V))
302    return ConstantInt::get(C->getType(), ~C->getValue());
303  return 0;
304}
305
306
307
308// dyn_castFoldableMul - If this value is a multiply that can be folded into
309// other computations (because it has a constant operand), return the
310// non-constant operand of the multiply, and set CST to point to the multiplier.
311// Otherwise, return null.
312//
313static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
314  if (V->hasOneUse() && V->getType()->isInteger())
315    if (Instruction *I = dyn_cast<Instruction>(V)) {
316      if (I->getOpcode() == Instruction::Mul)
317        if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
318          return I->getOperand(0);
319      if (I->getOpcode() == Instruction::Shl)
320        if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
321          // The multiplier is really 1 << CST.
322          uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
323          uint32_t CSTVal = CST->getLimitedValue(BitWidth);
324          CST = ConstantInt::get(V->getType()->getContext(),
325                                 APInt(BitWidth, 1).shl(CSTVal));
326          return I->getOperand(0);
327        }
328    }
329  return 0;
330}
331
332/// AddOne - Add one to a ConstantInt
333static Constant *AddOne(Constant *C) {
334  return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
335}
336/// SubOne - Subtract one from a ConstantInt
337static Constant *SubOne(ConstantInt *C) {
338  return ConstantExpr::getSub(C,  ConstantInt::get(C->getType(), 1));
339}
340/// MultiplyOverflows - True if the multiply can not be expressed in an int
341/// this size.
342static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
343  uint32_t W = C1->getBitWidth();
344  APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
345  if (sign) {
346    LHSExt.sext(W * 2);
347    RHSExt.sext(W * 2);
348  } else {
349    LHSExt.zext(W * 2);
350    RHSExt.zext(W * 2);
351  }
352
353  APInt MulExt = LHSExt * RHSExt;
354
355  if (!sign)
356    return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
357
358  APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
359  APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
360  return MulExt.slt(Min) || MulExt.sgt(Max);
361}
362
363
364
365/// AssociativeOpt - Perform an optimization on an associative operator.  This
366/// function is designed to check a chain of associative operators for a
367/// potential to apply a certain optimization.  Since the optimization may be
368/// applicable if the expression was reassociated, this checks the chain, then
369/// reassociates the expression as necessary to expose the optimization
370/// opportunity.  This makes use of a special Functor, which must define
371/// 'shouldApply' and 'apply' methods.
372///
373template<typename Functor>
374static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
375  unsigned Opcode = Root.getOpcode();
376  Value *LHS = Root.getOperand(0);
377
378  // Quick check, see if the immediate LHS matches...
379  if (F.shouldApply(LHS))
380    return F.apply(Root);
381
382  // Otherwise, if the LHS is not of the same opcode as the root, return.
383  Instruction *LHSI = dyn_cast<Instruction>(LHS);
384  while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
385    // Should we apply this transform to the RHS?
386    bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
387
388    // If not to the RHS, check to see if we should apply to the LHS...
389    if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
390      cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS
391      ShouldApply = true;
392    }
393
394    // If the functor wants to apply the optimization to the RHS of LHSI,
395    // reassociate the expression from ((? op A) op B) to (? op (A op B))
396    if (ShouldApply) {
397      // Now all of the instructions are in the current basic block, go ahead
398      // and perform the reassociation.
399      Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
400
401      // First move the selected RHS to the LHS of the root...
402      Root.setOperand(0, LHSI->getOperand(1));
403
404      // Make what used to be the LHS of the root be the user of the root...
405      Value *ExtraOperand = TmpLHSI->getOperand(1);
406      if (&Root == TmpLHSI) {
407        Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
408        return 0;
409      }
410      Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI
411      TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root
412      BasicBlock::iterator ARI = &Root; ++ARI;
413      TmpLHSI->moveBefore(ARI);                  // Move TmpLHSI to after Root
414      ARI = Root;
415
416      // Now propagate the ExtraOperand down the chain of instructions until we
417      // get to LHSI.
418      while (TmpLHSI != LHSI) {
419        Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
420        // Move the instruction to immediately before the chain we are
421        // constructing to avoid breaking dominance properties.
422        NextLHSI->moveBefore(ARI);
423        ARI = NextLHSI;
424
425        Value *NextOp = NextLHSI->getOperand(1);
426        NextLHSI->setOperand(1, ExtraOperand);
427        TmpLHSI = NextLHSI;
428        ExtraOperand = NextOp;
429      }
430
431      // Now that the instructions are reassociated, have the functor perform
432      // the transformation...
433      return F.apply(Root);
434    }
435
436    LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
437  }
438  return 0;
439}
440
441namespace {
442
443// AddRHS - Implements: X + X --> X << 1
444struct AddRHS {
445  Value *RHS;
446  explicit AddRHS(Value *rhs) : RHS(rhs) {}
447  bool shouldApply(Value *LHS) const { return LHS == RHS; }
448  Instruction *apply(BinaryOperator &Add) const {
449    return BinaryOperator::CreateShl(Add.getOperand(0),
450                                     ConstantInt::get(Add.getType(), 1));
451  }
452};
453
454// AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
455//                 iff C1&C2 == 0
456struct AddMaskingAnd {
457  Constant *C2;
458  explicit AddMaskingAnd(Constant *c) : C2(c) {}
459  bool shouldApply(Value *LHS) const {
460    ConstantInt *C1;
461    return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
462           ConstantExpr::getAnd(C1, C2)->isNullValue();
463  }
464  Instruction *apply(BinaryOperator &Add) const {
465    return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1));
466  }
467};
468
469}
470
471static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
472                                             InstCombiner *IC) {
473  if (CastInst *CI = dyn_cast<CastInst>(&I))
474    return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType());
475
476  // Figure out if the constant is the left or the right argument.
477  bool ConstIsRHS = isa<Constant>(I.getOperand(1));
478  Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
479
480  if (Constant *SOC = dyn_cast<Constant>(SO)) {
481    if (ConstIsRHS)
482      return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
483    return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
484  }
485
486  Value *Op0 = SO, *Op1 = ConstOperand;
487  if (!ConstIsRHS)
488    std::swap(Op0, Op1);
489
490  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
491    return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1,
492                                    SO->getName()+".op");
493  if (ICmpInst *CI = dyn_cast<ICmpInst>(&I))
494    return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
495                                   SO->getName()+".cmp");
496  if (FCmpInst *CI = dyn_cast<FCmpInst>(&I))
497    return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
498                                   SO->getName()+".cmp");
499  llvm_unreachable("Unknown binary instruction type!");
500}
501
502// FoldOpIntoSelect - Given an instruction with a select as one operand and a
503// constant as the other operand, try to fold the binary operator into the
504// select arguments.  This also works for Cast instructions, which obviously do
505// not have a second operand.
506Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
507  // Don't modify shared select instructions
508  if (!SI->hasOneUse()) return 0;
509  Value *TV = SI->getOperand(1);
510  Value *FV = SI->getOperand(2);
511
512  if (isa<Constant>(TV) || isa<Constant>(FV)) {
513    // Bool selects with constant operands can be folded to logical ops.
514    if (SI->getType() == Type::getInt1Ty(SI->getContext())) return 0;
515
516    Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
517    Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
518
519    return SelectInst::Create(SI->getCondition(), SelectTrueVal,
520                              SelectFalseVal);
521  }
522  return 0;
523}
524
525
526/// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which
527/// has a PHI node as operand #0, see if we can fold the instruction into the
528/// PHI (which is only possible if all operands to the PHI are constants).
529///
530/// If AllowAggressive is true, FoldOpIntoPhi will allow certain transforms
531/// that would normally be unprofitable because they strongly encourage jump
532/// threading.
533Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I,
534                                         bool AllowAggressive) {
535  AllowAggressive = false;
536  PHINode *PN = cast<PHINode>(I.getOperand(0));
537  unsigned NumPHIValues = PN->getNumIncomingValues();
538  if (NumPHIValues == 0 ||
539      // We normally only transform phis with a single use, unless we're trying
540      // hard to make jump threading happen.
541      (!PN->hasOneUse() && !AllowAggressive))
542    return 0;
543
544
545  // Check to see if all of the operands of the PHI are simple constants
546  // (constantint/constantfp/undef).  If there is one non-constant value,
547  // remember the BB it is in.  If there is more than one or if *it* is a PHI,
548  // bail out.  We don't do arbitrary constant expressions here because moving
549  // their computation can be expensive without a cost model.
550  BasicBlock *NonConstBB = 0;
551  for (unsigned i = 0; i != NumPHIValues; ++i)
552    if (!isa<Constant>(PN->getIncomingValue(i)) ||
553        isa<ConstantExpr>(PN->getIncomingValue(i))) {
554      if (NonConstBB) return 0;  // More than one non-const value.
555      if (isa<PHINode>(PN->getIncomingValue(i))) return 0;  // Itself a phi.
556      NonConstBB = PN->getIncomingBlock(i);
557
558      // If the incoming non-constant value is in I's block, we have an infinite
559      // loop.
560      if (NonConstBB == I.getParent())
561        return 0;
562    }
563
564  // If there is exactly one non-constant value, we can insert a copy of the
565  // operation in that block.  However, if this is a critical edge, we would be
566  // inserting the computation one some other paths (e.g. inside a loop).  Only
567  // do this if the pred block is unconditionally branching into the phi block.
568  if (NonConstBB != 0 && !AllowAggressive) {
569    BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
570    if (!BI || !BI->isUnconditional()) return 0;
571  }
572
573  // Okay, we can do the transformation: create the new PHI node.
574  PHINode *NewPN = PHINode::Create(I.getType(), "");
575  NewPN->reserveOperandSpace(PN->getNumOperands()/2);
576  InsertNewInstBefore(NewPN, *PN);
577  NewPN->takeName(PN);
578
579  // Next, add all of the operands to the PHI.
580  if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
581    // We only currently try to fold the condition of a select when it is a phi,
582    // not the true/false values.
583    Value *TrueV = SI->getTrueValue();
584    Value *FalseV = SI->getFalseValue();
585    BasicBlock *PhiTransBB = PN->getParent();
586    for (unsigned i = 0; i != NumPHIValues; ++i) {
587      BasicBlock *ThisBB = PN->getIncomingBlock(i);
588      Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
589      Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
590      Value *InV = 0;
591      if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
592        InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
593      } else {
594        assert(PN->getIncomingBlock(i) == NonConstBB);
595        InV = SelectInst::Create(PN->getIncomingValue(i), TrueVInPred,
596                                 FalseVInPred,
597                                 "phitmp", NonConstBB->getTerminator());
598        Worklist.Add(cast<Instruction>(InV));
599      }
600      NewPN->addIncoming(InV, ThisBB);
601    }
602  } else if (I.getNumOperands() == 2) {
603    Constant *C = cast<Constant>(I.getOperand(1));
604    for (unsigned i = 0; i != NumPHIValues; ++i) {
605      Value *InV = 0;
606      if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
607        if (CmpInst *CI = dyn_cast<CmpInst>(&I))
608          InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
609        else
610          InV = ConstantExpr::get(I.getOpcode(), InC, C);
611      } else {
612        assert(PN->getIncomingBlock(i) == NonConstBB);
613        if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
614          InV = BinaryOperator::Create(BO->getOpcode(),
615                                       PN->getIncomingValue(i), C, "phitmp",
616                                       NonConstBB->getTerminator());
617        else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
618          InV = CmpInst::Create(CI->getOpcode(),
619                                CI->getPredicate(),
620                                PN->getIncomingValue(i), C, "phitmp",
621                                NonConstBB->getTerminator());
622        else
623          llvm_unreachable("Unknown binop!");
624
625        Worklist.Add(cast<Instruction>(InV));
626      }
627      NewPN->addIncoming(InV, PN->getIncomingBlock(i));
628    }
629  } else {
630    CastInst *CI = cast<CastInst>(&I);
631    const Type *RetTy = CI->getType();
632    for (unsigned i = 0; i != NumPHIValues; ++i) {
633      Value *InV;
634      if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
635        InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
636      } else {
637        assert(PN->getIncomingBlock(i) == NonConstBB);
638        InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i),
639                               I.getType(), "phitmp",
640                               NonConstBB->getTerminator());
641        Worklist.Add(cast<Instruction>(InV));
642      }
643      NewPN->addIncoming(InV, PN->getIncomingBlock(i));
644    }
645  }
646  return ReplaceInstUsesWith(I, NewPN);
647}
648
649
650/// WillNotOverflowSignedAdd - Return true if we can prove that:
651///    (sext (add LHS, RHS))  === (add (sext LHS), (sext RHS))
652/// This basically requires proving that the add in the original type would not
653/// overflow to change the sign bit or have a carry out.
654bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
655  // There are different heuristics we can use for this.  Here are some simple
656  // ones.
657
658  // Add has the property that adding any two 2's complement numbers can only
659  // have one carry bit which can change a sign.  As such, if LHS and RHS each
660  // have at least two sign bits, we know that the addition of the two values
661  // will sign extend fine.
662  if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
663    return true;
664
665
666  // If one of the operands only has one non-zero bit, and if the other operand
667  // has a known-zero bit in a more significant place than it (not including the
668  // sign bit) the ripple may go up to and fill the zero, but won't change the
669  // sign.  For example, (X & ~4) + 1.
670
671  // TODO: Implement.
672
673  return false;
674}
675
676
677Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
678  bool Changed = SimplifyCommutative(I);
679  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
680
681  if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
682                                 I.hasNoUnsignedWrap(), TD))
683    return ReplaceInstUsesWith(I, V);
684
685
686  if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
687    if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
688      // X + (signbit) --> X ^ signbit
689      const APInt& Val = CI->getValue();
690      uint32_t BitWidth = Val.getBitWidth();
691      if (Val == APInt::getSignBit(BitWidth))
692        return BinaryOperator::CreateXor(LHS, RHS);
693
694      // See if SimplifyDemandedBits can simplify this.  This handles stuff like
695      // (X & 254)+1 -> (X&254)|1
696      if (SimplifyDemandedInstructionBits(I))
697        return &I;
698
699      // zext(bool) + C -> bool ? C + 1 : C
700      if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
701        if (ZI->getSrcTy() == Type::getInt1Ty(I.getContext()))
702          return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
703    }
704
705    if (isa<PHINode>(LHS))
706      if (Instruction *NV = FoldOpIntoPhi(I))
707        return NV;
708
709    ConstantInt *XorRHS = 0;
710    Value *XorLHS = 0;
711    if (isa<ConstantInt>(RHSC) &&
712        match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
713      uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
714      const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
715
716      uint32_t Size = TySizeBits / 2;
717      APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1));
718      APInt CFF80Val(-C0080Val);
719      do {
720        if (TySizeBits > Size) {
721          // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
722          // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
723          if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
724              (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
725            // This is a sign extend if the top bits are known zero.
726            if (!MaskedValueIsZero(XorLHS,
727                   APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
728              Size = 0;  // Not a sign ext, but can't be any others either.
729            break;
730          }
731        }
732        Size >>= 1;
733        C0080Val = APIntOps::lshr(C0080Val, Size);
734        CFF80Val = APIntOps::ashr(CFF80Val, Size);
735      } while (Size >= 1);
736
737      // FIXME: This shouldn't be necessary. When the backends can handle types
738      // with funny bit widths then this switch statement should be removed. It
739      // is just here to get the size of the "middle" type back up to something
740      // that the back ends can handle.
741      const Type *MiddleType = 0;
742      switch (Size) {
743        default: break;
744        case 32:
745        case 16:
746        case  8: MiddleType = IntegerType::get(I.getContext(), Size); break;
747      }
748      if (MiddleType) {
749        Value *NewTrunc = Builder->CreateTrunc(XorLHS, MiddleType, "sext");
750        return new SExtInst(NewTrunc, I.getType(), I.getName());
751      }
752    }
753  }
754
755  if (I.getType() == Type::getInt1Ty(I.getContext()))
756    return BinaryOperator::CreateXor(LHS, RHS);
757
758  // X + X --> X << 1
759  if (I.getType()->isInteger()) {
760    if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS)))
761      return Result;
762
763    if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
764      if (RHSI->getOpcode() == Instruction::Sub)
765        if (LHS == RHSI->getOperand(1))                   // A + (B - A) --> B
766          return ReplaceInstUsesWith(I, RHSI->getOperand(0));
767    }
768    if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
769      if (LHSI->getOpcode() == Instruction::Sub)
770        if (RHS == LHSI->getOperand(1))                   // (B - A) + A --> B
771          return ReplaceInstUsesWith(I, LHSI->getOperand(0));
772    }
773  }
774
775  // -A + B  -->  B - A
776  // -A + -B  -->  -(A + B)
777  if (Value *LHSV = dyn_castNegVal(LHS)) {
778    if (LHS->getType()->isIntOrIntVector()) {
779      if (Value *RHSV = dyn_castNegVal(RHS)) {
780        Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
781        return BinaryOperator::CreateNeg(NewAdd);
782      }
783    }
784
785    return BinaryOperator::CreateSub(RHS, LHSV);
786  }
787
788  // A + -B  -->  A - B
789  if (!isa<Constant>(RHS))
790    if (Value *V = dyn_castNegVal(RHS))
791      return BinaryOperator::CreateSub(LHS, V);
792
793
794  ConstantInt *C2;
795  if (Value *X = dyn_castFoldableMul(LHS, C2)) {
796    if (X == RHS)   // X*C + X --> X * (C+1)
797      return BinaryOperator::CreateMul(RHS, AddOne(C2));
798
799    // X*C1 + X*C2 --> X * (C1+C2)
800    ConstantInt *C1;
801    if (X == dyn_castFoldableMul(RHS, C1))
802      return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
803  }
804
805  // X + X*C --> X * (C+1)
806  if (dyn_castFoldableMul(RHS, C2) == LHS)
807    return BinaryOperator::CreateMul(LHS, AddOne(C2));
808
809  // X + ~X --> -1   since   ~X = -X-1
810  if (dyn_castNotVal(LHS) == RHS ||
811      dyn_castNotVal(RHS) == LHS)
812    return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
813
814
815  // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
816  if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
817    if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
818      return R;
819
820  // A+B --> A|B iff A and B have no bits set in common.
821  if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
822    APInt Mask = APInt::getAllOnesValue(IT->getBitWidth());
823    APInt LHSKnownOne(IT->getBitWidth(), 0);
824    APInt LHSKnownZero(IT->getBitWidth(), 0);
825    ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
826    if (LHSKnownZero != 0) {
827      APInt RHSKnownOne(IT->getBitWidth(), 0);
828      APInt RHSKnownZero(IT->getBitWidth(), 0);
829      ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
830
831      // No bits in common -> bitwise or.
832      if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
833        return BinaryOperator::CreateOr(LHS, RHS);
834    }
835  }
836
837  // W*X + Y*Z --> W * (X+Z)  iff W == Y
838  if (I.getType()->isIntOrIntVector()) {
839    Value *W, *X, *Y, *Z;
840    if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
841        match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
842      if (W != Y) {
843        if (W == Z) {
844          std::swap(Y, Z);
845        } else if (Y == X) {
846          std::swap(W, X);
847        } else if (X == Z) {
848          std::swap(Y, Z);
849          std::swap(W, X);
850        }
851      }
852
853      if (W == Y) {
854        Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName());
855        return BinaryOperator::CreateMul(W, NewAdd);
856      }
857    }
858  }
859
860  if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
861    Value *X = 0;
862    if (match(LHS, m_Not(m_Value(X))))    // ~X + C --> (C-1) - X
863      return BinaryOperator::CreateSub(SubOne(CRHS), X);
864
865    // (X & FF00) + xx00  -> (X+xx00) & FF00
866    if (LHS->hasOneUse() &&
867        match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
868      Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
869      if (Anded == CRHS) {
870        // See if all bits from the first bit set in the Add RHS up are included
871        // in the mask.  First, get the rightmost bit.
872        const APInt& AddRHSV = CRHS->getValue();
873
874        // Form a mask of all bits from the lowest bit added through the top.
875        APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
876
877        // See if the and mask includes all of these bits.
878        APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
879
880        if (AddRHSHighBits == AddRHSHighBitsAnd) {
881          // Okay, the xform is safe.  Insert the new add pronto.
882          Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
883          return BinaryOperator::CreateAnd(NewAdd, C2);
884        }
885      }
886    }
887
888    // Try to fold constant add into select arguments.
889    if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
890      if (Instruction *R = FoldOpIntoSelect(I, SI))
891        return R;
892  }
893
894  // add (select X 0 (sub n A)) A  -->  select X A n
895  {
896    SelectInst *SI = dyn_cast<SelectInst>(LHS);
897    Value *A = RHS;
898    if (!SI) {
899      SI = dyn_cast<SelectInst>(RHS);
900      A = LHS;
901    }
902    if (SI && SI->hasOneUse()) {
903      Value *TV = SI->getTrueValue();
904      Value *FV = SI->getFalseValue();
905      Value *N;
906
907      // Can we fold the add into the argument of the select?
908      // We check both true and false select arguments for a matching subtract.
909      if (match(FV, m_Zero()) &&
910          match(TV, m_Sub(m_Value(N), m_Specific(A))))
911        // Fold the add into the true select value.
912        return SelectInst::Create(SI->getCondition(), N, A);
913      if (match(TV, m_Zero()) &&
914          match(FV, m_Sub(m_Value(N), m_Specific(A))))
915        // Fold the add into the false select value.
916        return SelectInst::Create(SI->getCondition(), A, N);
917    }
918  }
919
920  // Check for (add (sext x), y), see if we can merge this into an
921  // integer add followed by a sext.
922  if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
923    // (add (sext x), cst) --> (sext (add x, cst'))
924    if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
925      Constant *CI =
926        ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
927      if (LHSConv->hasOneUse() &&
928          ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
929          WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
930        // Insert the new, smaller add.
931        Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
932                                              CI, "addconv");
933        return new SExtInst(NewAdd, I.getType());
934      }
935    }
936
937    // (add (sext x), (sext y)) --> (sext (add int x, y))
938    if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
939      // Only do this if x/y have the same type, if at last one of them has a
940      // single use (so we don't increase the number of sexts), and if the
941      // integer add will not overflow.
942      if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
943          (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
944          WillNotOverflowSignedAdd(LHSConv->getOperand(0),
945                                   RHSConv->getOperand(0))) {
946        // Insert the new integer add.
947        Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
948                                              RHSConv->getOperand(0), "addconv");
949        return new SExtInst(NewAdd, I.getType());
950      }
951    }
952  }
953
954  return Changed ? &I : 0;
955}
956
957Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
958  bool Changed = SimplifyCommutative(I);
959  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
960
961  if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
962    // X + 0 --> X
963    if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
964      if (CFP->isExactlyValue(ConstantFP::getNegativeZero
965                              (I.getType())->getValueAPF()))
966        return ReplaceInstUsesWith(I, LHS);
967    }
968
969    if (isa<PHINode>(LHS))
970      if (Instruction *NV = FoldOpIntoPhi(I))
971        return NV;
972  }
973
974  // -A + B  -->  B - A
975  // -A + -B  -->  -(A + B)
976  if (Value *LHSV = dyn_castFNegVal(LHS))
977    return BinaryOperator::CreateFSub(RHS, LHSV);
978
979  // A + -B  -->  A - B
980  if (!isa<Constant>(RHS))
981    if (Value *V = dyn_castFNegVal(RHS))
982      return BinaryOperator::CreateFSub(LHS, V);
983
984  // Check for X+0.0.  Simplify it to X if we know X is not -0.0.
985  if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
986    if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
987      return ReplaceInstUsesWith(I, LHS);
988
989  // Check for (add double (sitofp x), y), see if we can merge this into an
990  // integer add followed by a promotion.
991  if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
992    // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
993    // ... if the constant fits in the integer value.  This is useful for things
994    // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
995    // requires a constant pool load, and generally allows the add to be better
996    // instcombined.
997    if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
998      Constant *CI =
999      ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
1000      if (LHSConv->hasOneUse() &&
1001          ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
1002          WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
1003        // Insert the new integer add.
1004        Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1005                                              CI, "addconv");
1006        return new SIToFPInst(NewAdd, I.getType());
1007      }
1008    }
1009
1010    // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
1011    if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
1012      // Only do this if x/y have the same type, if at last one of them has a
1013      // single use (so we don't increase the number of int->fp conversions),
1014      // and if the integer add will not overflow.
1015      if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
1016          (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1017          WillNotOverflowSignedAdd(LHSConv->getOperand(0),
1018                                   RHSConv->getOperand(0))) {
1019        // Insert the new integer add.
1020        Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1021                                              RHSConv->getOperand(0),"addconv");
1022        return new SIToFPInst(NewAdd, I.getType());
1023      }
1024    }
1025  }
1026
1027  return Changed ? &I : 0;
1028}
1029
1030
1031/// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
1032/// code necessary to compute the offset from the base pointer (without adding
1033/// in the base pointer).  Return the result as a signed integer of intptr size.
1034Value *InstCombiner::EmitGEPOffset(User *GEP) {
1035  TargetData &TD = *getTargetData();
1036  gep_type_iterator GTI = gep_type_begin(GEP);
1037  const Type *IntPtrTy = TD.getIntPtrType(GEP->getContext());
1038  Value *Result = Constant::getNullValue(IntPtrTy);
1039
1040  // Build a mask for high order bits.
1041  unsigned IntPtrWidth = TD.getPointerSizeInBits();
1042  uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
1043
1044  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
1045       ++i, ++GTI) {
1046    Value *Op = *i;
1047    uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
1048    if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
1049      if (OpC->isZero()) continue;
1050
1051      // Handle a struct index, which adds its field offset to the pointer.
1052      if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
1053        Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
1054
1055        Result = Builder->CreateAdd(Result,
1056                                    ConstantInt::get(IntPtrTy, Size),
1057                                    GEP->getName()+".offs");
1058        continue;
1059      }
1060
1061      Constant *Scale = ConstantInt::get(IntPtrTy, Size);
1062      Constant *OC =
1063              ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
1064      Scale = ConstantExpr::getMul(OC, Scale);
1065      // Emit an add instruction.
1066      Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
1067      continue;
1068    }
1069    // Convert to correct type.
1070    if (Op->getType() != IntPtrTy)
1071      Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
1072    if (Size != 1) {
1073      Constant *Scale = ConstantInt::get(IntPtrTy, Size);
1074      // We'll let instcombine(mul) convert this to a shl if possible.
1075      Op = Builder->CreateMul(Op, Scale, GEP->getName()+".idx");
1076    }
1077
1078    // Emit an add instruction.
1079    Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
1080  }
1081  return Result;
1082}
1083
1084
1085
1086
1087/// Optimize pointer differences into the same array into a size.  Consider:
1088///  &A[10] - &A[0]: we should compile this to "10".  LHS/RHS are the pointer
1089/// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
1090///
1091Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
1092                                               const Type *Ty) {
1093  assert(TD && "Must have target data info for this");
1094
1095  // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
1096  // this.
1097  bool Swapped = false;
1098  GetElementPtrInst *GEP = 0;
1099  ConstantExpr *CstGEP = 0;
1100
1101  // TODO: Could also optimize &A[i] - &A[j] -> "i-j", and "&A.foo[i] - &A.foo".
1102  // For now we require one side to be the base pointer "A" or a constant
1103  // expression derived from it.
1104  if (GetElementPtrInst *LHSGEP = dyn_cast<GetElementPtrInst>(LHS)) {
1105    // (gep X, ...) - X
1106    if (LHSGEP->getOperand(0) == RHS) {
1107      GEP = LHSGEP;
1108      Swapped = false;
1109    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(RHS)) {
1110      // (gep X, ...) - (ce_gep X, ...)
1111      if (CE->getOpcode() == Instruction::GetElementPtr &&
1112          LHSGEP->getOperand(0) == CE->getOperand(0)) {
1113        CstGEP = CE;
1114        GEP = LHSGEP;
1115        Swapped = false;
1116      }
1117    }
1118  }
1119
1120  if (GetElementPtrInst *RHSGEP = dyn_cast<GetElementPtrInst>(RHS)) {
1121    // X - (gep X, ...)
1122    if (RHSGEP->getOperand(0) == LHS) {
1123      GEP = RHSGEP;
1124      Swapped = true;
1125    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(LHS)) {
1126      // (ce_gep X, ...) - (gep X, ...)
1127      if (CE->getOpcode() == Instruction::GetElementPtr &&
1128          RHSGEP->getOperand(0) == CE->getOperand(0)) {
1129        CstGEP = CE;
1130        GEP = RHSGEP;
1131        Swapped = true;
1132      }
1133    }
1134  }
1135
1136  if (GEP == 0)
1137    return 0;
1138
1139  // Emit the offset of the GEP and an intptr_t.
1140  Value *Result = EmitGEPOffset(GEP);
1141
1142  // If we had a constant expression GEP on the other side offsetting the
1143  // pointer, subtract it from the offset we have.
1144  if (CstGEP) {
1145    Value *CstOffset = EmitGEPOffset(CstGEP);
1146    Result = Builder->CreateSub(Result, CstOffset);
1147  }
1148
1149
1150  // If we have p - gep(p, ...)  then we have to negate the result.
1151  if (Swapped)
1152    Result = Builder->CreateNeg(Result, "diff.neg");
1153
1154  return Builder->CreateIntCast(Result, Ty, true);
1155}
1156
1157
1158Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1159  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1160
1161  if (Op0 == Op1)                        // sub X, X  -> 0
1162    return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1163
1164  // If this is a 'B = x-(-A)', change to B = x+A.  This preserves NSW/NUW.
1165  if (Value *V = dyn_castNegVal(Op1)) {
1166    BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
1167    Res->setHasNoSignedWrap(I.hasNoSignedWrap());
1168    Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
1169    return Res;
1170  }
1171
1172  if (isa<UndefValue>(Op0))
1173    return ReplaceInstUsesWith(I, Op0);    // undef - X -> undef
1174  if (isa<UndefValue>(Op1))
1175    return ReplaceInstUsesWith(I, Op1);    // X - undef -> undef
1176  if (I.getType() == Type::getInt1Ty(I.getContext()))
1177    return BinaryOperator::CreateXor(Op0, Op1);
1178
1179  if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
1180    // Replace (-1 - A) with (~A).
1181    if (C->isAllOnesValue())
1182      return BinaryOperator::CreateNot(Op1);
1183
1184    // C - ~X == X + (1+C)
1185    Value *X = 0;
1186    if (match(Op1, m_Not(m_Value(X))))
1187      return BinaryOperator::CreateAdd(X, AddOne(C));
1188
1189    // -(X >>u 31) -> (X >>s 31)
1190    // -(X >>s 31) -> (X >>u 31)
1191    if (C->isZero()) {
1192      if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) {
1193        if (SI->getOpcode() == Instruction::LShr) {
1194          if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1195            // Check to see if we are shifting out everything but the sign bit.
1196            if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
1197                SI->getType()->getPrimitiveSizeInBits()-1) {
1198              // Ok, the transformation is safe.  Insert AShr.
1199              return BinaryOperator::Create(Instruction::AShr,
1200                                          SI->getOperand(0), CU, SI->getName());
1201            }
1202          }
1203        } else if (SI->getOpcode() == Instruction::AShr) {
1204          if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1205            // Check to see if we are shifting out everything but the sign bit.
1206            if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
1207                SI->getType()->getPrimitiveSizeInBits()-1) {
1208              // Ok, the transformation is safe.  Insert LShr.
1209              return BinaryOperator::CreateLShr(
1210                                          SI->getOperand(0), CU, SI->getName());
1211            }
1212          }
1213        }
1214      }
1215    }
1216
1217    // Try to fold constant sub into select arguments.
1218    if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1219      if (Instruction *R = FoldOpIntoSelect(I, SI))
1220        return R;
1221
1222    // C - zext(bool) -> bool ? C - 1 : C
1223    if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
1224      if (ZI->getSrcTy() == Type::getInt1Ty(I.getContext()))
1225        return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
1226  }
1227
1228  if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
1229    if (Op1I->getOpcode() == Instruction::Add) {
1230      if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
1231        return BinaryOperator::CreateNeg(Op1I->getOperand(1),
1232                                         I.getName());
1233      else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
1234        return BinaryOperator::CreateNeg(Op1I->getOperand(0),
1235                                         I.getName());
1236      else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
1237        if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
1238          // C1-(X+C2) --> (C1-C2)-X
1239          return BinaryOperator::CreateSub(
1240            ConstantExpr::getSub(CI1, CI2), Op1I->getOperand(0));
1241      }
1242    }
1243
1244    if (Op1I->hasOneUse()) {
1245      // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
1246      // is not used by anyone else...
1247      //
1248      if (Op1I->getOpcode() == Instruction::Sub) {
1249        // Swap the two operands of the subexpr...
1250        Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
1251        Op1I->setOperand(0, IIOp1);
1252        Op1I->setOperand(1, IIOp0);
1253
1254        // Create the new top level add instruction...
1255        return BinaryOperator::CreateAdd(Op0, Op1);
1256      }
1257
1258      // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
1259      //
1260      if (Op1I->getOpcode() == Instruction::And &&
1261          (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
1262        Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
1263
1264        Value *NewNot = Builder->CreateNot(OtherOp, "B.not");
1265        return BinaryOperator::CreateAnd(Op0, NewNot);
1266      }
1267
1268      // 0 - (X sdiv C)  -> (X sdiv -C)
1269      if (Op1I->getOpcode() == Instruction::SDiv)
1270        if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
1271          if (CSI->isZero())
1272            if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
1273              return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
1274                                          ConstantExpr::getNeg(DivRHS));
1275
1276      // X - X*C --> X * (1-C)
1277      ConstantInt *C2 = 0;
1278      if (dyn_castFoldableMul(Op1I, C2) == Op0) {
1279        Constant *CP1 =
1280          ConstantExpr::getSub(ConstantInt::get(I.getType(), 1),
1281                                             C2);
1282        return BinaryOperator::CreateMul(Op0, CP1);
1283      }
1284    }
1285  }
1286
1287  if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1288    if (Op0I->getOpcode() == Instruction::Add) {
1289      if (Op0I->getOperand(0) == Op1)             // (Y+X)-Y == X
1290        return ReplaceInstUsesWith(I, Op0I->getOperand(1));
1291      else if (Op0I->getOperand(1) == Op1)        // (X+Y)-Y == X
1292        return ReplaceInstUsesWith(I, Op0I->getOperand(0));
1293    } else if (Op0I->getOpcode() == Instruction::Sub) {
1294      if (Op0I->getOperand(0) == Op1)             // (X-Y)-X == -Y
1295        return BinaryOperator::CreateNeg(Op0I->getOperand(1),
1296                                         I.getName());
1297    }
1298  }
1299
1300  ConstantInt *C1;
1301  if (Value *X = dyn_castFoldableMul(Op0, C1)) {
1302    if (X == Op1)  // X*C - X --> X * (C-1)
1303      return BinaryOperator::CreateMul(Op1, SubOne(C1));
1304
1305    ConstantInt *C2;   // X*C1 - X*C2 -> X * (C1-C2)
1306    if (X == dyn_castFoldableMul(Op1, C2))
1307      return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
1308  }
1309
1310  // Optimize pointer differences into the same array into a size.  Consider:
1311  //  &A[10] - &A[0]: we should compile this to "10".
1312  if (TD) {
1313    Value *LHSOp, *RHSOp;
1314    if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
1315        match(Op1, m_PtrToInt(m_Value(RHSOp))))
1316      if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1317        return ReplaceInstUsesWith(I, Res);
1318
1319    // trunc(p)-trunc(q) -> trunc(p-q)
1320    if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
1321        match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
1322      if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1323        return ReplaceInstUsesWith(I, Res);
1324  }
1325
1326  return 0;
1327}
1328
1329Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
1330  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1331
1332  // If this is a 'B = x-(-A)', change to B = x+A...
1333  if (Value *V = dyn_castFNegVal(Op1))
1334    return BinaryOperator::CreateFAdd(Op0, V);
1335
1336  if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
1337    if (Op1I->getOpcode() == Instruction::FAdd) {
1338      if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
1339        return BinaryOperator::CreateFNeg(Op1I->getOperand(1),
1340                                          I.getName());
1341      else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
1342        return BinaryOperator::CreateFNeg(Op1I->getOperand(0),
1343                                          I.getName());
1344    }
1345  }
1346
1347  return 0;
1348}
1349
1350Instruction *InstCombiner::visitMul(BinaryOperator &I) {
1351  bool Changed = SimplifyCommutative(I);
1352  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1353
1354  if (isa<UndefValue>(Op1))              // undef * X -> 0
1355    return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1356
1357  // Simplify mul instructions with a constant RHS.
1358  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
1359    if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1C)) {
1360
1361      // ((X << C1)*C2) == (X * (C2 << C1))
1362      if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0))
1363        if (SI->getOpcode() == Instruction::Shl)
1364          if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
1365            return BinaryOperator::CreateMul(SI->getOperand(0),
1366                                        ConstantExpr::getShl(CI, ShOp));
1367
1368      if (CI->isZero())
1369        return ReplaceInstUsesWith(I, Op1C);  // X * 0  == 0
1370      if (CI->equalsInt(1))                  // X * 1  == X
1371        return ReplaceInstUsesWith(I, Op0);
1372      if (CI->isAllOnesValue())              // X * -1 == 0 - X
1373        return BinaryOperator::CreateNeg(Op0, I.getName());
1374
1375      const APInt& Val = cast<ConstantInt>(CI)->getValue();
1376      if (Val.isPowerOf2()) {          // Replace X*(2^C) with X << C
1377        return BinaryOperator::CreateShl(Op0,
1378                 ConstantInt::get(Op0->getType(), Val.logBase2()));
1379      }
1380    } else if (isa<VectorType>(Op1C->getType())) {
1381      if (Op1C->isNullValue())
1382        return ReplaceInstUsesWith(I, Op1C);
1383
1384      if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1C)) {
1385        if (Op1V->isAllOnesValue())              // X * -1 == 0 - X
1386          return BinaryOperator::CreateNeg(Op0, I.getName());
1387
1388        // As above, vector X*splat(1.0) -> X in all defined cases.
1389        if (Constant *Splat = Op1V->getSplatValue()) {
1390          if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat))
1391            if (CI->equalsInt(1))
1392              return ReplaceInstUsesWith(I, Op0);
1393        }
1394      }
1395    }
1396
1397    if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
1398      if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
1399          isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1C)) {
1400        // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
1401        Value *Add = Builder->CreateMul(Op0I->getOperand(0), Op1C, "tmp");
1402        Value *C1C2 = Builder->CreateMul(Op1C, Op0I->getOperand(1));
1403        return BinaryOperator::CreateAdd(Add, C1C2);
1404
1405      }
1406
1407    // Try to fold constant mul into select arguments.
1408    if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1409      if (Instruction *R = FoldOpIntoSelect(I, SI))
1410        return R;
1411
1412    if (isa<PHINode>(Op0))
1413      if (Instruction *NV = FoldOpIntoPhi(I))
1414        return NV;
1415  }
1416
1417  if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
1418    if (Value *Op1v = dyn_castNegVal(Op1))
1419      return BinaryOperator::CreateMul(Op0v, Op1v);
1420
1421  // (X / Y) *  Y = X - (X % Y)
1422  // (X / Y) * -Y = (X % Y) - X
1423  {
1424    Value *Op1C = Op1;
1425    BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
1426    if (!BO ||
1427        (BO->getOpcode() != Instruction::UDiv &&
1428         BO->getOpcode() != Instruction::SDiv)) {
1429      Op1C = Op0;
1430      BO = dyn_cast<BinaryOperator>(Op1);
1431    }
1432    Value *Neg = dyn_castNegVal(Op1C);
1433    if (BO && BO->hasOneUse() &&
1434        (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
1435        (BO->getOpcode() == Instruction::UDiv ||
1436         BO->getOpcode() == Instruction::SDiv)) {
1437      Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
1438
1439      // If the division is exact, X % Y is zero.
1440      if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO))
1441        if (SDiv->isExact()) {
1442          if (Op1BO == Op1C)
1443            return ReplaceInstUsesWith(I, Op0BO);
1444          return BinaryOperator::CreateNeg(Op0BO);
1445        }
1446
1447      Value *Rem;
1448      if (BO->getOpcode() == Instruction::UDiv)
1449        Rem = Builder->CreateURem(Op0BO, Op1BO);
1450      else
1451        Rem = Builder->CreateSRem(Op0BO, Op1BO);
1452      Rem->takeName(BO);
1453
1454      if (Op1BO == Op1C)
1455        return BinaryOperator::CreateSub(Op0BO, Rem);
1456      return BinaryOperator::CreateSub(Rem, Op0BO);
1457    }
1458  }
1459
1460  /// i1 mul -> i1 and.
1461  if (I.getType() == Type::getInt1Ty(I.getContext()))
1462    return BinaryOperator::CreateAnd(Op0, Op1);
1463
1464  // X*(1 << Y) --> X << Y
1465  // (1 << Y)*X --> X << Y
1466  {
1467    Value *Y;
1468    if (match(Op0, m_Shl(m_One(), m_Value(Y))))
1469      return BinaryOperator::CreateShl(Op1, Y);
1470    if (match(Op1, m_Shl(m_One(), m_Value(Y))))
1471      return BinaryOperator::CreateShl(Op0, Y);
1472  }
1473
1474  // If one of the operands of the multiply is a cast from a boolean value, then
1475  // we know the bool is either zero or one, so this is a 'masking' multiply.
1476  //   X * Y (where Y is 0 or 1) -> X & (0-Y)
1477  if (!isa<VectorType>(I.getType())) {
1478    // -2 is "-1 << 1" so it is all bits set except the low one.
1479    APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
1480
1481    Value *BoolCast = 0, *OtherOp = 0;
1482    if (MaskedValueIsZero(Op0, Negative2))
1483      BoolCast = Op0, OtherOp = Op1;
1484    else if (MaskedValueIsZero(Op1, Negative2))
1485      BoolCast = Op1, OtherOp = Op0;
1486
1487    if (BoolCast) {
1488      Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()),
1489                                    BoolCast, "tmp");
1490      return BinaryOperator::CreateAnd(V, OtherOp);
1491    }
1492  }
1493
1494  return Changed ? &I : 0;
1495}
1496
1497Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
1498  bool Changed = SimplifyCommutative(I);
1499  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1500
1501  // Simplify mul instructions with a constant RHS...
1502  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
1503    if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1C)) {
1504      // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
1505      // ANSI says we can drop signals, so we can do this anyway." (from GCC)
1506      if (Op1F->isExactlyValue(1.0))
1507        return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
1508    } else if (isa<VectorType>(Op1C->getType())) {
1509      if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1C)) {
1510        // As above, vector X*splat(1.0) -> X in all defined cases.
1511        if (Constant *Splat = Op1V->getSplatValue()) {
1512          if (ConstantFP *F = dyn_cast<ConstantFP>(Splat))
1513            if (F->isExactlyValue(1.0))
1514              return ReplaceInstUsesWith(I, Op0);
1515        }
1516      }
1517    }
1518
1519    // Try to fold constant mul into select arguments.
1520    if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1521      if (Instruction *R = FoldOpIntoSelect(I, SI))
1522        return R;
1523
1524    if (isa<PHINode>(Op0))
1525      if (Instruction *NV = FoldOpIntoPhi(I))
1526        return NV;
1527  }
1528
1529  if (Value *Op0v = dyn_castFNegVal(Op0))     // -X * -Y = X*Y
1530    if (Value *Op1v = dyn_castFNegVal(Op1))
1531      return BinaryOperator::CreateFMul(Op0v, Op1v);
1532
1533  return Changed ? &I : 0;
1534}
1535
1536/// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
1537/// instruction.
1538bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
1539  SelectInst *SI = cast<SelectInst>(I.getOperand(1));
1540
1541  // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
1542  int NonNullOperand = -1;
1543  if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
1544    if (ST->isNullValue())
1545      NonNullOperand = 2;
1546  // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
1547  if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
1548    if (ST->isNullValue())
1549      NonNullOperand = 1;
1550
1551  if (NonNullOperand == -1)
1552    return false;
1553
1554  Value *SelectCond = SI->getOperand(0);
1555
1556  // Change the div/rem to use 'Y' instead of the select.
1557  I.setOperand(1, SI->getOperand(NonNullOperand));
1558
1559  // Okay, we know we replace the operand of the div/rem with 'Y' with no
1560  // problem.  However, the select, or the condition of the select may have
1561  // multiple uses.  Based on our knowledge that the operand must be non-zero,
1562  // propagate the known value for the select into other uses of it, and
1563  // propagate a known value of the condition into its other users.
1564
1565  // If the select and condition only have a single use, don't bother with this,
1566  // early exit.
1567  if (SI->use_empty() && SelectCond->hasOneUse())
1568    return true;
1569
1570  // Scan the current block backward, looking for other uses of SI.
1571  BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
1572
1573  while (BBI != BBFront) {
1574    --BBI;
1575    // If we found a call to a function, we can't assume it will return, so
1576    // information from below it cannot be propagated above it.
1577    if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
1578      break;
1579
1580    // Replace uses of the select or its condition with the known values.
1581    for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
1582         I != E; ++I) {
1583      if (*I == SI) {
1584        *I = SI->getOperand(NonNullOperand);
1585        Worklist.Add(BBI);
1586      } else if (*I == SelectCond) {
1587        *I = NonNullOperand == 1 ? ConstantInt::getTrue(BBI->getContext()) :
1588                                   ConstantInt::getFalse(BBI->getContext());
1589        Worklist.Add(BBI);
1590      }
1591    }
1592
1593    // If we past the instruction, quit looking for it.
1594    if (&*BBI == SI)
1595      SI = 0;
1596    if (&*BBI == SelectCond)
1597      SelectCond = 0;
1598
1599    // If we ran out of things to eliminate, break out of the loop.
1600    if (SelectCond == 0 && SI == 0)
1601      break;
1602
1603  }
1604  return true;
1605}
1606
1607
1608/// This function implements the transforms on div instructions that work
1609/// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
1610/// used by the visitors to those instructions.
1611/// @brief Transforms common to all three div instructions
1612Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
1613  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1614
1615  // undef / X -> 0        for integer.
1616  // undef / X -> undef    for FP (the undef could be a snan).
1617  if (isa<UndefValue>(Op0)) {
1618    if (Op0->getType()->isFPOrFPVector())
1619      return ReplaceInstUsesWith(I, Op0);
1620    return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1621  }
1622
1623  // X / undef -> undef
1624  if (isa<UndefValue>(Op1))
1625    return ReplaceInstUsesWith(I, Op1);
1626
1627  return 0;
1628}
1629
1630/// This function implements the transforms common to both integer division
1631/// instructions (udiv and sdiv). It is called by the visitors to those integer
1632/// division instructions.
1633/// @brief Common integer divide transforms
1634Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
1635  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1636
1637  // (sdiv X, X) --> 1     (udiv X, X) --> 1
1638  if (Op0 == Op1) {
1639    if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) {
1640      Constant *CI = ConstantInt::get(Ty->getElementType(), 1);
1641      std::vector<Constant*> Elts(Ty->getNumElements(), CI);
1642      return ReplaceInstUsesWith(I, ConstantVector::get(Elts));
1643    }
1644
1645    Constant *CI = ConstantInt::get(I.getType(), 1);
1646    return ReplaceInstUsesWith(I, CI);
1647  }
1648
1649  if (Instruction *Common = commonDivTransforms(I))
1650    return Common;
1651
1652  // Handle cases involving: [su]div X, (select Cond, Y, Z)
1653  // This does not apply for fdiv.
1654  if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1655    return &I;
1656
1657  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1658    // div X, 1 == X
1659    if (RHS->equalsInt(1))
1660      return ReplaceInstUsesWith(I, Op0);
1661
1662    // (X / C1) / C2  -> X / (C1*C2)
1663    if (Instruction *LHS = dyn_cast<Instruction>(Op0))
1664      if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
1665        if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
1666          if (MultiplyOverflows(RHS, LHSRHS,
1667                                I.getOpcode()==Instruction::SDiv))
1668            return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1669          else
1670            return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
1671                                      ConstantExpr::getMul(RHS, LHSRHS));
1672        }
1673
1674    if (!RHS->isZero()) { // avoid X udiv 0
1675      if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1676        if (Instruction *R = FoldOpIntoSelect(I, SI))
1677          return R;
1678      if (isa<PHINode>(Op0))
1679        if (Instruction *NV = FoldOpIntoPhi(I))
1680          return NV;
1681    }
1682  }
1683
1684  // 0 / X == 0, we don't need to preserve faults!
1685  if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
1686    if (LHS->equalsInt(0))
1687      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1688
1689  // It can't be division by zero, hence it must be division by one.
1690  if (I.getType() == Type::getInt1Ty(I.getContext()))
1691    return ReplaceInstUsesWith(I, Op0);
1692
1693  if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
1694    if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue()))
1695      // div X, 1 == X
1696      if (X->isOne())
1697        return ReplaceInstUsesWith(I, Op0);
1698  }
1699
1700  return 0;
1701}
1702
1703Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
1704  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1705
1706  // Handle the integer div common cases
1707  if (Instruction *Common = commonIDivTransforms(I))
1708    return Common;
1709
1710  if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
1711    // X udiv C^2 -> X >> C
1712    // Check to see if this is an unsigned division with an exact power of 2,
1713    // if so, convert to a right shift.
1714    if (C->getValue().isPowerOf2())  // 0 not included in isPowerOf2
1715      return BinaryOperator::CreateLShr(Op0,
1716            ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
1717
1718    // X udiv C, where C >= signbit
1719    if (C->getValue().isNegative()) {
1720      Value *IC = Builder->CreateICmpULT( Op0, C);
1721      return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
1722                                ConstantInt::get(I.getType(), 1));
1723    }
1724  }
1725
1726  // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
1727  if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) {
1728    if (RHSI->getOpcode() == Instruction::Shl &&
1729        isa<ConstantInt>(RHSI->getOperand(0))) {
1730      const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue();
1731      if (C1.isPowerOf2()) {
1732        Value *N = RHSI->getOperand(1);
1733        const Type *NTy = N->getType();
1734        if (uint32_t C2 = C1.logBase2())
1735          N = Builder->CreateAdd(N, ConstantInt::get(NTy, C2), "tmp");
1736        return BinaryOperator::CreateLShr(Op0, N);
1737      }
1738    }
1739  }
1740
1741  // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2)
1742  // where C1&C2 are powers of two.
1743  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1744    if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
1745      if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2)))  {
1746        const APInt &TVA = STO->getValue(), &FVA = SFO->getValue();
1747        if (TVA.isPowerOf2() && FVA.isPowerOf2()) {
1748          // Compute the shift amounts
1749          uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
1750          // Construct the "on true" case of the select
1751          Constant *TC = ConstantInt::get(Op0->getType(), TSA);
1752          Value *TSI = Builder->CreateLShr(Op0, TC, SI->getName()+".t");
1753
1754          // Construct the "on false" case of the select
1755          Constant *FC = ConstantInt::get(Op0->getType(), FSA);
1756          Value *FSI = Builder->CreateLShr(Op0, FC, SI->getName()+".f");
1757
1758          // construct the select instruction and return it.
1759          return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName());
1760        }
1761      }
1762  return 0;
1763}
1764
1765Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
1766  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1767
1768  // Handle the integer div common cases
1769  if (Instruction *Common = commonIDivTransforms(I))
1770    return Common;
1771
1772  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1773    // sdiv X, -1 == -X
1774    if (RHS->isAllOnesValue())
1775      return BinaryOperator::CreateNeg(Op0);
1776
1777    // sdiv X, C  -->  ashr X, log2(C)
1778    if (cast<SDivOperator>(&I)->isExact() &&
1779        RHS->getValue().isNonNegative() &&
1780        RHS->getValue().isPowerOf2()) {
1781      Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
1782                                            RHS->getValue().exactLogBase2());
1783      return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
1784    }
1785
1786    // -X/C  -->  X/-C  provided the negation doesn't overflow.
1787    if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
1788      if (isa<Constant>(Sub->getOperand(0)) &&
1789          cast<Constant>(Sub->getOperand(0))->isNullValue() &&
1790          Sub->hasNoSignedWrap())
1791        return BinaryOperator::CreateSDiv(Sub->getOperand(1),
1792                                          ConstantExpr::getNeg(RHS));
1793  }
1794
1795  // If the sign bits of both operands are zero (i.e. we can prove they are
1796  // unsigned inputs), turn this into a udiv.
1797  if (I.getType()->isInteger()) {
1798    APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
1799    if (MaskedValueIsZero(Op0, Mask)) {
1800      if (MaskedValueIsZero(Op1, Mask)) {
1801        // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
1802        return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1803      }
1804      ConstantInt *ShiftedInt;
1805      if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value())) &&
1806          ShiftedInt->getValue().isPowerOf2()) {
1807        // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1808        // Safe because the only negative value (1 << Y) can take on is
1809        // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1810        // the sign bit set.
1811        return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1812      }
1813    }
1814  }
1815
1816  return 0;
1817}
1818
1819Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
1820  return commonDivTransforms(I);
1821}
1822
1823/// This function implements the transforms on rem instructions that work
1824/// regardless of the kind of rem instruction it is (urem, srem, or frem). It
1825/// is used by the visitors to those instructions.
1826/// @brief Transforms common to all three rem instructions
1827Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
1828  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1829
1830  if (isa<UndefValue>(Op0)) {             // undef % X -> 0
1831    if (I.getType()->isFPOrFPVector())
1832      return ReplaceInstUsesWith(I, Op0);  // X % undef -> undef (could be SNaN)
1833    return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1834  }
1835  if (isa<UndefValue>(Op1))
1836    return ReplaceInstUsesWith(I, Op1);  // X % undef -> undef
1837
1838  // Handle cases involving: rem X, (select Cond, Y, Z)
1839  if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1840    return &I;
1841
1842  return 0;
1843}
1844
1845/// This function implements the transforms common to both integer remainder
1846/// instructions (urem and srem). It is called by the visitors to those integer
1847/// remainder instructions.
1848/// @brief Common integer remainder transforms
1849Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
1850  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1851
1852  if (Instruction *common = commonRemTransforms(I))
1853    return common;
1854
1855  // 0 % X == 0 for integer, we don't need to preserve faults!
1856  if (Constant *LHS = dyn_cast<Constant>(Op0))
1857    if (LHS->isNullValue())
1858      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1859
1860  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1861    // X % 0 == undef, we don't need to preserve faults!
1862    if (RHS->equalsInt(0))
1863      return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
1864
1865    if (RHS->equalsInt(1))  // X % 1 == 0
1866      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1867
1868    if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1869      if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1870        if (Instruction *R = FoldOpIntoSelect(I, SI))
1871          return R;
1872      } else if (isa<PHINode>(Op0I)) {
1873        if (Instruction *NV = FoldOpIntoPhi(I))
1874          return NV;
1875      }
1876
1877      // See if we can fold away this rem instruction.
1878      if (SimplifyDemandedInstructionBits(I))
1879        return &I;
1880    }
1881  }
1882
1883  return 0;
1884}
1885
1886Instruction *InstCombiner::visitURem(BinaryOperator &I) {
1887  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1888
1889  if (Instruction *common = commonIRemTransforms(I))
1890    return common;
1891
1892  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1893    // X urem C^2 -> X and C
1894    // Check to see if this is an unsigned remainder with an exact power of 2,
1895    // if so, convert to a bitwise and.
1896    if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
1897      if (C->getValue().isPowerOf2())
1898        return BinaryOperator::CreateAnd(Op0, SubOne(C));
1899  }
1900
1901  if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
1902    // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)
1903    if (RHSI->getOpcode() == Instruction::Shl &&
1904        isa<ConstantInt>(RHSI->getOperand(0))) {
1905      if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
1906        Constant *N1 = Constant::getAllOnesValue(I.getType());
1907        Value *Add = Builder->CreateAdd(RHSI, N1, "tmp");
1908        return BinaryOperator::CreateAnd(Op0, Add);
1909      }
1910    }
1911  }
1912
1913  // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2)
1914  // where C1&C2 are powers of two.
1915  if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
1916    if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
1917      if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
1918        // STO == 0 and SFO == 0 handled above.
1919        if ((STO->getValue().isPowerOf2()) &&
1920            (SFO->getValue().isPowerOf2())) {
1921          Value *TrueAnd = Builder->CreateAnd(Op0, SubOne(STO),
1922                                              SI->getName()+".t");
1923          Value *FalseAnd = Builder->CreateAnd(Op0, SubOne(SFO),
1924                                               SI->getName()+".f");
1925          return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
1926        }
1927      }
1928  }
1929
1930  return 0;
1931}
1932
1933Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
1934  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1935
1936  // Handle the integer rem common cases
1937  if (Instruction *Common = commonIRemTransforms(I))
1938    return Common;
1939
1940  if (Value *RHSNeg = dyn_castNegVal(Op1))
1941    if (!isa<Constant>(RHSNeg) ||
1942        (isa<ConstantInt>(RHSNeg) &&
1943         cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
1944      // X % -Y -> X % Y
1945      Worklist.AddValue(I.getOperand(1));
1946      I.setOperand(1, RHSNeg);
1947      return &I;
1948    }
1949
1950  // If the sign bits of both operands are zero (i.e. we can prove they are
1951  // unsigned inputs), turn this into a urem.
1952  if (I.getType()->isInteger()) {
1953    APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
1954    if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
1955      // X srem Y -> X urem Y, iff X and Y don't have sign bit set
1956      return BinaryOperator::CreateURem(Op0, Op1, I.getName());
1957    }
1958  }
1959
1960  // If it's a constant vector, flip any negative values positive.
1961  if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) {
1962    unsigned VWidth = RHSV->getNumOperands();
1963
1964    bool hasNegative = false;
1965    for (unsigned i = 0; !hasNegative && i != VWidth; ++i)
1966      if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i)))
1967        if (RHS->getValue().isNegative())
1968          hasNegative = true;
1969
1970    if (hasNegative) {
1971      std::vector<Constant *> Elts(VWidth);
1972      for (unsigned i = 0; i != VWidth; ++i) {
1973        if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) {
1974          if (RHS->getValue().isNegative())
1975            Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
1976          else
1977            Elts[i] = RHS;
1978        }
1979      }
1980
1981      Constant *NewRHSV = ConstantVector::get(Elts);
1982      if (NewRHSV != RHSV) {
1983        Worklist.AddValue(I.getOperand(1));
1984        I.setOperand(1, NewRHSV);
1985        return &I;
1986      }
1987    }
1988  }
1989
1990  return 0;
1991}
1992
1993Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
1994  return commonRemTransforms(I);
1995}
1996
1997// isOneBitSet - Return true if there is exactly one bit set in the specified
1998// constant.
1999static bool isOneBitSet(const ConstantInt *CI) {
2000  return CI->getValue().isPowerOf2();
2001}
2002
2003/// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits
2004/// are carefully arranged to allow folding of expressions such as:
2005///
2006///      (A < B) | (A > B) --> (A != B)
2007///
2008/// Note that this is only valid if the first and second predicates have the
2009/// same sign. Is illegal to do: (A u< B) | (A s> B)
2010///
2011/// Three bits are used to represent the condition, as follows:
2012///   0  A > B
2013///   1  A == B
2014///   2  A < B
2015///
2016/// <=>  Value  Definition
2017/// 000     0   Always false
2018/// 001     1   A >  B
2019/// 010     2   A == B
2020/// 011     3   A >= B
2021/// 100     4   A <  B
2022/// 101     5   A != B
2023/// 110     6   A <= B
2024/// 111     7   Always true
2025///
2026static unsigned getICmpCode(const ICmpInst *ICI) {
2027  switch (ICI->getPredicate()) {
2028    // False -> 0
2029  case ICmpInst::ICMP_UGT: return 1;  // 001
2030  case ICmpInst::ICMP_SGT: return 1;  // 001
2031  case ICmpInst::ICMP_EQ:  return 2;  // 010
2032  case ICmpInst::ICMP_UGE: return 3;  // 011
2033  case ICmpInst::ICMP_SGE: return 3;  // 011
2034  case ICmpInst::ICMP_ULT: return 4;  // 100
2035  case ICmpInst::ICMP_SLT: return 4;  // 100
2036  case ICmpInst::ICMP_NE:  return 5;  // 101
2037  case ICmpInst::ICMP_ULE: return 6;  // 110
2038  case ICmpInst::ICMP_SLE: return 6;  // 110
2039    // True -> 7
2040  default:
2041    llvm_unreachable("Invalid ICmp predicate!");
2042    return 0;
2043  }
2044}
2045
2046/// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
2047/// predicate into a three bit mask. It also returns whether it is an ordered
2048/// predicate by reference.
2049static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
2050  isOrdered = false;
2051  switch (CC) {
2052  case FCmpInst::FCMP_ORD: isOrdered = true; return 0;  // 000
2053  case FCmpInst::FCMP_UNO:                   return 0;  // 000
2054  case FCmpInst::FCMP_OGT: isOrdered = true; return 1;  // 001
2055  case FCmpInst::FCMP_UGT:                   return 1;  // 001
2056  case FCmpInst::FCMP_OEQ: isOrdered = true; return 2;  // 010
2057  case FCmpInst::FCMP_UEQ:                   return 2;  // 010
2058  case FCmpInst::FCMP_OGE: isOrdered = true; return 3;  // 011
2059  case FCmpInst::FCMP_UGE:                   return 3;  // 011
2060  case FCmpInst::FCMP_OLT: isOrdered = true; return 4;  // 100
2061  case FCmpInst::FCMP_ULT:                   return 4;  // 100
2062  case FCmpInst::FCMP_ONE: isOrdered = true; return 5;  // 101
2063  case FCmpInst::FCMP_UNE:                   return 5;  // 101
2064  case FCmpInst::FCMP_OLE: isOrdered = true; return 6;  // 110
2065  case FCmpInst::FCMP_ULE:                   return 6;  // 110
2066    // True -> 7
2067  default:
2068    // Not expecting FCMP_FALSE and FCMP_TRUE;
2069    llvm_unreachable("Unexpected FCmp predicate!");
2070    return 0;
2071  }
2072}
2073
2074/// getICmpValue - This is the complement of getICmpCode, which turns an
2075/// opcode and two operands into either a constant true or false, or a brand
2076/// new ICmp instruction. The sign is passed in to determine which kind
2077/// of predicate to use in the new icmp instruction.
2078static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) {
2079  switch (code) {
2080  default: llvm_unreachable("Illegal ICmp code!");
2081  case  0: return ConstantInt::getFalse(LHS->getContext());
2082  case  1:
2083    if (sign)
2084      return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
2085    else
2086      return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
2087  case  2: return new ICmpInst(ICmpInst::ICMP_EQ,  LHS, RHS);
2088  case  3:
2089    if (sign)
2090      return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
2091    else
2092      return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
2093  case  4:
2094    if (sign)
2095      return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
2096    else
2097      return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
2098  case  5: return new ICmpInst(ICmpInst::ICMP_NE,  LHS, RHS);
2099  case  6:
2100    if (sign)
2101      return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
2102    else
2103      return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
2104  case  7: return ConstantInt::getTrue(LHS->getContext());
2105  }
2106}
2107
2108/// getFCmpValue - This is the complement of getFCmpCode, which turns an
2109/// opcode and two operands into either a FCmp instruction. isordered is passed
2110/// in to determine which kind of predicate to use in the new fcmp instruction.
2111static Value *getFCmpValue(bool isordered, unsigned code,
2112                           Value *LHS, Value *RHS) {
2113  switch (code) {
2114  default: llvm_unreachable("Illegal FCmp code!");
2115  case  0:
2116    if (isordered)
2117      return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS);
2118    else
2119      return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS);
2120  case  1:
2121    if (isordered)
2122      return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS);
2123    else
2124      return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS);
2125  case  2:
2126    if (isordered)
2127      return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS);
2128    else
2129      return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS);
2130  case  3:
2131    if (isordered)
2132      return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS);
2133    else
2134      return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS);
2135  case  4:
2136    if (isordered)
2137      return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS);
2138    else
2139      return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS);
2140  case  5:
2141    if (isordered)
2142      return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS);
2143    else
2144      return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS);
2145  case  6:
2146    if (isordered)
2147      return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS);
2148    else
2149      return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS);
2150  case  7: return ConstantInt::getTrue(LHS->getContext());
2151  }
2152}
2153
2154/// PredicatesFoldable - Return true if both predicates match sign or if at
2155/// least one of them is an equality comparison (which is signless).
2156static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
2157  return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
2158         (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
2159         (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
2160}
2161
2162namespace {
2163// FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
2164struct FoldICmpLogical {
2165  InstCombiner &IC;
2166  Value *LHS, *RHS;
2167  ICmpInst::Predicate pred;
2168  FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI)
2169    : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)),
2170      pred(ICI->getPredicate()) {}
2171  bool shouldApply(Value *V) const {
2172    if (ICmpInst *ICI = dyn_cast<ICmpInst>(V))
2173      if (PredicatesFoldable(pred, ICI->getPredicate()))
2174        return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) ||
2175                (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS));
2176    return false;
2177  }
2178  Instruction *apply(Instruction &Log) const {
2179    ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0));
2180    if (ICI->getOperand(0) != LHS) {
2181      assert(ICI->getOperand(1) == LHS);
2182      ICI->swapOperands();  // Swap the LHS and RHS of the ICmp
2183    }
2184
2185    ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1));
2186    unsigned LHSCode = getICmpCode(ICI);
2187    unsigned RHSCode = getICmpCode(RHSICI);
2188    unsigned Code;
2189    switch (Log.getOpcode()) {
2190    case Instruction::And: Code = LHSCode & RHSCode; break;
2191    case Instruction::Or:  Code = LHSCode | RHSCode; break;
2192    case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
2193    default: llvm_unreachable("Illegal logical opcode!"); return 0;
2194    }
2195
2196    bool isSigned = RHSICI->isSigned() || ICI->isSigned();
2197    Value *RV = getICmpValue(isSigned, Code, LHS, RHS);
2198    if (Instruction *I = dyn_cast<Instruction>(RV))
2199      return I;
2200    // Otherwise, it's a constant boolean value...
2201    return IC.ReplaceInstUsesWith(Log, RV);
2202  }
2203};
2204} // end anonymous namespace
2205
2206// OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
2207// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
2208// guaranteed to be a binary operator.
2209Instruction *InstCombiner::OptAndOp(Instruction *Op,
2210                                    ConstantInt *OpRHS,
2211                                    ConstantInt *AndRHS,
2212                                    BinaryOperator &TheAnd) {
2213  Value *X = Op->getOperand(0);
2214  Constant *Together = 0;
2215  if (!Op->isShift())
2216    Together = ConstantExpr::getAnd(AndRHS, OpRHS);
2217
2218  switch (Op->getOpcode()) {
2219  case Instruction::Xor:
2220    if (Op->hasOneUse()) {
2221      // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
2222      Value *And = Builder->CreateAnd(X, AndRHS);
2223      And->takeName(Op);
2224      return BinaryOperator::CreateXor(And, Together);
2225    }
2226    break;
2227  case Instruction::Or:
2228    if (Together == AndRHS) // (X | C) & C --> C
2229      return ReplaceInstUsesWith(TheAnd, AndRHS);
2230
2231    if (Op->hasOneUse() && Together != OpRHS) {
2232      // (X | C1) & C2 --> (X | (C1&C2)) & C2
2233      Value *Or = Builder->CreateOr(X, Together);
2234      Or->takeName(Op);
2235      return BinaryOperator::CreateAnd(Or, AndRHS);
2236    }
2237    break;
2238  case Instruction::Add:
2239    if (Op->hasOneUse()) {
2240      // Adding a one to a single bit bit-field should be turned into an XOR
2241      // of the bit.  First thing to check is to see if this AND is with a
2242      // single bit constant.
2243      const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
2244
2245      // If there is only one bit set...
2246      if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
2247        // Ok, at this point, we know that we are masking the result of the
2248        // ADD down to exactly one bit.  If the constant we are adding has
2249        // no bits set below this bit, then we can eliminate the ADD.
2250        const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
2251
2252        // Check to see if any bits below the one bit set in AndRHSV are set.
2253        if ((AddRHS & (AndRHSV-1)) == 0) {
2254          // If not, the only thing that can effect the output of the AND is
2255          // the bit specified by AndRHSV.  If that bit is set, the effect of
2256          // the XOR is to toggle the bit.  If it is clear, then the ADD has
2257          // no effect.
2258          if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
2259            TheAnd.setOperand(0, X);
2260            return &TheAnd;
2261          } else {
2262            // Pull the XOR out of the AND.
2263            Value *NewAnd = Builder->CreateAnd(X, AndRHS);
2264            NewAnd->takeName(Op);
2265            return BinaryOperator::CreateXor(NewAnd, AndRHS);
2266          }
2267        }
2268      }
2269    }
2270    break;
2271
2272  case Instruction::Shl: {
2273    // We know that the AND will not produce any of the bits shifted in, so if
2274    // the anded constant includes them, clear them now!
2275    //
2276    uint32_t BitWidth = AndRHS->getType()->getBitWidth();
2277    uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
2278    APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
2279    ConstantInt *CI = ConstantInt::get(AndRHS->getContext(),
2280                                       AndRHS->getValue() & ShlMask);
2281
2282    if (CI->getValue() == ShlMask) {
2283    // Masking out bits that the shift already masks
2284      return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
2285    } else if (CI != AndRHS) {                  // Reducing bits set in and.
2286      TheAnd.setOperand(1, CI);
2287      return &TheAnd;
2288    }
2289    break;
2290  }
2291  case Instruction::LShr: {
2292    // We know that the AND will not produce any of the bits shifted in, so if
2293    // the anded constant includes them, clear them now!  This only applies to
2294    // unsigned shifts, because a signed shr may bring in set bits!
2295    //
2296    uint32_t BitWidth = AndRHS->getType()->getBitWidth();
2297    uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
2298    APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
2299    ConstantInt *CI = ConstantInt::get(Op->getContext(),
2300                                       AndRHS->getValue() & ShrMask);
2301
2302    if (CI->getValue() == ShrMask) {
2303    // Masking out bits that the shift already masks.
2304      return ReplaceInstUsesWith(TheAnd, Op);
2305    } else if (CI != AndRHS) {
2306      TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
2307      return &TheAnd;
2308    }
2309    break;
2310  }
2311  case Instruction::AShr:
2312    // Signed shr.
2313    // See if this is shifting in some sign extension, then masking it out
2314    // with an and.
2315    if (Op->hasOneUse()) {
2316      uint32_t BitWidth = AndRHS->getType()->getBitWidth();
2317      uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
2318      APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
2319      Constant *C = ConstantInt::get(Op->getContext(),
2320                                     AndRHS->getValue() & ShrMask);
2321      if (C == AndRHS) {          // Masking out bits shifted in.
2322        // (Val ashr C1) & C2 -> (Val lshr C1) & C2
2323        // Make the argument unsigned.
2324        Value *ShVal = Op->getOperand(0);
2325        ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
2326        return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
2327      }
2328    }
2329    break;
2330  }
2331  return 0;
2332}
2333
2334
2335/// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
2336/// true, otherwise (V < Lo || V >= Hi).  In pratice, we emit the more efficient
2337/// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi. isSigned indicates
2338/// whether to treat the V, Lo and HI as signed or not. IB is the location to
2339/// insert new instructions.
2340Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
2341                                           bool isSigned, bool Inside,
2342                                           Instruction &IB) {
2343  assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
2344            ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
2345         "Lo is not <= Hi in range emission code!");
2346
2347  if (Inside) {
2348    if (Lo == Hi)  // Trivially false.
2349      return new ICmpInst(ICmpInst::ICMP_NE, V, V);
2350
2351    // V >= Min && V < Hi --> V < Hi
2352    if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
2353      ICmpInst::Predicate pred = (isSigned ?
2354        ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
2355      return new ICmpInst(pred, V, Hi);
2356    }
2357
2358    // Emit V-Lo <u Hi-Lo
2359    Constant *NegLo = ConstantExpr::getNeg(Lo);
2360    Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
2361    Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
2362    return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
2363  }
2364
2365  if (Lo == Hi)  // Trivially true.
2366    return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
2367
2368  // V < Min || V >= Hi -> V > Hi-1
2369  Hi = SubOne(cast<ConstantInt>(Hi));
2370  if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
2371    ICmpInst::Predicate pred = (isSigned ?
2372        ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
2373    return new ICmpInst(pred, V, Hi);
2374  }
2375
2376  // Emit V-Lo >u Hi-1-Lo
2377  // Note that Hi has already had one subtracted from it, above.
2378  ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
2379  Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
2380  Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
2381  return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
2382}
2383
2384// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
2385// any number of 0s on either side.  The 1s are allowed to wrap from LSB to
2386// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
2387// not, since all 1s are not contiguous.
2388static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
2389  const APInt& V = Val->getValue();
2390  uint32_t BitWidth = Val->getType()->getBitWidth();
2391  if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
2392
2393  // look for the first zero bit after the run of ones
2394  MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
2395  // look for the first non-zero bit
2396  ME = V.getActiveBits();
2397  return true;
2398}
2399
2400/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
2401/// where isSub determines whether the operator is a sub.  If we can fold one of
2402/// the following xforms:
2403///
2404/// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
2405/// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
2406/// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
2407///
2408/// return (A +/- B).
2409///
2410Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
2411                                        ConstantInt *Mask, bool isSub,
2412                                        Instruction &I) {
2413  Instruction *LHSI = dyn_cast<Instruction>(LHS);
2414  if (!LHSI || LHSI->getNumOperands() != 2 ||
2415      !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
2416
2417  ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
2418
2419  switch (LHSI->getOpcode()) {
2420  default: return 0;
2421  case Instruction::And:
2422    if (ConstantExpr::getAnd(N, Mask) == Mask) {
2423      // If the AndRHS is a power of two minus one (0+1+), this is simple.
2424      if ((Mask->getValue().countLeadingZeros() +
2425           Mask->getValue().countPopulation()) ==
2426          Mask->getValue().getBitWidth())
2427        break;
2428
2429      // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
2430      // part, we don't need any explicit masks to take them out of A.  If that
2431      // is all N is, ignore it.
2432      uint32_t MB = 0, ME = 0;
2433      if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
2434        uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
2435        APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
2436        if (MaskedValueIsZero(RHS, Mask))
2437          break;
2438      }
2439    }
2440    return 0;
2441  case Instruction::Or:
2442  case Instruction::Xor:
2443    // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
2444    if ((Mask->getValue().countLeadingZeros() +
2445         Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
2446        && ConstantExpr::getAnd(N, Mask)->isNullValue())
2447      break;
2448    return 0;
2449  }
2450
2451  if (isSub)
2452    return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
2453  return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
2454}
2455
2456/// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
2457Instruction *InstCombiner::FoldAndOfICmps(Instruction &I,
2458                                          ICmpInst *LHS, ICmpInst *RHS) {
2459  Value *Val, *Val2;
2460  ConstantInt *LHSCst, *RHSCst;
2461  ICmpInst::Predicate LHSCC, RHSCC;
2462
2463  // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
2464  if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
2465                         m_ConstantInt(LHSCst))) ||
2466      !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
2467                         m_ConstantInt(RHSCst))))
2468    return 0;
2469
2470  if (LHSCst == RHSCst && LHSCC == RHSCC) {
2471    // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
2472    // where C is a power of 2
2473    if (LHSCC == ICmpInst::ICMP_ULT &&
2474        LHSCst->getValue().isPowerOf2()) {
2475      Value *NewOr = Builder->CreateOr(Val, Val2);
2476      return new ICmpInst(LHSCC, NewOr, LHSCst);
2477    }
2478
2479    // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
2480    if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
2481      Value *NewOr = Builder->CreateOr(Val, Val2);
2482      return new ICmpInst(LHSCC, NewOr, LHSCst);
2483    }
2484  }
2485
2486  // From here on, we only handle:
2487  //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
2488  if (Val != Val2) return 0;
2489
2490  // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
2491  if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
2492      RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
2493      LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
2494      RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
2495    return 0;
2496
2497  // We can't fold (ugt x, C) & (sgt x, C2).
2498  if (!PredicatesFoldable(LHSCC, RHSCC))
2499    return 0;
2500
2501  // Ensure that the larger constant is on the RHS.
2502  bool ShouldSwap;
2503  if (CmpInst::isSigned(LHSCC) ||
2504      (ICmpInst::isEquality(LHSCC) &&
2505       CmpInst::isSigned(RHSCC)))
2506    ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
2507  else
2508    ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
2509
2510  if (ShouldSwap) {
2511    std::swap(LHS, RHS);
2512    std::swap(LHSCst, RHSCst);
2513    std::swap(LHSCC, RHSCC);
2514  }
2515
2516  // At this point, we know we have have two icmp instructions
2517  // comparing a value against two constants and and'ing the result
2518  // together.  Because of the above check, we know that we only have
2519  // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
2520  // (from the FoldICmpLogical check above), that the two constants
2521  // are not equal and that the larger constant is on the RHS
2522  assert(LHSCst != RHSCst && "Compares not folded above?");
2523
2524  switch (LHSCC) {
2525  default: llvm_unreachable("Unknown integer condition code!");
2526  case ICmpInst::ICMP_EQ:
2527    switch (RHSCC) {
2528    default: llvm_unreachable("Unknown integer condition code!");
2529    case ICmpInst::ICMP_EQ:         // (X == 13 & X == 15) -> false
2530    case ICmpInst::ICMP_UGT:        // (X == 13 & X >  15) -> false
2531    case ICmpInst::ICMP_SGT:        // (X == 13 & X >  15) -> false
2532      return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2533    case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
2534    case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
2535    case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
2536      return ReplaceInstUsesWith(I, LHS);
2537    }
2538  case ICmpInst::ICMP_NE:
2539    switch (RHSCC) {
2540    default: llvm_unreachable("Unknown integer condition code!");
2541    case ICmpInst::ICMP_ULT:
2542      if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
2543        return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst);
2544      break;                        // (X != 13 & X u< 15) -> no change
2545    case ICmpInst::ICMP_SLT:
2546      if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
2547        return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst);
2548      break;                        // (X != 13 & X s< 15) -> no change
2549    case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
2550    case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
2551    case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
2552      return ReplaceInstUsesWith(I, RHS);
2553    case ICmpInst::ICMP_NE:
2554      if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
2555        Constant *AddCST = ConstantExpr::getNeg(LHSCst);
2556        Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
2557        return new ICmpInst(ICmpInst::ICMP_UGT, Add,
2558                            ConstantInt::get(Add->getType(), 1));
2559      }
2560      break;                        // (X != 13 & X != 15) -> no change
2561    }
2562    break;
2563  case ICmpInst::ICMP_ULT:
2564    switch (RHSCC) {
2565    default: llvm_unreachable("Unknown integer condition code!");
2566    case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
2567    case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
2568      return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2569    case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
2570      break;
2571    case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
2572    case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
2573      return ReplaceInstUsesWith(I, LHS);
2574    case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
2575      break;
2576    }
2577    break;
2578  case ICmpInst::ICMP_SLT:
2579    switch (RHSCC) {
2580    default: llvm_unreachable("Unknown integer condition code!");
2581    case ICmpInst::ICMP_EQ:         // (X s< 13 & X == 15) -> false
2582    case ICmpInst::ICMP_SGT:        // (X s< 13 & X s> 15) -> false
2583      return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2584    case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
2585      break;
2586    case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
2587    case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
2588      return ReplaceInstUsesWith(I, LHS);
2589    case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
2590      break;
2591    }
2592    break;
2593  case ICmpInst::ICMP_UGT:
2594    switch (RHSCC) {
2595    default: llvm_unreachable("Unknown integer condition code!");
2596    case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
2597    case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
2598      return ReplaceInstUsesWith(I, RHS);
2599    case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
2600      break;
2601    case ICmpInst::ICMP_NE:
2602      if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
2603        return new ICmpInst(LHSCC, Val, RHSCst);
2604      break;                        // (X u> 13 & X != 15) -> no change
2605    case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
2606      return InsertRangeTest(Val, AddOne(LHSCst),
2607                             RHSCst, false, true, I);
2608    case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
2609      break;
2610    }
2611    break;
2612  case ICmpInst::ICMP_SGT:
2613    switch (RHSCC) {
2614    default: llvm_unreachable("Unknown integer condition code!");
2615    case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
2616    case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
2617      return ReplaceInstUsesWith(I, RHS);
2618    case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
2619      break;
2620    case ICmpInst::ICMP_NE:
2621      if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
2622        return new ICmpInst(LHSCC, Val, RHSCst);
2623      break;                        // (X s> 13 & X != 15) -> no change
2624    case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
2625      return InsertRangeTest(Val, AddOne(LHSCst),
2626                             RHSCst, true, true, I);
2627    case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
2628      break;
2629    }
2630    break;
2631  }
2632
2633  return 0;
2634}
2635
2636Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS,
2637                                          FCmpInst *RHS) {
2638
2639  if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
2640      RHS->getPredicate() == FCmpInst::FCMP_ORD) {
2641    // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
2642    if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
2643      if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
2644        // If either of the constants are nans, then the whole thing returns
2645        // false.
2646        if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
2647          return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2648        return new FCmpInst(FCmpInst::FCMP_ORD,
2649                            LHS->getOperand(0), RHS->getOperand(0));
2650      }
2651
2652    // Handle vector zeros.  This occurs because the canonical form of
2653    // "fcmp ord x,x" is "fcmp ord x, 0".
2654    if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
2655        isa<ConstantAggregateZero>(RHS->getOperand(1)))
2656      return new FCmpInst(FCmpInst::FCMP_ORD,
2657                          LHS->getOperand(0), RHS->getOperand(0));
2658    return 0;
2659  }
2660
2661  Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
2662  Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
2663  FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
2664
2665
2666  if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
2667    // Swap RHS operands to match LHS.
2668    Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
2669    std::swap(Op1LHS, Op1RHS);
2670  }
2671
2672  if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
2673    // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
2674    if (Op0CC == Op1CC)
2675      return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
2676
2677    if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
2678      return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2679    if (Op0CC == FCmpInst::FCMP_TRUE)
2680      return ReplaceInstUsesWith(I, RHS);
2681    if (Op1CC == FCmpInst::FCMP_TRUE)
2682      return ReplaceInstUsesWith(I, LHS);
2683
2684    bool Op0Ordered;
2685    bool Op1Ordered;
2686    unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
2687    unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
2688    if (Op1Pred == 0) {
2689      std::swap(LHS, RHS);
2690      std::swap(Op0Pred, Op1Pred);
2691      std::swap(Op0Ordered, Op1Ordered);
2692    }
2693    if (Op0Pred == 0) {
2694      // uno && ueq -> uno && (uno || eq) -> ueq
2695      // ord && olt -> ord && (ord && lt) -> olt
2696      if (Op0Ordered == Op1Ordered)
2697        return ReplaceInstUsesWith(I, RHS);
2698
2699      // uno && oeq -> uno && (ord && eq) -> false
2700      // uno && ord -> false
2701      if (!Op0Ordered)
2702        return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2703      // ord && ueq -> ord && (uno || eq) -> oeq
2704      return cast<Instruction>(getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS));
2705    }
2706  }
2707
2708  return 0;
2709}
2710
2711
2712Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
2713  bool Changed = SimplifyCommutative(I);
2714  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2715
2716  if (Value *V = SimplifyAndInst(Op0, Op1, TD))
2717    return ReplaceInstUsesWith(I, V);
2718
2719  // See if we can simplify any instructions used by the instruction whose sole
2720  // purpose is to compute bits we don't care about.
2721  if (SimplifyDemandedInstructionBits(I))
2722    return &I;
2723
2724  if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
2725    const APInt &AndRHSMask = AndRHS->getValue();
2726    APInt NotAndRHS(~AndRHSMask);
2727
2728    // Optimize a variety of ((val OP C1) & C2) combinations...
2729    if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2730      Value *Op0LHS = Op0I->getOperand(0);
2731      Value *Op0RHS = Op0I->getOperand(1);
2732      switch (Op0I->getOpcode()) {
2733      default: break;
2734      case Instruction::Xor:
2735      case Instruction::Or:
2736        // If the mask is only needed on one incoming arm, push it up.
2737        if (!Op0I->hasOneUse()) break;
2738
2739        if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
2740          // Not masking anything out for the LHS, move to RHS.
2741          Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
2742                                             Op0RHS->getName()+".masked");
2743          return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
2744        }
2745        if (!isa<Constant>(Op0RHS) &&
2746            MaskedValueIsZero(Op0RHS, NotAndRHS)) {
2747          // Not masking anything out for the RHS, move to LHS.
2748          Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
2749                                             Op0LHS->getName()+".masked");
2750          return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
2751        }
2752
2753        break;
2754      case Instruction::Add:
2755        // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
2756        // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
2757        // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
2758        if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
2759          return BinaryOperator::CreateAnd(V, AndRHS);
2760        if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
2761          return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
2762        break;
2763
2764      case Instruction::Sub:
2765        // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
2766        // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
2767        // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
2768        if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
2769          return BinaryOperator::CreateAnd(V, AndRHS);
2770
2771        // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
2772        // has 1's for all bits that the subtraction with A might affect.
2773        if (Op0I->hasOneUse()) {
2774          uint32_t BitWidth = AndRHSMask.getBitWidth();
2775          uint32_t Zeros = AndRHSMask.countLeadingZeros();
2776          APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
2777
2778          ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
2779          if (!(A && A->isZero()) &&               // avoid infinite recursion.
2780              MaskedValueIsZero(Op0LHS, Mask)) {
2781            Value *NewNeg = Builder->CreateNeg(Op0RHS);
2782            return BinaryOperator::CreateAnd(NewNeg, AndRHS);
2783          }
2784        }
2785        break;
2786
2787      case Instruction::Shl:
2788      case Instruction::LShr:
2789        // (1 << x) & 1 --> zext(x == 0)
2790        // (1 >> x) & 1 --> zext(x == 0)
2791        if (AndRHSMask == 1 && Op0LHS == AndRHS) {
2792          Value *NewICmp =
2793            Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
2794          return new ZExtInst(NewICmp, I.getType());
2795        }
2796        break;
2797      }
2798
2799      if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
2800        if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
2801          return Res;
2802    } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
2803      // If this is an integer truncation or change from signed-to-unsigned, and
2804      // if the source is an and/or with immediate, transform it.  This
2805      // frequently occurs for bitfield accesses.
2806      if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
2807        if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
2808            CastOp->getNumOperands() == 2)
2809          if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))){
2810            if (CastOp->getOpcode() == Instruction::And) {
2811              // Change: and (cast (and X, C1) to T), C2
2812              // into  : and (cast X to T), trunc_or_bitcast(C1)&C2
2813              // This will fold the two constants together, which may allow
2814              // other simplifications.
2815              Value *NewCast = Builder->CreateTruncOrBitCast(
2816                CastOp->getOperand(0), I.getType(),
2817                CastOp->getName()+".shrunk");
2818              // trunc_or_bitcast(C1)&C2
2819              Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
2820              C3 = ConstantExpr::getAnd(C3, AndRHS);
2821              return BinaryOperator::CreateAnd(NewCast, C3);
2822            } else if (CastOp->getOpcode() == Instruction::Or) {
2823              // Change: and (cast (or X, C1) to T), C2
2824              // into  : trunc(C1)&C2 iff trunc(C1)&C2 == C2
2825              Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
2826              if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)
2827                // trunc(C1)&C2
2828                return ReplaceInstUsesWith(I, AndRHS);
2829            }
2830          }
2831      }
2832    }
2833
2834    // Try to fold constant and into select arguments.
2835    if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2836      if (Instruction *R = FoldOpIntoSelect(I, SI))
2837        return R;
2838    if (isa<PHINode>(Op0))
2839      if (Instruction *NV = FoldOpIntoPhi(I))
2840        return NV;
2841  }
2842
2843
2844  // (~A & ~B) == (~(A | B)) - De Morgan's Law
2845  if (Value *Op0NotVal = dyn_castNotVal(Op0))
2846    if (Value *Op1NotVal = dyn_castNotVal(Op1))
2847      if (Op0->hasOneUse() && Op1->hasOneUse()) {
2848        Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
2849                                      I.getName()+".demorgan");
2850        return BinaryOperator::CreateNot(Or);
2851      }
2852
2853  {
2854    Value *A = 0, *B = 0, *C = 0, *D = 0;
2855    // (A|B) & ~(A&B) -> A^B
2856    if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
2857        match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
2858        ((A == C && B == D) || (A == D && B == C)))
2859      return BinaryOperator::CreateXor(A, B);
2860
2861    // ~(A&B) & (A|B) -> A^B
2862    if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
2863        match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
2864        ((A == C && B == D) || (A == D && B == C)))
2865      return BinaryOperator::CreateXor(A, B);
2866
2867    if (Op0->hasOneUse() &&
2868        match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
2869      if (A == Op1) {                                // (A^B)&A -> A&(A^B)
2870        I.swapOperands();     // Simplify below
2871        std::swap(Op0, Op1);
2872      } else if (B == Op1) {                         // (A^B)&B -> B&(B^A)
2873        cast<BinaryOperator>(Op0)->swapOperands();
2874        I.swapOperands();     // Simplify below
2875        std::swap(Op0, Op1);
2876      }
2877    }
2878
2879    if (Op1->hasOneUse() &&
2880        match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2881      if (B == Op0) {                                // B&(A^B) -> B&(B^A)
2882        cast<BinaryOperator>(Op1)->swapOperands();
2883        std::swap(A, B);
2884      }
2885      if (A == Op0)                                // A&(A^B) -> A & ~B
2886        return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp"));
2887    }
2888
2889    // (A&((~A)|B)) -> A&B
2890    if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
2891        match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
2892      return BinaryOperator::CreateAnd(A, Op1);
2893    if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
2894        match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
2895      return BinaryOperator::CreateAnd(A, Op0);
2896  }
2897
2898  if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
2899    // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
2900    if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
2901      return R;
2902
2903    if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
2904      if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS))
2905        return Res;
2906  }
2907
2908  // fold (and (cast A), (cast B)) -> (cast (and A, B))
2909  if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
2910    if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2911      if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
2912        const Type *SrcTy = Op0C->getOperand(0)->getType();
2913        if (SrcTy == Op1C->getOperand(0)->getType() &&
2914            SrcTy->isIntOrIntVector() &&
2915            // Only do this if the casts both really cause code to be generated.
2916            ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
2917                              I.getType()) &&
2918            ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
2919                              I.getType())) {
2920          Value *NewOp = Builder->CreateAnd(Op0C->getOperand(0),
2921                                            Op1C->getOperand(0), I.getName());
2922          return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2923        }
2924      }
2925
2926  // (X >> Z) & (Y >> Z)  -> (X&Y) >> Z  for all shifts.
2927  if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
2928    if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
2929      if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
2930          SI0->getOperand(1) == SI1->getOperand(1) &&
2931          (SI0->hasOneUse() || SI1->hasOneUse())) {
2932        Value *NewOp =
2933          Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
2934                             SI0->getName());
2935        return BinaryOperator::Create(SI1->getOpcode(), NewOp,
2936                                      SI1->getOperand(1));
2937      }
2938  }
2939
2940  // If and'ing two fcmp, try combine them into one.
2941  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
2942    if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2943      if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS))
2944        return Res;
2945  }
2946
2947  return Changed ? &I : 0;
2948}
2949
2950/// CollectBSwapParts - Analyze the specified subexpression and see if it is
2951/// capable of providing pieces of a bswap.  The subexpression provides pieces
2952/// of a bswap if it is proven that each of the non-zero bytes in the output of
2953/// the expression came from the corresponding "byte swapped" byte in some other
2954/// value.  For example, if the current subexpression is "(shl i32 %X, 24)" then
2955/// we know that the expression deposits the low byte of %X into the high byte
2956/// of the bswap result and that all other bytes are zero.  This expression is
2957/// accepted, the high byte of ByteValues is set to X to indicate a correct
2958/// match.
2959///
2960/// This function returns true if the match was unsuccessful and false if so.
2961/// On entry to the function the "OverallLeftShift" is a signed integer value
2962/// indicating the number of bytes that the subexpression is later shifted.  For
2963/// example, if the expression is later right shifted by 16 bits, the
2964/// OverallLeftShift value would be -2 on entry.  This is used to specify which
2965/// byte of ByteValues is actually being set.
2966///
2967/// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
2968/// byte is masked to zero by a user.  For example, in (X & 255), X will be
2969/// processed with a bytemask of 1.  Because bytemask is 32-bits, this limits
2970/// this function to working on up to 32-byte (256 bit) values.  ByteMask is
2971/// always in the local (OverallLeftShift) coordinate space.
2972///
2973static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
2974                              SmallVector<Value*, 8> &ByteValues) {
2975  if (Instruction *I = dyn_cast<Instruction>(V)) {
2976    // If this is an or instruction, it may be an inner node of the bswap.
2977    if (I->getOpcode() == Instruction::Or) {
2978      return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
2979                               ByteValues) ||
2980             CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
2981                               ByteValues);
2982    }
2983
2984    // If this is a logical shift by a constant multiple of 8, recurse with
2985    // OverallLeftShift and ByteMask adjusted.
2986    if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
2987      unsigned ShAmt =
2988        cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
2989      // Ensure the shift amount is defined and of a byte value.
2990      if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
2991        return true;
2992
2993      unsigned ByteShift = ShAmt >> 3;
2994      if (I->getOpcode() == Instruction::Shl) {
2995        // X << 2 -> collect(X, +2)
2996        OverallLeftShift += ByteShift;
2997        ByteMask >>= ByteShift;
2998      } else {
2999        // X >>u 2 -> collect(X, -2)
3000        OverallLeftShift -= ByteShift;
3001        ByteMask <<= ByteShift;
3002        ByteMask &= (~0U >> (32-ByteValues.size()));
3003      }
3004
3005      if (OverallLeftShift >= (int)ByteValues.size()) return true;
3006      if (OverallLeftShift <= -(int)ByteValues.size()) return true;
3007
3008      return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
3009                               ByteValues);
3010    }
3011
3012    // If this is a logical 'and' with a mask that clears bytes, clear the
3013    // corresponding bytes in ByteMask.
3014    if (I->getOpcode() == Instruction::And &&
3015        isa<ConstantInt>(I->getOperand(1))) {
3016      // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
3017      unsigned NumBytes = ByteValues.size();
3018      APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
3019      const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
3020
3021      for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
3022        // If this byte is masked out by a later operation, we don't care what
3023        // the and mask is.
3024        if ((ByteMask & (1 << i)) == 0)
3025          continue;
3026
3027        // If the AndMask is all zeros for this byte, clear the bit.
3028        APInt MaskB = AndMask & Byte;
3029        if (MaskB == 0) {
3030          ByteMask &= ~(1U << i);
3031          continue;
3032        }
3033
3034        // If the AndMask is not all ones for this byte, it's not a bytezap.
3035        if (MaskB != Byte)
3036          return true;
3037
3038        // Otherwise, this byte is kept.
3039      }
3040
3041      return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
3042                               ByteValues);
3043    }
3044  }
3045
3046  // Okay, we got to something that isn't a shift, 'or' or 'and'.  This must be
3047  // the input value to the bswap.  Some observations: 1) if more than one byte
3048  // is demanded from this input, then it could not be successfully assembled
3049  // into a byteswap.  At least one of the two bytes would not be aligned with
3050  // their ultimate destination.
3051  if (!isPowerOf2_32(ByteMask)) return true;
3052  unsigned InputByteNo = CountTrailingZeros_32(ByteMask);
3053
3054  // 2) The input and ultimate destinations must line up: if byte 3 of an i32
3055  // is demanded, it needs to go into byte 0 of the result.  This means that the
3056  // byte needs to be shifted until it lands in the right byte bucket.  The
3057  // shift amount depends on the position: if the byte is coming from the high
3058  // part of the value (e.g. byte 3) then it must be shifted right.  If from the
3059  // low part, it must be shifted left.
3060  unsigned DestByteNo = InputByteNo + OverallLeftShift;
3061  if (InputByteNo < ByteValues.size()/2) {
3062    if (ByteValues.size()-1-DestByteNo != InputByteNo)
3063      return true;
3064  } else {
3065    if (ByteValues.size()-1-DestByteNo != InputByteNo)
3066      return true;
3067  }
3068
3069  // If the destination byte value is already defined, the values are or'd
3070  // together, which isn't a bswap (unless it's an or of the same bits).
3071  if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
3072    return true;
3073  ByteValues[DestByteNo] = V;
3074  return false;
3075}
3076
3077/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
3078/// If so, insert the new bswap intrinsic and return it.
3079Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
3080  const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
3081  if (!ITy || ITy->getBitWidth() % 16 ||
3082      // ByteMask only allows up to 32-byte values.
3083      ITy->getBitWidth() > 32*8)
3084    return 0;   // Can only bswap pairs of bytes.  Can't do vectors.
3085
3086  /// ByteValues - For each byte of the result, we keep track of which value
3087  /// defines each byte.
3088  SmallVector<Value*, 8> ByteValues;
3089  ByteValues.resize(ITy->getBitWidth()/8);
3090
3091  // Try to find all the pieces corresponding to the bswap.
3092  uint32_t ByteMask = ~0U >> (32-ByteValues.size());
3093  if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
3094    return 0;
3095
3096  // Check to see if all of the bytes come from the same value.
3097  Value *V = ByteValues[0];
3098  if (V == 0) return 0;  // Didn't find a byte?  Must be zero.
3099
3100  // Check to make sure that all of the bytes come from the same value.
3101  for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
3102    if (ByteValues[i] != V)
3103      return 0;
3104  const Type *Tys[] = { ITy };
3105  Module *M = I.getParent()->getParent()->getParent();
3106  Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
3107  return CallInst::Create(F, V);
3108}
3109
3110/// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D).  Check
3111/// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
3112/// we can simplify this expression to "cond ? C : D or B".
3113static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
3114                                         Value *C, Value *D) {
3115  // If A is not a select of -1/0, this cannot match.
3116  Value *Cond = 0;
3117  if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond))))
3118    return 0;
3119
3120  // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
3121  if (match(D, m_SelectCst<0, -1>(m_Specific(Cond))))
3122    return SelectInst::Create(Cond, C, B);
3123  if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
3124    return SelectInst::Create(Cond, C, B);
3125  // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
3126  if (match(B, m_SelectCst<0, -1>(m_Specific(Cond))))
3127    return SelectInst::Create(Cond, C, D);
3128  if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
3129    return SelectInst::Create(Cond, C, D);
3130  return 0;
3131}
3132
3133/// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
3134Instruction *InstCombiner::FoldOrOfICmps(Instruction &I,
3135                                         ICmpInst *LHS, ICmpInst *RHS) {
3136  Value *Val, *Val2;
3137  ConstantInt *LHSCst, *RHSCst;
3138  ICmpInst::Predicate LHSCC, RHSCC;
3139
3140  // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
3141  if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) ||
3142      !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst))))
3143    return 0;
3144
3145
3146  // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
3147  if (LHSCst == RHSCst && LHSCC == RHSCC &&
3148      LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
3149    Value *NewOr = Builder->CreateOr(Val, Val2);
3150    return new ICmpInst(LHSCC, NewOr, LHSCst);
3151  }
3152
3153  // From here on, we only handle:
3154  //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
3155  if (Val != Val2) return 0;
3156
3157  // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
3158  if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
3159      RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
3160      LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
3161      RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
3162    return 0;
3163
3164  // We can't fold (ugt x, C) | (sgt x, C2).
3165  if (!PredicatesFoldable(LHSCC, RHSCC))
3166    return 0;
3167
3168  // Ensure that the larger constant is on the RHS.
3169  bool ShouldSwap;
3170  if (CmpInst::isSigned(LHSCC) ||
3171      (ICmpInst::isEquality(LHSCC) &&
3172       CmpInst::isSigned(RHSCC)))
3173    ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
3174  else
3175    ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
3176
3177  if (ShouldSwap) {
3178    std::swap(LHS, RHS);
3179    std::swap(LHSCst, RHSCst);
3180    std::swap(LHSCC, RHSCC);
3181  }
3182
3183  // At this point, we know we have have two icmp instructions
3184  // comparing a value against two constants and or'ing the result
3185  // together.  Because of the above check, we know that we only have
3186  // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
3187  // FoldICmpLogical check above), that the two constants are not
3188  // equal.
3189  assert(LHSCst != RHSCst && "Compares not folded above?");
3190
3191  switch (LHSCC) {
3192  default: llvm_unreachable("Unknown integer condition code!");
3193  case ICmpInst::ICMP_EQ:
3194    switch (RHSCC) {
3195    default: llvm_unreachable("Unknown integer condition code!");
3196    case ICmpInst::ICMP_EQ:
3197      if (LHSCst == SubOne(RHSCst)) {
3198        // (X == 13 | X == 14) -> X-13 <u 2
3199        Constant *AddCST = ConstantExpr::getNeg(LHSCst);
3200        Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
3201        AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
3202        return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
3203      }
3204      break;                         // (X == 13 | X == 15) -> no change
3205    case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
3206    case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
3207      break;
3208    case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
3209    case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
3210    case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
3211      return ReplaceInstUsesWith(I, RHS);
3212    }
3213    break;
3214  case ICmpInst::ICMP_NE:
3215    switch (RHSCC) {
3216    default: llvm_unreachable("Unknown integer condition code!");
3217    case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
3218    case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
3219    case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
3220      return ReplaceInstUsesWith(I, LHS);
3221    case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
3222    case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
3223    case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
3224      return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
3225    }
3226    break;
3227  case ICmpInst::ICMP_ULT:
3228    switch (RHSCC) {
3229    default: llvm_unreachable("Unknown integer condition code!");
3230    case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
3231      break;
3232    case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
3233      // If RHSCst is [us]MAXINT, it is always false.  Not handling
3234      // this can cause overflow.
3235      if (RHSCst->isMaxValue(false))
3236        return ReplaceInstUsesWith(I, LHS);
3237      return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
3238                             false, false, I);
3239    case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
3240      break;
3241    case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
3242    case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
3243      return ReplaceInstUsesWith(I, RHS);
3244    case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
3245      break;
3246    }
3247    break;
3248  case ICmpInst::ICMP_SLT:
3249    switch (RHSCC) {
3250    default: llvm_unreachable("Unknown integer condition code!");
3251    case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
3252      break;
3253    case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
3254      // If RHSCst is [us]MAXINT, it is always false.  Not handling
3255      // this can cause overflow.
3256      if (RHSCst->isMaxValue(true))
3257        return ReplaceInstUsesWith(I, LHS);
3258      return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
3259                             true, false, I);
3260    case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
3261      break;
3262    case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
3263    case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
3264      return ReplaceInstUsesWith(I, RHS);
3265    case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
3266      break;
3267    }
3268    break;
3269  case ICmpInst::ICMP_UGT:
3270    switch (RHSCC) {
3271    default: llvm_unreachable("Unknown integer condition code!");
3272    case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
3273    case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
3274      return ReplaceInstUsesWith(I, LHS);
3275    case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
3276      break;
3277    case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
3278    case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
3279      return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
3280    case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
3281      break;
3282    }
3283    break;
3284  case ICmpInst::ICMP_SGT:
3285    switch (RHSCC) {
3286    default: llvm_unreachable("Unknown integer condition code!");
3287    case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
3288    case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
3289      return ReplaceInstUsesWith(I, LHS);
3290    case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
3291      break;
3292    case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
3293    case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
3294      return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
3295    case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
3296      break;
3297    }
3298    break;
3299  }
3300  return 0;
3301}
3302
3303Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS,
3304                                         FCmpInst *RHS) {
3305  if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
3306      RHS->getPredicate() == FCmpInst::FCMP_UNO &&
3307      LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
3308    if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
3309      if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
3310        // If either of the constants are nans, then the whole thing returns
3311        // true.
3312        if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
3313          return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
3314
3315        // Otherwise, no need to compare the two constants, compare the
3316        // rest.
3317        return new FCmpInst(FCmpInst::FCMP_UNO,
3318                            LHS->getOperand(0), RHS->getOperand(0));
3319      }
3320
3321    // Handle vector zeros.  This occurs because the canonical form of
3322    // "fcmp uno x,x" is "fcmp uno x, 0".
3323    if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
3324        isa<ConstantAggregateZero>(RHS->getOperand(1)))
3325      return new FCmpInst(FCmpInst::FCMP_UNO,
3326                          LHS->getOperand(0), RHS->getOperand(0));
3327
3328    return 0;
3329  }
3330
3331  Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
3332  Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
3333  FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
3334
3335  if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
3336    // Swap RHS operands to match LHS.
3337    Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
3338    std::swap(Op1LHS, Op1RHS);
3339  }
3340  if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
3341    // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
3342    if (Op0CC == Op1CC)
3343      return new FCmpInst((FCmpInst::Predicate)Op0CC,
3344                          Op0LHS, Op0RHS);
3345    if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
3346      return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
3347    if (Op0CC == FCmpInst::FCMP_FALSE)
3348      return ReplaceInstUsesWith(I, RHS);
3349    if (Op1CC == FCmpInst::FCMP_FALSE)
3350      return ReplaceInstUsesWith(I, LHS);
3351    bool Op0Ordered;
3352    bool Op1Ordered;
3353    unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
3354    unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
3355    if (Op0Ordered == Op1Ordered) {
3356      // If both are ordered or unordered, return a new fcmp with
3357      // or'ed predicates.
3358      Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS);
3359      if (Instruction *I = dyn_cast<Instruction>(RV))
3360        return I;
3361      // Otherwise, it's a constant boolean value...
3362      return ReplaceInstUsesWith(I, RV);
3363    }
3364  }
3365  return 0;
3366}
3367
3368/// FoldOrWithConstants - This helper function folds:
3369///
3370///     ((A | B) & C1) | (B & C2)
3371///
3372/// into:
3373///
3374///     (A & C1) | B
3375///
3376/// when the XOR of the two constants is "all ones" (-1).
3377Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
3378                                               Value *A, Value *B, Value *C) {
3379  ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
3380  if (!CI1) return 0;
3381
3382  Value *V1 = 0;
3383  ConstantInt *CI2 = 0;
3384  if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
3385
3386  APInt Xor = CI1->getValue() ^ CI2->getValue();
3387  if (!Xor.isAllOnesValue()) return 0;
3388
3389  if (V1 == A || V1 == B) {
3390    Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
3391    return BinaryOperator::CreateOr(NewOp, V1);
3392  }
3393
3394  return 0;
3395}
3396
3397Instruction *InstCombiner::visitOr(BinaryOperator &I) {
3398  bool Changed = SimplifyCommutative(I);
3399  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3400
3401  if (Value *V = SimplifyOrInst(Op0, Op1, TD))
3402    return ReplaceInstUsesWith(I, V);
3403
3404
3405  // See if we can simplify any instructions used by the instruction whose sole
3406  // purpose is to compute bits we don't care about.
3407  if (SimplifyDemandedInstructionBits(I))
3408    return &I;
3409
3410  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3411    ConstantInt *C1 = 0; Value *X = 0;
3412    // (X & C1) | C2 --> (X | C2) & (C1|C2)
3413    if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
3414        isOnlyUse(Op0)) {
3415      Value *Or = Builder->CreateOr(X, RHS);
3416      Or->takeName(Op0);
3417      return BinaryOperator::CreateAnd(Or,
3418                         ConstantInt::get(I.getContext(),
3419                                          RHS->getValue() | C1->getValue()));
3420    }
3421
3422    // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
3423    if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
3424        isOnlyUse(Op0)) {
3425      Value *Or = Builder->CreateOr(X, RHS);
3426      Or->takeName(Op0);
3427      return BinaryOperator::CreateXor(Or,
3428                 ConstantInt::get(I.getContext(),
3429                                  C1->getValue() & ~RHS->getValue()));
3430    }
3431
3432    // Try to fold constant and into select arguments.
3433    if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
3434      if (Instruction *R = FoldOpIntoSelect(I, SI))
3435        return R;
3436    if (isa<PHINode>(Op0))
3437      if (Instruction *NV = FoldOpIntoPhi(I))
3438        return NV;
3439  }
3440
3441  Value *A = 0, *B = 0;
3442  ConstantInt *C1 = 0, *C2 = 0;
3443
3444  // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
3445  // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
3446  if (match(Op0, m_Or(m_Value(), m_Value())) ||
3447      match(Op1, m_Or(m_Value(), m_Value())) ||
3448      (match(Op0, m_Shift(m_Value(), m_Value())) &&
3449       match(Op1, m_Shift(m_Value(), m_Value())))) {
3450    if (Instruction *BSwap = MatchBSwap(I))
3451      return BSwap;
3452  }
3453
3454  // (X^C)|Y -> (X|Y)^C iff Y&C == 0
3455  if (Op0->hasOneUse() &&
3456      match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
3457      MaskedValueIsZero(Op1, C1->getValue())) {
3458    Value *NOr = Builder->CreateOr(A, Op1);
3459    NOr->takeName(Op0);
3460    return BinaryOperator::CreateXor(NOr, C1);
3461  }
3462
3463  // Y|(X^C) -> (X|Y)^C iff Y&C == 0
3464  if (Op1->hasOneUse() &&
3465      match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
3466      MaskedValueIsZero(Op0, C1->getValue())) {
3467    Value *NOr = Builder->CreateOr(A, Op0);
3468    NOr->takeName(Op0);
3469    return BinaryOperator::CreateXor(NOr, C1);
3470  }
3471
3472  // (A & C)|(B & D)
3473  Value *C = 0, *D = 0;
3474  if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
3475      match(Op1, m_And(m_Value(B), m_Value(D)))) {
3476    Value *V1 = 0, *V2 = 0, *V3 = 0;
3477    C1 = dyn_cast<ConstantInt>(C);
3478    C2 = dyn_cast<ConstantInt>(D);
3479    if (C1 && C2) {  // (A & C1)|(B & C2)
3480      // If we have: ((V + N) & C1) | (V & C2)
3481      // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
3482      // replace with V+N.
3483      if (C1->getValue() == ~C2->getValue()) {
3484        if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
3485            match(A, m_Add(m_Value(V1), m_Value(V2)))) {
3486          // Add commutes, try both ways.
3487          if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
3488            return ReplaceInstUsesWith(I, A);
3489          if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
3490            return ReplaceInstUsesWith(I, A);
3491        }
3492        // Or commutes, try both ways.
3493        if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
3494            match(B, m_Add(m_Value(V1), m_Value(V2)))) {
3495          // Add commutes, try both ways.
3496          if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
3497            return ReplaceInstUsesWith(I, B);
3498          if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
3499            return ReplaceInstUsesWith(I, B);
3500        }
3501      }
3502
3503      // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
3504      // iff (C1&C2) == 0 and (N&~C1) == 0
3505      if ((C1->getValue() & C2->getValue()) == 0) {
3506        if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
3507            ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) ||  // (V|N)
3508             (V2 == B && MaskedValueIsZero(V1, ~C1->getValue()))))   // (N|V)
3509          return BinaryOperator::CreateAnd(A,
3510                               ConstantInt::get(A->getContext(),
3511                                                C1->getValue()|C2->getValue()));
3512        // Or commutes, try both ways.
3513        if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
3514            ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) ||  // (V|N)
3515             (V2 == A && MaskedValueIsZero(V1, ~C2->getValue()))))   // (N|V)
3516          return BinaryOperator::CreateAnd(B,
3517                               ConstantInt::get(B->getContext(),
3518                                                C1->getValue()|C2->getValue()));
3519      }
3520    }
3521
3522    // Check to see if we have any common things being and'ed.  If so, find the
3523    // terms for V1 & (V2|V3).
3524    if (isOnlyUse(Op0) || isOnlyUse(Op1)) {
3525      V1 = 0;
3526      if (A == B)      // (A & C)|(A & D) == A & (C|D)
3527        V1 = A, V2 = C, V3 = D;
3528      else if (A == D) // (A & C)|(B & A) == A & (B|C)
3529        V1 = A, V2 = B, V3 = C;
3530      else if (C == B) // (A & C)|(C & D) == C & (A|D)
3531        V1 = C, V2 = A, V3 = D;
3532      else if (C == D) // (A & C)|(B & C) == C & (A|B)
3533        V1 = C, V2 = A, V3 = B;
3534
3535      if (V1) {
3536        Value *Or = Builder->CreateOr(V2, V3, "tmp");
3537        return BinaryOperator::CreateAnd(V1, Or);
3538      }
3539    }
3540
3541    // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) ->  C0 ? A : B, and commuted variants
3542    if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
3543      return Match;
3544    if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
3545      return Match;
3546    if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
3547      return Match;
3548    if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
3549      return Match;
3550
3551    // ((A&~B)|(~A&B)) -> A^B
3552    if ((match(C, m_Not(m_Specific(D))) &&
3553         match(B, m_Not(m_Specific(A)))))
3554      return BinaryOperator::CreateXor(A, D);
3555    // ((~B&A)|(~A&B)) -> A^B
3556    if ((match(A, m_Not(m_Specific(D))) &&
3557         match(B, m_Not(m_Specific(C)))))
3558      return BinaryOperator::CreateXor(C, D);
3559    // ((A&~B)|(B&~A)) -> A^B
3560    if ((match(C, m_Not(m_Specific(B))) &&
3561         match(D, m_Not(m_Specific(A)))))
3562      return BinaryOperator::CreateXor(A, B);
3563    // ((~B&A)|(B&~A)) -> A^B
3564    if ((match(A, m_Not(m_Specific(B))) &&
3565         match(D, m_Not(m_Specific(C)))))
3566      return BinaryOperator::CreateXor(C, B);
3567  }
3568
3569  // (X >> Z) | (Y >> Z)  -> (X|Y) >> Z  for all shifts.
3570  if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
3571    if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
3572      if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
3573          SI0->getOperand(1) == SI1->getOperand(1) &&
3574          (SI0->hasOneUse() || SI1->hasOneUse())) {
3575        Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
3576                                         SI0->getName());
3577        return BinaryOperator::Create(SI1->getOpcode(), NewOp,
3578                                      SI1->getOperand(1));
3579      }
3580  }
3581
3582  // ((A|B)&1)|(B&-2) -> (A&1) | B
3583  if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
3584      match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
3585    Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C);
3586    if (Ret) return Ret;
3587  }
3588  // (B&-2)|((A|B)&1) -> (A&1) | B
3589  if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
3590      match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
3591    Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
3592    if (Ret) return Ret;
3593  }
3594
3595  // (~A | ~B) == (~(A & B)) - De Morgan's Law
3596  if (Value *Op0NotVal = dyn_castNotVal(Op0))
3597    if (Value *Op1NotVal = dyn_castNotVal(Op1))
3598      if (Op0->hasOneUse() && Op1->hasOneUse()) {
3599        Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
3600                                        I.getName()+".demorgan");
3601        return BinaryOperator::CreateNot(And);
3602      }
3603
3604  // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
3605  if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
3606    if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
3607      return R;
3608
3609    if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
3610      if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS))
3611        return Res;
3612  }
3613
3614  // fold (or (cast A), (cast B)) -> (cast (or A, B))
3615  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
3616    if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
3617      if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
3618        if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
3619            !isa<ICmpInst>(Op1C->getOperand(0))) {
3620          const Type *SrcTy = Op0C->getOperand(0)->getType();
3621          if (SrcTy == Op1C->getOperand(0)->getType() &&
3622              SrcTy->isIntOrIntVector() &&
3623              // Only do this if the casts both really cause code to be
3624              // generated.
3625              ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
3626                                I.getType()) &&
3627              ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
3628                                I.getType())) {
3629            Value *NewOp = Builder->CreateOr(Op0C->getOperand(0),
3630                                             Op1C->getOperand(0), I.getName());
3631            return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
3632          }
3633        }
3634      }
3635  }
3636
3637
3638  // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
3639  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
3640    if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
3641      if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS))
3642        return Res;
3643  }
3644
3645  return Changed ? &I : 0;
3646}
3647
3648namespace {
3649
3650// XorSelf - Implements: X ^ X --> 0
3651struct XorSelf {
3652  Value *RHS;
3653  XorSelf(Value *rhs) : RHS(rhs) {}
3654  bool shouldApply(Value *LHS) const { return LHS == RHS; }
3655  Instruction *apply(BinaryOperator &Xor) const {
3656    return &Xor;
3657  }
3658};
3659
3660}
3661
3662Instruction *InstCombiner::visitXor(BinaryOperator &I) {
3663  bool Changed = SimplifyCommutative(I);
3664  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3665
3666  if (isa<UndefValue>(Op1)) {
3667    if (isa<UndefValue>(Op0))
3668      // Handle undef ^ undef -> 0 special case. This is a common
3669      // idiom (misuse).
3670      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3671    return ReplaceInstUsesWith(I, Op1);  // X ^ undef -> undef
3672  }
3673
3674  // xor X, X = 0, even if X is nested in a sequence of Xor's.
3675  if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
3676    assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
3677    return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3678  }
3679
3680  // See if we can simplify any instructions used by the instruction whose sole
3681  // purpose is to compute bits we don't care about.
3682  if (SimplifyDemandedInstructionBits(I))
3683    return &I;
3684  if (isa<VectorType>(I.getType()))
3685    if (isa<ConstantAggregateZero>(Op1))
3686      return ReplaceInstUsesWith(I, Op0);  // X ^ <0,0> -> X
3687
3688  // Is this a ~ operation?
3689  if (Value *NotOp = dyn_castNotVal(&I)) {
3690    if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
3691      if (Op0I->getOpcode() == Instruction::And ||
3692          Op0I->getOpcode() == Instruction::Or) {
3693        // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
3694        // ~(~X | Y) === (X & ~Y) - De Morgan's Law
3695        if (dyn_castNotVal(Op0I->getOperand(1)))
3696          Op0I->swapOperands();
3697        if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
3698          Value *NotY =
3699            Builder->CreateNot(Op0I->getOperand(1),
3700                               Op0I->getOperand(1)->getName()+".not");
3701          if (Op0I->getOpcode() == Instruction::And)
3702            return BinaryOperator::CreateOr(Op0NotVal, NotY);
3703          return BinaryOperator::CreateAnd(Op0NotVal, NotY);
3704        }
3705
3706        // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
3707        // ~(X | Y) === (~X & ~Y) - De Morgan's Law
3708        if (isFreeToInvert(Op0I->getOperand(0)) &&
3709            isFreeToInvert(Op0I->getOperand(1))) {
3710          Value *NotX =
3711            Builder->CreateNot(Op0I->getOperand(0), "notlhs");
3712          Value *NotY =
3713            Builder->CreateNot(Op0I->getOperand(1), "notrhs");
3714          if (Op0I->getOpcode() == Instruction::And)
3715            return BinaryOperator::CreateOr(NotX, NotY);
3716          return BinaryOperator::CreateAnd(NotX, NotY);
3717        }
3718      }
3719    }
3720  }
3721
3722
3723  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3724    if (RHS->isOne() && Op0->hasOneUse()) {
3725      // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
3726      if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
3727        return new ICmpInst(ICI->getInversePredicate(),
3728                            ICI->getOperand(0), ICI->getOperand(1));
3729
3730      if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
3731        return new FCmpInst(FCI->getInversePredicate(),
3732                            FCI->getOperand(0), FCI->getOperand(1));
3733    }
3734
3735    // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
3736    if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
3737      if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
3738        if (CI->hasOneUse() && Op0C->hasOneUse()) {
3739          Instruction::CastOps Opcode = Op0C->getOpcode();
3740          if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
3741              (RHS == ConstantExpr::getCast(Opcode,
3742                                           ConstantInt::getTrue(I.getContext()),
3743                                            Op0C->getDestTy()))) {
3744            CI->setPredicate(CI->getInversePredicate());
3745            return CastInst::Create(Opcode, CI, Op0C->getType());
3746          }
3747        }
3748      }
3749    }
3750
3751    if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
3752      // ~(c-X) == X-c-1 == X+(-c-1)
3753      if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
3754        if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
3755          Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
3756          Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
3757                                      ConstantInt::get(I.getType(), 1));
3758          return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
3759        }
3760
3761      if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
3762        if (Op0I->getOpcode() == Instruction::Add) {
3763          // ~(X-c) --> (-c-1)-X
3764          if (RHS->isAllOnesValue()) {
3765            Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
3766            return BinaryOperator::CreateSub(
3767                           ConstantExpr::getSub(NegOp0CI,
3768                                      ConstantInt::get(I.getType(), 1)),
3769                                      Op0I->getOperand(0));
3770          } else if (RHS->getValue().isSignBit()) {
3771            // (X + C) ^ signbit -> (X + C + signbit)
3772            Constant *C = ConstantInt::get(I.getContext(),
3773                                           RHS->getValue() + Op0CI->getValue());
3774            return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
3775
3776          }
3777        } else if (Op0I->getOpcode() == Instruction::Or) {
3778          // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
3779          if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
3780            Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
3781            // Anything in both C1 and C2 is known to be zero, remove it from
3782            // NewRHS.
3783            Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
3784            NewRHS = ConstantExpr::getAnd(NewRHS,
3785                                       ConstantExpr::getNot(CommonBits));
3786            Worklist.Add(Op0I);
3787            I.setOperand(0, Op0I->getOperand(0));
3788            I.setOperand(1, NewRHS);
3789            return &I;
3790          }
3791        }
3792      }
3793    }
3794
3795    // Try to fold constant and into select arguments.
3796    if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
3797      if (Instruction *R = FoldOpIntoSelect(I, SI))
3798        return R;
3799    if (isa<PHINode>(Op0))
3800      if (Instruction *NV = FoldOpIntoPhi(I))
3801        return NV;
3802  }
3803
3804  if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
3805    if (X == Op1)
3806      return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
3807
3808  if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
3809    if (X == Op0)
3810      return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
3811
3812
3813  BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
3814  if (Op1I) {
3815    Value *A, *B;
3816    if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
3817      if (A == Op0) {              // B^(B|A) == (A|B)^B
3818        Op1I->swapOperands();
3819        I.swapOperands();
3820        std::swap(Op0, Op1);
3821      } else if (B == Op0) {       // B^(A|B) == (A|B)^B
3822        I.swapOperands();     // Simplified below.
3823        std::swap(Op0, Op1);
3824      }
3825    } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
3826      return ReplaceInstUsesWith(I, B);                      // A^(A^B) == B
3827    } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
3828      return ReplaceInstUsesWith(I, A);                      // A^(B^A) == B
3829    } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
3830               Op1I->hasOneUse()){
3831      if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
3832        Op1I->swapOperands();
3833        std::swap(A, B);
3834      }
3835      if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
3836        I.swapOperands();     // Simplified below.
3837        std::swap(Op0, Op1);
3838      }
3839    }
3840  }
3841
3842  BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
3843  if (Op0I) {
3844    Value *A, *B;
3845    if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
3846        Op0I->hasOneUse()) {
3847      if (A == Op1)                                  // (B|A)^B == (A|B)^B
3848        std::swap(A, B);
3849      if (B == Op1)                                  // (A|B)^B == A & ~B
3850        return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1, "tmp"));
3851    } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
3852      return ReplaceInstUsesWith(I, B);                      // (A^B)^A == B
3853    } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
3854      return ReplaceInstUsesWith(I, A);                      // (B^A)^A == B
3855    } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
3856               Op0I->hasOneUse()){
3857      if (A == Op1)                                        // (A&B)^A -> (B&A)^A
3858        std::swap(A, B);
3859      if (B == Op1 &&                                      // (B&A)^A == ~B & A
3860          !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
3861        return BinaryOperator::CreateAnd(Builder->CreateNot(A, "tmp"), Op1);
3862      }
3863    }
3864  }
3865
3866  // (X >> Z) ^ (Y >> Z)  -> (X^Y) >> Z  for all shifts.
3867  if (Op0I && Op1I && Op0I->isShift() &&
3868      Op0I->getOpcode() == Op1I->getOpcode() &&
3869      Op0I->getOperand(1) == Op1I->getOperand(1) &&
3870      (Op1I->hasOneUse() || Op1I->hasOneUse())) {
3871    Value *NewOp =
3872      Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
3873                         Op0I->getName());
3874    return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
3875                                  Op1I->getOperand(1));
3876  }
3877
3878  if (Op0I && Op1I) {
3879    Value *A, *B, *C, *D;
3880    // (A & B)^(A | B) -> A ^ B
3881    if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
3882        match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
3883      if ((A == C && B == D) || (A == D && B == C))
3884        return BinaryOperator::CreateXor(A, B);
3885    }
3886    // (A | B)^(A & B) -> A ^ B
3887    if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
3888        match(Op1I, m_And(m_Value(C), m_Value(D)))) {
3889      if ((A == C && B == D) || (A == D && B == C))
3890        return BinaryOperator::CreateXor(A, B);
3891    }
3892
3893    // (A & B)^(C & D)
3894    if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
3895        match(Op0I, m_And(m_Value(A), m_Value(B))) &&
3896        match(Op1I, m_And(m_Value(C), m_Value(D)))) {
3897      // (X & Y)^(X & Y) -> (Y^Z) & X
3898      Value *X = 0, *Y = 0, *Z = 0;
3899      if (A == C)
3900        X = A, Y = B, Z = D;
3901      else if (A == D)
3902        X = A, Y = B, Z = C;
3903      else if (B == C)
3904        X = B, Y = A, Z = D;
3905      else if (B == D)
3906        X = B, Y = A, Z = C;
3907
3908      if (X) {
3909        Value *NewOp = Builder->CreateXor(Y, Z, Op0->getName());
3910        return BinaryOperator::CreateAnd(NewOp, X);
3911      }
3912    }
3913  }
3914
3915  // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
3916  if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
3917    if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
3918      return R;
3919
3920  // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
3921  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
3922    if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
3923      if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
3924        const Type *SrcTy = Op0C->getOperand(0)->getType();
3925        if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
3926            // Only do this if the casts both really cause code to be generated.
3927            ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
3928                              I.getType()) &&
3929            ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
3930                              I.getType())) {
3931          Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
3932                                            Op1C->getOperand(0), I.getName());
3933          return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
3934        }
3935      }
3936  }
3937
3938  return Changed ? &I : 0;
3939}
3940
3941
3942Instruction *InstCombiner::visitShl(BinaryOperator &I) {
3943  return commonShiftTransforms(I);
3944}
3945
3946Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
3947  return commonShiftTransforms(I);
3948}
3949
3950Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
3951  if (Instruction *R = commonShiftTransforms(I))
3952    return R;
3953
3954  Value *Op0 = I.getOperand(0);
3955
3956  // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
3957  if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
3958    if (CSI->isAllOnesValue())
3959      return ReplaceInstUsesWith(I, CSI);
3960
3961  // See if we can turn a signed shr into an unsigned shr.
3962  if (MaskedValueIsZero(Op0,
3963                        APInt::getSignBit(I.getType()->getScalarSizeInBits())))
3964    return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
3965
3966  // Arithmetic shifting an all-sign-bit value is a no-op.
3967  unsigned NumSignBits = ComputeNumSignBits(Op0);
3968  if (NumSignBits == Op0->getType()->getScalarSizeInBits())
3969    return ReplaceInstUsesWith(I, Op0);
3970
3971  return 0;
3972}
3973
3974Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
3975  assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
3976  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3977
3978  // shl X, 0 == X and shr X, 0 == X
3979  // shl 0, X == 0 and shr 0, X == 0
3980  if (Op1 == Constant::getNullValue(Op1->getType()) ||
3981      Op0 == Constant::getNullValue(Op0->getType()))
3982    return ReplaceInstUsesWith(I, Op0);
3983
3984  if (isa<UndefValue>(Op0)) {
3985    if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
3986      return ReplaceInstUsesWith(I, Op0);
3987    else                                    // undef << X -> 0, undef >>u X -> 0
3988      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3989  }
3990  if (isa<UndefValue>(Op1)) {
3991    if (I.getOpcode() == Instruction::AShr)  // X >>s undef -> X
3992      return ReplaceInstUsesWith(I, Op0);
3993    else                                     // X << undef, X >>u undef -> 0
3994      return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3995  }
3996
3997  // See if we can fold away this shift.
3998  if (SimplifyDemandedInstructionBits(I))
3999    return &I;
4000
4001  // Try to fold constant and into select arguments.
4002  if (isa<Constant>(Op0))
4003    if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
4004      if (Instruction *R = FoldOpIntoSelect(I, SI))
4005        return R;
4006
4007  if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
4008    if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
4009      return Res;
4010  return 0;
4011}
4012
4013Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
4014                                               BinaryOperator &I) {
4015  bool isLeftShift = I.getOpcode() == Instruction::Shl;
4016
4017  // See if we can simplify any instructions used by the instruction whose sole
4018  // purpose is to compute bits we don't care about.
4019  uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
4020
4021  // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate
4022  // a signed shift.
4023  //
4024  if (Op1->uge(TypeBits)) {
4025    if (I.getOpcode() != Instruction::AShr)
4026      return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
4027    else {
4028      I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
4029      return &I;
4030    }
4031  }
4032
4033  // ((X*C1) << C2) == (X * (C1 << C2))
4034  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
4035    if (BO->getOpcode() == Instruction::Mul && isLeftShift)
4036      if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
4037        return BinaryOperator::CreateMul(BO->getOperand(0),
4038                                        ConstantExpr::getShl(BOOp, Op1));
4039
4040  // Try to fold constant and into select arguments.
4041  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
4042    if (Instruction *R = FoldOpIntoSelect(I, SI))
4043      return R;
4044  if (isa<PHINode>(Op0))
4045    if (Instruction *NV = FoldOpIntoPhi(I))
4046      return NV;
4047
4048  // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
4049  if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
4050    Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
4051    // If 'shift2' is an ashr, we would have to get the sign bit into a funny
4052    // place.  Don't try to do this transformation in this case.  Also, we
4053    // require that the input operand is a shift-by-constant so that we have
4054    // confidence that the shifts will get folded together.  We could do this
4055    // xform in more cases, but it is unlikely to be profitable.
4056    if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
4057        isa<ConstantInt>(TrOp->getOperand(1))) {
4058      // Okay, we'll do this xform.  Make the shift of shift.
4059      Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
4060      // (shift2 (shift1 & 0x00FF), c2)
4061      Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
4062
4063      // For logical shifts, the truncation has the effect of making the high
4064      // part of the register be zeros.  Emulate this by inserting an AND to
4065      // clear the top bits as needed.  This 'and' will usually be zapped by
4066      // other xforms later if dead.
4067      unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
4068      unsigned DstSize = TI->getType()->getScalarSizeInBits();
4069      APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
4070
4071      // The mask we constructed says what the trunc would do if occurring
4072      // between the shifts.  We want to know the effect *after* the second
4073      // shift.  We know that it is a logical shift by a constant, so adjust the
4074      // mask as appropriate.
4075      if (I.getOpcode() == Instruction::Shl)
4076        MaskV <<= Op1->getZExtValue();
4077      else {
4078        assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
4079        MaskV = MaskV.lshr(Op1->getZExtValue());
4080      }
4081
4082      // shift1 & 0x00FF
4083      Value *And = Builder->CreateAnd(NSh,
4084                                      ConstantInt::get(I.getContext(), MaskV),
4085                                      TI->getName());
4086
4087      // Return the value truncated to the interesting size.
4088      return new TruncInst(And, I.getType());
4089    }
4090  }
4091
4092  if (Op0->hasOneUse()) {
4093    if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
4094      // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
4095      Value *V1, *V2;
4096      ConstantInt *CC;
4097      switch (Op0BO->getOpcode()) {
4098        default: break;
4099        case Instruction::Add:
4100        case Instruction::And:
4101        case Instruction::Or:
4102        case Instruction::Xor: {
4103          // These operators commute.
4104          // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
4105          if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
4106              match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
4107                    m_Specific(Op1)))) {
4108            Value *YS =         // (Y << C)
4109              Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
4110            // (X + (Y << C))
4111            Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
4112                                            Op0BO->getOperand(1)->getName());
4113            uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
4114            return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(),
4115                       APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
4116          }
4117
4118          // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
4119          Value *Op0BOOp1 = Op0BO->getOperand(1);
4120          if (isLeftShift && Op0BOOp1->hasOneUse() &&
4121              match(Op0BOOp1,
4122                    m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
4123                          m_ConstantInt(CC))) &&
4124              cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
4125            Value *YS =   // (Y << C)
4126              Builder->CreateShl(Op0BO->getOperand(0), Op1,
4127                                           Op0BO->getName());
4128            // X & (CC << C)
4129            Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
4130                                           V1->getName()+".mask");
4131            return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
4132          }
4133        }
4134
4135        // FALL THROUGH.
4136        case Instruction::Sub: {
4137          // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
4138          if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
4139              match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
4140                    m_Specific(Op1)))) {
4141            Value *YS =  // (Y << C)
4142              Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
4143            // (X + (Y << C))
4144            Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
4145                                            Op0BO->getOperand(0)->getName());
4146            uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
4147            return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(),
4148                       APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
4149          }
4150
4151          // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
4152          if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
4153              match(Op0BO->getOperand(0),
4154                    m_And(m_Shr(m_Value(V1), m_Value(V2)),
4155                          m_ConstantInt(CC))) && V2 == Op1 &&
4156              cast<BinaryOperator>(Op0BO->getOperand(0))
4157                  ->getOperand(0)->hasOneUse()) {
4158            Value *YS = // (Y << C)
4159              Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
4160            // X & (CC << C)
4161            Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
4162                                           V1->getName()+".mask");
4163
4164            return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
4165          }
4166
4167          break;
4168        }
4169      }
4170
4171
4172      // If the operand is an bitwise operator with a constant RHS, and the
4173      // shift is the only use, we can pull it out of the shift.
4174      if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
4175        bool isValid = true;     // Valid only for And, Or, Xor
4176        bool highBitSet = false; // Transform if high bit of constant set?
4177
4178        switch (Op0BO->getOpcode()) {
4179          default: isValid = false; break;   // Do not perform transform!
4180          case Instruction::Add:
4181            isValid = isLeftShift;
4182            break;
4183          case Instruction::Or:
4184          case Instruction::Xor:
4185            highBitSet = false;
4186            break;
4187          case Instruction::And:
4188            highBitSet = true;
4189            break;
4190        }
4191
4192        // If this is a signed shift right, and the high bit is modified
4193        // by the logical operation, do not perform the transformation.
4194        // The highBitSet boolean indicates the value of the high bit of
4195        // the constant which would cause it to be modified for this
4196        // operation.
4197        //
4198        if (isValid && I.getOpcode() == Instruction::AShr)
4199          isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
4200
4201        if (isValid) {
4202          Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
4203
4204          Value *NewShift =
4205            Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
4206          NewShift->takeName(Op0BO);
4207
4208          return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
4209                                        NewRHS);
4210        }
4211      }
4212    }
4213  }
4214
4215  // Find out if this is a shift of a shift by a constant.
4216  BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
4217  if (ShiftOp && !ShiftOp->isShift())
4218    ShiftOp = 0;
4219
4220  if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
4221    ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
4222    uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
4223    uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
4224    assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
4225    if (ShiftAmt1 == 0) return 0;  // Will be simplified in the future.
4226    Value *X = ShiftOp->getOperand(0);
4227
4228    uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
4229
4230    const IntegerType *Ty = cast<IntegerType>(I.getType());
4231
4232    // Check for (X << c1) << c2  and  (X >> c1) >> c2
4233    if (I.getOpcode() == ShiftOp->getOpcode()) {
4234      // If this is oversized composite shift, then unsigned shifts get 0, ashr
4235      // saturates.
4236      if (AmtSum >= TypeBits) {
4237        if (I.getOpcode() != Instruction::AShr)
4238          return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
4239        AmtSum = TypeBits-1;  // Saturate to 31 for i32 ashr.
4240      }
4241
4242      return BinaryOperator::Create(I.getOpcode(), X,
4243                                    ConstantInt::get(Ty, AmtSum));
4244    }
4245
4246    if (ShiftOp->getOpcode() == Instruction::LShr &&
4247        I.getOpcode() == Instruction::AShr) {
4248      if (AmtSum >= TypeBits)
4249        return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
4250
4251      // ((X >>u C1) >>s C2) -> (X >>u (C1+C2))  since C1 != 0.
4252      return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
4253    }
4254
4255    if (ShiftOp->getOpcode() == Instruction::AShr &&
4256        I.getOpcode() == Instruction::LShr) {
4257      // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
4258      if (AmtSum >= TypeBits)
4259        AmtSum = TypeBits-1;
4260
4261      Value *Shift = Builder->CreateAShr(X, ConstantInt::get(Ty, AmtSum));
4262
4263      APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
4264      return BinaryOperator::CreateAnd(Shift,
4265                                       ConstantInt::get(I.getContext(), Mask));
4266    }
4267
4268    // Okay, if we get here, one shift must be left, and the other shift must be
4269    // right.  See if the amounts are equal.
4270    if (ShiftAmt1 == ShiftAmt2) {
4271      // If we have ((X >>? C) << C), turn this into X & (-1 << C).
4272      if (I.getOpcode() == Instruction::Shl) {
4273        APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
4274        return BinaryOperator::CreateAnd(X,
4275                                         ConstantInt::get(I.getContext(),Mask));
4276      }
4277      // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
4278      if (I.getOpcode() == Instruction::LShr) {
4279        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
4280        return BinaryOperator::CreateAnd(X,
4281                                        ConstantInt::get(I.getContext(), Mask));
4282      }
4283      // We can simplify ((X << C) >>s C) into a trunc + sext.
4284      // NOTE: we could do this for any C, but that would make 'unusual' integer
4285      // types.  For now, just stick to ones well-supported by the code
4286      // generators.
4287      const Type *SExtType = 0;
4288      switch (Ty->getBitWidth() - ShiftAmt1) {
4289      case 1  :
4290      case 8  :
4291      case 16 :
4292      case 32 :
4293      case 64 :
4294      case 128:
4295        SExtType = IntegerType::get(I.getContext(),
4296                                    Ty->getBitWidth() - ShiftAmt1);
4297        break;
4298      default: break;
4299      }
4300      if (SExtType)
4301        return new SExtInst(Builder->CreateTrunc(X, SExtType, "sext"), Ty);
4302      // Otherwise, we can't handle it yet.
4303    } else if (ShiftAmt1 < ShiftAmt2) {
4304      uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
4305
4306      // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
4307      if (I.getOpcode() == Instruction::Shl) {
4308        assert(ShiftOp->getOpcode() == Instruction::LShr ||
4309               ShiftOp->getOpcode() == Instruction::AShr);
4310        Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
4311
4312        APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
4313        return BinaryOperator::CreateAnd(Shift,
4314                                         ConstantInt::get(I.getContext(),Mask));
4315      }
4316
4317      // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
4318      if (I.getOpcode() == Instruction::LShr) {
4319        assert(ShiftOp->getOpcode() == Instruction::Shl);
4320        Value *Shift = Builder->CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
4321
4322        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
4323        return BinaryOperator::CreateAnd(Shift,
4324                                         ConstantInt::get(I.getContext(),Mask));
4325      }
4326
4327      // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
4328    } else {
4329      assert(ShiftAmt2 < ShiftAmt1);
4330      uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
4331
4332      // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
4333      if (I.getOpcode() == Instruction::Shl) {
4334        assert(ShiftOp->getOpcode() == Instruction::LShr ||
4335               ShiftOp->getOpcode() == Instruction::AShr);
4336        Value *Shift = Builder->CreateBinOp(ShiftOp->getOpcode(), X,
4337                                            ConstantInt::get(Ty, ShiftDiff));
4338
4339        APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
4340        return BinaryOperator::CreateAnd(Shift,
4341                                         ConstantInt::get(I.getContext(),Mask));
4342      }
4343
4344      // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2)
4345      if (I.getOpcode() == Instruction::LShr) {
4346        assert(ShiftOp->getOpcode() == Instruction::Shl);
4347        Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
4348
4349        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
4350        return BinaryOperator::CreateAnd(Shift,
4351                                         ConstantInt::get(I.getContext(),Mask));
4352      }
4353
4354      // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
4355    }
4356  }
4357  return 0;
4358}
4359
4360
4361
4362/// FindElementAtOffset - Given a type and a constant offset, determine whether
4363/// or not there is a sequence of GEP indices into the type that will land us at
4364/// the specified offset.  If so, fill them into NewIndices and return the
4365/// resultant element type, otherwise return null.
4366const Type *InstCombiner::FindElementAtOffset(const Type *Ty, int64_t Offset,
4367                                          SmallVectorImpl<Value*> &NewIndices) {
4368  if (!TD) return 0;
4369  if (!Ty->isSized()) return 0;
4370
4371  // Start with the index over the outer type.  Note that the type size
4372  // might be zero (even if the offset isn't zero) if the indexed type
4373  // is something like [0 x {int, int}]
4374  const Type *IntPtrTy = TD->getIntPtrType(Ty->getContext());
4375  int64_t FirstIdx = 0;
4376  if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
4377    FirstIdx = Offset/TySize;
4378    Offset -= FirstIdx*TySize;
4379
4380    // Handle hosts where % returns negative instead of values [0..TySize).
4381    if (Offset < 0) {
4382      --FirstIdx;
4383      Offset += TySize;
4384      assert(Offset >= 0);
4385    }
4386    assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
4387  }
4388
4389  NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
4390
4391  // Index into the types.  If we fail, set OrigBase to null.
4392  while (Offset) {
4393    // Indexing into tail padding between struct/array elements.
4394    if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
4395      return 0;
4396
4397    if (const StructType *STy = dyn_cast<StructType>(Ty)) {
4398      const StructLayout *SL = TD->getStructLayout(STy);
4399      assert(Offset < (int64_t)SL->getSizeInBytes() &&
4400             "Offset must stay within the indexed type");
4401
4402      unsigned Elt = SL->getElementContainingOffset(Offset);
4403      NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
4404                                            Elt));
4405
4406      Offset -= SL->getElementOffset(Elt);
4407      Ty = STy->getElementType(Elt);
4408    } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
4409      uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
4410      assert(EltSize && "Cannot index into a zero-sized array");
4411      NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
4412      Offset %= EltSize;
4413      Ty = AT->getElementType();
4414    } else {
4415      // Otherwise, we can't index into the middle of this atomic type, bail.
4416      return 0;
4417    }
4418  }
4419
4420  return Ty;
4421}
4422
4423
4424/// GetSelectFoldableOperands - We want to turn code that looks like this:
4425///   %C = or %A, %B
4426///   %D = select %cond, %C, %A
4427/// into:
4428///   %C = select %cond, %B, 0
4429///   %D = or %A, %C
4430///
4431/// Assuming that the specified instruction is an operand to the select, return
4432/// a bitmask indicating which operands of this instruction are foldable if they
4433/// equal the other incoming value of the select.
4434///
4435static unsigned GetSelectFoldableOperands(Instruction *I) {
4436  switch (I->getOpcode()) {
4437  case Instruction::Add:
4438  case Instruction::Mul:
4439  case Instruction::And:
4440  case Instruction::Or:
4441  case Instruction::Xor:
4442    return 3;              // Can fold through either operand.
4443  case Instruction::Sub:   // Can only fold on the amount subtracted.
4444  case Instruction::Shl:   // Can only fold on the shift amount.
4445  case Instruction::LShr:
4446  case Instruction::AShr:
4447    return 1;
4448  default:
4449    return 0;              // Cannot fold
4450  }
4451}
4452
4453/// GetSelectFoldableConstant - For the same transformation as the previous
4454/// function, return the identity constant that goes into the select.
4455static Constant *GetSelectFoldableConstant(Instruction *I) {
4456  switch (I->getOpcode()) {
4457  default: llvm_unreachable("This cannot happen!");
4458  case Instruction::Add:
4459  case Instruction::Sub:
4460  case Instruction::Or:
4461  case Instruction::Xor:
4462  case Instruction::Shl:
4463  case Instruction::LShr:
4464  case Instruction::AShr:
4465    return Constant::getNullValue(I->getType());
4466  case Instruction::And:
4467    return Constant::getAllOnesValue(I->getType());
4468  case Instruction::Mul:
4469    return ConstantInt::get(I->getType(), 1);
4470  }
4471}
4472
4473/// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
4474/// have the same opcode and only one use each.  Try to simplify this.
4475Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
4476                                          Instruction *FI) {
4477  if (TI->getNumOperands() == 1) {
4478    // If this is a non-volatile load or a cast from the same type,
4479    // merge.
4480    if (TI->isCast()) {
4481      if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
4482        return 0;
4483    } else {
4484      return 0;  // unknown unary op.
4485    }
4486
4487    // Fold this by inserting a select from the input values.
4488    SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
4489                                          FI->getOperand(0), SI.getName()+".v");
4490    InsertNewInstBefore(NewSI, SI);
4491    return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
4492                            TI->getType());
4493  }
4494
4495  // Only handle binary operators here.
4496  if (!isa<BinaryOperator>(TI))
4497    return 0;
4498
4499  // Figure out if the operations have any operands in common.
4500  Value *MatchOp, *OtherOpT, *OtherOpF;
4501  bool MatchIsOpZero;
4502  if (TI->getOperand(0) == FI->getOperand(0)) {
4503    MatchOp  = TI->getOperand(0);
4504    OtherOpT = TI->getOperand(1);
4505    OtherOpF = FI->getOperand(1);
4506    MatchIsOpZero = true;
4507  } else if (TI->getOperand(1) == FI->getOperand(1)) {
4508    MatchOp  = TI->getOperand(1);
4509    OtherOpT = TI->getOperand(0);
4510    OtherOpF = FI->getOperand(0);
4511    MatchIsOpZero = false;
4512  } else if (!TI->isCommutative()) {
4513    return 0;
4514  } else if (TI->getOperand(0) == FI->getOperand(1)) {
4515    MatchOp  = TI->getOperand(0);
4516    OtherOpT = TI->getOperand(1);
4517    OtherOpF = FI->getOperand(0);
4518    MatchIsOpZero = true;
4519  } else if (TI->getOperand(1) == FI->getOperand(0)) {
4520    MatchOp  = TI->getOperand(1);
4521    OtherOpT = TI->getOperand(0);
4522    OtherOpF = FI->getOperand(1);
4523    MatchIsOpZero = true;
4524  } else {
4525    return 0;
4526  }
4527
4528  // If we reach here, they do have operations in common.
4529  SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT,
4530                                         OtherOpF, SI.getName()+".v");
4531  InsertNewInstBefore(NewSI, SI);
4532
4533  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
4534    if (MatchIsOpZero)
4535      return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
4536    else
4537      return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
4538  }
4539  llvm_unreachable("Shouldn't get here");
4540  return 0;
4541}
4542
4543static bool isSelect01(Constant *C1, Constant *C2) {
4544  ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
4545  if (!C1I)
4546    return false;
4547  ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
4548  if (!C2I)
4549    return false;
4550  return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne());
4551}
4552
4553/// FoldSelectIntoOp - Try fold the select into one of the operands to
4554/// facilitate further optimization.
4555Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
4556                                            Value *FalseVal) {
4557  // See the comment above GetSelectFoldableOperands for a description of the
4558  // transformation we are doing here.
4559  if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
4560    if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
4561        !isa<Constant>(FalseVal)) {
4562      if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
4563        unsigned OpToFold = 0;
4564        if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
4565          OpToFold = 1;
4566        } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
4567          OpToFold = 2;
4568        }
4569
4570        if (OpToFold) {
4571          Constant *C = GetSelectFoldableConstant(TVI);
4572          Value *OOp = TVI->getOperand(2-OpToFold);
4573          // Avoid creating select between 2 constants unless it's selecting
4574          // between 0 and 1.
4575          if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
4576            Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C);
4577            InsertNewInstBefore(NewSel, SI);
4578            NewSel->takeName(TVI);
4579            if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
4580              return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
4581            llvm_unreachable("Unknown instruction!!");
4582          }
4583        }
4584      }
4585    }
4586  }
4587
4588  if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
4589    if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
4590        !isa<Constant>(TrueVal)) {
4591      if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
4592        unsigned OpToFold = 0;
4593        if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
4594          OpToFold = 1;
4595        } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
4596          OpToFold = 2;
4597        }
4598
4599        if (OpToFold) {
4600          Constant *C = GetSelectFoldableConstant(FVI);
4601          Value *OOp = FVI->getOperand(2-OpToFold);
4602          // Avoid creating select between 2 constants unless it's selecting
4603          // between 0 and 1.
4604          if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
4605            Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp);
4606            InsertNewInstBefore(NewSel, SI);
4607            NewSel->takeName(FVI);
4608            if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
4609              return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
4610            llvm_unreachable("Unknown instruction!!");
4611          }
4612        }
4613      }
4614    }
4615  }
4616
4617  return 0;
4618}
4619
4620/// visitSelectInstWithICmp - Visit a SelectInst that has an
4621/// ICmpInst as its first operand.
4622///
4623Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
4624                                                   ICmpInst *ICI) {
4625  bool Changed = false;
4626  ICmpInst::Predicate Pred = ICI->getPredicate();
4627  Value *CmpLHS = ICI->getOperand(0);
4628  Value *CmpRHS = ICI->getOperand(1);
4629  Value *TrueVal = SI.getTrueValue();
4630  Value *FalseVal = SI.getFalseValue();
4631
4632  // Check cases where the comparison is with a constant that
4633  // can be adjusted to fit the min/max idiom. We may edit ICI in
4634  // place here, so make sure the select is the only user.
4635  if (ICI->hasOneUse())
4636    if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
4637      switch (Pred) {
4638      default: break;
4639      case ICmpInst::ICMP_ULT:
4640      case ICmpInst::ICMP_SLT: {
4641        // X < MIN ? T : F  -->  F
4642        if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
4643          return ReplaceInstUsesWith(SI, FalseVal);
4644        // X < C ? X : C-1  -->  X > C-1 ? C-1 : X
4645        Constant *AdjustedRHS = SubOne(CI);
4646        if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
4647            (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
4648          Pred = ICmpInst::getSwappedPredicate(Pred);
4649          CmpRHS = AdjustedRHS;
4650          std::swap(FalseVal, TrueVal);
4651          ICI->setPredicate(Pred);
4652          ICI->setOperand(1, CmpRHS);
4653          SI.setOperand(1, TrueVal);
4654          SI.setOperand(2, FalseVal);
4655          Changed = true;
4656        }
4657        break;
4658      }
4659      case ICmpInst::ICMP_UGT:
4660      case ICmpInst::ICMP_SGT: {
4661        // X > MAX ? T : F  -->  F
4662        if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
4663          return ReplaceInstUsesWith(SI, FalseVal);
4664        // X > C ? X : C+1  -->  X < C+1 ? C+1 : X
4665        Constant *AdjustedRHS = AddOne(CI);
4666        if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
4667            (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
4668          Pred = ICmpInst::getSwappedPredicate(Pred);
4669          CmpRHS = AdjustedRHS;
4670          std::swap(FalseVal, TrueVal);
4671          ICI->setPredicate(Pred);
4672          ICI->setOperand(1, CmpRHS);
4673          SI.setOperand(1, TrueVal);
4674          SI.setOperand(2, FalseVal);
4675          Changed = true;
4676        }
4677        break;
4678      }
4679      }
4680
4681      // (x <s 0) ? -1 : 0 -> ashr x, 31   -> all ones if signed
4682      // (x >s -1) ? -1 : 0 -> ashr x, 31  -> all ones if not signed
4683      CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
4684      if (match(TrueVal, m_ConstantInt<-1>()) &&
4685          match(FalseVal, m_ConstantInt<0>()))
4686        Pred = ICI->getPredicate();
4687      else if (match(TrueVal, m_ConstantInt<0>()) &&
4688               match(FalseVal, m_ConstantInt<-1>()))
4689        Pred = CmpInst::getInversePredicate(ICI->getPredicate());
4690
4691      if (Pred != CmpInst::BAD_ICMP_PREDICATE) {
4692        // If we are just checking for a icmp eq of a single bit and zext'ing it
4693        // to an integer, then shift the bit to the appropriate place and then
4694        // cast to integer to avoid the comparison.
4695        const APInt &Op1CV = CI->getValue();
4696
4697        // sext (x <s  0) to i32 --> x>>s31      true if signbit set.
4698        // sext (x >s -1) to i32 --> (x>>s31)^-1  true if signbit clear.
4699        if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
4700            (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
4701          Value *In = ICI->getOperand(0);
4702          Value *Sh = ConstantInt::get(In->getType(),
4703                                       In->getType()->getScalarSizeInBits()-1);
4704          In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
4705                                                        In->getName()+".lobit"),
4706                                   *ICI);
4707          if (In->getType() != SI.getType())
4708            In = CastInst::CreateIntegerCast(In, SI.getType(),
4709                                             true/*SExt*/, "tmp", ICI);
4710
4711          if (Pred == ICmpInst::ICMP_SGT)
4712            In = InsertNewInstBefore(BinaryOperator::CreateNot(In,
4713                                       In->getName()+".not"), *ICI);
4714
4715          return ReplaceInstUsesWith(SI, In);
4716        }
4717      }
4718    }
4719
4720  if (CmpLHS == TrueVal && CmpRHS == FalseVal) {
4721    // Transform (X == Y) ? X : Y  -> Y
4722    if (Pred == ICmpInst::ICMP_EQ)
4723      return ReplaceInstUsesWith(SI, FalseVal);
4724    // Transform (X != Y) ? X : Y  -> X
4725    if (Pred == ICmpInst::ICMP_NE)
4726      return ReplaceInstUsesWith(SI, TrueVal);
4727    /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
4728
4729  } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) {
4730    // Transform (X == Y) ? Y : X  -> X
4731    if (Pred == ICmpInst::ICMP_EQ)
4732      return ReplaceInstUsesWith(SI, FalseVal);
4733    // Transform (X != Y) ? Y : X  -> Y
4734    if (Pred == ICmpInst::ICMP_NE)
4735      return ReplaceInstUsesWith(SI, TrueVal);
4736    /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
4737  }
4738  return Changed ? &SI : 0;
4739}
4740
4741
4742/// CanSelectOperandBeMappingIntoPredBlock - SI is a select whose condition is a
4743/// PHI node (but the two may be in different blocks).  See if the true/false
4744/// values (V) are live in all of the predecessor blocks of the PHI.  For
4745/// example, cases like this cannot be mapped:
4746///
4747///   X = phi [ C1, BB1], [C2, BB2]
4748///   Y = add
4749///   Z = select X, Y, 0
4750///
4751/// because Y is not live in BB1/BB2.
4752///
4753static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
4754                                                   const SelectInst &SI) {
4755  // If the value is a non-instruction value like a constant or argument, it
4756  // can always be mapped.
4757  const Instruction *I = dyn_cast<Instruction>(V);
4758  if (I == 0) return true;
4759
4760  // If V is a PHI node defined in the same block as the condition PHI, we can
4761  // map the arguments.
4762  const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
4763
4764  if (const PHINode *VP = dyn_cast<PHINode>(I))
4765    if (VP->getParent() == CondPHI->getParent())
4766      return true;
4767
4768  // Otherwise, if the PHI and select are defined in the same block and if V is
4769  // defined in a different block, then we can transform it.
4770  if (SI.getParent() == CondPHI->getParent() &&
4771      I->getParent() != CondPHI->getParent())
4772    return true;
4773
4774  // Otherwise we have a 'hard' case and we can't tell without doing more
4775  // detailed dominator based analysis, punt.
4776  return false;
4777}
4778
4779/// FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form:
4780///   SPF2(SPF1(A, B), C)
4781Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner,
4782                                        SelectPatternFlavor SPF1,
4783                                        Value *A, Value *B,
4784                                        Instruction &Outer,
4785                                        SelectPatternFlavor SPF2, Value *C) {
4786  if (C == A || C == B) {
4787    // MAX(MAX(A, B), B) -> MAX(A, B)
4788    // MIN(MIN(a, b), a) -> MIN(a, b)
4789    if (SPF1 == SPF2)
4790      return ReplaceInstUsesWith(Outer, Inner);
4791
4792    // MAX(MIN(a, b), a) -> a
4793    // MIN(MAX(a, b), a) -> a
4794    if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
4795        (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
4796        (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
4797        (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
4798      return ReplaceInstUsesWith(Outer, C);
4799  }
4800
4801  // TODO: MIN(MIN(A, 23), 97)
4802  return 0;
4803}
4804
4805
4806
4807
4808Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
4809  Value *CondVal = SI.getCondition();
4810  Value *TrueVal = SI.getTrueValue();
4811  Value *FalseVal = SI.getFalseValue();
4812
4813  // select true, X, Y  -> X
4814  // select false, X, Y -> Y
4815  if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal))
4816    return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal);
4817
4818  // select C, X, X -> X
4819  if (TrueVal == FalseVal)
4820    return ReplaceInstUsesWith(SI, TrueVal);
4821
4822  if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
4823    return ReplaceInstUsesWith(SI, FalseVal);
4824  if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
4825    return ReplaceInstUsesWith(SI, TrueVal);
4826  if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
4827    if (isa<Constant>(TrueVal))
4828      return ReplaceInstUsesWith(SI, TrueVal);
4829    else
4830      return ReplaceInstUsesWith(SI, FalseVal);
4831  }
4832
4833  if (SI.getType() == Type::getInt1Ty(SI.getContext())) {
4834    if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
4835      if (C->getZExtValue()) {
4836        // Change: A = select B, true, C --> A = or B, C
4837        return BinaryOperator::CreateOr(CondVal, FalseVal);
4838      } else {
4839        // Change: A = select B, false, C --> A = and !B, C
4840        Value *NotCond =
4841          InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
4842                                             "not."+CondVal->getName()), SI);
4843        return BinaryOperator::CreateAnd(NotCond, FalseVal);
4844      }
4845    } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
4846      if (C->getZExtValue() == false) {
4847        // Change: A = select B, C, false --> A = and B, C
4848        return BinaryOperator::CreateAnd(CondVal, TrueVal);
4849      } else {
4850        // Change: A = select B, C, true --> A = or !B, C
4851        Value *NotCond =
4852          InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
4853                                             "not."+CondVal->getName()), SI);
4854        return BinaryOperator::CreateOr(NotCond, TrueVal);
4855      }
4856    }
4857
4858    // select a, b, a  -> a&b
4859    // select a, a, b  -> a|b
4860    if (CondVal == TrueVal)
4861      return BinaryOperator::CreateOr(CondVal, FalseVal);
4862    else if (CondVal == FalseVal)
4863      return BinaryOperator::CreateAnd(CondVal, TrueVal);
4864  }
4865
4866  // Selecting between two integer constants?
4867  if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
4868    if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
4869      // select C, 1, 0 -> zext C to int
4870      if (FalseValC->isZero() && TrueValC->getValue() == 1) {
4871        return CastInst::Create(Instruction::ZExt, CondVal, SI.getType());
4872      } else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
4873        // select C, 0, 1 -> zext !C to int
4874        Value *NotCond =
4875          InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
4876                                               "not."+CondVal->getName()), SI);
4877        return CastInst::Create(Instruction::ZExt, NotCond, SI.getType());
4878      }
4879
4880      if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) {
4881        // If one of the constants is zero (we know they can't both be) and we
4882        // have an icmp instruction with zero, and we have an 'and' with the
4883        // non-constant value, eliminate this whole mess.  This corresponds to
4884        // cases like this: ((X & 27) ? 27 : 0)
4885        if (TrueValC->isZero() || FalseValC->isZero())
4886          if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) &&
4887              cast<Constant>(IC->getOperand(1))->isNullValue())
4888            if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
4889              if (ICA->getOpcode() == Instruction::And &&
4890                  isa<ConstantInt>(ICA->getOperand(1)) &&
4891                  (ICA->getOperand(1) == TrueValC ||
4892                   ICA->getOperand(1) == FalseValC) &&
4893                  isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
4894                // Okay, now we know that everything is set up, we just don't
4895                // know whether we have a icmp_ne or icmp_eq and whether the
4896                // true or false val is the zero.
4897                bool ShouldNotVal = !TrueValC->isZero();
4898                ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
4899                Value *V = ICA;
4900                if (ShouldNotVal)
4901                  V = InsertNewInstBefore(BinaryOperator::Create(
4902                                  Instruction::Xor, V, ICA->getOperand(1)), SI);
4903                return ReplaceInstUsesWith(SI, V);
4904              }
4905      }
4906    }
4907
4908  // See if we are selecting two values based on a comparison of the two values.
4909  if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
4910    if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
4911      // Transform (X == Y) ? X : Y  -> Y
4912      if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
4913        // This is not safe in general for floating point:
4914        // consider X== -0, Y== +0.
4915        // It becomes safe if either operand is a nonzero constant.
4916        ConstantFP *CFPt, *CFPf;
4917        if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
4918              !CFPt->getValueAPF().isZero()) ||
4919            ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
4920             !CFPf->getValueAPF().isZero()))
4921        return ReplaceInstUsesWith(SI, FalseVal);
4922      }
4923      // Transform (X != Y) ? X : Y  -> X
4924      if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
4925        return ReplaceInstUsesWith(SI, TrueVal);
4926      // NOTE: if we wanted to, this is where to detect MIN/MAX
4927
4928    } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
4929      // Transform (X == Y) ? Y : X  -> X
4930      if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
4931        // This is not safe in general for floating point:
4932        // consider X== -0, Y== +0.
4933        // It becomes safe if either operand is a nonzero constant.
4934        ConstantFP *CFPt, *CFPf;
4935        if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
4936              !CFPt->getValueAPF().isZero()) ||
4937            ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
4938             !CFPf->getValueAPF().isZero()))
4939          return ReplaceInstUsesWith(SI, FalseVal);
4940      }
4941      // Transform (X != Y) ? Y : X  -> Y
4942      if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
4943        return ReplaceInstUsesWith(SI, TrueVal);
4944      // NOTE: if we wanted to, this is where to detect MIN/MAX
4945    }
4946    // NOTE: if we wanted to, this is where to detect ABS
4947  }
4948
4949  // See if we are selecting two values based on a comparison of the two values.
4950  if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
4951    if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
4952      return Result;
4953
4954  if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
4955    if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
4956      if (TI->hasOneUse() && FI->hasOneUse()) {
4957        Instruction *AddOp = 0, *SubOp = 0;
4958
4959        // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
4960        if (TI->getOpcode() == FI->getOpcode())
4961          if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
4962            return IV;
4963
4964        // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
4965        // even legal for FP.
4966        if ((TI->getOpcode() == Instruction::Sub &&
4967             FI->getOpcode() == Instruction::Add) ||
4968            (TI->getOpcode() == Instruction::FSub &&
4969             FI->getOpcode() == Instruction::FAdd)) {
4970          AddOp = FI; SubOp = TI;
4971        } else if ((FI->getOpcode() == Instruction::Sub &&
4972                    TI->getOpcode() == Instruction::Add) ||
4973                   (FI->getOpcode() == Instruction::FSub &&
4974                    TI->getOpcode() == Instruction::FAdd)) {
4975          AddOp = TI; SubOp = FI;
4976        }
4977
4978        if (AddOp) {
4979          Value *OtherAddOp = 0;
4980          if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
4981            OtherAddOp = AddOp->getOperand(1);
4982          } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
4983            OtherAddOp = AddOp->getOperand(0);
4984          }
4985
4986          if (OtherAddOp) {
4987            // So at this point we know we have (Y -> OtherAddOp):
4988            //        select C, (add X, Y), (sub X, Z)
4989            Value *NegVal;  // Compute -Z
4990            if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
4991              NegVal = ConstantExpr::getNeg(C);
4992            } else {
4993              NegVal = InsertNewInstBefore(
4994                    BinaryOperator::CreateNeg(SubOp->getOperand(1),
4995                                              "tmp"), SI);
4996            }
4997
4998            Value *NewTrueOp = OtherAddOp;
4999            Value *NewFalseOp = NegVal;
5000            if (AddOp != TI)
5001              std::swap(NewTrueOp, NewFalseOp);
5002            Instruction *NewSel =
5003              SelectInst::Create(CondVal, NewTrueOp,
5004                                 NewFalseOp, SI.getName() + ".p");
5005
5006            NewSel = InsertNewInstBefore(NewSel, SI);
5007            return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
5008          }
5009        }
5010      }
5011
5012  // See if we can fold the select into one of our operands.
5013  if (SI.getType()->isInteger()) {
5014    if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
5015      return FoldI;
5016
5017    // MAX(MAX(a, b), a) -> MAX(a, b)
5018    // MIN(MIN(a, b), a) -> MIN(a, b)
5019    // MAX(MIN(a, b), a) -> a
5020    // MIN(MAX(a, b), a) -> a
5021    Value *LHS, *RHS, *LHS2, *RHS2;
5022    if (SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS)) {
5023      if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2))
5024        if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
5025                                          SI, SPF, RHS))
5026          return R;
5027      if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2))
5028        if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
5029                                          SI, SPF, LHS))
5030          return R;
5031    }
5032
5033    // TODO.
5034    // ABS(-X) -> ABS(X)
5035    // ABS(ABS(X)) -> ABS(X)
5036  }
5037
5038  // See if we can fold the select into a phi node if the condition is a select.
5039  if (isa<PHINode>(SI.getCondition()))
5040    // The true/false values have to be live in the PHI predecessor's blocks.
5041    if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
5042        CanSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
5043      if (Instruction *NV = FoldOpIntoPhi(SI))
5044        return NV;
5045
5046  if (BinaryOperator::isNot(CondVal)) {
5047    SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
5048    SI.setOperand(1, FalseVal);
5049    SI.setOperand(2, TrueVal);
5050    return &SI;
5051  }
5052
5053  return 0;
5054}
5055
5056/// EnforceKnownAlignment - If the specified pointer points to an object that
5057/// we control, modify the object's alignment to PrefAlign. This isn't
5058/// often possible though. If alignment is important, a more reliable approach
5059/// is to simply align all global variables and allocation instructions to
5060/// their preferred alignment from the beginning.
5061///
5062static unsigned EnforceKnownAlignment(Value *V,
5063                                      unsigned Align, unsigned PrefAlign) {
5064
5065  User *U = dyn_cast<User>(V);
5066  if (!U) return Align;
5067
5068  switch (Operator::getOpcode(U)) {
5069  default: break;
5070  case Instruction::BitCast:
5071    return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
5072  case Instruction::GetElementPtr: {
5073    // If all indexes are zero, it is just the alignment of the base pointer.
5074    bool AllZeroOperands = true;
5075    for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
5076      if (!isa<Constant>(*i) ||
5077          !cast<Constant>(*i)->isNullValue()) {
5078        AllZeroOperands = false;
5079        break;
5080      }
5081
5082    if (AllZeroOperands) {
5083      // Treat this like a bitcast.
5084      return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
5085    }
5086    break;
5087  }
5088  }
5089
5090  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
5091    // If there is a large requested alignment and we can, bump up the alignment
5092    // of the global.
5093    if (!GV->isDeclaration()) {
5094      if (GV->getAlignment() >= PrefAlign)
5095        Align = GV->getAlignment();
5096      else {
5097        GV->setAlignment(PrefAlign);
5098        Align = PrefAlign;
5099      }
5100    }
5101  } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
5102    // If there is a requested alignment and if this is an alloca, round up.
5103    if (AI->getAlignment() >= PrefAlign)
5104      Align = AI->getAlignment();
5105    else {
5106      AI->setAlignment(PrefAlign);
5107      Align = PrefAlign;
5108    }
5109  }
5110
5111  return Align;
5112}
5113
5114/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
5115/// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
5116/// and it is more than the alignment of the ultimate object, see if we can
5117/// increase the alignment of the ultimate object, making this check succeed.
5118unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
5119                                                  unsigned PrefAlign) {
5120  unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
5121                      sizeof(PrefAlign) * CHAR_BIT;
5122  APInt Mask = APInt::getAllOnesValue(BitWidth);
5123  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
5124  ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
5125  unsigned TrailZ = KnownZero.countTrailingOnes();
5126  unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
5127
5128  if (PrefAlign > Align)
5129    Align = EnforceKnownAlignment(V, Align, PrefAlign);
5130
5131    // We don't need to make any adjustment.
5132  return Align;
5133}
5134
5135Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
5136  unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
5137  unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
5138  unsigned MinAlign = std::min(DstAlign, SrcAlign);
5139  unsigned CopyAlign = MI->getAlignment();
5140
5141  if (CopyAlign < MinAlign) {
5142    MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
5143                                             MinAlign, false));
5144    return MI;
5145  }
5146
5147  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
5148  // load/store.
5149  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
5150  if (MemOpLength == 0) return 0;
5151
5152  // Source and destination pointer types are always "i8*" for intrinsic.  See
5153  // if the size is something we can handle with a single primitive load/store.
5154  // A single load+store correctly handles overlapping memory in the memmove
5155  // case.
5156  unsigned Size = MemOpLength->getZExtValue();
5157  if (Size == 0) return MI;  // Delete this mem transfer.
5158
5159  if (Size > 8 || (Size&(Size-1)))
5160    return 0;  // If not 1/2/4/8 bytes, exit.
5161
5162  // Use an integer load+store unless we can find something better.
5163  Type *NewPtrTy =
5164            PointerType::getUnqual(IntegerType::get(MI->getContext(), Size<<3));
5165
5166  // Memcpy forces the use of i8* for the source and destination.  That means
5167  // that if you're using memcpy to move one double around, you'll get a cast
5168  // from double* to i8*.  We'd much rather use a double load+store rather than
5169  // an i64 load+store, here because this improves the odds that the source or
5170  // dest address will be promotable.  See if we can find a better type than the
5171  // integer datatype.
5172  if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
5173    const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
5174    if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
5175      // The SrcETy might be something like {{{double}}} or [1 x double].  Rip
5176      // down through these levels if so.
5177      while (!SrcETy->isSingleValueType()) {
5178        if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
5179          if (STy->getNumElements() == 1)
5180            SrcETy = STy->getElementType(0);
5181          else
5182            break;
5183        } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
5184          if (ATy->getNumElements() == 1)
5185            SrcETy = ATy->getElementType();
5186          else
5187            break;
5188        } else
5189          break;
5190      }
5191
5192      if (SrcETy->isSingleValueType())
5193        NewPtrTy = PointerType::getUnqual(SrcETy);
5194    }
5195  }
5196
5197
5198  // If the memcpy/memmove provides better alignment info than we can
5199  // infer, use it.
5200  SrcAlign = std::max(SrcAlign, CopyAlign);
5201  DstAlign = std::max(DstAlign, CopyAlign);
5202
5203  Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewPtrTy);
5204  Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewPtrTy);
5205  Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
5206  InsertNewInstBefore(L, *MI);
5207  InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
5208
5209  // Set the size of the copy to 0, it will be deleted on the next iteration.
5210  MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
5211  return MI;
5212}
5213
5214Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
5215  unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
5216  if (MI->getAlignment() < Alignment) {
5217    MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
5218                                             Alignment, false));
5219    return MI;
5220  }
5221
5222  // Extract the length and alignment and fill if they are constant.
5223  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
5224  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
5225  if (!LenC || !FillC || FillC->getType() != Type::getInt8Ty(MI->getContext()))
5226    return 0;
5227  uint64_t Len = LenC->getZExtValue();
5228  Alignment = MI->getAlignment();
5229
5230  // If the length is zero, this is a no-op
5231  if (Len == 0) return MI; // memset(d,c,0,a) -> noop
5232
5233  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
5234  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
5235    const Type *ITy = IntegerType::get(MI->getContext(), Len*8);  // n=1 -> i8.
5236
5237    Value *Dest = MI->getDest();
5238    Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy));
5239
5240    // Alignment 0 is identity for alignment 1 for memset, but not store.
5241    if (Alignment == 0) Alignment = 1;
5242
5243    // Extract the fill value and store.
5244    uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
5245    InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
5246                                      Dest, false, Alignment), *MI);
5247
5248    // Set the size of the copy to 0, it will be deleted on the next iteration.
5249    MI->setLength(Constant::getNullValue(LenC->getType()));
5250    return MI;
5251  }
5252
5253  return 0;
5254}
5255
5256
5257/// visitCallInst - CallInst simplification.  This mostly only handles folding
5258/// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
5259/// the heavy lifting.
5260///
5261Instruction *InstCombiner::visitCallInst(CallInst &CI) {
5262  if (isFreeCall(&CI))
5263    return visitFree(CI);
5264
5265  // If the caller function is nounwind, mark the call as nounwind, even if the
5266  // callee isn't.
5267  if (CI.getParent()->getParent()->doesNotThrow() &&
5268      !CI.doesNotThrow()) {
5269    CI.setDoesNotThrow();
5270    return &CI;
5271  }
5272
5273  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
5274  if (!II) return visitCallSite(&CI);
5275
5276  // Intrinsics cannot occur in an invoke, so handle them here instead of in
5277  // visitCallSite.
5278  if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
5279    bool Changed = false;
5280
5281    // memmove/cpy/set of zero bytes is a noop.
5282    if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
5283      if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
5284
5285      if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
5286        if (CI->getZExtValue() == 1) {
5287          // Replace the instruction with just byte operations.  We would
5288          // transform other cases to loads/stores, but we don't know if
5289          // alignment is sufficient.
5290        }
5291    }
5292
5293    // If we have a memmove and the source operation is a constant global,
5294    // then the source and dest pointers can't alias, so we can change this
5295    // into a call to memcpy.
5296    if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
5297      if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
5298        if (GVSrc->isConstant()) {
5299          Module *M = CI.getParent()->getParent()->getParent();
5300          Intrinsic::ID MemCpyID = Intrinsic::memcpy;
5301          const Type *Tys[1];
5302          Tys[0] = CI.getOperand(3)->getType();
5303          CI.setOperand(0,
5304                        Intrinsic::getDeclaration(M, MemCpyID, Tys, 1));
5305          Changed = true;
5306        }
5307    }
5308
5309    if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
5310      // memmove(x,x,size) -> noop.
5311      if (MTI->getSource() == MTI->getDest())
5312        return EraseInstFromFunction(CI);
5313    }
5314
5315    // If we can determine a pointer alignment that is bigger than currently
5316    // set, update the alignment.
5317    if (isa<MemTransferInst>(MI)) {
5318      if (Instruction *I = SimplifyMemTransfer(MI))
5319        return I;
5320    } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
5321      if (Instruction *I = SimplifyMemSet(MSI))
5322        return I;
5323    }
5324
5325    if (Changed) return II;
5326  }
5327
5328  switch (II->getIntrinsicID()) {
5329  default: break;
5330  case Intrinsic::bswap:
5331    // bswap(bswap(x)) -> x
5332    if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
5333      if (Operand->getIntrinsicID() == Intrinsic::bswap)
5334        return ReplaceInstUsesWith(CI, Operand->getOperand(1));
5335
5336    // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
5337    if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) {
5338      if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
5339        if (Operand->getIntrinsicID() == Intrinsic::bswap) {
5340          unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
5341                       TI->getType()->getPrimitiveSizeInBits();
5342          Value *CV = ConstantInt::get(Operand->getType(), C);
5343          Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
5344          return new TruncInst(V, TI->getType());
5345        }
5346    }
5347
5348    break;
5349  case Intrinsic::powi:
5350    if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) {
5351      // powi(x, 0) -> 1.0
5352      if (Power->isZero())
5353        return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
5354      // powi(x, 1) -> x
5355      if (Power->isOne())
5356        return ReplaceInstUsesWith(CI, II->getOperand(1));
5357      // powi(x, -1) -> 1/x
5358      if (Power->isAllOnesValue())
5359        return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
5360                                          II->getOperand(1));
5361    }
5362    break;
5363
5364  case Intrinsic::uadd_with_overflow: {
5365    Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
5366    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
5367    uint32_t BitWidth = IT->getBitWidth();
5368    APInt Mask = APInt::getSignBit(BitWidth);
5369    APInt LHSKnownZero(BitWidth, 0);
5370    APInt LHSKnownOne(BitWidth, 0);
5371    ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
5372    bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
5373    bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
5374
5375    if (LHSKnownNegative || LHSKnownPositive) {
5376      APInt RHSKnownZero(BitWidth, 0);
5377      APInt RHSKnownOne(BitWidth, 0);
5378      ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
5379      bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
5380      bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
5381      if (LHSKnownNegative && RHSKnownNegative) {
5382        // The sign bit is set in both cases: this MUST overflow.
5383        // Create a simple add instruction, and insert it into the struct.
5384        Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI);
5385        Worklist.Add(Add);
5386        Constant *V[] = {
5387          UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext())
5388        };
5389        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
5390        return InsertValueInst::Create(Struct, Add, 0);
5391      }
5392
5393      if (LHSKnownPositive && RHSKnownPositive) {
5394        // The sign bit is clear in both cases: this CANNOT overflow.
5395        // Create a simple add instruction, and insert it into the struct.
5396        Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI);
5397        Worklist.Add(Add);
5398        Constant *V[] = {
5399          UndefValue::get(LHS->getType()),
5400          ConstantInt::getFalse(II->getContext())
5401        };
5402        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
5403        return InsertValueInst::Create(Struct, Add, 0);
5404      }
5405    }
5406  }
5407  // FALL THROUGH uadd into sadd
5408  case Intrinsic::sadd_with_overflow:
5409    // Canonicalize constants into the RHS.
5410    if (isa<Constant>(II->getOperand(1)) &&
5411        !isa<Constant>(II->getOperand(2))) {
5412      Value *LHS = II->getOperand(1);
5413      II->setOperand(1, II->getOperand(2));
5414      II->setOperand(2, LHS);
5415      return II;
5416    }
5417
5418    // X + undef -> undef
5419    if (isa<UndefValue>(II->getOperand(2)))
5420      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
5421
5422    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
5423      // X + 0 -> {X, false}
5424      if (RHS->isZero()) {
5425        Constant *V[] = {
5426          UndefValue::get(II->getOperand(0)->getType()),
5427          ConstantInt::getFalse(II->getContext())
5428        };
5429        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
5430        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
5431      }
5432    }
5433    break;
5434  case Intrinsic::usub_with_overflow:
5435  case Intrinsic::ssub_with_overflow:
5436    // undef - X -> undef
5437    // X - undef -> undef
5438    if (isa<UndefValue>(II->getOperand(1)) ||
5439        isa<UndefValue>(II->getOperand(2)))
5440      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
5441
5442    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
5443      // X - 0 -> {X, false}
5444      if (RHS->isZero()) {
5445        Constant *V[] = {
5446          UndefValue::get(II->getOperand(1)->getType()),
5447          ConstantInt::getFalse(II->getContext())
5448        };
5449        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
5450        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
5451      }
5452    }
5453    break;
5454  case Intrinsic::umul_with_overflow:
5455  case Intrinsic::smul_with_overflow:
5456    // Canonicalize constants into the RHS.
5457    if (isa<Constant>(II->getOperand(1)) &&
5458        !isa<Constant>(II->getOperand(2))) {
5459      Value *LHS = II->getOperand(1);
5460      II->setOperand(1, II->getOperand(2));
5461      II->setOperand(2, LHS);
5462      return II;
5463    }
5464
5465    // X * undef -> undef
5466    if (isa<UndefValue>(II->getOperand(2)))
5467      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
5468
5469    if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) {
5470      // X*0 -> {0, false}
5471      if (RHSI->isZero())
5472        return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
5473
5474      // X * 1 -> {X, false}
5475      if (RHSI->equalsInt(1)) {
5476        Constant *V[] = {
5477          UndefValue::get(II->getOperand(1)->getType()),
5478          ConstantInt::getFalse(II->getContext())
5479        };
5480        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
5481        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
5482      }
5483    }
5484    break;
5485  case Intrinsic::ppc_altivec_lvx:
5486  case Intrinsic::ppc_altivec_lvxl:
5487  case Intrinsic::x86_sse_loadu_ps:
5488  case Intrinsic::x86_sse2_loadu_pd:
5489  case Intrinsic::x86_sse2_loadu_dq:
5490    // Turn PPC lvx     -> load if the pointer is known aligned.
5491    // Turn X86 loadups -> load if the pointer is known aligned.
5492    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
5493      Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
5494                                         PointerType::getUnqual(II->getType()));
5495      return new LoadInst(Ptr);
5496    }
5497    break;
5498  case Intrinsic::ppc_altivec_stvx:
5499  case Intrinsic::ppc_altivec_stvxl:
5500    // Turn stvx -> store if the pointer is known aligned.
5501    if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
5502      const Type *OpPtrTy =
5503        PointerType::getUnqual(II->getOperand(1)->getType());
5504      Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
5505      return new StoreInst(II->getOperand(1), Ptr);
5506    }
5507    break;
5508  case Intrinsic::x86_sse_storeu_ps:
5509  case Intrinsic::x86_sse2_storeu_pd:
5510  case Intrinsic::x86_sse2_storeu_dq:
5511    // Turn X86 storeu -> store if the pointer is known aligned.
5512    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
5513      const Type *OpPtrTy =
5514        PointerType::getUnqual(II->getOperand(2)->getType());
5515      Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
5516      return new StoreInst(II->getOperand(2), Ptr);
5517    }
5518    break;
5519
5520  case Intrinsic::x86_sse_cvttss2si: {
5521    // These intrinsics only demands the 0th element of its input vector.  If
5522    // we can simplify the input based on that, do so now.
5523    unsigned VWidth =
5524      cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
5525    APInt DemandedElts(VWidth, 1);
5526    APInt UndefElts(VWidth, 0);
5527    if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
5528                                              UndefElts)) {
5529      II->setOperand(1, V);
5530      return II;
5531    }
5532    break;
5533  }
5534
5535  case Intrinsic::ppc_altivec_vperm:
5536    // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
5537    if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
5538      assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
5539
5540      // Check that all of the elements are integer constants or undefs.
5541      bool AllEltsOk = true;
5542      for (unsigned i = 0; i != 16; ++i) {
5543        if (!isa<ConstantInt>(Mask->getOperand(i)) &&
5544            !isa<UndefValue>(Mask->getOperand(i))) {
5545          AllEltsOk = false;
5546          break;
5547        }
5548      }
5549
5550      if (AllEltsOk) {
5551        // Cast the input vectors to byte vectors.
5552        Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
5553        Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
5554        Value *Result = UndefValue::get(Op0->getType());
5555
5556        // Only extract each element once.
5557        Value *ExtractedElts[32];
5558        memset(ExtractedElts, 0, sizeof(ExtractedElts));
5559
5560        for (unsigned i = 0; i != 16; ++i) {
5561          if (isa<UndefValue>(Mask->getOperand(i)))
5562            continue;
5563          unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
5564          Idx &= 31;  // Match the hardware behavior.
5565
5566          if (ExtractedElts[Idx] == 0) {
5567            ExtractedElts[Idx] =
5568              Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
5569                  ConstantInt::get(Type::getInt32Ty(II->getContext()),
5570                                   Idx&15, false), "tmp");
5571          }
5572
5573          // Insert this value into the result vector.
5574          Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
5575                         ConstantInt::get(Type::getInt32Ty(II->getContext()),
5576                                          i, false), "tmp");
5577        }
5578        return CastInst::Create(Instruction::BitCast, Result, CI.getType());
5579      }
5580    }
5581    break;
5582
5583  case Intrinsic::stackrestore: {
5584    // If the save is right next to the restore, remove the restore.  This can
5585    // happen when variable allocas are DCE'd.
5586    if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
5587      if (SS->getIntrinsicID() == Intrinsic::stacksave) {
5588        BasicBlock::iterator BI = SS;
5589        if (&*++BI == II)
5590          return EraseInstFromFunction(CI);
5591      }
5592    }
5593
5594    // Scan down this block to see if there is another stack restore in the
5595    // same block without an intervening call/alloca.
5596    BasicBlock::iterator BI = II;
5597    TerminatorInst *TI = II->getParent()->getTerminator();
5598    bool CannotRemove = false;
5599    for (++BI; &*BI != TI; ++BI) {
5600      if (isa<AllocaInst>(BI) || isMalloc(BI)) {
5601        CannotRemove = true;
5602        break;
5603      }
5604      if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
5605        if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
5606          // If there is a stackrestore below this one, remove this one.
5607          if (II->getIntrinsicID() == Intrinsic::stackrestore)
5608            return EraseInstFromFunction(CI);
5609          // Otherwise, ignore the intrinsic.
5610        } else {
5611          // If we found a non-intrinsic call, we can't remove the stack
5612          // restore.
5613          CannotRemove = true;
5614          break;
5615        }
5616      }
5617    }
5618
5619    // If the stack restore is in a return/unwind block and if there are no
5620    // allocas or calls between the restore and the return, nuke the restore.
5621    if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
5622      return EraseInstFromFunction(CI);
5623    break;
5624  }
5625  }
5626
5627  return visitCallSite(II);
5628}
5629
5630// InvokeInst simplification
5631//
5632Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
5633  return visitCallSite(&II);
5634}
5635
5636/// isSafeToEliminateVarargsCast - If this cast does not affect the value
5637/// passed through the varargs area, we can eliminate the use of the cast.
5638static bool isSafeToEliminateVarargsCast(const CallSite CS,
5639                                         const CastInst * const CI,
5640                                         const TargetData * const TD,
5641                                         const int ix) {
5642  if (!CI->isLosslessCast())
5643    return false;
5644
5645  // The size of ByVal arguments is derived from the type, so we
5646  // can't change to a type with a different size.  If the size were
5647  // passed explicitly we could avoid this check.
5648  if (!CS.paramHasAttr(ix, Attribute::ByVal))
5649    return true;
5650
5651  const Type* SrcTy =
5652            cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
5653  const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
5654  if (!SrcTy->isSized() || !DstTy->isSized())
5655    return false;
5656  if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
5657    return false;
5658  return true;
5659}
5660
5661// visitCallSite - Improvements for call and invoke instructions.
5662//
5663Instruction *InstCombiner::visitCallSite(CallSite CS) {
5664  bool Changed = false;
5665
5666  // If the callee is a constexpr cast of a function, attempt to move the cast
5667  // to the arguments of the call/invoke.
5668  if (transformConstExprCastCall(CS)) return 0;
5669
5670  Value *Callee = CS.getCalledValue();
5671
5672  if (Function *CalleeF = dyn_cast<Function>(Callee))
5673    if (CalleeF->getCallingConv() != CS.getCallingConv()) {
5674      Instruction *OldCall = CS.getInstruction();
5675      // If the call and callee calling conventions don't match, this call must
5676      // be unreachable, as the call is undefined.
5677      new StoreInst(ConstantInt::getTrue(Callee->getContext()),
5678                UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
5679                                  OldCall);
5680      // If OldCall dues not return void then replaceAllUsesWith undef.
5681      // This allows ValueHandlers and custom metadata to adjust itself.
5682      if (!OldCall->getType()->isVoidTy())
5683        OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
5684      if (isa<CallInst>(OldCall))   // Not worth removing an invoke here.
5685        return EraseInstFromFunction(*OldCall);
5686      return 0;
5687    }
5688
5689  if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
5690    // This instruction is not reachable, just remove it.  We insert a store to
5691    // undef so that we know that this code is not reachable, despite the fact
5692    // that we can't modify the CFG here.
5693    new StoreInst(ConstantInt::getTrue(Callee->getContext()),
5694               UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
5695                  CS.getInstruction());
5696
5697    // If CS dues not return void then replaceAllUsesWith undef.
5698    // This allows ValueHandlers and custom metadata to adjust itself.
5699    if (!CS.getInstruction()->getType()->isVoidTy())
5700      CS.getInstruction()->
5701        replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
5702
5703    if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
5704      // Don't break the CFG, insert a dummy cond branch.
5705      BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
5706                         ConstantInt::getTrue(Callee->getContext()), II);
5707    }
5708    return EraseInstFromFunction(*CS.getInstruction());
5709  }
5710
5711  if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
5712    if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
5713      if (In->getIntrinsicID() == Intrinsic::init_trampoline)
5714        return transformCallThroughTrampoline(CS);
5715
5716  const PointerType *PTy = cast<PointerType>(Callee->getType());
5717  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
5718  if (FTy->isVarArg()) {
5719    int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
5720    // See if we can optimize any arguments passed through the varargs area of
5721    // the call.
5722    for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
5723           E = CS.arg_end(); I != E; ++I, ++ix) {
5724      CastInst *CI = dyn_cast<CastInst>(*I);
5725      if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
5726        *I = CI->getOperand(0);
5727        Changed = true;
5728      }
5729    }
5730  }
5731
5732  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
5733    // Inline asm calls cannot throw - mark them 'nounwind'.
5734    CS.setDoesNotThrow();
5735    Changed = true;
5736  }
5737
5738  return Changed ? CS.getInstruction() : 0;
5739}
5740
5741// transformConstExprCastCall - If the callee is a constexpr cast of a function,
5742// attempt to move the cast to the arguments of the call/invoke.
5743//
5744bool InstCombiner::transformConstExprCastCall(CallSite CS) {
5745  if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
5746  ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
5747  if (CE->getOpcode() != Instruction::BitCast ||
5748      !isa<Function>(CE->getOperand(0)))
5749    return false;
5750  Function *Callee = cast<Function>(CE->getOperand(0));
5751  Instruction *Caller = CS.getInstruction();
5752  const AttrListPtr &CallerPAL = CS.getAttributes();
5753
5754  // Okay, this is a cast from a function to a different type.  Unless doing so
5755  // would cause a type conversion of one of our arguments, change this call to
5756  // be a direct call with arguments casted to the appropriate types.
5757  //
5758  const FunctionType *FT = Callee->getFunctionType();
5759  const Type *OldRetTy = Caller->getType();
5760  const Type *NewRetTy = FT->getReturnType();
5761
5762  if (isa<StructType>(NewRetTy))
5763    return false; // TODO: Handle multiple return values.
5764
5765  // Check to see if we are changing the return type...
5766  if (OldRetTy != NewRetTy) {
5767    if (Callee->isDeclaration() &&
5768        // Conversion is ok if changing from one pointer type to another or from
5769        // a pointer to an integer of the same size.
5770        !((isa<PointerType>(OldRetTy) || !TD ||
5771           OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
5772          (isa<PointerType>(NewRetTy) || !TD ||
5773           NewRetTy == TD->getIntPtrType(Caller->getContext()))))
5774      return false;   // Cannot transform this return value.
5775
5776    if (!Caller->use_empty() &&
5777        // void -> non-void is handled specially
5778        !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
5779      return false;   // Cannot transform this return value.
5780
5781    if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
5782      Attributes RAttrs = CallerPAL.getRetAttributes();
5783      if (RAttrs & Attribute::typeIncompatible(NewRetTy))
5784        return false;   // Attribute not compatible with transformed value.
5785    }
5786
5787    // If the callsite is an invoke instruction, and the return value is used by
5788    // a PHI node in a successor, we cannot change the return type of the call
5789    // because there is no place to put the cast instruction (without breaking
5790    // the critical edge).  Bail out in this case.
5791    if (!Caller->use_empty())
5792      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
5793        for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
5794             UI != E; ++UI)
5795          if (PHINode *PN = dyn_cast<PHINode>(*UI))
5796            if (PN->getParent() == II->getNormalDest() ||
5797                PN->getParent() == II->getUnwindDest())
5798              return false;
5799  }
5800
5801  unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
5802  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
5803
5804  CallSite::arg_iterator AI = CS.arg_begin();
5805  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
5806    const Type *ParamTy = FT->getParamType(i);
5807    const Type *ActTy = (*AI)->getType();
5808
5809    if (!CastInst::isCastable(ActTy, ParamTy))
5810      return false;   // Cannot transform this parameter value.
5811
5812    if (CallerPAL.getParamAttributes(i + 1)
5813        & Attribute::typeIncompatible(ParamTy))
5814      return false;   // Attribute not compatible with transformed value.
5815
5816    // Converting from one pointer type to another or between a pointer and an
5817    // integer of the same size is safe even if we do not have a body.
5818    bool isConvertible = ActTy == ParamTy ||
5819      (TD && ((isa<PointerType>(ParamTy) ||
5820      ParamTy == TD->getIntPtrType(Caller->getContext())) &&
5821              (isa<PointerType>(ActTy) ||
5822              ActTy == TD->getIntPtrType(Caller->getContext()))));
5823    if (Callee->isDeclaration() && !isConvertible) return false;
5824  }
5825
5826  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
5827      Callee->isDeclaration())
5828    return false;   // Do not delete arguments unless we have a function body.
5829
5830  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
5831      !CallerPAL.isEmpty())
5832    // In this case we have more arguments than the new function type, but we
5833    // won't be dropping them.  Check that these extra arguments have attributes
5834    // that are compatible with being a vararg call argument.
5835    for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
5836      if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
5837        break;
5838      Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
5839      if (PAttrs & Attribute::VarArgsIncompatible)
5840        return false;
5841    }
5842
5843  // Okay, we decided that this is a safe thing to do: go ahead and start
5844  // inserting cast instructions as necessary...
5845  std::vector<Value*> Args;
5846  Args.reserve(NumActualArgs);
5847  SmallVector<AttributeWithIndex, 8> attrVec;
5848  attrVec.reserve(NumCommonArgs);
5849
5850  // Get any return attributes.
5851  Attributes RAttrs = CallerPAL.getRetAttributes();
5852
5853  // If the return value is not being used, the type may not be compatible
5854  // with the existing attributes.  Wipe out any problematic attributes.
5855  RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
5856
5857  // Add the new return attributes.
5858  if (RAttrs)
5859    attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
5860
5861  AI = CS.arg_begin();
5862  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
5863    const Type *ParamTy = FT->getParamType(i);
5864    if ((*AI)->getType() == ParamTy) {
5865      Args.push_back(*AI);
5866    } else {
5867      Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
5868          false, ParamTy, false);
5869      Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp"));
5870    }
5871
5872    // Add any parameter attributes.
5873    if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
5874      attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
5875  }
5876
5877  // If the function takes more arguments than the call was taking, add them
5878  // now.
5879  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
5880    Args.push_back(Constant::getNullValue(FT->getParamType(i)));
5881
5882  // If we are removing arguments to the function, emit an obnoxious warning.
5883  if (FT->getNumParams() < NumActualArgs) {
5884    if (!FT->isVarArg()) {
5885      errs() << "WARNING: While resolving call to function '"
5886             << Callee->getName() << "' arguments were dropped!\n";
5887    } else {
5888      // Add all of the arguments in their promoted form to the arg list.
5889      for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
5890        const Type *PTy = getPromotedType((*AI)->getType());
5891        if (PTy != (*AI)->getType()) {
5892          // Must promote to pass through va_arg area!
5893          Instruction::CastOps opcode =
5894            CastInst::getCastOpcode(*AI, false, PTy, false);
5895          Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp"));
5896        } else {
5897          Args.push_back(*AI);
5898        }
5899
5900        // Add any parameter attributes.
5901        if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
5902          attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
5903      }
5904    }
5905  }
5906
5907  if (Attributes FnAttrs =  CallerPAL.getFnAttributes())
5908    attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
5909
5910  if (NewRetTy->isVoidTy())
5911    Caller->setName("");   // Void type should not have a name.
5912
5913  const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
5914                                                     attrVec.end());
5915
5916  Instruction *NC;
5917  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
5918    NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
5919                            Args.begin(), Args.end(),
5920                            Caller->getName(), Caller);
5921    cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
5922    cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
5923  } else {
5924    NC = CallInst::Create(Callee, Args.begin(), Args.end(),
5925                          Caller->getName(), Caller);
5926    CallInst *CI = cast<CallInst>(Caller);
5927    if (CI->isTailCall())
5928      cast<CallInst>(NC)->setTailCall();
5929    cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
5930    cast<CallInst>(NC)->setAttributes(NewCallerPAL);
5931  }
5932
5933  // Insert a cast of the return type as necessary.
5934  Value *NV = NC;
5935  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
5936    if (!NV->getType()->isVoidTy()) {
5937      Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
5938                                                            OldRetTy, false);
5939      NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
5940
5941      // If this is an invoke instruction, we should insert it after the first
5942      // non-phi, instruction in the normal successor block.
5943      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
5944        BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
5945        InsertNewInstBefore(NC, *I);
5946      } else {
5947        // Otherwise, it's a call, just insert cast right after the call instr
5948        InsertNewInstBefore(NC, *Caller);
5949      }
5950      Worklist.AddUsersToWorkList(*Caller);
5951    } else {
5952      NV = UndefValue::get(Caller->getType());
5953    }
5954  }
5955
5956
5957  if (!Caller->use_empty())
5958    Caller->replaceAllUsesWith(NV);
5959
5960  EraseInstFromFunction(*Caller);
5961  return true;
5962}
5963
5964// transformCallThroughTrampoline - Turn a call to a function created by the
5965// init_trampoline intrinsic into a direct call to the underlying function.
5966//
5967Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
5968  Value *Callee = CS.getCalledValue();
5969  const PointerType *PTy = cast<PointerType>(Callee->getType());
5970  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
5971  const AttrListPtr &Attrs = CS.getAttributes();
5972
5973  // If the call already has the 'nest' attribute somewhere then give up -
5974  // otherwise 'nest' would occur twice after splicing in the chain.
5975  if (Attrs.hasAttrSomewhere(Attribute::Nest))
5976    return 0;
5977
5978  IntrinsicInst *Tramp =
5979    cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
5980
5981  Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts());
5982  const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
5983  const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
5984
5985  const AttrListPtr &NestAttrs = NestF->getAttributes();
5986  if (!NestAttrs.isEmpty()) {
5987    unsigned NestIdx = 1;
5988    const Type *NestTy = 0;
5989    Attributes NestAttr = Attribute::None;
5990
5991    // Look for a parameter marked with the 'nest' attribute.
5992    for (FunctionType::param_iterator I = NestFTy->param_begin(),
5993         E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
5994      if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
5995        // Record the parameter type and any other attributes.
5996        NestTy = *I;
5997        NestAttr = NestAttrs.getParamAttributes(NestIdx);
5998        break;
5999      }
6000
6001    if (NestTy) {
6002      Instruction *Caller = CS.getInstruction();
6003      std::vector<Value*> NewArgs;
6004      NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
6005
6006      SmallVector<AttributeWithIndex, 8> NewAttrs;
6007      NewAttrs.reserve(Attrs.getNumSlots() + 1);
6008
6009      // Insert the nest argument into the call argument list, which may
6010      // mean appending it.  Likewise for attributes.
6011
6012      // Add any result attributes.
6013      if (Attributes Attr = Attrs.getRetAttributes())
6014        NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
6015
6016      {
6017        unsigned Idx = 1;
6018        CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
6019        do {
6020          if (Idx == NestIdx) {
6021            // Add the chain argument and attributes.
6022            Value *NestVal = Tramp->getOperand(3);
6023            if (NestVal->getType() != NestTy)
6024              NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
6025            NewArgs.push_back(NestVal);
6026            NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
6027          }
6028
6029          if (I == E)
6030            break;
6031
6032          // Add the original argument and attributes.
6033          NewArgs.push_back(*I);
6034          if (Attributes Attr = Attrs.getParamAttributes(Idx))
6035            NewAttrs.push_back
6036              (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
6037
6038          ++Idx, ++I;
6039        } while (1);
6040      }
6041
6042      // Add any function attributes.
6043      if (Attributes Attr = Attrs.getFnAttributes())
6044        NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
6045
6046      // The trampoline may have been bitcast to a bogus type (FTy).
6047      // Handle this by synthesizing a new function type, equal to FTy
6048      // with the chain parameter inserted.
6049
6050      std::vector<const Type*> NewTypes;
6051      NewTypes.reserve(FTy->getNumParams()+1);
6052
6053      // Insert the chain's type into the list of parameter types, which may
6054      // mean appending it.
6055      {
6056        unsigned Idx = 1;
6057        FunctionType::param_iterator I = FTy->param_begin(),
6058          E = FTy->param_end();
6059
6060        do {
6061          if (Idx == NestIdx)
6062            // Add the chain's type.
6063            NewTypes.push_back(NestTy);
6064
6065          if (I == E)
6066            break;
6067
6068          // Add the original type.
6069          NewTypes.push_back(*I);
6070
6071          ++Idx, ++I;
6072        } while (1);
6073      }
6074
6075      // Replace the trampoline call with a direct call.  Let the generic
6076      // code sort out any function type mismatches.
6077      FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
6078                                                FTy->isVarArg());
6079      Constant *NewCallee =
6080        NestF->getType() == PointerType::getUnqual(NewFTy) ?
6081        NestF : ConstantExpr::getBitCast(NestF,
6082                                         PointerType::getUnqual(NewFTy));
6083      const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
6084                                                   NewAttrs.end());
6085
6086      Instruction *NewCaller;
6087      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
6088        NewCaller = InvokeInst::Create(NewCallee,
6089                                       II->getNormalDest(), II->getUnwindDest(),
6090                                       NewArgs.begin(), NewArgs.end(),
6091                                       Caller->getName(), Caller);
6092        cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
6093        cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
6094      } else {
6095        NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
6096                                     Caller->getName(), Caller);
6097        if (cast<CallInst>(Caller)->isTailCall())
6098          cast<CallInst>(NewCaller)->setTailCall();
6099        cast<CallInst>(NewCaller)->
6100          setCallingConv(cast<CallInst>(Caller)->getCallingConv());
6101        cast<CallInst>(NewCaller)->setAttributes(NewPAL);
6102      }
6103      if (!Caller->getType()->isVoidTy())
6104        Caller->replaceAllUsesWith(NewCaller);
6105      Caller->eraseFromParent();
6106      Worklist.Remove(Caller);
6107      return 0;
6108    }
6109  }
6110
6111  // Replace the trampoline call with a direct call.  Since there is no 'nest'
6112  // parameter, there is no need to adjust the argument list.  Let the generic
6113  // code sort out any function type mismatches.
6114  Constant *NewCallee =
6115    NestF->getType() == PTy ? NestF :
6116                              ConstantExpr::getBitCast(NestF, PTy);
6117  CS.setCalledFunction(NewCallee);
6118  return CS.getInstruction();
6119}
6120
6121
6122
6123Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
6124  SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
6125
6126  if (Value *V = SimplifyGEPInst(&Ops[0], Ops.size(), TD))
6127    return ReplaceInstUsesWith(GEP, V);
6128
6129  Value *PtrOp = GEP.getOperand(0);
6130
6131  if (isa<UndefValue>(GEP.getOperand(0)))
6132    return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
6133
6134  // Eliminate unneeded casts for indices.
6135  if (TD) {
6136    bool MadeChange = false;
6137    unsigned PtrSize = TD->getPointerSizeInBits();
6138
6139    gep_type_iterator GTI = gep_type_begin(GEP);
6140    for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
6141         I != E; ++I, ++GTI) {
6142      if (!isa<SequentialType>(*GTI)) continue;
6143
6144      // If we are using a wider index than needed for this platform, shrink it
6145      // to what we need.  If narrower, sign-extend it to what we need.  This
6146      // explicit cast can make subsequent optimizations more obvious.
6147      unsigned OpBits = cast<IntegerType>((*I)->getType())->getBitWidth();
6148      if (OpBits == PtrSize)
6149        continue;
6150
6151      *I = Builder->CreateIntCast(*I, TD->getIntPtrType(GEP.getContext()),true);
6152      MadeChange = true;
6153    }
6154    if (MadeChange) return &GEP;
6155  }
6156
6157  // Combine Indices - If the source pointer to this getelementptr instruction
6158  // is a getelementptr instruction, combine the indices of the two
6159  // getelementptr instructions into a single instruction.
6160  //
6161  if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
6162    // Note that if our source is a gep chain itself that we wait for that
6163    // chain to be resolved before we perform this transformation.  This
6164    // avoids us creating a TON of code in some cases.
6165    //
6166    if (GetElementPtrInst *SrcGEP =
6167          dyn_cast<GetElementPtrInst>(Src->getOperand(0)))
6168      if (SrcGEP->getNumOperands() == 2)
6169        return 0;   // Wait until our source is folded to completion.
6170
6171    SmallVector<Value*, 8> Indices;
6172
6173    // Find out whether the last index in the source GEP is a sequential idx.
6174    bool EndsWithSequential = false;
6175    for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
6176         I != E; ++I)
6177      EndsWithSequential = !isa<StructType>(*I);
6178
6179    // Can we combine the two pointer arithmetics offsets?
6180    if (EndsWithSequential) {
6181      // Replace: gep (gep %P, long B), long A, ...
6182      // With:    T = long A+B; gep %P, T, ...
6183      //
6184      Value *Sum;
6185      Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
6186      Value *GO1 = GEP.getOperand(1);
6187      if (SO1 == Constant::getNullValue(SO1->getType())) {
6188        Sum = GO1;
6189      } else if (GO1 == Constant::getNullValue(GO1->getType())) {
6190        Sum = SO1;
6191      } else {
6192        // If they aren't the same type, then the input hasn't been processed
6193        // by the loop above yet (which canonicalizes sequential index types to
6194        // intptr_t).  Just avoid transforming this until the input has been
6195        // normalized.
6196        if (SO1->getType() != GO1->getType())
6197          return 0;
6198        Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
6199      }
6200
6201      // Update the GEP in place if possible.
6202      if (Src->getNumOperands() == 2) {
6203        GEP.setOperand(0, Src->getOperand(0));
6204        GEP.setOperand(1, Sum);
6205        return &GEP;
6206      }
6207      Indices.append(Src->op_begin()+1, Src->op_end()-1);
6208      Indices.push_back(Sum);
6209      Indices.append(GEP.op_begin()+2, GEP.op_end());
6210    } else if (isa<Constant>(*GEP.idx_begin()) &&
6211               cast<Constant>(*GEP.idx_begin())->isNullValue() &&
6212               Src->getNumOperands() != 1) {
6213      // Otherwise we can do the fold if the first index of the GEP is a zero
6214      Indices.append(Src->op_begin()+1, Src->op_end());
6215      Indices.append(GEP.idx_begin()+1, GEP.idx_end());
6216    }
6217
6218    if (!Indices.empty())
6219      return (cast<GEPOperator>(&GEP)->isInBounds() &&
6220              Src->isInBounds()) ?
6221        GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(),
6222                                          Indices.end(), GEP.getName()) :
6223        GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(),
6224                                  Indices.end(), GEP.getName());
6225  }
6226
6227  // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
6228  if (Value *X = getBitCastOperand(PtrOp)) {
6229    assert(isa<PointerType>(X->getType()) && "Must be cast from pointer");
6230
6231    // If the input bitcast is actually "bitcast(bitcast(x))", then we don't
6232    // want to change the gep until the bitcasts are eliminated.
6233    if (getBitCastOperand(X)) {
6234      Worklist.AddValue(PtrOp);
6235      return 0;
6236    }
6237
6238    bool HasZeroPointerIndex = false;
6239    if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
6240      HasZeroPointerIndex = C->isZero();
6241
6242    // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
6243    // into     : GEP [10 x i8]* X, i32 0, ...
6244    //
6245    // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
6246    //           into     : GEP i8* X, ...
6247    //
6248    // This occurs when the program declares an array extern like "int X[];"
6249    if (HasZeroPointerIndex) {
6250      const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
6251      const PointerType *XTy = cast<PointerType>(X->getType());
6252      if (const ArrayType *CATy =
6253          dyn_cast<ArrayType>(CPTy->getElementType())) {
6254        // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
6255        if (CATy->getElementType() == XTy->getElementType()) {
6256          // -> GEP i8* X, ...
6257          SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
6258          return cast<GEPOperator>(&GEP)->isInBounds() ?
6259            GetElementPtrInst::CreateInBounds(X, Indices.begin(), Indices.end(),
6260                                              GEP.getName()) :
6261            GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
6262                                      GEP.getName());
6263        }
6264
6265        if (const ArrayType *XATy = dyn_cast<ArrayType>(XTy->getElementType())){
6266          // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
6267          if (CATy->getElementType() == XATy->getElementType()) {
6268            // -> GEP [10 x i8]* X, i32 0, ...
6269            // At this point, we know that the cast source type is a pointer
6270            // to an array of the same type as the destination pointer
6271            // array.  Because the array type is never stepped over (there
6272            // is a leading zero) we can fold the cast into this GEP.
6273            GEP.setOperand(0, X);
6274            return &GEP;
6275          }
6276        }
6277      }
6278    } else if (GEP.getNumOperands() == 2) {
6279      // Transform things like:
6280      // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
6281      // into:  %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
6282      const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
6283      const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
6284      if (TD && isa<ArrayType>(SrcElTy) &&
6285          TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
6286          TD->getTypeAllocSize(ResElTy)) {
6287        Value *Idx[2];
6288        Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
6289        Idx[1] = GEP.getOperand(1);
6290        Value *NewGEP = cast<GEPOperator>(&GEP)->isInBounds() ?
6291          Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) :
6292          Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName());
6293        // V and GEP are both pointer types --> BitCast
6294        return new BitCastInst(NewGEP, GEP.getType());
6295      }
6296
6297      // Transform things like:
6298      // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
6299      //   (where tmp = 8*tmp2) into:
6300      // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
6301
6302      if (TD && isa<ArrayType>(SrcElTy) &&
6303          ResElTy == Type::getInt8Ty(GEP.getContext())) {
6304        uint64_t ArrayEltSize =
6305            TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
6306
6307        // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We
6308        // allow either a mul, shift, or constant here.
6309        Value *NewIdx = 0;
6310        ConstantInt *Scale = 0;
6311        if (ArrayEltSize == 1) {
6312          NewIdx = GEP.getOperand(1);
6313          Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
6314        } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
6315          NewIdx = ConstantInt::get(CI->getType(), 1);
6316          Scale = CI;
6317        } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
6318          if (Inst->getOpcode() == Instruction::Shl &&
6319              isa<ConstantInt>(Inst->getOperand(1))) {
6320            ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
6321            uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
6322            Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
6323                                     1ULL << ShAmtVal);
6324            NewIdx = Inst->getOperand(0);
6325          } else if (Inst->getOpcode() == Instruction::Mul &&
6326                     isa<ConstantInt>(Inst->getOperand(1))) {
6327            Scale = cast<ConstantInt>(Inst->getOperand(1));
6328            NewIdx = Inst->getOperand(0);
6329          }
6330        }
6331
6332        // If the index will be to exactly the right offset with the scale taken
6333        // out, perform the transformation. Note, we don't know whether Scale is
6334        // signed or not. We'll use unsigned version of division/modulo
6335        // operation after making sure Scale doesn't have the sign bit set.
6336        if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
6337            Scale->getZExtValue() % ArrayEltSize == 0) {
6338          Scale = ConstantInt::get(Scale->getType(),
6339                                   Scale->getZExtValue() / ArrayEltSize);
6340          if (Scale->getZExtValue() != 1) {
6341            Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
6342                                                       false /*ZExt*/);
6343            NewIdx = Builder->CreateMul(NewIdx, C, "idxscale");
6344          }
6345
6346          // Insert the new GEP instruction.
6347          Value *Idx[2];
6348          Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
6349          Idx[1] = NewIdx;
6350          Value *NewGEP = cast<GEPOperator>(&GEP)->isInBounds() ?
6351            Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) :
6352            Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName());
6353          // The NewGEP must be pointer typed, so must the old one -> BitCast
6354          return new BitCastInst(NewGEP, GEP.getType());
6355        }
6356      }
6357    }
6358  }
6359
6360  /// See if we can simplify:
6361  ///   X = bitcast A* to B*
6362  ///   Y = gep X, <...constant indices...>
6363  /// into a gep of the original struct.  This is important for SROA and alias
6364  /// analysis of unions.  If "A" is also a bitcast, wait for A/X to be merged.
6365  if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
6366    if (TD &&
6367        !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
6368      // Determine how much the GEP moves the pointer.  We are guaranteed to get
6369      // a constant back from EmitGEPOffset.
6370      ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP));
6371      int64_t Offset = OffsetV->getSExtValue();
6372
6373      // If this GEP instruction doesn't move the pointer, just replace the GEP
6374      // with a bitcast of the real input to the dest type.
6375      if (Offset == 0) {
6376        // If the bitcast is of an allocation, and the allocation will be
6377        // converted to match the type of the cast, don't touch this.
6378        if (isa<AllocaInst>(BCI->getOperand(0)) ||
6379            isMalloc(BCI->getOperand(0))) {
6380          // See if the bitcast simplifies, if so, don't nuke this GEP yet.
6381          if (Instruction *I = visitBitCast(*BCI)) {
6382            if (I != BCI) {
6383              I->takeName(BCI);
6384              BCI->getParent()->getInstList().insert(BCI, I);
6385              ReplaceInstUsesWith(*BCI, I);
6386            }
6387            return &GEP;
6388          }
6389        }
6390        return new BitCastInst(BCI->getOperand(0), GEP.getType());
6391      }
6392
6393      // Otherwise, if the offset is non-zero, we need to find out if there is a
6394      // field at Offset in 'A's type.  If so, we can pull the cast through the
6395      // GEP.
6396      SmallVector<Value*, 8> NewIndices;
6397      const Type *InTy =
6398        cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
6399      if (FindElementAtOffset(InTy, Offset, NewIndices)) {
6400        Value *NGEP = cast<GEPOperator>(&GEP)->isInBounds() ?
6401          Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices.begin(),
6402                                     NewIndices.end()) :
6403          Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(),
6404                             NewIndices.end());
6405
6406        if (NGEP->getType() == GEP.getType())
6407          return ReplaceInstUsesWith(GEP, NGEP);
6408        NGEP->takeName(&GEP);
6409        return new BitCastInst(NGEP, GEP.getType());
6410      }
6411    }
6412  }
6413
6414  return 0;
6415}
6416
6417Instruction *InstCombiner::visitFree(Instruction &FI) {
6418  Value *Op = FI.getOperand(1);
6419
6420  // free undef -> unreachable.
6421  if (isa<UndefValue>(Op)) {
6422    // Insert a new store to null because we cannot modify the CFG here.
6423    new StoreInst(ConstantInt::getTrue(FI.getContext()),
6424           UndefValue::get(Type::getInt1PtrTy(FI.getContext())), &FI);
6425    return EraseInstFromFunction(FI);
6426  }
6427
6428  // If we have 'free null' delete the instruction.  This can happen in stl code
6429  // when lots of inlining happens.
6430  if (isa<ConstantPointerNull>(Op))
6431    return EraseInstFromFunction(FI);
6432
6433  // If we have a malloc call whose only use is a free call, delete both.
6434  if (isMalloc(Op)) {
6435    if (CallInst* CI = extractMallocCallFromBitCast(Op)) {
6436      if (Op->hasOneUse() && CI->hasOneUse()) {
6437        EraseInstFromFunction(FI);
6438        EraseInstFromFunction(*CI);
6439        return EraseInstFromFunction(*cast<Instruction>(Op));
6440      }
6441    } else {
6442      // Op is a call to malloc
6443      if (Op->hasOneUse()) {
6444        EraseInstFromFunction(FI);
6445        return EraseInstFromFunction(*cast<Instruction>(Op));
6446      }
6447    }
6448  }
6449
6450  return 0;
6451}
6452
6453
6454
6455Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
6456  // Change br (not X), label True, label False to: br X, label False, True
6457  Value *X = 0;
6458  BasicBlock *TrueDest;
6459  BasicBlock *FalseDest;
6460  if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
6461      !isa<Constant>(X)) {
6462    // Swap Destinations and condition...
6463    BI.setCondition(X);
6464    BI.setSuccessor(0, FalseDest);
6465    BI.setSuccessor(1, TrueDest);
6466    return &BI;
6467  }
6468
6469  // Cannonicalize fcmp_one -> fcmp_oeq
6470  FCmpInst::Predicate FPred; Value *Y;
6471  if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
6472                             TrueDest, FalseDest)) &&
6473      BI.getCondition()->hasOneUse())
6474    if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
6475        FPred == FCmpInst::FCMP_OGE) {
6476      FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
6477      Cond->setPredicate(FCmpInst::getInversePredicate(FPred));
6478
6479      // Swap Destinations and condition.
6480      BI.setSuccessor(0, FalseDest);
6481      BI.setSuccessor(1, TrueDest);
6482      Worklist.Add(Cond);
6483      return &BI;
6484    }
6485
6486  // Cannonicalize icmp_ne -> icmp_eq
6487  ICmpInst::Predicate IPred;
6488  if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
6489                      TrueDest, FalseDest)) &&
6490      BI.getCondition()->hasOneUse())
6491    if (IPred == ICmpInst::ICMP_NE  || IPred == ICmpInst::ICMP_ULE ||
6492        IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
6493        IPred == ICmpInst::ICMP_SGE) {
6494      ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
6495      Cond->setPredicate(ICmpInst::getInversePredicate(IPred));
6496      // Swap Destinations and condition.
6497      BI.setSuccessor(0, FalseDest);
6498      BI.setSuccessor(1, TrueDest);
6499      Worklist.Add(Cond);
6500      return &BI;
6501    }
6502
6503  return 0;
6504}
6505
6506Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
6507  Value *Cond = SI.getCondition();
6508  if (Instruction *I = dyn_cast<Instruction>(Cond)) {
6509    if (I->getOpcode() == Instruction::Add)
6510      if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
6511        // change 'switch (X+4) case 1:' into 'switch (X) case -3'
6512        for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
6513          SI.setOperand(i,
6514                   ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
6515                                                AddRHS));
6516        SI.setOperand(0, I->getOperand(0));
6517        Worklist.Add(I);
6518        return &SI;
6519      }
6520  }
6521  return 0;
6522}
6523
6524Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
6525  Value *Agg = EV.getAggregateOperand();
6526
6527  if (!EV.hasIndices())
6528    return ReplaceInstUsesWith(EV, Agg);
6529
6530  if (Constant *C = dyn_cast<Constant>(Agg)) {
6531    if (isa<UndefValue>(C))
6532      return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
6533
6534    if (isa<ConstantAggregateZero>(C))
6535      return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
6536
6537    if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
6538      // Extract the element indexed by the first index out of the constant
6539      Value *V = C->getOperand(*EV.idx_begin());
6540      if (EV.getNumIndices() > 1)
6541        // Extract the remaining indices out of the constant indexed by the
6542        // first index
6543        return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end());
6544      else
6545        return ReplaceInstUsesWith(EV, V);
6546    }
6547    return 0; // Can't handle other constants
6548  }
6549  if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
6550    // We're extracting from an insertvalue instruction, compare the indices
6551    const unsigned *exti, *exte, *insi, *inse;
6552    for (exti = EV.idx_begin(), insi = IV->idx_begin(),
6553         exte = EV.idx_end(), inse = IV->idx_end();
6554         exti != exte && insi != inse;
6555         ++exti, ++insi) {
6556      if (*insi != *exti)
6557        // The insert and extract both reference distinctly different elements.
6558        // This means the extract is not influenced by the insert, and we can
6559        // replace the aggregate operand of the extract with the aggregate
6560        // operand of the insert. i.e., replace
6561        // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
6562        // %E = extractvalue { i32, { i32 } } %I, 0
6563        // with
6564        // %E = extractvalue { i32, { i32 } } %A, 0
6565        return ExtractValueInst::Create(IV->getAggregateOperand(),
6566                                        EV.idx_begin(), EV.idx_end());
6567    }
6568    if (exti == exte && insi == inse)
6569      // Both iterators are at the end: Index lists are identical. Replace
6570      // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
6571      // %C = extractvalue { i32, { i32 } } %B, 1, 0
6572      // with "i32 42"
6573      return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
6574    if (exti == exte) {
6575      // The extract list is a prefix of the insert list. i.e. replace
6576      // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
6577      // %E = extractvalue { i32, { i32 } } %I, 1
6578      // with
6579      // %X = extractvalue { i32, { i32 } } %A, 1
6580      // %E = insertvalue { i32 } %X, i32 42, 0
6581      // by switching the order of the insert and extract (though the
6582      // insertvalue should be left in, since it may have other uses).
6583      Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
6584                                                 EV.idx_begin(), EV.idx_end());
6585      return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
6586                                     insi, inse);
6587    }
6588    if (insi == inse)
6589      // The insert list is a prefix of the extract list
6590      // We can simply remove the common indices from the extract and make it
6591      // operate on the inserted value instead of the insertvalue result.
6592      // i.e., replace
6593      // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
6594      // %E = extractvalue { i32, { i32 } } %I, 1, 0
6595      // with
6596      // %E extractvalue { i32 } { i32 42 }, 0
6597      return ExtractValueInst::Create(IV->getInsertedValueOperand(),
6598                                      exti, exte);
6599  }
6600  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) {
6601    // We're extracting from an intrinsic, see if we're the only user, which
6602    // allows us to simplify multiple result intrinsics to simpler things that
6603    // just get one value..
6604    if (II->hasOneUse()) {
6605      // Check if we're grabbing the overflow bit or the result of a 'with
6606      // overflow' intrinsic.  If it's the latter we can remove the intrinsic
6607      // and replace it with a traditional binary instruction.
6608      switch (II->getIntrinsicID()) {
6609      case Intrinsic::uadd_with_overflow:
6610      case Intrinsic::sadd_with_overflow:
6611        if (*EV.idx_begin() == 0) {  // Normal result.
6612          Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
6613          II->replaceAllUsesWith(UndefValue::get(II->getType()));
6614          EraseInstFromFunction(*II);
6615          return BinaryOperator::CreateAdd(LHS, RHS);
6616        }
6617        break;
6618      case Intrinsic::usub_with_overflow:
6619      case Intrinsic::ssub_with_overflow:
6620        if (*EV.idx_begin() == 0) {  // Normal result.
6621          Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
6622          II->replaceAllUsesWith(UndefValue::get(II->getType()));
6623          EraseInstFromFunction(*II);
6624          return BinaryOperator::CreateSub(LHS, RHS);
6625        }
6626        break;
6627      case Intrinsic::umul_with_overflow:
6628      case Intrinsic::smul_with_overflow:
6629        if (*EV.idx_begin() == 0) {  // Normal result.
6630          Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
6631          II->replaceAllUsesWith(UndefValue::get(II->getType()));
6632          EraseInstFromFunction(*II);
6633          return BinaryOperator::CreateMul(LHS, RHS);
6634        }
6635        break;
6636      default:
6637        break;
6638      }
6639    }
6640  }
6641  // Can't simplify extracts from other values. Note that nested extracts are
6642  // already simplified implicitely by the above (extract ( extract (insert) )
6643  // will be translated into extract ( insert ( extract ) ) first and then just
6644  // the value inserted, if appropriate).
6645  return 0;
6646}
6647
6648
6649
6650
6651/// TryToSinkInstruction - Try to move the specified instruction from its
6652/// current block into the beginning of DestBlock, which can only happen if it's
6653/// safe to move the instruction past all of the instructions between it and the
6654/// end of its block.
6655static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
6656  assert(I->hasOneUse() && "Invariants didn't hold!");
6657
6658  // Cannot move control-flow-involving, volatile loads, vaarg, etc.
6659  if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I))
6660    return false;
6661
6662  // Do not sink alloca instructions out of the entry block.
6663  if (isa<AllocaInst>(I) && I->getParent() ==
6664        &DestBlock->getParent()->getEntryBlock())
6665    return false;
6666
6667  // We can only sink load instructions if there is nothing between the load and
6668  // the end of block that could change the value.
6669  if (I->mayReadFromMemory()) {
6670    for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
6671         Scan != E; ++Scan)
6672      if (Scan->mayWriteToMemory())
6673        return false;
6674  }
6675
6676  BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
6677
6678  I->moveBefore(InsertPos);
6679  ++NumSunkInst;
6680  return true;
6681}
6682
6683
6684/// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
6685/// all reachable code to the worklist.
6686///
6687/// This has a couple of tricks to make the code faster and more powerful.  In
6688/// particular, we constant fold and DCE instructions as we go, to avoid adding
6689/// them to the worklist (this significantly speeds up instcombine on code where
6690/// many instructions are dead or constant).  Additionally, if we find a branch
6691/// whose condition is a known constant, we only visit the reachable successors.
6692///
6693static bool AddReachableCodeToWorklist(BasicBlock *BB,
6694                                       SmallPtrSet<BasicBlock*, 64> &Visited,
6695                                       InstCombiner &IC,
6696                                       const TargetData *TD) {
6697  bool MadeIRChange = false;
6698  SmallVector<BasicBlock*, 256> Worklist;
6699  Worklist.push_back(BB);
6700
6701  std::vector<Instruction*> InstrsForInstCombineWorklist;
6702  InstrsForInstCombineWorklist.reserve(128);
6703
6704  SmallPtrSet<ConstantExpr*, 64> FoldedConstants;
6705
6706  while (!Worklist.empty()) {
6707    BB = Worklist.back();
6708    Worklist.pop_back();
6709
6710    // We have now visited this block!  If we've already been here, ignore it.
6711    if (!Visited.insert(BB)) continue;
6712
6713    for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
6714      Instruction *Inst = BBI++;
6715
6716      // DCE instruction if trivially dead.
6717      if (isInstructionTriviallyDead(Inst)) {
6718        ++NumDeadInst;
6719        DEBUG(errs() << "IC: DCE: " << *Inst << '\n');
6720        Inst->eraseFromParent();
6721        continue;
6722      }
6723
6724      // ConstantProp instruction if trivially constant.
6725      if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0)))
6726        if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
6727          DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
6728                       << *Inst << '\n');
6729          Inst->replaceAllUsesWith(C);
6730          ++NumConstProp;
6731          Inst->eraseFromParent();
6732          continue;
6733        }
6734
6735
6736
6737      if (TD) {
6738        // See if we can constant fold its operands.
6739        for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end();
6740             i != e; ++i) {
6741          ConstantExpr *CE = dyn_cast<ConstantExpr>(i);
6742          if (CE == 0) continue;
6743
6744          // If we already folded this constant, don't try again.
6745          if (!FoldedConstants.insert(CE))
6746            continue;
6747
6748          Constant *NewC = ConstantFoldConstantExpression(CE, TD);
6749          if (NewC && NewC != CE) {
6750            *i = NewC;
6751            MadeIRChange = true;
6752          }
6753        }
6754      }
6755
6756
6757      InstrsForInstCombineWorklist.push_back(Inst);
6758    }
6759
6760    // Recursively visit successors.  If this is a branch or switch on a
6761    // constant, only visit the reachable successor.
6762    TerminatorInst *TI = BB->getTerminator();
6763    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
6764      if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
6765        bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
6766        BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
6767        Worklist.push_back(ReachableBB);
6768        continue;
6769      }
6770    } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
6771      if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
6772        // See if this is an explicit destination.
6773        for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
6774          if (SI->getCaseValue(i) == Cond) {
6775            BasicBlock *ReachableBB = SI->getSuccessor(i);
6776            Worklist.push_back(ReachableBB);
6777            continue;
6778          }
6779
6780        // Otherwise it is the default destination.
6781        Worklist.push_back(SI->getSuccessor(0));
6782        continue;
6783      }
6784    }
6785
6786    for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
6787      Worklist.push_back(TI->getSuccessor(i));
6788  }
6789
6790  // Once we've found all of the instructions to add to instcombine's worklist,
6791  // add them in reverse order.  This way instcombine will visit from the top
6792  // of the function down.  This jives well with the way that it adds all uses
6793  // of instructions to the worklist after doing a transformation, thus avoiding
6794  // some N^2 behavior in pathological cases.
6795  IC.Worklist.AddInitialGroup(&InstrsForInstCombineWorklist[0],
6796                              InstrsForInstCombineWorklist.size());
6797
6798  return MadeIRChange;
6799}
6800
6801bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
6802  MadeIRChange = false;
6803
6804  DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
6805        << F.getNameStr() << "\n");
6806
6807  {
6808    // Do a depth-first traversal of the function, populate the worklist with
6809    // the reachable instructions.  Ignore blocks that are not reachable.  Keep
6810    // track of which blocks we visit.
6811    SmallPtrSet<BasicBlock*, 64> Visited;
6812    MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
6813
6814    // Do a quick scan over the function.  If we find any blocks that are
6815    // unreachable, remove any instructions inside of them.  This prevents
6816    // the instcombine code from having to deal with some bad special cases.
6817    for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
6818      if (!Visited.count(BB)) {
6819        Instruction *Term = BB->getTerminator();
6820        while (Term != BB->begin()) {   // Remove instrs bottom-up
6821          BasicBlock::iterator I = Term; --I;
6822
6823          DEBUG(errs() << "IC: DCE: " << *I << '\n');
6824          // A debug intrinsic shouldn't force another iteration if we weren't
6825          // going to do one without it.
6826          if (!isa<DbgInfoIntrinsic>(I)) {
6827            ++NumDeadInst;
6828            MadeIRChange = true;
6829          }
6830
6831          // If I is not void type then replaceAllUsesWith undef.
6832          // This allows ValueHandlers and custom metadata to adjust itself.
6833          if (!I->getType()->isVoidTy())
6834            I->replaceAllUsesWith(UndefValue::get(I->getType()));
6835          I->eraseFromParent();
6836        }
6837      }
6838  }
6839
6840  while (!Worklist.isEmpty()) {
6841    Instruction *I = Worklist.RemoveOne();
6842    if (I == 0) continue;  // skip null values.
6843
6844    // Check to see if we can DCE the instruction.
6845    if (isInstructionTriviallyDead(I)) {
6846      DEBUG(errs() << "IC: DCE: " << *I << '\n');
6847      EraseInstFromFunction(*I);
6848      ++NumDeadInst;
6849      MadeIRChange = true;
6850      continue;
6851    }
6852
6853    // Instruction isn't dead, see if we can constant propagate it.
6854    if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
6855      if (Constant *C = ConstantFoldInstruction(I, TD)) {
6856        DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
6857
6858        // Add operands to the worklist.
6859        ReplaceInstUsesWith(*I, C);
6860        ++NumConstProp;
6861        EraseInstFromFunction(*I);
6862        MadeIRChange = true;
6863        continue;
6864      }
6865
6866    // See if we can trivially sink this instruction to a successor basic block.
6867    if (I->hasOneUse()) {
6868      BasicBlock *BB = I->getParent();
6869      Instruction *UserInst = cast<Instruction>(I->use_back());
6870      BasicBlock *UserParent;
6871
6872      // Get the block the use occurs in.
6873      if (PHINode *PN = dyn_cast<PHINode>(UserInst))
6874        UserParent = PN->getIncomingBlock(I->use_begin().getUse());
6875      else
6876        UserParent = UserInst->getParent();
6877
6878      if (UserParent != BB) {
6879        bool UserIsSuccessor = false;
6880        // See if the user is one of our successors.
6881        for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
6882          if (*SI == UserParent) {
6883            UserIsSuccessor = true;
6884            break;
6885          }
6886
6887        // If the user is one of our immediate successors, and if that successor
6888        // only has us as a predecessors (we'd have to split the critical edge
6889        // otherwise), we can keep going.
6890        if (UserIsSuccessor && UserParent->getSinglePredecessor())
6891          // Okay, the CFG is simple enough, try to sink this instruction.
6892          MadeIRChange |= TryToSinkInstruction(I, UserParent);
6893      }
6894    }
6895
6896    // Now that we have an instruction, try combining it to simplify it.
6897    Builder->SetInsertPoint(I->getParent(), I);
6898
6899#ifndef NDEBUG
6900    std::string OrigI;
6901#endif
6902    DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
6903    DEBUG(errs() << "IC: Visiting: " << OrigI << '\n');
6904
6905    if (Instruction *Result = visit(*I)) {
6906      ++NumCombined;
6907      // Should we replace the old instruction with a new one?
6908      if (Result != I) {
6909        DEBUG(errs() << "IC: Old = " << *I << '\n'
6910                     << "    New = " << *Result << '\n');
6911
6912        // Everything uses the new instruction now.
6913        I->replaceAllUsesWith(Result);
6914
6915        // Push the new instruction and any users onto the worklist.
6916        Worklist.Add(Result);
6917        Worklist.AddUsersToWorkList(*Result);
6918
6919        // Move the name to the new instruction first.
6920        Result->takeName(I);
6921
6922        // Insert the new instruction into the basic block...
6923        BasicBlock *InstParent = I->getParent();
6924        BasicBlock::iterator InsertPos = I;
6925
6926        if (!isa<PHINode>(Result))        // If combining a PHI, don't insert
6927          while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
6928            ++InsertPos;
6929
6930        InstParent->getInstList().insert(InsertPos, Result);
6931
6932        EraseInstFromFunction(*I);
6933      } else {
6934#ifndef NDEBUG
6935        DEBUG(errs() << "IC: Mod = " << OrigI << '\n'
6936                     << "    New = " << *I << '\n');
6937#endif
6938
6939        // If the instruction was modified, it's possible that it is now dead.
6940        // if so, remove it.
6941        if (isInstructionTriviallyDead(I)) {
6942          EraseInstFromFunction(*I);
6943        } else {
6944          Worklist.Add(I);
6945          Worklist.AddUsersToWorkList(*I);
6946        }
6947      }
6948      MadeIRChange = true;
6949    }
6950  }
6951
6952  Worklist.Zap();
6953  return MadeIRChange;
6954}
6955
6956
6957bool InstCombiner::runOnFunction(Function &F) {
6958  MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
6959  TD = getAnalysisIfAvailable<TargetData>();
6960
6961
6962  /// Builder - This is an IRBuilder that automatically inserts new
6963  /// instructions into the worklist when they are created.
6964  IRBuilder<true, TargetFolder, InstCombineIRInserter>
6965    TheBuilder(F.getContext(), TargetFolder(TD),
6966               InstCombineIRInserter(Worklist));
6967  Builder = &TheBuilder;
6968
6969  bool EverMadeChange = false;
6970
6971  // Iterate while there is work to do.
6972  unsigned Iteration = 0;
6973  while (DoOneIteration(F, Iteration++))
6974    EverMadeChange = true;
6975
6976  Builder = 0;
6977  return EverMadeChange;
6978}
6979
6980FunctionPass *llvm::createInstructionCombiningPass() {
6981  return new InstCombiner();
6982}
6983