Skip to content

Commit 54fad33

Browse files
committed
first implement of fixing issue 71030
1 parent 64f76de commit 54fad33

12 files changed

+864
-11
lines changed

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,141 @@ bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
52195219
// We limit the max depth to track incoming values of PHIs or binary ops
52205220
// (e.g. AND) to avoid excessive cost.
52215221
const unsigned MAX_BINOP_DEPTH = 1;
5222+
5223+
void PPCInstrInfo::replaceInstrAfterElimExt32To64(const Register &Reg,
5224+
MachineRegisterInfo *MRI,
5225+
unsigned BinOpDepth,
5226+
LiveVariables *LV) const {
5227+
if (MRI->getRegClass(Reg) == &PPC::G8RCRegClass)
5228+
return;
5229+
5230+
MachineInstr *MI = MRI->getVRegDef(Reg);
5231+
if (!MI)
5232+
return;
5233+
5234+
unsigned Opcode = MI->getOpcode();
5235+
bool IsRelplaceIntr = false;
5236+
switch (Opcode) {
5237+
case PPC::OR:
5238+
case PPC::OR8:
5239+
case PPC::PHI:
5240+
case PPC::ISEL:
5241+
if (BinOpDepth < MAX_BINOP_DEPTH) {
5242+
if (Opcode == PPC::OR)
5243+
IsRelplaceIntr = true;
5244+
unsigned OperandEnd = 3, OperandStride = 1;
5245+
if (MI->getOpcode() == PPC::PHI) {
5246+
OperandEnd = MI->getNumOperands();
5247+
OperandStride = 2;
5248+
}
5249+
5250+
for (unsigned I = 1; I != OperandEnd; I += OperandStride) {
5251+
assert(MI->getOperand(I).isReg() && "Operand must be register");
5252+
Register SrcReg = MI->getOperand(I).getReg();
5253+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth + 1, LV);
5254+
}
5255+
}
5256+
break;
5257+
// case PPC::COPY:
5258+
case PPC::ORI:
5259+
case PPC::XORI:
5260+
case PPC::ORI8:
5261+
case PPC::XORI8:
5262+
case PPC::ORIS:
5263+
case PPC::XORIS:
5264+
case PPC::ORIS8:
5265+
case PPC::XORIS8: {
5266+
if (Opcode == PPC::ORI || Opcode == PPC::XORI || Opcode == PPC::ORIS ||
5267+
Opcode == PPC::ORIS || Opcode == PPC::XORIS)
5268+
IsRelplaceIntr = true;
5269+
Register SrcReg = MI->getOperand(1).getReg();
5270+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth, LV);
5271+
break;
5272+
}
5273+
case PPC::AND:
5274+
case PPC::AND8: {
5275+
if (BinOpDepth < MAX_BINOP_DEPTH) {
5276+
if (Opcode == PPC::AND)
5277+
IsRelplaceIntr = true;
5278+
Register SrcReg1 = MI->getOperand(1).getReg();
5279+
replaceInstrAfterElimExt32To64(SrcReg1, MRI, BinOpDepth, LV);
5280+
Register SrcReg2 = MI->getOperand(2).getReg();
5281+
replaceInstrAfterElimExt32To64(SrcReg2, MRI, BinOpDepth, LV);
5282+
}
5283+
break;
5284+
}
5285+
default:
5286+
break;
5287+
}
5288+
5289+
const PPCInstrInfo *TII =
5290+
MI->getMF()->getSubtarget<PPCSubtarget>().getInstrInfo();
5291+
if ((TII->isSExt32To64(Opcode) && !TII->isZExt32To64(Opcode)) ||
5292+
IsRelplaceIntr) {
5293+
DebugLoc DL = MI->getDebugLoc();
5294+
auto MBB = MI->getParent();
5295+
5296+
// If the oprand of the instruction is Register which isPPC::GRCRegClass, we
5297+
// need to promot the Oprande to PPC::G8RCRegClass.
5298+
DenseMap<unsigned, Register> PromoteRegs;
5299+
for (unsigned i = 1; i < MI->getNumOperands(); i++) {
5300+
MachineOperand &Oprand = MI->getOperand(i);
5301+
if (Oprand.isReg()) {
5302+
Register OprandReg = Oprand.getReg();
5303+
if (!OprandReg.isVirtual())
5304+
continue;
5305+
if (MRI->getRegClass(OprandReg) == &PPC::GPRCRegClass) {
5306+
Register TmpReg = MRI->createVirtualRegister(&PPC::G8RCRegClass);
5307+
Register DstTmpReg = MRI->createVirtualRegister(&PPC::G8RCRegClass);
5308+
5309+
BuildMI(*MBB, MI, DL, TII->get(PPC::IMPLICIT_DEF), TmpReg);
5310+
BuildMI(*MBB, MI, DL, TII->get(PPC::INSERT_SUBREG), DstTmpReg)
5311+
.addReg(TmpReg)
5312+
.addReg(OprandReg)
5313+
.addImm(PPC::sub_32);
5314+
PromoteRegs[i] = DstTmpReg;
5315+
} else {
5316+
PromoteRegs[i] = OprandReg;
5317+
}
5318+
}
5319+
}
5320+
5321+
Register NewReg = MRI->createVirtualRegister(&PPC::G8RCRegClass);
5322+
Register SrcReg = MI->getOperand(0).getReg();
5323+
5324+
// Most of the opcode of 64-bit instruction equal to the opcode of 32-bit
5325+
// version of same instruction plus one. But there are some exception:
5326+
// PPC::ANDC_rec, PPC::ANDI_rec, PPC::ANDIS_rec.
5327+
unsigned NewOpcode = Opcode + 1;
5328+
5329+
if (Opcode == PPC::ANDC_rec)
5330+
NewOpcode = PPC::ANDC8_rec;
5331+
if (Opcode == PPC::ANDI_rec)
5332+
NewOpcode = PPC::ANDI8_rec;
5333+
if (Opcode == PPC::ANDIS_rec)
5334+
NewOpcode = PPC::ANDIS8_rec;
5335+
5336+
BuildMI(*MBB, MI, DL, TII->get(NewOpcode), NewReg);
5337+
MachineBasicBlock::instr_iterator Iter(MI);
5338+
--Iter;
5339+
for (unsigned i = 1; i < MI->getNumOperands(); i++)
5340+
if (PromoteRegs.find(i) != PromoteRegs.end())
5341+
MachineInstrBuilder(*Iter->getMF(), Iter)
5342+
.addReg(PromoteRegs[i], RegState::Kill);
5343+
else
5344+
Iter->addOperand(MI->getOperand(i));
5345+
5346+
for (auto Iter = PromoteRegs.begin(); Iter != PromoteRegs.end(); Iter++)
5347+
LV->recomputeForSingleDefVirtReg(Iter->second);
5348+
MI->eraseFromParent();
5349+
BuildMI(*MBB, ++Iter, DL, TII->get(PPC::COPY), SrcReg)
5350+
.addReg(NewReg, RegState::Kill, PPC::sub_32);
5351+
LV->recomputeForSingleDefVirtReg(NewReg);
5352+
return;
5353+
}
5354+
return;
5355+
}
5356+
52225357
// The isSignOrZeroExtended function is recursive. The parameter BinOpDepth
52235358
// does not count all of the recursions. The parameter BinOpDepth is incremented
52245359
// only when isSignOrZeroExtended calls itself more than once. This is done to

