LLVM 20.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"
28
29namespace llvm {
30class Constant;
31
32/// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
33/// values. Also applies target-specific constant folding when not using
34/// InstructionSimplify.
35class InstSimplifyFolder final : public IRBuilderFolder {
36 TargetFolder ConstFolder;
38
39 virtual void anchor();
40
41public:
42 explicit InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
43
44 //===--------------------------------------------------------------------===//
45 // Value-based folders.
46 //
47 // Return an existing value or a constant if the operation can be simplified.
48 // Otherwise return nullptr.
49 //===--------------------------------------------------------------------===//
50
52 Value *RHS) const override {
53 return simplifyBinOp(Opc, LHS, RHS, SQ);
54 }
55
57 bool IsExact) const override {
58 return simplifyBinOp(Opc, LHS, RHS, SQ);
59 }
60
62 bool HasNUW, bool HasNSW) const override {
63 return simplifyBinOp(Opc, LHS, RHS, SQ);
64 }
65
67 FastMathFlags FMF) const override {
68 return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
69 }
70
72 FastMathFlags FMF) const override {
73 return simplifyUnOp(Opc, V, FMF, SQ);
74 }
75
77 return simplifyCmpInst(P, LHS, RHS, SQ);
78 }
79
81 GEPNoWrapFlags NW) const override {
82 return simplifyGEPInst(Ty, Ptr, IdxList, NW, SQ);
83 }
84
85 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
86 return simplifySelectInst(C, True, False, SQ);
87 }
88
90 ArrayRef<unsigned> IdxList) const override {
91 return simplifyExtractValueInst(Agg, IdxList, SQ);
92 };
93
95 ArrayRef<unsigned> IdxList) const override {
96 return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
97 }
98
99 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
100 return simplifyExtractElementInst(Vec, Idx, SQ);
101 }
102
104 Value *Idx) const override {
105 return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
106 }
107
109 ArrayRef<int> Mask) const override {
111 cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
112 isa<ScalableVectorType>(V1->getType()));
113 return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
114 }
115
117 Type *DestTy) const override {
118 return simplifyCastInst(Op, V, DestTy, SQ);
119 }
120
122 Instruction *FMFSource) const override {
123 return simplifyBinaryIntrinsic(ID, Ty, LHS, RHS, SQ,
124 dyn_cast_if_present<CallBase>(FMFSource));
125 }
126
127 //===--------------------------------------------------------------------===//
128 // Cast/Conversion Operators
129 //===--------------------------------------------------------------------===//
130
131 Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
132 if (C->getType() == DestTy)
133 return C; // avoid calling Fold
134 return ConstFolder.CreatePointerCast(C, DestTy);
135 }
136
138 Type *DestTy) const override {
139 if (C->getType() == DestTy)
140 return C; // avoid calling Fold
141 return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
142 }
143};
144
145} // end namespace llvm
146
147#endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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
#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:673
This is an important base class in LLVM.
Definition: Constant.h:42
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
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
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:34
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:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
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.
Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.