Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Standalone View
lib/CodeGen/CFIInstrInserter.cpp
- This file was added.
//===------ 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/TargetFrameLowering.h" | |||||
MatzeB: Use doxygen comments:
```
/// \file This pass verifies ...
``` | |||||
#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: | |||||
I think most of the members can be private here. thegameg: I think most of the members can be private here. | |||||
StringRef getPassName() const override { return "CFI Instruction Inserter"; } | |||||
This feels slightly out of place as a member variable. Why not make it a return value of verify()? MatzeB: This feels slightly out of place as a member variable. Why not make it a return value of… | |||||
// 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; | |||||
}; | |||||
} | |||||
Not Done ReplyInline ActionsShould this be guarded by the EXPENSIVE_CHECKS macro? thegameg: Should this be guarded by the `EXPENSIVE_CHECKS` macro? | |||||
I wouldn't go as far as EXPENSIVE_CHECKS. But I think #ifndef NDEBUG would be good to skip the verification in release builds. MatzeB: I wouldn't go as far as EXPENSIVE_CHECKS. But I think `#ifndef NDEBUG` would be good to skip… | |||||
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() || | |||||
In LLVM we usually use struct MBBCFAInfo { [...] }; thegameg: In LLVM we usually use
struct MBBCFAInfo {
[...]
}; | |||||
MF.getFunction()->needsUnwindTableEntry()) && | |||||
(!MF.getTarget().getTargetTriple().isOSDarwin() && | |||||
please use /// for all comments describing declarations. aprantl: please use `///` for all comments describing declarations. | |||||
!MF.getTarget().getTargetTriple().isOSWindows()); | |||||
if (!NeedsDwarfCFI || | |||||
!MF.getSubtarget().getFrameLowering()->maintainsCFIInfo()) | |||||
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) { | |||||
MachineBasicBlocks have a dense numbering, so you can use a more efficient std::vector of length MachineFunction::getNumBlockIDs() instead. (See MachineTraceMetrics::BlockInfo for an example) MatzeB: MachineBasicBlocks have a dense numbering, so you can use a more efficient std::vector of… | |||||
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; | |||||
Why not a reference instead of a pointer? It can never be null, right? thegameg: Why not a reference instead of a pointer? It can never be null, right? | |||||
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. | |||||
We tend to use lowercase pass names here. So maybe "cfi-instr-inserter"? MatzeB: We tend to use lowercase pass names here. So maybe `"cfi-instr-inserter"`? | |||||
} 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; | |||||
} | |||||
} | |||||
Not Done ReplyInline ActionsNo need for struct here. thegameg: No need for `struct` here. | |||||
You could limit the scope of SuccInfo to the loop body. thegameg: You could limit the scope of `SuccInfo` to the loop body. | |||||
I think you can assume the reference is always non-null here, right? thegameg: I think you can assume the reference is always non-null here, right? | |||||
References must be non-null otherwise it's undefined behavior. This means the compiler will most likely optimize this assert away and it won't do anything even if the reference happened to illegally be nullptr. MatzeB: References must be non-null otherwise it's undefined behavior. This means the compiler will… | |||||
You can already initialize the variables here. thegameg: You can already initialize the variables here. | |||||
Please avoid auto in cases where the actual type is only slightly longer. I think that is friendlier to readers of the code. (same for a number of other loops below). MatzeB: Please avoid `auto` in cases where the actual type is only slightly longer. I think that is… | |||||
You could use for (MachineInstr &MI : make_range(MBBInfo->MBB->instr_begin(), MBBInfo->MBB->instr_end()) { ... } here. MatzeB: You could use `for (MachineInstr &MI : make_range(MBBInfo->MBB->instr_begin(), MBBInfo->MBB… | |||||
You could move this declaration down to the use (and combine it with the first assignment). MatzeB: You could move this declaration down to the use (and combine it with the first assignment). | |||||
The declarations could be move downwards to their first use. MatzeB: The declarations could be move downwards to their first use. | |||||
This could be a const MBBCFAInfo * to avoid copies. thegameg: This could be a `const MBBCFAInfo *` to avoid copies. | |||||
And this const MBBCFAInfo &. thegameg: And this `const MBBCFAInfo &`. | |||||
Same. thegameg: Same. | |||||
Same. thegameg: Same. | |||||
Same. thegameg: Same. |
Use doxygen comments: