构建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";
}
}
}