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/ValueTracking.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/IntrinsicsAMDGPU.h"
18 #include "llvm/IR/IntrinsicsX86.h"
19 #include "llvm/IR/PatternMatch.h"
20 #include "llvm/Support/KnownBits.h"
21 
22 using namespace llvm;
23 using namespace llvm::PatternMatch;
24 
25 #define DEBUG_TYPE "instcombine"
26 
27 namespace {
28 
29 struct AMDGPUImageDMaskIntrinsic {
30   unsigned Intr;
31 };
32 
33 #define GET_AMDGPUImageDMaskIntrinsicTable_IMPL
34 #include "InstCombineTables.inc"
35 
36 } // end anonymous namespace
37 
38 /// Check to see if the specified operand of the specified instruction is a
39 /// constant integer. If so, check to see if there are any bits set in the
40 /// constant that are not demanded. If so, shrink the constant and return true.
41 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
42                                    const APInt &Demanded) {
43   assert(I && "No instruction?");
44   assert(OpNo < I->getNumOperands() && "Operand index too large");
45 
46   // The operand must be a constant integer or splat integer.
47   Value *Op = I->getOperand(OpNo);
48   const APInt *C;
49   if (!match(Op, m_APInt(C)))
50     return false;
51 
52   // If there are no bits set that aren't demanded, nothing to do.
53   if (C->isSubsetOf(Demanded))
54     return false;
55 
56   // This instruction is producing bits that are not demanded. Shrink the RHS.
57   I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
58 
59   return true;
60 }
61 
62 
63 
64 /// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
65 /// the instruction has any properties that allow us to simplify its operands.
66 bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
67   unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
68   KnownBits Known(BitWidth);
69   APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
70 
71   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
72                                      0, &Inst);
73   if (!V) return false;
74   if (V == &Inst) return true;
75   replaceInstUsesWith(Inst, V);
76   return true;
77 }
78 
79 /// This form of SimplifyDemandedBits simplifies the specified instruction
80 /// operand if possible, updating it in place. It returns true if it made any
81 /// change and false otherwise.
82 bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
83                                         const APInt &DemandedMask,
84                                         KnownBits &Known,
85                                         unsigned Depth) {
86   Use &U = I->getOperandUse(OpNo);
87   Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, Known,
88                                           Depth, I);
89   if (!NewVal) return false;
90   U = NewVal;
91   return true;
92 }
93 
94 
95 /// This function attempts to replace V with a simpler value based on the
96 /// demanded bits. When this function is called, it is known that only the bits
97 /// set in DemandedMask of the result of V are ever used downstream.
98 /// Consequently, depending on the mask and V, it may be possible to replace V
99 /// with a constant or one of its operands. In such cases, this function does
100 /// the replacement and returns true. In all other cases, it returns false after
101 /// analyzing the expression and setting KnownOne and known to be one in the
102 /// expression. Known.Zero contains all the bits that are known to be zero in
103 /// the expression. These are provided to potentially allow the caller (which
104 /// might recursively be SimplifyDemandedBits itself) to simplify the
105 /// expression.
106 /// Known.One and Known.Zero always follow the invariant that:
107 ///   Known.One & Known.Zero == 0.
108 /// That is, a bit can't be both 1 and 0. Note that the bits in Known.One and
109 /// Known.Zero may only be accurate for those bits set in DemandedMask. Note
110 /// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all
111 /// be the same.
112 ///
113 /// This returns null if it did not change anything and it permits no
114 /// simplification.  This returns V itself if it did some simplification of V's
115 /// operands based on the information about what bits are demanded. This returns
116 /// some other non-null value if it found out that V is equal to another value
117 /// in the context where the specified bits are demanded, but not for all users.
118 Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
119                                              KnownBits &Known, 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(
126       (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
127       Known.getBitWidth() == BitWidth &&
128       "Value *V, DemandedMask and Known must have same BitWidth");
129 
130   if (isa<Constant>(V)) {
131     computeKnownBits(V, Known, Depth, CxtI);
132     return nullptr;
133   }
134 
135   Known.resetAll();
136   if (DemandedMask.isNullValue())     // Not demanding any bits from V.
137     return UndefValue::get(VTy);
138 
139   if (Depth == 6)        // Limit search depth.
140     return nullptr;
141 
142   Instruction *I = dyn_cast<Instruction>(V);
143   if (!I) {
144     computeKnownBits(V, Known, Depth, CxtI);
145     return nullptr;        // Only analyze instructions.
146   }
147 
148   // If there are multiple uses of this value and we aren't at the root, then
149   // we can't do any simplifications of the operands, because DemandedMask
150   // only reflects the bits demanded by *one* of the users.
151   if (Depth != 0 && !I->hasOneUse())
152     return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
153 
154   KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
155 
156   // If this is the root being simplified, allow it to have multiple uses,
157   // just set the DemandedMask to all bits so that we can try to simplify the
158   // operands.  This allows visitTruncInst (for example) to simplify the
159   // operand of a trunc without duplicating all the logic below.
160   if (Depth == 0 && !V->hasOneUse())
161     DemandedMask.setAllBits();
162 
163   switch (I->getOpcode()) {
164   default:
165     computeKnownBits(I, Known, Depth, CxtI);
166     break;
167   case Instruction::And: {
168     // If either the LHS or the RHS are Zero, the result is zero.
169     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
170         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
171                              Depth + 1))
172       return I;
173     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
174     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
175 
176     // Output known-0 are known to be clear if zero in either the LHS | RHS.
177     APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
178     // Output known-1 bits are only known if set in both the LHS & RHS.
179     APInt IKnownOne = RHSKnown.One & LHSKnown.One;
180 
181     // If the client is only demanding bits that we know, return the known
182     // constant.
183     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
184       return Constant::getIntegerValue(VTy, IKnownOne);
185 
186     // If all of the demanded bits are known 1 on one side, return the other.
187     // These bits cannot contribute to the result of the 'and'.
188     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
189       return I->getOperand(0);
190     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
191       return I->getOperand(1);
192 
193     // If the RHS is a constant, see if we can simplify it.
194     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero))
195       return I;
196 
197     Known.Zero = std::move(IKnownZero);
198     Known.One  = std::move(IKnownOne);
199     break;
200   }
201   case Instruction::Or: {
202     // If either the LHS or the RHS are One, the result is One.
203     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
204         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
205                              Depth + 1))
206       return I;
207     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
208     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
209 
210     // Output known-0 bits are only known if clear in both the LHS & RHS.
211     APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
212     // Output known-1 are known. to be set if s.et in either the LHS | RHS.
213     APInt IKnownOne = RHSKnown.One | LHSKnown.One;
214 
215     // If the client is only demanding bits that we know, return the known
216     // constant.
217     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
218       return Constant::getIntegerValue(VTy, IKnownOne);
219 
220     // If all of the demanded bits are known zero on one side, return the other.
221     // These bits cannot contribute to the result of the 'or'.
222     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
223       return I->getOperand(0);
224     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
225       return I->getOperand(1);
226 
227     // If the RHS is a constant, see if we can simplify it.
228     if (ShrinkDemandedConstant(I, 1, DemandedMask))
229       return I;
230 
231     Known.Zero = std::move(IKnownZero);
232     Known.One  = std::move(IKnownOne);
233     break;
234   }
235   case Instruction::Xor: {
236     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
237         SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
238       return I;
239     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
240     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
241 
242     // Output known-0 bits are known if clear or set in both the LHS & RHS.
243     APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
244                        (RHSKnown.One & LHSKnown.One);
245     // Output known-1 are known to be set if set in only one of the LHS, RHS.
246     APInt IKnownOne =  (RHSKnown.Zero & LHSKnown.One) |
247                        (RHSKnown.One & LHSKnown.Zero);
248 
249     // If the client is only demanding bits that we know, return the known
250     // constant.
251     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
252       return Constant::getIntegerValue(VTy, IKnownOne);
253 
254     // If all of the demanded bits are known zero on one side, return the other.
255     // These bits cannot contribute to the result of the 'xor'.
256     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
257       return I->getOperand(0);
258     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
259       return I->getOperand(1);
260 
261     // If all of the demanded bits are known to be zero on one side or the
262     // other, turn this into an *inclusive* or.
263     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
264     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
265       Instruction *Or =
266         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
267                                  I->getName());
268       return InsertNewInstWith(Or, *I);
269     }
270 
271     // If all of the demanded bits on one side are known, and all of the set
272     // bits on that side are also known to be set on the other side, turn this
273     // into an AND, as we know the bits will be cleared.
274     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
275     if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
276         RHSKnown.One.isSubsetOf(LHSKnown.One)) {
277       Constant *AndC = Constant::getIntegerValue(VTy,
278                                                  ~RHSKnown.One & DemandedMask);
279       Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
280       return InsertNewInstWith(And, *I);
281     }
282 
283     // If the RHS is a constant, see if we can simplify it.
284     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
285     if (ShrinkDemandedConstant(I, 1, DemandedMask))
286       return I;
287 
288     // If our LHS is an 'and' and if it has one use, and if any of the bits we
289     // are flipping are known to be set, then the xor is just resetting those
290     // bits to zero.  We can just knock out bits from the 'and' and the 'xor',
291     // simplifying both of them.
292     if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
293       if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
294           isa<ConstantInt>(I->getOperand(1)) &&
295           isa<ConstantInt>(LHSInst->getOperand(1)) &&
296           (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
297         ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
298         ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
299         APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
300 
301         Constant *AndC =
302           ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
303         Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
304         InsertNewInstWith(NewAnd, *I);
305 
306         Constant *XorC =
307           ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
308         Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
309         return InsertNewInstWith(NewXor, *I);
310       }
311 
312     // Output known-0 bits are known if clear or set in both the LHS & RHS.
313     Known.Zero = std::move(IKnownZero);
314     // Output known-1 are known to be set if set in only one of the LHS, RHS.
315     Known.One  = std::move(IKnownOne);
316     break;
317   }
318   case Instruction::Select: {
319     Value *LHS, *RHS;
320     SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
321     if (SPF == SPF_UMAX) {
322       // UMax(A, C) == A if ...
323       // The lowest non-zero bit of DemandMask is higher than the highest
324       // non-zero bit of C.
325       const APInt *C;
326       unsigned CTZ = DemandedMask.countTrailingZeros();
327       if (match(RHS, m_APInt(C)) && CTZ >= C->getActiveBits())
328         return LHS;
329     } else if (SPF == SPF_UMIN) {
330       // UMin(A, C) == A if ...
331       // The lowest non-zero bit of DemandMask is higher than the highest
332       // non-one bit of C.
333       // This comes from using DeMorgans on the above umax example.
334       const APInt *C;
335       unsigned CTZ = DemandedMask.countTrailingZeros();
336       if (match(RHS, m_APInt(C)) &&
337           CTZ >= C->getBitWidth() - C->countLeadingOnes())
338         return LHS;
339     }
340 
341     // If this is a select as part of any other min/max pattern, don't simplify
342     // any further in case we break the structure.
343     if (SPF != SPF_UNKNOWN)
344       return nullptr;
345 
346     if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
347         SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
348       return I;
349     assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
350     assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
351 
352     // If the operands are constants, see if we can simplify them.
353     // This is similar to ShrinkDemandedConstant, but for a select we want to
354     // try to keep the selected constants the same as icmp value constants, if
355     // we can. This helps not break apart (or helps put back together)
356     // canonical patterns like min and max.
357     auto CanonicalizeSelectConstant = [](Instruction *I, unsigned OpNo,
358                                          APInt DemandedMask) {
359       const APInt *SelC;
360       if (!match(I->getOperand(OpNo), m_APInt(SelC)))
361         return false;
362 
363       // Get the constant out of the ICmp, if there is one.
364       const APInt *CmpC;
365       ICmpInst::Predicate Pred;
366       if (!match(I->getOperand(0), m_c_ICmp(Pred, m_APInt(CmpC), m_Value())) ||
367           CmpC->getBitWidth() != SelC->getBitWidth())
368         return ShrinkDemandedConstant(I, OpNo, DemandedMask);
369 
370       // If the constant is already the same as the ICmp, leave it as-is.
371       if (*CmpC == *SelC)
372         return false;
373       // If the constants are not already the same, but can be with the demand
374       // mask, use the constant value from the ICmp.
375       if ((*CmpC & DemandedMask) == (*SelC & DemandedMask)) {
376         I->setOperand(OpNo, ConstantInt::get(I->getType(), *CmpC));
377         return true;
378       }
379       return ShrinkDemandedConstant(I, OpNo, DemandedMask);
380     };
381     if (CanonicalizeSelectConstant(I, 1, DemandedMask) ||
382         CanonicalizeSelectConstant(I, 2, DemandedMask))
383       return I;
384 
385     // Only known if known in both the LHS and RHS.
386     Known.One = RHSKnown.One & LHSKnown.One;
387     Known.Zero = RHSKnown.Zero & LHSKnown.Zero;
388     break;
389   }
390   case Instruction::ZExt:
391   case Instruction::Trunc: {
392     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
393 
394     APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth);
395     KnownBits InputKnown(SrcBitWidth);
396     if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
397       return I;
398     assert(InputKnown.getBitWidth() == SrcBitWidth && "Src width changed?");
399     Known = InputKnown.zextOrTrunc(BitWidth,
400                                    true /* ExtendedBitsAreKnownZero */);
401     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
402     break;
403   }
404   case Instruction::BitCast:
405     if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
406       return nullptr;  // vector->int or fp->int?
407 
408     if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
409       if (VectorType *SrcVTy =
410             dyn_cast<VectorType>(I->getOperand(0)->getType())) {
411         if (DstVTy->getNumElements() != SrcVTy->getNumElements())
412           // Don't touch a bitcast between vectors of different element counts.
413           return nullptr;
414       } else
415         // Don't touch a scalar-to-vector bitcast.
416         return nullptr;
417     } else if (I->getOperand(0)->getType()->isVectorTy())
418       // Don't touch a vector-to-scalar bitcast.
419       return nullptr;
420 
421     if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
422       return I;
423     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
424     break;
425   case Instruction::SExt: {
426     // Compute the bits in the result that are not present in the input.
427     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
428 
429     APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth);
430 
431     // If any of the sign extended bits are demanded, we know that the sign
432     // bit is demanded.
433     if (DemandedMask.getActiveBits() > SrcBitWidth)
434       InputDemandedBits.setBit(SrcBitWidth-1);
435 
436     KnownBits InputKnown(SrcBitWidth);
437     if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
438       return I;
439 
440     // If the input sign bit is known zero, or if the NewBits are not demanded
441     // convert this into a zero extension.
442     if (InputKnown.isNonNegative() ||
443         DemandedMask.getActiveBits() <= SrcBitWidth) {
444       // Convert to ZExt cast.
445       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
446       return InsertNewInstWith(NewCast, *I);
447      }
448 
449     // If the sign bit of the input is known set or clear, then we know the
450     // top bits of the result.
451     Known = InputKnown.sext(BitWidth);
452     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
453     break;
454   }
455   case Instruction::Add:
456   case Instruction::Sub: {
457     /// If the high-bits of an ADD/SUB are not demanded, then we do not care
458     /// about the high bits of the operands.
459     unsigned NLZ = DemandedMask.countLeadingZeros();
460     // Right fill the mask of bits for this ADD/SUB to demand the most
461     // significant bit and all those below it.
462     APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
463     if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
464         SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
465         ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
466         SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
467       if (NLZ > 0) {
468         // Disable the nsw and nuw flags here: We can no longer guarantee that
469         // we won't wrap after simplification. Removing the nsw/nuw flags is
470         // legal here because the top bit is not demanded.
471         BinaryOperator &BinOP = *cast<BinaryOperator>(I);
472         BinOP.setHasNoSignedWrap(false);
473         BinOP.setHasNoUnsignedWrap(false);
474       }
475       return I;
476     }
477 
478     // If we are known to be adding/subtracting zeros to every bit below
479     // the highest demanded bit, we just return the other side.
480     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
481       return I->getOperand(0);
482     // We can't do this with the LHS for subtraction, unless we are only
483     // demanding the LSB.
484     if ((I->getOpcode() == Instruction::Add ||
485          DemandedFromOps.isOneValue()) &&
486         DemandedFromOps.isSubsetOf(LHSKnown.Zero))
487       return I->getOperand(1);
488 
489     // Otherwise just compute the known bits of the result.
490     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
491     Known = KnownBits::computeForAddSub(I->getOpcode() == Instruction::Add,
492                                         NSW, LHSKnown, RHSKnown);
493     break;
494   }
495   case Instruction::Shl: {
496     const APInt *SA;
497     if (match(I->getOperand(1), m_APInt(SA))) {
498       const APInt *ShrAmt;
499       if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt))))
500         if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0)))
501           if (Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
502                                                     DemandedMask, Known))
503             return R;
504 
505       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
506       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
507 
508       // If the shift is NUW/NSW, then it does demand the high bits.
509       ShlOperator *IOp = cast<ShlOperator>(I);
510       if (IOp->hasNoSignedWrap())
511         DemandedMaskIn.setHighBits(ShiftAmt+1);
512       else if (IOp->hasNoUnsignedWrap())
513         DemandedMaskIn.setHighBits(ShiftAmt);
514 
515       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
516         return I;
517       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
518       Known.Zero <<= ShiftAmt;
519       Known.One  <<= ShiftAmt;
520       // low bits known zero.
521       if (ShiftAmt)
522         Known.Zero.setLowBits(ShiftAmt);
523     }
524     break;
525   }
526   case Instruction::LShr: {
527     const APInt *SA;
528     if (match(I->getOperand(1), m_APInt(SA))) {
529       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
530 
531       // Unsigned shift right.
532       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
533 
534       // If the shift is exact, then it does demand the low bits (and knows that
535       // they are zero).
536       if (cast<LShrOperator>(I)->isExact())
537         DemandedMaskIn.setLowBits(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       Known.Zero.lshrInPlace(ShiftAmt);
543       Known.One.lshrInPlace(ShiftAmt);
544       if (ShiftAmt)
545         Known.Zero.setHighBits(ShiftAmt);  // high bits known zero.
546     }
547     break;
548   }
549   case Instruction::AShr: {
550     // If this is an arithmetic shift right and only the low-bit is set, we can
551     // always convert this into a logical shr, even if the shift amount is
552     // variable.  The low bit of the shift cannot be an input sign bit unless
553     // the shift amount is >= the size of the datatype, which is undefined.
554     if (DemandedMask.isOneValue()) {
555       // Perform the logical shift right.
556       Instruction *NewVal = BinaryOperator::CreateLShr(
557                         I->getOperand(0), I->getOperand(1), I->getName());
558       return InsertNewInstWith(NewVal, *I);
559     }
560 
561     // If the sign bit is the only bit demanded by this ashr, then there is no
562     // need to do it, the shift doesn't change the high bit.
563     if (DemandedMask.isSignMask())
564       return I->getOperand(0);
565 
566     const APInt *SA;
567     if (match(I->getOperand(1), m_APInt(SA))) {
568       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
569 
570       // Signed shift right.
571       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
572       // If any of the high bits are demanded, we should set the sign bit as
573       // demanded.
574       if (DemandedMask.countLeadingZeros() <= ShiftAmt)
575         DemandedMaskIn.setSignBit();
576 
577       // If the shift is exact, then it does demand the low bits (and knows that
578       // they are zero).
579       if (cast<AShrOperator>(I)->isExact())
580         DemandedMaskIn.setLowBits(ShiftAmt);
581 
582       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
583         return I;
584 
585       unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI);
586 
587       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
588       // Compute the new bits that are at the top now plus sign bits.
589       APInt HighBits(APInt::getHighBitsSet(
590           BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
591       Known.Zero.lshrInPlace(ShiftAmt);
592       Known.One.lshrInPlace(ShiftAmt);
593 
594       // If the input sign bit is known to be zero, or if none of the top bits
595       // are demanded, turn this into an unsigned shift right.
596       assert(BitWidth > ShiftAmt && "Shift amount not saturated?");
597       if (Known.Zero[BitWidth-ShiftAmt-1] ||
598           !DemandedMask.intersects(HighBits)) {
599         BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
600                                                           I->getOperand(1));
601         LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
602         return InsertNewInstWith(LShr, *I);
603       } else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
604         Known.One |= HighBits;
605       }
606     }
607     break;
608   }
609   case Instruction::UDiv: {
610     // UDiv doesn't demand low bits that are zero in the divisor.
611     const APInt *SA;
612     if (match(I->getOperand(1), m_APInt(SA))) {
613       // If the shift is exact, then it does demand the low bits.
614       if (cast<UDivOperator>(I)->isExact())
615         break;
616 
617       // FIXME: Take the demanded mask of the result into account.
618       unsigned RHSTrailingZeros = SA->countTrailingZeros();
619       APInt DemandedMaskIn =
620           APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
621       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1))
622         return I;
623 
624       // Propagate zero bits from the input.
625       Known.Zero.setHighBits(std::min(
626           BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
627     }
628     break;
629   }
630   case Instruction::SRem:
631     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
632       // X % -1 demands all the bits because we don't want to introduce
633       // INT_MIN % -1 (== undef) by accident.
634       if (Rem->isMinusOne())
635         break;
636       APInt RA = Rem->getValue().abs();
637       if (RA.isPowerOf2()) {
638         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
639           return I->getOperand(0);
640 
641         APInt LowBits = RA - 1;
642         APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
643         if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
644           return I;
645 
646         // The low bits of LHS are unchanged by the srem.
647         Known.Zero = LHSKnown.Zero & LowBits;
648         Known.One = LHSKnown.One & LowBits;
649 
650         // If LHS is non-negative or has all low bits zero, then the upper bits
651         // are all zero.
652         if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
653           Known.Zero |= ~LowBits;
654 
655         // If LHS is negative and not all low bits are zero, then the upper bits
656         // are all one.
657         if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
658           Known.One |= ~LowBits;
659 
660         assert(!Known.hasConflict() && "Bits known to be one AND zero?");
661         break;
662       }
663     }
664 
665     // The sign bit is the LHS's sign bit, except when the result of the
666     // remainder is zero.
667     if (DemandedMask.isSignBitSet()) {
668       computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
669       // If it's known zero, our sign bit is also zero.
670       if (LHSKnown.isNonNegative())
671         Known.makeNonNegative();
672     }
673     break;
674   case Instruction::URem: {
675     KnownBits Known2(BitWidth);
676     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
677     if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
678         SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
679       return I;
680 
681     unsigned Leaders = Known2.countMinLeadingZeros();
682     Known.Zero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
683     break;
684   }
685   case Instruction::Call:
686     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
687       switch (II->getIntrinsicID()) {
688       default: break;
689       case Intrinsic::bswap: {
690         // If the only bits demanded come from one byte of the bswap result,
691         // just shift the input byte into position to eliminate the bswap.
692         unsigned NLZ = DemandedMask.countLeadingZeros();
693         unsigned NTZ = DemandedMask.countTrailingZeros();
694 
695         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
696         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
697         // have 14 leading zeros, round to 8.
698         NLZ &= ~7;
699         NTZ &= ~7;
700         // If we need exactly one byte, we can do this transformation.
701         if (BitWidth-NLZ-NTZ == 8) {
702           unsigned ResultBit = NTZ;
703           unsigned InputBit = BitWidth-NTZ-8;
704 
705           // Replace this with either a left or right shift to get the byte into
706           // the right place.
707           Instruction *NewVal;
708           if (InputBit > ResultBit)
709             NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
710                     ConstantInt::get(I->getType(), InputBit-ResultBit));
711           else
712             NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
713                     ConstantInt::get(I->getType(), ResultBit-InputBit));
714           NewVal->takeName(I);
715           return InsertNewInstWith(NewVal, *I);
716         }
717 
718         // TODO: Could compute known zero/one bits based on the input.
719         break;
720       }
721       case Intrinsic::fshr:
722       case Intrinsic::fshl: {
723         const APInt *SA;
724         if (!match(I->getOperand(2), m_APInt(SA)))
725           break;
726 
727         // Normalize to funnel shift left. APInt shifts of BitWidth are well-
728         // defined, so no need to special-case zero shifts here.
729         uint64_t ShiftAmt = SA->urem(BitWidth);
730         if (II->getIntrinsicID() == Intrinsic::fshr)
731           ShiftAmt = BitWidth - ShiftAmt;
732 
733         APInt DemandedMaskLHS(DemandedMask.lshr(ShiftAmt));
734         APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt));
735         if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, Depth + 1) ||
736             SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1))
737           return I;
738 
739         Known.Zero = LHSKnown.Zero.shl(ShiftAmt) |
740                      RHSKnown.Zero.lshr(BitWidth - ShiftAmt);
741         Known.One = LHSKnown.One.shl(ShiftAmt) |
742                     RHSKnown.One.lshr(BitWidth - ShiftAmt);
743         break;
744       }
745       case Intrinsic::x86_mmx_pmovmskb:
746       case Intrinsic::x86_sse_movmsk_ps:
747       case Intrinsic::x86_sse2_movmsk_pd:
748       case Intrinsic::x86_sse2_pmovmskb_128:
749       case Intrinsic::x86_avx_movmsk_ps_256:
750       case Intrinsic::x86_avx_movmsk_pd_256:
751       case Intrinsic::x86_avx2_pmovmskb: {
752         // MOVMSK copies the vector elements' sign bits to the low bits
753         // and zeros the high bits.
754         unsigned ArgWidth;
755         if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
756           ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
757         } else {
758           auto Arg = II->getArgOperand(0);
759           auto ArgType = cast<VectorType>(Arg->getType());
760           ArgWidth = ArgType->getNumElements();
761         }
762 
763         // If we don't need any of low bits then return zero,
764         // we know that DemandedMask is non-zero already.
765         APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
766         if (DemandedElts.isNullValue())
767           return ConstantInt::getNullValue(VTy);
768 
769         // We know that the upper bits are set to zero.
770         Known.Zero.setBitsFrom(ArgWidth);
771         return nullptr;
772       }
773       case Intrinsic::x86_sse42_crc32_64_64:
774         Known.Zero.setBitsFrom(32);
775         return nullptr;
776       }
777     }
778     computeKnownBits(V, Known, Depth, CxtI);
779     break;
780   }
781 
782   // If the client is only demanding bits that we know, return the known
783   // constant.
784   if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
785     return Constant::getIntegerValue(VTy, Known.One);
786   return nullptr;
787 }
788 
789 /// Helper routine of SimplifyDemandedUseBits. It computes Known
790 /// bits. It also tries to handle simplifications that can be done based on
791 /// DemandedMask, but without modifying the Instruction.
792 Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
793                                                      const APInt &DemandedMask,
794                                                      KnownBits &Known,
795                                                      unsigned Depth,
796                                                      Instruction *CxtI) {
797   unsigned BitWidth = DemandedMask.getBitWidth();
798   Type *ITy = I->getType();
799 
800   KnownBits LHSKnown(BitWidth);
801   KnownBits RHSKnown(BitWidth);
802 
803   // Despite the fact that we can't simplify this instruction in all User's
804   // context, we can at least compute the known bits, and we can
805   // do simplifications that apply to *just* the one user if we know that
806   // this instruction has a simpler value in that context.
807   switch (I->getOpcode()) {
808   case Instruction::And: {
809     // If either the LHS or the RHS are Zero, the result is zero.
810     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
811     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
812                      CxtI);
813 
814     // Output known-0 are known to be clear if zero in either the LHS | RHS.
815     APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
816     // Output known-1 bits are only known if set in both the LHS & RHS.
817     APInt IKnownOne = RHSKnown.One & LHSKnown.One;
818 
819     // If the client is only demanding bits that we know, return the known
820     // constant.
821     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
822       return Constant::getIntegerValue(ITy, IKnownOne);
823 
824     // If all of the demanded bits are known 1 on one side, return the other.
825     // These bits cannot contribute to the result of the 'and' in this
826     // context.
827     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
828       return I->getOperand(0);
829     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
830       return I->getOperand(1);
831 
832     Known.Zero = std::move(IKnownZero);
833     Known.One  = std::move(IKnownOne);
834     break;
835   }
836   case Instruction::Or: {
837     // We can simplify (X|Y) -> X or Y in the user's context if we know that
838     // only bits from X or Y are demanded.
839 
840     // If either the LHS or the RHS are One, the result is One.
841     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
842     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
843                      CxtI);
844 
845     // Output known-0 bits are only known if clear in both the LHS & RHS.
846     APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
847     // Output known-1 are known to be set if set in either the LHS | RHS.
848     APInt IKnownOne = RHSKnown.One | LHSKnown.One;
849 
850     // If the client is only demanding bits that we know, return the known
851     // constant.
852     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
853       return Constant::getIntegerValue(ITy, IKnownOne);
854 
855     // If all of the demanded bits are known zero on one side, return the
856     // other.  These bits cannot contribute to the result of the 'or' in this
857     // context.
858     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
859       return I->getOperand(0);
860     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
861       return I->getOperand(1);
862 
863     Known.Zero = std::move(IKnownZero);
864     Known.One  = std::move(IKnownOne);
865     break;
866   }
867   case Instruction::Xor: {
868     // We can simplify (X^Y) -> X or Y in the user's context if we know that
869     // only bits from X or Y are demanded.
870 
871     computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
872     computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
873                      CxtI);
874 
875     // Output known-0 bits are known if clear or set in both the LHS & RHS.
876     APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
877                        (RHSKnown.One & LHSKnown.One);
878     // Output known-1 are known to be set if set in only one of the LHS, RHS.
879     APInt IKnownOne =  (RHSKnown.Zero & LHSKnown.One) |
880                        (RHSKnown.One & LHSKnown.Zero);
881 
882     // If the client is only demanding bits that we know, return the known
883     // constant.
884     if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
885       return Constant::getIntegerValue(ITy, IKnownOne);
886 
887     // If all of the demanded bits are known zero on one side, return the
888     // other.
889     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
890       return I->getOperand(0);
891     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
892       return I->getOperand(1);
893 
894     // Output known-0 bits are known if clear or set in both the LHS & RHS.
895     Known.Zero = std::move(IKnownZero);
896     // Output known-1 are known to be set if set in only one of the LHS, RHS.
897     Known.One  = std::move(IKnownOne);
898     break;
899   }
900   default:
901     // Compute the Known bits to simplify things downstream.
902     computeKnownBits(I, Known, Depth, CxtI);
903 
904     // If this user is only demanding bits that we know, return the known
905     // constant.
906     if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
907       return Constant::getIntegerValue(ITy, Known.One);
908 
909     break;
910   }
911 
912   return nullptr;
913 }
914 
915 
916 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify
917 /// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
918 /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
919 /// of "C2-C1".
920 ///
921 /// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
922 /// ..., bn}, without considering the specific value X is holding.
923 /// This transformation is legal iff one of following conditions is hold:
924 ///  1) All the bit in S are 0, in this case E1 == E2.
925 ///  2) We don't care those bits in S, per the input DemandedMask.
926 ///  3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
927 ///     rest bits.
928 ///
929 /// Currently we only test condition 2).
930 ///
931 /// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
932 /// not successful.
933 Value *
934 InstCombiner::simplifyShrShlDemandedBits(Instruction *Shr, const APInt &ShrOp1,
935                                          Instruction *Shl, const APInt &ShlOp1,
936                                          const APInt &DemandedMask,
937                                          KnownBits &Known) {
938   if (!ShlOp1 || !ShrOp1)
939     return nullptr; // No-op.
940 
941   Value *VarX = Shr->getOperand(0);
942   Type *Ty = VarX->getType();
943   unsigned BitWidth = Ty->getScalarSizeInBits();
944   if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
945     return nullptr; // Undef.
946 
947   unsigned ShlAmt = ShlOp1.getZExtValue();
948   unsigned ShrAmt = ShrOp1.getZExtValue();
949 
950   Known.One.clearAllBits();
951   Known.Zero.setLowBits(ShlAmt - 1);
952   Known.Zero &= DemandedMask;
953 
954   APInt BitMask1(APInt::getAllOnesValue(BitWidth));
955   APInt BitMask2(APInt::getAllOnesValue(BitWidth));
956 
957   bool isLshr = (Shr->getOpcode() == Instruction::LShr);
958   BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
959                       (BitMask1.ashr(ShrAmt) << ShlAmt);
960 
961   if (ShrAmt <= ShlAmt) {
962     BitMask2 <<= (ShlAmt - ShrAmt);
963   } else {
964     BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
965                         BitMask2.ashr(ShrAmt - ShlAmt);
966   }
967 
968   // Check if condition-2 (see the comment to this function) is satified.
969   if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
970     if (ShrAmt == ShlAmt)
971       return VarX;
972 
973     if (!Shr->hasOneUse())
974       return nullptr;
975 
976     BinaryOperator *New;
977     if (ShrAmt < ShlAmt) {
978       Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
979       New = BinaryOperator::CreateShl(VarX, Amt);
980       BinaryOperator *Orig = cast<BinaryOperator>(Shl);
981       New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
982       New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
983     } else {
984       Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
985       New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
986                      BinaryOperator::CreateAShr(VarX, Amt);
987       if (cast<BinaryOperator>(Shr)->isExact())
988         New->setIsExact(true);
989     }
990 
991     return InsertNewInstWith(New, *Shl);
992   }
993 
994   return nullptr;
995 }
996 
997 /// Implement SimplifyDemandedVectorElts for amdgcn buffer and image intrinsics.
998 ///
999 /// Note: This only supports non-TFE/LWE image intrinsic calls; those have
1000 ///       struct returns.
1001 Value *InstCombiner::simplifyAMDGCNMemoryIntrinsicDemanded(IntrinsicInst *II,
1002                                                            APInt DemandedElts,
1003                                                            int DMaskIdx) {
1004 
1005   // FIXME: Allow v3i16/v3f16 in buffer intrinsics when the types are fully supported.
1006   if (DMaskIdx < 0 &&
1007       II->getType()->getScalarSizeInBits() != 32 &&
1008       DemandedElts.getActiveBits() == 3)
1009     return nullptr;
1010 
1011   unsigned VWidth = II->getType()->getVectorNumElements();
1012   if (VWidth == 1)
1013     return nullptr;
1014 
1015   IRBuilderBase::InsertPointGuard Guard(Builder);
1016   Builder.SetInsertPoint(II);
1017 
1018   // Assume the arguments are unchanged and later override them, if needed.
1019   SmallVector<Value *, 16> Args(II->arg_begin(), II->arg_end());
1020 
1021   if (DMaskIdx < 0) {
1022     // Buffer case.
1023 
1024     const unsigned ActiveBits = DemandedElts.getActiveBits();
1025     const unsigned UnusedComponentsAtFront = DemandedElts.countTrailingZeros();
1026 
1027     // Start assuming the prefix of elements is demanded, but possibly clear
1028     // some other bits if there are trailing zeros (unused components at front)
1029     // and update offset.
1030     DemandedElts = (1 << ActiveBits) - 1;
1031 
1032     if (UnusedComponentsAtFront > 0) {
1033       static const unsigned InvalidOffsetIdx = 0xf;
1034 
1035       unsigned OffsetIdx;
1036       switch (II->getIntrinsicID()) {
1037       case Intrinsic::amdgcn_raw_buffer_load:
1038         OffsetIdx = 1;
1039         break;
1040       case Intrinsic::amdgcn_s_buffer_load:
1041         // If resulting type is vec3, there is no point in trimming the
1042         // load with updated offset, as the vec3 would most likely be widened to
1043         // vec4 anyway during lowering.
1044         if (ActiveBits == 4 && UnusedComponentsAtFront == 1)
1045           OffsetIdx = InvalidOffsetIdx;
1046         else
1047           OffsetIdx = 1;
1048         break;
1049       case Intrinsic::amdgcn_struct_buffer_load:
1050         OffsetIdx = 2;
1051         break;
1052       default:
1053         // TODO: handle tbuffer* intrinsics.
1054         OffsetIdx = InvalidOffsetIdx;
1055         break;
1056       }
1057 
1058       if (OffsetIdx != InvalidOffsetIdx) {
1059         // Clear demanded bits and update the offset.
1060         DemandedElts &= ~((1 << UnusedComponentsAtFront) - 1);
1061         auto *Offset = II->getArgOperand(OffsetIdx);
1062         unsigned SingleComponentSizeInBits =
1063             getDataLayout().getTypeSizeInBits(II->getType()->getScalarType());
1064         unsigned OffsetAdd =
1065             UnusedComponentsAtFront * SingleComponentSizeInBits / 8;
1066         auto *OffsetAddVal = ConstantInt::get(Offset->getType(), OffsetAdd);
1067         Args[OffsetIdx] = Builder.CreateAdd(Offset, OffsetAddVal);
1068       }
1069     }
1070   } else {
1071     // Image case.
1072 
1073     ConstantInt *DMask = cast<ConstantInt>(II->getArgOperand(DMaskIdx));
1074     unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1075 
1076     // Mask off values that are undefined because the dmask doesn't cover them
1077     DemandedElts &= (1 << countPopulation(DMaskVal)) - 1;
1078 
1079     unsigned NewDMaskVal = 0;
1080     unsigned OrigLoadIdx = 0;
1081     for (unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) {
1082       const unsigned Bit = 1 << SrcIdx;
1083       if (!!(DMaskVal & Bit)) {
1084         if (!!DemandedElts[OrigLoadIdx])
1085           NewDMaskVal |= Bit;
1086         OrigLoadIdx++;
1087       }
1088     }
1089 
1090     if (DMaskVal != NewDMaskVal)
1091       Args[DMaskIdx] = ConstantInt::get(DMask->getType(), NewDMaskVal);
1092   }
1093 
1094   unsigned NewNumElts = DemandedElts.countPopulation();
1095   if (!NewNumElts)
1096     return UndefValue::get(II->getType());
1097 
1098   if (NewNumElts >= VWidth && DemandedElts.isMask()) {
1099     if (DMaskIdx >= 0)
1100       II->setArgOperand(DMaskIdx, Args[DMaskIdx]);
1101     return nullptr;
1102   }
1103 
1104   // Determine the overload types of the original intrinsic.
1105   auto IID = II->getIntrinsicID();
1106   SmallVector<Intrinsic::IITDescriptor, 16> Table;
1107   getIntrinsicInfoTableEntries(IID, Table);
1108   ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1109 
1110   // Validate function argument and return types, extracting overloaded types
1111   // along the way.
1112   FunctionType *FTy = II->getCalledFunction()->getFunctionType();
1113   SmallVector<Type *, 6> OverloadTys;
1114   Intrinsic::matchIntrinsicSignature(FTy, TableRef, OverloadTys);
1115 
1116   Module *M = II->getParent()->getParent()->getParent();
1117   Type *EltTy = II->getType()->getVectorElementType();
1118   Type *NewTy = (NewNumElts == 1) ? EltTy : VectorType::get(EltTy, NewNumElts);
1119 
1120   OverloadTys[0] = NewTy;
1121   Function *NewIntrin = Intrinsic::getDeclaration(M, IID, OverloadTys);
1122 
1123   CallInst *NewCall = Builder.CreateCall(NewIntrin, Args);
1124   NewCall->takeName(II);
1125   NewCall->copyMetadata(*II);
1126 
1127   if (NewNumElts == 1) {
1128     return Builder.CreateInsertElement(UndefValue::get(II->getType()), NewCall,
1129                                        DemandedElts.countTrailingZeros());
1130   }
1131 
1132   SmallVector<uint32_t, 8> EltMask;
1133   unsigned NewLoadIdx = 0;
1134   for (unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) {
1135     if (!!DemandedElts[OrigLoadIdx])
1136       EltMask.push_back(NewLoadIdx++);
1137     else
1138       EltMask.push_back(NewNumElts);
1139   }
1140 
1141   Value *Shuffle =
1142       Builder.CreateShuffleVector(NewCall, UndefValue::get(NewTy), EltMask);
1143 
1144   return Shuffle;
1145 }
1146 
1147 /// The specified value produces a vector with any number of elements.
1148 /// This method analyzes which elements of the operand are undef and returns
1149 /// that information in UndefElts.
1150 ///
1151 /// DemandedElts contains the set of elements that are actually used by the
1152 /// caller, and by default (AllowMultipleUsers equals false) the value is
1153 /// simplified only if it has a single caller. If AllowMultipleUsers is set
1154 /// to true, DemandedElts refers to the union of sets of elements that are
1155 /// used by all callers.
1156 ///
1157 /// If the information about demanded elements can be used to simplify the
1158 /// operation, the operation is simplified, then the resultant value is
1159 /// returned.  This returns null if no change was made.
1160 Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
1161                                                 APInt &UndefElts,
1162                                                 unsigned Depth,
1163                                                 bool AllowMultipleUsers) {
1164   unsigned VWidth = V->getType()->getVectorNumElements();
1165   APInt EltMask(APInt::getAllOnesValue(VWidth));
1166   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
1167 
1168   if (isa<UndefValue>(V)) {
1169     // If the entire vector is undefined, just return this info.
1170     UndefElts = EltMask;
1171     return nullptr;
1172   }
1173 
1174   if (DemandedElts.isNullValue()) { // If nothing is demanded, provide undef.
1175     UndefElts = EltMask;
1176     return UndefValue::get(V->getType());
1177   }
1178 
1179   UndefElts = 0;
1180 
1181   if (auto *C = dyn_cast<Constant>(V)) {
1182     // Check if this is identity. If so, return 0 since we are not simplifying
1183     // anything.
1184     if (DemandedElts.isAllOnesValue())
1185       return nullptr;
1186 
1187     Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1188     Constant *Undef = UndefValue::get(EltTy);
1189     SmallVector<Constant*, 16> Elts;
1190     for (unsigned i = 0; i != VWidth; ++i) {
1191       if (!DemandedElts[i]) {   // If not demanded, set to undef.
1192         Elts.push_back(Undef);
1193         UndefElts.setBit(i);
1194         continue;
1195       }
1196 
1197       Constant *Elt = C->getAggregateElement(i);
1198       if (!Elt) return nullptr;
1199 
1200       if (isa<UndefValue>(Elt)) {   // Already undef.
1201         Elts.push_back(Undef);
1202         UndefElts.setBit(i);
1203       } else {                               // Otherwise, defined.
1204         Elts.push_back(Elt);
1205       }
1206     }
1207 
1208     // If we changed the constant, return it.
1209     Constant *NewCV = ConstantVector::get(Elts);
1210     return NewCV != C ? NewCV : nullptr;
1211   }
1212 
1213   // Limit search depth.
1214   if (Depth == 10)
1215     return nullptr;
1216 
1217   if (!AllowMultipleUsers) {
1218     // If multiple users are using the root value, proceed with
1219     // simplification conservatively assuming that all elements
1220     // are needed.
1221     if (!V->hasOneUse()) {
1222       // Quit if we find multiple users of a non-root value though.
1223       // They'll be handled when it's their turn to be visited by
1224       // the main instcombine process.
1225       if (Depth != 0)
1226         // TODO: Just compute the UndefElts information recursively.
1227         return nullptr;
1228 
1229       // Conservatively assume that all elements are needed.
1230       DemandedElts = EltMask;
1231     }
1232   }
1233 
1234   Instruction *I = dyn_cast<Instruction>(V);
1235   if (!I) return nullptr;        // Only analyze instructions.
1236 
1237   bool MadeChange = false;
1238   auto simplifyAndSetOp = [&](Instruction *Inst, unsigned OpNum,
1239                               APInt Demanded, APInt &Undef) {
1240     auto *II = dyn_cast<IntrinsicInst>(Inst);
1241     Value *Op = II ? II->getArgOperand(OpNum) : Inst->getOperand(OpNum);
1242     if (Value *V = SimplifyDemandedVectorElts(Op, Demanded, Undef, Depth + 1)) {
1243       if (II)
1244         II->setArgOperand(OpNum, V);
1245       else
1246         Inst->setOperand(OpNum, V);
1247       MadeChange = true;
1248     }
1249   };
1250 
1251   APInt UndefElts2(VWidth, 0);
1252   APInt UndefElts3(VWidth, 0);
1253   switch (I->getOpcode()) {
1254   default: break;
1255 
1256   case Instruction::GetElementPtr: {
1257     // The LangRef requires that struct geps have all constant indices.  As
1258     // such, we can't convert any operand to partial undef.
1259     auto mayIndexStructType = [](GetElementPtrInst &GEP) {
1260       for (auto I = gep_type_begin(GEP), E = gep_type_end(GEP);
1261            I != E; I++)
1262         if (I.isStruct())
1263           return true;;
1264       return false;
1265     };
1266     if (mayIndexStructType(cast<GetElementPtrInst>(*I)))
1267       break;
1268 
1269     // Conservatively track the demanded elements back through any vector
1270     // operands we may have.  We know there must be at least one, or we
1271     // wouldn't have a vector result to get here. Note that we intentionally
1272     // merge the undef bits here since gepping with either an undef base or
1273     // index results in undef.
1274     for (unsigned i = 0; i < I->getNumOperands(); i++) {
1275       if (isa<UndefValue>(I->getOperand(i))) {
1276         // If the entire vector is undefined, just return this info.
1277         UndefElts = EltMask;
1278         return nullptr;
1279       }
1280       if (I->getOperand(i)->getType()->isVectorTy()) {
1281         APInt UndefEltsOp(VWidth, 0);
1282         simplifyAndSetOp(I, i, DemandedElts, UndefEltsOp);
1283         UndefElts |= UndefEltsOp;
1284       }
1285     }
1286 
1287     break;
1288   }
1289   case Instruction::InsertElement: {
1290     // If this is a variable index, we don't know which element it overwrites.
1291     // demand exactly the same input as we produce.
1292     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1293     if (!Idx) {
1294       // Note that we can't propagate undef elt info, because we don't know
1295       // which elt is getting updated.
1296       simplifyAndSetOp(I, 0, DemandedElts, UndefElts2);
1297       break;
1298     }
1299 
1300     // The element inserted overwrites whatever was there, so the input demanded
1301     // set is simpler than the output set.
1302     unsigned IdxNo = Idx->getZExtValue();
1303     APInt PreInsertDemandedElts = DemandedElts;
1304     if (IdxNo < VWidth)
1305       PreInsertDemandedElts.clearBit(IdxNo);
1306 
1307     simplifyAndSetOp(I, 0, PreInsertDemandedElts, UndefElts);
1308 
1309     // If this is inserting an element that isn't demanded, remove this
1310     // insertelement.
1311     if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1312       Worklist.push(I);
1313       return I->getOperand(0);
1314     }
1315 
1316     // The inserted element is defined.
1317     UndefElts.clearBit(IdxNo);
1318     break;
1319   }
1320   case Instruction::ShuffleVector: {
1321     auto *Shuffle = cast<ShuffleVectorInst>(I);
1322     assert(Shuffle->getOperand(0)->getType() ==
1323            Shuffle->getOperand(1)->getType() &&
1324            "Expected shuffle operands to have same type");
1325     unsigned OpWidth =
1326         Shuffle->getOperand(0)->getType()->getVectorNumElements();
1327     APInt LeftDemanded(OpWidth, 0), RightDemanded(OpWidth, 0);
1328     for (unsigned i = 0; i < VWidth; i++) {
1329       if (DemandedElts[i]) {
1330         unsigned MaskVal = Shuffle->getMaskValue(i);
1331         if (MaskVal != -1u) {
1332           assert(MaskVal < OpWidth * 2 &&
1333                  "shufflevector mask index out of range!");
1334           if (MaskVal < OpWidth)
1335             LeftDemanded.setBit(MaskVal);
1336           else
1337             RightDemanded.setBit(MaskVal - OpWidth);
1338         }
1339       }
1340     }
1341 
1342     APInt LHSUndefElts(OpWidth, 0);
1343     simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts);
1344 
1345     APInt RHSUndefElts(OpWidth, 0);
1346     simplifyAndSetOp(I, 1, RightDemanded, RHSUndefElts);
1347 
1348     // If this shuffle does not change the vector length and the elements
1349     // demanded by this shuffle are an identity mask, then this shuffle is
1350     // unnecessary.
1351     //
1352     // We are assuming canonical form for the mask, so the source vector is
1353     // operand 0 and operand 1 is not used.
1354     //
1355     // Note that if an element is demanded and this shuffle mask is undefined
1356     // for that element, then the shuffle is not considered an identity
1357     // operation. The shuffle prevents poison from the operand vector from
1358     // leaking to the result by replacing poison with an undefined value.
1359     if (VWidth == OpWidth) {
1360       bool IsIdentityShuffle = true;
1361       for (unsigned i = 0; i < VWidth; i++) {
1362         unsigned MaskVal = Shuffle->getMaskValue(i);
1363         if (DemandedElts[i] && i != MaskVal) {
1364           IsIdentityShuffle = false;
1365           break;
1366         }
1367       }
1368       if (IsIdentityShuffle)
1369         return Shuffle->getOperand(0);
1370     }
1371 
1372     bool NewUndefElts = false;
1373     unsigned LHSIdx = -1u, LHSValIdx = -1u;
1374     unsigned RHSIdx = -1u, RHSValIdx = -1u;
1375     bool LHSUniform = true;
1376     bool RHSUniform = true;
1377     for (unsigned i = 0; i < VWidth; i++) {
1378       unsigned MaskVal = Shuffle->getMaskValue(i);
1379       if (MaskVal == -1u) {
1380         UndefElts.setBit(i);
1381       } else if (!DemandedElts[i]) {
1382         NewUndefElts = true;
1383         UndefElts.setBit(i);
1384       } else if (MaskVal < OpWidth) {
1385         if (LHSUndefElts[MaskVal]) {
1386           NewUndefElts = true;
1387           UndefElts.setBit(i);
1388         } else {
1389           LHSIdx = LHSIdx == -1u ? i : OpWidth;
1390           LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth;
1391           LHSUniform = LHSUniform && (MaskVal == i);
1392         }
1393       } else {
1394         if (RHSUndefElts[MaskVal - OpWidth]) {
1395           NewUndefElts = true;
1396           UndefElts.setBit(i);
1397         } else {
1398           RHSIdx = RHSIdx == -1u ? i : OpWidth;
1399           RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth;
1400           RHSUniform = RHSUniform && (MaskVal - OpWidth == i);
1401         }
1402       }
1403     }
1404 
1405     // Try to transform shuffle with constant vector and single element from
1406     // this constant vector to single insertelement instruction.
1407     // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1408     // insertelement V, C[ci], ci-n
1409     if (OpWidth == Shuffle->getType()->getNumElements()) {
1410       Value *Op = nullptr;
1411       Constant *Value = nullptr;
1412       unsigned Idx = -1u;
1413 
1414       // Find constant vector with the single element in shuffle (LHS or RHS).
1415       if (LHSIdx < OpWidth && RHSUniform) {
1416         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1417           Op = Shuffle->getOperand(1);
1418           Value = CV->getOperand(LHSValIdx);
1419           Idx = LHSIdx;
1420         }
1421       }
1422       if (RHSIdx < OpWidth && LHSUniform) {
1423         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1424           Op = Shuffle->getOperand(0);
1425           Value = CV->getOperand(RHSValIdx);
1426           Idx = RHSIdx;
1427         }
1428       }
1429       // Found constant vector with single element - convert to insertelement.
1430       if (Op && Value) {
1431         Instruction *New = InsertElementInst::Create(
1432             Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1433             Shuffle->getName());
1434         InsertNewInstWith(New, *Shuffle);
1435         return New;
1436       }
1437     }
1438     if (NewUndefElts) {
1439       // Add additional discovered undefs.
1440       SmallVector<Constant*, 16> Elts;
1441       for (unsigned i = 0; i < VWidth; ++i) {
1442         if (UndefElts[i])
1443           Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1444         else
1445           Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1446                                           Shuffle->getMaskValue(i)));
1447       }
1448       I->setOperand(2, ConstantVector::get(Elts));
1449       MadeChange = true;
1450     }
1451     break;
1452   }
1453   case Instruction::Select: {
1454     // If this is a vector select, try to transform the select condition based
1455     // on the current demanded elements.
1456     SelectInst *Sel = cast<SelectInst>(I);
1457     if (Sel->getCondition()->getType()->isVectorTy()) {
1458       // TODO: We are not doing anything with UndefElts based on this call.
1459       // It is overwritten below based on the other select operands. If an
1460       // element of the select condition is known undef, then we are free to
1461       // choose the output value from either arm of the select. If we know that
1462       // one of those values is undef, then the output can be undef.
1463       simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1464     }
1465 
1466     // Next, see if we can transform the arms of the select.
1467     APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
1468     if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
1469       for (unsigned i = 0; i < VWidth; i++) {
1470         // isNullValue() always returns false when called on a ConstantExpr.
1471         // Skip constant expressions to avoid propagating incorrect information.
1472         Constant *CElt = CV->getAggregateElement(i);
1473         if (isa<ConstantExpr>(CElt))
1474           continue;
1475         // TODO: If a select condition element is undef, we can demand from
1476         // either side. If one side is known undef, choosing that side would
1477         // propagate undef.
1478         if (CElt->isNullValue())
1479           DemandedLHS.clearBit(i);
1480         else
1481           DemandedRHS.clearBit(i);
1482       }
1483     }
1484 
1485     simplifyAndSetOp(I, 1, DemandedLHS, UndefElts2);
1486     simplifyAndSetOp(I, 2, DemandedRHS, UndefElts3);
1487 
1488     // Output elements are undefined if the element from each arm is undefined.
1489     // TODO: This can be improved. See comment in select condition handling.
1490     UndefElts = UndefElts2 & UndefElts3;
1491     break;
1492   }
1493   case Instruction::BitCast: {
1494     // Vector->vector casts only.
1495     VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1496     if (!VTy) break;
1497     unsigned InVWidth = VTy->getNumElements();
1498     APInt InputDemandedElts(InVWidth, 0);
1499     UndefElts2 = APInt(InVWidth, 0);
1500     unsigned Ratio;
1501 
1502     if (VWidth == InVWidth) {
1503       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1504       // elements as are demanded of us.
1505       Ratio = 1;
1506       InputDemandedElts = DemandedElts;
1507     } else if ((VWidth % InVWidth) == 0) {
1508       // If the number of elements in the output is a multiple of the number of
1509       // elements in the input then an input element is live if any of the
1510       // corresponding output elements are live.
1511       Ratio = VWidth / InVWidth;
1512       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1513         if (DemandedElts[OutIdx])
1514           InputDemandedElts.setBit(OutIdx / Ratio);
1515     } else if ((InVWidth % VWidth) == 0) {
1516       // If the number of elements in the input is a multiple of the number of
1517       // elements in the output then an input element is live if the
1518       // corresponding output element is live.
1519       Ratio = InVWidth / VWidth;
1520       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1521         if (DemandedElts[InIdx / Ratio])
1522           InputDemandedElts.setBit(InIdx);
1523     } else {
1524       // Unsupported so far.
1525       break;
1526     }
1527 
1528     simplifyAndSetOp(I, 0, InputDemandedElts, UndefElts2);
1529 
1530     if (VWidth == InVWidth) {
1531       UndefElts = UndefElts2;
1532     } else if ((VWidth % InVWidth) == 0) {
1533       // If the number of elements in the output is a multiple of the number of
1534       // elements in the input then an output element is undef if the
1535       // corresponding input element is undef.
1536       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1537         if (UndefElts2[OutIdx / Ratio])
1538           UndefElts.setBit(OutIdx);
1539     } else if ((InVWidth % VWidth) == 0) {
1540       // If the number of elements in the input is a multiple of the number of
1541       // elements in the output then an output element is undef if all of the
1542       // corresponding input elements are undef.
1543       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1544         APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1545         if (SubUndef.countPopulation() == Ratio)
1546           UndefElts.setBit(OutIdx);
1547       }
1548     } else {
1549       llvm_unreachable("Unimp");
1550     }
1551     break;
1552   }
1553   case Instruction::FPTrunc:
1554   case Instruction::FPExt:
1555     simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1556     break;
1557 
1558   case Instruction::Call: {
1559     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1560     if (!II) break;
1561     switch (II->getIntrinsicID()) {
1562     case Intrinsic::masked_gather: // fallthrough
1563     case Intrinsic::masked_load: {
1564       // Subtlety: If we load from a pointer, the pointer must be valid
1565       // regardless of whether the element is demanded.  Doing otherwise risks
1566       // segfaults which didn't exist in the original program.
1567       APInt DemandedPtrs(APInt::getAllOnesValue(VWidth)),
1568         DemandedPassThrough(DemandedElts);
1569       if (auto *CV = dyn_cast<ConstantVector>(II->getOperand(2)))
1570         for (unsigned i = 0; i < VWidth; i++) {
1571           Constant *CElt = CV->getAggregateElement(i);
1572           if (CElt->isNullValue())
1573             DemandedPtrs.clearBit(i);
1574           else if (CElt->isAllOnesValue())
1575             DemandedPassThrough.clearBit(i);
1576         }
1577       if (II->getIntrinsicID() == Intrinsic::masked_gather)
1578         simplifyAndSetOp(II, 0, DemandedPtrs, UndefElts2);
1579       simplifyAndSetOp(II, 3, DemandedPassThrough, UndefElts3);
1580 
1581       // Output elements are undefined if the element from both sources are.
1582       // TODO: can strengthen via mask as well.
1583       UndefElts = UndefElts2 & UndefElts3;
1584       break;
1585     }
1586     case Intrinsic::x86_xop_vfrcz_ss:
1587     case Intrinsic::x86_xop_vfrcz_sd:
1588       // The instructions for these intrinsics are speced to zero upper bits not
1589       // pass them through like other scalar intrinsics. So we shouldn't just
1590       // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1591       // Instead we should return a zero vector.
1592       if (!DemandedElts[0]) {
1593         Worklist.push(II);
1594         return ConstantAggregateZero::get(II->getType());
1595       }
1596 
1597       // Only the lower element is used.
1598       DemandedElts = 1;
1599       simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1600 
1601       // Only the lower element is undefined. The high elements are zero.
1602       UndefElts = UndefElts[0];
1603       break;
1604 
1605     // Unary scalar-as-vector operations that work column-wise.
1606     case Intrinsic::x86_sse_rcp_ss:
1607     case Intrinsic::x86_sse_rsqrt_ss:
1608       simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1609 
1610       // If lowest element of a scalar op isn't used then use Arg0.
1611       if (!DemandedElts[0]) {
1612         Worklist.push(II);
1613         return II->getArgOperand(0);
1614       }
1615       // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1616       // checks).
1617       break;
1618 
1619     // Binary scalar-as-vector operations that work column-wise. The high
1620     // elements come from operand 0. The low element is a function of both
1621     // operands.
1622     case Intrinsic::x86_sse_min_ss:
1623     case Intrinsic::x86_sse_max_ss:
1624     case Intrinsic::x86_sse_cmp_ss:
1625     case Intrinsic::x86_sse2_min_sd:
1626     case Intrinsic::x86_sse2_max_sd:
1627     case Intrinsic::x86_sse2_cmp_sd: {
1628       simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1629 
1630       // If lowest element of a scalar op isn't used then use Arg0.
1631       if (!DemandedElts[0]) {
1632         Worklist.push(II);
1633         return II->getArgOperand(0);
1634       }
1635 
1636       // Only lower element is used for operand 1.
1637       DemandedElts = 1;
1638       simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1639 
1640       // Lower element is undefined if both lower elements are undefined.
1641       // Consider things like undef&0.  The result is known zero, not undef.
1642       if (!UndefElts2[0])
1643         UndefElts.clearBit(0);
1644 
1645       break;
1646     }
1647 
1648     // Binary scalar-as-vector operations that work column-wise. The high
1649     // elements come from operand 0 and the low element comes from operand 1.
1650     case Intrinsic::x86_sse41_round_ss:
1651     case Intrinsic::x86_sse41_round_sd: {
1652       // Don't use the low element of operand 0.
1653       APInt DemandedElts2 = DemandedElts;
1654       DemandedElts2.clearBit(0);
1655       simplifyAndSetOp(II, 0, DemandedElts2, UndefElts);
1656 
1657       // If lowest element of a scalar op isn't used then use Arg0.
1658       if (!DemandedElts[0]) {
1659         Worklist.push(II);
1660         return II->getArgOperand(0);
1661       }
1662 
1663       // Only lower element is used for operand 1.
1664       DemandedElts = 1;
1665       simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1666 
1667       // Take the high undef elements from operand 0 and take the lower element
1668       // from operand 1.
1669       UndefElts.clearBit(0);
1670       UndefElts |= UndefElts2[0];
1671       break;
1672     }
1673 
1674     // Three input scalar-as-vector operations that work column-wise. The high
1675     // elements come from operand 0 and the low element is a function of all
1676     // three inputs.
1677     case Intrinsic::x86_avx512_mask_add_ss_round:
1678     case Intrinsic::x86_avx512_mask_div_ss_round:
1679     case Intrinsic::x86_avx512_mask_mul_ss_round:
1680     case Intrinsic::x86_avx512_mask_sub_ss_round:
1681     case Intrinsic::x86_avx512_mask_max_ss_round:
1682     case Intrinsic::x86_avx512_mask_min_ss_round:
1683     case Intrinsic::x86_avx512_mask_add_sd_round:
1684     case Intrinsic::x86_avx512_mask_div_sd_round:
1685     case Intrinsic::x86_avx512_mask_mul_sd_round:
1686     case Intrinsic::x86_avx512_mask_sub_sd_round:
1687     case Intrinsic::x86_avx512_mask_max_sd_round:
1688     case Intrinsic::x86_avx512_mask_min_sd_round:
1689       simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1690 
1691       // If lowest element of a scalar op isn't used then use Arg0.
1692       if (!DemandedElts[0]) {
1693         Worklist.push(II);
1694         return II->getArgOperand(0);
1695       }
1696 
1697       // Only lower element is used for operand 1 and 2.
1698       DemandedElts = 1;
1699       simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1700       simplifyAndSetOp(II, 2, DemandedElts, UndefElts3);
1701 
1702       // Lower element is undefined if all three lower elements are undefined.
1703       // Consider things like undef&0.  The result is known zero, not undef.
1704       if (!UndefElts2[0] || !UndefElts3[0])
1705         UndefElts.clearBit(0);
1706 
1707       break;
1708 
1709     case Intrinsic::x86_sse2_packssdw_128:
1710     case Intrinsic::x86_sse2_packsswb_128:
1711     case Intrinsic::x86_sse2_packuswb_128:
1712     case Intrinsic::x86_sse41_packusdw:
1713     case Intrinsic::x86_avx2_packssdw:
1714     case Intrinsic::x86_avx2_packsswb:
1715     case Intrinsic::x86_avx2_packusdw:
1716     case Intrinsic::x86_avx2_packuswb:
1717     case Intrinsic::x86_avx512_packssdw_512:
1718     case Intrinsic::x86_avx512_packsswb_512:
1719     case Intrinsic::x86_avx512_packusdw_512:
1720     case Intrinsic::x86_avx512_packuswb_512: {
1721       auto *Ty0 = II->getArgOperand(0)->getType();
1722       unsigned InnerVWidth = Ty0->getVectorNumElements();
1723       assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1724 
1725       unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1726       unsigned VWidthPerLane = VWidth / NumLanes;
1727       unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1728 
1729       // Per lane, pack the elements of the first input and then the second.
1730       // e.g.
1731       // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1732       // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1733       for (int OpNum = 0; OpNum != 2; ++OpNum) {
1734         APInt OpDemandedElts(InnerVWidth, 0);
1735         for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1736           unsigned LaneIdx = Lane * VWidthPerLane;
1737           for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1738             unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1739             if (DemandedElts[Idx])
1740               OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1741           }
1742         }
1743 
1744         // Demand elements from the operand.
1745         APInt OpUndefElts(InnerVWidth, 0);
1746         simplifyAndSetOp(II, OpNum, OpDemandedElts, OpUndefElts);
1747 
1748         // Pack the operand's UNDEF elements, one lane at a time.
1749         OpUndefElts = OpUndefElts.zext(VWidth);
1750         for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1751           APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1752           LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
1753           LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
1754           UndefElts |= LaneElts;
1755         }
1756       }
1757       break;
1758     }
1759 
1760     // PSHUFB
1761     case Intrinsic::x86_ssse3_pshuf_b_128:
1762     case Intrinsic::x86_avx2_pshuf_b:
1763     case Intrinsic::x86_avx512_pshuf_b_512:
1764     // PERMILVAR
1765     case Intrinsic::x86_avx_vpermilvar_ps:
1766     case Intrinsic::x86_avx_vpermilvar_ps_256:
1767     case Intrinsic::x86_avx512_vpermilvar_ps_512:
1768     case Intrinsic::x86_avx_vpermilvar_pd:
1769     case Intrinsic::x86_avx_vpermilvar_pd_256:
1770     case Intrinsic::x86_avx512_vpermilvar_pd_512:
1771     // PERMV
1772     case Intrinsic::x86_avx2_permd:
1773     case Intrinsic::x86_avx2_permps: {
1774       simplifyAndSetOp(II, 1, DemandedElts, UndefElts);
1775       break;
1776     }
1777 
1778     // SSE4A instructions leave the upper 64-bits of the 128-bit result
1779     // in an undefined state.
1780     case Intrinsic::x86_sse4a_extrq:
1781     case Intrinsic::x86_sse4a_extrqi:
1782     case Intrinsic::x86_sse4a_insertq:
1783     case Intrinsic::x86_sse4a_insertqi:
1784       UndefElts.setHighBits(VWidth / 2);
1785       break;
1786     case Intrinsic::amdgcn_buffer_load:
1787     case Intrinsic::amdgcn_buffer_load_format:
1788     case Intrinsic::amdgcn_raw_buffer_load:
1789     case Intrinsic::amdgcn_raw_buffer_load_format:
1790     case Intrinsic::amdgcn_raw_tbuffer_load:
1791     case Intrinsic::amdgcn_s_buffer_load:
1792     case Intrinsic::amdgcn_struct_buffer_load:
1793     case Intrinsic::amdgcn_struct_buffer_load_format:
1794     case Intrinsic::amdgcn_struct_tbuffer_load:
1795     case Intrinsic::amdgcn_tbuffer_load:
1796       return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts);
1797     default: {
1798       if (getAMDGPUImageDMaskIntrinsic(II->getIntrinsicID()))
1799         return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts, 0);
1800 
1801       break;
1802     }
1803     } // switch on IntrinsicID
1804     break;
1805   } // case Call
1806   } // switch on Opcode
1807 
1808   // TODO: We bail completely on integer div/rem and shifts because they have
1809   // UB/poison potential, but that should be refined.
1810   BinaryOperator *BO;
1811   if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) {
1812     simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1813     simplifyAndSetOp(I, 1, DemandedElts, UndefElts2);
1814 
1815     // Any change to an instruction with potential poison must clear those flags
1816     // because we can not guarantee those constraints now. Other analysis may
1817     // determine that it is safe to re-apply the flags.
1818     if (MadeChange)
1819       BO->dropPoisonGeneratingFlags();
1820 
1821     // Output elements are undefined if both are undefined. Consider things
1822     // like undef & 0. The result is known zero, not undef.
1823     UndefElts &= UndefElts2;
1824   }
1825 
1826   // If we've proven all of the lanes undef, return an undef value.
1827   // TODO: Intersect w/demanded lanes
1828   if (UndefElts.isAllOnesValue())
1829     return UndefValue::get(I->getType());;
1830 
1831   return MadeChange ? I : nullptr;
1832 }
1833