Replace intersectWith with maximalIntersectWith. The latter guarantees that
[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 #include "llvm/LLVMContext.h"
22
23 namespace llvm {
24   
25 /// ConstantFolder - Create constants with minimum, target independent, folding.
26 class ConstantFolder {
27   LLVMContext &Context;
28   
29 public:
30   ConstantFolder(LLVMContext &C) : Context(C) { }
31
32   //===--------------------------------------------------------------------===//
33   // Binary Operators
34   //===--------------------------------------------------------------------===//
35
36   Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
37     return Context.getConstantExprAdd(LHS, RHS);
38   }
39   Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
40     return Context.getConstantExprFAdd(LHS, RHS);
41   }
42   Constant *CreateSub(Constant *LHS, Constant *RHS) const {
43     return Context.getConstantExprSub(LHS, RHS);
44   }
45   Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
46     return Context.getConstantExprFSub(LHS, RHS);
47   }
48   Constant *CreateMul(Constant *LHS, Constant *RHS) const {
49     return Context.getConstantExprMul(LHS, RHS);
50   }
51   Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
52     return Context.getConstantExprFMul(LHS, RHS);
53   }
54   Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
55     return Context.getConstantExprUDiv(LHS, RHS);
56   }
57   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
58     return Context.getConstantExprSDiv(LHS, RHS);
59   }
60   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
61     return Context.getConstantExprFDiv(LHS, RHS);
62   }
63   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
64     return Context.getConstantExprURem(LHS, RHS);
65   }
66   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
67     return Context.getConstantExprSRem(LHS, RHS);
68   }
69   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
70     return Context.getConstantExprFRem(LHS, RHS);
71   }
72   Constant *CreateShl(Constant *LHS, Constant *RHS) const {
73     return Context.getConstantExprShl(LHS, RHS);
74   }
75   Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
76     return Context.getConstantExprLShr(LHS, RHS);
77   }
78   Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
79     return Context.getConstantExprAShr(LHS, RHS);
80   }
81   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
82     return Context.getConstantExprAnd(LHS, RHS);
83   }
84   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
85     return Context.getConstantExprOr(LHS, RHS);
86   }
87   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
88     return Context.getConstantExprXor(LHS, RHS);
89   }
90
91   Constant *CreateBinOp(Instruction::BinaryOps Opc,
92                         Constant *LHS, Constant *RHS) const {
93     return Context.getConstantExpr(Opc, LHS, RHS);
94   }
95
96   //===--------------------------------------------------------------------===//
97   // Unary Operators
98   //===--------------------------------------------------------------------===//
99
100   Constant *CreateNeg(Constant *C) const {
101     return Context.getConstantExprNeg(C);
102   }
103   Constant *CreateFNeg(Constant *C) const {
104     return Context.getConstantExprFNeg(C);
105   }
106   Constant *CreateNot(Constant *C) const {
107     return Context.getConstantExprNot(C);
108   }
109
110   //===--------------------------------------------------------------------===//
111   // Memory Instructions
112   //===--------------------------------------------------------------------===//
113
114   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
115                                 unsigned NumIdx) const {
116     return Context.getConstantExprGetElementPtr(C, IdxList, NumIdx);
117   }
118   Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
119                                 unsigned NumIdx) const {
120     return Context.getConstantExprGetElementPtr(C, IdxList, NumIdx);
121   }
122
123   //===--------------------------------------------------------------------===//
124   // Cast/Conversion Operators
125   //===--------------------------------------------------------------------===//
126
127   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
128                        const Type *DestTy) const {
129     return Context.getConstantExprCast(Op, C, DestTy);
130   }
131   Constant *CreateIntCast(Constant *C, const Type *DestTy,
132                           bool isSigned) const {
133     return Context.getConstantExprIntegerCast(C, DestTy, isSigned);
134   }
135
136   Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
137     return CreateCast(Instruction::BitCast, C, DestTy);
138   }
139   Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
140     return CreateCast(Instruction::IntToPtr, C, DestTy);
141   }
142   Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
143     return CreateCast(Instruction::PtrToInt, C, DestTy);
144   }
145   Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
146     return Context.getConstantExprTruncOrBitCast(C, DestTy);
147   }
148
149   //===--------------------------------------------------------------------===//
150   // Compare Instructions
151   //===--------------------------------------------------------------------===//
152
153   Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
154                        Constant *RHS) const {
155     return Context.getConstantExprCompare(P, LHS, RHS);
156   }
157   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
158                        Constant *RHS) const {
159     return Context.getConstantExprCompare(P, LHS, RHS);
160   }
161
162   //===--------------------------------------------------------------------===//
163   // Other Instructions
164   //===--------------------------------------------------------------------===//
165
166   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
167     return Context.getConstantExprSelect(C, True, False);
168   }
169
170   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
171     return Context.getConstantExprExtractElement(Vec, Idx);
172   }
173
174   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
175                                 Constant *Idx) const {
176     return Context.getConstantExprInsertElement(Vec, NewElt, Idx);
177   }
178
179   Constant *CreateShuffleVector(Constant *V1, Constant *V2,
180                                 Constant *Mask) const {
181     return Context.getConstantExprShuffleVector(V1, V2, Mask);
182   }
183
184   Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
185                                unsigned NumIdx) const {
186     return Context.getConstantExprExtractValue(Agg, IdxList, NumIdx);
187   }
188
189   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
190                               const unsigned *IdxList, unsigned NumIdx) const {
191     return Context.getConstantExprInsertValue(Agg, Val, IdxList, NumIdx);
192   }
193 };
194
195 }
196
197 #endif