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/IR/Constants.h"
21 #include "llvm/IR/InstrTypes.h"
25 /// ConstantFolder - Create constants with minimum, target independent, folding.
26 class ConstantFolder {
28 explicit ConstantFolder() {}
30 //===--------------------------------------------------------------------===//
32 //===--------------------------------------------------------------------===//
34 Constant *CreateAdd(Constant *LHS, Constant *RHS,
35 bool HasNUW = false, bool HasNSW = false) const {
36 return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
38 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
39 return ConstantExpr::getFAdd(LHS, RHS);
41 Constant *CreateSub(Constant *LHS, Constant *RHS,
42 bool HasNUW = false, bool HasNSW = false) const {
43 return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
45 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
46 return ConstantExpr::getFSub(LHS, RHS);
48 Constant *CreateMul(Constant *LHS, Constant *RHS,
49 bool HasNUW = false, bool HasNSW = false) const {
50 return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
52 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
53 return ConstantExpr::getFMul(LHS, RHS);
55 Constant *CreateUDiv(Constant *LHS, Constant *RHS,
56 bool isExact = false) const {
57 return ConstantExpr::getUDiv(LHS, RHS, isExact);
59 Constant *CreateSDiv(Constant *LHS, Constant *RHS,
60 bool isExact = false) const {
61 return ConstantExpr::getSDiv(LHS, RHS, isExact);
63 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
64 return ConstantExpr::getFDiv(LHS, RHS);
66 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
67 return ConstantExpr::getURem(LHS, RHS);
69 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
70 return ConstantExpr::getSRem(LHS, RHS);
72 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
73 return ConstantExpr::getFRem(LHS, RHS);
75 Constant *CreateShl(Constant *LHS, Constant *RHS,
76 bool HasNUW = false, bool HasNSW = false) const {
77 return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
79 Constant *CreateLShr(Constant *LHS, Constant *RHS,
80 bool isExact = false) const {
81 return ConstantExpr::getLShr(LHS, RHS, isExact);
83 Constant *CreateAShr(Constant *LHS, Constant *RHS,
84 bool isExact = false) const {
85 return ConstantExpr::getAShr(LHS, RHS, isExact);
87 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
88 return ConstantExpr::getAnd(LHS, RHS);
90 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
91 return ConstantExpr::getOr(LHS, RHS);
93 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
94 return ConstantExpr::getXor(LHS, RHS);
97 Constant *CreateBinOp(Instruction::BinaryOps Opc,
98 Constant *LHS, Constant *RHS) const {
99 return ConstantExpr::get(Opc, LHS, RHS);
102 //===--------------------------------------------------------------------===//
104 //===--------------------------------------------------------------------===//
106 Constant *CreateNeg(Constant *C,
107 bool HasNUW = false, bool HasNSW = false) const {
108 return ConstantExpr::getNeg(C, HasNUW, HasNSW);
110 Constant *CreateFNeg(Constant *C) const {
111 return ConstantExpr::getFNeg(C);
113 Constant *CreateNot(Constant *C) const {
114 return ConstantExpr::getNot(C);
117 //===--------------------------------------------------------------------===//
118 // Memory Instructions
119 //===--------------------------------------------------------------------===//
121 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
122 ArrayRef<Constant *> IdxList) const {
123 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
125 Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
126 // This form of the function only exists to avoid ambiguous overload
127 // warnings about whether to convert Idx to ArrayRef<Constant *> or
128 // ArrayRef<Value *>.
129 return ConstantExpr::getGetElementPtr(Ty, C, Idx);
131 Constant *CreateGetElementPtr(Type *Ty, Constant *C,
132 ArrayRef<Value *> IdxList) const {
133 return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
136 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
137 ArrayRef<Constant *> IdxList) const {
138 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
140 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
141 Constant *Idx) const {
142 // This form of the function only exists to avoid ambiguous overload
143 // warnings about whether to convert Idx to ArrayRef<Constant *> or
144 // ArrayRef<Value *>.
145 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
147 Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
148 ArrayRef<Value *> IdxList) const {
149 return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
152 //===--------------------------------------------------------------------===//
153 // Cast/Conversion Operators
154 //===--------------------------------------------------------------------===//
156 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
157 Type *DestTy) const {
158 return ConstantExpr::getCast(Op, C, DestTy);
160 Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
161 return ConstantExpr::getPointerCast(C, DestTy);
164 Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
165 Type *DestTy) const {
166 return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
169 Constant *CreateIntCast(Constant *C, Type *DestTy,
170 bool isSigned) const {
171 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
173 Constant *CreateFPCast(Constant *C, Type *DestTy) const {
174 return ConstantExpr::getFPCast(C, DestTy);
177 Constant *CreateBitCast(Constant *C, Type *DestTy) const {
178 return CreateCast(Instruction::BitCast, C, DestTy);
180 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
181 return CreateCast(Instruction::IntToPtr, C, DestTy);
183 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
184 return CreateCast(Instruction::PtrToInt, C, DestTy);
186 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
187 return ConstantExpr::getZExtOrBitCast(C, DestTy);
189 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
190 return ConstantExpr::getSExtOrBitCast(C, DestTy);
193 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
194 return ConstantExpr::getTruncOrBitCast(C, DestTy);
197 //===--------------------------------------------------------------------===//
198 // Compare Instructions
199 //===--------------------------------------------------------------------===//
201 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
202 Constant *RHS) const {
203 return ConstantExpr::getCompare(P, LHS, RHS);
205 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
206 Constant *RHS) const {
207 return ConstantExpr::getCompare(P, LHS, RHS);
210 //===--------------------------------------------------------------------===//
211 // Other Instructions
212 //===--------------------------------------------------------------------===//
214 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
215 return ConstantExpr::getSelect(C, True, False);
218 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
219 return ConstantExpr::getExtractElement(Vec, Idx);
222 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
223 Constant *Idx) const {
224 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
227 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
228 Constant *Mask) const {
229 return ConstantExpr::getShuffleVector(V1, V2, Mask);
232 Constant *CreateExtractValue(Constant *Agg,
233 ArrayRef<unsigned> IdxList) const {
234 return ConstantExpr::getExtractValue(Agg, IdxList);
237 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
238 ArrayRef<unsigned> IdxList) const {
239 return ConstantExpr::getInsertValue(Agg, Val, IdxList);