1 //====-- llvm/Support/TargetFolder.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 TargetFolder class, which provides a set of methods
11 // for creating constants, with target dependent folding.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_TARGETFOLDER_H
17 #define LLVM_SUPPORT_TARGETFOLDER_H
19 #include "llvm/Constants.h"
20 #include "llvm/Analysis/ConstantFolding.h"
26 /// TargetFolder - Create constants with target dependent folding.
30 /// Fold - Fold the constant using target specific information.
31 Constant *Fold(Constant *C) const {
32 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
33 if (Constant *CF = ConstantFoldConstantExpression(CE, &TD))
39 TargetFolder(const TargetData &TheTD) : TD(TheTD) {}
41 //===--------------------------------------------------------------------===//
43 //===--------------------------------------------------------------------===//
45 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
46 return Fold(ConstantExpr::getAdd(LHS, RHS));
48 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
49 return Fold(ConstantExpr::getSub(LHS, RHS));
51 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
52 return Fold(ConstantExpr::getMul(LHS, RHS));
54 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
55 return Fold(ConstantExpr::getUDiv(LHS, RHS));
57 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
58 return Fold(ConstantExpr::getSDiv(LHS, RHS));
60 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
61 return Fold(ConstantExpr::getFDiv(LHS, RHS));
63 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
64 return Fold(ConstantExpr::getURem(LHS, RHS));
66 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
67 return Fold(ConstantExpr::getSRem(LHS, RHS));
69 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
70 return Fold(ConstantExpr::getFRem(LHS, RHS));
72 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
73 return Fold(ConstantExpr::getShl(LHS, RHS));
75 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
76 return Fold(ConstantExpr::getLShr(LHS, RHS));
78 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
79 return Fold(ConstantExpr::getAShr(LHS, RHS));
81 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
82 return Fold(ConstantExpr::getAnd(LHS, RHS));
84 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
85 return Fold(ConstantExpr::getOr(LHS, RHS));
87 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
88 return Fold(ConstantExpr::getXor(LHS, RHS));
91 Constant *CreateBinOp(Instruction::BinaryOps Opc,
92 Constant *LHS, Constant *RHS) const {
93 return Fold(ConstantExpr::get(Opc, LHS, RHS));
96 //===--------------------------------------------------------------------===//
98 //===--------------------------------------------------------------------===//
100 Constant *CreateNeg(Constant *C) const {
101 return Fold(ConstantExpr::getNeg(C));
103 Constant *CreateNot(Constant *C) const {
104 return Fold(ConstantExpr::getNot(C));
107 //===--------------------------------------------------------------------===//
108 // Memory Instructions
109 //===--------------------------------------------------------------------===//
111 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
112 unsigned NumIdx) const {
113 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
115 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
116 unsigned NumIdx) const {
117 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
120 //===--------------------------------------------------------------------===//
121 // Cast/Conversion Operators
122 //===--------------------------------------------------------------------===//
124 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
125 const Type *DestTy) const {
126 if (C->getType() == DestTy)
127 return C; // avoid calling Fold
128 return Fold(ConstantExpr::getCast(Op, C, DestTy));
130 Constant *CreateIntCast(Constant *C, const Type *DestTy,
131 bool isSigned) const {
132 if (C->getType() == DestTy)
133 return C; // avoid calling Fold
134 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
137 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
138 return CreateCast(Instruction::BitCast, C, DestTy);
140 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
141 return CreateCast(Instruction::IntToPtr, C, DestTy);
143 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
144 return CreateCast(Instruction::PtrToInt, C, DestTy);
146 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
147 if (C->getType() == DestTy)
148 return C; // avoid calling Fold
149 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
152 //===--------------------------------------------------------------------===//
153 // Compare Instructions
154 //===--------------------------------------------------------------------===//
156 Constant *CreateCompare(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
157 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
160 //===--------------------------------------------------------------------===//
161 // Other Instructions
162 //===--------------------------------------------------------------------===//
164 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
165 return Fold(ConstantExpr::getSelect(C, True, False));
168 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
169 return Fold(ConstantExpr::getExtractElement(Vec, Idx));
172 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx)const {
173 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
176 Constant *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
177 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
180 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
181 unsigned NumIdx) const {
182 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
185 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
186 const unsigned *IdxList, unsigned NumIdx) const {
187 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));