[llvm-commits] [llvm] r134577 - in /llvm/trunk: include/llvm/Target/TargetAsmInfo.h include/llvm/Target/TargetFrameLowering.h lib/MC/MCDwarf.cpp lib/Target/TargetAsmInfo.cpp lib/Target/X86/X86FrameLowering.cpp lib/Target/X86/X86FrameLowering.h

Bill Wendling isanbard at gmail.com
Wed Jul 6 17:54:13 PDT 2011


Author: void
Date: Wed Jul  6 19:54:13 2011
New Revision: 134577

URL: http://llvm.org/viewvc/llvm-project?rev=134577&view=rev
Log:
Add a target hook to encode the compact unwind information.

Modified:
    llvm/trunk/include/llvm/Target/TargetAsmInfo.h
    llvm/trunk/include/llvm/Target/TargetFrameLowering.h
    llvm/trunk/lib/MC/MCDwarf.cpp
    llvm/trunk/lib/Target/TargetAsmInfo.cpp
    llvm/trunk/lib/Target/X86/X86FrameLowering.cpp
    llvm/trunk/lib/Target/X86/X86FrameLowering.h

Modified: llvm/trunk/include/llvm/Target/TargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetAsmInfo.h?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetAsmInfo.h (original)
+++ llvm/trunk/include/llvm/Target/TargetAsmInfo.h Wed Jul  6 19:54:13 2011
@@ -30,8 +30,9 @@
   unsigned PointerSize;
   bool IsLittleEndian;
   TargetFrameLowering::StackDirection StackDir;
-  const TargetRegisterInfo *TRI;
   std::vector<MachineMove> InitialFrameState;
+  const TargetRegisterInfo *TRI;
+  const TargetFrameLowering *TFI;
   const TargetLoweringObjectFile *TLOF;
 
 public:
@@ -83,6 +84,11 @@
     return TLOF->isFunctionEHFrameSymbolPrivate();
   }
 
+  int getCompactUnwindEncoding(const std::vector<MCCFIInstruction> &Instrs,
+                               int DataAlignmentFactor, bool IsEH) const {
+    return TFI->getCompactUnwindEncoding(Instrs, DataAlignmentFactor, IsEH);
+  }
+
   const unsigned *getCalleeSavedRegs(MachineFunction *MF = 0) const {
     return TRI->getCalleeSavedRegs(MF);
   }
@@ -106,10 +112,6 @@
   int getSEHRegNum(unsigned RegNum) const {
     return TRI->getSEHRegNum(RegNum);
   }
-
-  int getCompactUnwindRegNum(unsigned RegNum, bool isEH) const {
-    return TRI->getCompactUnwindRegNum(RegNum, isEH);
-  }
 };
 
 }

Modified: llvm/trunk/include/llvm/Target/TargetFrameLowering.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetFrameLowering.h?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetFrameLowering.h (original)
+++ llvm/trunk/include/llvm/Target/TargetFrameLowering.h Wed Jul  6 19:54:13 2011
@@ -14,6 +14,7 @@
 #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
 #define LLVM_TARGET_TARGETFRAMELOWERING_H
 
+#include "llvm/MC/MCDwarf.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 
 #include <utility>
@@ -189,6 +190,14 @@
   ///
   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
   }
+
+  /// getCompactUnwindEncoding - Get the compact unwind encoding for the
+  /// function. Return 0 if the compact unwind isn't available.
+  virtual uint32_t getCompactUnwindEncoding(const std::vector<MCCFIInstruction>&,
+                                            int /*DataAlignmentFactor*/,
+                                            bool /*IsEH*/) const {
+    return 0;
+  }
 };
 
 } // End llvm namespace

Modified: llvm/trunk/lib/MC/MCDwarf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCDwarf.cpp?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCDwarf.cpp (original)
+++ llvm/trunk/lib/MC/MCDwarf.cpp Wed Jul  6 19:54:13 2011
@@ -499,7 +499,6 @@
     bool UsingCFI;
     bool IsEH;
     const MCSymbol *SectionStart;
-
   public:
     FrameEmitterImpl(bool usingCFI, bool isEH, const MCSymbol *sectionStart) :
       CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
@@ -714,6 +713,11 @@
   //   .quad __gxx_personality
   //   .quad except_tab1
 
+  uint32_t Encoding =
+    TAI.getCompactUnwindEncoding(Frame.Instructions,
+                                 getDataAlignmentFactor(Streamer), IsEH);
+  if (!Encoding) return false;
+
   Streamer.SwitchSection(TAI.getCompactUnwindSection());
 
   // Range Start
@@ -728,12 +732,10 @@
   if (VerboseAsm) Streamer.AddComment("Range Length");
   Streamer.EmitAbsValue(Range, 4);
 
-  // FIXME:
   // Compact Encoding
-  const std::vector<MachineMove> &Moves = TAI.getInitialFrameState();
-  uint32_t Encoding = 0;
   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
-  if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding");
+  if (VerboseAsm) Streamer.AddComment(Twine("Compact Unwind Encoding: 0x") +
+                                      Twine(llvm::utohexstr(Encoding)));
   Streamer.EmitIntValue(Encoding, Size);
 
   // Personality Function
@@ -774,7 +776,7 @@
   streamer.EmitLabel(sectionStart);
   CIENum++;
 
-  MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol();
+  MCSymbol *sectionEnd = context.CreateTempSymbol();
 
   // Length
   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,

