1193323Sed//======-- llvm/Support/NoFolder.h - Constant folding helper -*- C++ -*-======// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the NoFolder class, a helper for IRBuilder. It provides 11193323Sed// IRBuilder with a set of methods for creating unfolded constants. This is 12193323Sed// useful for learners trying to understand how LLVM IR works, and who don't 13193323Sed// want details to be hidden by the constant folder. For general constant 14193323Sed// creation and folding, use ConstantExpr and the routines in 15193323Sed// llvm/Analysis/ConstantFolding.h. 16193323Sed// 17193323Sed// Note: since it is not actually possible to create unfolded constants, this 18218893Sdim// class returns instructions rather than constants. 19193323Sed// 20193323Sed//===----------------------------------------------------------------------===// 21193323Sed 22193323Sed#ifndef LLVM_SUPPORT_NOFOLDER_H 23193323Sed#define LLVM_SUPPORT_NOFOLDER_H 24193323Sed 25224145Sdim#include "llvm/ADT/ArrayRef.h" 26252723Sdim#include "llvm/IR/Constants.h" 27252723Sdim#include "llvm/IR/Instructions.h" 28193323Sed 29193323Sednamespace llvm { 30193323Sed 31218893Sdim/// NoFolder - Create "constants" (actually, instructions) with no folding. 32193323Sedclass NoFolder { 33193323Sedpublic: 34221345Sdim explicit NoFolder() {} 35193323Sed 36193323Sed //===--------------------------------------------------------------------===// 37193323Sed // Binary Operators 38193323Sed //===--------------------------------------------------------------------===// 39193323Sed 40219077Sdim Instruction *CreateAdd(Constant *LHS, Constant *RHS, 41219077Sdim bool HasNUW = false, bool HasNSW = false) const { 42219077Sdim BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS); 43219077Sdim if (HasNUW) BO->setHasNoUnsignedWrap(); 44219077Sdim if (HasNSW) BO->setHasNoSignedWrap(); 45219077Sdim return BO; 46193323Sed } 47218893Sdim Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const { 48198090Srdivacky return BinaryOperator::CreateNSWAdd(LHS, RHS); 49198090Srdivacky } 50218893Sdim Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const { 51203954Srdivacky return BinaryOperator::CreateNUWAdd(LHS, RHS); 52203954Srdivacky } 53218893Sdim Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const { 54193574Sed return BinaryOperator::CreateFAdd(LHS, RHS); 55193574Sed } 56219077Sdim Instruction *CreateSub(Constant *LHS, Constant *RHS, 57219077Sdim bool HasNUW = false, bool HasNSW = false) const { 58219077Sdim BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS); 59219077Sdim if (HasNUW) BO->setHasNoUnsignedWrap(); 60219077Sdim if (HasNSW) BO->setHasNoSignedWrap(); 61219077Sdim return BO; 62193323Sed } 63218893Sdim Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const { 64198090Srdivacky return BinaryOperator::CreateNSWSub(LHS, RHS); 65198090Srdivacky } 66218893Sdim Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const { 67203954Srdivacky return BinaryOperator::CreateNUWSub(LHS, RHS); 68203954Srdivacky } 69218893Sdim Instruction *CreateFSub(Constant *LHS, Constant *RHS) const { 70193574Sed return BinaryOperator::CreateFSub(LHS, RHS); 71193574Sed } 72219077Sdim Instruction *CreateMul(Constant *LHS, Constant *RHS, 73219077Sdim bool HasNUW = false, bool HasNSW = false) const { 74219077Sdim BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS); 75219077Sdim if (HasNUW) BO->setHasNoUnsignedWrap(); 76219077Sdim if (HasNSW) BO->setHasNoSignedWrap(); 77219077Sdim return BO; 78193323Sed } 79218893Sdim Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const { 80201360Srdivacky return BinaryOperator::CreateNSWMul(LHS, RHS); 81201360Srdivacky } 82218893Sdim Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const { 83203954Srdivacky return BinaryOperator::CreateNUWMul(LHS, RHS); 84203954Srdivacky } 85218893Sdim Instruction *CreateFMul(Constant *LHS, Constant *RHS) const { 86193574Sed return BinaryOperator::CreateFMul(LHS, RHS); 87193574Sed } 88219077Sdim Instruction *CreateUDiv(Constant *LHS, Constant *RHS, 89219077Sdim bool isExact = false) const { 90219077Sdim if (!isExact) 91219077Sdim return BinaryOperator::CreateUDiv(LHS, RHS); 92219077Sdim return BinaryOperator::CreateExactUDiv(LHS, RHS); 93193323Sed } 94218893Sdim Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const { 95218893Sdim return BinaryOperator::CreateExactUDiv(LHS, RHS); 96218893Sdim } 97219077Sdim Instruction *CreateSDiv(Constant *LHS, Constant *RHS, 98219077Sdim bool isExact = false) const { 99219077Sdim if (!isExact) 100219077Sdim return BinaryOperator::CreateSDiv(LHS, RHS); 101219077Sdim return BinaryOperator::CreateExactSDiv(LHS, RHS); 102193323Sed } 103218893Sdim Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const { 104198090Srdivacky return BinaryOperator::CreateExactSDiv(LHS, RHS); 105198090Srdivacky } 106218893Sdim Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const { 107193323Sed return BinaryOperator::CreateFDiv(LHS, RHS); 108193323Sed } 109218893Sdim Instruction *CreateURem(Constant *LHS, Constant *RHS) const { 110193323Sed return BinaryOperator::CreateURem(LHS, RHS); 111193323Sed } 112218893Sdim Instruction *CreateSRem(Constant *LHS, Constant *RHS) const { 113193323Sed return BinaryOperator::CreateSRem(LHS, RHS); 114193323Sed } 115218893Sdim Instruction *CreateFRem(Constant *LHS, Constant *RHS) const { 116193323Sed return BinaryOperator::CreateFRem(LHS, RHS); 117193323Sed } 118219077Sdim Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false, 119219077Sdim bool HasNSW = false) const { 120219077Sdim BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS); 121219077Sdim if (HasNUW) BO->setHasNoUnsignedWrap(); 122219077Sdim if (HasNSW) BO->setHasNoSignedWrap(); 123219077Sdim return BO; 124193323Sed } 125219077Sdim Instruction *CreateLShr(Constant *LHS, Constant *RHS, 126219077Sdim bool isExact = false) const { 127219077Sdim if (!isExact) 128219077Sdim return BinaryOperator::CreateLShr(LHS, RHS); 129219077Sdim return BinaryOperator::CreateExactLShr(LHS, RHS); 130193323Sed } 131219077Sdim Instruction *CreateAShr(Constant *LHS, Constant *RHS, 132219077Sdim bool isExact = false) const { 133219077Sdim if (!isExact) 134219077Sdim return BinaryOperator::CreateAShr(LHS, RHS); 135219077Sdim return BinaryOperator::CreateExactAShr(LHS, RHS); 136193323Sed } 137218893Sdim Instruction *CreateAnd(Constant *LHS, Constant *RHS) const { 138193323Sed return BinaryOperator::CreateAnd(LHS, RHS); 139193323Sed } 140218893Sdim Instruction *CreateOr(Constant *LHS, Constant *RHS) const { 141193323Sed return BinaryOperator::CreateOr(LHS, RHS); 142193323Sed } 143218893Sdim Instruction *CreateXor(Constant *LHS, Constant *RHS) const { 144193323Sed return BinaryOperator::CreateXor(LHS, RHS); 145193323Sed } 146193323Sed 147218893Sdim Instruction *CreateBinOp(Instruction::BinaryOps Opc, 148218893Sdim Constant *LHS, Constant *RHS) const { 149193323Sed return BinaryOperator::Create(Opc, LHS, RHS); 150193323Sed } 151193323Sed 152193323Sed //===--------------------------------------------------------------------===// 153193323Sed // Unary Operators 154193323Sed //===--------------------------------------------------------------------===// 155193323Sed 156219077Sdim Instruction *CreateNeg(Constant *C, 157219077Sdim bool HasNUW = false, bool HasNSW = false) const { 158219077Sdim BinaryOperator *BO = BinaryOperator::CreateNeg(C); 159219077Sdim if (HasNUW) BO->setHasNoUnsignedWrap(); 160219077Sdim if (HasNSW) BO->setHasNoSignedWrap(); 161219077Sdim return BO; 162193323Sed } 163218893Sdim Instruction *CreateNSWNeg(Constant *C) const { 164201360Srdivacky return BinaryOperator::CreateNSWNeg(C); 165201360Srdivacky } 166218893Sdim Instruction *CreateNUWNeg(Constant *C) const { 167203954Srdivacky return BinaryOperator::CreateNUWNeg(C); 168203954Srdivacky } 169218893Sdim Instruction *CreateFNeg(Constant *C) const { 170218893Sdim return BinaryOperator::CreateFNeg(C); 171218893Sdim } 172218893Sdim Instruction *CreateNot(Constant *C) const { 173193323Sed return BinaryOperator::CreateNot(C); 174193323Sed } 175193323Sed 176193323Sed //===--------------------------------------------------------------------===// 177193323Sed // Memory Instructions 178193323Sed //===--------------------------------------------------------------------===// 179193323Sed 180226890Sdim Constant *CreateGetElementPtr(Constant *C, 181226890Sdim ArrayRef<Constant *> IdxList) const { 182226890Sdim return ConstantExpr::getGetElementPtr(C, IdxList); 183193323Sed } 184245431Sdim Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const { 185245431Sdim // This form of the function only exists to avoid ambiguous overload 186245431Sdim // warnings about whether to convert Idx to ArrayRef<Constant *> or 187245431Sdim // ArrayRef<Value *>. 188245431Sdim return ConstantExpr::getGetElementPtr(C, Idx); 189245431Sdim } 190226890Sdim Instruction *CreateGetElementPtr(Constant *C, 191226890Sdim ArrayRef<Value *> IdxList) const { 192226890Sdim return GetElementPtrInst::Create(C, IdxList); 193193323Sed } 194193323Sed 195226890Sdim Constant *CreateInBoundsGetElementPtr(Constant *C, 196226890Sdim ArrayRef<Constant *> IdxList) const { 197226890Sdim return ConstantExpr::getInBoundsGetElementPtr(C, IdxList); 198198090Srdivacky } 199245431Sdim Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const { 200245431Sdim // This form of the function only exists to avoid ambiguous overload 201245431Sdim // warnings about whether to convert Idx to ArrayRef<Constant *> or 202245431Sdim // ArrayRef<Value *>. 203245431Sdim return ConstantExpr::getInBoundsGetElementPtr(C, Idx); 204245431Sdim } 205226890Sdim Instruction *CreateInBoundsGetElementPtr(Constant *C, 206226890Sdim ArrayRef<Value *> IdxList) const { 207226890Sdim return GetElementPtrInst::CreateInBounds(C, IdxList); 208198090Srdivacky } 209198090Srdivacky 210193323Sed //===--------------------------------------------------------------------===// 211193323Sed // Cast/Conversion Operators 212193323Sed //===--------------------------------------------------------------------===// 213193323Sed 214218893Sdim Instruction *CreateCast(Instruction::CastOps Op, Constant *C, 215226890Sdim Type *DestTy) const { 216193323Sed return CastInst::Create(Op, C, DestTy); 217193323Sed } 218226890Sdim Instruction *CreatePointerCast(Constant *C, Type *DestTy) const { 219218893Sdim return CastInst::CreatePointerCast(C, DestTy); 220218893Sdim } 221226890Sdim Instruction *CreateIntCast(Constant *C, Type *DestTy, 222193323Sed bool isSigned) const { 223193323Sed return CastInst::CreateIntegerCast(C, DestTy, isSigned); 224193323Sed } 225226890Sdim Instruction *CreateFPCast(Constant *C, Type *DestTy) const { 226218893Sdim return CastInst::CreateFPCast(C, DestTy); 227218893Sdim } 228193323Sed 229226890Sdim Instruction *CreateBitCast(Constant *C, Type *DestTy) const { 230218893Sdim return CreateCast(Instruction::BitCast, C, DestTy); 231218893Sdim } 232226890Sdim Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const { 233218893Sdim return CreateCast(Instruction::IntToPtr, C, DestTy); 234218893Sdim } 235226890Sdim Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const { 236218893Sdim return CreateCast(Instruction::PtrToInt, C, DestTy); 237218893Sdim } 238226890Sdim Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const { 239218893Sdim return CastInst::CreateZExtOrBitCast(C, DestTy); 240218893Sdim } 241226890Sdim Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const { 242218893Sdim return CastInst::CreateSExtOrBitCast(C, DestTy); 243218893Sdim } 244218893Sdim 245226890Sdim Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const { 246218893Sdim return CastInst::CreateTruncOrBitCast(C, DestTy); 247218893Sdim } 248218893Sdim 249193323Sed //===--------------------------------------------------------------------===// 250193323Sed // Compare Instructions 251193323Sed //===--------------------------------------------------------------------===// 252193323Sed 253218893Sdim Instruction *CreateICmp(CmpInst::Predicate P, 254218893Sdim Constant *LHS, Constant *RHS) const { 255193323Sed return new ICmpInst(P, LHS, RHS); 256193323Sed } 257218893Sdim Instruction *CreateFCmp(CmpInst::Predicate P, 258218893Sdim Constant *LHS, Constant *RHS) const { 259193323Sed return new FCmpInst(P, LHS, RHS); 260193323Sed } 261193323Sed 262193323Sed //===--------------------------------------------------------------------===// 263193323Sed // Other Instructions 264193323Sed //===--------------------------------------------------------------------===// 265193323Sed 266218893Sdim Instruction *CreateSelect(Constant *C, 267218893Sdim Constant *True, Constant *False) const { 268193323Sed return SelectInst::Create(C, True, False); 269193323Sed } 270193323Sed 271218893Sdim Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const { 272199989Srdivacky return ExtractElementInst::Create(Vec, Idx); 273193323Sed } 274193323Sed 275218893Sdim Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt, 276218893Sdim Constant *Idx) const { 277193323Sed return InsertElementInst::Create(Vec, NewElt, Idx); 278193323Sed } 279193323Sed 280218893Sdim Instruction *CreateShuffleVector(Constant *V1, Constant *V2, 281218893Sdim Constant *Mask) const { 282193323Sed return new ShuffleVectorInst(V1, V2, Mask); 283193323Sed } 284193323Sed 285224145Sdim Instruction *CreateExtractValue(Constant *Agg, 286224145Sdim ArrayRef<unsigned> IdxList) const { 287224145Sdim return ExtractValueInst::Create(Agg, IdxList); 288193323Sed } 289193323Sed 290218893Sdim Instruction *CreateInsertValue(Constant *Agg, Constant *Val, 291224145Sdim ArrayRef<unsigned> IdxList) const { 292224145Sdim return InsertValueInst::Create(Agg, Val, IdxList); 293193323Sed } 294193323Sed}; 295193323Sed 296193323Sed} 297193323Sed 298193323Sed#endif 299