Skip to content

Commit b1f25f1

Browse files
committed
Replace PROLOG_LABEL with a new CFI_INSTRUCTION.
The old system was fairly convoluted: * A temporary label was created. * A single PROLOG_LABEL was created with it. * A few MCCFIInstructions were created with the same label. The semantics were that the cfi instructions were mapped to the PROLOG_LABEL via the temporary label. The output position was that of the PROLOG_LABEL. The temporary label itself was used only for doing the mapping. The new CFI_INSTRUCTION has a 1:1 mapping to MCCFIInstructions and points to one by holding an index into the CFI instructions of this function. I did consider removing MMI.getFrameInstructions completelly and having CFI_INSTRUCTION own a MCCFIInstruction, but MCCFIInstructions have non trivial constructors and destructors and are somewhat big, so the this setup is probably better. The net result is that we don't create temporary labels that are never used. llvm-svn: 203204
1 parent c994c6a commit b1f25f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+338
-335
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace llvm {
208208
/// function.
209209
void EmitFunctionBody();
210210

211-
void emitPrologLabel(const MachineInstr &MI);
211+
void emitCFIInstruction(const MachineInstr &MI);
212212

213213
enum CFIMoveType {
214214
CFI_M_None,

llvm/include/llvm/CodeGen/MachineInstr.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -637,19 +637,19 @@ class MachineInstr : public ilist_node<MachineInstr> {
637637
/// bundle remain bundled.
638638
void eraseFromBundle();
639639

640+
bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
641+
bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
642+
640643
/// isLabel - Returns true if the MachineInstr represents a label.
641644
///
642-
bool isLabel() const {
643-
return getOpcode() == TargetOpcode::PROLOG_LABEL ||
644-
getOpcode() == TargetOpcode::EH_LABEL ||
645-
getOpcode() == TargetOpcode::GC_LABEL;
645+
bool isLabel() const { return isEHLabel() || isGCLabel(); }
646+
bool isCFIInstruction() const {
647+
return getOpcode() == TargetOpcode::CFI_INSTRUCTION;
646648
}
647649

648-
bool isPrologLabel() const {
649-
return getOpcode() == TargetOpcode::PROLOG_LABEL;
650-
}
651-
bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
652-
bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
650+
// True if the instruction represents a position in the function.
651+
bool isPosition() const { return isLabel() || isCFIInstruction(); }
652+
653653
bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; }
654654
/// A DBG_VALUE is indirect iff the first operand is a register and
655655
/// the second operand is an immediate.
@@ -715,7 +715,7 @@ class MachineInstr : public ilist_node<MachineInstr> {
715715
// Pseudo-instructions that don't produce any real output.
716716
case TargetOpcode::IMPLICIT_DEF:
717717
case TargetOpcode::KILL:
718-
case TargetOpcode::PROLOG_LABEL:
718+
case TargetOpcode::CFI_INSTRUCTION:
719719
case TargetOpcode::EH_LABEL:
720720
case TargetOpcode::GC_LABEL:
721721
case TargetOpcode::DBG_VALUE:

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ class MachineInstrBuilder {
172172
MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
173173
return *this;
174174
}
175-
175+
176+
const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
177+
MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
178+
return *this;
179+
}
180+
176181
const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
177182
MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
178183
return *this;

llvm/include/llvm/CodeGen/MachineModuleInfo.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ class MachineModuleInfo : public ImmutablePass {
238238
return FrameInstructions;
239239
}
240240

241-
void addFrameInst(const MCCFIInstruction &Inst) {
241+
unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
242+
addFrameInst(const MCCFIInstruction &Inst) {
242243
FrameInstructions.push_back(Inst);
244+
return FrameInstructions.size() - 1;
243245
}
244246

245247
/// getCompactUnwindEncoding - Returns the compact unwind encoding for a

llvm/include/llvm/CodeGen/MachineOperand.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class MachineOperand {
5858
MO_RegisterMask, ///< Mask of preserved registers.
5959
MO_RegisterLiveOut, ///< Mask of live-out registers.
6060
MO_Metadata, ///< Metadata reference (for debug info)
61-
MO_MCSymbol ///< MCSymbol reference (for debug/eh info)
61+
MO_MCSymbol, ///< MCSymbol reference (for debug/eh info)
62+
MO_CFIIndex ///< MCCFIInstruction index.
6263
};
6364

6465
private:
@@ -156,7 +157,8 @@ class MachineOperand {
156157
int64_t ImmVal; // For MO_Immediate.
157158
const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut.
158159
const MDNode *MD; // For MO_Metadata.
159-
MCSymbol *Sym; // For MO_MCSymbol
160+
MCSymbol *Sym; // For MO_MCSymbol.
161+
unsigned CFIIndex; // For MO_CFI.
160162

161163
struct { // For MO_Register.
162164
// Register number is in SmallContents.RegNo.
@@ -252,7 +254,7 @@ class MachineOperand {
252254
/// isMetadata - Tests if this is a MO_Metadata operand.
253255
bool isMetadata() const { return OpKind == MO_Metadata; }
254256
bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
255-
257+
bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
256258

257259
//===--------------------------------------------------------------------===//
258260
// Accessors for Register Operands
@@ -443,6 +445,11 @@ class MachineOperand {
443445
return Contents.Sym;
444446
}
445447

448+
unsigned getCFIIndex() const {
449+
assert(isCFIIndex() && "Wrong MachineOperand accessor");
450+
return Contents.CFIIndex;
451+
}
452+
446453
/// getOffset - Return the offset from the symbol in this operand. This always
447454
/// returns 0 for ExternalSymbol operands.
448455
int64_t getOffset() const {
@@ -686,6 +693,12 @@ class MachineOperand {
686693
return Op;
687694
}
688695

696+
static MachineOperand CreateCFIIndex(unsigned CFIIndex) {
697+
MachineOperand Op(MachineOperand::MO_CFIIndex);
698+
Op.Contents.CFIIndex = CFIIndex;
699+
return Op;
700+
}
701+
689702
friend class MachineInstr;
690703
friend class MachineRegisterInfo;
691704
private:

llvm/include/llvm/Target/Target.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def INLINEASM : Instruction {
714714
let AsmString = "";
715715
let neverHasSideEffects = 1; // Note side effect is encoded in an operand.
716716
}
717-
def PROLOG_LABEL : Instruction {
717+
def CFI_INSTRUCTION : Instruction {
718718
let OutOperandList = (outs);
719719
let InOperandList = (ins i32imm:$id);
720720
let AsmString = "";

llvm/include/llvm/Target/TargetOpcodes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace TargetOpcode {
2525
enum {
2626
PHI = 0,
2727
INLINEASM = 1,
28-
PROLOG_LABEL = 2,
28+
CFI_INSTRUCTION = 2,
2929
EH_LABEL = 3,
3030
GC_LABEL = 4,
3131

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+8-17
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,7 @@ bool AsmPrinter::needsSEHMoves() {
699699
MF->getFunction()->needsUnwindTableEntry();
700700
}
701701

702-
void AsmPrinter::emitPrologLabel(const MachineInstr &MI) {
703-
const MCSymbol *Label = MI.getOperand(0).getMCSymbol();
704-
702+
void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
705703
ExceptionHandling::ExceptionsType ExceptionHandlingType =
706704
MAI->getExceptionHandlingType();
707705
if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
@@ -716,16 +714,9 @@ void AsmPrinter::emitPrologLabel(const MachineInstr &MI) {
716714

717715
const MachineModuleInfo &MMI = MF->getMMI();
718716
const std::vector<MCCFIInstruction> &Instrs = MMI.getFrameInstructions();
719-
bool FoundOne = false;
720-
(void)FoundOne;
721-
for (std::vector<MCCFIInstruction>::const_iterator I = Instrs.begin(),
722-
E = Instrs.end(); I != E; ++I) {
723-
if (I->getLabel() == Label) {
724-
emitCFIInstruction(*I);
725-
FoundOne = true;
726-
}
727-
}
728-
assert(FoundOne);
717+
unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
718+
const MCCFIInstruction &CFI = Instrs[CFIIndex];
719+
emitCFIInstruction(CFI);
729720
}
730721

731722
/// EmitFunctionBody - This method emits the body and trailer for a
@@ -748,7 +739,7 @@ void AsmPrinter::EmitFunctionBody() {
748739
LastMI = II;
749740

750741
// Print the assembly for the instruction.
751-
if (!II->isLabel() && !II->isImplicitDef() && !II->isKill() &&
742+
if (!II->isPosition() && !II->isImplicitDef() && !II->isKill() &&
752743
!II->isDebugValue()) {
753744
HasAnyRealCode = true;
754745
++EmittedInsts;
@@ -767,8 +758,8 @@ void AsmPrinter::EmitFunctionBody() {
767758
emitComments(*II, OutStreamer.GetCommentOS());
768759

769760
switch (II->getOpcode()) {
770-
case TargetOpcode::PROLOG_LABEL:
771-
emitPrologLabel(*II);
761+
case TargetOpcode::CFI_INSTRUCTION:
762+
emitCFIInstruction(*II);
772763
break;
773764

774765
case TargetOpcode::EH_LABEL:
@@ -811,7 +802,7 @@ void AsmPrinter::EmitFunctionBody() {
811802
// label equaling the end of function label and an invalid "row" in the
812803
// FDE. We need to emit a noop in this situation so that the FDE's rows are
813804
// valid.
814-
bool RequiresNoop = LastMI && LastMI->isPrologLabel();
805+
bool RequiresNoop = LastMI && LastMI->isCFIInstruction();
815806

816807
// If the function is empty and the object file uses .subsections_via_symbols,
817808
// then we need to emit *something* to the function body to prevent the

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ void DwarfDebug::beginFunction(const MachineFunction *MF) {
16101610
History.push_back(MI);
16111611
} else {
16121612
// Not a DBG_VALUE instruction.
1613-
if (!MI->isLabel())
1613+
if (!MI->isPosition())
16141614
AtBlockEntry = false;
16151615

16161616
// First known non-DBG_VALUE and non-frame setup location marks

llvm/lib/CodeGen/MachineBasicBlock.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
160160
MachineBasicBlock::iterator
161161
MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
162162
iterator E = end();
163-
while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue()))
163+
while (I != E && (I->isPHI() || I->isPosition() || I->isDebugValue()))
164164
++I;
165165
// FIXME: This needs to change if we wish to bundle labels / dbg_values
166166
// inside the bundle.

llvm/lib/CodeGen/MachineCSE.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
325325
}
326326

327327
bool MachineCSE::isCSECandidate(MachineInstr *MI) {
328-
if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() ||
329-
MI->isKill() || MI->isInlineAsm() || MI->isDebugValue())
328+
if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() ||
329+
MI->isInlineAsm() || MI->isDebugValue())
330330
return false;
331331

332332
// Ignore copies.

llvm/lib/CodeGen/MachineInstr.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
203203
return getRegMask() == Other.getRegMask();
204204
case MachineOperand::MO_MCSymbol:
205205
return getMCSymbol() == Other.getMCSymbol();
206+
case MachineOperand::MO_CFIIndex:
207+
return getCFIIndex() == Other.getCFIIndex();
206208
case MachineOperand::MO_Metadata:
207209
return getMetadata() == Other.getMetadata();
208210
}
@@ -247,6 +249,8 @@ hash_code llvm::hash_value(const MachineOperand &MO) {
247249
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
248250
case MachineOperand::MO_MCSymbol:
249251
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
252+
case MachineOperand::MO_CFIIndex:
253+
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
250254
}
251255
llvm_unreachable("Invalid machine operand type");
252256
}
@@ -380,6 +384,9 @@ void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
380384
case MachineOperand::MO_MCSymbol:
381385
OS << "<MCSym=" << *getMCSymbol() << '>';
382386
break;
387+
case MachineOperand::MO_CFIIndex:
388+
OS << "<call frame instruction>";
389+
break;
383390
}
384391

385392
if (unsigned TF = getTargetFlags())
@@ -1295,8 +1302,8 @@ bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
12951302
return false;
12961303
}
12971304

1298-
if (isLabel() || isDebugValue() ||
1299-
isTerminator() || hasUnmodeledSideEffects())
1305+
if (isPosition() || isDebugValue() || isTerminator() ||
1306+
hasUnmodeledSideEffects())
13001307
return false;
13011308

13021309
// See if this instruction does a load. If so, we have to guarantee that the

llvm/lib/CodeGen/PeepholeOptimizer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
596596

597597
// If there exists an instruction which belongs to the following
598598
// categories, we will discard the load candidate.
599-
if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() ||
599+
if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() ||
600600
MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() ||
601601
MI->hasUnmodeledSideEffects()) {
602602
FoldAsLoadDefReg = 0;

llvm/lib/CodeGen/ScheduleDAGInstrs.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,9 @@ void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
804804
"RPTracker can't find MI");
805805
}
806806

807-
assert((CanHandleTerminators || (!MI->isTerminator() && !MI->isLabel())) &&
808-
"Cannot schedule terminators or labels!");
807+
assert(
808+
(CanHandleTerminators || (!MI->isTerminator() && !MI->isPosition())) &&
809+
"Cannot schedule terminators or labels!");
809810

810811
// Add register-based dependencies (data, anti, and output).
811812
bool HasVRegDef = false;

llvm/lib/CodeGen/TargetInstrInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
645645
const MachineBasicBlock *MBB,
646646
const MachineFunction &MF) const {
647647
// Terminators and labels can't be scheduled around.
648-
if (MI->isTerminator() || MI->isLabel())
648+
if (MI->isTerminator() || MI->isPosition())
649649
return true;
650650

651651
// Don't attempt to schedule around any instruction that defines

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

+16-25
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,12 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF) const {
9292
if (NeedsFrameMoves && NumInitialBytes) {
9393
// We emit this update even if the CFA is set from a frame pointer later so
9494
// that the CFA is valid in the interim.
95-
MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
96-
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::PROLOG_LABEL))
97-
.addSym(SPLabel);
98-
9995
MachineLocation Dst(MachineLocation::VirtualFP);
10096
unsigned Reg = MRI->getDwarfRegNum(AArch64::XSP, true);
101-
MMI.addFrameInst(
102-
MCCFIInstruction::createDefCfa(SPLabel, Reg, -NumInitialBytes));
97+
unsigned CFIIndex = MMI.addFrameInst(
98+
MCCFIInstruction::createDefCfa(nullptr, Reg, -NumInitialBytes));
99+
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
100+
.addCFIIndex(CFIIndex);
103101
}
104102

105103
// Otherwise we need to set the frame pointer and/or add a second stack
@@ -129,12 +127,12 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF) const {
129127
- MFI->getStackSize());
130128

131129
if (NeedsFrameMoves) {
132-
MCSymbol *FPLabel = MMI.getContext().CreateTempSymbol();
133-
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::PROLOG_LABEL))
134-
.addSym(FPLabel);
135130
unsigned Reg = MRI->getDwarfRegNum(AArch64::X29, true);
136131
unsigned Offset = MFI->getObjectOffset(X29FrameIdx);
137-
MMI.addFrameInst(MCCFIInstruction::createDefCfa(FPLabel, Reg, Offset));
132+
unsigned CFIIndex = MMI.addFrameInst(
133+
MCCFIInstruction::createDefCfa(nullptr, Reg, Offset));
134+
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
135+
.addCFIIndex(CFIIndex);
138136
}
139137

140138
FPNeedsSetting = false;
@@ -155,36 +153,29 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF) const {
155153
if (!NeedsFrameMoves)
156154
return;
157155

158-
// Reuse the label if appropriate, so create it in this outer scope.
159-
MCSymbol *CSLabel = 0;
160-
161156
// The rest of the stack adjustment
162157
if (!hasFP(MF) && NumResidualBytes) {
163-
CSLabel = MMI.getContext().CreateTempSymbol();
164-
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::PROLOG_LABEL))
165-
.addSym(CSLabel);
166-
167158
MachineLocation Dst(MachineLocation::VirtualFP);
168159
unsigned Reg = MRI->getDwarfRegNum(AArch64::XSP, true);
169160
unsigned Offset = NumResidualBytes + NumInitialBytes;
170-
MMI.addFrameInst(MCCFIInstruction::createDefCfa(CSLabel, Reg, -Offset));
161+
unsigned CFIIndex =
162+
MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
163+
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
164+
.addCFIIndex(CFIIndex);
171165
}
172166

173167
// And any callee-saved registers (it's fine to leave them to the end here,
174168
// because the old values are still valid at this point.
175169
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
176170
if (CSI.size()) {
177-
if (!CSLabel) {
178-
CSLabel = MMI.getContext().CreateTempSymbol();
179-
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::PROLOG_LABEL))
180-
.addSym(CSLabel);
181-
}
182-
183171
for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
184172
E = CSI.end(); I != E; ++I) {
185173
unsigned Offset = MFI->getObjectOffset(I->getFrameIdx());
186174
unsigned Reg = MRI->getDwarfRegNum(I->getReg(), true);
187-
MMI.addFrameInst(MCCFIInstruction::createOffset(CSLabel, Reg, Offset));
175+
unsigned CFIIndex = MMI.addFrameInst(
176+
MCCFIInstruction::createOffset(nullptr, Reg, Offset));
177+
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
178+
.addCFIIndex(CFIIndex);
188179
}
189180
}
190181
}

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
735735
return getInstBundleLength(MI);
736736
case TargetOpcode::IMPLICIT_DEF:
737737
case TargetOpcode::KILL:
738-
case TargetOpcode::PROLOG_LABEL:
738+
case TargetOpcode::CFI_INSTRUCTION:
739739
case TargetOpcode::EH_LABEL:
740740
case TargetOpcode::GC_LABEL:
741741
case TargetOpcode::DBG_VALUE:

0 commit comments

Comments
 (0)