1 //===- ConstantFolder.h - Constant folding helper ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ConstantFolder class, a helper for IRBuilder.
11 // It provides IRBuilder with a set of methods for creating constants
12 // with minimal folding. For general constant creation and folding,
13 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_CONSTANTFOLDER_H
18 #define LLVM_IR_CONSTANTFOLDER_H
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/IR/Instruction.h"
27 /// ConstantFolder - Create constants with minimum, target independent, folding.
28 class ConstantFolder {
30 explicit ConstantFolder() = default;
32 //===--------------------------------------------------------------------===//
34 //===--------------------------------------------------------------------===//
36 Constant *CreateAdd(Constant *LHS, Constant *RHS,
37 bool HasNUW = false, bool HasNSW = false) const {
38 return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
41 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
42 return ConstantExpr::getFAdd(LHS, RHS);
45 Constant *CreateSub(Constant *LHS, Constant *RHS,
46 bool HasNUW = false, bool HasNSW = false) const {
47 return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
50 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
51 return ConstantExpr::getFSub(LHS, RHS);
54 Constant *CreateMul(Constant *LHS, Constant *RHS,
55 bool HasNUW = false, bool HasNSW = false) const {
56 return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
59 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
60 return ConstantExpr::getFMul(LHS, RHS);
63 Constant *CreateUDiv(Constant *LHS, Constant *RHS,
64 bool isExact = false) const {
65 return ConstantExpr::getUDiv(LHS, RHS, isExact);
68 Constant *CreateSDiv(Constant *LHS, Constant *RHS,
69 bool isExact = false) const {
70 return ConstantExpr::getSDiv(LHS, RHS, isExact);
73 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
74 return ConstantExpr::getFDiv(LHS, RHS);
77 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
78 return ConstantExpr::getURem(LHS, RHS);
81 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
82 return ConstantExpr::getSRem(LHS, RHS);
85 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
86 return ConstantExpr::getFRem(LHS, RHS);
89 Constant *CreateShl(Constant *LHS, Constant *RHS,
90 bool HasNUW = false, bool HasNSW = false) const {
91 return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
94 Constant *CreateLShr(Constant *LHS, Constant *RHS,
95 bool isExact = false) const {
96 return ConstantExpr::getLShr(LHS, RHS, isExact);
99 Constant *CreateAShr(Constant *LHS, Constant *RHS,
100 bool isExact = false) const {
101 return ConstantExpr::getAShr(LHS, RHS, isExact);
104 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
105 return ConstantExpr::getAnd(LHS, RHS);
108 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
109 return ConstantExpr::getOr(LHS, RHS);
112 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
113 return ConstantExpr::getXor(LHS, RHS);
116 Constant *CreateBinOp(Instruction::BinaryOps Opc,
117 Constant *LHS, Constant *RHS) const {
118 return ConstantExpr::get(Opc, LHS, RHS);
121 //===--------------------------------------------------------------------===//
123 //===--------------------------------------------------------------------===//
125 Constant *CreateNeg(Constant *C,
126 bool HasNUW = false, bool HasNSW = false) const {
127 return ConstantExpr::getNeg(C, HasNUW, HasNSW);
130 Constant *CreateFNeg(Constant *C) const {
131 return ConstantExpr::getFNeg(C);
134 Constant *CreateNot(Constant *C) const {
135 return ConstantExpr::getNot(C);
138 //===--------------------------------------------------------------------===//
139 // Memory Instructions
140 //===--------------------------------------------------------------------===//
142 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
143 ArrayRef<Constant *> IdxList) const {
144 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
147 Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
148 // This form of the function only exists to avoid ambiguous overload
149 // warnings about whether to convert Idx to ArrayRef<Constant *> or
150 // ArrayRef<Value *>.
151 return ConstantExpr::getGetElementPtr(Ty, C, Idx);
154 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
155 ArrayRef<Value *> IdxList) const {
156 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
159 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
160 ArrayRef<Constant *> IdxList) const {
161 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
164 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
165 Constant *Idx) const {
166 // This form of the function only exists to avoid ambiguous overload
167 // warnings about whether to convert Idx to ArrayRef<Constant *> or
168 // ArrayRef<Value *>.
169 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
172 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
173 ArrayRef<Value *> IdxList) const {
174 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
177 //===--------------------------------------------------------------------===//
178 // Cast/Conversion Operators
179 //===--------------------------------------------------------------------===//
181 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
182 Type *DestTy) const {
183 return ConstantExpr::getCast(Op, C, DestTy);
186 Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
187 return ConstantExpr::getPointerCast(C, DestTy);
190 Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
191 Type *DestTy) const {
192 return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
195 Constant *CreateIntCast(Constant *C, Type *DestTy,
196 bool isSigned) const {
197 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
200 Constant *CreateFPCast(Constant *C, Type *DestTy) const {
201 return ConstantExpr::getFPCast(C, DestTy);
204 Constant *CreateBitCast(Constant *C, Type *DestTy) const {
205 return CreateCast(Instruction::BitCast, C, DestTy);
208 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
209 return CreateCast(Instruction::IntToPtr, C, DestTy);
212 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
213 return CreateCast(Instruction::PtrToInt, C, DestTy);
216 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
217 return ConstantExpr::getZExtOrBitCast(C, DestTy);
220 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
221 return ConstantExpr::getSExtOrBitCast(C, DestTy);
224 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
225 return ConstantExpr::getTruncOrBitCast(C, DestTy);
228 //===--------------------------------------------------------------------===//
229 // Compare Instructions
230 //===--------------------------------------------------------------------===//
232 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
233 Constant *RHS) const {
234 return ConstantExpr::getCompare(P, LHS, RHS);
237 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
238 Constant *RHS) const {
239 return ConstantExpr::getCompare(P, LHS, RHS);
242 //===--------------------------------------------------------------------===//
243 // Other Instructions
244 //===--------------------------------------------------------------------===//
246 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
247 return ConstantExpr::getSelect(C, True, False);
250 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
251 return ConstantExpr::getExtractElement(Vec, Idx);
254 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
255 Constant *Idx) const {
256 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
259 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
260 Constant *Mask) const {
261 return ConstantExpr::getShuffleVector(V1, V2, Mask);
264 Constant *CreateExtractValue(Constant *Agg,
265 ArrayRef<unsigned> IdxList) const {
266 return ConstantExpr::getExtractValue(Agg, IdxList);
269 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
270 ArrayRef<unsigned> IdxList) const {
271 return ConstantExpr::getInsertValue(Agg, Val, IdxList);
275 } // end namespace llvm
277 #endif // LLVM_IR_CONSTANTFOLDER_H