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 *CreateFAdd(Constant *LHS, Constant *RHS) const {
56 return Fold(ConstantExpr::getFAdd(LHS, RHS));
58 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
59 return Fold(ConstantExpr::getSub(LHS, RHS));
61 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
62 return Fold(ConstantExpr::getNSWSub(LHS, RHS));
64 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
65 return Fold(ConstantExpr::getFSub(LHS, RHS));
67 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
68 return Fold(ConstantExpr::getMul(LHS, RHS));
70 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
71 return Fold(ConstantExpr::getFMul(LHS, RHS));
73 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
74 return Fold(ConstantExpr::getUDiv(LHS, RHS));
76 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
77 return Fold(ConstantExpr::getSDiv(LHS, RHS));
79 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
80 return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
82 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
83 return Fold(ConstantExpr::getFDiv(LHS, RHS));
85 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
86 return Fold(ConstantExpr::getURem(LHS, RHS));
88 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
89 return Fold(ConstantExpr::getSRem(LHS, RHS));
91 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
92 return Fold(ConstantExpr::getFRem(LHS, RHS));
94 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
95 return Fold(ConstantExpr::getShl(LHS, RHS));
97 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
98 return Fold(ConstantExpr::getLShr(LHS, RHS));
100 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
101 return Fold(ConstantExpr::getAShr(LHS, RHS));
103 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
104 return Fold(ConstantExpr::getAnd(LHS, RHS));
106 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
107 return Fold(ConstantExpr::getOr(LHS, RHS));
109 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
110 return Fold(ConstantExpr::getXor(LHS, RHS));
113 Constant *CreateBinOp(Instruction::BinaryOps Opc,
114 Constant *LHS, Constant *RHS) const {
115 return Fold(ConstantExpr::get(Opc, LHS, RHS));
118 //===--------------------------------------------------------------------===//
120 //===--------------------------------------------------------------------===//
122 Constant *CreateNeg(Constant *C) const {
123 return Fold(ConstantExpr::getNeg(C));
125 Constant *CreateFNeg(Constant *C) const {
126 return Fold(ConstantExpr::getFNeg(C));
128 Constant *CreateNot(Constant *C) const {
129 return Fold(ConstantExpr::getNot(C));
132 //===--------------------------------------------------------------------===//
133 // Memory Instructions
134 //===--------------------------------------------------------------------===//
136 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
137 unsigned NumIdx) const {
138 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
140 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
141 unsigned NumIdx) const {
142 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
145 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
146 unsigned NumIdx) const {
147 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
149 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
150 unsigned NumIdx) const {
151 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
154 //===--------------------------------------------------------------------===//
155 // Cast/Conversion Operators
156 //===--------------------------------------------------------------------===//
158 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
159 const Type *DestTy) const {
160 if (C->getType() == DestTy)
161 return C; // avoid calling Fold
162 return Fold(ConstantExpr::getCast(Op, C, DestTy));
164 Constant *CreateIntCast(Constant *C, const Type *DestTy,
165 bool isSigned) const {
166 if (C->getType() == DestTy)
167 return C; // avoid calling Fold
168 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
171 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
172 return CreateCast(Instruction::BitCast, C, DestTy);
174 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
175 return CreateCast(Instruction::IntToPtr, C, DestTy);
177 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
178 return CreateCast(Instruction::PtrToInt, C, DestTy);
180 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
181 if (C->getType() == DestTy)
182 return C; // avoid calling Fold
183 return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
185 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
186 if (C->getType() == DestTy)
187 return C; // avoid calling Fold
188 return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
190 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
191 if (C->getType() == DestTy)
192 return C; // avoid calling Fold
193 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
196 //===--------------------------------------------------------------------===//
197 // Compare Instructions
198 //===--------------------------------------------------------------------===//
200 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
201 Constant *RHS) const {
202 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
204 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
205 Constant *RHS) const {
206 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
209 //===--------------------------------------------------------------------===//
210 // Other Instructions
211 //===--------------------------------------------------------------------===//
213 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
214 return Fold(ConstantExpr::getSelect(C, True, False));
217 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
218 return Fold(ConstantExpr::getExtractElement(Vec, Idx));
221 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
222 Constant *Idx) const {
223 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
226 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
227 Constant *Mask) const {
228 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
231 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
232 unsigned NumIdx) const {
233 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
236 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
237 const unsigned *IdxList, unsigned NumIdx) const {
238 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));