blob: 0b13005ca076abc7b7fa3069ee7b605e045ecbb4 [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}