<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Feb 18, 2014, at 11:32 PM, Shemer, Anat <<a href="mailto:anat.shemer@intel.com">anat.shemer@intel.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div 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;"><font face="Verdana" size="2"><span style="font-size: 10pt;"><div><font color="purple">Hi Andrew,</font></div><div><font color="purple">The issue below refers to LSR, so I’ll appreciate your feedback. It also refers to instruction combining and might impact backends other than X86, so if you know of others that might be interested you are more than welcome to add them.</font></div><div><font color="purple">Thanks, Anat</font></div></span></font></div></blockquote><div><br></div><div>I'm not sure how you ended up with that IR. You have a 64-bit index that is 32-bit sign-extended within the loop. That's not something I've seen. Normally you have a pure 64-bit index or a 32-bit sign extended index. In this case, the sign extend would be hoisted out of the loop.</div><div><br></div><div>That said, LSR has a lot of trouble with sext/zext/trunc.</div><div><br></div><div>In this case, SCEV is getting it right:</div><div><br></div><div>%idxprom = ashr exact i64 %sext, 32</div><div> --> (sext i32 {0,+,1}<%L_entry> to i64) Exits: (sext i32 (trunc i64 (-1 + %iNumElements) to i32) to i64)</div><div><br></div><div>The presense of the shl does lose the nuw/nsw flags, but I don't think that's your problem.</div><div><br></div><div>I had to debug this a bit to understand the issue, hence my delay in answering. Some possibilities:</div><div><br></div><div>(1) Generate IR that is normally generated for C code, or change the source to match conventional patterns.</div><div><br></div><div>(2) -mllvm -disable-lsr. You'll still have two increments, but no crazy shifting. You could rewrite the code with a single counter/index and get perfect codegen.</div><div><br></div><div>(3) Teach IV-Users to see through the ashr instruction. Currently it thinks the ashr is not interesting so tells LSR to generate an expression for its operands. That's why you get the weird shifted IV. This is probably the right fix, but I can't say for sure without trying it.</div><div><br></div><div>(4) Don't let instcombine bitwise expand the trunc/sext when it feeds a gep. I'm not a big fan of the bitwise expansion but realize it was done for a reason. I don't know all of the optimizations that it exposes without doing some investigation. Before you could do this you would need to send a specific proposal titled "InstCombine should not expand trunc/sext into shl/ashr" and get the ok from other people on the list. You would also need to do performance analysis of llvm test-suite and get someone to do the same for arm.</div><div><br></div><div>-Andy</div><div><br></div><blockquote type="cite"><div 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;"><font face="Verdana" size="2"><span style="font-size: 10pt;"><div><font face="Calibri" size="2" color="purple"><span style="font-size: 11pt;"> </span></font></div><div><font face="Calibri" size="2" color="purple"><span style="font-size: 11pt;"> </span></font></div><div><font face="Tahoma">_____________________________________________<br><b>From:</b><span class="Apple-converted-space"> </span>Shemer, Anat<span class="Apple-converted-space"> </span><br><b>Sent:</b><span class="Apple-converted-space"> </span>Tuesday, February 18, 2014 15:07<br><b>To:</b><span class="Apple-converted-space"> </span>'<a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a>'<br><b>Subject:</b><span class="Apple-converted-space"> </span>better code for IV</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>Hi,</div><div> </div><div>I will be glad if you can give me some advice on how to make LLVM generate better code for IV in a simple loop case. There is IR sample below, I know of one way to do it, and I’m looking for advice on another way.</div><div> </div><div>When compiling a simple C loop (c[i]=a[i]+b[i], a, b, c are float*), the IR starts as follows:</div><div> </div><div><font face="Courier New"> define void @ArrayAdd1(float* nocapture %a, float* nocapture %b, float* nocapture %c, i64 %iNumElements) {</font></div><div><font face="Courier New"> Entry:</font></div><div><font face="Courier New"> br label %L_pre_head</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_pre_head: ; preds = %Entry</font></div><div><font face="Courier New"> br label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_entry: ; preds = %L_entry, %L_pre_head</font></div><div><font face="Courier New"> %L_ind_var = phi i64 [ 0, %L_pre_head ], [ %L_inc_ind_var, %L_entry ]</font></div><div><font face="Courier New"> %L_tid = phi i64 [ 0, %L_pre_head ], [ %L_inc_tid, %L_entry ]</font></div><div><font face="Courier New"> %trunc = trunc i64 %L_tid to i32</font></div><div><font face="Courier New"> %idxprom = sext i32 %trunc to i64</font></div><div><font face="Courier New"> %arrayidx = getelementptr inbounds float* %a, i64 %idxprom</font></div><div><font face="Courier New"> %0 = load float* %arrayidx, align 4</font></div><div><font face="Courier New"> %arrayidx2 = getelementptr inbounds float* %b, i64 %idxprom</font></div><div><font face="Courier New"> %1 = load float* %arrayidx2, align 4</font></div><div><font face="Courier New"> %add = fadd float %0, %1</font></div><div><font face="Courier New"> %arrayidx4 = getelementptr inbounds float* %c, i64 %idxprom</font></div><div><font face="Courier New"> store float %add, float* %arrayidx4, align 4</font></div><div><font face="Courier New"> %L_inc_ind_var = add nuw nsw i64 %L_ind_var, 1</font></div><div><font face="Courier New"> %L_cmp.to.max = icmp eq i64 %L_inc_ind_var, %iNumElements</font></div><div><font face="Courier New"> %L_inc_tid = add nuw nsw i64 %L_tid, 1</font></div><div><font face="Courier New"> br i1 %L_cmp.to.max, label %L_exit, label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_exit: ; preds = %L_entry</font></div><div><font face="Courier New"> br label %2</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> ; <label>:2 ; preds = %L_exit</font></div><div><font face="Courier New"> ret void</font></div><div><font face="Courier New"> }</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>And after going through all passes before code generation it becomes:</div><div> </div><div><font face="Courier New"> define void @ArrayAdd1(float * nocapture %a, float * nocapture %b, float * nocapture %c, i64 %iNumElements)</font></div><div><font face="Courier New"> {</font></div><div><font face="Courier New"> Entry:</font></div><div><font face="Courier New"> br label %L_pre_head</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_pre_head: ; preds = %Entry</font></div><div><font face="Courier New"> br label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_entry: ; preds = %L_entry, %L_pre_head</font></div><div><font face="Courier New"> %L_ind_var = phi i64 [ 0, %L_pre_head ], [ %L_inc_ind_var, %L_entry ]</font></div><div><font face="Courier New"> %L_tid = phi i64 [ 0, %L_pre_head ], [ %L_inc_tid, %L_entry ]</font></div><div><font face="Courier New"> %sext = shl i64 %L_tid, 32</font></div><div><font face="Courier New"> %idxprom = ashr exact i64 %sext, 32</font></div><div><font face="Courier New"> %arrayidx = getelementptr inbounds float * %a, i64 %idxprom</font></div><div><font face="Courier New"> %0 = load float * %arrayidx, align 4</font></div><div><font face="Courier New"> %arrayidx2 = getelementptr inbounds float * %b, i64 %idxprom</font></div><div><font face="Courier New"> %1 = load float * %arrayidx2, align 4</font></div><div><font face="Courier New"> %add = fadd float %0, %1</font></div><div><font face="Courier New"> %arrayidx4 = getelementptr inbounds float * %c, i64 %idxprom</font></div><div><font face="Courier New"> store float %add, float * %arrayidx4, align 4</font></div><div><font face="Courier New"> %L_inc_ind_var = add nuw nsw i64 %L_ind_var, 1</font></div><div><font face="Courier New"> %L_cmp.to.max = icmp eq i64 %L_inc_ind_var, %iNumElements</font></div><div><font face="Courier New"> %L_inc_tid = add nuw nsw i64 %L_tid, 1</font></div><div><font face="Courier New"> br i1 %L_cmp.to.max, label %L_exit, label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_exit: ; preds = %L_entry</font></div><div><font face="Courier New"> br label %2</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> ; <label>:2 ; preds = %L_exit</font></div><div><font face="Courier New"> ret void</font></div><div><font face="Courier New"> }</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>On the way to code gen the IR goes through some additional passes, among them the LSR in LoopStrengthReduce.cpp. This pass promotes the shl operation out of the loop. As a result the generated code is awkward and uses 4 registers for IV management instead of 2 (see transformed IR and result asm below).</div><div> </div><div>One way to deal with this is to prevent the transformation of</div><div><font face="Courier New"> %trunc = trunc i64 %L_tid to i32</font></div><div><font face="Courier New"> %idxprom = sext i32 %trunc to i64</font></div><div>to</div><div><font face="Courier New"> %sext = shl i64 %L_tid, 32</font></div><div><font face="Courier New"> %idxprom = ashr exact i64 %sext, 32</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>Sext(trunk()) remains nice until code gen and one asm instruction of the sort “move %eax, %rax” is generated, as I want (see asm sample at the end of this message). I implemented this locally in InstCombineCasts.cpp, in InstCombiner::visitSExt(). The code looks for the specific pattern of sext32to64(trunk64to32()) and doesn’t let it become ashr(shl()).</div><div> </div><div>Another way that I can think of, is to keep ashr32(shl32()) together and then it’s possible to replace both instructions with one asm instruction (a la “move %eax, %rax”) and avoid an additional register for monitoring end of loop. I’m not sure what is the right point in LSRInstance::LSRInstance to avoid this optimization.</div><div> </div><div>Do you think that the first way is ok? If you think that this should be done in the second way or a third way I will appreciate your guidance.</div><div> </div><div>Thanks, Anat</div><div> </div><div> </div><div>The IR after LSR:</div><div><font face="Courier New"> define void @ArrayAdd1(float* nocapture %a, float* nocapture %b, float* nocapture %c, i64 %iNumElements) {</font></div><div><font face="Courier New"> Entry:</font></div><div><font face="Courier New"> br label %L_pre_head</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_pre_head: ; preds = %Entry</font></div><div><font face="Courier New"> br label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_entry: ; preds = %L_entry, %L_pre_head</font></div><div><font face="Courier New"> %lsr.iv1 = phi i64 [ %lsr.iv.next2, %L_entry ], [ 0, %L_pre_head ]</font></div><div><font face="Courier New"> %lsr.iv = phi i64 [ %lsr.iv.next, %L_entry ], [ %iNumElements, %L_pre_head ]</font></div><div><font face="Courier New"> %idxprom = ashr exact i64 %lsr.iv1, 32</font></div><div><font face="Courier New"> %arrayidx = getelementptr inbounds float* %a, i64 %idxprom</font></div><div><font face="Courier New"> %0 = load float* %arrayidx, align 4</font></div><div><font face="Courier New"> %arrayidx2 = getelementptr inbounds float* %b, i64 %idxprom</font></div><div><font face="Courier New"> %1 = load float* %arrayidx2, align 4</font></div><div><font face="Courier New"> %add = fadd float %0, %1</font></div><div><font face="Courier New"> %arrayidx4 = getelementptr inbounds float* %c, i64 %idxprom</font></div><div><font face="Courier New"> store float %add, float* %arrayidx4, align 4</font></div><div><font face="Courier New"> %lsr.iv.next = add i64 %lsr.iv, -1</font></div><div><font face="Courier New"> %lsr.iv.next2 = add i64 %lsr.iv1, 4294967296</font></div><div><font face="Courier New"> %L_cmp.to.max = icmp eq i64 %lsr.iv.next, 0</font></div><div><font face="Courier New"> br i1 %L_cmp.to.max, label %L_exit, label %L_entry</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> L_exit: ; preds = %L_entry</font></div><div><font face="Courier New"> br label %2</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> ; <label>:2 ; preds = %L_exit</font></div><div><font face="Courier New"> ret void</font></div><div><font face="Courier New"> }</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>Asm code:</div><div><font face="Courier New"> ArrayAdd1: # @ArrayAdd1</font></div><div><font face="Courier New"> .cfi_startproc</font></div><div><font face="Courier New"> # BB#0: # %Entry</font></div><div><font face="Courier New"> xorl %r9d, %r9d</font></div><div><font face="Courier New"> movabsq $4294967296, %r8 # imm = 0x100000000</font></div><div><font face="Courier New"> .align 16, 0x90</font></div><div><font face="Courier New"> .LBB0_1: # %L_entry</font></div><div><font face="Courier New"> # =>This Inner Loop Header: Depth=1</font></div><div><font face="Courier New"> movq %r9, %rax</font></div><div><font face="Courier New"> sarq $32, %rax</font></div><div><font face="Courier New"> movss (%rdi,%rax,4), %xmm0</font></div><div><font face="Courier New"> addss (%rsi,%rax,4), %xmm0</font></div><div><font face="Courier New"> movss %xmm0, (%rdx,%rax,4)</font></div><div><font face="Courier New"> addq %r8, %r9</font></div><div><font face="Courier New"> decq %rcx</font></div><div><font face="Courier New"> jne .LBB0_1</font></div><div><font face="Courier New"> # BB#2:</font></div><div><font face="Courier New"> Ret</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div>This is what I want to get:</div><div><font face="Courier New"> ArrayAdd2: # @ArrayAdd2</font></div><div><font face="Courier New"> .cfi_startproc</font></div><div><font face="Courier New"> # BB#0: # %Entry</font></div><div><font face="Courier New"> xorl %eax, %eax</font></div><div><font face="Courier New"> .align 16, 0x90</font></div><div><font face="Courier New"> .LBB1_1: # %L_entry</font></div><div><font face="Courier New"> # =>This Inner Loop Header: Depth=1</font></div><div><font face="Courier New"> movslq %eax, %r8</font></div><div><font face="Courier New"> movss (%rdi,%r8,4), %xmm0</font></div><div><font face="Courier New"> addss (%rsi,%r8,4), %xmm0</font></div><div><font face="Courier New"> movss %xmm0, (%rdx,%r8,4)</font></div><div><font face="Courier New"> incq %rax</font></div><div><font face="Courier New"> cmpq %rax, %rcx</font></div><div><font face="Courier New"> jne .LBB1_1</font></div><div><font face="Courier New"> # BB#2:</font></div><div><font face="Courier New"> Ret</font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div><div><font face="Calibri" size="2"><span style="font-size: 11pt;"> </span></font></div></span></font><p>---------------------------------------------------------------------<br>Intel Israel (74) Limited</p><p>This e-mail and any attachments may contain confidential material for<br>the sole use of the intended recipient(s). Any review or distribution<br>by others is strictly prohibited. If you are not the intended<br>recipient, please contact the sender and delete all copies.</p></div></blockquote></div><br></body></html>