Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:35:09+00:00 2026-05-27T13:35:09+00:00

Implementing a datastructure with two function pointers, key_eq and key_hash . When these function

  • 0

Implementing a datastructure with two function pointers, key_eq and key_hash. When these function pointers are set only once and never modified, I’d like gcc to compile in the function address directly instead of loading the struct member, however a single puts("") call will break gcc’s constant propagation (correct term?).

Below is a short snippet of ppc asm. What is seen is that the field key_hash is stored, we call libc puts and then key_hash is loaded. If puts is removed, the constant is correctly propagated. Is this unavoidable?

    lis 9,hasheq_string@ha   # tmp174,                                       
    la 0,hasheq_string@l(9)  # tmp173,, tmp174                               
    stw 3,16(1)      # h.flags,                                              
    lis 9,hashf_string@ha    # tmp176,                                       
    lis 3,.LC0@ha    # tmp178,                                               
    stw 0,36(1)      # h.key_eq, tmp173                                      
    la 0,hashf_string@l(9)   # tmp175,, tmp176                               
    la 3,.LC0@l(3)   #,, tmp178                                              
    stw 0,40(1)      # h.key_hash, tmp175                                    
    bl puts  #                                                               
    lwz 0,40(1)      # h.key_hash, h.key_hash                                
    mr 3,31  #, tmp164                                                       
    stw 31,32(1)     # h._key, tmp164                                        
    mtctr 0  #, h.key_hash                                                   
    bctrl    #   

I’m sorry for not having a C testcase to go with it, I have a hard time reproducing it, yet the short three-line “store, bl puts, load” sequence leads the question, is this impossible to work around?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:35:10+00:00Added an answer on May 27, 2026 at 1:35 pm

    It’s possible that GCC conservatively assumes that the call to puts may read/write memory, hence the value in key_hash may be needed by the called function and different before and after the call.

    This code:

    struct S
    {
      int f;
    };
    
    void bar ();
    
    int
    foo (struct S *s)
    {
      s->f = 314;
      bar ();
      return s->f + 2;
    }
    

    produces the “signature” sequence:

    foo:
        mflr 0
        stwu 1,-32(1)
        stw 29,20(1)
        mr 29,3
        stw 0,36(1)
        li 0,314
        stw 0,0(3)  ;;
        bl bar      ;; here we go
        lwz 3,0(29) ;;
        lwz 0,36(1)
        addi 3,3,2
        lwz 29,20(1)
        mtlr 0
        addi 1,1,32
        blr
    

    However, with a small modification (manual Scalar Replacement of Aggregates?):

    struct S
    {
      int f;
    };
    
    void bar ();
    
    int
    foo (struct S *s)
    {
      int i;
    
      s->f = i = 314;
      bar ();
      return i + 2;
    }
    

    one gets the desired result:

    foo:
        mflr 0
        stwu 1,-16(1)
        stw 0,20(1)
        li 0,314
        stw 0,0(3)
        bl bar
        lwz 0,20(1)
        li 3,316    ;; constant propagated and folded
        addi 1,1,16
        mtlr 0
        blr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a maxmin function, it works like matrix multiplication but instead of summing
I'm building a datastructure class with an std-like interface, and implementing different iterators for
I'm implementing a new machine learning algorithm in Java that extracts a prototype datastructure
How would one test whether data structure built properly? I'm implementing kind of modified
I'd like to do some stuffs before main function. I have multiple source files.
I am implementing a datastructure and want the user to be able to use
I have two excel files. The first excel file is like this: 12 A
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.