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.
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 /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
132 /// the end of C are accessed, they are assumed to be poison.
134  const DataLayout &DL);
135 
136 /// Extract value of C reinterpreted as Ty. Same as previous API with zero
137 /// offset.
139  const DataLayout &DL);
140 
141 /// Return the value that a load from C with offset Offset would produce if it
142 /// is constant and determinable. If this is not determinable, return null.
144  const DataLayout &DL);
145 
146 /// Return the value that a load from C would produce if it is constant and
147 /// determinable. If this is not determinable, return null.
149  const DataLayout &DL);
150 
151 /// If C is a uniform value where all bits are the same (either all zero, all
152 /// ones, all undef or all poison), return the corresponding uniform value in
153 /// the new type. If the value is not uniform or the result cannot be
154 /// represented, return null.
156 
157 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
158 /// the specified function.
159 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
160 
161 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
162 /// with the specified arguments, returning null if unsuccessful.
163 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
164  ArrayRef<Constant *> Operands,
165  const TargetLibraryInfo *TLI = nullptr);
166 
167 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
168 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
169 /// integer and vice versa if their sizes are equal.
171  const DataLayout &DL);
172 
173 /// Check whether the given call has no side-effects.
174 /// Specifically checks for math routimes which sometimes set errno.
175 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
176 }
177 
178 #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:1185
llvm
This is an optimization pass for GlobalISel generic memory operations.
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:1293
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:80
RHS
Value * RHS
Definition: X86PartialReduction.cpp:74
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: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:778
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:295
LHS
Value * LHS
Definition: X86PartialReduction.cpp:73
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:1300
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:1311
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:3044
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:1386
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:348
llvm::isMathLibCallNoop
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Definition: ConstantFolding.cpp:3080
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:790
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:726
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:1178
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:664
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:179
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:695
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:325
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:1172
llvm::ConstantFoldInstruction
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Definition: ConstantFolding.cpp:1108
llvm::codeview::PublicSymFlags::Function
@ Function