ConstantFolder.h revision 1dbf0df996bba398a70abccc714b1a9652330014
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) const {
73    return ConstantExpr::getUDiv(LHS, RHS);
74  }
75  Constant *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
76    return ConstantExpr::getExactUDiv(LHS, RHS);
77  }
78  Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
79    return ConstantExpr::getSDiv(LHS, RHS);
80  }
81  Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
82    return ConstantExpr::getExactSDiv(LHS, RHS);
83  }
84  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
85    return ConstantExpr::getFDiv(LHS, RHS);
86  }
87  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
88    return ConstantExpr::getURem(LHS, RHS);
89  }
90  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
91    return ConstantExpr::getSRem(LHS, RHS);
92  }
93  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
94    return ConstantExpr::getFRem(LHS, RHS);
95  }
96  Constant *CreateShl(Constant *LHS, Constant *RHS) const {
97    return ConstantExpr::getShl(LHS, RHS);
98  }
99  Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
100    return ConstantExpr::getLShr(LHS, RHS);
101  }
102  Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
103    return ConstantExpr::getAShr(LHS, RHS);
104  }
105  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
106    return ConstantExpr::getAnd(LHS, RHS);
107  }
108  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
109    return ConstantExpr::getOr(LHS, RHS);
110  }
111  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
112    return ConstantExpr::getXor(LHS, RHS);
113  }
114
115  Constant *CreateBinOp(Instruction::BinaryOps Opc,
116                        Constant *LHS, Constant *RHS) const {
117    return ConstantExpr::get(Opc, LHS, RHS);
118  }
119
120  //===--------------------------------------------------------------------===//
121  // Unary Operators
122  //===--------------------------------------------------------------------===//
123
124  Constant *CreateNeg(Constant *C) const {
125    return ConstantExpr::getNeg(C);
126  }
127  Constant *CreateNSWNeg(Constant *C) const {
128    return ConstantExpr::getNSWNeg(C);
129  }
130  Constant *CreateNUWNeg(Constant *C) const {
131    return ConstantExpr::getNUWNeg(C);
132  }
133  Constant *CreateFNeg(Constant *C) const {
134    return ConstantExpr::getFNeg(C);
135  }
136  Constant *CreateNot(Constant *C) const {
137    return ConstantExpr::getNot(C);
138  }
139
140  //===--------------------------------------------------------------------===//
141  // Memory Instructions
142  //===--------------------------------------------------------------------===//
143
144  Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
145                                unsigned NumIdx) const {
146    return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
147  }
148  Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
149                                unsigned NumIdx) const {
150    return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
151  }
152
153  Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
154                                        unsigned NumIdx) const {
155    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
156  }
157  Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
158                                        unsigned NumIdx) const {
159    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
160  }
161
162  //===--------------------------------------------------------------------===//
163  // Cast/Conversion Operators
164  //===--------------------------------------------------------------------===//
165
166  Constant *CreateCast(Instruction::CastOps Op, Constant *C,
167                       const Type *DestTy) const {
168    return ConstantExpr::getCast(Op, C, DestTy);
169  }
170  Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
171    return ConstantExpr::getPointerCast(C, DestTy);
172  }
173  Constant *CreateIntCast(Constant *C, const Type *DestTy,
174                          bool isSigned) const {
175    return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
176  }
177  Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
178    return ConstantExpr::getFPCast(C, DestTy);
179  }
180
181  Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
182    return CreateCast(Instruction::BitCast, C, DestTy);
183  }
184  Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
185    return CreateCast(Instruction::IntToPtr, C, DestTy);
186  }
187  Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
188    return CreateCast(Instruction::PtrToInt, C, DestTy);
189  }
190  Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
191    return ConstantExpr::getZExtOrBitCast(C, DestTy);
192  }
193  Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
194    return ConstantExpr::getSExtOrBitCast(C, DestTy);
195  }
196
197  Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
198    return ConstantExpr::getTruncOrBitCast(C, DestTy);
199  }
200
201  //===--------------------------------------------------------------------===//
202  // Compare Instructions
203  //===--------------------------------------------------------------------===//
204
205  Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
206                       Constant *RHS) const {
207    return ConstantExpr::getCompare(P, LHS, RHS);
208  }
209  Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
210                       Constant *RHS) const {
211    return ConstantExpr::getCompare(P, LHS, RHS);
212  }
213
214  //===--------------------------------------------------------------------===//
215  // Other Instructions
216  //===--------------------------------------------------------------------===//
217
218  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
219    return ConstantExpr::getSelect(C, True, False);
220  }
221
222  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
223    return ConstantExpr::getExtractElement(Vec, Idx);
224  }
225
226  Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
227                                Constant *Idx) const {
228    return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
229  }
230
231  Constant *CreateShuffleVector(Constant *V1, Constant *V2,
232                                Constant *Mask) const {
233    return ConstantExpr::getShuffleVector(V1, V2, Mask);
234  }
235
236  Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
237                               unsigned NumIdx) const {
238    return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
239  }
240
241  Constant *CreateInsertValue(Constant *Agg, Constant *Val,
242                              const unsigned *IdxList, unsigned NumIdx) const {
243    return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
244  }
245};
246
247}
248
249#endif
250