[interp] Improve copy propagation (mono/mono#17154)
authorVlad Brezae <brezaevlad@gmail.com>
Fri, 4 Oct 2019 11:20:52 +0000 (14:20 +0300)
committerGitHub <noreply@github.com>
Fri, 4 Oct 2019 11:20:52 +0000 (14:20 +0300)
commitefc473a3fa0c5dc5023fc7ffba1de7d066669d24
treecd99c6673ed4b5faac86e8b00d9bd29441c177c7
parent826238b071d16b23820682c216521091a0913737
[interp] Improve copy propagation (mono/mono#17154)

* [interp] Refactor tracking of stack/local values

Previously we had just an InterpInst* inside StackContentInfo, which was representing the instruction that pushed a certain local/value on the stack. This makes many things awkward, since an instruction is logically different from a value, that a local or a stack slot have. If we clear an instruction, it doesn't necessarily mean that the value that the instruction produced can't be stored on stack or in a local.

This commit creates a new structure StackValue, which holds the value.

* [interp] Generalize contents of StackValue

StackValue contains an opcode and some data to enable reconstruction of the value. For example instead of doing a LDLOC for a local, we can see if the local has a known value and use it instead (this could be a LDLOC from another local, whose value was propagated, or in the future a LDC). This will make more sense when we also start to track constant values.

Also decouple MOVLOC instructions from the cprop pass. They serve no purpose there. They are useful though when we do deadce, since we currently don't know how to kill instructions that change the stack.

* [interp] Handle dup opcode during copy propagation

* [interp] Avoid losing track of stack values for some opcodes

For some opcodes, that access stack values below the top of the stack, we were assuming they pop everything and then push the stack back, in order to prevent optimizing away instructions that pushed some of these values, since the original instruction expects those values to reside on the stack. We handle these instruction separately and keep track of the values of the stack, so we can further propagate the stack values, even though we currently can't optimize away those instructions.

* [interp] Propagate values for ctor arguments

* [interp] MINT_CASTCLASS no longer clobbers top of stack

Commit migrated from https://github.com/mono/mono/commit/d1b3ee2e3234657044bc5d0f4a9facf0fa06599c
src/mono/mono/mini/interp/mintops.def
src/mono/mono/mini/interp/transform.c