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;
}