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 *CreateFAdd(Constant *LHS, Constant *RHS) const {
36 return ConstantExpr::getFAdd(LHS, RHS);
38 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
39 return ConstantExpr::getSub(LHS, RHS);
41 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
42 return ConstantExpr::getFSub(LHS, RHS);
44 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
45 return ConstantExpr::getMul(LHS, RHS);
47 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
48 return ConstantExpr::getFMul(LHS, RHS);
50 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
51 return ConstantExpr::getUDiv(LHS, RHS);
53 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
54 return ConstantExpr::getSDiv(LHS, RHS);
56 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
57 return ConstantExpr::getFDiv(LHS, RHS);
59 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
60 return ConstantExpr::getURem(LHS, RHS);
62 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
63 return ConstantExpr::getSRem(LHS, RHS);
65 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
66 return ConstantExpr::getFRem(LHS, RHS);
68 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
69 return ConstantExpr::getShl(LHS, RHS);
71 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
72 return ConstantExpr::getLShr(LHS, RHS);
74 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
75 return ConstantExpr::getAShr(LHS, RHS);
77 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
78 return ConstantExpr::getAnd(LHS, RHS);
80 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
81 return ConstantExpr::getOr(LHS, RHS);
83 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
84 return ConstantExpr::getXor(LHS, RHS);
87 Constant *CreateBinOp(Instruction::BinaryOps Opc,
88 Constant *LHS, Constant *RHS) const {
89 return ConstantExpr::get(Opc, LHS, RHS);
92 //===--------------------------------------------------------------------===//
94 //===--------------------------------------------------------------------===//
96 Constant *CreateNeg(Constant *C) const {
97 return ConstantExpr::getNeg(C);
99 Constant *CreateFNeg(Constant *C) const {
100 return ConstantExpr::getFNeg(C);
102 Constant *CreateNot(Constant *C) const {
103 return ConstantExpr::getNot(C);
106 //===--------------------------------------------------------------------===//
107 // Memory Instructions
108 //===--------------------------------------------------------------------===//
110 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
111 unsigned NumIdx) const {
112 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
114 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
115 unsigned NumIdx) const {
116 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
119 //===--------------------------------------------------------------------===//
120 // Cast/Conversion Operators
121 //===--------------------------------------------------------------------===//
123 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
124 const Type *DestTy) const {
125 return ConstantExpr::getCast(Op, C, DestTy);
127 Constant *CreateIntCast(Constant *C, const Type *DestTy,
128 bool isSigned) const {
129 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
132 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
133 return CreateCast(Instruction::BitCast, C, DestTy);
135 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
136 return CreateCast(Instruction::IntToPtr, C, DestTy);
138 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
139 return CreateCast(Instruction::PtrToInt, C, DestTy);
141 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
142 return ConstantExpr::getTruncOrBitCast(C, DestTy);
145 //===--------------------------------------------------------------------===//
146 // Compare Instructions
147 //===--------------------------------------------------------------------===//
149 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
150 Constant *RHS) const {
151 return ConstantExpr::getCompare(P, LHS, RHS);
153 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
154 Constant *RHS) const {
155 return ConstantExpr::getCompare(P, LHS, RHS);
157 Constant *CreateVICmp(CmpInst::Predicate P, Constant *LHS,
158 Constant *RHS) const {
159 return ConstantExpr::getCompare(P, LHS, RHS);
161 Constant *CreateVFCmp(CmpInst::Predicate P, Constant *LHS,
162 Constant *RHS) const {
163 return ConstantExpr::getCompare(P, LHS, RHS);
166 //===--------------------------------------------------------------------===//
167 // Other Instructions
168 //===--------------------------------------------------------------------===//
170 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
171 return ConstantExpr::getSelect(C, True, False);
174 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
175 return ConstantExpr::getExtractElement(Vec, Idx);
178 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
179 Constant *Idx) const {
180 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
183 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
184 Constant *Mask) const {
185 return ConstantExpr::getShuffleVector(V1, V2, Mask);
188 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
189 unsigned NumIdx) const {
190 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
193 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
194 const unsigned *IdxList, unsigned NumIdx) const {
195 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);