Modified: llvm/trunk/lib/Target/TargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetAsmInfo.cpp?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/lib/Target/TargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/TargetAsmInfo.cpp Wed Jul  6 19:54:13 2011
@@ -20,8 +20,9 @@
   const TargetData &TD = *TM.getTargetData();
   IsLittleEndian = TD.isLittleEndian();
   PointerSize = TD.getPointerSize();
-  const TargetFrameLowering &TFI = *TM.getFrameLowering();
-  StackDir = TFI.getStackGrowthDirection();
+
+  TFI = TM.getFrameLowering();
+  StackDir = TFI->getStackGrowthDirection();
   TRI = TM.getRegisterInfo();
-  TFI.getInitialFrameState(InitialFrameState);
+  TFI->getInitialFrameState(InitialFrameState);
 }

Modified: llvm/trunk/lib/Target/X86/X86FrameLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FrameLowering.cpp?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86FrameLowering.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86FrameLowering.cpp Wed Jul  6 19:54:13 2011
@@ -23,6 +23,7 @@
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCSymbol.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Support/CommandLine.h"
@@ -1029,3 +1030,100 @@
     FrameIdx = 0;
   }
 }
+
+uint32_t X86FrameLowering::
+getCompactUnwindEncoding(const std::vector<MCCFIInstruction> &Instrs,
+                         int DataAlignmentFactor, bool IsEH) const {
+  uint32_t Encoding = 0;
+  int CFAOffset = 0;
+  const TargetRegisterInfo *TRI = TM.getRegisterInfo();
+  SmallVector<unsigned, 8> SavedRegs;
+  int FramePointerReg = -1;
+
+  for (std::vector<MCCFIInstruction>::const_iterator
+         I = Instrs.begin(), E = Instrs.end(); I != E; ++I) {
+    const MCCFIInstruction &Inst = *I;
+    MCSymbol *Label = Inst.getLabel();
+
+    // Ignore invalid labels.
+    if (Label && !Label->isDefined()) continue;
+
+    unsigned Operation = Inst.getOperation();
+    if (Operation != MCCFIInstruction::Move &&
+        Operation != MCCFIInstruction::RelMove)
+      // FIXME: We can't handle this frame just yet.
+      return 0;
+
+    const MachineLocation &Dst = Inst.getDestination();
+    const MachineLocation &Src = Inst.getSource();
+    const bool IsRelative = (Operation == MCCFIInstruction::RelMove);
+
+    if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
+      if (Src.getReg() == MachineLocation::VirtualFP) {
+        // DW_CFA_def_cfa_offset
+        if (IsRelative)
+          CFAOffset += Src.getOffset();
+        else
+          CFAOffset = -Src.getOffset();
+      } // else DW_CFA_def_cfa
+
+      continue;
+    }
+
+    if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
+      // DW_CFA_def_cfa_register
+      FramePointerReg = Dst.getReg();
+      continue;
+    }
+
+    unsigned Reg = Src.getReg();
+    int Offset = Dst.getOffset();
+    if (IsRelative)
+      Offset -= CFAOffset;
+    Offset /= DataAlignmentFactor;
+
+    if (Offset < 0) {
+      // FIXME: Handle?
+      // DW_CFA_offset_extended_sf
+      return 0;
+    } else if (Reg < 64) {
+      // DW_CFA_offset + Reg
+      SavedRegs.push_back(Reg);
+    } else {
+      // FIXME: Handle?
+      // DW_CFA_offset_extended
+      return 0;
+    }
+  }
+
+  CFAOffset /= 4;
+
+  // Check if the offset is too big.
+  if ((CFAOffset & 0xFF) != CFAOffset)
+    return 0;
+
+  // Bail if there are too many registers to encode.
+  unsigned NumRegsToEncode = SavedRegs.size() - (FramePointerReg != -1 ? 1 : 0);
+  if (NumRegsToEncode > 5) return 0;
+
+  if (TRI->getLLVMRegNum(FramePointerReg, IsEH) != X86::EBP &&
+      TRI->getLLVMRegNum(FramePointerReg, IsEH) != X86::RBP)
+    // FIXME: Handle frameless version!
+    return 0;
+
+  Encoding |= 1 << 24;
+  Encoding |= (CFAOffset & 0xFF) << 16;
+
+  unsigned Idx = 0;
+  for (SmallVectorImpl<unsigned>::iterator
+         I = SavedRegs.begin(), E = SavedRegs.end(); I != E; ++I) {
+    if (*I == unsigned(FramePointerReg)) continue;
+
+    int CURegNum = TRI->getCompactUnwindRegNum(*I, IsEH);
+    if (CURegNum == -1) return 0;
+
+    Encoding |= (CURegNum & 0x7) << (Idx++ * 3);
+  }
+
+  return Encoding;
+}

Modified: llvm/trunk/lib/Target/X86/X86FrameLowering.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FrameLowering.h?rev=134577&r1=134576&r2=134577&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86FrameLowering.h (original)
+++ llvm/trunk/lib/Target/X86/X86FrameLowering.h Wed Jul  6 19:54:13 2011
@@ -15,6 +15,7 @@
 #define X86_FRAMELOWERING_H
 
 #include "X86Subtarget.h"
+#include "llvm/MC/MCDwarf.h"
 #include "llvm/Target/TargetFrameLowering.h"
 
 namespace llvm {
@@ -58,6 +59,9 @@
 
   void getInitialFrameState(std::vector<MachineMove> &Moves) const;
   int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
+
+  uint32_t getCompactUnwindEncoding(const std::vector<MCCFIInstruction> &Instrs,
+                                    int DataAlignmentFactor, bool IsEH) const;
 };
 
 } // End llvm namespace





More information about the llvm-commits mailing list