LLVM 22.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
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;
36class DataLayout;
37class Function;
38class GlobalValue;
39class GlobalVariable;
40class Instruction;
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
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.
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.
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.
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.
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.
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.
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.
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.
120 bool IsOutput);
121
122/// Attempt to constant fold a cast with the specified operand. If it
123/// fails, it returns a constant expression of the specified operand.
125 Type *DestTy, const DataLayout &DL);
126
127/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
128/// DestTy is wider or narrower than C. Returns nullptr on failure.
130 bool IsSigned, const DataLayout &DL);
131
132/// Extract value of C at the given Offset reinterpreted as Ty. If bits past
133/// the end of C are accessed, they are assumed to be poison.
135 const APInt &Offset,
136 const DataLayout &DL);
137
138/// Extract value of C reinterpreted as Ty. Same as previous API with zero
139/// offset.
141 const DataLayout &DL);
142
143/// Return the value that a load from C with offset Offset would produce if it
144/// is constant and determinable. If this is not determinable, return null.
147 const DataLayout &DL);
148
149/// Return the value that a load from C would produce if it is constant and
150/// determinable. If this is not determinable, return null.
152 const DataLayout &DL);
153
154/// If C is a uniform value where all bits are the same (either all zero, all
155/// ones, all undef or all poison), return the corresponding uniform value in
156/// the new type. If the value is not uniform or the result cannot be
157/// represented, return null.
159 const DataLayout &DL);
160
161/// canConstantFoldCallTo - Return true if its even possible to fold a call to
162/// the specified function.
164
165/// ConstantFoldCall - Attempt to constant fold a call to the specified function
166/// with the specified arguments, returning null if unsuccessful.
168 ArrayRef<Constant *> Operands,
169 const TargetLibraryInfo *TLI = nullptr,
170 bool AllowNonDeterministic = true);
171
173 Constant *RHS, Type *Ty,
175
176/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
177/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
178/// integer and vice versa if their sizes are equal.
180 const DataLayout &DL);
181
182/// Check whether the given call has no side-effects.
183/// Specifically checks for math routimes which sometimes set errno.
185 const TargetLibraryInfo *TLI);
186
188 uint64_t Offset);
189
191 bool NNeg = false;
192 bool NUW = false;
193 bool NSW = false;
194};
195
196/// Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or
197/// CastOp(InvC) is a refined value of undefined C. Will try best to
198/// preserve the flags.
200 unsigned CastOp, const DataLayout &DL,
201 PreservedCastFlags *Flags = nullptr);
202
205 PreservedCastFlags *Flags = nullptr);
206
208 const DataLayout &DL,
209 PreservedCastFlags *Flags = nullptr);
210} // namespace llvm
211
212#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:213
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
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:43
Wrapper for a function that represents a value that functionally represents the original function.
Definition Constants.h:952
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
This provides a helper for copying FMF from an instruction or setting specified flags.
Definition IRBuilder.h:93
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:45
CallInst * Call
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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:477
LLVM_ABI Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
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 bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
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 * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) 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 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 * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
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.
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...