From: Dylan McKay Date: Sat, 13 May 2017 00:22:34 +0000 (+0000) Subject: [AVR] When lowering Select8/Select16, put newly generated MBBs in the same spot X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=af4bf777613b01743621ec1e72758aad473abb24;p=llvm [AVR] When lowering Select8/Select16, put newly generated MBBs in the same spot Contributed by Dr. Gergő Érdi. Fixes a bug. Raised from (https://github.com/avr-rust/rust/issues/49). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302973 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/AVR/AVRISelLowering.cpp b/lib/Target/AVR/AVRISelLowering.cpp index 0d8c44e8761..ef9c00e4b78 100644 --- a/lib/Target/AVR/AVRISelLowering.cpp +++ b/lib/Target/AVR/AVRISelLowering.cpp @@ -1610,8 +1610,9 @@ AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *trueMBB = MF->CreateMachineBasicBlock(LLVM_BB); MachineBasicBlock *falseMBB = MF->CreateMachineBasicBlock(LLVM_BB); - MachineFunction::iterator I = MBB->getParent()->begin(); - ++I; + MachineFunction::iterator I; + for (I = MF->begin(); I != MF->end() && &(*I) != MBB; ++I); + if (I != MF->end()) ++I; MF->insert(I, trueMBB); MF->insert(I, falseMBB); diff --git a/test/CodeGen/AVR/select-mbb-placement-bug.ll b/test/CodeGen/AVR/select-mbb-placement-bug.ll new file mode 100644 index 00000000000..ca7ec1ab831 --- /dev/null +++ b/test/CodeGen/AVR/select-mbb-placement-bug.ll @@ -0,0 +1,35 @@ +; RUN: llc -mcpu=atmega328p < %s -march=avr | FileCheck %s + +; CHECK-LABEL: loopy +define internal fastcc void @loopy() { + +; In this case, when we expand `Select8`/`Select16`, we should be +; replacing the existing MBB instead of adding a new one. +; +; https://github.com/avr-rust/rust/issues/49 + +; CHECK: LBB0_1: +; CHECK: LBB0_2: +; CHECK-NOT: LBB0_3: +start: + br label %bb7.preheader + +bb7.preheader: ; preds = %bb10, %start + %i = phi i8 [ 0, %start ], [ %j, %bb10 ] + %j = phi i8 [ 1, %start ], [ %next, %bb10 ] + br label %bb10 + +bb4: ; preds = %bb10 + ret void + +bb10: ; preds = %bb7.preheader + tail call fastcc void @observe(i8 %i, i8 1) + %0 = icmp ult i8 %j, 20 + %1 = zext i1 %0 to i8 + %next = add i8 %j, %1 + br i1 %0, label %bb7.preheader, label %bb4 + +} + +declare void @observe(i8, i8); +