-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDatapathCodehMotion.cpp
More file actions
115 lines (88 loc) · 3.49 KB
/
DatapathCodehMotion.cpp
File metadata and controls
115 lines (88 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- DatapathCodeMotion.cpp - Move the data-path operations ---*- C++ -*-===//
//
// The Shang HLS frameowrk //
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the VTM implementation of the DatapathCodeMotion pass.
//
//===----------------------------------------------------------------------===//
#include "vtm/Utilities.h"
#include "vtm/VInstrInfo.h"
#include "vtm/VerilogBackendMCTargetDesc.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
namespace llvm {
bool hoistDatapathOp(MachineInstr *MI, MachineDominatorTree *DT,
MachineRegisterInfo *MRI) {
assert(VInstrInfo::isDatapath(MI->getOpcode()) && "Expect datapath operation!");
MachineBasicBlock *MBBToHoist = DT->getRoot();
MachineBasicBlock *CurMBB = MI->getParent();
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
if (!MO.isReg() || MO.isDef() || !MO.getReg()) continue;
MachineInstr *DefMI = MRI->getVRegDef(MO.getReg());
assert(DefMI && "Not in SSA form!");
MachineBasicBlock *DefMBB = DefMI->getParent();
if (DT->dominates(MBBToHoist, DefMBB)) {
MBBToHoist = DefMBB;
continue;
}
assert(DT->dominates(DefMBB, MBBToHoist)
&& "Operands not in a path of the dominator tree!");
}
if (MBBToHoist == CurMBB) return false;
MI->removeFromParent();
MachineBasicBlock::instr_iterator IP = MBBToHoist->getFirstInstrTerminator();
// Insert the imp_def before the PHI incoming copies.
while (llvm::prior(IP)->getOpcode() == VTM::VOpMvPhi)
--IP;
MBBToHoist->insert(IP, MI);
return true;
}
bool hoistDatapathOpInMBB(MachineBasicBlock *MBB, MachineDominatorTree *DT,
MachineRegisterInfo *MRI) {
bool MadeChange = false;
typedef MachineBasicBlock::instr_iterator instr_it;
for (instr_it I = MBB->instr_begin(), E = MBB->instr_end(); I != E; /*++I*/) {
MachineInstr *MI = I++;
if (VInstrInfo::isDatapath(MI->getOpcode()))
MadeChange |= hoistDatapathOp(MI, DT, MRI);
}
return MadeChange;
}
bool hoistDatapathOpInSuccs(MachineBasicBlock *MBB, MachineDominatorTree *DT,
MachineRegisterInfo *MRI) {
bool MadeChange = false;
typedef MachineBasicBlock::succ_iterator succ_iterator;
for (succ_iterator I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I)
MadeChange |= hoistDatapathOpInMBB(MBB, DT, MRI);
return MadeChange;
}
struct HoistDatapathPass : public MachineFunctionPass {
static char ID;
HoistDatapathPass() : MachineFunctionPass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<MachineDominatorTree>();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool runOnMachineFunction(MachineFunction &MF) {
bool Changed = false;
MachineDominatorTree &DT = getAnalysis<MachineDominatorTree>();
MachineRegisterInfo &MRI = MF.getRegInfo();
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Changed |= hoistDatapathOpInMBB(I, &DT, &MRI);
return Changed;
}
};
Pass *createHoistDatapathPass() {
return new llvm::HoistDatapathPass();
}
}
char llvm::HoistDatapathPass::ID = 0;