2015-05-28 02:02:19 +08:00
|
|
|
//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the class that parses the optional LLVM IR and machine
|
|
|
|
// functions that are stored in MIR files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MIRParser/MIRParser.h"
|
2015-06-23 01:02:30 +08:00
|
|
|
#include "MIParser.h"
|
2015-06-27 00:46:11 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-04-09 00:40:43 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2015-06-27 06:56:48 +08:00
|
|
|
#include "llvm/AsmParser/SlotMapping.h"
|
2016-04-09 00:40:43 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
|
|
|
|
#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
|
|
|
|
#include "llvm/CodeGen/MIRYamlMapping.h"
|
2015-07-21 04:51:18 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2016-04-09 00:40:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2015-08-19 08:13:25 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2015-06-25 03:56:10 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2015-06-20 01:43:07 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2016-04-15 02:29:59 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2015-06-16 07:07:38 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-06-20 01:43:07 +08:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2015-05-30 01:05:41 +08:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2016-04-09 00:40:43 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
namespace llvm {
|
2015-05-28 02:02:19 +08:00
|
|
|
|
|
|
|
/// This class implements the parsing of LLVM IR that's embedded inside a MIR
|
|
|
|
/// file.
|
|
|
|
class MIRParserImpl {
|
|
|
|
SourceMgr SM;
|
2017-06-06 08:44:35 +08:00
|
|
|
yaml::Input In;
|
2015-05-28 02:02:19 +08:00
|
|
|
StringRef Filename;
|
|
|
|
LLVMContext &Context;
|
2015-06-27 06:56:48 +08:00
|
|
|
SlotMapping IRSlots;
|
2015-07-10 06:23:13 +08:00
|
|
|
/// Maps from register class names to register classes.
|
2017-01-18 08:59:19 +08:00
|
|
|
Name2RegClassMap Names2RegClasses;
|
2016-04-09 00:40:43 +08:00
|
|
|
/// Maps from register bank names to register banks.
|
2017-01-18 08:59:19 +08:00
|
|
|
Name2RegBankMap Names2RegBanks;
|
2017-06-06 08:44:35 +08:00
|
|
|
/// True when the MIR file doesn't have LLVM IR. Dummy IR functions are
|
|
|
|
/// created and inserted into the given module when this is true.
|
|
|
|
bool NoLLVMIR = false;
|
|
|
|
/// True when a well formed MIR file does not contain any MIR/machine function
|
|
|
|
/// parts.
|
|
|
|
bool NoMIRDocuments = false;
|
2015-05-28 02:02:19 +08:00
|
|
|
|
|
|
|
public:
|
2017-06-06 08:44:35 +08:00
|
|
|
MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
|
|
|
|
StringRef Filename, LLVMContext &Context);
|
2015-05-28 02:02:19 +08:00
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
void reportDiagnostic(const SMDiagnostic &Diag);
|
|
|
|
|
|
|
|
/// Report an error with the given message at unknown location.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(const Twine &Message);
|
|
|
|
|
2015-07-09 04:22:20 +08:00
|
|
|
/// Report an error with the given message at the given location.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(SMLoc Loc, const Twine &Message);
|
|
|
|
|
2015-07-01 01:55:00 +08:00
|
|
|
/// Report a given error with the location translated from the location in an
|
|
|
|
/// embedded string literal to a location in the MIR file.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(const SMDiagnostic &Error, SMRange SourceRange);
|
|
|
|
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Try to parse the optional LLVM module and the machine functions in the MIR
|
|
|
|
/// file.
|
2015-05-28 02:02:19 +08:00
|
|
|
///
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Return null if an error occurred.
|
2017-06-06 08:44:35 +08:00
|
|
|
std::unique_ptr<Module> parseIRModule();
|
|
|
|
|
|
|
|
bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
|
2015-05-29 06:41:12 +08:00
|
|
|
|
|
|
|
/// Parse the machine function in the current YAML document.
|
|
|
|
///
|
2015-06-16 07:07:38 +08:00
|
|
|
///
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Return true if an error occurred.
|
2017-06-06 08:44:35 +08:00
|
|
|
bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
|
2015-05-30 01:05:41 +08:00
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
/// Initialize the machine function to the state that's described in the MIR
|
|
|
|
/// file.
|
|
|
|
///
|
|
|
|
/// Return true if error occurred.
|
2017-06-06 08:44:35 +08:00
|
|
|
bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
|
|
|
|
MachineFunction &MF);
|
2015-06-16 04:30:22 +08:00
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineFunction &YamlMF);
|
2015-06-25 03:56:10 +08:00
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
|
2015-08-11 08:32:49 +08:00
|
|
|
const yaml::MachineFunction &YamlMF);
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineFunction &YamlMF);
|
2015-07-25 06:22:50 +08:00
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
|
2015-07-25 06:22:50 +08:00
|
|
|
std::vector<CalleeSavedInfo> &CSIInfo,
|
|
|
|
const yaml::StringValue &RegisterSource,
|
2017-09-29 02:52:14 +08:00
|
|
|
bool IsRestored, int FrameIdx);
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
|
2015-08-19 08:13:25 +08:00
|
|
|
const yaml::MachineStackObject &Object,
|
|
|
|
int FrameIdx);
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool initializeConstantPool(PerFunctionMIParsingState &PFS,
|
|
|
|
MachineConstantPool &ConstantPool,
|
|
|
|
const yaml::MachineFunction &YamlMF);
|
2015-07-21 04:51:18 +08:00
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineJumpTable &YamlJTI);
|
2015-07-16 07:31:07 +08:00
|
|
|
|
2015-05-30 01:05:41 +08:00
|
|
|
private:
|
2016-10-11 11:13:01 +08:00
|
|
|
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
|
2016-07-14 06:23:23 +08:00
|
|
|
const yaml::StringValue &Source);
|
2015-08-19 08:13:25 +08:00
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool parseMBBReference(PerFunctionMIParsingState &PFS,
|
2016-07-14 06:23:23 +08:00
|
|
|
MachineBasicBlock *&MBB,
|
|
|
|
const yaml::StringValue &Source);
|
2015-07-30 04:57:11 +08:00
|
|
|
|
2015-06-24 06:39:23 +08:00
|
|
|
/// Return a MIR diagnostic converted from an MI string diagnostic.
|
|
|
|
SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange);
|
|
|
|
|
2015-08-14 04:30:11 +08:00
|
|
|
/// Return a MIR diagnostic converted from a diagnostic located in a YAML
|
|
|
|
/// block scalar string.
|
|
|
|
SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange);
|
2015-06-16 07:07:38 +08:00
|
|
|
|
2015-07-10 06:23:13 +08:00
|
|
|
void initNames2RegClasses(const MachineFunction &MF);
|
2016-04-09 00:40:43 +08:00
|
|
|
void initNames2RegBanks(const MachineFunction &MF);
|
2015-07-10 06:23:13 +08:00
|
|
|
|
|
|
|
/// Check if the given identifier is a name of a register class.
|
|
|
|
///
|
|
|
|
/// Return null if the name isn't a register class.
|
|
|
|
const TargetRegisterClass *getRegClass(const MachineFunction &MF,
|
|
|
|
StringRef Name);
|
2016-04-09 00:40:43 +08:00
|
|
|
|
|
|
|
/// Check if the given identifier is a name of a register bank.
|
|
|
|
///
|
|
|
|
/// Return null if the name isn't a register bank.
|
|
|
|
const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name);
|
2016-08-24 05:19:49 +08:00
|
|
|
|
|
|
|
void computeFunctionProperties(MachineFunction &MF);
|
2015-05-28 02:02:19 +08:00
|
|
|
};
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
} // end namespace llvm
|
2015-05-28 02:02:19 +08:00
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
|
|
|
|
reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
|
|
|
|
}
|
|
|
|
|
2015-05-28 02:02:19 +08:00
|
|
|
MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
|
|
|
|
StringRef Filename, LLVMContext &Context)
|
2017-06-06 08:44:35 +08:00
|
|
|
: SM(),
|
|
|
|
In(SM.getMemoryBuffer(
|
|
|
|
SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))->getBuffer(),
|
|
|
|
nullptr, handleYAMLDiag, this),
|
|
|
|
Filename(Filename),
|
|
|
|
Context(Context) {
|
|
|
|
In.setContext(&In);
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
bool MIRParserImpl::error(const Twine &Message) {
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
|
|
DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-09 04:22:20 +08:00
|
|
|
bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
|
|
DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-01 01:55:00 +08:00
|
|
|
bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
|
|
|
|
assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
|
|
|
|
reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
|
|
|
|
DiagnosticSeverity Kind;
|
|
|
|
switch (Diag.getKind()) {
|
|
|
|
case SourceMgr::DK_Error:
|
|
|
|
Kind = DS_Error;
|
|
|
|
break;
|
|
|
|
case SourceMgr::DK_Warning:
|
|
|
|
Kind = DS_Warning;
|
|
|
|
break;
|
|
|
|
case SourceMgr::DK_Note:
|
|
|
|
Kind = DS_Note;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
|
|
|
|
}
|
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
std::unique_ptr<Module> MIRParserImpl::parseIRModule() {
|
2015-05-29 06:41:12 +08:00
|
|
|
if (!In.setCurrentDocument()) {
|
2015-06-16 04:30:22 +08:00
|
|
|
if (In.error())
|
2015-05-29 06:41:12 +08:00
|
|
|
return nullptr;
|
|
|
|
// Create an empty module when the MIR file is empty.
|
2017-06-06 08:44:35 +08:00
|
|
|
NoMIRDocuments = true;
|
2015-05-29 06:41:12 +08:00
|
|
|
return llvm::make_unique<Module>(Filename, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Module> M;
|
2015-05-28 02:02:19 +08:00
|
|
|
// Parse the block scalar manually so that we can return unique pointer
|
|
|
|
// without having to go trough YAML traits.
|
2015-05-29 06:41:12 +08:00
|
|
|
if (const auto *BSN =
|
|
|
|
dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
|
2015-06-16 04:30:22 +08:00
|
|
|
SMDiagnostic Error;
|
2015-05-29 06:41:12 +08:00
|
|
|
M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
|
2015-06-27 06:56:48 +08:00
|
|
|
Context, &IRSlots);
|
2015-05-30 01:05:41 +08:00
|
|
|
if (!M) {
|
2015-08-14 04:30:11 +08:00
|
|
|
reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
|
2016-07-14 08:42:37 +08:00
|
|
|
return nullptr;
|
2015-05-30 01:05:41 +08:00
|
|
|
}
|
2015-05-29 06:41:12 +08:00
|
|
|
In.nextDocument();
|
|
|
|
if (!In.setCurrentDocument())
|
2017-06-06 08:44:35 +08:00
|
|
|
NoMIRDocuments = true;
|
2015-05-29 06:41:12 +08:00
|
|
|
} else {
|
|
|
|
// Create an new, empty module.
|
|
|
|
M = llvm::make_unique<Module>(Filename, Context);
|
2015-06-16 07:07:38 +08:00
|
|
|
NoLLVMIR = true;
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
2017-06-06 08:44:35 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
|
|
|
|
if (NoMIRDocuments)
|
|
|
|
return false;
|
2015-05-28 02:02:19 +08:00
|
|
|
|
2015-05-29 06:41:12 +08:00
|
|
|
// Parse the machine functions.
|
|
|
|
do {
|
2017-06-06 08:44:35 +08:00
|
|
|
if (parseMachineFunction(M, MMI))
|
|
|
|
return true;
|
2015-05-29 06:41:12 +08:00
|
|
|
In.nextDocument();
|
|
|
|
} while (In.setCurrentDocument());
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
/// Create an empty function with the given name.
|
|
|
|
static Function *createDummyFunction(StringRef Name, Module &M) {
|
2015-06-16 07:07:38 +08:00
|
|
|
auto &Context = M.getContext();
|
|
|
|
Function *F = cast<Function>(M.getOrInsertFunction(
|
|
|
|
Name, FunctionType::get(Type::getVoidTy(Context), false)));
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
|
|
|
|
new UnreachableInst(Context, BB);
|
2017-06-06 08:44:35 +08:00
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
|
|
|
|
// Parse the yaml.
|
|
|
|
yaml::MachineFunction YamlMF;
|
|
|
|
yaml::EmptyContext Ctx;
|
|
|
|
yaml::yamlize(In, YamlMF, false, Ctx);
|
|
|
|
if (In.error())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search for the corresponding IR function.
|
|
|
|
StringRef FunctionName = YamlMF.Name;
|
|
|
|
Function *F = M.getFunction(FunctionName);
|
|
|
|
if (!F) {
|
|
|
|
if (NoLLVMIR) {
|
|
|
|
F = createDummyFunction(FunctionName, M);
|
|
|
|
} else {
|
|
|
|
return error(Twine("function '") + FunctionName +
|
|
|
|
"' isn't defined in the provided LLVM IR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (MMI.getMachineFunction(*F) != nullptr)
|
|
|
|
return error(Twine("redefinition of machine function '") + FunctionName +
|
|
|
|
"'");
|
|
|
|
|
|
|
|
// Create the MachineFunction.
|
|
|
|
MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
|
|
|
|
if (initializeMachineFunction(YamlMF, MF))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2015-06-16 07:07:38 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 09:32:41 +08:00
|
|
|
static bool isSSA(const MachineFunction &MF) {
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
|
|
|
|
if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:19:49 +08:00
|
|
|
void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
|
2016-08-24 09:32:41 +08:00
|
|
|
MachineFunctionProperties &Properties = MF.getProperties();
|
2016-08-25 06:34:06 +08:00
|
|
|
|
|
|
|
bool HasPHI = false;
|
|
|
|
bool HasInlineAsm = false;
|
|
|
|
for (const MachineBasicBlock &MBB : MF) {
|
|
|
|
for (const MachineInstr &MI : MBB) {
|
|
|
|
if (MI.isPHI())
|
|
|
|
HasPHI = true;
|
|
|
|
if (MI.isInlineAsm())
|
|
|
|
HasInlineAsm = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!HasPHI)
|
2016-08-24 09:32:41 +08:00
|
|
|
Properties.set(MachineFunctionProperties::Property::NoPHIs);
|
2016-08-25 06:34:06 +08:00
|
|
|
MF.setHasInlineAsm(HasInlineAsm);
|
2016-08-24 09:32:41 +08:00
|
|
|
|
|
|
|
if (isSSA(MF))
|
|
|
|
Properties.set(MachineFunctionProperties::Property::IsSSA);
|
|
|
|
else
|
2016-08-27 06:09:11 +08:00
|
|
|
Properties.reset(MachineFunctionProperties::Property::IsSSA);
|
2016-08-25 09:27:13 +08:00
|
|
|
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
if (MRI.getNumVirtRegs() == 0)
|
|
|
|
Properties.set(MachineFunctionProperties::Property::NoVRegs);
|
2016-08-24 05:19:49 +08:00
|
|
|
}
|
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
bool
|
|
|
|
MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
|
|
|
|
MachineFunction &MF) {
|
2015-06-16 04:30:22 +08:00
|
|
|
// TODO: Recreate the machine function.
|
2017-01-18 08:59:19 +08:00
|
|
|
initNames2RegClasses(MF);
|
|
|
|
initNames2RegBanks(MF);
|
2015-06-16 08:10:47 +08:00
|
|
|
if (YamlMF.Alignment)
|
|
|
|
MF.setAlignment(YamlMF.Alignment);
|
|
|
|
MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
|
2016-08-02 23:10:25 +08:00
|
|
|
|
|
|
|
if (YamlMF.Legalized)
|
|
|
|
MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
|
2016-08-03 00:17:10 +08:00
|
|
|
if (YamlMF.RegBankSelected)
|
|
|
|
MF.getProperties().set(
|
|
|
|
MachineFunctionProperties::Property::RegBankSelected);
|
2016-08-03 00:49:19 +08:00
|
|
|
if (YamlMF.Selected)
|
|
|
|
MF.getProperties().set(MachineFunctionProperties::Property::Selected);
|
2016-08-02 23:10:25 +08:00
|
|
|
|
2017-01-18 08:59:19 +08:00
|
|
|
PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
|
|
|
|
Names2RegBanks);
|
2016-10-11 11:13:01 +08:00
|
|
|
if (parseRegisterInfo(PFS, YamlMF))
|
2015-06-25 03:56:10 +08:00
|
|
|
return true;
|
2015-07-21 04:51:18 +08:00
|
|
|
if (!YamlMF.Constants.empty()) {
|
|
|
|
auto *ConstantPool = MF.getConstantPool();
|
|
|
|
assert(ConstantPool && "Constant pool must be created");
|
2016-07-14 06:23:23 +08:00
|
|
|
if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
|
2015-07-21 04:51:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-25 03:56:10 +08:00
|
|
|
|
2016-07-14 07:27:50 +08:00
|
|
|
StringRef BlockStr = YamlMF.Body.Value.Value;
|
2015-08-14 07:10:16 +08:00
|
|
|
SMDiagnostic Error;
|
2016-07-14 07:27:50 +08:00
|
|
|
SourceMgr BlockSM;
|
|
|
|
BlockSM.AddNewSourceBuffer(
|
|
|
|
MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
|
|
|
|
SMLoc());
|
|
|
|
PFS.SM = &BlockSM;
|
|
|
|
if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
|
2015-08-14 07:10:16 +08:00
|
|
|
reportDiagnostic(
|
|
|
|
diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
|
|
|
|
return true;
|
2015-06-27 00:46:11 +08:00
|
|
|
}
|
2016-07-14 07:27:50 +08:00
|
|
|
PFS.SM = &SM;
|
2015-06-27 00:46:11 +08:00
|
|
|
|
2015-07-30 05:09:09 +08:00
|
|
|
// Initialize the frame information after creating all the MBBs so that the
|
|
|
|
// MBB references in the frame information can be resolved.
|
2016-07-14 06:23:23 +08:00
|
|
|
if (initializeFrameInfo(PFS, YamlMF))
|
2015-07-30 05:09:09 +08:00
|
|
|
return true;
|
2015-07-16 07:31:07 +08:00
|
|
|
// Initialize the jump table after creating all the MBBs so that the MBB
|
|
|
|
// references can be resolved.
|
|
|
|
if (!YamlMF.JumpTableInfo.Entries.empty() &&
|
2016-07-14 06:23:23 +08:00
|
|
|
initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
|
2015-07-16 07:31:07 +08:00
|
|
|
return true;
|
2015-08-14 07:10:16 +08:00
|
|
|
// Parse the machine instructions after creating all of the MBBs so that the
|
|
|
|
// parser can resolve the MBB references.
|
2016-07-14 07:27:50 +08:00
|
|
|
StringRef InsnStr = YamlMF.Body.Value.Value;
|
|
|
|
SourceMgr InsnSM;
|
|
|
|
InsnSM.AddNewSourceBuffer(
|
|
|
|
MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
|
|
|
|
SMLoc());
|
|
|
|
PFS.SM = &InsnSM;
|
|
|
|
if (parseMachineInstructions(PFS, InsnStr, Error)) {
|
2015-08-14 07:10:16 +08:00
|
|
|
reportDiagnostic(
|
|
|
|
diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
|
|
|
|
return true;
|
2015-06-20 01:43:07 +08:00
|
|
|
}
|
2016-07-14 07:27:50 +08:00
|
|
|
PFS.SM = &SM;
|
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
if (setupRegisterInfo(PFS, YamlMF))
|
|
|
|
return true;
|
2016-08-24 05:19:49 +08:00
|
|
|
|
|
|
|
computeFunctionProperties(MF);
|
|
|
|
|
2015-07-25 01:44:49 +08:00
|
|
|
MF.verify();
|
2015-06-20 01:43:07 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineFunction &YamlMF) {
|
2016-07-14 06:23:23 +08:00
|
|
|
MachineFunction &MF = PFS.MF;
|
2015-07-29 00:48:37 +08:00
|
|
|
MachineRegisterInfo &RegInfo = MF.getRegInfo();
|
2015-06-25 03:56:10 +08:00
|
|
|
assert(RegInfo.tracksLiveness());
|
|
|
|
if (!YamlMF.TracksRegLiveness)
|
|
|
|
RegInfo.invalidateLiveness();
|
2015-07-10 06:23:13 +08:00
|
|
|
|
2015-07-25 04:35:40 +08:00
|
|
|
SMDiagnostic Error;
|
2015-07-10 06:23:13 +08:00
|
|
|
// Parse the virtual register information.
|
|
|
|
for (const auto &VReg : YamlMF.VirtualRegisters) {
|
2016-10-11 11:13:01 +08:00
|
|
|
VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
|
|
|
|
if (Info.Explicit)
|
|
|
|
return error(VReg.ID.SourceRange.Start,
|
|
|
|
Twine("redefinition of virtual register '%") +
|
|
|
|
Twine(VReg.ID.Value) + "'");
|
|
|
|
Info.Explicit = true;
|
|
|
|
|
2016-03-08 09:17:03 +08:00
|
|
|
if (StringRef(VReg.Class.Value).equals("_")) {
|
2016-10-11 11:13:01 +08:00
|
|
|
Info.Kind = VRegInfo::GENERIC;
|
2016-03-08 09:17:03 +08:00
|
|
|
} else {
|
|
|
|
const auto *RC = getRegClass(MF, VReg.Class.Value);
|
2016-04-09 00:40:43 +08:00
|
|
|
if (RC) {
|
2016-10-11 11:13:01 +08:00
|
|
|
Info.Kind = VRegInfo::NORMAL;
|
|
|
|
Info.D.RC = RC;
|
2016-04-09 00:40:43 +08:00
|
|
|
} else {
|
2016-10-11 11:13:01 +08:00
|
|
|
const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
|
2016-04-09 00:40:43 +08:00
|
|
|
if (!RegBank)
|
|
|
|
return error(
|
|
|
|
VReg.Class.SourceRange.Start,
|
|
|
|
Twine("use of undefined register class or register bank '") +
|
|
|
|
VReg.Class.Value + "'");
|
2016-10-11 11:13:01 +08:00
|
|
|
Info.Kind = VRegInfo::REGBANK;
|
|
|
|
Info.D.RegBank = RegBank;
|
2016-04-09 00:40:43 +08:00
|
|
|
}
|
2016-03-08 09:17:03 +08:00
|
|
|
}
|
2016-10-11 11:13:01 +08:00
|
|
|
|
2015-07-25 04:35:40 +08:00
|
|
|
if (!VReg.PreferredRegister.Value.empty()) {
|
2016-10-11 11:13:01 +08:00
|
|
|
if (Info.Kind != VRegInfo::NORMAL)
|
|
|
|
return error(VReg.Class.SourceRange.Start,
|
|
|
|
Twine("preferred register can only be set for normal vregs"));
|
2016-11-15 08:03:14 +08:00
|
|
|
|
|
|
|
if (parseRegisterReference(PFS, Info.PreferredReg,
|
|
|
|
VReg.PreferredRegister.Value, Error))
|
2015-07-25 04:35:40 +08:00
|
|
|
return error(Error, VReg.PreferredRegister.SourceRange);
|
|
|
|
}
|
2015-07-10 06:23:13 +08:00
|
|
|
}
|
2015-07-28 01:42:45 +08:00
|
|
|
|
|
|
|
// Parse the liveins.
|
|
|
|
for (const auto &LiveIn : YamlMF.LiveIns) {
|
|
|
|
unsigned Reg = 0;
|
2016-07-14 07:27:50 +08:00
|
|
|
if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
|
2015-07-28 01:42:45 +08:00
|
|
|
return error(Error, LiveIn.Register.SourceRange);
|
|
|
|
unsigned VReg = 0;
|
|
|
|
if (!LiveIn.VirtualRegister.Value.empty()) {
|
2016-10-11 11:13:01 +08:00
|
|
|
VRegInfo *Info;
|
|
|
|
if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
|
2016-07-14 07:27:50 +08:00
|
|
|
Error))
|
2015-07-28 01:42:45 +08:00
|
|
|
return error(Error, LiveIn.VirtualRegister.SourceRange);
|
2016-10-11 11:13:01 +08:00
|
|
|
VReg = Info->VReg;
|
2015-07-28 01:42:45 +08:00
|
|
|
}
|
|
|
|
RegInfo.addLiveIn(Reg, VReg);
|
|
|
|
}
|
2015-08-11 08:32:49 +08:00
|
|
|
|
2017-03-19 16:14:18 +08:00
|
|
|
// Parse the callee saved registers (Registers that will
|
|
|
|
// be saved for the caller).
|
|
|
|
if (YamlMF.CalleeSavedRegisters) {
|
|
|
|
SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
|
|
|
|
for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
|
|
|
|
unsigned Reg = 0;
|
|
|
|
if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
|
|
|
|
return error(Error, RegSource.SourceRange);
|
|
|
|
CalleeSavedRegisters.push_back(Reg);
|
|
|
|
}
|
|
|
|
RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
|
2015-08-11 08:32:49 +08:00
|
|
|
}
|
2017-03-19 16:14:18 +08:00
|
|
|
|
2015-06-25 03:56:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
|
2015-08-11 08:32:49 +08:00
|
|
|
const yaml::MachineFunction &YamlMF) {
|
2016-10-11 11:13:01 +08:00
|
|
|
MachineFunction &MF = PFS.MF;
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
bool Error = false;
|
|
|
|
// Create VRegs
|
|
|
|
for (auto P : PFS.VRegInfos) {
|
|
|
|
const VRegInfo &Info = *P.second;
|
|
|
|
unsigned Reg = Info.VReg;
|
|
|
|
switch (Info.Kind) {
|
|
|
|
case VRegInfo::UNKNOWN:
|
|
|
|
error(Twine("Cannot determine class/bank of virtual register ") +
|
|
|
|
Twine(P.first) + " in function '" + MF.getName() + "'");
|
|
|
|
Error = true;
|
|
|
|
break;
|
|
|
|
case VRegInfo::NORMAL:
|
|
|
|
MRI.setRegClass(Reg, Info.D.RC);
|
|
|
|
if (Info.PreferredReg != 0)
|
|
|
|
MRI.setSimpleHint(Reg, Info.PreferredReg);
|
|
|
|
break;
|
|
|
|
case VRegInfo::GENERIC:
|
|
|
|
break;
|
|
|
|
case VRegInfo::REGBANK:
|
|
|
|
MRI.setRegBank(Reg, *Info.D.RegBank);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute MachineRegisterInfo::UsedPhysRegMask
|
2017-03-19 16:14:18 +08:00
|
|
|
for (const MachineBasicBlock &MBB : MF) {
|
|
|
|
for (const MachineInstr &MI : MBB) {
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
|
|
|
if (!MO.isRegMask())
|
|
|
|
continue;
|
|
|
|
MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
|
2015-08-11 08:32:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 11:13:01 +08:00
|
|
|
|
|
|
|
// FIXME: This is a temporary workaround until the reserved registers can be
|
|
|
|
// serialized.
|
|
|
|
MRI.freezeReservedRegs(MF);
|
|
|
|
return Error;
|
2015-08-11 08:32:49 +08:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineFunction &YamlMF) {
|
|
|
|
MachineFunction &MF = PFS.MF;
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2015-07-25 06:22:50 +08:00
|
|
|
const Function &F = *MF.getFunction();
|
2015-07-11 02:13:57 +08:00
|
|
|
const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
|
|
|
|
MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
|
|
|
|
MFI.setHasStackMap(YamlMFI.HasStackMap);
|
|
|
|
MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
|
|
|
|
MFI.setStackSize(YamlMFI.StackSize);
|
|
|
|
MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
|
|
|
|
if (YamlMFI.MaxAlignment)
|
|
|
|
MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
|
|
|
|
MFI.setAdjustsStack(YamlMFI.AdjustsStack);
|
|
|
|
MFI.setHasCalls(YamlMFI.HasCalls);
|
2017-05-02 06:32:25 +08:00
|
|
|
if (YamlMFI.MaxCallFrameSize != ~0u)
|
|
|
|
MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
|
|
|
|
MFI.setHasVAStart(YamlMFI.HasVAStart);
|
|
|
|
MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
|
2015-07-30 05:09:09 +08:00
|
|
|
if (!YamlMFI.SavePoint.Value.empty()) {
|
|
|
|
MachineBasicBlock *MBB = nullptr;
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
|
2015-07-30 05:09:09 +08:00
|
|
|
return true;
|
|
|
|
MFI.setSavePoint(MBB);
|
|
|
|
}
|
|
|
|
if (!YamlMFI.RestorePoint.Value.empty()) {
|
|
|
|
MachineBasicBlock *MBB = nullptr;
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
|
2015-07-30 05:09:09 +08:00
|
|
|
return true;
|
|
|
|
MFI.setRestorePoint(MBB);
|
|
|
|
}
|
2015-07-11 02:13:57 +08:00
|
|
|
|
2015-07-25 06:22:50 +08:00
|
|
|
std::vector<CalleeSavedInfo> CSIInfo;
|
2015-07-14 02:07:26 +08:00
|
|
|
// Initialize the fixed frame objects.
|
|
|
|
for (const auto &Object : YamlMF.FixedStackObjects) {
|
|
|
|
int ObjectIdx;
|
|
|
|
if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
|
|
|
|
ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
|
|
|
|
Object.IsImmutable, Object.IsAliased);
|
|
|
|
else
|
|
|
|
ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
|
|
|
|
MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
|
2017-07-21 05:03:45 +08:00
|
|
|
MFI.setStackID(ObjectIdx, Object.StackID);
|
2015-08-11 07:45:02 +08:00
|
|
|
if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
|
|
|
|
ObjectIdx))
|
|
|
|
.second)
|
|
|
|
return error(Object.ID.SourceRange.Start,
|
|
|
|
Twine("redefinition of fixed stack object '%fixed-stack.") +
|
|
|
|
Twine(Object.ID.Value) + "'");
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
|
2017-09-29 02:52:14 +08:00
|
|
|
Object.CalleeSavedRestored, ObjectIdx))
|
2015-07-25 06:22:50 +08:00
|
|
|
return true;
|
2015-07-14 02:07:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the ordinary frame objects.
|
2015-07-11 02:13:57 +08:00
|
|
|
for (const auto &Object : YamlMF.StackObjects) {
|
2015-07-14 08:26:26 +08:00
|
|
|
int ObjectIdx;
|
2015-07-16 06:14:49 +08:00
|
|
|
const AllocaInst *Alloca = nullptr;
|
|
|
|
const yaml::StringValue &Name = Object.Name;
|
|
|
|
if (!Name.Value.empty()) {
|
|
|
|
Alloca = dyn_cast_or_null<AllocaInst>(
|
2016-09-17 14:00:02 +08:00
|
|
|
F.getValueSymbolTable()->lookup(Name.Value));
|
2015-07-16 06:14:49 +08:00
|
|
|
if (!Alloca)
|
|
|
|
return error(Name.SourceRange.Start,
|
|
|
|
"alloca instruction named '" + Name.Value +
|
|
|
|
"' isn't defined in the function '" + F.getName() +
|
|
|
|
"'");
|
|
|
|
}
|
2015-07-14 08:26:26 +08:00
|
|
|
if (Object.Type == yaml::MachineStackObject::VariableSized)
|
2015-07-16 06:14:49 +08:00
|
|
|
ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
|
2015-07-14 08:26:26 +08:00
|
|
|
else
|
|
|
|
ObjectIdx = MFI.CreateStackObject(
|
|
|
|
Object.Size, Object.Alignment,
|
2015-07-16 06:14:49 +08:00
|
|
|
Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
|
2015-07-11 02:13:57 +08:00
|
|
|
MFI.setObjectOffset(ObjectIdx, Object.Offset);
|
2017-07-21 05:03:45 +08:00
|
|
|
MFI.setStackID(ObjectIdx, Object.StackID);
|
|
|
|
|
2015-08-11 07:50:41 +08:00
|
|
|
if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
|
|
|
|
.second)
|
|
|
|
return error(Object.ID.SourceRange.Start,
|
|
|
|
Twine("redefinition of stack object '%stack.") +
|
|
|
|
Twine(Object.ID.Value) + "'");
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
|
2017-09-29 02:52:14 +08:00
|
|
|
Object.CalleeSavedRestored, ObjectIdx))
|
2015-07-25 06:22:50 +08:00
|
|
|
return true;
|
2015-08-18 06:17:42 +08:00
|
|
|
if (Object.LocalOffset)
|
|
|
|
MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
|
2015-08-19 08:13:25 +08:00
|
|
|
return true;
|
2015-07-11 02:13:57 +08:00
|
|
|
}
|
2015-07-25 06:22:50 +08:00
|
|
|
MFI.setCalleeSavedInfo(CSIInfo);
|
|
|
|
if (!CSIInfo.empty())
|
|
|
|
MFI.setCalleeSavedInfoValid(true);
|
2015-08-19 06:26:26 +08:00
|
|
|
|
|
|
|
// Initialize the various stack object references after initializing the
|
|
|
|
// stack objects.
|
|
|
|
if (!YamlMFI.StackProtector.Value.empty()) {
|
|
|
|
SMDiagnostic Error;
|
|
|
|
int FI;
|
2016-07-14 07:27:50 +08:00
|
|
|
if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
|
2015-08-19 06:26:26 +08:00
|
|
|
return error(Error, YamlMFI.StackProtector.SourceRange);
|
|
|
|
MFI.setStackProtectorIndex(FI);
|
|
|
|
}
|
2015-07-25 06:22:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
|
2015-07-25 06:22:50 +08:00
|
|
|
std::vector<CalleeSavedInfo> &CSIInfo,
|
2017-09-29 02:52:14 +08:00
|
|
|
const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
|
2015-07-25 06:22:50 +08:00
|
|
|
if (RegisterSource.Value.empty())
|
|
|
|
return false;
|
|
|
|
unsigned Reg = 0;
|
|
|
|
SMDiagnostic Error;
|
2016-07-14 07:27:50 +08:00
|
|
|
if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
|
2015-07-25 06:22:50 +08:00
|
|
|
return error(Error, RegisterSource.SourceRange);
|
2017-09-29 02:52:14 +08:00
|
|
|
CalleeSavedInfo CSI(Reg, FrameIdx);
|
|
|
|
CSI.setRestored(IsRestored);
|
|
|
|
CSIInfo.push_back(CSI);
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-19 08:13:25 +08:00
|
|
|
/// Verify that given node is of a certain type. Return true on error.
|
|
|
|
template <typename T>
|
|
|
|
static bool typecheckMDNode(T *&Result, MDNode *Node,
|
|
|
|
const yaml::StringValue &Source,
|
|
|
|
StringRef TypeString, MIRParserImpl &Parser) {
|
|
|
|
if (!Node)
|
|
|
|
return false;
|
|
|
|
Result = dyn_cast<T>(Node);
|
|
|
|
if (!Result)
|
|
|
|
return Parser.error(Source.SourceRange.Start,
|
|
|
|
"expected a reference to a '" + TypeString +
|
|
|
|
"' metadata node");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
|
2015-08-19 08:13:25 +08:00
|
|
|
const yaml::MachineStackObject &Object, int FrameIdx) {
|
|
|
|
// Debug information can only be attached to stack objects; Fixed stack
|
|
|
|
// objects aren't supported.
|
|
|
|
assert(FrameIdx >= 0 && "Expected a stack object frame index");
|
|
|
|
MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseMDNode(PFS, Var, Object.DebugVar) ||
|
|
|
|
parseMDNode(PFS, Expr, Object.DebugExpr) ||
|
|
|
|
parseMDNode(PFS, Loc, Object.DebugLoc))
|
2015-08-19 08:13:25 +08:00
|
|
|
return true;
|
|
|
|
if (!Var && !Expr && !Loc)
|
|
|
|
return false;
|
|
|
|
DILocalVariable *DIVar = nullptr;
|
|
|
|
DIExpression *DIExpr = nullptr;
|
|
|
|
DILocation *DILoc = nullptr;
|
|
|
|
if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
|
|
|
|
typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
|
|
|
|
typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
|
|
|
|
return true;
|
2016-12-01 07:48:50 +08:00
|
|
|
PFS.MF.setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
|
2015-08-19 08:13:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
|
2016-07-14 06:23:23 +08:00
|
|
|
MDNode *&Node, const yaml::StringValue &Source) {
|
2015-08-19 08:13:25 +08:00
|
|
|
if (Source.Value.empty())
|
|
|
|
return false;
|
|
|
|
SMDiagnostic Error;
|
2016-07-14 07:27:50 +08:00
|
|
|
if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
|
2015-08-19 08:13:25 +08:00
|
|
|
return error(Error, Source.SourceRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
|
|
|
|
MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
|
|
|
|
DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
|
|
|
|
const MachineFunction &MF = PFS.MF;
|
2015-07-21 04:51:18 +08:00
|
|
|
const auto &M = *MF.getFunction()->getParent();
|
|
|
|
SMDiagnostic Error;
|
|
|
|
for (const auto &YamlConstant : YamlMF.Constants) {
|
2017-08-02 19:09:30 +08:00
|
|
|
if (YamlConstant.IsTargetSpecific)
|
|
|
|
// FIXME: Support target-specific constant pools
|
|
|
|
return error(YamlConstant.Value.SourceRange.Start,
|
|
|
|
"Can't parse target-specific constant pool entries yet");
|
2015-07-21 04:51:18 +08:00
|
|
|
const Constant *Value = dyn_cast_or_null<Constant>(
|
|
|
|
parseConstantValue(YamlConstant.Value.Value, Error, M));
|
|
|
|
if (!Value)
|
|
|
|
return error(Error, YamlConstant.Value.SourceRange);
|
|
|
|
unsigned Alignment =
|
|
|
|
YamlConstant.Alignment
|
|
|
|
? YamlConstant.Alignment
|
|
|
|
: M.getDataLayout().getPrefTypeAlignment(Value->getType());
|
2015-07-31 06:00:17 +08:00
|
|
|
unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
|
|
|
|
if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
|
|
|
|
.second)
|
|
|
|
return error(YamlConstant.ID.SourceRange.Start,
|
|
|
|
Twine("redefinition of constant pool item '%const.") +
|
|
|
|
Twine(YamlConstant.ID.Value) + "'");
|
2015-07-21 04:51:18 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:23:23 +08:00
|
|
|
bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
|
|
|
|
const yaml::MachineJumpTable &YamlJTI) {
|
|
|
|
MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
|
2015-07-16 07:31:07 +08:00
|
|
|
for (const auto &Entry : YamlJTI.Entries) {
|
|
|
|
std::vector<MachineBasicBlock *> Blocks;
|
|
|
|
for (const auto &MBBSource : Entry.Blocks) {
|
|
|
|
MachineBasicBlock *MBB = nullptr;
|
2016-07-14 06:23:23 +08:00
|
|
|
if (parseMBBReference(PFS, MBB, MBBSource.Value))
|
2015-07-30 04:57:11 +08:00
|
|
|
return true;
|
2015-07-16 07:31:07 +08:00
|
|
|
Blocks.push_back(MBB);
|
|
|
|
}
|
2015-07-16 07:38:35 +08:00
|
|
|
unsigned Index = JTI->createJumpTableIndex(Blocks);
|
2015-08-01 07:13:23 +08:00
|
|
|
if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
|
|
|
|
.second)
|
|
|
|
return error(Entry.ID.SourceRange.Start,
|
|
|
|
Twine("redefinition of jump table entry '%jump-table.") +
|
|
|
|
Twine(Entry.ID.Value) + "'");
|
2015-07-16 07:31:07 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:01 +08:00
|
|
|
bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
|
2016-07-14 06:23:23 +08:00
|
|
|
MachineBasicBlock *&MBB,
|
|
|
|
const yaml::StringValue &Source) {
|
2015-07-30 04:57:11 +08:00
|
|
|
SMDiagnostic Error;
|
2016-07-14 07:27:50 +08:00
|
|
|
if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
|
2015-07-30 04:57:11 +08:00
|
|
|
return error(Error, Source.SourceRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-24 06:39:23 +08:00
|
|
|
SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange) {
|
|
|
|
assert(SourceRange.isValid() && "Invalid source range");
|
|
|
|
SMLoc Loc = SourceRange.Start;
|
|
|
|
bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
|
|
|
|
*Loc.getPointer() == '\'';
|
|
|
|
// Translate the location of the error from the location in the MI string to
|
|
|
|
// the corresponding location in the MIR file.
|
|
|
|
Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
|
|
|
|
(HasQuote ? 1 : 0));
|
|
|
|
|
|
|
|
// TODO: Translate any source ranges as well.
|
|
|
|
return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
|
|
|
|
Error.getFixIts());
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:30:11 +08:00
|
|
|
SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange) {
|
2015-05-30 01:05:41 +08:00
|
|
|
assert(SourceRange.isValid());
|
|
|
|
|
|
|
|
// Translate the location of the error from the location in the llvm IR string
|
|
|
|
// to the corresponding location in the MIR file.
|
|
|
|
auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
|
|
|
|
unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
|
|
|
|
unsigned Column = Error.getColumnNo();
|
|
|
|
StringRef LineStr = Error.getLineContents();
|
|
|
|
SMLoc Loc = Error.getLoc();
|
|
|
|
|
|
|
|
// Get the full line and adjust the column number by taking the indentation of
|
|
|
|
// LLVM IR into account.
|
|
|
|
for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
|
|
|
|
L != E; ++L) {
|
|
|
|
if (L.line_number() == Line) {
|
|
|
|
LineStr = *L;
|
|
|
|
Loc = SMLoc::getFromPointer(LineStr.data());
|
|
|
|
auto Indent = LineStr.find(Error.getLineContents());
|
|
|
|
if (Indent != StringRef::npos)
|
|
|
|
Column += Indent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
|
|
|
|
Error.getMessage(), LineStr, Error.getRanges(),
|
|
|
|
Error.getFixIts());
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:23:13 +08:00
|
|
|
void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
|
|
|
|
if (!Names2RegClasses.empty())
|
|
|
|
return;
|
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
|
|
|
|
const auto *RC = TRI->getRegClass(I);
|
|
|
|
Names2RegClasses.insert(
|
|
|
|
std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 00:40:43 +08:00
|
|
|
void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
|
|
|
|
if (!Names2RegBanks.empty())
|
|
|
|
return;
|
|
|
|
const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
|
|
|
|
// If the target does not support GlobalISel, we may not have a
|
|
|
|
// register bank info.
|
|
|
|
if (!RBI)
|
|
|
|
return;
|
|
|
|
for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
|
|
|
|
const auto &RegBank = RBI->getRegBank(I);
|
|
|
|
Names2RegBanks.insert(
|
|
|
|
std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:23:13 +08:00
|
|
|
const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
|
|
|
|
StringRef Name) {
|
|
|
|
auto RegClassInfo = Names2RegClasses.find(Name);
|
|
|
|
if (RegClassInfo == Names2RegClasses.end())
|
|
|
|
return nullptr;
|
|
|
|
return RegClassInfo->getValue();
|
|
|
|
}
|
|
|
|
|
2016-04-09 00:40:43 +08:00
|
|
|
const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
|
|
|
|
StringRef Name) {
|
|
|
|
auto RegBankInfo = Names2RegBanks.find(Name);
|
|
|
|
if (RegBankInfo == Names2RegBanks.end())
|
|
|
|
return nullptr;
|
|
|
|
return RegBankInfo->getValue();
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
|
|
|
|
: Impl(std::move(Impl)) {}
|
|
|
|
|
|
|
|
MIRParser::~MIRParser() {}
|
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
std::unique_ptr<Module> MIRParser::parseIRModule() {
|
|
|
|
return Impl->parseIRModule();
|
|
|
|
}
|
2015-06-16 04:30:22 +08:00
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
|
|
|
|
return Impl->parseMachineFunctions(M, MMI);
|
2015-06-16 04:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
|
|
|
|
SMDiagnostic &Error,
|
|
|
|
LLVMContext &Context) {
|
2017-06-07 04:06:57 +08:00
|
|
|
auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
|
2015-05-28 02:02:19 +08:00
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
|
|
|
Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + EC.message());
|
2015-06-16 04:30:22 +08:00
|
|
|
return nullptr;
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
2015-06-16 04:30:22 +08:00
|
|
|
return createMIRParser(std::move(FileOrErr.get()), Context);
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
std::unique_ptr<MIRParser>
|
|
|
|
llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
|
|
|
|
LLVMContext &Context) {
|
2016-09-17 13:41:02 +08:00
|
|
|
auto Filename = Contents->getBufferIdentifier();
|
2016-09-17 13:33:58 +08:00
|
|
|
if (Context.shouldDiscardValueNames()) {
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
|
|
DS_Error,
|
|
|
|
SMDiagnostic(
|
|
|
|
Filename, SourceMgr::DK_Error,
|
|
|
|
"Can't read MIR with a Context that discards named Values")));
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-16 04:30:22 +08:00
|
|
|
return llvm::make_unique<MIRParser>(
|
|
|
|
llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|