Skip to content

Commit

Permalink
Fix for issue #496 (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Budiu authored and ChrisDodd committed Apr 22, 2017
1 parent f3f5b64 commit a9beb5b
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 149 deletions.
2 changes: 1 addition & 1 deletion backends/p4test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MidEnd::MidEnd(CompilerOptions& options) {
new P4::RemoveAllUnusedDeclarations(&refMap),
new P4::ClearTypeMap(&typeMap),
evaluator,
new VisitFunctor([v1controls, evaluator](const IR::Node *root) -> const IR::Node * {
new VisitFunctor([v1controls, evaluator](const IR::Node *root) -> const IR::Node * {
auto toplevel = evaluator->getToplevelBlock();
auto main = toplevel->getMain();
if (main == nullptr)
Expand Down
10 changes: 10 additions & 0 deletions frontends/p4/unusedDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ const IR::Node* RemoveUnusedDeclarations::preorder(IR::Declaration_Instance* dec
if (!refMap->isUsed(getOriginal<IR::Declaration_Instance>())) {
if (giveWarning(getOriginal()))
::warning("%1%: unused instance", decl);
// We won't delete extern instances; these may be useful even if not references.
auto type = decl->type;
if (type->is<IR::Type_Specialized>())
type = type->to<IR::Type_Specialized>()->baseType;
if (type->is<IR::Type_Name>())
type = refMap->getDeclaration(type->to<IR::Type_Name>()->path, true)->to<IR::Type>();
if (!type->is<IR::Type_Extern>())
return process(decl);
prune();
return decl;
}
// don't scan the initializer: we don't want to delete virtual methods
prune();
Expand Down
12 changes: 6 additions & 6 deletions frontends/p4/unusedDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace P4 {

class RemoveUnusedDeclarations : public Transform {
const ReferenceMap* refMap;
// If non-null give warnings about unused declarations
/// If non-null give warnings about unused declarations
std::set<const IR::Node*>* warned;
const IR::Node* process(const IR::IDeclaration* decl);

Expand All @@ -38,8 +38,8 @@ class RemoveUnusedDeclarations : public Transform {
using Transform::preorder;
using Transform::init_apply;

// True if we should report a warning; the node is
// added to warned in this case
/// True if we should report a warning; the node is
/// added to warned in this case
bool giveWarning(const IR::Node* node);

Visitor::profile_t init_apply(const IR::Node *root) override;
Expand All @@ -52,7 +52,7 @@ class RemoveUnusedDeclarations : public Transform {

const IR::Node* preorder(IR::Declaration_Instance* decl) override;

// Do not delete the following even if unused
// The following kinds of nodes are not deleted even if they are unreferenced
const IR::Node* preorder(IR::Type_Error* type) override
{ prune(); return type; }
const IR::Node* preorder(IR::Declaration_MatchKind* decl) override
Expand All @@ -63,14 +63,14 @@ class RemoveUnusedDeclarations : public Transform {
{ prune(); return type; }
const IR::Node* preorder(IR::Type_Method* type) override
{ prune(); return type; }
const IR::Node* preorder(IR::Parameter* param) override { return param; } // never dead

const IR::Node* preorder(IR::Declaration_Variable* decl) override;
const IR::Node* preorder(IR::Parameter* param) override { return param; } // never dead
const IR::Node* preorder(IR::Declaration* decl) override { return process(decl); }
const IR::Node* preorder(IR::Type_Declaration* decl) override { return process(decl); }
};

// Iterates RemoveUnusedDeclarations until convergence.
/// Iterates RemoveUnusedDeclarations until convergence.
class RemoveAllUnusedDeclarations : public PassManager {
public:
explicit RemoveAllUnusedDeclarations(ReferenceMap* refMap, bool warn = false) {
Expand Down
Loading

0 comments on commit a9beb5b

Please sign in to comment.