LCOV - code coverage report
Current view: top level - lib/Analysis - CmpInstAnalysis.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 65 77 84.4 %
Date: 2017-09-14 15:23:50 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file holds routines to help analyse compare instructions
      11             : // and fold them into constants or other compare instructions
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/Analysis/CmpInstAnalysis.h"
      16             : #include "llvm/IR/Constants.h"
      17             : #include "llvm/IR/Instructions.h"
      18             : #include "llvm/IR/PatternMatch.h"
      19             : 
      20             : using namespace llvm;
      21             : 
      22          24 : unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
      23          48 :   ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
      24          48 :                                         : ICI->getPredicate();
      25          24 :   switch (Pred) {
      26             :       // False -> 0
      27             :     case ICmpInst::ICMP_UGT: return 1;  // 001
      28             :     case ICmpInst::ICMP_SGT: return 1;  // 001
      29           9 :     case ICmpInst::ICMP_EQ:  return 2;  // 010
      30           0 :     case ICmpInst::ICMP_UGE: return 3;  // 011
      31           0 :     case ICmpInst::ICMP_SGE: return 3;  // 011
      32           3 :     case ICmpInst::ICMP_ULT: return 4;  // 100
      33           5 :     case ICmpInst::ICMP_SLT: return 4;  // 100
      34           1 :     case ICmpInst::ICMP_NE:  return 5;  // 101
      35           0 :     case ICmpInst::ICMP_ULE: return 6;  // 110
      36           0 :     case ICmpInst::ICMP_SLE: return 6;  // 110
      37             :       // True -> 7
      38           0 :     default:
      39           0 :       llvm_unreachable("Invalid ICmp predicate!");
      40             :   }
      41             : }
      42             : 
      43          12 : Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
      44             :                           CmpInst::Predicate &NewICmpPred) {
      45          12 :   switch (Code) {
      46           0 :     default: llvm_unreachable("Illegal ICmp code!");
      47           0 :     case 0: // False.
      48           0 :       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
      49           0 :     case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
      50           0 :     case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
      51           3 :     case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
      52           0 :     case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
      53           3 :     case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
      54           5 :     case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
      55           1 :     case 7: // True.
      56           1 :       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
      57             :   }
      58             :   return nullptr;
      59             : }
      60             : 
      61        3390 : bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
      62        3526 :   return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
      63        3565 :          (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
      64         129 :          (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
      65             : }
      66             : 
      67      508958 : bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
      68             :                                 CmpInst::Predicate &Pred,
      69             :                                 Value *&X, APInt &Mask, bool LookThruTrunc) {
      70             :   using namespace PatternMatch;
      71             : 
      72             :   const APInt *C;
      73     1017916 :   if (!match(RHS, m_APInt(C)))
      74             :     return false;
      75             : 
      76      243102 :   switch (Pred) {
      77             :   default:
      78             :     return false;
      79        3648 :   case ICmpInst::ICMP_SLT:
      80             :     // X < 0 is equivalent to (X & SignMask) != 0.
      81        7296 :     if (!C->isNullValue())
      82             :       return false;
      83        1564 :     Mask = APInt::getSignMask(C->getBitWidth());
      84         391 :     Pred = ICmpInst::ICMP_NE;
      85         391 :     break;
      86          16 :   case ICmpInst::ICMP_SLE:
      87             :     // X <= -1 is equivalent to (X & SignMask) != 0.
      88          32 :     if (!C->isAllOnesValue())
      89             :       return false;
      90           4 :     Mask = APInt::getSignMask(C->getBitWidth());
      91           1 :     Pred = ICmpInst::ICMP_NE;
      92           1 :     break;
      93        5933 :   case ICmpInst::ICMP_SGT:
      94             :     // X > -1 is equivalent to (X & SignMask) == 0.
      95       11866 :     if (!C->isAllOnesValue())
      96             :       return false;
      97        1296 :     Mask = APInt::getSignMask(C->getBitWidth());
      98         324 :     Pred = ICmpInst::ICMP_EQ;
      99         324 :     break;
     100          34 :   case ICmpInst::ICMP_SGE:
     101             :     // X >= 0 is equivalent to (X & SignMask) == 0.
     102          68 :     if (!C->isNullValue())
     103             :       return false;
     104         112 :     Mask = APInt::getSignMask(C->getBitWidth());
     105          28 :     Pred = ICmpInst::ICMP_EQ;
     106          28 :     break;
     107       67826 :   case ICmpInst::ICMP_ULT:
     108             :     // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
     109       67826 :     if (!C->isPowerOf2())
     110             :       return false;
     111       76638 :     Mask = -*C;
     112       12773 :     Pred = ICmpInst::ICMP_EQ;
     113       12773 :     break;
     114          51 :   case ICmpInst::ICMP_ULE:
     115             :     // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
     116         255 :     if (!(*C + 1).isPowerOf2())
     117             :       return false;
     118         108 :     Mask = ~*C;
     119          18 :     Pred = ICmpInst::ICMP_EQ;
     120          18 :     break;
     121       32304 :   case ICmpInst::ICMP_UGT:
     122             :     // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
     123      161520 :     if (!(*C + 1).isPowerOf2())
     124             :       return false;
     125      130026 :     Mask = ~*C;
     126       21671 :     Pred = ICmpInst::ICMP_NE;
     127       21671 :     break;
     128           1 :   case ICmpInst::ICMP_UGE:
     129             :     // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
     130           1 :     if (!C->isPowerOf2())
     131             :       return false;
     132           6 :     Mask = -*C;
     133           1 :     Pred = ICmpInst::ICMP_NE;
     134           1 :     break;
     135             :   }
     136             : 
     137      146959 :   if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
     138         279 :     Mask = Mask.zext(X->getType()->getScalarSizeInBits());
     139             :   } else {
     140       35114 :     X = LHS;
     141             :   }
     142             : 
     143             :   return true;
     144             : }

Generated by: LCOV version 1.13