mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-09 11:25:55 +00:00
Added support to optimize comparisons with "lshr exact" of a constant.
Patch by Rahul Jain. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210040 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -2318,6 +2318,32 @@ static bool swapMayExposeCSEOpportunities(const Value * Op0,
|
|||||||
return GlobalSwapBenefits > 0;
|
return GlobalSwapBenefits > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to check whether Op represents a lshr/ashr exact
|
||||||
|
// instruction. For example:
|
||||||
|
// (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1)
|
||||||
|
// Here if Op represents -> (ashr exact const2, A), and CI represents
|
||||||
|
// const1, we compute Quotient as const2/const1.
|
||||||
|
|
||||||
|
static bool checkShrExact(Value *Op, APInt &Quotient, const ConstantInt *CI,
|
||||||
|
Value *&A) {
|
||||||
|
|
||||||
|
ConstantInt *CI2;
|
||||||
|
if (match(Op, m_AShr(m_ConstantInt(CI2), m_Value(A))) &&
|
||||||
|
(cast<BinaryOperator>(Op)->isExact())) {
|
||||||
|
Quotient = CI2->getValue().sdiv(CI->getValue());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the case for lhsr.
|
||||||
|
if (match(Op, m_LShr(m_ConstantInt(CI2), m_Value(A))) &&
|
||||||
|
(cast<BinaryOperator>(Op)->isExact())) {
|
||||||
|
Quotient = CI2->getValue().udiv(CI->getValue());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
|
Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
|
||||||
bool Changed = false;
|
bool Changed = false;
|
||||||
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
||||||
@@ -2443,13 +2469,10 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
|
|||||||
// (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1)
|
// (icmp (ashr exact const2, A), const1) -> icmp A, Log2(const2/const1)
|
||||||
// Cases where const1 doesn't divide const2 exactly or Quotient is not
|
// Cases where const1 doesn't divide const2 exactly or Quotient is not
|
||||||
// exact of log2 are handled by SimplifyICmpInst call above where we
|
// exact of log2 are handled by SimplifyICmpInst call above where we
|
||||||
// return false.
|
// return false. Similar for lshr.
|
||||||
// TODO: Handle this for lshr exact with udiv.
|
|
||||||
{
|
{
|
||||||
ConstantInt *CI2;
|
APInt Quotient;
|
||||||
if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) &&
|
if (checkShrExact(Op0, Quotient, CI, A)) {
|
||||||
(cast<BinaryOperator>(Op0)->isExact())) {
|
|
||||||
APInt Quotient = CI2->getValue().sdiv(CI->getValue());
|
|
||||||
unsigned shift = Quotient.logBase2();
|
unsigned shift = Quotient.logBase2();
|
||||||
return new ICmpInst(I.getPredicate(), A,
|
return new ICmpInst(I.getPredicate(), A,
|
||||||
ConstantInt::get(A->getType(), shift));
|
ConstantInt::get(A->getType(), shift));
|
||||||
|
@@ -1390,3 +1390,11 @@ define i1 @exact_ashr_eq_false(i32 %a) {
|
|||||||
%cmp = icmp eq i32 %shr, -15
|
%cmp = icmp eq i32 %shr, -15
|
||||||
ret i1 %cmp
|
ret i1 %cmp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
; CHECK-LABEL: @exact_lhsr
|
||||||
|
; CHECK-NEXT: icmp eq i32 %a, 3
|
||||||
|
define i1 @exact_lhsr(i32 %a) {
|
||||||
|
%shr = lshr exact i32 80, %a
|
||||||
|
%cmp = icmp eq i32 %shr, 10
|
||||||
|
ret i1 %cmp
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user