Annotate memcpy's of globals with info about the src/dst
authorJon Roelofs <jonathan_roelofs@apple.com>
Wed, 9 Jun 2021 23:36:38 +0000 (16:36 -0700)
committerJon Roelofs <jonathan_roelofs@apple.com>
Thu, 10 Jun 2021 01:11:08 +0000 (18:11 -0700)
Differential revision: https://reviews.llvm.org/D103994

llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
llvm/test/CodeGen/AArch64/memsize-remarks.ll

index 1ad75f7..8ee7e0c 100644 (file)
@@ -285,8 +285,23 @@ void MemoryOpRemark::visitSizeOperand(Value *V, OptimizationRemarkMissed &R) {
   }
 }
 
+static Optional<StringRef> nameOrNone(const Value *V) {
+  if (V->hasName())
+    return V->getName();
+  return None;
+}
+
 void MemoryOpRemark::visitVariable(const Value *V,
                                    SmallVectorImpl<VariableInfo> &Result) {
+  if (auto *GV = dyn_cast<GlobalVariable>(V)) {
+    auto *Ty = cast<PointerType>(GV->getType())->getElementType();
+    int64_t Size = DL.getTypeSizeInBits(Ty).getFixedSize();
+    VariableInfo Var{nameOrNone(GV), Size};
+    if (!Var.isEmpty())
+      Result.push_back(std::move(Var));
+    return;
+  }
+
   // If we find some information in the debug info, take that.
   bool FoundDI = false;
   // Try to get an llvm.dbg.declare, which has a DILocalVariable giving us the
@@ -312,12 +327,10 @@ void MemoryOpRemark::visitVariable(const Value *V,
     return;
 
   // If not, get it from the alloca.
-  Optional<StringRef> Name = AI->hasName() ? Optional<StringRef>(AI->getName())
-                                           : Optional<StringRef>(None);
   Optional<TypeSize> TySize = AI->getAllocationSizeInBits(DL);
   Optional<uint64_t> Size =
       TySize ? getSizeInBytes(TySize->getFixedSize()) : None;
-  VariableInfo Var{Name, Size};
+  VariableInfo Var{nameOrNone(AI), Size};
   if (!Var.isEmpty())
     Result.push_back(std::move(Var));
 }
index 10af36c..d505c75 100644 (file)
@@ -302,6 +302,19 @@ define void @known_call_with_dereferenceable_bytes(i8* dereferenceable(42) %dst,
   ret void
 }
 
+@dropbear = external unnamed_addr constant [3 x i8], align 1
+@koala = external unnamed_addr constant [7 x i8], align 1
+
+define void @slicePun() {
+bb:
+; GISEL: remark: <unknown>:0:0: Call to memcpy. Memory operation size: 24 bytes.{{$}}
+; GISEL-NEXT: Read Variables: koala (56 bytes).
+; GISEL-NEXT: Written Variables: dropbear (24 bytes).
+  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 getelementptr inbounds ([3 x i8], [3 x i8]* @dropbear, i64 0, i64 0),
+                                            i8* getelementptr inbounds ([7 x i8], [7 x i8]* @koala, i64 0, i64 0), i64 24, i1 false)
+  ret void
+}
+
 attributes #0 = { noinline nounwind ssp uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="apple-a7" "target-features"="+aes,+crypto,+fp-armv8,+neon,+sha2,+zcm,+zcz" }
 attributes #1 = { nounwind "frame-pointer"="non-leaf" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="apple-a7" "target-features"="+aes,+crypto,+fp-armv8,+neon,+sha2,+zcm,+zcz" }
 attributes #2 = { nofree nosync nounwind readnone speculatable willreturn }