LLVM API Documentation
00001 //====-- llvm/Support/TargetFolder.h - Constant folding helper -*- C++ -*-====// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the TargetFolder class, a helper for IRBuilder. 00011 // It provides IRBuilder with a set of methods for creating constants with 00012 // target dependent folding, in addition to the same target-independent 00013 // folding that the ConstantFolder class provides. For general constant 00014 // creation and folding, use ConstantExpr and the routines in 00015 // llvm/Analysis/ConstantFolding.h. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_SUPPORT_TARGETFOLDER_H 00020 #define LLVM_SUPPORT_TARGETFOLDER_H 00021 00022 #include "llvm/Constants.h" 00023 #include "llvm/Analysis/ConstantFolding.h" 00024 00025 namespace llvm { 00026 00027 class TargetData; 00028 class LLVMContext; 00029 00030 /// TargetFolder - Create constants with target dependent folding. 00031 class TargetFolder { 00032 const TargetData *TD; 00033 LLVMContext &Context; 00034 00035 /// Fold - Fold the constant using target specific information. 00036 Constant *Fold(Constant *C) const { 00037 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 00038 if (Constant *CF = ConstantFoldConstantExpression(CE, Context, TD)) 00039 return CF; 00040 return C; 00041 } 00042 00043 public: 00044 explicit TargetFolder(const TargetData *TheTD, LLVMContext &C) : 00045 TD(TheTD), Context(C) {} 00046 00047 //===--------------------------------------------------------------------===// 00048 // Binary Operators 00049 //===--------------------------------------------------------------------===// 00050 00051 Constant *CreateAdd(Constant *LHS, Constant *RHS) const { 00052 return Fold(ConstantExpr::getAdd(LHS, RHS)); 00053 } 00054 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const { 00055 return Fold(ConstantExpr::getNSWAdd(LHS, RHS)); 00056 } 00057 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { 00058 return Fold(ConstantExpr::getFAdd(LHS, RHS)); 00059 } 00060 Constant *CreateSub(Constant *LHS, Constant *RHS) const { 00061 return Fold(ConstantExpr::getSub(LHS, RHS)); 00062 } 00063 Constant *CreateFSub(Constant *LHS, Constant *RHS) const { 00064 return Fold(ConstantExpr::getFSub(LHS, RHS)); 00065 } 00066 Constant *CreateMul(Constant *LHS, Constant *RHS) const { 00067 return Fold(ConstantExpr::getMul(LHS, RHS)); 00068 } 00069 Constant *CreateFMul(Constant *LHS, Constant *RHS) const { 00070 return Fold(ConstantExpr::getFMul(LHS, RHS)); 00071 } 00072 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const { 00073 return Fold(ConstantExpr::getUDiv(LHS, RHS)); 00074 } 00075 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const { 00076 return Fold(ConstantExpr::getSDiv(LHS, RHS)); 00077 } 00078 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const { 00079 return Fold(ConstantExpr::getExactSDiv(LHS, RHS)); 00080 } 00081 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { 00082 return Fold(ConstantExpr::getFDiv(LHS, RHS)); 00083 } 00084 Constant *CreateURem(Constant *LHS, Constant *RHS) const { 00085 return Fold(ConstantExpr::getURem(LHS, RHS)); 00086 } 00087 Constant *CreateSRem(Constant *LHS, Constant *RHS) const { 00088 return Fold(ConstantExpr::getSRem(LHS, RHS)); 00089 } 00090 Constant *CreateFRem(Constant *LHS, Constant *RHS) const { 00091 return Fold(ConstantExpr::getFRem(LHS, RHS)); 00092 } 00093 Constant *CreateShl(Constant *LHS, Constant *RHS) const { 00094 return Fold(ConstantExpr::getShl(LHS, RHS)); 00095 } 00096 Constant *CreateLShr(Constant *LHS, Constant *RHS) const { 00097 return Fold(ConstantExpr::getLShr(LHS, RHS)); 00098 } 00099 Constant *CreateAShr(Constant *LHS, Constant *RHS) const { 00100 return Fold(ConstantExpr::getAShr(LHS, RHS)); 00101 } 00102 Constant *CreateAnd(Constant *LHS, Constant *RHS) const { 00103 return Fold(ConstantExpr::getAnd(LHS, RHS)); 00104 } 00105 Constant *CreateOr(Constant *LHS, Constant *RHS) const { 00106 return Fold(ConstantExpr::getOr(LHS, RHS)); 00107 } 00108 Constant *CreateXor(Constant *LHS, Constant *RHS) const { 00109 return Fold(ConstantExpr::getXor(LHS, RHS)); 00110 } 00111 00112 Constant *CreateBinOp(Instruction::BinaryOps Opc, 00113 Constant *LHS, Constant *RHS) const { 00114 return Fold(ConstantExpr::get(Opc, LHS, RHS)); 00115 } 00116 00117 //===--------------------------------------------------------------------===// 00118 // Unary Operators 00119 //===--------------------------------------------------------------------===// 00120 00121 Constant *CreateNeg(Constant *C) const { 00122 return Fold(ConstantExpr::getNeg(C)); 00123 } 00124 Constant *CreateFNeg(Constant *C) const { 00125 return Fold(ConstantExpr::getFNeg(C)); 00126 } 00127 Constant *CreateNot(Constant *C) const { 00128 return Fold(ConstantExpr::getNot(C)); 00129 } 00130 00131 //===--------------------------------------------------------------------===// 00132 // Memory Instructions 00133 //===--------------------------------------------------------------------===// 00134 00135 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList, 00136 unsigned NumIdx) const { 00137 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); 00138 } 00139 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList, 00140 unsigned NumIdx) const { 00141 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); 00142 } 00143 00144 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, 00145 unsigned NumIdx) const { 00146 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); 00147 } 00148 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, 00149 unsigned NumIdx) const { 00150 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); 00151 } 00152 00153 //===--------------------------------------------------------------------===// 00154 // Cast/Conversion Operators 00155 //===--------------------------------------------------------------------===// 00156 00157 Constant *CreateCast(Instruction::CastOps Op, Constant *C, 00158 const Type *DestTy) const { 00159 if (C->getType() == DestTy) 00160 return C; // avoid calling Fold 00161 return Fold(ConstantExpr::getCast(Op, C, DestTy)); 00162 } 00163 Constant *CreateIntCast(Constant *C, const Type *DestTy, 00164 bool isSigned) const { 00165 if (C->getType() == DestTy) 00166 return C; // avoid calling Fold 00167 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned)); 00168 } 00169 00170 Constant *CreateBitCast(Constant *C, const Type *DestTy) const { 00171 return CreateCast(Instruction::BitCast, C, DestTy); 00172 } 00173 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const { 00174 return CreateCast(Instruction::IntToPtr, C, DestTy); 00175 } 00176 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const { 00177 return CreateCast(Instruction::PtrToInt, C, DestTy); 00178 } 00179 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const { 00180 if (C->getType() == DestTy) 00181 return C; // avoid calling Fold 00182 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy)); 00183 } 00184 00185 //===--------------------------------------------------------------------===// 00186 // Compare Instructions 00187 //===--------------------------------------------------------------------===// 00188 00189 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS, 00190 Constant *RHS) const { 00191 return Fold(ConstantExpr::getCompare(P, LHS, RHS)); 00192 } 00193 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS, 00194 Constant *RHS) const { 00195 return Fold(ConstantExpr::getCompare(P, LHS, RHS)); 00196 } 00197 00198 //===--------------------------------------------------------------------===// 00199 // Other Instructions 00200 //===--------------------------------------------------------------------===// 00201 00202 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const { 00203 return Fold(ConstantExpr::getSelect(C, True, False)); 00204 } 00205 00206 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const { 00207 return Fold(ConstantExpr::getExtractElement(Vec, Idx)); 00208 } 00209 00210 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, 00211 Constant *Idx) const { 00212 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx)); 00213 } 00214 00215 Constant *CreateShuffleVector(Constant *V1, Constant *V2, 00216 Constant *Mask) const { 00217 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask)); 00218 } 00219 00220 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList, 00221 unsigned NumIdx) const { 00222 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx)); 00223 } 00224 00225 Constant *CreateInsertValue(Constant *Agg, Constant *Val, 00226 const unsigned *IdxList, unsigned NumIdx) const { 00227 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx)); 00228 } 00229 }; 00230 00231 } 00232 00233 #endif