1 //===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
18 #define LLVM_SUPPORT_CONSTANTFOLDER_H
20 #include "llvm/Constants.h"
21 #include "llvm/InstrTypes.h"
27 /// ConstantFolder - Create constants with minimum, target independent, folding.
28 class ConstantFolder {
30 explicit ConstantFolder(LLVMContext &) {}
32 //===--------------------------------------------------------------------===//
34 //===--------------------------------------------------------------------===//
36 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
37 return ConstantExpr::getAdd(LHS, RHS);
39 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
40 return ConstantExpr::getNSWAdd(LHS, RHS);
42 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
43 return ConstantExpr::getFAdd(LHS, RHS);
45 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
46 return ConstantExpr::getSub(LHS, RHS);
48 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
49 return ConstantExpr::getNSWSub(LHS, RHS);
51 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
52 return ConstantExpr::getFSub(LHS, RHS);
54 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
55 return ConstantExpr::getMul(LHS, RHS);
57 Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const {
58 return ConstantExpr::getNSWMul(LHS, RHS);
60 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
61 return ConstantExpr::getFMul(LHS, RHS);
63 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
64 return ConstantExpr::getUDiv(LHS, RHS);
66 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
67 return ConstantExpr::getSDiv(LHS, RHS);
69 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
70 return ConstantExpr::getExactSDiv(LHS, RHS);
72 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
73 return ConstantExpr::getFDiv(LHS, RHS);
75 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
76 return ConstantExpr::getURem(LHS, RHS);
78 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
79 return ConstantExpr::getSRem(LHS, RHS);
81 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
82 return ConstantExpr::getFRem(LHS, RHS);
84 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
85 return ConstantExpr::getShl(LHS, RHS);
87 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
88 return ConstantExpr::getLShr(LHS, RHS);
90 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
91 return ConstantExpr::getAShr(LHS, RHS);
93 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
94 return ConstantExpr::getAnd(LHS, RHS);
96 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
97 return ConstantExpr::getOr(LHS, RHS);
99 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
100 return ConstantExpr::getXor(LHS, RHS);
103 Constant *CreateBinOp(Instruction::BinaryOps Opc,
104 Constant *LHS, Constant *RHS) const {
105 return ConstantExpr::get(Opc, LHS, RHS);
108 //===--------------------------------------------------------------------===//
110 //===--------------------------------------------------------------------===//
112 Constant *CreateNeg(Constant *C) const {
113 return ConstantExpr::getNeg(C);
115 Constant *CreateNSWNeg(Constant *C) const {
116 return ConstantExpr::getNSWNeg(C);
118 Constant *CreateFNeg(Constant *C) const {
119 return ConstantExpr::getFNeg(C);
121 Constant *CreateNot(Constant *C) const {
122 return ConstantExpr::getNot(C);
125 //===--------------------------------------------------------------------===//
126 // Memory Instructions
127 //===--------------------------------------------------------------------===//
129 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
130 unsigned NumIdx) const {
131 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
133 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
134 unsigned NumIdx) const {
135 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
138 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
139 unsigned NumIdx) const {
140 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
142 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
143 unsigned NumIdx) const {
144 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
147 //===--------------------------------------------------------------------===//
148 // Cast/Conversion Operators
149 //===--------------------------------------------------------------------===//
151 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
152 const Type *DestTy) const {
153 return ConstantExpr::getCast(Op, C, DestTy);
155 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
156 return ConstantExpr::getPointerCast(C, DestTy);
158 Constant *CreateIntCast(Constant *C, const Type *DestTy,
159 bool isSigned) const {
160 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
162 Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
163 return ConstantExpr::getFPCast(C, DestTy);
166 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
167 return CreateCast(Instruction::BitCast, C, DestTy);
169 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
170 return CreateCast(Instruction::IntToPtr, C, DestTy);
172 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
173 return CreateCast(Instruction::PtrToInt, C, DestTy);
175 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
176 return ConstantExpr::getZExtOrBitCast(C, DestTy);
178 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
179 return ConstantExpr::getSExtOrBitCast(C, DestTy);
182 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
183 return ConstantExpr::getTruncOrBitCast(C, DestTy);
186 //===--------------------------------------------------------------------===//
187 // Compare Instructions
188 //===--------------------------------------------------------------------===//
190 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
191 Constant *RHS) const {
192 return ConstantExpr::getCompare(P, LHS, RHS);
194 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
195 Constant *RHS) const {
196 return ConstantExpr::getCompare(P, LHS, RHS);
199 //===--------------------------------------------------------------------===//
200 // Other Instructions
201 //===--------------------------------------------------------------------===//
203 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
204 return ConstantExpr::getSelect(C, True, False);
207 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
208 return ConstantExpr::getExtractElement(Vec, Idx);
211 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
212 Constant *Idx) const {
213 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
216 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
217 Constant *Mask) const {
218 return ConstantExpr::getShuffleVector(V1, V2, Mask);
221 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
222 unsigned NumIdx) const {
223 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
226 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
227 const unsigned *IdxList, unsigned NumIdx) const {
228 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);