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 *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
69     return nullptr;
70   }
71 
72   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
73                  bool IsInBounds = false) const override {
74     return nullptr;
75   }
76 
77   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
78     return nullptr;
79   }
80 
81   Value *FoldExtractValue(Value *Agg,
82                           ArrayRef<unsigned> IdxList) const override {
83     return nullptr;
84   }
85 
86   Value *FoldInsertValue(Value *Agg, Value *Val,
87                          ArrayRef<unsigned> IdxList) const override {
88     return nullptr;
89   }
90 
91   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
92     return nullptr;
93   }
94 
95   Value *FoldInsertElement(Value *Vec, Value *NewElt,
96                            Value *Idx) const override {
97     return nullptr;
98   }
99 
100   Value *FoldShuffleVector(Value *V1, Value *V2,
101                            ArrayRef<int> Mask) const override {
102     return nullptr;
103   }
104 
105   //===--------------------------------------------------------------------===//
106   // Unary Operators
107   //===--------------------------------------------------------------------===//
108 
109   Instruction *CreateFNeg(Constant *C) const override {
110     return UnaryOperator::CreateFNeg(C);
111   }
112 
113   Instruction *CreateUnOp(Instruction::UnaryOps Opc,
114                           Constant *C) const override {
115     return UnaryOperator::Create(Opc, C);
116   }
117 
118   //===--------------------------------------------------------------------===//
119   // Cast/Conversion Operators
120   //===--------------------------------------------------------------------===//
121 
122   Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
123                           Type *DestTy) const override {
124     return CastInst::Create(Op, C, DestTy);
125   }
126 
127   Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
128     return CastInst::CreatePointerCast(C, DestTy);
129   }
130 
131   Instruction *CreatePointerBitCastOrAddrSpaceCast(
132       Constant *C, Type *DestTy) const override {
133     return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
134   }
135 
136   Instruction *CreateIntCast(Constant *C, Type *DestTy,
137                              bool isSigned) const override {
138     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
139   }
140 
141   Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
142     return CastInst::CreateFPCast(C, DestTy);
143   }
144 
145   Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
146     return CreateCast(Instruction::BitCast, C, DestTy);
147   }
148 
149   Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
150     return CreateCast(Instruction::IntToPtr, C, DestTy);
151   }
152 
153   Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
154     return CreateCast(Instruction::PtrToInt, C, DestTy);
155   }
156 
157   Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
158     return CastInst::CreateZExtOrBitCast(C, DestTy);
159   }
160 
161   Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
162     return CastInst::CreateSExtOrBitCast(C, DestTy);
163   }
164 
165   Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
166     return CastInst::CreateTruncOrBitCast(C, DestTy);
167   }
168 
169   //===--------------------------------------------------------------------===//
170   // Compare Instructions
171   //===--------------------------------------------------------------------===//
172 
173   Instruction *CreateFCmp(CmpInst::Predicate P,
174                           Constant *LHS, Constant *RHS) const override {
175     return new FCmpInst(P, LHS, RHS);
176   }
177 };
178 
179 } // end namespace llvm
180 
181 #endif // LLVM_IR_NOFOLDER_H
182