[mono/llvm] [4 commits] bc88a14e: Workaround some problems in the ARMConstantIsland pass.

"Zoltan Varga ([email protected])" <[email protected]> Tue, 12 Nov 2013 23:43:43 +0000
Newsgroups gmane.comp.gnome.mono.patches
Message-ID <000001424eb300cc-01f62000-123c-4be1-b527-6c8f9a030770-000000@email.amazonses.com>
   Branch: refs/heads/mono2
     Home: https://github.com/mono/llvm
  Compare: https://github.com/mono/llvm/compare/21397d0dfa56...ff9b28af498c

   Commit: bc88a14e06b9b02940e816ba6ae86edb5fdd89c8
   Author: Zoltan Varga <[email protected]> (vargaz)
     Date: 2013-11-12 22:27:40 GMT
      URL: https://github.com/mono/llvm/commit/bc88a14e06b9b02940e816ba6ae86edb5fdd89c8

Workaround some problems in the ARMConstantIsland pass.

Changed paths:
  M lib/Target/ARM/ARMConstantIslandPass.cpp

Modified: lib/Target/ARM/ARMConstantIslandPass.cpp
===================================================================
@@ -777,7 +777,8 @@ void ARMConstantIslands::scanFunctionJumpTables() {
           // Remember that this is a user of a CP entry.
           unsigned CPI = I->getOperand(op).getIndex();
           MachineInstr *CPEMI = CPEMIs[CPI];
-          unsigned MaxOffs = ((1 << Bits)-1) * Scale;
+          // Substract 64 as a workaround for overflow problems when targeting thumb2
+          unsigned MaxOffs = (((1 << Bits)-1) * Scale) - 64;
           CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
 
           // Increment corresponding CPEntry reference count.
@@ -1936,6 +1937,10 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
 
       DEBUG(dbgs() << "Shrink JT: " << *MI << "     addr: " << *AddrMI
                    << "      lea: " << *LeaMI);
+
+      // The .byte directives generated by this optimization sometimes overflow
+      continue;
+
       unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
       MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
         .addReg(IdxReg, getKillRegState(IdxRegKill))

   Commit: d880295cd0d5e521c1e4b57c19454c19644fa4bc
   Author: Zoltan Varga <[email protected]> (vargaz)
     Date: 2013-11-12 22:31:10 GMT
      URL: https://github.com/mono/llvm/commit/d880295cd0d5e521c1e4b57c19454c19644fa4bc

Add a mono specific flag in the form of custom metadata, toh make it possible/easier for LLVM to hoist loads out of loops.

Changed paths:
  M lib/Analysis/ValueTracking.cpp

Modified: lib/Analysis/ValueTracking.cpp
===================================================================
@@ -2008,6 +2008,11 @@ bool llvm::isSafeToSpeculativelyExecute(const Value *V,
     const LoadInst *LI = cast<LoadInst>(Inst);
     if (!LI->isUnordered())
       return false;
+    //
+    // Mono extension, the metadata identifies loads which can't fail.
+    //
+    if (LI->getMetadata("mono.nofail.load"))
+      return true;
     return LI->getPointerOperand()->isDereferenceablePointer();
   }
   case Instruction::Call: {

   Commit: 4e5efd72a38485cd623e29e542e83a04c54bdcf5
   Author: Zoltan Varga <[email protected]> (vargaz)
     Date: 2013-11-12 23:28:23 GMT
      URL: https://github.com/mono/llvm/commit/4e5efd72a38485cd623e29e542e83a04c54bdcf5

Generate DWARF CFI unwind info on arm.

Changed paths:
  M lib/Target/ARM/ARMFrameLowering.cpp
  M lib/Target/ARM/Thumb1FrameLowering.cpp

Modified: lib/Target/ARM/ARMFrameLowering.cpp
===================================================================
@@ -20,6 +20,7 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/RegisterScavenging.h"
 #include "llvm/IR/CallingConv.h"
 #include "llvm/IR/Function.h"
@@ -112,6 +113,9 @@ static bool isCSRestore(MachineInstr *MI,
       MI->getOperand(1).getReg() == ARM::SP)
     return true;
 
+  if ((MI->getOpcode() == ARM::LDR_POST_IMM || MI->getOpcode() == ARM::LDR_POST_REG) && isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
+    return true;
+
   return false;
 }
 
@@ -129,6 +133,49 @@ static bool isCSRestore(MachineInstr *MI,
                            Pred, PredReg, TII, MIFlags);
 }
 
+//
+// Functions to emit CFI Instructions.
+//
+
+static void
+emitDefCfaOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+                 DebugLoc dl, const ARMBaseInstrInfo &TII,
+                 MachineModuleInfo &MMI,
+                 MCSymbol *FrameLabel,
+                 int CfaOffset) {
+  if (!FrameLabel) {
+    FrameLabel = MMI.getContext().CreateTempSymbol();
+    BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+  }
+  MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(FrameLabel, CfaOffset));
+}
+
+static void
+emitDefCfa(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+           DebugLoc dl, const ARMBaseInstrInfo &TII,
+		   MachineModuleInfo &MMI,
+           int CfaReg, int CfaOffset) {
+  const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
+
+  MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol();
+  BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+
+  unsigned DwarfReg = MRI->getDwarfRegNum(CfaReg, true);
+  MMI.addFrameInst(MCCFIInstruction::createDefCfa(FrameLabel, DwarfReg, CfaOffset));
+}
+
+static void
+emitCfaOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+              DebugLoc dl, const ARMBaseInstrInfo &TII,
+              MachineModuleInfo &MMI,
+              MCSymbol *FrameLabel,
+              int Reg, int Offset) {
+  const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
+
+  unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
+  MMI.addFrameInst(MCCFIInstruction::createOffset(FrameLabel, DwarfReg, Offset));
+}
+
 void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
   MachineBasicBlock &MBB = MF.front();
   MachineBasicBlock::iterator MBBI = MBB.begin();
