TargetFolder.h revision 1dbf0df996bba398a70abccc714b1a9652330014
1//====-- llvm/Support/TargetFolder.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 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. 16// 17//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_SUPPORT_TARGETFOLDER_H 20#define LLVM_SUPPORT_TARGETFOLDER_H 21 22#include "llvm/Constants.h" 23#include "llvm/InstrTypes.h" 24#include "llvm/Analysis/ConstantFolding.h" 25 26namespace llvm { 27 28class TargetData; 29 30/// TargetFolder - Create constants with target dependent folding. 31class TargetFolder { 32 const TargetData *TD; 33 34 /// Fold - Fold the constant using target specific information. 35 Constant *Fold(Constant *C) const { 36 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 37 if (Constant *CF = ConstantFoldConstantExpression(CE, TD)) 38 return CF; 39 return C; 40 } 41 42public: 43 explicit TargetFolder(const TargetData *TheTD) : TD(TheTD) {} 44 45 //===--------------------------------------------------------------------===// 46 // Binary Operators 47 //===--------------------------------------------------------------------===// 48 49 Constant *CreateAdd(Constant *LHS, Constant *RHS) const { 50 return Fold(ConstantExpr::getAdd(LHS, RHS)); 51 } 52 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const { 53 return Fold(ConstantExpr::getNSWAdd(LHS, RHS)); 54 } 55 Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const { 56 return Fold(ConstantExpr::getNUWAdd(LHS, RHS)); 57 } 58 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { 59 return Fold(ConstantExpr::getFAdd(LHS, RHS)); 60 } 61 Constant *CreateSub(Constant *LHS, Constant *RHS) const { 62 return Fold(ConstantExpr::getSub(LHS, RHS)); 63 } 64 Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const { 65 return Fold(ConstantExpr::getNSWSub(LHS, RHS)); 66 } 67 Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const { 68 return Fold(ConstantExpr::getNUWSub(LHS, RHS)); 69 } 70 Constant *CreateFSub(Constant *LHS, Constant *RHS) const { 71 return Fold(ConstantExpr::getFSub(LHS, RHS)); 72 } 73 Constant *CreateMul(Constant *LHS, Constant *RHS) const { 74 return Fold(ConstantExpr::getMul(LHS, RHS)); 75 } 76 Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const { 77 return Fold(ConstantExpr::getNSWMul(LHS, RHS)); 78 } 79 Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const { 80 return Fold(ConstantExpr::getNUWMul(LHS, RHS)); 81 } 82 Constant *CreateFMul(Constant *LHS, Constant *RHS) const { 83 return Fold(ConstantExpr::getFMul(LHS, RHS)); 84 } 85 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const { 86 return Fold(ConstantExpr::getUDiv(LHS, RHS)); 87 } 88 Constant *CreateExactUDiv(Constant *LHS, Constant *RHS) const { 89 return Fold(ConstantExpr::getExactUDiv(LHS, RHS)); 90 } 91 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const { 92 return Fold(ConstantExpr::getSDiv(LHS, RHS)); 93 } 94 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const { 95 return Fold(ConstantExpr::getExactSDiv(LHS, RHS)); 96 } 97 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { 98 return Fold(ConstantExpr::getFDiv(LHS, RHS)); 99 } 100 Constant *CreateURem(Constant *LHS, Constant *RHS) const { 101 return Fold(ConstantExpr::getURem(LHS, RHS)); 102 } 103 Constant *CreateSRem(Constant *LHS, Constant *RHS) const { 104 return Fold(ConstantExpr::getSRem(LHS, RHS)); 105 } 106 Constant *CreateFRem(Constant *LHS, Constant *RHS) const { 107 return Fold(ConstantExpr::getFRem(LHS, RHS)); 108 } 109 Constant *CreateShl(Constant *LHS, Constant *RHS) const { 110 return Fold(ConstantExpr::getShl(LHS, RHS)); 111 } 112 Constant *CreateLShr(Constant *LHS, Constant *RHS) const { 113 return Fold(ConstantExpr::getLShr(LHS, RHS)); 114 } 115 Constant *CreateAShr(Constant *LHS, Constant *RHS) const { 116 return Fold(ConstantExpr::getAShr(LHS, RHS)); 117 } 118 Constant *CreateAnd(Constant *LHS, Constant *RHS) const { 119 return Fold(ConstantExpr::getAnd(LHS, RHS)); 120 } 121 Constant *CreateOr(Constant *LHS, Constant *RHS) const { 122 return Fold(ConstantExpr::getOr(LHS, RHS)); 123 } 124 Constant *CreateXor(Constant *LHS, Constant *RHS) const { 125 return Fold(ConstantExpr::getXor(LHS, RHS)); 126 } 127 128 Constant *CreateBinOp(Instruction::BinaryOps Opc, 129 Constant *LHS, Constant *RHS) const { 130 return Fold(ConstantExpr::get(Opc, LHS, RHS)); 131 } 132 133 //===--------------------------------------------------------------------===// 134 // Unary Operators 135 //===--------------------------------------------------------------------===// 136 137 Constant *CreateNeg(Constant *C) const { 138 return Fold(ConstantExpr::getNeg(C)); 139 } 140 Constant *CreateNSWNeg(Constant *C) const { 141 return Fold(ConstantExpr::getNSWNeg(C)); 142 } 143 Constant *CreateNUWNeg(Constant *C) const { 144 return Fold(ConstantExpr::getNUWNeg(C)); 145 } 146 Constant *CreateFNeg(Constant *C) const { 147 return Fold(ConstantExpr::getFNeg(C)); 148 } 149 Constant *CreateNot(Constant *C) const { 150 return Fold(ConstantExpr::getNot(C)); 151 } 152 153 //===--------------------------------------------------------------------===// 154 // Memory Instructions 155 //===--------------------------------------------------------------------===// 156 157 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList, 158 unsigned NumIdx) const { 159 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); 160 } 161 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList, 162 unsigned NumIdx) const { 163 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); 164 } 165 166 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, 167 unsigned NumIdx) const { 168 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); 169 } 170 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, 171 unsigned NumIdx) const { 172 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); 173 } 174 175 //===--------------------------------------------------------------------===// 176 // Cast/Conversion Operators 177 //===--------------------------------------------------------------------===// 178 179 Constant *CreateCast(Instruction::CastOps Op, Constant *C, 180 const Type *DestTy) const { 181 if (C->getType() == DestTy) 182 return C; // avoid calling Fold 183 return Fold(ConstantExpr::getCast(Op, C, DestTy)); 184 } 185 Constant *CreateIntCast(Constant *C, const Type *DestTy, 186 bool isSigned) const { 187 if (C->getType() == DestTy) 188 return C; // avoid calling Fold 189 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned)); 190 } 191 Constant *CreatePointerCast(Constant *C, const Type *DestTy) const { 192 return ConstantExpr::getPointerCast(C, DestTy); 193 } 194 Constant *CreateBitCast(Constant *C, const Type *DestTy) const { 195 return CreateCast(Instruction::BitCast, C, DestTy); 196 } 197 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const { 198 return CreateCast(Instruction::IntToPtr, C, DestTy); 199 } 200 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const { 201 return CreateCast(Instruction::PtrToInt, C, DestTy); 202 } 203 Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const { 204 if (C->getType() == DestTy) 205 return C; // avoid calling Fold 206 return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy)); 207 } 208 Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const { 209 if (C->getType() == DestTy) 210 return C; // avoid calling Fold 211 return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy)); 212 } 213 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const { 214 if (C->getType() == DestTy) 215 return C; // avoid calling Fold 216 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy)); 217 } 218 219 //===--------------------------------------------------------------------===// 220 // Compare Instructions 221 //===--------------------------------------------------------------------===// 222 223 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS, 224 Constant *RHS) const { 225 return Fold(ConstantExpr::getCompare(P, LHS, RHS)); 226 } 227 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS, 228 Constant *RHS) const { 229 return Fold(ConstantExpr::getCompare(P, LHS, RHS)); 230 } 231 232 //===--------------------------------------------------------------------===// 233 // Other Instructions 234 //===--------------------------------------------------------------------===// 235 236 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const { 237 return Fold(ConstantExpr::getSelect(C, True, False)); 238 } 239 240 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const { 241 return Fold(ConstantExpr::getExtractElement(Vec, Idx)); 242 } 243 244 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt, 245 Constant *Idx) const { 246 return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx)); 247 } 248 249 Constant *CreateShuffleVector(Constant *V1, Constant *V2, 250 Constant *Mask) const { 251 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask)); 252 } 253 254 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList, 255 unsigned NumIdx) const { 256 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx)); 257 } 258 259 Constant *CreateInsertValue(Constant *Agg, Constant *Val, 260 const unsigned *IdxList, unsigned NumIdx) const { 261 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx)); 262 } 263}; 264 265} 266 267#endif 268