Skip to content

Commit

Permalink
Reorder to avoid a bad dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
amstrnad committed Oct 7, 2019
1 parent 05b44b2 commit 67ab73e
Showing 1 changed file with 52 additions and 51 deletions.
103 changes: 52 additions & 51 deletions llvm/lib/Target/RISCV/ISPMetadataPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,59 +54,60 @@ static void setMIFlags(MachineInstr *MI) {

bool RISCVISPMetadata::runOnMachineFunction(MachineFunction &MF) {

for (auto &MBB : MF) {

// check first instruction
auto MI = MBB.getFirstNonDebugInstr();
setMIFlags(&*MI);
if ( MI == MBB.end() )
continue;

MBB.getSymbol()->modifyFlags((&MBB == &*MF.begin() ?
MachineInstr::CallTarget :
MachineInstr::BranchTarget),
0);

for(auto &pred : MBB.predecessors()){
const auto &last = pred->getLastNonDebugInstr();
if(last != pred->end()) {
if(last->isCall()) {
MI->setFlag(MachineInstr::ReturnTarget);
}
if(last->isBranch())
MI->setFlag(MachineInstr::BranchTarget);
}
for (auto &MBB : MF) {

// check first instruction
auto MI = MBB.getFirstNonDebugInstr();
if ( MI == MBB.end() )
continue;

MBB.getSymbol()->modifyFlags((&MBB == &*MF.begin() ?
MachineInstr::CallTarget :
MachineInstr::BranchTarget),
0);

setMIFlags(&*MI);

for(auto &pred : MBB.predecessors()){
const auto &last = pred->getLastNonDebugInstr();
if(last != pred->end()) {
if(last->isCall()) {
MI->setFlag(MachineInstr::ReturnTarget);
}
if(last->isBranch())
MI->setFlag(MachineInstr::BranchTarget);
}
}

// check all other instructions

auto last = MI;
for( auto MI = std::next(MBB.instr_begin()); MI != MBB.instr_end(); MI++ ) {

setMIFlags(&*MI);

//The zero size instructions from RISCVInstrInfo.cpp - getInstSizeInBytes
//wasn't obvious how to call it, so here's this unmaintable approach
switch(MI->getOpcode()){
case TargetOpcode::EH_LABEL:
case TargetOpcode::IMPLICIT_DEF:
case TargetOpcode::KILL:
case TargetOpcode::DBG_VALUE:
continue;
default: //do nothing
break; //breaks the switch not the loop
}

if(last->isCall())
MI->setFlag(MachineInstr::ReturnTarget);

if(last->isBranch())
MI->setFlag(MachineInstr::BranchTarget);

last = MI;
}
}

// check all other instructions

auto last = MI;
for( auto MI = std::next(MBB.instr_begin()); MI != MBB.instr_end(); MI++ ) {

setMIFlags(&*MI);

//The zero size instructions from RISCVInstrInfo.cpp - getInstSizeInBytes
//wasn't obvious how to call it, so here's this unmaintable approach
switch(MI->getOpcode()){
case TargetOpcode::EH_LABEL:
case TargetOpcode::IMPLICIT_DEF:
case TargetOpcode::KILL:
case TargetOpcode::DBG_VALUE:
continue;
default: //do nothing
break; //breaks the switch not the loop
}

if(last->isCall())
MI->setFlag(MachineInstr::ReturnTarget);

if(last->isBranch())
MI->setFlag(MachineInstr::BranchTarget);

last = MI;
}
}

return false;
}

Expand Down

0 comments on commit 67ab73e

Please sign in to comment.