Skip to content

Commit 42d641e

Browse files
authored
AMDGPU/GlobalISel: Select all constants in tablegen (llvm#100788)
This regresses the arbitrary address space pointer case. Ideally we could write a pattern that matches a pointer based only on its size, but using iPTR/iPTRAny seem to not work for this.
1 parent 1d0723d commit 42d641e

File tree

4 files changed

+62
-296
lines changed

4 files changed

+62
-296
lines changed

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,98 +2503,6 @@ bool AMDGPUInstructionSelector::selectG_FPEXT(MachineInstr &I) const {
25032503
return false;
25042504
}
25052505

2506-
bool AMDGPUInstructionSelector::selectG_CONSTANT(MachineInstr &I) const {
2507-
if (selectImpl(I, *CoverageInfo))
2508-
return true;
2509-
2510-
// FIXME: Relying on manual selection for 64-bit case, and pointer typed
2511-
// constants.
2512-
MachineBasicBlock *BB = I.getParent();
2513-
MachineOperand &ImmOp = I.getOperand(1);
2514-
Register DstReg = I.getOperand(0).getReg();
2515-
LLT Ty = MRI->getType(DstReg);
2516-
unsigned Size = Ty.getSizeInBits();
2517-
assert((Size == 64 || Ty.isPointer()) &&
2518-
"patterns should have selected this");
2519-
2520-
bool IsFP = false;
2521-
2522-
// The AMDGPU backend only supports Imm operands and not CImm or FPImm.
2523-
if (ImmOp.isFPImm()) {
2524-
const APInt &Imm = ImmOp.getFPImm()->getValueAPF().bitcastToAPInt();
2525-
ImmOp.ChangeToImmediate(Imm.getZExtValue());
2526-
IsFP = true;
2527-
} else if (ImmOp.isCImm()) {
2528-
ImmOp.ChangeToImmediate(ImmOp.getCImm()->getSExtValue());
2529-
} else {
2530-
llvm_unreachable("Not supported by g_constants");
2531-
}
2532-
2533-
const RegisterBank *DstRB = RBI.getRegBank(DstReg, *MRI, TRI);
2534-
const bool IsSgpr = DstRB->getID() == AMDGPU::SGPRRegBankID;
2535-
2536-
unsigned Opcode;
2537-
if (DstRB->getID() == AMDGPU::VCCRegBankID) {
2538-
Opcode = STI.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
2539-
} else if (Size == 64 &&
2540-
AMDGPU::isValid32BitLiteral(I.getOperand(1).getImm(), IsFP)) {
2541-
Opcode = IsSgpr ? AMDGPU::S_MOV_B64_IMM_PSEUDO : AMDGPU::V_MOV_B64_PSEUDO;
2542-
I.setDesc(TII.get(Opcode));
2543-
I.addImplicitDefUseOperands(*MF);
2544-
return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
2545-
} else {
2546-
Opcode = IsSgpr ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
2547-
2548-
// We should never produce s1 values on banks other than VCC. If the user of
2549-
// this already constrained the register, we may incorrectly think it's VCC
2550-
// if it wasn't originally.
2551-
if (Size == 1)
2552-
return false;
2553-
}
2554-
2555-
if (Size != 64) {
2556-
I.setDesc(TII.get(Opcode));
2557-
I.addImplicitDefUseOperands(*MF);
2558-
return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
2559-
}
2560-
2561-
const DebugLoc &DL = I.getDebugLoc();
2562-
2563-
APInt Imm(Size, I.getOperand(1).getImm());
2564-
2565-
MachineInstr *ResInst;
2566-
if (IsSgpr && TII.isInlineConstant(Imm)) {
2567-
ResInst = BuildMI(*BB, &I, DL, TII.get(AMDGPU::S_MOV_B64), DstReg)
2568-
.addImm(I.getOperand(1).getImm());
2569-
} else {
2570-
const TargetRegisterClass *RC = IsSgpr ?
2571-
&AMDGPU::SReg_32RegClass : &AMDGPU::VGPR_32RegClass;
2572-
Register LoReg = MRI->createVirtualRegister(RC);
2573-
Register HiReg = MRI->createVirtualRegister(RC);
2574-
2575-
BuildMI(*BB, &I, DL, TII.get(Opcode), LoReg)
2576-
.addImm(Imm.trunc(32).getZExtValue());
2577-
2578-
BuildMI(*BB, &I, DL, TII.get(Opcode), HiReg)
2579-
.addImm(Imm.ashr(32).getZExtValue());
2580-
2581-
ResInst = BuildMI(*BB, &I, DL, TII.get(AMDGPU::REG_SEQUENCE), DstReg)
2582-
.addReg(LoReg)
2583-
.addImm(AMDGPU::sub0)
2584-
.addReg(HiReg)
2585-
.addImm(AMDGPU::sub1);
2586-
}
2587-
2588-
// We can't call constrainSelectedInstRegOperands here, because it doesn't
2589-
// work for target independent opcodes
2590-
I.eraseFromParent();
2591-
const TargetRegisterClass *DstRC =
2592-
TRI.getConstrainedRegClassForOperand(ResInst->getOperand(0), *MRI);
2593-
if (!DstRC)
2594-
return true;
2595-
return RBI.constrainGenericRegister(DstReg, *DstRC, *MRI);
2596-
}
2597-
25982506
bool AMDGPUInstructionSelector::selectG_FNEG(MachineInstr &MI) const {
25992507
// Only manually handle the f64 SGPR case.
26002508
//
@@ -3521,9 +3429,6 @@ bool AMDGPUInstructionSelector::select(MachineInstr &I) {
35213429
case TargetOpcode::G_PTRTOINT:
35223430
case TargetOpcode::G_FREEZE:
35233431
return selectCOPY(I);
3524-
case TargetOpcode::G_CONSTANT:
3525-
case TargetOpcode::G_FCONSTANT:
3526-
return selectG_CONSTANT(I);
35273432
case TargetOpcode::G_FNEG:
35283433
if (selectImpl(I, *CoverageInfo))
35293434
return true;
@@ -3629,6 +3534,8 @@ bool AMDGPUInstructionSelector::select(MachineInstr &I) {
36293534
return selectStackRestore(I);
36303535
case AMDGPU::G_PHI:
36313536
return selectPHI(I);
3537+
case TargetOpcode::G_CONSTANT:
3538+
case TargetOpcode::G_FCONSTANT:
36323539
default:
36333540
return selectImpl(I, *CoverageInfo);
36343541
}

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ class AMDGPUInstructionSelector final : public InstructionSelector {
9191
bool selectG_TRUNC(MachineInstr &I) const;
9292
bool selectG_SZA_EXT(MachineInstr &I) const;
9393
bool selectG_FPEXT(MachineInstr &I) const;
94-
bool selectG_CONSTANT(MachineInstr &I) const;
9594
bool selectG_FNEG(MachineInstr &I) const;
9695
bool selectG_FABS(MachineInstr &I) const;
9796
bool selectG_AND_OR_XOR(MachineInstr &I) const;

llvm/lib/Target/AMDGPU/SIInstructions.td

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,15 +2140,17 @@ def : GCNPat <
21402140

21412141
// FIXME: Remove VGPRImm. Should be inferrable from register bank.
21422142

2143-
def : GCNPat <
2144-
(VGPRImm<(i32 imm)>:$imm),
2145-
(V_MOV_B32_e32 imm:$imm)
2146-
>;
2143+
foreach vt = [i32, p3, p5, p6, p2] in {
2144+
def : GCNPat <
2145+
(VGPRImm<(vt imm)>:$imm),
2146+
(V_MOV_B32_e32 imm:$imm)
2147+
>;
21472148

2148-
def : GCNPat <
2149-
(i32 imm:$imm),
2150-
(S_MOV_B32 imm:$imm)
2151-
>;
2149+
def : GCNPat <
2150+
(vt imm:$imm),
2151+
(S_MOV_B32 imm:$imm)
2152+
>;
2153+
}
21522154

21532155
def : GCNPat <
21542156
(p5 frameindex:$fi),
@@ -2257,20 +2259,22 @@ def : GCNPat <
22572259
(S_MOV_B32 (f32 (bitcast_fpimm_to_i32 $imm)))
22582260
>;
22592261

2260-
def : GCNPat <
2261-
(VGPRImm<(i64 imm)>:$imm),
2262-
(V_MOV_B64_PSEUDO imm:$imm)
2263-
>;
2262+
foreach vt = [i64, p1, p0, p4] in { // FIXME: Should accept arbitrary addrspace
2263+
def : GCNPat <
2264+
(VGPRImm<(vt imm)>:$imm),
2265+
(V_MOV_B64_PSEUDO imm:$imm)
2266+
>;
22642267

2265-
def : GCNPat <
2266-
(i64 InlineImm64:$imm),
2267-
(S_MOV_B64 InlineImm64:$imm)
2268-
>;
2268+
def : GCNPat <
2269+
(vt InlineImm64:$imm),
2270+
(S_MOV_B64 InlineImm64:$imm)
2271+
>;
22692272

2270-
def : GCNPat <
2271-
(i64 imm:$imm),
2272-
(S_MOV_B64_IMM_PSEUDO imm:$imm)
2273-
>;
2273+
def : GCNPat <
2274+
(vt imm:$imm),
2275+
(S_MOV_B64_IMM_PSEUDO imm:$imm)
2276+
>;
2277+
}
22742278

22752279
def : GCNPat <
22762280
(VGPRImm<(f64 fpimm)>:$imm),

0 commit comments

Comments
 (0)