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 select instruction with the specified 83 /// operands. The constant result is returned if successful; if not, null is 84 /// returned. 85 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, 86 Constant *V2); 87 88 /// \brief Attempt to constant fold a cast with the specified operand. If it 89 /// fails, it returns a constant expression of the specified operand. 90 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, 91 const DataLayout &DL); 92 93 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue 94 /// instruction with the specified operands and indices. The constant result is 95 /// returned if successful; if not, null is returned. 96 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, 97 ArrayRef<unsigned> Idxs); 98 99 /// \brief Attempt to constant fold an extractvalue instruction with the 100 /// specified operands and indices. The constant result is returned if 101 /// successful; if not, null is returned. 102 Constant *ConstantFoldExtractValueInstruction(Constant *Agg, 103 ArrayRef<unsigned> Idxs); 104 105 /// \brief Attempt to constant fold an extractelement instruction with the 106 /// specified operands and indices. The constant result is returned if 107 /// successful; if not, null is returned. 108 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); 109 110 /// \brief Attempt to constant fold a shufflevector instruction with the 111 /// specified operands and indices. The constant result is returned if 112 /// successful; if not, null is returned. 113 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, 114 Constant *Mask); 115 116 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 117 /// produce if it is constant and determinable. If this is not determinable, 118 /// return null. 119 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); 120 121 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 122 /// getelementptr constantexpr, return the constant value being addressed by the 123 /// constant expression, or null if something is funny and we can't decide. 124 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); 125 126 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 127 /// indices (with an *implied* zero pointer index that is not in the list), 128 /// return the constant value being addressed by a virtual load, or null if 129 /// something is funny and we can't decide. 130 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, 131 ArrayRef<Constant *> Indices); 132 133 /// canConstantFoldCallTo - Return true if its even possible to fold a call to 134 /// the specified function. 135 bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F); 136 137 /// ConstantFoldCall - Attempt to constant fold a call to the specified function 138 /// with the specified arguments, returning null if unsuccessful. 139 Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F, 140 ArrayRef<Constant *> Operands, 141 const TargetLibraryInfo *TLI = nullptr); 142 143 /// \brief Check whether the given call has no side-effects. 144 /// Specifically checks for math routimes which sometimes set errno. 145 bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI); 146 } 147 148 #endif 149