LLVM  14.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 namespace llvm {
23 class APInt;
24 template <typename T> class ArrayRef;
25 class CallBase;
26 class Constant;
27 class ConstantExpr;
28 class DSOLocalEquivalent;
29 class DataLayout;
30 class Function;
31 class GlobalValue;
32 class Instruction;
33 class TargetLibraryInfo;
34 class Type;
35 
36 /// If this constant is a constant offset from a global, return the global and
37 /// the constant. Because of constantexprs, this function is recursive.
38 /// If the global is part of a dso_local_equivalent constant, return it through
39 /// `Equiv` if it is provided.
40 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
41  const DataLayout &DL,
42  DSOLocalEquivalent **DSOEquiv = nullptr);
43 
44 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
45 /// If successful, the constant result is returned, if not, null is returned.
46 /// Note that this fails if not all of the operands are constant. Otherwise,
47 /// this function can only fail when attempting to fold instructions like loads
48 /// and stores, which have no constant expression form.
49 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
50  const TargetLibraryInfo *TLI = nullptr);
51 
52 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
53 /// This function always returns a non-null constant: Either the folding result,
54 /// or the original constant if further folding is not possible.
55 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
56  const TargetLibraryInfo *TLI = nullptr);
57 
58 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
59 /// specified operands. If successful, the constant result is returned, if not,
60 /// null is returned. Note that this function can fail when attempting to
61 /// fold instructions like loads and stores, which have no constant expression
62 /// form.
63 ///
64 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
65  const DataLayout &DL,
66  const TargetLibraryInfo *TLI = nullptr);
67 
68 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
69 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
70 /// returns a constant expression of the specified operands.
71 ///
72 Constant *
74  Constant *RHS, const DataLayout &DL,
75  const TargetLibraryInfo *TLI = nullptr);
76 
77 /// Attempt to constant fold a unary operation with the specified
78 /// operand. If it fails, it returns a constant expression of the specified
79 /// operands.
81  const DataLayout &DL);
82 
83 /// Attempt to constant fold a binary operation with the specified
84 /// operands. If it fails, it returns a constant expression of the specified
85 /// operands.
86 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
87  Constant *RHS, const DataLayout &DL);
88 
89 /// Attempt to constant fold a select instruction with the specified
90 /// operands. The constant result is returned if successful; if not, null is
91 /// returned.
93  Constant *V2);
94 
95 /// Attempt to constant fold a cast with the specified operand. If it
96 /// fails, it returns a constant expression of the specified operand.
97 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
98  const DataLayout &DL);
99 
100 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
101 /// instruction with the specified operands and indices. The constant result is
102 /// returned if successful; if not, null is returned.
104  ArrayRef<unsigned> Idxs);
105 
106 /// Attempt to constant fold an extractvalue instruction with the
107 /// specified operands and indices. The constant result is returned if
108 /// successful; if not, null is returned.
110  ArrayRef<unsigned> Idxs);
111 
112 /// Attempt to constant fold an insertelement instruction with the
113 /// specified operands and indices. The constant result is returned if
114 /// successful; if not, null is returned.
116  Constant *Elt,
117  Constant *Idx);
118 
119 /// Attempt to constant fold an extractelement instruction with the
120 /// specified operands and indices. The constant result is returned if
121 /// successful; if not, null is returned.
123 
124 /// Attempt to constant fold a shufflevector instruction with the
125 /// specified operands and mask. See class ShuffleVectorInst for a description
126 /// of the mask representation. The constant result is returned if successful;
127 /// if not, null is returned.
129  ArrayRef<int> Mask);
130 
131 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
132 /// produce if it is constant and determinable. If this is not determinable,
133 /// return null.
134 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
135 
136 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
137 /// getelementptr constantexpr, return the constant value being addressed by the
138 /// constant expression, or null if something is funny and we can't decide.
140  Type *Ty,
141  const DataLayout &DL);
142 
143 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
144 /// indices (with an *implied* zero pointer index that is not in the list),
145 /// return the constant value being addressed by a virtual load, or null if
146 /// something is funny and we can't decide.
148  ArrayRef<Constant *> Indices);
149 
150 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
151 /// the specified function.
152 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
153 
154 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
155 /// with the specified arguments, returning null if unsuccessful.
156 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
157  ArrayRef<Constant *> Operands,
158  const TargetLibraryInfo *TLI = nullptr);
159 
160 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
161 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
162 /// integer and vice versa if their sizes are equal.
164  const DataLayout &DL);
165 
166 /// Check whether the given call has no side-effects.
167 /// Specifically checks for math routimes which sometimes set errno.
168 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
169 }
170 
171 #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:1236
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
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:1323
llvm::ConstantFoldSelectInstruction
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Definition: ConstantFold.cpp:529
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::BitmaskEnumDetail::Mask
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
F
#define F(x, y, z)
Definition: MD5.cpp:56
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:774
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:303
llvm::ConstantFoldLoadThroughGEPIndices
Constant * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant * > Indices)
ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr indices (with an implied zero ...
Definition: ConstantFolding.cpp:1420
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:1330
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:1341
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:3114
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:1436
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:153
llvm::ConstantFoldExtractElementInstruction
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
Definition: ConstantFold.cpp:618
Operands
mir Rename Register Operands
Definition: MIRNamerPass.cpp:78
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:356
llvm::isMathLibCallNoop
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Definition: ConstantFolding.cpp:3150
llvm::PPC::Predicate
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
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:786
I
#define I(x, y, z)
Definition: MD5.cpp:59
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:682
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:1229
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:179
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::ConstantFoldLoadThroughGEPConstantExpr
Constant * ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE, Type *Ty, const DataLayout &DL)
ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a getelementptr constantexpr,...
Definition: ConstantFolding.cpp:1402
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
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:719
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:1223
llvm::ConstantFoldInstruction
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Definition: ConstantFolding.cpp:1159
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::ConstantFoldLoadFromConstPtr
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
Definition: ConstantFolding.cpp:675