[clang] [Clang] Allow devirtualization involving array subscripts with constant indices when the pointee type is known (PR #130528)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 18 06:20:13 PDT 2025


================
@@ -2465,6 +2465,27 @@ CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
   if (Base->isPRValue() && Base->getType()->isRecordType())
     return this;
 
+  // Handle array subscripts with constant indices when the pointee type is
+  // known
+  if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Base)) {
+    QualType BaseTy = ASE->getBase()->getType();
+
+    // Check if it's a pointer to a record type
+    if (BaseTy->isPointerType() && BaseTy->getPointeeType()->isRecordType()) {
+      // For C++17 and later, we can devirtualize array access beyond p[0]
----------------
erichkeane wrote:

I don't see the 'beyond [0]' in that standardeeze, can you better clarify it?

Also, where are you checking for >0 here?  It seems like you'd do this in every case?  What is making this skip the 0 case?

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


More information about the cfe-commits mailing list