LLVM 22.0.0git
LowerTypeTests.h
Go to the documentation of this file.
1//===- LowerTypeTests.h - type metadata lowering pass -----------*- 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 parts of the type test lowering pass implementation that
10// may be usefully unit tested.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
15#define LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
16
18#include "llvm/IR/PassManager.h"
20#include <cstdint>
21#include <cstring>
22#include <limits>
23#include <set>
24#include <vector>
25
26namespace llvm {
27
28class Module;
30class raw_ostream;
31
32namespace lowertypetests {
33
34struct BitSetInfo {
35 // The indices of the set bits in the bitset.
36 std::set<uint64_t> Bits;
37
38 // The byte offset into the combined global represented by the bitset.
40
41 // The size of the bitset in bits.
43
44 // Log2 alignment of the bit set relative to the combined global.
45 // For example, a log2 alignment of 3 means that bits in the bitset
46 // represent addresses 8 bytes apart.
47 unsigned AlignLog2;
48
49 bool isSingleOffset() const {
50 return Bits.size() == 1;
51 }
52
53 bool isAllOnes() const {
54 return Bits.size() == BitSize;
55 }
56
58
59 LLVM_ABI void print(raw_ostream &OS) const;
60};
61
64 uint64_t Min = std::numeric_limits<uint64_t>::max();
66
68 if (!Offsets.empty()) {
69 auto [MinIt, MaxIt] = std::minmax_element(Offsets.begin(), Offsets.end());
70 Min = *MinIt;
71 Max = *MaxIt;
72 }
73 }
74
76};
77
78/// This class implements a layout algorithm for globals referenced by bit sets
79/// that tries to keep members of small bit sets together. This can
80/// significantly reduce bit set sizes in many cases.
81///
82/// It works by assembling fragments of layout from sets of referenced globals.
83/// Each set of referenced globals causes the algorithm to create a new
84/// fragment, which is assembled by appending each referenced global in the set
85/// into the fragment. If a referenced global has already been referenced by an
86/// fragment created earlier, we instead delete that fragment and append its
87/// contents into the fragment we are assembling.
88///
89/// By starting with the smallest fragments, we minimize the size of the
90/// fragments that are copied into larger fragments. This is most intuitively
91/// thought about when considering the case where the globals are virtual tables
92/// and the bit sets represent their derived classes: in a single inheritance
93/// hierarchy, the optimum layout would involve a depth-first search of the
94/// class hierarchy (and in fact the computed layout ends up looking a lot like
95/// a DFS), but a naive DFS would not work well in the presence of multiple
96/// inheritance. This aspect of the algorithm ends up fitting smaller
97/// hierarchies inside larger ones where that would be beneficial.
98///
99/// For example, consider this class hierarchy:
100///
101/// A B
102/// \ / | \
103/// C D E
104///
105/// We have five bit sets: bsA (A, C), bsB (B, C, D, E), bsC (C), bsD (D) and
106/// bsE (E). If we laid out our objects by DFS traversing B followed by A, our
107/// layout would be {B, C, D, E, A}. This is optimal for bsB as it needs to
108/// cover the only 4 objects in its hierarchy, but not for bsA as it needs to
109/// cover 5 objects, i.e. the entire layout. Our algorithm proceeds as follows:
110///
111/// Add bsC, fragments {{C}}
112/// Add bsD, fragments {{C}, {D}}
113/// Add bsE, fragments {{C}, {D}, {E}}
114/// Add bsA, fragments {{A, C}, {D}, {E}}
115/// Add bsB, fragments {{B, A, C, D, E}}
116///
117/// This layout is optimal for bsA, as it now only needs to cover two (i.e. 3
118/// fewer) objects, at the cost of bsB needing to cover 1 more object.
119///
120/// The bit set lowering pass assigns an object index to each object that needs
121/// to be laid out, and calls addFragment for each bit set passing the object
122/// indices of its referenced globals. It then assembles a layout from the
123/// computed layout in the Fragments field.
125 /// The computed layout. Each element of this vector contains a fragment of
126 /// layout (which may be empty) consisting of object indices.
127 std::vector<std::vector<uint64_t>> Fragments;
128
129 /// Mapping from object index to fragment index.
130 std::vector<uint64_t> FragmentMap;
131
133 : Fragments(1), FragmentMap(NumObjects) {}
134
135 /// Add F to the layout while trying to keep its indices contiguous.
136 /// If a previously seen fragment uses any of F's indices, that
137 /// fragment will be laid out inside F.
138 LLVM_ABI void addFragment(const std::set<uint64_t> &F);
139};
140
141/// This class is used to build a byte array containing overlapping bit sets. By
142/// loading from indexed offsets into the byte array and applying a mask, a
143/// program can test bits from the bit set with a relatively short instruction
144/// sequence. For example, suppose we have 15 bit sets to lay out:
145///
146/// A (16 bits), B (15 bits), C (14 bits), D (13 bits), E (12 bits),
147/// F (11 bits), G (10 bits), H (9 bits), I (7 bits), J (6 bits), K (5 bits),
148/// L (4 bits), M (3 bits), N (2 bits), O (1 bit)
149///
150/// These bits can be laid out in a 16-byte array like this:
151///
152/// Byte Offset
153/// 0123456789ABCDEF
154/// Bit
155/// 7 HHHHHHHHHIIIIIII
156/// 6 GGGGGGGGGGJJJJJJ
157/// 5 FFFFFFFFFFFKKKKK
158/// 4 EEEEEEEEEEEELLLL
159/// 3 DDDDDDDDDDDDDMMM
160/// 2 CCCCCCCCCCCCCCNN
161/// 1 BBBBBBBBBBBBBBBO
162/// 0 AAAAAAAAAAAAAAAA
163///
164/// For example, to test bit X of A, we evaluate ((bits[X] & 1) != 0), or to
165/// test bit X of I, we evaluate ((bits[9 + X] & 0x80) != 0). This can be done
166/// in 1-2 machine instructions on x86, or 4-6 instructions on ARM.
167///
168/// This is a byte array, rather than (say) a 2-byte array or a 4-byte array,
169/// because for one thing it gives us better packing (the more bins there are,
170/// the less evenly they will be filled), and for another, the instruction
171/// sequences can be slightly shorter, both on x86 and ARM.
173 /// The byte array built so far.
174 std::vector<uint8_t> Bytes;
175
176 enum { BitsPerByte = 8 };
177
178 /// The number of bytes allocated so far for each of the bits.
180
182 memset(BitAllocs, 0, sizeof(BitAllocs));
183 }
184
185 /// Allocate BitSize bits in the byte array where Bits contains the bits to
186 /// set. AllocByteOffset is set to the offset within the byte array and
187 /// AllocMask is set to the bitmask for those bits. This uses the LPT (Longest
188 /// Processing Time) multiprocessor scheduling algorithm to lay out the bits
189 /// efficiently; the pass allocates bit sets in decreasing size order.
190 LLVM_ABI void allocate(const std::set<uint64_t> &Bits, uint64_t BitSize,
191 uint64_t &AllocByteOffset, uint8_t &AllocMask);
192};
193
195
196/// Specifies how to drop type tests.
197enum class DropTestKind {
198 None, /// Do not drop type tests (default).
199 Assume, /// Drop only llvm.assumes using type test value.
200 All, /// Drop the type test and all uses.
201};
202
203} // end namespace lowertypetests
204
205class LowerTypeTestsPass : public PassInfoMixin<LowerTypeTestsPass> {
206 bool UseCommandLine = false;
207
208 ModuleSummaryIndex *ExportSummary = nullptr;
209 const ModuleSummaryIndex *ImportSummary = nullptr;
210 lowertypetests::DropTestKind DropTypeTests =
212
213public:
214 LowerTypeTestsPass() : UseCommandLine(true) {}
216 const ModuleSummaryIndex *ImportSummary,
217 lowertypetests::DropTestKind DropTypeTests =
219 : ExportSummary(ExportSummary), ImportSummary(ImportSummary),
220 DropTypeTests(DropTypeTests) {}
222};
223
224class SimplifyTypeTestsPass : public PassInfoMixin<SimplifyTypeTestsPass> {
225public:
227};
228
229} // end namespace llvm
230
231#endif // LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
#define LLVM_ABI
Definition Compiler.h:213
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:55
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
LowerTypeTestsPass(ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary, lowertypetests::DropTestKind DropTypeTests=lowertypetests::DropTestKind::None)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
DropTestKind
Specifies how to drop type tests.
@ Assume
Do not drop type tests (default).
@ All
Drop only llvm.assumes using type test value.
LLVM_ABI bool isJumpTableCanonical(Function *F)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
SmallVector< uint64_t, 16 > Offsets
BitSetBuilder(ArrayRef< uint64_t > Offsets)
LLVM_ABI bool containsGlobalOffset(uint64_t Offset) const
LLVM_ABI void print(raw_ostream &OS) const
uint64_t BitAllocs[BitsPerByte]
The number of bytes allocated so far for each of the bits.
std::vector< uint8_t > Bytes
The byte array built so far.
LLVM_ABI void allocate(const std::set< uint64_t > &Bits, uint64_t BitSize, uint64_t &AllocByteOffset, uint8_t &AllocMask)
Allocate BitSize bits in the byte array where Bits contains the bits to set.
std::vector< std::vector< uint64_t > > Fragments
The computed layout.
LLVM_ABI void addFragment(const std::set< uint64_t > &F)
Add F to the layout while trying to keep its indices contiguous.
std::vector< uint64_t > FragmentMap
Mapping from object index to fragment index.