LLVM 24.0.0git
ConstantFolding.h
Go to the documentation of this file.
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
24#include <stdint.h>
25
26namespace llvm {
27
28namespace Intrinsic {
29using ID = unsigned;
30}
31
32class APFloat;
33class APInt;
34template <typename T> class ArrayRef;
35class CallBase;
36class Constant;
38class DataLayout;
39class Function;
40class GlobalValue;
41class GlobalVariable;
42class Instruction;
44class Type;
45
46/// If this constant is a constant offset from a global, return the global and
47/// the constant. Because of constantexprs, this function is recursive.
48/// If the global is part of a dso_local_equivalent constant, return it through
49/// `Equiv` if it is provided.
50LLVM_ABI bool
52 const DataLayout &DL,
53 DSOLocalEquivalent **DSOEquiv = nullptr);
54
55/// ConstantFoldInstruction - Try to constant fold the specified instruction.
56/// If successful, the constant result is returned, if not, null is returned.
57/// Note that this fails if not all of the operands are constant. Otherwise,
58/// this function can only fail when attempting to fold instructions like loads
59/// and stores, which have no constant expression form.
62 const TargetLibraryInfo *TLI = nullptr);
63
64/// ConstantFoldConstant - Fold the constant using the specified DataLayout.
65/// This function always returns a non-null constant: Either the folding result,
66/// or the original constant if further folding is not possible.
68 const TargetLibraryInfo *TLI = nullptr);
69
70/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
71/// specified operands. If successful, the constant result is returned, if not,
72/// null is returned. Note that this function can fail when attempting to
73/// fold instructions like loads and stores, which have no constant expression
74/// form.
75///
76/// In some cases, constant folding may return one value chosen from a set of
77/// multiple legal return values. For example, the exact bit pattern of NaN
78/// results is not guaranteed. Using such a result is usually only valid if
79/// all uses of the original operation are replaced by the constant-folded
80/// result. The \p AllowNonDeterministic parameter controls whether this is
81/// allowed.
84 const TargetLibraryInfo *TLI = nullptr, bool AllowNonDeterministic = true);
85
86/// Attempt to constant fold a compare instruction (icmp/fcmp) with the
87/// specified operands. Returns null or a constant expression of the specified
88/// operands on failure.
89/// Denormal inputs may be flushed based on the denormal handling mode.
91 unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
92 const TargetLibraryInfo *TLI = nullptr, const Function *CtxF = nullptr);
93
94/// Attempt to constant fold a unary operation with the specified operand.
95/// Returns null on failure.
97 const DataLayout &DL);
98
99/// Attempt to constant fold a binary operation with the specified operands.
100/// Returns null or a constant expression of the specified operands on failure.
102 Constant *RHS,
103 const DataLayout &DL);
104
105/// Attempt to constant fold a floating point binary operation with the
106/// specified operands, applying the denormal handling mod to the operands.
107/// Returns null or a constant expression of the specified operands on failure.
110 const DataLayout &DL, const Instruction *I,
111 bool AllowNonDeterministic = true);
112
113/// Evaluate \p NativeFP on \p V using the host libm, then convert the result to
114/// \p Ty. Denormal inputs and results are flushed according to \p DenormMode.
115/// Returns nullptr if the host call raised a non-inexact floating-point
116/// exception, or if \p DenormMode has a dynamic input or output mode.
117/// \p DenormMode must be valid.
119ConstantFoldFP(double (*NativeFP)(double), const APFloat &V, Type *Ty,
120 DenormalMode DenormMode = DenormalMode::getIEEE());
121
122/// Attempt to flush float point constant according to denormal mode set in the
123/// instruction's parent function attributes. If so, return a zero with the
124/// correct sign, otherwise return the original constant. Inputs and outputs to
125/// floating point instructions can have their mode set separately, so the
126/// direction is also needed.
127///
128/// If the calling function's denormal_fpenv input mode is dynamic for the
129/// floating-point type, returns nullptr for denormal inputs.
130LLVM_ABI Constant *FlushFPConstant(Constant *Operand, const Function *CtxF,
131 bool IsOutput);
132
133/// Attempt to constant fold a cast with the specified operand. If it
134/// fails, it returns a constant expression of the specified operand.
136 Type *DestTy, const DataLayout &DL);
137
138/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
139/// DestTy is wider or narrower than C. Returns nullptr on failure.
141 bool IsSigned, const DataLayout &DL);
142
143/// Attempt to constant fold a bitinsert instruction with the specified
144/// operands. The constant result is returned if successful. Otherwise, null is
145/// returned.
148 const DataLayout &DL);
149
150/// Attempt to constant fold a bitextract instruction with the specified result
151/// type and operands. The constant result is returned if successful.
152/// Otherwise, null is returned.
155 const DataLayout &DL);
156
157/// Extract value of C at the given Offset reinterpreted as Ty. If bits past
158/// the end of C are accessed, they are assumed to be poison.
160 const APInt &Offset,
161 const DataLayout &DL);
162
163/// Extract value of C reinterpreted as Ty. Same as previous API with zero
164/// offset.
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.
172 const DataLayout &DL);
173
174/// Return the value that a load from C would produce if it is constant and
175/// determinable. If this is not determinable, return null.
177 const DataLayout &DL);
178
179/// If C is a uniform value where all bits are the same (either all zero, all
180/// ones, all undef or all poison), return the corresponding uniform value in
181/// the new type. If the value is not uniform or the result cannot be
182/// represented, return null.
184 const DataLayout &DL);
185
186/// canConstantFoldCallTo - Return true if its even possible to fold a call to
187/// the specified function.
189 const TargetLibraryInfo *TLI = nullptr);
190
191/// ConstantFoldCall - Attempt to constant fold a call to the specified function
192/// with the specified arguments, returning null if unsuccessful.
195 const TargetLibraryInfo *TLI = nullptr,
196 bool AllowNonDeterministic = true);
197
200 const DataLayout &DL,
201 const Function *CtxF = nullptr);
202
203/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
204/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
205/// integer and vice versa if their sizes are equal.
207 const DataLayout &DL);
208
209/// Check whether the given call has no side-effects.
210/// Specifically checks for math routimes which sometimes set errno.
212 const TargetLibraryInfo *TLI);
213
216
218 bool NNeg = false;
219 bool NUW = false;
220 bool NSW = false;
221};
222
223/// Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or
224/// CastOp(InvC) is a refined value of undefined C. Will try best to
225/// preserve the flags.
227 unsigned CastOp, const DataLayout &DL,
228 PreservedCastFlags *Flags = nullptr);
229
232 PreservedCastFlags *Flags = nullptr);
233
235 const DataLayout &DL,
236 PreservedCastFlags *Flags = nullptr);
237} // namespace llvm
238
239#endif
const TargetLowering & TLI
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
Utilities for dealing with flags related to floating point properties and mode controls.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
SI Fold Operands
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This is an important base class in LLVM.
Definition Constant.h:44
Wrapper for a function that represents a value that functionally represents the original function.
Definition Constants.h:1143
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
CallInst * Call
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
LLVM_ABI Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
LLVM_ABI bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
LLVM_ABI bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Constant * ConstantFoldBitInsertOperands(Constant *Base, Constant *Val, Constant *Offset, const DataLayout &DL)
Attempt to constant fold a bitinsert instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F, const TargetLibraryInfo *TLI=nullptr)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
LLVM_ABI Constant * ConstantFoldIntrinsic(Intrinsic::ID ID, ArrayRef< Constant * > Ops, Type *Ty, const DataLayout &DL, const Function *CtxF=nullptr)
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Function *CtxF=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_ABI Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI Constant * getLosslessUnsignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Constant * getLosslessSignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI Constant * FlushFPConstant(Constant *Operand, const Function *CtxF, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
LLVM_ABI Constant * ConstantFoldBitExtractOperands(Type *Ty, Constant *Src, Constant *Offset, const DataLayout &DL)
Attempt to constant fold a bitextract instruction with the specified result type and operands.
DWARFExpression::Operation Op
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI Constant * ConstantFoldFP(double(*NativeFP)(double), const APFloat &V, Type *Ty, DenormalMode DenormMode=DenormalMode::getIEEE())
Evaluate NativeFP on V using the host libm, then convert the result to Ty.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getIEEE()