Skip to content

Commit 79aaf13

Browse files
committed
promote Pseduo Opcode from 32bit to 64bits after eliminating the extsw instruction in PPCMIPeepholes optimization
1 parent 70ada5b commit 79aaf13

17 files changed

+281
-24
lines changed

llvm/lib/Target/PowerPC/P10InstrResources.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ def : InstRW<[P10W_F2_4C, P10W_DISP_EVEN, P10W_DISP_ANY, P10F2_Read],
833833
def : InstRW<[P10W_F2_4C, P10W_DISP_EVEN, P10W_DISP_ANY, P10F2_Read, P10F2_Read],
834834
(instrs
835835
SRAD_rec,
836-
SRAW_rec
836+
SRAW_rec,
837+
SRAW8_rec
837838
)>;
838839

839840
// 2-way crack instructions
@@ -1008,7 +1009,7 @@ def : InstRW<[P10W_FX_3C, P10W_DISP_ANY, P10FX_Read, P10FX_Read],
10081009
SLD,
10091010
SLW, SLW8,
10101011
SRAD,
1011-
SRAW,
1012+
SRAW, SRAW8,
10121013
SRD,
10131014
SRW, SRW8,
10141015
SUBF, SUBF8,

llvm/lib/Target/PowerPC/P9InstrResources.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def : InstRW<[P9_ALU_2C, IP_EXEC_1C, DISP_3SLOTS_1C],
189189
(instregex "F(N)?ABS(D|S)$"),
190190
(instregex "FNEG(D|S)$"),
191191
(instregex "FCPSGN(D|S)$"),
192-
(instregex "SRAW(I)?$"),
192+
(instregex "SRAW(I|8)?$"),
193193
(instregex "ISEL(8)?$"),
194194
RLDIMI,
195195
XSIEXPDP,
@@ -1091,7 +1091,7 @@ def : InstRW<[P9_ALUOpAndALUOp_4C, IP_EXEC_1C, IP_EXEC_1C,
10911091
(instregex "RLD(I)?C(R|L)_rec$"),
10921092
(instregex "RLW(IMI|INM|NM)(8)?_rec$"),
10931093
(instregex "SLW(8)?_rec$"),
1094-
(instregex "SRAW(I)?_rec$"),
1094+
(instregex "SRAW(8|I)?_rec$"),
10951095
(instregex "SRW(8)?_rec$"),
10961096
RLDICL_32_rec,
10971097
RLDIMI_rec

llvm/lib/Target/PowerPC/PPC.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,18 @@ def getAltVSXFMAOpcode : InstrMapping {
534534
let ValueCols = [["1"]];
535535
}
536536

537+
def get64BitInstrFromSignedExt32BitInstr : InstrMapping {
538+
let FilterClass = "SExt32To64";
539+
// Instructions with the same opcode.
540+
let RowFields = ["Inst"];
541+
// Instructions with the same Interpretation64Bit value form a column.
542+
let ColFields = ["Interpretation64Bit"];
543+
// The key column are not the Interpretation64Bit-form instructions.
544+
let KeyCol = ["0"];
545+
// Value columns are the Interpretation64Bit-form instructions.
546+
let ValueCols = [["1"]];
547+
}
548+
537549
//===----------------------------------------------------------------------===//
538550
// Register File Description
539551
//===----------------------------------------------------------------------===//

llvm/lib/Target/PowerPC/PPCInstr64Bit.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,11 @@ defm SLW8 : XForm_6r<31, 24, (outs g8rc:$RA), (ins g8rc:$RST, g8rc:$RB),
932932
"slw", "$RA, $RST, $RB", IIC_IntGeneral, []>, ZExt32To64;
933933
defm SRW8 : XForm_6r<31, 536, (outs g8rc:$RA), (ins g8rc:$RST, g8rc:$RB),
934934
"srw", "$RA, $RST, $RB", IIC_IntGeneral, []>, ZExt32To64;
935+
936+
defm SRAW8 : XForm_6rc<31, 792, (outs g8rc:$RA), (ins g8rc:$RST, g8rc:$RB),
937+
"sraw", "$RA, $RST, $RB", IIC_IntShift,
938+
[(set i64:$RA, (PPCsra i64:$RST, i64:$RB))]>, SExt32To64;
939+
935940
} // Interpretation64Bit
936941

