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