LLVM 22.0.0git
CSEMIRBuilder.h
Go to the documentation of this file.
1//===-- llvm/CodeGen/GlobalISel/CSEMIRBuilder.h --*- 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/// \file
9/// This file implements a version of MachineIRBuilder which CSEs insts within
10/// a MachineBasicBlock.
11//===----------------------------------------------------------------------===//
12#ifndef LLVM_CODEGEN_GLOBALISEL_CSEMIRBUILDER_H
13#define LLVM_CODEGEN_GLOBALISEL_CSEMIRBUILDER_H
14
17
18namespace llvm {
19
21/// Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
22/// Eg usage.
23///
24/// \code
25/// GISelCSEInfo *Info =
26/// &getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEInfo();
27/// CSEMIRBuilder CB(Builder.getState());
28/// CB.setCSEInfo(Info);
29/// auto A = CB.buildConstant(s32, 42);
30/// auto B = CB.buildConstant(s32, 42);
31/// assert(A == B);
32/// unsigned CReg = MRI.createGenericVirtualRegister(s32);
33/// auto C = CB.buildConstant(CReg, 42);
34/// assert(C->getOpcode() == TargetOpcode::COPY);
35/// \endcode
36///
37/// Explicitly passing in a register would materialize a copy if possible.
38/// CSEMIRBuilder also does trivial constant folding for binary ops.
40
41 /// Returns true if A dominates B (within the same basic block).
42 /// Both iterators must be in the same basic block.
43 //
44 // TODO: Another approach for checking dominance is having two iterators and
45 // making them go towards each other until they meet or reach begin/end. Which
46 // approach is better? Should this even change dynamically? For G_CONSTANTS
47 // most of which will be at the top of the BB, the top down approach would be
48 // a better choice. Does IRTranslator placing constants at the beginning still
49 // make sense? Should this change based on Opcode?
52
53 /// For given ID, find a machineinstr in the CSE Map. If found, check if it
54 /// dominates the current insertion point and if not, move it just before the
55 /// current insertion point and return it. If not found, return Null
56 /// MachineInstrBuilder.
57 MachineInstrBuilder getDominatingInstrForID(FoldingSetNodeID &ID,
58 void *&NodeInsertPos);
59 /// Simple check if we can CSE (we have the CSEInfo) or if this Opcode is
60 /// safe to CSE.
61 bool canPerformCSEForOpc(unsigned Opc) const;
62
63 void profileDstOp(const DstOp &Op, GISelInstProfileBuilder &B) const;
64
65 void profileDstOps(ArrayRef<DstOp> Ops, GISelInstProfileBuilder &B) const {
66 for (const DstOp &Op : Ops)
67 profileDstOp(Op, B);
68 }
69
70 void profileSrcOp(const SrcOp &Op, GISelInstProfileBuilder &B) const;
71
72 void profileSrcOps(ArrayRef<SrcOp> Ops, GISelInstProfileBuilder &B) const {
73 for (const SrcOp &Op : Ops)
74 profileSrcOp(Op, B);
75 }
76
77 void profileMBBOpcode(GISelInstProfileBuilder &B, unsigned Opc) const;
78
79 void profileEverything(unsigned Opc, ArrayRef<DstOp> DstOps,
80 ArrayRef<SrcOp> SrcOps, std::optional<unsigned> Flags,
82
83 // Takes a MachineInstrBuilder and inserts it into the CSEMap using the
84 // NodeInsertPos.
85 MachineInstrBuilder memoizeMI(MachineInstrBuilder MIB, void *NodeInsertPos);
86
87 // If we have can CSE an instruction, but still need to materialize to a VReg,
88 // we emit a copy from the CSE'd inst to the VReg.
89 MachineInstrBuilder generateCopiesIfRequired(ArrayRef<DstOp> DstOps,
91
92 // If we have can CSE an instruction, but still need to materialize to a VReg,
93 // check if we can generate copies. It's not possible to return a single MIB,
94 // while emitting copies to multiple vregs.
95 bool checkCopyToDefsPossible(ArrayRef<DstOp> DstOps);
96
97public:
98 // Pull in base class constructors.
100 // Unhide buildInstr
102 buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps, ArrayRef<SrcOp> SrcOps,
103 std::optional<unsigned> Flag = std::nullopt) override;
104 // Bring in the other overload from the base class.
106
108 const ConstantInt &Val) override;
109
110 // Bring in the other overload from the base class.
113 const ConstantFP &Val) override;
114};
115} // namespace llvm
116#endif
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file declares the MachineIRBuilder class.
static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A, const MachineInstr &B)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
MachineInstrBuilder buildInstr(unsigned Opc, ArrayRef< DstOp > DstOps, ArrayRef< SrcOp > SrcOps, std::optional< unsigned > Flag=std::nullopt) override
MachineIRBuilder()=default
Some constructors for easy use.
MachineInstrBuilder buildFConstant(const DstOp &Res, const ConstantFP &Val) override
Build and insert Res = G_FCONSTANT Val.
MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val) override
Build and insert Res = G_CONSTANT Val.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:330
MachineInstrBundleIterator< const MachineInstr > const_iterator
virtual MachineInstrBuilder buildFConstant(const DstOp &Res, const ConstantFP &Val)
Build and insert Res = G_FCONSTANT Val.
MachineIRBuilder()=default
Some constructors for easy use.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
DWARFExpression::Operation Op