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/InstrTypes.h"
24 #include "llvm/Analysis/ConstantFolding.h"
30 /// TargetFolder - Create constants with target dependent folding.
34 /// Fold - Fold the constant using target specific information.
35 Constant *Fold(Constant *C) const {
36 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
37 if (Constant *CF = ConstantFoldConstantExpression(CE, TD))
43 explicit TargetFolder(const TargetData *TheTD) : TD(TheTD) {}
45 //===--------------------------------------------------------------------===//
47 //===--------------------------------------------------------------------===//
49 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
50 return Fold(ConstantExpr::getAdd(LHS, RHS));
52 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
53 return Fold(ConstantExpr::getNSWAdd(LHS, RHS));
55 Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
56 return Fold(ConstantExpr::getNUWAdd(LHS, RHS));
58 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
59 return Fold(ConstantExpr::getFAdd(LHS, RHS));
61 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
62 return Fold(ConstantExpr::getSub(LHS, RHS));
64 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
65 return Fold(ConstantExpr::getNSWSub(LHS, RHS));
67 Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const {
68 return Fold(ConstantExpr::getNUWSub(LHS, RHS));
70 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
71 return Fold(ConstantExpr::getFSub(LHS, RHS));
73 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
74 return Fold(ConstantExpr::getMul(LHS, RHS));
76 Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const {
77 return Fold(ConstantExpr::getNSWMul(LHS, RHS));
79 Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const {
80 return Fold(ConstantExpr::getNUWMul(LHS, RHS));
82 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
83 return Fold(ConstantExpr::getFMul(LHS, RHS));
85 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
86 return Fold(ConstantExpr::getUDiv(LHS, RHS));
88 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
89 return Fold(ConstantExpr::getSDiv(LHS, RHS));
91 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
92 return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
94 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
95 return Fold(ConstantExpr::getFDiv(LHS, RHS));
97 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
98 return Fold(ConstantExpr::getURem(LHS, RHS));
100 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
101 return Fold(ConstantExpr::getSRem(LHS, RHS));
103 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
104 return Fold(ConstantExpr::getFRem(LHS, RHS));
106 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
107 return Fold(ConstantExpr::getShl(LHS, RHS));
109 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
110 return Fold(ConstantExpr::getLShr(LHS, RHS));
112 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
113 return Fold(ConstantExpr::getAShr(LHS, RHS));
115 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
116 return Fold(ConstantExpr::getAnd(LHS, RHS));
118 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
119 return Fold(ConstantExpr::getOr(LHS, RHS));
121 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
122 return Fold(ConstantExpr::getXor(LHS, RHS));
125 Constant *CreateBinOp(Instruction::BinaryOps Opc,
126 Constant *LHS, Constant *RHS) const {
127 return Fold(ConstantExpr::get(Opc, LHS, RHS));
130 //===--------------------------------------------------------------------===//
132 //===--------------------------------------------------------------------===//
134 Constant *CreateNeg(Constant *C) const {
135 return Fold(ConstantExpr::getNeg(C));
137 Constant *CreateNSWNeg(Constant *C) const {
138 return Fold(ConstantExpr::getNSWNeg(C));
140 Constant *CreateNUWNeg(Constant *C) const {
141 return Fold(ConstantExpr::getNUWNeg(C));
143 Constant *CreateFNeg(Constant *C) const {
144 return Fold(ConstantExpr::getFNeg(C));
146 Constant *CreateNot(Constant *C) const {
147 return Fold(ConstantExpr::getNot(C));
150 //===--------------------------------------------------------------------===//
151 // Memory Instructions
152 //===--------------------------------------------------------------------===//
154 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
155 unsigned NumIdx) const {
156 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
158 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
159 unsigned NumIdx) const {
160 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
163 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
164 unsigned NumIdx) const {
165 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
167 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
168 unsigned NumIdx) const {
169 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
172 //===--------------------------------------------------------------------===//
173 // Cast/Conversion Operators
174 //===--------------------------------------------------------------------===//
176 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
177 const Type *DestTy) const {
178 if (C->getType() == DestTy)
179 return C; // avoid calling Fold
180 return Fold(ConstantExpr::getCast(Op, C, DestTy));
182 Constant *CreateIntCast(Constant *C, const Type *DestTy,
183 bool isSigned) const {
184 if (C->getType() == DestTy)
185 return C; // avoid calling Fold
186 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
188 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
189 return ConstantExpr::getPointerCast(C, DestTy);
191 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
192 return CreateCast(Instruction::BitCast, C, DestTy);
194 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
195 return CreateCast(Instruction::IntToPtr, C, DestTy);
197 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
198 return CreateCast(Instruction::PtrToInt, C, DestTy);
200 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
201 if (C->getType() == DestTy)
202 return C; // avoid calling Fold
203 return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
205 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
206 if (C->getType() == DestTy)
207 return C; // avoid calling Fold
208 return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
210 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
211 if (C->getType() == DestTy)
212 return C; // avoid calling Fold
213 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
216 //===--------------------------------------------------------------------===//
217 // Compare Instructions
218 //===--------------------------------------------------------------------===//
220 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
221 Constant *RHS) const {
222 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
224 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
225 Constant *RHS) const {
226 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
229 //===--------------------------------------------------------------------===//
230 // Other Instructions
231 //===--------------------------------------------------------------------===//
233 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
234 return Fold(ConstantExpr::getSelect(C, True, False));
237 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
238 return Fold(ConstantExpr::getExtractElement(Vec, Idx));
241 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
242 Constant *Idx) const {
243 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
246 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
247 Constant *Mask) const {
248 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
251 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
252 unsigned NumIdx) const {
253 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
256 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
257 const unsigned *IdxList, unsigned NumIdx) const {
258 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));