关于ipa的介绍和基本使用,在之前的博客中:编译优化之 - 过程间优化(IPA/IPO)入门。这里主要是记录一下gcc-8.2.0版本中新建一个ipa pass的过程。
- 进入gcc-8.2.0/gcc目录,新建ipa-test.c文件。代码可参考
ipa-*.c
那些pass中的过程进行修改,在此参考ipa-hsa.c文件,其代码较少。拷贝头文件到ipa-test.c
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "is-a.h"
#include "hash-set.h"
#include "vec.h"
#include "tree.h"
#include "tree-pass.h"
#include "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "dumpfile.h"
#include "gimple-pretty-print.h"
#include "tree-streamer.h"
#include "stringpool.h"
#include "cgraph.h"
#include "print-tree.h"
#include "symbol-summary.h"
- 拷贝namespace中的const pass_data相关信息进行修改:
namespace {
const pass_data pass_data_ipa_test =
{
IPA_PASS, /* type */
"ipa-test", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_IPA_TEST, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_dump_symtab, /* todo_flags_finish */
};
...
}
- 拷贝class pass_ipa_hsa信息进行修改:
class pass_ipa_test : public ipa_opt_pass_d
{
public:
pass_ipa_test (gcc::context *ctxt)
: ipa_opt_pass_d (pass_data_ipa_test, ctxt,
NULL, /* generate_summary */
NULL, /* write_summary */
NULL, /* read_summary */
NULL, /* write_optimization_summary */
NULL, /* read_optimization_summary */
NULL, /* stmt_fixup */
0, /* function_transform_todo_flags_start */
NULL, /* function_transform */
NULL) /* variable_transform */
{}
/* opt_pass methods: */
virtual bool gate (function *);
virtual unsigned int execute (function *) { return process_test_functions (); }
}; // class pass_ipa_reference
bool
pass_ipa_test::gate (function *)
{
return hsa_gen_requested_p ();
}
} // anon namespace
- 拷贝make_pass_ipa进行修改并在namespace外添加process_test_functions函数:
static unsigned int
process_test_functions (void)
{
return 0;
}
...
ipa_opt_pass_d *
make_pass_ipa_test (gcc::context *ctxt)
{
return new pass_ipa_test (ctxt);
}
修改完成后代码如下:
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "is-a.h"
#include "hash-set.h"
#include "vec.h"
#include "tree.h"
#include "tree-pass.h"
#include "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "dumpfile.h"
#include "gimple-pretty-print.h"
#include "tree-streamer.h"
#include "stringpool.h"
#include "cgraph.h"
#include "print-tree.h"
#include "symbol-summary.h"
static unsigned int
process_test_functions(void)
{
return 0;
}
namespace {
const pass_data pass_data_ipa_test =
{
IPA_PASS, /* type */
"ipa-test", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_IPA_TEST, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_dump_symtab, /* todo_flags_finish */
;
class pass_ipa_test : public ipa_opt_pass_d
{
public:
pass_ipa_test (gcc::context *ctxt)
: ipa_opt_pass_d (pass_data_ipa_test, ctxt,
NULL, /* generate_summary */
NULL, /* write_summary */
NULL, /* read_summary */
NULL, /* write_optimization_summary */
NULL, /* read_optimization_summary */
NULL, /* stmt_fixup */
0, /* function_transform_todo_flags_start */
NULL, /* function_transform */
NULL) /* variable_transform */
{}
/* opt_pass methods: */
virtual bool gate (function *);
virtual unsigned int execute (function *) { return process_test_functions (); }
}; // class pass_ipa_referen
bool
pass_ipa_test::gate (function *)
{
return hsa_gen_requested_p ();
}
} // anon namespace
ipa_opt_pass_d *
make_pass_ipa_test (gcc::context *ctxt)
{
return new pass_ipa_test (ctxt);
}
- 在gcc/tree-pass.h中添加声明:
extern ipa_opt_pass_d *make_pass_ipa_test (gcc::context *ctxt);
- 在gcc/passes.def中新增:
NEXT_PASS (pass_ipa_test);
- 在gcc/timevar.def中新增:
DEFTIMEVAR (TV_IPA_TEST, "load ipa test pass")
- 在gcc/Makefile.in中新增ipa-test.c和ipa-test.o
- 进行
../configure、make、make install
即可