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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of llvm/include/llvm/Analysis/ConstantFolding.h