[llvm] [NFC] Add DIExpression::calculateFragmentIntersect (PR #97738)
Orlando Cazalet-Hyams via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 11 05:08:36 PDT 2024
================
@@ -2052,6 +2052,75 @@ std::optional<DIExpression *> DIExpression::createFragmentExpression(
return DIExpression::get(Expr->getContext(), Ops);
}
+/// See declaration for more info.
+bool DIExpression::calculateFragmentIntersect(
+ const DataLayout &DL, const Value *SliceStart, uint64_t SliceOffsetInBits,
+ uint64_t SliceSizeInBits, const Value *DbgPtr, int64_t DbgPtrOffsetInBits,
+ int64_t DbgExtractOffsetInBits, DIExpression::FragmentInfo VarFrag,
+ std::optional<DIExpression::FragmentInfo> &Result,
+ int64_t &OffsetFromLocationInBits) {
+
+ if (VarFrag.SizeInBits == 0)
+ return false; // Variable size is unknown.
+
+ // Difference between mem slice start and the dbg location start.
+ // 0 4 8 12 16 ...
+ // | |
+ // dbg location start
+ // |
+ // mem slice start
+ // Here MemStartRelToDbgStartInBits is 8. Note this can be negative.
+ int64_t MemStartRelToDbgStartInBits;
+ {
+ auto MemOffsetFromDbgInBytes = SliceStart->getPointerOffsetFrom(DbgPtr, DL);
+ if (!MemOffsetFromDbgInBytes)
+ return false; // Can't calculate difference in addresses.
+ // Difference between the pointers.
+ MemStartRelToDbgStartInBits = *MemOffsetFromDbgInBytes * 8;
+ // Add the difference of the offsets.
+ MemStartRelToDbgStartInBits +=
+ SliceOffsetInBits - (DbgPtrOffsetInBits + DbgExtractOffsetInBits);
+ }
+
+ // Out-param. Invert offset to get offset from debug location.
+ OffsetFromLocationInBits = -MemStartRelToDbgStartInBits;
+
+ // Check if the variable fragment sits outside (before) this memory slice.
+ int64_t MemEndRelToDbgStart = MemStartRelToDbgStartInBits + SliceSizeInBits;
+ if (MemEndRelToDbgStart < 0) {
+ Result = {0, 0}; // Out-param.
+ return true;
+ }
+
+ // Work towards creating SliceOfVariable which is the bits of the variable
+ // that the memory region covers.
+ // 0 4 8 12 16 ...
+ // | |
+ // dbg location start with VarFrag offset=32
+ // |
+ // mem slice start: SliceOfVariable offset=40
+ int64_t MemStartRelToFragInBits =
+ MemStartRelToDbgStartInBits + VarFrag.OffsetInBits;
----------------
OCHyams wrote:
Nice catch. Change Frag to Var. Still not an amazing name, but I found coming up with meaningful names in this function was pretty difficult (hence all the comments!). Happy to take further suggestions?
https://github.com/llvm/llvm-project/pull/97738
More information about the llvm-commits
mailing list