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