[llvm-commits] [llvm] r161062 - in /llvm/trunk: lib/CodeGen/MachineSink.cpp test/CodeGen/X86/sink-out-of-loop.ll

Evan Cheng evan.cheng at apple.com
Tue Jul 31 12:39:48 PDT 2012


Hi Manman,

Is it possible for multiple successors to have the same loop depth? If so, should they be sorted with stable_sort instead?

Evan

On Jul 31, 2012, at 11:10 AM, Manman Ren <mren at apple.com> wrote:

> Author: mren
> Date: Tue Jul 31 13:10:39 2012
> New Revision: 161062
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=161062&view=rev
> Log:
> MachineSink: Sort the successors before trying to find SuccToSinkTo.
> 
> One motivating example is to sink an instruction from a basic block which has
> two successors: one outside the loop, the other inside the loop. We should try
> to sink the instruction outside the loop.
> 
> rdar://11980766
> 
> Added:
>    llvm/trunk/test/CodeGen/X86/sink-out-of-loop.ll
> Modified:
>    llvm/trunk/lib/CodeGen/MachineSink.cpp
> 
> Modified: llvm/trunk/lib/CodeGen/MachineSink.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineSink.cpp?rev=161062&r1=161061&r2=161062&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/MachineSink.cpp (original)
> +++ llvm/trunk/lib/CodeGen/MachineSink.cpp Tue Jul 31 13:10:39 2012
> @@ -99,6 +99,16 @@
>     bool PerformTrivialForwardCoalescing(MachineInstr *MI,
>                                          MachineBasicBlock *MBB);
>   };
> +
> +  // SuccessorSorter - Sort Successors according to their loop depth. 
> +  struct SuccessorSorter {
> +    SuccessorSorter(MachineLoopInfo *LoopInfo) : LI(LoopInfo) {}
> +    bool operator()(const MachineBasicBlock *LHS,
> +                    const MachineBasicBlock *RHS) const {
> +      return LI->getLoopDepth(LHS) < LI->getLoopDepth(RHS);
> +    }
> +    MachineLoopInfo *LI;
> +  };
> } // end anonymous namespace
> 
> char MachineSinking::ID = 0;
> @@ -526,8 +536,11 @@
> 
>       // Otherwise, we should look at all the successors and decide which one
>       // we should sink to.
> -      for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
> -           E = MBB->succ_end(); SI != E; ++SI) {
> +      // We give successors with smaller loop depth higher priority.
> +      SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(), MBB->succ_end());
> +      std::sort(Succs.begin(), Succs.end(), SuccessorSorter(LI));
> +      for (SmallVector<MachineBasicBlock*, 4>::iterator SI = Succs.begin(),
> +           E = Succs.end(); SI != E; ++SI) {
>         MachineBasicBlock *SuccBlock = *SI;
>         bool LocalUse = false;
>         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
> 
> Added: llvm/trunk/test/CodeGen/X86/sink-out-of-loop.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/sink-out-of-loop.ll?rev=161062&view=auto
> ==============================================================================
> --- llvm/trunk/test/CodeGen/X86/sink-out-of-loop.ll (added)
> +++ llvm/trunk/test/CodeGen/X86/sink-out-of-loop.ll Tue Jul 31 13:10:39 2012
> @@ -0,0 +1,54 @@
> +; RUN: llc -mtriple=x86_64-apple-darwin < %s | FileCheck %s
> +
> +; A MOV32ri is inside a loop, it has two successors, one successor is inside the
> +; same loop, the other successor is outside the loop. We should be able to sink
> +; MOV32ri outside the loop.
> +; rdar://11980766
> +define i32 @sink_succ(i32 %argc, i8** nocapture %argv) nounwind uwtable ssp {
> +; CHECK: sink_succ
> +; CHECK: [[OUTER_LN1:LBB0_[0-9]+]]: ## %preheader
> +; CHECK: %exit
> +; CHECK-NOT: movl
> +; CHECK: jne [[OUTER_LN1]]
> +; CHECK: movl
> +; CHECK: [[LN2:LBB0_[0-9]+]]: ## %for.body2
> +; CHECK: jne [[LN2]]
> +; CHECK: ret
> +entry:
> +  br label %preheader
> +
> +preheader:
> +  %i.127 = phi i32 [ 0, %entry ], [ %inc9, %exit ]
> +  br label %for.body1.lr
> +
> +for.body1.lr:
> +  %iv30 = phi i32 [ 1, %preheader ], [ %iv.next31, %for.inc40.i ]
> +  br label %for.body1
> +
> +for.body1:
> +  %iv.i = phi i64 [ 0, %for.body1.lr ], [ %iv.next.i, %for.body1 ]
> +  %iv.next.i = add i64 %iv.i, 1
> +  %lftr.wideiv32 = trunc i64 %iv.next.i to i32
> +  %exitcond33 = icmp eq i32 %lftr.wideiv32, %iv30
> +  br i1 %exitcond33, label %for.inc40.i, label %for.body1
> +
> +for.inc40.i:
> +  %iv.next31 = add i32 %iv30, 1
> +  %exitcond49.i = icmp eq i32 %iv.next31, 32
> +  br i1 %exitcond49.i, label %exit, label %for.body1.lr
> +
> +exit:
> +  %inc9 = add nsw i32 %i.127, 1
> +  %exitcond34 = icmp eq i32 %inc9, 10
> +  br i1 %exitcond34, label %for.body2, label %preheader
> +
> +for.body2:
> +  %iv = phi i64 [ %iv.next, %for.body2 ], [ 0, %exit ]
> +  %iv.next = add i64 %iv, 1
> +  %lftr.wideiv = trunc i64 %iv.next to i32
> +  %exitcond = icmp eq i32 %lftr.wideiv, 2048
> +  br i1 %exitcond, label %for.end20, label %for.body2
> +
> +for.end20:
> +  ret i32 0
> +}
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list