Skip to content

Commit

Permalink
Commit for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
yomaytk committed Oct 2, 2024
1 parent f77f741 commit f7ce9ae
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 53 deletions.
2 changes: 1 addition & 1 deletion backend/remill/include/remill/BC/HelperMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
// #define OPT_ALGO_DEBUG 1
// #define OPT_GEN_IR_DEBUG 1
// #define OPT_CALL_FUNC_DEBUG 1
// #define OPT_REAL_REGS_DEBUG 1
// #define OPT_REAL_REGS_DEBUG 1
3 changes: 2 additions & 1 deletion backend/remill/include/remill/BC/InstructionLifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class EcvReg {
static std::pair<EcvReg, EcvRegClass> GetRegInfo(const std::string &_reg_name);

std::string GetRegName(EcvRegClass ecv_reg_class) const;
bool CheckNoChangedReg() const;
bool CheckPassedArgsRegs() const;
bool CheckPassedReturnRegs() const;

Expand Down Expand Up @@ -197,6 +196,8 @@ class BBRegInfoNode {
// Save the args registers by semantic functions (for debug)
std::unordered_map<llvm::CallInst *, std::vector<std::pair<EcvReg, EcvRegClass>>>
sema_func_args_reg_map;
// Save the pc of semantics functions (for debug)
std::unordered_map<llvm::CallInst *, uint64_t> sema_func_pc_map;

std::unordered_map<llvm::Value *, std::pair<EcvReg, EcvRegClass>> post_update_regs;

Expand Down
9 changes: 7 additions & 2 deletions backend/remill/include/remill/BC/TraceLifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class PhiRegsBBBagNode {
PhiRegsBBBagNode() {}
static void Reset() {
bb_regs_bag_map.clear();
bag_passed_caller_reg_map.clear();
bag_num = 0;
debug_bag_map.clear();
}
Expand All @@ -136,7 +135,6 @@ class PhiRegsBBBagNode {
static inline std::size_t bag_num = 0;
static inline std::unordered_map<PhiRegsBBBagNode *, uint32_t> debug_bag_map = {};
// The register set which should be passed from caller function.
static inline EcvRegMap<EcvRegClass> bag_passed_caller_reg_map;

PhiRegsBBBagNode *GetTrueBag();
void MergePrecedingRegMap(PhiRegsBBBagNode *moved_bag);
Expand Down Expand Up @@ -250,9 +248,13 @@ class VirtualRegsOpt {
&cache_map);

void AnalyzeRegsBags();
static void CalPassedCallerRegForBJump();

void OptimizeVirtualRegsUsage();

static inline std::unordered_map<llvm::Function *, VirtualRegsOpt *> func_v_r_opt_map = {};
static inline std::unordered_map<llvm::Function *, std::vector<llvm::Function *>>
b_jump_callees_map = {};

llvm::Function *func;
TraceLifter::Impl *impl;
Expand All @@ -273,6 +275,9 @@ class VirtualRegsOpt {

std::unordered_map<llvm::BasicBlock *, PhiRegsBBBagNode *> bb_regs_bag_map;
EcvRegMap<EcvRegClass> passed_caller_reg_map;
EcvRegMap<EcvRegClass> passed_callee_ret_reg_map;

std::set<llvm::ReturnInst *> ret_inst_set;

// for debug
uint64_t fun_vma;
Expand Down
17 changes: 10 additions & 7 deletions backend/remill/lib/BC/InstructionLifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,12 @@ std::string EcvReg::GetRegName(EcvRegClass ecv_reg_class) const {
return "";
}

bool EcvReg::CheckNoChangedReg() const {
return STATE_ORDER == number || RUNTIME_ORDER == number || IGNORE_WRITE_TO_WZR_ORDER == number ||
IGNORE_WRITE_TO_XZR_ORDER == number || BRANCH_TAKEN_ORDER == number;
}

bool EcvReg::CheckPassedArgsRegs() const {
return (0 <= number && number <= 7) || SP_ORDER == number;
}

bool EcvReg::CheckPassedReturnRegs() const {
return (0 <= number && number <= 7) || SP_ORDER == number;
return (0 <= number && number <= 1) || SP_ORDER == number;
}

std::string EcvRegClass2String(EcvRegClass ecv_reg_class) {
Expand Down Expand Up @@ -242,7 +237,9 @@ BBRegInfoNode::BBRegInfoNode(llvm::Function *func, llvm::Value *state_val,
void BBRegInfoNode::join_reg_info_node(BBRegInfoNode *child) {
// Join bb_load_reg_map
for (auto [_ecv_reg, _ecv_reg_class] : child->bb_load_reg_map) {
bb_load_reg_map.insert({_ecv_reg, _ecv_reg_class});
if (!bb_store_reg_map.contains(_ecv_reg)) {
bb_load_reg_map.insert({_ecv_reg, _ecv_reg_class});
}
}
// Join bb_store_reg_map
for (auto [_ecv_reg, _ecv_reg_class] : child->bb_store_reg_map) {
Expand All @@ -266,6 +263,10 @@ void BBRegInfoNode::join_reg_info_node(BBRegInfoNode *child) {
for (auto key_value : child->sema_func_args_reg_map) {
sema_func_args_reg_map.insert(key_value);
}
// Join sema_func_pc_map
for (auto key_value : child->sema_func_pc_map) {
sema_func_pc_map.insert(key_value);
}
}

InstructionLifter::Impl::Impl(const Arch *arch_, const IntrinsicTable *intrinsics_)
Expand Down Expand Up @@ -571,6 +572,8 @@ LiftStatus InstructionLifter::LiftIntoBlock(Instruction &arch_inst, llvm::BasicB
<< "Unexpected to multiple lift the call instruction.";
bb_reg_info_node->sema_call_written_reg_map.insert({sema_inst, write_regs});

bb_reg_info_node->sema_func_pc_map.insert({sema_inst, arch_inst.pc});

// Update pre-post index for the target register.
if (!arch_inst.updated_addr_reg.name.empty()) {
const auto [update_reg_ptr_reg, _] =
Expand Down
Loading

0 comments on commit f7ce9ae

Please sign in to comment.