1 //===- NoFolder.h - Constant folding helper ---------------------*- 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 the NoFolder class, a helper for IRBuilder.  It provides
10 // IRBuilder with a set of methods for creating unfolded constants.  This is
11 // useful for learners trying to understand how LLVM IR works, and who don't
12 // want details to be hidden by the constant folder.  For general constant
13 // creation and folding, use ConstantExpr and the routines in
14 // llvm/Analysis/ConstantFolding.h.
15 //
16 // Note: since it is not actually possible to create unfolded constants, this
17 // class returns instructions rather than constants.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_IR_NOFOLDER_H
22 #define LLVM_IR_NOFOLDER_H
23 
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/FMF.h"
27 #include "llvm/IR/IRBuilderFolder.h"
28 #include "llvm/IR/InstrTypes.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 
32 namespace llvm {
33 
34 /// NoFolder - Create "constants" (actually, instructions) with no folding.
35 class NoFolder final : public IRBuilderFolder {
36   virtual void anchor();
37 
38 public:
39   explicit NoFolder() = default;
40 
41   //===--------------------------------------------------------------------===//
42   // Value-based folders.
43   //
44   // Return an existing value or a constant if the operation can be simplified.
45   // Otherwise return nullptr.
46   //===--------------------------------------------------------------------===//
47 
48   Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
49                    Value *RHS) const override {
50     return nullptr;
51   }
52 
53   Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
54                         bool IsExact) const override {
55     return nullptr;
56   }
57 
58   Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
59                          bool HasNUW, bool HasNSW) const override {
60     return nullptr;
61   }
62 
63   Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
64                       FastMathFlags FMF) const override {
65     return nullptr;
66   }
67 
68   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
69                      FastMathFlags FMF) const override {
70     return nullptr;
71   }
72 
73   Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
74     return nullptr;
75   }
76 
77   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
78                  bool IsInBounds = false) const override {
79     return nullptr;
80   }
81 
82   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
83     return nullptr;
84   }
85 
86   Value *FoldExtractValue(Value *Agg,
87                           ArrayRef<unsigned> IdxList) const override {
88     return nullptr;
89   }
90 
91   Value *FoldInsertValue(Value *Agg, Value *Val,
92                          ArrayRef<unsigned> IdxList) const override {
93     return nullptr;
94   }
95 
96   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
97     return nullptr;
98   }
99 
100   Value *FoldInsertElement(Value *Vec, Value *NewElt,
101                            Value *Idx) const override {
102     return nullptr;
103   }
104 
105   Value *FoldShuffleVector(Value *V1, Value *V2,
106                            ArrayRef<int> Mask) const override {
107     return nullptr;
108   }
109 
110   //===--------------------------------------------------------------------===//
111   // Cast/Conversion Operators
112   //===--------------------------------------------------------------------===//
113 
114   Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
115                           Type *DestTy) const override {
116     return CastInst::Create(Op, C, DestTy);
117   }
118 
119   Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
120     return CastInst::CreatePointerCast(C, DestTy);
121   }
122 
123   Instruction *CreatePointerBitCastOrAddrSpaceCast(
124       Constant *C, Type *DestTy) const override {
125     return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
126   }
127 
128   Instruction *CreateIntCast(Constant *C, Type *DestTy,
129                              bool isSigned) const override {
130     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
131   }
132 
133   Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
134     return CastInst::CreateFPCast(C, DestTy);
135   }
136 
137   Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
138     return CreateCast(Instruction::BitCast, C, DestTy);
139   }
140 
141   Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
142     return CreateCast(Instruction::IntToPtr, C, DestTy);
143   }
144 
145   Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
146     return CreateCast(Instruction::PtrToInt, C, DestTy);
147   }
148 
149   Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
150     return CastInst::CreateZExtOrBitCast(C, DestTy);
151   }
152 
153   Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
154     return CastInst::CreateSExtOrBitCast(C, DestTy);
155   }
156 
157   Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
158     return CastInst::CreateTruncOrBitCast(C, DestTy);
159   }
160 
161   //===--------------------------------------------------------------------===//
162   // Compare Instructions
163   //===--------------------------------------------------------------------===//
164 
165   Instruction *CreateFCmp(CmpInst::Predicate P,
166                           Constant *LHS, Constant *RHS) const override {
167     return new FCmpInst(P, LHS, RHS);
168   }
169 };
170 
171 } // end namespace llvm
172 
173 #endif // LLVM_IR_NOFOLDER_H
174