-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCallExtractor.cpp
More file actions
112 lines (102 loc) · 3.83 KB
/
CallExtractor.cpp
File metadata and controls
112 lines (102 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//===--- CallExtractor.cpp - Extract each call into a new block -*- C++ -*-===//
//
// Traits Static Analyzer (SAPFOR)
//
// Copyright 2019 DVM System Group
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===---------------------------------------------------------------------===//
//
// This file implements a pass to extract each call instruction
// (except debug instructions) into its own new basic block.
//
//===---------------------------------------------------------------------===//
#include "tsar/Transform/IR/Passes.h"
#include "tsar/Analysis/KnownFunctionTraits.h"
#include <bcl/utility.h>
#include <llvm/Analysis/LoopInfo.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/Pass.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
using namespace tsar;
namespace {
class CallExtractorPass : public FunctionPass, private bcl::Uncopyable {
public:
static char ID;
CallExtractorPass() : FunctionPass(ID) {
initializeCallExtractorPassPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function& F) override;
};
}
#undef DEBUG_TYPE
#define DEBUG_TYPE "extract-call"
char CallExtractorPass::ID = 0;
INITIALIZE_PASS(CallExtractorPass, "extract-call",
"Extract calls into new basic block", false, false)
FunctionPass * llvm::createCallExtractorPass() {
return new CallExtractorPass();
}
inline static CallInst * needToExtract(Instruction *Inst) {
if (auto *II = dyn_cast<IntrinsicInst>(Inst))
return isMemoryMarkerIntrinsic(II->getIntrinsicID()) ||
isDbgInfoIntrinsic(II->getIntrinsicID()) ? nullptr : cast<CallInst>(Inst);
return dyn_cast<CallInst>(Inst);
}
inline static Instruction *getNextUsefulInstruction(Instruction *Inst) {
for (Instruction *I = Inst->getNextNode(); I; I = I->getNextNode()) {
if (auto *II = dyn_cast<IntrinsicInst>(Inst))
if (isMemoryMarkerIntrinsic(II->getIntrinsicID()) ||
isDbgInfoIntrinsic(II->getIntrinsicID()))
continue;
return I;
}
return nullptr;
}
bool CallExtractorPass::runOnFunction(Function& F) {
LLVM_DEBUG(dbgs() << "[EXTRACT CALL]: start processing of the function "
<< F.getName() << "\n");
if (!F.empty()) {
for (auto CurrBB = F.begin(), LastBB = F.end();
CurrBB != LastBB; ++CurrBB) {
auto *TermInst = CurrBB->getTerminator();
if (TermInst == nullptr || CurrBB->size() < 2)
continue;
auto CurrInstr = CurrBB->begin();
if (auto CallCurrInst = needToExtract(&*CurrInstr)) {
auto NextInstr = getNextUsefulInstruction(CallCurrInst);
assert(NextInstr && "Instruction must not be null!");
if (NextInstr != TermInst)
CurrBB->splitBasicBlock(NextInstr);
} else {
for (Instruction* I = &*(++CurrInstr); I != TermInst;
++CurrInstr, I = &*CurrInstr) {
if (auto* CallCurrInst = needToExtract(I)) {
BasicBlock* NewBB = CurrBB->splitBasicBlock(CallCurrInst);
auto NextInstr = getNextUsefulInstruction(CallCurrInst);
assert(NextInstr && "Instruction must not be null!");
if (NextInstr != TermInst)
NewBB->splitBasicBlock(NextInstr);
break;
}
}
}
}
}
LLVM_DEBUG(dbgs() << "[EXTRACT CALL]: end processing of the function "
<< F.getName() << "\n");
return true;
}