On Tue, Nov 24, 2020 at 4:47 PM Qing Zhao <qing.z...@oracle.com> wrote: > > > > > On Nov 24, 2020, at 1:32 AM, Richard Biener <richard.guent...@gmail.com> > > wrote: > > > > On Tue, Nov 24, 2020 at 12:05 AM Qing Zhao via Gcc-patches > > <gcc-patches@gcc.gnu.org> wrote: > >> > >> Hi, > >> > >> Does gcc provide an iterator to traverse all the local variables that are > >> declared in the current routine? > >> > >> If not, what’s the best way to traverse the local variables? > > > > Depends on what for. There's the source level view you get by walking > > BLOCK_VARS of the > > scope tree, theres cfun->local_variables (FOR_EACH_LOCAL_DECL) and > > there's SSA names > > (FOR_EACH_SSA_NAME). > > I am planing to add a new phase immediately after > “pass_late_warn_uninitialized” to initialize all auto-variables that are > not explicitly initialized in the declaration, the basic idea is following: > > ** The proposal: > > A. add a new GCC option: (same name and meaning as CLANG) > -ftrivial-auto-var-init=[pattern|zero], similar pattern init as CLANG; > > B. add a new attribute for variable: > __attribute((uninitialized) > the marked variable is uninitialized intentionaly for performance purpose. > > C. The implementation needs to keep the current static warning on > uninitialized > variables untouched in order to avoid "forking the language". > > > ** The implementation: > > There are two major requirements for the implementation: > > 1. all auto-variables that do not have an explicit initializer should be > initialized to > zero by this option. (Same behavior as CLANG) > > 2. keep the current static warning on uninitialized variables untouched. > > In order to satisfy 1, we should check whether an auto-variable has > initializer > or not; > In order to satisfy 2, we should add this new transformation after > "pass_late_warn_uninitialized". > > So, we should be able to check whether an auto-variable has initializer or > not after “pass_late_warn_uninitialized”, > If Not, then insert an initialization for it. > > For this purpose, I guess that “FOR_EACH_LOCAL_DECL” might be better?
Yes, but do you want to catch variables promoted to register as well or just variables on the stack? > Another issue is, in order to check whether an auto-variable has initializer, > I plan to add a new bit in “decl_common” as: > /* In a VAR_DECL, this is DECL_IS_INITIALIZED. */ > unsigned decl_is_initialized :1; > > /* IN VAR_DECL, set when the decl is initialized at the declaration. */ > #define DECL_IS_INITIALIZED(NODE) \ > (DECL_COMMON_CHECK (NODE)->decl_common.decl_is_initialized) > > set this bit when setting DECL_INITIAL for the variables in FE. then keep it > even though DECL_INITIAL might be NULLed. For locals it would be more reliable to set this flag during gimplification. > Do you have any comment and suggestions? As said above - do you want to cover registers as well as locals? I'd do the actual zeroing during RTL expansion instead since otherwise you have to figure youself whether a local is actually used (see expand_stack_vars) Note that optimization will already made have use of "uninitialized" state of locals so depending on what the actual goal is here "late" may be too late. Richard. > > Thanks a lot for the help. > > Qing > > > Richard. > > > >> > >> Thanks. > >> > >> Qing >