LLVM 22.0.0git
InstSimplifyFolder.h
Go to the documentation of this file.
1//===- InstSimplifyFolder.h - InstSimplify folding helper --------*- 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 defines the InstSimplifyFolder class, a helper for IRBuilder.
10// It provides IRBuilder with a set of methods for folding operations to
11// existing values using InstructionSimplify. At the moment, only a subset of
12// the implementation uses InstructionSimplify. The rest of the implementation
13// only folds constants.
14//
15// The folder also applies target-specific constant folding.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
20#define LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
21
22#include "llvm/ADT/ArrayRef.h"
27#include "llvm/IR/Instruction.h"
29
30namespace llvm {
31class Constant;
32
33/// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
34/// values. Also applies target-specific constant folding when not using
35/// InstructionSimplify.
37 TargetFolder ConstFolder;
39
41
42public:
43 explicit InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
44
45 //===--------------------------------------------------------------------===//
46 // Value-based folders.
47 //
48 // Return an existing value or a constant if the operation can be simplified.
49 // Otherwise return nullptr.
50 //===--------------------------------------------------------------------===//
51
53 Value *RHS) const override {
54 return simplifyBinOp(Opc, LHS, RHS, SQ);
55 }
56
58 bool IsExact) const override {
59 return simplifyBinOp(Opc, LHS, RHS, SQ);
60 }
61
63 bool HasNUW, bool HasNSW) const override {
64 return simplifyBinOp(Opc, LHS, RHS, SQ);
65 }
66
68 FastMathFlags FMF) const override {
69 return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
70 }
71
73 FastMathFlags FMF) const override {
74 return simplifyUnOp(Opc, V, FMF, SQ);
75 }
76
78 return simplifyCmpInst(P, LHS, RHS, SQ);
79 }
80
82 GEPNoWrapFlags NW) const override {
83 return simplifyGEPInst(Ty, Ptr, IdxList, NW, SQ);
84 }
85
86 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
87 return simplifySelectInst(C, True, False, SQ);
88 }
89
91 ArrayRef<unsigned> IdxList) const override {
92 return simplifyExtractValueInst(Agg, IdxList, SQ);
93 };
94
96 ArrayRef<unsigned> IdxList) const override {
97 return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
98 }
99
100 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
101 return simplifyExtractElementInst(Vec, Idx, SQ);
102 }
103
105 Value *Idx) const override {
106 return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
107 }
108
110 ArrayRef<int> Mask) const override {
111 Type *RetTy = VectorType::get(
112 cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
113 isa<ScalableVectorType>(V1->getType()));
114 return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
115 }
116
118 Type *DestTy) const override {
119 return simplifyCastInst(Op, V, DestTy, SQ);
120 }
121
123 Instruction *FMFSource) const override {
124 return simplifyBinaryIntrinsic(ID, Ty, LHS, RHS, SQ,
125 dyn_cast_if_present<CallBase>(FMFSource));
126 }
127
128 //===--------------------------------------------------------------------===//
129 // Cast/Conversion Operators
130 //===--------------------------------------------------------------------===//
131
132 Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
133 if (C->getType() == DestTy)
134 return C; // avoid calling Fold
135 return ConstFolder.CreatePointerCast(C, DestTy);
136 }
137
139 Type *DestTy) const override {
140 if (C->getType() == DestTy)
141 return C; // avoid calling Fold
142 return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
143 }
144};
145
146} // end namespace llvm
147
148#endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION()
\macro LLVM_VIRTUAL_ANCHOR_FUNCTION This macro is used to adhere to LLVM's policy that each class wit...
Definition: Compiler.h:728
#define LLVM_ABI
Definition: Compiler.h:213
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &, unsigned)
Given operands for an ExtractValueInst, see if we can fold the result.
static Value * simplifySelectInst(Value *, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a SelectInst, see if we can fold the result.
static Value * simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
static Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q, unsigned)
Given operands for an ExtractElementInst, see if we can fold the result.
static Value * simplifyCmpInst(CmpPredicate, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a CmpInst, see if we can fold the result.
static Value * simplifyGEPInst(Type *, Value *, ArrayRef< Value * >, GEPNoWrapFlags, const SimplifyQuery &, unsigned)
Given operands for an GetElementPtrInst, see if we can fold the result.
static Value * simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, unsigned)
static Value * simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a BinaryOperator, see if we can fold the result.
static Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q, unsigned)
Given operands for an InsertValueInst, see if we can fold the result.
#define P(N)
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
This is an important base class in LLVM.
Definition: Constant.h:43
This class represents an Operation in the Expression.
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
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
Represents flags for the getelementptr instruction/expression.
IRBuilderFolder - Interface for constant folding in IRBuilder.
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, GEPNoWrapFlags NW) const override
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
InstSimplifyFolder(const DataLayout &DL)
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Value * FoldCast(Instruction::CastOps Op, Value *V, Type *DestTy) const override
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Value * FoldCmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Value * FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty, Instruction *FMFSource) const override
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Value * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Value * CreatePointerCast(Constant *C, Type *DestTy) const override
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:35
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Value * simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, Value *Op0, Value *Op1, const SimplifyQuery &Q, const CallBase *Call)
Given operands for a BinaryIntrinsic, fold the result or return null.
LLVM_ABI Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.