@@ -1007,6 +1007,205 @@ void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old,
1007
1007
}
1008
1008
}
1009
1009
1010
+ auto MachineFunction::salvageCopySSA (MachineInstr &MI)
1011
+ -> DebugInstrOperandPair {
1012
+ MachineRegisterInfo &MRI = getRegInfo ();
1013
+ const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo ();
1014
+ const TargetInstrInfo &TII = *getSubtarget ().getInstrInfo ();
1015
+
1016
+ // Chase the value read by a copy-like instruction back to the instruction
1017
+ // that ultimately _defines_ that value. This may pass:
1018
+ // * Through multiple intermediate copies, including subregister moves /
1019
+ // copies,
1020
+ // * Copies from physical registers that must then be traced back to the
1021
+ // defining instruction,
1022
+ // * Or, physical registers may be live-in to (only) the entry block, which
1023
+ // requires a DBG_PHI to be created.
1024
+ // We can pursue this problem in that order: trace back through copies,
1025
+ // optionally through a physical register, to a defining instruction. We
1026
+ // should never move from physreg to vreg. As we're still in SSA form, no need
1027
+ // to worry about partial definitions of registers.
1028
+
1029
+ // Helper lambda to interpret a copy-like instruction. Takes instruction,
1030
+ // returns the register read and any subregister identifying which part is
1031
+ // read.
1032
+ auto GetRegAndSubreg =
1033
+ [&](const MachineInstr &Cpy) -> std::pair<Register, unsigned > {
1034
+ Register NewReg, OldReg;
1035
+ unsigned SubReg;
1036
+ if (Cpy.isCopy ()) {
1037
+ OldReg = Cpy.getOperand (0 ).getReg ();
1038
+ NewReg = Cpy.getOperand (1 ).getReg ();
1039
+ SubReg = Cpy.getOperand (1 ).getSubReg ();
1040
+ } else if (Cpy.isSubregToReg ()) {
1041
+ OldReg = Cpy.getOperand (0 ).getReg ();
1042
+ NewReg = Cpy.getOperand (2 ).getReg ();
1043
+ SubReg = Cpy.getOperand (3 ).getImm ();
1044
+ } else {
1045
+ auto CopyDetails = *TII.isCopyInstr (Cpy);
1046
+ const MachineOperand &Src = *CopyDetails.Source ;
1047
+ const MachineOperand &Dest = *CopyDetails.Destination ;
1048
+ OldReg = Dest.getReg ();
1049
+ NewReg = Src.getReg ();
1050
+ SubReg = Src.getSubReg ();
1051
+ }
1052
+
1053
+ return {NewReg, SubReg};
1054
+ };
1055
+
1056
+ // First seek either the defining instruction, or a copy from a physreg.
1057
+ // During search, the current state is the current copy instruction, and which
1058
+ // register we've read. Accumulate qualifying subregisters into SubregsSeen;
1059
+ // deal with those later.
1060
+ auto State = GetRegAndSubreg (MI);
1061
+ auto CurInst = MI.getIterator ();
1062
+ SmallVector<unsigned , 4 > SubregsSeen;
1063
+ while (true ) {
1064
+ // If we've found a copy from a physreg, first portion of search is over.
1065
+ if (!State.first .isVirtual ())
1066
+ break ;
1067
+
1068
+ // Record any subregister qualifier.
1069
+ if (State.second )
1070
+ SubregsSeen.push_back (State.second );
1071
+
1072
+ assert (MRI.hasOneDef (State.first ));
1073
+ MachineInstr &Inst = *MRI.def_begin (State.first )->getParent ();
1074
+ CurInst = Inst.getIterator ();
1075
+
1076
+ // Any non-copy instruction is the defining instruction we're seeking.
1077
+ if (!Inst.isCopyLike () && !TII.isCopyInstr (Inst))
1078
+ break ;
1079
+ State = GetRegAndSubreg (Inst);
1080
+ };
1081
+
1082
+ // Helper lambda to apply additional subregister substitutions to a known
1083
+ // instruction/operand pair. Adds new (fake) substitutions so that we can
1084
+ // record the subregister. FIXME: this isn't very space efficient if multiple
1085
+ // values are tracked back through the same copies; cache something later.
1086
+ auto ApplySubregisters =
1087
+ [&](DebugInstrOperandPair P) -> DebugInstrOperandPair {
1088
+ for (unsigned Subreg : reverse (SubregsSeen)) {
1089
+ // Fetch a new instruction number, not attached to an actual instruction.
1090
+ unsigned NewInstrNumber = getNewDebugInstrNum ();
1091
+ // Add a substitution from the "new" number to the known one, with a
1092
+ // qualifying subreg.
1093
+ makeDebugValueSubstitution ({NewInstrNumber, 0 }, P, Subreg);
1094
+ // Return the new number; to find the underlying value, consumers need to
1095
+ // deal with the qualifying subreg.
1096
+ P = {NewInstrNumber, 0 };
1097
+ }
1098
+ return P;
1099
+ };
1100
+
1101
+ // If we managed to find the defining instruction after COPYs, return an
1102
+ // instruction / operand pair after adding subregister qualifiers.
1103
+ if (State.first .isVirtual ()) {
1104
+ // Virtual register def -- we can just look up where this happens.
1105
+ MachineInstr *Inst = MRI.def_begin (State.first )->getParent ();
1106
+ for (auto &MO : Inst->operands ()) {
1107
+ if (!MO.isReg () || !MO.isDef () || MO.getReg () != State.first )
1108
+ continue ;
1109
+ return ApplySubregisters (
1110
+ {Inst->getDebugInstrNum (), Inst->getOperandNo (&MO)});
1111
+ }
1112
+
1113
+ llvm_unreachable (" Vreg def with no corresponding operand?" );
1114
+ }
1115
+
1116
+ // Our search ended in a copy from a physreg: walk back up the function
1117
+ // looking for whatever defines the physreg.
1118
+ assert (CurInst->isCopyLike () || TII.isCopyInstr (*CurInst));
1119
+ State = GetRegAndSubreg (*CurInst);
1120
+ Register RegToSeek = State.first ;
1121
+
1122
+ auto RMII = CurInst->getReverseIterator ();
1123
+ auto PrevInstrs = make_range (RMII, CurInst->getParent ()->instr_rend ());
1124
+ for (auto &ToExamine : PrevInstrs) {
1125
+ for (auto &MO : ToExamine.operands ()) {
1126
+ // Test for operand that defines something aliasing RegToSeek.
1127
+ if (!MO.isReg () || !MO.isDef () ||
1128
+ !TRI.regsOverlap (RegToSeek, MO.getReg ()))
1129
+ continue ;
1130
+
1131
+ return ApplySubregisters (
1132
+ {ToExamine.getDebugInstrNum (), ToExamine.getOperandNo (&MO)});
1133
+ }
1134
+ }
1135
+
1136
+ // We reached the start of the block before finding a defining instruction.
1137
+ // It must be an argument: assert that this is the entry block, and produce
1138
+ // a DBG_PHI.
1139
+ assert (!State.first .isVirtual ());
1140
+ MachineBasicBlock &TargetBB = *CurInst->getParent ();
1141
+ assert (&*TargetBB.getParent ()->begin () == &TargetBB);
1142
+
1143
+ // Create DBG_PHI for specified physreg.
1144
+ auto Builder = BuildMI (TargetBB, TargetBB.getFirstNonPHI (), DebugLoc (),
1145
+ TII.get (TargetOpcode::DBG_PHI));
1146
+ Builder.addReg (State.first , RegState::Debug);
1147
+ unsigned NewNum = getNewDebugInstrNum ();
1148
+ Builder.addImm (NewNum);
1149
+ return ApplySubregisters ({NewNum, 0u });
1150
+ }
1151
+
1152
+ void MachineFunction::finalizeDebugInstrRefs () {
1153
+ auto *TII = getSubtarget ().getInstrInfo ();
1154
+
1155
+ auto MakeDbgValue = [&](MachineInstr &MI) {
1156
+ const MCInstrDesc &RefII = TII->get (TargetOpcode::DBG_VALUE);
1157
+ MI.setDesc (RefII);
1158
+ MI.getOperand (1 ).ChangeToRegister (0 , false );
1159
+ MI.getOperand (0 ).setIsDebug ();
1160
+ };
1161
+
1162
+ if (!getTarget ().Options .ValueTrackingVariableLocations )
1163
+ return ;
1164
+
1165
+ for (auto &MBB : *this ) {
1166
+ for (auto &MI : MBB) {
1167
+ if (!MI.isDebugRef () || !MI.getOperand (0 ).isReg ())
1168
+ continue ;
1169
+
1170
+ Register Reg = MI.getOperand (0 ).getReg ();
1171
+
1172
+ // Some vregs can be deleted as redundant in the meantime. Mark those
1173
+ // as DBG_VALUE $noreg.
1174
+ if (Reg == 0 ) {
1175
+ MakeDbgValue (MI);
1176
+ continue ;
1177
+ }
1178
+
1179
+ assert (Reg.isVirtual ());
1180
+ MachineInstr &DefMI = *RegInfo->def_instr_begin (Reg);
1181
+ assert (RegInfo->hasOneDef (Reg));
1182
+
1183
+ // If we've found a copy-like instruction, follow it back to the
1184
+ // instruction that defines the source value, see salvageCopySSA docs
1185
+ // for why this is important.
1186
+ if (DefMI.isCopyLike () || TII->isCopyInstr (DefMI)) {
1187
+ auto Result = salvageCopySSA (DefMI);
1188
+ MI.getOperand (0 ).ChangeToImmediate (Result.first );
1189
+ MI.getOperand (1 ).setImm (Result.second );
1190
+ } else {
1191
+ // Otherwise, identify the operand number that the VReg refers to.
1192
+ unsigned OperandIdx = 0 ;
1193
+ for (const auto &MO : DefMI.operands ()) {
1194
+ if (MO.isReg () && MO.isDef () && MO.getReg () == Reg)
1195
+ break ;
1196
+ ++OperandIdx;
1197
+ }
1198
+ assert (OperandIdx < DefMI.getNumOperands ());
1199
+
1200
+ // Morph this instr ref to point at the given instruction and operand.
1201
+ unsigned ID = DefMI.getDebugInstrNum ();
1202
+ MI.getOperand (0 ).ChangeToImmediate (ID);
1203
+ MI.getOperand (1 ).setImm (OperandIdx);
1204
+ }
1205
+ }
1206
+ }
1207
+ }
1208
+
1010
1209
// / \}
1011
1210
1012
1211
// ===----------------------------------------------------------------------===//
0 commit comments