LLVM  5.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 & SignMask) != 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 & SignMask) == 0.
84  if (!C->isMinusOne())
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:172
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1074
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:143
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:958
unsigned less or equal
Definition: InstrTypes.h:886
unsigned less than
Definition: InstrTypes.h:885
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:1001
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:209
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:207
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
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:860
Value * getOperand(unsigned i) const
Definition: User.h:154
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:934
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:697
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:457
signed greater than
Definition: InstrTypes.h:887
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:548
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
signed less than
Definition: InstrTypes.h:889
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:560
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
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...
signed less or equal
Definition: InstrTypes.h:890
llvm_unreachable("Unknown RegBank!")
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:884
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
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:323
LLVM Value Representation.
Definition: Value.h:73
unsigned greater than
Definition: InstrTypes.h:883
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
signed greater or equal
Definition: InstrTypes.h:888