#include "InstCombineInternal.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
using namespace llvm;
using namespace PatternMatch;
#define DEBUG_TYPE "instcombine"
bool canTryToConstantAddTwoShiftAmounts(Value *Sh0, Value *ShAmt0, Value *Sh1,
Value *ShAmt1) {
if (ShAmt0->getType() != ShAmt1->getType())
return false;
unsigned MaximalPossibleTotalShiftAmount =
(Sh0->getType()->getScalarSizeInBits() - 1) +
(Sh1->getType()->getScalarSizeInBits() - 1);
APInt MaximalRepresentableShiftAmount =
APInt::getAllOnes(ShAmt0->getType()->getScalarSizeInBits());
return MaximalRepresentableShiftAmount.uge(MaximalPossibleTotalShiftAmount);
}
Value *InstCombinerImpl::reassociateShiftAmtsOfTwoSameDirectionShifts(
BinaryOperator *Sh0, const SimplifyQuery &SQ,
bool AnalyzeForSignBitExtraction) {
Instruction *Sh0Op0;
Value *ShAmt0;
if (!match(Sh0,
m_Shift(m_Instruction(Sh0Op0), m_ZExtOrSelf(m_Value(ShAmt0)))))
return nullptr;
Instruction *Sh1;
Value *Trunc = nullptr;
match(Sh0Op0,
m_CombineOr(m_CombineAnd(m_Trunc(m_Instruction(Sh1)), m_Value(Trunc)),
m_Instruction(Sh1)));
Value *X, *ShAmt1;
if (!match(Sh1, m_Shift(m_Value(X), m_ZExtOrSelf(m_Value(ShAmt1)))))
return nullptr;
if (!canTryToConstantAddTwoShiftAmounts(Sh0, ShAmt0, Sh1, ShAmt1))
return nullptr;
bool HadTwoRightShifts = match(Sh0, m_Shr(m_Value(), m_Value())) &&
match(Sh1, m_Shr(m_Value(), m_Value()));
if (AnalyzeForSignBitExtraction && !HadTwoRightShifts)
return nullptr;
Instruction::BinaryOps ShiftOpcode = Sh0->getOpcode();
bool IdenticalShOpcodes = Sh0->getOpcode() == Sh1->getOpcode();
if (!IdenticalShOpcodes && !AnalyzeForSignBitExtraction)
return nullptr;
if (Trunc && !AnalyzeForSignBitExtraction &&
!match(Sh0, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
return nullptr;
auto *NewShAmt = dyn_cast_or_null<Constant>(
simplifyAddInst(ShAmt0, ShAmt1, false, false,
SQ.getWithInstruction(Sh0)));
if (!NewShAmt)
return nullptr; unsigned NewShAmtBitWidth = NewShAmt->getType()->getScalarSizeInBits();
unsigned XBitWidth = X->getType()->getScalarSizeInBits();
if (!match(NewShAmt, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
APInt(NewShAmtBitWidth, XBitWidth))))
return nullptr;
if (HadTwoRightShifts && (Trunc || AnalyzeForSignBitExtraction)) {
if (!match(NewShAmt,
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
APInt(NewShAmtBitWidth, XBitWidth - 1))))
return nullptr;
if (AnalyzeForSignBitExtraction)
return X;
}
assert(IdenticalShOpcodes && "Should not get here with different shifts.");
NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, X->getType());
BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt);
if (!Trunc) {
if (ShiftOpcode == Instruction::BinaryOps::Shl) {
NewShift->setHasNoUnsignedWrap(Sh0->hasNoUnsignedWrap() &&
Sh1->hasNoUnsignedWrap());
NewShift->setHasNoSignedWrap(Sh0->hasNoSignedWrap() &&
Sh1->hasNoSignedWrap());
} else {
NewShift->setIsExact(Sh0->isExact() && Sh1->isExact());
}
}
Instruction *Ret = NewShift;
if (Trunc) {
Builder.Insert(NewShift);
Ret = CastInst::Create(Instruction::Trunc, NewShift, Sh0->getType());
}
return Ret;
}
static Instruction *
dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
const SimplifyQuery &Q,
InstCombiner::BuilderTy &Builder) {
assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
"The input must be 'shl'!");
Value *Masked, *ShiftShAmt;
match(OuterShift,
m_Shift(m_Value(Masked), m_ZExtOrSelf(m_Value(ShiftShAmt))));
Value *Trunc;
if (match(Masked, m_CombineAnd(m_Trunc(m_Value(Masked)), m_Value(Trunc))) &&
!Trunc->hasOneUse())
return nullptr;
Type *NarrowestTy = OuterShift->getType();
Type *WidestTy = Masked->getType();
bool HadTrunc = WidestTy != NarrowestTy;
Type *ExtendedTy = WidestTy->getExtendedType();
Value *MaskShAmt;
auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
auto MaskC = m_LShr(m_AllOnes(), m_Value(MaskShAmt));
auto MaskD =
m_LShr(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_Deferred(MaskShAmt));
Value *X;
Constant *NewMask;
if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
MaskShAmt))
return nullptr;
auto *SumOfShAmts = dyn_cast_or_null<Constant>(simplifyAddInst(
MaskShAmt, ShiftShAmt, false, false, Q));
if (!SumOfShAmts)
return nullptr;
SumOfShAmts = Constant::replaceUndefsWith(
SumOfShAmts, ConstantInt::get(SumOfShAmts->getType()->getScalarType(),
ExtendedTy->getScalarSizeInBits()));
auto *ExtendedSumOfShAmts = ConstantExpr::getZExt(SumOfShAmts, ExtendedTy);
auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
auto *ExtendedInvertedMask =
ConstantExpr::getShl(ExtendedAllOnes, ExtendedSumOfShAmts);
NewMask = ConstantExpr::getNot(ExtendedInvertedMask);
} else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
m_Deferred(MaskShAmt)))) {
match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
MaskShAmt))
return nullptr;
auto *ShAmtsDiff = dyn_cast_or_null<Constant>(simplifySubInst(
ShiftShAmt, MaskShAmt, false, false, Q));
if (!ShAmtsDiff)
return nullptr;
unsigned WidestTyBitWidth = WidestTy->getScalarSizeInBits();
ShAmtsDiff = Constant::replaceUndefsWith(
ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(),
-WidestTyBitWidth));
auto *ExtendedNumHighBitsToClear = ConstantExpr::getZExt(
ConstantExpr::getSub(ConstantInt::get(ShAmtsDiff->getType(),
WidestTyBitWidth,
false),
ShAmtsDiff),
ExtendedTy);
auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
NewMask =
ConstantExpr::getLShr(ExtendedAllOnes, ExtendedNumHighBitsToClear);
} else
return nullptr;
NewMask = ConstantExpr::getTrunc(NewMask, NarrowestTy);
bool NeedMask = !match(NewMask, m_AllOnes());
if (NeedMask) {
if (!Masked->hasOneUse())
return nullptr;
if (match(Masked, m_AShr(m_Value(), m_Value())))
return nullptr;
}
if (HadTrunc)
X = Builder.CreateTrunc(X, NarrowestTy);
auto *NewShift = BinaryOperator::Create(OuterShift->getOpcode(), X,
OuterShift->getOperand(1));
if (!NeedMask)
return NewShift;
Builder.Insert(NewShift);
return BinaryOperator::Create(Instruction::And, NewShift, NewMask);
}
static Instruction *foldShiftOfShiftedLogic(BinaryOperator &I,
InstCombiner::BuilderTy &Builder) {
assert(I.isShift() && "Expected a shift as input");
auto *LogicInst = dyn_cast<BinaryOperator>(I.getOperand(0));
if (!LogicInst || !LogicInst->isBitwiseLogicOp() || !LogicInst->hasOneUse())
return nullptr;
Constant *C0, *C1;
if (!match(I.getOperand(1), m_Constant(C1)))
return nullptr;
Instruction::BinaryOps ShiftOpcode = I.getOpcode();
Type *Ty = I.getType();
Value *X, *Y;
auto matchFirstShift = [&](Value *V) {
APInt Threshold(Ty->getScalarSizeInBits(), Ty->getScalarSizeInBits());
return match(V, m_BinOp(ShiftOpcode, m_Value(), m_Value())) &&
match(V, m_OneUse(m_Shift(m_Value(X), m_Constant(C0)))) &&
match(ConstantExpr::getAdd(C0, C1),
m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
};
if (matchFirstShift(LogicInst->getOperand(0)))
Y = LogicInst->getOperand(1);
else if (matchFirstShift(LogicInst->getOperand(1)))
Y = LogicInst->getOperand(0);
else
return nullptr;
Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1);
Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC);
Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, I.getOperand(1));
return BinaryOperator::Create(LogicInst->getOpcode(), NewShift1, NewShift2);
}
Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
if (Instruction *Phi = foldBinopWithPhiOperands(I))
return Phi;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
assert(Op0->getType() == Op1->getType());
Type *Ty = I.getType();
Value *Y;
if (match(Op1, m_OneUse(m_SExt(m_Value(Y))))) {
Value *NewExt = Builder.CreateZExt(Y, Ty, Op1->getName());
return BinaryOperator::Create(I.getOpcode(), Op0, NewExt);
}
if (SimplifyDemandedInstructionBits(I))
return &I;
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *R = FoldOpIntoSelect(I, SI))
return R;
if (Constant *CUI = dyn_cast<Constant>(Op1))
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
return Res;
if (auto *NewShift = cast_or_null<Instruction>(
reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
return NewShift;
Value *A;
Constant *C, *C1;
if (match(Op0, m_Constant(C)) &&
match(Op1, m_NUWAdd(m_Value(A), m_Constant(C1)))) {
Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1);
return BinaryOperator::Create(I.getOpcode(), NewC, A);
}
unsigned BitWidth = Ty->getScalarSizeInBits();
const APInt *AC, *AddC;
if (match(Op0, m_APInt(AC)) && match(Op1, m_Add(m_Value(A), m_APInt(AddC))) &&
AddC->isNegative() && (-*AddC).ult(BitWidth)) {
assert(!AC->isZero() && "Expected simplify of shifted zero");
unsigned PosOffset = (-*AddC).getZExtValue();
auto isSuitableForPreShift = [PosOffset, &I, AC]() {
switch (I.getOpcode()) {
default:
return false;
case Instruction::Shl:
return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
AC->eq(AC->lshr(PosOffset).shl(PosOffset));
case Instruction::LShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset));
case Instruction::AShr:
return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset));
}
};
if (isSuitableForPreShift()) {
Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl
? AC->lshr(PosOffset)
: AC->shl(PosOffset));
BinaryOperator *NewShiftOp =
BinaryOperator::Create(I.getOpcode(), NewC, A);
if (I.getOpcode() == Instruction::Shl) {
NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
} else {
NewShiftOp->setIsExact();
}
return NewShiftOp;
}
}
if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Constant(C))) &&
match(C, m_Power2())) {
Constant *Mask = ConstantExpr::getSub(C, ConstantInt::get(Ty, 1));
Value *Rem = Builder.CreateAnd(A, Mask, Op1->getName());
return replaceOperand(I, 1, Rem);
}
if (Instruction *Logic = foldShiftOfShiftedLogic(I, Builder))
return Logic;
return nullptr;
}
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
Instruction *InnerShift,
InstCombinerImpl &IC, Instruction *CxtI) {
assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
const APInt *InnerShiftConst;
if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
return false;
bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
if (IsInnerShl == IsOuterShl)
return true;
if (*InnerShiftConst == OuterShAmt)
return true;
unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
unsigned InnerShAmt = InnerShiftConst->getZExtValue();
unsigned MaskShift =
IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
return true;
}
return false;
}
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
InstCombinerImpl &IC, Instruction *CxtI) {
if (isa<Constant>(V))
return true;
Instruction *I = dyn_cast<Instruction>(V);
if (!I) return false;
if (!I->hasOneUse()) return false;
switch (I->getOpcode()) {
default: return false;
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
case Instruction::Shl:
case Instruction::LShr:
return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
case Instruction::Select: {
SelectInst *SI = cast<SelectInst>(I);
Value *TrueVal = SI->getTrueValue();
Value *FalseVal = SI->getFalseValue();
return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
}
case Instruction::PHI: {
PHINode *PN = cast<PHINode>(I);
for (Value *IncValue : PN->incoming_values())
if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
return false;
return true;
}
case Instruction::Mul: {
const APInt *MulConst;
return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
MulConst->isNegatedPowerOf2() &&
MulConst->countTrailingZeros() == NumBits;
}
}
}
static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
bool IsOuterShl,
InstCombiner::BuilderTy &Builder) {
bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
Type *ShType = InnerShift->getType();
unsigned TypeWidth = ShType->getScalarSizeInBits();
const APInt *C1;
match(InnerShift->getOperand(1), m_APInt(C1));
unsigned InnerShAmt = C1->getZExtValue();
auto NewInnerShift = [&](unsigned ShAmt) {
InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
if (IsInnerShl) {
InnerShift->setHasNoUnsignedWrap(false);
InnerShift->setHasNoSignedWrap(false);
} else {
InnerShift->setIsExact(false);
}
return InnerShift;
};
if (IsInnerShl == IsOuterShl) {
if (InnerShAmt + OuterShAmt >= TypeWidth)
return Constant::getNullValue(ShType);
return NewInnerShift(InnerShAmt + OuterShAmt);
}
if (InnerShAmt == OuterShAmt) {
APInt Mask = IsInnerShl
? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
: APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
ConstantInt::get(ShType, Mask));
if (auto *AndI = dyn_cast<Instruction>(And)) {
AndI->moveBefore(InnerShift);
AndI->takeName(InnerShift);
}
return And;
}
assert(InnerShAmt > OuterShAmt &&
"Unexpected opposite direction logical shift pair");
return NewInnerShift(InnerShAmt - OuterShAmt);
}
static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
InstCombinerImpl &IC, const DataLayout &DL) {
if (Constant *C = dyn_cast<Constant>(V)) {
if (isLeftShift)
return IC.Builder.CreateShl(C, NumBits);
else
return IC.Builder.CreateLShr(C, NumBits);
}
Instruction *I = cast<Instruction>(V);
IC.addToWorklist(I);
switch (I->getOpcode()) {
default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
I->setOperand(
0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
I->setOperand(
1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
return I;
case Instruction::Shl:
case Instruction::LShr:
return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
IC.Builder);
case Instruction::Select:
I->setOperand(
1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
I->setOperand(
2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
return I;
case Instruction::PHI: {
PHINode *PN = cast<PHINode>(I);
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
isLeftShift, IC, DL));
return PN;
}
case Instruction::Mul: {
assert(!isLeftShift && "Unexpected shift direction!");
auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
IC.InsertNewInstWith(Neg, *I);
unsigned TypeWidth = I->getType()->getScalarSizeInBits();
APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits);
auto *And = BinaryOperator::CreateAnd(Neg,
ConstantInt::get(I->getType(), Mask));
And->takeName(I);
return IC.InsertNewInstWith(And, *I);
}
}
}
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift,
BinaryOperator *BO) {
switch (BO->getOpcode()) {
default:
return false; case Instruction::Add:
return Shift.getOpcode() == Instruction::Shl;
case Instruction::Or:
case Instruction::And:
return true;
case Instruction::Xor:
return !(Shift.isLogicalShift() && match(BO, m_Not(m_Value())));
}
}
Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1,
BinaryOperator &I) {
Constant *C2;
Value *X;
if (match(Op0, m_BinOp(I.getOpcode(), m_Constant(C2), m_Value(X))))
return BinaryOperator::Create(
I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), C2, C1), X);
const APInt *Op1C;
if (!match(C1, m_APInt(Op1C)))
return nullptr;
bool IsLeftShift = I.getOpcode() == Instruction::Shl;
if (I.getOpcode() != Instruction::AShr &&
canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, *this, &I)) {
LLVM_DEBUG(
dbgs() << "ICE: GetShiftedValue propagating shift through expression"
" to eliminate shift:\n IN: "
<< *Op0 << "\n SH: " << I << "\n");
return replaceInstUsesWith(
I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift, *this, DL));
}
Type *Ty = I.getType();
unsigned TypeBits = Ty->getScalarSizeInBits();
assert(!Op1C->uge(TypeBits) &&
"Shift over the type width should have been removed already");
(void)TypeBits;
if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
return FoldedShift;
if (!Op0->hasOneUse())
return nullptr;
if (auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
const APInt *Op0C;
if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
Value *NewRHS =
Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(1), C1);
Value *NewShift =
Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), C1);
NewShift->takeName(Op0BO);
return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, NewRHS);
}
}
}
Value *Cond;
BinaryOperator *TBO;
Value *FalseVal;
if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
m_Value(FalseVal)))) {
const APInt *C;
if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
match(TBO->getOperand(1), m_APInt(C)) &&
canShiftBinOpWithConstantRHS(I, TBO)) {
Value *NewRHS =
Builder.CreateBinOp(I.getOpcode(), TBO->getOperand(1), C1);
Value *NewShift = Builder.CreateBinOp(I.getOpcode(), FalseVal, C1);
Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift, NewRHS);
return SelectInst::Create(Cond, NewOp, NewShift);
}
}
BinaryOperator *FBO;
Value *TrueVal;
if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
m_OneUse(m_BinOp(FBO))))) {
const APInt *C;
if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
match(FBO->getOperand(1), m_APInt(C)) &&
canShiftBinOpWithConstantRHS(I, FBO)) {
Value *NewRHS =
Builder.CreateBinOp(I.getOpcode(), FBO->getOperand(1), C1);
Value *NewShift = Builder.CreateBinOp(I.getOpcode(), TrueVal, C1);
Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift, NewRHS);
return SelectInst::Create(Cond, NewShift, NewOp);
}
}
return nullptr;
}
Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
const SimplifyQuery Q = SQ.getWithInstruction(&I);
if (Value *V = simplifyShlInst(I.getOperand(0), I.getOperand(1),
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), Q))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldVectorBinop(I))
return X;
if (Instruction *V = commonShiftTransforms(I))
return V;
if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, Q, Builder))
return V;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
unsigned BitWidth = Ty->getScalarSizeInBits();
const APInt *C;
if (match(Op1, m_APInt(C))) {
unsigned ShAmtC = C->getZExtValue();
Value *X;
if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
unsigned SrcWidth = X->getType()->getScalarSizeInBits();
if (ShAmtC < SrcWidth &&
MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmtC), 0, &I))
return new ZExtInst(Builder.CreateShl(X, ShAmtC), Ty);
}
if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
}
const APInt *C1;
if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(C1)))) &&
C1->ult(BitWidth)) {
unsigned ShrAmt = C1->getZExtValue();
if (ShrAmt < ShAmtC) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
return NewShl;
}
if (ShrAmt > ShAmtC) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
auto *NewShr = BinaryOperator::Create(
cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
NewShr->setIsExact(true);
return NewShr;
}
}
if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_APInt(C1)))) &&
C1->ult(BitWidth)) {
unsigned ShrAmt = C1->getZExtValue();
if (ShrAmt < ShAmtC) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
Builder.Insert(NewShl);
APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
}
if (ShrAmt > ShAmtC) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
auto *OldShr = cast<BinaryOperator>(Op0);
auto *NewShr =
BinaryOperator::Create(OldShr->getOpcode(), X, ShiftDiff);
NewShr->setIsExact(OldShr->isExact());
Builder.Insert(NewShr);
APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask));
}
}
BinaryOperator *Shr;
if (match(Op0, m_OneUse(m_Trunc(m_OneUse(m_BinOp(Shr))))) &&
match(Shr, m_Shr(m_Value(X), m_APInt(C1)))) {
unsigned ShrAmtC = C1->getZExtValue();
unsigned ShDiff = ShrAmtC > ShAmtC ? ShrAmtC - ShAmtC : ShAmtC - ShrAmtC;
Constant *ShiftDiffC = ConstantInt::get(X->getType(), ShDiff);
auto ShiftOpc = ShrAmtC > ShAmtC ? Shr->getOpcode() : Instruction::Shl;
Value *NewShift = Builder.CreateBinOp(ShiftOpc, X, ShiftDiffC, "sh.diff");
Value *Trunc = Builder.CreateTrunc(NewShift, Ty, "tr.sh.diff");
APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, Mask));
}
if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
unsigned AmtSum = ShAmtC + C1->getZExtValue();
if (AmtSum < BitWidth)
return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
}
auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
switch (BinOpcode) {
default:
return false;
case Instruction::Add:
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
case Instruction::Sub:
return true;
}
};
BinaryOperator *Op0BO;
if (match(Op0, m_OneUse(m_BinOp(Op0BO))) &&
isSuitableBinOpcode(Op0BO->getOpcode())) {
Value *Shr = Op0BO->getOperand(0);
Value *Y = Op0BO->getOperand(1);
Value *X;
const APInt *CC;
if (Op0BO->isCommutative() && Y->hasOneUse() &&
(match(Y, m_Shr(m_Value(), m_Specific(Op1))) ||
match(Y, m_And(m_OneUse(m_Shr(m_Value(), m_Specific(Op1))),
m_APInt(CC)))))
std::swap(Shr, Y);
if (match(Shr, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
Value *B =
Builder.CreateBinOp(Op0BO->getOpcode(), X, YS, Shr->getName());
unsigned Op1Val = C->getLimitedValue(BitWidth);
APInt Bits = APInt::getHighBitsSet(BitWidth, BitWidth - Op1Val);
Constant *Mask = ConstantInt::get(Ty, Bits);
return BinaryOperator::CreateAnd(B, Mask);
}
if (match(Shr,
m_OneUse(m_And(m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))),
m_APInt(CC))))) {
Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
Value *M = Builder.CreateAnd(X, ConstantInt::get(Ty, CC->shl(*C)),
X->getName() + ".mask");
return BinaryOperator::Create(Op0BO->getOpcode(), M, YS);
}
}
if (match(Op0, m_OneUse(m_Sub(m_APInt(C1), m_Value(X))))) {
Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C));
Value *NewShift = Builder.CreateShl(X, Op1);
return BinaryOperator::CreateSub(NewLHS, NewShift);
}
if (!I.hasNoUnsignedWrap() &&
MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmtC), 0,
&I)) {
I.setHasNoUnsignedWrap();
return &I;
}
if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmtC) {
I.setHasNoSignedWrap();
return &I;
}
}
Value *X;
if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
Value *Mask = Builder.CreateShl(AllOnes, Op1);
return BinaryOperator::CreateAnd(Mask, X);
}
Constant *C1;
if (match(Op1, m_Constant(C1))) {
Constant *C2;
Value *X;
if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
auto *NewC = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C1);
return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
}
}
if (match(Op0, m_One()) &&
match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X))))
return BinaryOperator::CreateLShr(
ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
return nullptr;
}
Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
if (Value *V = simplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldVectorBinop(I))
return X;
if (Instruction *R = commonShiftTransforms(I))
return R;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
const APInt *C;
if (match(Op1, m_APInt(C))) {
unsigned ShAmtC = C->getZExtValue();
unsigned BitWidth = Ty->getScalarSizeInBits();
auto *II = dyn_cast<IntrinsicInst>(Op0);
if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmtC &&
(II->getIntrinsicID() == Intrinsic::ctlz ||
II->getIntrinsicID() == Intrinsic::cttz ||
II->getIntrinsicID() == Intrinsic::ctpop)) {
bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
return new ZExtInst(Cmp, Ty);
}
Value *X;
const APInt *C1;
if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
if (C1->ult(ShAmtC)) {
unsigned ShlAmtC = C1->getZExtValue();
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShlAmtC);
if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
NewLShr->setIsExact(I.isExact());
return NewLShr;
}
if (Op0->hasOneUse()) {
Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
}
} else if (C1->ugt(ShAmtC)) {
unsigned ShlAmtC = C1->getZExtValue();
Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC);
if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
NewShl->setHasNoUnsignedWrap(true);
return NewShl;
}
if (Op0->hasOneUse()) {
Value *NewShl = Builder.CreateShl(X, ShiftDiff);
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
}
} else {
assert(*C1 == ShAmtC);
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
}
}
Value *Y;
if (match(Op0,
m_OneUse(m_c_Add(m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))),
m_Value(Y))))) {
Value *NewLshr = Builder.CreateLShr(Y, Op1);
Value *NewAdd = Builder.CreateAdd(NewLshr, X);
unsigned Op1Val = C->getLimitedValue(BitWidth);
APInt Bits = APInt::getLowBitsSet(BitWidth, BitWidth - Op1Val);
Constant *Mask = ConstantInt::get(Ty, Bits);
return BinaryOperator::CreateAnd(NewAdd, Mask);
}
if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
(!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
assert(ShAmtC < X->getType()->getScalarSizeInBits() &&
"Big shift not simplified to zero?");
Value *NewLShr = Builder.CreateLShr(X, ShAmtC);
return new ZExtInst(NewLShr, Ty);
}
if (match(Op0, m_SExt(m_Value(X)))) {
unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
if (SrcTyBitWidth == 1) {
auto *NewC = ConstantInt::get(
Ty, APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
}
if ((!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType())) &&
Op0->hasOneUse()) {
if (ShAmtC == BitWidth - 1) {
Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
return new ZExtInst(NewLShr, Ty);
}
if (ShAmtC == BitWidth - SrcTyBitWidth) {
unsigned NewShAmt = std::min(ShAmtC, SrcTyBitWidth - 1);
Value *AShr = Builder.CreateAShr(X, NewShAmt);
return new ZExtInst(AShr, Ty);
}
}
}
if (ShAmtC == BitWidth - 1) {
if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
return new ZExtInst(Builder.CreateIsNotNull(X), Ty);
if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
return new ZExtInst(Builder.CreateICmpSLT(X, Y), Ty);
if (match(Op0, m_OneUse(m_SRem(m_Value(X), m_SpecificInt(2))))) {
Value *Signbit = Builder.CreateLShr(X, ShAmtC);
return BinaryOperator::CreateAnd(Signbit, X);
}
}
if (match(Op0, m_LShr(m_Value(X), m_APInt(C1)))) {
unsigned AmtSum = ShAmtC + C1->getZExtValue();
if (AmtSum < BitWidth)
return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
}
Instruction *TruncSrc;
if (match(Op0, m_OneUse(m_Trunc(m_Instruction(TruncSrc)))) &&
match(TruncSrc, m_LShr(m_Value(X), m_APInt(C1)))) {
unsigned SrcWidth = X->getType()->getScalarSizeInBits();
unsigned AmtSum = ShAmtC + C1->getZExtValue();
if (AmtSum < SrcWidth &&
(TruncSrc->hasOneUse() || C1->uge(SrcWidth - BitWidth))) {
Value *SumShift = Builder.CreateLShr(X, AmtSum, "sum.shift");
Value *Trunc = Builder.CreateTrunc(SumShift, Ty, I.getName());
APInt MaskC = APInt::getAllOnes(BitWidth).lshr(ShAmtC);
return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, MaskC));
}
}
const APInt *MulC;
if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC)))) {
if (BitWidth > 2 && ShAmtC * 2 == BitWidth && (*MulC - 1).isPowerOf2() &&
MulC->logBase2() == ShAmtC)
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
if (Op0->hasOneUse()) {
APInt NewMulC = MulC->lshr(ShAmtC);
if (MulC->eq(NewMulC.shl(ShAmtC))) {
auto *NewMul =
BinaryOperator::CreateNUWMul(X, ConstantInt::get(Ty, NewMulC));
BinaryOperator *OrigMul = cast<BinaryOperator>(Op0);
NewMul->setHasNoSignedWrap(OrigMul->hasNoSignedWrap());
return NewMul;
}
}
}
if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::bswap>(
m_OneUse(m_ZExt(m_Value(X))))))) {
unsigned SrcWidth = X->getType()->getScalarSizeInBits();
unsigned WidthDiff = BitWidth - SrcWidth;
if (SrcWidth % 16 == 0) {
Value *NarrowSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
if (ShAmtC >= WidthDiff) {
Value *NewShift = Builder.CreateLShr(NarrowSwap, ShAmtC - WidthDiff);
return new ZExtInst(NewShift, Ty);
} else {
Value *NewZExt = Builder.CreateZExt(NarrowSwap, Ty);
Constant *ShiftDiff = ConstantInt::get(Ty, WidthDiff - ShAmtC);
return BinaryOperator::CreateShl(NewZExt, ShiftDiff);
}
}
}
if (!I.isExact() &&
MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmtC), 0, &I)) {
I.setIsExact();
return &I;
}
}
Value *X;
if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
Value *Mask = Builder.CreateLShr(AllOnes, Op1);
return BinaryOperator::CreateAnd(Mask, X);
}
return nullptr;
}
Instruction *
InstCombinerImpl::foldVariableSignZeroExtensionOfVariableHighBitExtract(
BinaryOperator &OldAShr) {
assert(OldAShr.getOpcode() == Instruction::AShr &&
"Must be called with arithmetic right-shift instruction only.");
auto BitWidthSplat = [](Constant *C, Value *V) {
return match(
C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
APInt(C->getType()->getScalarSizeInBits(),
V->getType()->getScalarSizeInBits())));
};
Value *NBits;
Instruction *MaybeTrunc;
Constant *C1, *C2;
if (!match(&OldAShr,
m_AShr(m_Shl(m_Instruction(MaybeTrunc),
m_ZExtOrSelf(m_Sub(m_Constant(C1),
m_ZExtOrSelf(m_Value(NBits))))),
m_ZExtOrSelf(m_Sub(m_Constant(C2),
m_ZExtOrSelf(m_Deferred(NBits)))))) ||
!BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr))
return nullptr;
Instruction *HighBitExtract;
match(MaybeTrunc, m_TruncOrSelf(m_Instruction(HighBitExtract)));
bool HadTrunc = MaybeTrunc != HighBitExtract;
Value *X, *NumLowBitsToSkip;
if (!match(HighBitExtract, m_Shr(m_Value(X), m_Value(NumLowBitsToSkip))))
return nullptr;
Constant *C0;
if (!match(NumLowBitsToSkip,
m_ZExtOrSelf(
m_Sub(m_Constant(C0), m_ZExtOrSelf(m_Specific(NBits))))) ||
!BitWidthSplat(C0, HighBitExtract))
return nullptr;
if (HighBitExtract->getOpcode() == OldAShr.getOpcode())
return replaceInstUsesWith(OldAShr, MaybeTrunc);
if (HadTrunc && !match(&OldAShr, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
return nullptr;
Instruction *NewAShr =
BinaryOperator::Create(OldAShr.getOpcode(), X, NumLowBitsToSkip);
NewAShr->copyIRFlags(HighBitExtract); if (!HadTrunc)
return NewAShr;
Builder.Insert(NewAShr);
return TruncInst::CreateTruncOrBitCast(NewAShr, OldAShr.getType());
}
Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
if (Value *V = simplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldVectorBinop(I))
return X;
if (Instruction *R = commonShiftTransforms(I))
return R;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
unsigned BitWidth = Ty->getScalarSizeInBits();
const APInt *ShAmtAPInt;
if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
unsigned ShAmt = ShAmtAPInt->getZExtValue();
Value *X;
if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
return new SExtInst(X, Ty);
const APInt *ShOp1;
if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
ShOp1->ult(BitWidth)) {
unsigned ShlAmt = ShOp1->getZExtValue();
if (ShlAmt < ShAmt) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
NewAShr->setIsExact(I.isExact());
return NewAShr;
}
if (ShlAmt > ShAmt) {
Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
NewShl->setHasNoSignedWrap(true);
return NewShl;
}
}
if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
ShOp1->ult(BitWidth)) {
unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
AmtSum = std::min(AmtSum, BitWidth - 1);
return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
}
if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
(Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
Type *SrcTy = X->getType();
ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
return new SExtInst(NewSh, Ty);
}
if (ShAmt == BitWidth - 1) {
if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
return new SExtInst(Builder.CreateIsNotNull(X), Ty);
Value *Y;
if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
}
if (!I.isExact() &&
MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
I.setIsExact();
return &I;
}
}
Value *X;
if (match(Op1, m_SpecificIntAllowUndef(BitWidth - 1)) &&
match(Op0, m_OneUse(m_Shl(m_Value(X),
m_SpecificIntAllowUndef(BitWidth - 1))))) {
Constant *Mask = ConstantInt::get(Ty, 1);
Mask = Constant::mergeUndefsWith(
Constant::mergeUndefsWith(Mask, cast<Constant>(Op1)),
cast<Constant>(cast<Instruction>(Op0)->getOperand(1)));
X = Builder.CreateAnd(X, Mask);
return BinaryOperator::CreateNeg(X);
}
if (Instruction *R = foldVariableSignZeroExtensionOfVariableHighBitExtract(I))
return R;
if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
return BinaryOperator::CreateLShr(Op0, Op1);
if (match(Op0, m_OneUse(m_Not(m_Value(X))))) {
auto *NewAShr = Builder.CreateAShr(X, Op1, Op0->getName() + ".not");
return BinaryOperator::CreateNot(NewAShr);
}
return nullptr;
}