Index: lib/Transforms/Utils/LoopUnrollPeel.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollPeel.cpp +++ lib/Transforms/Utils/LoopUnrollPeel.cpp @@ -415,6 +415,11 @@ cloneLoopBlocks(L, Iter, InsertTop, InsertBot, Exit, NewBlocks, LoopBlocks, VMap, LVMap, DT, LI); + + // Remap to use values from the current iteration instead of the + // previous one. + remapInstructionsInBlocks(NewBlocks, VMap); + if (DT) { // Latches of the cloned loops dominate over the loop exit, so idom of the // latter is the first cloned loop body, as original PreHeader dominates @@ -437,10 +442,6 @@ F->getBasicBlockList().splice(InsertTop->getIterator(), F->getBasicBlockList(), NewBlocks[0]->getIterator(), F->end()); - - // Remap to use values from the current iteration instead of the - // previous one. - remapInstructionsInBlocks(NewBlocks, VMap); } // Now adjust the phi nodes in the loop header to get their initial values Index: test/Transforms/LoopUnroll/peel-loop.ll =================================================================== --- test/Transforms/LoopUnroll/peel-loop.ll +++ test/Transforms/LoopUnroll/peel-loop.ll @@ -94,3 +94,34 @@ %ret = phi i32 [ 0, %entry], [ %inc, %for.cond.for.end_crit_edge ] ret i32 %ret } + +; Check if loop composed of several BB is peeled correctly. +declare void @funcb() +define void @funca(i8* readnone %b, i8* readnone %e) local_unnamed_addr { +entry: + %cmp2 = icmp eq i8* %b, %e + br i1 %cmp2, label %for.end, label %for.body.preheader + +for.body.preheader: + br label %for.body + +for.body: + %Comma.04 = phi i1 [ true, %if.end ], [ false, %for.body.preheader ] + %b.addr.03 = phi i8* [ %incdec.ptr, %if.end ], [ %b, %for.body.preheader ] + br i1 %Comma.04, label %if.then, label %if.end + +if.then: + tail call void @funcb() + br label %if.end + +if.end: + %incdec.ptr = getelementptr inbounds i8, i8* %b.addr.03, i64 1 + %cmp = icmp eq i8* %incdec.ptr, %e + br i1 %cmp, label %for.end.loopexit, label %for.body + +for.end.loopexit: + br label %for.end + +for.end: + ret void +}