[PM] Rename InstCombine.h to InstCombineInternal.h in preparation for
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineSimplifyDemanded.cpp
1 //===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains logic for simplifying instructions based on information
11 // about how they are used.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "InstCombineInternal.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PatternMatch.h"
19
20 using namespace llvm;
21 using namespace llvm::PatternMatch;
22
23 #define DEBUG_TYPE "instcombine"
24
25 /// ShrinkDemandedConstant - Check to see if the specified operand of the
26 /// specified instruction is a constant integer.  If so, check to see if there
27 /// are any bits set in the constant that are not demanded.  If so, shrink the
28 /// constant and return true.
29 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
30                                    APInt Demanded) {
31   assert(I && "No instruction?");
32   assert(OpNo < I->getNumOperands() && "Operand index too large");
33
34   // If the operand is not a constant integer, nothing to do.
35   ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
36   if (!OpC) return false;
37
38   // If there are no bits set that aren't demanded, nothing to do.
39   Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
40   if ((~Demanded & OpC->getValue()) == 0)
41     return false;
42
43   // This instruction is producing bits that are not demanded. Shrink the RHS.
44   Demanded &= OpC->getValue();
45   I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
46
47   // If either 'nsw' or 'nuw' is set and the constant is negative,
48   // removing *any* bits from the constant could make overflow occur.
49   // Remove 'nsw' and 'nuw' from the instruction in this case.
50   if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I)) {
51     assert(OBO->getOpcode() == Instruction::Add);
52     if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap()) {
53       if (OpC->getValue().isNegative()) {
54         cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
55         cast<BinaryOperator>(OBO)->setHasNoUnsignedWrap(false);
56       }
57     }
58   }
59
60   return true;
61 }
62
63
64
65 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
66 /// SimplifyDemandedBits knows about.  See if the instruction has any
67 /// properties that allow us to simplify its operands.
68 bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
69   unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
70   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
71   APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
72
73   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
74                                      KnownZero, KnownOne, 0, &Inst);
75   if (!V) return false;
76   if (V == &Inst) return true;
77   ReplaceInstUsesWith(Inst, V);
78   return true;
79 }
80
81 /// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
82 /// specified instruction operand if possible, updating it in place.  It returns
83 /// true if it made any change and false otherwise.
84 bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
85                                         APInt &KnownZero, APInt &KnownOne,
86                                         unsigned Depth) {
87   Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
88                                           KnownZero, KnownOne, Depth,
89                                           dyn_cast<Instruction>(U.getUser()));
90   if (!NewVal) return false;
91   U = NewVal;
92   return true;
93 }
94
95
96 /// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
97 /// value based on the demanded bits.  When this function is called, it is known
98 /// that only the bits set in DemandedMask of the result of V are ever used
99 /// downstream. Consequently, depending on the mask and V, it may be possible
100 /// to replace V with a constant or one of its operands. In such cases, this
101 /// function does the replacement and returns true. In all other cases, it
102 /// returns false after analyzing the expression and setting KnownOne and known
103 /// to be one in the expression.  KnownZero contains all the bits that are known
104 /// to be zero in the expression. These are provided to potentially allow the
105 /// caller (which might recursively be SimplifyDemandedBits itself) to simplify
106 /// the expression. KnownOne and KnownZero always follow the invariant that
107 /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
108 /// the bits in KnownOne and KnownZero may only be accurate for those bits set
109 /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
110 /// and KnownOne must all be the same.
111 ///
112 /// This returns null if it did not change anything and it permits no
113 /// simplification.  This returns V itself if it did some simplification of V's
114 /// operands based on the information about what bits are demanded. This returns
115 /// some other non-null value if it found out that V is equal to another value
116 /// in the context where the specified bits are demanded, but not for all users.
117 Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
118                                              APInt &KnownZero, APInt &KnownOne,
119                                              unsigned Depth,
120                                              Instruction *CxtI) {
121   assert(V != nullptr && "Null pointer of Value???");
122   assert(Depth <= 6 && "Limit Search Depth");
123   uint32_t BitWidth = DemandedMask.getBitWidth();
124   Type *VTy = V->getType();
125   assert((DL || !VTy->isPointerTy()) &&
126          "SimplifyDemandedBits needs to know bit widths!");
127   assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
128          (!VTy->isIntOrIntVectorTy() ||
129           VTy->getScalarSizeInBits() == BitWidth) &&
130          KnownZero.getBitWidth() == BitWidth &&
131          KnownOne.getBitWidth() == BitWidth &&
132          "Value *V, DemandedMask, KnownZero and KnownOne "
133          "must have same BitWidth");
134   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
135     // We know all of the bits for a constant!
136     KnownOne = CI->getValue() & DemandedMask;
137     KnownZero = ~KnownOne & DemandedMask;
138     return nullptr;
139   }
140   if (isa<ConstantPointerNull>(V)) {
141     // We know all of the bits for a constant!
142     KnownOne.clearAllBits();
143     KnownZero = DemandedMask;
144     return nullptr;
145   }
146
147   KnownZero.clearAllBits();
148   KnownOne.clearAllBits();
149   if (DemandedMask == 0) {   // Not demanding any bits from V.
150     if (isa<UndefValue>(V))
151       return nullptr;
152     return UndefValue::get(VTy);
153   }
154
155   if (Depth == 6)        // Limit search depth.
156     return nullptr;
157
158   APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
159   APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
160
161   Instruction *I = dyn_cast<Instruction>(V);
162   if (!I) {
163     computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
164     return nullptr;        // Only analyze instructions.
165   }
166
167   // If there are multiple uses of this value and we aren't at the root, then
168   // we can't do any simplifications of the operands, because DemandedMask
169   // only reflects the bits demanded by *one* of the users.
170   if (Depth != 0 && !I->hasOneUse()) {
171     // Despite the fact that we can't simplify this instruction in all User's
172     // context, we can at least compute the knownzero/knownone bits, and we can
173     // do simplifications that apply to *just* the one user if we know that
174     // this instruction has a simpler value in that context.
175     if (I->getOpcode() == Instruction::And) {
176       // If either the LHS or the RHS are Zero, the result is zero.
177       computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
178                        CxtI);
179       computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
180                        CxtI);
181
182       // If all of the demanded bits are known 1 on one side, return the other.
183       // These bits cannot contribute to the result of the 'and' in this
184       // context.
185       if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
186           (DemandedMask & ~LHSKnownZero))
187         return I->getOperand(0);
188       if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
189           (DemandedMask & ~RHSKnownZero))
190         return I->getOperand(1);
191
192       // If all of the demanded bits in the inputs are known zeros, return zero.
193       if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
194         return Constant::getNullValue(VTy);
195
196     } else if (I->getOpcode() == Instruction::Or) {
197       // We can simplify (X|Y) -> X or Y in the user's context if we know that
198       // only bits from X or Y are demanded.
199
200       // If either the LHS or the RHS are One, the result is One.
201       computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
202                        CxtI);
203       computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
204                        CxtI);
205
206       // If all of the demanded bits are known zero on one side, return the
207       // other.  These bits cannot contribute to the result of the 'or' in this
208       // context.
209       if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
210           (DemandedMask & ~LHSKnownOne))
211         return I->getOperand(0);
212       if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
213           (DemandedMask & ~RHSKnownOne))
214         return I->getOperand(1);
215
216       // If all of the potentially set bits on one side are known to be set on
217       // the other side, just use the 'other' side.
218       if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
219           (DemandedMask & (~RHSKnownZero)))
220         return I->getOperand(0);
221       if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
222           (DemandedMask & (~LHSKnownZero)))
223         return I->getOperand(1);
224     } else if (I->getOpcode() == Instruction::Xor) {
225       // We can simplify (X^Y) -> X or Y in the user's context if we know that
226       // only bits from X or Y are demanded.
227
228       computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
229                        CxtI);
230       computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
231                        CxtI);
232
233       // If all of the demanded bits are known zero on one side, return the
234       // other.
235       if ((DemandedMask & RHSKnownZero) == DemandedMask)
236         return I->getOperand(0);
237       if ((DemandedMask & LHSKnownZero) == DemandedMask)
238         return I->getOperand(1);
239     }
240
241     // Compute the KnownZero/KnownOne bits to simplify things downstream.
242     computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
243     return nullptr;
244   }
245
246   // If this is the root being simplified, allow it to have multiple uses,
247   // just set the DemandedMask to all bits so that we can try to simplify the
248   // operands.  This allows visitTruncInst (for example) to simplify the
249   // operand of a trunc without duplicating all the logic below.
250   if (Depth == 0 && !V->hasOneUse())
251     DemandedMask = APInt::getAllOnesValue(BitWidth);
252
253   switch (I->getOpcode()) {
254   default:
255     computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
256     break;
257   case Instruction::And:
258     // If either the LHS or the RHS are Zero, the result is zero.
259     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
260                              RHSKnownZero, RHSKnownOne, Depth+1) ||
261         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
262                              LHSKnownZero, LHSKnownOne, Depth+1))
263       return I;
264     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
265     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
266
267     // If the client is only demanding bits that we know, return the known
268     // constant.
269     if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
270                          (RHSKnownOne & LHSKnownOne))) == DemandedMask)
271       return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
272
273     // If all of the demanded bits are known 1 on one side, return the other.
274     // These bits cannot contribute to the result of the 'and'.
275     if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
276         (DemandedMask & ~LHSKnownZero))
277       return I->getOperand(0);
278     if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
279         (DemandedMask & ~RHSKnownZero))
280       return I->getOperand(1);
281
282     // If all of the demanded bits in the inputs are known zeros, return zero.
283     if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
284       return Constant::getNullValue(VTy);
285
286     // If the RHS is a constant, see if we can simplify it.
287     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
288       return I;
289
290     // Output known-1 bits are only known if set in both the LHS & RHS.
291     KnownOne = RHSKnownOne & LHSKnownOne;
292     // Output known-0 are known to be clear if zero in either the LHS | RHS.
293     KnownZero = RHSKnownZero | LHSKnownZero;
294     break;
295   case Instruction::Or:
296     // If either the LHS or the RHS are One, the result is One.
297     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
298                              RHSKnownZero, RHSKnownOne, Depth+1) ||
299         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
300                              LHSKnownZero, LHSKnownOne, Depth+1))
301       return I;
302     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
303     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
304
305     // If the client is only demanding bits that we know, return the known
306     // constant.
307     if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
308                          (RHSKnownOne | LHSKnownOne))) == DemandedMask)
309       return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
310
311     // If all of the demanded bits are known zero on one side, return the other.
312     // These bits cannot contribute to the result of the 'or'.
313     if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
314         (DemandedMask & ~LHSKnownOne))
315       return I->getOperand(0);
316     if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
317         (DemandedMask & ~RHSKnownOne))
318       return I->getOperand(1);
319
320     // If all of the potentially set bits on one side are known to be set on
321     // the other side, just use the 'other' side.
322     if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
323         (DemandedMask & (~RHSKnownZero)))
324       return I->getOperand(0);
325     if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
326         (DemandedMask & (~LHSKnownZero)))
327       return I->getOperand(1);
328
329     // If the RHS is a constant, see if we can simplify it.
330     if (ShrinkDemandedConstant(I, 1, DemandedMask))
331       return I;
332
333     // Output known-0 bits are only known if clear in both the LHS & RHS.
334     KnownZero = RHSKnownZero & LHSKnownZero;
335     // Output known-1 are known to be set if set in either the LHS | RHS.
336     KnownOne = RHSKnownOne | LHSKnownOne;
337     break;
338   case Instruction::Xor: {
339     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
340                              RHSKnownZero, RHSKnownOne, Depth+1) ||
341         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
342                              LHSKnownZero, LHSKnownOne, Depth+1))
343       return I;
344     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
345     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
346
347     // Output known-0 bits are known if clear or set in both the LHS & RHS.
348     APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
349                        (RHSKnownOne & LHSKnownOne);
350     // Output known-1 are known to be set if set in only one of the LHS, RHS.
351     APInt IKnownOne =  (RHSKnownZero & LHSKnownOne) |
352                        (RHSKnownOne & LHSKnownZero);
353
354     // If the client is only demanding bits that we know, return the known
355     // constant.
356     if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
357       return Constant::getIntegerValue(VTy, IKnownOne);
358
359     // If all of the demanded bits are known zero on one side, return the other.
360     // These bits cannot contribute to the result of the 'xor'.
361     if ((DemandedMask & RHSKnownZero) == DemandedMask)
362       return I->getOperand(0);
363     if ((DemandedMask & LHSKnownZero) == DemandedMask)
364       return I->getOperand(1);
365
366     // If all of the demanded bits are known to be zero on one side or the
367     // other, turn this into an *inclusive* or.
368     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
369     if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
370       Instruction *Or =
371         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
372                                  I->getName());
373       return InsertNewInstWith(Or, *I);
374     }
375
376     // If all of the demanded bits on one side are known, and all of the set
377     // bits on that side are also known to be set on the other side, turn this
378     // into an AND, as we know the bits will be cleared.
379     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
380     if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
381       // all known
382       if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
383         Constant *AndC = Constant::getIntegerValue(VTy,
384                                                    ~RHSKnownOne & DemandedMask);
385         Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
386         return InsertNewInstWith(And, *I);
387       }
388     }
389
390     // If the RHS is a constant, see if we can simplify it.
391     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
392     if (ShrinkDemandedConstant(I, 1, DemandedMask))
393       return I;
394
395     // If our LHS is an 'and' and if it has one use, and if any of the bits we
396     // are flipping are known to be set, then the xor is just resetting those
397     // bits to zero.  We can just knock out bits from the 'and' and the 'xor',
398     // simplifying both of them.
399     if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
400       if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
401           isa<ConstantInt>(I->getOperand(1)) &&
402           isa<ConstantInt>(LHSInst->getOperand(1)) &&
403           (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
404         ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
405         ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
406         APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
407
408         Constant *AndC =
409           ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
410         Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
411         InsertNewInstWith(NewAnd, *I);
412
413         Constant *XorC =
414           ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
415         Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
416         return InsertNewInstWith(NewXor, *I);
417       }
418
419     // Output known-0 bits are known if clear or set in both the LHS & RHS.
420     KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
421     // Output known-1 are known to be set if set in only one of the LHS, RHS.
422     KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
423     break;
424   }
425   case Instruction::Select:
426     if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
427                              RHSKnownZero, RHSKnownOne, Depth+1) ||
428         SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
429                              LHSKnownZero, LHSKnownOne, Depth+1))
430       return I;
431     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
432     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
433
434     // If the operands are constants, see if we can simplify them.
435     if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
436         ShrinkDemandedConstant(I, 2, DemandedMask))
437       return I;
438
439     // Only known if known in both the LHS and RHS.
440     KnownOne = RHSKnownOne & LHSKnownOne;
441     KnownZero = RHSKnownZero & LHSKnownZero;
442     break;
443   case Instruction::Trunc: {
444     unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
445     DemandedMask = DemandedMask.zext(truncBf);
446     KnownZero = KnownZero.zext(truncBf);
447     KnownOne = KnownOne.zext(truncBf);
448     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
449                              KnownZero, KnownOne, Depth+1))
450       return I;
451     DemandedMask = DemandedMask.trunc(BitWidth);
452     KnownZero = KnownZero.trunc(BitWidth);
453     KnownOne = KnownOne.trunc(BitWidth);
454     assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
455     break;
456   }
457   case Instruction::BitCast:
458     if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
459       return nullptr;  // vector->int or fp->int?
460
461     if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
462       if (VectorType *SrcVTy =
463             dyn_cast<VectorType>(I->getOperand(0)->getType())) {
464         if (DstVTy->getNumElements() != SrcVTy->getNumElements())
465           // Don't touch a bitcast between vectors of different element counts.
466           return nullptr;
467       } else
468         // Don't touch a scalar-to-vector bitcast.
469         return nullptr;
470     } else if (I->getOperand(0)->getType()->isVectorTy())
471       // Don't touch a vector-to-scalar bitcast.
472       return nullptr;
473
474     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
475                              KnownZero, KnownOne, Depth+1))
476       return I;
477     assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
478     break;
479   case Instruction::ZExt: {
480     // Compute the bits in the result that are not present in the input.
481     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
482
483     DemandedMask = DemandedMask.trunc(SrcBitWidth);
484     KnownZero = KnownZero.trunc(SrcBitWidth);
485     KnownOne = KnownOne.trunc(SrcBitWidth);
486     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
487                              KnownZero, KnownOne, Depth+1))
488       return I;
489     DemandedMask = DemandedMask.zext(BitWidth);
490     KnownZero = KnownZero.zext(BitWidth);
491     KnownOne = KnownOne.zext(BitWidth);
492     assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
493     // The top bits are known to be zero.
494     KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
495     break;
496   }
497   case Instruction::SExt: {
498     // Compute the bits in the result that are not present in the input.
499     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
500
501     APInt InputDemandedBits = DemandedMask &
502                               APInt::getLowBitsSet(BitWidth, SrcBitWidth);
503
504     APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
505     // If any of the sign extended bits are demanded, we know that the sign
506     // bit is demanded.
507     if ((NewBits & DemandedMask) != 0)
508       InputDemandedBits.setBit(SrcBitWidth-1);
509
510     InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
511     KnownZero = KnownZero.trunc(SrcBitWidth);
512     KnownOne = KnownOne.trunc(SrcBitWidth);
513     if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
514                              KnownZero, KnownOne, Depth+1))
515       return I;
516     InputDemandedBits = InputDemandedBits.zext(BitWidth);
517     KnownZero = KnownZero.zext(BitWidth);
518     KnownOne = KnownOne.zext(BitWidth);
519     assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
520
521     // If the sign bit of the input is known set or clear, then we know the
522     // top bits of the result.
523
524     // If the input sign bit is known zero, or if the NewBits are not demanded
525     // convert this into a zero extension.
526     if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
527       // Convert to ZExt cast
528       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
529       return InsertNewInstWith(NewCast, *I);
530     } else if (KnownOne[SrcBitWidth-1]) {    // Input sign bit known set
531       KnownOne |= NewBits;
532     }
533     break;
534   }
535   case Instruction::Add: {
536     // Figure out what the input bits are.  If the top bits of the and result
537     // are not demanded, then the add doesn't demand them from its input
538     // either.
539     unsigned NLZ = DemandedMask.countLeadingZeros();
540
541     // If there is a constant on the RHS, there are a variety of xformations
542     // we can do.
543     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
544       // If null, this should be simplified elsewhere.  Some of the xforms here
545       // won't work if the RHS is zero.
546       if (RHS->isZero())
547         break;
548
549       // If the top bit of the output is demanded, demand everything from the
550       // input.  Otherwise, we demand all the input bits except NLZ top bits.
551       APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
552
553       // Find information about known zero/one bits in the input.
554       if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
555                                LHSKnownZero, LHSKnownOne, Depth+1))
556         return I;
557
558       // If the RHS of the add has bits set that can't affect the input, reduce
559       // the constant.
560       if (ShrinkDemandedConstant(I, 1, InDemandedBits))
561         return I;
562
563       // Avoid excess work.
564       if (LHSKnownZero == 0 && LHSKnownOne == 0)
565         break;
566
567       // Turn it into OR if input bits are zero.
568       if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
569         Instruction *Or =
570           BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
571                                    I->getName());
572         return InsertNewInstWith(Or, *I);
573       }
574
575       // We can say something about the output known-zero and known-one bits,
576       // depending on potential carries from the input constant and the
577       // unknowns.  For example if the LHS is known to have at most the 0x0F0F0
578       // bits set and the RHS constant is 0x01001, then we know we have a known
579       // one mask of 0x00001 and a known zero mask of 0xE0F0E.
580
581       // To compute this, we first compute the potential carry bits.  These are
582       // the bits which may be modified.  I'm not aware of a better way to do
583       // this scan.
584       const APInt &RHSVal = RHS->getValue();
585       APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
586
587       // Now that we know which bits have carries, compute the known-1/0 sets.
588
589       // Bits are known one if they are known zero in one operand and one in the
590       // other, and there is no input carry.
591       KnownOne = ((LHSKnownZero & RHSVal) |
592                   (LHSKnownOne & ~RHSVal)) & ~CarryBits;
593
594       // Bits are known zero if they are known zero in both operands and there
595       // is no input carry.
596       KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
597     } else {
598       // If the high-bits of this ADD are not demanded, then it does not demand
599       // the high bits of its LHS or RHS.
600       if (DemandedMask[BitWidth-1] == 0) {
601         // Right fill the mask of bits for this ADD to demand the most
602         // significant bit and all those below it.
603         APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
604         if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
605                                  LHSKnownZero, LHSKnownOne, Depth+1) ||
606             SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
607                                  LHSKnownZero, LHSKnownOne, Depth+1))
608           return I;
609       }
610     }
611     break;
612   }
613   case Instruction::Sub:
614     // If the high-bits of this SUB are not demanded, then it does not demand
615     // the high bits of its LHS or RHS.
616     if (DemandedMask[BitWidth-1] == 0) {
617       // Right fill the mask of bits for this SUB to demand the most
618       // significant bit and all those below it.
619       uint32_t NLZ = DemandedMask.countLeadingZeros();
620       APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
621       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
622                                LHSKnownZero, LHSKnownOne, Depth+1) ||
623           SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
624                                LHSKnownZero, LHSKnownOne, Depth+1))
625         return I;
626     }
627
628     // Otherwise just hand the sub off to computeKnownBits to fill in
629     // the known zeros and ones.
630     computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
631
632     // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
633     // zero.
634     if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
635       APInt I0 = C0->getValue();
636       if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
637         Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
638         return InsertNewInstWith(Xor, *I);
639       }
640     }
641     break;
642   case Instruction::Shl:
643     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
644       {
645         Value *VarX; ConstantInt *C1;
646         if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
647           Instruction *Shr = cast<Instruction>(I->getOperand(0));
648           Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
649                                                 KnownZero, KnownOne);
650           if (R)
651             return R;
652         }
653       }
654
655       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
656       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
657
658       // If the shift is NUW/NSW, then it does demand the high bits.
659       ShlOperator *IOp = cast<ShlOperator>(I);
660       if (IOp->hasNoSignedWrap())
661         DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
662       else if (IOp->hasNoUnsignedWrap())
663         DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
664
665       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
666                                KnownZero, KnownOne, Depth+1))
667         return I;
668       assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
669       KnownZero <<= ShiftAmt;
670       KnownOne  <<= ShiftAmt;
671       // low bits known zero.
672       if (ShiftAmt)
673         KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
674     }
675     break;
676   case Instruction::LShr:
677     // For a logical shift right
678     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
679       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
680
681       // Unsigned shift right.
682       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
683
684       // If the shift is exact, then it does demand the low bits (and knows that
685       // they are zero).
686       if (cast<LShrOperator>(I)->isExact())
687         DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
688
689       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
690                                KnownZero, KnownOne, Depth+1))
691         return I;
692       assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
693       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
694       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
695       if (ShiftAmt) {
696         // Compute the new bits that are at the top now.
697         APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
698         KnownZero |= HighBits;  // high bits known zero.
699       }
700     }
701     break;
702   case Instruction::AShr:
703     // If this is an arithmetic shift right and only the low-bit is set, we can
704     // always convert this into a logical shr, even if the shift amount is
705     // variable.  The low bit of the shift cannot be an input sign bit unless
706     // the shift amount is >= the size of the datatype, which is undefined.
707     if (DemandedMask == 1) {
708       // Perform the logical shift right.
709       Instruction *NewVal = BinaryOperator::CreateLShr(
710                         I->getOperand(0), I->getOperand(1), I->getName());
711       return InsertNewInstWith(NewVal, *I);
712     }
713
714     // If the sign bit is the only bit demanded by this ashr, then there is no
715     // need to do it, the shift doesn't change the high bit.
716     if (DemandedMask.isSignBit())
717       return I->getOperand(0);
718
719     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
720       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
721
722       // Signed shift right.
723       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
724       // If any of the "high bits" are demanded, we should set the sign bit as
725       // demanded.
726       if (DemandedMask.countLeadingZeros() <= ShiftAmt)
727         DemandedMaskIn.setBit(BitWidth-1);
728
729       // If the shift is exact, then it does demand the low bits (and knows that
730       // they are zero).
731       if (cast<AShrOperator>(I)->isExact())
732         DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
733
734       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
735                                KnownZero, KnownOne, Depth+1))
736         return I;
737       assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
738       // Compute the new bits that are at the top now.
739       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
740       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
741       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
742
743       // Handle the sign bits.
744       APInt SignBit(APInt::getSignBit(BitWidth));
745       // Adjust to where it is now in the mask.
746       SignBit = APIntOps::lshr(SignBit, ShiftAmt);
747
748       // If the input sign bit is known to be zero, or if none of the top bits
749       // are demanded, turn this into an unsigned shift right.
750       if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
751           (HighBits & ~DemandedMask) == HighBits) {
752         // Perform the logical shift right.
753         BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
754                                                             SA, I->getName());
755         NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
756         return InsertNewInstWith(NewVal, *I);
757       } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
758         KnownOne |= HighBits;
759       }
760     }
761     break;
762   case Instruction::SRem:
763     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
764       // X % -1 demands all the bits because we don't want to introduce
765       // INT_MIN % -1 (== undef) by accident.
766       if (Rem->isAllOnesValue())
767         break;
768       APInt RA = Rem->getValue().abs();
769       if (RA.isPowerOf2()) {
770         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
771           return I->getOperand(0);
772
773         APInt LowBits = RA - 1;
774         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
775         if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
776                                  LHSKnownZero, LHSKnownOne, Depth+1))
777           return I;
778
779         // The low bits of LHS are unchanged by the srem.
780         KnownZero = LHSKnownZero & LowBits;
781         KnownOne = LHSKnownOne & LowBits;
782
783         // If LHS is non-negative or has all low bits zero, then the upper bits
784         // are all zero.
785         if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
786           KnownZero |= ~LowBits;
787
788         // If LHS is negative and not all low bits are zero, then the upper bits
789         // are all one.
790         if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
791           KnownOne |= ~LowBits;
792
793         assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
794       }
795     }
796
797     // The sign bit is the LHS's sign bit, except when the result of the
798     // remainder is zero.
799     if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
800       APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
801       computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
802                        CxtI);
803       // If it's known zero, our sign bit is also zero.
804       if (LHSKnownZero.isNegative())
805         KnownZero.setBit(KnownZero.getBitWidth() - 1);
806     }
807     break;
808   case Instruction::URem: {
809     APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
810     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
811     if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
812                              KnownZero2, KnownOne2, Depth+1) ||
813         SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
814                              KnownZero2, KnownOne2, Depth+1))
815       return I;
816
817     unsigned Leaders = KnownZero2.countLeadingOnes();
818     Leaders = std::max(Leaders,
819                        KnownZero2.countLeadingOnes());
820     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
821     break;
822   }
823   case Instruction::Call:
824     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
825       switch (II->getIntrinsicID()) {
826       default: break;
827       case Intrinsic::bswap: {
828         // If the only bits demanded come from one byte of the bswap result,
829         // just shift the input byte into position to eliminate the bswap.
830         unsigned NLZ = DemandedMask.countLeadingZeros();
831         unsigned NTZ = DemandedMask.countTrailingZeros();
832
833         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
834         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
835         // have 14 leading zeros, round to 8.
836         NLZ &= ~7;
837         NTZ &= ~7;
838         // If we need exactly one byte, we can do this transformation.
839         if (BitWidth-NLZ-NTZ == 8) {
840           unsigned ResultBit = NTZ;
841           unsigned InputBit = BitWidth-NTZ-8;
842
843           // Replace this with either a left or right shift to get the byte into
844           // the right place.
845           Instruction *NewVal;
846           if (InputBit > ResultBit)
847             NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
848                     ConstantInt::get(I->getType(), InputBit-ResultBit));
849           else
850             NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
851                     ConstantInt::get(I->getType(), ResultBit-InputBit));
852           NewVal->takeName(I);
853           return InsertNewInstWith(NewVal, *I);
854         }
855
856         // TODO: Could compute known zero/one bits based on the input.
857         break;
858       }
859       case Intrinsic::x86_sse42_crc32_64_64:
860         KnownZero = APInt::getHighBitsSet(64, 32);
861         return nullptr;
862       }
863     }
864     computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
865     break;
866   }
867
868   // If the client is only demanding bits that we know, return the known
869   // constant.
870   if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
871     return Constant::getIntegerValue(VTy, KnownOne);
872   return nullptr;
873 }
874
875 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify
876 /// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
877 /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
878 /// of "C2-C1".
879 ///
880 /// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
881 /// ..., bn}, without considering the specific value X is holding.
882 /// This transformation is legal iff one of following conditions is hold:
883 ///  1) All the bit in S are 0, in this case E1 == E2.
884 ///  2) We don't care those bits in S, per the input DemandedMask.
885 ///  3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
886 ///     rest bits.
887 ///
888 /// Currently we only test condition 2).
889 ///
890 /// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
891 /// not successful.
892 Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
893   Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
894
895   const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
896   const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
897   if (!ShlOp1 || !ShrOp1)
898       return nullptr; // Noop.
899
900   Value *VarX = Shr->getOperand(0);
901   Type *Ty = VarX->getType();
902   unsigned BitWidth = Ty->getIntegerBitWidth();
903   if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
904     return nullptr; // Undef.
905
906   unsigned ShlAmt = ShlOp1.getZExtValue();
907   unsigned ShrAmt = ShrOp1.getZExtValue();
908
909   KnownOne.clearAllBits();
910   KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
911   KnownZero &= DemandedMask;
912
913   APInt BitMask1(APInt::getAllOnesValue(BitWidth));
914   APInt BitMask2(APInt::getAllOnesValue(BitWidth));
915
916   bool isLshr = (Shr->getOpcode() == Instruction::LShr);
917   BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
918                       (BitMask1.ashr(ShrAmt) << ShlAmt);
919
920   if (ShrAmt <= ShlAmt) {
921     BitMask2 <<= (ShlAmt - ShrAmt);
922   } else {
923     BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
924                         BitMask2.ashr(ShrAmt - ShlAmt);
925   }
926
927   // Check if condition-2 (see the comment to this function) is satified.
928   if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
929     if (ShrAmt == ShlAmt)
930       return VarX;
931
932     if (!Shr->hasOneUse())
933       return nullptr;
934
935     BinaryOperator *New;
936     if (ShrAmt < ShlAmt) {
937       Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
938       New = BinaryOperator::CreateShl(VarX, Amt);
939       BinaryOperator *Orig = cast<BinaryOperator>(Shl);
940       New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
941       New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
942     } else {
943       Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
944       New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
945                      BinaryOperator::CreateAShr(VarX, Amt);
946       if (cast<BinaryOperator>(Shr)->isExact())
947         New->setIsExact(true);
948     }
949
950     return InsertNewInstWith(New, *Shl);
951   }
952
953   return nullptr;
954 }
955
956 /// SimplifyDemandedVectorElts - The specified value produces a vector with
957 /// any number of elements. DemandedElts contains the set of elements that are
958 /// actually used by the caller.  This method analyzes which elements of the
959 /// operand are undef and returns that information in UndefElts.
960 ///
961 /// If the information about demanded elements can be used to simplify the
962 /// operation, the operation is simplified, then the resultant value is
963 /// returned.  This returns null if no change was made.
964 Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
965                                                 APInt &UndefElts,
966                                                 unsigned Depth) {
967   unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
968   APInt EltMask(APInt::getAllOnesValue(VWidth));
969   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
970
971   if (isa<UndefValue>(V)) {
972     // If the entire vector is undefined, just return this info.
973     UndefElts = EltMask;
974     return nullptr;
975   }
976
977   if (DemandedElts == 0) { // If nothing is demanded, provide undef.
978     UndefElts = EltMask;
979     return UndefValue::get(V->getType());
980   }
981
982   UndefElts = 0;
983
984   // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
985   if (Constant *C = dyn_cast<Constant>(V)) {
986     // Check if this is identity. If so, return 0 since we are not simplifying
987     // anything.
988     if (DemandedElts.isAllOnesValue())
989       return nullptr;
990
991     Type *EltTy = cast<VectorType>(V->getType())->getElementType();
992     Constant *Undef = UndefValue::get(EltTy);
993
994     SmallVector<Constant*, 16> Elts;
995     for (unsigned i = 0; i != VWidth; ++i) {
996       if (!DemandedElts[i]) {   // If not demanded, set to undef.
997         Elts.push_back(Undef);
998         UndefElts.setBit(i);
999         continue;
1000       }
1001
1002       Constant *Elt = C->getAggregateElement(i);
1003       if (!Elt) return nullptr;
1004
1005       if (isa<UndefValue>(Elt)) {   // Already undef.
1006         Elts.push_back(Undef);
1007         UndefElts.setBit(i);
1008       } else {                               // Otherwise, defined.
1009         Elts.push_back(Elt);
1010       }
1011     }
1012
1013     // If we changed the constant, return it.
1014     Constant *NewCV = ConstantVector::get(Elts);
1015     return NewCV != C ? NewCV : nullptr;
1016   }
1017
1018   // Limit search depth.
1019   if (Depth == 10)
1020     return nullptr;
1021
1022   // If multiple users are using the root value, proceed with
1023   // simplification conservatively assuming that all elements
1024   // are needed.
1025   if (!V->hasOneUse()) {
1026     // Quit if we find multiple users of a non-root value though.
1027     // They'll be handled when it's their turn to be visited by
1028     // the main instcombine process.
1029     if (Depth != 0)
1030       // TODO: Just compute the UndefElts information recursively.
1031       return nullptr;
1032
1033     // Conservatively assume that all elements are needed.
1034     DemandedElts = EltMask;
1035   }
1036
1037   Instruction *I = dyn_cast<Instruction>(V);
1038   if (!I) return nullptr;        // Only analyze instructions.
1039
1040   bool MadeChange = false;
1041   APInt UndefElts2(VWidth, 0);
1042   Value *TmpV;
1043   switch (I->getOpcode()) {
1044   default: break;
1045
1046   case Instruction::InsertElement: {
1047     // If this is a variable index, we don't know which element it overwrites.
1048     // demand exactly the same input as we produce.
1049     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1050     if (!Idx) {
1051       // Note that we can't propagate undef elt info, because we don't know
1052       // which elt is getting updated.
1053       TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1054                                         UndefElts2, Depth+1);
1055       if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1056       break;
1057     }
1058
1059     // If this is inserting an element that isn't demanded, remove this
1060     // insertelement.
1061     unsigned IdxNo = Idx->getZExtValue();
1062     if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1063       Worklist.Add(I);
1064       return I->getOperand(0);
1065     }
1066
1067     // Otherwise, the element inserted overwrites whatever was there, so the
1068     // input demanded set is simpler than the output set.
1069     APInt DemandedElts2 = DemandedElts;
1070     DemandedElts2.clearBit(IdxNo);
1071     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1072                                       UndefElts, Depth+1);
1073     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1074
1075     // The inserted element is defined.
1076     UndefElts.clearBit(IdxNo);
1077     break;
1078   }
1079   case Instruction::ShuffleVector: {
1080     ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1081     uint64_t LHSVWidth =
1082       cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1083     APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1084     for (unsigned i = 0; i < VWidth; i++) {
1085       if (DemandedElts[i]) {
1086         unsigned MaskVal = Shuffle->getMaskValue(i);
1087         if (MaskVal != -1u) {
1088           assert(MaskVal < LHSVWidth * 2 &&
1089                  "shufflevector mask index out of range!");
1090           if (MaskVal < LHSVWidth)
1091             LeftDemanded.setBit(MaskVal);
1092           else
1093             RightDemanded.setBit(MaskVal - LHSVWidth);
1094         }
1095       }
1096     }
1097
1098     APInt UndefElts4(LHSVWidth, 0);
1099     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1100                                       UndefElts4, Depth+1);
1101     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1102
1103     APInt UndefElts3(LHSVWidth, 0);
1104     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1105                                       UndefElts3, Depth+1);
1106     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1107
1108     bool NewUndefElts = false;
1109     for (unsigned i = 0; i < VWidth; i++) {
1110       unsigned MaskVal = Shuffle->getMaskValue(i);
1111       if (MaskVal == -1u) {
1112         UndefElts.setBit(i);
1113       } else if (!DemandedElts[i]) {
1114         NewUndefElts = true;
1115         UndefElts.setBit(i);
1116       } else if (MaskVal < LHSVWidth) {
1117         if (UndefElts4[MaskVal]) {
1118           NewUndefElts = true;
1119           UndefElts.setBit(i);
1120         }
1121       } else {
1122         if (UndefElts3[MaskVal - LHSVWidth]) {
1123           NewUndefElts = true;
1124           UndefElts.setBit(i);
1125         }
1126       }
1127     }
1128
1129     if (NewUndefElts) {
1130       // Add additional discovered undefs.
1131       SmallVector<Constant*, 16> Elts;
1132       for (unsigned i = 0; i < VWidth; ++i) {
1133         if (UndefElts[i])
1134           Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1135         else
1136           Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1137                                           Shuffle->getMaskValue(i)));
1138       }
1139       I->setOperand(2, ConstantVector::get(Elts));
1140       MadeChange = true;
1141     }
1142     break;
1143   }
1144   case Instruction::Select: {
1145     APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1146     if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1147       for (unsigned i = 0; i < VWidth; i++) {
1148         if (CV->getAggregateElement(i)->isNullValue())
1149           LeftDemanded.clearBit(i);
1150         else
1151           RightDemanded.clearBit(i);
1152       }
1153     }
1154
1155     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1156                                       UndefElts, Depth+1);
1157     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1158
1159     TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1160                                       UndefElts2, Depth+1);
1161     if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
1162
1163     // Output elements are undefined if both are undefined.
1164     UndefElts &= UndefElts2;
1165     break;
1166   }
1167   case Instruction::BitCast: {
1168     // Vector->vector casts only.
1169     VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1170     if (!VTy) break;
1171     unsigned InVWidth = VTy->getNumElements();
1172     APInt InputDemandedElts(InVWidth, 0);
1173     unsigned Ratio;
1174
1175     if (VWidth == InVWidth) {
1176       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1177       // elements as are demanded of us.
1178       Ratio = 1;
1179       InputDemandedElts = DemandedElts;
1180     } else if (VWidth > InVWidth) {
1181       // Untested so far.
1182       break;
1183
1184       // If there are more elements in the result than there are in the source,
1185       // then an input element is live if any of the corresponding output
1186       // elements are live.
1187       Ratio = VWidth/InVWidth;
1188       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1189         if (DemandedElts[OutIdx])
1190           InputDemandedElts.setBit(OutIdx/Ratio);
1191       }
1192     } else {
1193       // Untested so far.
1194       break;
1195
1196       // If there are more elements in the source than there are in the result,
1197       // then an input element is live if the corresponding output element is
1198       // live.
1199       Ratio = InVWidth/VWidth;
1200       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1201         if (DemandedElts[InIdx/Ratio])
1202           InputDemandedElts.setBit(InIdx);
1203     }
1204
1205     // div/rem demand all inputs, because they don't want divide by zero.
1206     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1207                                       UndefElts2, Depth+1);
1208     if (TmpV) {
1209       I->setOperand(0, TmpV);
1210       MadeChange = true;
1211     }
1212
1213     UndefElts = UndefElts2;
1214     if (VWidth > InVWidth) {
1215       llvm_unreachable("Unimp");
1216       // If there are more elements in the result than there are in the source,
1217       // then an output element is undef if the corresponding input element is
1218       // undef.
1219       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1220         if (UndefElts2[OutIdx/Ratio])
1221           UndefElts.setBit(OutIdx);
1222     } else if (VWidth < InVWidth) {
1223       llvm_unreachable("Unimp");
1224       // If there are more elements in the source than there are in the result,
1225       // then a result element is undef if all of the corresponding input
1226       // elements are undef.
1227       UndefElts = ~0ULL >> (64-VWidth);  // Start out all undef.
1228       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1229         if (!UndefElts2[InIdx])            // Not undef?
1230           UndefElts.clearBit(InIdx/Ratio);    // Clear undef bit.
1231     }
1232     break;
1233   }
1234   case Instruction::And:
1235   case Instruction::Or:
1236   case Instruction::Xor:
1237   case Instruction::Add:
1238   case Instruction::Sub:
1239   case Instruction::Mul:
1240     // div/rem demand all inputs, because they don't want divide by zero.
1241     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1242                                       UndefElts, Depth+1);
1243     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1244     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1245                                       UndefElts2, Depth+1);
1246     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1247
1248     // Output elements are undefined if both are undefined.  Consider things
1249     // like undef&0.  The result is known zero, not undef.
1250     UndefElts &= UndefElts2;
1251     break;
1252   case Instruction::FPTrunc:
1253   case Instruction::FPExt:
1254     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1255                                       UndefElts, Depth+1);
1256     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1257     break;
1258
1259   case Instruction::Call: {
1260     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1261     if (!II) break;
1262     switch (II->getIntrinsicID()) {
1263     default: break;
1264
1265     // Binary vector operations that work column-wise.  A dest element is a
1266     // function of the corresponding input elements from the two inputs.
1267     case Intrinsic::x86_sse_sub_ss:
1268     case Intrinsic::x86_sse_mul_ss:
1269     case Intrinsic::x86_sse_min_ss:
1270     case Intrinsic::x86_sse_max_ss:
1271     case Intrinsic::x86_sse2_sub_sd:
1272     case Intrinsic::x86_sse2_mul_sd:
1273     case Intrinsic::x86_sse2_min_sd:
1274     case Intrinsic::x86_sse2_max_sd:
1275       TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1276                                         UndefElts, Depth+1);
1277       if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1278       TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1279                                         UndefElts2, Depth+1);
1280       if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1281
1282       // If only the low elt is demanded and this is a scalarizable intrinsic,
1283       // scalarize it now.
1284       if (DemandedElts == 1) {
1285         switch (II->getIntrinsicID()) {
1286         default: break;
1287         case Intrinsic::x86_sse_sub_ss:
1288         case Intrinsic::x86_sse_mul_ss:
1289         case Intrinsic::x86_sse2_sub_sd:
1290         case Intrinsic::x86_sse2_mul_sd:
1291           // TODO: Lower MIN/MAX/ABS/etc
1292           Value *LHS = II->getArgOperand(0);
1293           Value *RHS = II->getArgOperand(1);
1294           // Extract the element as scalars.
1295           LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
1296             ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
1297           RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
1298             ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
1299
1300           switch (II->getIntrinsicID()) {
1301           default: llvm_unreachable("Case stmts out of sync!");
1302           case Intrinsic::x86_sse_sub_ss:
1303           case Intrinsic::x86_sse2_sub_sd:
1304             TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
1305                                                         II->getName()), *II);
1306             break;
1307           case Intrinsic::x86_sse_mul_ss:
1308           case Intrinsic::x86_sse2_mul_sd:
1309             TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
1310                                                          II->getName()), *II);
1311             break;
1312           }
1313
1314           Instruction *New =
1315             InsertElementInst::Create(
1316               UndefValue::get(II->getType()), TmpV,
1317               ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1318                                       II->getName());
1319           InsertNewInstWith(New, *II);
1320           return New;
1321         }
1322       }
1323
1324       // Output elements are undefined if both are undefined.  Consider things
1325       // like undef&0.  The result is known zero, not undef.
1326       UndefElts &= UndefElts2;
1327       break;
1328     }
1329     break;
1330   }
1331   }
1332   return MadeChange ? I : nullptr;
1333 }