Skip to content

Commit 2065a2f

Browse files
author
Krzysztof Parzyszek
committed
Properly handle PHIs with subregisters in UnreachableBlockElim
When a PHI operand has a subregister, create a COPY instead of simply replacing the PHI output with the input it. Differential Revision: https://reviews.llvm.org/D32650 llvm-svn: 301699
1 parent 0b3acbb commit 2065a2f

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

llvm/lib/CodeGen/UnreachableBlockElim.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/SmallPtrSet.h"
2626
#include "llvm/CodeGen/MachineDominators.h"
2727
#include "llvm/CodeGen/MachineFunctionPass.h"
28+
#include "llvm/CodeGen/MachineInstrBuilder.h"
2829
#include "llvm/CodeGen/MachineLoopInfo.h"
2930
#include "llvm/CodeGen/MachineModuleInfo.h"
3031
#include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -195,18 +196,30 @@ bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
195196
}
196197

197198
if (phi->getNumOperands() == 3) {
198-
unsigned Input = phi->getOperand(1).getReg();
199-
unsigned Output = phi->getOperand(0).getReg();
200-
201-
phi++->eraseFromParent();
199+
const MachineOperand &Input = phi->getOperand(1);
200+
const MachineOperand &Output = phi->getOperand(0);
201+
unsigned InputReg = Input.getReg();
202+
unsigned OutputReg = Output.getReg();
203+
assert(Output.getSubReg() == 0 && "Cannot have output subregister");
202204
ModifiedPHI = true;
203205

204-
if (Input != Output) {
206+
if (InputReg != OutputReg) {
205207
MachineRegisterInfo &MRI = F.getRegInfo();
206-
MRI.constrainRegClass(Input, MRI.getRegClass(Output));
207-
MRI.replaceRegWith(Output, Input);
208+
unsigned InputSub = Input.getSubReg();
209+
if (InputSub == 0) {
210+
MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg));
211+
MRI.replaceRegWith(OutputReg, InputReg);
212+
} else {
213+
// The input register to the PHI has a subregister:
214+
// insert a COPY instead of simply replacing the output
215+
// with the input.
216+
const TargetInstrInfo *TII = F.getSubtarget().getInstrInfo();
217+
BuildMI(*BB, BB->getFirstNonPHI(), phi->getDebugLoc(),
218+
TII->get(TargetOpcode::COPY), OutputReg)
219+
.addReg(InputReg, getRegState(Input), InputSub);
220+
}
221+
phi++->eraseFromParent();
208222
}
209-
210223
continue;
211224
}
212225

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# RUN: llc -march=hexagon -run-pass unreachable-mbb-elimination %s -o - | FileCheck %s
2+
3+
---
4+
name: fred
5+
tracksRegLiveness: true
6+
body: |
7+
bb.0:
8+
liveins: %d0
9+
successors: %bb.2
10+
11+
%0 : doubleregs = COPY %d0
12+
J2_jump %bb.2, implicit-def %pc
13+
14+
bb.1:
15+
successors: %bb.2
16+
A2_nop
17+
18+
bb.2:
19+
; Make sure that the subregister from the PHI operand is preserved.
20+
; CHECK: %[[REG:[0-9]+]] = COPY %0.isub_lo
21+
; CHECK: %r0 = COPY %[[REG]]
22+
%1 : intregs = PHI %0.isub_lo, %bb.0, %0.isub_hi, %bb.1
23+
%r0 = COPY %1
24+
...
25+

0 commit comments

Comments
 (0)