llvm.org GIT mirror llvm / 77450cd
[Alignment][NFC] Use MaybeAlign in AttrBuilder Summary: This is patch is part of a series to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: courbet Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69300 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375496 91177308-0d34-0410-b5e6-96231b3b80d8 Guillaume Chatelet 1 year, 6 days ago
4 changed file(s) with 39 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
773773 bool hasAlignmentAttr() const;
774774
775775 /// Retrieve the alignment attribute, if it exists.
776 uint64_t getAlignment() const { return Alignment ? Alignment->value() : 0; }
776 MaybeAlign getAlignment() const { return Alignment; }
777777
778778 /// Retrieve the stack alignment attribute, if it exists.
779 uint64_t getStackAlignment() const {
780 return StackAlignment ? StackAlignment->value() : 0;
781 }
779 MaybeAlign getStackAlignment() const { return StackAlignment; }
782780
783781 /// Retrieve the number of dereferenceable bytes, if the
784782 /// dereferenceable attribute exists (zero is returned otherwise).
139139 // If the alignment was parsed as an attribute, move to the alignment
140140 // field.
141141 if (FnAttrs.hasAlignmentAttr()) {
142 Fn->setAlignment(MaybeAlign(FnAttrs.getAlignment()));
142 Fn->setAlignment(FnAttrs.getAlignment());
143143 FnAttrs.removeAttribute(Attribute::Alignment);
144144 }
145145
11211121 if (ParseToken(lltok::StringConstant, "expected partition string"))
11221122 return true;
11231123 } else if (Lex.getKind() == lltok::kw_align) {
1124 unsigned Alignment;
1124 MaybeAlign Alignment;
11251125 if (ParseOptionalAlignment(Alignment)) return true;
1126 GV->setAlignment(MaybeAlign(Alignment));
1126 GV->setAlignment(Alignment);
11271127 } else if (Lex.getKind() == lltok::MetadataVar) {
11281128 if (ParseGlobalObjectMetadataAttachment(*GV))
11291129 return true;
12281228 // As a hack, we allow function alignment to be initially parsed as an
12291229 // attribute on a function declaration/definition or added to an attribute
12301230 // group and later moved to the alignment field.
1231 unsigned Alignment;
1231 MaybeAlign Alignment;
12321232 if (inAttrGrp) {
12331233 Lex.Lex();
1234 if (ParseToken(lltok::equal, "expected '=' here") ||
1235 ParseUInt32(Alignment))
1234 uint32_t Value = 0;
1235 if (ParseToken(lltok::equal, "expected '=' here") || ParseUInt32(Value))
12361236 return true;
1237 Alignment = Align(Value);
12371238 } else {
12381239 if (ParseOptionalAlignment(Alignment))
12391240 return true;
16021603 continue;
16031604 }
16041605 case lltok::kw_align: {
1605 unsigned Alignment;
1606 MaybeAlign Alignment;
16061607 if (ParseOptionalAlignment(Alignment))
16071608 return true;
16081609 B.addAlignmentAttr(Alignment);
17191720 continue;
17201721 }
17211722 case lltok::kw_align: {
1722 unsigned Alignment;
1723 MaybeAlign Alignment;
17231724 if (ParseOptionalAlignment(Alignment))
17241725 return true;
17251726 B.addAlignmentAttr(Alignment);
20682069 /// ParseOptionalAlignment
20692070 /// ::= /* empty */
20702071 /// ::= 'align' 4
2071 bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
2072 Alignment = 0;
2072 bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment) {
2073 Alignment = None;
20732074 if (!EatIfPresent(lltok::kw_align))
20742075 return false;
20752076 LocTy AlignLoc = Lex.getLoc();
2076 if (ParseUInt32(Alignment)) return true;
2077 if (!isPowerOf2_32(Alignment))
2077 uint32_t Value = 0;
2078 if (ParseUInt32(Value))
2079 return true;
2080 if (!isPowerOf2_32(Value))
20782081 return Error(AlignLoc, "alignment is not a power of two");
2079 if (Alignment > Value::MaximumAlignment)
2082 if (Value > Value::MaximumAlignment)
20802083 return Error(AlignLoc, "huge alignments are not supported yet");
2084 Alignment = Align(Value);
20812085 return false;
20822086 }
20832087
21142118 ///
21152119 /// This returns with AteExtraComma set to true if it ate an excess comma at the
21162120 /// end.
2117 bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
2121 bool LLParser::ParseOptionalCommaAlign(MaybeAlign &Alignment,
21182122 bool &AteExtraComma) {
21192123 AteExtraComma = false;
21202124 while (EatIfPresent(lltok::comma)) {
53695373 LocTy BuiltinLoc;
53705374 std::string Section;
53715375 std::string Partition;
5372 unsigned Alignment;
5376 MaybeAlign Alignment;
53735377 std::string GC;
53745378 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
53755379 unsigned AddrSpace = 0;
68646868 int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
68656869 Value *Size = nullptr;
68666870 LocTy SizeLoc, TyLoc, ASLoc;
6867 unsigned Alignment = 0;
6871 MaybeAlign Alignment;
68686872 unsigned AddrSpace = 0;
68696873 Type *Ty = nullptr;
68706874
69126916 if (Size && !Size->getType()->isIntegerTy())
69136917 return Error(SizeLoc, "element count must have integer type");
69146918
6915 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
6919 AllocaInst *AI =
6920 new AllocaInst(Ty, AddrSpace, Size, Alignment ? Alignment->value() : 0);
69166921 AI->setUsedWithInAlloca(IsInAlloca);
69176922 AI->setSwiftError(IsSwiftError);
69186923 Inst = AI;
69256930 /// 'singlethread'? AtomicOrdering (',' 'align' i32)?
69266931 int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
69276932 Value *Val; LocTy Loc;
6928 unsigned Alignment = 0;
6933 MaybeAlign Alignment;
69296934 bool AteExtraComma = false;
69306935 bool isAtomic = false;
69316936 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
69636968 return Error(ExplicitTypeLoc,
69646969 "explicit pointee type doesn't match operand's pointee type");
69656970
6966 Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
6971 Inst = new LoadInst(Ty, Val, "", isVolatile,
6972 Alignment ? Alignment->value() : 0, Ordering, SSID);
69676973 return AteExtraComma ? InstExtraComma : InstNormal;
69686974 }
69696975
69746980 /// 'singlethread'? AtomicOrdering (',' 'align' i32)?
69756981 int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
69766982 Value *Val, *Ptr; LocTy Loc, PtrLoc;
6977 unsigned Alignment = 0;
6983 MaybeAlign Alignment;
69786984 bool AteExtraComma = false;
69796985 bool isAtomic = false;
69806986 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
70107016 Ordering == AtomicOrdering::AcquireRelease)
70117017 return Error(Loc, "atomic store cannot use Acquire ordering");
70127018
7013 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
7019 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment ? Alignment->value() : 0,
7020 Ordering, SSID);
70147021 return AteExtraComma ? InstExtraComma : InstNormal;
70157022 }
70167023
280280 void ParseOptionalVisibility(unsigned &Res);
281281 void ParseOptionalDLLStorageClass(unsigned &Res);
282282 bool ParseOptionalCallingConv(unsigned &CC);
283 bool ParseOptionalAlignment(unsigned &Alignment);
283 bool ParseOptionalAlignment(MaybeAlign &Alignment);
284284 bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
285285 bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
286286 AtomicOrdering &Ordering);
287287 bool ParseScope(SyncScope::ID &SSID);
288288 bool ParseOrdering(AtomicOrdering &Ordering);
289289 bool ParseOptionalStackAlignment(unsigned &Alignment);
290 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
290 bool ParseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma);
291291 bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
292292 bool &AteExtraComma);
293293 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
778778 Attr = Attribute::getWithByValType(C, B.getByValType());
779779 break;
780780 case Attribute::Alignment:
781 Attr = Attribute::getWithAlignment(C, Align(B.getAlignment()));
781 assert(B.getAlignment() && "Alignment must be set");
782 Attr = Attribute::getWithAlignment(C, *B.getAlignment());
782783 break;
783784 case Attribute::StackAlignment:
784 Attr = Attribute::getWithStackAlignment(C, Align(B.getStackAlignment()));
785 assert(B.getStackAlignment() && "StackAlignment must be set");
786 Attr = Attribute::getWithStackAlignment(C, *B.getStackAlignment());
785787 break;
786788 case Attribute::Dereferenceable:
787789 Attr = Attribute::getWithDereferenceableBytes(
11611163 // FIXME it is not obvious how this should work for alignment. For now, say
11621164 // we can't change a known alignment.
11631165 const MaybeAlign OldAlign = getAttributes(Index).getAlignment();
1164 unsigned NewAlign = B.getAlignment();
1166 const MaybeAlign NewAlign = B.getAlignment();
11651167 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
11661168 "Attempt to change alignment!");
11671169 #endif
14591461 Attrs[Kind] = true;
14601462
14611463 if (Kind == Attribute::Alignment)
1462 Alignment = MaybeAlign(Attr.getAlignment());
1464 Alignment = Attr.getAlignment();
14631465 else if (Kind == Attribute::StackAlignment)
1464 StackAlignment = MaybeAlign(Attr.getStackAlignment());
1466 StackAlignment = Attr.getStackAlignment();
14651467 else if (Kind == Attribute::ByVal)
14661468 ByValType = Attr.getValueAsType();
14671469 else if (Kind == Attribute::Dereferenceable)