937942
// For fast-isel:

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,6 +5234,218 @@ bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
52345234
// We limit the max depth to track incoming values of PHIs or binary ops
52355235
// (e.g. AND) to avoid excessive cost.
52365236
const unsigned MAX_BINOP_DEPTH = 1;
5237+
5238+
void PPCInstrInfo::replaceInstrAfterElimExt32To64(const Register &Reg,
5239+
MachineRegisterInfo *MRI,
5240+
unsigned BinOpDepth,
5241+
LiveVariables *LV) const {
5242+
MachineInstr *MI = MRI->getVRegDef(Reg);
5243+
if (!MI)
5244+
return;
5245+
5246+
unsigned Opcode = MI->getOpcode();
5247+
bool IsReplaceInstr = false;
5248+
int NewOpcode = -1;
5249+
5250+
auto SetNewOpcode = [&](int NewOpc) {
5251+
if (!IsReplaceInstr) {
5252+
NewOpcode = NewOpc;
5253+
IsReplaceInstr = true;
5254+
}
5255+
};
5256+
5257+
switch (Opcode) {
5258+
case PPC::OR:
5259+
SetNewOpcode(PPC::OR8);
5260+
[[fallthrough]];
5261+
case PPC::ISEL:
5262+
SetNewOpcode(PPC::ISEL8);
5263+
[[fallthrough]];
5264+
case PPC::OR8:
5265+
case PPC::PHI:
5266+
if (BinOpDepth < MAX_BINOP_DEPTH) {
5267+
unsigned OperandEnd = 3, OperandStride = 1;
5268+
if (Opcode == PPC::PHI) {
5269+
OperandEnd = MI->getNumOperands();
5270+
OperandStride = 2;
5271+
}
5272+
5273+
for (unsigned I = 1; I != OperandEnd; I += OperandStride) {
5274+
assert(MI->getOperand(I).isReg() && "Operand must be register");
5275+
Register SrcReg = MI->getOperand(I).getReg();
5276+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth + 1, LV);
5277+
}
5278+
5279+
if (!IsReplaceInstr)
5280+
return;
5281+
}
5282+
break;
5283+
case PPC::COPY: {
5284+
Register SrcReg = MI->getOperand(1).getReg();
5285+
const MachineFunction *MF = MI->getMF();
5286+
if (!MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) {
5287+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth, LV);
5288+
return;
5289+
}
5290+
// From here on everything is SVR4ABI
5291+
if (MI->getParent()->getBasicBlock() == &MF->getFunction().getEntryBlock())
5292+
return;
5293+
5294+
if (SrcReg != PPC::X3) {
5295+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth, LV);
5296+
return;
5297+
}
5298+
}
5299+
return;
5300+
case PPC::ORI:
5301+
SetNewOpcode(PPC::ORI8);
5302+
[[fallthrough]];
5303+
case PPC::XORI:
5304+
SetNewOpcode(PPC::XORI8);
5305+
[[fallthrough]];
5306+
case PPC::ORIS:
5307+
SetNewOpcode(PPC::ORIS8);
5308+
[[fallthrough]];
5309+
case PPC::XORIS:
5310+
SetNewOpcode(PPC::XORIS8);
5311+
[[fallthrough]];
5312+
case PPC::ORI8:
5313+
case PPC::XORI8:
5314+
case PPC::ORIS8:
5315+
case PPC::XORIS8: {
5316+
Register SrcReg = MI->getOperand(1).getReg();
5317+
replaceInstrAfterElimExt32To64(SrcReg, MRI, BinOpDepth, LV);
5318+
5319+
if (!IsReplaceInstr)
5320+
return;
5321+
break;
5322+
}
5323+
case PPC::AND:
5324+
SetNewOpcode(PPC::AND8);
5325+
[[fallthrough]];
5326+
case PPC::AND8: {
5327+
if (BinOpDepth < MAX_BINOP_DEPTH) {
5328+
Register SrcReg1 = MI->getOperand(1).getReg();
5329+
replaceInstrAfterElimExt32To64(SrcReg1, MRI, BinOpDepth, LV);
5330+
Register SrcReg2 = MI->getOperand(2).getReg();
5331+
replaceInstrAfterElimExt32To64(SrcReg2, MRI, BinOpDepth, LV);
5332+
if (!IsReplaceInstr)
5333+
return;
5334+
}
5335+
break;
5336+
}
5337+
case PPC::RLWINM:
5338+
SetNewOpcode(PPC::RLWINM8);
5339+
break;
5340+
case PPC::RLWINM_rec:
5341+
SetNewOpcode(PPC::RLWINM8_rec);
5342+
break;
5343+
case PPC::RLWNM:
5344+
SetNewOpcode(PPC ::RLWNM8);
5345+
break;
5346+
case PPC::RLWNM_rec:
5347+
SetNewOpcode(PPC::RLWNM8_rec);
5348+
break;
5349+
case PPC::ANDC_rec:
5350+
SetNewOpcode(PPC::ANDC8_rec);
5351+
break;
5352+
case PPC::ANDIS_rec:
5353+
SetNewOpcode(PPC::ANDIS8_rec);
5354+
break;
5355+
default:
5356+
break;
5357+
}
5358+
5359+
const PPCInstrInfo *TII =
5360+
MI->getMF()->getSubtarget<PPCSubtarget>().getInstrInfo();
5361+
if ((definedBySignExtendingOp(Reg, MRI) && !TII->isZExt32To64(Opcode) &&
5362+
!isOpZeroOfSubwordPreincLoad(Opcode)) ||
5363+
IsReplaceInstr) {
5364+
5365+
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
5366+
5367+
if (RC == &PPC::G8RCRegClass || RC == &PPC::G8RC_and_G8RC_NOX0RegClass)
5368+
return;
5369+
5370+
if (!IsReplaceInstr)
5371+
NewOpcode = PPC::get64BitInstrFromSignedExt32BitInstr(Opcode);
5372+
5373+
assert(NewOpcode != -1 &&
5374+
"Must have a 64-bit opcode to map the 32-bit opcode!");
5375+
5376+
const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
5377+
const MCInstrDesc &MCID = TII->get(NewOpcode);
5378+
5379+
Register SrcReg = MI->getOperand(0).getReg();
5380+
const TargetRegisterClass *NewRC =
5381+
TRI->getRegClass(MCID.operands()[0].RegClass);
5382+
const TargetRegisterClass *SrcRC = MRI->getRegClass(SrcReg);
5383+
5384+
if (NewRC == SrcRC)
5385+
return;
5386+
5387+
DebugLoc DL = MI->getDebugLoc();
5388+
auto MBB = MI->getParent();
5389+
5390+
// Since the pseudo-opcode of the instruction is promoted from 32-bit to
5391+
// 64-bit, if the operand of the original instruction belongs to
5392+
// PPC::GRCRegClass or PPC::GPRC_and_GPRC_NOR0RegClass, we need to promote
5393+
// the operand to PPC::G8CRegClass or PPC::G8RC_and_G8RC_NOR0RegClass,
5394+
// respectively.
5395+
DenseMap<unsigned, Register> PromoteRegs;
5396+
DenseMap<unsigned, Register> ReCalRegs;
5397+
for (unsigned i = 1; i < MI->getNumOperands(); i++) {
5398+
MachineOperand &Operand = MI->getOperand(i);
5399+
if (Operand.isReg()) {
5400+
Register OperandReg = Operand.getReg();
5401+
if (!OperandReg.isVirtual())
5402+
continue;
5403+
5404+
const TargetRegisterClass *RC =
5405+
TRI->getRegClass(MCID.operands()[i].RegClass);
5406+
const TargetRegisterClass *OrgRC = MRI->getRegClass(OperandReg);
5407+
if (RC != MRI->getRegClass(OperandReg) &&
5408+
(OrgRC == &PPC::GPRCRegClass ||
5409+
OrgRC == &PPC::GPRC_and_GPRC_NOR0RegClass)) {
5410+
Register TmpReg = MRI->createVirtualRegister(RC);
5411+
Register DstTmpReg = MRI->createVirtualRegister(RC);
5412+
BuildMI(*MBB, MI, DL, TII->get(PPC::IMPLICIT_DEF), TmpReg);
5413+
BuildMI(*MBB, MI, DL, TII->get(PPC::INSERT_SUBREG), DstTmpReg)
5414+
.addReg(TmpReg)
5415+
.addReg(OperandReg)
5416+
.addImm(PPC::sub_32);
5417+
PromoteRegs[i] = DstTmpReg;
5418+
ReCalRegs[i] = DstTmpReg;
5419+
} else {
5420+
ReCalRegs[i] = OperandReg;
5421+
}
5422+
}
5423+
}
5424+
5425+
Register NewReg = MRI->createVirtualRegister(NewRC);
5426+
5427+
BuildMI(*MBB, MI, DL, TII->get(NewOpcode), NewReg);
5428+
MachineBasicBlock::instr_iterator Iter(MI);
5429+
--Iter;
5430+
for (unsigned i = 1; i < MI->getNumOperands(); i++)
5431+
if (PromoteRegs.find(i) != PromoteRegs.end())
5432+
MachineInstrBuilder(*Iter->getMF(), Iter)
5433+
.addReg(PromoteRegs[i], RegState::Kill);
5434+
else
5435+
Iter->addOperand(MI->getOperand(i));
5436+
5437+
for (auto Iter = ReCalRegs.begin(); Iter != ReCalRegs.end(); Iter++)
5438+
LV->recomputeForSingleDefVirtReg(Iter->second);
5439+
MI->eraseFromParent();
5440+
5441+
BuildMI(*MBB, ++Iter, DL, TII->get(PPC::COPY), SrcReg)
5442+
.addReg(NewReg, RegState::Kill, PPC::sub_32);
5443+
LV->recomputeForSingleDefVirtReg(NewReg);
5444+
return;
5445+
}
5446+
return;
5447+
}
5448+
52375449
// The isSignOrZeroExtended function is recursive. The parameter BinOpDepth
52385450
// does not count all of the recursions. The parameter BinOpDepth is incremented
52395451
// 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
@@ -624,6 +625,10 @@ class PPCInstrInfo : public PPCGenInstrInfo {
624625
const MachineRegisterInfo *MRI) const {
625626
return isSignOrZeroExtended(Reg, 0, MRI).second;
626627
}
628+
void replaceInstrAfterElimExt32To64(const Register &Reg,
629+
MachineRegisterInfo *MRI,
630+
unsigned BinOpDepth,
631+
LiveVariables *LV) const;
627632

