<div dir="ltr">I think there are a number of ways that integers with undesired length result in generating redundant instructions. For a PowerPC example see this PR <a href="https://llvm.org/bugs/show_bug.cgi?id=25581" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=25581</a> (See comment 3. The problem is more general than originally reported in the PR. The fix is limited to the reported problem, not the general one). Maybe we need a transformation, somewhere shortly before ISEL, that looks at integers and make final decision about their widths.<br><br>Particularly interesting candidates for such a transformation will be GEP indices. Also, it definitely requires target-specific information. The algorithm for such a pass should have some strict complexity (running time) guarantees.<br><br>Any thoughts?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 13, 2016 at 12:50 AM, Philip Reames via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm pretty sure I was the last person to touch this code, so let me try to summarize my recollection of the situation.<br>
<br>
Originally, this logic only lived in SelectionDAG. As a result, we'd have GEPs with non-pointer sized indices flowing through the rest of the optimizer.<br>
<br>
IIRC, the specific problem which motivated the InstCombine rules came up in IndVarSimplify. Having an instruction which implicitly widens or shrinks it's operand isn't really modelled. It's not a correctness problem, but was leading to poor optimization/canonicalization decisions. There was also a concern that many other places in the optimizer had the same systemic bias. In general, having "one true way" to represent sign extension and truncation seemed likely to lead to better overall results.<span class=""><br>
<br>
On 05/18/2016 05:56 PM, Manuel Jacob via llvm-dev wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
InstCombine canonicalizes index operands (unless they are into struct types) to pointer size. The comment says: "If we are using a wider index than needed for this platform, shrink it to what we need. If narrower, sign-extend it to what we need. This explicit cast can make subsequent optimizations more obvious.".<br>
<br>
For our architecture, the canonicalization is a bit problematic. For example, our load operation can take any width and will implicitly sign-extend or truncate it. The extra explicit cast however will show up as an extra operation in the machine code. It is of course easy to eliminate the cast in a peephole optimization in the backend.<br>
</blockquote></span>
Writing the peephole or isel seems like the obvious solution here. Is there a reason that's not working out? (Beyond the one discussed below?)<span class=""><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
More interesting is the effect of this canonicalization on subsequent transformations. Which optimizations are more obvious now, as the comment says? I found examples where it enables IndVarSimplify to promote an index variable to pointer size. However that introduces truncations, which can't be optimized away by simple peephole optimizations in the backend anymore.<br>
</blockquote></span>
At least in the example you gave below, it's looking like IndVarSimplify widened something which wasn't profitable to widen. That's separate from how we represent some of the sign extensions.<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Does it make sense to add a target hook for this?<br>
<br>
-Manuel<br>
<br>
<br>
Example:<br>
<br>
define void @foo(i32 %n, i32* %a) {<br>
entry:<br>
%cmp1 = icmp slt i32 0, %n<br>
br i1 %cmp1, label %for.body, label %for.end<br>
<br>
for.body: ; preds = %for.body, %entry<br>
%i = phi i32 [ %inc, %for.body ], [ 0, %entry ]<br>
%ptr = getelementptr inbounds i32, i32* %a, i32 %i<br>
store i32 %i, i32* %ptr, align 4<br>
%inc = add nsw i32 %i, 1<br>
%cmp = icmp slt i32 %inc, %n<br>
br i1 %cmp, label %for.body, label %for.end<br>
<br>
for.end: ; preds = %for.body, %entry<br>
ret void<br>
}<br>
<br>
<br>
InstCombine introduces a sext instruction:<br>
<br>
define void @foo(i32 %n, i32* %a) {<br>
entry:<br>
%cmp1 = icmp sgt i32 %n, 0<br>
br i1 %cmp1, label %for.body, label %for.end<br>
<br>
for.body: ; preds = %for.body, %entry<br>
%i = phi i32 [ %inc, %for.body ], [ 0, %entry ]<br>
%0 = sext i32 %i to i64<br>
%ptr = getelementptr inbounds i32, i32* %a, i64 %0<br>
store i32 %i, i32* %ptr, align 4<br>
%inc = add nsw i32 %i, 1<br>
%cmp = icmp slt i32 %inc, %n<br>
br i1 %cmp, label %for.body, label %for.end<br>
<br>
for.end: ; preds = %for.body, %entry<br>
ret void<br>
}<br>
<br>
<br>
IndVarSimplify promotes %i to i64, requiring two additional truncs:<br>
<br>
define void @foo(i32 %n, i32* %a) {<br>
entry:<br>
%cmp1 = icmp sgt i32 %n, 0<br>
br i1 %cmp1, label %for.body.preheader, label %for.end<br>
<br>
for.body.preheader: ; preds = %entry<br>
br label %for.body<br>
<br>
for.body: ; preds = %for.body.preheader, %for.body<br>
%indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]<br>
%ptr = getelementptr inbounds i32, i32* %a, i64 %indvars.iv<br>
%0 = trunc i64 %indvars.iv to i32<br>
store i32 %0, i32* %ptr, align 4<br>
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1<br>
%lftr.wideiv = trunc i64 %indvars.iv.next to i32<br>
%exitcond = icmp ne i32 %lftr.wideiv, %n<br>
br i1 %exitcond, label %for.body, label %for.end.loopexit<br>
<br>
for.end.loopexit: ; preds = %for.body<br>
br label %for.end<br>
<br>
for.end: ; preds = %for.end.loopexit, %entry<br>
ret void<br>
}<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div>