Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares routines for folding instructions into constants when all |
| 10 | // operands are constants, for example "sub i32 1, 0" -> "1". |
| 11 | // |
| 12 | // Also, to supplement the basic VMCore ConstantExpr simplifications, |
| 13 | // this file declares some additional folding routines that can make use of |
| 14 | // DataLayout information. These functions cannot go in VMCore due to library |
| 15 | // dependency issues. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H |
| 20 | #define LLVM_ANALYSIS_CONSTANTFOLDING_H |
| 21 | |
| 22 | namespace llvm { |
| 23 | class APInt; |
| 24 | template <typename T> class ArrayRef; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 25 | class CallBase; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | class Constant; |
| 27 | class ConstantExpr; |
| 28 | class ConstantVector; |
| 29 | class DataLayout; |
| 30 | class Function; |
| 31 | class GlobalValue; |
| 32 | class Instruction; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 33 | class TargetLibraryInfo; |
| 34 | class Type; |
| 35 | |
| 36 | /// If this constant is a constant offset from a global, return the global and |
| 37 | /// the constant. Because of constantexprs, this function is recursive. |
| 38 | bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, |
| 39 | const DataLayout &DL); |
| 40 | |
| 41 | /// ConstantFoldInstruction - Try to constant fold the specified instruction. |
| 42 | /// If successful, the constant result is returned, if not, null is returned. |
| 43 | /// Note that this fails if not all of the operands are constant. Otherwise, |
| 44 | /// this function can only fail when attempting to fold instructions like loads |
| 45 | /// and stores, which have no constant expression form. |
| 46 | Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, |
| 47 | const TargetLibraryInfo *TLI = nullptr); |
| 48 | |
| 49 | /// ConstantFoldConstant - Attempt to fold the constant using the |
| 50 | /// specified DataLayout. |
| 51 | /// If successful, the constant result is returned, if not, null is returned. |
| 52 | Constant *ConstantFoldConstant(const Constant *C, 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 | /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare |
| 66 | /// instruction (icmp/fcmp) with the specified operands. If it fails, it |
| 67 | /// returns a constant expression of the specified operands. |
| 68 | /// |
| 69 | Constant * |
| 70 | ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, |
| 71 | Constant *RHS, const DataLayout &DL, |
| 72 | const TargetLibraryInfo *TLI = nullptr); |
| 73 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 74 | /// Attempt to constant fold a binary operation with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 75 | /// operands. If it fails, it returns a constant expression of the specified |
| 76 | /// operands. |
| 77 | Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, |
| 78 | Constant *RHS, const DataLayout &DL); |
| 79 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | /// Attempt to constant fold a select instruction with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | /// operands. The constant result is returned if successful; if not, null is |
| 82 | /// returned. |
| 83 | Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, |
| 84 | Constant *V2); |
| 85 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | /// Attempt to constant fold a cast with the specified operand. If it |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | /// fails, it returns a constant expression of the specified operand. |
| 88 | Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, |
| 89 | const DataLayout &DL); |
| 90 | |
| 91 | /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue |
| 92 | /// instruction with the specified operands and indices. The constant result is |
| 93 | /// returned if successful; if not, null is returned. |
| 94 | Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, |
| 95 | ArrayRef<unsigned> Idxs); |
| 96 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 97 | /// Attempt to constant fold an extractvalue instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 98 | /// specified operands and indices. The constant result is returned if |
| 99 | /// successful; if not, null is returned. |
| 100 | Constant *ConstantFoldExtractValueInstruction(Constant *Agg, |
| 101 | ArrayRef<unsigned> Idxs); |
| 102 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 103 | /// Attempt to constant fold an insertelement instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | /// specified operands and indices. The constant result is returned if |
| 105 | /// successful; if not, null is returned. |
| 106 | Constant *ConstantFoldInsertElementInstruction(Constant *Val, |
| 107 | Constant *Elt, |
| 108 | Constant *Idx); |
| 109 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 110 | /// Attempt to constant fold an extractelement instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 115 | /// Attempt to constant fold a shufflevector instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 116 | /// specified operands and indices. The constant result is returned if |
| 117 | /// successful; if not, null is returned. |
| 118 | Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, |
| 119 | Constant *Mask); |
| 120 | |
| 121 | /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would |
| 122 | /// produce if it is constant and determinable. If this is not determinable, |
| 123 | /// return null. |
| 124 | Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); |
| 125 | |
| 126 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 127 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 128 | /// constant expression, or null if something is funny and we can't decide. |
| 129 | Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); |
| 130 | |
| 131 | /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr |
| 132 | /// indices (with an *implied* zero pointer index that is not in the list), |
| 133 | /// return the constant value being addressed by a virtual load, or null if |
| 134 | /// something is funny and we can't decide. |
| 135 | Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, |
| 136 | ArrayRef<Constant *> Indices); |
| 137 | |
| 138 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 139 | /// the specified function. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 140 | bool canConstantFoldCallTo(const CallBase *Call, const Function *F); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 141 | |
| 142 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 143 | /// with the specified arguments, returning null if unsuccessful. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 144 | Constant *ConstantFoldCall(const CallBase *Call, Function *F, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 145 | ArrayRef<Constant *> Operands, |
| 146 | const TargetLibraryInfo *TLI = nullptr); |
| 147 | |
| 148 | /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type |
| 149 | /// returning null if unsuccessful. Can cast pointer to pointer or pointer to |
| 150 | /// integer and vice versa if their sizes are equal. |
| 151 | Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, |
| 152 | const DataLayout &DL); |
| 153 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 154 | /// Check whether the given call has no side-effects. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | /// Specifically checks for math routimes which sometimes set errno. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 156 | bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | #endif |