1*9880d681SAndroid Build Coastguard Worker //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file holds routines to help analyse compare instructions
11*9880d681SAndroid Build Coastguard Worker // and fold them into constants or other compare instructions
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker using namespace llvm;
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits
22*9880d681SAndroid Build Coastguard Worker /// are carefully arranged to allow folding of expressions such as:
23*9880d681SAndroid Build Coastguard Worker ///
24*9880d681SAndroid Build Coastguard Worker /// (A < B) | (A > B) --> (A != B)
25*9880d681SAndroid Build Coastguard Worker ///
26*9880d681SAndroid Build Coastguard Worker /// Note that this is only valid if the first and second predicates have the
27*9880d681SAndroid Build Coastguard Worker /// same sign. Is illegal to do: (A u< B) | (A s> B)
28*9880d681SAndroid Build Coastguard Worker ///
29*9880d681SAndroid Build Coastguard Worker /// Three bits are used to represent the condition, as follows:
30*9880d681SAndroid Build Coastguard Worker /// 0 A > B
31*9880d681SAndroid Build Coastguard Worker /// 1 A == B
32*9880d681SAndroid Build Coastguard Worker /// 2 A < B
33*9880d681SAndroid Build Coastguard Worker ///
34*9880d681SAndroid Build Coastguard Worker /// <=> Value Definition
35*9880d681SAndroid Build Coastguard Worker /// 000 0 Always false
36*9880d681SAndroid Build Coastguard Worker /// 001 1 A > B
37*9880d681SAndroid Build Coastguard Worker /// 010 2 A == B
38*9880d681SAndroid Build Coastguard Worker /// 011 3 A >= B
39*9880d681SAndroid Build Coastguard Worker /// 100 4 A < B
40*9880d681SAndroid Build Coastguard Worker /// 101 5 A != B
41*9880d681SAndroid Build Coastguard Worker /// 110 6 A <= B
42*9880d681SAndroid Build Coastguard Worker /// 111 7 Always true
43*9880d681SAndroid Build Coastguard Worker ///
getICmpCode(const ICmpInst * ICI,bool InvertPred)44*9880d681SAndroid Build Coastguard Worker unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
45*9880d681SAndroid Build Coastguard Worker ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
46*9880d681SAndroid Build Coastguard Worker : ICI->getPredicate();
47*9880d681SAndroid Build Coastguard Worker switch (Pred) {
48*9880d681SAndroid Build Coastguard Worker // False -> 0
49*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_UGT: return 1; // 001
50*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_SGT: return 1; // 001
51*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_EQ: return 2; // 010
52*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_UGE: return 3; // 011
53*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_SGE: return 3; // 011
54*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_ULT: return 4; // 100
55*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_SLT: return 4; // 100
56*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_NE: return 5; // 101
57*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_ULE: return 6; // 110
58*9880d681SAndroid Build Coastguard Worker case ICmpInst::ICMP_SLE: return 6; // 110
59*9880d681SAndroid Build Coastguard Worker // True -> 7
60*9880d681SAndroid Build Coastguard Worker default:
61*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid ICmp predicate!");
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker /// getICmpValue - This is the complement of getICmpCode, which turns an
66*9880d681SAndroid Build Coastguard Worker /// opcode and two operands into either a constant true or false, or the
67*9880d681SAndroid Build Coastguard Worker /// predicate for a new ICmp instruction. The sign is passed in to determine
68*9880d681SAndroid Build Coastguard Worker /// which kind of predicate to use in the new icmp instruction.
69*9880d681SAndroid Build Coastguard Worker /// Non-NULL return value will be a true or false constant.
70*9880d681SAndroid Build Coastguard Worker /// NULL return means a new ICmp is needed. The predicate for which is
71*9880d681SAndroid Build Coastguard Worker /// output in NewICmpPred.
getICmpValue(bool Sign,unsigned Code,Value * LHS,Value * RHS,CmpInst::Predicate & NewICmpPred)72*9880d681SAndroid Build Coastguard Worker Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
73*9880d681SAndroid Build Coastguard Worker CmpInst::Predicate &NewICmpPred) {
74*9880d681SAndroid Build Coastguard Worker switch (Code) {
75*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Illegal ICmp code!");
76*9880d681SAndroid Build Coastguard Worker case 0: // False.
77*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
78*9880d681SAndroid Build Coastguard Worker case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
79*9880d681SAndroid Build Coastguard Worker case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
80*9880d681SAndroid Build Coastguard Worker case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
81*9880d681SAndroid Build Coastguard Worker case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
82*9880d681SAndroid Build Coastguard Worker case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
83*9880d681SAndroid Build Coastguard Worker case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
84*9880d681SAndroid Build Coastguard Worker case 7: // True.
85*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker return nullptr;
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker /// PredicatesFoldable - Return true if both predicates match sign or if at
91*9880d681SAndroid Build Coastguard Worker /// least one of them is an equality comparison (which is signless).
PredicatesFoldable(ICmpInst::Predicate p1,ICmpInst::Predicate p2)92*9880d681SAndroid Build Coastguard Worker bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
93*9880d681SAndroid Build Coastguard Worker return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
94*9880d681SAndroid Build Coastguard Worker (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
95*9880d681SAndroid Build Coastguard Worker (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
96*9880d681SAndroid Build Coastguard Worker }
97