mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-17 21:29:20 +00:00
Generalize the new code in instcombine's ComputeNumSignBits for handling
and/or to handle more cases (such as this add-sitofp.ll testcase), and port it to selectiondag's ComputeNumSignBits. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51469 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
e4977cf750
commit
a332f17c8c
@ -1628,6 +1628,7 @@ unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
|
|||||||
assert(MVT::isInteger(VT) && "Invalid VT!");
|
assert(MVT::isInteger(VT) && "Invalid VT!");
|
||||||
unsigned VTBits = MVT::getSizeInBits(VT);
|
unsigned VTBits = MVT::getSizeInBits(VT);
|
||||||
unsigned Tmp, Tmp2;
|
unsigned Tmp, Tmp2;
|
||||||
|
unsigned FirstAnswer = 1;
|
||||||
|
|
||||||
if (Depth == 6)
|
if (Depth == 6)
|
||||||
return 1; // Limit search depth.
|
return 1; // Limit search depth.
|
||||||
@ -1683,11 +1684,16 @@ unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
|
|||||||
case ISD::AND:
|
case ISD::AND:
|
||||||
case ISD::OR:
|
case ISD::OR:
|
||||||
case ISD::XOR: // NOT is handled here.
|
case ISD::XOR: // NOT is handled here.
|
||||||
// Logical binary ops preserve the number of sign bits.
|
// Logical binary ops preserve the number of sign bits at the worst.
|
||||||
Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
|
Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
|
||||||
if (Tmp == 1) return 1; // Early out.
|
if (Tmp != 1) {
|
||||||
Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
|
Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
|
||||||
return std::min(Tmp, Tmp2);
|
FirstAnswer = std::min(Tmp, Tmp2);
|
||||||
|
// We computed what we know about the sign bits as our first
|
||||||
|
// answer. Now proceed to the generic code that uses
|
||||||
|
// ComputeMaskedBits, and pick whichever answer is better.
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case ISD::SELECT:
|
case ISD::SELECT:
|
||||||
Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
|
Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
|
||||||
@ -1801,7 +1807,7 @@ unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
|
|||||||
Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
|
Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
|
||||||
Op.getOpcode() == ISD::INTRINSIC_VOID) {
|
Op.getOpcode() == ISD::INTRINSIC_VOID) {
|
||||||
unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
|
unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
|
||||||
if (NumBits > 1) return NumBits;
|
if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, if we can prove that the top bits of the result are 0's or 1's,
|
// Finally, if we can prove that the top bits of the result are 0's or 1's,
|
||||||
@ -1816,7 +1822,7 @@ unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
|
|||||||
Mask = KnownOne;
|
Mask = KnownOne;
|
||||||
} else {
|
} else {
|
||||||
// Nothing known.
|
// Nothing known.
|
||||||
return 1;
|
return FirstAnswer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Okay, we know that the sign bit in Mask is set. Use CLZ to determine
|
// Okay, we know that the sign bit in Mask is set. Use CLZ to determine
|
||||||
@ -1825,7 +1831,7 @@ unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
|
|||||||
Mask <<= Mask.getBitWidth()-VTBits;
|
Mask <<= Mask.getBitWidth()-VTBits;
|
||||||
// Return # leading zeros. We use 'min' here in case Val was zero before
|
// Return # leading zeros. We use 'min' here in case Val was zero before
|
||||||
// shifting. We don't want to return '64' as for an i32 "0".
|
// shifting. We don't want to return '64' as for an i32 "0".
|
||||||
return std::min(VTBits, Mask.countLeadingZeros());
|
return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2072,6 +2072,7 @@ unsigned InstCombiner::ComputeNumSignBits(Value *V, unsigned Depth) const{
|
|||||||
const IntegerType *Ty = cast<IntegerType>(V->getType());
|
const IntegerType *Ty = cast<IntegerType>(V->getType());
|
||||||
unsigned TyBits = Ty->getBitWidth();
|
unsigned TyBits = Ty->getBitWidth();
|
||||||
unsigned Tmp, Tmp2;
|
unsigned Tmp, Tmp2;
|
||||||
|
unsigned FirstAnswer = 1;
|
||||||
|
|
||||||
if (Depth == 6)
|
if (Depth == 6)
|
||||||
return 1; // Limit search depth.
|
return 1; // Limit search depth.
|
||||||
@ -2101,54 +2102,18 @@ unsigned InstCombiner::ComputeNumSignBits(Value *V, unsigned Depth) const{
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Instruction::And:
|
case Instruction::And:
|
||||||
// Logical binary ops preserve the number of sign bits at the worst.
|
|
||||||
Tmp = ComputeNumSignBits(U->getOperand(0), Depth+1);
|
|
||||||
if (Tmp != 1) {
|
|
||||||
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
|
||||||
Tmp = std::min(Tmp, Tmp2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// X & C has sign bits equal to C if C's top bits are zeros.
|
|
||||||
if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
|
|
||||||
// See what bits are known to be zero on the output.
|
|
||||||
APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
|
|
||||||
APInt Mask = APInt::getAllOnesValue(TyBits);
|
|
||||||
ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
|
|
||||||
|
|
||||||
KnownZero |= ~C->getValue();
|
|
||||||
// If we know that we have leading zeros, we know we have at least that
|
|
||||||
// many sign bits.
|
|
||||||
Tmp = std::max(Tmp, KnownZero.countLeadingOnes());
|
|
||||||
}
|
|
||||||
return Tmp;
|
|
||||||
|
|
||||||
case Instruction::Or:
|
case Instruction::Or:
|
||||||
|
case Instruction::Xor: // NOT is handled here.
|
||||||
// Logical binary ops preserve the number of sign bits at the worst.
|
// Logical binary ops preserve the number of sign bits at the worst.
|
||||||
Tmp = ComputeNumSignBits(U->getOperand(0), Depth+1);
|
Tmp = ComputeNumSignBits(U->getOperand(0), Depth+1);
|
||||||
if (Tmp != 1) {
|
if (Tmp != 1) {
|
||||||
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
||||||
Tmp = std::min(Tmp, Tmp2);
|
FirstAnswer = std::min(Tmp, Tmp2);
|
||||||
|
// We computed what we know about the sign bits as our first
|
||||||
|
// answer. Now proceed to the generic code that uses
|
||||||
|
// ComputeMaskedBits, and pick whichever answer is better.
|
||||||
}
|
}
|
||||||
// X & C has sign bits equal to C if C's top bits are zeros.
|
break;
|
||||||
if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
|
|
||||||
// See what bits are known to be one on the output.
|
|
||||||
APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
|
|
||||||
APInt Mask = APInt::getAllOnesValue(TyBits);
|
|
||||||
ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
|
|
||||||
|
|
||||||
KnownOne |= C->getValue();
|
|
||||||
// If we know that we have leading ones, we know we have at least that
|
|
||||||
// many sign bits.
|
|
||||||
Tmp = std::max(Tmp, KnownOne.countLeadingOnes());
|
|
||||||
}
|
|
||||||
return Tmp;
|
|
||||||
|
|
||||||
case Instruction::Xor: // NOT is handled here.
|
|
||||||
// Logical binary ops preserve the number of sign bits.
|
|
||||||
Tmp = ComputeNumSignBits(U->getOperand(0), Depth+1);
|
|
||||||
if (Tmp == 1) return 1; // Early out.
|
|
||||||
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
|
||||||
return std::min(Tmp, Tmp2);
|
|
||||||
|
|
||||||
case Instruction::Select:
|
case Instruction::Select:
|
||||||
Tmp = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
Tmp = ComputeNumSignBits(U->getOperand(1), Depth+1);
|
||||||
@ -2232,7 +2197,7 @@ unsigned InstCombiner::ComputeNumSignBits(Value *V, unsigned Depth) const{
|
|||||||
Mask = KnownOne;
|
Mask = KnownOne;
|
||||||
} else {
|
} else {
|
||||||
// Nothing known.
|
// Nothing known.
|
||||||
return 1;
|
return FirstAnswer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Okay, we know that the sign bit in Mask is set. Use CLZ to determine
|
// Okay, we know that the sign bit in Mask is set. Use CLZ to determine
|
||||||
@ -2241,7 +2206,7 @@ unsigned InstCombiner::ComputeNumSignBits(Value *V, unsigned Depth) const{
|
|||||||
Mask <<= Mask.getBitWidth()-TyBits;
|
Mask <<= Mask.getBitWidth()-TyBits;
|
||||||
// Return # leading zeros. We use 'min' here in case Val was zero before
|
// Return # leading zeros. We use 'min' here in case Val was zero before
|
||||||
// shifting. We don't want to return '64' as for an i32 "0".
|
// shifting. We don't want to return '64' as for an i32 "0".
|
||||||
return std::min(TyBits, Mask.countLeadingZeros());
|
return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
9
test/Transforms/InstCombine/add-sitofp.ll
Normal file
9
test/Transforms/InstCombine/add-sitofp.ll
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {add i32}
|
||||||
|
|
||||||
|
define double @x(i32 %a, i32 %b) nounwind {
|
||||||
|
%m = lshr i32 %a, 24
|
||||||
|
%n = and i32 %m, %b
|
||||||
|
%o = sitofp i32 %n to double
|
||||||
|
%p = add double %o, 1.0
|
||||||
|
ret double %p
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user