another typo
[oota-llvm.git] / include / llvm / Support / NoFolder.h
1 //======-- llvm/Support/NoFolder.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 NoFolder class, a helper for IRBuilder.  It provides
11 // IRBuilder with a set of methods for creating unfolded constants.  This is
12 // useful for learners trying to understand how LLVM IR works, and who don't
13 // want details to be hidden by the constant folder.  For general constant
14 // creation and folding, use ConstantExpr and the routines in
15 // llvm/Analysis/ConstantFolding.h.
16 //
17 // Note: since it is not actually possible to create unfolded constants, this
18 // class returns instructions rather than constants.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_NOFOLDER_H
23 #define LLVM_SUPPORT_NOFOLDER_H
24
25 #include "llvm/Constants.h"
26 #include "llvm/Instructions.h"
27
28 namespace llvm {
29
30 /// NoFolder - Create "constants" (actually, instructions) with no folding.
31 class NoFolder {
32 public:
33   explicit NoFolder() {}
34
35   //===--------------------------------------------------------------------===//
36   // Binary Operators
37   //===--------------------------------------------------------------------===//
38
39   Instruction *CreateAdd(Constant *LHS, Constant *RHS,
40                          bool HasNUW = false, bool HasNSW = false) const {
41     BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
42     if (HasNUW) BO->setHasNoUnsignedWrap();
43     if (HasNSW) BO->setHasNoSignedWrap();
44     return BO;
45   }
46   Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
47     return BinaryOperator::CreateNSWAdd(LHS, RHS);
48   }
49   Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
50     return BinaryOperator::CreateNUWAdd(LHS, RHS);
51   }
52   Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
53     return BinaryOperator::CreateFAdd(LHS, RHS);
54   }
55   Instruction *CreateSub(Constant *LHS, Constant *RHS,
56                          bool HasNUW = false, bool HasNSW = false) const {
57     BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
58     if (HasNUW) BO->setHasNoUnsignedWrap();
59     if (HasNSW) BO->setHasNoSignedWrap();
60     return BO;
61   }
62   Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
63     return BinaryOperator::CreateNSWSub(LHS, RHS);
64   }
65   Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
66     return BinaryOperator::CreateNUWSub(LHS, RHS);
67   }
68   Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
69     return BinaryOperator::CreateFSub(LHS, RHS);
70   }
71   Instruction *CreateMul(Constant *LHS, Constant *RHS,
72                          bool HasNUW = false, bool HasNSW = false) const {
73     BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
74     if (HasNUW) BO->setHasNoUnsignedWrap();
75     if (HasNSW) BO->setHasNoSignedWrap();
76     return BO;
77   }
78   Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
79     return BinaryOperator::CreateNSWMul(LHS, RHS);
80   }
81   Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
82     return BinaryOperator::CreateNUWMul(LHS, RHS);
83   }
84   Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
85     return BinaryOperator::CreateFMul(LHS, RHS);
86   }
87   Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
88                           bool isExact = false) const {
89     if (!isExact)
90       return BinaryOperator::CreateUDiv(LHS, RHS);
91     return BinaryOperator::CreateExactUDiv(LHS, RHS);
92   }
93   Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
94     return BinaryOperator::CreateExactUDiv(LHS, RHS);
95   }
96   Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
97                           bool isExact = false) const {
98     if (!isExact)
99       return BinaryOperator::CreateSDiv(LHS, RHS);
100     return BinaryOperator::CreateExactSDiv(LHS, RHS);
101   }
102   Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
103     return BinaryOperator::CreateExactSDiv(LHS, RHS);
104   }
105   Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
106     return BinaryOperator::CreateFDiv(LHS, RHS);
107   }
108   Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
109     return BinaryOperator::CreateURem(LHS, RHS);
110   }
111   Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
112     return BinaryOperator::CreateSRem(LHS, RHS);
113   }
114   Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
115     return BinaryOperator::CreateFRem(LHS, RHS);
116   }
117   Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
118                          bool HasNSW = false) const {
119     BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
120     if (HasNUW) BO->setHasNoUnsignedWrap();
121     if (HasNSW) BO->setHasNoSignedWrap();
122     return BO;
123   }
124   Instruction *CreateLShr(Constant *LHS, Constant *RHS,
125                           bool isExact = false) const {
126     if (!isExact)
127       return BinaryOperator::CreateLShr(LHS, RHS);
128     return BinaryOperator::CreateExactLShr(LHS, RHS);
129   }
130   Instruction *CreateAShr(Constant *LHS, Constant *RHS,
131                           bool isExact = false) const {
132     if (!isExact)
133       return BinaryOperator::CreateAShr(LHS, RHS);
134     return BinaryOperator::CreateExactAShr(LHS, RHS);
135   }
136   Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
137     return BinaryOperator::CreateAnd(LHS, RHS);
138   }
139   Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
140     return BinaryOperator::CreateOr(LHS, RHS);
141   }
142   Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
143     return BinaryOperator::CreateXor(LHS, RHS);
144   }
145
146   Instruction *CreateBinOp(Instruction::BinaryOps Opc,
147                            Constant *LHS, Constant *RHS) const {
148     return BinaryOperator::Create(Opc, LHS, RHS);
149   }
150
151   //===--------------------------------------------------------------------===//
152   // Unary Operators
153   //===--------------------------------------------------------------------===//
154
155   Instruction *CreateNeg(Constant *C,
156                          bool HasNUW = false, bool HasNSW = false) const {
157     BinaryOperator *BO = BinaryOperator::CreateNeg(C);
158     if (HasNUW) BO->setHasNoUnsignedWrap();
159     if (HasNSW) BO->setHasNoSignedWrap();
160     return BO;
161   }
162   Instruction *CreateNSWNeg(Constant *C) const {
163     return BinaryOperator::CreateNSWNeg(C);
164   }
165   Instruction *CreateNUWNeg(Constant *C) const {
166     return BinaryOperator::CreateNUWNeg(C);
167   }
168   Instruction *CreateFNeg(Constant *C) const {
169     return BinaryOperator::CreateFNeg(C);
170   }
171   Instruction *CreateNot(Constant *C) const {
172     return BinaryOperator::CreateNot(C);
173   }
174
175   //===--------------------------------------------------------------------===//
176   // Memory Instructions
177   //===--------------------------------------------------------------------===//
178
179   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
180                                 unsigned NumIdx) const {
181     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
182   }
183   Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
184                                    unsigned NumIdx) const {
185     return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
186   }
187
188   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
189                                         unsigned NumIdx) const {
190     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
191   }
192   Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
193                                            unsigned NumIdx) const {
194     return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
195   }
196
197   //===--------------------------------------------------------------------===//
198   // Cast/Conversion Operators
199   //===--------------------------------------------------------------------===//
200
201   Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
202                     const Type *DestTy) const {
203     return CastInst::Create(Op, C, DestTy);
204   }
205   Instruction *CreatePointerCast(Constant *C, const Type *DestTy) const {
206     return CastInst::CreatePointerCast(C, DestTy);
207   }
208   Instruction *CreateIntCast(Constant *C, const Type *DestTy,
209                        bool isSigned) const {
210     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
211   }
212   Instruction *CreateFPCast(Constant *C, const Type *DestTy) const {
213     return CastInst::CreateFPCast(C, DestTy);
214   }
215
216   Instruction *CreateBitCast(Constant *C, const Type *DestTy) const {
217     return CreateCast(Instruction::BitCast, C, DestTy);
218   }
219   Instruction *CreateIntToPtr(Constant *C, const Type *DestTy) const {
220     return CreateCast(Instruction::IntToPtr, C, DestTy);
221   }
222   Instruction *CreatePtrToInt(Constant *C, const Type *DestTy) const {
223     return CreateCast(Instruction::PtrToInt, C, DestTy);
224   }
225   Instruction *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
226     return CastInst::CreateZExtOrBitCast(C, DestTy);
227   }
228   Instruction *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
229     return CastInst::CreateSExtOrBitCast(C, DestTy);
230   }
231
232   Instruction *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
233     return CastInst::CreateTruncOrBitCast(C, DestTy);
234   }
235
236   //===--------------------------------------------------------------------===//
237   // Compare Instructions
238   //===--------------------------------------------------------------------===//
239
240   Instruction *CreateICmp(CmpInst::Predicate P,
241                           Constant *LHS, Constant *RHS) const {
242     return new ICmpInst(P, LHS, RHS);
243   }
244   Instruction *CreateFCmp(CmpInst::Predicate P,
245                           Constant *LHS, Constant *RHS) const {
246     return new FCmpInst(P, LHS, RHS);
247   }
248
249   //===--------------------------------------------------------------------===//
250   // Other Instructions
251   //===--------------------------------------------------------------------===//
252
253   Instruction *CreateSelect(Constant *C,
254                             Constant *True, Constant *False) const {
255     return SelectInst::Create(C, True, False);
256   }
257
258   Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
259     return ExtractElementInst::Create(Vec, Idx);
260   }
261
262   Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
263                                    Constant *Idx) const {
264     return InsertElementInst::Create(Vec, NewElt, Idx);
265   }
266
267   Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
268                                    Constant *Mask) const {
269     return new ShuffleVectorInst(V1, V2, Mask);
270   }
271
272   Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
273                                   unsigned NumIdx) const {
274     return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
275   }
276
277   Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
278                                  const unsigned *IdxList,
279                                  unsigned NumIdx) const {
280     return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
281   }
282 };
283
284 }
285
286 #endif