forked from etherzhhb/Shang
/
FixTerminators.cpp
123 lines (109 loc) · 4.43 KB
/
FixTerminators.cpp
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
116
117
118
119
120
121
122
123
//===- FixMachineCode.cpp - Fix The Terminators in Machine Code -*- C++ -*-===//
//
// The Shang HLS frameowrk //
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a pass that add branch instruction that branch to fall
// through block explicitly.
//
//===----------------------------------------------------------------------===//
#include "vtm/Utilities.h"
#include "vtm/VerilogBackendMCTargetDesc.h"
#include "vtm/VInstrInfo.h"
#include "llvm/../../lib/CodeGen/BranchFolding.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#define DEBUG_TYPE "vtm-fix-terminator"
#include "llvm/Support/Debug.h"
#include <set>
using namespace llvm;
STATISTIC(UnconditionalBranches,
"Number of unconditionnal branches inserted for fall through edges");
STATISTIC(Unreachables,
"Number of Unreachable inserted for machine basic block without sucessor");
namespace llvm {
void fixTerminators(MachineFunction &MF) {
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
fixTerminators(I);
}
void fixTerminators(MachineBasicBlock *MBB) {
SmallPtrSet<MachineBasicBlock*, 2> MissedSuccs;
MissedSuccs.insert(MBB->succ_begin(), MBB->succ_end());
MachineInstr *FirstTerminator = 0;
for (MachineBasicBlock::iterator II = MBB->getFirstTerminator(),
IE = MBB->end(); II != IE; ++II) {
MachineInstr *Inst = II;
if (!VInstrInfo::isBrCndLike(Inst->getOpcode())) continue;
MachineBasicBlock *TargetBB = Inst->getOperand(1).getMBB();
MachineOperand Cnd = Inst->getOperand(0);
//bool inserted;
//jt_it at;
//tie(at, inserted) = Table.insert(std::make_pair(TargetBB, Cnd));
// BranchFolding may generate code that jumping to same bb with multiple
// instruction, merge the condition.
//if (!inserted) {
// at->second = VInstrInfo::MergePred(Cnd, at->second, *MBB,
// MBB->getFirstTerminator(), &MRI,
// TII, VTM::VOpOr);
//}
// Change the unconditional branch after conditional branch to
// conditional branch.
if (FirstTerminator && VInstrInfo::isUnConditionalBranch(Inst)){
MachineOperand &TrueCnd = FirstTerminator->getOperand(0);
MachineOperand &FalseCnd = Inst->getOperand(0);
TrueCnd.setIsKill(false);
FalseCnd.setReg(TrueCnd.getReg());
FalseCnd.setTargetFlags(TrueCnd.getTargetFlags());
VInstrInfo::ReversePredicateCondition(FalseCnd);
}
FirstTerminator = Inst;
MissedSuccs.erase(TargetBB);
}
// Make sure each basic block have a terminator.
if (!MissedSuccs.empty()) {
assert(MissedSuccs.size() == 1 && "Fall through to multiple blocks?");
++UnconditionalBranches;
MachineOperand Cnd = VInstrInfo::CreatePredicate();
if (FirstTerminator) {
MachineOperand &TrueCnd = FirstTerminator->getOperand(0);
assert(TrueCnd.getReg() != 0 && "Two unconditional branch?");
// We will use the register somewhere else
TrueCnd.setIsKill(false);
Cnd = TrueCnd;
VInstrInfo::ReversePredicateCondition(Cnd);
}
BuildMI(MBB, DebugLoc(), VInstrInfo::getDesc(VTM::VOpToStateb))
.addOperand(Cnd).addMBB(*MissedSuccs.begin())
.addOperand(VInstrInfo::CreatePredicate())
.addOperand(VInstrInfo::CreateTrace());
}
//else if (Table.size() != MBB->succ_size()) {
// // Also fix the CFG.
// while (!MBB->succ_empty())
// MBB->removeSuccessor(MBB->succ_end() - 1);
// for (jt_it JI = Table.begin(), JE = Table.end(); JI != JE; ++JI)
// MBB->addSuccessor(JI->first);
// // Try to correct the CFG.
// TII->RemoveBranch(*MBB);
// VInstrInfo::insertJumpTable(*MBB, Table, DebugLoc());
//}
//Table.clear();
if (MBB->succ_size() == 0 && MBB->getFirstTerminator() == MBB->end()) {
++Unreachables;
BuildMI(MBB, DebugLoc(), VInstrInfo::getDesc(VTM::VOpUnreachable))
.addOperand(VInstrInfo::CreatePredicate())
.addOperand(VInstrInfo::CreateTrace());
}
}
}