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 Constant; 27 class ConstantExpr; 28 class DataLayout; 29 class Function; 30 class GlobalValue; 31 class Instruction; 32 class TargetLibraryInfo; 33 class Type; 34 35 /// If this constant is a constant offset from a global, return the global and 36 /// the constant. Because of constantexprs, this function is recursive. 37 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, 38 const DataLayout &DL); 39 40 /// ConstantFoldInstruction - Try to constant fold the specified instruction. 41 /// If successful, the constant result is returned, if not, null is returned. 42 /// Note that this fails if not all of the operands are constant. Otherwise, 43 /// this function can only fail when attempting to fold instructions like loads 44 /// and stores, which have no constant expression form. 45 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, 46 const TargetLibraryInfo *TLI = nullptr); 47 48 /// ConstantFoldConstantExpression - Attempt to fold the constant expression 49 /// using the specified DataLayout. If successful, the constant result is 50 /// result is returned, if not, null is returned. 51 Constant * 52 ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, 53 const TargetLibraryInfo *TLI = nullptr); 54 55 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 56 /// specified operands. If successful, the constant result is returned, if not, 57 /// null is returned. Note that this function can fail when attempting to 58 /// fold instructions like loads and stores, which have no constant expression 59 /// form. 60 /// 61 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, 62 const DataLayout &DL, 63 const TargetLibraryInfo *TLI = nullptr); 64 65 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 66 /// specified operands. If successful, the constant result is returned, if not, 67 /// null is returned. Note that this function can fail when attempting to 68 /// fold instructions like loads and stores, which have no constant expression 69 /// form. 70 /// 71 /// This function doesn't work for compares (use ConstantFoldCompareInstOperands 72 /// for this) and GEPs. 73 Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, 74 ArrayRef<Constant *> Ops, 75 const DataLayout &DL, 76 const TargetLibraryInfo *TLI = nullptr); 77 78 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 79 /// instruction (icmp/fcmp) with the specified operands. If it fails, it 80 /// returns a constant expression of the specified operands. 81 /// 82 Constant * 83 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, 84 Constant *RHS, const DataLayout &DL, 85 const TargetLibraryInfo *TLI = nullptr); 86 87 /// \brief Attempt to constant fold a binary operation with the specified 88 /// operands. If it fails, it returns a constant expression of the specified 89 /// operands. 90 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, 91 Constant *RHS, const DataLayout &DL); 92 93 /// \brief Attempt to constant fold a cast with the specified operand. If it 94 /// fails, it returns a constant expression of the specified operand. 95 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, 96 const DataLayout &DL); 97 98 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue 99 /// instruction with the specified operands and indices. The constant result is 100 /// returned if successful; if not, null is returned. 101 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, 102 ArrayRef<unsigned> Idxs); 103 104 /// \brief Attempt to constant fold an extractvalue instruction with the 105 /// specified operands and indices. The constant result is returned if 106 /// successful; if not, null is returned. 107 Constant *ConstantFoldExtractValueInstruction(Constant *Agg, 108 ArrayRef<unsigned> Idxs); 109 110 /// \brief Attempt to constant fold an extractelement instruction with the 111 /// specified operands and indices. The constant result is returned if 112 /// successful; if not, null is returned. 113 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); 114 115 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 116 /// produce if it is constant and determinable. If this is not determinable, 117 /// return null. 118 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); 119 120 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 121 /// getelementptr constantexpr, return the constant value being addressed by the 122 /// constant expression, or null if something is funny and we can't decide. 123 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); 124 125 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 126 /// indices (with an *implied* zero pointer index that is not in the list), 127 /// return the constant value being addressed by a virtual load, or null if 128 /// something is funny and we can't decide. 129 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, 130 ArrayRef<Constant *> Indices); 131 132 /// canConstantFoldCallTo - Return true if its even possible to fold a call to 133 /// the specified function. 134 bool canConstantFoldCallTo(const Function *F); 135 136 /// ConstantFoldCall - Attempt to constant fold a call to the specified function 137 /// with the specified arguments, returning null if unsuccessful. 138 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands, 139 const TargetLibraryInfo *TLI = nullptr); 140 } 141 142 #endif 143