Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 76 | /// Attempt to constant fold a binary operation with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 82 | /// Attempt to constant fold a select instruction with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 88 | /// Attempt to constant fold a cast with the specified operand. If it |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 99 | /// Attempt to constant fold an extractvalue instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 105 | /// Attempt to constant fold an insertelement instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | /// specified operands and indices. The constant result is returned if |
| 107 | /// successful; if not, null is returned. |
| 108 | Constant *ConstantFoldInsertElementInstruction(Constant *Val, |
| 109 | Constant *Elt, |
| 110 | Constant *Idx); |
| 111 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 112 | /// Attempt to constant fold an extractelement instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 113 | /// specified operands and indices. The constant result is returned if |
| 114 | /// successful; if not, null is returned. |
| 115 | Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); |
| 116 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 117 | /// Attempt to constant fold a shufflevector instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | /// specified operands and indices. The constant result is returned if |
| 119 | /// successful; if not, null is returned. |
| 120 | Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, |
| 121 | Constant *Mask); |
| 122 | |
| 123 | /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would |
| 124 | /// produce if it is constant and determinable. If this is not determinable, |
| 125 | /// return null. |
| 126 | Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); |
| 127 | |
| 128 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 129 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 130 | /// constant expression, or null if something is funny and we can't decide. |
| 131 | Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); |
| 132 | |
| 133 | /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr |
| 134 | /// indices (with an *implied* zero pointer index that is not in the list), |
| 135 | /// return the constant value being addressed by a virtual load, or null if |
| 136 | /// something is funny and we can't decide. |
| 137 | Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, |
| 138 | ArrayRef<Constant *> Indices); |
| 139 | |
| 140 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 141 | /// the specified function. |
| 142 | bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F); |
| 143 | |
| 144 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 145 | /// with the specified arguments, returning null if unsuccessful. |
| 146 | Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F, |
| 147 | ArrayRef<Constant *> Operands, |
| 148 | const TargetLibraryInfo *TLI = nullptr); |
| 149 | |
| 150 | /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type |
| 151 | /// returning null if unsuccessful. Can cast pointer to pointer or pointer to |
| 152 | /// integer and vice versa if their sizes are equal. |
| 153 | Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, |
| 154 | const DataLayout &DL); |
| 155 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 156 | /// Check whether the given call has no side-effects. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 157 | /// Specifically checks for math routimes which sometimes set errno. |
| 158 | bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI); |
| 159 | } |
| 160 | |
| 161 | #endif |