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