@@ -1327,6 +1327,33 @@ Register SIInstrInfo::insertNE(MachineBasicBlock *MBB,
1327
1327
return Reg;
1328
1328
}
1329
1329
1330
+ bool SIInstrInfo::getConstValDefinedInReg (const MachineInstr &MI,
1331
+ const Register Reg,
1332
+ int64_t &ImmVal) const {
1333
+ // TODO: Handle all the special cases handled in SIShrinkInstructions
1334
+ // (e.g. s_brev_b32 imm -> reverse(imm))
1335
+ switch (MI.getOpcode ()) {
1336
+ case AMDGPU::V_MOV_B32_e32:
1337
+ case AMDGPU::S_MOV_B32:
1338
+ case AMDGPU::S_MOVK_I32:
1339
+ case AMDGPU::S_MOV_B64:
1340
+ case AMDGPU::V_MOV_B64_e32:
1341
+ case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
1342
+ case AMDGPU::S_MOV_B64_IMM_PSEUDO:
1343
+ case AMDGPU::V_MOV_B64_PSEUDO: {
1344
+ const MachineOperand &Src0 = MI.getOperand (1 );
1345
+ if (Src0.isImm ()) {
1346
+ ImmVal = Src0.getImm ();
1347
+ return MI.getOperand (0 ).getReg () == Reg;
1348
+ }
1349
+
1350
+ return false ;
1351
+ }
1352
+ default :
1353
+ return false ;
1354
+ }
1355
+ }
1356
+
1330
1357
unsigned SIInstrInfo::getMovOpcode (const TargetRegisterClass *DstRC) const {
1331
1358
1332
1359
if (RI.isAGPRClass (DstRC))
@@ -3395,27 +3422,11 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
3395
3422
if (!MRI->hasOneNonDBGUse (Reg))
3396
3423
return false ;
3397
3424
3398
- switch (DefMI.getOpcode ()) {
3399
- default :
3400
- return false ;
3401
- case AMDGPU::V_MOV_B64_e32:
3402
- case AMDGPU::S_MOV_B64:
3403
- case AMDGPU::V_MOV_B64_PSEUDO:
3404
- case AMDGPU::S_MOV_B64_IMM_PSEUDO:
3405
- case AMDGPU::V_MOV_B32_e32:
3406
- case AMDGPU::S_MOV_B32:
3407
- case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
3408
- break ;
3409
- }
3410
-
3411
- const MachineOperand *ImmOp = getNamedOperand (DefMI, AMDGPU::OpName::src0);
3412
- assert (ImmOp);
3413
- // FIXME: We could handle FrameIndex values here.
3414
- if (!ImmOp->isImm ())
3425
+ int64_t Imm;
3426
+ if (!getConstValDefinedInReg (DefMI, Reg, Imm))
3415
3427
return false ;
3416
3428
3417
- auto getImmFor = [ImmOp](const MachineOperand &UseOp) -> int64_t {
3418
- int64_t Imm = ImmOp->getImm ();
3429
+ auto getImmFor = [=](const MachineOperand &UseOp) -> int64_t {
3419
3430
switch (UseOp.getSubReg ()) {
3420
3431
default :
3421
3432
return Imm;
@@ -3502,12 +3513,14 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
3502
3513
// If this is a free constant, there's no reason to do this.
3503
3514
// TODO: We could fold this here instead of letting SIFoldOperands do it
3504
3515
// later.
3505
- MachineOperand *Src0 = getNamedOperand (UseMI, AMDGPU::OpName::src0);
3516
+ int Src0Idx = getNamedOperandIdx (UseMI. getOpcode () , AMDGPU::OpName::src0);
3506
3517
3507
3518
// Any src operand can be used for the legality check.
3508
- if (isInlineConstant (UseMI, *Src0, *ImmOp ))
3519
+ if (isInlineConstant (UseMI, Src0Idx, Imm ))
3509
3520
return false ;
3510
3521
3522
+ MachineOperand *Src0 = &UseMI.getOperand (Src0Idx);
3523
+
3511
3524
bool IsF32 = Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 ||
3512
3525
Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64;
3513
3526
bool IsFMA =
@@ -4267,18 +4280,11 @@ bool SIInstrInfo::isInlineConstant(const APFloat &Imm) const {
4267
4280
}
4268
4281
}
4269
4282
4270
- bool SIInstrInfo::isInlineConstant (const MachineOperand &MO,
4271
- uint8_t OperandType) const {
4272
- assert (!MO.isReg () && " isInlineConstant called on register operand!" );
4273
- if (!MO.isImm ())
4274
- return false ;
4275
-
4283
+ bool SIInstrInfo::isInlineConstant (int64_t Imm, uint8_t OperandType) const {
4276
4284
// MachineOperand provides no way to tell the true operand size, since it only
4277
4285
// records a 64-bit value. We need to know the size to determine if a 32-bit
4278
4286
// floating point immediate bit pattern is legal for an integer immediate. It
4279
4287
// would be for any 32-bit integer operand, but would not be for a 64-bit one.
4280
-
4281
- int64_t Imm = MO.getImm ();
4282
4288
switch (OperandType) {
4283
4289
case AMDGPU::OPERAND_REG_IMM_INT32:
4284
4290
case AMDGPU::OPERAND_REG_IMM_FP32:
@@ -4300,8 +4306,7 @@ bool SIInstrInfo::isInlineConstant(const MachineOperand &MO,
4300
4306
case AMDGPU::OPERAND_REG_INLINE_C_INT64:
4301
4307
case AMDGPU::OPERAND_REG_INLINE_C_FP64:
4302
4308
case AMDGPU::OPERAND_REG_INLINE_AC_FP64:
4303
- return AMDGPU::isInlinableLiteral64 (MO.getImm (),
4304
- ST.hasInv2PiInlineImm ());
4309
+ return AMDGPU::isInlinableLiteral64 (Imm, ST.hasInv2PiInlineImm ());
4305
4310
case AMDGPU::OPERAND_REG_IMM_INT16:
4306
4311
case AMDGPU::OPERAND_REG_INLINE_C_INT16:
4307
4312
case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
0 commit comments