@@ -158,6 +158,9 @@ class MIRParserImpl {
158158 MachineFunction &MF,
159159 const yaml::MachineFunction &YMF);
160160
161+ bool parseCalledGlobals (PerFunctionMIParsingState &PFS, MachineFunction &MF,
162+ const yaml::MachineFunction &YMF);
163+
161164private:
162165 bool parseMDNode (PerFunctionMIParsingState &PFS, MDNode *&Node,
163166 const yaml::StringValue &Source);
@@ -183,6 +186,9 @@ class MIRParserImpl {
183186
184187 void setupDebugValueTracking (MachineFunction &MF,
185188 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF);
189+
190+ bool parseMachineInst (MachineFunction &MF, yaml::MachineInstrLoc MILoc,
191+ MachineInstr const *&MI);
186192};
187193
188194} // end namespace llvm
@@ -457,24 +463,34 @@ bool MIRParserImpl::computeFunctionProperties(
457463 return false ;
458464}
459465
466+ bool MIRParserImpl::parseMachineInst (MachineFunction &MF,
467+ yaml::MachineInstrLoc MILoc,
468+ MachineInstr const *&MI) {
469+ if (MILoc.BlockNum >= MF.size ()) {
470+ return error (Twine (MF.getName ()) +
471+ Twine (" instruction block out of range." ) +
472+ " Unable to reference bb:" + Twine (MILoc.BlockNum ));
473+ }
474+ auto BB = std::next (MF.begin (), MILoc.BlockNum );
475+ if (MILoc.Offset >= BB->size ())
476+ return error (
477+ Twine (MF.getName ()) + Twine (" instruction offset out of range." ) +
478+ " Unable to reference instruction at bb: " + Twine (MILoc.BlockNum ) +
479+ " at offset:" + Twine (MILoc.Offset ));
480+ MI = &*std::next (BB->instr_begin (), MILoc.Offset );
481+ return false ;
482+ }
483+
460484bool MIRParserImpl::initializeCallSiteInfo (
461485 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) {
462486 MachineFunction &MF = PFS.MF ;
463487 SMDiagnostic Error;
464488 const TargetMachine &TM = MF.getTarget ();
465489 for (auto &YamlCSInfo : YamlMF.CallSitesInfo ) {
466- yaml::CallSiteInfo::MachineInstrLoc MILoc = YamlCSInfo.CallLocation ;
467- if (MILoc.BlockNum >= MF.size ())
468- return error (Twine (MF.getName ()) +
469- Twine (" call instruction block out of range." ) +
470- " Unable to reference bb:" + Twine (MILoc.BlockNum ));
471- auto CallB = std::next (MF.begin (), MILoc.BlockNum );
472- if (MILoc.Offset >= CallB->size ())
473- return error (Twine (MF.getName ()) +
474- Twine (" call instruction offset out of range." ) +
475- " Unable to reference instruction at bb: " +
476- Twine (MILoc.BlockNum ) + " at offset:" + Twine (MILoc.Offset ));
477- auto CallI = std::next (CallB->instr_begin (), MILoc.Offset );
490+ yaml::MachineInstrLoc MILoc = YamlCSInfo.CallLocation ;
491+ const MachineInstr *CallI;
492+ if (parseMachineInst (MF, MILoc, CallI))
493+ return true ;
478494 if (!CallI->isCall (MachineInstr::IgnoreBundle))
479495 return error (Twine (MF.getName ()) +
480496 Twine (" call site info should reference call "
@@ -641,6 +657,9 @@ MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
641657 if (initializeCallSiteInfo (PFS, YamlMF))
642658 return true ;
643659
660+ if (parseCalledGlobals (PFS, MF, YamlMF))
661+ return true ;
662+
644663 setupDebugValueTracking (MF, PFS, YamlMF);
645664
646665 MF.getSubtarget ().mirFileLoaded (MF);
@@ -1111,6 +1130,37 @@ bool MIRParserImpl::parseMachineMetadataNodes(
11111130 return false ;
11121131}
11131132
1133+ bool MIRParserImpl::parseCalledGlobals (PerFunctionMIParsingState &PFS,
1134+ MachineFunction &MF,
1135+ const yaml::MachineFunction &YMF) {
1136+ Function &F = MF.getFunction ();
1137+ for (const auto &YamlCG : YMF.CalledGlobals ) {
1138+ yaml::MachineInstrLoc MILoc = YamlCG.CallSite ;
1139+ const MachineInstr *CallI;
1140+ if (parseMachineInst (MF, MILoc, CallI))
1141+ return true ;
1142+ if (!CallI->isCall (MachineInstr::IgnoreBundle))
1143+ return error (Twine (MF.getName ()) +
1144+ Twine (" called global should reference call "
1145+ " instruction. Instruction at bb:" ) +
1146+ Twine (MILoc.BlockNum ) + " at offset:" + Twine (MILoc.Offset ) +
1147+ " is not a call instruction" );
1148+
1149+ auto Callee =
1150+ F.getParent ()->getValueSymbolTable ().lookup (YamlCG.Callee .Value );
1151+ if (!Callee)
1152+ return error (YamlCG.Callee .SourceRange .Start ,
1153+ " use of undefined global '" + YamlCG.Callee .Value + " '" );
1154+ if (!isa<GlobalValue>(Callee))
1155+ return error (YamlCG.Callee .SourceRange .Start ,
1156+ " use of non-global value '" + YamlCG.Callee .Value + " '" );
1157+
1158+ MF.addCalledGlobal (CallI, {cast<GlobalValue>(Callee), YamlCG.Flags });
1159+ }
1160+
1161+ return false ;
1162+ }
1163+
11141164SMDiagnostic MIRParserImpl::diagFromMIStringDiag (const SMDiagnostic &Error,
11151165 SMRange SourceRange) {
11161166 assert (SourceRange.isValid () && " Invalid source range" );
0 commit comments