ConstantFolder.h revision db125cfaf57cc83e7dd7453de2d509bc8efd0e5e
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 23namespace llvm { 24 25/// ConstantFolder - Create constants with minimum, target independent, folding. 26class ConstantFolder { 27public: 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 } 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 } 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 } 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 } 63 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { 64 return ConstantExpr::getFDiv(LHS, RHS); 65 } 66 Constant *CreateURem(Constant *LHS, Constant *RHS) const { 67 return ConstantExpr::getURem(LHS, RHS); 68 } 69 Constant *CreateSRem(Constant *LHS, Constant *RHS) const { 70 return ConstantExpr::getSRem(LHS, RHS); 71 } 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 } 87 Constant *CreateAnd(Constant *LHS, Constant *RHS) const { 88 return ConstantExpr::getAnd(LHS, RHS); 89 } 90 Constant *CreateOr(Constant *LHS, Constant *RHS) const { 91 return ConstantExpr::getOr(LHS, RHS); 92 } 93 Constant *CreateXor(Constant *LHS, Constant *RHS) const { 94 return ConstantExpr::getXor(LHS, RHS); 95 } 96 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 } 110 Constant *CreateFNeg(Constant *C) const { 111 return ConstantExpr::getFNeg(C); 112 } 113 Constant *CreateNot(Constant *C) const { 114 return ConstantExpr::getNot(C); 115 } 116 117 //===--------------------------------------------------------------------===// 118 // Memory Instructions 119 //===--------------------------------------------------------------------===// 120 121 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList, 122 unsigned NumIdx) const { 123 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); 124 } 125 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList, 126 unsigned NumIdx) const { 127 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); 128 } 129 130 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, 131 unsigned NumIdx) const { 132 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); 133 } 134 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, 135 unsigned NumIdx) const { 136 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); 137 } 138 139 //===--------------------------------------------------------------------===// 140 // Cast/Conversion Operators 141 //===--------------------------------------------------------------------===// 142 143 Constant *CreateCast(Instruction::CastOps Op, Constant *C, 144 Type *DestTy) const { 145 return ConstantExpr::getCast(Op, C, DestTy); 146 } 147 Constant *CreatePointerCast(Constant *C, Type *DestTy) const { 148 return ConstantExpr::getPointerCast(C, DestTy); 149 } 150 Constant *CreateIntCast(Constant *C, Type *DestTy, 151 bool isSigned) const { 152 return ConstantExpr::getIntegerCast(C, DestTy, isSigned); 153 } 154 Constant *CreateFPCast(Constant *C, Type *DestTy) const { 155 return ConstantExpr::getFPCast(C, DestTy); 156 } 157 158 Constant *CreateBitCast(Constant *C, Type *DestTy) const { 159 return CreateCast(Instruction::BitCast, C, DestTy); 160 } 161 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const { 162 return CreateCast(Instruction::IntToPtr, C, DestTy); 163 } 164 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const { 165 return CreateCast(Instruction::PtrToInt, C, DestTy); 166 } 167 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const { 168 return ConstantExpr::getZExtOrBitCast(C, DestTy); 169 } 170 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const { 171 return ConstantExpr::getSExtOrBitCast(C, DestTy); 172 } 173 174 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const { 175 return ConstantExpr::getTruncOrBitCast(C, DestTy); 176 } 177 178 //===--------------------------------------------------------------------===// 179 // Compare Instructions 180 //===--------------------------------------------------------------------===// 181 182 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS, 183 Constant *RHS) const { 184 return ConstantExpr::getCompare(P, LHS, RHS); 185 } 186 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS, 187 Constant *RHS) const { 188 return ConstantExpr::getCompare(P, LHS, RHS); 189 } 190 191 //===--------------------------------------------------------------------===// 192 // Other Instructions 193 //===--------------------------------------------------------------------===// 194 195 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const { 196 return ConstantExpr::getSelect(C, True, False); 197 } 198 199 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const { 200 return ConstantExpr::getExtractElement(Vec, Idx); 201 } 202 203 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, 204 Constant *Idx) const { 205 return ConstantExpr::getInsertElement(Vec, NewElt, Idx); 206 } 207 208 Constant *CreateShuffleVector(Constant *V1, Constant *V2, 209 Constant *Mask) const { 210 return ConstantExpr::getShuffleVector(V1, V2, Mask); 211 } 212 213 Constant *CreateExtractValue(Constant *Agg, 214 ArrayRef<unsigned> IdxList) const { 215 return ConstantExpr::getExtractValue(Agg, IdxList); 216 } 217 218 Constant *CreateInsertValue(Constant *Agg, Constant *Val, 219 ArrayRef<unsigned> IdxList) const { 220 return ConstantExpr::getInsertValue(Agg, Val, IdxList); 221 } 222}; 223 224} 225 226#endif 227