Ark Compiler Analysis III - Code Analysis (First Part)

        2021SC@SDUSC

The old rule is to paste the official source code first:

https://code.opensource.huaweicloud.com/HarmonyOS/OpenArkCompiler/file?ref=master&path=doc%252FDevelopment_Preparation.md

Research and analyze the me_function part of the compiler code today

The definition of MeFunction is in src/maple_me/include/me_function.h:

Specific links in src/mapleall/maple_me/include/me_function.h. Ark Compiler/OpenArkCompiler - Gitee.comhttps://gitee.com/openarkcompiler/OpenArkCompiler/blob/master/src/mapleall/maple_me/include/me_function.h

class MeFunction : public FuncEmit {
  using BBPtrHolder = MapleVector<BB*>;

 public:
  MeFunction(MIRModule *mod, MIRFunction *func, MemPool *memPool, MemPool *versMemPool,
             const std::string &fileName)
      : memPool(memPool),
        alloc(memPool),
        versMemPool(versMemPool),
        versAlloc(versMemPool),
        mirModule(*mod),
        mirFunc(func),
        nextBBId(0),
        labelBBIdMap(alloc.Adapter()),
        bbVec(alloc.Adapter()),
        theCFG(nullptr),
        meSSATab(nullptr),
        irmap(nullptr),
        bbTryNodeMap(alloc.Adapter()),
        endTryBB2TryBB(alloc.Adapter()),
        fileName(fileName),
        regNum(0),
        hints(0),
        hasEH(false),
        secondPass(false) {}

  virtual ~MeFunction() {}
...

MeFunction inherits from the FuncEmit class, which is defined in src/maple_me/include/func_emit.h and implemented in src/maple_me/src/func_emit.cpp. Its definition is simpler, mainly providing code generation services for MeFunction and WpoFunction. According to this description, MeFunction and WpoFunction should both be subclasses. However, in this release, they are notFind something about WpoFunction, and perhaps wait until some other source code is open source to see what this class is doing. FuncEmit has only one public member function, EmitBeforeHSSA, which inserts the bb of MapleVector <bb*>into the func.

MeFunction inherits from FuncEmit's EmitBeforeHSSA member function and is used only once in the current open source code range, in src/maple_me/src/me_emit.cpp.

MeDoEmission is a phase inherited from MeFuncPhase, that is, it is a phase in the MeFuncPhase category, and its core function is the Run function.

In addition to Prepare() and EmitBeforeHSSA() mentioned above, MeFunction has a series of member functions for BasicBlock-related operations. There are three new member functions for BasicBlock:

BB *MeFunction::NewBasicBlock() {

// new a basic block and insert before position
BB *MeFunction::InsertNewBasicBlock(BB *position) {

/* Split BB at split_point */
BB *MeFunction::SplitBB(BB *bb, StmtNode *splitPoint) {

(1) The NewBasicBlock function is mainly called by void MeFunction::CreateBasicBlocks(). The CreateBasicBlocks() function is also called in the Prepare function:

void MeFunction::Prepare(unsigned long rangeNum) {
  if (!MeOptions::quiet) {
    LogInfo::MapleLogger() << "---Preparing Function  < " << mirModule.CurFunction()->GetName() << " > [" << rangeNum
                           << "] ---\n";
  }
  /* lower first */
  MIRLower mirLowerer(mirModule, mirModule.CurFunction());
  mirLowerer.Init();
  mirLowerer.SetLowerME();
  mirLowerer.SetLowerExpandArray();
  mirLowerer.LowerFunc(mirModule.CurFunction());
  CreateBasicBlocks();
  if (NumBBs() == 0) {
    /* there's no basicblock generated */
    return;
  }
  RemoveEhEdgesInSyncRegion();
  theCFG = memPool->New<MirCFG>(this);
  theCFG->BuildMirCFG();
  theCFG->FixMirCFG();
  theCFG->VerifyLabels();
  theCFG->UnreachCodeAnalysis();
  theCFG->WontExitAnalysis();
  theCFG->Verify();
}

We can see that it is called after the lower operation and before the CFG (Control Flow Graph) related action is executed.

2) The InsertNewBasicBlock() function is to insert a new BasicBlock before the location of a BasicBlock. This member function is not called in this open source.

3) The SplitBB() function, which splits a BasicBlock in a split point, is called primarily in the void MirCFG::FixMirCFG() function (src/maple_me/src/me_cfg.cpp).

Of the three new BasicBlock functions above, only NewBasicBlock() is called by MeFunction::CreateBasicBlocks(). There are also member functions related to BasicBlock:

 

void MeFunction::DeleteBasicBlock(const BB *bb) {
  ASSERT(bbVec[bb->GetBBId().idx] == bb, "runtime check error");
  /* update first_bb_ and last_bb if needed */
  bbVec.at(bb->GetBBId().idx) = nullptr;
}

/* clone stmtnode in orig bb to newBB */
void MeFunction::CloneBasicBlock(BB *newBB, BB *orig) {
  if (orig == nullptr || orig->IsEmpty()) {
    return;
  }
  for (auto &stmt : orig->GetStmtNodes()) {
    StmtNode *newStmt = static_cast<StmtNode*>(stmt.CloneTree(mirModule.CurFuncCodeMemPoolAllocator()));
    ASSERT(newStmt != nullptr, "null ptr check");
    newStmt->SetNext(nullptr);
    newStmt->SetPrev(nullptr);
    newBB->AddStmtNode(newStmt);
    if (meSSATab != nullptr) {
      meSSATab->CreateSSAStmt(newStmt, newBB);
    }
  }
}

There are also functions for BB acquisition:

/* get next bb in bbVec */
BB *MeFunction::NextBB(const BB *bb) {
  auto bbIt = std::find(begin(), end(), bb);
  CHECK_FATAL(bbIt != end(), "bb must be inside bb_vec");
  for (auto it = ++bbIt; it != end(); ++it) {
    if (*it != nullptr) {
      return *it;
    }
  }
  return nullptr;
}

/* get prev bb in bbVec */
BB *MeFunction::PrevBB(const BB *bb) {
  auto bbIt = std::find(rbegin(), rend(), bb);
  CHECK_FATAL(bbIt != rend(), "bb must be inside bb_vec");
  for (auto it = ++bbIt; it != rend(); ++it) {
    if (*it != nullptr) {
      return *it;
    }
  }
  return nullptr;
}

MeFunction:: The RemoveEhEdgesInSyncRegion function is designed to handle situations related to exception handling. No specific analysis is done here, and it will be analyzed in the exception handling section later.

Summary: MeFunction is an important class that typically occurs after MIRFunction over the life cycle. MeFunction is the carrier for transforming phase into the MeFuncPhase category, which must be built before phase can be executed and operated on lower, CFG, and so on.

Tags: html5 html css

Posted on Fri, 08 Oct 2021 12:46:19 -0400 by pauls74462