xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/IRBuilderFolder.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- IRBuilderFolder.h - Const folder interface for IRBuilder -*- 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 defines for constant folding interface used by IRBuilder.
10 // It is implemented by ConstantFolder (default), TargetFolder and NoFoler.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_IRBUILDERFOLDER_H
15 #define LLVM_IR_IRBUILDERFOLDER_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/IR/GEPNoWrapFlags.h"
19 #include "llvm/IR/InstrTypes.h"
20 #include "llvm/IR/Instruction.h"
21 
22 namespace llvm {
23 
24 /// IRBuilderFolder - Interface for constant folding in IRBuilder.
25 class IRBuilderFolder {
26 public:
27   virtual ~IRBuilderFolder();
28 
29   //===--------------------------------------------------------------------===//
30   // Value-based folders.
31   //
32   // Return an existing value or a constant if the operation can be simplified.
33   // Otherwise return nullptr.
34   //===--------------------------------------------------------------------===//
35 
36   virtual Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
37                            Value *RHS) const = 0;
38 
39   virtual Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS,
40                                 Value *RHS, bool IsExact) const = 0;
41 
42   virtual Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS,
43                                  Value *RHS, bool HasNUW,
44                                  bool HasNSW) const = 0;
45 
46   virtual Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS,
47                               Value *RHS, FastMathFlags FMF) const = 0;
48 
49   virtual Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
50                              FastMathFlags FMF) const = 0;
51 
52   virtual Value *FoldCmp(CmpInst::Predicate P, Value *LHS,
53                          Value *RHS) const = 0;
54 
55   virtual Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
56                          GEPNoWrapFlags NW) const = 0;
57 
58   virtual Value *FoldSelect(Value *C, Value *True, Value *False) const = 0;
59 
60   virtual Value *FoldExtractValue(Value *Agg,
61                                   ArrayRef<unsigned> IdxList) const = 0;
62 
63   virtual Value *FoldInsertValue(Value *Agg, Value *Val,
64                                  ArrayRef<unsigned> IdxList) const = 0;
65 
66   virtual Value *FoldExtractElement(Value *Vec, Value *Idx) const = 0;
67 
68   virtual Value *FoldInsertElement(Value *Vec, Value *NewElt,
69                                    Value *Idx) const = 0;
70 
71   virtual Value *FoldShuffleVector(Value *V1, Value *V2,
72                                    ArrayRef<int> Mask) const = 0;
73 
74   virtual Value *FoldCast(Instruction::CastOps Op, Value *V,
75                           Type *DestTy) const = 0;
76 
77   virtual Value *
78   FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty,
79                       Instruction *FMFSource = nullptr) const = 0;
80 
81   //===--------------------------------------------------------------------===//
82   // Cast/Conversion Operators
83   //===--------------------------------------------------------------------===//
84 
85   virtual Value *CreatePointerCast(Constant *C, Type *DestTy) const = 0;
86   virtual Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
87                                                      Type *DestTy) const = 0;
88 };
89 
90 } // end namespace llvm
91 
92 #endif // LLVM_IR_IRBUILDERFOLDER_H
93