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