LLVM 22.0.0git
PackReuse.cpp
Go to the documentation of this file.
1//===- PackReuse.cpp - A pack de-duplication pass -------------------------===//
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
11
12namespace llvm::sandboxir {
13
15 if (Rgn.empty())
16 return Change;
17 // The key to the map is the ordered operands of the pack.
18 // The value is a vector of all Pack Instrs with the same operands.
21 PacksMap;
22 // Go over the region and look for pack patterns.
23 for (auto *I : Rgn) {
24 auto PackOpt = VecUtils::matchPack(I);
25 if (PackOpt) {
26 // TODO: For now limit pack reuse within a BB.
27 BasicBlock *BB = (*PackOpt->Instrs.front()).getParent();
28 PacksMap[{BB, PackOpt->Operands}].push_back(PackOpt->Instrs);
29 }
30 }
31 for (auto &Pair : PacksMap) {
32 auto &Packs = Pair.second;
33 if (Packs.size() <= 1)
34 continue;
35 // Sort packs by program order.
36 sort(Packs, [](const auto &PackInstrs1, const auto &PackInstrs2) {
37 return PackInstrs1.front()->comesBefore(PackInstrs2.front());
38 });
39 Instruction *TopMostPack = Packs[0].front();
40 // Replace duplicate packs with the first one.
41 for (const auto &PackInstrs :
42 make_range(std::next(Packs.begin()), Packs.end())) {
43 PackInstrs.front()->replaceAllUsesWith(TopMostPack);
44 // Delete the pack instrs bottom-up since they are now dead.
45 for (auto *PackI : PackInstrs)
46 PackI->eraseFromParent();
47 }
48 Change = true;
49 }
50 return Change;
51}
52
53} // namespace llvm::sandboxir
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define I(x, y, z)
Definition MD5.cpp:58
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
bool runOnRegion(Region &Rgn, const Analyses &A) final
\Returns true if it modifies R.
Definition PackReuse.cpp:14
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition Region.h:96
bool empty() const
Returns true if the Region has no instructions.
Definition Region.h:152
static std::optional< PackPattern > matchPack(Instruction *I)
If I is the last instruction of a pack pattern (i.e., an InsertElement into a vector),...
Definition VecUtils.h:226
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
Definition BasicBlock.h:75
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652