TargetFolder.h revision e5db7c4166957e13a7df84570be0b9ddc2ef07fe
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, in addition to the same target-independent
13// folding that the ConstantFolder class provides.  For general constant
14// creation and folding, use ConstantExpr and the routines in
15// llvm/Analysis/ConstantFolding.h.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_SUPPORT_TARGETFOLDER_H
20#define LLVM_SUPPORT_TARGETFOLDER_H
21
22#include "llvm/Constants.h"
23#include "llvm/InstrTypes.h"
24#include "llvm/Analysis/ConstantFolding.h"
25
26namespace llvm {
27
28class TargetData;
29class LLVMContext;
30
31/// TargetFolder - Create constants with target dependent folding.
32class TargetFolder {
33  const TargetData *TD;
34  LLVMContext &Context;
35
36  /// Fold - Fold the constant using target specific information.
37  Constant *Fold(Constant *C) const {
38    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
39      if (Constant *CF = ConstantFoldConstantExpression(CE, Context, TD))
40        return CF;
41    return C;
42  }
43
44public:
45  explicit TargetFolder(const TargetData *TheTD, LLVMContext &C) :
46    TD(TheTD), Context(C) {}
47
48  //===--------------------------------------------------------------------===//
49  // Binary Operators
50  //===--------------------------------------------------------------------===//
51
52  Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
53    return Fold(ConstantExpr::getAdd(LHS, RHS));
54  }
55  Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
56    return Fold(ConstantExpr::getNSWAdd(LHS, RHS));
57  }
58  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
59    return Fold(ConstantExpr::getFAdd(LHS, RHS));
60  }
61  Constant *CreateSub(Constant *LHS, Constant *RHS) const {
62    return Fold(ConstantExpr::getSub(LHS, RHS));
63  }
64  Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
65    return Fold(ConstantExpr::getNSWSub(LHS, RHS));
66  }
67  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
68    return Fold(ConstantExpr::getFSub(LHS, RHS));
69  }
70  Constant *CreateMul(Constant *LHS, Constant *RHS) const {
71    return Fold(ConstantExpr::getMul(LHS, RHS));
72  }
73  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
74    return Fold(ConstantExpr::getFMul(LHS, RHS));
75  }
76  Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
77    return Fold(ConstantExpr::getUDiv(LHS, RHS));
78  }
79  Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
80    return Fold(ConstantExpr::getSDiv(LHS, RHS));
81  }
82  Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
83    return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
84  }
85  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
86    return Fold(ConstantExpr::getFDiv(LHS, RHS));
87  }
88  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
89    return Fold(ConstantExpr::getURem(LHS, RHS));
90  }
91  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
92    return Fold(ConstantExpr::getSRem(LHS, RHS));
93  }
94  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
95    return Fold(ConstantExpr::getFRem(LHS, RHS));
96  }
97  Constant *CreateShl(Constant *LHS, Constant *RHS) const {
98    return Fold(ConstantExpr::getShl(LHS, RHS));
99  }
100  Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
101    return Fold(ConstantExpr::getLShr(LHS, RHS));
102  }
103  Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
104    return Fold(ConstantExpr::getAShr(LHS, RHS));
105  }
106  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
107    return Fold(ConstantExpr::getAnd(LHS, RHS));
108  }
109  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
110    return Fold(ConstantExpr::getOr(LHS, RHS));
111  }
112  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
113    return Fold(ConstantExpr::getXor(LHS, RHS));
114  }
115
116  Constant *CreateBinOp(Instruction::BinaryOps Opc,
117                        Constant *LHS, Constant *RHS) const {
118    return Fold(ConstantExpr::get(Opc, LHS, RHS));
119  }
120
121  //===--------------------------------------------------------------------===//
122  // Unary Operators
123  //===--------------------------------------------------------------------===//
124
125  Constant *CreateNeg(Constant *C) const {
126    return Fold(ConstantExpr::getNeg(C));
127  }
128  Constant *CreateFNeg(Constant *C) const {
129    return Fold(ConstantExpr::getFNeg(C));
130  }
131  Constant *CreateNot(Constant *C) const {
132    return Fold(ConstantExpr::getNot(C));
133  }
134
135  //===--------------------------------------------------------------------===//
136  // Memory Instructions
137  //===--------------------------------------------------------------------===//
138
139  Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
140                                unsigned NumIdx) const {
141    return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
142  }
143  Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
144                                unsigned NumIdx) const {
145    return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
146  }
147
148  Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
149                                        unsigned NumIdx) const {
150    return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
151  }
152  Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
153                                        unsigned NumIdx) const {
154    return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
155  }
156
157  //===--------------------------------------------------------------------===//
158  // Cast/Conversion Operators
159  //===--------------------------------------------------------------------===//
160
161  Constant *CreateCast(Instruction::CastOps Op, Constant *C,
162                       const Type *DestTy) const {
163    if (C->getType() == DestTy)
164      return C; // avoid calling Fold
165    return Fold(ConstantExpr::getCast(Op, C, DestTy));
166  }
167  Constant *CreateIntCast(Constant *C, const Type *DestTy,
168                          bool isSigned) const {
169    if (C->getType() == DestTy)
170      return C; // avoid calling Fold
171    return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
172  }
173
174  Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
175    return CreateCast(Instruction::BitCast, C, DestTy);
176  }
177  Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
178    return CreateCast(Instruction::IntToPtr, C, DestTy);
179  }
180  Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
181    return CreateCast(Instruction::PtrToInt, C, DestTy);
182  }
183  Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
184    if (C->getType() == DestTy)
185      return C; // avoid calling Fold
186    return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
187  }
188  Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
189    if (C->getType() == DestTy)
190      return C; // avoid calling Fold
191    return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
192  }
193  Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
194    if (C->getType() == DestTy)
195      return C; // avoid calling Fold
196    return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
197  }
198
199  //===--------------------------------------------------------------------===//
200  // Compare Instructions
201  //===--------------------------------------------------------------------===//
202
203  Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
204                       Constant *RHS) const {
205    return Fold(ConstantExpr::getCompare(P, LHS, RHS));
206  }
207  Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
208                       Constant *RHS) const {
209    return Fold(ConstantExpr::getCompare(P, LHS, RHS));
210  }
211
212  //===--------------------------------------------------------------------===//
213  // Other Instructions
214  //===--------------------------------------------------------------------===//
215
216  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
217    return Fold(ConstantExpr::getSelect(C, True, False));
218  }
219
220  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
221    return Fold(ConstantExpr::getExtractElement(Vec, Idx));
222  }
223
224  Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
225                                Constant *Idx) const {
226    return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
227  }
228
229  Constant *CreateShuffleVector(Constant *V1, Constant *V2,
230                                Constant *Mask) const {
231    return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
232  }
233
234  Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
235                               unsigned NumIdx) const {
236    return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
237  }
238
239  Constant *CreateInsertValue(Constant *Agg, Constant *Val,
240                              const unsigned *IdxList, unsigned NumIdx) const {
241    return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));
242  }
243};
244
245}
246
247#endif
248