Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/IR/InstrTypes.h" |
| 18 | |
| 19 | namespace llvm { |
| 20 | class ICmpInst; |
| 21 | class Value; |
| 22 | |
| 23 | /// Encode a icmp predicate into a three bit mask. These bits are carefully |
| 24 | /// arranged to allow folding of expressions such as: |
| 25 | /// |
| 26 | /// (A < B) | (A > B) --> (A != B) |
| 27 | /// |
| 28 | /// Note that this is only valid if the first and second predicates have the |
| 29 | /// same sign. It is illegal to do: (A u< B) | (A s> B) |
| 30 | /// |
| 31 | /// Three bits are used to represent the condition, as follows: |
| 32 | /// 0 A > B |
| 33 | /// 1 A == B |
| 34 | /// 2 A < B |
| 35 | /// |
| 36 | /// <=> Value Definition |
| 37 | /// 000 0 Always false |
| 38 | /// 001 1 A > B |
| 39 | /// 010 2 A == B |
| 40 | /// 011 3 A >= B |
| 41 | /// 100 4 A < B |
| 42 | /// 101 5 A != B |
| 43 | /// 110 6 A <= B |
| 44 | /// 111 7 Always true |
| 45 | /// |
| 46 | unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false); |
| 47 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 48 | /// This is the complement of getICmpCode. It turns a predicate code into |
| 49 | /// either a constant true or false or the predicate for a new ICmp. |
| 50 | /// The sign is passed in to determine which kind of predicate to use in the |
| 51 | /// new ICmp instruction. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// Non-NULL return value will be a true or false constant. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 53 | /// NULL return means a new ICmp is needed. The predicate is output in Pred. |
| 54 | Constant *getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, |
| 55 | CmpInst::Predicate &Pred); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 56 | |
| 57 | /// Return true if both predicates match sign or if at least one of them is an |
| 58 | /// equality comparison (which is signless). |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 59 | bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 60 | |
| 61 | /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The |
| 62 | /// returned predicate is either == or !=. Returns false if decomposition |
| 63 | /// fails. |
| 64 | bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, |
| 65 | Value *&X, APInt &Mask, |
| 66 | bool LookThroughTrunc = true); |
| 67 | |
| 68 | } // end namespace llvm |
| 69 | |
| 70 | #endif |