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; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 28 | class DSOLocalEquivalent; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 38 | /// If the global is part of a dso_local_equivalent constant, return it through |
| 39 | /// `Equiv` if it is provided. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 40 | bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | const DataLayout &DL, |
| 42 | DSOLocalEquivalent **DSOEquiv = nullptr); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 43 | |
| 44 | /// ConstantFoldInstruction - Try to constant fold the specified instruction. |
| 45 | /// If successful, the constant result is returned, if not, null is returned. |
| 46 | /// Note that this fails if not all of the operands are constant. Otherwise, |
| 47 | /// this function can only fail when attempting to fold instructions like loads |
| 48 | /// and stores, which have no constant expression form. |
| 49 | Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, |
| 50 | const TargetLibraryInfo *TLI = nullptr); |
| 51 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 52 | /// ConstantFoldConstant - Fold the constant using the specified DataLayout. |
| 53 | /// This function always returns a non-null constant: Either the folding result, |
| 54 | /// or the original constant if further folding is not possible. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL, |
| 56 | const TargetLibraryInfo *TLI = nullptr); |
| 57 | |
| 58 | /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the |
| 59 | /// specified operands. If successful, the constant result is returned, if not, |
| 60 | /// null is returned. Note that this function can fail when attempting to |
| 61 | /// fold instructions like loads and stores, which have no constant expression |
| 62 | /// form. |
| 63 | /// |
| 64 | Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, |
| 65 | const DataLayout &DL, |
| 66 | const TargetLibraryInfo *TLI = nullptr); |
| 67 | |
| 68 | /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare |
| 69 | /// instruction (icmp/fcmp) with the specified operands. If it fails, it |
| 70 | /// returns a constant expression of the specified operands. |
| 71 | /// |
| 72 | Constant * |
| 73 | ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, |
| 74 | Constant *RHS, const DataLayout &DL, |
| 75 | const TargetLibraryInfo *TLI = nullptr); |
| 76 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 77 | /// Attempt to constant fold a unary operation with the specified |
| 78 | /// operand. If it fails, it returns a constant expression of the specified |
| 79 | /// operands. |
| 80 | Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, |
| 81 | const DataLayout &DL); |
| 82 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 83 | /// Attempt to constant fold a binary operation with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | /// operands. If it fails, it returns a constant expression of the specified |
| 85 | /// operands. |
| 86 | Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, |
| 87 | Constant *RHS, const DataLayout &DL); |
| 88 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 89 | /// Attempt to constant fold a select instruction with the specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 90 | /// operands. The constant result is returned if successful; if not, null is |
| 91 | /// returned. |
| 92 | Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, |
| 93 | Constant *V2); |
| 94 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | /// Attempt to constant fold a cast with the specified operand. If it |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | /// fails, it returns a constant expression of the specified operand. |
| 97 | Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, |
| 98 | const DataLayout &DL); |
| 99 | |
| 100 | /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue |
| 101 | /// instruction with the specified operands and indices. The constant result is |
| 102 | /// returned if successful; if not, null is returned. |
| 103 | Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, |
| 104 | ArrayRef<unsigned> Idxs); |
| 105 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 106 | /// Attempt to constant fold an extractvalue instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 107 | /// specified operands and indices. The constant result is returned if |
| 108 | /// successful; if not, null is returned. |
| 109 | Constant *ConstantFoldExtractValueInstruction(Constant *Agg, |
| 110 | ArrayRef<unsigned> Idxs); |
| 111 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 112 | /// Attempt to constant fold an insertelement 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 *ConstantFoldInsertElementInstruction(Constant *Val, |
| 116 | Constant *Elt, |
| 117 | Constant *Idx); |
| 118 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 119 | /// Attempt to constant fold an extractelement instruction with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | /// specified operands and indices. The constant result is returned if |
| 121 | /// successful; if not, null is returned. |
| 122 | Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); |
| 123 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 124 | /// Attempt to constant fold a shufflevector instruction with the |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 125 | /// specified operands and mask. See class ShuffleVectorInst for a description |
| 126 | /// of the mask representation. The constant result is returned if successful; |
| 127 | /// if not, null is returned. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 128 | Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 129 | ArrayRef<int> Mask); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 130 | |
| 131 | /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would |
| 132 | /// produce if it is constant and determinable. If this is not determinable, |
| 133 | /// return null. |
| 134 | Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); |
| 135 | |
| 136 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 137 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 138 | /// constant expression, or null if something is funny and we can't decide. |
| 139 | Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); |
| 140 | |
| 141 | /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr |
| 142 | /// indices (with an *implied* zero pointer index that is not in the list), |
| 143 | /// return the constant value being addressed by a virtual load, or null if |
| 144 | /// something is funny and we can't decide. |
| 145 | Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, |
| 146 | ArrayRef<Constant *> Indices); |
| 147 | |
| 148 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 149 | /// the specified function. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 150 | bool canConstantFoldCallTo(const CallBase *Call, const Function *F); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | |
| 152 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 153 | /// with the specified arguments, returning null if unsuccessful. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 154 | Constant *ConstantFoldCall(const CallBase *Call, Function *F, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | ArrayRef<Constant *> Operands, |
| 156 | const TargetLibraryInfo *TLI = nullptr); |
| 157 | |
| 158 | /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type |
| 159 | /// returning null if unsuccessful. Can cast pointer to pointer or pointer to |
| 160 | /// integer and vice versa if their sizes are equal. |
| 161 | Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, |
| 162 | const DataLayout &DL); |
| 163 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 164 | /// Check whether the given call has no side-effects. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 165 | /// Specifically checks for math routimes which sometimes set errno. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 166 | bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #endif |