LLVM API Documentation

ConstantFolder.h
Go to the documentation of this file.
00001 //===- ConstantFolder.h - Constant folding helper ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ConstantFolder class, a helper for IRBuilder.
00011 // It provides IRBuilder with a set of methods for creating constants
00012 // with minimal folding.  For general constant creation and folding,
00013 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_IR_CONSTANTFOLDER_H
00018 #define LLVM_IR_CONSTANTFOLDER_H
00019 
00020 #include "llvm/IR/Constants.h"
00021 #include "llvm/IR/InstrTypes.h"
00022 
00023 namespace llvm {
00024 
00025 /// ConstantFolder - Create constants with minimum, target independent, folding.
00026 class ConstantFolder {
00027 public:
00028   explicit ConstantFolder() {}
00029 
00030   //===--------------------------------------------------------------------===//
00031   // Binary Operators
00032   //===--------------------------------------------------------------------===//
00033 
00034   Constant *CreateAdd(Constant *LHS, Constant *RHS,
00035                       bool HasNUW = false, bool HasNSW = false) const {
00036     return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
00037   }
00038   Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
00039     return ConstantExpr::getFAdd(LHS, RHS);
00040   }
00041   Constant *CreateSub(Constant *LHS, Constant *RHS,
00042                       bool HasNUW = false, bool HasNSW = false) const {
00043     return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
00044   }
00045   Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
00046     return ConstantExpr::getFSub(LHS, RHS);
00047   }
00048   Constant *CreateMul(Constant *LHS, Constant *RHS,
00049                       bool HasNUW = false, bool HasNSW = false) const {
00050     return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
00051   }
00052   Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
00053     return ConstantExpr::getFMul(LHS, RHS);
00054   }
00055   Constant *CreateUDiv(Constant *LHS, Constant *RHS,
00056                        bool isExact = false) const {
00057     return ConstantExpr::getUDiv(LHS, RHS, isExact);
00058   }
00059   Constant *CreateSDiv(Constant *LHS, Constant *RHS,
00060                        bool isExact = false) const {
00061     return ConstantExpr::getSDiv(LHS, RHS, isExact);
00062   }
00063   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
00064     return ConstantExpr::getFDiv(LHS, RHS);
00065   }
00066   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
00067     return ConstantExpr::getURem(LHS, RHS);
00068   }
00069   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
00070     return ConstantExpr::getSRem(LHS, RHS);
00071   }
00072   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
00073     return ConstantExpr::getFRem(LHS, RHS);
00074   }
00075   Constant *CreateShl(Constant *LHS, Constant *RHS,
00076                       bool HasNUW = false, bool HasNSW = false) const {
00077     return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
00078   }
00079   Constant *CreateLShr(Constant *LHS, Constant *RHS,
00080                        bool isExact = false) const {
00081     return ConstantExpr::getLShr(LHS, RHS, isExact);
00082   }
00083   Constant *CreateAShr(Constant *LHS, Constant *RHS,
00084                        bool isExact = false) const {
00085     return ConstantExpr::getAShr(LHS, RHS, isExact);
00086   }
00087   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
00088     return ConstantExpr::getAnd(LHS, RHS);
00089   }
00090   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
00091     return ConstantExpr::getOr(LHS, RHS);
00092   }
00093   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
00094     return ConstantExpr::getXor(LHS, RHS);
00095   }
00096 
00097   Constant *CreateBinOp(Instruction::BinaryOps Opc,
00098                         Constant *LHS, Constant *RHS) const {
00099     return ConstantExpr::get(Opc, LHS, RHS);
00100   }
00101 
00102   //===--------------------------------------------------------------------===//
00103   // Unary Operators
00104   //===--------------------------------------------------------------------===//
00105 
00106   Constant *CreateNeg(Constant *C,
00107                       bool HasNUW = false, bool HasNSW = false) const {
00108     return ConstantExpr::getNeg(C, HasNUW, HasNSW);
00109   }
00110   Constant *CreateFNeg(Constant *C) const {
00111     return ConstantExpr::getFNeg(C);
00112   }
00113   Constant *CreateNot(Constant *C) const {
00114     return ConstantExpr::getNot(C);
00115   }
00116 
00117   //===--------------------------------------------------------------------===//
00118   // Memory Instructions
00119   //===--------------------------------------------------------------------===//
00120 
00121   Constant *CreateGetElementPtr(Constant *C,
00122                                 ArrayRef<Constant *> IdxList) const {
00123     return ConstantExpr::getGetElementPtr(C, IdxList);
00124   }
00125   Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
00126     // This form of the function only exists to avoid ambiguous overload
00127     // warnings about whether to convert Idx to ArrayRef<Constant *> or
00128     // ArrayRef<Value *>.
00129     return ConstantExpr::getGetElementPtr(C, Idx);
00130   }
00131   Constant *CreateGetElementPtr(Constant *C,
00132                                 ArrayRef<Value *> IdxList) const {
00133     return ConstantExpr::getGetElementPtr(C, IdxList);
00134   }
00135 
00136   Constant *CreateInBoundsGetElementPtr(Constant *C,
00137                                         ArrayRef<Constant *> IdxList) const {
00138     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
00139   }
00140   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
00141     // This form of the function only exists to avoid ambiguous overload
00142     // warnings about whether to convert Idx to ArrayRef<Constant *> or
00143     // ArrayRef<Value *>.
00144     return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
00145   }
00146   Constant *CreateInBoundsGetElementPtr(Constant *C,
00147                                         ArrayRef<Value *> IdxList) const {
00148     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
00149   }
00150 
00151   //===--------------------------------------------------------------------===//
00152   // Cast/Conversion Operators
00153   //===--------------------------------------------------------------------===//
00154 
00155   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
00156                        Type *DestTy) const {
00157     return ConstantExpr::getCast(Op, C, DestTy);
00158   }
00159   Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
00160     return ConstantExpr::getPointerCast(C, DestTy);
00161   }
00162 
00163   Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
00164                                                 Type *DestTy) const {
00165     return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
00166   }
00167 
00168   Constant *CreateIntCast(Constant *C, Type *DestTy,
00169                           bool isSigned) const {
00170     return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
00171   }
00172   Constant *CreateFPCast(Constant *C, Type *DestTy) const {
00173     return ConstantExpr::getFPCast(C, DestTy);
00174   }
00175 
00176   Constant *CreateBitCast(Constant *C, Type *DestTy) const {
00177     return CreateCast(Instruction::BitCast, C, DestTy);
00178   }
00179   Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
00180     return CreateCast(Instruction::IntToPtr, C, DestTy);
00181   }
00182   Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
00183     return CreateCast(Instruction::PtrToInt, C, DestTy);
00184   }
00185   Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
00186     return ConstantExpr::getZExtOrBitCast(C, DestTy);
00187   }
00188   Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
00189     return ConstantExpr::getSExtOrBitCast(C, DestTy);
00190   }
00191 
00192   Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
00193     return ConstantExpr::getTruncOrBitCast(C, DestTy);
00194   }
00195 
00196   //===--------------------------------------------------------------------===//
00197   // Compare Instructions
00198   //===--------------------------------------------------------------------===//
00199 
00200   Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
00201                        Constant *RHS) const {
00202     return ConstantExpr::getCompare(P, LHS, RHS);
00203   }
00204   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
00205                        Constant *RHS) const {
00206     return ConstantExpr::getCompare(P, LHS, RHS);
00207   }
00208 
00209   //===--------------------------------------------------------------------===//
00210   // Other Instructions
00211   //===--------------------------------------------------------------------===//
00212 
00213   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
00214     return ConstantExpr::getSelect(C, True, False);
00215   }
00216 
00217   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
00218     return ConstantExpr::getExtractElement(Vec, Idx);
00219   }
00220 
00221   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
00222                                 Constant *Idx) const {
00223     return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
00224   }
00225 
00226   Constant *CreateShuffleVector(Constant *V1, Constant *V2,
00227                                 Constant *Mask) const {
00228     return ConstantExpr::getShuffleVector(V1, V2, Mask);
00229   }
00230 
00231   Constant *CreateExtractValue(Constant *Agg,
00232                                ArrayRef<unsigned> IdxList) const {
00233     return ConstantExpr::getExtractValue(Agg, IdxList);
00234   }
00235 
00236   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
00237                               ArrayRef<unsigned> IdxList) const {
00238     return ConstantExpr::getInsertValue(Agg, Val, IdxList);
00239   }
00240 };
00241 
00242 }
00243 
00244 #endif