628633
bool convertToImmediateForm(MachineInstr &MI,
629634
SmallSet<Register, 4> &RegsToUpdate,

llvm/lib/Target/PowerPC/PPCInstrInfo.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -1051,6 +1051,7 @@ bool PPCMIPeephole::simplifyCode() {
10511051
TII->isSignExtended(NarrowReg, MRI)) {
10521052
// We can eliminate EXTSW if the input is known to be already
10531053
// sign-extended.
1054+
TII->replaceInstrAfterElimExt32To64(NarrowReg, MRI, 0, LV);
10541055
LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n");
10551056
Register TmpReg =
10561057
MF->getRegInfo().createVirtualRegister(&PPC::G8RCRegClass);

llvm/lib/Target/PowerPC/PPCScheduleP7.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ let SchedModel = P7Model in {
216216
RLWNM, RLWNM8, RLWNM_rec, RLDIMI, RLDIMI_rec,
217217
RLDICL_32, RLDICL_32_64, RLDICL_32_rec, RLDICR_32, RLWINM8_rec, RLWNM8_rec,
218218
SLD, SLD_rec, SLW, SLW8, SLW_rec, SLW8_rec, SRD, SRD_rec, SRW, SRW8, SRW_rec,
219-
SRW8_rec, SRADI, SRADI_rec, SRAWI, SRAWI_rec, SRAD, SRAD_rec, SRAW, SRAW_rec,
219+
SRW8_rec, SRADI, SRADI_rec, SRAWI, SRAWI_rec, SRAD, SRAD_rec, SRAW, SRAW_rec, SRAW8, SRAW8_rec,
220220
SRADI_32, SUBFE, SUBFE8, SUBFE8O_rec, SUBFEO_rec
221221
)>;
222222

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ body: |
256256
%3 = IMPLICIT_DEF
257257
%2 = LI 170
258258
%4 = RLWNM killed %1, %2, 20, 27
259-
; CHECK: RLWINM killed %1, 10, 20, 27
259+
; CHECK: RLWINM8 killed %6, 10, 20, 27
260260
; CHECK-LATE: rlwinm 3, 3, 10, 20, 27
261261
$x3 = EXTSW_32_64 %4
262262
BLR8 implicit $lr8, implicit $rm, implicit $x3
@@ -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
@@ -722,7 +722,7 @@ body: |
722722
%3 = COPY %0.sub_32
723723
%4 = SRAW killed %3, killed %2, implicit-def dead $carry
724724
; CHECK: LI 48
725-
; CHECK: SRAW killed %3, killed %2, implicit-def dead $carry
725+
; CHECK: SRAW8 killed %7, killed %9, implicit-def $carry, implicit-def dead $carry
726726
; CHECK-LATE: sraw 3, 3, 4
727727
%5 = EXTSW_32_64 killed %4
728728
$x3 = COPY %5
@@ -779,7 +779,7 @@ body: |
779779
%2 = LI 80
780780
%3 = COPY %0.sub_32
781781
%4 = SRAW_rec killed %3, %2, implicit-def dead $carry, implicit-def $cr0
782-
; CHECK: SRAW_rec killed %3, %2, implicit-def dead $carry, implicit-def $cr0
782+
; CHECK: killed %10, killed %12, implicit-def $carry, implicit-def $cr0, implicit-def dead $carry, implicit-def $cr0
783783
; CHECK-LATE: sraw. 3, 3, 4
784784
%5 = COPY killed $cr0
785785
%6 = ISEL %2, %4, %5.sub_eq

0 commit comments

Comments
 (0)