<div dir="ltr"><div>Hi,</div><div><br></div><div>I recently worked on doing constant <span style="font-size:13px">canonicalisation in DAGCombine level to make as more folding as possible. </span></div><div><span style="font-size:13px">And I found the behavior of "visitShiftByConstant" is same with what I have done, just only it only be enabled under</span></div><div><span style="font-size:13px"> a shift context. But I think maybe we could expand it to as more case as possible.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"> The original issuse of it:</span></div><div><span style="font-size:13px"><br></span></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-size:13px">For code as below:</span></div><div><span style="font-size:13px"><br></span></div><div><div>unsigned array[4];</div></div><div><div>unsigned foo(unsigned long x) {</div></div><div><div>          return array[(x>>2)&3ul];</div></div><div><br></div><div>sequence before this <span style="font-size:13px">canonicalisation (ARM):</span></div><div><span style="font-size:13px"><br></span></div><div><div>foo:</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>.fnstart</div></div><div><div>@ BB#0:                                 @ %entry</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>lsrs<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r0, r0, #2</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>movs<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r1, #3</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ands<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r1, r0</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>lsls<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r0, r1, #2</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ldr<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r1, .LCPI0_0</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>ldr<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r0, [r1, r0]</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>bx<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>lr</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>.p2align<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>2</div></div><div><br></div><div>sequence after this <span style="font-size:13px">canonicalisation:</span></div><div><div>foo:</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>.fnstart</div></div><div><div>@ BB#0:                                 @ %entry</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>movs<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r1, #12</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>ands<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r1, r0</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ldr<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r0, .LCPI0_0</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>ldr<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r0, [r0, r1]</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>bx<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>lr</div></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>.p2align<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>2</div></div></blockquote><div><div style="font-size:13px"><br></div></div><div><div>This canonicalisation makes shift folding possible. But I wonder if only shift context could benifit from this.When I worked on </div><div>CoreMark optimization recently, I found a case similar to below:</div></div><div><br></div><div><div style="font-size:13px">define i32 @test(i32 %v) {</div><div style="font-size:13px">entry:</div><div style="font-size:13px">  %a = and i32 %v, 65534</div><div style="font-size:13px">  %b = lshr i32 %a, 1</div><div style="font-size:13px">  %c = and i32 %v, 65535</div><div style="font-size:13px">  %d = lshr i32 %c, 1</div><div style="font-size:13px">  %e = add i32 %b, %d</div><div style="font-size:13px">  ret i32 %e</div><div style="font-size:13px">}</div></div><div style="font-size:13px"><br></div><div style="font-size:13px">It would be profitable as well if we could enable the <span style="font-size:small">canonicalisation on it.</span></div><div style="font-size:13px"><span style="font-size:small">sequence before this </span>canonicalisation (ARM):</div><div><div>test:</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>.fnstart</div><div>@ BB#0:                                 @ %entry</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>movw<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r1, #65534</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>and<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r1, r0, r1</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>ubfx<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r0, r0, #1, #15</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>add<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r0, r0, r1, lsr #1</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>bx<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>lr</div><div style="font-size:13px"><br></div></div><div style="font-size:13px"><span style="font-size:small">sequence after this </span>canonicalisation:<br></div><div><div>test:</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>.fnstart</div><div>@ BB#0:                                 @ %entry</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>ubfx<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>r0, r0, #1, #15</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>add<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>r0, r0, r0</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>bx<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>lr</div><div style="font-size:13px"><br></div></div><div style="font-size:13px">But when I tried to expand the <span style="font-size:small">condition to this case, there are lots of various regression fails.</span></div><div style="font-size:13px"><span style="font-size:small">The expanding may prevent some other possible folding or something. </span>Maybe I missed something or</div><div style="font-size:13px">the expanding is not reasonable at all.but I didn't figure out yet.</div><div style="font-size:13px">Any suggestions? Any inspiration is highly appreciated.</div><div style="font-size:13px"><br></div><div style="font-size:13px">And there maybe some other cases could be included also. If you have any findings, please let</div><div style="font-size:13px">me know as well.</div><div style="font-size:13px"><br></div><div><span style="font-size:13px">Thank you very much! :)</span></div><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Best Regards,<br></div>Jojo<br></div></div></div>
</div>