remove struct code illustration(1)--构建vertex为StructType的DAG

构建StructDAG:getIRStructType();
IR中,将StructType存为DAG的vertex, StructType分解的subStructType同样存入DAG的vertex,并建立父子关系,遍历 IR 所有的Struct Type 构建出DAG 。

void RemoveStruct::getIRStructType() {
  //why is Identified StructType???
  //All IR StructTypes in module_->getIdentifiedStructTypes()
  //create the structDAG by IR StructTypes
  std::vector<StructType *> struct_type =  module_->getIdentifiedStructTypes();
  std::vector<StructType *>::iterator it;
  for (it = struct_type.begin(); it != struct_type.end(); it++) {
    if (StructDAG_->DAGMapNotFind(*it)) {
      DAGAddVertex(*it);
    } else {
      //outs()<<*(*it)<<" struct type exists!"<<"\n";
    }
  }
  StructDAG_->printStructType();
//StringRef temp_name = temp->getName();
//  TopologicalSort();
//  outs()<<"\n";
}

// create new vertex by new StructType, and resolve the StructType
void RemoveStruct::DAGAddVertex(StructType *st) {
  StructDAG::Vertex *ver = StructDAG_->CreateVertex(st);
  StructDAG_->addVertex(ver);
  resloveStructType(ver);
}

//resloveStructType and create the relations between others
void RemoveStruct::resloveStructType(StructDAG::Vertex *ver) {
  StructType *st = ver->struct_Type_;
  //outs()<<st->getName()<<"  "<<st<<"\n";
  int struct_type_num = st->getNumElements();
  for (int i = 0; i < struct_type_num; i++) {
    if (st->getStructElementType(i)->isStructTy()) {
      StructType *sub_st = dyn_cast<StructType>(st->getStructElementType(i));
      // if current type isn't exist in map, create new vertex
      if (StructDAG_->DAGMapNotFind(sub_st)){
        StructDAG::Vertex *sub_ver = StructDAG_->CreateVertex(sub_st);
        sub_ver->vertex_parent_.insert(ver);
        ver->vertex_children_.insert(sub_ver);
        StructDAG_->addVertex(sub_ver);
        resloveStructType(sub_ver);
      } else {
        // if current type is exist in map, link to other vertex
        StructDAG::Vertex *exist_ver = StructDAG_->DAGMapFindVertex(sub_st);
        exist_ver->vertex_parent_.insert(ver);
        ver->vertex_children_.insert(exist_ver);
        resloveStructType(exist_ver);
      }
    } else if (st->getStructElementType(i)->isArrayTy() &&
        IsArrayStructVal(st->getStructElementType(i))) {
      Type *temp = st->getStructElementType(i);
      while (temp->isArrayTy()) {
        temp = temp->getArrayElementType();
      }
      assert(temp->isStructTy());
      StructType *sub_st = llvm::dyn_cast<StructType>(temp);
      if (StructDAG_->DAGMapNotFind(sub_st)){
        StructDAG::Vertex *sub_ver = StructDAG_->CreateVertex(sub_st);
        sub_ver->vertex_parent_.insert(ver);
        ver->vertex_children_.insert(sub_ver);
        StructDAG_->addVertex(sub_ver);
        resloveStructType(sub_ver);
      } else {
        StructDAG::Vertex *exist_ver = StructDAG_->DAGMapFindVertex(sub_st);
        exist_ver->vertex_parent_.insert(ver);
        ver->vertex_children_.insert(exist_ver);
        resloveStructType(exist_ver);
      }
    } 
    // st->getStructElementType(i) dose not have structure, ignore it 
    else {
      //outs()<<"\n"<<"sub type is: " << *st->getStructElementType(i)<<"\n";
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值