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, which provides a set of methods
11 // for creating constants, with minimal folding.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
16 #define LLVM_SUPPORT_CONSTANTFOLDER_H
18 #include "llvm/Constants.h"
22 /// ConstantFolder - Create constants with minimum, target independent, folding.
23 class ConstantFolder {
26 //===--------------------------------------------------------------------===//
28 //===--------------------------------------------------------------------===//
30 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
31 return ConstantExpr::getAdd(LHS, RHS);
33 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
34 return ConstantExpr::getSub(LHS, RHS);
36 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
37 return ConstantExpr::getMul(LHS, RHS);
39 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
40 return ConstantExpr::getUDiv(LHS, RHS);
42 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
43 return ConstantExpr::getSDiv(LHS, RHS);
45 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
46 return ConstantExpr::getFDiv(LHS, RHS);
48 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
49 return ConstantExpr::getURem(LHS, RHS);
51 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
52 return ConstantExpr::getSRem(LHS, RHS);
54 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
55 return ConstantExpr::getFRem(LHS, RHS);
57 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
58 return ConstantExpr::getShl(LHS, RHS);
60 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
61 return ConstantExpr::getLShr(LHS, RHS);
63 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
64 return ConstantExpr::getAShr(LHS, RHS);
66 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
67 return ConstantExpr::getAnd(LHS, RHS);
69 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
70 return ConstantExpr::getOr(LHS, RHS);
72 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
73 return ConstantExpr::getXor(LHS, RHS);
76 Constant *CreateBinOp(Instruction::BinaryOps Opc,
77 Constant *LHS, Constant *RHS) const {
78 return ConstantExpr::get(Opc, LHS, RHS);
81 //===--------------------------------------------------------------------===//
83 //===--------------------------------------------------------------------===//
85 Constant *CreateNeg(Constant *C) const {
86 return ConstantExpr::getNeg(C);
88 Constant *CreateNot(Constant *C) const {
89 return ConstantExpr::getNot(C);
92 //===--------------------------------------------------------------------===//
93 // Memory Instructions
94 //===--------------------------------------------------------------------===//
96 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
97 unsigned NumIdx) const {
98 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
100 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
101 unsigned NumIdx) const {
102 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
105 //===--------------------------------------------------------------------===//
106 // Cast/Conversion Operators
107 //===--------------------------------------------------------------------===//
109 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
110 const Type *DestTy) const {
111 return ConstantExpr::getCast(Op, C, DestTy);
113 Constant *CreateIntCast(Constant *C, const Type *DestTy,
114 bool isSigned) const {
115 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
118 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
119 return CreateCast(Instruction::BitCast, C, DestTy);
121 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
122 return CreateCast(Instruction::IntToPtr, C, DestTy);
124 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
125 return CreateCast(Instruction::PtrToInt, C, DestTy);
127 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
128 return ConstantExpr::getTruncOrBitCast(C, DestTy);
131 //===--------------------------------------------------------------------===//
132 // Compare Instructions
133 //===--------------------------------------------------------------------===//
135 Constant *CreateCompare(CmpInst::Predicate P, Constant *LHS,
136 Constant *RHS) const {
137 return ConstantExpr::getCompare(P, LHS, RHS);
140 //===--------------------------------------------------------------------===//
141 // Other Instructions
142 //===--------------------------------------------------------------------===//
144 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
145 return ConstantExpr::getSelect(C, True, False);
148 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
149 return ConstantExpr::getExtractElement(Vec, Idx);
152 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
153 Constant *Idx) const {
154 return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
157 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
158 Constant *Mask) const {
159 return ConstantExpr::getShuffleVector(V1, V2, Mask);
162 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
163 unsigned NumIdx) const {
164 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
167 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
168 const unsigned *IdxList, unsigned NumIdx) const {
169 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);