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