Index: lib/Transforms/Utils/LoopUnrollPeel.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollPeel.cpp +++ lib/Transforms/Utils/LoopUnrollPeel.cpp @@ -415,6 +415,16 @@ cloneLoopBlocks(L, Iter, InsertTop, InsertBot, Exit, NewBlocks, LoopBlocks, VMap, LVMap, DT, LI); + + // Rewrite the cloned instruction operands to use the values created when + // the clone is created. + for (BasicBlock *BB : NewBlocks) { + for (Instruction &I : *BB) { + RemapInstruction(&I, VMap, + RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); + } + } + 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 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 +}