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