llvm/lib/Target/PowerPC/PPCInstrInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "PPC.h"
1818
#include "PPCRegisterInfo.h"
1919
#include "llvm/ADT/SmallSet.h"
20+
#include "llvm/CodeGen/LiveVariables.h"
2021
#include "llvm/CodeGen/TargetInstrInfo.h"
2122

2223
#define GET_INSTRINFO_HEADER
@@ -610,6 +611,10 @@ class PPCInstrInfo : public PPCGenInstrInfo {
610611
const MachineRegisterInfo *MRI) const {
611612
return isSignOrZeroExtended(Reg, 0, MRI).second;
612613
}
614+
void replaceInstrAfterElimExt32To64(const Register &Reg,
615+
MachineRegisterInfo *MRI,
616+
unsigned BinOpDepth,
617+
LiveVariables *LV) const;
613618

614619
bool convertToImmediateForm(MachineInstr &MI,
615620
SmallSet<Register, 4> &RegsToUpdate,

llvm/lib/Target/PowerPC/PPCInstrInfo.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ defm SRW : XForm_6r<31, 536, (outs gprc:$RA), (ins gprc:$RST, gprc:$RB),
24082408
[(set i32:$RA, (PPCsrl i32:$RST, i32:$RB))]>, ZExt32To64;
24092409
defm SRAW : XForm_6rc<31, 792, (outs gprc:$RA), (ins gprc:$RST, gprc:$RB),
24102410
"sraw", "$RA, $RST, $RB", IIC_IntShift,
2411-
[(set i32:$RA, (PPCsra i32:$RST, i32:$RB))]>, SExt32To64;
2411+
[(set i32:$RA, (PPCsra i32:$RST, i32:$RB))]>;
24122412
}
24132413

