<div dir="ltr"><div>Hello,</div><div><br></div><div>I am a newbie working on a backend for a custom target, and I am getting a crash when I try to compile the code generated for an LLVM switch instruction. My backend doesn't currently support jump tables, so this happens for code generated to implement a switch as a binary tree (by SelectionDAGBuilder).</div><div><br></div><div>I am working with LLVM 3.5.</div><div><br></div><div>The following assert is failing in the AsmStreamer's EmitLabel method for my target:</div><div>assert(Symbol->isUndefined() && "Cannot define a symbol twice!");</div><div><br></div><div>Without this check, the emitted code containes several different basic blocks with the same label. Several labels are emitted for each block: The MachineBasicBlock symbol, and any symbols used to refer to that block. While the MachineBasicBlock symbols are unique in the emitted code, the symbols used to reference blocks are not.</div><div><br></div><div>An example:</div><div><br></div><div>Ltmp0:                                  // Block address taken</div><div>BB0_1:                                  // %entry</div><div>...</div><div>Ltmp0:                                  // Block address taken</div><div>BB0_2:                                  // %entry</div><div>...</div><div><br></div><div>This seems to happen because of the difference between MachineModuleInfo::getAddrLabelSymbolToEmit (the labels used to refer to the block) and MachineBasicBlock::getSymbol(), both of which are called in AsmPrinter::EmitBasicBlockStart when the block is being emitted, to print the different labels.</div><div><br></div><div>My target only supports indirect branches (through a register) so I custom lower branches to an indirect branch - this is why both (all) labels are marked as "Block address taken".</div><div><br></div><div>The reason the the same label is emitted for different basic blocks is that SelectionDAGBuilder uses the same BasicBlock insance to create several MachineBasicBlock instances. This can be seen in the following snippet from SelectionDAGBuilder::handleBTSplitSwitchCase:</div><div><br></div><div>} else {</div><div>    FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);</div><div>    CurMF->insert(BBI, FalseBB);</div><div>    WorkList.push_back(CaseRec(FalseBB,<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__CR.LT&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=Uskh8eJU6YjYu-4Zj0fOhKOURuiGz5KgkyaB040XiAI&s=b6zuKz7_jTR-4H2SnbaaJglWBaBOgk4dYpOfiuph4PQ&e=">CR.LT</a>,C,RHSR));</div><div><br></div><div>Some of the calls to CreateMachineBasicBlock get the same LLVMBB instance. I am not sure this is what is supposed to happen.</div><div><br></div><div>Below is the code I use to custom lower BR_CC nodes, in context. Perhaps I'm missing something I did wrong there:</div><div>SDValue CustomTargetLowering::LowerOperation(SDValue Op,</div><div>                                             SelectionDAG &DAG) const {</div><div><span class="" style="white-space:pre">       </span>SDValue Chain = Op->getOperand(0);</div><div><span class="" style="white-space:pre">      </span>SDValue Target = Op->getOperand(1);</div><div><span class="" style="white-space:pre">     </span>SDLoc dl(Op.getNode());</div><div><span class="" style="white-space:pre">    </span>BasicBlockSDNode *BBNode = cast<BasicBlockSDNode>(Target);</div><div><span class="" style="white-space:pre">   </span>MachineBasicBlock *MBB = BBNode->getBasicBlock();</div><div><span class="" style="white-space:pre">       </span>const BasicBlock * BB = MBB->getBasicBlock();</div><div><span class="" style="white-space:pre">   </span>MBB->setHasAddressTaken();</div><div><span class="" style="white-space:pre">      </span>SDValue BlockAddressNode = DAG.getBlockAddress(BlockAddress::get(const_cast<BasicBlock*>(BB)), EVT(MVT::i64));</div><div><span class="" style="white-space:pre">       </span>return DAG.getNode(ISD::BRIND, dl, EVT(MVT::Other), Chain, BlockAddressNode);</div><div>}</div><div><br></div><div>Another issue is that the emitted branches target the same label when they should be targetting different blocks, so a patch to make the streamer stop emitting the same label twice would not be sufficient to solve this issue.</div><div><br></div><div>Note: There is a post to the list entitled "Problem using a label to a MachineBasicBlock" (can be found here: <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-November/045748.html">http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-November/045748.html</a>) from someone who encountered the same symptom, it seems that the underlying cause is different.</div><div><br></div><div>Please help</div><div><br></div><div>Simon.</div><div><br></div><div><br></div>-- <br><div class="gmail_signature">Regards,<br>   Simon.</div>
</div>