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