1 //===-- ConstantFolding.h - Fold instructions into constants ----*- 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 declares routines for folding instructions into constants when all 11 // operands are constants, for example "sub i32 1, 0" -> "1". 12 // 13 // Also, to supplement the basic VMCore ConstantExpr simplifications, 14 // this file declares some additional folding routines that can make use of 15 // DataLayout information. These functions cannot go in VMCore due to library 16 // dependency issues. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H 21 #define LLVM_ANALYSIS_CONSTANTFOLDING_H 22 23 namespace llvm { 24 class APInt; 25 template <typename T> class ArrayRef; 26 class CallSite; 27 class Constant; 28 class ConstantExpr; 29 class ConstantVector; 30 class DataLayout; 31 class Function; 32 class GlobalValue; 33 class Instruction; 34 class ImmutableCallSite; 35 class TargetLibraryInfo; 36 class Type; 37 38 /// If this constant is a constant offset from a global, return the global and 39 /// the constant. Because of constantexprs, this function is recursive. 40 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, 41 const DataLayout &DL); 42 43 /// ConstantFoldInstruction - Try to constant fold the specified instruction. 44 /// If successful, the constant result is returned, if not, null is returned. 45 /// Note that this fails if not all of the operands are constant. Otherwise, 46 /// this function can only fail when attempting to fold instructions like loads 47 /// and stores, which have no constant expression form. 48 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, 49 const TargetLibraryInfo *TLI = nullptr); 50 51 /// ConstantFoldConstant - Attempt to fold the constant using the 52 /// specified DataLayout. 53 /// If successful, the constant result is returned, if not, null is returned. 54 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL, 55 const TargetLibraryInfo *TLI = nullptr); 56 57 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 58 /// specified operands. If successful, the constant result is returned, if not, 59 /// null is returned. Note that this function can fail when attempting to 60 /// fold instructions like loads and stores, which have no constant expression 61 /// form. 62 /// 63 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, 64 const DataLayout &DL, 65 const TargetLibraryInfo *TLI = nullptr); 66 67 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 68 /// instruction (icmp/fcmp) with the specified operands. If it fails, it 69 /// returns a constant expression of the specified operands. 70 /// 71 Constant * 72 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, 73 Constant *RHS, const DataLayout &DL, 74 const TargetLibraryInfo *TLI = nullptr); 75 76 /// \brief Attempt to constant fold a binary operation with the specified 77 /// operands. If it fails, it returns a constant expression of the specified 78 /// operands. 79 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, 80 Constant *RHS, const DataLayout &DL); 81 82 /// \brief Attempt to constant fold a cast with the specified operand. If it 83 /// fails, it returns a constant expression of the specified operand. 84 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, 85 const DataLayout &DL); 86 87 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue 88 /// instruction with the specified operands and indices. The constant result is 89 /// returned if successful; if not, null is returned. 90 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, 91 ArrayRef<unsigned> Idxs); 92 93 /// \brief Attempt to constant fold an extractvalue instruction with the 94 /// specified operands and indices. The constant result is returned if 95 /// successful; if not, null is returned. 96 Constant *ConstantFoldExtractValueInstruction(Constant *Agg, 97 ArrayRef<unsigned> Idxs); 98 99 /// \brief Attempt to constant fold an extractelement instruction with the 100 /// specified operands and indices. The constant result is returned if 101 /// successful; if not, null is returned. 102 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); 103 104 /// \brief Attempt to constant fold a shufflevector instruction with the 105 /// specified operands and indices. The constant result is returned if 106 /// successful; if not, null is returned. 107 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, 108 Constant *Mask); 109 110 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 111 /// produce if it is constant and determinable. If this is not determinable, 112 /// return null. 113 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); 114 115 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 116 /// getelementptr constantexpr, return the constant value being addressed by the 117 /// constant expression, or null if something is funny and we can't decide. 118 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); 119 120 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 121 /// indices (with an *implied* zero pointer index that is not in the list), 122 /// return the constant value being addressed by a virtual load, or null if 123 /// something is funny and we can't decide. 124 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, 125 ArrayRef<Constant *> Indices); 126 127 /// canConstantFoldCallTo - Return true if its even possible to fold a call to 128 /// the specified function. 129 bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F); 130 131 /// ConstantFoldCall - Attempt to constant fold a call to the specified function 132 /// with the specified arguments, returning null if unsuccessful. 133 Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F, 134 ArrayRef<Constant *> Operands, 135 const TargetLibraryInfo *TLI = nullptr); 136 137 /// \brief Check whether the given call has no side-effects. 138 /// Specifically checks for math routimes which sometimes set errno. 139 bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI); 140 } 141 142 #endif 143