Skip to content

Commit 2761011

Browse files
committed
[NFC] [hwasan] factor out some opt handling
Pull Request: llvm#84414
1 parent b408241 commit 2761011

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ static cl::opt<bool> ClUsePageAliases("hwasan-experimental-use-page-aliases",
260260

261261
namespace {
262262

263+
template <typename T> T optOr(cl::opt<T> &Opt, T Other) {
264+
return Opt.getNumOccurrences() ? Opt : Other;
265+
}
266+
263267
bool shouldUsePageAliases(const Triple &TargetTriple) {
264268
return ClUsePageAliases && TargetTriple.getArch() == Triple::x86_64;
265269
}
@@ -269,14 +273,11 @@ bool shouldInstrumentStack(const Triple &TargetTriple) {
269273
}
270274

271275
bool shouldInstrumentWithCalls(const Triple &TargetTriple) {
272-
return ClInstrumentWithCalls.getNumOccurrences()
273-
? ClInstrumentWithCalls
274-
: TargetTriple.getArch() == Triple::x86_64;
276+
return optOr(ClInstrumentWithCalls, TargetTriple.getArch() == Triple::x86_64);
275277
}
276278

277279
bool mightUseStackSafetyAnalysis(bool DisableOptimization) {
278-
return ClUseStackSafety.getNumOccurrences() ? ClUseStackSafety
279-
: !DisableOptimization;
280+
return optOr(ClUseStackSafety, !DisableOptimization);
280281
}
281282

282283
bool shouldUseStackSafetyAnalysis(const Triple &TargetTriple,
@@ -296,10 +297,8 @@ class HWAddressSanitizer {
296297
HWAddressSanitizer(Module &M, bool CompileKernel, bool Recover,
297298
const StackSafetyGlobalInfo *SSI)
298299
: M(M), SSI(SSI) {
299-
this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover;
300-
this->CompileKernel = ClEnableKhwasan.getNumOccurrences() > 0
301-
? ClEnableKhwasan
302-
: CompileKernel;
300+
this->Recover = optOr(ClRecover, Recover);
301+
this->CompileKernel = optOr(ClEnableKhwasan, CompileKernel);
303302
this->Rng =
304303
RandomSkipRate.getNumOccurrences() ? M.createRNG("hwasan") : nullptr;
305304

@@ -624,19 +623,14 @@ void HWAddressSanitizer::initializeModule() {
624623
bool NewRuntime =
625624
!TargetTriple.isAndroid() || !TargetTriple.isAndroidVersionLT(30);
626625

627-
UseShortGranules =
628-
ClUseShortGranules.getNumOccurrences() ? ClUseShortGranules : NewRuntime;
629-
OutlinedChecks =
630-
(TargetTriple.isAArch64() || TargetTriple.isRISCV64()) &&
631-
TargetTriple.isOSBinFormatELF() &&
632-
(ClInlineAllChecks.getNumOccurrences() ? !ClInlineAllChecks : !Recover);
626+
UseShortGranules = optOr(ClUseShortGranules, NewRuntime);
627+
OutlinedChecks = (TargetTriple.isAArch64() || TargetTriple.isRISCV64()) &&
628+
TargetTriple.isOSBinFormatELF() &&
629+
!optOr(ClInlineAllChecks, Recover);
633630

634-
InlineFastPath =
635-
(ClInlineFastPathChecks.getNumOccurrences()
636-
? ClInlineFastPathChecks
637-
: !(TargetTriple.isAndroid() ||
638-
TargetTriple.isOSFuchsia())); // These platforms may prefer less
639-
// inlining to reduce binary size.
631+
// These platforms may prefer less inlining to reduce binary size.
632+
InlineFastPath = optOr(ClInlineFastPathChecks, !(TargetTriple.isAndroid() ||
633+
TargetTriple.isOSFuchsia()));
640634

641635
if (ClMatchAllTag.getNumOccurrences()) {
642636
if (ClMatchAllTag != -1) {
@@ -648,22 +642,17 @@ void HWAddressSanitizer::initializeModule() {
648642
UseMatchAllCallback = !CompileKernel && MatchAllTag.has_value();
649643

650644
// If we don't have personality function support, fall back to landing pads.
651-
InstrumentLandingPads = ClInstrumentLandingPads.getNumOccurrences()
652-
? ClInstrumentLandingPads
653-
: !NewRuntime;
645+
InstrumentLandingPads = optOr(ClInstrumentLandingPads, !NewRuntime);
654646

655647
if (!CompileKernel) {
656648
createHwasanCtorComdat();
657-
bool InstrumentGlobals =
658-
ClGlobals.getNumOccurrences() ? ClGlobals : NewRuntime;
649+
bool InstrumentGlobals = optOr(ClGlobals, NewRuntime);
659650

660651
if (InstrumentGlobals && !UsePageAliases)
661652
instrumentGlobals();
662653

663654
bool InstrumentPersonalityFunctions =
664-
ClInstrumentPersonalityFunctions.getNumOccurrences()
665-
? ClInstrumentPersonalityFunctions
666-
: NewRuntime;
655+
optOr(ClInstrumentPersonalityFunctions, NewRuntime);
667656
if (InstrumentPersonalityFunctions)
668657
instrumentPersonalityFunctions();
669658
}

0 commit comments

Comments
 (0)