2626#include " llvm/IR/InstIterator.h"
2727#include " llvm/IR/Instruction.h"
2828#include " llvm/IR/Instructions.h"
29+ #include " llvm/IR/IntrinsicInst.h"
2930#include " llvm/Support/Casting.h"
3031
3132#include " nlohmann/json.hpp"
@@ -47,47 +48,60 @@ LLVMBasedCFG::getFunctionOf(const llvm::Instruction *Inst) const {
4748
4849vector<const llvm::Instruction *>
4950LLVMBasedCFG::getPredsOf (const llvm::Instruction *I) const {
50- vector<const llvm::Instruction *> Preds;
5151 if (!IgnoreDbgInstructions) {
52- if (I->getPrevNode ()) {
53- Preds. push_back (I-> getPrevNode ()) ;
52+ if (auto *PrevInst = I->getPrevNode ()) {
53+ return {PrevInst} ;
5454 }
5555 } else {
56- if (I->getPrevNonDebugInstruction ()) {
57- Preds.push_back (I->getPrevNonDebugInstruction ());
56+ if (auto *PrevNonDbgInst =
57+ I->getPrevNonDebugInstruction (false /* Only debug instructions*/ )) {
58+ return {PrevNonDbgInst};
5859 }
5960 }
6061 // If we do not have a predecessor yet, look for basic blocks which
6162 // lead to our instruction in question!
62- if (Preds.empty ()) {
63- std::transform (llvm::pred_begin (I->getParent ()),
64- llvm::pred_end (I->getParent ()), back_inserter (Preds),
65- [](const llvm::BasicBlock *BB) {
66- assert (BB && " BB under analysis was not well formed." );
67- return BB->getTerminator ();
68- });
69- }
63+
64+ std::vector<const llvm::Instruction *> Preds;
65+ std::transform (llvm::pred_begin (I->getParent ()),
66+ llvm::pred_end (I->getParent ()), back_inserter (Preds),
67+ [](const llvm::BasicBlock *BB) {
68+ assert (BB && " BB under analysis was not well formed." );
69+ const llvm::Instruction *Pred = BB->getTerminator ();
70+ if (llvm::isa<llvm::DbgInfoIntrinsic>(Pred)) {
71+ Pred = Pred->getPrevNonDebugInstruction (
72+ false /* Only debug instructions*/ );
73+ }
74+ return Pred;
75+ });
76+
7077 return Preds;
7178}
7279
7380vector<const llvm::Instruction *>
7481LLVMBasedCFG::getSuccsOf (const llvm::Instruction *I) const {
75- vector<const llvm::Instruction *> Successors;
82+ std:: vector<const llvm::Instruction *> Successors;
7683 // case we wish to consider LLVM's debug instructions
7784 if (!IgnoreDbgInstructions) {
78- if (I->getNextNode ()) {
79- Successors. push_back (I-> getNextNode ()) ;
85+ if (auto *NextInst = I->getNextNode ()) {
86+ return {NextInst} ;
8087 }
8188 } else {
82- if (I->getNextNonDebugInstruction ()) {
83- Successors.push_back (I->getNextNonDebugInstruction ());
89+ if (auto *NextNonDbgInst =
90+ I->getNextNonDebugInstruction (false /* Only debug instructions*/ )) {
91+ Successors.push_back (NextNonDbgInst);
8492 }
8593 }
8694 if (I->isTerminator ()) {
8795 Successors.reserve (I->getNumSuccessors () + Successors.size ());
8896 std::transform (llvm::succ_begin (I), llvm::succ_end (I),
89- back_inserter (Successors),
90- [](const llvm::BasicBlock *BB) { return &BB->front (); });
97+ back_inserter (Successors), [](const llvm::BasicBlock *BB) {
98+ const llvm::Instruction *Succ = &BB->front ();
99+ if (llvm::isa<llvm::DbgInfoIntrinsic>(Succ)) {
100+ Succ = Succ->getNextNonDebugInstruction (
101+ false /* Only debug instructions*/ );
102+ }
103+ return Succ;
104+ });
91105 }
92106 return Successors;
93107}
@@ -135,7 +149,12 @@ LLVMBasedCFG::getStartPointsOf(const llvm::Function *Fun) const {
135149 return {};
136150 }
137151 if (!Fun->isDeclaration ()) {
138- return {&Fun->front ().front ()};
152+ auto *EntryInst = &Fun->front ().front ();
153+ if (IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(EntryInst)) {
154+ return {EntryInst->getNextNonDebugInstruction (
155+ false /* Only debug instructions*/ )};
156+ }
157+ return {EntryInst};
139158 } else {
140159 LOG_IF_ENABLE (BOOST_LOG_SEV (lg::get (), DEBUG)
141160 << " Could not get starting points of '"
0 commit comments