[llvm] 4646cab - [SanitizerCoverage] Avoid unnecessary inttoptr

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 9 09:19:17 PST 2024


Author: Alexander Richardson
Date: 2024-12-09T09:19:13-08:00
New Revision: 4646cabf01c8ca8e301a75ee46c56ce76eb412e0

URL: https://github.com/llvm/llvm-project/commit/4646cabf01c8ca8e301a75ee46c56ce76eb412e0
DIFF: https://github.com/llvm/llvm-project/commit/4646cabf01c8ca8e301a75ee46c56ce76eb412e0.diff

LOG: [SanitizerCoverage] Avoid unnecessary inttoptr

We are indexing into an array here, so we can use
CreateConstInBoundsGEP2_64 instead of the manual arithmetic.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/119097

Added: 
    

Modified: 
    llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
    llvm/test/Instrumentation/SanitizerCoverage/cmp-tracing-api-x86_32.ll
    llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 22acf59c78a385..ac033d92e30d85 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -1045,10 +1045,8 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
         ->setCannotMerge(); // gets the PC using GET_CALLER_PC.
   }
   if (Options.TracePCGuard) {
-    auto GuardPtr = IRB.CreateIntToPtr(
-        IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
-                      ConstantInt::get(IntptrTy, Idx * 4)),
-        PtrTy);
+    auto GuardPtr = IRB.CreateConstInBoundsGEP2_64(
+        FunctionGuardArray->getValueType(), FunctionGuardArray, 0, Idx);
     if (Options.GatedCallbacks) {
       Instruction *I = &*IP;
       auto GateBranch = CreateGateBranch(F, FunctionGateCmp, I);

diff  --git a/llvm/test/Instrumentation/SanitizerCoverage/cmp-tracing-api-x86_32.ll b/llvm/test/Instrumentation/SanitizerCoverage/cmp-tracing-api-x86_32.ll
index 6ade3e32bd2791..e57ff078a5274f 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/cmp-tracing-api-x86_32.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/cmp-tracing-api-x86_32.ll
@@ -7,13 +7,14 @@ target triple = "i386-unknown-linux-gnu"
 define i32 @foo() #0 {
 ; CHECK-LABEL: define i32 @foo() comdat {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr inttoptr (i32 ptrtoint (ptr @__sancov_gen_ to i32) to ptr)) #[[ATTR1:[0-9]+]]
+; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr @__sancov_gen_) #[[ATTR1:[0-9]+]]
 ; CHECK-NEXT:    ret i32 0
 ;
 entry:
   ret i32 0
 }
 
+; UTC_ARGS: --disable
 ; CHECK-DAG: declare void @__sanitizer_cov_trace_pc_indir(i32)
 ; CHECK-DAG: declare void @__sanitizer_cov_trace_cmp1(i8 zeroext, i8 zeroext)
 ; CHECK-DAG: declare void @__sanitizer_cov_trace_cmp2(i16 zeroext, i16 zeroext)

diff  --git a/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll b/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
index fdd6b7e7d24caa..35684346c4d5a8 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
@@ -12,7 +12,7 @@ define i32 @with_dbg(ptr %a, ptr %b) !dbg !3 {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 42
 ; CHECK-NEXT:    br i1 [[CMP]], label %[[BB0:.*]], label %[[BB1:.*]]
 ; CHECK:       [[BB0]]:
-; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr inttoptr (i64 add (i64 ptrtoint (ptr @__sancov_gen_ to i64), i64 4) to ptr)) #[[ATTR1]], !dbg [[DBG7:![0-9]+]]
+; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr getelementptr inbounds ([2 x i32], ptr @__sancov_gen_, i64 0, i64 1)) #[[ATTR1]], !dbg [[DBG7:![0-9]+]]
 ; CHECK-NEXT:    store i32 [[TMP1]], ptr [[B]], align 4
 ; CHECK-NEXT:    br label %[[BB1]]
 ; CHECK:       [[BB1]]:
@@ -38,7 +38,7 @@ define i32 @without_dbg(ptr %a, ptr %b) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 42
 ; CHECK-NEXT:    br i1 [[CMP]], label %[[BB0:.*]], label %[[BB1:.*]]
 ; CHECK:       [[BB0]]:
-; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr inttoptr (i64 add (i64 ptrtoint (ptr @__sancov_gen_.1 to i64), i64 4) to ptr)) #[[ATTR1]]
+; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc_guard(ptr getelementptr inbounds ([2 x i32], ptr @__sancov_gen_.1, i64 0, i64 1)) #[[ATTR1]]
 ; CHECK-NEXT:    store i32 [[TMP1]], ptr [[B]], align 4
 ; CHECK-NEXT:    br label %[[BB1]]
 ; CHECK:       [[BB1]]:


        


More information about the llvm-commits mailing list