[PATCH] D31281: [LoopUnroll] Remap references in peeled iteration

Serge Pavlov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 04:56:09 PDT 2017


sepavloff created this revision.

If a loop contains several BBs, peeling produces code in which jump
targets point to original loop blocks rather than cloned BBs. This
patch uses the same way as loop unrolling to remap cloned blocks.


https://reviews.llvm.org/D31281

Files:
  lib/Transforms/Utils/LoopUnrollPeel.cpp
  test/Transforms/LoopUnroll/peel-loop.ll


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
+}
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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31281.92781.patch
Type: text/x-patch
Size: 1970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170323/80cef35f/attachment.bin>


More information about the llvm-commits mailing list