Skip to content
Snippets Groups Projects
Commit 47104c0f authored by Alessandro Di Federico's avatar Alessandro Di Federico
Browse files

Replace `getNext` with `nextNonMarker`

Most of the times, when we need to get the next instruction, we actually
want to skip over "marker" function calls (e.g., calls to `newpc` and
`function_call`). `nextNonMarker` does exactly this.

`FunctionCallIdentification::isCall` and `JumpTargetManager::setCFGForm`
have also been extended to correctly handle such situations.
parent 486f289e
No related branches found
No related tags found
No related merge requests found
...@@ -218,7 +218,7 @@ void FBD::collectFunctionCalls() { ...@@ -218,7 +218,7 @@ void FBD::collectFunctionCalls() {
if (auto *Call = dyn_cast<CallInst>(U)) { if (auto *Call = dyn_cast<CallInst>(U)) {
BasicBlock *ReturnBB = getBlock(Call->getOperand(1)); BasicBlock *ReturnBB = getBlock(Call->getOperand(1));
uint32_t ReturnPC = getLimitedValue(Call->getOperand(2)); uint32_t ReturnPC = getLimitedValue(Call->getOperand(2));
auto *Terminator = cast<TerminatorInst>(getNext(Call)); auto *Terminator = cast<TerminatorInst>(nextNonMarker(Call));
assert(Terminator != nullptr); assert(Terminator != nullptr);
FunctionCalls[Terminator] = ReturnBB; FunctionCalls[Terminator] = ReturnBB;
CallPredecessors[ReturnBB].push_back(Call->getParent()); CallPredecessors[ReturnBB].push_back(Call->getParent());
......
...@@ -44,11 +44,13 @@ public: ...@@ -44,11 +44,13 @@ public:
bool isCall(llvm::TerminatorInst *T) const { bool isCall(llvm::TerminatorInst *T) const {
assert(T != nullptr); assert(T != nullptr);
llvm::Instruction *Previous = getPrevious(T); llvm::Instruction *Previous = getPrevious(T);
if (Previous == nullptr) while (Previous != nullptr && isMarker(Previous)) {
return false; auto *Call = llvm::cast<llvm::CallInst>(Previous);
if (Call->getCalledFunction() == FunctionCall)
return true;
if (auto *Call = llvm::dyn_cast<llvm::CallInst>(Previous)) Previous = getPrevious(Previous);
return Call->getCalledFunction() == FunctionCall; }
return false; return false;
} }
......
...@@ -1237,7 +1237,7 @@ void JumpTargetManager::setCFGForm(CFGForm NewForm) { ...@@ -1237,7 +1237,7 @@ void JumpTargetManager::setCFGForm(CFGForm NewForm) {
if (auto *FunctionCall = TheModule.getFunction("function_call")) { if (auto *FunctionCall = TheModule.getFunction("function_call")) {
for (User *U : FunctionCall->users()) { for (User *U : FunctionCall->users()) {
auto *Call = cast<CallInst>(U); auto *Call = cast<CallInst>(U);
auto *Terminator = cast<TerminatorInst>(Call->getNextNode()); auto *Terminator = cast<TerminatorInst>(nextNonMarker(Call));
assert(Terminator->getNumSuccessors() == 1); assert(Terminator->getNumSuccessors() == 1);
// Get the correct argument, the first is the callee, the second the // Get the correct argument, the first is the callee, the second the
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
// Local includes
#include "ir-helpers.h"
namespace llvm { namespace llvm {
class GlobalVariable; class GlobalVariable;
}; };
...@@ -139,4 +141,30 @@ static inline bool contains(T Range, typename T::value_type V) { ...@@ -139,4 +141,30 @@ static inline bool contains(T Range, typename T::value_type V) {
return std::find(std::begin(Range), std::end(Range), V) != std::end(Range); return std::find(std::begin(Range), std::end(Range), V) != std::end(Range);
} }
static const std::array<llvm::StringRef, 3> MarkerFunctionNames = {
"newpc",
"function_call",
"exitTB"
};
static inline bool isMarker(llvm::Instruction *I) {
using namespace std::placeholders;
using llvm::any_of;
using std::bind;
return any_of(MarkerFunctionNames, bind(isCallTo, I, _1));
}
static inline llvm::Instruction *nextNonMarker(llvm::Instruction *I) {
auto It = I->getIterator();
auto End = I->getParent()->end();
do {
It++;
assert(It != End);
} while (isMarker(&*It));
assert(It != End);
return &*It;
}
#endif // _REVAMB_H #endif // _REVAMB_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment