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 *(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 *(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 | Value *FoldCast(Instruction::CastOps Op, Value *V, |
111 | Type *DestTy) const override { |
112 | return nullptr; |
113 | } |
114 | |
115 | //===--------------------------------------------------------------------===// |
116 | // Cast/Conversion Operators |
117 | //===--------------------------------------------------------------------===// |
118 | |
119 | Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override { |
120 | return CastInst::CreatePointerCast(S: C, Ty: DestTy); |
121 | } |
122 | |
123 | Instruction *CreatePointerBitCastOrAddrSpaceCast( |
124 | Constant *C, Type *DestTy) const override { |
125 | return CastInst::CreatePointerBitCastOrAddrSpaceCast(S: C, Ty: DestTy); |
126 | } |
127 | |
128 | //===--------------------------------------------------------------------===// |
129 | // Compare Instructions |
130 | //===--------------------------------------------------------------------===// |
131 | |
132 | Instruction *CreateFCmp(CmpInst::Predicate P, |
133 | Constant *LHS, Constant *RHS) const override { |
134 | return new FCmpInst(P, LHS, RHS); |
135 | } |
136 | }; |
137 | |
138 | } // end namespace llvm |
139 | |
140 | #endif // LLVM_IR_NOFOLDER_H |
141 | |