1 | //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===// |
2 | // |
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 |
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 | #include <stdint.h> |
23 | |
24 | namespace llvm { |
25 | |
26 | namespace Intrinsic { |
27 | using ID = unsigned; |
28 | } |
29 | |
30 | class APInt; |
31 | template <typename T> class ArrayRef; |
32 | class CallBase; |
33 | class Constant; |
34 | class DSOLocalEquivalent; |
35 | class DataLayout; |
36 | class Function; |
37 | class GlobalValue; |
38 | class GlobalVariable; |
39 | class Instruction; |
40 | class TargetLibraryInfo; |
41 | class Type; |
42 | |
43 | /// If this constant is a constant offset from a global, return the global and |
44 | /// the constant. Because of constantexprs, this function is recursive. |
45 | /// If the global is part of a dso_local_equivalent constant, return it through |
46 | /// `Equiv` if it is provided. |
47 | bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, |
48 | const DataLayout &DL, |
49 | DSOLocalEquivalent **DSOEquiv = nullptr); |
50 | |
51 | /// ConstantFoldInstruction - Try to constant fold the specified instruction. |
52 | /// If successful, the constant result is returned, if not, null is returned. |
53 | /// Note that this fails if not all of the operands are constant. Otherwise, |
54 | /// this function can only fail when attempting to fold instructions like loads |
55 | /// and stores, which have no constant expression form. |
56 | Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, |
57 | const TargetLibraryInfo *TLI = nullptr); |
58 | |
59 | /// ConstantFoldConstant - Fold the constant using the specified DataLayout. |
60 | /// This function always returns a non-null constant: Either the folding result, |
61 | /// or the original constant if further folding is not possible. |
62 | Constant *ConstantFoldConstant(const Constant *C, 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 | Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, |
72 | const DataLayout &DL, |
73 | const TargetLibraryInfo *TLI = nullptr); |
74 | |
75 | /// Attempt to constant fold a compare instruction (icmp/fcmp) with the |
76 | /// specified operands. Returns null or a constant expression of the specified |
77 | /// operands on failure. |
78 | /// Denormal inputs may be flushed based on the denormal handling mode. |
79 | Constant *ConstantFoldCompareInstOperands( |
80 | unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, |
81 | const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr); |
82 | |
83 | /// Attempt to constant fold a unary operation with the specified operand. |
84 | /// Returns null on failure. |
85 | Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, |
86 | const DataLayout &DL); |
87 | |
88 | /// Attempt to constant fold a binary operation with the specified operands. |
89 | /// Returns null or a constant expression of the specified operands on failure. |
90 | Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, |
91 | Constant *RHS, const DataLayout &DL); |
92 | |
93 | /// Attempt to constant fold a floating point binary operation with the |
94 | /// specified operands, applying the denormal handling mod to the operands. |
95 | /// Returns null or a constant expression of the specified operands on failure. |
96 | Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, |
97 | Constant *RHS, const DataLayout &DL, |
98 | const Instruction *I); |
99 | |
100 | /// Attempt to flush float point constant according to denormal mode set in the |
101 | /// instruction's parent function attributes. If so, return a zero with the |
102 | /// correct sign, otherwise return the original constant. Inputs and outputs to |
103 | /// floating point instructions can have their mode set separately, so the |
104 | /// direction is also needed. |
105 | /// |
106 | /// If the calling function's "denormal-fp-math" input mode is "dynamic" for the |
107 | /// floating-point type, returns nullptr for denormal inputs. |
108 | Constant *FlushFPConstant(Constant *Operand, const Instruction *I, |
109 | bool IsOutput); |
110 | |
111 | /// Attempt to constant fold a select instruction with the specified |
112 | /// operands. The constant result is returned if successful; if not, null is |
113 | /// returned. |
114 | Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, |
115 | Constant *V2); |
116 | |
117 | /// Attempt to constant fold a cast with the specified operand. If it |
118 | /// fails, it returns a constant expression of the specified operand. |
119 | Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, |
120 | const DataLayout &DL); |
121 | |
122 | /// Constant fold a zext, sext or trunc, depending on IsSigned and whether the |
123 | /// DestTy is wider or narrower than C. Returns nullptr on failure. |
124 | Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, |
125 | const DataLayout &DL); |
126 | |
127 | /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue |
128 | /// instruction with the specified operands and indices. The constant result is |
129 | /// returned if successful; if not, null is returned. |
130 | Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, |
131 | ArrayRef<unsigned> Idxs); |
132 | |
133 | /// Attempt to constant fold an extractvalue instruction with the |
134 | /// specified operands and indices. The constant result is returned if |
135 | /// successful; if not, null is returned. |
136 | Constant *(Constant *Agg, |
137 | ArrayRef<unsigned> Idxs); |
138 | |
139 | /// Attempt to constant fold an insertelement instruction with the |
140 | /// specified operands and indices. The constant result is returned if |
141 | /// successful; if not, null is returned. |
142 | Constant *ConstantFoldInsertElementInstruction(Constant *Val, |
143 | Constant *Elt, |
144 | Constant *Idx); |
145 | |
146 | /// Attempt to constant fold an extractelement instruction with the |
147 | /// specified operands and indices. The constant result is returned if |
148 | /// successful; if not, null is returned. |
149 | Constant *(Constant *Val, Constant *Idx); |
150 | |
151 | /// Attempt to constant fold a shufflevector instruction with the |
152 | /// specified operands and mask. See class ShuffleVectorInst for a description |
153 | /// of the mask representation. The constant result is returned if successful; |
154 | /// if not, null is returned. |
155 | Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, |
156 | ArrayRef<int> Mask); |
157 | |
158 | /// Extract value of C at the given Offset reinterpreted as Ty. If bits past |
159 | /// the end of C are accessed, they are assumed to be poison. |
160 | Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, |
161 | const DataLayout &DL); |
162 | |
163 | /// Extract value of C reinterpreted as Ty. Same as previous API with zero |
164 | /// offset. |
165 | Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, |
166 | const DataLayout &DL); |
167 | |
168 | /// Return the value that a load from C with offset Offset would produce if it |
169 | /// is constant and determinable. If this is not determinable, return null. |
170 | Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, |
171 | const DataLayout &DL); |
172 | |
173 | /// Return the value that a load from C would produce if it is constant and |
174 | /// determinable. If this is not determinable, return null. |
175 | Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, |
176 | const DataLayout &DL); |
177 | |
178 | /// If C is a uniform value where all bits are the same (either all zero, all |
179 | /// ones, all undef or all poison), return the corresponding uniform value in |
180 | /// the new type. If the value is not uniform or the result cannot be |
181 | /// represented, return null. |
182 | Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, |
183 | const DataLayout &DL); |
184 | |
185 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
186 | /// the specified function. |
187 | bool canConstantFoldCallTo(const CallBase *Call, const Function *F); |
188 | |
189 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
190 | /// with the specified arguments, returning null if unsuccessful. |
191 | Constant *ConstantFoldCall(const CallBase *Call, Function *F, |
192 | ArrayRef<Constant *> Operands, |
193 | const TargetLibraryInfo *TLI = nullptr); |
194 | |
195 | Constant *ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, |
196 | Constant *RHS, Type *Ty, |
197 | Instruction *FMFSource); |
198 | |
199 | /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type |
200 | /// returning null if unsuccessful. Can cast pointer to pointer or pointer to |
201 | /// integer and vice versa if their sizes are equal. |
202 | Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, |
203 | const DataLayout &DL); |
204 | |
205 | /// Check whether the given call has no side-effects. |
206 | /// Specifically checks for math routimes which sometimes set errno. |
207 | bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI); |
208 | |
209 | Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset); |
210 | } |
211 | |
212 | #endif |
213 | |