Compare commits
4 Commits
master
...
5337dd75cd
| Author | SHA1 | Date | |
|---|---|---|---|
| 5337dd75cd | |||
| c11942584a | |||
| 79d57493b8 | |||
| 8af2221626 |
@@ -24,7 +24,7 @@ namespace SAPFOR
|
||||
|
||||
std::vector<BasicBlock*> next;
|
||||
std::vector<BasicBlock*> prev;
|
||||
|
||||
BasicBlock* idom{};
|
||||
//reaching definition
|
||||
std::map<SAPFOR::Argument*, std::set<int>> RD_in, RD_out;
|
||||
|
||||
@@ -42,6 +42,7 @@ namespace SAPFOR
|
||||
void addInstruction(IR_Block* item);
|
||||
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }
|
||||
void addNext(BasicBlock* next_) { next.push_back(next_); }
|
||||
void setIdom(BasicBlock* idom_) { idom = idom_; }
|
||||
|
||||
int removePrev(BasicBlock* removed);
|
||||
int removeNext(BasicBlock* removed);
|
||||
@@ -69,7 +70,8 @@ namespace SAPFOR
|
||||
const std::vector<IR_Block*>& getInstructions() const { return instructions; }
|
||||
const std::vector<BasicBlock*>& getNext() const { return next; }
|
||||
const std::vector<BasicBlock*>& getPrev() const { return prev; }
|
||||
|
||||
BasicBlock* getIdom() const { return idom; }
|
||||
|
||||
/*
|
||||
* FOR LIVE ANALYSIS
|
||||
*/
|
||||
@@ -146,4 +148,4 @@ static inline void deleteCFG(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*
|
||||
byFunc.second.clear();
|
||||
}
|
||||
cfg.clear();
|
||||
}
|
||||
}
|
||||
|
||||
111
src/ProjectParameters/domTree.h
Normal file
111
src/ProjectParameters/domTree.h
Normal file
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include "vector"
|
||||
#include "map"
|
||||
|
||||
#include "../CFGraph/CFGraph.h"
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace SAPFOR {
|
||||
class DominatorFinder {
|
||||
private:
|
||||
BasicBlock* entry;
|
||||
std::vector<BasicBlock*> vertices;
|
||||
std::unordered_map<BasicBlock*, int> dfs_num;
|
||||
std::vector<int> parent, semi, vertex, ancestor, label;
|
||||
std::vector<std::vector<int>> bucket;
|
||||
int n;
|
||||
|
||||
void DFS(BasicBlock* v, int parent_num) {
|
||||
dfs_num[v] = n;
|
||||
vertex[n] = n;
|
||||
semi[n] = n;
|
||||
label[n] = n;
|
||||
ancestor[n] = -1;
|
||||
parent[n] = parent_num;
|
||||
vertices[n++] = v;
|
||||
|
||||
for (const auto& w : v->getNext()) {
|
||||
if (dfs_num[w] == -1) {
|
||||
DFS(w, dfs_num[v]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Compress(int v) {
|
||||
if (ancestor[ancestor[v]] != -1) {
|
||||
Compress(ancestor[v]);
|
||||
if (semi[label[ancestor[v]]] < semi[label[v]])
|
||||
label[v] = label[ancestor[v]];
|
||||
ancestor[v] = ancestor[ancestor[v]];
|
||||
}
|
||||
}
|
||||
|
||||
int Eval(int v) {
|
||||
if (ancestor[v] == -1) return v;
|
||||
Compress(v);
|
||||
return label[v];
|
||||
}
|
||||
|
||||
void Link(int v, int w) {
|
||||
ancestor[w] = v;
|
||||
}
|
||||
|
||||
public:
|
||||
DominatorFinder(std::vector<BasicBlock*>& blocks) {
|
||||
if (blocks.empty()) return;
|
||||
entry = blocks[0];
|
||||
n = 0;
|
||||
|
||||
for (auto block : blocks) dfs_num[block] = -1;
|
||||
|
||||
int max_size = blocks.size();
|
||||
vertices.resize(max_size);
|
||||
parent.assign(max_size, -1);
|
||||
semi.assign(max_size, -1);
|
||||
vertex.assign(max_size, -1);
|
||||
ancestor.assign(max_size, -1);
|
||||
label.assign(max_size, -1);
|
||||
bucket.resize(max_size);
|
||||
|
||||
DFS(entry, -1);
|
||||
|
||||
for (int i = n - 1; i > 0; --i) {
|
||||
int w = vertex[i];
|
||||
|
||||
for (BasicBlock* v : vertices[w]->getPrev()) {
|
||||
int u = Eval(dfs_num[v]);
|
||||
if (semi[u] < semi[w])
|
||||
semi[w] = semi[u];
|
||||
}
|
||||
bucket[vertex[semi[w]]].push_back(w);
|
||||
Link(parent[w], w);
|
||||
|
||||
for (int v : bucket[parent[w]])
|
||||
{
|
||||
int u = Eval(v);
|
||||
if (semi[u] < semi[v])
|
||||
vertices[v]->setIdom(vertices[u]);
|
||||
else
|
||||
vertices[v]->setIdom(vertices[parent[w]]);
|
||||
}
|
||||
bucket[parent[w]].clear();
|
||||
}
|
||||
|
||||
for (int i = 1; i < n; ++i) {
|
||||
int w = vertex[i];
|
||||
if (vertices[w]->getIdom() != vertices[vertex[semi[w]]])
|
||||
vertices[w]->setIdom(vertices[w]->getIdom()->getIdom());
|
||||
}
|
||||
|
||||
entry->setIdom(nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
void buildDominatorTreeLT(std::vector<BasicBlock*>& blocks) {
|
||||
DominatorFinder finder(blocks);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +1,438 @@
|
||||
#include "../Utils/leak_detector.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
|
||||
#include "../Utils/SgUtils.h"
|
||||
#include "../CFGraph/CFGraph.h"
|
||||
#include "CFGraph/IR.h"
|
||||
#include "Distribution/Array.h"
|
||||
#include "dvm.h"
|
||||
#include "../Utils/errors.h"
|
||||
#include "../Utils/SgUtils.h"
|
||||
#include "../GraphCall/graph_calls.h"
|
||||
#include "../GraphCall/graph_calls_func.h"
|
||||
|
||||
#include "libSage++.h"
|
||||
#include "projectParameters.h"
|
||||
#include "domTree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
map< pair<string, int>, set<string>>
|
||||
findParameters(const map<string, vector<DefUseList>> &defUseByFunctions,
|
||||
const map<string, CommonBlock*> &commonBlocks,
|
||||
const map<string, vector<FuncInfo*>> &allFuncInfo)
|
||||
tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> stmtToIR(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, SgStatement* stmt)
|
||||
{
|
||||
map< pair<string, int>, set<string>> foundParameters;
|
||||
|
||||
|
||||
return foundParameters;
|
||||
}
|
||||
SgStatement* cur = stmt;
|
||||
cur->switchToFile();
|
||||
while (cur->variant() != PROC_HEDR && cur->variant() != PROG_HEDR && cur->variant() != FUNC_HEDR)
|
||||
cur = cur->controlParent();
|
||||
|
||||
string funcName = ((SgProcHedrStmt*)cur)->nameWithContains();
|
||||
|
||||
int stmtID = stmt->id();
|
||||
for (const auto& [func, bblocks] : CFGraph)
|
||||
{
|
||||
if (func->funcName != funcName)
|
||||
continue;
|
||||
|
||||
for (auto basicBlock : bblocks)
|
||||
for (auto ins : basicBlock->getInstructions())
|
||||
if (stmtID == ins->getInstruction()->getOperator()->id())
|
||||
return make_tuple(func, ins->getInstruction(), basicBlock);
|
||||
}
|
||||
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
return { NULL, NULL, NULL };
|
||||
}
|
||||
|
||||
tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> IRByNumber(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, int num)
|
||||
{
|
||||
if (num < 0)
|
||||
return { NULL, NULL, NULL };
|
||||
|
||||
for (const auto& [func, bblocks] : CFGraph)
|
||||
for (auto byBB : bblocks)
|
||||
if (byBB->getInstructions().front()->getNumber() <= num && byBB->getInstructions().back()->getNumber() >= num)
|
||||
return make_tuple(func, getInstructionByNumber(byBB->getInstructions(), num), byBB);
|
||||
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
return { NULL, NULL, NULL};
|
||||
}
|
||||
|
||||
template<typename Iterator>
|
||||
void processArgument(set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* arg,
|
||||
Iterator instr,
|
||||
Iterator first_instr)
|
||||
{
|
||||
if (arg == NULL)
|
||||
return;
|
||||
if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
||||
extract_vars_from_reg(worklist, arg, instr, first_instr);
|
||||
else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||
{
|
||||
std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT
|
||||
worklist.insert(arg);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Iterator>
|
||||
void extract_vars_from_reg(set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* reg,
|
||||
Iterator instr,
|
||||
Iterator first_instr)
|
||||
{
|
||||
for (; instr >= first_instr; instr--)
|
||||
{
|
||||
if ((*instr)->getInstruction()->getResult() == reg)
|
||||
{
|
||||
processArgument(worklist, (*instr)->getInstruction()->getArg1(), instr, first_instr);
|
||||
processArgument(worklist, (*instr)->getInstruction()->getArg2(), instr, first_instr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Instruction* instr,
|
||||
SAPFOR::BasicBlock* bblock,
|
||||
FuncInfo* cur_func,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
{
|
||||
while (bblock)
|
||||
{
|
||||
std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT
|
||||
|
||||
auto first_instr = bblock->getInstructions().begin();
|
||||
auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) {
|
||||
return i->getInstruction() == instr;
|
||||
});
|
||||
|
||||
for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--)
|
||||
{
|
||||
auto instr = (*cur_instr)->getInstruction();
|
||||
// std::cout << instr->getNumber() << '\n';
|
||||
auto result_arg = instr->getResult();
|
||||
auto arg1 = instr->getArg1();
|
||||
auto arg2 = instr->getArg2();
|
||||
|
||||
if (worklist.count(result_arg))
|
||||
{
|
||||
processArgument(worklist, arg1, cur_instr, first_instr);
|
||||
processArgument(worklist, arg2, cur_instr, first_instr);
|
||||
std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT
|
||||
worklist.erase(result_arg);
|
||||
}
|
||||
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1))
|
||||
{
|
||||
// skip to F_CALL
|
||||
auto f_call_instr = cur_instr;
|
||||
while ((*f_call_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL)
|
||||
f_call_instr++;
|
||||
|
||||
if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ")
|
||||
{
|
||||
auto stmt_before = (*f_call_instr)->getInstruction()->getOperator();
|
||||
auto filename = stmt_before->fileName();
|
||||
auto line = stmt_before->lineNumber();
|
||||
auto var_name = arg1->getValue().substr(arg1->getValue().find('%') + 1);
|
||||
__spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg1->getValue().c_str(), line, filename);
|
||||
auto toAdd = make_tuple(stmt_before, var_name, MODE::AFTER);
|
||||
where_to_add.insert(toAdd);
|
||||
std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT
|
||||
worklist.erase(arg1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto& RD = bblock->getRD_In();
|
||||
map<SAPFOR::BasicBlock*, SAPFOR::Instruction*> group_by_block;
|
||||
for (auto& arg : worklist)
|
||||
{
|
||||
if (RD.count(arg))
|
||||
{
|
||||
if (RD.at(arg).size() == 1 && *RD.at(arg).begin() == SAPFOR::CFG_VAL::UNINIT)
|
||||
__spf_print(1, "variable %s has no definition", arg->getValue().c_str());
|
||||
else if (RD.at(arg).size() > 1)
|
||||
{
|
||||
auto stmt_after = (*first_instr)->getInstruction()->getOperator();
|
||||
auto filename = stmt_after->fileName();
|
||||
auto line = stmt_after->lineNumber();
|
||||
auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1);
|
||||
__spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str());
|
||||
__spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename);
|
||||
auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE);
|
||||
where_to_add.insert(toAdd);
|
||||
std::cout << "worklist erase: " << arg->getValue() << std::endl; //DEBUG PRINT
|
||||
worklist.erase(arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto instr_num = *RD.at(arg).begin();
|
||||
auto [func, instr, bblock] = IRByNumber(fullIR, instr_num);
|
||||
if (cur_func == func && group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num)
|
||||
group_by_block[bblock] = instr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (bblock && group_by_block.find(bblock) == group_by_block.end())
|
||||
bblock = bblock->getIdom();
|
||||
if (bblock)
|
||||
instr = group_by_block[bblock];
|
||||
}
|
||||
|
||||
// other variables are from global scope
|
||||
const auto& RD = fullIR.at(cur_func).front()->getRD_In();
|
||||
for (auto& arg : worklist)
|
||||
{
|
||||
if (arg->isMemGlobal())
|
||||
{
|
||||
std::cout << "global: " << arg->getValue() << " : " << RD.count(arg) << '\n';
|
||||
set<int> found_rd;
|
||||
if (RD.count(arg))
|
||||
found_rd = RD.at(arg);
|
||||
if (found_rd.size() == 0)
|
||||
{
|
||||
auto call_instr = call_sites[cur_func].size() ? call_sites[cur_func].front() : NULL;
|
||||
while (call_instr && found_rd.size() == 0)
|
||||
{
|
||||
auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber());
|
||||
std::cout << "caller: " << call_func->funcName << '\n';
|
||||
if (call_bblock->getRD_Out().count(arg))
|
||||
{
|
||||
std::cout << *call_bblock->getRD_Out().at(arg).begin() << "\n";
|
||||
found_rd = call_bblock->getRD_Out().at(arg);
|
||||
}
|
||||
|
||||
call_instr = call_sites[call_func].size() ? call_sites[call_func].front() : NULL;
|
||||
}
|
||||
}
|
||||
if (found_rd.size() == 1 && *found_rd.begin() == SAPFOR::CFG_VAL::UNINIT)
|
||||
{
|
||||
__spf_print(1, "variable %s has no definition", arg->getValue().c_str());
|
||||
} else if (found_rd.size() > 1)
|
||||
{
|
||||
auto first_instr = fullIR.at(cur_func).front()->getInstructions().begin();
|
||||
auto stmt_after = (*first_instr)->getInstruction()->getOperator();
|
||||
auto filename = stmt_after->fileName();
|
||||
auto line = stmt_after->lineNumber();
|
||||
auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1);
|
||||
__spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str());
|
||||
__spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename);
|
||||
auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE);
|
||||
where_to_add.insert(toAdd);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto instr_num = *found_rd.begin();
|
||||
auto [func, instr, bblock] = IRByNumber(fullIR, instr_num);
|
||||
set<SAPFOR::Argument*> new_worklist = {arg};
|
||||
|
||||
lookup_for_vars(where_to_add, new_worklist, instr, bblock, func, fullIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const auto& call_instr : call_sites[cur_func])
|
||||
{
|
||||
set<SAPFOR::Argument*> new_worklist;
|
||||
auto params_num = cur_func->funcParams.countOfPars;
|
||||
|
||||
auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber());
|
||||
std::cout << "caller: " << call_func->funcName << '\n';
|
||||
auto first_instr = call_bblock->getInstructions().begin();
|
||||
auto cur_instr = std::find_if(first_instr, call_bblock->getInstructions().end(), [call_instr](SAPFOR::IR_Block* i) {
|
||||
return i->getInstruction() == call_instr;
|
||||
});
|
||||
for (auto& arg : worklist)
|
||||
{
|
||||
if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_)
|
||||
{
|
||||
auto param_num= stoi(arg->getValue().substr(arg->getValue().find('%', arg->getValue().find('%') + 1) + 1));
|
||||
// std::cout << params_num << '\n';
|
||||
auto param_instr = (cur_instr - (params_num - param_num));
|
||||
auto param_arg = (*param_instr)->getInstruction()->getArg1();
|
||||
std::cout << "param_val: " << param_arg->getValue() << " : " << param_num << '\n';
|
||||
processArgument(new_worklist, param_arg, param_instr, first_instr);
|
||||
}
|
||||
}
|
||||
lookup_for_vars(where_to_add, new_worklist, call_instr, call_bblock, call_func, fullIR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void handle_single_allocate(std::set<tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
SgStatement* alloc_statement,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
{
|
||||
auto [func, instr, bblock] = stmtToIR(fullIR, alloc_statement);
|
||||
|
||||
auto first_instr = bblock->getInstructions().begin();
|
||||
auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) {
|
||||
return i->getInstruction() == instr;
|
||||
});
|
||||
auto alloc_instr = cur_instr;
|
||||
|
||||
// skip to F_CALL _ALLOC n
|
||||
while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL ||
|
||||
(*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC")
|
||||
alloc_instr++;
|
||||
|
||||
|
||||
auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue());
|
||||
std::cout << "arrays_num: " << arrays_num << std::endl; //DEBUG PRINT
|
||||
|
||||
set<SAPFOR::Argument*> worklist;
|
||||
for (int i = 0; i < arrays_num; i++)
|
||||
{
|
||||
auto param_instr = --alloc_instr;
|
||||
auto param_reg = (*param_instr)->getInstruction()->getArg1();
|
||||
|
||||
while ((*param_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::LOAD ||
|
||||
(*param_instr)->getInstruction()->getResult() != param_reg)
|
||||
param_instr--;
|
||||
|
||||
|
||||
auto dimensions_num = stoi((*param_instr)->getInstruction()->getArg2()->getValue());
|
||||
|
||||
for (int j = 0; j < dimensions_num; j++)
|
||||
{
|
||||
auto ref_instr = --param_instr;
|
||||
if ((*ref_instr)->getInstruction()->getOperation() == SAPFOR::CFG_OP::RANGE)
|
||||
{
|
||||
vector<SAPFOR::Argument*> range_args = {(*ref_instr)->getInstruction()->getArg1(),
|
||||
(*ref_instr)->getInstruction()->getArg2(),
|
||||
(*ref_instr)->getInstruction()->getResult()};
|
||||
for (auto& arg : range_args)
|
||||
{
|
||||
if (arg)
|
||||
std::cout << "range-arg: " << arg->getValue() << '\n';
|
||||
processArgument(worklist, arg, ref_instr, first_instr);
|
||||
}
|
||||
} else
|
||||
{
|
||||
auto arg = (*ref_instr)->getInstruction()->getArg1();
|
||||
processArgument(worklist, arg, ref_instr, first_instr);
|
||||
}
|
||||
}
|
||||
}
|
||||
lookup_for_vars(where_to_add,worklist, instr, bblock, func, fullIR);
|
||||
}
|
||||
|
||||
|
||||
void handle_single_loop(std::set<tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
SgStatement* loop_stmt,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
{
|
||||
auto [func, instr, bblock] = stmtToIR(fullIR, loop_stmt);
|
||||
std::cout << "bblock: " << bblock->getNumber() << " instr: " << instr->getNumber() << std::endl;
|
||||
|
||||
auto cur_instr = bblock->getInstructions().end() - 1;
|
||||
|
||||
set<SAPFOR::Argument*> worklist;
|
||||
extract_vars_from_reg(worklist, (*cur_instr)->getInstruction()->getResult(), cur_instr, bblock->getInstructions().begin());
|
||||
|
||||
lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, func, fullIR);
|
||||
}
|
||||
|
||||
void
|
||||
findParameters(ResultSet& foundParameters,
|
||||
std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR,
|
||||
const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
|
||||
{
|
||||
set<tuple<SgStatement*, string, MODE>> where_to_add;
|
||||
|
||||
map<string, FuncInfo*> name_to_func;
|
||||
for (const auto& [func, _] : fullIR)
|
||||
name_to_func[func->funcName] = func;
|
||||
|
||||
for (auto& [func, bblocks] : fullIR)
|
||||
{
|
||||
for (const auto& block : bblocks)
|
||||
{
|
||||
for (const auto& ir_block : block->getInstructions())
|
||||
{
|
||||
auto instr = ir_block->getInstruction();
|
||||
if (instr->getOperation() == SAPFOR::CFG_OP::F_CALL)
|
||||
{
|
||||
auto func_name = instr->getArg1()->getValue();
|
||||
auto func_info = name_to_func.find(func_name);
|
||||
|
||||
if (func_info != name_to_func.end())
|
||||
call_sites[func_info->second].push_back(instr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SAPFOR::buildDominatorTreeLT(bblocks);
|
||||
for (auto block : bblocks)
|
||||
{
|
||||
if (block->getIdom() != NULL)
|
||||
std::cout << "BB: " << block->getNumber() << " IDOM: " << block->getIdom()->getNumber() << std::endl;
|
||||
}
|
||||
std::cout << "+++++++++++\n";
|
||||
}
|
||||
|
||||
std::set<SgStatement*> alloc_statements;
|
||||
for (const auto& [func, bblocks] : fullIR)
|
||||
for (const auto& block : bblocks)
|
||||
for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr)
|
||||
{
|
||||
auto op = (*instr)->getInstruction()->getOperator();
|
||||
if (op && op->variant() == ALLOCATE_STMT) {
|
||||
alloc_statements.insert(op);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& alloc_statement : alloc_statements)
|
||||
{
|
||||
alloc_statement->unparsestdout();
|
||||
handle_single_allocate(where_to_add, alloc_statement, fullIR);
|
||||
}
|
||||
|
||||
set<SgStatement*> for_statements;
|
||||
// Find all FOR statements in the program
|
||||
for (const auto& [func, bblocks] : fullIR)
|
||||
for (const auto& block : bblocks)
|
||||
for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr)
|
||||
{
|
||||
auto op = (*instr)->getInstruction()->getOperator();
|
||||
if (op && op->variant() == FOR_NODE)
|
||||
for_statements.insert(op);
|
||||
}
|
||||
|
||||
for (const auto& stmt : for_statements)
|
||||
{
|
||||
std::cout << string(stmt->fileName()) << ":" << stmt->lineNumber() << '\n';
|
||||
handle_single_loop(where_to_add, stmt, fullIR);
|
||||
}
|
||||
|
||||
for (const auto& [stmt_before, var_name, mode] : where_to_add)
|
||||
{
|
||||
stmt_before->switchToFile();
|
||||
SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str());
|
||||
SgVarRefExp* var = new SgVarRefExp(var_symb);
|
||||
SgValueExp* zero = new SgValueExp(1337);
|
||||
SgExprListExp* ex = new SgExprListExp();
|
||||
ex->setLhs(new SgExpression(ASSGN_OP, var, zero));
|
||||
std::cout << "stmt_before: " << stmt_before->unparse();
|
||||
SgStatement* toAdd = new SgStatement(ASSIGN_STAT, NULL, NULL, var, zero, NULL);
|
||||
toAdd->unparsestdout();
|
||||
// toAdd->lineNumber() = stmt_before->lineNumber();
|
||||
toAdd->setlineNumber(stmt_before->lineNumber());
|
||||
toAdd->setLocalLineNumber(stmt_before->lineNumber());
|
||||
toAdd->setFileId(stmt_before->getFileId());
|
||||
toAdd->setProject(stmt_before->getProject());
|
||||
stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent());
|
||||
stmt_before->controlParent()->unparsestdout();
|
||||
|
||||
foundParameters.insert(make_tuple(stmt_before->fileName(), stmt_before->lineNumber(), var_name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
std::map< std::pair<std::string, int>, std::set<std::string>> findParameters(const std::map<std::string, std::vector<DefUseList>> &defUseByFunctions, const std::map<std::string, CommonBlock*> &commonBlocks, const std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo);
|
||||
#include "libSage++.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
using ResultSet = std::set<std::tuple<std::string, int, std::string>>;
|
||||
|
||||
enum class MODE
|
||||
{
|
||||
BEFORE,
|
||||
AFTER
|
||||
};
|
||||
|
||||
static std::map<FuncInfo*, std::vector<SAPFOR::Instruction*>> call_sites;
|
||||
|
||||
template<typename Iterator>
|
||||
void extract_vars_from_reg(std::set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* reg,
|
||||
Iterator instr,
|
||||
Iterator first_instr);
|
||||
|
||||
|
||||
template<typename Iterator>
|
||||
void processArgument(std::set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* arg,
|
||||
Iterator instr,
|
||||
Iterator first_instr);
|
||||
|
||||
void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
std::set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Instruction* instr,
|
||||
SAPFOR::BasicBlock* bblock,
|
||||
FuncInfo* cur_func,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR);
|
||||
|
||||
void
|
||||
findParameters(ResultSet& foundParameters,
|
||||
std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR,
|
||||
const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays);
|
||||
|
||||
@@ -1881,7 +1881,9 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
|
||||
else if (curr_regime == RENAME_SYMBOLS)
|
||||
runRenameSymbols(&project, commonBlocks);
|
||||
else if (curr_regime == FIND_PARAMETERS)
|
||||
parametersOfProject = findParameters(defUseByFunctions, commonBlocks, allFuncInfo);
|
||||
{
|
||||
findParameters(parametersOfProject, fullIR, declaredArrays);
|
||||
}
|
||||
else if (curr_regime == BUILD_IR)
|
||||
{
|
||||
auto CFG_forFile = buildCFG(commonBlocks, allFuncInfo_IR, SAPFOR::CFG_Settings(0));
|
||||
@@ -2326,6 +2328,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
|
||||
case INSERT_NO_DISTR_FLAGS_FROM_GUI:
|
||||
case PRIVATE_REMOVING:
|
||||
case RENAME_INLCUDES:
|
||||
case FIND_PARAMETERS:
|
||||
runAnalysis(*project, curr_regime, true, "", folderName);
|
||||
break;
|
||||
case INLINE_PROCEDURES:
|
||||
|
||||
@@ -168,7 +168,7 @@ std::map<int, UserFiles> filesInfo; // information about open,close,write and re
|
||||
//
|
||||
|
||||
//for FIND_PARAMETERS
|
||||
std::map< std::pair<std::string, int>, std::set<std::string>> parametersOfProject; // [file, line] -> set[vars]
|
||||
std::set<std::tuple<std::string, int, std::string>> parametersOfProject; // [file, line, varname]
|
||||
//
|
||||
|
||||
//for GET_MIN_MAX_BLOCK_DIST
|
||||
|
||||
52
tests/sapfor/parameter/dynamic_array_maximum.f90
Normal file
52
tests/sapfor/parameter/dynamic_array_maximum.f90
Normal file
@@ -0,0 +1,52 @@
|
||||
program dynamic_array_maximum_3d
|
||||
implicit none
|
||||
integer :: n1, n2, n3, n4 , k, i, j, l, a
|
||||
integer :: sum3
|
||||
real :: max_element
|
||||
real, allocatable :: array(:,:,:), array2(:,:,:), array3(:,:,:)
|
||||
|
||||
write(*, *) "Enter 3 integers"
|
||||
read(*, *) n, m, k
|
||||
m = 100
|
||||
|
||||
if (1 .eq. 1) then
|
||||
a = 3
|
||||
else if (2 .eq. 1) then
|
||||
a = 4
|
||||
endif
|
||||
|
||||
m = m + 1
|
||||
k = m * 1000 + n * 10
|
||||
|
||||
allocate(array(n, m + n, k + m + n), &
|
||||
&array2(k, m + n, k), &
|
||||
&array3(k, m, k + n))
|
||||
|
||||
call random_seed()
|
||||
do i = 1, n1
|
||||
do j = 1, m * n1
|
||||
do l = 1, k * m * n1
|
||||
call random_number(array(i,j,l))
|
||||
array(i,j,l) = int(array(i,j,l) * 100)
|
||||
end do
|
||||
end do
|
||||
end do
|
||||
|
||||
max_element = array(1,1,1)
|
||||
do i = 1, n1
|
||||
do j = 1, m
|
||||
do l = 1, k
|
||||
max_element = MAX(array(i,j,l), max_element)
|
||||
end do
|
||||
end do
|
||||
end do
|
||||
deallocate(array, array2, array3)
|
||||
write(*, *) max_element
|
||||
end program dynamic_array_maximum_3d
|
||||
|
||||
! function sum3(x, y, z)
|
||||
! implicit none
|
||||
! integer :: x, y, z
|
||||
! integer :: sum3
|
||||
! sum3 = x + y + z
|
||||
! end function sum3
|
||||
Reference in New Issue
Block a user