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