Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,6 @@ class LLVMBasedICFG

~LLVMBasedICFG() override;

// Re-override getSuccsOf and getPredsOf from LLVMBasedCFG to incorporate
// information about global ctors and globale dtors
[[nodiscard]] std::vector<const llvm::Instruction *>
getPredsOf(const llvm::Instruction *Inst) const override;

[[nodiscard]] std::vector<const llvm::Instruction *>
getSuccsOf(const llvm::Instruction *Inst) const override;

[[nodiscard]] const llvm::Function *getFirstGlobalCtorOrNull() const;

[[nodiscard]] const llvm::Function *getLastGlobalDtorOrNull() const;
Expand Down
61 changes: 40 additions & 21 deletions lib/PhasarLLVM/ControlFlow/LLVMBasedCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Support/Casting.h"

#include "nlohmann/json.hpp"
Expand All @@ -47,47 +48,60 @@ LLVMBasedCFG::getFunctionOf(const llvm::Instruction *Inst) const {

vector<const llvm::Instruction *>
LLVMBasedCFG::getPredsOf(const llvm::Instruction *I) const {
vector<const llvm::Instruction *> Preds;
if (!IgnoreDbgInstructions) {
if (I->getPrevNode()) {
Preds.push_back(I->getPrevNode());
if (auto *PrevInst = I->getPrevNode()) {
return {PrevInst};
}
} else {
if (I->getPrevNonDebugInstruction()) {
Preds.push_back(I->getPrevNonDebugInstruction());
if (auto *PrevNonDbgInst =
I->getPrevNonDebugInstruction(false /*Only debug instructions*/)) {
return {PrevNonDbgInst};
}
}
// If we do not have a predecessor yet, look for basic blocks which
// lead to our instruction in question!
if (Preds.empty()) {
std::transform(llvm::pred_begin(I->getParent()),
llvm::pred_end(I->getParent()), back_inserter(Preds),
[](const llvm::BasicBlock *BB) {
assert(BB && "BB under analysis was not well formed.");
return BB->getTerminator();
});
}

std::vector<const llvm::Instruction *> Preds;
std::transform(llvm::pred_begin(I->getParent()),
llvm::pred_end(I->getParent()), back_inserter(Preds),
[](const llvm::BasicBlock *BB) {
assert(BB && "BB under analysis was not well formed.");
const llvm::Instruction *Pred = BB->getTerminator();
if (llvm::isa<llvm::DbgInfoIntrinsic>(Pred)) {
Pred = Pred->getPrevNonDebugInstruction(
false /*Only debug instructions*/);
}
return Pred;
});

return Preds;
}

vector<const llvm::Instruction *>
LLVMBasedCFG::getSuccsOf(const llvm::Instruction *I) const {
vector<const llvm::Instruction *> Successors;
std::vector<const llvm::Instruction *> Successors;
// case we wish to consider LLVM's debug instructions
if (!IgnoreDbgInstructions) {
if (I->getNextNode()) {
Successors.push_back(I->getNextNode());
if (auto *NextInst = I->getNextNode()) {
return {NextInst};
}
} else {
if (I->getNextNonDebugInstruction()) {
Successors.push_back(I->getNextNonDebugInstruction());
if (auto *NextNonDbgInst =
I->getNextNonDebugInstruction(false /*Only debug instructions*/)) {
Successors.push_back(NextNonDbgInst);
}
}
if (I->isTerminator()) {
Successors.reserve(I->getNumSuccessors() + Successors.size());
std::transform(llvm::succ_begin(I), llvm::succ_end(I),
back_inserter(Successors),
[](const llvm::BasicBlock *BB) { return &BB->front(); });
back_inserter(Successors), [](const llvm::BasicBlock *BB) {
const llvm::Instruction *Succ = &BB->front();
if (llvm::isa<llvm::DbgInfoIntrinsic>(Succ)) {
Succ = Succ->getNextNonDebugInstruction(
false /*Only debug instructions*/);
}
return Succ;
});
}
return Successors;
}
Expand Down Expand Up @@ -135,7 +149,12 @@ LLVMBasedCFG::getStartPointsOf(const llvm::Function *Fun) const {
return {};
}
if (!Fun->isDeclaration()) {
return {&Fun->front().front()};
auto *EntryInst = &Fun->front().front();
if (IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(EntryInst)) {
return {EntryInst->getNextNonDebugInstruction(
false /*Only debug instructions*/)};
}
return {EntryInst};
} else {
LOG_IF_ENABLE(BOOST_LOG_SEV(lg::get(), DEBUG)
<< "Could not get starting points of '"
Expand Down
65 changes: 4 additions & 61 deletions lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,65 +444,6 @@ const llvm::Function *LLVMBasedICFG::getFunction(const string &Fun) const {
return IRDB.getFunction(Fun);
}

std::vector<const llvm::Instruction *>
LLVMBasedICFG::getPredsOf(const llvm::Instruction *Inst) const {
if (!IgnoreDbgInstructions) {
if (Inst->getPrevNode()) {
return {Inst->getPrevNode()};
}
} else {
if (Inst->getPrevNonDebugInstruction()) {
return {Inst->getPrevNonDebugInstruction()};
}
}
// If we do not have a predecessor yet, look for basic blocks which
// lead to our instruction in question!

vector<const llvm::Instruction *> Preds;
std::transform(llvm::pred_begin(Inst->getParent()),
llvm::pred_end(Inst->getParent()), back_inserter(Preds),
[](const llvm::BasicBlock *BB) {
assert(BB && "BB under analysis was not well formed.");
return BB->getTerminator();
});

/// TODO: Add function-local static variable initialization workaround here

return Preds;
}

std::vector<const llvm::Instruction *>
LLVMBasedICFG::getSuccsOf(const llvm::Instruction *Inst) const {

vector<const llvm::Instruction *> Successors;
// case we wish to consider LLVM's debug instructions
if (!IgnoreDbgInstructions) {
if (Inst->getNextNode()) {
Successors.push_back(Inst->getNextNode());
}
} else {
if (Inst->getNextNonDebugInstruction()) {
Successors.push_back(Inst->getNextNonDebugInstruction());
}
}
if (Successors.empty()) {
if (const auto *Branch = llvm::dyn_cast<llvm::BranchInst>(Inst);
Branch && isStaticVariableLazyInitializationBranch(Branch)) {
// Skip the "already initialized" case, such that the analysis is always
// aware of the initialized value.
Successors.push_back(&Branch->getSuccessor(0)->front());

} else {
Successors.reserve(Inst->getNumSuccessors() + Successors.size());
std::transform(llvm::succ_begin(Inst), llvm::succ_end(Inst),
std::back_inserter(Successors),
[](const llvm::BasicBlock *BB) { return &BB->front(); });
}
}

return Successors;
}

const llvm::Function *LLVMBasedICFG::getFirstGlobalCtorOrNull() const {
auto it = GlobalCtors.begin();
if (it != GlobalCtors.end()) {
Expand Down Expand Up @@ -676,11 +617,13 @@ LLVMBasedICFG::getReturnSitesOfCallAt(const llvm::Instruction *N) const {
auto *UnwindSucc = &Invoke->getUnwindDest()->front();
if (!IgnoreDbgInstructions &&
llvm::isa<llvm::DbgInfoIntrinsic>(NormalSucc)) {
NormalSucc = NormalSucc->getNextNonDebugInstruction();
NormalSucc = NormalSucc->getNextNonDebugInstruction(
false /*Only debug instructions*/);
}
if (!IgnoreDbgInstructions &&
llvm::isa<llvm::DbgInfoIntrinsic>(UnwindSucc)) {
UnwindSucc = UnwindSucc->getNextNonDebugInstruction();
UnwindSucc = UnwindSucc->getNextNonDebugInstruction(
false /*Only debug instructions*/);
}
if (NormalSucc != nullptr) {
ReturnSites.insert(NormalSucc);
Expand Down