[PATCH] D25152: [RFC/WIP] CodeGen: Initial support for references to absolute symbols in immediate operands.
Peter Collingbourne via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 30 18:23:00 PDT 2016
pcc created this revision.
pcc added reviewers: chandlerc, eugenis, mehdi_amini, sanjoy.
pcc added subscribers: krasin, llvm-commits.
Our support for references to absolute symbols is not very good. The symbol
will be resolved accurately in non-PIC code, but suboptimally: the symbol
reference cannot currently appear as the immediate operand of an instruction,
and the code generator cannot make any assumptions about the value of the
symbol (so for example, it could not use a R_X86_64_8 relocation if the
value is known to be in the range 0..255).
In PIC mode, if the reference is not known to be DSO-local, the value is loaded
from the GOT (or a synthetic GOT entry), which again means suboptimal code. If
the reference is known to be DSO-local, the symbol will be referenced with a PC
relative relocation and therefore cannot be resolved properly to an absolute
value (c.f. https://reviews.llvm.org/D19844). The latter case in particular would seem to indicate
that a representational change is required for correctness to distinguish
references to absolute symbols from references to regular symbols. The
specific change I have in mind is to add an "absolute" flag to GlobalObject.
To allow the code generator to narrow absolute global references appearing
as immediates, we would allow !range metadata on GlobalObjects. This would
be similar to existing !range metadata, but it would apply to the "address"
of the attached GlobalObject, rather than any value loaded from it. See
for example the test case included with this patch, which involves a testb
instruction with an absolute immediate operand.
This patch implements part of this change. Rather than introducing the
"absolute" flag, it just checks for !range metadata on GlobalObjects
(switching to the correct representation is a TODO). At the SDAG level,
if a global reference has !range metadata and the target/object format
supports the required relocations (ELF only for now), we combine it into a
special ConstantSDNode which holds a reference to the symbol. Like any other
ConstantSDNode, this ConstantSDNode can appear as an immediate operand. This
means that we now have ConstantSDNodes which are not in fact constant integers,
but to a certain extent this seems like the least worst approach. I considered
other approaches before arriving at this one:
- Introduce a new opcode for absolute symbol constants. This intuitively seemed like the least risky approach, as individual instructions could "opt in" to the new absolute symbol references. However, this seems hard to fit into the existing SDAG pattern matching engine, as the engine expects each "variable" to have a specific opcode. I tried adding special support for "either of the two constant opcodes" to the matcher, but I could not see a good way to do it without making fundamental changes to how patterns are matched.
- Use the ISD::Constant opcode for absolute symbol constants, but introduce a separate class for them. This also seemed problematic, as there is a strong assumption (both in existing SDAG code and in generated code) of a many-to-one mapping from opcodes to classes.
In the end I decided that because
- this ConstantSDNode is morally similar in certain respects to a regular constant that can appear as an immediate, and SDAG already has an opcode which in principle inhibits looking at the constant value (i.e. TargetConstant)
- we can consider documenting absolute symbol references as an experimental feature
- we can avoid exposing this feature to C and so we have control over how it is used
it may be reasonable to proceed even in light of potential type confusion
problems.
https://reviews.llvm.org/D25152
Files:
llvm/include/llvm/CodeGen/SelectionDAG.h
llvm/include/llvm/CodeGen/SelectionDAGNodes.h
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/absolute-constant.ll
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25152.73184.patch
Type: text/x-patch
Size: 15197 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161001/29749c87/attachment.bin>
More information about the llvm-commits
mailing list