Skip to content

Commit 5ad7a38

Browse files
committed
!fixup address comments, thanks
1 parent 6697934 commit 5ad7a38

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ class MemoryLocation;
2929

3030
/// A simple AA result that uses TBAA metadata to answer queries.
3131
class TypeBasedAAResult : public AAResultBase {
32+
/// True if type sanitizer is enabled. When TypeSanitizer is used, don't use
33+
/// TBAA information for alias analysis as this might cause us to remove
34+
/// memory accesses that we need to verify at runtime.
35+
bool UsingTypeSanitizer;
36+
3237
public:
38+
TypeBasedAAResult(bool UsingTypeSanitizer)
39+
: UsingTypeSanitizer(UsingTypeSanitizer) {}
40+
3341
/// Handle invalidation events from the new pass manager.
3442
///
3543
/// By definition, this result is stateless and so remains valid.

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,26 +372,10 @@ static bool isStructPathTBAA(const MDNode *MD) {
372372
return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
373373
}
374374

375-
// When using the TypeSanitizer, don't use TBAA information for alias analysis.
376-
// This might cause us to remove memory accesses that we need to verify at
377-
// runtime.
378-
static bool usingSanitizeType(const Value *V) {
379-
const Function *F;
380-
381-
if (auto *I = dyn_cast<Instruction>(V))
382-
F = I->getParent()->getParent();
383-
else if (auto *A = dyn_cast<Argument>(V))
384-
F = A->getParent();
385-
else
386-
return false;
387-
388-
return F->hasFnAttribute(Attribute::SanitizeType);
389-
}
390-
391375
AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
392376
const MemoryLocation &LocB,
393377
AAQueryInfo &AAQI, const Instruction *) {
394-
if (!EnableTBAA || usingSanitizeType(LocA.Ptr) || usingSanitizeType(LocB.Ptr))
378+
if (!EnableTBAA || UsingTypeSanitizer || UsingTypeSanitizer)
395379
return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
396380

397381
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
@@ -442,7 +426,7 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
442426
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
443427
const MemoryLocation &Loc,
444428
AAQueryInfo &AAQI) {
445-
if (!EnableTBAA || usingSanitizeType(Call))
429+
if (!EnableTBAA || UsingTypeSanitizer)
446430
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
447431

448432
if (const MDNode *L = Loc.AATags.TBAA)
@@ -456,7 +440,7 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
456440
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
457441
const CallBase *Call2,
458442
AAQueryInfo &AAQI) {
459-
if (!EnableTBAA || usingSanitizeType(Call1))
443+
if (!EnableTBAA || UsingTypeSanitizer)
460444
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
461445

462446
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
@@ -724,7 +708,7 @@ bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
724708
AnalysisKey TypeBasedAA::Key;
725709

726710
TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) {
727-
return TypeBasedAAResult();
711+
return TypeBasedAAResult(F.hasFnAttribute(Attribute::SanitizeType));
728712
}
729713

730714
char TypeBasedAAWrapperPass::ID = 0;
@@ -740,7 +724,7 @@ TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) {
740724
}
741725

742726
bool TypeBasedAAWrapperPass::doInitialization(Module &M) {
743-
Result.reset(new TypeBasedAAResult());
727+
Result.reset(new TypeBasedAAResult(false));
744728
return false;
745729
}
746730

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct TypeSanitizer {
110110
TypeNameMapTy &TypeNames, Module &M);
111111

112112
const Triple TargetTriple;
113+
Regex AnonNameRegex;
113114
Type *IntptrTy;
114115
uint64_t PtrShift;
115116
IntegerType *OrdTy;
@@ -122,7 +123,8 @@ struct TypeSanitizer {
122123
} // namespace
123124

124125
TypeSanitizer::TypeSanitizer(Module &M)
125-
: TargetTriple(Triple(M.getTargetTriple())) {
126+
: TargetTriple(Triple(M.getTargetTriple())),
127+
AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") {
126128
const DataLayout &DL = M.getDataLayout();
127129
IntptrTy = DL.getIntPtrType(M.getContext());
128130
PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8);
@@ -237,16 +239,6 @@ static std::string encodeName(StringRef Name) {
237239
return Output;
238240
}
239241

240-
static bool isAnonymousNamespaceName(StringRef Name) {
241-
// Types that are in an anonymous namespace are local to this module.
242-
// FIXME: This should really be marked by the frontend in the metadata
243-
// instead of having us guess this from the mangled name. Moreover, the regex
244-
// here can pick up (unlikely) names in the non-reserved namespace (because
245-
// it needs to search into the type to pick up cases where the type in the
246-
// anonymous namespace is a template parameter, etc.).
247-
return AnonNameRegex.match(Name);
248-
}
249-
250242
std::string
251243
TypeSanitizer::getAnonymousStructIdentifier(const MDNode *MD,
252244
TypeNameMapTy &TypeNames) {
@@ -352,7 +344,13 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
352344
TDSubTys.push_back(IntptrTy);
353345
TDSubData.push_back(ConstantInt::get(IntptrTy, Members.size()));
354346

355-
bool ShouldBeComdat = !isAnonymousNamespaceName(NameNode->getString());
347+
// Types that are in an anonymous namespace are local to this module.
348+
// FIXME: This should really be marked by the frontend in the metadata
349+
// instead of having us guess this from the mangled name. Moreover, the regex
350+
// here can pick up (unlikely) names in the non-reserved namespace (because
351+
// it needs to search into the type to pick up cases where the type in the
352+
// anonymous namespace is a template parameter, etc.).
353+
bool ShouldBeComdat = !AnonNameRegex.match(NameNode->getString());
356354
for (auto &Member : Members) {
357355
TDSubTys.push_back(Member.first->getType());
358356
TDSubData.push_back(Member.first);

llvm/unittests/Analysis/AliasSetTrackerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TEST(AliasSetTracker, AliasUnknownInst) {
6262
TargetLibraryInfoImpl TLII(Trip);
6363
TargetLibraryInfo TLI(TLII);
6464
AAResults AA(TLI);
65-
TypeBasedAAResult TBAAR;
65+
TypeBasedAAResult TBAAR(false);
6666
AA.addAAResult(TBAAR);
6767

6868
// Initialize the alias set tracker for the @test function.

0 commit comments

Comments
 (0)