From: David Greene Date: Fri, 29 Jul 2011 19:07:07 +0000 (+0000) Subject: [AVX] Create Inits Via Factory Method X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=dcd35c797d458d8b1dbc36cf7f1504166d5b2f16 [AVX] Create Inits Via Factory Method Replace uses of new *Init with *Init::get. This hides the allocation implementation so that we can unique Inits in various ways. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136486 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/utils/TableGen/CodeEmitterGen.cpp b/utils/TableGen/CodeEmitterGen.cpp index c11e4d0beb1..a95f4b8f3f1 100644 --- a/utils/TableGen/CodeEmitterGen.cpp +++ b/utils/TableGen/CodeEmitterGen.cpp @@ -56,7 +56,7 @@ void CodeEmitterGen::reverseBits(std::vector &Insts) { NewBits[middle] = BI->getBit(middle); } - BitsInit *NewBI = new BitsInit(ArrayRef(NewBits)); + const BitsInit *NewBI = BitsInit::get(NewBits); // Update the bits in reversed order so that emitInstrOpBits will get the // correct endianness. diff --git a/utils/TableGen/CodeGenDAGPatterns.cpp b/utils/TableGen/CodeGenDAGPatterns.cpp index 2a75849d67e..a1cf63f17a8 100644 --- a/utils/TableGen/CodeGenDAGPatterns.cpp +++ b/utils/TableGen/CodeGenDAGPatterns.cpp @@ -1747,9 +1747,10 @@ TreePatternNode *TreePattern::ParseTreePattern(const Init *TheInit, StringRef Op // TreePatternNode of its own. For example: /// (foo GPR, imm) -> (foo GPR, (imm)) if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) - return ParseTreePattern(new DagInit(DI, "", - std::vector >()), - OpName); + return ParseTreePattern( + DagInit::get(DI, "", + std::vector >()), + OpName); // Input argument? TreePatternNode *Res = new TreePatternNode(DI, 1); @@ -1860,7 +1861,7 @@ TreePatternNode *TreePattern::ParseTreePattern(const Init *TheInit, StringRef Op else // Otherwise, no chain. Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); - TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID), 1); + TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1); Children.insert(Children.begin(), IIDNode); } @@ -2192,7 +2193,7 @@ void CodeGenDAGPatterns::ParseDefaultOperands() { for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) Ops.push_back(std::make_pair(DefaultInfo->getArg(op), DefaultInfo->getArgName(op))); - const DagInit *DI = new DagInit(SomeSDNode, "", Ops); + const DagInit *DI = DagInit::get(SomeSDNode, "", Ops); // Create a TreePattern to parse this. TreePattern P(DefaultOps[iter][i], DI, false, *this); diff --git a/utils/TableGen/CodeGenRegisters.cpp b/utils/TableGen/CodeGenRegisters.cpp index 298009edc80..8f4ebfb26ed 100644 --- a/utils/TableGen/CodeGenRegisters.cpp +++ b/utils/TableGen/CodeGenRegisters.cpp @@ -183,7 +183,7 @@ struct TupleExpander : SetTheory::Expander { // Precompute some types. Record *RegisterCl = Def->getRecords().getClass("Register"); RecTy *RegisterRecTy = RecordRecTy::get(RegisterCl); - const StringInit *BlankName = new StringInit(""); + const StringInit *BlankName = StringInit::get(""); // Zip them up. for (unsigned n = 0; n != Length; ++n) { @@ -216,7 +216,7 @@ struct TupleExpander : SetTheory::Expander { // Replace the sub-register list with Tuple. if (RV.getName() == "SubRegs") - RV.setValue(new ListInit(Tuple, RegisterRecTy)); + RV.setValue(ListInit::get(Tuple, RegisterRecTy)); // Provide a blank AsmName. MC hacks are required anyway. if (RV.getName() == "AsmName") @@ -224,7 +224,7 @@ struct TupleExpander : SetTheory::Expander { // CostPerUse is aggregated from all Tuple members. if (RV.getName() == "CostPerUse") - RV.setValue(new IntInit(CostPerUse)); + RV.setValue(IntInit::get(CostPerUse)); // Copy fields from the RegisterTuples def. if (RV.getName() == "SubRegIndices" || diff --git a/utils/TableGen/Record.cpp b/utils/TableGen/Record.cpp index e2f505e17eb..17fe2fad6e7 100644 --- a/utils/TableGen/Record.cpp +++ b/utils/TableGen/Record.cpp @@ -98,7 +98,7 @@ const Init *BitRecTy::convertValue(const IntInit *II) { int64_t Val = II->getValue(); if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit! - return new BitInit(Val != 0); + return BitInit::get(Val != 0); } const Init *BitRecTy::convertValue(const TypedInit *VI) { @@ -125,14 +125,14 @@ const Init *BitsRecTy::convertValue(const UnsetInit *UI) { SmallVector NewBits(Size); for (unsigned i = 0; i != Size; ++i) - NewBits[i] = new UnsetInit(); + NewBits[i] = UnsetInit::get(); - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } const Init *BitsRecTy::convertValue(const BitInit *UI) { if (Size != 1) return 0; // Can only convert single bit. - return new BitsInit(ArrayRef(UI)); + return BitsInit::get(UI); } /// canFitInBitfield - Return true if the number of bits is large enough to hold @@ -155,9 +155,9 @@ const Init *BitsRecTy::convertValue(const IntInit *II) { SmallVector NewBits(Size); for (unsigned i = 0; i != Size; ++i) - NewBits[i] = new BitInit(Value & (1LL << i)); + NewBits[i] = BitInit::get(Value & (1LL << i)); - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } const Init *BitsRecTy::convertValue(const BitsInit *BI) { @@ -173,12 +173,12 @@ const Init *BitsRecTy::convertValue(const TypedInit *VI) { SmallVector NewBits(Size); for (unsigned i = 0; i != Size; ++i) - NewBits[i] = new VarBitInit(VI, i); - return new BitsInit(ArrayRef(NewBits)); + NewBits[i] = VarBitInit::get(VI, i); + return BitsInit::get(NewBits); } if (Size == 1 && dynamic_cast(VI->getType())) - return new BitsInit(ArrayRef(VI)); + return BitsInit::get(VI); if (const TernOpInit *Tern = dynamic_cast(VI)) { if (Tern->getOpcode() == TernOpInit::IF) { @@ -198,12 +198,12 @@ const Init *BitsRecTy::convertValue(const TypedInit *VI) { for (unsigned i = 0; i != Size; ++i) NewBits[i] = - new TernOpInit(TernOpInit::IF, LHS, - new IntInit((MHSVal & (1LL << i)) ? 1 : 0), - new IntInit((RHSVal & (1LL << i)) ? 1 : 0), - VI->getType()); + TernOpInit::get(TernOpInit::IF, LHS, + IntInit::get((MHSVal & (1LL << i)) ? 1 : 0), + IntInit::get((RHSVal & (1LL << i)) ? 1 : 0), + VI->getType()); - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } } else { const BitsInit *MHSbs = dynamic_cast(MHS); @@ -213,12 +213,12 @@ const Init *BitsRecTy::convertValue(const TypedInit *VI) { SmallVector NewBits(Size); for (unsigned i = 0; i != Size; ++i) - NewBits[i] = new TernOpInit(TernOpInit::IF, LHS, - MHSbs->getBit(i), - RHSbs->getBit(i), - VI->getType()); + NewBits[i] = TernOpInit::get(TernOpInit::IF, LHS, + MHSbs->getBit(i), + RHSbs->getBit(i), + VI->getType()); - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } } } @@ -228,7 +228,7 @@ const Init *BitsRecTy::convertValue(const TypedInit *VI) { } const Init *IntRecTy::convertValue(const BitInit *BI) { - return new IntInit(BI->getValue()); + return IntInit::get(BI->getValue()); } const Init *IntRecTy::convertValue(const BitsInit *BI) { @@ -239,7 +239,7 @@ const Init *IntRecTy::convertValue(const BitsInit *BI) { } else { return 0; } - return new IntInit(Result); + return IntInit::get(Result); } const Init *IntRecTy::convertValue(const TypedInit *TI) { @@ -253,7 +253,7 @@ const Init *StringRecTy::convertValue(const UnOpInit *BO) { const Init *L = BO->getOperand()->convertInitializerTo(this); if (L == 0) return 0; if (L != BO->getOperand()) - return new UnOpInit(UnOpInit::CAST, L, new StringRecTy); + return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy); return BO; } @@ -266,7 +266,7 @@ const Init *StringRecTy::convertValue(const BinOpInit *BO) { const Init *R = BO->getRHS()->convertInitializerTo(this); if (L == 0 || R == 0) return 0; if (L != BO->getLHS() || R != BO->getRHS()) - return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy); + return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy); return BO; } @@ -300,7 +300,7 @@ const Init *ListRecTy::convertValue(const ListInit *LI) { return 0; } - return new ListInit(Elements, this); + return ListInit::get(Elements, this); } const Init *ListRecTy::convertValue(const TypedInit *TI) { @@ -328,7 +328,7 @@ const Init *DagRecTy::convertValue(const UnOpInit *BO) { const Init *L = BO->getOperand()->convertInitializerTo(this); if (L == 0) return 0; if (L != BO->getOperand()) - return new UnOpInit(UnOpInit::CAST, L, new DagRecTy); + return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy); return BO; } return 0; @@ -340,7 +340,7 @@ const Init *DagRecTy::convertValue(const BinOpInit *BO) { const Init *R = BO->getRHS()->convertInitializerTo(this); if (L == 0 || R == 0) return 0; if (L != BO->getLHS() || R != BO->getRHS()) - return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy); + return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy); return BO; } return 0; @@ -442,6 +442,18 @@ RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { void Init::dump() const { return print(errs()); } +const UnsetInit *UnsetInit::get() { + return new UnsetInit; +} + +const BitInit *BitInit::get(bool V) { + return new BitInit(V); +} + +const BitsInit *BitsInit::get(ArrayRef Range) { + return new BitsInit(Range); +} + const Init * BitsInit::convertInitializerBitRange(const std::vector &Bits) const { SmallVector NewBits(Bits.size()); @@ -451,7 +463,7 @@ BitsInit::convertInitializerBitRange(const std::vector &Bits) const { return 0; NewBits[i] = getBit(Bits[i]); } - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } std::string BitsInit::getAsString() const { @@ -486,11 +498,15 @@ const Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { } if (Changed) - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); return this; } +const IntInit *IntInit::get(int64_t V) { + return new IntInit(V); +} + std::string IntInit::getAsString() const { return itostr(Value); } @@ -503,9 +519,21 @@ IntInit::convertInitializerBitRange(const std::vector &Bits) const { if (Bits[i] >= 64) return 0; - NewBits[i] = new BitInit(Value & (INT64_C(1) << Bits[i])); + NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); } - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); +} + +const StringInit *StringInit::get(const std::string &V) { + return new StringInit(V); +} + +const CodeInit *CodeInit::get(const std::string &V) { + return new CodeInit(V); +} + +const ListInit *ListInit::get(ArrayRef Range, RecTy *EltTy) { + return new ListInit(Range, EltTy); } const Init * @@ -516,7 +544,7 @@ ListInit::convertInitListSlice(const std::vector &Elements) const { return 0; Vals.push_back(getElement(Elements[i])); } - return new ListInit(Vals, getType()); + return ListInit::get(Vals, getType()); } Record *ListInit::getElementAsRecord(unsigned i) const { @@ -544,7 +572,7 @@ const Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { } if (Changed) - return new ListInit(Resolved, getType()); + return ListInit::get(Resolved, getType()); return this; } @@ -599,6 +627,10 @@ const Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, return 0; } +const UnOpInit *UnOpInit::get(UnaryOp opc, const Init *lhs, RecTy *Type) { + return new UnOpInit(opc, lhs, Type); +} + const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { switch (getOpcode()) { default: assert(0 && "Unknown unop"); @@ -611,7 +643,7 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { const DefInit *LHSd = dynamic_cast(LHS); if (LHSd) { - return new StringInit(LHSd->getDef()->getName()); + return StringInit::get(LHSd->getDef()->getName()); } } else { const StringInit *LHSs = dynamic_cast(LHS); @@ -623,7 +655,7 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { if (const RecordVal *RV = CurRec->getValue(Name)) { if (RV->getType() != getType()) throw "type mismatch in cast"; - return new VarInit(Name, RV->getType()); + return VarInit::get(Name, RV->getType()); } std::string TemplateArgName = CurRec->getName()+":"+Name; @@ -634,7 +666,7 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { if (RV->getType() != getType()) throw "type mismatch in cast"; - return new VarInit(TemplateArgName, RV->getType()); + return VarInit::get(TemplateArgName, RV->getType()); } } @@ -647,7 +679,7 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { if (RV->getType() != getType()) throw "type mismatch in cast"; - return new VarInit(MCName, RV->getType()); + return VarInit::get(MCName, RV->getType()); } } @@ -677,15 +709,13 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { assert(0 && "Empty list in cdr"); return 0; } - ListInit::const_iterator begin = LHSl->begin()+1; - ListInit::const_iterator end = LHSl->end(); - // We can't pass these iterators directly to ArrayRef because - // they are not convertible to Init **. Fortunately, - // RandomAccessIterator::operator * is guaranteed to return an - // lvalue. - ListInit *Result = - new ListInit(ArrayRef(&*begin, end - begin), - LHSl->getType()); + // Note the +1. We can't just pass the result of getValues() + // directly. + ArrayRef::iterator begin = LHSl->getValues().begin()+1; + ArrayRef::iterator end = LHSl->getValues().end(); + const ListInit *Result = + ListInit::get(ArrayRef(begin, end - begin), + LHSl->getType()); return Result; } break; @@ -694,17 +724,17 @@ const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { const ListInit *LHSl = dynamic_cast(LHS); if (LHSl) { if (LHSl->getSize() == 0) { - return new IntInit(1); + return IntInit::get(1); } else { - return new IntInit(0); + return IntInit::get(0); } } const StringInit *LHSs = dynamic_cast(LHS); if (LHSs) { if (LHSs->getValue().empty()) { - return new IntInit(1); + return IntInit::get(1); } else { - return new IntInit(0); + return IntInit::get(0); } } @@ -718,7 +748,7 @@ const Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { const Init *lhs = LHS->resolveReferences(R, RV); if (LHS != lhs) - return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0); + return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0); return Fold(&R, 0); } @@ -733,6 +763,11 @@ std::string UnOpInit::getAsString() const { return Result + "(" + LHS->getAsString() + ")"; } +const BinOpInit *BinOpInit::get(BinaryOp opc, const Init *lhs, + const Init *rhs, RecTy *Type) { + return new BinOpInit(opc, lhs, rhs, Type); +} + const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { switch (getOpcode()) { default: assert(0 && "Unknown binop"); @@ -754,7 +789,7 @@ const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { Args.push_back(RHSs->getArg(i)); ArgNames.push_back(RHSs->getArgName(i)); } - return new DagInit(LHSs->getOperator(), "", Args, ArgNames); + return DagInit::get(LHSs->getOperator(), "", Args, ArgNames); } break; } @@ -762,7 +797,7 @@ const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { const StringInit *LHSs = dynamic_cast(LHS); const StringInit *RHSs = dynamic_cast(RHS); if (LHSs && RHSs) - return new StringInit(LHSs->getValue() + RHSs->getValue()); + return StringInit::get(LHSs->getValue() + RHSs->getValue()); break; } case EQ: { @@ -774,14 +809,14 @@ const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { dynamic_cast(RHS->convertInitializerTo(IntRecTy::get())); if (L && R) - return new IntInit(L->getValue() == R->getValue()); + return IntInit::get(L->getValue() == R->getValue()); const StringInit *LHSs = dynamic_cast(LHS); const StringInit *RHSs = dynamic_cast(RHS); // Make sure we've resolved if (LHSs && RHSs) - return new IntInit(LHSs->getValue() == RHSs->getValue()); + return IntInit::get(LHSs->getValue() == RHSs->getValue()); break; } @@ -799,7 +834,7 @@ const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { case SRA: Result = LHSv >> RHSv; break; case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; } - return new IntInit(Result); + return IntInit::get(Result); } break; } @@ -812,7 +847,7 @@ const Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { const Init *rhs = RHS->resolveReferences(R, RV); if (LHS != lhs || RHS != rhs) - return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0); + return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0); return Fold(&R, 0); } @@ -829,6 +864,12 @@ std::string BinOpInit::getAsString() const { return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; } +const TernOpInit *TernOpInit::get(TernaryOp opc, const Init *lhs, + const Init *mhs, const Init *rhs, + RecTy *Type) { + return new TernOpInit(opc, lhs, mhs, rhs, Type); +} + static const Init *ForeachHelper(const Init *LHS, const Init *MHS, const Init *RHS, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass); @@ -873,10 +914,9 @@ static const Init *EvaluateOperation(const OpInit *RHSo, const Init *LHS, // Now run the operator and use its result as the new leaf const OpInit *NewOp = RHSo->clone(NewOperands); const Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); - if (NewVal != NewOp) { - delete NewOp; + if (NewVal != NewOp) return NewVal; - } + return 0; } @@ -929,7 +969,7 @@ static const Init *ForeachHelper(const Init *LHS, const Init *MHS, args.push_back(std::make_pair(Arg, ArgName)); } - return new DagInit(Val, "", args); + return DagInit::get(Val, "", args); } if (MHSl) { std::vector NewOperands; @@ -953,12 +993,10 @@ static const Init *ForeachHelper(const Init *LHS, const Init *MHS, // Now run the operator and use its result as the new list item const OpInit *NewOp = RHSo->clone(NewOperands); const Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); - if (NewItem != NewOp) { + if (NewItem != NewOp) *li = NewItem; - delete NewOp; - } } - return new ListInit(NewList, MHSl->getType()); + return ListInit::get(NewList, MHSl->getType()); } } return 0; @@ -995,7 +1033,7 @@ const Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { if (LHSv->getAsString() == RHSv->getAsString()) { Val = MHSv->getName(); } - return new VarInit(Val, getType()); + return VarInit::get(Val, getType()); } if (RHSs) { std::string Val = RHSs->getValue(); @@ -1010,7 +1048,7 @@ const Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { idx = found + MHSs->getValue().size(); } while (found != std::string::npos); - return new StringInit(Val); + return StringInit::get(Val); } } break; @@ -1055,12 +1093,12 @@ const Init *TernOpInit::resolveReferences(Record &R, // Short-circuit if (Value->getValue()) { const Init *mhs = MHS->resolveReferences(R, RV); - return (new TernOpInit(getOpcode(), lhs, mhs, - RHS, getType()))->Fold(&R, 0); + return (TernOpInit::get(getOpcode(), lhs, mhs, + RHS, getType()))->Fold(&R, 0); } else { const Init *rhs = RHS->resolveReferences(R, RV); - return (new TernOpInit(getOpcode(), lhs, MHS, - rhs, getType()))->Fold(&R, 0); + return (TernOpInit::get(getOpcode(), lhs, MHS, + rhs, getType()))->Fold(&R, 0); } } } @@ -1069,7 +1107,8 @@ const Init *TernOpInit::resolveReferences(Record &R, const Init *rhs = RHS->resolveReferences(R, RV); if (LHS != lhs || MHS != mhs || RHS != rhs) - return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0); + return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, + getType()))->Fold(&R, 0); return Fold(&R, 0); } @@ -1106,9 +1145,9 @@ TypedInit::convertInitializerBitRange(const std::vector &Bits) const { if (Bits[i] >= NumBits) return 0; - NewBits[i] = new VarBitInit(this, Bits[i]); + NewBits[i] = VarBitInit::get(this, Bits[i]); } - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } const Init * @@ -1117,16 +1156,20 @@ TypedInit::convertInitListSlice(const std::vector &Elements) const { if (T == 0) return 0; // Cannot subscript a non-list variable. if (Elements.size() == 1) - return new VarListElementInit(this, Elements[0]); + return VarListElementInit::get(this, Elements[0]); std::vector ListInits; ListInits.reserve(Elements.size()); for (unsigned i = 0, e = Elements.size(); i != e; ++i) - ListInits.push_back(new VarListElementInit(this, Elements[i])); - return new ListInit(ListInits, T); + ListInits.push_back(VarListElementInit::get(this, Elements[i])); + return ListInit::get(ListInits, T); } +const VarInit *VarInit::get(const std::string &VN, RecTy *T) { + return new VarInit(VN, T); +} + const Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV, unsigned Bit) const { if (R.isTemplateArg(getName())) return 0; @@ -1160,7 +1203,7 @@ const Init *VarInit::resolveListElementReference(Record &R, if (!LI) { const VarInit *VI = dynamic_cast(RV->getValue()); assert(VI && "Invalid list element!"); - return new VarListElementInit(VI, Elt); + return VarListElementInit::get(VI, Elt); } if (Elt >= LI->getSize()) @@ -1210,6 +1253,10 @@ const Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { return this; } +const VarBitInit *VarBitInit::get(const TypedInit *T, unsigned B) { + return new VarBitInit(T, B); +} + std::string VarBitInit::getAsString() const { return TI->getAsString() + "{" + utostr(Bit) + "}"; } @@ -1221,6 +1268,11 @@ const Init *VarBitInit::resolveReferences(Record &R, return this; } +const VarListElementInit *VarListElementInit::get(const TypedInit *T, + unsigned E) { + return new VarListElementInit(T, E); +} + std::string VarListElementInit::getAsString() const { return TI->getAsString() + "[" + utostr(Element) + "]"; } @@ -1249,7 +1301,7 @@ resolveListElementReference(Record &R, const RecordVal *RV, return 0; } -DefInit *DefInit::get(Record *R) { +const DefInit *DefInit::get(Record *R) { return R->getDefInit(); } @@ -1269,6 +1321,10 @@ std::string DefInit::getAsString() const { return Def->getName(); } +const FieldInit *FieldInit::get(const Init *R, const std::string &FN) { + return new FieldInit(R, FN); +} + const Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV, unsigned Bit) const { if (const Init *BitsVal = Rec->getFieldInit(R, RV, FieldName)) @@ -1309,11 +1365,37 @@ const Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { } if (NewRec != Rec) { - return new FieldInit(NewRec, FieldName); + return FieldInit::get(NewRec, FieldName); } return this; } +const DagInit * +DagInit::get(const Init *V, const std::string &VN, + const std::vector > &args) { + typedef std::pair PairType; + + std::vector Args; + std::vector Names; + + for (std::vector::const_iterator i = args.begin(), + iend = args.end(); + i != iend; + ++i) { + Args.push_back(i->first); + Names.push_back(i->second); + } + + return DagInit::get(V, VN, Args, Names); +} + +const DagInit * +DagInit::get(const Init *V, const std::string &VN, + const std::vector &args, + const std::vector &argNames) { + return new DagInit(V, VN, args, argNames); +} + const Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { std::vector NewArgs; for (unsigned i = 0, e = Args.size(); i != e; ++i) @@ -1322,7 +1404,7 @@ const Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { const Init *Op = Val->resolveReferences(R, RV); if (Args != NewArgs || Op != Val) - return new DagInit(Op, ValName, NewArgs, ArgNames); + return DagInit::get(Op, ValName, NewArgs, ArgNames); return this; } @@ -1350,7 +1432,7 @@ std::string DagInit::getAsString() const { RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) : Name(N), Ty(T), Prefix(P) { - Value = Ty->convertValue(new UnsetInit()); + Value = Ty->convertValue(UnsetInit::get()); assert(Value && "Cannot create unset value for current type!"); } diff --git a/utils/TableGen/Record.h b/utils/TableGen/Record.h index c49a720edd8..c3ce2dd6046 100644 --- a/utils/TableGen/Record.h +++ b/utils/TableGen/Record.h @@ -16,6 +16,7 @@ #define RECORD_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/raw_ostream.h" @@ -485,6 +486,12 @@ RecTy *resolveTypes(RecTy *T1, RecTy *T2); //===----------------------------------------------------------------------===// class Init { + Init(const Init &); // Do not define. + Init &operator=(const Init &); // Do not define. + +protected: + Init(void) {} + public: virtual ~Init() {} @@ -562,9 +569,14 @@ inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) { /// class TypedInit : public Init { RecTy *Ty; -public: + + TypedInit(const TypedInit &Other); // Do not define. + TypedInit &operator=(const TypedInit &Other); // Do not define. + +protected: explicit TypedInit(RecTy *T) : Ty(T) {} +public: RecTy *getType() const { return Ty; } virtual const Init * @@ -596,7 +608,13 @@ public: /// UnsetInit - ? - Represents an uninitialized value /// class UnsetInit : public Init { + UnsetInit() : Init() {} + UnsetInit(const UnsetInit &); // Do not define. + UnsetInit &operator=(const UnsetInit &Other); // Do not define. + public: + static const UnsetInit *get(); + virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); } @@ -610,8 +628,13 @@ public: /// class BitInit : public Init { bool Value; -public: + explicit BitInit(bool V) : Value(V) {} + BitInit(const BitInit &Other); // Do not define. + BitInit &operator=(BitInit &Other); // Do not define. + +public: + static const BitInit *get(bool V); bool getValue() const { return Value; } @@ -627,10 +650,17 @@ public: /// class BitsInit : public Init { std::vector Bits; -public: - explicit BitsInit(unsigned Size) : Bits(Size) {} + + BitsInit(unsigned Size) : Bits(Size) {} + BitsInit(ArrayRef Range) : Bits(Range.begin(), Range.end()) {} + BitsInit(const BitsInit &Other); // Do not define. + BitsInit &operator=(const BitsInit &Other); // Do not define. + +public: + static const BitsInit *get(ArrayRef Range); + unsigned getNumBits() const { return Bits.size(); } const Init *getBit(unsigned Bit) const { @@ -664,9 +694,15 @@ public: /// class IntInit : public TypedInit { int64_t Value; -public: + explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {} + IntInit(const IntInit &Other); // Do not define. + IntInit &operator=(const IntInit &Other); // Do note define. + +public: + static const IntInit *get(int64_t V); + int64_t getValue() const { return Value; } virtual const Init *convertInitializerTo(RecTy *Ty) const { @@ -702,10 +738,16 @@ public: /// class StringInit : public TypedInit { std::string Value; -public: + explicit StringInit(const std::string &V) : TypedInit(StringRecTy::get()), Value(V) {} + StringInit(const StringInit &Other); // Do not define. + StringInit &operator=(const StringInit &Other); // Do not define. + +public: + static const StringInit *get(const std::string &V); + const std::string &getValue() const { return Value; } virtual const Init *convertInitializerTo(RecTy *Ty) const { @@ -738,9 +780,15 @@ public: /// class CodeInit : public Init { std::string Value; -public: + explicit CodeInit(const std::string &V) : Value(V) {} + CodeInit(const CodeInit &Other); // Do not define. + CodeInit &operator=(const CodeInit &Other); // Do not define. + +public: + static const CodeInit *get(const std::string &V); + const std::string &getValue() const { return Value; } virtual const Init *convertInitializerTo(RecTy *Ty) const { @@ -764,6 +812,12 @@ public: explicit ListInit(ArrayRef Range, RecTy *EltTy) : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {} + ListInit(const ListInit &Other); // Do not define. + ListInit &operator=(const ListInit &Other); // Do not define. + +public: + static const ListInit *get(ArrayRef Range, RecTy *EltTy); + unsigned getSize() const { return Values.size(); } const Init *getElement(unsigned i) const { assert(i < Values.size() && "List element index out of range!"); @@ -816,9 +870,13 @@ public: /// OpInit - Base class for operators /// class OpInit : public TypedInit { -public: - OpInit(RecTy *Type) : TypedInit(Type) {} + OpInit(const OpInit &Other); // Do not define. + OpInit &operator=(OpInit &Other); // Do not define. +protected: + explicit OpInit(RecTy *Type) : TypedInit(Type) {} + +public: // Clone - Clone this operator, replacing arguments with the new list virtual const OpInit *clone(std::vector &Operands) const = 0; @@ -849,16 +907,21 @@ public: private: UnaryOp Opc; const Init *LHS; + + UnOpInit(UnaryOp opc, const Init *lhs, RecTy *Type) + : OpInit(Type), Opc(opc), LHS(lhs) {} + + UnOpInit(const UnOpInit &Other); // Do not define. + UnOpInit &operator=(const UnOpInit &Other); // Do not define. + public: - UnOpInit(UnaryOp opc, const Init *lhs, RecTy *Type) : - OpInit(Type), Opc(opc), LHS(lhs) { - } + static const UnOpInit *get(UnaryOp opc, const Init *lhs, RecTy *Type); // Clone - Clone this operator, replacing arguments with the new list virtual const OpInit *clone(std::vector &Operands) const { assert(Operands.size() == 1 && "Wrong number of operands for unary operation"); - return new UnOpInit(getOpcode(), *Operands.begin(), getType()); + return UnOpInit::get(getOpcode(), *Operands.begin(), getType()); } int getNumOperands() const { return 1; } @@ -887,16 +950,22 @@ public: private: BinaryOp Opc; const Init *LHS, *RHS; -public: + BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, RecTy *Type) : - OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) { - } + OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {} + + BinOpInit(const BinOpInit &Other); // Do not define. + BinOpInit &operator=(const BinOpInit &Other); // Do not define. + +public: + static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs, + RecTy *Type); // Clone - Clone this operator, replacing arguments with the new list virtual const OpInit *clone(std::vector &Operands) const { assert(Operands.size() == 2 && "Wrong number of operands for binary operation"); - return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType()); + return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType()); } int getNumOperands() const { return 2; } @@ -930,18 +999,25 @@ public: private: TernaryOp Opc; const Init *LHS, *MHS, *RHS; -public: + TernOpInit(TernaryOp opc, const Init *lhs, const Init *mhs, const Init *rhs, RecTy *Type) : - OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) { - } + OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {} + + TernOpInit(const TernOpInit &Other); // Do not define. + TernOpInit &operator=(const TernOpInit &Other); // Do not define. + +public: + static const TernOpInit *get(TernaryOp opc, const Init *lhs, + const Init *mhs, const Init *rhs, + RecTy *Type); // Clone - Clone this operator, replacing arguments with the new list virtual const OpInit *clone(std::vector &Operands) const { assert(Operands.size() == 3 && "Wrong number of operands for ternary operation"); - return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2], - getType()); + return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2], + getType()); } int getNumOperands() const { return 3; } @@ -978,9 +1054,16 @@ public: /// class VarInit : public TypedInit { std::string VarName; -public: + explicit VarInit(const std::string &VN, RecTy *T) - : TypedInit(T), VarName(VN) {} + : TypedInit(T), VarName(VN) {} + + VarInit(const VarInit &Other); // Do not define. + VarInit &operator=(const VarInit &Other); // Do not define. + +public: + static const VarInit *get(const std::string &VN, RecTy *T); + static const VarInit *get(const Init *VN, RecTy *T); virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); @@ -1013,13 +1096,19 @@ public: class VarBitInit : public Init { const TypedInit *TI; unsigned Bit; -public: + VarBitInit(const TypedInit *T, unsigned B) : TI(T), Bit(B) { assert(T->getType() && dynamic_cast(T->getType()) && ((BitsRecTy*)T->getType())->getNumBits() > B && "Illegal VarBitInit expression!"); } + VarBitInit(const VarBitInit &Other); // Do not define. + VarBitInit &operator=(const VarBitInit &Other); // Do not define. + +public: + static const VarBitInit *get(const TypedInit *T, unsigned B); + virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); } @@ -1036,14 +1125,22 @@ public: class VarListElementInit : public TypedInit { const TypedInit *TI; unsigned Element; -public: + VarListElementInit(const TypedInit *T, unsigned E) - : TypedInit(dynamic_cast(T->getType())->getElementType()), - TI(T), Element(E) { + : TypedInit(dynamic_cast(T->getType())->getElementType()), + TI(T), Element(E) { assert(T->getType() && dynamic_cast(T->getType()) && "Illegal VarBitInit expression!"); } + VarListElementInit(const VarListElementInit &Other); // Do not define. + VarListElementInit &operator=(const VarListElementInit &Other); // Do + // not + // define. + +public: + static const VarListElementInit *get(const TypedInit *T, unsigned E); + virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); } @@ -1069,10 +1166,15 @@ public: /// class DefInit : public TypedInit { Record *Def; + DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {} friend class Record; + + DefInit(const DefInit &Other); // Do not define. + DefInit &operator=(const DefInit &Other); // Do not define. + public: - static DefInit *get(Record*); + static const DefInit *get(Record*); virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); @@ -1115,12 +1217,19 @@ public: class FieldInit : public TypedInit { const Init *Rec; // Record we are referring to std::string FieldName; // Field we are accessing -public: + FieldInit(const Init *R, const std::string &FN) - : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { + : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { assert(getType() && "FieldInit with non-record type!"); } + FieldInit(const FieldInit &Other); // Do not define. + FieldInit &operator=(const FieldInit &Other); // Do not define. + +public: + static const FieldInit *get(const Init *R, const std::string &FN); + static const FieldInit *get(const Init *R, const Init *FN); + virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); } @@ -1147,8 +1256,8 @@ class DagInit : public TypedInit { std::string ValName; std::vector Args; std::vector ArgNames; -public: - DagInit(const Init *V, std::string VN, + + DagInit(const Init *V, const std::string &VN, const std::vector > &args) : TypedInit(DagRecTy::get()), Val(V), ValName(VN) { Args.reserve(args.size()); @@ -1158,11 +1267,24 @@ public: ArgNames.push_back(args[i].second); } } - DagInit(const Init *V, std::string VN, const std::vector &args, + DagInit(const Init *V, const std::string &VN, + const std::vector &args, const std::vector &argNames) : TypedInit(DagRecTy::get()), Val(V), ValName(VN), Args(args), ArgNames(argNames) { } + DagInit(const DagInit &Other); // Do not define. + DagInit &operator=(const DagInit &Other); // Do not define. + +public: + static const DagInit *get(const Init *V, const std::string &VN, + const std::vector< + std::pair > &args); + + static const DagInit *get(const Init *V, const std::string &VN, + const std::vector &args, + const std::vector &argNames); + virtual const Init *convertInitializerTo(RecTy *Ty) const { return Ty->convertValue(this); } diff --git a/utils/TableGen/TGParser.cpp b/utils/TableGen/TGParser.cpp index 1565959a69b..0cfc2c5c285 100644 --- a/utils/TableGen/TGParser.cpp +++ b/utils/TableGen/TGParser.cpp @@ -131,7 +131,7 @@ bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName, if (NewBits[i] == 0) NewBits[i] = CurVal->getBit(i); - V = new BitsInit(ArrayRef(NewBits)); + V = BitsInit::get(NewBits); } if (RV->setValue(V)) @@ -647,13 +647,13 @@ const Init *TGParser::ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc) { if (CurRec) { if (const RecordVal *RV = CurRec->getValue(Name)) - return new VarInit(Name, RV->getType()); + return VarInit::get(Name, RV->getType()); std::string TemplateArgName = CurRec->getName()+":"+Name; if (CurRec->isTemplateArg(TemplateArgName)) { const RecordVal *RV = CurRec->getValue(TemplateArgName); assert(RV && "Template arg doesn't exist??"); - return new VarInit(TemplateArgName, RV->getType()); + return VarInit::get(TemplateArgName, RV->getType()); } } @@ -662,7 +662,7 @@ const Init *TGParser::ParseIDValue(Record *CurRec, if (CurMultiClass->Rec.isTemplateArg(MCName)) { const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); assert(RV && "Template arg doesn't exist??"); - return new VarInit(MCName, RV->getType()); + return VarInit::get(MCName, RV->getType()); } } @@ -790,7 +790,7 @@ const Init *TGParser::ParseOperation(Record *CurRec) { return 0; } Lex.Lex(); // eat the ')' - return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass); + return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); } case tgtok::XConcat: @@ -848,14 +848,14 @@ const Init *TGParser::ParseOperation(Record *CurRec) { if (Code == BinOpInit::STRCONCAT) { while (InitList.size() > 2) { const Init *RHS = InitList.pop_back_val(); - RHS = (new BinOpInit(Code, InitList.back(), RHS, Type)) - ->Fold(CurRec, CurMultiClass); + RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) + ->Fold(CurRec, CurMultiClass); InitList.back() = RHS; } } if (InitList.size() == 2) - return (new BinOpInit(Code, InitList[0], InitList[1], Type)) + return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) ->Fold(CurRec, CurMultiClass); Error(OpLoc, "expected two operands to operator"); @@ -982,7 +982,7 @@ const Init *TGParser::ParseOperation(Record *CurRec) { break; } } - return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, + return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass); } } @@ -1042,7 +1042,7 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { const Init *R = 0; switch (Lex.getCode()) { default: TokError("Unknown token when parsing a value"); break; - case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break; + case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; case tgtok::StrVal: { std::string Val = Lex.getCurStrVal(); Lex.Lex(); @@ -1053,15 +1053,15 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { Lex.Lex(); } - R = new StringInit(Val); + R = StringInit::get(Val); break; } case tgtok::CodeFragment: - R = new CodeInit(Lex.getCurStrVal()); + R = CodeInit::get(Lex.getCurStrVal()); Lex.Lex(); break; case tgtok::question: - R = new UnsetInit(); + R = UnsetInit::get(); Lex.Lex(); break; case tgtok::Id: { @@ -1138,7 +1138,7 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { } NewBits[Vals.size()-i-1] = Bit; } - return new BitsInit(ArrayRef(NewBits)); + return BitsInit::get(NewBits); } case tgtok::l_square: { // Value ::= '[' ValueList ']' Lex.Lex(); // eat the '[' @@ -1237,7 +1237,7 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { DeducedEltTy = EltTy; } - return new ListInit(Vals, DeducedEltTy); + return ListInit::get(Vals, DeducedEltTy); } case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' Lex.Lex(); // eat the '(' @@ -1272,7 +1272,7 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { } Lex.Lex(); // eat the ')' - return new DagInit(Operator, OperatorName, DagArgs); + return DagInit::get(Operator, OperatorName, DagArgs); } case tgtok::XHead: @@ -1362,7 +1362,7 @@ const Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) { Result->getAsString() + "'"); return 0; } - Result = new FieldInit(Result, Lex.getCurStrVal()); + Result = FieldInit::get(Result, Lex.getCurStrVal()); Lex.Lex(); // eat field name break; }