|
|
|||
File indexing completed on 2026-05-10 08:43:12
0001 //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file holds routines to help analyse compare instructions 0010 // and fold them into constants or other compare instructions 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H 0015 #define LLVM_ANALYSIS_CMPINSTANALYSIS_H 0016 0017 #include "llvm/ADT/APInt.h" 0018 #include "llvm/IR/InstrTypes.h" 0019 0020 namespace llvm { 0021 class Type; 0022 class Value; 0023 0024 /// Encode a icmp predicate into a three bit mask. These bits are carefully 0025 /// arranged to allow folding of expressions such as: 0026 /// 0027 /// (A < B) | (A > B) --> (A != B) 0028 /// 0029 /// Note that this is only valid if the first and second predicates have the 0030 /// same sign. It is illegal to do: (A u< B) | (A s> B) 0031 /// 0032 /// Three bits are used to represent the condition, as follows: 0033 /// 0 A > B 0034 /// 1 A == B 0035 /// 2 A < B 0036 /// 0037 /// <=> Value Definition 0038 /// 000 0 Always false 0039 /// 001 1 A > B 0040 /// 010 2 A == B 0041 /// 011 3 A >= B 0042 /// 100 4 A < B 0043 /// 101 5 A != B 0044 /// 110 6 A <= B 0045 /// 111 7 Always true 0046 /// 0047 unsigned getICmpCode(CmpInst::Predicate Pred); 0048 0049 /// This is the complement of getICmpCode. It turns a predicate code into 0050 /// either a constant true or false or the predicate for a new ICmp. 0051 /// The sign is passed in to determine which kind of predicate to use in the 0052 /// new ICmp instruction. 0053 /// Non-NULL return value will be a true or false constant. 0054 /// NULL return means a new ICmp is needed. The predicate is output in Pred. 0055 Constant *getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, 0056 CmpInst::Predicate &Pred); 0057 0058 /// Return true if both predicates match sign or if at least one of them is an 0059 /// equality comparison (which is signless). 0060 bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2); 0061 0062 /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate 0063 /// into a four bit mask. 0064 inline unsigned getFCmpCode(CmpInst::Predicate CC) { 0065 assert(CmpInst::FCMP_FALSE <= CC && CC <= CmpInst::FCMP_TRUE && 0066 "Unexpected FCmp predicate!"); 0067 // Take advantage of the bit pattern of CmpInst::Predicate here. 0068 // U L G E 0069 static_assert(CmpInst::FCMP_FALSE == 0); // 0 0 0 0 0070 static_assert(CmpInst::FCMP_OEQ == 1); // 0 0 0 1 0071 static_assert(CmpInst::FCMP_OGT == 2); // 0 0 1 0 0072 static_assert(CmpInst::FCMP_OGE == 3); // 0 0 1 1 0073 static_assert(CmpInst::FCMP_OLT == 4); // 0 1 0 0 0074 static_assert(CmpInst::FCMP_OLE == 5); // 0 1 0 1 0075 static_assert(CmpInst::FCMP_ONE == 6); // 0 1 1 0 0076 static_assert(CmpInst::FCMP_ORD == 7); // 0 1 1 1 0077 static_assert(CmpInst::FCMP_UNO == 8); // 1 0 0 0 0078 static_assert(CmpInst::FCMP_UEQ == 9); // 1 0 0 1 0079 static_assert(CmpInst::FCMP_UGT == 10); // 1 0 1 0 0080 static_assert(CmpInst::FCMP_UGE == 11); // 1 0 1 1 0081 static_assert(CmpInst::FCMP_ULT == 12); // 1 1 0 0 0082 static_assert(CmpInst::FCMP_ULE == 13); // 1 1 0 1 0083 static_assert(CmpInst::FCMP_UNE == 14); // 1 1 1 0 0084 static_assert(CmpInst::FCMP_TRUE == 15); // 1 1 1 1 0085 return CC; 0086 } 0087 0088 /// This is the complement of getFCmpCode. It turns a predicate code into 0089 /// either a constant true or false or the predicate for a new FCmp. 0090 /// Non-NULL return value will be a true or false constant. 0091 /// NULL return means a new ICmp is needed. The predicate is output in Pred. 0092 Constant *getPredForFCmpCode(unsigned Code, Type *OpTy, 0093 CmpInst::Predicate &Pred); 0094 0095 /// Represents the operation icmp (X & Mask) pred C, where pred can only be 0096 /// eq or ne. 0097 struct DecomposedBitTest { 0098 Value *X; 0099 CmpInst::Predicate Pred; 0100 APInt Mask; 0101 APInt C; 0102 }; 0103 0104 /// Decompose an icmp into the form ((X & Mask) pred C) if possible. 0105 /// Unless \p AllowNonZeroC is true, C will always be 0. 0106 std::optional<DecomposedBitTest> 0107 decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, 0108 bool LookThroughTrunc = true, 0109 bool AllowNonZeroC = false); 0110 0111 /// Decompose an icmp into the form ((X & Mask) pred C) if 0112 /// possible. Unless \p AllowNonZeroC is true, C will always be 0. 0113 std::optional<DecomposedBitTest> 0114 decomposeBitTest(Value *Cond, bool LookThroughTrunc = true, 0115 bool AllowNonZeroC = false); 0116 0117 } // end namespace llvm 0118 0119 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|