<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><DIV><SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; ">Hi Andy,<BR class="Apple-interchange-newline"></SPAN> </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>I've been working through a backend for the first time over the last several weeks, so I thought I'd share what insights I have into the subjects you mention.</DIV><BR><DIV><DIV>On Mar 22, 2007, at 9:38 AM, Andy Nisbet wrote:</DIV><BR class="Apple-interchange-newline"><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">I have spent some time looking at the PPC and SPARC backends, but obviously<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">these are much more complicated than what I require to implement.<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Consequently, I am not correctly grasping the interactions between<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ARCHInstrInfo.td and ARCHDAGToDAGISel.cpp I did manage to hack something<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">together based on a copy of SPARC (with a SABRE namespace etc) but the<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">instruction selection was incorrect and I obtained a "Cannot yet<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">select:0x..." assertion failure from SABREDAGToDAGIsel::SelectCode when I<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">attempted a</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">llc -march sabre helloworld.bc -o helloworld.s</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Can anyone offer any guidance on how to proceed with debugging instruction<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">selection issues? <BR></DIV></BLOCKQUOTE><DIV><BR class="khtml-block-placeholder"></DIV><DIV>The *InstrInfo.td will be used to generate a file called *GenDAGIsel.inc in the build directory under lib/Target that you're working on. This is a C++ file that is included into the DAGIsel.cpp file and it contains the instruction selection rules specified in the *InstrInfo.td.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>When instruction selection is performed control flow first enters the Select() method of your instruction selector object (usually named something like *DAGToDAGIsel.cpp), if that method doesn't select the dag it calls another method, SelectCode(), which calls into the tblgen generated instruction selection code that is in the .inc file.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>The specific assert you mention is in the tblgen generated .inc file when it fails to find a pattern to match your dag. My suggestion would be to step through the Select() function and then into the .inc file to see where you instruction selection may be going awry.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><BR><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">WRT defining the instructions themselves: am I right in thinking that it is<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">sensible (for instruction selection) to represent the instruction set as a<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">collection of instructions targetting register register and register<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">immediate, so for example I would create defs for</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ADDrr to match ADD %a,%b</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ADDri to match ADD %a, immediate</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">I have used multiclass to achieve this. Previously I was attempting to<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">match the opcode %a,%b,immediate general form.</DIV></BLOCKQUOTE><DIV><BR class="khtml-block-placeholder"></DIV><DIV>This seems the sensible way to proceed, to me.</DIV><BR><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Clearly I also need a way to load a 32 bit constant value into a register<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">in order to be able to address<SPAN class="Apple-converted-space">  </SPAN>more than 64K of memory. I know the PPC<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">does something similar ...</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">So for example for SABRE<SPAN class="Apple-converted-space">  </SPAN>this instruction output would perform the<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">necessary ...</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">MOVri %a, HI16(32 bit constant)</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">LSHri %a,16</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ORri %a, LO16(same 32 bit constant)</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">LD %d, %a // ie load the contents of the memory at the address stored in %a<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">into register %d</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">where the HI/LO16 are performed at code generation by LLVM. I'm a little<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">confused as to how to specify this as a pattern in tablegen syntax, even<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">with the PPC example.</DIV></BLOCKQUOTE><BR></DIV><DIV>Code generating immediates like this for global variables addresses and constant pool addresses involves an interaction between instruction selection and the target lowering implementation. Generating numeric immediates only requires some patterns in the instruction selector. The key is that the global addresses and the numeric immediates follow two separate but similar paths to being code gen'd.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Numeric Immediates:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>You'll need NodeXForm's in your InstrInfo.td that implement the LO16/HI16 part, like:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">def LO16 : SDNodeXForm<imm, [{</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">  </SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">// Transformation function: get the low 16 bits.</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">  </SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">return CurDAG->getTargetConstant((unsigned)N->getValue() & 0xFFFF, MVT::i32);</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">}]>;</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">At this point numeric immediates are simply going to be a pattern like:</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><BR class="khtml-block-placeholder"></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="2"><SPAN class="Apple-style-span" style="font-size: 10px;">def : Pat<(i32 imm:$imm), (ORri (LSHri (MOVri RZero, (HI16 imm:$imm)), 16), (LO16 imm:$imm))>;</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Global/Constant Pool Addresses:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>I don't completely understand why it's not possible to simply instruction select these as one does with integer immediates, but all the targets I've looked at follow a similar approach that uses customer lowering of these values using two target specific dag nodes. If you look at the Sparc target lowering call (LowerOperation()) for ISD::GlobalAddress you'll see how it gets split into a dag that includes some target specific nodes (SPISD::Hi/Lo).</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Then in the InstrInfo.td look for the the SDNode declarations for these target specific nodes and then the selection patterns that match them. It's pretty similar to the pattern for integer immediates.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Hope this helps.</DIV><DIV><DIV><DIV>--</DIV><DIV>Christopher Lamb</DIV><DIV><A href="mailto:christopher.lamb@gmail.com">christopher.lamb@gmail.com</A></DIV></DIV><BR class="khtml-block-placeholder"></DIV><BR></BODY></HTML>