@@ -147,6 +194,12 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
   unsigned FramePtr = RegInfo->getFrameRegister(MF);
+  MachineModuleInfo &MMI = MF.getMMI();
+  bool NeedsFrameInfo = MF.getFunction()->needsUnwindTableEntry();
+  // The cfa register
+  int CfaReg = ARM::SP;
+  // The offset between the value of CfaReg and the CFA
+  int CfaOffset = 0;
 
   // Determine the sizes of each callee-save spill areas and record which frame
   // belongs to which callee-save spill areas.
@@ -163,11 +216,15 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
   if (ArgRegsSaveSize)
     emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
                  MachineInstr::FrameSetup);
+  CfaOffset += ArgRegsSaveSize;
 
   if (!AFI->hasStackFrame()) {
     if (NumBytes != 0)
       emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
                    MachineInstr::FrameSetup);
+    CfaOffset += NumBytes;
+    if (NeedsFrameInfo && CfaOffset)
+      emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, NULL, CfaOffset);
     return;
   }
 
@@ -218,6 +275,28 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
   // Move past area 1.
   if (GPRCS1Size > 0) MBBI++;
 
+  CfaOffset += GPRCS1Size;
+  if (NeedsFrameInfo) {
+    MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol();
+    BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+    // CFA = sp + offset
+    emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, FrameLabel, CfaOffset);
+
+    // Emit moves for the registers in spill area 1
+    for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
+      unsigned Reg = CSI[i].getReg();
+      int FI = CSI[i].getFrameIdx();
+      int64_t Offset = MFI->getObjectOffset(FI);
+
+      // The offset is relative to the incoming stack pointer which is
+      // the cfa
+      if (AFI->isGPRCalleeSavedArea1Frame(FI)) {
+        // Reg is saved at cfa + offset
+        emitCfaOffset(MBB, MBBI, dl, TII, MMI, FrameLabel, Reg, Offset);
+      }
+    }
+  }
+
   // Set FP to point to the stack slot that contains the previous FP.
   // For iOS, FP is R7, which has now been stored in spill area 1.
   // Otherwise, if this is not iOS, all the callee-saved registers go
@@ -231,11 +310,37 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
       .addFrameIndex(FramePtrSpillFI).addImm(0)
       .setMIFlag(MachineInstr::FrameSetup);
     AddDefaultCC(AddDefaultPred(MIB));
+
+    if (NeedsFrameInfo) {
+      // CFA = FramePtr + offset from cfa where fp was stored
+      CfaReg = FramePtr;
+      CfaOffset = MFI->getObjectOffset (FramePtrSpillFI);
+      emitDefCfa(MBB, MBBI, dl, TII, MMI, CfaReg, CfaOffset);
+    }
   }
 
   // Move past area 2.
   if (GPRCS2Size > 0) MBBI++;
 
