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