Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / IR / ConstantFolder.h
blob1971cb854e340925f046651235b6c5bd6978f80d
1 //===- ConstantFolder.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 ConstantFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants
11 // with minimal folding. For general constant creation and folding,
12 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_CONSTANTFOLDER_H
17 #define LLVM_IR_CONSTANTFOLDER_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/InstrTypes.h"
22 #include "llvm/IR/Instruction.h"
24 namespace llvm {
26 /// ConstantFolder - Create constants with minimum, target independent, folding.
27 class ConstantFolder {
28 public:
29 explicit ConstantFolder() = default;
31 //===--------------------------------------------------------------------===//
32 // Binary Operators
33 //===--------------------------------------------------------------------===//
35 Constant *CreateAdd(Constant *LHS, Constant *RHS,
36 bool HasNUW = false, bool HasNSW = false) const {
37 return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
40 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
41 return ConstantExpr::getFAdd(LHS, RHS);
44 Constant *CreateSub(Constant *LHS, Constant *RHS,
45 bool HasNUW = false, bool HasNSW = false) const {
46 return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
49 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
50 return ConstantExpr::getFSub(LHS, RHS);
53 Constant *CreateMul(Constant *LHS, Constant *RHS,
54 bool HasNUW = false, bool HasNSW = false) const {
55 return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
58 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
59 return ConstantExpr::getFMul(LHS, RHS);
62 Constant *CreateUDiv(Constant *LHS, Constant *RHS,
63 bool isExact = false) const {
64 return ConstantExpr::getUDiv(LHS, RHS, isExact);
67 Constant *CreateSDiv(Constant *LHS, Constant *RHS,
68 bool isExact = false) const {
69 return ConstantExpr::getSDiv(LHS, RHS, isExact);
72 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
73 return ConstantExpr::getFDiv(LHS, RHS);
76 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
77 return ConstantExpr::getURem(LHS, RHS);
80 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
81 return ConstantExpr::getSRem(LHS, RHS);
84 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
85 return ConstantExpr::getFRem(LHS, RHS);
88 Constant *CreateShl(Constant *LHS, Constant *RHS,
89 bool HasNUW = false, bool HasNSW = false) const {
90 return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
93 Constant *CreateLShr(Constant *LHS, Constant *RHS,
94 bool isExact = false) const {
95 return ConstantExpr::getLShr(LHS, RHS, isExact);
98 Constant *CreateAShr(Constant *LHS, Constant *RHS,
99 bool isExact = false) const {
100 return ConstantExpr::getAShr(LHS, RHS, isExact);
103 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
104 return ConstantExpr::getAnd(LHS, RHS);
107 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
108 return ConstantExpr::getOr(LHS, RHS);
111 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
112 return ConstantExpr::getXor(LHS, RHS);
115 Constant *CreateBinOp(Instruction::BinaryOps Opc,
116 Constant *LHS, Constant *RHS) const {
117 return ConstantExpr::get(Opc, LHS, RHS);
120 //===--------------------------------------------------------------------===//
121 // Unary Operators
122 //===--------------------------------------------------------------------===//
124 Constant *CreateNeg(Constant *C,
125 bool HasNUW = false, bool HasNSW = false) const {
126 return ConstantExpr::getNeg(C, HasNUW, HasNSW);
129 Constant *CreateFNeg(Constant *C) const {
130 return ConstantExpr::getFNeg(C);
133 Constant *CreateNot(Constant *C) const {
134 return ConstantExpr::getNot(C);
137 //===--------------------------------------------------------------------===//
138 // Memory Instructions
139 //===--------------------------------------------------------------------===//
141 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
142 ArrayRef<Constant *> IdxList) const {
143 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
146 Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
147 // This form of the function only exists to avoid ambiguous overload
148 // warnings about whether to convert Idx to ArrayRef<Constant *> or
149 // ArrayRef<Value *>.
150 return ConstantExpr::getGetElementPtr(Ty, C, Idx);
153 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
154 ArrayRef<Value *> IdxList) const {
155 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
158 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
159 ArrayRef<Constant *> IdxList) const {
160 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
163 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
164 Constant *Idx) const {
165 // This form of the function only exists to avoid ambiguous overload
166 // warnings about whether to convert Idx to ArrayRef<Constant *> or
167 // ArrayRef<Value *>.
168 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
171 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
172 ArrayRef<Value *> IdxList) const {
173 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
176 //===--------------------------------------------------------------------===//
177 // Cast/Conversion Operators
178 //===--------------------------------------------------------------------===//
180 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
181 Type *DestTy) const {
182 return ConstantExpr::getCast(Op, C, DestTy);
185 Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
186 return ConstantExpr::getPointerCast(C, DestTy);
189 Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
190 Type *DestTy) const {
191 return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
194 Constant *CreateIntCast(Constant *C, Type *DestTy,
195 bool isSigned) const {
196 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
199 Constant *CreateFPCast(Constant *C, Type *DestTy) const {
200 return ConstantExpr::getFPCast(C, DestTy);
203 Constant *CreateBitCast(Constant *C, Type *DestTy) const {
204 return CreateCast(Instruction::BitCast, C, DestTy);
207 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
208 return CreateCast(Instruction::IntToPtr, C, DestTy);
211 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
212 return CreateCast(Instruction::PtrToInt, C, DestTy);
215 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
216 return ConstantExpr::getZExtOrBitCast(C, DestTy);
219 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
220 return ConstantExpr::getSExtOrBitCast(C, DestTy);
223 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
224 return ConstantExpr::getTruncOrBitCast(C, DestTy);
227 //===--------------------------------------------------------------------===//
228 // Compare Instructions
229 //===--------------------------------------------------------------------===//
231 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
232 Constant *RHS) const {
233 return ConstantExpr::getCompare(P, LHS, RHS);
236 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
237 Constant *RHS) const {
238 return ConstantExpr::getCompare(P, LHS, RHS);
241 //===--------------------------------------------------------------------===//
242 // Other Instructions
243 //===--------------------------------------------------------------------===//
245 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
246 return ConstantExpr::getSelect(C, True, False);
249 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
250 return ConstantExpr::getExtractElement(Vec, Idx);
253 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
254 Constant *Idx) const {
255 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
258 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
259 Constant *Mask) const {
260 return ConstantExpr::getShuffleVector(V1, V2, Mask);
263 Constant *CreateExtractValue(Constant *Agg,
264 ArrayRef<unsigned> IdxList) const {
265 return ConstantExpr::getExtractValue(Agg, IdxList);
268 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
269 ArrayRef<unsigned> IdxList) const {
270 return ConstantExpr::getInsertValue(Agg, Val, IdxList);
274 } // end namespace llvm
276 #endif // LLVM_IR_CONSTANTFOLDER_H