24142414
def : InstAlias<"mr $rA, $rB", (OR gprc:$rA, gprc:$rB, gprc:$rB)>;
@@ -2423,8 +2423,7 @@ let PPC970_Unit = 1 in { // FXU Operations.
24232423
let hasSideEffects = 0 in {
24242424
defm SRAWI : XForm_10rc<31, 824, (outs gprc:$RA), (ins gprc:$RST, u5imm:$RB),
24252425
"srawi", "$RA, $RST, $RB", IIC_IntShift,
2426-
[(set i32:$RA, (sra i32:$RST, (i32 imm:$RB)))]>,
2427-
SExt32To64;
2426+
[(set i32:$RA, (sra i32:$RST, (i32 imm:$RB)))]>;
24282427
defm CNTLZW : XForm_11r<31, 26, (outs gprc:$RA), (ins gprc:$RST),
24292428
"cntlzw", "$RA, $RST", IIC_IntGeneral,
24302429
[(set i32:$RA, (ctlz i32:$RST))]>, ZExt32To64;

llvm/lib/Target/PowerPC/PPCMIPeephole.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ bool PPCMIPeephole::simplifyCode() {
10371037
TII->isSignExtended(NarrowReg, MRI)) {
10381038
// We can eliminate EXTSW if the input is known to be already
10391039
// sign-extended.
1040+
TII->replaceInstrAfterElimExt32To64(NarrowReg, MRI, 0, LV);
10401041
LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n");
10411042
Register TmpReg =
10421043
MF->getRegInfo().createVirtualRegister(&PPC::G8RCRegClass);

llvm/test/CodeGen/PowerPC/convert-rr-to-ri-instrs-out-of-range.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ body: |
604604
%2 = LI 48
605605
%5 = COPY %0.sub_32
606606
%8 = SRW killed %5, killed %2
607-
; CHECK: LI 0
607+
; CHECK: LI8 0
608608
; CHECK-LATE: li 3, 0
609609
$x3 = EXTSW_32_64 %8
610610
BLR8 implicit $lr8, implicit $rm, implicit $x3

llvm/test/CodeGen/PowerPC/convert-rr-to-ri-instrs.mir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ body: |
13481348
%1 = LI 77
13491349
%2 = ADDI killed %1, 44
13501350
%3 = EXTSW_32_64 killed %2
1351-
; CHECK: LI 121
1351+
; CHECK: LI8 121
13521352
; CHECK-LATE: li 3, 121
13531353
$x3 = COPY %3
13541354
BLR8 implicit $lr8, implicit $rm, implicit $x3
@@ -3573,7 +3573,7 @@ body: |
35733573
35743574
%0 = LI 777
35753575
%1 = ORI %0, 88
3576-
; CHECK: LI 857
3576+
; CHECK: LI8 857
35773577
; CHECK-LATE: li 3, 857
35783578
$x3 = EXTSW_32_64 %1
35793579
BLR8 implicit $lr8, implicit $rm, implicit $x3
@@ -4145,7 +4145,7 @@ body: |
41454145
%3 = IMPLICIT_DEF
41464146
%2 = LI 17
41474147
%4 = RLWINM killed %2, 4, 20, 27
4148-
; CHECK: LI 272
4148+
; CHECK: LI8 272
41494149
; CHECK-LATE: li 3, 272
41504150
$x3 = EXTSW_32_64 %4
41514151
BLR8 implicit $lr8, implicit $rm, implicit $x3
@@ -6456,7 +6456,7 @@ body: |
64566456
64576457
%0 = LI 871
64586458
%1 = XORI %0, 17
6459-
; CHECK: LI 886
6459+
; CHECK: LI8 886
64606460
; CHECK-LATE: li 3, 886
64616461
$x3 = EXTSW_32_64 %1
64626462
BLR8 implicit $lr8, implicit $rm, implicit $x3

0 commit comments

Comments
 (0)