xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1 //===- ReduceOperandBundes.cpp - Specialized Delta 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 //
9 // This file implements a function which calls the Generic Delta pass in order
10 // to reduce uninteresting operand bundes from calls.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceOperandBundles.h"
15 #include "Delta.h"
16 #include "TestRunner.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/Sequence.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <algorithm>
26 #include <iterator>
27 #include <vector>
28 
29 namespace llvm {
30 class Module;
31 } // namespace llvm
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 /// Given ChunksToKeep, produce a map of calls and indexes of operand bundles
38 /// to be preserved for each call.
39 class OperandBundleRemapper : public InstVisitor<OperandBundleRemapper> {
40   Oracle &O;
41 
42 public:
43   DenseMap<CallBase *, std::vector<unsigned>> CallsToRefine;
44 
OperandBundleRemapper(Oracle & O)45   explicit OperandBundleRemapper(Oracle &O) : O(O) {}
46 
47   /// So far only CallBase sub-classes can have operand bundles.
48   /// Let's see which of the operand bundles of this call are to be kept.
visitCallBase(CallBase & Call)49   void visitCallBase(CallBase &Call) {
50     if (!Call.hasOperandBundles())
51       return; // No bundles to begin with.
52 
53     // Insert this call into map, we will likely want to rebuild it.
54     auto &OperandBundlesToKeepIndexes = CallsToRefine[&Call];
55     OperandBundlesToKeepIndexes.reserve(Call.getNumOperandBundles());
56 
57     // Enumerate every operand bundle on this call.
58     for (unsigned BundleIndex : seq(0U, Call.getNumOperandBundles()))
59       if (O.shouldKeep()) // Should we keep this one?
60         OperandBundlesToKeepIndexes.emplace_back(BundleIndex);
61   }
62 };
63 
64 struct OperandBundleCounter : public InstVisitor<OperandBundleCounter> {
65   /// How many features (in this case, operand bundles) did we count, total?
66   int OperandBundeCount = 0;
67 
68   /// So far only CallBase sub-classes can have operand bundles.
visitCallBase__anon97cdba0a0111::OperandBundleCounter69   void visitCallBase(CallBase &Call) {
70     // Just accumulate the total number of operand bundles.
71     OperandBundeCount += Call.getNumOperandBundles();
72   }
73 };
74 
75 } // namespace
76 
maybeRewriteCallWithDifferentBundles(CallBase * OrigCall,ArrayRef<unsigned> OperandBundlesToKeepIndexes)77 static void maybeRewriteCallWithDifferentBundles(
78     CallBase *OrigCall, ArrayRef<unsigned> OperandBundlesToKeepIndexes) {
79   if (OperandBundlesToKeepIndexes.size() == OrigCall->getNumOperandBundles())
80     return; // Not modifying operand bundles of this call after all.
81 
82   std::vector<OperandBundleDef> NewBundles;
83   NewBundles.reserve(OperandBundlesToKeepIndexes.size());
84 
85   // Actually copy over the bundles that we want to keep.
86   transform(OperandBundlesToKeepIndexes, std::back_inserter(NewBundles),
87             [OrigCall](unsigned Index) {
88               return OperandBundleDef(OrigCall->getOperandBundleAt(Index));
89             });
90 
91   // Finally actually replace the bundles on the call.
92   CallBase *NewCall = CallBase::Create(OrigCall, NewBundles, OrigCall);
93   OrigCall->replaceAllUsesWith(NewCall);
94   OrigCall->eraseFromParent();
95 }
96 
97 /// Removes out-of-chunk operand bundles from calls.
extractOperandBundesFromModule(Oracle & O,ReducerWorkItem & WorkItem)98 static void extractOperandBundesFromModule(Oracle &O,
99                                            ReducerWorkItem &WorkItem) {
100   Module &Program = WorkItem.getModule();
101   OperandBundleRemapper R(O);
102   R.visit(Program);
103 
104   for (const auto &I : R.CallsToRefine)
105     maybeRewriteCallWithDifferentBundles(I.first, I.second);
106 }
107 
reduceOperandBundesDeltaPass(TestRunner & Test)108 void llvm::reduceOperandBundesDeltaPass(TestRunner &Test) {
109   runDeltaPass(Test, extractOperandBundesFromModule,
110                "Reducing Operand Bundles");
111 }
112