Home | History | Annotate | Download | only in Support
      1 //======-- llvm/Support/NoFolder.h - Constant folding helper -*- C++ -*-======//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the NoFolder class, a helper for IRBuilder.  It provides
     11 // IRBuilder with a set of methods for creating unfolded constants.  This is
     12 // useful for learners trying to understand how LLVM IR works, and who don't
     13 // want details to be hidden by the constant folder.  For general constant
     14 // creation and folding, use ConstantExpr and the routines in
     15 // llvm/Analysis/ConstantFolding.h.
     16 //
     17 // Note: since it is not actually possible to create unfolded constants, this
     18 // class returns instructions rather than constants.
     19 //
     20 //===----------------------------------------------------------------------===//
     21 
     22 #ifndef LLVM_SUPPORT_NOFOLDER_H
     23 #define LLVM_SUPPORT_NOFOLDER_H
     24 
     25 #include "llvm/ADT/ArrayRef.h"
     26 #include "llvm/Constants.h"
     27 #include "llvm/Instructions.h"
     28 
     29 namespace llvm {
     30 
     31 /// NoFolder - Create "constants" (actually, instructions) with no folding.
     32 class NoFolder {
     33 public:
     34   explicit NoFolder() {}
     35 
     36   //===--------------------------------------------------------------------===//
     37   // Binary Operators
     38   //===--------------------------------------------------------------------===//
     39 
     40   Instruction *CreateAdd(Constant *LHS, Constant *RHS,
     41                          bool HasNUW = false, bool HasNSW = false) const {
     42     BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
     43     if (HasNUW) BO->setHasNoUnsignedWrap();
     44     if (HasNSW) BO->setHasNoSignedWrap();
     45     return BO;
     46   }
     47   Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
     48     return BinaryOperator::CreateNSWAdd(LHS, RHS);
     49   }
     50   Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
     51     return BinaryOperator::CreateNUWAdd(LHS, RHS);
     52   }
     53   Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
     54     return BinaryOperator::CreateFAdd(LHS, RHS);
     55   }
     56   Instruction *CreateSub(Constant *LHS, Constant *RHS,
     57                          bool HasNUW = false, bool HasNSW = false) const {
     58     BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
     59     if (HasNUW) BO->setHasNoUnsignedWrap();
     60     if (HasNSW) BO->setHasNoSignedWrap();
     61     return BO;
     62   }
     63   Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
     64     return BinaryOperator::CreateNSWSub(LHS, RHS);
     65   }
     66   Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
     67     return BinaryOperator::CreateNUWSub(LHS, RHS);
     68   }
     69   Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
     70     return BinaryOperator::CreateFSub(LHS, RHS);
     71   }
     72   Instruction *CreateMul(Constant *LHS, Constant *RHS,
     73                          bool HasNUW = false, bool HasNSW = false) const {
     74     BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
     75     if (HasNUW) BO->setHasNoUnsignedWrap();
     76     if (HasNSW) BO->setHasNoSignedWrap();
     77     return BO;
     78   }
     79   Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
     80     return BinaryOperator::CreateNSWMul(LHS, RHS);
     81   }
     82   Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
     83     return BinaryOperator::CreateNUWMul(LHS, RHS);
     84   }
     85   Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
     86     return BinaryOperator::CreateFMul(LHS, RHS);
     87   }
     88   Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
     89                           bool isExact = false) const {
     90     if (!isExact)
     91       return BinaryOperator::CreateUDiv(LHS, RHS);
     92     return BinaryOperator::CreateExactUDiv(LHS, RHS);
     93   }
     94   Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
     95     return BinaryOperator::CreateExactUDiv(LHS, RHS);
     96   }
     97   Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
     98                           bool isExact = false) const {
     99     if (!isExact)
    100       return BinaryOperator::CreateSDiv(LHS, RHS);
    101     return BinaryOperator::CreateExactSDiv(LHS, RHS);
    102   }
    103   Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
    104     return BinaryOperator::CreateExactSDiv(LHS, RHS);
    105   }
    106   Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
    107     return BinaryOperator::CreateFDiv(LHS, RHS);
    108   }
    109   Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
    110     return BinaryOperator::CreateURem(LHS, RHS);
    111   }
    112   Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
    113     return BinaryOperator::CreateSRem(LHS, RHS);
    114   }
    115   Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
    116     return BinaryOperator::CreateFRem(LHS, RHS);
    117   }
    118   Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
    119                          bool HasNSW = false) const {
    120     BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
    121     if (HasNUW) BO->setHasNoUnsignedWrap();
    122     if (HasNSW) BO->setHasNoSignedWrap();
    123     return BO;
    124   }
    125   Instruction *CreateLShr(Constant *LHS, Constant *RHS,
    126                           bool isExact = false) const {
    127     if (!isExact)
    128       return BinaryOperator::CreateLShr(LHS, RHS);
    129     return BinaryOperator::CreateExactLShr(LHS, RHS);
    130   }
    131   Instruction *CreateAShr(Constant *LHS, Constant *RHS,
    132                           bool isExact = false) const {
    133     if (!isExact)
    134       return BinaryOperator::CreateAShr(LHS, RHS);
    135     return BinaryOperator::CreateExactAShr(LHS, RHS);
    136   }
    137   Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
    138     return BinaryOperator::CreateAnd(LHS, RHS);
    139   }
    140   Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
    141     return BinaryOperator::CreateOr(LHS, RHS);
    142   }
    143   Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
    144     return BinaryOperator::CreateXor(LHS, RHS);
    145   }
    146 
    147   Instruction *CreateBinOp(Instruction::BinaryOps Opc,
    148                            Constant *LHS, Constant *RHS) const {
    149     return BinaryOperator::Create(Opc, LHS, RHS);
    150   }
    151 
    152   //===--------------------------------------------------------------------===//
    153   // Unary Operators
    154   //===--------------------------------------------------------------------===//
    155 
    156   Instruction *CreateNeg(Constant *C,
    157                          bool HasNUW = false, bool HasNSW = false) const {
    158     BinaryOperator *BO = BinaryOperator::CreateNeg(C);
    159     if (HasNUW) BO->setHasNoUnsignedWrap();
    160     if (HasNSW) BO->setHasNoSignedWrap();
    161     return BO;
    162   }
    163   Instruction *CreateNSWNeg(Constant *C) const {
    164     return BinaryOperator::CreateNSWNeg(C);
    165   }
    166   Instruction *CreateNUWNeg(Constant *C) const {
    167     return BinaryOperator::CreateNUWNeg(C);
    168   }
    169   Instruction *CreateFNeg(Constant *C) const {
    170     return BinaryOperator::CreateFNeg(C);
    171   }
    172   Instruction *CreateNot(Constant *C) const {
    173     return BinaryOperator::CreateNot(C);
    174   }
    175 
    176   //===--------------------------------------------------------------------===//
    177   // Memory Instructions
    178   //===--------------------------------------------------------------------===//
    179 
    180   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
    181                                 unsigned NumIdx) const {
    182     return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
    183   }
    184   Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
    185                                    unsigned NumIdx) const {
    186     return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
    187   }
    188 
    189   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
    190                                         unsigned NumIdx) const {
    191     return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
    192   }
    193   Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
    194                                            unsigned NumIdx) const {
    195     return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
    196   }
    197 
    198   //===--------------------------------------------------------------------===//
    199   // Cast/Conversion Operators
    200   //===--------------------------------------------------------------------===//
    201 
    202   Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
    203                     Type *DestTy) const {
    204     return CastInst::Create(Op, C, DestTy);
    205   }
    206   Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
    207     return CastInst::CreatePointerCast(C, DestTy);
    208   }
    209   Instruction *CreateIntCast(Constant *C, Type *DestTy,
    210                        bool isSigned) const {
    211     return CastInst::CreateIntegerCast(C, DestTy, isSigned);
    212   }
    213   Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
    214     return CastInst::CreateFPCast(C, DestTy);
    215   }
    216 
    217   Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
    218     return CreateCast(Instruction::BitCast, C, DestTy);
    219   }
    220   Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
    221     return CreateCast(Instruction::IntToPtr, C, DestTy);
    222   }
    223   Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
    224     return CreateCast(Instruction::PtrToInt, C, DestTy);
    225   }
    226   Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
    227     return CastInst::CreateZExtOrBitCast(C, DestTy);
    228   }
    229   Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
    230     return CastInst::CreateSExtOrBitCast(C, DestTy);
    231   }
    232 
    233   Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
    234     return CastInst::CreateTruncOrBitCast(C, DestTy);
    235   }
    236 
    237   //===--------------------------------------------------------------------===//
    238   // Compare Instructions
    239   //===--------------------------------------------------------------------===//
    240 
    241   Instruction *CreateICmp(CmpInst::Predicate P,
    242                           Constant *LHS, Constant *RHS) const {
    243     return new ICmpInst(P, LHS, RHS);
    244   }
    245   Instruction *CreateFCmp(CmpInst::Predicate P,
    246                           Constant *LHS, Constant *RHS) const {
    247     return new FCmpInst(P, LHS, RHS);
    248   }
    249 
    250   //===--------------------------------------------------------------------===//
    251   // Other Instructions
    252   //===--------------------------------------------------------------------===//
    253 
    254   Instruction *CreateSelect(Constant *C,
    255                             Constant *True, Constant *False) const {
    256     return SelectInst::Create(C, True, False);
    257   }
    258 
    259   Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
    260     return ExtractElementInst::Create(Vec, Idx);
    261   }
    262 
    263   Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
    264                                    Constant *Idx) const {
    265     return InsertElementInst::Create(Vec, NewElt, Idx);
    266   }
    267 
    268   Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
    269                                    Constant *Mask) const {
    270     return new ShuffleVectorInst(V1, V2, Mask);
    271   }
    272 
    273   Instruction *CreateExtractValue(Constant *Agg,
    274                                   ArrayRef<unsigned> IdxList) const {
    275     return ExtractValueInst::Create(Agg, IdxList);
    276   }
    277 
    278   Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
    279                                  ArrayRef<unsigned> IdxList) const {
    280     return InsertValueInst::Create(Agg, Val, IdxList);
    281   }
    282 };
    283 
    284 }
    285 
    286 #endif
    287