LLVM API Documentation

TargetFolder.h

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