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