[SCEV] Phi-node backedge is assumed to be at the same index for all phi-nodes in getConstantEvolutionLoopExitValue
| Bugzilla Link | 25060 |
| Resolution | FIXED |
| Resolved on | Oct 12, 2015 02:55 |
| Version | trunk |
| OS | Linux |
| Attachments | Reduced input file |
| CC | @hfinkel,@sanjoy |
Extended Description
Run opt on the attached file like this:
opt -S -instcombine -licm -sroa -indvars -o t1.ll
Results:
; ModuleID = './t1.ll'
@gi = global i16 0
define i16 @fn3() { br label %bb1
bb7: ; preds = %bb2 %_tmp2 = add i16 %i.9.0, -1 br label %bb2
bb2: ; preds = %bb2.preheader, %bb7 %i.9.0 = phi i16 [ %i.9.1, %bb2.preheader ], [ %_tmp2, %bb7 ] %_tmp6 = icmp eq i16 %i.9.0, 0 br i1 %_tmp6, label %bb4, label %bb7
bb4: ; preds = %bb2 br label %bb1
bb1: ; preds = %bb4, %0 %i.9.1 = phi i16 [ 1, %0 ], [ 0, %bb4 ] %_tmp10 = icmp eq i16 %i.9.1, 0 br i1 %_tmp10, label %bb5, label %bb2.preheader
bb2.preheader: ; preds = %bb1 br label %bb2
bb5: ; preds = %bb1 store i16 1, i16* @gi, align 2 ret i16 0
bb6: ; No predecessors! ret i16 0 }
The error is that value 1 is stored on the line store i16 1, i16* @gi, align 2 The correct value to store is 2.
I have been debugging this and found a problem in ScalarEvolution.cpp:
// Since the loop is canonicalized, the PHI node must have two entries. One
// entry must be a constant (coming in from outside of the loop), and the
// second must be derived from the same PHI.
bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
PHINode *PHI = nullptr;
for (BasicBlock::iterator I = Header->begin();
(PHI = dyn_cast
Here the code assumes that all phi-nodes have the incoming backedge value at the same index, but this is not true in this example.
Is this a problem in ScalarEvolution or is the problem already in the input with badly created phi-nodes?
Fix up for review at http://reviews.llvm.org/D13458
I have verified that this patch does indeed fix the issue that I was debugging.
Thank you for working on this!
Fix up for review at http://reviews.llvm.org/D13458
Here the code assumes that all phi-nodes have the incoming backedge value at the same index, but this is not true in this example.
Is this a problem in ScalarEvolution or is the problem already in the input with badly created phi-nodes?
This definitely looks like a problem in ScalarEvolution. Even if the IR isn't canonical, SCEV / IndVars is not allowed to miscompile.
Here is input from a debugging sessions right before RewriteLoopExitValues is about to run in IndVarsSimplify. Look at order of the incoming values of the phi-nodes in bb1:
(gdb) p F.dump()
; Function Attrs: nounwind define i16 @fn3() #4 { br label %bb1, !dbg !14
bb7: ; preds = %bb2 %_tmp2 = add i16 %i.9.0, -1, !dbg !15 br label %bb2, !dbg !17
bb2: ; preds = %bb2.preheader, %bb7 %i.9.0 = phi i16 [ %i.9.1, %bb2.preheader ], [ %_tmp2, %bb7 ] %_tmp6 = icmp eq i16 %i.9.0, 0, !dbg !17 br i1 %_tmp6, label %bb4, label %bb7, !dbg !17
bb4: ; preds = %bb2 %1 = add i16 %i.9.1, %storemerge, !dbg !17 %_tmp8 = add i16 %1, 1, !dbg !19 br label %bb1, !dbg !20
bb1: ; preds = %bb4, %0 %i.9.1 = phi i16 [ 1, %0 ], [ 0, %bb4 ] %storemerge = phi i16 [ %_tmp8, %bb4 ], [ 0, %0 ] %_tmp10 = icmp eq i16 %i.9.1, 0, !dbg !20 br i1 %_tmp10, label %bb5, label %bb2.preheader, !dbg !20
bb2.preheader: ; preds = %bb1 br label %bb2, !dbg !17
bb5: ; preds = %bb1 %i.9.1.lcssa = phi i16 [ %i.9.1, %bb1 ] %storemerge.lcssa = phi i16 [ %storemerge, %bb1 ] store i16 %storemerge.lcssa, i16* @gi, align 1, !dbg !22 ret i16 %i.9.1.lcssa, !dbg !24 }
assigned to @sanjoy