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"
24 /// ConstantFolder - Create constants with minimum, target independent, folding.
25 class ConstantFolder {
28 //===--------------------------------------------------------------------===//
30 //===--------------------------------------------------------------------===//
32 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
33 return ConstantExpr::getAdd(LHS, RHS);
35 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
36 return ConstantExpr::getSub(LHS, RHS);
38 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
39 return ConstantExpr::getMul(LHS, RHS);
41 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
42 return ConstantExpr::getUDiv(LHS, RHS);
44 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
45 return ConstantExpr::getSDiv(LHS, RHS);
47 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
48 return ConstantExpr::getFDiv(LHS, RHS);
50 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
51 return ConstantExpr::getURem(LHS, RHS);
53 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
54 return ConstantExpr::getSRem(LHS, RHS);
56 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
57 return ConstantExpr::getFRem(LHS, RHS);
59 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
60 return ConstantExpr::getShl(LHS, RHS);
62 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
63 return ConstantExpr::getLShr(LHS, RHS);
65 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
66 return ConstantExpr::getAShr(LHS, RHS);
68 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
69 return ConstantExpr::getAnd(LHS, RHS);
71 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
72 return ConstantExpr::getOr(LHS, RHS);
74 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
75 return ConstantExpr::getXor(LHS, RHS);
78 Constant *CreateBinOp(Instruction::BinaryOps Opc,
79 Constant *LHS, Constant *RHS) const {
80 return ConstantExpr::get(Opc, LHS, RHS);
83 //===--------------------------------------------------------------------===//
85 //===--------------------------------------------------------------------===//
87 Constant *CreateNeg(Constant *C) const {
88 return ConstantExpr::getNeg(C);
90 Constant *CreateNot(Constant *C) const {
91 return ConstantExpr::getNot(C);
94 //===--------------------------------------------------------------------===//
95 // Memory Instructions
96 //===--------------------------------------------------------------------===//
98 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
99 unsigned NumIdx) const {
100 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
102 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
103 unsigned NumIdx) const {
104 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
107 //===--------------------------------------------------------------------===//
108 // Cast/Conversion Operators
109 //===--------------------------------------------------------------------===//
111 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
112 const Type *DestTy) const {
113 return ConstantExpr::getCast(Op, C, DestTy);
115 Constant *CreateIntCast(Constant *C, const Type *DestTy,
116 bool isSigned) const {
117 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
120 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
121 return CreateCast(Instruction::BitCast, C, DestTy);
123 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
124 return CreateCast(Instruction::IntToPtr, C, DestTy);
126 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
127 return CreateCast(Instruction::PtrToInt, C, DestTy);
129 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
130 return ConstantExpr::getTruncOrBitCast(C, DestTy);
133 //===--------------------------------------------------------------------===//
134 // Compare Instructions
135 //===--------------------------------------------------------------------===//
137 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
138 Constant *RHS) const {
139 return ConstantExpr::getCompare(P, LHS, RHS);
141 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
142 Constant *RHS) const {
143 return ConstantExpr::getCompare(P, LHS, RHS);
145 Constant *CreateVICmp(CmpInst::Predicate P, Constant *LHS,
146 Constant *RHS) const {
147 return ConstantExpr::getCompare(P, LHS, RHS);
149 Constant *CreateVFCmp(CmpInst::Predicate P, Constant *LHS,
150 Constant *RHS) const {
151 return ConstantExpr::getCompare(P, LHS, RHS);
154 //===--------------------------------------------------------------------===//
155 // Other Instructions
156 //===--------------------------------------------------------------------===//
158 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
159 return ConstantExpr::getSelect(C, True, False);
162 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
163 return ConstantExpr::getExtractElement(Vec, Idx);
166 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
167 Constant *Idx) const {
168 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
171 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
172 Constant *Mask) const {
173 return ConstantExpr::getShuffleVector(V1, V2, Mask);
176 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
177 unsigned NumIdx) const {
178 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
181 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
182 const unsigned *IdxList, unsigned NumIdx) const {
183 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);