+  if (NeedsFrameInfo) {
+    MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol();
+    BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+
+    // Emit moves for the registers in spill area 2
+    for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
+      unsigned Reg = CSI[i].getReg();
+      int FI = CSI[i].getFrameIdx();
+      int64_t Offset = MFI->getObjectOffset(FI);
+
+      // The offset is relative to the incoming stack pointer which is
+      // the cfa
+      if (AFI->isGPRCalleeSavedArea2Frame(FI)) {
+        // Reg is saved at cfa + offset
+        emitCfaOffset(MBB, MBBI, dl, TII, MMI, FrameLabel, Reg, Offset);
+      }
+    }
+  }
+
   // Determine starting offsets of spill areas.
   unsigned DPRCSOffset  = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
@@ -281,6 +386,11 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
       // an inconsistent state (pointing to the middle of callee-saved area).
       // The interrupt handler can end up clobbering the registers.
       AFI->setShouldRestoreSPFromFP(true);
+
+    if (NeedsFrameInfo && CfaReg == ARM::SP) {
+      CfaOffset += NumBytes;
+      emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, NULL, CfaOffset);
+    }
   }
 
   if (STI.isTargetELF() && hasFP(MF))
@@ -426,8 +536,9 @@ void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
                                  ARM::SP)
             .addReg(FramePtr));
       }
-    } else if (NumBytes)
+    } else if (NumBytes) {
       emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
+    }
 
     // Increment past our save areas.
     if (AFI->getDPRCalleeSavedAreaSize()) {

Modified: lib/Target/ARM/Thumb1FrameLowering.cpp
===================================================================
@@ -17,6 +17,10 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/Target/TargetOptions.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/IR/Function.h"
 
 using namespace llvm;
 
@@ -43,7 +47,6 @@ bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
                             MRI, MIFlags);
 }
 
-
 void Thumb1FrameLowering::
 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
                               MachineBasicBlock::iterator I) const {
@@ -78,6 +81,49 @@ bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
   MBB.erase(I);
 }
 
+//
+// Functions to emit CFI Instructions.
+//
+
+static void
+emitDefCfaOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+                 DebugLoc dl, const ARMBaseInstrInfo &TII,
+                 MachineModuleInfo &MMI,
+                 MCSymbol *FrameLabel,
+                 int CfaOffset) {
+  if (!FrameLabel) {
+    FrameLabel = MMI.getContext().CreateTempSymbol();
+    BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+  }
+  MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(FrameLabel, CfaOffset));
+}
+
+static void
+emitDefCfa(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+           DebugLoc dl, const ARMBaseInstrInfo &TII,
+		   MachineModuleInfo &MMI,
+           int CfaReg, int CfaOffset) {
+  const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
+
+  MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol();
+  BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+
+  unsigned DwarfReg = MRI->getDwarfRegNum(CfaReg, true);
+  MMI.addFrameInst(MCCFIInstruction::createDefCfa(FrameLabel, DwarfReg, CfaOffset));
+}
+
+static void
+emitCfaOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
+              DebugLoc dl, const ARMBaseInstrInfo &TII,
+              MachineModuleInfo &MMI,
+              MCSymbol *FrameLabel,
+              int Reg, int Offset) {
+  const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
+
+  unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
+  MMI.addFrameInst(MCCFIInstruction::createOffset(FrameLabel, DwarfReg, Offset));
+}
+
 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
   MachineBasicBlock &MBB = MF.front();
   MachineBasicBlock::iterator MBBI = MBB.begin();
@@ -95,6 +141,12 @@ void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
   unsigned FramePtr = RegInfo->getFrameRegister(MF);
   unsigned BasePtr = RegInfo->getBaseRegister();
+  MachineModuleInfo &MMI = MF.getMMI();
+  bool NeedsFrameMoves = MF.getFunction()->needsUnwindTableEntry();
+  // The cfa register
+  int CfaReg = ARM::SP;
+  // The offset between the value of CfaReg and the CFA
+  int CfaOffset = 0;
 
   // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
   NumBytes = (NumBytes + 3) & ~3;
@@ -108,11 +160,15 @@ void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
   if (ArgRegsSaveSize)
     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
                  MachineInstr::FrameSetup);
+  CfaOffset += ArgRegsSaveSize;
 
   if (!AFI->hasStackFrame()) {
     if (NumBytes != 0)
       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
                    MachineInstr::FrameSetup);
+    CfaOffset += NumBytes;
+    if (NeedsFrameMoves && CfaOffset)
+      emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, NULL, CfaOffset);
     return;
   }
 
@@ -156,6 +212,28 @@ void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
       dl = MBBI->getDebugLoc();
   }
 
