| 1 |
//===- FunctionExternalizeFinder.cpp - Find symbols to be externalized *- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Finds which symbols must be externalized when extracting functions. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#include "FunctionExternalizeFinder.hh" |
| 17 |
#include "FunctionDepsFinder.hh" |
| 18 |
#include "LLVMMisc.hh" |
| 19 |
|
| 20 |
FunctionExternalizeFinder::FunctionExternalizeFinder(ASTUnit *ast, |
| 21 |
std::vector<std::string> &to_extract, |
| 22 |
std::vector<std::string> &to_externalize, |
| 23 |
bool keep_includes, |
| 24 |
InlineAnalysis &ia) |
| 25 |
: MustExternalize(to_externalize.begin(), to_externalize.end()), |
| 26 |
MustNotExternalize(to_extract.begin(), to_extract.end()), |
| 27 |
ToExtract(to_extract.begin(), to_extract.end()), |
| 28 |
KeepIncludes(keep_includes), |
| 29 |
AST(ast), |
| 30 |
IA(ia) |
| 31 |
{ |
| 32 |
Run_Analysis(); |
| 33 |
|
| 34 |
/* Compute intersection. They must be empty. */ |
| 35 |
std::unordered_set<std::string> c; |
| 36 |
for (const std::string &e : MustNotExternalize) { |
| 37 |
if (MustExternalize.count(e) > 0) { |
| 38 |
c.insert(e); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
assert(c.size() == 0 && "Exernalize INTERSECTION Export is not empty."); |
| 43 |
} |
| 44 |
|
| 45 |
bool FunctionExternalizeFinder::Should_Externalize(CallGraphNode *node) |
| 46 |
{ |
| 47 |
Decl *decl = node->getDecl(); |
| 48 |
if (decl) { |
| 49 |
return Should_Externalize(dyn_cast<FunctionDecl>(decl)); |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
bool FunctionExternalizeFinder::Should_Externalize(const DeclaratorDecl *decl) |
| 56 |
{ |
| 57 |
if (!decl) |
| 58 |
return false; |
| 59 |
|
| 60 |
if (Must_Not_Externalize(decl)) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
/* Builtin stuff inserted by the compiled should never be externalized. */ |
| 65 |
if (Is_Builtin_Decl(decl)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
/* In case the declarator is a function. */ |
| 70 |
if (const FunctionDecl *func = dyn_cast<const FunctionDecl>(decl)) { |
| 71 |
|
| 72 |
/* If the function is marked for extraction then we don't externalize it. */ |
| 73 |
if (Should_Extract(func)) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/* If the function is declared inline then do not externalize it. */ |
| 78 |
if (func->isInlined()) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
/* TODO: Get mangled name. */ |
| 83 |
const std::string &func_name = decl->getNameAsString(); |
| 84 |
if (IA.Needs_Externalization(func_name) == ExternalizationType::NONE) { |
| 85 |
/* No need for externalization. */ |
| 86 |
return false; |
| 87 |
} else { |
| 88 |
/* Externalization is necessary. */ |
| 89 |
return true; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/* In case the declarator is a variable. */ |
| 94 |
if (const VarDecl *var = dyn_cast<const VarDecl>(decl)) { |
| 95 |
|
| 96 |
/* If the variable is not global there is no point in externalizing it. */ |
| 97 |
if (!var->hasGlobalStorage()) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/* If the symbol needs to be externalized, then do it. */ |
| 102 |
const std::string &var_name = decl->getNameAsString(); |
| 103 |
if (IA.Needs_Externalization(var_name) == ExternalizationType::NONE) { |
| 104 |
/* No need for externalization. */ |
| 105 |
return false; |
| 106 |
} else { |
| 107 |
/* Externalization is necessary. */ |
| 108 |
return true; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* Not a function or variable? */ |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
bool FunctionExternalizeFinder::Analyze_Function(FunctionDecl *decl) |
| 117 |
{ |
| 118 |
if (!decl) |
| 119 |
return false; |
| 120 |
|
| 121 |
if (Should_Externalize(decl)) { |
| 122 |
Mark_For_Externalization(decl->getName().str()); |
| 123 |
return true; |
| 124 |
} else { |
| 125 |
/* Else check if we need to externalize any of its variables. */ |
| 126 |
Externalize_DeclRefs(decl); |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
bool FunctionExternalizeFinder::Analyze_Node(CallGraphNode *node) |
| 132 |
{ |
| 133 |
Decl *ndecl = node->getDecl(); |
| 134 |
FunctionDecl *decl = ndecl ? dyn_cast<FunctionDecl>(ndecl) : nullptr; |
| 135 |
|
| 136 |
if (Is_Already_Analyzed(node)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
AnalyzedNodes.insert(node); |
| 141 |
|
| 142 |
if (Analyze_Function(decl)) { |
| 143 |
return true; |
| 144 |
} else { |
| 145 |
/* Continue looking. */ |
| 146 |
CallGraphNode::const_iterator child_it; |
| 147 |
for (child_it = node->begin(); child_it != node->end(); ++child_it) { |
| 148 |
CallGraphNode *child = child_it->Callee; |
| 149 |
Analyze_Node(child); |
| 150 |
} |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
bool FunctionExternalizeFinder::Externalize_DeclRefs(FunctionDecl *decl) |
| 156 |
{ |
| 157 |
if (!decl) |
| 158 |
return false; |
| 159 |
|
| 160 |
bool externalized = false; |
| 161 |
|
| 162 |
Stmt *body = decl->getBody(); |
| 163 |
if (body) { |
| 164 |
externalized = Externalize_DeclRefs(body); |
| 165 |
} |
| 166 |
|
| 167 |
return externalized; |
| 168 |
} |
| 169 |
|
| 170 |
bool FunctionExternalizeFinder::Externalize_DeclRefs(Stmt *stmt) |
| 171 |
{ |
| 172 |
if (!stmt) |
| 173 |
return false; |
| 174 |
|
| 175 |
bool externalized = false; |
| 176 |
|
| 177 |
if (DeclRefExpr::classof(stmt)) { |
| 178 |
DeclRefExpr *expr = (DeclRefExpr *) stmt; |
| 179 |
DeclaratorDecl *decl = dyn_cast<DeclaratorDecl>(expr->getDecl()); |
| 180 |
|
| 181 |
if (Should_Externalize(decl)) { |
| 182 |
externalized = Mark_For_Externalization(decl->getNameAsString()); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/* Iterate through the list of all children Statements and repeat the |
| 187 |
process. */ |
| 188 |
clang::Stmt::child_iterator it, it_end; |
| 189 |
for (it = stmt->child_begin(), it_end = stmt->child_end(); |
| 190 |
it != it_end; ++it) { |
| 191 |
|
| 192 |
Stmt *child = *it; |
| 193 |
externalized |= Externalize_DeclRefs(child); |
| 194 |
} |
| 195 |
|
| 196 |
return externalized; |
| 197 |
} |
| 198 |
|
| 199 |
void FunctionExternalizeFinder::Run_Analysis(void) |
| 200 |
{ |
| 201 |
//CallGraphWithBodylessFunctions *cg = Build_CallGraph_From_AST(AST); |
| 202 |
CallGraph *cg = Build_CallGraph_From_AST(AST); |
| 203 |
|
| 204 |
for (auto it = cg->begin(); it != cg->end(); ++it) { |
| 205 |
CallGraphNode *node = (*it).getSecond().get(); |
| 206 |
if (node->getDecl()) { |
| 207 |
if (FunctionDecl *func = node->getDefinition()) { |
| 208 |
if (Should_Extract(func)) { |
| 209 |
Analyze_Node(node); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
delete cg; |
| 216 |
} |
| 217 |
|
| 218 |
std::vector<std::string> FunctionExternalizeFinder::Get_To_Externalize(void) |
| 219 |
{ |
| 220 |
std::vector<std::string> externalize; |
| 221 |
|
| 222 |
for (const std::string &name : MustExternalize) { |
| 223 |
externalize.push_back(name); |
| 224 |
} |
| 225 |
|
| 226 |
return externalize; |
| 227 |
} |