LLVM  4.0.0
CmpInstAnalysis.cpp
Go to the documentation of this file.
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 
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 
19 using namespace llvm;
20 
21 unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
22  ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
23  : ICI->getPredicate();
24  switch (Pred) {
25  // False -> 0
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
36  // True -> 7
37  default:
38  llvm_unreachable("Invalid ICmp predicate!");
39  }
40 }
41 
42 Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
43  CmpInst::Predicate &NewICmpPred) {
44  switch (Code) {
45  default: llvm_unreachable("Illegal ICmp code!");
46  case 0: // False.
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;
54  case 7: // True.
56  }
57  return nullptr;
58 }
59 
61  return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
64 }
65 
67  Value *&X, Value *&Y, Value *&Z) {
69  if (!C)
70  return false;
71 
72  switch (I->getPredicate()) {
73  default:
74  return false;
75  case ICmpInst::ICMP_SLT:
76  // X < 0 is equivalent to (X & SignBit) != 0.
77  if (!C->isZero())
78  return false;
80  Pred = ICmpInst::ICMP_NE;
81  break;
82  case ICmpInst::ICMP_SGT:
83  // X > -1 is equivalent to (X & SignBit) == 0.
84  if (!C->isAllOnesValue())
85  return false;
87  Pred = ICmpInst::ICMP_EQ;
88  break;
89  case ICmpInst::ICMP_ULT:
90  // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
91  if (!C->getValue().isPowerOf2())
92  return false;
93  Y = ConstantInt::get(I->getContext(), -C->getValue());
94  Pred = ICmpInst::ICMP_EQ;
95  break;
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())
99  return false;
100  Y = ConstantInt::get(I->getContext(), ~C->getValue());
101  Pred = ICmpInst::ICMP_NE;
102  break;
103  }
104 
105  X = I->getOperand(0);
107  return true;
108 }
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:177
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Definition: APInt.h:451
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1112
bool decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred, Value *&X, Value *&Y, Value *&Z)
Decompose an icmp into the form ((X & Y) pred Z) if possible.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:148
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
Encode a icmp predicate into a three bit mask.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
Definition: InstrTypes.h:984
unsigned less or equal
Definition: InstrTypes.h:906
unsigned less than
Definition: InstrTypes.h:905
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:1027
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:195
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:143
bool isEquality() const
Return true if this predicate is either EQ or NE.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:880
Value * getOperand(unsigned i) const
Definition: User.h:145
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:960
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:654
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:391
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
signed greater than
Definition: InstrTypes.h:907
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
signed less than
Definition: InstrTypes.h:909
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:558
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:198
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
Return true if both predicates match sign or if at least one of them is an equality comparison (which...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:105
signed less or equal
Definition: InstrTypes.h:910
Value * getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, CmpInst::Predicate &NewICmpPred)
This is the complement of getICmpCode, which turns an opcode and two operands into either a constant ...
unsigned greater or equal
Definition: InstrTypes.h:904
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
LLVM Value Representation.
Definition: Value.h:71
unsigned greater than
Definition: InstrTypes.h:903
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
signed greater or equal
Definition: InstrTypes.h:908