diff --git a/llvm/include/llvm/CodeGen/MIRYamlMapping.h b/llvm/include/llvm/CodeGen/MIRYamlMapping.h --- a/llvm/include/llvm/CodeGen/MIRYamlMapping.h +++ b/llvm/include/llvm/CodeGen/MIRYamlMapping.h @@ -142,6 +142,23 @@ } }; +template <> struct ScalarTraits { + static void output(const MaybeAlign &Alignment, void *, + llvm::raw_ostream &out) { + out << uint64_t(Alignment ? Alignment->value() : 0U); + } + static StringRef input(StringRef Scalar, void *, MaybeAlign &Alignment) { + unsigned long long n; + if (getAsUnsignedInteger(Scalar, 10, n)) + return "invalid number"; + if (n > 0 && !isPowerOf2_64(n)) + return "must be 0 or a power of two"; + Alignment = MaybeAlign(n); + return StringRef(); + } + static QuotingType mustQuote(StringRef) { return QuotingType::None; } +}; + } // end namespace yaml } // end namespace llvm @@ -212,7 +229,7 @@ ObjectType Type = DefaultType; int64_t Offset = 0; uint64_t Size = 0; - unsigned Alignment = 0; + MaybeAlign Alignment = None; TargetStackID::Value StackID; StringValue CalleeSavedRegister; bool CalleeSavedRestored = true; @@ -252,7 +269,7 @@ YamlIO.mapOptional("offset", Object.Offset, (int64_t)0); if (Object.Type != MachineStackObject::VariableSized) YamlIO.mapRequired("size", Object.Size); - YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0); + YamlIO.mapOptional("alignment", Object.Alignment, None); YamlIO.mapOptional("stack-id", Object.StackID, TargetStackID::Default); YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister, StringValue()); // Don't print it out when it's empty. @@ -278,7 +295,7 @@ ObjectType Type = DefaultType; int64_t Offset = 0; uint64_t Size = 0; - unsigned Alignment = 0; + MaybeAlign Alignment = None; TargetStackID::Value StackID; bool IsImmutable = false; bool IsAliased = false; @@ -327,7 +344,7 @@ FixedMachineStackObject::DefaultType); // Don't print the default type. YamlIO.mapOptional("offset", Object.Offset, (int64_t)0); YamlIO.mapOptional("size", Object.Size, (uint64_t)0); - YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0); + YamlIO.mapOptional("alignment", Object.Alignment, None); YamlIO.mapOptional("stack-id", Object.StackID, TargetStackID::Default); if (Object.Type != FixedMachineStackObject::SpillSlot) { YamlIO.mapOptional("isImmutable", Object.IsImmutable, false); @@ -411,7 +428,7 @@ struct MachineConstantPoolValue { UnsignedValue ID; StringValue Value; - unsigned Alignment = 0; + MaybeAlign Alignment = None; bool IsTargetSpecific = false; bool operator==(const MachineConstantPoolValue &Other) const { @@ -425,7 +442,7 @@ static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) { YamlIO.mapRequired("id", Constant.ID); YamlIO.mapOptional("value", Constant.Value, StringValue()); - YamlIO.mapOptional("alignment", Constant.Alignment, (unsigned)0); + YamlIO.mapOptional("alignment", Constant.Alignment, None); YamlIO.mapOptional("isTargetSpecific", Constant.IsTargetSpecific, false); } }; @@ -571,7 +588,7 @@ struct MachineFunction { StringRef Name; - unsigned Alignment = 0; + MaybeAlign Alignment = None; bool ExposesReturnsTwice = false; // GISel MachineFunctionProperties. bool Legalized = false; @@ -599,7 +616,7 @@ template <> struct MappingTraits { static void mapping(IO &YamlIO, MachineFunction &MF) { YamlIO.mapRequired("name", MF.Name); - YamlIO.mapOptional("alignment", MF.Alignment, (unsigned)0); + YamlIO.mapOptional("alignment", MF.Alignment, None); YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice, false); YamlIO.mapOptional("legalized", MF.Legalized, false); YamlIO.mapOptional("regBankSelected", MF.RegBankSelected, false); diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp --- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp @@ -401,8 +401,7 @@ Target.reset(new PerTargetMIParsingState(MF.getSubtarget())); } - if (YamlMF.Alignment) - MF.setAlignment(Align(YamlMF.Alignment)); + MF.setAlignment(YamlMF.Alignment.valueOrOne()); MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); MF.setHasWinCFI(YamlMF.HasWinCFI); @@ -691,7 +690,7 @@ return error(Object.ID.SourceRange.Start, Twine("StackID is not supported by target")); MFI.setStackID(ObjectIdx, Object.StackID); - MFI.setObjectAlignment(ObjectIdx, Object.Alignment); + MFI.setObjectAlignment(ObjectIdx, Object.Alignment.valueOrOne()); if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx)) .second) @@ -723,10 +722,11 @@ return error(Object.ID.SourceRange.Start, Twine("StackID is not supported by target")); if (Object.Type == yaml::MachineStackObject::VariableSized) - ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca); + ObjectIdx = + MFI.CreateVariableSizedObject(Object.Alignment.valueOrOne(), Alloca); else ObjectIdx = MFI.CreateStackObject( - Object.Size, Object.Alignment, + Object.Size, Object.Alignment.valueOrOne(), Object.Type == yaml::MachineStackObject::SpillSlot, Alloca, Object.StackID); MFI.setObjectOffset(ObjectIdx, Object.Offset); @@ -838,11 +838,11 @@ 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()); - unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment); + const Align PrefTypeAlign = + M.getDataLayout().getPrefTypeAlign(Value->getType()); + const Align Alignment = YamlConstant.Alignment.getValueOr(PrefTypeAlign); + unsigned Index = + ConstantPool.getConstantPoolIndex(Value, Alignment.value()); if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index)) .second) return error(YamlConstant.ID.SourceRange.Start, diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp --- a/llvm/lib/CodeGen/MIRPrinter.cpp +++ b/llvm/lib/CodeGen/MIRPrinter.cpp @@ -198,7 +198,7 @@ yaml::MachineFunction YamlMF; YamlMF.Name = MF.getName(); - YamlMF.Alignment = MF.getAlignment().value(); + YamlMF.Alignment = MF.getAlignment(); YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); YamlMF.HasWinCFI = MF.hasWinCFI(); @@ -373,7 +373,7 @@ : yaml::FixedMachineStackObject::DefaultType; YamlObject.Offset = MFI.getObjectOffset(I); YamlObject.Size = MFI.getObjectSize(I); - YamlObject.Alignment = MFI.getObjectAlignment(I); + YamlObject.Alignment = MFI.getObjectAlign(I); YamlObject.StackID = (TargetStackID::Value)MFI.getStackID(I); YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I); YamlObject.IsAliased = MFI.isAliasedObjectIndex(I); @@ -400,7 +400,7 @@ : yaml::MachineStackObject::DefaultType; YamlObject.Offset = MFI.getObjectOffset(I); YamlObject.Size = MFI.getObjectSize(I); - YamlObject.Alignment = MFI.getObjectAlignment(I); + YamlObject.Alignment = MFI.getObjectAlign(I); YamlObject.StackID = (TargetStackID::Value)MFI.getStackID(I); YMF.StackObjects.push_back(YamlObject); @@ -514,7 +514,7 @@ yaml::MachineConstantPoolValue YamlConstant; YamlConstant.ID = ID++; YamlConstant.Value = StrOS.str(); - YamlConstant.Alignment = Constant.getAlignment(); + YamlConstant.Alignment = MaybeAlign(Constant.getAlignment()); YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry(); MF.Constants.push_back(YamlConstant);