<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I've developed a working back-end for a custom architecture, based on LLVM 2.6. I'm now trying to cover more of the unique features of this architecture.<div><br></div><div>To make use of one such feature, I'm trying something cunning/crazy with the stack - implementing it in a type of memory that can only be addressed via immediates.</div><div><br></div><div>I've got this mostly working. However, I came across a problem which I've been unable to work around: lowering the IR (even without any optimisations enabled) often requires the pattern:</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">   </span>i32 = FrameIndex <n></div><div><br></div></div><div>For normal memory, I was using the following instruction to match this pattern:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">    // Get the address in memory corresponding to the given frame index, saving the address</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">    // in a register.</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">    def MOV_FI : PseudoInstr<(outs GPR:$dst), (ins frameIndex:$addr),</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">                             "// $dst := frame index $addr",</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">                             [(set GPR:$dst, frameIndex:$addr)]>;</div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px; "><br></span></font></div></div><div>Which is later replaced by a MOV (output register = stack pointer + constant offset) in eliminateFrameIndex().</div><div><br></div><div>However, it isn't appropriate to do this with the proposed stack memory - it doesn't make sense to move the address into a register (where arithmetic can be performed on it), as it isn't possible to move that back to the domain of an immediate. So I conditionally disabled this instruction. But that leads to most programs failing to select the above pattern.</div><div><br></div><div>The issue is that this pattern is required even in code that doesn't conceptually seem to need it (see the example below). I couldn't figure out how to avoid this during DAG legalisation. Most often, the resulting machine assembly when the above pattern is enabled, simply stores a particular stack slot in a register, for later use in the same basic block, e.g.:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">    </span>MOV out=r4 in=SP+4</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>LOAD out=r4 addr=r4</div><div><br></div><div>despite patterns existing for LOAD with a constant offset (which is successfully used by other stack slots in the same basic block), e.g.:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">       </span>LOAD out=r3 addr=SP off=8</div><div><br></div><div>Am I missing some other patterns that would avoid this? For example, is it possible to write patterns that allow for arithmetic involving only immediates, with the result being another immediate?</div><div><br></div><div>If all else fails, I was thinking of writing a custom pass to identify and remove these. But that could be a lot of work.</div><div><br></div><div>Thanks,</div><div><br></div><div>- Mark</div><div><br></div><div><br></div><div>Example:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; "><span style="color: #c800a2">int</span> result;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; "><span style="color: #c800a2">int</span> foo(<span style="color: #c800a2">int</span> cond, <span style="color: #c800a2">int</span> a, <span style="color: #c800a2">int</span> b)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">     </span><span style="color: #c800a2">return</span> cond? a : b;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; "><span style="color: #c800a2">int</span> main()</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">      </span><span style="color: #c800a2">return</span> result = foo(<span style="color: #3a00dc">1</span>, <span style="color: #3a00dc">2</span>, <span style="color: #3a00dc">3</span>);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 137, 0); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span>// Expected: result = 2.</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">}</div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div></div><div>Resulting IR:</div><div><br></div><div><div>@result = common global i32 0, align 4            ; <i32*> [#uses=2]</div><div><br></div><div>define i32 @foo(i32 %cond, i32 %a, i32 %b) nounwind {</div><div>entry:</div><div>  %retval = alloca i32                            ; <i32*> [#uses=2]</div><div>  %cond.addr = alloca i32                         ; <i32*> [#uses=2]</div><div>  %a.addr = alloca i32                            ; <i32*> [#uses=2]</div><div>  %b.addr = alloca i32                            ; <i32*> [#uses=2]</div><div>  store i32 %cond, i32* %cond.addr</div><div>  store i32 %a, i32* %a.addr</div><div>  store i32 %b, i32* %b.addr</div><div>  %tmp = load i32* %cond.addr                     ; <i32> [#uses=1]</div><div>  %tobool = icmp ne i32 %tmp, 0                   ; <i1> [#uses=1]</div><div>  %tmp1 = load i32* %a.addr                       ; <i32> [#uses=1]</div><div>  %tmp2 = load i32* %b.addr                       ; <i32> [#uses=1]</div><div>  %cond3 = select i1 %tobool, i32 %tmp1, i32 %tmp2 ; <i32> [#uses=1]</div><div>  store i32 %cond3, i32* %retval</div><div>  %0 = load i32* %retval                          ; <i32> [#uses=1]</div><div>  ret i32 %0</div><div>}</div><div><br></div><div>define i32 @main() nounwind {</div><div>entry:</div><div>  %retval = alloca i32                            ; <i32*> [#uses=3]</div><div>  store i32 0, i32* %retval</div><div>  %call = call i32 @foo(i32 1, i32 2, i32 3)      ; <i32> [#uses=1]</div><div>  store i32 %call, i32* @result</div><div>  %tmp = load i32* @result                        ; <i32> [#uses=1]</div><div>  store i32 %tmp, i32* %retval</div><div>  %0 = load i32* %retval                          ; <i32> [#uses=1]</div><div>  ret i32 %0</div><div>}</div><div><br></div><div>(Note: for simplicity, the calling convention in use here places all arguments on the stack)</div><div><br></div></div></body></html>