mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-04-07 18:21:58 +00:00

CFI instructions that set appropriate cfa offset and cfa register are now inserted in emitEpilogue() in X86FrameLowering. Majority of the changes in this patch: 1. Ensure that CFI instructions do not affect code generation. 2. Enable maintaining correct information about cfa offset and cfa register in a function when basic blocks are reordered, merged, split, duplicated. These changes are target independent and described below. Changed CFI instructions so that they: 1. are duplicable 2. are not counted as instructions when tail duplicating or tail merging 3. can be compared as equal Add information to each MachineBasicBlock about cfa offset and cfa register that are valid at its entry and exit (incoming and outgoing CFI info). Add support for updating this information when basic blocks are merged, split, duplicated, created. Add a verification pass (CFIInfoVerifier) that checks that outgoing cfa offset and register of predecessor blocks match incoming values of their successors. Incoming and outgoing CFI information is used by a late pass (CFIInstrInserter) that corrects CFA calculation rule for a basic block if needed. That means that additional CFI instructions get inserted at basic block beginning to correct the rule for calculating CFA. Having CFI instructions in function epilogue can cause incorrect CFA calculation rule for some basic blocks. This can happen if, due to basic block reordering, or the existence of multiple epilogue blocks, some of the blocks have wrong cfa offset and register values set by the epilogue block above them. Patch by Violeta Vukobrat. Differential Revision: https://reviews.llvm.org/D18046 llvm-svn: 306529
125 lines
4.8 KiB
C++
125 lines
4.8 KiB
C++
//===------ CFIInstrInserter.cpp - Insert additional CFI instructions -----===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Insert CFI instructions at the beginnings of basic blocks if needed. CFI
|
|
// instructions are inserted if basic blocks have incorrect offset or register
|
|
// set by prevoius blocks.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
#include "llvm/CodeGen/Passes.h"
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
class CFIInstrInserter : public MachineFunctionPass {
|
|
public:
|
|
CFIInstrInserter() : MachineFunctionPass(ID) {
|
|
initializeCFIInstrInserterPass(*PassRegistry::getPassRegistry());
|
|
}
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
static char ID;
|
|
|
|
private:
|
|
StringRef getPassName() const override { return "CFI Instruction Inserter"; }
|
|
|
|
// Check if incoming CFI information of a basic block matches outgoing CFI
|
|
// information of the previous block. If it doesn't, insert CFI instruction at
|
|
// the beginning of the block that corrects the CFA calculation rule for that
|
|
// block.
|
|
void CorrectCFA(MachineFunction &MF);
|
|
|
|
// Return the cfa offset value that should be set at the beginning of MBB if
|
|
// needed. The negated value is needed when creating CFI instructions that set
|
|
// absolute offset.
|
|
int getCorrectCFAOffset(MachineBasicBlock &MBB) {
|
|
return -MBB.getIncomingCFAOffset();
|
|
}
|
|
|
|
// Were any CFI instructions inserted
|
|
bool InsertedCFIInstr = false;
|
|
};
|
|
}
|
|
|
|
char CFIInstrInserter::ID = 0;
|
|
INITIALIZE_PASS(CFIInstrInserter, "cfiinstrinserter",
|
|
"Check CFI info and insert CFI instructions if needed", false,
|
|
false)
|
|
|
|
FunctionPass *llvm::createCFIInstrInserter() { return new CFIInstrInserter(); }
|
|
|
|
bool CFIInstrInserter::runOnMachineFunction(MachineFunction &MF) {
|
|
bool NeedsDwarfCFI = (MF.getMMI().hasDebugInfo() ||
|
|
MF.getFunction()->needsUnwindTableEntry()) &&
|
|
(!MF.getTarget().getTargetTriple().isOSDarwin() &&
|
|
!MF.getTarget().getTargetTriple().isOSWindows());
|
|
|
|
if (!NeedsDwarfCFI) return false;
|
|
|
|
// Insert appropriate CFI instructions for each MBB if CFA calculation rule
|
|
// needs to be corrected for that MBB.
|
|
CorrectCFA(MF);
|
|
|
|
return InsertedCFIInstr;
|
|
}
|
|
|
|
void CFIInstrInserter::CorrectCFA(MachineFunction &MF) {
|
|
|
|
MachineBasicBlock &FirstMBB = MF.front();
|
|
MachineBasicBlock *PrevMBB = &FirstMBB;
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
|
InsertedCFIInstr = false;
|
|
|
|
for (auto &MBB : MF) {
|
|
// Skip the first MBB in a function
|
|
if (MBB.getNumber() == FirstMBB.getNumber()) continue;
|
|
|
|
auto MBBI = MBB.begin();
|
|
DebugLoc DL = MBB.findDebugLoc(MBBI);
|
|
|
|
if (PrevMBB->getOutgoingCFAOffset() != MBB.getIncomingCFAOffset()) {
|
|
// If both outgoing offset and register of a previous block don't match
|
|
// incoming offset and register of this block, add a def_cfa instruction
|
|
// with the correct offset and register for this block.
|
|
if (PrevMBB->getOutgoingCFARegister() != MBB.getIncomingCFARegister()) {
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
nullptr, MBB.getIncomingCFARegister(), getCorrectCFAOffset(MBB)));
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
.addCFIIndex(CFIIndex);
|
|
// If outgoing offset of a previous block doesn't match incoming offset
|
|
// of this block, add a def_cfa_offset instruction with the correct
|
|
// offset for this block.
|
|
} else {
|
|
unsigned CFIIndex =
|
|
MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(
|
|
nullptr, getCorrectCFAOffset(MBB)));
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
.addCFIIndex(CFIIndex);
|
|
}
|
|
InsertedCFIInstr = true;
|
|
// If outgoing register of a previous block doesn't match incoming
|
|
// register of this block, add a def_cfa_register instruction with the
|
|
// correct register for this block.
|
|
} else if (PrevMBB->getOutgoingCFARegister() !=
|
|
MBB.getIncomingCFARegister()) {
|
|
unsigned CFIIndex =
|
|
MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
|
|
nullptr, MBB.getIncomingCFARegister()));
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
.addCFIIndex(CFIIndex);
|
|
InsertedCFIInstr = true;
|
|
}
|
|
PrevMBB = &MBB;
|
|
}
|
|
}
|