1 //===- 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_IR_CONSTANTFOLDER_H 18 #define LLVM_IR_CONSTANTFOLDER_H 19 20 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/InstrTypes.h" 22 23 namespace llvm { 24 25 /// ConstantFolder - Create constants with minimum, target independent, folding. 26 class ConstantFolder { 27 public: ConstantFolder()28 explicit ConstantFolder() {} 29 30 //===--------------------------------------------------------------------===// 31 // Binary Operators 32 //===--------------------------------------------------------------------===// 33 34 Constant *CreateAdd(Constant *LHS, Constant *RHS, 35 bool HasNUW = false, bool HasNSW = false) const { 36 return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW); 37 } CreateFAdd(Constant * LHS,Constant * RHS)38 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { 39 return ConstantExpr::getFAdd(LHS, RHS); 40 } 41 Constant *CreateSub(Constant *LHS, Constant *RHS, 42 bool HasNUW = false, bool HasNSW = false) const { 43 return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW); 44 } CreateFSub(Constant * LHS,Constant * RHS)45 Constant *CreateFSub(Constant *LHS, Constant *RHS) const { 46 return ConstantExpr::getFSub(LHS, RHS); 47 } 48 Constant *CreateMul(Constant *LHS, Constant *RHS, 49 bool HasNUW = false, bool HasNSW = false) const { 50 return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW); 51 } CreateFMul(Constant * LHS,Constant * RHS)52 Constant *CreateFMul(Constant *LHS, Constant *RHS) const { 53 return ConstantExpr::getFMul(LHS, RHS); 54 } 55 Constant *CreateUDiv(Constant *LHS, Constant *RHS, 56 bool isExact = false) const { 57 return ConstantExpr::getUDiv(LHS, RHS, isExact); 58 } 59 Constant *CreateSDiv(Constant *LHS, Constant *RHS, 60 bool isExact = false) const { 61 return ConstantExpr::getSDiv(LHS, RHS, isExact); 62 } CreateFDiv(Constant * LHS,Constant * RHS)63 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { 64 return ConstantExpr::getFDiv(LHS, RHS); 65 } CreateURem(Constant * LHS,Constant * RHS)66 Constant *CreateURem(Constant *LHS, Constant *RHS) const { 67 return ConstantExpr::getURem(LHS, RHS); 68 } CreateSRem(Constant * LHS,Constant * RHS)69 Constant *CreateSRem(Constant *LHS, Constant *RHS) const { 70 return ConstantExpr::getSRem(LHS, RHS); 71 } CreateFRem(Constant * LHS,Constant * RHS)72 Constant *CreateFRem(Constant *LHS, Constant *RHS) const { 73 return ConstantExpr::getFRem(LHS, RHS); 74 } 75 Constant *CreateShl(Constant *LHS, Constant *RHS, 76 bool HasNUW = false, bool HasNSW = false) const { 77 return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW); 78 } 79 Constant *CreateLShr(Constant *LHS, Constant *RHS, 80 bool isExact = false) const { 81 return ConstantExpr::getLShr(LHS, RHS, isExact); 82 } 83 Constant *CreateAShr(Constant *LHS, Constant *RHS, 84 bool isExact = false) const { 85 return ConstantExpr::getAShr(LHS, RHS, isExact); 86 } CreateAnd(Constant * LHS,Constant * RHS)87 Constant *CreateAnd(Constant *LHS, Constant *RHS) const { 88 return ConstantExpr::getAnd(LHS, RHS); 89 } CreateOr(Constant * LHS,Constant * RHS)90 Constant *CreateOr(Constant *LHS, Constant *RHS) const { 91 return ConstantExpr::getOr(LHS, RHS); 92 } CreateXor(Constant * LHS,Constant * RHS)93 Constant *CreateXor(Constant *LHS, Constant *RHS) const { 94 return ConstantExpr::getXor(LHS, RHS); 95 } 96 CreateBinOp(Instruction::BinaryOps Opc,Constant * LHS,Constant * RHS)97 Constant *CreateBinOp(Instruction::BinaryOps Opc, 98 Constant *LHS, Constant *RHS) const { 99 return ConstantExpr::get(Opc, LHS, RHS); 100 } 101 102 //===--------------------------------------------------------------------===// 103 // Unary Operators 104 //===--------------------------------------------------------------------===// 105 106 Constant *CreateNeg(Constant *C, 107 bool HasNUW = false, bool HasNSW = false) const { 108 return ConstantExpr::getNeg(C, HasNUW, HasNSW); 109 } CreateFNeg(Constant * C)110 Constant *CreateFNeg(Constant *C) const { 111 return ConstantExpr::getFNeg(C); 112 } CreateNot(Constant * C)113 Constant *CreateNot(Constant *C) const { 114 return ConstantExpr::getNot(C); 115 } 116 117 //===--------------------------------------------------------------------===// 118 // Memory Instructions 119 //===--------------------------------------------------------------------===// 120 CreateGetElementPtr(Constant * C,ArrayRef<Constant * > IdxList)121 Constant *CreateGetElementPtr(Constant *C, 122 ArrayRef<Constant *> IdxList) const { 123 return ConstantExpr::getGetElementPtr(C, IdxList); 124 } CreateGetElementPtr(Constant * C,Constant * Idx)125 Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const { 126 // This form of the function only exists to avoid ambiguous overload 127 // warnings about whether to convert Idx to ArrayRef<Constant *> or 128 // ArrayRef<Value *>. 129 return ConstantExpr::getGetElementPtr(C, Idx); 130 } CreateGetElementPtr(Constant * C,ArrayRef<Value * > IdxList)131 Constant *CreateGetElementPtr(Constant *C, 132 ArrayRef<Value *> IdxList) const { 133 return ConstantExpr::getGetElementPtr(C, IdxList); 134 } 135 CreateInBoundsGetElementPtr(Constant * C,ArrayRef<Constant * > IdxList)136 Constant *CreateInBoundsGetElementPtr(Constant *C, 137 ArrayRef<Constant *> IdxList) const { 138 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList); 139 } CreateInBoundsGetElementPtr(Constant * C,Constant * Idx)140 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const { 141 // This form of the function only exists to avoid ambiguous overload 142 // warnings about whether to convert Idx to ArrayRef<Constant *> or 143 // ArrayRef<Value *>. 144 return ConstantExpr::getInBoundsGetElementPtr(C, Idx); 145 } CreateInBoundsGetElementPtr(Constant * C,ArrayRef<Value * > IdxList)146 Constant *CreateInBoundsGetElementPtr(Constant *C, 147 ArrayRef<Value *> IdxList) const { 148 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList); 149 } 150 151 //===--------------------------------------------------------------------===// 152 // Cast/Conversion Operators 153 //===--------------------------------------------------------------------===// 154 CreateCast(Instruction::CastOps Op,Constant * C,Type * DestTy)155 Constant *CreateCast(Instruction::CastOps Op, Constant *C, 156 Type *DestTy) const { 157 return ConstantExpr::getCast(Op, C, DestTy); 158 } CreatePointerCast(Constant * C,Type * DestTy)159 Constant *CreatePointerCast(Constant *C, Type *DestTy) const { 160 return ConstantExpr::getPointerCast(C, DestTy); 161 } 162 CreatePointerBitCastOrAddrSpaceCast(Constant * C,Type * DestTy)163 Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C, 164 Type *DestTy) const { 165 return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy); 166 } 167 CreateIntCast(Constant * C,Type * DestTy,bool isSigned)168 Constant *CreateIntCast(Constant *C, Type *DestTy, 169 bool isSigned) const { 170 return ConstantExpr::getIntegerCast(C, DestTy, isSigned); 171 } CreateFPCast(Constant * C,Type * DestTy)172 Constant *CreateFPCast(Constant *C, Type *DestTy) const { 173 return ConstantExpr::getFPCast(C, DestTy); 174 } 175 CreateBitCast(Constant * C,Type * DestTy)176 Constant *CreateBitCast(Constant *C, Type *DestTy) const { 177 return CreateCast(Instruction::BitCast, C, DestTy); 178 } CreateIntToPtr(Constant * C,Type * DestTy)179 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const { 180 return CreateCast(Instruction::IntToPtr, C, DestTy); 181 } CreatePtrToInt(Constant * C,Type * DestTy)182 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const { 183 return CreateCast(Instruction::PtrToInt, C, DestTy); 184 } CreateZExtOrBitCast(Constant * C,Type * DestTy)185 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const { 186 return ConstantExpr::getZExtOrBitCast(C, DestTy); 187 } CreateSExtOrBitCast(Constant * C,Type * DestTy)188 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const { 189 return ConstantExpr::getSExtOrBitCast(C, DestTy); 190 } 191 CreateTruncOrBitCast(Constant * C,Type * DestTy)192 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const { 193 return ConstantExpr::getTruncOrBitCast(C, DestTy); 194 } 195 196 //===--------------------------------------------------------------------===// 197 // Compare Instructions 198 //===--------------------------------------------------------------------===// 199 CreateICmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)200 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS, 201 Constant *RHS) const { 202 return ConstantExpr::getCompare(P, LHS, RHS); 203 } CreateFCmp(CmpInst::Predicate P,Constant * LHS,Constant * RHS)204 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS, 205 Constant *RHS) const { 206 return ConstantExpr::getCompare(P, LHS, RHS); 207 } 208 209 //===--------------------------------------------------------------------===// 210 // Other Instructions 211 //===--------------------------------------------------------------------===// 212 CreateSelect(Constant * C,Constant * True,Constant * False)213 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const { 214 return ConstantExpr::getSelect(C, True, False); 215 } 216 CreateExtractElement(Constant * Vec,Constant * Idx)217 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const { 218 return ConstantExpr::getExtractElement(Vec, Idx); 219 } 220 CreateInsertElement(Constant * Vec,Constant * NewElt,Constant * Idx)221 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, 222 Constant *Idx) const { 223 return ConstantExpr::getInsertElement(Vec, NewElt, Idx); 224 } 225 CreateShuffleVector(Constant * V1,Constant * V2,Constant * Mask)226 Constant *CreateShuffleVector(Constant *V1, Constant *V2, 227 Constant *Mask) const { 228 return ConstantExpr::getShuffleVector(V1, V2, Mask); 229 } 230 CreateExtractValue(Constant * Agg,ArrayRef<unsigned> IdxList)231 Constant *CreateExtractValue(Constant *Agg, 232 ArrayRef<unsigned> IdxList) const { 233 return ConstantExpr::getExtractValue(Agg, IdxList); 234 } 235 CreateInsertValue(Constant * Agg,Constant * Val,ArrayRef<unsigned> IdxList)236 Constant *CreateInsertValue(Constant *Agg, Constant *Val, 237 ArrayRef<unsigned> IdxList) const { 238 return ConstantExpr::getInsertValue(Agg, Val, IdxList); 239 } 240 }; 241 242 } 243 244 #endif 245