Index: lib/Transforms/IPO/PassManagerBuilder.cpp =================================================================== --- lib/Transforms/IPO/PassManagerBuilder.cpp +++ lib/Transforms/IPO/PassManagerBuilder.cpp @@ -373,6 +373,9 @@ if (!DisableUnrollLoops) { MPM.add(createLoopUnrollPass()); // Unroll small loops + // LoopUnroll may generate some redundency to cleanup. + MPM.add(createInstructionCombiningPass()); + // This is a barrier pass to avoid combine LICM pass and loop unroll pass // within same loop pass manager. MPM.add(createInstructionSimplifierPass()); Index: test/Transforms/LoopUnroll/unroll-cleanup.ll =================================================================== --- test/Transforms/LoopUnroll/unroll-cleanup.ll +++ test/Transforms/LoopUnroll/unroll-cleanup.ll @@ -0,0 +1,83 @@ +; The test is to check instcombine pass should cleanup the +; redundency produced by loop unroll pass. +; RUN: opt < %s -O2 -S | FileCheck %s + +; After loop unroll: +; %dec18 = add nsw i32 %dec18.in, -1 +; ... +; %dec18.1 = add nsw i32 %dec18, -1 +; should be merged to: +; %dec18.1 = add nsw i32 %dec18.in, -2 +; +; CHECK-LABEL: @_Z3fn1v( +; CHECK: %dec18.1 = add nsw i32 %dec18.in, -2 + +; ModuleID = 'unroll-cleanup.ll' +target triple = "x86_64-unknown-linux-gnu" + +@b = global i32 0, align 4 +@c = global i32 0, align 4 + +; Function Attrs: nounwind uwtable +define void @_Z3fn1v() #0 { +entry: + %0 = load i32, i32* @b, align 4 + %tobool20 = icmp eq i32 %0, 0 + br i1 %tobool20, label %for.end6, label %for.body.lr.ph + +for.body.lr.ph: ; preds = %entry + br label %for.body + +for.cond1.for.cond.loopexit_crit_edge: ; preds = %for.inc + %add.ptr.lcssa = phi i16* [ %add.ptr, %for.inc ] + %incdec.ptr.lcssa = phi i8* [ %incdec.ptr, %for.inc ] + br label %for.cond.loopexit + +for.cond.loopexit: ; preds = %for.cond1.for.cond.loopexit_crit_edge, %for.body + %r.1.lcssa = phi i16* [ %add.ptr.lcssa, %for.cond1.for.cond.loopexit_crit_edge ], [ %r.022, %for.body ] + %a.1.lcssa = phi i8* [ %incdec.ptr.lcssa, %for.cond1.for.cond.loopexit_crit_edge ], [ %a.021, %for.body ] + %1 = load i32, i32* @b, align 4 + %tobool = icmp eq i32 %1, 0 + br i1 %tobool, label %for.cond.for.end6_crit_edge, label %for.body + +for.body: ; preds = %for.body.lr.ph, %for.cond.loopexit + %r.022 = phi i16* [ undef, %for.body.lr.ph ], [ %r.1.lcssa, %for.cond.loopexit ] + %a.021 = phi i8* [ undef, %for.body.lr.ph ], [ %a.1.lcssa, %for.cond.loopexit ] + %2 = load i32, i32* @c, align 4 + %tobool215 = icmp eq i32 %2, 0 + br i1 %tobool215, label %for.cond.loopexit, label %for.body3.lr.ph + +for.body3.lr.ph: ; preds = %for.body + br label %for.body3 + +for.body3: ; preds = %for.body3.lr.ph, %for.inc + %dec18.in = phi i32 [ %2, %for.body3.lr.ph ], [ %dec18, %for.inc ] + %r.117 = phi i16* [ %r.022, %for.body3.lr.ph ], [ %add.ptr, %for.inc ] + %a.116 = phi i8* [ %a.021, %for.body3.lr.ph ], [ %incdec.ptr, %for.inc ] + %dec18 = add nsw i32 %dec18.in, -1 + %3 = load i8, i8* %a.116, align 1 + %cmp = icmp eq i8 %3, 0 + br i1 %cmp, label %if.then, label %for.inc + +if.then: ; preds = %for.body3 + %arrayidx = getelementptr inbounds i16, i16* %r.117, i64 1 + store i16 0, i16* %arrayidx, align 2 + store i16 0, i16* %r.117, align 2 + %arrayidx5 = getelementptr inbounds i16, i16* %r.117, i64 2 + store i16 0, i16* %arrayidx5, align 2 + br label %for.inc + +for.inc: ; preds = %for.body3, %if.then + %incdec.ptr = getelementptr inbounds i8, i8* %a.116, i64 1 + %add.ptr = getelementptr inbounds i16, i16* %r.117, i64 3 + %tobool2 = icmp eq i32 %dec18, 0 + br i1 %tobool2, label %for.cond1.for.cond.loopexit_crit_edge, label %for.body3 + +for.cond.for.end6_crit_edge: ; preds = %for.cond.loopexit + br label %for.end6 + +for.end6: ; preds = %for.cond.for.end6_crit_edge, %entry + ret void +} + +attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }