[llvm] r331095 - [MachineOutliner] Add defs to calls + don't track liveness on outlined functions

Jessica Paquette via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 27 16:36:35 PDT 2018


Author: paquette
Date: Fri Apr 27 16:36:35 2018
New Revision: 331095

URL: http://llvm.org/viewvc/llvm-project?rev=331095&view=rev
Log:
[MachineOutliner] Add defs to calls + don't track liveness on outlined functions

This commit makes it so that if you outline a def of some register, then the
call instruction created by the outliner actually reflects that the register
is defined by the call. It also makes it so that outlined functions don't
have the TracksLiveness property.

Outlined calls shouldn't break liveness assumptions that someone might make.

This also un-XFAILs the noredzone test, and updates the calls test.


Modified:
    llvm/trunk/lib/CodeGen/MachineOutliner.cpp
    llvm/trunk/lib/Target/AArch64/AArch64InstrInfo.cpp
    llvm/trunk/test/CodeGen/AArch64/machine-outliner-calls.mir
    llvm/trunk/test/CodeGen/AArch64/machine-outliner-noredzone.ll

Modified: llvm/trunk/lib/CodeGen/MachineOutliner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineOutliner.cpp?rev=331095&r1=331094&r2=331095&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineOutliner.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineOutliner.cpp Fri Apr 27 16:36:35 2018
@@ -1323,6 +1323,8 @@ MachineOutliner::createOutlinedFunction(
     DB.finalize();
   }
 
+  // Outlined functions shouldn't preserve liveness.
+  MF.getProperties().reset(MachineFunctionProperties::Property::TracksLiveness);
   MF.getRegInfo().freezeReservedRegs(MF);
   return &MF;
 }
@@ -1357,8 +1359,6 @@ bool MachineOutliner::outline(
     MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
     assert(EndIt != MBB->end() && "EndIt out of bounds!");
 
-    EndIt++; // Erase needs one past the end index.
-
     // Does this candidate have a function yet?
     if (!OF.MF) {
       OF.MF = createOutlinedFunction(M, OF, Mapper);
@@ -1401,10 +1401,40 @@ bool MachineOutliner::outline(
     const TargetInstrInfo &TII = *STI.getInstrInfo();
 
     // Insert a call to the new function and erase the old sequence.
-    TII.insertOutlinedCall(M, *MBB, StartIt, *MF, C.MInfo);
+    auto CallInst = TII.insertOutlinedCall(M, *MBB, StartIt, *MF, C.MInfo);
     StartIt = Mapper.InstrList[C.getStartIdx()];
-    MBB->erase(StartIt, EndIt);
 
+    // If the caller tracks liveness, then we need to make sure that anything
+    // we outline doesn't break liveness assumptions.
+    // The outlined functions themselves currently don't track liveness, but
+    // we should make sure that the ranges we yank things out of aren't
+    // wrong.
+    if (MBB->getParent()->getProperties().hasProperty(
+            MachineFunctionProperties::Property::TracksLiveness)) {
+      // Helper lambda for adding implicit def operands to the call instruction.
+      auto CopyDefs = [&CallInst](MachineInstr &MI) {
+        for (MachineOperand &MOP : MI.operands()) {
+          // Skip over anything that isn't a register.
+          if (!MOP.isReg())
+            continue;
+
+          // If it's a def, add it to the call instruction.
+          if (MOP.isDef())
+            CallInst->addOperand(
+                MachineOperand::CreateReg(MOP.getReg(), true, /* isDef = true */
+                                          true /* isImp = true */));
+        }
+      };
+
+      // Copy over the defs in the outlined range.
+      // First inst in outlined range <-- Anything that's defined in this
+      // ...                           .. range has to be added as an implicit
+      // Last inst in outlined range  <-- def to the call instruction.
+      std::for_each(CallInst, EndIt, CopyDefs);
+    }
+
+    EndIt++; // Erase needs one past the end index.
+    MBB->erase(StartIt, EndIt);
     OutlinedSomething = true;
 
     // Statistics.

Modified: llvm/trunk/lib/Target/AArch64/AArch64InstrInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64InstrInfo.cpp?rev=331095&r1=331094&r2=331095&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AArch64/AArch64InstrInfo.cpp (original)
+++ llvm/trunk/lib/Target/AArch64/AArch64InstrInfo.cpp Fri Apr 27 16:36:35 2018
@@ -5351,6 +5351,9 @@ MachineBasicBlock::iterator AArch64Instr
     return It;
   }
 
+  // We want to return the spot where we inserted the call.
+  MachineBasicBlock::iterator CallPt;
+
   // We have a default call. Save the link register.
   MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
                               .addReg(AArch64::SP, RegState::Define)
@@ -5363,7 +5366,7 @@ MachineBasicBlock::iterator AArch64Instr
   // Insert the call.
   It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
                           .addGlobalAddress(M.getNamedValue(MF.getName())));
-
+  CallPt = It;
   It++;
 
   // Restore the link register.
@@ -5374,5 +5377,5 @@ MachineBasicBlock::iterator AArch64Instr
                                .addImm(16);
   It = MBB.insert(It, LDRXpost);
 
-  return It;
+  return CallPt;
 }

Modified: llvm/trunk/test/CodeGen/AArch64/machine-outliner-calls.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/machine-outliner-calls.mir?rev=331095&r1=331094&r2=331095&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/AArch64/machine-outliner-calls.mir (original)
+++ llvm/trunk/test/CodeGen/AArch64/machine-outliner-calls.mir Fri Apr 27 16:36:35 2018
@@ -56,7 +56,6 @@ body:             |
 
 # CHECK: name:            OUTLINED_FUNCTION_0
 # CHECK-DAG: bb.0:
-# CHECK-NEXT: liveins: $lr
 # CHECK-DAG: frame-setup CFI_INSTRUCTION def_cfa_offset -16
 # CHECK-NEXT: frame-setup CFI_INSTRUCTION offset $w30, 16
 # CHECK-NEXT: early-clobber $sp = STRXpre $lr, $sp, -16

Modified: llvm/trunk/test/CodeGen/AArch64/machine-outliner-noredzone.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/machine-outliner-noredzone.ll?rev=331095&r1=331094&r2=331095&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/AArch64/machine-outliner-noredzone.ll (original)
+++ llvm/trunk/test/CodeGen/AArch64/machine-outliner-noredzone.ll Fri Apr 27 16:36:35 2018
@@ -1,4 +1,3 @@
-; XFAIL: *
 ; RUN: llc -verify-machineinstrs -enable-machine-outliner %s -o - | FileCheck %s
 ; RUN: llc -verify-machineinstrs -enable-machine-outliner -aarch64-redzone %s -o - | FileCheck %s -check-prefix=REDZONE
 




More information about the llvm-commits mailing list