Skip to content

Commit

Permalink
Back out "support editable cfg in referene-update"
Browse files Browse the repository at this point in the history
Summary:
Original commit changeset: 84589ae9a593

Original Phabricator Diff: D46542429

Reviewed By: NTillmann

Differential Revision: D46943406

fbshipit-source-id: 98bd06d36c1454df3675a11f340c3f7de1736d2d
  • Loading branch information
beicy authored and facebook-github-bot committed Jun 23, 2023
1 parent e8a98f4 commit 2247e77
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion service/class-merging/ModelMethodMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void ModelMethodMerger::fix_visibility() {
auto call_sites =
method_reference::collect_call_refs(m_scope, vmethods_created);
for (const auto& callsite : call_sites) {
auto insn = callsite.insn;
auto insn = callsite.mie->insn;
always_assert(opcode::is_invoke_direct(insn->opcode()));
insn->set_opcode(OPCODE_INVOKE_VIRTUAL);
}
Expand Down
2 changes: 1 addition & 1 deletion service/method-dedup/ConstantLifting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ std::vector<DexMethod*> ConstantLifting::lift_constants_from(
auto call_sites = method_reference::collect_call_refs(scope, lifted);
for (const auto& callsite : call_sites) {
auto meth = callsite.caller;
auto insn = callsite.insn;
auto insn = callsite.mie->insn;
const auto callee =
resolve_method(insn->get_method(), opcode_to_search(insn));
always_assert(callee != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion service/method-dedup/NormalizeConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ uint32_t dedup_constructors(const std::vector<DexClass*>& classes,
redex_assert(new_callee != old_callee);
auto new_field_id_to_arg_id =
methods_summaries[new_callee]->field_id_to_origin;
auto insn = callsite.insn;
auto insn = callsite.mie->insn;
insn->set_method(new_callee);
reorder_callsite_args(old_field_id_to_arg_id, new_field_id_to_arg_id, insn);
}
Expand Down
23 changes: 9 additions & 14 deletions service/reference-update/MethodReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

#include "MethodReference.h"

#include "ControlFlow.h"
#include "IRList.h"
#include "Resolver.h"
#include "ScopedCFG.h"
#include "Show.h"
#include "Trace.h"
#include "Walkers.h"
Expand Down Expand Up @@ -67,20 +65,19 @@ void patch_callsite(const CallSite& callsite, const NewCallee& new_callee) {
SHOW(new_callee.method), SHOW(callsite.caller));

auto code = callsite.caller->get_code();
cfg::ScopedCFG cfg(code);
auto insn = callsite.insn;
auto iterator = cfg->find_insn(insn);
auto iterator = code->iterator_to(*callsite.mie);
auto insn = callsite.mie->insn;
if (new_callee.additional_args != boost::none) {
const auto& args = new_callee.additional_args.get();
auto old_size = insn->srcs_size();
insn->set_srcs_size(old_size + args.size());
size_t pos = old_size;
for (uint32_t arg : args) {
auto reg = cfg->allocate_temp();
auto reg = code->allocate_temp();
// Seems it is different from dasm(OPCODE_CONST, {{VREG, reg}, {LITERAL,
// arg}}) which will cause instruction_lowering crash. Why?
auto load_const = make_load_const(reg, arg);
cfg->insert_before(iterator, load_const);
code->insert_before(iterator, load_const);
insn->set_src(pos++, reg);
}
}
Expand All @@ -96,8 +93,7 @@ void update_call_refs_simple(
}

auto patcher = [&](DexMethod* meth, IRCode& code) {
cfg::ScopedCFG cfg(&code);
for (auto& mie : cfg::InstructionIterable(*cfg)) {
for (auto& mie : InstructionIterable(code)) {
auto insn = mie.insn;
if (!insn->has_method()) {
continue;
Expand Down Expand Up @@ -140,8 +136,8 @@ CallSites collect_call_refs(const Scope& scope, const T& callees) {
if (!code) {
return call_sites;
}
cfg::ScopedCFG cfg(code);
for (auto& mie : cfg::InstructionIterable(*cfg)) {

for (auto& mie : InstructionIterable(caller->get_code())) {
auto insn = mie.insn;
if (!insn->has_method()) {
continue;
Expand All @@ -154,7 +150,7 @@ CallSites collect_call_refs(const Scope& scope, const T& callees) {
continue;
}

call_sites.emplace_back(caller, insn, callee);
call_sites.emplace_back(caller, &mie, callee);
TRACE(REFU, 9, " Found call %s from %s", SHOW(insn), SHOW(caller));
}

Expand Down Expand Up @@ -203,8 +199,7 @@ int wrap_instance_call_with_static(
}
auto code = method->get_code();
if (code) {
cfg::ScopedCFG cfg(code);
for (auto& mie : cfg::InstructionIterable(*cfg)) {
for (auto& mie : InstructionIterable(code)) {
IRInstruction* insn = mie.insn;
if (insn->opcode() != OPCODE_INVOKE_VIRTUAL) {
continue;
Expand Down
8 changes: 5 additions & 3 deletions service/reference-update/MethodReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
#include "DexStore.h"
#include "IRInstruction.h"

struct MethodItemEntry;

using OrderedMethodSet = std::set<DexMethod*, dexmethods_comparator>;

namespace method_reference {

// A callsite instruction in caller. mie should always contain an IRInstruction.
struct CallSite {
DexMethod* caller;
IRInstruction* insn;
MethodItemEntry* mie;
DexMethod* callee;
CallSite(DexMethod* caller, IRInstruction* insn, DexMethod* callee)
: caller(caller), insn(insn), callee(callee) {}
CallSite(DexMethod* caller, MethodItemEntry* mie, DexMethod* callee)
: caller(caller), mie(mie), callee(callee) {}
};

using CallSites = std::vector<CallSite>;
Expand Down
29 changes: 7 additions & 22 deletions service/reference-update/TypeReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

#include "TypeReference.h"

#include "ControlFlow.h"
#include "MethodReference.h"
#include "Resolver.h"
#include "ScopedCFG.h"
#include "Show.h"
#include "Trace.h"
#include "Walkers.h"
Expand Down Expand Up @@ -499,8 +497,7 @@ void update_method_signature_type_references(

// Ensure that no method references left that still refer old types.
walk::parallel::code(scope, [&old_types](DexMethod*, IRCode& code) {
cfg::ScopedCFG cfg(&code);
for (auto& mie : InstructionIterable(*cfg)) {
for (auto& mie : InstructionIterable(code)) {
auto insn = mie.insn;
if (insn->has_method()) {
auto proto = insn->get_method()->get_proto();
Expand Down Expand Up @@ -535,8 +532,7 @@ void update_field_type_references(
walk::parallel::fields(scope, update_field);

walk::parallel::code(scope, [&old_to_new](DexMethod*, IRCode& code) {
cfg::ScopedCFG cfg(&code);
for (auto& mie : InstructionIterable(*cfg)) {
for (auto& mie : InstructionIterable(code)) {
auto insn = mie.insn;
if (insn->has_field()) {
const auto ref_type = insn->get_field()->get_type();
Expand Down Expand Up @@ -579,22 +575,12 @@ void fix_colliding_dmethods(
num_additional_args[meth] = arg_count;

auto code = meth->get_code();
cfg::ScopedCFG cfg(code);
auto block = cfg->entry_block();
auto last_loading = block->get_last_param_loading_insn();
for (size_t i = 0; i < arg_count; ++i) {
auto new_param_reg = cfg->allocate_temp();
auto new_param_reg = code->allocate_temp();
auto params = code->get_param_instructions();
auto new_param_load = new IRInstruction(IOPCODE_LOAD_PARAM);
new_param_load->set_dest(new_param_reg);
if (last_loading != block->end()) {
cfg->insert_after(block->to_cfg_instruction_iterator(last_loading),
new_param_load);
} else {
cfg->insert_before(block->to_cfg_instruction_iterator(
block->get_first_non_param_loading_insn()),
new_param_load);
}
last_loading = block->get_last_param_loading_insn();
code->insert_before(params.end(), new_param_load);
}
TRACE(REFU,
9,
Expand All @@ -605,8 +591,7 @@ void fix_colliding_dmethods(

walk::parallel::code(scope, [&](DexMethod* meth, IRCode& code) {
method_reference::CallSites callsites;
cfg::ScopedCFG cfg(&code);
for (auto& mie : InstructionIterable(*cfg)) {
for (auto& mie : InstructionIterable(code)) {
auto insn = mie.insn;
if (!insn->has_method()) {
continue;
Expand All @@ -618,7 +603,7 @@ void fix_colliding_dmethods(
num_additional_args.find(callee) == num_additional_args.end()) {
continue;
}
callsites.emplace_back(meth, insn, callee);
callsites.emplace_back(meth, &mie, callee);
}

for (const auto& callsite : callsites) {
Expand Down

0 comments on commit 2247e77

Please sign in to comment.