diff options
Diffstat (limited to 'contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp')
-rw-r--r-- | contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp | 165 |
1 files changed, 143 insertions, 22 deletions
diff --git a/contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp b/contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp index 5438334277f1..d463bee67595 100644 --- a/contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp +++ b/contrib/llvm/lib/CodeGen/PrologEpilogInserter.cpp @@ -1,9 +1,8 @@ //===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -32,6 +31,7 @@ #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstr.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineLoopInfo.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineOperand.h" @@ -169,6 +169,46 @@ void PEI::getAnalysisUsage(AnalysisUsage &AU) const { /// StackObjSet - A set of stack object indexes using StackObjSet = SmallSetVector<int, 8>; +using SavedDbgValuesMap = + SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>; + +/// Stash DBG_VALUEs that describe parameters and which are placed at the start +/// of the block. Later on, after the prologue code has been emitted, the +/// stashed DBG_VALUEs will be reinserted at the start of the block. +static void stashEntryDbgValues(MachineBasicBlock &MBB, + SavedDbgValuesMap &EntryDbgValues) { + SmallVector<const MachineInstr *, 4> FrameIndexValues; + + for (auto &MI : MBB) { + if (!MI.isDebugInstr()) + break; + if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter()) + continue; + if (MI.getOperand(0).isFI()) { + // We can only emit valid locations for frame indices after the frame + // setup, so do not stash away them. + FrameIndexValues.push_back(&MI); + continue; + } + const DILocalVariable *Var = MI.getDebugVariable(); + const DIExpression *Expr = MI.getDebugExpression(); + auto Overlaps = [Var, Expr](const MachineInstr *DV) { + return Var == DV->getDebugVariable() && + Expr->fragmentsOverlap(DV->getDebugExpression()); + }; + // See if the debug value overlaps with any preceding debug value that will + // not be stashed. If that is the case, then we can't stash this value, as + // we would then reorder the values at reinsertion. + if (llvm::none_of(FrameIndexValues, Overlaps)) + EntryDbgValues[&MBB].push_back(&MI); + } + + // Remove stashed debug values from the block. + if (EntryDbgValues.count(&MBB)) + for (auto *MI : EntryDbgValues[&MBB]) + MI->removeFromParent(); +} + /// runOnMachineFunction - Insert prolog/epilog code and replace abstract /// frame indexes with appropriate references. bool PEI::runOnMachineFunction(MachineFunction &MF) { @@ -179,8 +219,6 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) { RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr; FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF); - FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) || - TRI->requiresFrameIndexReplacementScavenging(MF); ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE(); // Calculate the MaxCallFrameSize and AdjustsStack variables for the @@ -192,6 +230,11 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) { // place all spills in the entry block, all restores in return blocks. calculateSaveRestoreBlocks(MF); + // Stash away DBG_VALUEs that should not be moved by insertion of prolog code. + SavedDbgValuesMap EntryDbgValues; + for (MachineBasicBlock *SaveBlock : SaveBlocks) + stashEntryDbgValues(*SaveBlock, EntryDbgValues); + // Handle CSR spilling and restoring, for targets that need it. if (MF.getTarget().usesPhysRegsForPEI()) spillCalleeSavedRegs(MF); @@ -211,6 +254,10 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) { if (!F.hasFnAttribute(Attribute::Naked)) insertPrologEpilogCode(MF); + // Reinsert stashed debug values at the start of the entry blocks. + for (auto &I : EntryDbgValues) + I.first->insert(I.first->begin(), I.second.begin(), I.second.end()); + // Replace all MO_FrameIndex operands with physical register references // and actual offsets. // @@ -495,9 +542,16 @@ static void insertCSRSaves(MachineBasicBlock &SaveBlock, for (const CalleeSavedInfo &CS : CSI) { // Insert the spill to the stack frame. unsigned Reg = CS.getReg(); - const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); - TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC, - TRI); + + if (CS.isSpilledToReg()) { + BuildMI(SaveBlock, I, DebugLoc(), + TII.get(TargetOpcode::COPY), CS.getDstReg()) + .addReg(Reg, getKillRegState(true)); + } else { + const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); + TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC, + TRI); + } } } } @@ -517,12 +571,17 @@ static void insertCSRRestores(MachineBasicBlock &RestoreBlock, if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) { for (const CalleeSavedInfo &CI : reverse(CSI)) { unsigned Reg = CI.getReg(); - const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); - TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI); - assert(I != RestoreBlock.begin() && - "loadRegFromStackSlot didn't insert any code!"); - // Insert in reverse order. loadRegFromStackSlot can insert - // multiple instructions. + if (CI.isSpilledToReg()) { + BuildMI(RestoreBlock, I, DebugLoc(), TII.get(TargetOpcode::COPY), Reg) + .addReg(CI.getDstReg(), getKillRegState(true)); + } else { + const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); + TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI); + assert(I != RestoreBlock.begin() && + "loadRegFromStackSlot didn't insert any code!"); + // Insert in reverse order. loadRegFromStackSlot can insert + // multiple instructions. + } } } } @@ -615,10 +674,13 @@ computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown, SmallVector<int, 16> AllocatedFrameSlots; // Add fixed objects. for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) - AllocatedFrameSlots.push_back(i); + // StackSlot scavenging is only implemented for the default stack. + if (MFI.getStackID(i) == TargetStackID::Default) + AllocatedFrameSlots.push_back(i); // Add callee-save objects. for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i) - AllocatedFrameSlots.push_back(i); + if (MFI.getStackID(i) == TargetStackID::Default) + AllocatedFrameSlots.push_back(i); for (int i : AllocatedFrameSlots) { // These are converted from int64_t, but they should always fit in int @@ -740,11 +802,23 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { // Skew to be applied to alignment. unsigned Skew = TFI.getStackAlignmentSkew(MF); +#ifdef EXPENSIVE_CHECKS + for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) + if (!MFI.isDeadObjectIndex(i) && + MFI.getStackID(i) == TargetStackID::Default) + assert(MFI.getObjectAlignment(i) <= MFI.getMaxAlignment() && + "MaxAlignment is invalid"); +#endif + // If there are fixed sized objects that are preallocated in the local area, // non-fixed objects can't be allocated right at the start of local area. // Adjust 'Offset' to point to the end of last fixed sized preallocated // object. for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) { + if (MFI.getStackID(i) != + TargetStackID::Default) // Only allocate objects on the default stack. + continue; + int64_t FixedOff; if (StackGrowsDown) { // The maximum distance from the stack pointer is at lower address of @@ -763,6 +837,10 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { // callee saved registers. if (StackGrowsDown) { for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) { + if (MFI.getStackID(i) != + TargetStackID::Default) // Only allocate objects on the default stack. + continue; + // If the stack grows down, we need to add the size to find the lowest // address of the object. Offset += MFI.getObjectSize(i); @@ -777,6 +855,10 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { } else if (MaxCSFrameIndex >= MinCSFrameIndex) { // Be careful about underflow in comparisons agains MinCSFrameIndex. for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) { + if (MFI.getStackID(i) != + TargetStackID::Default) // Only allocate objects on the default stack. + continue; + if (MFI.isDeadObjectIndex(i)) continue; @@ -874,6 +956,9 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { continue; if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i) continue; + if (MFI.getStackID(i) != + TargetStackID::Default) // Only allocate objects on the default stack. + continue; switch (MFI.getObjectSSPLayout(i)) { case MachineFrameInfo::SSPLK_None: @@ -925,6 +1010,9 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) { continue; if (ProtectedObjs.count(i)) continue; + if (MFI.getStackID(i) != + TargetStackID::Default) // Only allocate objects on the default stack. + continue; // Add the objects that we need to allocate to our working set. ObjectsToAllocate.push_back(i); @@ -1041,8 +1129,16 @@ void PEI::insertPrologEpilogCode(MachineFunction &MF) { /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical /// register references and actual offsets. void PEI::replaceFrameIndices(MachineFunction &MF) { - const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); - if (!TFI.needsFrameIndexResolution(MF)) return; + const auto &ST = MF.getSubtarget(); + const TargetFrameLowering &TFI = *ST.getFrameLowering(); + if (!TFI.needsFrameIndexResolution(MF)) + return; + + const TargetRegisterInfo *TRI = ST.getRegisterInfo(); + + // Allow the target to determine this after knowing the frame size. + FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) || + TRI->requiresFrameIndexReplacementScavenging(MF); // Store SPAdj at exit of a basic block. SmallVector<int, 8> SPState; @@ -1110,12 +1206,37 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF, assert(i == 0 && "Frame indices can only appear as the first " "operand of a DBG_VALUE machine instruction"); unsigned Reg; + unsigned FrameIdx = MI.getOperand(0).getIndex(); + unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx); + int64_t Offset = - TFI->getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg); + TFI->getFrameIndexReference(MF, FrameIdx, Reg); MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/); MI.getOperand(0).setIsDebug(); - auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(), - DIExpression::NoDeref, Offset); + + const DIExpression *DIExpr = MI.getDebugExpression(); + + // If we have a direct DBG_VALUE, and its location expression isn't + // currently complex, then adding an offset will morph it into a + // complex location that is interpreted as being a memory address. + // This changes a pointer-valued variable to dereference that pointer, + // which is incorrect. Fix by adding DW_OP_stack_value. + unsigned PrependFlags = DIExpression::ApplyOffset; + if (!MI.isIndirectDebugValue() && !DIExpr->isComplex()) + PrependFlags |= DIExpression::StackValue; + + // If we have DBG_VALUE that is indirect and has a Implicit location + // expression need to insert a deref before prepending a Memory + // location expression. Also after doing this we change the DBG_VALUE + // to be direct. + if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) { + SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size}; + bool WithStackValue = true; + DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue); + // Make the DBG_VALUE direct. + MI.getOperand(1).ChangeToRegister(0, false); + } + DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset); MI.getOperand(3).setMetadata(DIExpr); continue; } |