1 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file holds routines to help analyse compare instructions 10 // and fold them into constants or other compare instructions 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H 15 #define LLVM_ANALYSIS_CMPINSTANALYSIS_H 16 17 #include "llvm/ADT/APInt.h" 18 #include "llvm/IR/InstrTypes.h" 19 20 namespace llvm { 21 class Type; 22 class Value; 23 24 /// Encode a icmp predicate into a three bit mask. These bits are carefully 25 /// arranged to allow folding of expressions such as: 26 /// 27 /// (A < B) | (A > B) --> (A != B) 28 /// 29 /// Note that this is only valid if the first and second predicates have the 30 /// same sign. It is illegal to do: (A u< B) | (A s> B) 31 /// 32 /// Three bits are used to represent the condition, as follows: 33 /// 0 A > B 34 /// 1 A == B 35 /// 2 A < B 36 /// 37 /// <=> Value Definition 38 /// 000 0 Always false 39 /// 001 1 A > B 40 /// 010 2 A == B 41 /// 011 3 A >= B 42 /// 100 4 A < B 43 /// 101 5 A != B 44 /// 110 6 A <= B 45 /// 111 7 Always true 46 /// 47 unsigned getICmpCode(CmpInst::Predicate Pred); 48 49 /// This is the complement of getICmpCode. It turns a predicate code into 50 /// either a constant true or false or the predicate for a new ICmp. 51 /// The sign is passed in to determine which kind of predicate to use in the 52 /// new ICmp instruction. 53 /// Non-NULL return value will be a true or false constant. 54 /// NULL return means a new ICmp is needed. The predicate is output in Pred. 55 Constant *getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, 56 CmpInst::Predicate &Pred); 57 58 /// Return true if both predicates match sign or if at least one of them is an 59 /// equality comparison (which is signless). 60 bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2); 61 62 /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate 63 /// into a four bit mask. 64 inline unsigned getFCmpCode(CmpInst::Predicate CC) { 65 assert(CmpInst::FCMP_FALSE <= CC && CC <= CmpInst::FCMP_TRUE && 66 "Unexpected FCmp predicate!"); 67 // Take advantage of the bit pattern of CmpInst::Predicate here. 68 // U L G E 69 static_assert(CmpInst::FCMP_FALSE == 0); // 0 0 0 0 70 static_assert(CmpInst::FCMP_OEQ == 1); // 0 0 0 1 71 static_assert(CmpInst::FCMP_OGT == 2); // 0 0 1 0 72 static_assert(CmpInst::FCMP_OGE == 3); // 0 0 1 1 73 static_assert(CmpInst::FCMP_OLT == 4); // 0 1 0 0 74 static_assert(CmpInst::FCMP_OLE == 5); // 0 1 0 1 75 static_assert(CmpInst::FCMP_ONE == 6); // 0 1 1 0 76 static_assert(CmpInst::FCMP_ORD == 7); // 0 1 1 1 77 static_assert(CmpInst::FCMP_UNO == 8); // 1 0 0 0 78 static_assert(CmpInst::FCMP_UEQ == 9); // 1 0 0 1 79 static_assert(CmpInst::FCMP_UGT == 10); // 1 0 1 0 80 static_assert(CmpInst::FCMP_UGE == 11); // 1 0 1 1 81 static_assert(CmpInst::FCMP_ULT == 12); // 1 1 0 0 82 static_assert(CmpInst::FCMP_ULE == 13); // 1 1 0 1 83 static_assert(CmpInst::FCMP_UNE == 14); // 1 1 1 0 84 static_assert(CmpInst::FCMP_TRUE == 15); // 1 1 1 1 85 return CC; 86 } 87 88 /// This is the complement of getFCmpCode. It turns a predicate code into 89 /// either a constant true or false or the predicate for a new FCmp. 90 /// Non-NULL return value will be a true or false constant. 91 /// NULL return means a new ICmp is needed. The predicate is output in Pred. 92 Constant *getPredForFCmpCode(unsigned Code, Type *OpTy, 93 CmpInst::Predicate &Pred); 94 95 /// Represents the operation icmp (X & Mask) pred C, where pred can only be 96 /// eq or ne. 97 struct DecomposedBitTest { 98 Value *X; 99 CmpInst::Predicate Pred; 100 APInt Mask; 101 APInt C; 102 }; 103 104 /// Decompose an icmp into the form ((X & Mask) pred C) if possible. 105 /// Unless \p AllowNonZeroC is true, C will always be 0. 106 std::optional<DecomposedBitTest> 107 decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, 108 bool LookThroughTrunc = true, 109 bool AllowNonZeroC = false); 110 111 /// Decompose an icmp into the form ((X & Mask) pred C) if 112 /// possible. Unless \p AllowNonZeroC is true, C will always be 0. 113 std::optional<DecomposedBitTest> 114 decomposeBitTest(Value *Cond, bool LookThroughTrunc = true, 115 bool AllowNonZeroC = false); 116 117 } // end namespace llvm 118 119 #endif 120