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/ADT/ArrayRef.h"
25 #include "llvm/Analysis/ConstantFolding.h"
31 /// TargetFolder - Create constants with target dependent folding.
35 /// Fold - Fold the constant using target specific information.
36 Constant *Fold(Constant *C) const {
37 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
38 if (Constant *CF = ConstantFoldConstantExpression(CE, TD))
44 explicit TargetFolder(const TargetData *TheTD) : TD(TheTD) {}
46 //===--------------------------------------------------------------------===//
48 //===--------------------------------------------------------------------===//
50 Constant *CreateAdd(Constant *LHS, Constant *RHS,
51 bool HasNUW = false, bool HasNSW = false) const {
52 return Fold(ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW));
54 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
55 return Fold(ConstantExpr::getFAdd(LHS, RHS));
57 Constant *CreateSub(Constant *LHS, Constant *RHS,
58 bool HasNUW = false, bool HasNSW = false) const {
59 return Fold(ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW));
61 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
62 return Fold(ConstantExpr::getFSub(LHS, RHS));
64 Constant *CreateMul(Constant *LHS, Constant *RHS,
65 bool HasNUW = false, bool HasNSW = false) const {
66 return Fold(ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW));
68 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
69 return Fold(ConstantExpr::getFMul(LHS, RHS));
71 Constant *CreateUDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
72 return Fold(ConstantExpr::getUDiv(LHS, RHS, isExact));
74 Constant *CreateSDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
75 return Fold(ConstantExpr::getSDiv(LHS, RHS, isExact));
77 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
78 return Fold(ConstantExpr::getFDiv(LHS, RHS));
80 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
81 return Fold(ConstantExpr::getURem(LHS, RHS));
83 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
84 return Fold(ConstantExpr::getSRem(LHS, RHS));
86 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
87 return Fold(ConstantExpr::getFRem(LHS, RHS));
89 Constant *CreateShl(Constant *LHS, Constant *RHS,
90 bool HasNUW = false, bool HasNSW = false) const {
91 return Fold(ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW));
93 Constant *CreateLShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
94 return Fold(ConstantExpr::getLShr(LHS, RHS, isExact));
96 Constant *CreateAShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
97 return Fold(ConstantExpr::getAShr(LHS, RHS, isExact));
99 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
100 return Fold(ConstantExpr::getAnd(LHS, RHS));
102 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
103 return Fold(ConstantExpr::getOr(LHS, RHS));
105 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
106 return Fold(ConstantExpr::getXor(LHS, RHS));
109 Constant *CreateBinOp(Instruction::BinaryOps Opc,
110 Constant *LHS, Constant *RHS) const {
111 return Fold(ConstantExpr::get(Opc, LHS, RHS));
114 //===--------------------------------------------------------------------===//
116 //===--------------------------------------------------------------------===//
118 Constant *CreateNeg(Constant *C,
119 bool HasNUW = false, bool HasNSW = false) const {
120 return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
122 Constant *CreateFNeg(Constant *C) const {
123 return Fold(ConstantExpr::getFNeg(C));
125 Constant *CreateNot(Constant *C) const {
126 return Fold(ConstantExpr::getNot(C));
129 //===--------------------------------------------------------------------===//
130 // Memory Instructions
131 //===--------------------------------------------------------------------===//
133 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
134 unsigned NumIdx) const {
135 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
137 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
138 unsigned NumIdx) const {
139 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
142 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
143 unsigned NumIdx) const {
144 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
146 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
147 unsigned NumIdx) const {
148 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
151 //===--------------------------------------------------------------------===//
152 // Cast/Conversion Operators
153 //===--------------------------------------------------------------------===//
155 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
156 const Type *DestTy) const {
157 if (C->getType() == DestTy)
158 return C; // avoid calling Fold
159 return Fold(ConstantExpr::getCast(Op, C, DestTy));
161 Constant *CreateIntCast(Constant *C, const Type *DestTy,
162 bool isSigned) const {
163 if (C->getType() == DestTy)
164 return C; // avoid calling Fold
165 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
167 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
168 return ConstantExpr::getPointerCast(C, DestTy);
170 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
171 return CreateCast(Instruction::BitCast, C, DestTy);
173 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
174 return CreateCast(Instruction::IntToPtr, C, DestTy);
176 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
177 return CreateCast(Instruction::PtrToInt, C, DestTy);
179 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
180 if (C->getType() == DestTy)
181 return C; // avoid calling Fold
182 return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
184 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
185 if (C->getType() == DestTy)
186 return C; // avoid calling Fold
187 return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
189 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
190 if (C->getType() == DestTy)
191 return C; // avoid calling Fold
192 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
195 //===--------------------------------------------------------------------===//
196 // Compare Instructions
197 //===--------------------------------------------------------------------===//
199 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
200 Constant *RHS) const {
201 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
203 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
204 Constant *RHS) const {
205 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
208 //===--------------------------------------------------------------------===//
209 // Other Instructions
210 //===--------------------------------------------------------------------===//
212 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
213 return Fold(ConstantExpr::getSelect(C, True, False));
216 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
217 return Fold(ConstantExpr::getExtractElement(Vec, Idx));
220 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
221 Constant *Idx) const {
222 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
225 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
226 Constant *Mask) const {
227 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
230 Constant *CreateExtractValue(Constant *Agg,
231 ArrayRef<unsigned> IdxList) const {
232 return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
235 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
236 ArrayRef<unsigned> IdxList) const {
237 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));