Make it possible to use different constant
[oota-llvm.git] / include / llvm / Support / TargetFolder.h
1 //====-- llvm/Support/TargetFolder.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 TargetFolder class, which provides a set of methods
11 // for creating constants, with target dependent folding.
12 //
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_TARGETFOLDER_H
17 #define LLVM_SUPPORT_TARGETFOLDER_H
18
19 #include "llvm/Constants.h"
20 #include "llvm/Analysis/ConstantFolding.h"
21
22 namespace llvm {
23
24 class TargetData;
25
26 /// TargetFolder - Create constants with target dependent folding.
27 class TargetFolder {
28   const TargetData &TD;
29
30   /// Fold - Fold the constant using target specific information.
31   Constant *Fold(Constant *C) const {
32     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
33       if (Constant *CF = ConstantFoldConstantExpression(CE, &TD))
34         return CF;
35     return C;
36   }
37
38 public:
39   TargetFolder(const TargetData &TheTD) : TD(TheTD) {}
40
41   //===--------------------------------------------------------------------===//
42   // Binary Operators
43   //===--------------------------------------------------------------------===//
44
45   Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
46     return Fold(ConstantExpr::getAdd(LHS, RHS));
47   }
48   Constant *CreateSub(Constant *LHS, Constant *RHS) const {
49     return Fold(ConstantExpr::getSub(LHS, RHS));
50   }
51   Constant *CreateMul(Constant *LHS, Constant *RHS) const {
52     return Fold(ConstantExpr::getMul(LHS, RHS));
53   }
54   Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
55     return Fold(ConstantExpr::getUDiv(LHS, RHS));
56   }
57   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
58     return Fold(ConstantExpr::getSDiv(LHS, RHS));
59   }
60   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
61     return Fold(ConstantExpr::getFDiv(LHS, RHS));
62   }
63   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
64     return Fold(ConstantExpr::getURem(LHS, RHS));
65   }
66   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
67     return Fold(ConstantExpr::getSRem(LHS, RHS));
68   }
69   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
70     return Fold(ConstantExpr::getFRem(LHS, RHS));
71   }
72   Constant *CreateShl(Constant *LHS, Constant *RHS) const {
73     return Fold(ConstantExpr::getShl(LHS, RHS));
74   }
75   Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
76     return Fold(ConstantExpr::getLShr(LHS, RHS));
77   }
78   Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
79     return Fold(ConstantExpr::getAShr(LHS, RHS));
80   }
81   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
82     return Fold(ConstantExpr::getAnd(LHS, RHS));
83   }
84   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
85     return Fold(ConstantExpr::getOr(LHS, RHS));
86   }
87   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
88     return Fold(ConstantExpr::getXor(LHS, RHS));
89   }
90
91   Constant *CreateBinOp(Instruction::BinaryOps Opc,
92                         Constant *LHS, Constant *RHS) const {
93     return Fold(ConstantExpr::get(Opc, LHS, RHS));
94   }
95
96   //===--------------------------------------------------------------------===//
97   // Unary Operators
98   //===--------------------------------------------------------------------===//
99
100   Constant *CreateNeg(Constant *C) const {
101     return Fold(ConstantExpr::getNeg(C));
102   }
103   Constant *CreateNot(Constant *C) const {
104     return Fold(ConstantExpr::getNot(C));
105   }
106
107   //===--------------------------------------------------------------------===//
108   // Memory Instructions
109   //===--------------------------------------------------------------------===//
110
111   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
112                                 unsigned NumIdx) const {
113     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
114   }
115   Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
116                                 unsigned NumIdx) const {
117     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
118   }
119
120   //===--------------------------------------------------------------------===//
121   // Cast/Conversion Operators
122   //===--------------------------------------------------------------------===//
123
124   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
125                        const Type *DestTy) const {
126     if (C->getType() == DestTy)
127       return C; // avoid calling Fold
128     return Fold(ConstantExpr::getCast(Op, C, DestTy));
129   }
130   Constant *CreateIntCast(Constant *C, const Type *DestTy,
131                           bool isSigned) const {
132     if (C->getType() == DestTy)
133       return C; // avoid calling Fold
134     return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
135   }
136
137   Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
138     return CreateCast(Instruction::BitCast, C, DestTy);
139   }
140   Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
141     return CreateCast(Instruction::IntToPtr, C, DestTy);
142   }
143   Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
144     return CreateCast(Instruction::PtrToInt, C, DestTy);
145   }
146   Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
147     if (C->getType() == DestTy)
148       return C; // avoid calling Fold
149     return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
150   }
151
152   //===--------------------------------------------------------------------===//
153   // Compare Instructions
154   //===--------------------------------------------------------------------===//
155
156   Constant *CreateCompare(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
157     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
158   }
159
160   //===--------------------------------------------------------------------===//
161   // Other Instructions
162   //===--------------------------------------------------------------------===//
163
164   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
165     return Fold(ConstantExpr::getSelect(C, True, False));
166   }
167
168   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
169     return Fold(ConstantExpr::getExtractElement(Vec, Idx));
170   }
171
172   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx)const {
173     return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
174   }
175
176   Constant *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
177     return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
178   }
179
180   Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
181                                unsigned NumIdx) const {
182     return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
183   }
184
185   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
186                               const unsigned *IdxList, unsigned NumIdx) const {
187     return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));
188   }
189 };
190
191 }
192
193 #endif