Skip to content

Commit f082782

Browse files
[NFC][Fuzzer] Extract CreateGateBranch method. (llvm#117236)
A Pre-commit for use in adding gated tracing callbacks support to trace-cmp [llvm#113227](llvm@53b316d) rdar://135404160 Patch by: Andrea Fioraldi
1 parent 0e3c791 commit f082782

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ class ModuleSanitizerCoverage {
250250
const char *Section);
251251
GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
252252
void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
253+
Instruction *CreateGateBranch(Function &F, Value *&FunctionGateCmp,
254+
Instruction *I);
253255
Value *CreateFunctionLocalGateCmp(IRBuilder<> &IRB);
254256
void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
255257
Value *&FunctionGateCmp, bool IsLeafFunc = true);
@@ -815,6 +817,24 @@ Value *ModuleSanitizerCoverage::CreateFunctionLocalGateCmp(IRBuilder<> &IRB) {
815817
return Cmp;
816818
}
817819

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+
818838
bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
819839
ArrayRef<BasicBlock *> AllBlocks,
820840
bool IsLeafFunc) {
@@ -1012,19 +1032,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
10121032
ConstantInt::get(IntptrTy, Idx * 4)),
10131033
PtrTy);
10141034
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();
10281039
} else {
10291040
IRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
10301041
}

0 commit comments

Comments
 (0)