<div dir="ltr">Hello,<div><br></div><div>here is a proposed patch to pass down BinaryOperator the NoUnsignedWrap/NoSignedWrap and IsExact flags down to the SDNodes so that backends can use them for additional optimizations.</div>
<div><br></div><div>The core of the patch is using the SubclassData field of SDNode for BinarySDNode to store the flags information.</div><div>SelectionDAGBuilder will then set the correct flag information the moment the Binary instruction is visited during construction.</div>
<div><br></div><div>In doing this I also refactored a little bit the getNode() method accepting two operands, extracting the main constant folding logic into a separate static function.</div><div><br></div><div>The patch implementing this is the file "flag_nodes.patch" attached.</div>
<div><br></div><div>In addition I implemented an optimization where an unneeded "test instruction" in X86 where emitted that uses this new feature.</div><div><br></div><div>This optimization is contained in the "x86_opt_wrap.patch" attached.</div>
<div><br></div><div>I also added a test case that exercises the optimization in the "factorial_test.patch"</div><div><br></div><div>The patch makes this snippet of code:</div><div><br></div><div>LBB0_1:                                 ## %while.body<br>
</div><div><div>                                               ## =>This Inner Loop Header: Depth=1</div><div>         imulq   %rdi, %rax</div><div>         decq    %rdi</div><div>         testq    %rdi, %rdi</div><div>
         jg      LBB0_1</div></div><div><br></div><div>become this:</div><div><br></div><div><div>LBB0_1:                                 ## %while.body<br></div><div><div>                                               ## =>This Inner Loop Header: Depth=1</div>
<div>         imulq   %rdi, %rax</div><div>         decq    %rdi</div><div>         jg      LBB0_1</div></div></div><div><br></div><div>if the NSW flag is set on the SDNode of that represents the DEC instruction (presumably an ADD or a SUB).</div>
<div><br></div><div>If somebody could review this it would be awesome :)</div><div><br></div><div>Cheers,</div><div>Marcello</div></div>