+  CfaOffset += GPRCS1Size;
+  if (NeedsFrameMoves) {
+    MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol();
+    BuildMI(MBB, MBBI, dl, TII.get(ARM::PROLOG_LABEL)).addSym(FrameLabel);
+    // CFA = sp + offset
+    emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, FrameLabel, CfaOffset);
+
+    // Emit moves for the registers in spill area 1
+    for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
+      unsigned Reg = CSI[i].getReg();
+      int FI = CSI[i].getFrameIdx();
+      int64_t Offset = MFI->getObjectOffset(FI);
+
+      // The offset is relative to the incoming stack pointer which is
+      // the cfa
+      if (AFI->isGPRCalleeSavedArea1Frame(FI)) {
+        // Reg is saved at cfa + offset
+        emitCfaOffset(MBB, MBBI, dl, TII, MMI, FrameLabel, Reg, Offset);
+      }
+    }
+  }
+
   // Determine starting offsets of spill areas.
   unsigned DPRCSOffset  = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
@@ -178,13 +256,26 @@ void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
       // If offset is > 508 then sp cannot be adjusted in a single instruction,
       // try restoring from fp instead.
       AFI->setShouldRestoreSPFromFP(true);
+
+    if (NeedsFrameMoves) {
+      // CFA = FramePtr + offset from cfa where fp was stored
+      CfaReg = FramePtr;
+      CfaOffset = MFI->getObjectOffset (FramePtrSpillFI);
+      emitDefCfa(MBB, MBBI, dl, TII, MMI, CfaReg, CfaOffset);
+    }
   }
 
-  if (NumBytes)
+  if (NumBytes) {
     // Insert it after all the callee-save spills.
     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
                  MachineInstr::FrameSetup);
 
+    if (NeedsFrameMoves && CfaReg == ARM::SP) {
+      CfaOffset += NumBytes;
+      emitDefCfaOffset(MBB, MBBI, dl, TII, MMI, NULL, CfaOffset);
+    }
+  }
+
   if (STI.isTargetELF() && HasFP)
     MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
                              AFI->getFramePtrSpillOffset());

   Commit: ff9b28af498cf8a57c6a37b2be8fdffe0c0c8e1e
   Author: Zoltan Varga <[email protected]> (vargaz)
     Date: 2013-11-12 23:39:09 GMT
      URL: https://github.com/mono/llvm/commit/ff9b28af498cf8a57c6a37b2be8fdffe0c0c8e1e

Use DWARF EH on ARM.

Changed paths:
  M lib/Target/ARM/ARMISelLowering.cpp
  M lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
  M lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp

Modified: lib/Target/ARM/ARMISelLowering.cpp
===================================================================
@@ -743,7 +743,8 @@ static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
 
-  if (!Subtarget->isTargetDarwin()) {
+  // Mono uses dwarf EH on darwin as well
+  if (true) { //if (!Subtarget->isTargetDarwin()) {
     // Non-Darwin platforms may return values in these registers via the
     // personality function.
     setExceptionPointerRegister(ARM::R0);

Modified: lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
===================================================================
@@ -33,8 +33,8 @@ void ARMMCAsmInfoDarwin::anchor() { }
 
   SupportsDebugInformation = true;
 
-  // Exceptions handling
-  ExceptionsType = ExceptionHandling::SjLj;
+  // Mono uses a variant of Dwarf CFI
+  ExceptionsType = ExceptionHandling::DwarfCFI;
 }
 
 void ARMELFMCAsmInfo::anchor() { }
@@ -53,6 +53,10 @@ void ARMELFMCAsmInfo::anchor() { }
   SupportsDebugInformation = true;
 
   // Exceptions handling
+  // Mono uses a variant of Dwarf CFI
+  ExceptionsType = ExceptionHandling::DwarfCFI;
+#if 0
   if (EnableARMEHABI)
     ExceptionsType = ExceptionHandling::ARM;
+#endif
 }

Modified: lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp
===================================================================
@@ -214,11 +214,20 @@ static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
 
 static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
   Triple TheTriple(TT);
+  MCAsmInfo *MAI;
 
   if (TheTriple.isOSDarwin())
-    return new ARMMCAsmInfoDarwin();
+    MAI = new ARMMCAsmInfoDarwin();
+  else
+    MAI = new ARMELFMCAsmInfo();
 
-  return new ARMELFMCAsmInfo();
+  // Initialize initial frame state.
+  // Initial state of the frame pointer is sp
+  MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(
+      0, MRI.getDwarfRegNum(ARM::SP, true), 0);
+  MAI->addInitialFrameState(Inst);
+
+  return MAI;
 }
 
 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,


_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches