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