1 //===- ReduceInstructionsMIR.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 MachineInstr from the MachineFunction.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceInstructionsMIR.h"
15 
16 #include "llvm/CodeGen/MachineDominators.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/TargetInstrInfo.h"
21 
22 using namespace llvm;
23 
24 static Register getPrevDefOfRCInMBB(MachineBasicBlock &MBB,
25                                     MachineBasicBlock::reverse_iterator &RI,
26                                     const RegClassOrRegBank &RC,
27                                     SetVector<MachineInstr *> &ExcludeMIs) {
28   auto MRI = &MBB.getParent()->getRegInfo();
29   for (MachineBasicBlock::reverse_instr_iterator E = MBB.instr_rend(); RI != E;
30        ++RI) {
31     auto &MI = *RI;
32     // All Def operands explicit and implicit.
33     for (auto &MO : MI.operands()) {
34       if (!MO.isReg() || !MO.isDef())
35         continue;
36       auto Reg = MO.getReg();
37       if (Register::isPhysicalRegister(Reg))
38         continue;
39 
40       if (MRI->getRegClassOrRegBank(Reg) == RC &&
41           !ExcludeMIs.count(MO.getParent()))
42         return Reg;
43     }
44   }
45   return 0;
46 }
47 
48 static void extractInstrFromModule(Oracle &O, MachineFunction &MF) {
49   MachineDominatorTree MDT;
50   MDT.runOnMachineFunction(MF);
51 
52   auto MRI = &MF.getRegInfo();
53   SetVector<MachineInstr *> ToDelete;
54 
55   MachineInstr *TopMI = nullptr;
56 
57   // Mark MIs for deletion according to some criteria.
58   for (auto &MBB : MF) {
59     for (auto &MI : MBB) {
60       if (MI.isTerminator())
61         continue;
62       if (MBB.isEntryBlock() && !TopMI) {
63         TopMI = &MI;
64         continue;
65       }
66       if (!O.shouldKeep())
67         ToDelete.insert(&MI);
68     }
69   }
70 
71   // For each MI to be deleted update users of regs defined by that MI to use
72   // some other dominating definition (that is not to be deleted).
73   for (auto *MI : ToDelete) {
74     for (auto &MO : MI->operands()) {
75       if (!MO.isReg() || !MO.isDef())
76         continue;
77       auto Reg = MO.getReg();
78       if (Register::isPhysicalRegister(Reg))
79         continue;
80       auto UI = MRI->use_begin(Reg);
81       auto UE = MRI->use_end();
82 
83       const auto &RegRC = MRI->getRegClassOrRegBank(Reg);
84       Register NewReg = 0;
85       // If this is not a physical register and there are some uses.
86       if (UI != UE) {
87         MachineBasicBlock::reverse_iterator RI(*MI);
88         MachineBasicBlock *BB = MI->getParent();
89         ++RI;
90         while (NewReg == 0 && BB) {
91           NewReg = getPrevDefOfRCInMBB(*BB, RI, RegRC, ToDelete);
92           // Prepare for idom(BB).
93           if (auto *IDM = MDT.getNode(BB)->getIDom()) {
94             BB = IDM->getBlock();
95             RI = BB->rbegin();
96           } else {
97             BB = nullptr;
98           }
99         }
100       }
101 
102       // If no dominating definition was found then add an implicit one to the
103       // first instruction in the entry block.
104       if (!NewReg && TopMI) {
105         NewReg = MRI->cloneVirtualRegister(Reg);
106         TopMI->addOperand(MachineOperand::CreateReg(
107             NewReg, true /*IsDef*/, true /*IsImp*/, false /*IsKill*/));
108       }
109 
110       // Update all uses.
111       while (UI != UE) {
112         auto &UMO = *UI++;
113         UMO.setReg(NewReg);
114       }
115     }
116   }
117 
118   // Finally delete the MIs.
119   for (auto *MI : ToDelete)
120     MI->eraseFromParent();
121 }
122 
123 void llvm::reduceInstructionsMIRDeltaPass(TestRunner &Test) {
124   outs() << "*** Reducing Instructions...\n";
125   runDeltaPass(Test, extractInstrFromModule);
126 }
127