[sanitizer-coverage] change trace-pc to use 8-byte guards

llvm-svn: 281809
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index f8844bd..3c137c9 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -293,7 +293,7 @@
   SanCovTracePC = checkSanitizerInterfaceFunction(
       M.getOrInsertFunction(SanCovTracePCName, VoidTy, nullptr));
   SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
-      SanCovTracePCGuardName, VoidTy, IRB.getInt8PtrTy(), nullptr));
+      SanCovTracePCGuardName, VoidTy, Int64PtrTy, nullptr));
   SanCovTraceEnter = checkSanitizerInterfaceFunction(
       M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy, nullptr));
   SanCovTraceBB = checkSanitizerInterfaceFunction(
@@ -352,16 +352,18 @@
   if (Options.TracePCGuard) {
     Function *CtorFunc;
     std::string SectionName(SanCovTracePCGuardSection);
-    auto Start =
-        new GlobalVariable(M, Int8PtrTy, false, GlobalVariable::ExternalLinkage,
-                           nullptr, "__start_" + SectionName);
-    auto Stop =
-        new GlobalVariable(M, Int8PtrTy, false, GlobalVariable::ExternalLinkage,
-                           nullptr, "__stop_" + SectionName);
+    GlobalVariable *Bounds[2];
+    const char *Prefix[2] = {"__start_", "__stop_"};
+    for (int i = 0; i < 2; i++) {
+      Bounds[i] = new GlobalVariable(M, Int64PtrTy, false,
+                                     GlobalVariable::ExternalLinkage, nullptr,
+                                     Prefix[i] + SectionName);
+      Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
+    }
     std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
         M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
-        {Int8PtrTy, Int8PtrTy}, {IRB.CreatePointerCast(Start, Int8PtrTy),
-                                 IRB.CreatePointerCast(Stop, Int8PtrTy)});
+        {Int64PtrTy, Int64PtrTy}, {IRB.CreatePointerCast(Bounds[0], Int64PtrTy),
+                                 IRB.CreatePointerCast(Bounds[1], Int64PtrTy)});
 
     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
 
@@ -662,23 +664,24 @@
     IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
   } else if (Options.TracePCGuard) {
-    auto GuardVar = new GlobalVariable(*F.getParent(), IRB.getInt8Ty(), false,
-                                       GlobalVariable::LinkOnceODRLinkage,
-                                       Constant::getNullValue(IRB.getInt8Ty()),
-                                       "__sancov_guard." + F.getName());
+    auto GuardVar = new GlobalVariable(
+        *F.getParent(), Int64Ty, false, GlobalVariable::LinkOnceODRLinkage,
+        Constant::getNullValue(Int64Ty), "__sancov_guard." + F.getName());
     // TODO: add debug into to GuardVar.
     GuardVar->setSection(SanCovTracePCGuardSection);
-    auto GuardPtr = IRB.CreatePointerCast(GuardVar, IRB.getInt8PtrTy());
-    auto GuardLoad = IRB.CreateLoad(GuardPtr);
-    GuardLoad->setAtomic(AtomicOrdering::Monotonic);
-    GuardLoad->setAlignment(1);
-    SetNoSanitizeMetadata(GuardLoad);  // Don't instrument with e.g. asan.
-    auto Cmp = IRB.CreateICmpNE(
-        Constant::getAllOnesValue(GuardLoad->getType()), GuardLoad);
-    auto Ins = SplitBlockAndInsertIfThen(
-        Cmp, &*IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
-    IRB.SetCurrentDebugLocation(EntryLoc);
-    IRB.SetInsertPoint(Ins);
+    auto GuardPtr = IRB.CreatePointerCast(GuardVar, Int64PtrTy);
+    if (!UseCalls) {
+      auto GuardLoad = IRB.CreateLoad(GuardPtr);
+      GuardLoad->setAtomic(AtomicOrdering::Monotonic);
+      GuardLoad->setAlignment(8);
+      SetNoSanitizeMetadata(GuardLoad);  // Don't instrument with e.g. asan.
+      auto Cmp = IRB.CreateICmpSGE(
+          GuardLoad, Constant::getNullValue(GuardLoad->getType()));
+      auto Ins = SplitBlockAndInsertIfThen(
+          Cmp, &*IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
+      IRB.SetCurrentDebugLocation(EntryLoc);
+      IRB.SetInsertPoint(Ins);
+    }
     IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
   } else if (Options.TraceBB) {