'Difference between XXXISelDAGToDAG and XXXISelLowering in llvm
I am currently exploring custom backend creation for a target. In the official docs of llvm https://llvm.org/docs/WritingAnLLVMBackend.html#basic-steps , it is mentioned that for Dag lowering we need to modify [Target]ISelDAGToDAG.cpp and [Target]ISelLowering.cpp. I explored few llvm targets. But unable to grasp the difference.
Solution 1:[1]
Interesting point because learning the backend code structure is not trivial and each target is implemented in a different subfolder under the Target
with several .cpp, .h, and .td files.
Since following is mentioned in your provided link:
"Also write code in XXXISelLowering.cpp to replace or remove operations and data types that are not supported natively in a SelectionDAG."
Let's take this forward. I would answer with a bigger picture showing basic steps
prior to instruction selection, starting with the LLVM IR
step that is to the top-left and then citing Getting Started with LLVM
Core Libraries: 
First, a
SelectionDAGBuilderinstance (seeSelectionDAGISel.cppfor details) visits every function and creates aSelectionDAGobject for each basic block. During this process, some special IR instructions such ascallandretalready need targetspecific idioms—for instance, how to pass call arguments and how to return from a function—to be transformed intoSelectionDAGnodes. To solve this issue, the algorithms in theTargetLoweringclass are used for the first time. This class is an abstract interface that each target must implement, but also has plenty of common functionality used throughout all backends.
To implement this abstract interface, each target declares a
TargetLoweringsubclass named<Target>TargetLowering. Each target also overloads methods that implement how a specific target-independent, high-level node should be lowered to a level closer to the one of this machine. As expected, only a small subset of nodes must be lowered in this way, while the majority of the others are matched and replaced at instruction selection. For instance, inSelectionDAGfromsum. bc, theX86TargetLowering::LowerReturn()method (seelib/Target/X86/ X86ISelLowering.cpp) is used to lower the IRretinstruction. While doing this, it generates theX86ISD::RET_FLAGnode, which copies the function result toEAX—a target-specific way to handle the function return.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BZKN |
