LLVM API Documentation

NoFolder.h

Go to the documentation of this file.
00001 //======-- llvm/Support/NoFolder.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 NoFolder class, a helper for IRBuilder.  It provides
00011 // IRBuilder with a set of methods for creating unfolded constants.  This is
00012 // useful for learners trying to understand how LLVM IR works, and who don't
00013 // want details to be hidden by the constant folder.  For general constant
00014 // creation and folding, use ConstantExpr and the routines in
00015 // llvm/Analysis/ConstantFolding.h.
00016 //
00017 // Note: since it is not actually possible to create unfolded constants, this
00018 // class returns values rather than constants.  The values do not have names,
00019 // even if names were provided to IRBuilder, which may be confusing.
00020 //
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_SUPPORT_NOFOLDER_H
00024 #define LLVM_SUPPORT_NOFOLDER_H
00025 
00026 #include "llvm/Constants.h"
00027 #include "llvm/Instructions.h"
00028 
00029 namespace llvm {
00030 
00031 class LLVMContext;
00032 
00033 /// NoFolder - Create "constants" (actually, values) with no folding.
00034 class NoFolder {
00035 public:
00036   explicit NoFolder(LLVMContext &) {}
00037 
00038   //===--------------------------------------------------------------------===//
00039   // Binary Operators
00040   //===--------------------------------------------------------------------===//
00041 
00042   Value *CreateAdd(Constant *LHS, Constant *RHS) const {
00043     return BinaryOperator::CreateAdd(LHS, RHS);
00044   }
00045   Value *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
00046     return BinaryOperator::CreateNSWAdd(LHS, RHS);
00047   }
00048   Value *CreateFAdd(Constant *LHS, Constant *RHS) const {
00049     return BinaryOperator::CreateFAdd(LHS, RHS);
00050   }
00051   Value *CreateSub(Constant *LHS, Constant *RHS) const {
00052     return BinaryOperator::CreateSub(LHS, RHS);
00053   }
00054   Value *CreateFSub(Constant *LHS, Constant *RHS) const {
00055     return BinaryOperator::CreateFSub(LHS, RHS);
00056   }
00057   Value *CreateMul(Constant *LHS, Constant *RHS) const {
00058     return BinaryOperator::CreateMul(LHS, RHS);
00059   }
00060   Value *CreateFMul(Constant *LHS, Constant *RHS) const {
00061     return BinaryOperator::CreateFMul(LHS, RHS);
00062   }
00063   Value *CreateUDiv(Constant *LHS, Constant *RHS) const {
00064     return BinaryOperator::CreateUDiv(LHS, RHS);
00065   }
00066   Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
00067     return BinaryOperator::CreateSDiv(LHS, RHS);
00068   }
00069   Value *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
00070     return BinaryOperator::CreateExactSDiv(LHS, RHS);
00071   }
00072   Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
00073     return BinaryOperator::CreateFDiv(LHS, RHS);
00074   }
00075   Value *CreateURem(Constant *LHS, Constant *RHS) const {
00076     return BinaryOperator::CreateURem(LHS, RHS);
00077   }
00078   Value *CreateSRem(Constant *LHS, Constant *RHS) const {
00079     return BinaryOperator::CreateSRem(LHS, RHS);
00080   }
00081   Value *CreateFRem(Constant *LHS, Constant *RHS) const {
00082     return BinaryOperator::CreateFRem(LHS, RHS);
00083   }
00084   Value *CreateShl(Constant *LHS, Constant *RHS) const {
00085     return BinaryOperator::CreateShl(LHS, RHS);
00086   }
00087   Value *CreateLShr(Constant *LHS, Constant *RHS) const {
00088     return BinaryOperator::CreateLShr(LHS, RHS);
00089   }
00090   Value *CreateAShr(Constant *LHS, Constant *RHS) const {
00091     return BinaryOperator::CreateAShr(LHS, RHS);
00092   }
00093   Value *CreateAnd(Constant *LHS, Constant *RHS) const {
00094     return BinaryOperator::CreateAnd(LHS, RHS);
00095   }
00096   Value *CreateOr(Constant *LHS, Constant *RHS) const {
00097     return BinaryOperator::CreateOr(LHS, RHS);
00098   }
00099   Value *CreateXor(Constant *LHS, Constant *RHS) const {
00100     return BinaryOperator::CreateXor(LHS, RHS);
00101   }
00102 
00103   Value *CreateBinOp(Instruction::BinaryOps Opc,
00104                      Constant *LHS, Constant *RHS) const {
00105     return BinaryOperator::Create(Opc, LHS, RHS);
00106   }
00107 
00108   //===--------------------------------------------------------------------===//
00109   // Unary Operators
00110   //===--------------------------------------------------------------------===//
00111 
00112   Value *CreateNeg(Constant *C) const {
00113     return BinaryOperator::CreateNeg(C);
00114   }
00115   Value *CreateNot(Constant *C) const {
00116     return BinaryOperator::CreateNot(C);
00117   }
00118 
00119   //===--------------------------------------------------------------------===//
00120   // Memory Instructions
00121   //===--------------------------------------------------------------------===//
00122 
00123   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
00124                                 unsigned NumIdx) const {
00125     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
00126   }
00127   Value *CreateGetElementPtr(Constant *C, Value* const *IdxList,
00128                              unsigned NumIdx) const {
00129     return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
00130   }
00131 
00132   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
00133                                         unsigned NumIdx) const {
00134     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
00135   }
00136   Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
00137                                      unsigned NumIdx) const {
00138     return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
00139   }
00140 
00141   //===--------------------------------------------------------------------===//
00142   // Cast/Conversion Operators
00143   //===--------------------------------------------------------------------===//
00144 
00145   Value *CreateCast(Instruction::CastOps Op, Constant *C,
00146                     const Type *DestTy) const {
00147     return CastInst::Create(Op, C, DestTy);
00148   }
00149   Value *CreateIntCast(Constant *C, const Type *DestTy,
00150                        bool isSigned) const {
00151     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
00152   }
00153 
00154   //===--------------------------------------------------------------------===//
00155   // Compare Instructions
00156   //===--------------------------------------------------------------------===//
00157 
00158   Value *CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
00159     return new ICmpInst(P, LHS, RHS);
00160   }
00161   Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
00162     return new FCmpInst(P, LHS, RHS);
00163   }
00164 
00165   //===--------------------------------------------------------------------===//
00166   // Other Instructions
00167   //===--------------------------------------------------------------------===//
00168 
00169   Value *CreateSelect(Constant *C, Constant *True, Constant *False) const {
00170     return SelectInst::Create(C, True, False);
00171   }
00172 
00173   Value *CreateExtractElement(Constant *Vec, Constant *Idx) const {
00174     return new ExtractElementInst(Vec, Idx);
00175   }
00176 
00177   Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
00178                              Constant *Idx) const {
00179     return InsertElementInst::Create(Vec, NewElt, Idx);
00180   }
00181 
00182   Value *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
00183     return new ShuffleVectorInst(V1, V2, Mask);
00184   }
00185 
00186   Value *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
00187                             unsigned NumIdx) const {
00188     return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
00189   }
00190 
00191   Value *CreateInsertValue(Constant *Agg, Constant *Val,
00192                            const unsigned *IdxList, unsigned NumIdx) const {
00193     return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
00194   }
00195 };
00196 
00197 }
00198 
00199 #endif