[clang] [llvm] [ThinLTO] Support dead RTTI data elimination under -fno-split-lto-unit (PR #126336)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 24 22:52:24 PST 2025


================
@@ -50,6 +51,41 @@ findCallsAtConstantOffset(SmallVectorImpl<DevirtCallSite> &DevirtCalls,
   }
 }
 
+static bool hasTypeIdLoadAtConstantOffset(const Module *M, Value *VPtr,
+                                          int64_t Offset, const CallInst *CI,
+                                          CXXABI *ABI) {
+  bool HasTypeIdLoad = false;
+  for (const Use &U : VPtr->uses()) {
+    Value *User = U.getUser();
+    if (isa<BitCastInst>(User)) {
+      HasTypeIdLoad |= hasTypeIdLoadAtConstantOffset(M, User, Offset, CI, ABI);
+    } else if (isa<LoadInst>(User)) {
+      if (Offset ==
+          ABI->getOffsetFromTypeInfoSlotToAddressPoint(M->getDataLayout()))
+        return true;
+    } else if (auto GEP = dyn_cast<GetElementPtrInst>(User)) {
+      // Take into account the GEP offset.
+      if (VPtr == GEP->getPointerOperand() && GEP->hasAllConstantIndices()) {
+        SmallVector<Value *, 8> Indices(drop_begin(GEP->operands()));
+        int64_t GEPOffset = M->getDataLayout().getIndexedOffsetInType(
+            GEP->getSourceElementType(), Indices);
+        HasTypeIdLoad |=
+            hasTypeIdLoadAtConstantOffset(M, User, Offset + GEPOffset, CI, ABI);
+      }
+    } else if (auto *Call = dyn_cast<CallInst>(User)) {
+      if (Call->getIntrinsicID() == llvm::Intrinsic::load_relative) {
+        if (auto *LoadOffset = dyn_cast<ConstantInt>(Call->getOperand(1))) {
+          HasTypeIdLoad |=
+              hasTypeIdLoadAtConstantOffset(M, User, Offset, CI, ABI);
+        }
+      }
+    } else {
+      HasTypeIdLoad = true;
+    }
----------------
luxufan wrote:

Thank you for testing this patch and providing the test case. I have fixed the issue and added it as a test case.

https://github.com/llvm/llvm-project/pull/126336


More information about the llvm-commits mailing list