ConstantFolder.h revision 413c3bd9fd820a0969cd0e84124f3a3fadc5cc61
1//===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
18#define LLVM_SUPPORT_CONSTANTFOLDER_H
19
20#include "llvm/Constants.h"
21#include "llvm/InstrTypes.h"
22
23namespace llvm {
24
25class LLVMContext;
26
27/// ConstantFolder - Create constants with minimum, target independent, folding.
28class ConstantFolder {
29public:
30  explicit ConstantFolder(LLVMContext &) {}
31
32  //===--------------------------------------------------------------------===//
33  // Binary Operators
34  //===--------------------------------------------------------------------===//
35
36  Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
37    return ConstantExpr::getAdd(LHS, RHS);
38  }
39  Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
40    return ConstantExpr::getNSWAdd(LHS, RHS);
41  }
42  Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
43    return ConstantExpr::getNUWAdd(LHS, RHS);
44  }
45  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
46    return ConstantExpr::getFAdd(LHS, RHS);
47  }
48  Constant *CreateSub(Constant *LHS, Constant *RHS) const {
49    return ConstantExpr::getSub(LHS, RHS);
50  }
51  Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
52    return ConstantExpr::getNSWSub(LHS, RHS);
53  }
54  Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const {
55    return ConstantExpr::getNUWSub(LHS, RHS);
56  }
57  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
58    return ConstantExpr::getFSub(LHS, RHS);
59  }
60  Constant *CreateMul(Constant *LHS, Constant *RHS) const {
61    return ConstantExpr::getMul(LHS, RHS);
62  }
63  Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const {
64    return ConstantExpr::getNSWMul(LHS, RHS);
65  }
66  Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const {
67    return ConstantExpr::getNUWMul(LHS, RHS);
68  }
69  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
70    return ConstantExpr::getFMul(LHS, RHS);
71  }
72  Constant *CreateUDiv(Constant *LHS, Constant *RHS,
73                       bool isExact = false) const {
74    return ConstantExpr::getUDiv(LHS, RHS, isExact);
75  }
76  Constant *CreateSDiv(Constant *LHS, Constant *RHS,
77                       bool isExact = false) const {
78    return ConstantExpr::getSDiv(LHS, RHS, isExact);
79  }
80  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
81    return ConstantExpr::getFDiv(LHS, RHS);
82  }
83  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
84    return ConstantExpr::getURem(LHS, RHS);
85  }
86  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
87    return ConstantExpr::getSRem(LHS, RHS);
88  }
89  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
90    return ConstantExpr::getFRem(LHS, RHS);
91  }
92  Constant *CreateShl(Constant *LHS, Constant *RHS) const {
93    return ConstantExpr::getShl(LHS, RHS);
94  }
95  Constant *CreateLShr(Constant *LHS, Constant *RHS,
96                       bool isExact = false) const {
97    return ConstantExpr::getLShr(LHS, RHS, isExact);
98  }
99  Constant *CreateAShr(Constant *LHS, Constant *RHS,
100                       bool isExact = false) const {
101    return ConstantExpr::getAShr(LHS, RHS, isExact);
102  }
103  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
104    return ConstantExpr::getAnd(LHS, RHS);
105  }
106  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
107    return ConstantExpr::getOr(LHS, RHS);
108  }
109  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
110    return ConstantExpr::getXor(LHS, RHS);
111  }
112
113  Constant *CreateBinOp(Instruction::BinaryOps Opc,
114                        Constant *LHS, Constant *RHS) const {
115    return ConstantExpr::get(Opc, LHS, RHS);
116  }
117
118  //===--------------------------------------------------------------------===//
119  // Unary Operators
120  //===--------------------------------------------------------------------===//
121
122  Constant *CreateNeg(Constant *C) const {
123    return ConstantExpr::getNeg(C);
124  }
125  Constant *CreateNSWNeg(Constant *C) const {
126    return ConstantExpr::getNSWNeg(C);
127  }
128  Constant *CreateNUWNeg(Constant *C) const {
129    return ConstantExpr::getNUWNeg(C);
130  }
131  Constant *CreateFNeg(Constant *C) const {
132    return ConstantExpr::getFNeg(C);
133  }
134  Constant *CreateNot(Constant *C) const {
135    return ConstantExpr::getNot(C);
136  }
137
138  //===--------------------------------------------------------------------===//
139  // Memory Instructions
140  //===--------------------------------------------------------------------===//
141
142  Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
143                                unsigned NumIdx) const {
144    return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
145  }
146  Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
147                                unsigned NumIdx) const {
148    return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
149  }
150
151  Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
152                                        unsigned NumIdx) const {
153    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
154  }
155  Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
156                                        unsigned NumIdx) const {
157    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
158  }
159
160  //===--------------------------------------------------------------------===//
161  // Cast/Conversion Operators
162  //===--------------------------------------------------------------------===//
163
164  Constant *CreateCast(Instruction::CastOps Op, Constant *C,
165                       const Type *DestTy) const {
166    return ConstantExpr::getCast(Op, C, DestTy);
167  }
168  Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
169    return ConstantExpr::getPointerCast(C, DestTy);
170  }
171  Constant *CreateIntCast(Constant *C, const Type *DestTy,
172                          bool isSigned) const {
173    return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
174  }
175  Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
176    return ConstantExpr::getFPCast(C, DestTy);
177  }
178
179  Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
180    return CreateCast(Instruction::BitCast, C, DestTy);
181  }
182  Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
183    return CreateCast(Instruction::IntToPtr, C, DestTy);
184  }
185  Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
186    return CreateCast(Instruction::PtrToInt, C, DestTy);
187  }
188  Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
189    return ConstantExpr::getZExtOrBitCast(C, DestTy);
190  }
191  Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
192    return ConstantExpr::getSExtOrBitCast(C, DestTy);
193  }
194
195  Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
196    return ConstantExpr::getTruncOrBitCast(C, DestTy);
197  }
198
199  //===--------------------------------------------------------------------===//
200  // Compare Instructions
201  //===--------------------------------------------------------------------===//
202
203  Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
204                       Constant *RHS) const {
205    return ConstantExpr::getCompare(P, LHS, RHS);
206  }
207  Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
208                       Constant *RHS) const {
209    return ConstantExpr::getCompare(P, LHS, RHS);
210  }
211
212  //===--------------------------------------------------------------------===//
213  // Other Instructions
214  //===--------------------------------------------------------------------===//
215
216  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
217    return ConstantExpr::getSelect(C, True, False);
218  }
219
220  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
221    return ConstantExpr::getExtractElement(Vec, Idx);
222  }
223
224  Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
225                                Constant *Idx) const {
226    return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
227  }
228
229  Constant *CreateShuffleVector(Constant *V1, Constant *V2,
230                                Constant *Mask) const {
231    return ConstantExpr::getShuffleVector(V1, V2, Mask);
232  }
233
234  Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
235                               unsigned NumIdx) const {
236    return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
237  }
238
239  Constant *CreateInsertValue(Constant *Agg, Constant *Val,
240                              const unsigned *IdxList, unsigned NumIdx) const {
241    return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
242  }
243};
244
245}
246
247#endif
248