blob: 9f4d9c7e3981072cc69233e4abc0e208c2d843a2 [file] [log] [blame]
Pete Cooperebf98c12011-12-17 01:20:32 +00001//===- 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/Transforms/Utils/CmpInstAnalysis.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Instructions.h"
Pete Cooperebf98c12011-12-17 01:20:32 +000018
19using namespace llvm;
20
Pete Cooperebf98c12011-12-17 01:20:32 +000021unsigned 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!");
Pete Cooperebf98c12011-12-17 01:20:32 +000039 }
40}
41
Pete Cooperebf98c12011-12-17 01:20:32 +000042Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
43 CmpInst::Predicate &NewICmpPred) {
44 switch (Code) {
Craig Toppera2886c22012-02-07 05:05:23 +000045 default: llvm_unreachable("Illegal ICmp code!");
Pete Cooperebf98c12011-12-17 01:20:32 +000046 case 0: // False.
47 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
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.
55 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
56 }
Craig Topperf40110f2014-04-25 05:29:35 +000057 return nullptr;
Pete Cooperebf98c12011-12-17 01:20:32 +000058}
59
Pete Cooperebf98c12011-12-17 01:20:32 +000060bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
61 return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
62 (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
63 (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
64}
Sanjay Patel683170b2016-07-20 17:18:45 +000065
66bool llvm::decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred,
67 Value *&X, Value *&Y, Value *&Z) {
68 ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1));
69 if (!C)
70 return false;
71
72 switch (I->getPredicate()) {
73 default:
74 return false;
75 case ICmpInst::ICMP_SLT:
Craig Topperbcfd2d12017-04-20 16:56:25 +000076 // X < 0 is equivalent to (X & SignMask) != 0.
Sanjay Patel683170b2016-07-20 17:18:45 +000077 if (!C->isZero())
78 return false;
Craig Topperbcfd2d12017-04-20 16:56:25 +000079 Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
Sanjay Patel683170b2016-07-20 17:18:45 +000080 Pred = ICmpInst::ICMP_NE;
81 break;
82 case ICmpInst::ICMP_SGT:
Craig Topperbcfd2d12017-04-20 16:56:25 +000083 // X > -1 is equivalent to (X & SignMask) == 0.
Sanjay Patel683170b2016-07-20 17:18:45 +000084 if (!C->isAllOnesValue())
85 return false;
Craig Topperbcfd2d12017-04-20 16:56:25 +000086 Y = ConstantInt::get(I->getContext(), APInt::getSignMask(C->getBitWidth()));
Sanjay Patel683170b2016-07-20 17:18:45 +000087 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);
106 Z = ConstantInt::getNullValue(C->getType());
107 return true;
108}