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