1 //===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains logic for simplifying instructions based on information
10 // about how they are used.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InstCombineInternal.h"
15 #include "llvm/Analysis/TargetTransformInfo.h"
16 #include "llvm/Analysis/ValueTracking.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PatternMatch.h"
19 #include "llvm/Support/KnownBits.h"
20 #include "llvm/Transforms/InstCombine/InstCombiner.h"
21 
22 using namespace llvm;
23 using namespace llvm::PatternMatch;
24 
25 #define DEBUG_TYPE "instcombine"
26 
27 /// Check to see if the specified operand of the specified instruction is a
28 /// constant integer. If so, check to see if there are any bits set in the
29 /// constant that are not demanded. If so, shrink the constant and return true.
30 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
31                                    const APInt &Demanded) {
32   assert(I && "No instruction?");
33   assert(OpNo < I->getNumOperands() && "Operand index too large");
34 
35   // The operand must be a constant integer or splat integer.
36   Value *Op = I->getOperand(OpNo);
37   const APInt *C;
38   if (!match(Op, m_APInt(C)))
39     return false;
40 
41   // If there are no bits set that aren't demanded, nothing to do.
42   if (C->isSubsetOf(Demanded))
43     return false;
44 
45   // This instruction is producing bits that are not demanded. Shrink the RHS.
46   I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
47 
48   return true;
49 }
50 
51 
52 
53 /// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
54 /// the instruction has any properties that allow us to simplify its operands.
55 bool InstCombinerImpl::SimplifyDemandedInstructionBits(Instruction &Inst) {
56   unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
57   KnownBits Known(BitWidth);
58   APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
59 
60   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
61                                      0, &Inst);
62   if (!V) return false;
63   if (V == &Inst) return true;
64   replaceInstUsesWith(Inst, V);
65   return true;
66 }
67 
68 /// This form of SimplifyDemandedBits simplifies the specified instruction
69 /// operand if possible, updating it in place. It returns true if it made any
70 /// change and false otherwise.
71 bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
72                                             const APInt &DemandedMask,
73                                             KnownBits &Known, unsigned Depth) {
74   Use &U = I->getOperandUse(OpNo);
75   Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, Known,
76                                           Depth, I);
77   if (!NewVal) return false;
78   if (Instruction* OpInst = dyn_cast<Instruction>(U))
79     salvageDebugInfo(*OpInst);
80 
81   replaceUse(U, NewVal);
82   return true;
83 }
84 
85 /// This function attempts to replace V with a simpler value based on the
86 /// demanded bits. When this function is called, it is known that only the bits
87 /// set in DemandedMask of the result of V are ever used downstream.
88 /// Consequently, depending on the mask and V, it may be possible to replace V
89 /// with a constant or one of its operands. In such cases, this function does
90 /// the replacement and returns true. In all other cases, it returns false after
91 /// analyzing the expression and setting KnownOne and known to be one in the
92 /// expression. Known.Zero contains all the bits that are known to be zero in
93 /// the expression. These are provided to potentially allow the caller (which
94 /// might recursively be SimplifyDemandedBits itself) to simplify the
95 /// expression.
96 /// Known.One and Known.Zero always follow the invariant that:
97 ///   Known.One & Known.Zero == 0.
98 /// That is, a bit can't be both 1 and 0. Note that the bits in Known.One and
99 /// Known.Zero may only be accurate for those bits set in DemandedMask. Note
100 /// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all
101 /// be the same.
102 ///
103 /// This returns null if it did not change anything and it permits no
104 /// simplification.  This returns V itself if it did some simplification of V's
105 /// operands based on the information about what bits are demanded. This returns
106 /// some other non-null value if it found out that V is equal to another value
107 /// in the context where the specified bits are demanded, but not for all users.
108 Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
109                                                  KnownBits &Known,
110                                                  unsigned Depth,
111                                                  Instruction *CxtI) {
112   assert(V != nullptr && "Null pointer of Value???");
113   assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
114   uint32_t BitWidth = DemandedMask.getBitWidth();
115   Type *VTy = V->getType();
116   assert(
117       (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
118       Known.getBitWidth() == BitWidth &&
119       "Value *V, DemandedMask and Known must have same BitWidth");
120 
121   if (isa<Constant>(V)) {
122     computeKnownBits(V, Known, Depth, CxtI);
123     return nullptr;
124   }
125 
126   Known.resetAll();
127   if (DemandedMask.isNullValue())     // Not demanding any bits from V.
128     return UndefValue::get(VTy);
129 
130   if (Depth == MaxAnalysisRecursionDepth)
131     return nullptr;
132 
133   if (isa<ScalableVectorType>(VTy))
134     return nullptr;
135 
136   Instruction *I = dyn_cast<Instruction>(V);
137   if (!I) {
138     computeKnownBits(V, Known, Depth, CxtI);
139     return nullptr;        // Only analyze instructions.
140   }
141 
142   // If there are multiple uses of this value and we aren't at the root, then
143   // we can't do any simplifications of the operands, because DemandedMask
144   // only reflects the bits demanded by *one* of the users.
145   if (Depth != 0 && !I->hasOneUse())
146     return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
147 
148   KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
149 
150   // If this is the root being simplified, allow it to have multiple uses,
151   // just set the DemandedMask to all bits so that we can try to simplify the
152   // operands.  This allows visitTruncInst (for example) to simplify the
153   // operand of a trunc without duplicating all the logic below.
154   if (Depth == 0 && !V->hasOneUse())
155     DemandedMask.setAllBits();
156 
157   switch (I->getOpcode()) {
158   default:
159     computeKnownBits(I, Known, Depth, CxtI);
160     break;
161   case Instruction::And: {
162     // If either the LHS or the RHS are Zero, the result is zero.
163     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
164         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
165                              Depth + 1))
166       return I;
167     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
168     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
169 
170     Known = LHSKnown & RHSKnown;
171 
172     // If the client is only demanding bits that we know, return the known
173     // constant.
174     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
175       return Constant::getIntegerValue(VTy, Known.One);
176 
177     // If all of the demanded bits are known 1 on one side, return the other.
178     // These bits cannot contribute to the result of the 'and'.
179     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
180       return I->getOperand(0);
181     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
182       return I->getOperand(1);
183 
184     // If the RHS is a constant, see if we can simplify it.
185     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero))
186       return I;
187 
188     break;
189   }
190   case Instruction::Or: {
191     // If either the LHS or the RHS are One, the result is One.
192     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
193         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
194                              Depth + 1))
195       return I;
196     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
197     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
198 
199     Known = LHSKnown | RHSKnown;
200 
201     // If the client is only demanding bits that we know, return the known
202     // constant.
203     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
204       return Constant::getIntegerValue(VTy, Known.One);
205 
206     // If all of the demanded bits are known zero on one side, return the other.
207     // These bits cannot contribute to the result of the 'or'.
208     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
209       return I->getOperand(0);
210     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
211       return I->getOperand(1);
212 
213     // If the RHS is a constant, see if we can simplify it.
214     if (ShrinkDemandedConstant(I, 1, DemandedMask))
215       return I;
216 
217     break;
218   }
219   case Instruction::Xor: {
220     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
221         SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
222       return I;
223     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
224     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
225 
226     Known = LHSKnown ^ RHSKnown;
227 
228     // If the client is only demanding bits that we know, return the known
229     // constant.
230     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
231       return Constant::getIntegerValue(VTy, Known.One);
232 
233     // If all of the demanded bits are known zero on one side, return the other.
234     // These bits cannot contribute to the result of the 'xor'.
235     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
236       return I->getOperand(0);
237     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
238       return I->getOperand(1);
239 
240     // If all of the demanded bits are known to be zero on one side or the
241     // other, turn this into an *inclusive* or.
242     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
243     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
244       Instruction *Or =
245         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
246                                  I->getName());
247       return InsertNewInstWith(Or, *I);
248     }
249 
250     // If all of the demanded bits on one side are known, and all of the set
251     // bits on that side are also known to be set on the other side, turn this
252     // into an AND, as we know the bits will be cleared.
253     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
254     if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
255         RHSKnown.One.isSubsetOf(LHSKnown.One)) {
256       Constant *AndC = Constant::getIntegerValue(VTy,
257                                                  ~RHSKnown.One & DemandedMask);
258       Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
259       return InsertNewInstWith(And, *I);
260     }
261 
262     // If the RHS is a constant, see if we can change it. Don't alter a -1
263     // constant because that's a canonical 'not' op, and that is better for
264     // combining, SCEV, and codegen.
265     const APInt *C;
266     if (match(I->getOperand(1), m_APInt(C)) && !C->isAllOnesValue()) {
267       if ((*C | ~DemandedMask).isAllOnesValue()) {
268         // Force bits to 1 to create a 'not' op.
269         I->setOperand(1, ConstantInt::getAllOnesValue(VTy));
270         return I;
271       }
272       // If we can't turn this into a 'not', try to shrink the constant.
273       if (ShrinkDemandedConstant(I, 1, DemandedMask))
274         return I;
275     }
276 
277     // If our LHS is an 'and' and if it has one use, and if any of the bits we
278     // are flipping are known to be set, then the xor is just resetting those
279     // bits to zero.  We can just knock out bits from the 'and' and the 'xor',
280     // simplifying both of them.
281     if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0))) {
282       ConstantInt *AndRHS, *XorRHS;
283       if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
284           match(I->getOperand(1), m_ConstantInt(XorRHS)) &&
285           match(LHSInst->getOperand(1), m_ConstantInt(AndRHS)) &&
286           (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
287         APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
288 
289         Constant *AndC =
290             ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
291         Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
292         InsertNewInstWith(NewAnd, *I);
293 
294         Constant *XorC =
295             ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
296         Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
297         return InsertNewInstWith(NewXor, *I);
298       }
299     }
300     break;
301   }
302   case Instruction::Select: {
303     Value *LHS, *RHS;
304     SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
305     if (SPF == SPF_UMAX) {
306       // UMax(A, C) == A if ...
307       // The lowest non-zero bit of DemandMask is higher than the highest
308       // non-zero bit of C.
309       const APInt *C;
310       unsigned CTZ = DemandedMask.countTrailingZeros();
311       if (match(RHS, m_APInt(C)) && CTZ >= C->getActiveBits())
312         return LHS;
313     } else if (SPF == SPF_UMIN) {
314       // UMin(A, C) == A if ...
315       // The lowest non-zero bit of DemandMask is higher than the highest
316       // non-one bit of C.
317       // This comes from using DeMorgans on the above umax example.
318       const APInt *C;
319       unsigned CTZ = DemandedMask.countTrailingZeros();
320       if (match(RHS, m_APInt(C)) &&
321           CTZ >= C->getBitWidth() - C->countLeadingOnes())
322         return LHS;
323     }
324 
325     // If this is a select as part of any other min/max pattern, don't simplify
326     // any further in case we break the structure.
327     if (SPF != SPF_UNKNOWN)
328       return nullptr;
329 
330     if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
331         SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
332       return I;
333     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
334     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
335 
336     // If the operands are constants, see if we can simplify them.
337     // This is similar to ShrinkDemandedConstant, but for a select we want to
338     // try to keep the selected constants the same as icmp value constants, if
339     // we can. This helps not break apart (or helps put back together)
340     // canonical patterns like min and max.
341     auto CanonicalizeSelectConstant = [](Instruction *I, unsigned OpNo,
342                                          const APInt &DemandedMask) {
343       const APInt *SelC;
344       if (!match(I->getOperand(OpNo), m_APInt(SelC)))
345         return false;
346 
347       // Get the constant out of the ICmp, if there is one.
348       // Only try this when exactly 1 operand is a constant (if both operands
349       // are constant, the icmp should eventually simplify). Otherwise, we may
350       // invert the transform that reduces set bits and infinite-loop.
351       Value *X;
352       const APInt *CmpC;
353       ICmpInst::Predicate Pred;
354       if (!match(I->getOperand(0), m_ICmp(Pred, m_Value(X), m_APInt(CmpC))) ||
355           isa<Constant>(X) || CmpC->getBitWidth() != SelC->getBitWidth())
356         return ShrinkDemandedConstant(I, OpNo, DemandedMask);
357 
358       // If the constant is already the same as the ICmp, leave it as-is.
359       if (*CmpC == *SelC)
360         return false;
361       // If the constants are not already the same, but can be with the demand
362       // mask, use the constant value from the ICmp.
363       if ((*CmpC & DemandedMask) == (*SelC & DemandedMask)) {
364         I->setOperand(OpNo, ConstantInt::get(I->getType(), *CmpC));
365         return true;
366       }
367       return ShrinkDemandedConstant(I, OpNo, DemandedMask);
368     };
369     if (CanonicalizeSelectConstant(I, 1, DemandedMask) ||
370         CanonicalizeSelectConstant(I, 2, DemandedMask))
371       return I;
372 
373     // Only known if known in both the LHS and RHS.
374     Known = KnownBits::commonBits(LHSKnown, RHSKnown);
375     break;
376   }
377   case Instruction::ZExt:
378   case Instruction::Trunc: {
379     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
380 
381     APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth);
382     KnownBits InputKnown(SrcBitWidth);
383     if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
384       return I;
385     assert(InputKnown.getBitWidth() == SrcBitWidth && "Src width changed?");
386     Known = InputKnown.zextOrTrunc(BitWidth);
387     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
388     break;
389   }
390   case Instruction::BitCast:
391     if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
392       return nullptr;  // vector->int or fp->int?
393 
394     if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
395       if (VectorType *SrcVTy =
396             dyn_cast<VectorType>(I->getOperand(0)->getType())) {
397         if (cast<FixedVectorType>(DstVTy)->getNumElements() !=
398             cast<FixedVectorType>(SrcVTy)->getNumElements())
399           // Don't touch a bitcast between vectors of different element counts.
400           return nullptr;
401       } else
402         // Don't touch a scalar-to-vector bitcast.
403         return nullptr;
404     } else if (I->getOperand(0)->getType()->isVectorTy())
405       // Don't touch a vector-to-scalar bitcast.
406       return nullptr;
407 
408     if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
409       return I;
410     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
411     break;
412   case Instruction::SExt: {
413     // Compute the bits in the result that are not present in the input.
414     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
415 
416     APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth);
417 
418     // If any of the sign extended bits are demanded, we know that the sign
419     // bit is demanded.
420     if (DemandedMask.getActiveBits() > SrcBitWidth)
421       InputDemandedBits.setBit(SrcBitWidth-1);
422 
423     KnownBits InputKnown(SrcBitWidth);
424     if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
425       return I;
426 
427     // If the input sign bit is known zero, or if the NewBits are not demanded
428     // convert this into a zero extension.
429     if (InputKnown.isNonNegative() ||
430         DemandedMask.getActiveBits() <= SrcBitWidth) {
431       // Convert to ZExt cast.
432       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
433       return InsertNewInstWith(NewCast, *I);
434      }
435 
436     // If the sign bit of the input is known set or clear, then we know the
437     // top bits of the result.
438     Known = InputKnown.sext(BitWidth);
439     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
440     break;
441   }
442   case Instruction::Add:
443     if ((DemandedMask & 1) == 0) {
444       // If we do not need the low bit, try to convert bool math to logic:
445       // add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN
446       Value *X, *Y;
447       if (match(I, m_c_Add(m_OneUse(m_ZExt(m_Value(X))),
448                            m_OneUse(m_SExt(m_Value(Y))))) &&
449           X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
450         // Truth table for inputs and output signbits:
451         //       X:0 | X:1
452         //      ----------
453         // Y:0  |  0 | 0 |
454         // Y:1  | -1 | 0 |
455         //      ----------
456         IRBuilderBase::InsertPointGuard Guard(Builder);
457         Builder.SetInsertPoint(I);
458         Value *AndNot = Builder.CreateAnd(Builder.CreateNot(X), Y);
459         return Builder.CreateSExt(AndNot, VTy);
460       }
461 
462       // add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
463       // TODO: Relax the one-use checks because we are removing an instruction?
464       if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
465                          m_OneUse(m_SExt(m_Value(Y))))) &&
466           X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
467         // Truth table for inputs and output signbits:
468         //       X:0 | X:1
469         //      -----------
470         // Y:0  | -1 | -1 |
471         // Y:1  | -1 |  0 |
472         //      -----------
473         IRBuilderBase::InsertPointGuard Guard(Builder);
474         Builder.SetInsertPoint(I);
475         Value *Or = Builder.CreateOr(X, Y);
476         return Builder.CreateSExt(Or, VTy);
477       }
478     }
479     LLVM_FALLTHROUGH;
480   case Instruction::Sub: {
481     /// If the high-bits of an ADD/SUB are not demanded, then we do not care
482     /// about the high bits of the operands.
483     unsigned NLZ = DemandedMask.countLeadingZeros();
484     // Right fill the mask of bits for this ADD/SUB to demand the most
485     // significant bit and all those below it.
486     APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
487     if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
488         SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
489         ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
490         SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
491       if (NLZ > 0) {
492         // Disable the nsw and nuw flags here: We can no longer guarantee that
493         // we won't wrap after simplification. Removing the nsw/nuw flags is
494         // legal here because the top bit is not demanded.
495         BinaryOperator &BinOP = *cast<BinaryOperator>(I);
496         BinOP.setHasNoSignedWrap(false);
497         BinOP.setHasNoUnsignedWrap(false);
498       }
499       return I;
500     }
501 
502     // If we are known to be adding/subtracting zeros to every bit below
503     // the highest demanded bit, we just return the other side.
504     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
505       return I->getOperand(0);
506     // We can't do this with the LHS for subtraction, unless we are only
507     // demanding the LSB.
508     if ((I->getOpcode() == Instruction::Add ||
509          DemandedFromOps.isOneValue()) &&
510         DemandedFromOps.isSubsetOf(LHSKnown.Zero))
511       return I->getOperand(1);
512 
513     // Otherwise just compute the known bits of the result.
514     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
515     Known = KnownBits::computeForAddSub(I->getOpcode() == Instruction::Add,
516                                         NSW, LHSKnown, RHSKnown);
517     break;
518   }
519   case Instruction::Shl: {
520     const APInt *SA;
521     if (match(I->getOperand(1), m_APInt(SA))) {
522       const APInt *ShrAmt;
523       if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt))))
524         if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0)))
525           if (Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
526                                                     DemandedMask, Known))
527             return R;
528 
529       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
530       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
531 
532       // If the shift is NUW/NSW, then it does demand the high bits.
533       ShlOperator *IOp = cast<ShlOperator>(I);
534       if (IOp->hasNoSignedWrap())
535         DemandedMaskIn.setHighBits(ShiftAmt+1);
536       else if (IOp->hasNoUnsignedWrap())
537         DemandedMaskIn.setHighBits(ShiftAmt);
538 
539       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
540         return I;
541       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
542 
543       bool SignBitZero = Known.Zero.isSignBitSet();
544       bool SignBitOne = Known.One.isSignBitSet();
545       Known.Zero <<= ShiftAmt;
546       Known.One  <<= ShiftAmt;
547       // low bits known zero.
548       if (ShiftAmt)
549         Known.Zero.setLowBits(ShiftAmt);
550 
551       // If this shift has "nsw" keyword, then the result is either a poison
552       // value or has the same sign bit as the first operand.
553       if (IOp->hasNoSignedWrap()) {
554         if (SignBitZero)
555           Known.Zero.setSignBit();
556         else if (SignBitOne)
557           Known.One.setSignBit();
558         if (Known.hasConflict())
559           return UndefValue::get(I->getType());
560       }
561     } else {
562       computeKnownBits(I, Known, Depth, CxtI);
563     }
564     break;
565   }
566   case Instruction::LShr: {
567     const APInt *SA;
568     if (match(I->getOperand(1), m_APInt(SA))) {
569       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
570 
571       // Unsigned shift right.
572       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
573 
574       // If the shift is exact, then it does demand the low bits (and knows that
575       // they are zero).
576       if (cast<LShrOperator>(I)->isExact())
577         DemandedMaskIn.setLowBits(ShiftAmt);
578 
579       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
580         return I;
581       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
582       Known.Zero.lshrInPlace(ShiftAmt);
583       Known.One.lshrInPlace(ShiftAmt);
584       if (ShiftAmt)
585         Known.Zero.setHighBits(ShiftAmt);  // high bits known zero.
586     } else {
587       computeKnownBits(I, Known, Depth, CxtI);
588     }
589     break;
590   }
591   case Instruction::AShr: {
592     // If this is an arithmetic shift right and only the low-bit is set, we can
593     // always convert this into a logical shr, even if the shift amount is
594     // variable.  The low bit of the shift cannot be an input sign bit unless
595     // the shift amount is >= the size of the datatype, which is undefined.
596     if (DemandedMask.isOneValue()) {
597       // Perform the logical shift right.
598       Instruction *NewVal = BinaryOperator::CreateLShr(
599                         I->getOperand(0), I->getOperand(1), I->getName());
600       return InsertNewInstWith(NewVal, *I);
601     }
602 
603     // If the sign bit is the only bit demanded by this ashr, then there is no
604     // need to do it, the shift doesn't change the high bit.
605     if (DemandedMask.isSignMask())
606       return I->getOperand(0);
607 
608     const APInt *SA;
609     if (match(I->getOperand(1), m_APInt(SA))) {
610       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
611 
612       // Signed shift right.
613       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
614       // If any of the high bits are demanded, we should set the sign bit as
615       // demanded.
616       if (DemandedMask.countLeadingZeros() <= ShiftAmt)
617         DemandedMaskIn.setSignBit();
618 
619       // If the shift is exact, then it does demand the low bits (and knows that
620       // they are zero).
621       if (cast<AShrOperator>(I)->isExact())
622         DemandedMaskIn.setLowBits(ShiftAmt);
623 
624       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
625         return I;
626 
627       unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI);
628 
629       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
630       // Compute the new bits that are at the top now plus sign bits.
631       APInt HighBits(APInt::getHighBitsSet(
632           BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
633       Known.Zero.lshrInPlace(ShiftAmt);
634       Known.One.lshrInPlace(ShiftAmt);
635 
636       // If the input sign bit is known to be zero, or if none of the top bits
637       // are demanded, turn this into an unsigned shift right.
638       assert(BitWidth > ShiftAmt && "Shift amount not saturated?");
639       if (Known.Zero[BitWidth-ShiftAmt-1] ||
640           !DemandedMask.intersects(HighBits)) {
641         BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
642                                                           I->getOperand(1));
643         LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
644         return InsertNewInstWith(LShr, *I);
645       } else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
646         Known.One |= HighBits;
647       }
648     } else {
649       computeKnownBits(I, Known, Depth, CxtI);
650     }
651     break;
652   }
653   case Instruction::UDiv: {
654     // UDiv doesn't demand low bits that are zero in the divisor.
655     const APInt *SA;
656     if (match(I->getOperand(1), m_APInt(SA))) {
657       // If the shift is exact, then it does demand the low bits.
658       if (cast<UDivOperator>(I)->isExact())
659         break;
660 
661       // FIXME: Take the demanded mask of the result into account.
662       unsigned RHSTrailingZeros = SA->countTrailingZeros();
663       APInt DemandedMaskIn =
664           APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
665       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1))
666         return I;
667 
668       // Propagate zero bits from the input.
669       Known.Zero.setHighBits(std::min(
670           BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
671     } else {
672       computeKnownBits(I, Known, Depth, CxtI);
673     }
674     break;
675   }
676   case Instruction::SRem: {
677     ConstantInt *Rem;
678     if (match(I->getOperand(1), m_ConstantInt(Rem))) {
679       // X % -1 demands all the bits because we don't want to introduce
680       // INT_MIN % -1 (== undef) by accident.
681       if (Rem->isMinusOne())
682         break;
683       APInt RA = Rem->getValue().abs();
684       if (RA.isPowerOf2()) {
685         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
686           return I->getOperand(0);
687 
688         APInt LowBits = RA - 1;
689         APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
690         if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
691           return I;
692 
693         // The low bits of LHS are unchanged by the srem.
694         Known.Zero = LHSKnown.Zero & LowBits;
695         Known.One = LHSKnown.One & LowBits;
696 
697         // If LHS is non-negative or has all low bits zero, then the upper bits
698         // are all zero.
699         if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
700           Known.Zero |= ~LowBits;
701 
702         // If LHS is negative and not all low bits are zero, then the upper bits
703         // are all one.
704         if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
705           Known.One |= ~LowBits;
706 
707         assert(!Known.hasConflict() && "Bits known to be one AND zero?");
708         break;
709       }
710     }
711 
712     // The sign bit is the LHS's sign bit, except when the result of the
713     // remainder is zero.
714     if (DemandedMask.isSignBitSet()) {
715       computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
716       // If it's known zero, our sign bit is also zero.
717       if (LHSKnown.isNonNegative())
718         Known.makeNonNegative();
719     }
720     break;
721   }
722   case Instruction::URem: {
723     KnownBits Known2(BitWidth);
724     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
725     if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
726         SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
727       return I;
728 
729     unsigned Leaders = Known2.countMinLeadingZeros();
730     Known.Zero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
731     break;
732   }
733   case Instruction::Call: {
734     bool KnownBitsComputed = false;
735     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
736       switch (II->getIntrinsicID()) {
737       case Intrinsic::abs: {
738         if (DemandedMask == 1)
739           return II->getArgOperand(0);
740         break;
741       }
742       case Intrinsic::ctpop: {
743         // Checking if the number of clear bits is odd (parity)? If the type has
744         // an even number of bits, that's the same as checking if the number of
745         // set bits is odd, so we can eliminate the 'not' op.
746         Value *X;
747         if (DemandedMask == 1 && VTy->getScalarSizeInBits() % 2 == 0 &&
748             match(II->getArgOperand(0), m_Not(m_Value(X)))) {
749           Function *Ctpop = Intrinsic::getDeclaration(
750               II->getModule(), Intrinsic::ctpop, II->getType());
751           return InsertNewInstWith(CallInst::Create(Ctpop, {X}), *I);
752         }
753         break;
754       }
755       case Intrinsic::bswap: {
756         // If the only bits demanded come from one byte of the bswap result,
757         // just shift the input byte into position to eliminate the bswap.
758         unsigned NLZ = DemandedMask.countLeadingZeros();
759         unsigned NTZ = DemandedMask.countTrailingZeros();
760 
761         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
762         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
763         // have 14 leading zeros, round to 8.
764         NLZ &= ~7;
765         NTZ &= ~7;
766         // If we need exactly one byte, we can do this transformation.
767         if (BitWidth-NLZ-NTZ == 8) {
768           unsigned ResultBit = NTZ;
769           unsigned InputBit = BitWidth-NTZ-8;
770 
771           // Replace this with either a left or right shift to get the byte into
772           // the right place.
773           Instruction *NewVal;
774           if (InputBit > ResultBit)
775             NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
776                     ConstantInt::get(I->getType(), InputBit-ResultBit));
777           else
778             NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
779                     ConstantInt::get(I->getType(), ResultBit-InputBit));
780           NewVal->takeName(I);
781           return InsertNewInstWith(NewVal, *I);
782         }
783         break;
784       }
785       case Intrinsic::fshr:
786       case Intrinsic::fshl: {
787         const APInt *SA;
788         if (!match(I->getOperand(2), m_APInt(SA)))
789           break;
790 
791         // Normalize to funnel shift left. APInt shifts of BitWidth are well-
792         // defined, so no need to special-case zero shifts here.
793         uint64_t ShiftAmt = SA->urem(BitWidth);
794         if (II->getIntrinsicID() == Intrinsic::fshr)
795           ShiftAmt = BitWidth - ShiftAmt;
796 
797         APInt DemandedMaskLHS(DemandedMask.lshr(ShiftAmt));
798         APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt));
799         if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, Depth + 1) ||
800             SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1))
801           return I;
802 
803         Known.Zero = LHSKnown.Zero.shl(ShiftAmt) |
804                      RHSKnown.Zero.lshr(BitWidth - ShiftAmt);
805         Known.One = LHSKnown.One.shl(ShiftAmt) |
806                     RHSKnown.One.lshr(BitWidth - ShiftAmt);
807         KnownBitsComputed = true;
808         break;
809       }
810       default: {
811         // Handle target specific intrinsics
812         Optional<Value *> V = targetSimplifyDemandedUseBitsIntrinsic(
813             *II, DemandedMask, Known, KnownBitsComputed);
814         if (V.hasValue())
815           return V.getValue();
816         break;
817       }
818       }
819     }
820 
821     if (!KnownBitsComputed)
822       computeKnownBits(V, Known, Depth, CxtI);
823     break;
824   }
825   }
826 
827   // If the client is only demanding bits that we know, return the known
828   // constant.
829   if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
830     return Constant::getIntegerValue(VTy, Known.One);
831   return nullptr;
832 }
833 
834 /// Helper routine of SimplifyDemandedUseBits. It computes Known
835 /// bits. It also tries to handle simplifications that can be done based on
836 /// DemandedMask, but without modifying the Instruction.
837 Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
838     Instruction *I, const APInt &DemandedMask, KnownBits &Known, unsigned Depth,
839     Instruction *CxtI) {
840   unsigned BitWidth = DemandedMask.getBitWidth();
841   Type *ITy = I->getType();
842 
843   KnownBits LHSKnown(BitWidth);
844   KnownBits RHSKnown(BitWidth);
845 
846   // Despite the fact that we can't simplify this instruction in all User's
847   // context, we can at least compute the known bits, and we can
848   // do simplifications that apply to *just* the one user if we know that
849   // this instruction has a simpler value in that context.
850   switch (I->getOpcode()) {
851   case Instruction::And: {
852     // If either the LHS or the RHS are Zero, the result is zero.
853     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
854     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
855                      CxtI);
856 
857     Known = LHSKnown & RHSKnown;
858 
859     // If the client is only demanding bits that we know, return the known
860     // constant.
861     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
862       return Constant::getIntegerValue(ITy, Known.One);
863 
864     // If all of the demanded bits are known 1 on one side, return the other.
865     // These bits cannot contribute to the result of the 'and' in this
866     // context.
867     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
868       return I->getOperand(0);
869     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
870       return I->getOperand(1);
871 
872     break;
873   }
874   case Instruction::Or: {
875     // We can simplify (X|Y) -> X or Y in the user's context if we know that
876     // only bits from X or Y are demanded.
877 
878     // If either the LHS or the RHS are One, the result is One.
879     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
880     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
881                      CxtI);
882 
883     Known = LHSKnown | RHSKnown;
884 
885     // If the client is only demanding bits that we know, return the known
886     // constant.
887     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
888       return Constant::getIntegerValue(ITy, Known.One);
889 
890     // If all of the demanded bits are known zero on one side, return the
891     // other.  These bits cannot contribute to the result of the 'or' in this
892     // context.
893     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
894       return I->getOperand(0);
895     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
896       return I->getOperand(1);
897 
898     break;
899   }
900   case Instruction::Xor: {
901     // We can simplify (X^Y) -> X or Y in the user's context if we know that
902     // only bits from X or Y are demanded.
903 
904     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
905     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
906                      CxtI);
907 
908     Known = LHSKnown ^ RHSKnown;
909 
910     // If the client is only demanding bits that we know, return the known
911     // constant.
912     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
913       return Constant::getIntegerValue(ITy, Known.One);
914 
915     // If all of the demanded bits are known zero on one side, return the
916     // other.
917     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
918       return I->getOperand(0);
919     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
920       return I->getOperand(1);
921 
922     break;
923   }
924   case Instruction::AShr: {
925     // Compute the Known bits to simplify things downstream.
926     computeKnownBits(I, Known, Depth, CxtI);
927 
928     // If this user is only demanding bits that we know, return the known
929     // constant.
930     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
931       return Constant::getIntegerValue(ITy, Known.One);
932 
933     // If the right shift operand 0 is a result of a left shift by the same
934     // amount, this is probably a zero/sign extension, which may be unnecessary,
935     // if we do not demand any of the new sign bits. So, return the original
936     // operand instead.
937     const APInt *ShiftRC;
938     const APInt *ShiftLC;
939     Value *X;
940     unsigned BitWidth = DemandedMask.getBitWidth();
941     if (match(I,
942               m_AShr(m_Shl(m_Value(X), m_APInt(ShiftLC)), m_APInt(ShiftRC))) &&
943         ShiftLC == ShiftRC && ShiftLC->ult(BitWidth) &&
944         DemandedMask.isSubsetOf(APInt::getLowBitsSet(
945             BitWidth, BitWidth - ShiftRC->getZExtValue()))) {
946       return X;
947     }
948 
949     break;
950   }
951   default:
952     // Compute the Known bits to simplify things downstream.
953     computeKnownBits(I, Known, Depth, CxtI);
954 
955     // If this user is only demanding bits that we know, return the known
956     // constant.
957     if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
958       return Constant::getIntegerValue(ITy, Known.One);
959 
960     break;
961   }
962 
963   return nullptr;
964 }
965 
966 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify
967 /// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
968 /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
969 /// of "C2-C1".
970 ///
971 /// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
972 /// ..., bn}, without considering the specific value X is holding.
973 /// This transformation is legal iff one of following conditions is hold:
974 ///  1) All the bit in S are 0, in this case E1 == E2.
975 ///  2) We don't care those bits in S, per the input DemandedMask.
976 ///  3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
977 ///     rest bits.
978 ///
979 /// Currently we only test condition 2).
980 ///
981 /// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
982 /// not successful.
983 Value *InstCombinerImpl::simplifyShrShlDemandedBits(
984     Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
985     const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known) {
986   if (!ShlOp1 || !ShrOp1)
987     return nullptr; // No-op.
988 
989   Value *VarX = Shr->getOperand(0);
990   Type *Ty = VarX->getType();
991   unsigned BitWidth = Ty->getScalarSizeInBits();
992   if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
993     return nullptr; // Undef.
994 
995   unsigned ShlAmt = ShlOp1.getZExtValue();
996   unsigned ShrAmt = ShrOp1.getZExtValue();
997 
998   Known.One.clearAllBits();
999   Known.Zero.setLowBits(ShlAmt - 1);
1000   Known.Zero &= DemandedMask;
1001 
1002   APInt BitMask1(APInt::getAllOnesValue(BitWidth));
1003   APInt BitMask2(APInt::getAllOnesValue(BitWidth));
1004 
1005   bool isLshr = (Shr->getOpcode() == Instruction::LShr);
1006   BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
1007                       (BitMask1.ashr(ShrAmt) << ShlAmt);
1008 
1009   if (ShrAmt <= ShlAmt) {
1010     BitMask2 <<= (ShlAmt - ShrAmt);
1011   } else {
1012     BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
1013                         BitMask2.ashr(ShrAmt - ShlAmt);
1014   }
1015 
1016   // Check if condition-2 (see the comment to this function) is satified.
1017   if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
1018     if (ShrAmt == ShlAmt)
1019       return VarX;
1020 
1021     if (!Shr->hasOneUse())
1022       return nullptr;
1023 
1024     BinaryOperator *New;
1025     if (ShrAmt < ShlAmt) {
1026       Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
1027       New = BinaryOperator::CreateShl(VarX, Amt);
1028       BinaryOperator *Orig = cast<BinaryOperator>(Shl);
1029       New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
1030       New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
1031     } else {
1032       Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
1033       New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
1034                      BinaryOperator::CreateAShr(VarX, Amt);
1035       if (cast<BinaryOperator>(Shr)->isExact())
1036         New->setIsExact(true);
1037     }
1038 
1039     return InsertNewInstWith(New, *Shl);
1040   }
1041 
1042   return nullptr;
1043 }
1044 
1045 /// The specified value produces a vector with any number of elements.
1046 /// This method analyzes which elements of the operand are undef or poison and
1047 /// returns that information in UndefElts.
1048 ///
1049 /// DemandedElts contains the set of elements that are actually used by the
1050 /// caller, and by default (AllowMultipleUsers equals false) the value is
1051 /// simplified only if it has a single caller. If AllowMultipleUsers is set
1052 /// to true, DemandedElts refers to the union of sets of elements that are
1053 /// used by all callers.
1054 ///
1055 /// If the information about demanded elements can be used to simplify the
1056 /// operation, the operation is simplified, then the resultant value is
1057 /// returned.  This returns null if no change was made.
1058 Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
1059                                                     APInt DemandedElts,
1060                                                     APInt &UndefElts,
1061                                                     unsigned Depth,
1062                                                     bool AllowMultipleUsers) {
1063   // Cannot analyze scalable type. The number of vector elements is not a
1064   // compile-time constant.
1065   if (isa<ScalableVectorType>(V->getType()))
1066     return nullptr;
1067 
1068   unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements();
1069   APInt EltMask(APInt::getAllOnesValue(VWidth));
1070   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
1071 
1072   if (match(V, m_Undef())) {
1073     // If the entire vector is undef or poison, just return this info.
1074     UndefElts = EltMask;
1075     return nullptr;
1076   }
1077 
1078   if (DemandedElts.isNullValue()) { // If nothing is demanded, provide poison.
1079     UndefElts = EltMask;
1080     return PoisonValue::get(V->getType());
1081   }
1082 
1083   UndefElts = 0;
1084 
1085   if (auto *C = dyn_cast<Constant>(V)) {
1086     // Check if this is identity. If so, return 0 since we are not simplifying
1087     // anything.
1088     if (DemandedElts.isAllOnesValue())
1089       return nullptr;
1090 
1091     Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1092     Constant *Poison = PoisonValue::get(EltTy);
1093     SmallVector<Constant*, 16> Elts;
1094     for (unsigned i = 0; i != VWidth; ++i) {
1095       if (!DemandedElts[i]) {   // If not demanded, set to poison.
1096         Elts.push_back(Poison);
1097         UndefElts.setBit(i);
1098         continue;
1099       }
1100 
1101       Constant *Elt = C->getAggregateElement(i);
1102       if (!Elt) return nullptr;
1103 
1104       Elts.push_back(Elt);
1105       if (isa<UndefValue>(Elt))   // Already undef or poison.
1106         UndefElts.setBit(i);
1107     }
1108 
1109     // If we changed the constant, return it.
1110     Constant *NewCV = ConstantVector::get(Elts);
1111     return NewCV != C ? NewCV : nullptr;
1112   }
1113 
1114   // Limit search depth.
1115   if (Depth == 10)
1116     return nullptr;
1117 
1118   if (!AllowMultipleUsers) {
1119     // If multiple users are using the root value, proceed with
1120     // simplification conservatively assuming that all elements
1121     // are needed.
1122     if (!V->hasOneUse()) {
1123       // Quit if we find multiple users of a non-root value though.
1124       // They'll be handled when it's their turn to be visited by
1125       // the main instcombine process.
1126       if (Depth != 0)
1127         // TODO: Just compute the UndefElts information recursively.
1128         return nullptr;
1129 
1130       // Conservatively assume that all elements are needed.
1131       DemandedElts = EltMask;
1132     }
1133   }
1134 
1135   Instruction *I = dyn_cast<Instruction>(V);
1136   if (!I) return nullptr;        // Only analyze instructions.
1137 
1138   bool MadeChange = false;
1139   auto simplifyAndSetOp = [&](Instruction *Inst, unsigned OpNum,
1140                               APInt Demanded, APInt &Undef) {
1141     auto *II = dyn_cast<IntrinsicInst>(Inst);
1142     Value *Op = II ? II->getArgOperand(OpNum) : Inst->getOperand(OpNum);
1143     if (Value *V = SimplifyDemandedVectorElts(Op, Demanded, Undef, Depth + 1)) {
1144       replaceOperand(*Inst, OpNum, V);
1145       MadeChange = true;
1146     }
1147   };
1148 
1149   APInt UndefElts2(VWidth, 0);
1150   APInt UndefElts3(VWidth, 0);
1151   switch (I->getOpcode()) {
1152   default: break;
1153 
1154   case Instruction::GetElementPtr: {
1155     // The LangRef requires that struct geps have all constant indices.  As
1156     // such, we can't convert any operand to partial undef.
1157     auto mayIndexStructType = [](GetElementPtrInst &GEP) {
1158       for (auto I = gep_type_begin(GEP), E = gep_type_end(GEP);
1159            I != E; I++)
1160         if (I.isStruct())
1161           return true;;
1162       return false;
1163     };
1164     if (mayIndexStructType(cast<GetElementPtrInst>(*I)))
1165       break;
1166 
1167     // Conservatively track the demanded elements back through any vector
1168     // operands we may have.  We know there must be at least one, or we
1169     // wouldn't have a vector result to get here. Note that we intentionally
1170     // merge the undef bits here since gepping with either an undef base or
1171     // index results in undef.
1172     for (unsigned i = 0; i < I->getNumOperands(); i++) {
1173       if (match(I->getOperand(i), m_Undef())) {
1174         // If the entire vector is undefined, just return this info.
1175         UndefElts = EltMask;
1176         return nullptr;
1177       }
1178       if (I->getOperand(i)->getType()->isVectorTy()) {
1179         APInt UndefEltsOp(VWidth, 0);
1180         simplifyAndSetOp(I, i, DemandedElts, UndefEltsOp);
1181         UndefElts |= UndefEltsOp;
1182       }
1183     }
1184 
1185     break;
1186   }
1187   case Instruction::InsertElement: {
1188     // If this is a variable index, we don't know which element it overwrites.
1189     // demand exactly the same input as we produce.
1190     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1191     if (!Idx) {
1192       // Note that we can't propagate undef elt info, because we don't know
1193       // which elt is getting updated.
1194       simplifyAndSetOp(I, 0, DemandedElts, UndefElts2);
1195       break;
1196     }
1197 
1198     // The element inserted overwrites whatever was there, so the input demanded
1199     // set is simpler than the output set.
1200     unsigned IdxNo = Idx->getZExtValue();
1201     APInt PreInsertDemandedElts = DemandedElts;
1202     if (IdxNo < VWidth)
1203       PreInsertDemandedElts.clearBit(IdxNo);
1204 
1205     // If we only demand the element that is being inserted and that element
1206     // was extracted from the same index in another vector with the same type,
1207     // replace this insert with that other vector.
1208     // Note: This is attempted before the call to simplifyAndSetOp because that
1209     //       may change UndefElts to a value that does not match with Vec.
1210     Value *Vec;
1211     if (PreInsertDemandedElts == 0 &&
1212         match(I->getOperand(1),
1213               m_ExtractElt(m_Value(Vec), m_SpecificInt(IdxNo))) &&
1214         Vec->getType() == I->getType()) {
1215       return Vec;
1216     }
1217 
1218     simplifyAndSetOp(I, 0, PreInsertDemandedElts, UndefElts);
1219 
1220     // If this is inserting an element that isn't demanded, remove this
1221     // insertelement.
1222     if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1223       Worklist.push(I);
1224       return I->getOperand(0);
1225     }
1226 
1227     // The inserted element is defined.
1228     UndefElts.clearBit(IdxNo);
1229     break;
1230   }
1231   case Instruction::ShuffleVector: {
1232     auto *Shuffle = cast<ShuffleVectorInst>(I);
1233     assert(Shuffle->getOperand(0)->getType() ==
1234            Shuffle->getOperand(1)->getType() &&
1235            "Expected shuffle operands to have same type");
1236     unsigned OpWidth = cast<FixedVectorType>(Shuffle->getOperand(0)->getType())
1237                            ->getNumElements();
1238     // Handle trivial case of a splat. Only check the first element of LHS
1239     // operand.
1240     if (all_of(Shuffle->getShuffleMask(), [](int Elt) { return Elt == 0; }) &&
1241         DemandedElts.isAllOnesValue()) {
1242       if (!match(I->getOperand(1), m_Undef())) {
1243         I->setOperand(1, UndefValue::get(I->getOperand(1)->getType()));
1244         MadeChange = true;
1245       }
1246       APInt LeftDemanded(OpWidth, 1);
1247       APInt LHSUndefElts(OpWidth, 0);
1248       simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts);
1249       if (LHSUndefElts[0])
1250         UndefElts = EltMask;
1251       else
1252         UndefElts.clearAllBits();
1253       break;
1254     }
1255 
1256     APInt LeftDemanded(OpWidth, 0), RightDemanded(OpWidth, 0);
1257     for (unsigned i = 0; i < VWidth; i++) {
1258       if (DemandedElts[i]) {
1259         unsigned MaskVal = Shuffle->getMaskValue(i);
1260         if (MaskVal != -1u) {
1261           assert(MaskVal < OpWidth * 2 &&
1262                  "shufflevector mask index out of range!");
1263           if (MaskVal < OpWidth)
1264             LeftDemanded.setBit(MaskVal);
1265           else
1266             RightDemanded.setBit(MaskVal - OpWidth);
1267         }
1268       }
1269     }
1270 
1271     APInt LHSUndefElts(OpWidth, 0);
1272     simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts);
1273 
1274     APInt RHSUndefElts(OpWidth, 0);
1275     simplifyAndSetOp(I, 1, RightDemanded, RHSUndefElts);
1276 
1277     // If this shuffle does not change the vector length and the elements
1278     // demanded by this shuffle are an identity mask, then this shuffle is
1279     // unnecessary.
1280     //
1281     // We are assuming canonical form for the mask, so the source vector is
1282     // operand 0 and operand 1 is not used.
1283     //
1284     // Note that if an element is demanded and this shuffle mask is undefined
1285     // for that element, then the shuffle is not considered an identity
1286     // operation. The shuffle prevents poison from the operand vector from
1287     // leaking to the result by replacing poison with an undefined value.
1288     if (VWidth == OpWidth) {
1289       bool IsIdentityShuffle = true;
1290       for (unsigned i = 0; i < VWidth; i++) {
1291         unsigned MaskVal = Shuffle->getMaskValue(i);
1292         if (DemandedElts[i] && i != MaskVal) {
1293           IsIdentityShuffle = false;
1294           break;
1295         }
1296       }
1297       if (IsIdentityShuffle)
1298         return Shuffle->getOperand(0);
1299     }
1300 
1301     bool NewUndefElts = false;
1302     unsigned LHSIdx = -1u, LHSValIdx = -1u;
1303     unsigned RHSIdx = -1u, RHSValIdx = -1u;
1304     bool LHSUniform = true;
1305     bool RHSUniform = true;
1306     for (unsigned i = 0; i < VWidth; i++) {
1307       unsigned MaskVal = Shuffle->getMaskValue(i);
1308       if (MaskVal == -1u) {
1309         UndefElts.setBit(i);
1310       } else if (!DemandedElts[i]) {
1311         NewUndefElts = true;
1312         UndefElts.setBit(i);
1313       } else if (MaskVal < OpWidth) {
1314         if (LHSUndefElts[MaskVal]) {
1315           NewUndefElts = true;
1316           UndefElts.setBit(i);
1317         } else {
1318           LHSIdx = LHSIdx == -1u ? i : OpWidth;
1319           LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth;
1320           LHSUniform = LHSUniform && (MaskVal == i);
1321         }
1322       } else {
1323         if (RHSUndefElts[MaskVal - OpWidth]) {
1324           NewUndefElts = true;
1325           UndefElts.setBit(i);
1326         } else {
1327           RHSIdx = RHSIdx == -1u ? i : OpWidth;
1328           RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth;
1329           RHSUniform = RHSUniform && (MaskVal - OpWidth == i);
1330         }
1331       }
1332     }
1333 
1334     // Try to transform shuffle with constant vector and single element from
1335     // this constant vector to single insertelement instruction.
1336     // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1337     // insertelement V, C[ci], ci-n
1338     if (OpWidth ==
1339         cast<FixedVectorType>(Shuffle->getType())->getNumElements()) {
1340       Value *Op = nullptr;
1341       Constant *Value = nullptr;
1342       unsigned Idx = -1u;
1343 
1344       // Find constant vector with the single element in shuffle (LHS or RHS).
1345       if (LHSIdx < OpWidth && RHSUniform) {
1346         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1347           Op = Shuffle->getOperand(1);
1348           Value = CV->getOperand(LHSValIdx);
1349           Idx = LHSIdx;
1350         }
1351       }
1352       if (RHSIdx < OpWidth && LHSUniform) {
1353         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1354           Op = Shuffle->getOperand(0);
1355           Value = CV->getOperand(RHSValIdx);
1356           Idx = RHSIdx;
1357         }
1358       }
1359       // Found constant vector with single element - convert to insertelement.
1360       if (Op && Value) {
1361         Instruction *New = InsertElementInst::Create(
1362             Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1363             Shuffle->getName());
1364         InsertNewInstWith(New, *Shuffle);
1365         return New;
1366       }
1367     }
1368     if (NewUndefElts) {
1369       // Add additional discovered undefs.
1370       SmallVector<int, 16> Elts;
1371       for (unsigned i = 0; i < VWidth; ++i) {
1372         if (UndefElts[i])
1373           Elts.push_back(UndefMaskElem);
1374         else
1375           Elts.push_back(Shuffle->getMaskValue(i));
1376       }
1377       Shuffle->setShuffleMask(Elts);
1378       MadeChange = true;
1379     }
1380     break;
1381   }
1382   case Instruction::Select: {
1383     // If this is a vector select, try to transform the select condition based
1384     // on the current demanded elements.
1385     SelectInst *Sel = cast<SelectInst>(I);
1386     if (Sel->getCondition()->getType()->isVectorTy()) {
1387       // TODO: We are not doing anything with UndefElts based on this call.
1388       // It is overwritten below based on the other select operands. If an
1389       // element of the select condition is known undef, then we are free to
1390       // choose the output value from either arm of the select. If we know that
1391       // one of those values is undef, then the output can be undef.
1392       simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1393     }
1394 
1395     // Next, see if we can transform the arms of the select.
1396     APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
1397     if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
1398       for (unsigned i = 0; i < VWidth; i++) {
1399         // isNullValue() always returns false when called on a ConstantExpr.
1400         // Skip constant expressions to avoid propagating incorrect information.
1401         Constant *CElt = CV->getAggregateElement(i);
1402         if (isa<ConstantExpr>(CElt))
1403           continue;
1404         // TODO: If a select condition element is undef, we can demand from
1405         // either side. If one side is known undef, choosing that side would
1406         // propagate undef.
1407         if (CElt->isNullValue())
1408           DemandedLHS.clearBit(i);
1409         else
1410           DemandedRHS.clearBit(i);
1411       }
1412     }
1413 
1414     simplifyAndSetOp(I, 1, DemandedLHS, UndefElts2);
1415     simplifyAndSetOp(I, 2, DemandedRHS, UndefElts3);
1416 
1417     // Output elements are undefined if the element from each arm is undefined.
1418     // TODO: This can be improved. See comment in select condition handling.
1419     UndefElts = UndefElts2 & UndefElts3;
1420     break;
1421   }
1422   case Instruction::BitCast: {
1423     // Vector->vector casts only.
1424     VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1425     if (!VTy) break;
1426     unsigned InVWidth = cast<FixedVectorType>(VTy)->getNumElements();
1427     APInt InputDemandedElts(InVWidth, 0);
1428     UndefElts2 = APInt(InVWidth, 0);
1429     unsigned Ratio;
1430 
1431     if (VWidth == InVWidth) {
1432       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1433       // elements as are demanded of us.
1434       Ratio = 1;
1435       InputDemandedElts = DemandedElts;
1436     } else if ((VWidth % InVWidth) == 0) {
1437       // If the number of elements in the output is a multiple of the number of
1438       // elements in the input then an input element is live if any of the
1439       // corresponding output elements are live.
1440       Ratio = VWidth / InVWidth;
1441       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1442         if (DemandedElts[OutIdx])
1443           InputDemandedElts.setBit(OutIdx / Ratio);
1444     } else if ((InVWidth % VWidth) == 0) {
1445       // If the number of elements in the input is a multiple of the number of
1446       // elements in the output then an input element is live if the
1447       // corresponding output element is live.
1448       Ratio = InVWidth / VWidth;
1449       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1450         if (DemandedElts[InIdx / Ratio])
1451           InputDemandedElts.setBit(InIdx);
1452     } else {
1453       // Unsupported so far.
1454       break;
1455     }
1456 
1457     simplifyAndSetOp(I, 0, InputDemandedElts, UndefElts2);
1458 
1459     if (VWidth == InVWidth) {
1460       UndefElts = UndefElts2;
1461     } else if ((VWidth % InVWidth) == 0) {
1462       // If the number of elements in the output is a multiple of the number of
1463       // elements in the input then an output element is undef if the
1464       // corresponding input element is undef.
1465       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1466         if (UndefElts2[OutIdx / Ratio])
1467           UndefElts.setBit(OutIdx);
1468     } else if ((InVWidth % VWidth) == 0) {
1469       // If the number of elements in the input is a multiple of the number of
1470       // elements in the output then an output element is undef if all of the
1471       // corresponding input elements are undef.
1472       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1473         APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1474         if (SubUndef.countPopulation() == Ratio)
1475           UndefElts.setBit(OutIdx);
1476       }
1477     } else {
1478       llvm_unreachable("Unimp");
1479     }
1480     break;
1481   }
1482   case Instruction::FPTrunc:
1483   case Instruction::FPExt:
1484     simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1485     break;
1486 
1487   case Instruction::Call: {
1488     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1489     if (!II) break;
1490     switch (II->getIntrinsicID()) {
1491     case Intrinsic::masked_gather: // fallthrough
1492     case Intrinsic::masked_load: {
1493       // Subtlety: If we load from a pointer, the pointer must be valid
1494       // regardless of whether the element is demanded.  Doing otherwise risks
1495       // segfaults which didn't exist in the original program.
1496       APInt DemandedPtrs(APInt::getAllOnesValue(VWidth)),
1497         DemandedPassThrough(DemandedElts);
1498       if (auto *CV = dyn_cast<ConstantVector>(II->getOperand(2)))
1499         for (unsigned i = 0; i < VWidth; i++) {
1500           Constant *CElt = CV->getAggregateElement(i);
1501           if (CElt->isNullValue())
1502             DemandedPtrs.clearBit(i);
1503           else if (CElt->isAllOnesValue())
1504             DemandedPassThrough.clearBit(i);
1505         }
1506       if (II->getIntrinsicID() == Intrinsic::masked_gather)
1507         simplifyAndSetOp(II, 0, DemandedPtrs, UndefElts2);
1508       simplifyAndSetOp(II, 3, DemandedPassThrough, UndefElts3);
1509 
1510       // Output elements are undefined if the element from both sources are.
1511       // TODO: can strengthen via mask as well.
1512       UndefElts = UndefElts2 & UndefElts3;
1513       break;
1514     }
1515     default: {
1516       // Handle target specific intrinsics
1517       Optional<Value *> V = targetSimplifyDemandedVectorEltsIntrinsic(
1518           *II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
1519           simplifyAndSetOp);
1520       if (V.hasValue())
1521         return V.getValue();
1522       break;
1523     }
1524     } // switch on IntrinsicID
1525     break;
1526   } // case Call
1527   } // switch on Opcode
1528 
1529   // TODO: We bail completely on integer div/rem and shifts because they have
1530   // UB/poison potential, but that should be refined.
1531   BinaryOperator *BO;
1532   if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) {
1533     simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1534     simplifyAndSetOp(I, 1, DemandedElts, UndefElts2);
1535 
1536     // Any change to an instruction with potential poison must clear those flags
1537     // because we can not guarantee those constraints now. Other analysis may
1538     // determine that it is safe to re-apply the flags.
1539     if (MadeChange)
1540       BO->dropPoisonGeneratingFlags();
1541 
1542     // Output elements are undefined if both are undefined. Consider things
1543     // like undef & 0. The result is known zero, not undef.
1544     UndefElts &= UndefElts2;
1545   }
1546 
1547   // If we've proven all of the lanes undef, return an undef value.
1548   // TODO: Intersect w/demanded lanes
1549   if (UndefElts.isAllOnesValue())
1550     return UndefValue::get(I->getType());;
1551 
1552   return MadeChange ? I : nullptr;
1553 }
1554