xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Analysis/ConstantFolding.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 /// In some cases, constant folding may return one value chosen from a set of
72 /// multiple legal return values. For example, the exact bit pattern of NaN
73 /// results is not guaranteed. Using such a result is usually only valid if
74 /// all uses of the original operation are replaced by the constant-folded
75 /// result. The \p AllowNonDeterministic parameter controls whether this is
76 /// allowed.
77 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
78                                    const DataLayout &DL,
79                                    const TargetLibraryInfo *TLI = nullptr,
80                                    bool AllowNonDeterministic = true);
81 
82 /// Attempt to constant fold a compare instruction (icmp/fcmp) with the
83 /// specified operands. Returns null or a constant expression of the specified
84 /// operands on failure.
85 /// Denormal inputs may be flushed based on the denormal handling mode.
86 Constant *ConstantFoldCompareInstOperands(
87     unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
88     const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
89 
90 /// Attempt to constant fold a unary operation with the specified operand.
91 /// Returns null on failure.
92 Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
93                                      const DataLayout &DL);
94 
95 /// Attempt to constant fold a binary operation with the specified operands.
96 /// Returns null or a constant expression of the specified operands on failure.
97 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
98                                        Constant *RHS, const DataLayout &DL);
99 
100 /// Attempt to constant fold a floating point binary operation with the
101 /// specified operands, applying the denormal handling mod to the operands.
102 /// Returns null or a constant expression of the specified operands on failure.
103 Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
104                                      Constant *RHS, const DataLayout &DL,
105                                      const Instruction *I,
106                                      bool AllowNonDeterministic = true);
107 
108 /// Attempt to flush float point constant according to denormal mode set in the
109 /// instruction's parent function attributes. If so, return a zero with the
110 /// correct sign, otherwise return the original constant. Inputs and outputs to
111 /// floating point instructions can have their mode set separately, so the
112 /// direction is also needed.
113 ///
114 /// If the calling function's "denormal-fp-math" input mode is "dynamic" for the
115 /// floating-point type, returns nullptr for denormal inputs.
116 Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
117                           bool IsOutput);
118 
119 /// Attempt to constant fold a select instruction with the specified
120 /// operands. The constant result is returned if successful; if not, null is
121 /// returned.
122 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
123                                         Constant *V2);
124 
125 /// Attempt to constant fold a cast with the specified operand.  If it
126 /// fails, it returns a constant expression of the specified operand.
127 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
128                                   const DataLayout &DL);
129 
130 /// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
131 /// DestTy is wider or narrower than C. Returns nullptr on failure.
132 Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned,
133                                   const DataLayout &DL);
134 
135 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
136 /// instruction with the specified operands and indices.  The constant result is
137 /// returned if successful; if not, null is returned.
138 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
139                                              ArrayRef<unsigned> Idxs);
140 
141 /// Attempt to constant fold an extractvalue instruction with the
142 /// specified operands and indices.  The constant result is returned if
143 /// successful; if not, null is returned.
144 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
145                                               ArrayRef<unsigned> Idxs);
146 
147 /// Attempt to constant fold an insertelement instruction with the
148 /// specified operands and indices.  The constant result is returned if
149 /// successful; if not, null is returned.
150 Constant *ConstantFoldInsertElementInstruction(Constant *Val,
151                                                Constant *Elt,
152                                                Constant *Idx);
153 
154 /// Attempt to constant fold an extractelement instruction with the
155 /// specified operands and indices.  The constant result is returned if
156 /// successful; if not, null is returned.
157 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
158 
159 /// Attempt to constant fold a shufflevector instruction with the
160 /// specified operands and mask.  See class ShuffleVectorInst for a description
161 /// of the mask representation. The constant result is returned if successful;
162 /// if not, null is returned.
163 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
164                                                ArrayRef<int> Mask);
165 
166 /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
167 /// the end of C are accessed, they are assumed to be poison.
168 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
169                                     const DataLayout &DL);
170 
171 /// Extract value of C reinterpreted as Ty. Same as previous API with zero
172 /// offset.
173 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
174                                     const DataLayout &DL);
175 
176 /// Return the value that a load from C with offset Offset would produce if it
177 /// is constant and determinable. If this is not determinable, return null.
178 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
179                                        const DataLayout &DL);
180 
181 /// Return the value that a load from C would produce if it is constant and
182 /// determinable. If this is not determinable, return null.
183 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
184                                        const DataLayout &DL);
185 
186 /// If C is a uniform value where all bits are the same (either all zero, all
187 /// ones, all undef or all poison), return the corresponding uniform value in
188 /// the new type. If the value is not uniform or the result cannot be
189 /// represented, return null.
190 Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty,
191                                            const DataLayout &DL);
192 
193 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
194 /// the specified function.
195 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
196 
197 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
198 /// with the specified arguments, returning null if unsuccessful.
199 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
200                            ArrayRef<Constant *> Operands,
201                            const TargetLibraryInfo *TLI = nullptr,
202                            bool AllowNonDeterministic = true);
203 
204 Constant *ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS,
205                                       Constant *RHS, Type *Ty,
206                                       Instruction *FMFSource);
207 
208 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
209 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
210 /// integer and vice versa if their sizes are equal.
211 Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
212                                          const DataLayout &DL);
213 
214 /// Check whether the given call has no side-effects.
215 /// Specifically checks for math routimes which sometimes set errno.
216 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
217 
218 Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
219 }
220 
221 #endif
222