C++ frontend improves alias recognition with lambda template arguments
GCC's C++ frontend now more efficiently recognizes aliases involving lambda template arguments, leading to minor compilation speed improvements.
This commit enhances the GCC C++ frontend’s ability to recognize dependencies when lambda expressions are used as template arguments. A new flag, TYPE_DECL_OPAQUE_ALIAS_P, is introduced to mark declarations, avoiding expensive tree traversals in dependent_opaque_alias_p. This optimization specifically targets deeply nested lambdas, which previously fooled the alias detection logic. The change yields a marginal but measurable improvement in compilation time, particularly for complex C++ codebases like range-v3.
In Details
This commit optimizes alias detection within the C++ frontend, specifically targeting type declarations involving lambda expressions as template arguments. The core issue lies in dependent_opaque_alias_p needing to determine if a type alias is 'opaque' (i.e., its underlying type depends on a template parameter, often involving lambdas). Previously, this involved costly walk_tree operations to search for LAMBDA_EXPR nodes. This patch introduces TYPE_DECL_OPAQUE_ALIAS_P, a direct flag on TYPE_DECL to quickly mark such aliases, bypassing the expensive tree traversal. This avoids redund…
For Context
When you write C++ code, especially with modern features like templates and lambda functions, the compiler needs to understand how different parts of your code relate to each other. Sometimes, a complex type definition might be an "alias"—a different name for another type. If this alias depends on a "lambda" (a small, anonymous function) as a template argument, the compiler needs to figure out if this alias is opaque, meaning its exact type isn't fully known until later stages of compilation. Previously, the GCC C++ compiler spent a lot of time searching through the code's internal structure to make this determination, especially with deeply nested lambdas. This change adds a shortcut: a special flag that marks these specific types of aliases directly. This allows the compiler to quickly identify them without a lengthy search, resulting in a slightly faster compilation process for C++ programs that use these advanced features.