through my work on writing a backend, I’ve noticed that when I need to replace a pattern in the SelectionDAG with a different pattern the correct way to do it seems to be through the LowerOperation hook in the TargetLowering class.
I was wondering if there is some way to achieve the same thing using tablegen, thank you!
this question conflates a whole bunch of concepts at the heart of how SDAG, lowering, ISEL, etc. works. As such, it isn’t likely that you’ll get a particularly satisfying answer. Well formulated, targeted questions are likely to be answered in detail. Very general and vague questions such as this are not likely to receive a lot of attention from the community.
That being said, I think the first step would be for you to familiarize yourself with how instruction selection works. Eli has a couple of excellent blog posts regarding this:
I encourage you to look through those along with LLVM’s own documentation for a target-independent code generator (http://llvm.org/docs/CodeGenerator.html). The right approach for what you’re after should be quite clear once you have a thorough understanding of type/operation legalization, DAG combines and SDAG ISEL.
A bit more information about the actual question you asked…
LowerOperation function is called during the lowering process for operations that are marked as ‘Custom’ through ‘setOperationAction’. If an operation is marked
Expand, the target-independent code will expand it to other operations that are likely to be available on most targets. And of course, if it is marked
Legal, it goes straight to instruction selection unchanged.
Furthermore, the SDAG provides DAG Combines. These are meant to replace patterns/nodes that satisfy certain conditions with other patterns/nodes that are more likely to produce efficient code. Similarly to lowering, there’s the target-independent version (in lib/CodeGen/SelectionDAG/DAGCombiner.cpp) and the target-specific versions (in your
PerformDAGCombine()). The target-independent combines always run and you can tell the SDAG you want to try your target-specific combines for specific nodes by calling
setTargetDAGCombine() for those nodes.
In effect, all of these processes replace “patterns” with other “patterns” (more specifically, [groups of] nodes with other [groups of] nodes). So can you replace a pattern with a different pattern through the .td files? Of course - all of ISEL is essentially replacing [Target]ISD nodes with machine SD nodes. The only limitation is that after ISEL, the DAG should only contain machine SD nodes, so if you want special handling post-isel, you should probably use pseudo-instructions.
Hope this helps.