LLVM  15.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 
22 #include <stdint.h>
23 
24 namespace llvm {
25 class APInt;
26 template <typename T> class ArrayRef;
27 class CallBase;
28 class Constant;
29 class DSOLocalEquivalent;
30 class DataLayout;
31 class Function;
32 class GlobalValue;
33 class GlobalVariable;
34 class Instruction;
35 class TargetLibraryInfo;
36 class Type;
37 
38 /// If this constant is a constant offset from a global, return the global and
39 /// the constant. Because of constantexprs, this function is recursive.
40 /// If the global is part of a dso_local_equivalent constant, return it through
41 /// `Equiv` if it is provided.
42 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
43  const DataLayout &DL,
44  DSOLocalEquivalent **DSOEquiv = nullptr);
45 
46 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
47 /// If successful, the constant result is returned, if not, null is returned.
48 /// Note that this fails if not all of the operands are constant. Otherwise,
49 /// this function can only fail when attempting to fold instructions like loads
50 /// and stores, which have no constant expression form.
51 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
52  const TargetLibraryInfo *TLI = nullptr);
53 
54 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
55 /// This function always returns a non-null constant: Either the folding result,
56 /// or the original constant if further folding is not possible.
57 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
58  const TargetLibraryInfo *TLI = nullptr);
59 
60 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
61 /// specified operands. If successful, the constant result is returned, if not,
62 /// null is returned. Note that this function can fail when attempting to
63 /// fold instructions like loads and stores, which have no constant expression
64 /// form.
65 ///
66 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
67  const DataLayout &DL,
68  const TargetLibraryInfo *TLI = nullptr);
69 
70 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
71 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
72 /// returns a constant expression of the specified operands.
73 ///
74 Constant *
76  Constant *RHS, const DataLayout &DL,
77  const TargetLibraryInfo *TLI = nullptr);
78 
79 /// Attempt to constant fold a unary operation with the specified
80 /// operand. If it fails, it returns a constant expression of the specified
81 /// operands.
83  const DataLayout &DL);
84 
85 /// Attempt to constant fold a binary operation with the specified
86 /// operands. If it fails, it returns a constant expression of the specified
87 /// operands.
89  Constant *RHS, const DataLayout &DL);
90 
91 /// Attempt to constant fold a floating point binary operation with the
92 /// specified operands, applying the denormal handling mod to the operands. If
93 /// it fails, it returns a constant expression of the specified operands.
95  Constant *RHS, const DataLayout &DL,
96  const Instruction *I);
97 
98 /// Attempt to constant fold a select instruction with the specified
99 /// operands. The constant result is returned if successful; if not, null is
100 /// returned.
102  Constant *V2);
103 
104 /// Attempt to constant fold a cast with the specified operand. If it
105 /// fails, it returns a constant expression of the specified operand.
106 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
107  const DataLayout &DL);
108 
109 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
110 /// instruction with the specified operands and indices. The constant result is
111 /// returned if successful; if not, null is returned.
113  ArrayRef<unsigned> Idxs);
114 
115 /// Attempt to constant fold an extractvalue instruction with the
116 /// specified operands and indices. The constant result is returned if
117 /// successful; if not, null is returned.
119  ArrayRef<unsigned> Idxs);
120 
121 /// Attempt to constant fold an insertelement instruction with the
122 /// specified operands and indices. The constant result is returned if
123 /// successful; if not, null is returned.
125  Constant *Elt,
126  Constant *Idx);
127 
128 /// Attempt to constant fold an extractelement instruction with the
129 /// specified operands and indices. The constant result is returned if
130 /// successful; if not, null is returned.
132 
133 /// Attempt to constant fold a shufflevector instruction with the
134 /// specified operands and mask. See class ShuffleVectorInst for a description
135 /// of the mask representation. The constant result is returned if successful;
136 /// if not, null is returned.
138  ArrayRef<int> Mask);
139 
140 /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
141 /// the end of C are accessed, they are assumed to be poison.
143  const DataLayout &DL);
144 
145 /// Extract value of C reinterpreted as Ty. Same as previous API with zero
146 /// offset.
148  const DataLayout &DL);
149 
150 /// Return the value that a load from C with offset Offset would produce if it
151 /// is constant and determinable. If this is not determinable, return null.
153  const DataLayout &DL);
154 
155 /// Return the value that a load from C would produce if it is constant and
156 /// determinable. If this is not determinable, return null.
158  const DataLayout &DL);
159 
160 /// If C is a uniform value where all bits are the same (either all zero, all
161 /// ones, all undef or all poison), return the corresponding uniform value in
162 /// the new type. If the value is not uniform or the result cannot be
163 /// represented, return null.
165 
166 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
167 /// the specified function.
168 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
169 
170 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
171 /// with the specified arguments, returning null if unsuccessful.
172 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
173  ArrayRef<Constant *> Operands,
174  const TargetLibraryInfo *TLI = nullptr);
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.
184 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
185 
186 Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
187 }
188 
189 #endif
llvm::ConstantFoldCompareInstOperands
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
Definition: ConstantFolding.cpp:1219
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::ConstantFoldUnaryOpOperand
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Definition: ConstantFolding.cpp:1327
llvm::ConstantFoldFPInstOperands
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I)
Attempt to constant fold a floating point binary operation with the specified operands,...
Definition: ConstantFolding.cpp:1384
llvm::ConstantFoldSelectInstruction
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Definition: ConstantFold.cpp:521
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::ConstantFoldExtractValueInstruction
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Definition: ConstantFold.cpp:775
llvm::IsConstantOffsetFromGlobal
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.
Definition: ConstantFolding.cpp:294
llvm::BitmaskEnumDetail::Mask
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::ConstantFoldBinaryOpOperands
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Definition: ConstantFolding.cpp:1334
llvm::ConstantFoldCastOperand
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Definition: ConstantFolding.cpp:1402
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ConstantFoldCall
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Definition: ConstantFolding.cpp:3186
llvm::canConstantFoldCallTo
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
Definition: ConstantFolding.cpp:1490
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
llvm::ConstantFoldExtractElementInstruction
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
Definition: ConstantFold.cpp:610
Operands
mir Rename Register Operands
Definition: MIRNamerPass.cpp:74
llvm::ConstantFoldLoadThroughBitcast
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
Definition: ConstantFolding.cpp:347
llvm::isMathLibCallNoop
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Definition: ConstantFolding.cpp:3222
llvm::PPC::Predicate
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
uint64_t
llvm::ConstantFoldInsertValueInstruction
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Definition: ConstantFold.cpp:787
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::ConstantFoldLoadFromUniformValue
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Definition: ConstantFolding.cpp:759
TemplateParamKind::Type
@ Type
llvm::ConstantFoldInsertElementInstruction
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
Definition: ConstantFold.cpp:674
llvm::ConstantFoldInstOperands
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Definition: ConstantFolding.cpp:1212
llvm::ConstantFoldLoadFromConst
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Definition: ConstantFolding.cpp:697
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:137
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::ConstantFoldLoadFromConstPtr
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...
Definition: ConstantFolding.cpp:728
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:345
llvm::ConstantFoldShuffleVectorInstruction
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
Definition: ConstantFold.cpp:716
llvm::ConstantFoldConstant
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Definition: ConstantFolding.cpp:1206
llvm::ConstantFoldInstruction
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Definition: ConstantFolding.cpp:1142
llvm::ReadByteArrayFromGlobal
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
Definition: ConstantFolding.cpp:641
llvm::codeview::PublicSymFlags::Function
@ Function