1 //===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ConstantFolder class, a helper for IRBuilder. 11 // It provides IRBuilder with a set of methods for creating constants 12 // with minimal folding. For general constant creation and folding, 13 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_SUPPORT_CONSTANTFOLDER_H 18 #define LLVM_SUPPORT_CONSTANTFOLDER_H 19 20 #include "llvm/Constants.h" 21 #include "llvm/InstrTypes.h" 22 23 namespace llvm { 24 25 class LLVMContext; 26 27 /// ConstantFolder - Create constants with minimum, target independent, folding. 28 class ConstantFolder { 29 public: ConstantFolder(LLVMContext &)30 explicit ConstantFolder(LLVMContext &) {} 31 32 //===--------------------------------------------------------------------===// 33 // Binary Operators 34 //===--------------------------------------------------------------------===// 35 CreateAdd(Constant * LHS,Constant * RHS)36 Constant *CreateAdd(Constant *LHS, Constant *RHS) const { 37 return ConstantExpr::getAdd(LHS, RHS); 38 } CreateNSWAdd(Constant * LHS,Constant * RHS)39 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const { 40 return ConstantExpr::getNSWAdd(LHS, RHS); 41 } CreateNUWAdd(Constant * LHS,Constant * RHS)42 Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const { 43 return ConstantExpr::getNUWAdd(LHS, RHS); 44 } CreateFAdd(Constant * LHS,Constant * RHS)45 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { 46 return ConstantExpr::getFAdd(LHS, RHS); 47 } CreateSub(Constant * LHS,Constant * RHS)48 Constant *CreateSub(Constant *LHS, Constant *RHS) const { 49 return ConstantExpr::getSub(LHS, RHS); 50 } CreateNSWSub(Constant * LHS,Constant * RHS)51 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const { 52 return ConstantExpr::getNSWSub(LHS, RHS); 53 } CreateNUWSub(Constant * LHS,Constant * RHS)54 Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const { 55 return ConstantExpr::getNUWSub(LHS, RHS); 56 } CreateFSub(Constant * LHS,Constant * RHS)57 Constant *CreateFSub(Constant *LHS, Constant *RHS) const { 58 return ConstantExpr::getFSub(LHS, RHS); 59 } CreateMul(Constant * LHS,Constant * RHS)60 Constant *CreateMul(Constant *LHS, Constant *RHS) const { 61 return ConstantExpr::getMul(LHS, RHS); 62 } CreateNSWMul(Constant * LHS,Constant * RHS)63 Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const { 64 return ConstantExpr::getNSWMul(LHS, RHS); 65 } CreateNUWMul(Constant * LHS,Constant * RHS)66 Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const { 67 return ConstantExpr::getNUWMul(LHS, RHS); 68 } CreateFMul(Constant * LHS,Constant * RHS)69 Constant *CreateFMul(Constant *LHS, Constant *RHS) const { 70 return ConstantExpr::getFMul(LHS, RHS); 71 } CreateUDiv(Constant * LHS,Constant * RHS)72 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const { 73 return ConstantExpr::getUDiv(LHS, RHS); 74 } CreateSDiv(Constant * LHS,Constant * RHS)75 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const { 76 return ConstantExpr::getSDiv(LHS, RHS); 77 } CreateExactSDiv(Constant * LHS,Constant * RHS)78 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const { 79 return ConstantExpr::getExactSDiv(LHS, RHS); 80 } CreateFDiv(Constant * LHS,Constant * RHS)81 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { 82 return ConstantExpr::getFDiv(LHS, RHS); 83 } CreateURem(Constant * LHS,Constant * RHS)84 Constant *CreateURem(Constant *LHS, Constant *RHS) const { 85 return ConstantExpr::getURem(LHS, RHS); 86 } CreateSRem(Constant * LHS,Constant * RHS)87 Constant *CreateSRem(Constant *LHS, Constant *RHS) const { 88 return ConstantExpr::getSRem(LHS, RHS); 89 } CreateFRem(Constant * LHS,Constant * RHS)90 Constant *CreateFRem(Constant *LHS, Constant *RHS) const { 91 return ConstantExpr::getFRem(LHS, RHS); 92 } CreateShl(Constant * LHS,Constant * RHS)93 Constant *CreateShl(Constant *LHS, Constant *RHS) const { 94 return ConstantExpr::getShl(LHS, RHS); 95 } CreateLShr(Constant * LHS,Constant * RHS)96 Constant *CreateLShr(Constant *LHS, Constant *RHS) const { 97 return ConstantExpr::getLShr(LHS, RHS); 98 } CreateAShr(Constant * LHS,Constant * RHS)99 Constant *CreateAShr(Constant *LHS, Constant *RHS) const { 100 return ConstantExpr::getAShr(LHS, RHS); 101 } CreateAnd(Constant * LHS,Constant * RHS)102 Constant *CreateAnd(Constant *LHS, Constant *RHS) const { 103 return ConstantExpr::getAnd(LHS, RHS); 104 } CreateOr(Constant * LHS,Constant * RHS)105 Constant *CreateOr(Constant *LHS, Constant *RHS) const { 106 return ConstantExpr::getOr(LHS, RHS); 107 } CreateXor(Constant * LHS,Constant * RHS)108 Constant *CreateXor(Constant *LHS, Constant *RHS) const { 109 return ConstantExpr::getXor(LHS, RHS); 110 } 111 CreateBinOp(Instruction::BinaryOps Opc,Constant * LHS,Constant * RHS)112 Constant *CreateBinOp(Instruction::BinaryOps Opc, 113 Constant *LHS, Constant *RHS) const { 114 return ConstantExpr::get(Opc, LHS, RHS); 115 } 116 117 //===--------------------------------------------------------------------===// 118 // Unary Operators 119 //===--------------------------------------------------------------------===// 120 CreateNeg(Constant * C)121 Constant *CreateNeg(Constant *C) const { 122 return ConstantExpr::getNeg(C); 123 } CreateNSWNeg(Constant * C)124 Constant *CreateNSWNeg(Constant *C) const { 125 return ConstantExpr::getNSWNeg(C); 126 } CreateNUWNeg(Constant * C)127 Constant *CreateNUWNeg(Constant *C) const { 128 return ConstantExpr::getNUWNeg(C); 129 } CreateFNeg(Constant * C)130 Constant *CreateFNeg(Constant *C) const { 131 return ConstantExpr::getFNeg(C); 132 } CreateNot(Constant * C)133 Constant *CreateNot(Constant *C) const { 134 return ConstantExpr::getNot(C); 135 } 136 137 //===--------------------------------------------------------------------===// 138 // Memory Instructions 139 //===--------------------------------------------------------------------===// 140 CreateGetElementPtr(Constant * C,Constant * const * IdxList,unsigned NumIdx)141 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList, 142 unsigned NumIdx) const { 143 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); 144 } CreateGetElementPtr(Constant * C,Value * const * IdxList,unsigned NumIdx)145 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList, 146 unsigned NumIdx) const { 147 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); 148 } 149 CreateInBoundsGetElementPtr(Constant * C,Constant * const * IdxList,unsigned NumIdx)150 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, 151 unsigned NumIdx) const { 152 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); 153 } CreateInBoundsGetElementPtr(Constant * C,Value * const * IdxList,unsigned NumIdx)154 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, 155 unsigned NumIdx) const { 156 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); 157 } 158 159 //===--------------------------------------------------------------------===// 160 // Cast/Conversion Operators 161 //===--------------------------------------------------------------------===// 162 CreateCast(Instruction::CastOps Op,Constant * C,const Type * DestTy)163 Constant *CreateCast(Instruction::CastOps Op, Constant *C, 164 const Type *DestTy) const { 165 return ConstantExpr::getCast(Op, C, DestTy); 166 } CreatePointerCast(Constant * C,const Type * DestTy)167 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const { 168 return ConstantExpr::getPointerCast(C, DestTy); 169 } CreateIntCast(Constant * C,const Type * DestTy,bool isSigned)170 Constant *CreateIntCast(Constant *C, const Type *DestTy, 171 bool isSigned) const { 172 return ConstantExpr::getIntegerCast(C, DestTy, isSigned); 173 } CreateFPCast(Constant * C,const Type * DestTy)174 Constant *CreateFPCast(Constant *C, const Type *DestTy) const { 175 return ConstantExpr::getFPCast(C, DestTy); 176 } 177 CreateBitCast(Constant * C,const Type * DestTy)178 Constant *CreateBitCast(Constant *C, const Type *DestTy) const { 179 return CreateCast(Instruction::BitCast, C, DestTy); 180 } CreateIntToPtr(Constant * C,const Type * DestTy)181 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const { 182 return CreateCast(Instruction::IntToPtr, C, DestTy); 183 } CreatePtrToInt(Constant * C,const Type * DestTy)184 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const { 185 return CreateCast(Instruction::PtrToInt, C, DestTy); 186 } CreateZExtOrBitCast(Constant * C,const Type * DestTy)187 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const { 188 return ConstantExpr::getZExtOrBitCast(C, DestTy); 189 } CreateSExtOrBitCast(Constant * C,const Type * DestTy)190 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const { 191 return ConstantExpr::getSExtOrBitCast(C, DestTy); 192 } 193 CreateTruncOrBitCast(Constant * C,const Type * DestTy)194 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const { 195 return ConstantExpr::getTruncOrBitCast(C, DestTy); 196 } 197 198 //===--------------------------------------------------------------------===// 199 // Compare Instructions 200 //===--------------------------------------------------------------------===// 201 CreateICmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)202 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS, 203 Constant *RHS) const { 204 return ConstantExpr::getCompare(P, LHS, RHS); 205 } CreateFCmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)206 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS, 207 Constant *RHS) const { 208 return ConstantExpr::getCompare(P, LHS, RHS); 209 } 210 211 //===--------------------------------------------------------------------===// 212 // Other Instructions 213 //===--------------------------------------------------------------------===// 214 CreateSelect(Constant * C,Constant * True,Constant * False)215 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const { 216 return ConstantExpr::getSelect(C, True, False); 217 } 218 CreateExtractElement(Constant * Vec,Constant * Idx)219 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const { 220 return ConstantExpr::getExtractElement(Vec, Idx); 221 } 222 CreateInsertElement(Constant * Vec,Constant * NewElt,Constant * Idx)223 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, 224 Constant *Idx) const { 225 return ConstantExpr::getInsertElement(Vec, NewElt, Idx); 226 } 227 CreateShuffleVector(Constant * V1,Constant * V2,Constant * Mask)228 Constant *CreateShuffleVector(Constant *V1, Constant *V2, 229 Constant *Mask) const { 230 return ConstantExpr::getShuffleVector(V1, V2, Mask); 231 } 232 CreateExtractValue(Constant * Agg,const unsigned * IdxList,unsigned NumIdx)233 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList, 234 unsigned NumIdx) const { 235 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx); 236 } 237 CreateInsertValue(Constant * Agg,Constant * Val,const unsigned * IdxList,unsigned NumIdx)238 Constant *CreateInsertValue(Constant *Agg, Constant *Val, 239 const unsigned *IdxList, unsigned NumIdx) const { 240 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx); 241 } 242 }; 243 244 } 245 246 #endif 247