Ark compiler 11 - code analysis

2021SC@SDUSC

A long time_ The IR part is finally over. I'm also relieved. It's time to change the content below.

As analyzed earlier, a series of preparations should be carried out before the phase of MeFuncPhase category is executed, and some of them are related to the CFG of MeFunction. The CFG implementation of MeFunction is mainly implemented through the MeCFG class. This paper will make a simple analysis on the implementation and use of MeCFG.

The definition and implementation of MeCFG is in the file src/maple_me/include/me_cfg.h and src/maple_me/src/me_cfg.cpp.

MeCFG has only two member variables:

private:
  MeFunction &func;
  bool hasDoWhile = false;

func is used to map MeCFG to a specific MeFunction. Hasdowile is used to indicate whether there is dowile. Judging from the currently published code, it belongs to redundant member variables and is not used.

The member functions of MeCFG mainly include:

void BuildMirCFG();
  void FixMirCFG();
  void ConvertPhis2IdentityAssigns(BB &meBB) const;
  void UnreachCodeAnalysis(bool updatePhi = false);
  void WontExitAnalysis();
  void Verify() const;
  void VerifyLabels() const;
  void Dump() const;
  void DumpToFile(const std::string &prefix, bool dumpInStrs = false) const;
  bool FindExprUse(const BaseNode &expr, StIdx stIdx) const;
  bool FindUse(const StmtNode &stmt, StIdx stIdx) const;
  bool FindDef(const StmtNode &stmt, StIdx stIdx) const;
  bool HasNoOccBetween(StmtNode &from, const StmtNode &to, StIdx stIdx) const;
  • BuildMirCFG builds the control flow diagram of MIR;
  • FixMirCFG is the control flow diagram for repairing MIR;
  • Convertphis2identityassignments converts the phi instruction into an explicit identification assignment;
  • UnreachCodeAnalysis does not reach code analysis. It mainly analyzes BB S that cannot be reached from the function entry, and then deletes them;
  • WontExitAnalysis analyzes the BB that does not reach the exit position of the function, adds a no exit flag to it, and creates a comment for it_ exit_ Path of BB;
  • Verify that the BB attribute and CFG attribute in MeFunction are normal;
  • VerifyLabels checks whether the label is correct;
  • Dump and DumpToFile both need to dump information, but in different ways;
  • FindExprUse finds whether the expression is used, specifically whether stIdx (symbol) is used in expr;
  • FindUse finds whether stIdx (symbol) is used in stmt;
  • FindDef finds whether stIdx (symbol) is defined in stmt;
  • HasNoOccBetween searches for stIdx (symbol) that is not used or defined in from to. If not, it returns true. This member function needs to call FindUse and FindDef.

MeCFG is used in code only in MeFunction. There are two parts to use, the first part:

src/maple_me/include/me_function.h

MeCFG *GetTheCfg() {
    return theCFG;
  }

  void SetTheCfg(MeCFG *currTheCfg) {
    theCFG = currTheCfg;
  }

Get or set the member variable theCFG of MeCFG * type of MeFunction. The member variable is defined as follows:

MeCFG *theCFG = nullptr;

The second part of MeCFG used in MeFunction is to build CFG in the process of preparing for the operation of MeFunctionPhase mentioned earlier:

src/maple_me/src/me_function.cpp

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

These steps are mainly implemented:

 theCFG = memPool->New<MeCFG>(*this);
  theCFG->BuildMirCFG();
  theCFG->FixMirCFG();
  theCFG->VerifyLabels();
  theCFG->UnreachCodeAnalysis();
  theCFG->WontExitAnalysis();
  theCFG->Verify();

The first statement is theCFG to create a new object and apply for space. The subsequent operations are introduced in 3, mainly creating a control flow diagram, and then various analysis and verification processes.

It is worth noting that this process of using CFG is the standard process of using CFG in the ark. Because of the current open source, the use of CFG is based on this official order. All subsequent source codes are open. You can hack whether the verification order here can be adjusted when debugging and analysis.

 

Keywords: harmonyos

Added by php1999 on Thu, 16 Dec 2021 06:53:16 +0200