优化c ++编译器如何重用函数的堆栈槽? [英] how does an optimizing c++ compiler reuse stack slots of a function?

查看:211
本文介绍了优化c ++编译器如何重用函数的堆栈槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

优化c ++编译器如何确定函数的堆栈槽(函数的堆栈帧的一部分)何时不再需要该函数,从而可以重用其内存? 。

通过堆栈槽我的意思是一个函数的堆栈帧的一部分,不一定是一个函数的整个堆栈帧和一个例子澄清事实是,假设我们有一个函数有六个整数变量在它的作用域中定义,当它在函数中使用第六个变量时,第五个变量变得无用,因此编译器可以对第五和第六个变量使用相同的内存块。

任何关于此主题的信息都是赞赏的。 p>

How does an optimizing c++ compiler determine when a stack slot of a function(part of stack frame of a function) is no longer needed by that function, so it can reuse its memory? .
By stack slot I mean a part of stack frame of a function, not necessarily a whole stack frame of a function and an example to clarify the matter is, suppose we have a function that has six integer variables defined in its scope, when it's time to use sixth variable in the function, fifth variable's become useless so compiler can use same memory block for fifth and sixth variables.
any information on this subject is appreciated.

推荐答案

很容易的部分是:当一个函数退出时,该函数的所有局部变量都被释放。因此,函数exit表示整个栈帧可以被释放。这是一个没有脑子的,但是,你不会提到优化编译器,如果这是你以后。

The easy part is: When a function exits, all local variables of that function are released. Thus, function exit indicates that the whole stack frame can be freed. That's a no-brainer, though, and you wouldn't have mentioned "optimizing compiler" if that's what you were after.

理论上,一个编译器可以做流量分析对函数,找出在什么时候使用哪些内存块,甚至可能基于变量可用的顺序重新排序堆栈分配。然后,如果在函数中间或函数内部嵌套的其他范围(而不是在其开头)引入新的自动变量,那些最近释放的槽可以重新使用。

In theory, a compiler can do flow analysis on a function, find out which chunks of memory are used at what time, and perhaps even re-order stack allocation based on the order in which variables become available. Then, if new automatic variables are introduced somewhere in the middle of the function or other scope nested within the function (rather than at its beginning), those recently freed slots could be re-used.

在实践中,这听起来像很多旋转齿轮,我怀疑堆栈只是当变量进入范围和分配en块通过减少堆栈指针时范围完成时分配。但我承认我对这个话题不是专家。有更多权威知识的人可能会来,纠正我。

In practice, this sounds like a lot of spinning gears, and I suspect that stack is simply allocated whenever variables come in scope and popped off en block by decrementing the stack pointer when the scope finishes. But I admit I'm no expert on this topic. Someone with more authoritative knowledge may come along and correct me.

这篇关于优化c ++编译器如何重用函数的堆栈槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