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"
26 /// ConstantFolder - Create constants with minimum, target independent, folding.
27 class ConstantFolder {
29 explicit ConstantFolder(LLVMContext &) {}
31 //===--------------------------------------------------------------------===//
33 //===--------------------------------------------------------------------===//
35 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
36 return ConstantExpr::getAdd(LHS, RHS);
38 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
39 return ConstantExpr::getFAdd(LHS, RHS);
41 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
42 return ConstantExpr::getSub(LHS, RHS);
44 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
45 return ConstantExpr::getFSub(LHS, RHS);
47 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
48 return ConstantExpr::getMul(LHS, RHS);
50 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
51 return ConstantExpr::getFMul(LHS, RHS);
53 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
54 return ConstantExpr::getUDiv(LHS, RHS);
56 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
57 return ConstantExpr::getSDiv(LHS, RHS);
59 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
60 return ConstantExpr::getExactSDiv(LHS, RHS);
62 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
63 return ConstantExpr::getFDiv(LHS, RHS);
65 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
66 return ConstantExpr::getURem(LHS, RHS);
68 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
69 return ConstantExpr::getSRem(LHS, RHS);
71 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
72 return ConstantExpr::getFRem(LHS, RHS);
74 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
75 return ConstantExpr::getShl(LHS, RHS);
77 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
78 return ConstantExpr::getLShr(LHS, RHS);
80 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
81 return ConstantExpr::getAShr(LHS, RHS);
83 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
84 return ConstantExpr::getAnd(LHS, RHS);
86 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
87 return ConstantExpr::getOr(LHS, RHS);
89 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
90 return ConstantExpr::getXor(LHS, RHS);
93 Constant *CreateBinOp(Instruction::BinaryOps Opc,
94 Constant *LHS, Constant *RHS) const {
95 return ConstantExpr::get(Opc, LHS, RHS);
98 //===--------------------------------------------------------------------===//
100 //===--------------------------------------------------------------------===//
102 Constant *CreateNeg(Constant *C) const {
103 return ConstantExpr::getNeg(C);
105 Constant *CreateFNeg(Constant *C) const {
106 return ConstantExpr::getFNeg(C);
108 Constant *CreateNot(Constant *C) const {
109 return ConstantExpr::getNot(C);
112 //===--------------------------------------------------------------------===//
113 // Memory Instructions
114 //===--------------------------------------------------------------------===//
116 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
117 unsigned NumIdx) const {
118 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
120 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
121 unsigned NumIdx) const {
122 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
125 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
126 unsigned NumIdx) const {
127 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
129 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
130 unsigned NumIdx) const {
131 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
134 //===--------------------------------------------------------------------===//
135 // Cast/Conversion Operators
136 //===--------------------------------------------------------------------===//
138 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
139 const Type *DestTy) const {
140 return ConstantExpr::getCast(Op, C, DestTy);
142 Constant *CreateIntCast(Constant *C, const Type *DestTy,
143 bool isSigned) const {
144 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
147 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
148 return CreateCast(Instruction::BitCast, C, DestTy);
150 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
151 return CreateCast(Instruction::IntToPtr, C, DestTy);
153 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
154 return CreateCast(Instruction::PtrToInt, C, DestTy);
156 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
157 return ConstantExpr::getTruncOrBitCast(C, DestTy);
160 //===--------------------------------------------------------------------===//
161 // Compare Instructions
162 //===--------------------------------------------------------------------===//
164 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
165 Constant *RHS) const {
166 return ConstantExpr::getCompare(P, LHS, RHS);
168 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
169 Constant *RHS) const {
170 return ConstantExpr::getCompare(P, LHS, RHS);
173 //===--------------------------------------------------------------------===//
174 // Other Instructions
175 //===--------------------------------------------------------------------===//
177 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
178 return ConstantExpr::getSelect(C, True, False);
181 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
182 return ConstantExpr::getExtractElement(Vec, Idx);
185 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
186 Constant *Idx) const {
187 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
190 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
191 Constant *Mask) const {
192 return ConstantExpr::getShuffleVector(V1, V2, Mask);
195 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
196 unsigned NumIdx) const {
197 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
200 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
201 const unsigned *IdxList, unsigned NumIdx) const {
202 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);