[PATCH] D37874: [TwoAddressInstructionPass] When prepending COPYs when processing tied operands, take the register class constraint from the instruction operands not the register we're copying.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 16:20:56 PDT 2017


craig.topper created this revision.
Herald added subscribers: wdng, nemanjai.

We're creating a new copy here and I think all that's important is that the result of the copy satisfies the instruction that's using it. The register we're copying from may have been overconstrained by the requirements of other users and we don't need to keep those constraints if we're making a copy.

I observed this when I tried to remove the explicit copies in X86InstrCompiler.td

  def : Pat<(i8 (trunc GR32:$src)),
            (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
                            sub_8bit)>,
        Requires<[Not64BitMode]>;
  def : Pat<(i8 (trunc GR16:$src)),
            (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
                            sub_8bit)>,
        Requires<[Not64BitMode]>;

The EXTRACT_SUBREG handling in InstrEmitter.cpp should make these unnecessary by constraining the register class for the input to COPY that does the extract or emitting a new COPY before the extract.   If it uses the constraint, then that constraint can be propagated to the result of a COPY introduced by the two address instruction pass.

Unfortunately, this seems to have broken a PowerPC test because there is a check in the pass to only handle COPY with the same source and dest class and the copy it was finding came from the two address instruction pass.


https://reviews.llvm.org/D37874

Files:
  lib/CodeGen/TwoAddressInstructionPass.cpp
  test/CodeGen/PowerPC/fma-mutate.ll


Index: test/CodeGen/PowerPC/fma-mutate.ll
===================================================================
--- test/CodeGen/PowerPC/fma-mutate.ll
+++ test/CodeGen/PowerPC/fma-mutate.ll
@@ -15,7 +15,7 @@
 
 ; CHECK: @foo3
 ; CHECK: xsnmsubadp [[REG:[0-9]+]], {{[0-9]+}}, [[REG]]
-; CHECK: xsmaddmdp
+; CHECK: xsmaddadp
 ; CHECK: xsmaddadp
 }
 
Index: lib/CodeGen/TwoAddressInstructionPass.cpp
===================================================================
--- lib/CodeGen/TwoAddressInstructionPass.cpp
+++ lib/CodeGen/TwoAddressInstructionPass.cpp
@@ -1518,7 +1518,8 @@
     // If this operand is folding a truncation, the truncation now moves to the
     // copy so that the register classes remain valid for the operands.
     MIB.addReg(RegB, 0, SubRegB);
-    const TargetRegisterClass *RC = MRI->getRegClass(RegB);
+    const TargetRegisterClass *RC = TII->getRegClass(MI->getDesc(), SrcIdx, TRI,
+                                                     *MF);
     if (SubRegB) {
       if (TargetRegisterInfo::isVirtualRegister(RegA)) {
         assert(TRI->getMatchingSuperRegClass(RC, MRI->getRegClass(RegA),
@@ -1578,15 +1579,21 @@
   if (AllUsesCopied) {
     if (!IsEarlyClobber) {
       // Replace other (un-tied) uses of regB with LastCopiedReg.
-      for (MachineOperand &MO : MI->operands()) {
+      for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
+        MachineOperand &MO = MI->getOperand(i);
         if (MO.isReg() && MO.getReg() == RegB &&
             MO.isUse()) {
           if (MO.isKill()) {
             MO.setIsKill(false);
             RemovedKillFlag = true;
           }
           MO.setReg(LastCopiedReg);
           MO.setSubReg(MO.getSubReg());
+          // Apply any constraints for this operand.
+          if (TargetRegisterInfo::isVirtualRegister(LastCopiedReg))
+            MRI->constrainRegClass(LastCopiedReg,
+                                   TII->getRegClass(MI->getDesc(), i, TRI,
+                                                    *MF));
         }
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37874.115313.patch
Type: text/x-patch
Size: 2032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170914/729b91b0/attachment.bin>


More information about the llvm-commits mailing list