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 values rather than constants.  The values do not have names,
19 // even if names were provided to IRBuilder, which may be confusing.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_SUPPORT_NOFOLDER_H
24 #define LLVM_SUPPORT_NOFOLDER_H
25 
26 #include "llvm/Constants.h"
27 #include "llvm/Instructions.h"
28 
29 namespace llvm {
30 
31 class LLVMContext;
32 
33 /// NoFolder - Create "constants" (actually, values) with no folding.
34 class NoFolder {
35 public:
NoFolder(LLVMContext &)36   explicit NoFolder(LLVMContext &) {}
37 
38   //===--------------------------------------------------------------------===//
39   // Binary Operators
40   //===--------------------------------------------------------------------===//
41 
CreateAdd(Constant * LHS,Constant * RHS)42   Value *CreateAdd(Constant *LHS, Constant *RHS) const {
43     return BinaryOperator::CreateAdd(LHS, RHS);
44   }
CreateNSWAdd(Constant * LHS,Constant * RHS)45   Value *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
46     return BinaryOperator::CreateNSWAdd(LHS, RHS);
47   }
CreateNUWAdd(Constant * LHS,Constant * RHS)48   Value *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
49     return BinaryOperator::CreateNUWAdd(LHS, RHS);
50   }
CreateFAdd(Constant * LHS,Constant * RHS)51   Value *CreateFAdd(Constant *LHS, Constant *RHS) const {
52     return BinaryOperator::CreateFAdd(LHS, RHS);
53   }
CreateSub(Constant * LHS,Constant * RHS)54   Value *CreateSub(Constant *LHS, Constant *RHS) const {
55     return BinaryOperator::CreateSub(LHS, RHS);
56   }
CreateNSWSub(Constant * LHS,Constant * RHS)57   Value *CreateNSWSub(Constant *LHS, Constant *RHS) const {
58     return BinaryOperator::CreateNSWSub(LHS, RHS);
59   }
CreateNUWSub(Constant * LHS,Constant * RHS)60   Value *CreateNUWSub(Constant *LHS, Constant *RHS) const {
61     return BinaryOperator::CreateNUWSub(LHS, RHS);
62   }
CreateFSub(Constant * LHS,Constant * RHS)63   Value *CreateFSub(Constant *LHS, Constant *RHS) const {
64     return BinaryOperator::CreateFSub(LHS, RHS);
65   }
CreateMul(Constant * LHS,Constant * RHS)66   Value *CreateMul(Constant *LHS, Constant *RHS) const {
67     return BinaryOperator::CreateMul(LHS, RHS);
68   }
CreateNSWMul(Constant * LHS,Constant * RHS)69   Value *CreateNSWMul(Constant *LHS, Constant *RHS) const {
70     return BinaryOperator::CreateNSWMul(LHS, RHS);
71   }
CreateNUWMul(Constant * LHS,Constant * RHS)72   Value *CreateNUWMul(Constant *LHS, Constant *RHS) const {
73     return BinaryOperator::CreateNUWMul(LHS, RHS);
74   }
CreateFMul(Constant * LHS,Constant * RHS)75   Value *CreateFMul(Constant *LHS, Constant *RHS) const {
76     return BinaryOperator::CreateFMul(LHS, RHS);
77   }
CreateUDiv(Constant * LHS,Constant * RHS)78   Value *CreateUDiv(Constant *LHS, Constant *RHS) const {
79     return BinaryOperator::CreateUDiv(LHS, RHS);
80   }
CreateSDiv(Constant * LHS,Constant * RHS)81   Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
82     return BinaryOperator::CreateSDiv(LHS, RHS);
83   }
CreateExactSDiv(Constant * LHS,Constant * RHS)84   Value *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
85     return BinaryOperator::CreateExactSDiv(LHS, RHS);
86   }
CreateFDiv(Constant * LHS,Constant * RHS)87   Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
88     return BinaryOperator::CreateFDiv(LHS, RHS);
89   }
CreateURem(Constant * LHS,Constant * RHS)90   Value *CreateURem(Constant *LHS, Constant *RHS) const {
91     return BinaryOperator::CreateURem(LHS, RHS);
92   }
CreateSRem(Constant * LHS,Constant * RHS)93   Value *CreateSRem(Constant *LHS, Constant *RHS) const {
94     return BinaryOperator::CreateSRem(LHS, RHS);
95   }
CreateFRem(Constant * LHS,Constant * RHS)96   Value *CreateFRem(Constant *LHS, Constant *RHS) const {
97     return BinaryOperator::CreateFRem(LHS, RHS);
98   }
CreateShl(Constant * LHS,Constant * RHS)99   Value *CreateShl(Constant *LHS, Constant *RHS) const {
100     return BinaryOperator::CreateShl(LHS, RHS);
101   }
CreateLShr(Constant * LHS,Constant * RHS)102   Value *CreateLShr(Constant *LHS, Constant *RHS) const {
103     return BinaryOperator::CreateLShr(LHS, RHS);
104   }
CreateAShr(Constant * LHS,Constant * RHS)105   Value *CreateAShr(Constant *LHS, Constant *RHS) const {
106     return BinaryOperator::CreateAShr(LHS, RHS);
107   }
CreateAnd(Constant * LHS,Constant * RHS)108   Value *CreateAnd(Constant *LHS, Constant *RHS) const {
109     return BinaryOperator::CreateAnd(LHS, RHS);
110   }
CreateOr(Constant * LHS,Constant * RHS)111   Value *CreateOr(Constant *LHS, Constant *RHS) const {
112     return BinaryOperator::CreateOr(LHS, RHS);
113   }
CreateXor(Constant * LHS,Constant * RHS)114   Value *CreateXor(Constant *LHS, Constant *RHS) const {
115     return BinaryOperator::CreateXor(LHS, RHS);
116   }
117 
CreateBinOp(Instruction::BinaryOps Opc,Constant * LHS,Constant * RHS)118   Value *CreateBinOp(Instruction::BinaryOps Opc,
119                      Constant *LHS, Constant *RHS) const {
120     return BinaryOperator::Create(Opc, LHS, RHS);
121   }
122 
123   //===--------------------------------------------------------------------===//
124   // Unary Operators
125   //===--------------------------------------------------------------------===//
126 
CreateNeg(Constant * C)127   Value *CreateNeg(Constant *C) const {
128     return BinaryOperator::CreateNeg(C);
129   }
CreateNSWNeg(Constant * C)130   Value *CreateNSWNeg(Constant *C) const {
131     return BinaryOperator::CreateNSWNeg(C);
132   }
CreateNUWNeg(Constant * C)133   Value *CreateNUWNeg(Constant *C) const {
134     return BinaryOperator::CreateNUWNeg(C);
135   }
CreateNot(Constant * C)136   Value *CreateNot(Constant *C) const {
137     return BinaryOperator::CreateNot(C);
138   }
139 
140   //===--------------------------------------------------------------------===//
141   // Memory Instructions
142   //===--------------------------------------------------------------------===//
143 
CreateGetElementPtr(Constant * C,Constant * const * IdxList,unsigned NumIdx)144   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
145                                 unsigned NumIdx) const {
146     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
147   }
CreateGetElementPtr(Constant * C,Value * const * IdxList,unsigned NumIdx)148   Value *CreateGetElementPtr(Constant *C, Value* const *IdxList,
149                              unsigned NumIdx) const {
150     return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
151   }
152 
CreateInBoundsGetElementPtr(Constant * C,Constant * const * IdxList,unsigned NumIdx)153   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
154                                         unsigned NumIdx) const {
155     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
156   }
CreateInBoundsGetElementPtr(Constant * C,Value * const * IdxList,unsigned NumIdx)157   Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
158                                      unsigned NumIdx) const {
159     return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
160   }
161 
162   //===--------------------------------------------------------------------===//
163   // Cast/Conversion Operators
164   //===--------------------------------------------------------------------===//
165 
CreateCast(Instruction::CastOps Op,Constant * C,const Type * DestTy)166   Value *CreateCast(Instruction::CastOps Op, Constant *C,
167                     const Type *DestTy) const {
168     return CastInst::Create(Op, C, DestTy);
169   }
CreateIntCast(Constant * C,const Type * DestTy,bool isSigned)170   Value *CreateIntCast(Constant *C, const Type *DestTy,
171                        bool isSigned) const {
172     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
173   }
174 
175   //===--------------------------------------------------------------------===//
176   // Compare Instructions
177   //===--------------------------------------------------------------------===//
178 
CreateICmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)179   Value *CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
180     return new ICmpInst(P, LHS, RHS);
181   }
CreateFCmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)182   Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
183     return new FCmpInst(P, LHS, RHS);
184   }
185 
186   //===--------------------------------------------------------------------===//
187   // Other Instructions
188   //===--------------------------------------------------------------------===//
189 
CreateSelect(Constant * C,Constant * True,Constant * False)190   Value *CreateSelect(Constant *C, Constant *True, Constant *False) const {
191     return SelectInst::Create(C, True, False);
192   }
193 
CreateExtractElement(Constant * Vec,Constant * Idx)194   Value *CreateExtractElement(Constant *Vec, Constant *Idx) const {
195     return ExtractElementInst::Create(Vec, Idx);
196   }
197 
CreateInsertElement(Constant * Vec,Constant * NewElt,Constant * Idx)198   Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
199                              Constant *Idx) const {
200     return InsertElementInst::Create(Vec, NewElt, Idx);
201   }
202 
CreateShuffleVector(Constant * V1,Constant * V2,Constant * Mask)203   Value *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
204     return new ShuffleVectorInst(V1, V2, Mask);
205   }
206 
CreateExtractValue(Constant * Agg,const unsigned * IdxList,unsigned NumIdx)207   Value *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
208                             unsigned NumIdx) const {
209     return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
210   }
211 
CreateInsertValue(Constant * Agg,Constant * Val,const unsigned * IdxList,unsigned NumIdx)212   Value *CreateInsertValue(Constant *Agg, Constant *Val,
213                            const unsigned *IdxList, unsigned NumIdx) const {
214     return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
215   }
216 };
217 
218 }
219 
220 #endif
221