Skip to content

Commit

Permalink
[InstSimplify] fix formatting and add bool function argument comments…
Browse files Browse the repository at this point in the history
…; NFC

Make existing code conform with proposed additions in D140733.
  • Loading branch information
rotateright committed Dec 29, 2022
1 parent af5dd27 commit b16d04d
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Expand Up @@ -780,7 +780,7 @@ static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1,

/// Given operands for a Sub, see if we can fold the result.
/// If not, this returns null.
static Value *simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
const SimplifyQuery &Q, unsigned MaxRecurse) {
if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
return C;
Expand All @@ -806,14 +806,14 @@ static Value *simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
// Is this a negation?
if (match(Op0, m_Zero())) {
// 0 - X -> 0 if the sub is NUW.
if (isNUW)
if (IsNUW)
return Constant::getNullValue(Op0->getType());

KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
if (Known.Zero.isMaxSignedValue()) {
// Op1 is either 0 or the minimum signed value. If the sub is NSW, then
// Op1 must be 0 because negating the minimum signed value is undefined.
if (isNSW)
if (IsNSW)
return Constant::getNullValue(Op0->getType());

// 0 - X -> X if X is 0 or the minimum signed value.
Expand Down Expand Up @@ -915,9 +915,9 @@ static Value *simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
return nullptr;
}

Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
const SimplifyQuery &Q) {
return ::simplifySubInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
}

/// Given operands for a Mul, see if we can fold the result.
Expand Down Expand Up @@ -1389,7 +1389,7 @@ static Value *simplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
/// Given operands for an Shl, LShr or AShr, see if we can
/// fold the result. If not, this returns null.
static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
Value *Op1, bool isExact,
Value *Op1, bool IsExact,
const SimplifyQuery &Q, unsigned MaxRecurse) {
if (Value *V =
simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
Expand All @@ -1402,10 +1402,10 @@ static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
// undef >> X -> 0
// undef >> X -> undef (if it's exact)
if (Q.isUndefValue(Op0))
return isExact ? Op0 : Constant::getNullValue(Op0->getType());
return IsExact ? Op0 : Constant::getNullValue(Op0->getType());

// The low bit cannot be shifted out of an exact shift if it is set.
if (isExact) {
if (IsExact) {
KnownBits Op0Known =
computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
if (Op0Known.One[0])
Expand All @@ -1417,16 +1417,16 @@ static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,

/// Given operands for an Shl, see if we can fold the result.
/// If not, this returns null.
static Value *simplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
const SimplifyQuery &Q, unsigned MaxRecurse) {
if (Value *V =
simplifyShift(Instruction::Shl, Op0, Op1, isNSW, Q, MaxRecurse))
simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
return V;

// undef << X -> 0
// undef << X -> undef if (if it's NSW/NUW)
if (Q.isUndefValue(Op0))
return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType());
return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Op0->getType());

// (X >> A) << A -> X
Value *X;
Expand All @@ -1435,24 +1435,24 @@ static Value *simplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
return X;

// shl nuw i8 C, %x -> C iff C has sign bit set.
if (isNUW && match(Op0, m_Negative()))
if (IsNUW && match(Op0, m_Negative()))
return Op0;
// NOTE: could use computeKnownBits() / LazyValueInfo,
// but the cost-benefit analysis suggests it isn't worth it.

return nullptr;
}

Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
const SimplifyQuery &Q) {
return ::simplifyShlInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
}

/// Given operands for an LShr, see if we can fold the result.
/// If not, this returns null.
static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
const SimplifyQuery &Q, unsigned MaxRecurse) {
if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q,
MaxRecurse))
return V;

Expand Down Expand Up @@ -1480,16 +1480,16 @@ static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
return nullptr;
}

Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
const SimplifyQuery &Q) {
return ::simplifyLShrInst(Op0, Op1, isExact, Q, RecursionLimit);
return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
}

/// Given operands for an AShr, see if we can fold the result.
/// If not, this returns null.
static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
const SimplifyQuery &Q, unsigned MaxRecurse) {
if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q,
MaxRecurse))
return V;

Expand All @@ -1513,9 +1513,9 @@ static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
return nullptr;
}

Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
const SimplifyQuery &Q) {
return ::simplifyAShrInst(Op0, Op1, isExact, Q, RecursionLimit);
return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
}

/// Commuted variants are assumed to be handled by calling this function again
Expand Down Expand Up @@ -1722,25 +1722,25 @@ static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
return nullptr;

Type *ITy = Op0->getType();
bool isNSW = IIQ.hasNoSignedWrap(AddInst);
bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);

const APInt Delta = *C1 - *C0;
if (C0->isStrictlyPositive()) {
if (Delta == 2) {
if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
return getFalse(ITy);
if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW)
if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
return getFalse(ITy);
}
if (Delta == 1) {
if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
return getFalse(ITy);
if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW)
if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
return getFalse(ITy);
}
}
if (C0->getBoolValue() && isNUW) {
if (C0->getBoolValue() && IsNUW) {
if (Delta == 2)
if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
return getFalse(ITy);
Expand Down Expand Up @@ -1879,25 +1879,25 @@ static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
return nullptr;

Type *ITy = Op0->getType();
bool isNSW = IIQ.hasNoSignedWrap(AddInst);
bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);

const APInt Delta = *C1 - *C0;
if (C0->isStrictlyPositive()) {
if (Delta == 2) {
if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
return getTrue(ITy);
if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW)
if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
return getTrue(ITy);
}
if (Delta == 1) {
if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
return getTrue(ITy);
if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW)
if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
return getTrue(ITy);
}
}
if (C0->getBoolValue() && isNUW) {
if (C0->getBoolValue() && IsNUW) {
if (Delta == 2)
if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
return getTrue(ITy);
Expand Down Expand Up @@ -5670,9 +5670,11 @@ static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
const SimplifyQuery &Q, unsigned MaxRecurse) {
switch (Opcode) {
case Instruction::Add:
return simplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse);
return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
MaxRecurse);
case Instruction::Sub:
return simplifySubInst(LHS, RHS, false, false, Q, MaxRecurse);
return simplifySubInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
MaxRecurse);
case Instruction::Mul:
return simplifyMulInst(LHS, RHS, Q, MaxRecurse);
case Instruction::SDiv:
Expand All @@ -5684,11 +5686,12 @@ static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
case Instruction::URem:
return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
case Instruction::Shl:
return simplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse);
return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
MaxRecurse);
case Instruction::LShr:
return simplifyLShrInst(LHS, RHS, false, Q, MaxRecurse);
return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
case Instruction::AShr:
return simplifyAShrInst(LHS, RHS, false, Q, MaxRecurse);
return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
case Instruction::And:
return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
case Instruction::Or:
Expand Down

0 comments on commit b16d04d

Please sign in to comment.