@@ -250,6 +250,8 @@ class ModuleSanitizerCoverage {
250
250
const char *Section);
251
251
GlobalVariable *CreatePCArray (Function &F, ArrayRef<BasicBlock *> AllBlocks);
252
252
void CreateFunctionLocalArrays (Function &F, ArrayRef<BasicBlock *> AllBlocks);
253
+ Instruction *CreateGateBranch (Function &F, Value *&FunctionGateCmp,
254
+ Instruction *I);
253
255
Value *CreateFunctionLocalGateCmp (IRBuilder<> &IRB);
254
256
void InjectCoverageAtBlock (Function &F, BasicBlock &BB, size_t Idx,
255
257
Value *&FunctionGateCmp, bool IsLeafFunc = true );
@@ -815,6 +817,24 @@ Value *ModuleSanitizerCoverage::CreateFunctionLocalGateCmp(IRBuilder<> &IRB) {
815
817
return Cmp;
816
818
}
817
819
820
+ Instruction *ModuleSanitizerCoverage::CreateGateBranch (Function &F,
821
+ Value *&FunctionGateCmp,
822
+ Instruction *IP) {
823
+ if (!FunctionGateCmp) {
824
+ // Create this in the entry block
825
+ BasicBlock &BB = F.getEntryBlock ();
826
+ BasicBlock::iterator IP = BB.getFirstInsertionPt ();
827
+ IP = PrepareToSplitEntryBlock (BB, IP);
828
+ IRBuilder<> EntryIRB (&*IP);
829
+ FunctionGateCmp = CreateFunctionLocalGateCmp (EntryIRB);
830
+ }
831
+ // Set the branch weights in order to minimize the price paid when the
832
+ // gate is turned off, allowing the default enablement of this
833
+ // instrumentation with as little of a performance cost as possible
834
+ auto Weights = MDBuilder (*C).createBranchWeights (1 , 100000 );
835
+ return SplitBlockAndInsertIfThen (FunctionGateCmp, IP, false , Weights);
836
+ }
837
+
818
838
bool ModuleSanitizerCoverage::InjectCoverage (Function &F,
819
839
ArrayRef<BasicBlock *> AllBlocks,
820
840
bool IsLeafFunc) {
@@ -1012,19 +1032,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
1012
1032
ConstantInt::get (IntptrTy, Idx * 4 )),
1013
1033
PtrTy);
1014
1034
if (Options.GatedCallbacks ) {
1015
- if (!FunctionGateCmp) {
1016
- // Create this in the entry block
1017
- assert (IsEntryBB);
1018
- FunctionGateCmp = CreateFunctionLocalGateCmp (IRB);
1019
- }
1020
- // Set the branch weights in order to minimize the price paid when the
1021
- // gate is turned off, allowing the default enablement of this
1022
- // instrumentation with as little of a performance cost as possible
1023
- auto Weights = MDBuilder (*C).createBranchWeights (1 , 100000 );
1024
- auto ThenTerm =
1025
- SplitBlockAndInsertIfThen (FunctionGateCmp, &*IP, false , Weights);
1026
- IRBuilder<> ThenIRB (ThenTerm);
1027
- ThenIRB.CreateCall (SanCovTracePCGuard, GuardPtr)->setCannotMerge ();
1035
+ Instruction *I = &*IP;
1036
+ auto GateBranch = CreateGateBranch (F, FunctionGateCmp, I);
1037
+ IRBuilder<> GateIRB (GateBranch);
1038
+ GateIRB.CreateCall (SanCovTracePCGuard, GuardPtr)->setCannotMerge ();
1028
1039
} else {
1029
1040
IRB.CreateCall (SanCovTracePCGuard, GuardPtr)->setCannotMerge ();
1030
1041
}
0 commit comments