1 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file holds routines to help analyse compare instructions
11 // and fold them into constants or other compare instructions
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
21 unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
22 ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
23 : ICI->getPredicate();
26 case ICmpInst::ICMP_UGT: return 1; // 001
27 case ICmpInst::ICMP_SGT: return 1; // 001
28 case ICmpInst::ICMP_EQ: return 2; // 010
29 case ICmpInst::ICMP_UGE: return 3; // 011
30 case ICmpInst::ICMP_SGE: return 3; // 011
31 case ICmpInst::ICMP_ULT: return 4; // 100
32 case ICmpInst::ICMP_SLT: return 4; // 100
33 case ICmpInst::ICMP_NE: return 5; // 101
34 case ICmpInst::ICMP_ULE: return 6; // 110
35 case ICmpInst::ICMP_SLE: return 6; // 110
38 llvm_unreachable("Invalid ICmp predicate!");
42 Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
43 CmpInst::Predicate &NewICmpPred) {
45 default: llvm_unreachable("Illegal ICmp code!");
47 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
48 case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
49 case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
50 case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
51 case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
52 case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
53 case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
55 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
60 bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
61 return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
62 (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
63 (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
66 bool llvm::decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred,
67 Value *&X, Value *&Y, Value *&Z) {
68 ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1));
72 switch (I->getPredicate()) {
75 case ICmpInst::ICMP_SLT:
76 // X < 0 is equivalent to (X & SignMask) != 0.
79 Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
80 Pred = ICmpInst::ICMP_NE;
82 case ICmpInst::ICMP_SGT:
83 // X > -1 is equivalent to (X & SignMask) == 0.
84 if (!C->isAllOnesValue())
86 Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
87 Pred = ICmpInst::ICMP_EQ;
89 case ICmpInst::ICMP_ULT:
90 // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
91 if (!C->getValue().isPowerOf2())
93 Y = ConstantInt::get(I->getContext(), -C->getValue());
94 Pred = ICmpInst::ICMP_EQ;
96 case ICmpInst::ICMP_UGT:
97 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
98 if (!(C->getValue() + 1).isPowerOf2())
100 Y = ConstantInt::get(I->getContext(), ~C->getValue());
101 Pred = ICmpInst::ICMP_NE;
105 X = I->getOperand(0);
106 Z = ConstantInt::getNullValue(C->getType());