[llvm-dev] Accessing the associated LLVM IR Instruction for an SDNode used in instruction selection (back end)

Alex Susu via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 18 18:09:58 PST 2016


   Hello.
     I managed to solve the problem with keeping track of the SDNode-LLVM IR associations 
created in the DAG combiner phase, etc.

     First I noticed that SelectionDAGISel::CodeGenAndEmitDAG() is calling all the passes 
of instruction selection - described, for example, in the book on LLVM of Cardoso-Lopes et 
al (https://www.amazon.com/Getting-Started-LLVM-Core-Libraries/dp/1782166920 ), page 150; 
note that scheduling and register allocation are called somewhere else.

     So I created another DenseMap object (I discussed about it in the previous emails 
below) in the SelectionDAG class and some methods to update it, called 
SelectionDAG::SetNodeMap() and SelectionDAG::UpdateNodeMapSDValue().
     Then in the method SelectionDAGISel::CodeGenAndEmitDAG() I call 
CurDAG->SetNodeMap(&crtNodeMap) and in DAGCombiner::Run() I call 
DAG.UpdateNodeMapSDValue() method.

     (Note also that the following 2 classes take care of the type and vector legalization:
	http://llvm.org/docs/doxygen/html/LegalizeVectorTypes_8cpp_source.html,
	http://llvm.org/docs/doxygen/html/LegalizeTypes_8cpp_source.html)

   Best regards,
     Alex

On 11/11/2016 3:59 PM, Alex Susu wrote:
>   Hello.
>     I would like to ask for your advice in the following problem I have: some of the
> SDNodes are created in the phases following the generation of the SelectionDAG, namely DAG
> combiner (DAGCombiner.cpp), type (LegalizeTypes.h) and DAG operation legalization. While
> the SelectionDAGBuilder class already has the map:
>       DenseMap<const Value*, SDValue> NodeMap;
>    from LLVM IR Value to SDValue/SDNode, the other classes do NOT have such maps, so I
> need to create them myself and merge them in a map containing all associations.
>
>     Do you know a better way to retrieve the associated LLVM IR Instruction for an already
> created SDNode?
>
>   Thank you,
>     Alex
>
> On 11/9/2016 2:55 PM, Alex Susu wrote:
>>   Hello.
>>     I come back to this thread to add that moving SDB->clear() at the beginning of the
>> method messes up later at instruction selection with an error message like:
>>       lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1466: llvm::SDValue
>> {anonymous}::DAGCombiner::combine(llvm::SDNode*): Assertion `N->getOpcode() !=
>> ISD::DELETED_NODE && "Node was deleted but visit returned NULL!"' failed.
>>     So, I added in class SelectionDAGISel another DenseMap crtNodeMap, which I copy in
>> SelectionDAGISel::SelectBasicBlock() before the original SDB->clear() is given.
>>
>>    Doing this no longer gives me any errors.
>>
>>   Best regards,
>>     Alex
>>
>> On 10/24/2016 1:11 PM, Alex Susu wrote:
>>>   Hello.
>>>     Quentin, retrieving the LLVM IR instruction from which an SDNode originates is useful
>>> during the instruction selection phase. For example, I need to recover the LLVM IR
>>> variable which is used to fill an entire vector with the ISD::BUILD_VECTOR
>>> target-independent SDNode  .
>>>     From the recovered LLVM IR variable I can walk on the use-def-chains in order to get
>>> the most complete definition of this variable (in terms of the input program variables),
>>> which is something I can't do at the level of SelectionDAGs (because, for example, actual
>>> function arguments don't appear in the DAG).
>>>
>>>   Best regards,
>>>     Alex
>>>
>>>
>>> On 10/22/2016 12:29 AM, Quentin Colombet wrote:
>>>> Hi Alex,
>>>>
>>>> Out of curiosity, what is your use case for that?
>>>>
>>>> Generally speaking I would recommend against doing that. When the SDBuilder is done, I
>>>> would expect the SDNodes to not query anything outside of the SD layer. We are not here
>>>> now, though.
>>>>
>>>> Cheers, -Quentin
>>>>> On Oct 21, 2016, at 4:57 AM, Alex Susu via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>>>>>
>>>>> Hello. I would like to access the LLVM IR Instruction from which an SDNode (from
>>>>> SelectionDAG) originates. For this I have modified: -
>>>>> llvm/lib/CodeGen/SelectionDAGISel.cpp, SelectionDAGISel::SelectBasicBlock(), namely I
>>>>> put SDB->clear() at the beginning of the method in order to avoid clearing NodeMap
>>>>> after creating the initial SelectionDAG from LLVM IR, since I want to access it after
>>>>> that; - llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h - added an accessor for
>>>>> the private NodeMap object relating LLVM IR Instructions to generated SDNodes:
>>>>> public: // Alex: added a getter for NodeMap DenseMap<const Value*, SDValue>
>>>>> &getNodeMap() { return NodeMap; }
>>>>>
>>>>> A small problem is that it seems that when I access the NodeMap in
>>>>> [Target]DAGToDAGISel::Select(SDNode *Node) method (part of subclass of
>>>>> SelectionDAGISel), the NodeMap contains quite a few SDNodes tagged as <<Deleted
>>>>> Node!>> (for the nodes that were already selected). But the current SDNode, given as
>>>>> parameter to Select() is valid and we can search for it in NodeMap in order to find
>>>>> the LLVM IR Instruction (llvm::Value, more exactly) that generated it. As a side
>>>>> question, is there a better modality to find the key associated to a value in a
>>>>> DenseMap object than to iterate through all of DenseMap's elements (I could not find
>>>>> anything better at
>>>>> http://llvm.org/docs/doxygen/html/classllvm_1_1DenseMapBase.html)?
>>>>>
>>>>> Could you please tell me if there is a better modality to retrieve the LLVM IR
>>>>> Instruction from which an SDNode originates?
>>>>>
>>>>> Thank you, Alex _______________________________________________ LLVM Developers
>>>>> mailing list llvm-dev at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>>>
>>>>


More information about the llvm-dev mailing list