1 //===-- llvm/Support/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_SUPPORT_CONSTANTFOLDER_H
18 #define LLVM_SUPPORT_CONSTANTFOLDER_H
20 #include "llvm/Constants.h"
21 #include "llvm/InstrTypes.h"
27 /// ConstantFolder - Create constants with minimum, target independent, folding.
28 class ConstantFolder {
30 explicit ConstantFolder(LLVMContext &) {}
32 //===--------------------------------------------------------------------===//
34 //===--------------------------------------------------------------------===//
36 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
37 return ConstantExpr::getAdd(LHS, RHS);
39 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
40 return ConstantExpr::getNSWAdd(LHS, RHS);
42 Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
43 return ConstantExpr::getNUWAdd(LHS, RHS);
45 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
46 return ConstantExpr::getFAdd(LHS, RHS);
48 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
49 return ConstantExpr::getSub(LHS, RHS);
51 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
52 return ConstantExpr::getNSWSub(LHS, RHS);
54 Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const {
55 return ConstantExpr::getNUWSub(LHS, RHS);
57 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
58 return ConstantExpr::getFSub(LHS, RHS);
60 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
61 return ConstantExpr::getMul(LHS, RHS);
63 Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const {
64 return ConstantExpr::getNSWMul(LHS, RHS);
66 Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const {
67 return ConstantExpr::getNUWMul(LHS, RHS);
69 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
70 return ConstantExpr::getFMul(LHS, RHS);
72 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
73 return ConstantExpr::getUDiv(LHS, RHS);
75 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
76 return ConstantExpr::getSDiv(LHS, RHS);
78 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
79 return ConstantExpr::getExactSDiv(LHS, RHS);
81 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
82 return ConstantExpr::getFDiv(LHS, RHS);
84 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
85 return ConstantExpr::getURem(LHS, RHS);
87 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
88 return ConstantExpr::getSRem(LHS, RHS);
90 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
91 return ConstantExpr::getFRem(LHS, RHS);
93 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
94 return ConstantExpr::getShl(LHS, RHS);
96 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
97 return ConstantExpr::getLShr(LHS, RHS);
99 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
100 return ConstantExpr::getAShr(LHS, RHS);
102 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
103 return ConstantExpr::getAnd(LHS, RHS);
105 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
106 return ConstantExpr::getOr(LHS, RHS);
108 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
109 return ConstantExpr::getXor(LHS, RHS);
112 Constant *CreateBinOp(Instruction::BinaryOps Opc,
113 Constant *LHS, Constant *RHS) const {
114 return ConstantExpr::get(Opc, LHS, RHS);
117 //===--------------------------------------------------------------------===//
119 //===--------------------------------------------------------------------===//
121 Constant *CreateNeg(Constant *C) const {
122 return ConstantExpr::getNeg(C);
124 Constant *CreateNSWNeg(Constant *C) const {
125 return ConstantExpr::getNSWNeg(C);
127 Constant *CreateNUWNeg(Constant *C) const {
128 return ConstantExpr::getNUWNeg(C);
130 Constant *CreateFNeg(Constant *C) const {
131 return ConstantExpr::getFNeg(C);
133 Constant *CreateNot(Constant *C) const {
134 return ConstantExpr::getNot(C);
137 //===--------------------------------------------------------------------===//
138 // Memory Instructions
139 //===--------------------------------------------------------------------===//
141 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
142 unsigned NumIdx) const {
143 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
145 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
146 unsigned NumIdx) const {
147 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
150 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
151 unsigned NumIdx) const {
152 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
154 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
155 unsigned NumIdx) const {
156 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
159 //===--------------------------------------------------------------------===//
160 // Cast/Conversion Operators
161 //===--------------------------------------------------------------------===//
163 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
164 const Type *DestTy) const {
165 return ConstantExpr::getCast(Op, C, DestTy);
167 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
168 return ConstantExpr::getPointerCast(C, DestTy);
170 Constant *CreateIntCast(Constant *C, const Type *DestTy,
171 bool isSigned) const {
172 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
174 Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
175 return ConstantExpr::getFPCast(C, DestTy);
178 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
179 return CreateCast(Instruction::BitCast, C, DestTy);
181 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
182 return CreateCast(Instruction::IntToPtr, C, DestTy);
184 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
185 return CreateCast(Instruction::PtrToInt, C, DestTy);
187 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
188 return ConstantExpr::getZExtOrBitCast(C, DestTy);
190 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
191 return ConstantExpr::getSExtOrBitCast(C, DestTy);
194 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
195 return ConstantExpr::getTruncOrBitCast(C, DestTy);
198 //===--------------------------------------------------------------------===//
199 // Compare Instructions
200 //===--------------------------------------------------------------------===//
202 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
203 Constant *RHS) const {
204 return ConstantExpr::getCompare(P, LHS, RHS);
206 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
207 Constant *RHS) const {
208 return ConstantExpr::getCompare(P, LHS, RHS);
211 //===--------------------------------------------------------------------===//
212 // Other Instructions
213 //===--------------------------------------------------------------------===//
215 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
216 return ConstantExpr::getSelect(C, True, False);
219 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
220 return ConstantExpr::getExtractElement(Vec, Idx);
223 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
224 Constant *Idx) const {
225 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
228 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
229 Constant *Mask) const {
230 return ConstantExpr::getShuffleVector(V1, V2, Mask);
233 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
234 unsigned NumIdx) const {
235 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
238 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
239 const unsigned *IdxList, unsigned NumIdx) const {
240 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);