snowman icon indicating copy to clipboard operation
snowman copied to clipboard

Add partial constant folding

Open ghost opened this issue 7 years ago • 9 comments

` uint32_t _Z5isqrtj(uint32_t edi) { uint32_t v2; uint32_t v3; uint32_t v4;

v2 = edi;
v3 = 0;
v4 = 0x40000000;
do {
    if (v3 + v4 <= v2) {
        v2 = v2 - (v3 + v4);
        v3 = v3 + v4 + v4;
    }
    v3 = v3 >> 1;
    v4 = v4 >> 2;
} while (v4);
if (v2 > v3) {
    ++v3;
}
return v3;

} int64_t main(int32_t edi, int64_t rsi) { int64_t rsi3; uint32_t eax4; uint32_t eax5; int64_t rax6;

rsi3 = rsi;
eax4 = *reinterpret_cast<uint32_t*>(&rsi3);
*reinterpret_cast<unsigned char*>(&eax4) = static_cast<unsigned char>(static_cast<uint1_t>(eax4 >> 5));
eax5 = _Z5isqrtj(0x2137);
*reinterpret_cast<uint32_t*>(&rax6) = eax4 + eax5;
*reinterpret_cast<int32_t*>(reinterpret_cast<int64_t>(&rax6) + 4) = 0;
return rax6;

} `

In this code the value being passed to isqrt is known and isqrt does not reference global memory at all. Evaluating completely pure functions and embedding their results into the generated code could improve the output considerably in certain situations. The generated code would include a comment telling the user that the function was evaluated and the values that were passed to it. This could be toggled on and off by the user if they would prefer pure functions not be evaluated.

Would anyone object to this being added?

ghost avatar Mar 25 '17 16:03 ghost

No one would object. Actually, there was some code for inlining some time ago, see, e.g., 37391125ed8b90a2f411f4868f2f45f8164f16d6. The code was not used and was removed. One could bring it back and provide an interface to the user, so that she could say which functions to inline and/or which calls to inline.

yegord avatar Mar 25 '17 22:03 yegord

Why was it removed?

ghost avatar Mar 25 '17 23:03 ghost

Because it was not used. There was no access to inlining from the GUI.

yegord avatar Mar 25 '17 23:03 yegord

Seems like it could be useful if applied automatically to trivial functions, like one-line return functions or provably 100% pure functions

ghost avatar Mar 25 '17 23:03 ghost

Well, you definition of trivial may be different from the one of other users. :) So, Snowman prefers to be on the safe side: it produces what it sees (modulo bugs). At least, it cannot confuse anybody this way.

yegord avatar Mar 25 '17 23:03 yegord

Fair 'nuff, how about a toggle?

ghost avatar Mar 25 '17 23:03 ghost

I would prefer to give the user an ability to inline a particular function or a particular call through the GUI. But a switch is a good first step. At least, better than nothing.

yegord avatar Mar 25 '17 23:03 yegord

where exactly is the last compiled result for the entire compilation unit cached?

On Mar 25, 2017 7:43 PM, "yegord" [email protected] wrote:

I would prefer to give the user an ability to inline a particular function or a particular call through the GUI. But a switch is a good first step. At least, better than nothing.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yegord/snowman/issues/115#issuecomment-289247438, or mute the thread https://github.com/notifications/unsubscribe-auth/AFDrY5GE9WtR4m0skWNXJEugp_pKfjkTks5rpaaSgaJpZM4MpH6G .

ghost avatar Mar 26 '17 00:03 ghost

nc::gui::Project::context() nc::gui::MainWindow stores a pointer to a Project.

yegord avatar Mar 26 '17 00:03 yegord