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, a helper for IRBuilder.
11 // It provides IRBuilder with a set of methods for creating constants with
12 // target dependent folding, in addition to the same target-independent
13 // folding that the ConstantFolder class provides. For general constant
14 // creation and folding, use ConstantExpr and the routines in
15 // llvm/Analysis/ConstantFolding.h.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_SUPPORT_TARGETFOLDER_H
20 #define LLVM_SUPPORT_TARGETFOLDER_H
22 #include "llvm/Constants.h"
23 #include "llvm/Analysis/ConstantFolding.h"
29 /// TargetFolder - Create constants with target dependent folding.
33 /// Fold - Fold the constant using target specific information.
34 Constant *Fold(Constant *C) const {
35 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
36 if (Constant *CF = ConstantFoldConstantExpression(CE, TD))
42 explicit TargetFolder(const TargetData *TheTD) : TD(TheTD) {}
44 //===--------------------------------------------------------------------===//
46 //===--------------------------------------------------------------------===//
48 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
49 return Fold(ConstantExpr::getAdd(LHS, RHS));
51 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
52 return Fold(ConstantExpr::getFAdd(LHS, RHS));
54 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
55 return Fold(ConstantExpr::getSub(LHS, RHS));
57 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
58 return Fold(ConstantExpr::getFSub(LHS, RHS));
60 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
61 return Fold(ConstantExpr::getMul(LHS, RHS));
63 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
64 return Fold(ConstantExpr::getFMul(LHS, RHS));
66 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
67 return Fold(ConstantExpr::getUDiv(LHS, RHS));
69 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
70 return Fold(ConstantExpr::getSDiv(LHS, RHS));
72 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
73 return Fold(ConstantExpr::getFDiv(LHS, RHS));
75 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
76 return Fold(ConstantExpr::getURem(LHS, RHS));
78 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
79 return Fold(ConstantExpr::getSRem(LHS, RHS));
81 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
82 return Fold(ConstantExpr::getFRem(LHS, RHS));
84 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
85 return Fold(ConstantExpr::getShl(LHS, RHS));
87 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
88 return Fold(ConstantExpr::getLShr(LHS, RHS));
90 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
91 return Fold(ConstantExpr::getAShr(LHS, RHS));
93 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
94 return Fold(ConstantExpr::getAnd(LHS, RHS));
96 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
97 return Fold(ConstantExpr::getOr(LHS, RHS));
99 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
100 return Fold(ConstantExpr::getXor(LHS, RHS));
103 Constant *CreateBinOp(Instruction::BinaryOps Opc,
104 Constant *LHS, Constant *RHS) const {
105 return Fold(ConstantExpr::get(Opc, LHS, RHS));
108 //===--------------------------------------------------------------------===//
110 //===--------------------------------------------------------------------===//
112 Constant *CreateNeg(Constant *C) const {
113 return Fold(ConstantExpr::getNeg(C));
115 Constant *CreateFNeg(Constant *C) const {
116 return Fold(ConstantExpr::getFNeg(C));
118 Constant *CreateNot(Constant *C) const {
119 return Fold(ConstantExpr::getNot(C));
122 //===--------------------------------------------------------------------===//
123 // Memory Instructions
124 //===--------------------------------------------------------------------===//
126 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
127 unsigned NumIdx) const {
128 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
130 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
131 unsigned NumIdx) const {
132 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
135 //===--------------------------------------------------------------------===//
136 // Cast/Conversion Operators
137 //===--------------------------------------------------------------------===//
139 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
140 const Type *DestTy) const {
141 if (C->getType() == DestTy)
142 return C; // avoid calling Fold
143 return Fold(ConstantExpr::getCast(Op, C, DestTy));
145 Constant *CreateIntCast(Constant *C, const Type *DestTy,
146 bool isSigned) const {
147 if (C->getType() == DestTy)
148 return C; // avoid calling Fold
149 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
152 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
153 return CreateCast(Instruction::BitCast, C, DestTy);
155 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
156 return CreateCast(Instruction::IntToPtr, C, DestTy);
158 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
159 return CreateCast(Instruction::PtrToInt, C, DestTy);
161 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
162 if (C->getType() == DestTy)
163 return C; // avoid calling Fold
164 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
167 //===--------------------------------------------------------------------===//
168 // Compare Instructions
169 //===--------------------------------------------------------------------===//
171 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
172 Constant *RHS) const {
173 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
175 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
176 Constant *RHS) const {
177 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
179 Constant *CreateVICmp(CmpInst::Predicate P, Constant *LHS,
180 Constant *RHS) const {
181 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
183 Constant *CreateVFCmp(CmpInst::Predicate P, Constant *LHS,
184 Constant *RHS) const {
185 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
188 //===--------------------------------------------------------------------===//
189 // Other Instructions
190 //===--------------------------------------------------------------------===//
192 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
193 return Fold(ConstantExpr::getSelect(C, True, False));
196 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
197 return Fold(ConstantExpr::getExtractElement(Vec, Idx));
200 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
201 Constant *Idx) const {
202 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
205 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
206 Constant *Mask) const {
207 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
210 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
211 unsigned NumIdx) const {
212 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
215 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
216 const unsigned *IdxList, unsigned NumIdx) const {
217 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));