LLVM 19.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"
26#include "llvm/IR/Instruction.h"
27
28namespace llvm {
29class Constant;
30
31/// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
32/// values. Also applies target-specific constant folding when not using
33/// InstructionSimplify.
34class InstSimplifyFolder final : public IRBuilderFolder {
35 TargetFolder ConstFolder;
37
38 virtual void anchor();
39
40public:
41 InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
42
43 //===--------------------------------------------------------------------===//
44 // Value-based folders.
45 //
46 // Return an existing value or a constant if the operation can be simplified.
47 // Otherwise return nullptr.
48 //===--------------------------------------------------------------------===//
49
51 Value *RHS) const override {
52 return simplifyBinOp(Opc, LHS, RHS, SQ);
53 }
54
56 bool IsExact) const override {
57 return simplifyBinOp(Opc, LHS, RHS, SQ);
58 }
59
61 bool HasNUW, bool HasNSW) const override {
62 return simplifyBinOp(Opc, LHS, RHS, SQ);
63 }
64
66 FastMathFlags FMF) const override {
67 return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
68 }
69
71 FastMathFlags FMF) const override {
72 return simplifyUnOp(Opc, V, FMF, SQ);
73 }
74
76 return simplifyICmpInst(P, LHS, RHS, SQ);
77 }
78
80 bool IsInBounds = false) const override {
81 return simplifyGEPInst(Ty, Ptr, IdxList, IsInBounds, SQ);
82 }
83
84 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
85 return simplifySelectInst(C, True, False, SQ);
86 }
87
89 ArrayRef<unsigned> IdxList) const override {
90 return simplifyExtractValueInst(Agg, IdxList, SQ);
91 };
92
94 ArrayRef<unsigned> IdxList) const override {
95 return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
96 }
97
98 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
99 return simplifyExtractElementInst(Vec, Idx, SQ);
100 }
101
103 Value *Idx) const override {
104 return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
105 }
106
108 ArrayRef<int> Mask) const override {
110 cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
111 isa<ScalableVectorType>(V1->getType()));
112 return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
113 }
114
116 Type *DestTy) const override {
117 return simplifyCastInst(Op, V, DestTy, SQ);
118 }
119
121 Instruction *FMFSource) const override {
122 return simplifyBinaryIntrinsic(ID, Ty, LHS, RHS, SQ,
123 dyn_cast_if_present<CallBase>(FMFSource));
124 }
125
126 //===--------------------------------------------------------------------===//
127 // Cast/Conversion Operators
128 //===--------------------------------------------------------------------===//
129
130 Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
131 if (C->getType() == DestTy)
132 return C; // avoid calling Fold
133 return ConstFolder.CreatePointerCast(C, DestTy);
134 }
135
137 Type *DestTy) const override {
138 if (C->getType() == DestTy)
139 return C; // avoid calling Fold
140 return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
141 }
142
143 //===--------------------------------------------------------------------===//
144 // Compare Instructions
145 //===--------------------------------------------------------------------===//
146
148 Constant *RHS) const override {
149 return ConstFolder.CreateFCmp(P, LHS, RHS);
150 }
151};
152
153} // end namespace llvm
154
155#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:993
This is an important base class in LLVM.
Definition: Constant.h:41
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:110
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
IRBuilderFolder - Interface for constant folding in IRBuilder.
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) 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 * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) 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 * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Value * FoldExtractElement(Value *Vec, Value *Idx) 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.
Definition: Type.cpp:676
@ 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 * 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 * simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, 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 * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, bool InBounds, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, 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.