Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/CmpInstAnalysis.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file holds routines to help analyse compare instructions
10
// and fold them into constants or other compare instructions
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Analysis/CmpInstAnalysis.h"
15
#include "llvm/IR/Constants.h"
16
#include "llvm/IR/Instructions.h"
17
#include "llvm/IR/PatternMatch.h"
18
19
using namespace llvm;
20
21
88
unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
22
88
  ICmpInst::Predicate Pred = InvertPred ? 
ICI->getInversePredicate()0
23
88
                                        : ICI->getPredicate();
24
88
  switch (Pred) {
25
88
      // False -> 0
26
88
    
case ICmpInst::ICMP_UGT: return 14
; // 001
27
88
    
case ICmpInst::ICMP_SGT: return 111
; // 001
28
88
    
case ICmpInst::ICMP_EQ: return 234
; // 010
29
88
    
case ICmpInst::ICMP_UGE: return 30
; // 011
30
88
    
case ICmpInst::ICMP_SGE: return 30
; // 011
31
88
    
case ICmpInst::ICMP_ULT: return 411
; // 100
32
88
    
case ICmpInst::ICMP_SLT: return 420
; // 100
33
88
    
case ICmpInst::ICMP_NE: return 58
; // 101
34
88
    
case ICmpInst::ICMP_ULE: return 60
; // 110
35
88
    
case ICmpInst::ICMP_SLE: return 60
; // 110
36
88
      // True -> 7
37
88
    default:
38
0
      llvm_unreachable("Invalid ICmp predicate!");
39
88
  }
40
88
}
41
42
Constant *llvm::getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy,
43
44
                                   CmpInst::Predicate &Pred) {
44
44
  switch (Code) {
45
44
    
default: 0
llvm_unreachable0
("Illegal ICmp code!");
46
44
    case 0: // False.
47
1
      return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 0);
48
44
    
case 1: Pred = Sign 6
?
ICmpInst::ICMP_SGT6
:
ICmpInst::ICMP_UGT0
; break;
49
44
    
case 2: Pred = ICmpInst::ICMP_EQ; break0
;
50
44
    
case 3: Pred = Sign 10
?
ICmpInst::ICMP_SGE8
:
ICmpInst::ICMP_UGE2
; break;
51
44
    
case 4: Pred = Sign 0
?
ICmpInst::ICMP_SLT0
:
ICmpInst::ICMP_ULT0
; break;
52
44
    
case 5: Pred = ICmpInst::ICMP_NE; break5
;
53
44
    
case 6: Pred = Sign 20
?
ICmpInst::ICMP_SLE11
:
ICmpInst::ICMP_ULE9
; break;
54
44
    case 7: // True.
55
2
      return ConstantInt::get(CmpInst::makeCmpResultType(OpTy), 1);
56
41
  }
57
41
  return nullptr;
58
41
}
59
60
748k
bool llvm::predicatesFoldable(ICmpInst::Predicate P1, ICmpInst::Predicate P2) {
61
748k
  return (CmpInst::isSigned(P1) == CmpInst::isSigned(P2)) ||
62
748k
         
(179k
CmpInst::isSigned(P1)179k
&&
ICmpInst::isEquality(P2)51.9k
) ||
63
748k
         
(153k
CmpInst::isSigned(P2)153k
&&
ICmpInst::isEquality(P1)127k
);
64
748k
}
65
66
bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
67
                                CmpInst::Predicate &Pred,
68
3.35M
                                Value *&X, APInt &Mask, bool LookThruTrunc) {
69
3.35M
  using namespace PatternMatch;
70
3.35M
71
3.35M
  const APInt *C;
72
3.35M
  if (!match(RHS, m_APInt(C)))
73
1.26M
    return false;
74
2.09M
75
2.09M
  switch (Pred) {
76
2.09M
  default:
77
1.07M
    return false;
78
2.09M
  case ICmpInst::ICMP_SLT:
79
437k
    // X < 0 is equivalent to (X & SignMask) != 0.
80
437k
    if (!C->isNullValue())
81
179k
      return false;
82
258k
    Mask = APInt::getSignMask(C->getBitWidth());
83
258k
    Pred = ICmpInst::ICMP_NE;
84
258k
    break;
85
258k
  case ICmpInst::ICMP_SLE:
86
84
    // X <= -1 is equivalent to (X & SignMask) != 0.
87
84
    if (!C->isAllOnesValue())
88
83
      return false;
89
1
    Mask = APInt::getSignMask(C->getBitWidth());
90
1
    Pred = ICmpInst::ICMP_NE;
91
1
    break;
92
304k
  case ICmpInst::ICMP_SGT:
93
304k
    // X > -1 is equivalent to (X & SignMask) == 0.
94
304k
    if (!C->isAllOnesValue())
95
258k
      return false;
96
45.9k
    Mask = APInt::getSignMask(C->getBitWidth());
97
45.9k
    Pred = ICmpInst::ICMP_EQ;
98
45.9k
    break;
99
45.9k
  case ICmpInst::ICMP_SGE:
100
275
    // X >= 0 is equivalent to (X & SignMask) == 0.
101
275
    if (!C->isNullValue())
102
106
      return false;
103
169
    Mask = APInt::getSignMask(C->getBitWidth());
104
169
    Pred = ICmpInst::ICMP_EQ;
105
169
    break;
106
149k
  case ICmpInst::ICMP_ULT:
107
149k
    // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
108
149k
    if (!C->isPowerOf2())
109
34.3k
      return false;
110
114k
    Mask = -*C;
111
114k
    Pred = ICmpInst::ICMP_EQ;
112
114k
    break;
113
114k
  case ICmpInst::ICMP_ULE:
114
147
    // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
115
147
    if (!(*C + 1).isPowerOf2())
116
95
      return false;
117
52
    Mask = ~*C;
118
52
    Pred = ICmpInst::ICMP_EQ;
119
52
    break;
120
135k
  case ICmpInst::ICMP_UGT:
121
135k
    // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
122
135k
    if (!(*C + 1).isPowerOf2())
123
73.6k
      return false;
124
61.4k
    Mask = ~*C;
125
61.4k
    Pred = ICmpInst::ICMP_NE;
126
61.4k
    break;
127
61.4k
  case ICmpInst::ICMP_UGE:
128
193
    // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
129
193
    if (!C->isPowerOf2())
130
164
      return false;
131
29
    Mask = -*C;
132
29
    Pred = ICmpInst::ICMP_NE;
133
29
    break;
134
481k
  }
135
481k
136
481k
  if (LookThruTrunc && 
match(LHS, m_Trunc(m_Value(X)))481k
) {
137
2.31k
    Mask = Mask.zext(X->getType()->getScalarSizeInBits());
138
478k
  } else {
139
478k
    X = LHS;
140
478k
  }
141
481k
142
481k
  return true;
143
481k
}