<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 19, 2015, at 3:48 PM, escha via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 19, 2015, at 1:45 PM, Steve King via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">In the targets I know, shifts are</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">cheaper than divides in both speed and size.</span></div></blockquote></div><br class=""><div class="">From what I remember, udiv by power of 2 already gets turned into a shift in instcombine; the tricky case is sdiv by power of 2, which takes significantly more than one instruction. The generic implementation is this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>// Splat the sign bit into the register</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SDValue</span> SGN =</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getNode</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ISD</span>::<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">SRA</span>, DL, VT, N0,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getConstant</span>(VT.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getScalarSizeInBits</span>() - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>, DL,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getShiftAmountTy</span>(N0.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getValueType</span>())));</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""> </span>AddToWorklist<span style="" class="">(SGN.</span>getNode<span style="" class="">());</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>// Add (N0 < 0) ? abs2 - 1 : 0;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SDValue</span> SRL =</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getNode</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ISD</span>::<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">SRL</span>, DL, VT, SGN,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getConstant</span>(VT.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getScalarSizeInBits</span>() - lg2, DL,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getShiftAmountTy</span>(SGN.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getValueType</span>())));</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SDValue</span> ADD = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getNode</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ISD</span>::<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">ADD</span>, DL, VT, N0, SRL);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""> </span>AddToWorklist<span style="" class="">(SRL.</span>getNode<span style="" class="">());</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""> </span>AddToWorklist<span style="" class="">(ADD.</span>getNode<span style="" class="">()); </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// Divide by pow2</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SDValue</span> SRA = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getNode</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ISD</span>::<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">SRA</span>, DL, VT, ADD,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DAG</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getConstant</span>(lg2, DL,</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getShiftAmountTy</span>(ADD.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">getValueType</span>())));</div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">And there’s already a target-specific hook to add a custom implementation (e.g. on PowerPC):</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><div style="margin: 0px; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>// Target-specific implementation of sdiv x, pow2.</div><div style="margin: 0px;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">SDValue</span> Res = <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">BuildSDIVPow2</span>(N))</div><div style="margin: 0px;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> Res;</div></div></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>Isn’t the problem the fact that the patch makes it harder for a target to get the generic code to reach its custom hook?</div><div>Now the "cheap pow2 sdiv” is merged with the generic “cheap div” you can’t distinguish anymore.</div><div><br class=""></div><div>— </div><div>Mehdi</div><div><br class=""></div></div></body></html>