Skip to content

Commit 107185f

Browse files
authored
[sanitizer] Remove unneeded pointer casts and migrate away from getInt8PtrTy. NFC (#72327)
After opaque pointer migration, getInt8PtrTy() is considered legacy. Replace it with getPtrTy(), and while here, remove some unneeded pointer casts.
1 parent 506c0fa commit 107185f

File tree

4 files changed

+90
-112
lines changed

4 files changed

+90
-112
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ struct AddressSanitizer {
660660
DL = &M.getDataLayout();
661661
LongSize = M.getDataLayout().getPointerSizeInBits();
662662
IntptrTy = Type::getIntNTy(*C, LongSize);
663-
Int8PtrTy = PointerType::getUnqual(*C);
663+
PtrTy = PointerType::getUnqual(*C);
664664
Int32Ty = Type::getInt32Ty(*C);
665665
TargetTriple = Triple(M.getTargetTriple());
666666

@@ -752,8 +752,8 @@ struct AddressSanitizer {
752752
bool UseAfterScope;
753753
AsanDetectStackUseAfterReturnMode UseAfterReturn;
754754
Type *IntptrTy;
755-
Type *Int8PtrTy;
756755
Type *Int32Ty;
756+
PointerType *PtrTy;
757757
ShadowMapping Mapping;
758758
FunctionCallee AsanHandleNoReturnFunc;
759759
FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
@@ -807,6 +807,7 @@ class ModuleAddressSanitizer {
807807
C = &(M.getContext());
808808
int LongSize = M.getDataLayout().getPointerSizeInBits();
809809
IntptrTy = Type::getIntNTy(*C, LongSize);
810+
PtrTy = PointerType::getUnqual(*C);
810811
TargetTriple = Triple(M.getTargetTriple());
811812
Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
812813

@@ -863,6 +864,7 @@ class ModuleAddressSanitizer {
863864
AsanDtorKind DestructorKind;
864865
AsanCtorKind ConstructorKind;
865866
Type *IntptrTy;
867+
PointerType *PtrTy;
866868
LLVMContext *C;
867869
Triple TargetTriple;
868870
ShadowMapping Mapping;
@@ -1233,15 +1235,13 @@ Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
12331235
void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
12341236
InstrumentationIRBuilder IRB(MI);
12351237
if (isa<MemTransferInst>(MI)) {
1236-
IRB.CreateCall(
1237-
isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
1238-
{IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1239-
IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
1240-
IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
1238+
IRB.CreateCall(isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
1239+
{MI->getOperand(0), MI->getOperand(1),
1240+
IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
12411241
} else if (isa<MemSetInst>(MI)) {
12421242
IRB.CreateCall(
12431243
AsanMemset,
1244-
{IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1244+
{MI->getOperand(0),
12451245
IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
12461246
IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
12471247
}
@@ -1696,9 +1696,8 @@ Instruction *AddressSanitizer::instrumentAMDGPUAddress(
16961696
return InsertBefore;
16971697
// Instrument generic addresses in supported addressspaces.
16981698
IRBuilder<> IRB(InsertBefore);
1699-
Value *AddrLong = IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy());
1700-
Value *IsShared = IRB.CreateCall(AMDGPUAddressShared, {AddrLong});
1701-
Value *IsPrivate = IRB.CreateCall(AMDGPUAddressPrivate, {AddrLong});
1699+
Value *IsShared = IRB.CreateCall(AMDGPUAddressShared, {Addr});
1700+
Value *IsPrivate = IRB.CreateCall(AMDGPUAddressPrivate, {Addr});
17021701
Value *IsSharedOrPrivate = IRB.CreateOr(IsShared, IsPrivate);
17031702
Value *Cmp = IRB.CreateNot(IsSharedOrPrivate);
17041703
Value *AddrSpaceZeroLanding =
@@ -1729,7 +1728,7 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
17291728
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
17301729
IRB.CreateCall(
17311730
Intrinsic::getDeclaration(M, Intrinsic::asan_check_memaccess),
1732-
{IRB.CreatePointerCast(Addr, Int8PtrTy),
1731+
{IRB.CreatePointerCast(Addr, PtrTy),
17331732
ConstantInt::get(Int32Ty, AccessInfo.Packed)});
17341733
return;
17351734
}
@@ -2452,7 +2451,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
24522451
G->eraseFromParent();
24532452
NewGlobals[i] = NewGlobal;
24542453

2455-
Constant *ODRIndicator = ConstantExpr::getNullValue(IRB.getInt8PtrTy());
2454+
Constant *ODRIndicator = ConstantPointerNull::get(PtrTy);
24562455
GlobalValue *InstrumentedGlobal = NewGlobal;
24572456

24582457
bool CanUsePrivateAliases =
@@ -2467,8 +2466,8 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
24672466

24682467
// ODR should not happen for local linkage.
24692468
if (NewGlobal->hasLocalLinkage()) {
2470-
ODRIndicator = ConstantExpr::getIntToPtr(ConstantInt::get(IntptrTy, -1),
2471-
IRB.getInt8PtrTy());
2469+
ODRIndicator =
2470+
ConstantExpr::getIntToPtr(ConstantInt::get(IntptrTy, -1), PtrTy);
24722471
} else if (UseOdrIndicator) {
24732472
// With local aliases, we need to provide another externally visible
24742473
// symbol __odr_asan_XXX to detect ODR violation.
@@ -2688,15 +2687,12 @@ void AddressSanitizer::initializeCallbacks(Module &M, const TargetLibraryInfo *T
26882687
? std::string("")
26892688
: ClMemoryAccessCallbackPrefix;
26902689
AsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
2691-
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2692-
IRB.getInt8PtrTy(), IntptrTy);
2693-
AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
2694-
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2695-
IRB.getInt8PtrTy(), IntptrTy);
2690+
PtrTy, PtrTy, PtrTy, IntptrTy);
2691+
AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy", PtrTy,
2692+
PtrTy, PtrTy, IntptrTy);
26962693
AsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
26972694
TLI->getAttrList(C, {1}, /*Signed=*/false),
2698-
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2699-
IRB.getInt32Ty(), IntptrTy);
2695+
PtrTy, PtrTy, IRB.getInt32Ty(), IntptrTy);
27002696

27012697
AsanHandleNoReturnFunc =
27022698
M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy());
@@ -2709,10 +2705,10 @@ void AddressSanitizer::initializeCallbacks(Module &M, const TargetLibraryInfo *T
27092705
AsanShadowGlobal = M.getOrInsertGlobal("__asan_shadow",
27102706
ArrayType::get(IRB.getInt8Ty(), 0));
27112707

2712-
AMDGPUAddressShared = M.getOrInsertFunction(
2713-
kAMDGPUAddressSharedName, IRB.getInt1Ty(), IRB.getInt8PtrTy());
2714-
AMDGPUAddressPrivate = M.getOrInsertFunction(
2715-
kAMDGPUAddressPrivateName, IRB.getInt1Ty(), IRB.getInt8PtrTy());
2708+
AMDGPUAddressShared =
2709+
M.getOrInsertFunction(kAMDGPUAddressSharedName, IRB.getInt1Ty(), PtrTy);
2710+
AMDGPUAddressPrivate =
2711+
M.getOrInsertFunction(kAMDGPUAddressPrivateName, IRB.getInt1Ty(), PtrTy);
27162712
}
27172713

27182714
bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
@@ -3504,7 +3500,7 @@ void FunctionStackPoisoner::processStaticAllocas() {
35043500
IntptrTy, IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
35053501
IRBPoison.CreateStore(
35063502
Constant::getNullValue(IRBPoison.getInt8Ty()),
3507-
IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
3503+
IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getPtrTy()));
35083504
} else {
35093505
// For larger frames call __asan_stack_free_*.
35103506
IRBPoison.CreateCall(

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class HWAddressSanitizer {
384384

385385
Type *VoidTy = Type::getVoidTy(M.getContext());
386386
Type *IntptrTy;
387-
Type *Int8PtrTy;
387+
PointerType *PtrTy;
388388
Type *Int8Ty;
389389
Type *Int32Ty;
390390
Type *Int64Ty = Type::getInt64Ty(M.getContext());
@@ -587,7 +587,7 @@ void HWAddressSanitizer::initializeModule() {
587587
C = &(M.getContext());
588588
IRBuilder<> IRB(*C);
589589
IntptrTy = IRB.getIntPtrTy(DL);
590-
Int8PtrTy = IRB.getInt8PtrTy();
590+
PtrTy = IRB.getPtrTy();
591591
Int8Ty = IRB.getInt8Ty();
592592
Int32Ty = IRB.getInt32Ty();
593593

@@ -667,19 +667,19 @@ void HWAddressSanitizer::initializeCallbacks(Module &M) {
667667
FunctionType::get(VoidTy, {IntptrTy, IntptrTy, Int8Ty}, false);
668668
HwasanMemoryAccessCallbackFnTy =
669669
FunctionType::get(VoidTy, {IntptrTy, Int8Ty}, false);
670-
HwasanMemTransferFnTy = FunctionType::get(
671-
Int8PtrTy, {Int8PtrTy, Int8PtrTy, IntptrTy, Int8Ty}, false);
672-
HwasanMemsetFnTy = FunctionType::get(
673-
Int8PtrTy, {Int8PtrTy, Int32Ty, IntptrTy, Int8Ty}, false);
670+
HwasanMemTransferFnTy =
671+
FunctionType::get(PtrTy, {PtrTy, PtrTy, IntptrTy, Int8Ty}, false);
672+
HwasanMemsetFnTy =
673+
FunctionType::get(PtrTy, {PtrTy, Int32Ty, IntptrTy, Int8Ty}, false);
674674
} else {
675675
HwasanMemoryAccessCallbackSizedFnTy =
676676
FunctionType::get(VoidTy, {IntptrTy, IntptrTy}, false);
677677
HwasanMemoryAccessCallbackFnTy =
678678
FunctionType::get(VoidTy, {IntptrTy}, false);
679679
HwasanMemTransferFnTy =
680-
FunctionType::get(Int8PtrTy, {Int8PtrTy, Int8PtrTy, IntptrTy}, false);
680+
FunctionType::get(PtrTy, {PtrTy, PtrTy, IntptrTy}, false);
681681
HwasanMemsetFnTy =
682-
FunctionType::get(Int8PtrTy, {Int8PtrTy, Int32Ty, IntptrTy}, false);
682+
FunctionType::get(PtrTy, {PtrTy, Int32Ty, IntptrTy}, false);
683683
}
684684

685685
for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
@@ -713,7 +713,7 @@ void HWAddressSanitizer::initializeCallbacks(Module &M) {
713713
MemIntrinCallbackPrefix + "memset" + MatchAllStr, HwasanMemsetFnTy);
714714

715715
HwasanTagMemoryFunc = M.getOrInsertFunction("__hwasan_tag_memory", VoidTy,
716-
Int8PtrTy, Int8Ty, IntptrTy);
716+
PtrTy, Int8Ty, IntptrTy);
717717
HwasanGenerateTagFunc =
718718
M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
719719

@@ -733,7 +733,7 @@ Value *HWAddressSanitizer::getOpaqueNoopCast(IRBuilder<> &IRB, Value *Val) {
733733
// This prevents code bloat as a result of rematerializing trivial definitions
734734
// such as constants or global addresses at every load and store.
735735
InlineAsm *Asm =
736-
InlineAsm::get(FunctionType::get(Int8PtrTy, {Val->getType()}, false),
736+
InlineAsm::get(FunctionType::get(PtrTy, {Val->getType()}, false),
737737
StringRef(""), StringRef("=r,0"),
738738
/*hasSideEffects=*/false);
739739
return IRB.CreateCall(Asm, {Val}, ".hwasan.shadow");
@@ -747,15 +747,15 @@ Value *HWAddressSanitizer::getShadowNonTls(IRBuilder<> &IRB) {
747747
if (Mapping.Offset != kDynamicShadowSentinel)
748748
return getOpaqueNoopCast(
749749
IRB, ConstantExpr::getIntToPtr(
750-
ConstantInt::get(IntptrTy, Mapping.Offset), Int8PtrTy));
750+
ConstantInt::get(IntptrTy, Mapping.Offset), PtrTy));
751751

752752
if (Mapping.InGlobal)
753753
return getDynamicShadowIfunc(IRB);
754754

755755
Value *GlobalDynamicAddress =
756756
IRB.GetInsertBlock()->getParent()->getParent()->getOrInsertGlobal(
757-
kHwasanShadowMemoryDynamicAddress, Int8PtrTy);
758-
return IRB.CreateLoad(Int8PtrTy, GlobalDynamicAddress);
757+
kHwasanShadowMemoryDynamicAddress, PtrTy);
758+
return IRB.CreateLoad(PtrTy, GlobalDynamicAddress);
759759
}
760760

761761
bool HWAddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) {
@@ -860,7 +860,7 @@ Value *HWAddressSanitizer::memToShadow(Value *Mem, IRBuilder<> &IRB) {
860860
// Mem >> Scale
861861
Value *Shadow = IRB.CreateLShr(Mem, Mapping.Scale);
862862
if (Mapping.Offset == 0)
863-
return IRB.CreateIntToPtr(Shadow, Int8PtrTy);
863+
return IRB.CreateIntToPtr(Shadow, PtrTy);
864864
// (Mem >> Scale) + Offset
865865
return IRB.CreateGEP(Int8Ty, ShadowBase, Shadow);
866866
}
@@ -952,7 +952,7 @@ void HWAddressSanitizer::instrumentMemAccessInline(Value *Ptr, bool IsWrite,
952952

953953
IRB.SetInsertPoint(TCI.TagMismatchTerm);
954954
Value *InlineTagAddr = IRB.CreateOr(TCI.AddrLong, 15);
955-
InlineTagAddr = IRB.CreateIntToPtr(InlineTagAddr, Int8PtrTy);
955+
InlineTagAddr = IRB.CreateIntToPtr(InlineTagAddr, PtrTy);
956956
Value *InlineTag = IRB.CreateLoad(Int8Ty, InlineTagAddr);
957957
Value *InlineTagMismatch = IRB.CreateICmpNE(TCI.PtrTag, InlineTag);
958958
SplitBlockAndInsertIfThen(InlineTagMismatch, TCI.TagMismatchTerm, false,
@@ -1013,16 +1013,15 @@ void HWAddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
10131013
IRBuilder<> IRB(MI);
10141014
if (isa<MemTransferInst>(MI)) {
10151015
SmallVector<Value *, 4> Args{
1016-
IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1017-
IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
1016+
MI->getOperand(0), MI->getOperand(1),
10181017
IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)};
10191018

10201019
if (UseMatchAllCallback)
10211020
Args.emplace_back(ConstantInt::get(Int8Ty, *MatchAllTag));
10221021
IRB.CreateCall(isa<MemMoveInst>(MI) ? HwasanMemmove : HwasanMemcpy, Args);
10231022
} else if (isa<MemSetInst>(MI)) {
10241023
SmallVector<Value *, 4> Args{
1025-
IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1024+
MI->getOperand(0),
10261025
IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
10271026
IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)};
10281027
if (UseMatchAllCallback)
@@ -1084,7 +1083,7 @@ void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag,
10841083
Tag = IRB.CreateTrunc(Tag, Int8Ty);
10851084
if (InstrumentWithCalls) {
10861085
IRB.CreateCall(HwasanTagMemoryFunc,
1087-
{IRB.CreatePointerCast(AI, Int8PtrTy), Tag,
1086+
{IRB.CreatePointerCast(AI, PtrTy), Tag,
10881087
ConstantInt::get(IntptrTy, AlignedSize)});
10891088
} else {
10901089
size_t ShadowSize = Size >> Mapping.Scale;
@@ -1102,9 +1101,9 @@ void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag,
11021101
const uint8_t SizeRemainder = Size % Mapping.getObjectAlignment().value();
11031102
IRB.CreateStore(ConstantInt::get(Int8Ty, SizeRemainder),
11041103
IRB.CreateConstGEP1_32(Int8Ty, ShadowPtr, ShadowSize));
1105-
IRB.CreateStore(Tag, IRB.CreateConstGEP1_32(
1106-
Int8Ty, IRB.CreatePointerCast(AI, Int8PtrTy),
1107-
AlignedSize - 1));
1104+
IRB.CreateStore(
1105+
Tag, IRB.CreateConstGEP1_32(Int8Ty, IRB.CreatePointerCast(AI, PtrTy),
1106+
AlignedSize - 1));
11081107
}
11091108
}
11101109
}
@@ -1243,7 +1242,7 @@ Value *HWAddressSanitizer::getSP(IRBuilder<> &IRB) {
12431242
Module *M = F->getParent();
12441243
auto *GetStackPointerFn = Intrinsic::getDeclaration(
12451244
M, Intrinsic::frameaddress,
1246-
IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
1245+
IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
12471246
CachedSP = IRB.CreatePtrToInt(
12481247
IRB.CreateCall(GetStackPointerFn, {Constant::getNullValue(Int32Ty)}),
12491248
IntptrTy);
@@ -1344,7 +1343,7 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
13441343
ThreadLongMaybeUntagged,
13451344
ConstantInt::get(IntptrTy, (1ULL << kShadowBaseAlignment) - 1)),
13461345
ConstantInt::get(IntptrTy, 1), "hwasan.shadow");
1347-
ShadowBase = IRB.CreateIntToPtr(ShadowBase, Int8PtrTy);
1346+
ShadowBase = IRB.CreateIntToPtr(ShadowBase, PtrTy);
13481347
}
13491348
}
13501349

@@ -1404,7 +1403,7 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
14041403
size_t Size = memtag::getAllocaSizeInBytes(*AI);
14051404
size_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment());
14061405

1407-
Value *AICast = IRB.CreatePointerCast(AI, Int8PtrTy);
1406+
Value *AICast = IRB.CreatePointerCast(AI, PtrTy);
14081407

14091408
auto HandleLifetime = [&](IntrinsicInst *II) {
14101409
// Set the lifetime intrinsic to cover the whole alloca. This reduces the
@@ -1723,8 +1722,8 @@ void HWAddressSanitizer::instrumentPersonalityFunctions() {
17231722
return;
17241723

17251724
FunctionCallee HwasanPersonalityWrapper = M.getOrInsertFunction(
1726-
"__hwasan_personality_wrapper", Int32Ty, Int32Ty, Int32Ty, Int64Ty,
1727-
Int8PtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy);
1725+
"__hwasan_personality_wrapper", Int32Ty, Int32Ty, Int32Ty, Int64Ty, PtrTy,
1726+
PtrTy, PtrTy, PtrTy, PtrTy);
17281727
FunctionCallee UnwindGetGR = M.getOrInsertFunction("_Unwind_GetGR", VoidTy);
17291728
FunctionCallee UnwindGetCFA = M.getOrInsertFunction("_Unwind_GetCFA", VoidTy);
17301729

@@ -1733,7 +1732,7 @@ void HWAddressSanitizer::instrumentPersonalityFunctions() {
17331732
if (P.first)
17341733
ThunkName += ("." + P.first->getName()).str();
17351734
FunctionType *ThunkFnTy = FunctionType::get(
1736-
Int32Ty, {Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int8PtrTy}, false);
1735+
Int32Ty, {Int32Ty, Int32Ty, Int64Ty, PtrTy, PtrTy}, false);
17371736
bool IsLocal = P.first && (!isa<GlobalValue>(P.first) ||
17381737
cast<GlobalValue>(P.first)->hasLocalLinkage());
17391738
auto *ThunkFn = Function::Create(ThunkFnTy,
@@ -1751,7 +1750,7 @@ void HWAddressSanitizer::instrumentPersonalityFunctions() {
17511750
HwasanPersonalityWrapper,
17521751
{ThunkFn->getArg(0), ThunkFn->getArg(1), ThunkFn->getArg(2),
17531752
ThunkFn->getArg(3), ThunkFn->getArg(4),
1754-
P.first ? P.first : Constant::getNullValue(Int8PtrTy),
1753+
P.first ? P.first : Constant::getNullValue(PtrTy),
17551754
UnwindGetGR.getCallee(), UnwindGetCFA.getCallee()});
17561755
WrapperCall->setTailCall();
17571756
IRB.CreateRet(WrapperCall);

0 commit comments

Comments
 (0)