2017-08-14 21:09:32 +02:00
|
|
|
//===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-08-14 21:09:32 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file holds routines to help analyse compare instructions
|
|
|
|
// and fold them into constants or other compare instructions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-08-14 23:39:51 +02:00
|
|
|
#include "llvm/Analysis/CmpInstAnalysis.h"
|
2017-08-14 21:09:32 +02:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2017-08-14 23:39:51 +02:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2017-08-14 21:09:32 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
|
|
|
|
ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
|
|
|
|
: ICI->getPredicate();
|
|
|
|
switch (Pred) {
|
|
|
|
// False -> 0
|
|
|
|
case ICmpInst::ICMP_UGT: return 1; // 001
|
|
|
|
case ICmpInst::ICMP_SGT: return 1; // 001
|
|
|
|
case ICmpInst::ICMP_EQ: return 2; // 010
|
|
|
|
case ICmpInst::ICMP_UGE: return 3; // 011
|
|
|
|
case ICmpInst::ICMP_SGE: return 3; // 011
|
|
|
|
case ICmpInst::ICMP_ULT: return 4; // 100
|
|
|
|
case ICmpInst::ICMP_SLT: return 4; // 100
|
|
|
|
case ICmpInst::ICMP_NE: return 5; // 101
|
|
|
|
case ICmpInst::ICMP_ULE: return 6; // 110
|
|
|
|
case ICmpInst::ICMP_SLE: return 6; // 110
|
|
|
|
// True -> 7
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid ICmp predicate!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:53:27 +01:00
|
|
|
Constant *llvm::getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
|
|
|
|
CmpInst::Predicate &Pred) {
|
2017-08-14 21:09:32 +02:00
|
|
|
switch (Code) {
|
|
|
|
default: llvm_unreachable("Illegal ICmp code!");
|
|
|
|
case 0: // False.
|
2018-12-04 19:53:27 +01:00
|
|
|
return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
|
|
|
|
case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
|
|
|
|
case 2: Pred = ICmpInst::ICMP_EQ; break;
|
|
|
|
case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
|
|
|
|
case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
|
|
|
|
case 5: Pred = ICmpInst::ICMP_NE; break;
|
|
|
|
case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
|
2017-08-14 21:09:32 +02:00
|
|
|
case 7: // True.
|
2018-12-04 19:53:27 +01:00
|
|
|
return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
|
2017-08-14 21:09:32 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:48:30 +01:00
|
|
|
bool llvm::predicatesFoldable(ICmpInst::Predicate P1, ICmpInst::Predicate P2) {
|
|
|
|
return (CmpInst::isSigned(P1) == CmpInst::isSigned(P2)) ||
|
|
|
|
(CmpInst::isSigned(P1) && ICmpInst::isEquality(P2)) ||
|
|
|
|
(CmpInst::isSigned(P2) && ICmpInst::isEquality(P1));
|
2017-08-14 21:09:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 23:39:51 +02:00
|
|
|
bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
|
|
|
|
CmpInst::Predicate &Pred,
|
2017-09-01 23:27:34 +02:00
|
|
|
Value *&X, APInt &Mask, bool LookThruTrunc) {
|
|
|
|
using namespace PatternMatch;
|
|
|
|
|
2017-08-14 23:39:51 +02:00
|
|
|
const APInt *C;
|
2017-09-01 23:27:34 +02:00
|
|
|
if (!match(RHS, m_APInt(C)))
|
2017-08-14 21:09:32 +02:00
|
|
|
return false;
|
|
|
|
|
2017-08-14 23:39:51 +02:00
|
|
|
switch (Pred) {
|
2017-08-14 21:09:32 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case ICmpInst::ICMP_SLT:
|
|
|
|
// X < 0 is equivalent to (X & SignMask) != 0.
|
2017-08-14 23:39:51 +02:00
|
|
|
if (!C->isNullValue())
|
2017-08-14 21:09:32 +02:00
|
|
|
return false;
|
2017-08-14 23:39:51 +02:00
|
|
|
Mask = APInt::getSignMask(C->getBitWidth());
|
2017-08-14 21:09:32 +02:00
|
|
|
Pred = ICmpInst::ICMP_NE;
|
|
|
|
break;
|
2017-08-15 00:11:43 +02:00
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
// X <= -1 is equivalent to (X & SignMask) != 0.
|
|
|
|
if (!C->isAllOnesValue())
|
|
|
|
return false;
|
|
|
|
Mask = APInt::getSignMask(C->getBitWidth());
|
|
|
|
Pred = ICmpInst::ICMP_NE;
|
|
|
|
break;
|
2017-08-14 21:09:32 +02:00
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
// X > -1 is equivalent to (X & SignMask) == 0.
|
2017-08-14 23:39:51 +02:00
|
|
|
if (!C->isAllOnesValue())
|
2017-08-14 21:09:32 +02:00
|
|
|
return false;
|
2017-08-14 23:39:51 +02:00
|
|
|
Mask = APInt::getSignMask(C->getBitWidth());
|
2017-08-14 21:09:32 +02:00
|
|
|
Pred = ICmpInst::ICMP_EQ;
|
|
|
|
break;
|
2017-08-15 00:11:43 +02:00
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
// X >= 0 is equivalent to (X & SignMask) == 0.
|
|
|
|
if (!C->isNullValue())
|
|
|
|
return false;
|
|
|
|
Mask = APInt::getSignMask(C->getBitWidth());
|
|
|
|
Pred = ICmpInst::ICMP_EQ;
|
|
|
|
break;
|
2017-08-14 21:09:32 +02:00
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
|
2017-08-14 23:39:51 +02:00
|
|
|
if (!C->isPowerOf2())
|
2017-08-14 21:09:32 +02:00
|
|
|
return false;
|
2017-08-14 23:39:51 +02:00
|
|
|
Mask = -*C;
|
2017-08-14 21:09:32 +02:00
|
|
|
Pred = ICmpInst::ICMP_EQ;
|
|
|
|
break;
|
2017-08-15 00:11:43 +02:00
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
// X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
|
|
|
|
if (!(*C + 1).isPowerOf2())
|
|
|
|
return false;
|
|
|
|
Mask = ~*C;
|
|
|
|
Pred = ICmpInst::ICMP_EQ;
|
|
|
|
break;
|
2017-08-14 21:09:32 +02:00
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
// X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
|
2017-08-14 23:39:51 +02:00
|
|
|
if (!(*C + 1).isPowerOf2())
|
2017-08-14 21:09:32 +02:00
|
|
|
return false;
|
2017-08-14 23:39:51 +02:00
|
|
|
Mask = ~*C;
|
2017-08-14 21:09:32 +02:00
|
|
|
Pred = ICmpInst::ICMP_NE;
|
|
|
|
break;
|
2017-08-15 00:11:43 +02:00
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
// X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
|
|
|
|
if (!C->isPowerOf2())
|
|
|
|
return false;
|
|
|
|
Mask = -*C;
|
|
|
|
Pred = ICmpInst::ICMP_NE;
|
|
|
|
break;
|
2017-08-14 21:09:32 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:27:34 +02:00
|
|
|
if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
|
|
|
|
Mask = Mask.zext(X->getType()->getScalarSizeInBits());
|
|
|
|
} else {
|
|
|
|
X = LHS;
|
|
|
|
}
|
|
|
|
|
2017-08-14 21:09:32 +02:00
|
|
|
return true;
|
|
|
|
}
|