-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLoopPredAssumePass.cpp
More file actions
83 lines (69 loc) · 2.74 KB
/
Copy pathLoopPredAssumePass.cpp
File metadata and controls
83 lines (69 loc) · 2.74 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
/**
* Copyright (C) 2014 - 2020 Map2Check tool
* This file is part of the Map2Check tool, and is made available under
* the terms of the GNU General Public License version 2.
*
* LLVM -> NCSA
*
* SPDX-License-Identifier: (GPL-2.0 AND NCSA)
**/
#include "LoopPredAssumePass.hpp"
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
using llvm::IRBuilder;
namespace {
inline Instruction* BBIteratorToInst(BasicBlock::iterator i) {
Instruction* pointer = reinterpret_cast<Instruction*>(&*i);
return pointer;
}
} // namespace
void LoopPredAssumePass::getConditionInLoop(Loop* L) {
BasicBlock* header = L->getHeader();
this->map2check_assume =
header->getParent()->getParent()->getOrInsertFunction(
"map2check_assume_loop", Type::getVoidTy(header->getContext()),
Type::getInt1Ty(header->getContext()));
if (BranchInst* bi = dyn_cast<BranchInst>(header->getTerminator())) {
if (bi->isConditional()) {
Value* loopCond = bi->getCondition();
CmpInst* cmpInst = dyn_cast<CmpInst>(&*loopCond);
if (bi->getNumSuccessors() > 0) {
BasicBlock* succ_cond_bb = bi->getSuccessor(0);
BasicBlock::iterator iT = --succ_cond_bb->end();
auto* new_inst = cmpInst->clone();
auto* inst_pos = dyn_cast<Instruction>(&*--succ_cond_bb->end());
new_inst->insertBefore(inst_pos);
CmpInst* new_cmpInst = dyn_cast<CmpInst>(&*new_inst);
new_cmpInst->setPredicate(new_cmpInst->getInversePredicate());
Value* new_loop_cond =
dyn_cast<Value>(&*new_cmpInst); // Convert CmpInst to Value;
IRBuilder<> builder(BBIteratorToInst(iT));
Value* args[] = {new_loop_cond};
builder.CreateCall(this->map2check_assume, args);
}
}
}
}
PreservedAnalyses LoopPredAssumePass::run(Loop& L,
llvm::LoopAnalysisManager& AM,
llvm::LoopStandardAnalysisResults& AR,
llvm::LPMUpdater& U) {
getConditionInLoop(&L);
return PreservedAnalyses::none();
}
// --- New Pass Manager plugin registration ---
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "LoopPredAssumePass", LLVM_VERSION_STRING,
[](llvm::PassBuilder& PB) {
PB.registerPipelineParsingCallback(
[](llvm::StringRef Name, llvm::LoopPassManager& LPM,
llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (Name == "loop-pred-assume") {
LPM.addPass(LoopPredAssumePass());
return true;
}
return false;
});
}};
}