From c7fb36d42d489206bfd1a2cc7dd55b02e5e3dc9a Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 5 Dec 2016 05:21:18 +0000 Subject: TableGen: Factor out STRCONCAT constructor, add shortcut. Introduce new constructor for STRCONCAT binop with a shortcut that immediately concatenates if the two arguments are StringInits. Makes the QualifyName code more readable and tablegen 2-3% faster. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288639 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/TableGen/Record.cpp | 51 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'lib/TableGen') diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 22773c22982..8c2f5a1dcd3 100644 --- a/lib/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -813,6 +813,13 @@ void BinOpInit::Profile(FoldingSetNodeID &ID) const { ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType()); } +static StringInit *ConcatStringInits(const StringInit *I0, + const StringInit *I1) { + SmallString<80> Concat(I0->getValue()); + Concat.append(I1->getValue()); + return StringInit::get(Concat); +} + Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { switch (getOpcode()) { case CONCAT: { @@ -852,12 +859,8 @@ Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { case STRCONCAT: { StringInit *LHSs = dyn_cast(LHS); StringInit *RHSs = dyn_cast(RHS); - if (LHSs && RHSs) { - // STRCONCAT is common; Use a SmallString to avoid most heap allocations. - SmallString<80> Concat(LHSs->getValue()); - Concat.append(RHSs->getValue()); - return StringInit::get(Concat); - } + if (LHSs && RHSs) + return ConcatStringInits(LHSs, RHSs); break; } case EQ: { @@ -1940,31 +1943,27 @@ RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const { return Defs; } +static Init *GetStrConcat(Init *I0, Init *I1) { + // Shortcut for the common case of concatenating two strings. + if (const StringInit *I0s = dyn_cast(I0)) + if (const StringInit *I1s = dyn_cast(I1)) + return ConcatStringInits(I0s, I1s); + return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get()); +} + Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, StringRef Scoper) { - RecTy *Type = cast(Name)->getType(); - - BinOpInit *NewName = - BinOpInit::get(BinOpInit::STRCONCAT, - BinOpInit::get(BinOpInit::STRCONCAT, - CurRec.getNameInit(), - StringInit::get(Scoper), - Type)->Fold(&CurRec, CurMultiClass), - Name, - Type); - + Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper)); + NewName = GetStrConcat(NewName, Name); if (CurMultiClass && Scoper != "::") { - NewName = - BinOpInit::get(BinOpInit::STRCONCAT, - BinOpInit::get(BinOpInit::STRCONCAT, - CurMultiClass->Rec.getNameInit(), - StringInit::get("::"), - Type)->Fold(&CurRec, CurMultiClass), - NewName->Fold(&CurRec, CurMultiClass), - Type); + Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(), + StringInit::get("::")); + NewName = GetStrConcat(Prefix, NewName); } - return NewName->Fold(&CurRec, CurMultiClass); + if (BinOpInit *BinOp = dyn_cast(NewName)) + NewName = BinOp->Fold(&CurRec, CurMultiClass); + return NewName; } Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, -- cgit v1.2.3