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 
FoldBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS)48   Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
49                    Value *RHS) const override {
50     return nullptr;
51   }
52 
FoldExactBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,bool IsExact)53   Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
54                         bool IsExact) const override {
55     return nullptr;
56   }
57 
FoldNoWrapBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,bool HasNUW,bool HasNSW)58   Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
59                          bool HasNUW, bool HasNSW) const override {
60     return nullptr;
61   }
62 
FoldBinOpFMF(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,FastMathFlags FMF)63   Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
64                       FastMathFlags FMF) const override {
65     return nullptr;
66   }
67 
FoldUnOpFMF(Instruction::UnaryOps Opc,Value * V,FastMathFlags FMF)68   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
69                      FastMathFlags FMF) const override {
70     return nullptr;
71   }
72 
FoldICmp(CmpInst::Predicate P,Value * LHS,Value * RHS)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 
FoldSelect(Value * C,Value * True,Value * False)82   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
83     return nullptr;
84   }
85 
FoldExtractValue(Value * Agg,ArrayRef<unsigned> IdxList)86   Value *FoldExtractValue(Value *Agg,
87                           ArrayRef<unsigned> IdxList) const override {
88     return nullptr;
89   }
90 
FoldInsertValue(Value * Agg,Value * Val,ArrayRef<unsigned> IdxList)91   Value *FoldInsertValue(Value *Agg, Value *Val,
92                          ArrayRef<unsigned> IdxList) const override {
93     return nullptr;
94   }
95 
FoldExtractElement(Value * Vec,Value * Idx)96   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
97     return nullptr;
98   }
99 
FoldInsertElement(Value * Vec,Value * NewElt,Value * Idx)100   Value *FoldInsertElement(Value *Vec, Value *NewElt,
101                            Value *Idx) const override {
102     return nullptr;
103   }
104 
FoldShuffleVector(Value * V1,Value * V2,ArrayRef<int> Mask)105   Value *FoldShuffleVector(Value *V1, Value *V2,
106                            ArrayRef<int> Mask) const override {
107     return nullptr;
108   }
109 
FoldCast(Instruction::CastOps Op,Value * V,Type * DestTy)110   Value *FoldCast(Instruction::CastOps Op, Value *V,
111                   Type *DestTy) const override {
112     return nullptr;
113   }
114 
FoldBinaryIntrinsic(Intrinsic::ID ID,Value * LHS,Value * RHS,Type * Ty,Instruction * FMFSource)115   Value *FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty,
116                              Instruction *FMFSource) const override {
117     return nullptr;
118   }
119 
120   //===--------------------------------------------------------------------===//
121   // Cast/Conversion Operators
122   //===--------------------------------------------------------------------===//
123 
CreatePointerCast(Constant * C,Type * DestTy)124   Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
125     return CastInst::CreatePointerCast(C, DestTy);
126   }
127 
CreatePointerBitCastOrAddrSpaceCast(Constant * C,Type * DestTy)128   Instruction *CreatePointerBitCastOrAddrSpaceCast(
129       Constant *C, Type *DestTy) const override {
130     return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
131   }
132 
133   //===--------------------------------------------------------------------===//
134   // Compare Instructions
135   //===--------------------------------------------------------------------===//
136 
CreateFCmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)137   Instruction *CreateFCmp(CmpInst::Predicate P,
138                           Constant *LHS, Constant *RHS) const override {
139     return new FCmpInst(P, LHS, RHS);
140   }
141 };
142 
143 } // end namespace llvm
144 
145 #endif // LLVM_IR_NOFOLDER_H
146