LLVM API Documentation
00001 //===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file holds routines to help analyse compare instructions 00011 // and fold them into constants or other compare instructions 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Transforms/Utils/CmpInstAnalysis.h" 00016 #include "llvm/IR/Constants.h" 00017 #include "llvm/IR/Instructions.h" 00018 00019 using namespace llvm; 00020 00021 /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits 00022 /// are carefully arranged to allow folding of expressions such as: 00023 /// 00024 /// (A < B) | (A > B) --> (A != B) 00025 /// 00026 /// Note that this is only valid if the first and second predicates have the 00027 /// same sign. Is illegal to do: (A u< B) | (A s> B) 00028 /// 00029 /// Three bits are used to represent the condition, as follows: 00030 /// 0 A > B 00031 /// 1 A == B 00032 /// 2 A < B 00033 /// 00034 /// <=> Value Definition 00035 /// 000 0 Always false 00036 /// 001 1 A > B 00037 /// 010 2 A == B 00038 /// 011 3 A >= B 00039 /// 100 4 A < B 00040 /// 101 5 A != B 00041 /// 110 6 A <= B 00042 /// 111 7 Always true 00043 /// 00044 unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) { 00045 ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate() 00046 : ICI->getPredicate(); 00047 switch (Pred) { 00048 // False -> 0 00049 case ICmpInst::ICMP_UGT: return 1; // 001 00050 case ICmpInst::ICMP_SGT: return 1; // 001 00051 case ICmpInst::ICMP_EQ: return 2; // 010 00052 case ICmpInst::ICMP_UGE: return 3; // 011 00053 case ICmpInst::ICMP_SGE: return 3; // 011 00054 case ICmpInst::ICMP_ULT: return 4; // 100 00055 case ICmpInst::ICMP_SLT: return 4; // 100 00056 case ICmpInst::ICMP_NE: return 5; // 101 00057 case ICmpInst::ICMP_ULE: return 6; // 110 00058 case ICmpInst::ICMP_SLE: return 6; // 110 00059 // True -> 7 00060 default: 00061 llvm_unreachable("Invalid ICmp predicate!"); 00062 } 00063 } 00064 00065 /// getICmpValue - This is the complement of getICmpCode, which turns an 00066 /// opcode and two operands into either a constant true or false, or the 00067 /// predicate for a new ICmp instruction. The sign is passed in to determine 00068 /// which kind of predicate to use in the new icmp instruction. 00069 /// Non-NULL return value will be a true or false constant. 00070 /// NULL return means a new ICmp is needed. The predicate for which is 00071 /// output in NewICmpPred. 00072 Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, 00073 CmpInst::Predicate &NewICmpPred) { 00074 switch (Code) { 00075 default: llvm_unreachable("Illegal ICmp code!"); 00076 case 0: // False. 00077 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); 00078 case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break; 00079 case 2: NewICmpPred = ICmpInst::ICMP_EQ; break; 00080 case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break; 00081 case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break; 00082 case 5: NewICmpPred = ICmpInst::ICMP_NE; break; 00083 case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break; 00084 case 7: // True. 00085 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1); 00086 } 00087 return nullptr; 00088 } 00089 00090 /// PredicatesFoldable - Return true if both predicates match sign or if at 00091 /// least one of them is an equality comparison (which is signless). 00092 bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { 00093 return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) || 00094 (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) || 00095 (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1)); 00096 }