xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (revision 71ac745d76c3ba442e753daff1870893f272b29d)
10b57cec5SDimitry Andric //===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains logic for simplifying instructions based on information
100b57cec5SDimitry Andric // about how they are used.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "InstCombineInternal.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
1681ad6265SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
170b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
180b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
190b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h"
20e8d8bef9SDimitry Andric #include "llvm/Transforms/InstCombine/InstCombiner.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric using namespace llvm::PatternMatch;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #define DEBUG_TYPE "instcombine"
260b57cec5SDimitry Andric 
275f757f3fSDimitry Andric static cl::opt<bool>
285f757f3fSDimitry Andric     VerifyKnownBits("instcombine-verify-known-bits",
295f757f3fSDimitry Andric                     cl::desc("Verify that computeKnownBits() and "
305f757f3fSDimitry Andric                              "SimplifyDemandedBits() are consistent"),
315f757f3fSDimitry Andric                     cl::Hidden, cl::init(false));
325f757f3fSDimitry Andric 
330b57cec5SDimitry Andric /// Check to see if the specified operand of the specified instruction is a
340b57cec5SDimitry Andric /// constant integer. If so, check to see if there are any bits set in the
350b57cec5SDimitry Andric /// constant that are not demanded. If so, shrink the constant and return true.
ShrinkDemandedConstant(Instruction * I,unsigned OpNo,const APInt & Demanded)360b57cec5SDimitry Andric static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
370b57cec5SDimitry Andric                                    const APInt &Demanded) {
380b57cec5SDimitry Andric   assert(I && "No instruction?");
390b57cec5SDimitry Andric   assert(OpNo < I->getNumOperands() && "Operand index too large");
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // The operand must be a constant integer or splat integer.
420b57cec5SDimitry Andric   Value *Op = I->getOperand(OpNo);
430b57cec5SDimitry Andric   const APInt *C;
440b57cec5SDimitry Andric   if (!match(Op, m_APInt(C)))
450b57cec5SDimitry Andric     return false;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   // If there are no bits set that aren't demanded, nothing to do.
480b57cec5SDimitry Andric   if (C->isSubsetOf(Demanded))
490b57cec5SDimitry Andric     return false;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   // This instruction is producing bits that are not demanded. Shrink the RHS.
520b57cec5SDimitry Andric   I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   return true;
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
575f757f3fSDimitry Andric /// Returns the bitwidth of the given scalar or pointer type. For vector types,
585f757f3fSDimitry Andric /// returns the element type's bitwidth.
getBitWidth(Type * Ty,const DataLayout & DL)595f757f3fSDimitry Andric static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
605f757f3fSDimitry Andric   if (unsigned BitWidth = Ty->getScalarSizeInBits())
615f757f3fSDimitry Andric     return BitWidth;
620b57cec5SDimitry Andric 
635f757f3fSDimitry Andric   return DL.getPointerTypeSizeInBits(Ty);
645f757f3fSDimitry Andric }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric /// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
670b57cec5SDimitry Andric /// the instruction has any properties that allow us to simplify its operands.
SimplifyDemandedInstructionBits(Instruction & Inst,KnownBits & Known)685f757f3fSDimitry Andric bool InstCombinerImpl::SimplifyDemandedInstructionBits(Instruction &Inst,
695f757f3fSDimitry Andric                                                        KnownBits &Known) {
705f757f3fSDimitry Andric   APInt DemandedMask(APInt::getAllOnes(Known.getBitWidth()));
710b57cec5SDimitry Andric   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
720fca6ea1SDimitry Andric                                      0, SQ.getWithInstruction(&Inst));
730b57cec5SDimitry Andric   if (!V) return false;
740b57cec5SDimitry Andric   if (V == &Inst) return true;
750b57cec5SDimitry Andric   replaceInstUsesWith(Inst, V);
760b57cec5SDimitry Andric   return true;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
795f757f3fSDimitry Andric /// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
805f757f3fSDimitry Andric /// the instruction has any properties that allow us to simplify its operands.
SimplifyDemandedInstructionBits(Instruction & Inst)815f757f3fSDimitry Andric bool InstCombinerImpl::SimplifyDemandedInstructionBits(Instruction &Inst) {
825f757f3fSDimitry Andric   KnownBits Known(getBitWidth(Inst.getType(), DL));
835f757f3fSDimitry Andric   return SimplifyDemandedInstructionBits(Inst, Known);
845f757f3fSDimitry Andric }
855f757f3fSDimitry Andric 
860b57cec5SDimitry Andric /// This form of SimplifyDemandedBits simplifies the specified instruction
870b57cec5SDimitry Andric /// operand if possible, updating it in place. It returns true if it made any
880b57cec5SDimitry Andric /// change and false otherwise.
SimplifyDemandedBits(Instruction * I,unsigned OpNo,const APInt & DemandedMask,KnownBits & Known,unsigned Depth,const SimplifyQuery & Q)89e8d8bef9SDimitry Andric bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
900b57cec5SDimitry Andric                                             const APInt &DemandedMask,
910fca6ea1SDimitry Andric                                             KnownBits &Known, unsigned Depth,
920fca6ea1SDimitry Andric                                             const SimplifyQuery &Q) {
930b57cec5SDimitry Andric   Use &U = I->getOperandUse(OpNo);
940fca6ea1SDimitry Andric   Value *V = U.get();
950fca6ea1SDimitry Andric   if (isa<Constant>(V)) {
960fca6ea1SDimitry Andric     llvm::computeKnownBits(V, Known, Depth, Q);
970fca6ea1SDimitry Andric     return false;
980fca6ea1SDimitry Andric   }
990fca6ea1SDimitry Andric 
1000fca6ea1SDimitry Andric   Known.resetAll();
1010fca6ea1SDimitry Andric   if (DemandedMask.isZero()) {
1020fca6ea1SDimitry Andric     // Not demanding any bits from V.
1030fca6ea1SDimitry Andric     replaceUse(U, UndefValue::get(V->getType()));
1040fca6ea1SDimitry Andric     return true;
1050fca6ea1SDimitry Andric   }
1060fca6ea1SDimitry Andric 
1070fca6ea1SDimitry Andric   if (Depth == MaxAnalysisRecursionDepth)
1080fca6ea1SDimitry Andric     return false;
1090fca6ea1SDimitry Andric 
1100fca6ea1SDimitry Andric   Instruction *VInst = dyn_cast<Instruction>(V);
1110fca6ea1SDimitry Andric   if (!VInst) {
1120fca6ea1SDimitry Andric     llvm::computeKnownBits(V, Known, Depth, Q);
1130fca6ea1SDimitry Andric     return false;
1140fca6ea1SDimitry Andric   }
1150fca6ea1SDimitry Andric 
1160fca6ea1SDimitry Andric   Value *NewVal;
1170fca6ea1SDimitry Andric   if (VInst->hasOneUse()) {
1180fca6ea1SDimitry Andric     // If the instruction has one use, we can directly simplify it.
1190fca6ea1SDimitry Andric     NewVal = SimplifyDemandedUseBits(VInst, DemandedMask, Known, Depth, Q);
1200fca6ea1SDimitry Andric   } else {
1210fca6ea1SDimitry Andric     // If there are multiple uses of this instruction, then we can simplify
1220fca6ea1SDimitry Andric     // VInst to some other value, but not modify the instruction.
1230fca6ea1SDimitry Andric     NewVal =
1240fca6ea1SDimitry Andric         SimplifyMultipleUseDemandedBits(VInst, DemandedMask, Known, Depth, Q);
1250fca6ea1SDimitry Andric   }
1260b57cec5SDimitry Andric   if (!NewVal) return false;
1275ffd83dbSDimitry Andric   if (Instruction* OpInst = dyn_cast<Instruction>(U))
1285ffd83dbSDimitry Andric     salvageDebugInfo(*OpInst);
1295ffd83dbSDimitry Andric 
1305ffd83dbSDimitry Andric   replaceUse(U, NewVal);
1310b57cec5SDimitry Andric   return true;
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric /// This function attempts to replace V with a simpler value based on the
1350b57cec5SDimitry Andric /// demanded bits. When this function is called, it is known that only the bits
1360b57cec5SDimitry Andric /// set in DemandedMask of the result of V are ever used downstream.
1370b57cec5SDimitry Andric /// Consequently, depending on the mask and V, it may be possible to replace V
1380b57cec5SDimitry Andric /// with a constant or one of its operands. In such cases, this function does
1390b57cec5SDimitry Andric /// the replacement and returns true. In all other cases, it returns false after
1400b57cec5SDimitry Andric /// analyzing the expression and setting KnownOne and known to be one in the
1410b57cec5SDimitry Andric /// expression. Known.Zero contains all the bits that are known to be zero in
1420b57cec5SDimitry Andric /// the expression. These are provided to potentially allow the caller (which
1430b57cec5SDimitry Andric /// might recursively be SimplifyDemandedBits itself) to simplify the
1440b57cec5SDimitry Andric /// expression.
1450b57cec5SDimitry Andric /// Known.One and Known.Zero always follow the invariant that:
1460b57cec5SDimitry Andric ///   Known.One & Known.Zero == 0.
1475f757f3fSDimitry Andric /// That is, a bit can't be both 1 and 0. The bits in Known.One and Known.Zero
1485f757f3fSDimitry Andric /// are accurate even for bits not in DemandedMask. Note
1490b57cec5SDimitry Andric /// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all
1500b57cec5SDimitry Andric /// be the same.
1510b57cec5SDimitry Andric ///
1520b57cec5SDimitry Andric /// This returns null if it did not change anything and it permits no
1530b57cec5SDimitry Andric /// simplification.  This returns V itself if it did some simplification of V's
1540b57cec5SDimitry Andric /// operands based on the information about what bits are demanded. This returns
1550b57cec5SDimitry Andric /// some other non-null value if it found out that V is equal to another value
1560b57cec5SDimitry Andric /// in the context where the specified bits are demanded, but not for all users.
SimplifyDemandedUseBits(Instruction * I,const APInt & DemandedMask,KnownBits & Known,unsigned Depth,const SimplifyQuery & Q)1570fca6ea1SDimitry Andric Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
1580fca6ea1SDimitry Andric                                                  const APInt &DemandedMask,
159e8d8bef9SDimitry Andric                                                  KnownBits &Known,
160e8d8bef9SDimitry Andric                                                  unsigned Depth,
1610fca6ea1SDimitry Andric                                                  const SimplifyQuery &Q) {
1620fca6ea1SDimitry Andric   assert(I != nullptr && "Null pointer of Value???");
163e8d8bef9SDimitry Andric   assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1640b57cec5SDimitry Andric   uint32_t BitWidth = DemandedMask.getBitWidth();
1650fca6ea1SDimitry Andric   Type *VTy = I->getType();
1660b57cec5SDimitry Andric   assert(
1670b57cec5SDimitry Andric       (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
1680b57cec5SDimitry Andric       Known.getBitWidth() == BitWidth &&
1690b57cec5SDimitry Andric       "Value *V, DemandedMask and Known must have same BitWidth");
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
1720b57cec5SDimitry Andric 
173bdd1243dSDimitry Andric   // Update flags after simplifying an operand based on the fact that some high
174bdd1243dSDimitry Andric   // order bits are not demanded.
175bdd1243dSDimitry Andric   auto disableWrapFlagsBasedOnUnusedHighBits = [](Instruction *I,
176bdd1243dSDimitry Andric                                                   unsigned NLZ) {
177bdd1243dSDimitry Andric     if (NLZ > 0) {
178bdd1243dSDimitry Andric       // Disable the nsw and nuw flags here: We can no longer guarantee that
179bdd1243dSDimitry Andric       // we won't wrap after simplification. Removing the nsw/nuw flags is
180bdd1243dSDimitry Andric       // legal here because the top bit is not demanded.
181bdd1243dSDimitry Andric       I->setHasNoSignedWrap(false);
182bdd1243dSDimitry Andric       I->setHasNoUnsignedWrap(false);
183bdd1243dSDimitry Andric     }
184bdd1243dSDimitry Andric     return I;
185bdd1243dSDimitry Andric   };
186bdd1243dSDimitry Andric 
18781ad6265SDimitry Andric   // If the high-bits of an ADD/SUB/MUL are not demanded, then we do not care
18881ad6265SDimitry Andric   // about the high bits of the operands.
18981ad6265SDimitry Andric   auto simplifyOperandsBasedOnUnusedHighBits = [&](APInt &DemandedFromOps) {
19006c3fb27SDimitry Andric     unsigned NLZ = DemandedMask.countl_zero();
19181ad6265SDimitry Andric     // Right fill the mask of bits for the operands to demand the most
19281ad6265SDimitry Andric     // significant bit and all those below it.
19381ad6265SDimitry Andric     DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
19481ad6265SDimitry Andric     if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
1950fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1, Q) ||
19681ad6265SDimitry Andric         ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
1970fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q)) {
198bdd1243dSDimitry Andric       disableWrapFlagsBasedOnUnusedHighBits(I, NLZ);
19981ad6265SDimitry Andric       return true;
20081ad6265SDimitry Andric     }
20181ad6265SDimitry Andric     return false;
20281ad6265SDimitry Andric   };
20381ad6265SDimitry Andric 
2040b57cec5SDimitry Andric   switch (I->getOpcode()) {
2050b57cec5SDimitry Andric   default:
2060fca6ea1SDimitry Andric     llvm::computeKnownBits(I, Known, Depth, Q);
2070b57cec5SDimitry Andric     break;
2080b57cec5SDimitry Andric   case Instruction::And: {
2090b57cec5SDimitry Andric     // If either the LHS or the RHS are Zero, the result is zero.
2100fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
2110b57cec5SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
2120fca6ea1SDimitry Andric                              Depth + 1, Q))
2130b57cec5SDimitry Andric       return I;
2140b57cec5SDimitry Andric 
21506c3fb27SDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
2160fca6ea1SDimitry Andric                                          Depth, Q);
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
2190b57cec5SDimitry Andric     // constant.
2205ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
2215ffd83dbSDimitry Andric       return Constant::getIntegerValue(VTy, Known.One);
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric     // If all of the demanded bits are known 1 on one side, return the other.
2240b57cec5SDimitry Andric     // These bits cannot contribute to the result of the 'and'.
2250b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
2260b57cec5SDimitry Andric       return I->getOperand(0);
2270b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
2280b57cec5SDimitry Andric       return I->getOperand(1);
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     // If the RHS is a constant, see if we can simplify it.
2310b57cec5SDimitry Andric     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero))
2320b57cec5SDimitry Andric       return I;
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     break;
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric   case Instruction::Or: {
2370b57cec5SDimitry Andric     // If either the LHS or the RHS are One, the result is One.
2380fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
2390b57cec5SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
2400fca6ea1SDimitry Andric                              Depth + 1, Q)) {
2415f757f3fSDimitry Andric       // Disjoint flag may not longer hold.
2425f757f3fSDimitry Andric       I->dropPoisonGeneratingFlags();
2430b57cec5SDimitry Andric       return I;
2445f757f3fSDimitry Andric     }
2450b57cec5SDimitry Andric 
24606c3fb27SDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
2470fca6ea1SDimitry Andric                                          Depth, Q);
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
2500b57cec5SDimitry Andric     // constant.
2515ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
2525ffd83dbSDimitry Andric       return Constant::getIntegerValue(VTy, Known.One);
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric     // If all of the demanded bits are known zero on one side, return the other.
2550b57cec5SDimitry Andric     // These bits cannot contribute to the result of the 'or'.
2560b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
2570b57cec5SDimitry Andric       return I->getOperand(0);
2580b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
2590b57cec5SDimitry Andric       return I->getOperand(1);
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric     // If the RHS is a constant, see if we can simplify it.
2620b57cec5SDimitry Andric     if (ShrinkDemandedConstant(I, 1, DemandedMask))
2630b57cec5SDimitry Andric       return I;
2640b57cec5SDimitry Andric 
2655f757f3fSDimitry Andric     // Infer disjoint flag if no common bits are set.
2665f757f3fSDimitry Andric     if (!cast<PossiblyDisjointInst>(I)->isDisjoint()) {
2675f757f3fSDimitry Andric       WithCache<const Value *> LHSCache(I->getOperand(0), LHSKnown),
2685f757f3fSDimitry Andric           RHSCache(I->getOperand(1), RHSKnown);
2690fca6ea1SDimitry Andric       if (haveNoCommonBitsSet(LHSCache, RHSCache, Q)) {
2705f757f3fSDimitry Andric         cast<PossiblyDisjointInst>(I)->setIsDisjoint(true);
2715f757f3fSDimitry Andric         return I;
2725f757f3fSDimitry Andric       }
2735f757f3fSDimitry Andric     }
2745f757f3fSDimitry Andric 
2750b57cec5SDimitry Andric     break;
2760b57cec5SDimitry Andric   }
2770b57cec5SDimitry Andric   case Instruction::Xor: {
2780fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
2790fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1, Q))
2800b57cec5SDimitry Andric       return I;
281fe6060f1SDimitry Andric     Value *LHS, *RHS;
282fe6060f1SDimitry Andric     if (DemandedMask == 1 &&
283fe6060f1SDimitry Andric         match(I->getOperand(0), m_Intrinsic<Intrinsic::ctpop>(m_Value(LHS))) &&
284fe6060f1SDimitry Andric         match(I->getOperand(1), m_Intrinsic<Intrinsic::ctpop>(m_Value(RHS)))) {
285fe6060f1SDimitry Andric       // (ctpop(X) ^ ctpop(Y)) & 1 --> ctpop(X^Y) & 1
286fe6060f1SDimitry Andric       IRBuilderBase::InsertPointGuard Guard(Builder);
287fe6060f1SDimitry Andric       Builder.SetInsertPoint(I);
288fe6060f1SDimitry Andric       auto *Xor = Builder.CreateXor(LHS, RHS);
289fe6060f1SDimitry Andric       return Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, Xor);
290fe6060f1SDimitry Andric     }
291fe6060f1SDimitry Andric 
29206c3fb27SDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
2930fca6ea1SDimitry Andric                                          Depth, Q);
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
2960b57cec5SDimitry Andric     // constant.
2975ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
2985ffd83dbSDimitry Andric       return Constant::getIntegerValue(VTy, Known.One);
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric     // If all of the demanded bits are known zero on one side, return the other.
3010b57cec5SDimitry Andric     // These bits cannot contribute to the result of the 'xor'.
3020b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
3030b57cec5SDimitry Andric       return I->getOperand(0);
3040b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
3050b57cec5SDimitry Andric       return I->getOperand(1);
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric     // If all of the demanded bits are known to be zero on one side or the
3080b57cec5SDimitry Andric     // other, turn this into an *inclusive* or.
3090b57cec5SDimitry Andric     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
3100b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
3110b57cec5SDimitry Andric       Instruction *Or =
3125f757f3fSDimitry Andric           BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1));
3135f757f3fSDimitry Andric       if (DemandedMask.isAllOnes())
3145f757f3fSDimitry Andric         cast<PossiblyDisjointInst>(Or)->setIsDisjoint(true);
3155f757f3fSDimitry Andric       Or->takeName(I);
3165f757f3fSDimitry Andric       return InsertNewInstWith(Or, I->getIterator());
3170b57cec5SDimitry Andric     }
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric     // If all of the demanded bits on one side are known, and all of the set
3200b57cec5SDimitry Andric     // bits on that side are also known to be set on the other side, turn this
3210b57cec5SDimitry Andric     // into an AND, as we know the bits will be cleared.
3220b57cec5SDimitry Andric     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
3230b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
3240b57cec5SDimitry Andric         RHSKnown.One.isSubsetOf(LHSKnown.One)) {
3250b57cec5SDimitry Andric       Constant *AndC = Constant::getIntegerValue(VTy,
3260b57cec5SDimitry Andric                                                  ~RHSKnown.One & DemandedMask);
3270b57cec5SDimitry Andric       Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
3285f757f3fSDimitry Andric       return InsertNewInstWith(And, I->getIterator());
3290b57cec5SDimitry Andric     }
3300b57cec5SDimitry Andric 
331e8d8bef9SDimitry Andric     // If the RHS is a constant, see if we can change it. Don't alter a -1
332e8d8bef9SDimitry Andric     // constant because that's a canonical 'not' op, and that is better for
333e8d8bef9SDimitry Andric     // combining, SCEV, and codegen.
334e8d8bef9SDimitry Andric     const APInt *C;
335349cc55cSDimitry Andric     if (match(I->getOperand(1), m_APInt(C)) && !C->isAllOnes()) {
336349cc55cSDimitry Andric       if ((*C | ~DemandedMask).isAllOnes()) {
337e8d8bef9SDimitry Andric         // Force bits to 1 to create a 'not' op.
338e8d8bef9SDimitry Andric         I->setOperand(1, ConstantInt::getAllOnesValue(VTy));
339e8d8bef9SDimitry Andric         return I;
340e8d8bef9SDimitry Andric       }
341e8d8bef9SDimitry Andric       // If we can't turn this into a 'not', try to shrink the constant.
3420b57cec5SDimitry Andric       if (ShrinkDemandedConstant(I, 1, DemandedMask))
3430b57cec5SDimitry Andric         return I;
344e8d8bef9SDimitry Andric     }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric     // If our LHS is an 'and' and if it has one use, and if any of the bits we
3470b57cec5SDimitry Andric     // are flipping are known to be set, then the xor is just resetting those
3480b57cec5SDimitry Andric     // bits to zero.  We can just knock out bits from the 'and' and the 'xor',
3490b57cec5SDimitry Andric     // simplifying both of them.
350e8d8bef9SDimitry Andric     if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0))) {
351e8d8bef9SDimitry Andric       ConstantInt *AndRHS, *XorRHS;
3520b57cec5SDimitry Andric       if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
353e8d8bef9SDimitry Andric           match(I->getOperand(1), m_ConstantInt(XorRHS)) &&
354e8d8bef9SDimitry Andric           match(LHSInst->getOperand(1), m_ConstantInt(AndRHS)) &&
3550b57cec5SDimitry Andric           (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
3560b57cec5SDimitry Andric         APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
3570b57cec5SDimitry Andric 
35881ad6265SDimitry Andric         Constant *AndC = ConstantInt::get(VTy, NewMask & AndRHS->getValue());
3590b57cec5SDimitry Andric         Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
3605f757f3fSDimitry Andric         InsertNewInstWith(NewAnd, I->getIterator());
3610b57cec5SDimitry Andric 
36281ad6265SDimitry Andric         Constant *XorC = ConstantInt::get(VTy, NewMask & XorRHS->getValue());
3630b57cec5SDimitry Andric         Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
3645f757f3fSDimitry Andric         return InsertNewInstWith(NewXor, I->getIterator());
3650b57cec5SDimitry Andric       }
366e8d8bef9SDimitry Andric     }
3670b57cec5SDimitry Andric     break;
3680b57cec5SDimitry Andric   }
3690b57cec5SDimitry Andric   case Instruction::Select: {
3700fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1, Q) ||
3710fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1, Q))
3720b57cec5SDimitry Andric       return I;
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric     // If the operands are constants, see if we can simplify them.
375480093f4SDimitry Andric     // This is similar to ShrinkDemandedConstant, but for a select we want to
376480093f4SDimitry Andric     // try to keep the selected constants the same as icmp value constants, if
377480093f4SDimitry Andric     // we can. This helps not break apart (or helps put back together)
378480093f4SDimitry Andric     // canonical patterns like min and max.
379480093f4SDimitry Andric     auto CanonicalizeSelectConstant = [](Instruction *I, unsigned OpNo,
380e8d8bef9SDimitry Andric                                          const APInt &DemandedMask) {
381480093f4SDimitry Andric       const APInt *SelC;
382480093f4SDimitry Andric       if (!match(I->getOperand(OpNo), m_APInt(SelC)))
383480093f4SDimitry Andric         return false;
384480093f4SDimitry Andric 
385480093f4SDimitry Andric       // Get the constant out of the ICmp, if there is one.
386d409305fSDimitry Andric       // Only try this when exactly 1 operand is a constant (if both operands
387d409305fSDimitry Andric       // are constant, the icmp should eventually simplify). Otherwise, we may
388d409305fSDimitry Andric       // invert the transform that reduces set bits and infinite-loop.
389d409305fSDimitry Andric       Value *X;
390480093f4SDimitry Andric       const APInt *CmpC;
391480093f4SDimitry Andric       ICmpInst::Predicate Pred;
392d409305fSDimitry Andric       if (!match(I->getOperand(0), m_ICmp(Pred, m_Value(X), m_APInt(CmpC))) ||
393d409305fSDimitry Andric           isa<Constant>(X) || CmpC->getBitWidth() != SelC->getBitWidth())
394480093f4SDimitry Andric         return ShrinkDemandedConstant(I, OpNo, DemandedMask);
395480093f4SDimitry Andric 
396480093f4SDimitry Andric       // If the constant is already the same as the ICmp, leave it as-is.
397480093f4SDimitry Andric       if (*CmpC == *SelC)
398480093f4SDimitry Andric         return false;
399480093f4SDimitry Andric       // If the constants are not already the same, but can be with the demand
400480093f4SDimitry Andric       // mask, use the constant value from the ICmp.
401480093f4SDimitry Andric       if ((*CmpC & DemandedMask) == (*SelC & DemandedMask)) {
402480093f4SDimitry Andric         I->setOperand(OpNo, ConstantInt::get(I->getType(), *CmpC));
403480093f4SDimitry Andric         return true;
404480093f4SDimitry Andric       }
405480093f4SDimitry Andric       return ShrinkDemandedConstant(I, OpNo, DemandedMask);
406480093f4SDimitry Andric     };
407480093f4SDimitry Andric     if (CanonicalizeSelectConstant(I, 1, DemandedMask) ||
408480093f4SDimitry Andric         CanonicalizeSelectConstant(I, 2, DemandedMask))
4090b57cec5SDimitry Andric       return I;
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric     // Only known if known in both the LHS and RHS.
4120fca6ea1SDimitry Andric     adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(1),
4130fca6ea1SDimitry Andric                                 /*Invert=*/false, Depth, Q);
4140fca6ea1SDimitry Andric     adjustKnownBitsForSelectArm(RHSKnown, I->getOperand(0), I->getOperand(2),
4150fca6ea1SDimitry Andric                                 /*Invert=*/true, Depth, Q);
41606c3fb27SDimitry Andric     Known = LHSKnown.intersectWith(RHSKnown);
4170b57cec5SDimitry Andric     break;
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric   case Instruction::Trunc: {
420349cc55cSDimitry Andric     // If we do not demand the high bits of a right-shifted and truncated value,
421349cc55cSDimitry Andric     // then we may be able to truncate it before the shift.
422349cc55cSDimitry Andric     Value *X;
423349cc55cSDimitry Andric     const APInt *C;
424349cc55cSDimitry Andric     if (match(I->getOperand(0), m_OneUse(m_LShr(m_Value(X), m_APInt(C))))) {
425349cc55cSDimitry Andric       // The shift amount must be valid (not poison) in the narrow type, and
426349cc55cSDimitry Andric       // it must not be greater than the high bits demanded of the result.
42781ad6265SDimitry Andric       if (C->ult(VTy->getScalarSizeInBits()) &&
42806c3fb27SDimitry Andric           C->ule(DemandedMask.countl_zero())) {
429349cc55cSDimitry Andric         // trunc (lshr X, C) --> lshr (trunc X), C
430349cc55cSDimitry Andric         IRBuilderBase::InsertPointGuard Guard(Builder);
431349cc55cSDimitry Andric         Builder.SetInsertPoint(I);
43281ad6265SDimitry Andric         Value *Trunc = Builder.CreateTrunc(X, VTy);
433349cc55cSDimitry Andric         return Builder.CreateLShr(Trunc, C->getZExtValue());
434349cc55cSDimitry Andric       }
435349cc55cSDimitry Andric     }
436349cc55cSDimitry Andric   }
437bdd1243dSDimitry Andric     [[fallthrough]];
438349cc55cSDimitry Andric   case Instruction::ZExt: {
4390b57cec5SDimitry Andric     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric     APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth);
4420b57cec5SDimitry Andric     KnownBits InputKnown(SrcBitWidth);
4430fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1,
4440fca6ea1SDimitry Andric                              Q)) {
4455f757f3fSDimitry Andric       // For zext nneg, we may have dropped the instruction which made the
4465f757f3fSDimitry Andric       // input non-negative.
4475f757f3fSDimitry Andric       I->dropPoisonGeneratingFlags();
4480b57cec5SDimitry Andric       return I;
4495f757f3fSDimitry Andric     }
4500b57cec5SDimitry Andric     assert(InputKnown.getBitWidth() == SrcBitWidth && "Src width changed?");
4515f757f3fSDimitry Andric     if (I->getOpcode() == Instruction::ZExt && I->hasNonNeg() &&
4525f757f3fSDimitry Andric         !InputKnown.isNegative())
4535f757f3fSDimitry Andric       InputKnown.makeNonNegative();
4545ffd83dbSDimitry Andric     Known = InputKnown.zextOrTrunc(BitWidth);
4555f757f3fSDimitry Andric 
4560b57cec5SDimitry Andric     break;
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric   case Instruction::SExt: {
4590b57cec5SDimitry Andric     // Compute the bits in the result that are not present in the input.
4600b57cec5SDimitry Andric     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric     APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth);
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric     // If any of the sign extended bits are demanded, we know that the sign
4650b57cec5SDimitry Andric     // bit is demanded.
4660b57cec5SDimitry Andric     if (DemandedMask.getActiveBits() > SrcBitWidth)
4670b57cec5SDimitry Andric       InputDemandedBits.setBit(SrcBitWidth-1);
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric     KnownBits InputKnown(SrcBitWidth);
4700fca6ea1SDimitry Andric     if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1, Q))
4710b57cec5SDimitry Andric       return I;
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric     // If the input sign bit is known zero, or if the NewBits are not demanded
4740b57cec5SDimitry Andric     // convert this into a zero extension.
4750b57cec5SDimitry Andric     if (InputKnown.isNonNegative() ||
4760b57cec5SDimitry Andric         DemandedMask.getActiveBits() <= SrcBitWidth) {
4770b57cec5SDimitry Andric       // Convert to ZExt cast.
4785f757f3fSDimitry Andric       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy);
4795f757f3fSDimitry Andric       NewCast->takeName(I);
4805f757f3fSDimitry Andric       return InsertNewInstWith(NewCast, I->getIterator());
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     // If the sign bit of the input is known set or clear, then we know the
4840b57cec5SDimitry Andric     // top bits of the result.
4850b57cec5SDimitry Andric     Known = InputKnown.sext(BitWidth);
4860b57cec5SDimitry Andric     break;
4870b57cec5SDimitry Andric   }
488bdd1243dSDimitry Andric   case Instruction::Add: {
4895ffd83dbSDimitry Andric     if ((DemandedMask & 1) == 0) {
4905ffd83dbSDimitry Andric       // If we do not need the low bit, try to convert bool math to logic:
4915ffd83dbSDimitry Andric       // add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN
4925ffd83dbSDimitry Andric       Value *X, *Y;
4935ffd83dbSDimitry Andric       if (match(I, m_c_Add(m_OneUse(m_ZExt(m_Value(X))),
4945ffd83dbSDimitry Andric                            m_OneUse(m_SExt(m_Value(Y))))) &&
4955ffd83dbSDimitry Andric           X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
4965ffd83dbSDimitry Andric         // Truth table for inputs and output signbits:
4975ffd83dbSDimitry Andric         //       X:0 | X:1
4985ffd83dbSDimitry Andric         //      ----------
4995ffd83dbSDimitry Andric         // Y:0  |  0 | 0 |
5005ffd83dbSDimitry Andric         // Y:1  | -1 | 0 |
5015ffd83dbSDimitry Andric         //      ----------
5025ffd83dbSDimitry Andric         IRBuilderBase::InsertPointGuard Guard(Builder);
5035ffd83dbSDimitry Andric         Builder.SetInsertPoint(I);
5045ffd83dbSDimitry Andric         Value *AndNot = Builder.CreateAnd(Builder.CreateNot(X), Y);
5055ffd83dbSDimitry Andric         return Builder.CreateSExt(AndNot, VTy);
5065ffd83dbSDimitry Andric       }
5075ffd83dbSDimitry Andric 
5085ffd83dbSDimitry Andric       // add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
5090fca6ea1SDimitry Andric       if (match(I, m_Add(m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
5100fca6ea1SDimitry Andric           X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
5110fca6ea1SDimitry Andric           (I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {
5120fca6ea1SDimitry Andric 
5135ffd83dbSDimitry Andric         // Truth table for inputs and output signbits:
5145ffd83dbSDimitry Andric         //       X:0 | X:1
5155ffd83dbSDimitry Andric         //      -----------
5165ffd83dbSDimitry Andric         // Y:0  | -1 | -1 |
5175ffd83dbSDimitry Andric         // Y:1  | -1 |  0 |
5185ffd83dbSDimitry Andric         //      -----------
5195ffd83dbSDimitry Andric         IRBuilderBase::InsertPointGuard Guard(Builder);
5205ffd83dbSDimitry Andric         Builder.SetInsertPoint(I);
5215ffd83dbSDimitry Andric         Value *Or = Builder.CreateOr(X, Y);
5225ffd83dbSDimitry Andric         return Builder.CreateSExt(Or, VTy);
5235ffd83dbSDimitry Andric       }
5245ffd83dbSDimitry Andric     }
5250b57cec5SDimitry Andric 
526bdd1243dSDimitry Andric     // Right fill the mask of bits for the operands to demand the most
527bdd1243dSDimitry Andric     // significant bit and all those below it.
52806c3fb27SDimitry Andric     unsigned NLZ = DemandedMask.countl_zero();
529bdd1243dSDimitry Andric     APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
530bdd1243dSDimitry Andric     if (ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
5310fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q))
532bdd1243dSDimitry Andric       return disableWrapFlagsBasedOnUnusedHighBits(I, NLZ);
533bdd1243dSDimitry Andric 
534bdd1243dSDimitry Andric     // If low order bits are not demanded and known to be zero in one operand,
535bdd1243dSDimitry Andric     // then we don't need to demand them from the other operand, since they
536bdd1243dSDimitry Andric     // can't cause overflow into any bits that are demanded in the result.
53706c3fb27SDimitry Andric     unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countr_one();
538bdd1243dSDimitry Andric     APInt DemandedFromLHS = DemandedFromOps;
539bdd1243dSDimitry Andric     DemandedFromLHS.clearLowBits(NTZ);
540bdd1243dSDimitry Andric     if (ShrinkDemandedConstant(I, 0, DemandedFromLHS) ||
5410fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedFromLHS, LHSKnown, Depth + 1, Q))
542bdd1243dSDimitry Andric       return disableWrapFlagsBasedOnUnusedHighBits(I, NLZ);
543bdd1243dSDimitry Andric 
544bdd1243dSDimitry Andric     // If we are known to be adding zeros to every bit below
545bdd1243dSDimitry Andric     // the highest demanded bit, we just return the other side.
546bdd1243dSDimitry Andric     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
547bdd1243dSDimitry Andric       return I->getOperand(0);
548bdd1243dSDimitry Andric     if (DemandedFromOps.isSubsetOf(LHSKnown.Zero))
549bdd1243dSDimitry Andric       return I->getOperand(1);
550bdd1243dSDimitry Andric 
5515f757f3fSDimitry Andric     // (add X, C) --> (xor X, C) IFF C is equal to the top bit of the DemandMask
5525f757f3fSDimitry Andric     {
5535f757f3fSDimitry Andric       const APInt *C;
5545f757f3fSDimitry Andric       if (match(I->getOperand(1), m_APInt(C)) &&
5555f757f3fSDimitry Andric           C->isOneBitSet(DemandedMask.getActiveBits() - 1)) {
5565f757f3fSDimitry Andric         IRBuilderBase::InsertPointGuard Guard(Builder);
5575f757f3fSDimitry Andric         Builder.SetInsertPoint(I);
5585f757f3fSDimitry Andric         return Builder.CreateXor(I->getOperand(0), ConstantInt::get(VTy, *C));
5595f757f3fSDimitry Andric       }
5605f757f3fSDimitry Andric     }
5615f757f3fSDimitry Andric 
562bdd1243dSDimitry Andric     // Otherwise just compute the known bits of the result.
563bdd1243dSDimitry Andric     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
5640fca6ea1SDimitry Andric     bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
5650fca6ea1SDimitry Andric     Known = KnownBits::computeForAddSub(true, NSW, NUW, LHSKnown, RHSKnown);
566bdd1243dSDimitry Andric     break;
567bdd1243dSDimitry Andric   }
568bdd1243dSDimitry Andric   case Instruction::Sub: {
569bdd1243dSDimitry Andric     // Right fill the mask of bits for the operands to demand the most
570bdd1243dSDimitry Andric     // significant bit and all those below it.
57106c3fb27SDimitry Andric     unsigned NLZ = DemandedMask.countl_zero();
572bdd1243dSDimitry Andric     APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
573bdd1243dSDimitry Andric     if (ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
5740fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q))
575bdd1243dSDimitry Andric       return disableWrapFlagsBasedOnUnusedHighBits(I, NLZ);
576bdd1243dSDimitry Andric 
577bdd1243dSDimitry Andric     // If low order bits are not demanded and are known to be zero in RHS,
578bdd1243dSDimitry Andric     // then we don't need to demand them from LHS, since they can't cause a
579bdd1243dSDimitry Andric     // borrow from any bits that are demanded in the result.
58006c3fb27SDimitry Andric     unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countr_one();
581bdd1243dSDimitry Andric     APInt DemandedFromLHS = DemandedFromOps;
582bdd1243dSDimitry Andric     DemandedFromLHS.clearLowBits(NTZ);
583bdd1243dSDimitry Andric     if (ShrinkDemandedConstant(I, 0, DemandedFromLHS) ||
5840fca6ea1SDimitry Andric         SimplifyDemandedBits(I, 0, DemandedFromLHS, LHSKnown, Depth + 1, Q))
585bdd1243dSDimitry Andric       return disableWrapFlagsBasedOnUnusedHighBits(I, NLZ);
586bdd1243dSDimitry Andric 
587bdd1243dSDimitry Andric     // If we are known to be subtracting zeros from every bit below
5880b57cec5SDimitry Andric     // the highest demanded bit, we just return the other side.
5890b57cec5SDimitry Andric     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
5900b57cec5SDimitry Andric       return I->getOperand(0);
5910b57cec5SDimitry Andric     // We can't do this with the LHS for subtraction, unless we are only
5920b57cec5SDimitry Andric     // demanding the LSB.
593bdd1243dSDimitry Andric     if (DemandedFromOps.isOne() && DemandedFromOps.isSubsetOf(LHSKnown.Zero))
5940b57cec5SDimitry Andric       return I->getOperand(1);
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric     // Otherwise just compute the known bits of the result.
5970b57cec5SDimitry Andric     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
5980fca6ea1SDimitry Andric     bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
5990fca6ea1SDimitry Andric     Known = KnownBits::computeForAddSub(false, NSW, NUW, LHSKnown, RHSKnown);
6000b57cec5SDimitry Andric     break;
6010b57cec5SDimitry Andric   }
60281ad6265SDimitry Andric   case Instruction::Mul: {
60381ad6265SDimitry Andric     APInt DemandedFromOps;
60481ad6265SDimitry Andric     if (simplifyOperandsBasedOnUnusedHighBits(DemandedFromOps))
60581ad6265SDimitry Andric       return I;
60681ad6265SDimitry Andric 
60781ad6265SDimitry Andric     if (DemandedMask.isPowerOf2()) {
60881ad6265SDimitry Andric       // The LSB of X*Y is set only if (X & 1) == 1 and (Y & 1) == 1.
60981ad6265SDimitry Andric       // If we demand exactly one bit N and we have "X * (C' << N)" where C' is
61081ad6265SDimitry Andric       // odd (has LSB set), then the left-shifted low bit of X is the answer.
61106c3fb27SDimitry Andric       unsigned CTZ = DemandedMask.countr_zero();
61281ad6265SDimitry Andric       const APInt *C;
61306c3fb27SDimitry Andric       if (match(I->getOperand(1), m_APInt(C)) && C->countr_zero() == CTZ) {
61481ad6265SDimitry Andric         Constant *ShiftC = ConstantInt::get(VTy, CTZ);
61581ad6265SDimitry Andric         Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC);
6165f757f3fSDimitry Andric         return InsertNewInstWith(Shl, I->getIterator());
61781ad6265SDimitry Andric       }
61881ad6265SDimitry Andric     }
61981ad6265SDimitry Andric     // For a squared value "X * X", the bottom 2 bits are 0 and X[0] because:
62081ad6265SDimitry Andric     // X * X is odd iff X is odd.
62181ad6265SDimitry Andric     // 'Quadratic Reciprocity': X * X -> 0 for bit[1]
62281ad6265SDimitry Andric     if (I->getOperand(0) == I->getOperand(1) && DemandedMask.ult(4)) {
62381ad6265SDimitry Andric       Constant *One = ConstantInt::get(VTy, 1);
62481ad6265SDimitry Andric       Instruction *And1 = BinaryOperator::CreateAnd(I->getOperand(0), One);
6255f757f3fSDimitry Andric       return InsertNewInstWith(And1, I->getIterator());
62681ad6265SDimitry Andric     }
62781ad6265SDimitry Andric 
6280fca6ea1SDimitry Andric     llvm::computeKnownBits(I, Known, Depth, Q);
62981ad6265SDimitry Andric     break;
63081ad6265SDimitry Andric   }
6310b57cec5SDimitry Andric   case Instruction::Shl: {
6320b57cec5SDimitry Andric     const APInt *SA;
6330b57cec5SDimitry Andric     if (match(I->getOperand(1), m_APInt(SA))) {
6340b57cec5SDimitry Andric       const APInt *ShrAmt;
6350b57cec5SDimitry Andric       if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt))))
6360b57cec5SDimitry Andric         if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0)))
6370b57cec5SDimitry Andric           if (Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
6380b57cec5SDimitry Andric                                                     DemandedMask, Known))
6390b57cec5SDimitry Andric             return R;
6400b57cec5SDimitry Andric 
6410fca6ea1SDimitry Andric       // Do not simplify if shl is part of funnel-shift pattern
6420fca6ea1SDimitry Andric       if (I->hasOneUse()) {
6430fca6ea1SDimitry Andric         auto *Inst = dyn_cast<Instruction>(I->user_back());
6440fca6ea1SDimitry Andric         if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
6450fca6ea1SDimitry Andric           if (auto Opt = convertOrOfShiftsToFunnelShift(*Inst)) {
6460fca6ea1SDimitry Andric             auto [IID, FShiftArgs] = *Opt;
6470fca6ea1SDimitry Andric             if ((IID == Intrinsic::fshl || IID == Intrinsic::fshr) &&
6480fca6ea1SDimitry Andric                 FShiftArgs[0] == FShiftArgs[1]) {
6490fca6ea1SDimitry Andric               llvm::computeKnownBits(I, Known, Depth, Q);
6500fca6ea1SDimitry Andric               break;
6510fca6ea1SDimitry Andric             }
6520fca6ea1SDimitry Andric           }
6530fca6ea1SDimitry Andric         }
6540fca6ea1SDimitry Andric       }
6550fca6ea1SDimitry Andric 
6560fca6ea1SDimitry Andric       // We only want bits that already match the signbit then we don't
65781ad6265SDimitry Andric       // need to shift.
6580fca6ea1SDimitry Andric       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth - 1);
6590fca6ea1SDimitry Andric       if (DemandedMask.countr_zero() >= ShiftAmt) {
6600fca6ea1SDimitry Andric         if (I->hasNoSignedWrap()) {
6610fca6ea1SDimitry Andric           unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
6620fca6ea1SDimitry Andric           unsigned SignBits =
6630fca6ea1SDimitry Andric               ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
6640fca6ea1SDimitry Andric           if (SignBits > ShiftAmt && SignBits - ShiftAmt >= NumHiDemandedBits)
6650fca6ea1SDimitry Andric             return I->getOperand(0);
6660fca6ea1SDimitry Andric         }
66781ad6265SDimitry Andric 
66881ad6265SDimitry Andric         // If we can pre-shift a right-shifted constant to the left without
6690fca6ea1SDimitry Andric         // losing any high bits and we don't demand the low bits, then eliminate
67081ad6265SDimitry Andric         // the left-shift:
6710fca6ea1SDimitry Andric         // (C >> X) << LeftShiftAmtC --> (C << LeftShiftAmtC) >> X
67281ad6265SDimitry Andric         Value *X;
67381ad6265SDimitry Andric         Constant *C;
6740fca6ea1SDimitry Andric         if (match(I->getOperand(0), m_LShr(m_ImmConstant(C), m_Value(X)))) {
67581ad6265SDimitry Andric           Constant *LeftShiftAmtC = ConstantInt::get(VTy, ShiftAmt);
6765f757f3fSDimitry Andric           Constant *NewC = ConstantFoldBinaryOpOperands(Instruction::Shl, C,
6775f757f3fSDimitry Andric                                                         LeftShiftAmtC, DL);
6780fca6ea1SDimitry Andric           if (ConstantFoldBinaryOpOperands(Instruction::LShr, NewC,
6790fca6ea1SDimitry Andric                                            LeftShiftAmtC, DL) == C) {
68081ad6265SDimitry Andric             Instruction *Lshr = BinaryOperator::CreateLShr(NewC, X);
6815f757f3fSDimitry Andric             return InsertNewInstWith(Lshr, I->getIterator());
68281ad6265SDimitry Andric           }
68381ad6265SDimitry Andric         }
6840fca6ea1SDimitry Andric       }
68581ad6265SDimitry Andric 
6860b57cec5SDimitry Andric       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric       // If the shift is NUW/NSW, then it does demand the high bits.
6890b57cec5SDimitry Andric       ShlOperator *IOp = cast<ShlOperator>(I);
6900b57cec5SDimitry Andric       if (IOp->hasNoSignedWrap())
6910b57cec5SDimitry Andric         DemandedMaskIn.setHighBits(ShiftAmt+1);
6920b57cec5SDimitry Andric       else if (IOp->hasNoUnsignedWrap())
6930b57cec5SDimitry Andric         DemandedMaskIn.setHighBits(ShiftAmt);
6940b57cec5SDimitry Andric 
6950fca6ea1SDimitry Andric       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q))
6960b57cec5SDimitry Andric         return I;
6975ffd83dbSDimitry Andric 
69806c3fb27SDimitry Andric       Known = KnownBits::shl(Known,
69906c3fb27SDimitry Andric                              KnownBits::makeConstant(APInt(BitWidth, ShiftAmt)),
70006c3fb27SDimitry Andric                              /* NUW */ IOp->hasNoUnsignedWrap(),
70106c3fb27SDimitry Andric                              /* NSW */ IOp->hasNoSignedWrap());
7025ffd83dbSDimitry Andric     } else {
703fe6060f1SDimitry Andric       // This is a variable shift, so we can't shift the demand mask by a known
704fe6060f1SDimitry Andric       // amount. But if we are not demanding high bits, then we are not
705fe6060f1SDimitry Andric       // demanding those bits from the pre-shifted operand either.
70606c3fb27SDimitry Andric       if (unsigned CTLZ = DemandedMask.countl_zero()) {
707fe6060f1SDimitry Andric         APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ));
7080fca6ea1SDimitry Andric         if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1, Q)) {
709fe6060f1SDimitry Andric           // We can't guarantee that nsw/nuw hold after simplifying the operand.
710fe6060f1SDimitry Andric           I->dropPoisonGeneratingFlags();
711fe6060f1SDimitry Andric           return I;
712fe6060f1SDimitry Andric         }
713fe6060f1SDimitry Andric       }
7140fca6ea1SDimitry Andric       llvm::computeKnownBits(I, Known, Depth, Q);
7150b57cec5SDimitry Andric     }
7160b57cec5SDimitry Andric     break;
7170b57cec5SDimitry Andric   }
7180b57cec5SDimitry Andric   case Instruction::LShr: {
7190b57cec5SDimitry Andric     const APInt *SA;
7200b57cec5SDimitry Andric     if (match(I->getOperand(1), m_APInt(SA))) {
7210b57cec5SDimitry Andric       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
7220b57cec5SDimitry Andric 
7230fca6ea1SDimitry Andric       // Do not simplify if lshr is part of funnel-shift pattern
7240fca6ea1SDimitry Andric       if (I->hasOneUse()) {
7250fca6ea1SDimitry Andric         auto *Inst = dyn_cast<Instruction>(I->user_back());
7260fca6ea1SDimitry Andric         if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
7270fca6ea1SDimitry Andric           if (auto Opt = convertOrOfShiftsToFunnelShift(*Inst)) {
7280fca6ea1SDimitry Andric             auto [IID, FShiftArgs] = *Opt;
7290fca6ea1SDimitry Andric             if ((IID == Intrinsic::fshl || IID == Intrinsic::fshr) &&
7300fca6ea1SDimitry Andric                 FShiftArgs[0] == FShiftArgs[1]) {
7310fca6ea1SDimitry Andric               llvm::computeKnownBits(I, Known, Depth, Q);
7320fca6ea1SDimitry Andric               break;
7330fca6ea1SDimitry Andric             }
7340fca6ea1SDimitry Andric           }
7350fca6ea1SDimitry Andric         }
7360fca6ea1SDimitry Andric       }
7370fca6ea1SDimitry Andric 
73881ad6265SDimitry Andric       // If we are just demanding the shifted sign bit and below, then this can
73981ad6265SDimitry Andric       // be treated as an ASHR in disguise.
74006c3fb27SDimitry Andric       if (DemandedMask.countl_zero() >= ShiftAmt) {
74181ad6265SDimitry Andric         // If we only want bits that already match the signbit then we don't
74281ad6265SDimitry Andric         // need to shift.
74306c3fb27SDimitry Andric         unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
74481ad6265SDimitry Andric         unsigned SignBits =
7450fca6ea1SDimitry Andric             ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
74681ad6265SDimitry Andric         if (SignBits >= NumHiDemandedBits)
74781ad6265SDimitry Andric           return I->getOperand(0);
74881ad6265SDimitry Andric 
74981ad6265SDimitry Andric         // If we can pre-shift a left-shifted constant to the right without
75081ad6265SDimitry Andric         // losing any low bits (we already know we don't demand the high bits),
75181ad6265SDimitry Andric         // then eliminate the right-shift:
75281ad6265SDimitry Andric         // (C << X) >> RightShiftAmtC --> (C >> RightShiftAmtC) << X
75381ad6265SDimitry Andric         Value *X;
75481ad6265SDimitry Andric         Constant *C;
75581ad6265SDimitry Andric         if (match(I->getOperand(0), m_Shl(m_ImmConstant(C), m_Value(X)))) {
75681ad6265SDimitry Andric           Constant *RightShiftAmtC = ConstantInt::get(VTy, ShiftAmt);
7575f757f3fSDimitry Andric           Constant *NewC = ConstantFoldBinaryOpOperands(Instruction::LShr, C,
7585f757f3fSDimitry Andric                                                         RightShiftAmtC, DL);
7595f757f3fSDimitry Andric           if (ConstantFoldBinaryOpOperands(Instruction::Shl, NewC,
7605f757f3fSDimitry Andric                                            RightShiftAmtC, DL) == C) {
76181ad6265SDimitry Andric             Instruction *Shl = BinaryOperator::CreateShl(NewC, X);
7625f757f3fSDimitry Andric             return InsertNewInstWith(Shl, I->getIterator());
76381ad6265SDimitry Andric           }
76481ad6265SDimitry Andric         }
76581ad6265SDimitry Andric       }
76681ad6265SDimitry Andric 
7670b57cec5SDimitry Andric       // Unsigned shift right.
7680b57cec5SDimitry Andric       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
7690fca6ea1SDimitry Andric       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q)) {
7705f757f3fSDimitry Andric         // exact flag may not longer hold.
7715f757f3fSDimitry Andric         I->dropPoisonGeneratingFlags();
7720b57cec5SDimitry Andric         return I;
7735f757f3fSDimitry Andric       }
7740b57cec5SDimitry Andric       Known.Zero.lshrInPlace(ShiftAmt);
7750b57cec5SDimitry Andric       Known.One.lshrInPlace(ShiftAmt);
7760b57cec5SDimitry Andric       if (ShiftAmt)
7770b57cec5SDimitry Andric         Known.Zero.setHighBits(ShiftAmt);  // high bits known zero.
7785ffd83dbSDimitry Andric     } else {
7790fca6ea1SDimitry Andric       llvm::computeKnownBits(I, Known, Depth, Q);
7800b57cec5SDimitry Andric     }
7810b57cec5SDimitry Andric     break;
7820b57cec5SDimitry Andric   }
7830b57cec5SDimitry Andric   case Instruction::AShr: {
7840fca6ea1SDimitry Andric     unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
78581ad6265SDimitry Andric 
78681ad6265SDimitry Andric     // If we only want bits that already match the signbit then we don't need
78781ad6265SDimitry Andric     // to shift.
78806c3fb27SDimitry Andric     unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
78981ad6265SDimitry Andric     if (SignBits >= NumHiDemandedBits)
79081ad6265SDimitry Andric       return I->getOperand(0);
79181ad6265SDimitry Andric 
7920b57cec5SDimitry Andric     // If this is an arithmetic shift right and only the low-bit is set, we can
7930b57cec5SDimitry Andric     // always convert this into a logical shr, even if the shift amount is
7940b57cec5SDimitry Andric     // variable.  The low bit of the shift cannot be an input sign bit unless
7950b57cec5SDimitry Andric     // the shift amount is >= the size of the datatype, which is undefined.
796349cc55cSDimitry Andric     if (DemandedMask.isOne()) {
7970b57cec5SDimitry Andric       // Perform the logical shift right.
7980b57cec5SDimitry Andric       Instruction *NewVal = BinaryOperator::CreateLShr(
7990b57cec5SDimitry Andric                         I->getOperand(0), I->getOperand(1), I->getName());
8005f757f3fSDimitry Andric       return InsertNewInstWith(NewVal, I->getIterator());
8010b57cec5SDimitry Andric     }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric     const APInt *SA;
8040b57cec5SDimitry Andric     if (match(I->getOperand(1), m_APInt(SA))) {
8050b57cec5SDimitry Andric       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric       // Signed shift right.
8080b57cec5SDimitry Andric       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
8090fca6ea1SDimitry Andric       // If any of the bits being shifted in are demanded, then we should set
8100fca6ea1SDimitry Andric       // the sign bit as demanded.
8110fca6ea1SDimitry Andric       bool ShiftedInBitsDemanded = DemandedMask.countl_zero() < ShiftAmt;
8120fca6ea1SDimitry Andric       if (ShiftedInBitsDemanded)
8130b57cec5SDimitry Andric         DemandedMaskIn.setSignBit();
8140fca6ea1SDimitry Andric       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q)) {
8155f757f3fSDimitry Andric         // exact flag may not longer hold.
8165f757f3fSDimitry Andric         I->dropPoisonGeneratingFlags();
8170b57cec5SDimitry Andric         return I;
8185f757f3fSDimitry Andric       }
8190b57cec5SDimitry Andric 
8200fca6ea1SDimitry Andric       // If the input sign bit is known to be zero, or if none of the shifted in
8210fca6ea1SDimitry Andric       // bits are demanded, turn this into an unsigned shift right.
8220fca6ea1SDimitry Andric       if (Known.Zero[BitWidth - 1] || !ShiftedInBitsDemanded) {
8230b57cec5SDimitry Andric         BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
8240b57cec5SDimitry Andric                                                           I->getOperand(1));
8250b57cec5SDimitry Andric         LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
8265f757f3fSDimitry Andric         LShr->takeName(I);
8275f757f3fSDimitry Andric         return InsertNewInstWith(LShr, I->getIterator());
8280b57cec5SDimitry Andric       }
8290fca6ea1SDimitry Andric 
8300fca6ea1SDimitry Andric       Known = KnownBits::ashr(
8310fca6ea1SDimitry Andric           Known, KnownBits::makeConstant(APInt(BitWidth, ShiftAmt)),
8320fca6ea1SDimitry Andric           ShiftAmt != 0, I->isExact());
8335ffd83dbSDimitry Andric     } else {
8340fca6ea1SDimitry Andric       llvm::computeKnownBits(I, Known, Depth, Q);
8350b57cec5SDimitry Andric     }
8360b57cec5SDimitry Andric     break;
8370b57cec5SDimitry Andric   }
8380b57cec5SDimitry Andric   case Instruction::UDiv: {
8390b57cec5SDimitry Andric     // UDiv doesn't demand low bits that are zero in the divisor.
8400b57cec5SDimitry Andric     const APInt *SA;
8410b57cec5SDimitry Andric     if (match(I->getOperand(1), m_APInt(SA))) {
842bdd1243dSDimitry Andric       // TODO: Take the demanded mask of the result into account.
84306c3fb27SDimitry Andric       unsigned RHSTrailingZeros = SA->countr_zero();
8440b57cec5SDimitry Andric       APInt DemandedMaskIn =
8450b57cec5SDimitry Andric           APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
8460fca6ea1SDimitry Andric       if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1, Q)) {
847bdd1243dSDimitry Andric         // We can't guarantee that "exact" is still true after changing the
848bdd1243dSDimitry Andric         // the dividend.
849bdd1243dSDimitry Andric         I->dropPoisonGeneratingFlags();
8500b57cec5SDimitry Andric         return I;
851bdd1243dSDimitry Andric       }
8520b57cec5SDimitry Andric 
85306c3fb27SDimitry Andric       Known = KnownBits::udiv(LHSKnown, KnownBits::makeConstant(*SA),
85406c3fb27SDimitry Andric                               cast<BinaryOperator>(I)->isExact());
8555ffd83dbSDimitry Andric     } else {
8560fca6ea1SDimitry Andric       llvm::computeKnownBits(I, Known, Depth, Q);
8570b57cec5SDimitry Andric     }
8580b57cec5SDimitry Andric     break;
8590b57cec5SDimitry Andric   }
860e8d8bef9SDimitry Andric   case Instruction::SRem: {
86181ad6265SDimitry Andric     const APInt *Rem;
86281ad6265SDimitry Andric     if (match(I->getOperand(1), m_APInt(Rem))) {
8630b57cec5SDimitry Andric       // X % -1 demands all the bits because we don't want to introduce
8640b57cec5SDimitry Andric       // INT_MIN % -1 (== undef) by accident.
86581ad6265SDimitry Andric       if (Rem->isAllOnes())
8660b57cec5SDimitry Andric         break;
86781ad6265SDimitry Andric       APInt RA = Rem->abs();
8680b57cec5SDimitry Andric       if (RA.isPowerOf2()) {
8690b57cec5SDimitry Andric         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
8700b57cec5SDimitry Andric           return I->getOperand(0);
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric         APInt LowBits = RA - 1;
8730b57cec5SDimitry Andric         APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
8740fca6ea1SDimitry Andric         if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
8750b57cec5SDimitry Andric           return I;
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric         // The low bits of LHS are unchanged by the srem.
8780b57cec5SDimitry Andric         Known.Zero = LHSKnown.Zero & LowBits;
8790b57cec5SDimitry Andric         Known.One = LHSKnown.One & LowBits;
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric         // If LHS is non-negative or has all low bits zero, then the upper bits
8820b57cec5SDimitry Andric         // are all zero.
8830b57cec5SDimitry Andric         if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
8840b57cec5SDimitry Andric           Known.Zero |= ~LowBits;
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric         // If LHS is negative and not all low bits are zero, then the upper bits
8870b57cec5SDimitry Andric         // are all one.
8880b57cec5SDimitry Andric         if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
8890b57cec5SDimitry Andric           Known.One |= ~LowBits;
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric         break;
8920b57cec5SDimitry Andric       }
8930b57cec5SDimitry Andric     }
8940b57cec5SDimitry Andric 
8950fca6ea1SDimitry Andric     llvm::computeKnownBits(I, Known, Depth, Q);
8960b57cec5SDimitry Andric     break;
8970b57cec5SDimitry Andric   }
8985ffd83dbSDimitry Andric   case Instruction::Call: {
8995ffd83dbSDimitry Andric     bool KnownBitsComputed = false;
9000b57cec5SDimitry Andric     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
9010b57cec5SDimitry Andric       switch (II->getIntrinsicID()) {
902fe6060f1SDimitry Andric       case Intrinsic::abs: {
903fe6060f1SDimitry Andric         if (DemandedMask == 1)
904fe6060f1SDimitry Andric           return II->getArgOperand(0);
905fe6060f1SDimitry Andric         break;
906fe6060f1SDimitry Andric       }
907fe6060f1SDimitry Andric       case Intrinsic::ctpop: {
908fe6060f1SDimitry Andric         // Checking if the number of clear bits is odd (parity)? If the type has
909fe6060f1SDimitry Andric         // an even number of bits, that's the same as checking if the number of
910fe6060f1SDimitry Andric         // set bits is odd, so we can eliminate the 'not' op.
911fe6060f1SDimitry Andric         Value *X;
912fe6060f1SDimitry Andric         if (DemandedMask == 1 && VTy->getScalarSizeInBits() % 2 == 0 &&
913fe6060f1SDimitry Andric             match(II->getArgOperand(0), m_Not(m_Value(X)))) {
914fe6060f1SDimitry Andric           Function *Ctpop = Intrinsic::getDeclaration(
91581ad6265SDimitry Andric               II->getModule(), Intrinsic::ctpop, VTy);
9165f757f3fSDimitry Andric           return InsertNewInstWith(CallInst::Create(Ctpop, {X}), I->getIterator());
917fe6060f1SDimitry Andric         }
918fe6060f1SDimitry Andric         break;
919fe6060f1SDimitry Andric       }
9200b57cec5SDimitry Andric       case Intrinsic::bswap: {
9210b57cec5SDimitry Andric         // If the only bits demanded come from one byte of the bswap result,
9220b57cec5SDimitry Andric         // just shift the input byte into position to eliminate the bswap.
92306c3fb27SDimitry Andric         unsigned NLZ = DemandedMask.countl_zero();
92406c3fb27SDimitry Andric         unsigned NTZ = DemandedMask.countr_zero();
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
9270b57cec5SDimitry Andric         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
9280b57cec5SDimitry Andric         // have 14 leading zeros, round to 8.
92904eeddc0SDimitry Andric         NLZ = alignDown(NLZ, 8);
93004eeddc0SDimitry Andric         NTZ = alignDown(NTZ, 8);
9310b57cec5SDimitry Andric         // If we need exactly one byte, we can do this transformation.
9320b57cec5SDimitry Andric         if (BitWidth - NLZ - NTZ == 8) {
9330b57cec5SDimitry Andric           // Replace this with either a left or right shift to get the byte into
9340b57cec5SDimitry Andric           // the right place.
9350b57cec5SDimitry Andric           Instruction *NewVal;
93604eeddc0SDimitry Andric           if (NLZ > NTZ)
93704eeddc0SDimitry Andric             NewVal = BinaryOperator::CreateLShr(
93881ad6265SDimitry Andric                 II->getArgOperand(0), ConstantInt::get(VTy, NLZ - NTZ));
9390b57cec5SDimitry Andric           else
94004eeddc0SDimitry Andric             NewVal = BinaryOperator::CreateShl(
94181ad6265SDimitry Andric                 II->getArgOperand(0), ConstantInt::get(VTy, NTZ - NLZ));
9420b57cec5SDimitry Andric           NewVal->takeName(I);
9435f757f3fSDimitry Andric           return InsertNewInstWith(NewVal, I->getIterator());
9440b57cec5SDimitry Andric         }
9450b57cec5SDimitry Andric         break;
9460b57cec5SDimitry Andric       }
9475f757f3fSDimitry Andric       case Intrinsic::ptrmask: {
9485f757f3fSDimitry Andric         unsigned MaskWidth = I->getOperand(1)->getType()->getScalarSizeInBits();
9495f757f3fSDimitry Andric         RHSKnown = KnownBits(MaskWidth);
9505f757f3fSDimitry Andric         // If either the LHS or the RHS are Zero, the result is zero.
9510fca6ea1SDimitry Andric         if (SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1, Q) ||
9525f757f3fSDimitry Andric             SimplifyDemandedBits(
9535f757f3fSDimitry Andric                 I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth),
9540fca6ea1SDimitry Andric                 RHSKnown, Depth + 1, Q))
9555f757f3fSDimitry Andric           return I;
9565f757f3fSDimitry Andric 
9575f757f3fSDimitry Andric         // TODO: Should be 1-extend
9585f757f3fSDimitry Andric         RHSKnown = RHSKnown.anyextOrTrunc(BitWidth);
9595f757f3fSDimitry Andric 
9605f757f3fSDimitry Andric         Known = LHSKnown & RHSKnown;
9615f757f3fSDimitry Andric         KnownBitsComputed = true;
9625f757f3fSDimitry Andric 
9635f757f3fSDimitry Andric         // If the client is only demanding bits we know to be zero, return
9645f757f3fSDimitry Andric         // `llvm.ptrmask(p, 0)`. We can't return `null` here due to pointer
9655f757f3fSDimitry Andric         // provenance, but making the mask zero will be easily optimizable in
9665f757f3fSDimitry Andric         // the backend.
9675f757f3fSDimitry Andric         if (DemandedMask.isSubsetOf(Known.Zero) &&
9685f757f3fSDimitry Andric             !match(I->getOperand(1), m_Zero()))
9695f757f3fSDimitry Andric           return replaceOperand(
9705f757f3fSDimitry Andric               *I, 1, Constant::getNullValue(I->getOperand(1)->getType()));
9715f757f3fSDimitry Andric 
9725f757f3fSDimitry Andric         // Mask in demanded space does nothing.
9735f757f3fSDimitry Andric         // NOTE: We may have attributes associated with the return value of the
9745f757f3fSDimitry Andric         // llvm.ptrmask intrinsic that will be lost when we just return the
9755f757f3fSDimitry Andric         // operand. We should try to preserve them.
9765f757f3fSDimitry Andric         if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
9775f757f3fSDimitry Andric           return I->getOperand(0);
9785f757f3fSDimitry Andric 
9795f757f3fSDimitry Andric         // If the RHS is a constant, see if we can simplify it.
9805f757f3fSDimitry Andric         if (ShrinkDemandedConstant(
9815f757f3fSDimitry Andric                 I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth)))
9825f757f3fSDimitry Andric           return I;
9835f757f3fSDimitry Andric 
9840fca6ea1SDimitry Andric         // Combine:
9850fca6ea1SDimitry Andric         // (ptrmask (getelementptr i8, ptr p, imm i), imm mask)
9860fca6ea1SDimitry Andric         //   -> (ptrmask (getelementptr i8, ptr p, imm (i & mask)), imm mask)
9870fca6ea1SDimitry Andric         // where only the low bits known to be zero in the pointer are changed
9880fca6ea1SDimitry Andric         Value *InnerPtr;
9890fca6ea1SDimitry Andric         uint64_t GEPIndex;
9900fca6ea1SDimitry Andric         uint64_t PtrMaskImmediate;
9910fca6ea1SDimitry Andric         if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
9920fca6ea1SDimitry Andric                          m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
9930fca6ea1SDimitry Andric                          m_ConstantInt(PtrMaskImmediate)))) {
9940fca6ea1SDimitry Andric 
9950fca6ea1SDimitry Andric           LHSKnown = computeKnownBits(InnerPtr, Depth + 1, I);
9960fca6ea1SDimitry Andric           if (!LHSKnown.isZero()) {
9970fca6ea1SDimitry Andric             const unsigned trailingZeros = LHSKnown.countMinTrailingZeros();
9980fca6ea1SDimitry Andric             uint64_t PointerAlignBits = (uint64_t(1) << trailingZeros) - 1;
9990fca6ea1SDimitry Andric 
10000fca6ea1SDimitry Andric             uint64_t HighBitsGEPIndex = GEPIndex & ~PointerAlignBits;
10010fca6ea1SDimitry Andric             uint64_t MaskedLowBitsGEPIndex =
10020fca6ea1SDimitry Andric                 GEPIndex & PointerAlignBits & PtrMaskImmediate;
10030fca6ea1SDimitry Andric 
10040fca6ea1SDimitry Andric             uint64_t MaskedGEPIndex = HighBitsGEPIndex | MaskedLowBitsGEPIndex;
10050fca6ea1SDimitry Andric 
10060fca6ea1SDimitry Andric             if (MaskedGEPIndex != GEPIndex) {
1007*71ac745dSDimitry Andric               auto *GEP = cast<GEPOperator>(II->getArgOperand(0));
10080fca6ea1SDimitry Andric               Builder.SetInsertPoint(I);
10090fca6ea1SDimitry Andric               Type *GEPIndexType =
10100fca6ea1SDimitry Andric                   DL.getIndexType(GEP->getPointerOperand()->getType());
10110fca6ea1SDimitry Andric               Value *MaskedGEP = Builder.CreateGEP(
10120fca6ea1SDimitry Andric                   GEP->getSourceElementType(), InnerPtr,
10130fca6ea1SDimitry Andric                   ConstantInt::get(GEPIndexType, MaskedGEPIndex),
10140fca6ea1SDimitry Andric                   GEP->getName(), GEP->isInBounds());
10150fca6ea1SDimitry Andric 
10160fca6ea1SDimitry Andric               replaceOperand(*I, 0, MaskedGEP);
10170fca6ea1SDimitry Andric               return I;
10180fca6ea1SDimitry Andric             }
10190fca6ea1SDimitry Andric           }
10200fca6ea1SDimitry Andric         }
10210fca6ea1SDimitry Andric 
10225f757f3fSDimitry Andric         break;
10235f757f3fSDimitry Andric       }
10245f757f3fSDimitry Andric 
10250b57cec5SDimitry Andric       case Intrinsic::fshr:
10260b57cec5SDimitry Andric       case Intrinsic::fshl: {
10270b57cec5SDimitry Andric         const APInt *SA;
10280b57cec5SDimitry Andric         if (!match(I->getOperand(2), m_APInt(SA)))
10290b57cec5SDimitry Andric           break;
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric         // Normalize to funnel shift left. APInt shifts of BitWidth are well-
10320b57cec5SDimitry Andric         // defined, so no need to special-case zero shifts here.
10330b57cec5SDimitry Andric         uint64_t ShiftAmt = SA->urem(BitWidth);
10340b57cec5SDimitry Andric         if (II->getIntrinsicID() == Intrinsic::fshr)
10350b57cec5SDimitry Andric           ShiftAmt = BitWidth - ShiftAmt;
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric         APInt DemandedMaskLHS(DemandedMask.lshr(ShiftAmt));
10380b57cec5SDimitry Andric         APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt));
103906c3fb27SDimitry Andric         if (I->getOperand(0) != I->getOperand(1)) {
104006c3fb27SDimitry Andric           if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown,
10410fca6ea1SDimitry Andric                                    Depth + 1, Q) ||
10420fca6ea1SDimitry Andric               SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1,
10430fca6ea1SDimitry Andric                                    Q))
10440b57cec5SDimitry Andric             return I;
104506c3fb27SDimitry Andric         } else { // fshl is a rotate
104606c3fb27SDimitry Andric           // Avoid converting rotate into funnel shift.
104706c3fb27SDimitry Andric           // Only simplify if one operand is constant.
104806c3fb27SDimitry Andric           LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, I);
104906c3fb27SDimitry Andric           if (DemandedMaskLHS.isSubsetOf(LHSKnown.Zero | LHSKnown.One) &&
105006c3fb27SDimitry Andric               !match(I->getOperand(0), m_SpecificInt(LHSKnown.One))) {
105106c3fb27SDimitry Andric             replaceOperand(*I, 0, Constant::getIntegerValue(VTy, LHSKnown.One));
105206c3fb27SDimitry Andric             return I;
105306c3fb27SDimitry Andric           }
105406c3fb27SDimitry Andric 
105506c3fb27SDimitry Andric           RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, I);
105606c3fb27SDimitry Andric           if (DemandedMaskRHS.isSubsetOf(RHSKnown.Zero | RHSKnown.One) &&
105706c3fb27SDimitry Andric               !match(I->getOperand(1), m_SpecificInt(RHSKnown.One))) {
105806c3fb27SDimitry Andric             replaceOperand(*I, 1, Constant::getIntegerValue(VTy, RHSKnown.One));
105906c3fb27SDimitry Andric             return I;
106006c3fb27SDimitry Andric           }
106106c3fb27SDimitry Andric         }
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric         Known.Zero = LHSKnown.Zero.shl(ShiftAmt) |
10640b57cec5SDimitry Andric                      RHSKnown.Zero.lshr(BitWidth - ShiftAmt);
10650b57cec5SDimitry Andric         Known.One = LHSKnown.One.shl(ShiftAmt) |
10660b57cec5SDimitry Andric                     RHSKnown.One.lshr(BitWidth - ShiftAmt);
10675ffd83dbSDimitry Andric         KnownBitsComputed = true;
10680b57cec5SDimitry Andric         break;
10690b57cec5SDimitry Andric       }
1070349cc55cSDimitry Andric       case Intrinsic::umax: {
1071349cc55cSDimitry Andric         // UMax(A, C) == A if ...
1072349cc55cSDimitry Andric         // The lowest non-zero bit of DemandMask is higher than the highest
1073349cc55cSDimitry Andric         // non-zero bit of C.
1074349cc55cSDimitry Andric         const APInt *C;
107506c3fb27SDimitry Andric         unsigned CTZ = DemandedMask.countr_zero();
1076349cc55cSDimitry Andric         if (match(II->getArgOperand(1), m_APInt(C)) &&
1077349cc55cSDimitry Andric             CTZ >= C->getActiveBits())
1078349cc55cSDimitry Andric           return II->getArgOperand(0);
1079349cc55cSDimitry Andric         break;
1080349cc55cSDimitry Andric       }
1081349cc55cSDimitry Andric       case Intrinsic::umin: {
1082349cc55cSDimitry Andric         // UMin(A, C) == A if ...
1083349cc55cSDimitry Andric         // The lowest non-zero bit of DemandMask is higher than the highest
1084349cc55cSDimitry Andric         // non-one bit of C.
1085349cc55cSDimitry Andric         // This comes from using DeMorgans on the above umax example.
1086349cc55cSDimitry Andric         const APInt *C;
108706c3fb27SDimitry Andric         unsigned CTZ = DemandedMask.countr_zero();
1088349cc55cSDimitry Andric         if (match(II->getArgOperand(1), m_APInt(C)) &&
108906c3fb27SDimitry Andric             CTZ >= C->getBitWidth() - C->countl_one())
1090349cc55cSDimitry Andric           return II->getArgOperand(0);
1091349cc55cSDimitry Andric         break;
1092349cc55cSDimitry Andric       }
1093e8d8bef9SDimitry Andric       default: {
1094e8d8bef9SDimitry Andric         // Handle target specific intrinsics
1095bdd1243dSDimitry Andric         std::optional<Value *> V = targetSimplifyDemandedUseBitsIntrinsic(
1096e8d8bef9SDimitry Andric             *II, DemandedMask, Known, KnownBitsComputed);
109781ad6265SDimitry Andric         if (V)
1098bdd1243dSDimitry Andric           return *V;
10995ffd83dbSDimitry Andric         break;
11000b57cec5SDimitry Andric       }
11010b57cec5SDimitry Andric       }
11020b57cec5SDimitry Andric     }
11035ffd83dbSDimitry Andric 
11045ffd83dbSDimitry Andric     if (!KnownBitsComputed)
11050fca6ea1SDimitry Andric       llvm::computeKnownBits(I, Known, Depth, Q);
11060b57cec5SDimitry Andric     break;
11070b57cec5SDimitry Andric   }
11085ffd83dbSDimitry Andric   }
11090b57cec5SDimitry Andric 
11100fca6ea1SDimitry Andric   if (I->getType()->isPointerTy()) {
11110fca6ea1SDimitry Andric     Align Alignment = I->getPointerAlignment(DL);
11125f757f3fSDimitry Andric     Known.Zero.setLowBits(Log2(Alignment));
11135f757f3fSDimitry Andric   }
11145f757f3fSDimitry Andric 
11150b57cec5SDimitry Andric   // If the client is only demanding bits that we know, return the known
11165f757f3fSDimitry Andric   // constant. We can't directly simplify pointers as a constant because of
11175f757f3fSDimitry Andric   // pointer provenance.
11185f757f3fSDimitry Andric   // TODO: We could return `(inttoptr const)` for pointers.
11190fca6ea1SDimitry Andric   if (!I->getType()->isPointerTy() &&
11200fca6ea1SDimitry Andric       DemandedMask.isSubsetOf(Known.Zero | Known.One))
11210b57cec5SDimitry Andric     return Constant::getIntegerValue(VTy, Known.One);
11225f757f3fSDimitry Andric 
11235f757f3fSDimitry Andric   if (VerifyKnownBits) {
11240fca6ea1SDimitry Andric     KnownBits ReferenceKnown = llvm::computeKnownBits(I, Depth, Q);
11255f757f3fSDimitry Andric     if (Known != ReferenceKnown) {
11260fca6ea1SDimitry Andric       errs() << "Mismatched known bits for " << *I << " in "
11275f757f3fSDimitry Andric              << I->getFunction()->getName() << "\n";
11285f757f3fSDimitry Andric       errs() << "computeKnownBits(): " << ReferenceKnown << "\n";
11295f757f3fSDimitry Andric       errs() << "SimplifyDemandedBits(): " << Known << "\n";
11305f757f3fSDimitry Andric       std::abort();
11315f757f3fSDimitry Andric     }
11325f757f3fSDimitry Andric   }
11335f757f3fSDimitry Andric 
11340b57cec5SDimitry Andric   return nullptr;
11350b57cec5SDimitry Andric }
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric /// Helper routine of SimplifyDemandedUseBits. It computes Known
11380b57cec5SDimitry Andric /// bits. It also tries to handle simplifications that can be done based on
11390b57cec5SDimitry Andric /// DemandedMask, but without modifying the Instruction.
SimplifyMultipleUseDemandedBits(Instruction * I,const APInt & DemandedMask,KnownBits & Known,unsigned Depth,const SimplifyQuery & Q)1140e8d8bef9SDimitry Andric Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
1141e8d8bef9SDimitry Andric     Instruction *I, const APInt &DemandedMask, KnownBits &Known, unsigned Depth,
11420fca6ea1SDimitry Andric     const SimplifyQuery &Q) {
11430b57cec5SDimitry Andric   unsigned BitWidth = DemandedMask.getBitWidth();
11440b57cec5SDimitry Andric   Type *ITy = I->getType();
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   KnownBits LHSKnown(BitWidth);
11470b57cec5SDimitry Andric   KnownBits RHSKnown(BitWidth);
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric   // Despite the fact that we can't simplify this instruction in all User's
11500b57cec5SDimitry Andric   // context, we can at least compute the known bits, and we can
11510b57cec5SDimitry Andric   // do simplifications that apply to *just* the one user if we know that
11520b57cec5SDimitry Andric   // this instruction has a simpler value in that context.
11530b57cec5SDimitry Andric   switch (I->getOpcode()) {
11540b57cec5SDimitry Andric   case Instruction::And: {
11550fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
11560fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
11575f757f3fSDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
11580fca6ea1SDimitry Andric                                          Depth, Q);
11590fca6ea1SDimitry Andric     computeKnownBitsFromContext(I, Known, Depth, Q);
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
11620b57cec5SDimitry Andric     // constant.
11635ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
11645ffd83dbSDimitry Andric       return Constant::getIntegerValue(ITy, Known.One);
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric     // If all of the demanded bits are known 1 on one side, return the other.
1167bdd1243dSDimitry Andric     // These bits cannot contribute to the result of the 'and' in this context.
11680b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
11690b57cec5SDimitry Andric       return I->getOperand(0);
11700b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
11710b57cec5SDimitry Andric       return I->getOperand(1);
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric     break;
11740b57cec5SDimitry Andric   }
11750b57cec5SDimitry Andric   case Instruction::Or: {
11760fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
11770fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
11785f757f3fSDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
11790fca6ea1SDimitry Andric                                          Depth, Q);
11800fca6ea1SDimitry Andric     computeKnownBitsFromContext(I, Known, Depth, Q);
11810b57cec5SDimitry Andric 
11820b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
11830b57cec5SDimitry Andric     // constant.
11845ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
11855ffd83dbSDimitry Andric       return Constant::getIntegerValue(ITy, Known.One);
11860b57cec5SDimitry Andric 
1187bdd1243dSDimitry Andric     // We can simplify (X|Y) -> X or Y in the user's context if we know that
1188bdd1243dSDimitry Andric     // only bits from X or Y are demanded.
1189bdd1243dSDimitry Andric     // If all of the demanded bits are known zero on one side, return the other.
1190bdd1243dSDimitry Andric     // These bits cannot contribute to the result of the 'or' in this context.
11910b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
11920b57cec5SDimitry Andric       return I->getOperand(0);
11930b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
11940b57cec5SDimitry Andric       return I->getOperand(1);
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric     break;
11970b57cec5SDimitry Andric   }
11980b57cec5SDimitry Andric   case Instruction::Xor: {
11990fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
12000fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
12015f757f3fSDimitry Andric     Known = analyzeKnownBitsFromAndXorOr(cast<Operator>(I), LHSKnown, RHSKnown,
12020fca6ea1SDimitry Andric                                          Depth, Q);
12030fca6ea1SDimitry Andric     computeKnownBitsFromContext(I, Known, Depth, Q);
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric     // If the client is only demanding bits that we know, return the known
12060b57cec5SDimitry Andric     // constant.
12075ffd83dbSDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
12085ffd83dbSDimitry Andric       return Constant::getIntegerValue(ITy, Known.One);
12090b57cec5SDimitry Andric 
1210bdd1243dSDimitry Andric     // We can simplify (X^Y) -> X or Y in the user's context if we know that
1211bdd1243dSDimitry Andric     // only bits from X or Y are demanded.
1212bdd1243dSDimitry Andric     // If all of the demanded bits are known zero on one side, return the other.
12130b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(RHSKnown.Zero))
12140b57cec5SDimitry Andric       return I->getOperand(0);
12150b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(LHSKnown.Zero))
12160b57cec5SDimitry Andric       return I->getOperand(1);
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric     break;
12190b57cec5SDimitry Andric   }
1220bdd1243dSDimitry Andric   case Instruction::Add: {
122106c3fb27SDimitry Andric     unsigned NLZ = DemandedMask.countl_zero();
1222bdd1243dSDimitry Andric     APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
1223bdd1243dSDimitry Andric 
1224bdd1243dSDimitry Andric     // If an operand adds zeros to every bit below the highest demanded bit,
1225bdd1243dSDimitry Andric     // that operand doesn't change the result. Return the other side.
12260fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1227bdd1243dSDimitry Andric     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
1228bdd1243dSDimitry Andric       return I->getOperand(0);
1229bdd1243dSDimitry Andric 
12300fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1231bdd1243dSDimitry Andric     if (DemandedFromOps.isSubsetOf(LHSKnown.Zero))
1232bdd1243dSDimitry Andric       return I->getOperand(1);
1233bdd1243dSDimitry Andric 
123406c3fb27SDimitry Andric     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
12350fca6ea1SDimitry Andric     bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
12360fca6ea1SDimitry Andric     Known =
12370fca6ea1SDimitry Andric         KnownBits::computeForAddSub(/*Add=*/true, NSW, NUW, LHSKnown, RHSKnown);
12380fca6ea1SDimitry Andric     computeKnownBitsFromContext(I, Known, Depth, Q);
1239bdd1243dSDimitry Andric     break;
1240bdd1243dSDimitry Andric   }
1241bdd1243dSDimitry Andric   case Instruction::Sub: {
124206c3fb27SDimitry Andric     unsigned NLZ = DemandedMask.countl_zero();
1243bdd1243dSDimitry Andric     APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
1244bdd1243dSDimitry Andric 
1245bdd1243dSDimitry Andric     // If an operand subtracts zeros from every bit below the highest demanded
1246bdd1243dSDimitry Andric     // bit, that operand doesn't change the result. Return the other side.
12470fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1248bdd1243dSDimitry Andric     if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
1249bdd1243dSDimitry Andric       return I->getOperand(0);
1250bdd1243dSDimitry Andric 
125106c3fb27SDimitry Andric     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
12520fca6ea1SDimitry Andric     bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
12530fca6ea1SDimitry Andric     llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
12540fca6ea1SDimitry Andric     Known = KnownBits::computeForAddSub(/*Add=*/false, NSW, NUW, LHSKnown,
12550fca6ea1SDimitry Andric                                         RHSKnown);
12560fca6ea1SDimitry Andric     computeKnownBitsFromContext(I, Known, Depth, Q);
1257bdd1243dSDimitry Andric     break;
1258bdd1243dSDimitry Andric   }
1259e8d8bef9SDimitry Andric   case Instruction::AShr: {
1260e8d8bef9SDimitry Andric     // Compute the Known bits to simplify things downstream.
12610fca6ea1SDimitry Andric     llvm::computeKnownBits(I, Known, Depth, Q);
1262e8d8bef9SDimitry Andric 
1263e8d8bef9SDimitry Andric     // If this user is only demanding bits that we know, return the known
1264e8d8bef9SDimitry Andric     // constant.
1265e8d8bef9SDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero | Known.One))
1266e8d8bef9SDimitry Andric       return Constant::getIntegerValue(ITy, Known.One);
1267e8d8bef9SDimitry Andric 
1268e8d8bef9SDimitry Andric     // If the right shift operand 0 is a result of a left shift by the same
1269e8d8bef9SDimitry Andric     // amount, this is probably a zero/sign extension, which may be unnecessary,
1270e8d8bef9SDimitry Andric     // if we do not demand any of the new sign bits. So, return the original
1271e8d8bef9SDimitry Andric     // operand instead.
1272e8d8bef9SDimitry Andric     const APInt *ShiftRC;
1273e8d8bef9SDimitry Andric     const APInt *ShiftLC;
1274e8d8bef9SDimitry Andric     Value *X;
1275e8d8bef9SDimitry Andric     unsigned BitWidth = DemandedMask.getBitWidth();
1276e8d8bef9SDimitry Andric     if (match(I,
1277e8d8bef9SDimitry Andric               m_AShr(m_Shl(m_Value(X), m_APInt(ShiftLC)), m_APInt(ShiftRC))) &&
1278fe6060f1SDimitry Andric         ShiftLC == ShiftRC && ShiftLC->ult(BitWidth) &&
1279e8d8bef9SDimitry Andric         DemandedMask.isSubsetOf(APInt::getLowBitsSet(
1280e8d8bef9SDimitry Andric             BitWidth, BitWidth - ShiftRC->getZExtValue()))) {
1281e8d8bef9SDimitry Andric       return X;
1282e8d8bef9SDimitry Andric     }
1283e8d8bef9SDimitry Andric 
1284e8d8bef9SDimitry Andric     break;
1285e8d8bef9SDimitry Andric   }
12860b57cec5SDimitry Andric   default:
12870b57cec5SDimitry Andric     // Compute the Known bits to simplify things downstream.
12880fca6ea1SDimitry Andric     llvm::computeKnownBits(I, Known, Depth, Q);
12890b57cec5SDimitry Andric 
12900b57cec5SDimitry Andric     // If this user is only demanding bits that we know, return the known
12910b57cec5SDimitry Andric     // constant.
12920b57cec5SDimitry Andric     if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
12930b57cec5SDimitry Andric       return Constant::getIntegerValue(ITy, Known.One);
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric     break;
12960b57cec5SDimitry Andric   }
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric   return nullptr;
12990b57cec5SDimitry Andric }
13000b57cec5SDimitry Andric 
13010b57cec5SDimitry Andric /// Helper routine of SimplifyDemandedUseBits. It tries to simplify
13020b57cec5SDimitry Andric /// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
13030b57cec5SDimitry Andric /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
13040b57cec5SDimitry Andric /// of "C2-C1".
13050b57cec5SDimitry Andric ///
13060b57cec5SDimitry Andric /// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
13070b57cec5SDimitry Andric /// ..., bn}, without considering the specific value X is holding.
13080b57cec5SDimitry Andric /// This transformation is legal iff one of following conditions is hold:
13090b57cec5SDimitry Andric ///  1) All the bit in S are 0, in this case E1 == E2.
13100b57cec5SDimitry Andric ///  2) We don't care those bits in S, per the input DemandedMask.
13110b57cec5SDimitry Andric ///  3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
13120b57cec5SDimitry Andric ///     rest bits.
13130b57cec5SDimitry Andric ///
13140b57cec5SDimitry Andric /// Currently we only test condition 2).
13150b57cec5SDimitry Andric ///
13160b57cec5SDimitry Andric /// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
13170b57cec5SDimitry Andric /// not successful.
simplifyShrShlDemandedBits(Instruction * Shr,const APInt & ShrOp1,Instruction * Shl,const APInt & ShlOp1,const APInt & DemandedMask,KnownBits & Known)1318e8d8bef9SDimitry Andric Value *InstCombinerImpl::simplifyShrShlDemandedBits(
1319e8d8bef9SDimitry Andric     Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
1320e8d8bef9SDimitry Andric     const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known) {
13210b57cec5SDimitry Andric   if (!ShlOp1 || !ShrOp1)
13220b57cec5SDimitry Andric     return nullptr; // No-op.
13230b57cec5SDimitry Andric 
13240b57cec5SDimitry Andric   Value *VarX = Shr->getOperand(0);
13250b57cec5SDimitry Andric   Type *Ty = VarX->getType();
13260b57cec5SDimitry Andric   unsigned BitWidth = Ty->getScalarSizeInBits();
13270b57cec5SDimitry Andric   if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
13280b57cec5SDimitry Andric     return nullptr; // Undef.
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric   unsigned ShlAmt = ShlOp1.getZExtValue();
13310b57cec5SDimitry Andric   unsigned ShrAmt = ShrOp1.getZExtValue();
13320b57cec5SDimitry Andric 
13330b57cec5SDimitry Andric   Known.One.clearAllBits();
13340b57cec5SDimitry Andric   Known.Zero.setLowBits(ShlAmt - 1);
13350b57cec5SDimitry Andric   Known.Zero &= DemandedMask;
13360b57cec5SDimitry Andric 
1337349cc55cSDimitry Andric   APInt BitMask1(APInt::getAllOnes(BitWidth));
1338349cc55cSDimitry Andric   APInt BitMask2(APInt::getAllOnes(BitWidth));
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric   bool isLshr = (Shr->getOpcode() == Instruction::LShr);
13410b57cec5SDimitry Andric   BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
13420b57cec5SDimitry Andric                       (BitMask1.ashr(ShrAmt) << ShlAmt);
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric   if (ShrAmt <= ShlAmt) {
13450b57cec5SDimitry Andric     BitMask2 <<= (ShlAmt - ShrAmt);
13460b57cec5SDimitry Andric   } else {
13470b57cec5SDimitry Andric     BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
13480b57cec5SDimitry Andric                         BitMask2.ashr(ShrAmt - ShlAmt);
13490b57cec5SDimitry Andric   }
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   // Check if condition-2 (see the comment to this function) is satified.
13520b57cec5SDimitry Andric   if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
13530b57cec5SDimitry Andric     if (ShrAmt == ShlAmt)
13540b57cec5SDimitry Andric       return VarX;
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric     if (!Shr->hasOneUse())
13570b57cec5SDimitry Andric       return nullptr;
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric     BinaryOperator *New;
13600b57cec5SDimitry Andric     if (ShrAmt < ShlAmt) {
13610b57cec5SDimitry Andric       Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
13620b57cec5SDimitry Andric       New = BinaryOperator::CreateShl(VarX, Amt);
13630b57cec5SDimitry Andric       BinaryOperator *Orig = cast<BinaryOperator>(Shl);
13640b57cec5SDimitry Andric       New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
13650b57cec5SDimitry Andric       New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
13660b57cec5SDimitry Andric     } else {
13670b57cec5SDimitry Andric       Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
13680b57cec5SDimitry Andric       New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
13690b57cec5SDimitry Andric                      BinaryOperator::CreateAShr(VarX, Amt);
13700b57cec5SDimitry Andric       if (cast<BinaryOperator>(Shr)->isExact())
13710b57cec5SDimitry Andric         New->setIsExact(true);
13720b57cec5SDimitry Andric     }
13730b57cec5SDimitry Andric 
13745f757f3fSDimitry Andric     return InsertNewInstWith(New, Shl->getIterator());
13750b57cec5SDimitry Andric   }
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric   return nullptr;
13780b57cec5SDimitry Andric }
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric /// The specified value produces a vector with any number of elements.
1381cb14a3feSDimitry Andric /// This method analyzes which elements of the operand are poison and
1382cb14a3feSDimitry Andric /// returns that information in PoisonElts.
13838bcb0991SDimitry Andric ///
13840b57cec5SDimitry Andric /// DemandedElts contains the set of elements that are actually used by the
13858bcb0991SDimitry Andric /// caller, and by default (AllowMultipleUsers equals false) the value is
13868bcb0991SDimitry Andric /// simplified only if it has a single caller. If AllowMultipleUsers is set
13878bcb0991SDimitry Andric /// to true, DemandedElts refers to the union of sets of elements that are
13888bcb0991SDimitry Andric /// used by all callers.
13890b57cec5SDimitry Andric ///
13900b57cec5SDimitry Andric /// If the information about demanded elements can be used to simplify the
13910b57cec5SDimitry Andric /// operation, the operation is simplified, then the resultant value is
13920b57cec5SDimitry Andric /// returned.  This returns null if no change was made.
SimplifyDemandedVectorElts(Value * V,APInt DemandedElts,APInt & PoisonElts,unsigned Depth,bool AllowMultipleUsers)1393e8d8bef9SDimitry Andric Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
1394e8d8bef9SDimitry Andric                                                     APInt DemandedElts,
1395cb14a3feSDimitry Andric                                                     APInt &PoisonElts,
13968bcb0991SDimitry Andric                                                     unsigned Depth,
13978bcb0991SDimitry Andric                                                     bool AllowMultipleUsers) {
13985ffd83dbSDimitry Andric   // Cannot analyze scalable type. The number of vector elements is not a
13995ffd83dbSDimitry Andric   // compile-time constant.
14005ffd83dbSDimitry Andric   if (isa<ScalableVectorType>(V->getType()))
14015ffd83dbSDimitry Andric     return nullptr;
14025ffd83dbSDimitry Andric 
14035ffd83dbSDimitry Andric   unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements();
1404349cc55cSDimitry Andric   APInt EltMask(APInt::getAllOnes(VWidth));
14050b57cec5SDimitry Andric   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
14060b57cec5SDimitry Andric 
1407cb14a3feSDimitry Andric   if (match(V, m_Poison())) {
1408cb14a3feSDimitry Andric     // If the entire vector is poison, just return this info.
1409cb14a3feSDimitry Andric     PoisonElts = EltMask;
14100b57cec5SDimitry Andric     return nullptr;
14110b57cec5SDimitry Andric   }
14120b57cec5SDimitry Andric 
1413349cc55cSDimitry Andric   if (DemandedElts.isZero()) { // If nothing is demanded, provide poison.
1414cb14a3feSDimitry Andric     PoisonElts = EltMask;
1415e8d8bef9SDimitry Andric     return PoisonValue::get(V->getType());
14160b57cec5SDimitry Andric   }
14170b57cec5SDimitry Andric 
1418cb14a3feSDimitry Andric   PoisonElts = 0;
14190b57cec5SDimitry Andric 
14200b57cec5SDimitry Andric   if (auto *C = dyn_cast<Constant>(V)) {
14210b57cec5SDimitry Andric     // Check if this is identity. If so, return 0 since we are not simplifying
14220b57cec5SDimitry Andric     // anything.
1423349cc55cSDimitry Andric     if (DemandedElts.isAllOnes())
14240b57cec5SDimitry Andric       return nullptr;
14250b57cec5SDimitry Andric 
14260b57cec5SDimitry Andric     Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1427e8d8bef9SDimitry Andric     Constant *Poison = PoisonValue::get(EltTy);
14280b57cec5SDimitry Andric     SmallVector<Constant*, 16> Elts;
14290b57cec5SDimitry Andric     for (unsigned i = 0; i != VWidth; ++i) {
1430e8d8bef9SDimitry Andric       if (!DemandedElts[i]) {   // If not demanded, set to poison.
1431e8d8bef9SDimitry Andric         Elts.push_back(Poison);
1432cb14a3feSDimitry Andric         PoisonElts.setBit(i);
14330b57cec5SDimitry Andric         continue;
14340b57cec5SDimitry Andric       }
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric       Constant *Elt = C->getAggregateElement(i);
14370b57cec5SDimitry Andric       if (!Elt) return nullptr;
14380b57cec5SDimitry Andric 
14390b57cec5SDimitry Andric       Elts.push_back(Elt);
1440cb14a3feSDimitry Andric       if (isa<PoisonValue>(Elt)) // Already poison.
1441cb14a3feSDimitry Andric         PoisonElts.setBit(i);
14420b57cec5SDimitry Andric     }
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric     // If we changed the constant, return it.
14450b57cec5SDimitry Andric     Constant *NewCV = ConstantVector::get(Elts);
14460b57cec5SDimitry Andric     return NewCV != C ? NewCV : nullptr;
14470b57cec5SDimitry Andric   }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric   // Limit search depth.
14500b57cec5SDimitry Andric   if (Depth == 10)
14510b57cec5SDimitry Andric     return nullptr;
14520b57cec5SDimitry Andric 
14538bcb0991SDimitry Andric   if (!AllowMultipleUsers) {
14540b57cec5SDimitry Andric     // If multiple users are using the root value, proceed with
14550b57cec5SDimitry Andric     // simplification conservatively assuming that all elements
14560b57cec5SDimitry Andric     // are needed.
14570b57cec5SDimitry Andric     if (!V->hasOneUse()) {
14580b57cec5SDimitry Andric       // Quit if we find multiple users of a non-root value though.
14590b57cec5SDimitry Andric       // They'll be handled when it's their turn to be visited by
14600b57cec5SDimitry Andric       // the main instcombine process.
14610b57cec5SDimitry Andric       if (Depth != 0)
1462cb14a3feSDimitry Andric         // TODO: Just compute the PoisonElts information recursively.
14630b57cec5SDimitry Andric         return nullptr;
14640b57cec5SDimitry Andric 
14650b57cec5SDimitry Andric       // Conservatively assume that all elements are needed.
14660b57cec5SDimitry Andric       DemandedElts = EltMask;
14670b57cec5SDimitry Andric     }
14688bcb0991SDimitry Andric   }
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   Instruction *I = dyn_cast<Instruction>(V);
14710b57cec5SDimitry Andric   if (!I) return nullptr;        // Only analyze instructions.
14720b57cec5SDimitry Andric 
14730b57cec5SDimitry Andric   bool MadeChange = false;
14740b57cec5SDimitry Andric   auto simplifyAndSetOp = [&](Instruction *Inst, unsigned OpNum,
14750b57cec5SDimitry Andric                               APInt Demanded, APInt &Undef) {
14760b57cec5SDimitry Andric     auto *II = dyn_cast<IntrinsicInst>(Inst);
14770b57cec5SDimitry Andric     Value *Op = II ? II->getArgOperand(OpNum) : Inst->getOperand(OpNum);
14780b57cec5SDimitry Andric     if (Value *V = SimplifyDemandedVectorElts(Op, Demanded, Undef, Depth + 1)) {
14795ffd83dbSDimitry Andric       replaceOperand(*Inst, OpNum, V);
14800b57cec5SDimitry Andric       MadeChange = true;
14810b57cec5SDimitry Andric     }
14820b57cec5SDimitry Andric   };
14830b57cec5SDimitry Andric 
1484cb14a3feSDimitry Andric   APInt PoisonElts2(VWidth, 0);
1485cb14a3feSDimitry Andric   APInt PoisonElts3(VWidth, 0);
14860b57cec5SDimitry Andric   switch (I->getOpcode()) {
14870b57cec5SDimitry Andric   default: break;
14880b57cec5SDimitry Andric 
14890b57cec5SDimitry Andric   case Instruction::GetElementPtr: {
14900b57cec5SDimitry Andric     // The LangRef requires that struct geps have all constant indices.  As
14910b57cec5SDimitry Andric     // such, we can't convert any operand to partial undef.
14920b57cec5SDimitry Andric     auto mayIndexStructType = [](GetElementPtrInst &GEP) {
14930b57cec5SDimitry Andric       for (auto I = gep_type_begin(GEP), E = gep_type_end(GEP);
14940b57cec5SDimitry Andric            I != E; I++)
14950b57cec5SDimitry Andric         if (I.isStruct())
14961fd87a68SDimitry Andric           return true;
14970b57cec5SDimitry Andric       return false;
14980b57cec5SDimitry Andric     };
14990b57cec5SDimitry Andric     if (mayIndexStructType(cast<GetElementPtrInst>(*I)))
15000b57cec5SDimitry Andric       break;
15010b57cec5SDimitry Andric 
15020b57cec5SDimitry Andric     // Conservatively track the demanded elements back through any vector
15030b57cec5SDimitry Andric     // operands we may have.  We know there must be at least one, or we
15040b57cec5SDimitry Andric     // wouldn't have a vector result to get here. Note that we intentionally
15051fd87a68SDimitry Andric     // merge the undef bits here since gepping with either an poison base or
15061fd87a68SDimitry Andric     // index results in poison.
15070b57cec5SDimitry Andric     for (unsigned i = 0; i < I->getNumOperands(); i++) {
15081fd87a68SDimitry Andric       if (i == 0 ? match(I->getOperand(i), m_Undef())
15091fd87a68SDimitry Andric                  : match(I->getOperand(i), m_Poison())) {
15100b57cec5SDimitry Andric         // If the entire vector is undefined, just return this info.
1511cb14a3feSDimitry Andric         PoisonElts = EltMask;
15120b57cec5SDimitry Andric         return nullptr;
15130b57cec5SDimitry Andric       }
15140b57cec5SDimitry Andric       if (I->getOperand(i)->getType()->isVectorTy()) {
1515cb14a3feSDimitry Andric         APInt PoisonEltsOp(VWidth, 0);
1516cb14a3feSDimitry Andric         simplifyAndSetOp(I, i, DemandedElts, PoisonEltsOp);
15171fd87a68SDimitry Andric         // gep(x, undef) is not undef, so skip considering idx ops here
15181fd87a68SDimitry Andric         // Note that we could propagate poison, but we can't distinguish between
15191fd87a68SDimitry Andric         // undef & poison bits ATM
15201fd87a68SDimitry Andric         if (i == 0)
1521cb14a3feSDimitry Andric           PoisonElts |= PoisonEltsOp;
15220b57cec5SDimitry Andric       }
15230b57cec5SDimitry Andric     }
15240b57cec5SDimitry Andric 
15250b57cec5SDimitry Andric     break;
15260b57cec5SDimitry Andric   }
15270b57cec5SDimitry Andric   case Instruction::InsertElement: {
15280b57cec5SDimitry Andric     // If this is a variable index, we don't know which element it overwrites.
15290b57cec5SDimitry Andric     // demand exactly the same input as we produce.
15300b57cec5SDimitry Andric     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
15310b57cec5SDimitry Andric     if (!Idx) {
15320b57cec5SDimitry Andric       // Note that we can't propagate undef elt info, because we don't know
15330b57cec5SDimitry Andric       // which elt is getting updated.
1534cb14a3feSDimitry Andric       simplifyAndSetOp(I, 0, DemandedElts, PoisonElts2);
15350b57cec5SDimitry Andric       break;
15360b57cec5SDimitry Andric     }
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric     // The element inserted overwrites whatever was there, so the input demanded
15390b57cec5SDimitry Andric     // set is simpler than the output set.
15400b57cec5SDimitry Andric     unsigned IdxNo = Idx->getZExtValue();
15410b57cec5SDimitry Andric     APInt PreInsertDemandedElts = DemandedElts;
15420b57cec5SDimitry Andric     if (IdxNo < VWidth)
15430b57cec5SDimitry Andric       PreInsertDemandedElts.clearBit(IdxNo);
15440b57cec5SDimitry Andric 
1545e8d8bef9SDimitry Andric     // If we only demand the element that is being inserted and that element
1546e8d8bef9SDimitry Andric     // was extracted from the same index in another vector with the same type,
1547e8d8bef9SDimitry Andric     // replace this insert with that other vector.
1548e8d8bef9SDimitry Andric     // Note: This is attempted before the call to simplifyAndSetOp because that
1549cb14a3feSDimitry Andric     //       may change PoisonElts to a value that does not match with Vec.
1550e8d8bef9SDimitry Andric     Value *Vec;
1551e8d8bef9SDimitry Andric     if (PreInsertDemandedElts == 0 &&
1552e8d8bef9SDimitry Andric         match(I->getOperand(1),
1553e8d8bef9SDimitry Andric               m_ExtractElt(m_Value(Vec), m_SpecificInt(IdxNo))) &&
1554e8d8bef9SDimitry Andric         Vec->getType() == I->getType()) {
1555e8d8bef9SDimitry Andric       return Vec;
1556e8d8bef9SDimitry Andric     }
1557e8d8bef9SDimitry Andric 
1558cb14a3feSDimitry Andric     simplifyAndSetOp(I, 0, PreInsertDemandedElts, PoisonElts);
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric     // If this is inserting an element that isn't demanded, remove this
15610b57cec5SDimitry Andric     // insertelement.
15620b57cec5SDimitry Andric     if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
15635ffd83dbSDimitry Andric       Worklist.push(I);
15640b57cec5SDimitry Andric       return I->getOperand(0);
15650b57cec5SDimitry Andric     }
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric     // The inserted element is defined.
1568cb14a3feSDimitry Andric     PoisonElts.clearBit(IdxNo);
15690b57cec5SDimitry Andric     break;
15700b57cec5SDimitry Andric   }
15710b57cec5SDimitry Andric   case Instruction::ShuffleVector: {
1572480093f4SDimitry Andric     auto *Shuffle = cast<ShuffleVectorInst>(I);
1573480093f4SDimitry Andric     assert(Shuffle->getOperand(0)->getType() ==
1574480093f4SDimitry Andric            Shuffle->getOperand(1)->getType() &&
1575480093f4SDimitry Andric            "Expected shuffle operands to have same type");
1576e8d8bef9SDimitry Andric     unsigned OpWidth = cast<FixedVectorType>(Shuffle->getOperand(0)->getType())
1577e8d8bef9SDimitry Andric                            ->getNumElements();
15785ffd83dbSDimitry Andric     // Handle trivial case of a splat. Only check the first element of LHS
15795ffd83dbSDimitry Andric     // operand.
15805ffd83dbSDimitry Andric     if (all_of(Shuffle->getShuffleMask(), [](int Elt) { return Elt == 0; }) &&
1581349cc55cSDimitry Andric         DemandedElts.isAllOnes()) {
1582cb14a3feSDimitry Andric       if (!isa<PoisonValue>(I->getOperand(1))) {
1583fe6060f1SDimitry Andric         I->setOperand(1, PoisonValue::get(I->getOperand(1)->getType()));
15845ffd83dbSDimitry Andric         MadeChange = true;
15855ffd83dbSDimitry Andric       }
15865ffd83dbSDimitry Andric       APInt LeftDemanded(OpWidth, 1);
1587cb14a3feSDimitry Andric       APInt LHSPoisonElts(OpWidth, 0);
1588cb14a3feSDimitry Andric       simplifyAndSetOp(I, 0, LeftDemanded, LHSPoisonElts);
1589cb14a3feSDimitry Andric       if (LHSPoisonElts[0])
1590cb14a3feSDimitry Andric         PoisonElts = EltMask;
15915ffd83dbSDimitry Andric       else
1592cb14a3feSDimitry Andric         PoisonElts.clearAllBits();
15935ffd83dbSDimitry Andric       break;
15945ffd83dbSDimitry Andric     }
15955ffd83dbSDimitry Andric 
1596480093f4SDimitry Andric     APInt LeftDemanded(OpWidth, 0), RightDemanded(OpWidth, 0);
15970b57cec5SDimitry Andric     for (unsigned i = 0; i < VWidth; i++) {
15980b57cec5SDimitry Andric       if (DemandedElts[i]) {
15990b57cec5SDimitry Andric         unsigned MaskVal = Shuffle->getMaskValue(i);
16000b57cec5SDimitry Andric         if (MaskVal != -1u) {
1601480093f4SDimitry Andric           assert(MaskVal < OpWidth * 2 &&
16020b57cec5SDimitry Andric                  "shufflevector mask index out of range!");
1603480093f4SDimitry Andric           if (MaskVal < OpWidth)
16040b57cec5SDimitry Andric             LeftDemanded.setBit(MaskVal);
16050b57cec5SDimitry Andric           else
1606480093f4SDimitry Andric             RightDemanded.setBit(MaskVal - OpWidth);
16070b57cec5SDimitry Andric         }
16080b57cec5SDimitry Andric       }
16090b57cec5SDimitry Andric     }
16100b57cec5SDimitry Andric 
1611cb14a3feSDimitry Andric     APInt LHSPoisonElts(OpWidth, 0);
1612cb14a3feSDimitry Andric     simplifyAndSetOp(I, 0, LeftDemanded, LHSPoisonElts);
16130b57cec5SDimitry Andric 
1614cb14a3feSDimitry Andric     APInt RHSPoisonElts(OpWidth, 0);
1615cb14a3feSDimitry Andric     simplifyAndSetOp(I, 1, RightDemanded, RHSPoisonElts);
16160b57cec5SDimitry Andric 
1617480093f4SDimitry Andric     // If this shuffle does not change the vector length and the elements
1618480093f4SDimitry Andric     // demanded by this shuffle are an identity mask, then this shuffle is
1619480093f4SDimitry Andric     // unnecessary.
1620480093f4SDimitry Andric     //
1621480093f4SDimitry Andric     // We are assuming canonical form for the mask, so the source vector is
1622480093f4SDimitry Andric     // operand 0 and operand 1 is not used.
1623480093f4SDimitry Andric     //
1624480093f4SDimitry Andric     // Note that if an element is demanded and this shuffle mask is undefined
1625480093f4SDimitry Andric     // for that element, then the shuffle is not considered an identity
1626480093f4SDimitry Andric     // operation. The shuffle prevents poison from the operand vector from
1627480093f4SDimitry Andric     // leaking to the result by replacing poison with an undefined value.
1628480093f4SDimitry Andric     if (VWidth == OpWidth) {
1629480093f4SDimitry Andric       bool IsIdentityShuffle = true;
1630480093f4SDimitry Andric       for (unsigned i = 0; i < VWidth; i++) {
1631480093f4SDimitry Andric         unsigned MaskVal = Shuffle->getMaskValue(i);
1632480093f4SDimitry Andric         if (DemandedElts[i] && i != MaskVal) {
1633480093f4SDimitry Andric           IsIdentityShuffle = false;
1634480093f4SDimitry Andric           break;
1635480093f4SDimitry Andric         }
1636480093f4SDimitry Andric       }
1637480093f4SDimitry Andric       if (IsIdentityShuffle)
1638480093f4SDimitry Andric         return Shuffle->getOperand(0);
1639480093f4SDimitry Andric     }
1640480093f4SDimitry Andric 
1641cb14a3feSDimitry Andric     bool NewPoisonElts = false;
16420b57cec5SDimitry Andric     unsigned LHSIdx = -1u, LHSValIdx = -1u;
16430b57cec5SDimitry Andric     unsigned RHSIdx = -1u, RHSValIdx = -1u;
16440b57cec5SDimitry Andric     bool LHSUniform = true;
16450b57cec5SDimitry Andric     bool RHSUniform = true;
16460b57cec5SDimitry Andric     for (unsigned i = 0; i < VWidth; i++) {
16470b57cec5SDimitry Andric       unsigned MaskVal = Shuffle->getMaskValue(i);
16480b57cec5SDimitry Andric       if (MaskVal == -1u) {
1649cb14a3feSDimitry Andric         PoisonElts.setBit(i);
16500b57cec5SDimitry Andric       } else if (!DemandedElts[i]) {
1651cb14a3feSDimitry Andric         NewPoisonElts = true;
1652cb14a3feSDimitry Andric         PoisonElts.setBit(i);
1653480093f4SDimitry Andric       } else if (MaskVal < OpWidth) {
1654cb14a3feSDimitry Andric         if (LHSPoisonElts[MaskVal]) {
1655cb14a3feSDimitry Andric           NewPoisonElts = true;
1656cb14a3feSDimitry Andric           PoisonElts.setBit(i);
16570b57cec5SDimitry Andric         } else {
1658480093f4SDimitry Andric           LHSIdx = LHSIdx == -1u ? i : OpWidth;
1659480093f4SDimitry Andric           LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth;
16600b57cec5SDimitry Andric           LHSUniform = LHSUniform && (MaskVal == i);
16610b57cec5SDimitry Andric         }
16620b57cec5SDimitry Andric       } else {
1663cb14a3feSDimitry Andric         if (RHSPoisonElts[MaskVal - OpWidth]) {
1664cb14a3feSDimitry Andric           NewPoisonElts = true;
1665cb14a3feSDimitry Andric           PoisonElts.setBit(i);
16660b57cec5SDimitry Andric         } else {
1667480093f4SDimitry Andric           RHSIdx = RHSIdx == -1u ? i : OpWidth;
1668480093f4SDimitry Andric           RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth;
1669480093f4SDimitry Andric           RHSUniform = RHSUniform && (MaskVal - OpWidth == i);
16700b57cec5SDimitry Andric         }
16710b57cec5SDimitry Andric       }
16720b57cec5SDimitry Andric     }
16730b57cec5SDimitry Andric 
16740b57cec5SDimitry Andric     // Try to transform shuffle with constant vector and single element from
16750b57cec5SDimitry Andric     // this constant vector to single insertelement instruction.
16760b57cec5SDimitry Andric     // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
16770b57cec5SDimitry Andric     // insertelement V, C[ci], ci-n
1678e8d8bef9SDimitry Andric     if (OpWidth ==
1679e8d8bef9SDimitry Andric         cast<FixedVectorType>(Shuffle->getType())->getNumElements()) {
16800b57cec5SDimitry Andric       Value *Op = nullptr;
16810b57cec5SDimitry Andric       Constant *Value = nullptr;
16820b57cec5SDimitry Andric       unsigned Idx = -1u;
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric       // Find constant vector with the single element in shuffle (LHS or RHS).
1685480093f4SDimitry Andric       if (LHSIdx < OpWidth && RHSUniform) {
16860b57cec5SDimitry Andric         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
16870b57cec5SDimitry Andric           Op = Shuffle->getOperand(1);
16880b57cec5SDimitry Andric           Value = CV->getOperand(LHSValIdx);
16890b57cec5SDimitry Andric           Idx = LHSIdx;
16900b57cec5SDimitry Andric         }
16910b57cec5SDimitry Andric       }
1692480093f4SDimitry Andric       if (RHSIdx < OpWidth && LHSUniform) {
16930b57cec5SDimitry Andric         if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
16940b57cec5SDimitry Andric           Op = Shuffle->getOperand(0);
16950b57cec5SDimitry Andric           Value = CV->getOperand(RHSValIdx);
16960b57cec5SDimitry Andric           Idx = RHSIdx;
16970b57cec5SDimitry Andric         }
16980b57cec5SDimitry Andric       }
16990b57cec5SDimitry Andric       // Found constant vector with single element - convert to insertelement.
17000b57cec5SDimitry Andric       if (Op && Value) {
17010b57cec5SDimitry Andric         Instruction *New = InsertElementInst::Create(
170206c3fb27SDimitry Andric             Op, Value, ConstantInt::get(Type::getInt64Ty(I->getContext()), Idx),
17030b57cec5SDimitry Andric             Shuffle->getName());
17045f757f3fSDimitry Andric         InsertNewInstWith(New, Shuffle->getIterator());
17050b57cec5SDimitry Andric         return New;
17060b57cec5SDimitry Andric       }
17070b57cec5SDimitry Andric     }
1708cb14a3feSDimitry Andric     if (NewPoisonElts) {
17090b57cec5SDimitry Andric       // Add additional discovered undefs.
17105ffd83dbSDimitry Andric       SmallVector<int, 16> Elts;
17110b57cec5SDimitry Andric       for (unsigned i = 0; i < VWidth; ++i) {
1712cb14a3feSDimitry Andric         if (PoisonElts[i])
171306c3fb27SDimitry Andric           Elts.push_back(PoisonMaskElem);
17140b57cec5SDimitry Andric         else
17155ffd83dbSDimitry Andric           Elts.push_back(Shuffle->getMaskValue(i));
17160b57cec5SDimitry Andric       }
17175ffd83dbSDimitry Andric       Shuffle->setShuffleMask(Elts);
17180b57cec5SDimitry Andric       MadeChange = true;
17190b57cec5SDimitry Andric     }
17200b57cec5SDimitry Andric     break;
17210b57cec5SDimitry Andric   }
17220b57cec5SDimitry Andric   case Instruction::Select: {
17230b57cec5SDimitry Andric     // If this is a vector select, try to transform the select condition based
17240b57cec5SDimitry Andric     // on the current demanded elements.
17250b57cec5SDimitry Andric     SelectInst *Sel = cast<SelectInst>(I);
17260b57cec5SDimitry Andric     if (Sel->getCondition()->getType()->isVectorTy()) {
1727cb14a3feSDimitry Andric       // TODO: We are not doing anything with PoisonElts based on this call.
17280b57cec5SDimitry Andric       // It is overwritten below based on the other select operands. If an
17290b57cec5SDimitry Andric       // element of the select condition is known undef, then we are free to
17300b57cec5SDimitry Andric       // choose the output value from either arm of the select. If we know that
17310b57cec5SDimitry Andric       // one of those values is undef, then the output can be undef.
1732cb14a3feSDimitry Andric       simplifyAndSetOp(I, 0, DemandedElts, PoisonElts);
17330b57cec5SDimitry Andric     }
17340b57cec5SDimitry Andric 
17350b57cec5SDimitry Andric     // Next, see if we can transform the arms of the select.
17360b57cec5SDimitry Andric     APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
17370b57cec5SDimitry Andric     if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
17380b57cec5SDimitry Andric       for (unsigned i = 0; i < VWidth; i++) {
17390b57cec5SDimitry Andric         // isNullValue() always returns false when called on a ConstantExpr.
17400b57cec5SDimitry Andric         // Skip constant expressions to avoid propagating incorrect information.
17410b57cec5SDimitry Andric         Constant *CElt = CV->getAggregateElement(i);
17420b57cec5SDimitry Andric         if (isa<ConstantExpr>(CElt))
17430b57cec5SDimitry Andric           continue;
17440b57cec5SDimitry Andric         // TODO: If a select condition element is undef, we can demand from
17450b57cec5SDimitry Andric         // either side. If one side is known undef, choosing that side would
17460b57cec5SDimitry Andric         // propagate undef.
17470b57cec5SDimitry Andric         if (CElt->isNullValue())
17480b57cec5SDimitry Andric           DemandedLHS.clearBit(i);
17490b57cec5SDimitry Andric         else
17500b57cec5SDimitry Andric           DemandedRHS.clearBit(i);
17510b57cec5SDimitry Andric       }
17520b57cec5SDimitry Andric     }
17530b57cec5SDimitry Andric 
1754cb14a3feSDimitry Andric     simplifyAndSetOp(I, 1, DemandedLHS, PoisonElts2);
1755cb14a3feSDimitry Andric     simplifyAndSetOp(I, 2, DemandedRHS, PoisonElts3);
17560b57cec5SDimitry Andric 
17570b57cec5SDimitry Andric     // Output elements are undefined if the element from each arm is undefined.
17580b57cec5SDimitry Andric     // TODO: This can be improved. See comment in select condition handling.
1759cb14a3feSDimitry Andric     PoisonElts = PoisonElts2 & PoisonElts3;
17600b57cec5SDimitry Andric     break;
17610b57cec5SDimitry Andric   }
17620b57cec5SDimitry Andric   case Instruction::BitCast: {
17630b57cec5SDimitry Andric     // Vector->vector casts only.
17640b57cec5SDimitry Andric     VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
17650b57cec5SDimitry Andric     if (!VTy) break;
1766e8d8bef9SDimitry Andric     unsigned InVWidth = cast<FixedVectorType>(VTy)->getNumElements();
17670b57cec5SDimitry Andric     APInt InputDemandedElts(InVWidth, 0);
1768cb14a3feSDimitry Andric     PoisonElts2 = APInt(InVWidth, 0);
17690b57cec5SDimitry Andric     unsigned Ratio;
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric     if (VWidth == InVWidth) {
17720b57cec5SDimitry Andric       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
17730b57cec5SDimitry Andric       // elements as are demanded of us.
17740b57cec5SDimitry Andric       Ratio = 1;
17750b57cec5SDimitry Andric       InputDemandedElts = DemandedElts;
17760b57cec5SDimitry Andric     } else if ((VWidth % InVWidth) == 0) {
17770b57cec5SDimitry Andric       // If the number of elements in the output is a multiple of the number of
17780b57cec5SDimitry Andric       // elements in the input then an input element is live if any of the
17790b57cec5SDimitry Andric       // corresponding output elements are live.
17800b57cec5SDimitry Andric       Ratio = VWidth / InVWidth;
17810b57cec5SDimitry Andric       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
17820b57cec5SDimitry Andric         if (DemandedElts[OutIdx])
17830b57cec5SDimitry Andric           InputDemandedElts.setBit(OutIdx / Ratio);
17840b57cec5SDimitry Andric     } else if ((InVWidth % VWidth) == 0) {
17850b57cec5SDimitry Andric       // If the number of elements in the input is a multiple of the number of
17860b57cec5SDimitry Andric       // elements in the output then an input element is live if the
17870b57cec5SDimitry Andric       // corresponding output element is live.
17880b57cec5SDimitry Andric       Ratio = InVWidth / VWidth;
17890b57cec5SDimitry Andric       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
17900b57cec5SDimitry Andric         if (DemandedElts[InIdx / Ratio])
17910b57cec5SDimitry Andric           InputDemandedElts.setBit(InIdx);
17920b57cec5SDimitry Andric     } else {
17930b57cec5SDimitry Andric       // Unsupported so far.
17940b57cec5SDimitry Andric       break;
17950b57cec5SDimitry Andric     }
17960b57cec5SDimitry Andric 
1797cb14a3feSDimitry Andric     simplifyAndSetOp(I, 0, InputDemandedElts, PoisonElts2);
17980b57cec5SDimitry Andric 
17990b57cec5SDimitry Andric     if (VWidth == InVWidth) {
1800cb14a3feSDimitry Andric       PoisonElts = PoisonElts2;
18010b57cec5SDimitry Andric     } else if ((VWidth % InVWidth) == 0) {
18020b57cec5SDimitry Andric       // If the number of elements in the output is a multiple of the number of
18030b57cec5SDimitry Andric       // elements in the input then an output element is undef if the
18040b57cec5SDimitry Andric       // corresponding input element is undef.
18050b57cec5SDimitry Andric       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1806cb14a3feSDimitry Andric         if (PoisonElts2[OutIdx / Ratio])
1807cb14a3feSDimitry Andric           PoisonElts.setBit(OutIdx);
18080b57cec5SDimitry Andric     } else if ((InVWidth % VWidth) == 0) {
18090b57cec5SDimitry Andric       // If the number of elements in the input is a multiple of the number of
18100b57cec5SDimitry Andric       // elements in the output then an output element is undef if all of the
18110b57cec5SDimitry Andric       // corresponding input elements are undef.
18120b57cec5SDimitry Andric       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1813cb14a3feSDimitry Andric         APInt SubUndef = PoisonElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
181406c3fb27SDimitry Andric         if (SubUndef.popcount() == Ratio)
1815cb14a3feSDimitry Andric           PoisonElts.setBit(OutIdx);
18160b57cec5SDimitry Andric       }
18170b57cec5SDimitry Andric     } else {
18180b57cec5SDimitry Andric       llvm_unreachable("Unimp");
18190b57cec5SDimitry Andric     }
18200b57cec5SDimitry Andric     break;
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric   case Instruction::FPTrunc:
18230b57cec5SDimitry Andric   case Instruction::FPExt:
1824cb14a3feSDimitry Andric     simplifyAndSetOp(I, 0, DemandedElts, PoisonElts);
18250b57cec5SDimitry Andric     break;
18260b57cec5SDimitry Andric 
18270b57cec5SDimitry Andric   case Instruction::Call: {
18280b57cec5SDimitry Andric     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
18290b57cec5SDimitry Andric     if (!II) break;
18300b57cec5SDimitry Andric     switch (II->getIntrinsicID()) {
18310b57cec5SDimitry Andric     case Intrinsic::masked_gather: // fallthrough
18320b57cec5SDimitry Andric     case Intrinsic::masked_load: {
18330b57cec5SDimitry Andric       // Subtlety: If we load from a pointer, the pointer must be valid
18340b57cec5SDimitry Andric       // regardless of whether the element is demanded.  Doing otherwise risks
18350b57cec5SDimitry Andric       // segfaults which didn't exist in the original program.
1836349cc55cSDimitry Andric       APInt DemandedPtrs(APInt::getAllOnes(VWidth)),
18370b57cec5SDimitry Andric           DemandedPassThrough(DemandedElts);
18380b57cec5SDimitry Andric       if (auto *CV = dyn_cast<ConstantVector>(II->getOperand(2)))
18390b57cec5SDimitry Andric         for (unsigned i = 0; i < VWidth; i++) {
18400b57cec5SDimitry Andric           Constant *CElt = CV->getAggregateElement(i);
18410b57cec5SDimitry Andric           if (CElt->isNullValue())
18420b57cec5SDimitry Andric             DemandedPtrs.clearBit(i);
18430b57cec5SDimitry Andric           else if (CElt->isAllOnesValue())
18440b57cec5SDimitry Andric             DemandedPassThrough.clearBit(i);
18450b57cec5SDimitry Andric         }
18460b57cec5SDimitry Andric       if (II->getIntrinsicID() == Intrinsic::masked_gather)
1847cb14a3feSDimitry Andric         simplifyAndSetOp(II, 0, DemandedPtrs, PoisonElts2);
1848cb14a3feSDimitry Andric       simplifyAndSetOp(II, 3, DemandedPassThrough, PoisonElts3);
18490b57cec5SDimitry Andric 
18500b57cec5SDimitry Andric       // Output elements are undefined if the element from both sources are.
18510b57cec5SDimitry Andric       // TODO: can strengthen via mask as well.
1852cb14a3feSDimitry Andric       PoisonElts = PoisonElts2 & PoisonElts3;
18530b57cec5SDimitry Andric       break;
18540b57cec5SDimitry Andric     }
18550b57cec5SDimitry Andric     default: {
1856e8d8bef9SDimitry Andric       // Handle target specific intrinsics
1857bdd1243dSDimitry Andric       std::optional<Value *> V = targetSimplifyDemandedVectorEltsIntrinsic(
1858cb14a3feSDimitry Andric           *II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
1859e8d8bef9SDimitry Andric           simplifyAndSetOp);
186081ad6265SDimitry Andric       if (V)
1861bdd1243dSDimitry Andric         return *V;
18620b57cec5SDimitry Andric       break;
18630b57cec5SDimitry Andric     }
18640b57cec5SDimitry Andric     } // switch on IntrinsicID
18650b57cec5SDimitry Andric     break;
18660b57cec5SDimitry Andric   } // case Call
18670b57cec5SDimitry Andric   } // switch on Opcode
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric   // TODO: We bail completely on integer div/rem and shifts because they have
18700b57cec5SDimitry Andric   // UB/poison potential, but that should be refined.
18710b57cec5SDimitry Andric   BinaryOperator *BO;
18720b57cec5SDimitry Andric   if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) {
187306c3fb27SDimitry Andric     Value *X = BO->getOperand(0);
187406c3fb27SDimitry Andric     Value *Y = BO->getOperand(1);
187506c3fb27SDimitry Andric 
187606c3fb27SDimitry Andric     // Look for an equivalent binop except that one operand has been shuffled.
187706c3fb27SDimitry Andric     // If the demand for this binop only includes elements that are the same as
187806c3fb27SDimitry Andric     // the other binop, then we may be able to replace this binop with a use of
187906c3fb27SDimitry Andric     // the earlier one.
188006c3fb27SDimitry Andric     //
188106c3fb27SDimitry Andric     // Example:
188206c3fb27SDimitry Andric     // %other_bo = bo (shuf X, {0}), Y
188306c3fb27SDimitry Andric     // %this_extracted_bo = extelt (bo X, Y), 0
188406c3fb27SDimitry Andric     // -->
188506c3fb27SDimitry Andric     // %other_bo = bo (shuf X, {0}), Y
188606c3fb27SDimitry Andric     // %this_extracted_bo = extelt %other_bo, 0
188706c3fb27SDimitry Andric     //
188806c3fb27SDimitry Andric     // TODO: Handle demand of an arbitrary single element or more than one
188906c3fb27SDimitry Andric     //       element instead of just element 0.
189006c3fb27SDimitry Andric     // TODO: Unlike general demanded elements transforms, this should be safe
189106c3fb27SDimitry Andric     //       for any (div/rem/shift) opcode too.
189206c3fb27SDimitry Andric     if (DemandedElts == 1 && !X->hasOneUse() && !Y->hasOneUse() &&
189306c3fb27SDimitry Andric         BO->hasOneUse() ) {
189406c3fb27SDimitry Andric 
189506c3fb27SDimitry Andric       auto findShufBO = [&](bool MatchShufAsOp0) -> User * {
189606c3fb27SDimitry Andric         // Try to use shuffle-of-operand in place of an operand:
189706c3fb27SDimitry Andric         // bo X, Y --> bo (shuf X), Y
189806c3fb27SDimitry Andric         // bo X, Y --> bo X, (shuf Y)
189906c3fb27SDimitry Andric         BinaryOperator::BinaryOps Opcode = BO->getOpcode();
190006c3fb27SDimitry Andric         Value *ShufOp = MatchShufAsOp0 ? X : Y;
190106c3fb27SDimitry Andric         Value *OtherOp = MatchShufAsOp0 ? Y : X;
190206c3fb27SDimitry Andric         for (User *U : OtherOp->users()) {
19030fca6ea1SDimitry Andric           ArrayRef<int> Mask;
19040fca6ea1SDimitry Andric           auto Shuf = m_Shuffle(m_Specific(ShufOp), m_Value(), m_Mask(Mask));
190506c3fb27SDimitry Andric           if (BO->isCommutative()
190606c3fb27SDimitry Andric                   ? match(U, m_c_BinOp(Opcode, Shuf, m_Specific(OtherOp)))
190706c3fb27SDimitry Andric                   : MatchShufAsOp0
190806c3fb27SDimitry Andric                         ? match(U, m_BinOp(Opcode, Shuf, m_Specific(OtherOp)))
190906c3fb27SDimitry Andric                         : match(U, m_BinOp(Opcode, m_Specific(OtherOp), Shuf)))
19100fca6ea1SDimitry Andric             if (match(Mask, m_ZeroMask()) && Mask[0] != PoisonMaskElem)
191106c3fb27SDimitry Andric               if (DT.dominates(U, I))
191206c3fb27SDimitry Andric                 return U;
191306c3fb27SDimitry Andric         }
191406c3fb27SDimitry Andric         return nullptr;
191506c3fb27SDimitry Andric       };
191606c3fb27SDimitry Andric 
191706c3fb27SDimitry Andric       if (User *ShufBO = findShufBO(/* MatchShufAsOp0 */ true))
191806c3fb27SDimitry Andric         return ShufBO;
191906c3fb27SDimitry Andric       if (User *ShufBO = findShufBO(/* MatchShufAsOp0 */ false))
192006c3fb27SDimitry Andric         return ShufBO;
192106c3fb27SDimitry Andric     }
192206c3fb27SDimitry Andric 
1923cb14a3feSDimitry Andric     simplifyAndSetOp(I, 0, DemandedElts, PoisonElts);
1924cb14a3feSDimitry Andric     simplifyAndSetOp(I, 1, DemandedElts, PoisonElts2);
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric     // Output elements are undefined if both are undefined. Consider things
19270b57cec5SDimitry Andric     // like undef & 0. The result is known zero, not undef.
1928cb14a3feSDimitry Andric     PoisonElts &= PoisonElts2;
19290b57cec5SDimitry Andric   }
19300b57cec5SDimitry Andric 
1931cb14a3feSDimitry Andric   // If we've proven all of the lanes poison, return a poison value.
19320b57cec5SDimitry Andric   // TODO: Intersect w/demanded lanes
1933cb14a3feSDimitry Andric   if (PoisonElts.isAllOnes())
1934cb14a3feSDimitry Andric     return PoisonValue::get(I->getType());
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric   return MadeChange ? I : nullptr;
19370b57cec5SDimitry Andric }
19380fca6ea1SDimitry Andric 
19390fca6ea1SDimitry Andric /// For floating-point classes that resolve to a single bit pattern, return that
19400fca6ea1SDimitry Andric /// value.
getFPClassConstant(Type * Ty,FPClassTest Mask)19410fca6ea1SDimitry Andric static Constant *getFPClassConstant(Type *Ty, FPClassTest Mask) {
19420fca6ea1SDimitry Andric   switch (Mask) {
19430fca6ea1SDimitry Andric   case fcPosZero:
19440fca6ea1SDimitry Andric     return ConstantFP::getZero(Ty);
19450fca6ea1SDimitry Andric   case fcNegZero:
19460fca6ea1SDimitry Andric     return ConstantFP::getZero(Ty, true);
19470fca6ea1SDimitry Andric   case fcPosInf:
19480fca6ea1SDimitry Andric     return ConstantFP::getInfinity(Ty);
19490fca6ea1SDimitry Andric   case fcNegInf:
19500fca6ea1SDimitry Andric     return ConstantFP::getInfinity(Ty, true);
19510fca6ea1SDimitry Andric   case fcNone:
19520fca6ea1SDimitry Andric     return PoisonValue::get(Ty);
19530fca6ea1SDimitry Andric   default:
19540fca6ea1SDimitry Andric     return nullptr;
19550fca6ea1SDimitry Andric   }
19560fca6ea1SDimitry Andric }
19570fca6ea1SDimitry Andric 
SimplifyDemandedUseFPClass(Value * V,const FPClassTest DemandedMask,KnownFPClass & Known,unsigned Depth,Instruction * CxtI)19580fca6ea1SDimitry Andric Value *InstCombinerImpl::SimplifyDemandedUseFPClass(
19590fca6ea1SDimitry Andric     Value *V, const FPClassTest DemandedMask, KnownFPClass &Known,
19600fca6ea1SDimitry Andric     unsigned Depth, Instruction *CxtI) {
19610fca6ea1SDimitry Andric   assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
19620fca6ea1SDimitry Andric   Type *VTy = V->getType();
19630fca6ea1SDimitry Andric 
19640fca6ea1SDimitry Andric   assert(Known == KnownFPClass() && "expected uninitialized state");
19650fca6ea1SDimitry Andric 
19660fca6ea1SDimitry Andric   if (DemandedMask == fcNone)
19670fca6ea1SDimitry Andric     return isa<UndefValue>(V) ? nullptr : PoisonValue::get(VTy);
19680fca6ea1SDimitry Andric 
19690fca6ea1SDimitry Andric   if (Depth == MaxAnalysisRecursionDepth)
19700fca6ea1SDimitry Andric     return nullptr;
19710fca6ea1SDimitry Andric 
19720fca6ea1SDimitry Andric   Instruction *I = dyn_cast<Instruction>(V);
19730fca6ea1SDimitry Andric   if (!I) {
19740fca6ea1SDimitry Andric     // Handle constants and arguments
19750fca6ea1SDimitry Andric     Known = computeKnownFPClass(V, fcAllFlags, CxtI, Depth + 1);
19760fca6ea1SDimitry Andric     Value *FoldedToConst =
19770fca6ea1SDimitry Andric         getFPClassConstant(VTy, DemandedMask & Known.KnownFPClasses);
19780fca6ea1SDimitry Andric     return FoldedToConst == V ? nullptr : FoldedToConst;
19790fca6ea1SDimitry Andric   }
19800fca6ea1SDimitry Andric 
19810fca6ea1SDimitry Andric   if (!I->hasOneUse())
19820fca6ea1SDimitry Andric     return nullptr;
19830fca6ea1SDimitry Andric 
19840fca6ea1SDimitry Andric   // TODO: Should account for nofpclass/FastMathFlags on current instruction
19850fca6ea1SDimitry Andric   switch (I->getOpcode()) {
19860fca6ea1SDimitry Andric   case Instruction::FNeg: {
19870fca6ea1SDimitry Andric     if (SimplifyDemandedFPClass(I, 0, llvm::fneg(DemandedMask), Known,
19880fca6ea1SDimitry Andric                                 Depth + 1))
19890fca6ea1SDimitry Andric       return I;
19900fca6ea1SDimitry Andric     Known.fneg();
19910fca6ea1SDimitry Andric     break;
19920fca6ea1SDimitry Andric   }
19930fca6ea1SDimitry Andric   case Instruction::Call: {
19940fca6ea1SDimitry Andric     CallInst *CI = cast<CallInst>(I);
19950fca6ea1SDimitry Andric     switch (CI->getIntrinsicID()) {
19960fca6ea1SDimitry Andric     case Intrinsic::fabs:
19970fca6ea1SDimitry Andric       if (SimplifyDemandedFPClass(I, 0, llvm::inverse_fabs(DemandedMask), Known,
19980fca6ea1SDimitry Andric                                   Depth + 1))
19990fca6ea1SDimitry Andric         return I;
20000fca6ea1SDimitry Andric       Known.fabs();
20010fca6ea1SDimitry Andric       break;
20020fca6ea1SDimitry Andric     case Intrinsic::arithmetic_fence:
20030fca6ea1SDimitry Andric       if (SimplifyDemandedFPClass(I, 0, DemandedMask, Known, Depth + 1))
20040fca6ea1SDimitry Andric         return I;
20050fca6ea1SDimitry Andric       break;
20060fca6ea1SDimitry Andric     case Intrinsic::copysign: {
20070fca6ea1SDimitry Andric       // Flip on more potentially demanded classes
20080fca6ea1SDimitry Andric       const FPClassTest DemandedMaskAnySign = llvm::unknown_sign(DemandedMask);
20090fca6ea1SDimitry Andric       if (SimplifyDemandedFPClass(I, 0, DemandedMaskAnySign, Known, Depth + 1))
20100fca6ea1SDimitry Andric         return I;
20110fca6ea1SDimitry Andric 
20120fca6ea1SDimitry Andric       if ((DemandedMask & fcPositive) == fcNone) {
20130fca6ea1SDimitry Andric         // Roundabout way of replacing with fneg(fabs)
20140fca6ea1SDimitry Andric         I->setOperand(1, ConstantFP::get(VTy, -1.0));
20150fca6ea1SDimitry Andric         return I;
20160fca6ea1SDimitry Andric       }
20170fca6ea1SDimitry Andric 
20180fca6ea1SDimitry Andric       if ((DemandedMask & fcNegative) == fcNone) {
20190fca6ea1SDimitry Andric         // Roundabout way of replacing with fabs
20200fca6ea1SDimitry Andric         I->setOperand(1, ConstantFP::getZero(VTy));
20210fca6ea1SDimitry Andric         return I;
20220fca6ea1SDimitry Andric       }
20230fca6ea1SDimitry Andric 
20240fca6ea1SDimitry Andric       KnownFPClass KnownSign =
20250fca6ea1SDimitry Andric           computeKnownFPClass(I->getOperand(1), fcAllFlags, CxtI, Depth + 1);
20260fca6ea1SDimitry Andric       Known.copysign(KnownSign);
20270fca6ea1SDimitry Andric       break;
20280fca6ea1SDimitry Andric     }
20290fca6ea1SDimitry Andric     default:
20300fca6ea1SDimitry Andric       Known = computeKnownFPClass(I, ~DemandedMask, CxtI, Depth + 1);
20310fca6ea1SDimitry Andric       break;
20320fca6ea1SDimitry Andric     }
20330fca6ea1SDimitry Andric 
20340fca6ea1SDimitry Andric     break;
20350fca6ea1SDimitry Andric   }
20360fca6ea1SDimitry Andric   case Instruction::Select: {
20370fca6ea1SDimitry Andric     KnownFPClass KnownLHS, KnownRHS;
20380fca6ea1SDimitry Andric     if (SimplifyDemandedFPClass(I, 2, DemandedMask, KnownRHS, Depth + 1) ||
20390fca6ea1SDimitry Andric         SimplifyDemandedFPClass(I, 1, DemandedMask, KnownLHS, Depth + 1))
20400fca6ea1SDimitry Andric       return I;
20410fca6ea1SDimitry Andric 
20420fca6ea1SDimitry Andric     if (KnownLHS.isKnownNever(DemandedMask))
20430fca6ea1SDimitry Andric       return I->getOperand(2);
20440fca6ea1SDimitry Andric     if (KnownRHS.isKnownNever(DemandedMask))
20450fca6ea1SDimitry Andric       return I->getOperand(1);
20460fca6ea1SDimitry Andric 
20470fca6ea1SDimitry Andric     // TODO: Recognize clamping patterns
20480fca6ea1SDimitry Andric     Known = KnownLHS | KnownRHS;
20490fca6ea1SDimitry Andric     break;
20500fca6ea1SDimitry Andric   }
20510fca6ea1SDimitry Andric   default:
20520fca6ea1SDimitry Andric     Known = computeKnownFPClass(I, ~DemandedMask, CxtI, Depth + 1);
20530fca6ea1SDimitry Andric     break;
20540fca6ea1SDimitry Andric   }
20550fca6ea1SDimitry Andric 
20560fca6ea1SDimitry Andric   return getFPClassConstant(VTy, DemandedMask & Known.KnownFPClasses);
20570fca6ea1SDimitry Andric }
20580fca6ea1SDimitry Andric 
SimplifyDemandedFPClass(Instruction * I,unsigned OpNo,FPClassTest DemandedMask,KnownFPClass & Known,unsigned Depth)20590fca6ea1SDimitry Andric bool InstCombinerImpl::SimplifyDemandedFPClass(Instruction *I, unsigned OpNo,
20600fca6ea1SDimitry Andric                                                FPClassTest DemandedMask,
20610fca6ea1SDimitry Andric                                                KnownFPClass &Known,
20620fca6ea1SDimitry Andric                                                unsigned Depth) {
20630fca6ea1SDimitry Andric   Use &U = I->getOperandUse(OpNo);
20640fca6ea1SDimitry Andric   Value *NewVal =
20650fca6ea1SDimitry Andric       SimplifyDemandedUseFPClass(U.get(), DemandedMask, Known, Depth, I);
20660fca6ea1SDimitry Andric   if (!NewVal)
20670fca6ea1SDimitry Andric     return false;
20680fca6ea1SDimitry Andric   if (Instruction *OpInst = dyn_cast<Instruction>(U))
20690fca6ea1SDimitry Andric     salvageDebugInfo(*OpInst);
20700fca6ea1SDimitry Andric 
20710fca6ea1SDimitry Andric   replaceUse(U, NewVal);
20720fca6ea1SDimitry Andric   return true;
20730fca6ea1SDimitry Andric }
2074