Skip to content

Commit 5c5c9fd

Browse files
amstrnadarunthomas
authored andcommitted
Reorder to avoid a bad dereference
1 parent 6f905d4 commit 5c5c9fd

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

llvm/lib/Target/RISCV/ISPMetadataPass.cpp

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,59 +54,60 @@ static void setMIFlags(MachineInstr *MI) {
5454

5555
bool RISCVISPMetadata::runOnMachineFunction(MachineFunction &MF) {
5656

57-
for (auto &MBB : MF) {
58-
59-
// check first instruction
60-
auto MI = MBB.getFirstNonDebugInstr();
61-
setMIFlags(&*MI);
62-
if ( MI == MBB.end() )
63-
continue;
64-
65-
MBB.getSymbol()->modifyFlags((&MBB == &*MF.begin() ?
66-
MachineInstr::CallTarget :
67-
MachineInstr::BranchTarget),
68-
0);
69-
70-
for(auto &pred : MBB.predecessors()){
71-
const auto &last = pred->getLastNonDebugInstr();
72-
if(last != pred->end()) {
73-
if(last->isCall()) {
74-
MI->setFlag(MachineInstr::ReturnTarget);
75-
}
76-
if(last->isBranch())
77-
MI->setFlag(MachineInstr::BranchTarget);
78-
}
57+
for (auto &MBB : MF) {
58+
59+
// check first instruction
60+
auto MI = MBB.getFirstNonDebugInstr();
61+
if ( MI == MBB.end() )
62+
continue;
63+
64+
MBB.getSymbol()->modifyFlags((&MBB == &*MF.begin() ?
65+
MachineInstr::CallTarget :
66+
MachineInstr::BranchTarget),
67+
0);
68+
69+
setMIFlags(&*MI);
70+
71+
for(auto &pred : MBB.predecessors()){
72+
const auto &last = pred->getLastNonDebugInstr();
73+
if(last != pred->end()) {
74+
if(last->isCall()) {
75+
MI->setFlag(MachineInstr::ReturnTarget);
76+
}
77+
if(last->isBranch())
78+
MI->setFlag(MachineInstr::BranchTarget);
79+
}
80+
}
81+
82+
// check all other instructions
83+
84+
auto last = MI;
85+
for( auto MI = std::next(MBB.instr_begin()); MI != MBB.instr_end(); MI++ ) {
86+
87+
setMIFlags(&*MI);
88+
89+
//The zero size instructions from RISCVInstrInfo.cpp - getInstSizeInBytes
90+
//wasn't obvious how to call it, so here's this unmaintable approach
91+
switch(MI->getOpcode()){
92+
case TargetOpcode::EH_LABEL:
93+
case TargetOpcode::IMPLICIT_DEF:
94+
case TargetOpcode::KILL:
95+
case TargetOpcode::DBG_VALUE:
96+
continue;
97+
default: //do nothing
98+
break; //breaks the switch not the loop
99+
}
100+
101+
if(last->isCall())
102+
MI->setFlag(MachineInstr::ReturnTarget);
103+
104+
if(last->isBranch())
105+
MI->setFlag(MachineInstr::BranchTarget);
106+
107+
last = MI;
108+
}
79109
}
80110

81-
// check all other instructions
82-
83-
auto last = MI;
84-
for( auto MI = std::next(MBB.instr_begin()); MI != MBB.instr_end(); MI++ ) {
85-
86-
setMIFlags(&*MI);
87-
88-
//The zero size instructions from RISCVInstrInfo.cpp - getInstSizeInBytes
89-
//wasn't obvious how to call it, so here's this unmaintable approach
90-
switch(MI->getOpcode()){
91-
case TargetOpcode::EH_LABEL:
92-
case TargetOpcode::IMPLICIT_DEF:
93-
case TargetOpcode::KILL:
94-
case TargetOpcode::DBG_VALUE:
95-
continue;
96-
default: //do nothing
97-
break; //breaks the switch not the loop
98-
}
99-
100-
if(last->isCall())
101-
MI->setFlag(MachineInstr::ReturnTarget);
102-
103-
if(last->isBranch())
104-
MI->setFlag(MachineInstr::BranchTarget);
105-
106-
last = MI;
107-
}
108-
}
109-
110111
return false;
111112
}
112113

0 commit comments

Comments
 (0)