[原创]OLLVM控制流平坦化的改进
2022-10-19 10:9:0 Author: bbs.pediy.com(查看原文) 阅读量:7 收藏

插入标记数组和key数组,计算出每个基本块的key值数组(key_map),为每个基本块分配一个随机值用于更新其他节点的key,并在基本块后添加更新标记数组和key数组的代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

IRBuilder<> irb(&*oldEntry->getFirstInsertionPt());                                // generate context info key for each block

Value *visitedArray=irb.CreateAlloca(irb.getInt8Ty(),irb.getInt32(origBB.size()));

Value *keyArray=irb.CreateAlloca(irb.getInt32Ty(),irb.getInt32(origBB.size()));

irb.CreateMemSet(visitedArray,irb.getInt8(0),origBB.size(),(MaybeAlign)0);

irb.CreateMemSet(keyArray,irb.getInt8(0),origBB.size()*4,(MaybeAlign)0);

int idx=0;

std::vector<unsigned int> key_list;

DominatorTree tree(*f);

std::map<BasicBlock*,unsigned int> key_map;

std::map<BasicBlock*,unsigned int> index_map;

for(std::vector<BasicBlock *>::iterator b=origBB.begin();b!=origBB.end();b++)

{

  BasicBlock *block=*b;

  unsigned int num=getUniqueNumber(&key_list);

  key_list.push_back(num);

  key_map[block]=0;

}

for(std::vector<BasicBlock *>::iterator b=origBB.begin();b!=origBB.end();b++,idx++)

{

  BasicBlock *block=*b;

  std::vector<Constant*> doms;

  int i=0;

  for(std::vector<BasicBlock *>::iterator bb=origBB.begin();bb!=origBB.end();bb++,i++)

  {

      BasicBlock *block0=*bb;

      if(block0!=block && tree.dominates(block,block0))

      {

          doms.push_back(irb.getInt32(i));

          key_map[block0]^=key_list[idx];

      }

  }

  irb.SetInsertPoint(block->getTerminator());

  Value *ptr=irb.CreateGEP(irb.getInt8Ty(),visitedArray,irb.getInt32(idx));

  Value *visited=irb.CreateLoad(ptr);

  if(doms.size()!=0)

  {

      ArrayType *arrayType=ArrayType::get(irb.getInt32Ty(),doms.size());

      Constant *doms_array=ConstantArray::get(arrayType,ArrayRef<Constant*>(doms));

      GlobalVariable *dom_variable=new GlobalVariable(*(f->getParent()),arrayType,false,GlobalValue::LinkageTypes::PrivateLinkage,doms_array,"doms");

      irb.CreateCall(FunctionCallee(updateFunc),{visited,irb.getInt32(doms.size()),irb.CreateGEP(dom_variable,{irb.getInt32(0),irb.getInt32(0)}),keyArray,irb.getInt32(key_list[idx])});

  }

  irb.CreateStore(irb.getInt8(1),ptr);

  index_map[block]=idx;

}


文章来源: https://bbs.pediy.com/thread-274778.htm
如有侵权请联系:admin#unsafe.sh