LLVM API Documentation

ConstantFolder.h

Go to the documentation of this file.
00001 //===-- llvm/Support/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_SUPPORT_CONSTANTFOLDER_H
00018 #define LLVM_SUPPORT_CONSTANTFOLDER_H
00019 
00020 #include "llvm/Constants.h"
00021 
00022 namespace llvm {
00023 
00024 class LLVMContext;
00025 
00026 /// ConstantFolder - Create constants with minimum, target independent, folding.
00027 class ConstantFolder {
00028 public:
00029   explicit ConstantFolder(LLVMContext &) {}
00030 
00031   //===--------------------------------------------------------------------===//
00032   // Binary Operators
00033   //===--------------------------------------------------------------------===//
00034 
00035   Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
00036     return ConstantExpr::getAdd(LHS, RHS);
00037   }
00038   Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
00039     return ConstantExpr::getNSWAdd(LHS, RHS);
00040   }
00041   Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
00042     return ConstantExpr::getFAdd(LHS, RHS);
00043   }
00044   Constant *CreateSub(Constant *LHS, Constant *RHS) const {
00045     return ConstantExpr::getSub(LHS, RHS);
00046   }
00047   Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
00048     return ConstantExpr::getFSub(LHS, RHS);
00049   }
00050   Constant *CreateMul(Constant *LHS, Constant *RHS) const {
00051     return ConstantExpr::getMul(LHS, RHS);
00052   }
00053   Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
00054     return ConstantExpr::getFMul(LHS, RHS);
00055   }
00056   Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
00057     return ConstantExpr::getUDiv(LHS, RHS);
00058   }
00059   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
00060     return ConstantExpr::getSDiv(LHS, RHS);
00061   }
00062   Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
00063     return ConstantExpr::getExactSDiv(LHS, RHS);
00064   }
00065   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
00066     return ConstantExpr::getFDiv(LHS, RHS);
00067   }
00068   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
00069     return ConstantExpr::getURem(LHS, RHS);
00070   }
00071   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
00072     return ConstantExpr::getSRem(LHS, RHS);
00073   }
00074   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
00075     return ConstantExpr::getFRem(LHS, RHS);
00076   }
00077   Constant *CreateShl(Constant *LHS, Constant *RHS) const {
00078     return ConstantExpr::getShl(LHS, RHS);
00079   }
00080   Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
00081     return ConstantExpr::getLShr(LHS, RHS);
00082   }
00083   Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
00084     return ConstantExpr::getAShr(LHS, RHS);
00085   }
00086   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
00087     return ConstantExpr::getAnd(LHS, RHS);
00088   }
00089   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
00090     return ConstantExpr::getOr(LHS, RHS);
00091   }
00092   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
00093     return ConstantExpr::getXor(LHS, RHS);
00094   }
00095 
00096   Constant *CreateBinOp(Instruction::BinaryOps Opc,
00097                         Constant *LHS, Constant *RHS) const {
00098     return ConstantExpr::get(Opc, LHS, RHS);
00099   }
00100 
00101   //===--------------------------------------------------------------------===//
00102   // Unary Operators
00103   //===--------------------------------------------------------------------===//
00104 
00105   Constant *CreateNeg(Constant *C) const {
00106     return ConstantExpr::getNeg(C);
00107   }
00108   Constant *CreateFNeg(Constant *C) const {
00109     return ConstantExpr::getFNeg(C);
00110   }
00111   Constant *CreateNot(Constant *C) const {
00112     return ConstantExpr::getNot(C);
00113   }
00114 
00115   //===--------------------------------------------------------------------===//
00116   // Memory Instructions
00117   //===--------------------------------------------------------------------===//
00118 
00119   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
00120                                 unsigned NumIdx) const {
00121     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
00122   }
00123   Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
00124                                 unsigned NumIdx) const {
00125     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
00126   }
00127 
00128   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
00129                                         unsigned NumIdx) const {
00130     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
00131   }
00132   Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
00133                                         unsigned NumIdx) const {
00134     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
00135   }
00136 
00137   //===--------------------------------------------------------------------===//
00138   // Cast/Conversion Operators
00139   //===--------------------------------------------------------------------===//
00140 
00141   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
00142                        const Type *DestTy) const {
00143     return ConstantExpr::getCast(Op, C, DestTy);
00144   }
00145   Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
00146     return ConstantExpr::getPointerCast(C, DestTy);
00147   }
00148   Constant *CreateIntCast(Constant *C, const Type *DestTy,
00149                           bool isSigned) const {
00150     return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
00151   }
00152   Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
00153     return ConstantExpr::getFPCast(C, DestTy);
00154   }
00155 
00156   Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
00157     return CreateCast(Instruction::BitCast, C, DestTy);
00158   }
00159   Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
00160     return CreateCast(Instruction::IntToPtr, C, DestTy);
00161   }
00162   Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
00163     return CreateCast(Instruction::PtrToInt, C, DestTy);
00164   }
00165   Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
00166     return ConstantExpr::getZExtOrBitCast(C, DestTy);
00167   }
00168   Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
00169     return ConstantExpr::getSExtOrBitCast(C, DestTy);
00170   }
00171 
00172   Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
00173     return ConstantExpr::getTruncOrBitCast(C, DestTy);
00174   }
00175 
00176   //===--------------------------------------------------------------------===//
00177   // Compare Instructions
00178   //===--------------------------------------------------------------------===//
00179 
00180   Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
00181                        Constant *RHS) const {
00182     return ConstantExpr::getCompare(P, LHS, RHS);
00183   }
00184   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
00185                        Constant *RHS) const {
00186     return ConstantExpr::getCompare(P, LHS, RHS);
00187   }
00188 
00189   //===--------------------------------------------------------------------===//
00190   // Other Instructions
00191   //===--------------------------------------------------------------------===//
00192 
00193   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
00194     return ConstantExpr::getSelect(C, True, False);
00195   }
00196 
00197   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
00198     return ConstantExpr::getExtractElement(Vec, Idx);
00199   }
00200 
00201   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
00202                                 Constant *Idx) const {
00203     return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
00204   }
00205 
00206   Constant *CreateShuffleVector(Constant *V1, Constant *V2,
00207                                 Constant *Mask) const {
00208     return ConstantExpr::getShuffleVector(V1, V2, Mask);
00209   }
00210 
00211   Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
00212                                unsigned NumIdx) const {
00213     return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
00214   }
00215 
00216   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
00217                               const unsigned *IdxList, unsigned NumIdx) const {
00218     return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
00219   }
00220 };
00221 
00222 }
00223 
00224 #endif