<div dir="ltr"><div>The attached patch fixes a bug in ARM's constant island pass. The bug is in ARMConstantIslandPass.cpp:1307-1314 where the upper bound of the new water split point is computed:</div><div><br></div><div><p style="margin:0px;font-size:11px;font-family:Menlo">// This <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">could poi</span>nt off the end of the block if we've already got constant<br></p></div><div><p style="margin:0px;font-size:11px;font-family:Menlo">// pool entries following this block; only the last one is in the water list.</p><p style="margin:0px;font-size:11px;font-family:Menlo">// Back past any possible branches (allow for a conditional and a maximally</p><p style="margin:0px;font-size:11px;font-family:Menlo">// long unconditional).</p><p style="margin:0px;font-size:11px;font-family:Menlo">if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {</p><p style="margin:0px;font-size:11px;font-family:Menlo">    BaseInsertOffset = UserBBI.postOffset() - UPad - 8;</p><p style="margin:0px;font-size:11px;font-family:Menlo">    DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));</p><p style="margin:0px;font-size:11px;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><div>The split point is supposed to be somewhere between the machine instruction that loads from the constant pool entry and the end of the basic block, before branch instructions. The code above is fine if the basic block is large enough and there are a sufficient number of instructions following the machine instruction. However, if the machine instruction is near the end of the basic block, BaseInsertOffset can point to the machine instruction or another instruction that precedes it, and this can lead to convergence failure (see the example below).</div><div><br></div><div>The attached patch fixes this bug by ensuring BaseInsertOffset is larger than the offset of the instruction following the constant-loading instruction.</div><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo"></p><div style="color:rgb(34,34,34);font-family:arial;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"></div><p></p><div style="color:rgb(34,34,34);font-family:arial;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><p style="margin:0px;font-size:11px;font-family:Menlo">-    BaseInsertOffset = UserBBI.postOffset() - UPad - 8;</p><p style="margin:0px;font-size:11px;font-family:Menlo">+    BaseInsertOffset =<br></p><p style="margin:0px;font-size:11px;font-family:Menlo">+        std::max(UserBBI.postOffset() - UPad - 8,</p><p style="margin:0px;font-size:11px;font-family:Menlo">+                 UserOffset + TII->GetInstSizeInBytes(UserMI) + 1);</p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:arial;font-size:small">rdar://problem/18581150</span><br></p></div></div><div><br></div><div><br></div><div>This is how the bug can lead to convergence failure:</div><div><br></div><div>1. Instruction VLDRS is too far from the constant pool entry cp#136 in BB#189. A new water has to be created in BB#324 after VLDRS.</div><div><br></div><div><p style="margin:0px;font-size:11px;font-family:Menlo">BB#189: Align 2 (4 bytes)</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre"> </span>CONSTPOOL_ENTRY 345, <cp#136>, 4</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">...</p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">BB#323: Align 2 (4 bytes)</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">       </span>CONSTPOOL_ENTRY 479, <cp#156>, 4</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">BB#324: derived from LLVM BB %bb35</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Predecessors according to CFG: BB#187</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">        </span>%D0<def> = VORRd %D9, %D9, pred:14, pred:%noreg, %S18<imp-use></p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">       </span>%S6<def> = VLDRS <cp#345>, 0, pred:14, pred:%noreg, %D3<imp-def>; mem:LD4[ConstantPool]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">        </span>%D5<def> = VLDRD <cp#346>, 0, pred:14, pred:%noreg; mem:LD8[ConstantPool]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">    </span>t2STRi12 %R1<kill>, %R10, 0, pred:14, pred:%noreg; mem:ST4[%arg14]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre"> </span>t2B <BB#342>, pred:14, pred:%noreg</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Successors according to CFG: BB#342</p><div><br></div><div><br></div><div>2. However, new water (BB#325) is created before VLDRS and cp#136 is moved there, because VLDRS is too close to the end of its parent basic block. This causes all the constant pool entries between BB#189 and BB#323 to be moved after constant pool entry cp#136, because they are now out of range, and this in turn causes cp#136 to be out of range again.</div><div><br></div><div><p style="margin:0px;font-size:11px;font-family:Menlo">BB#324: derived from LLVM BB %bb35</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Predecessors according to CFG: BB#187</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">        </span>%D0<def> = VORRd %D9, %D9, pred:14, pred:%noreg, %S18<imp-use></p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">       </span>t2B <BB#461>, pred:14, pred:%noreg</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Successors according to CFG: BB#461</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">BB#325: Align 2 (4 bytes)</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre"> </span>CONSTPOOL_ENTRY 480, <cp#136>, 4</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px">...</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">BB#460: Align 2 (4 bytes)</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">    </span>CONSTPOOL_ENTRY 615, <cp#156>, 4</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">BB#461: derived from LLVM BB %bb35</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Predecessors according to CFG: BB#324</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">        </span>%S6<def> = VLDRS <cp#480>, 0, pred:14, pred:%noreg, %D3<imp-def>; mem:LD4[ConstantPool]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">        </span>%D5<def> = VLDRD <cp#481>, 0, pred:14, pred:%noreg; mem:LD8[ConstantPool]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre">    </span>t2STRi12 %R1<kill>, %R10, 0, pred:14, pred:%noreg; mem:ST4[%arg14]</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span class="" style="white-space:pre"> </span>t2B <BB#479>, pred:14, pred:%noreg</p><p style="margin:0px;font-size:11px;font-family:Menlo">    Successors according to CFG: BB#479</p></div></div></div>