X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=utils%2FTableGen%2FCodeGenDAGPatterns.cpp;h=6e7dd1eae05dd9a946434f53196f2b939d24f9f5;hb=13d5b71a957f51fc86e3d21606a312b391039a4b;hp=882c7245f6126b5bae863dc9d8aee39060689754;hpb=83ec4b6711980242ef3c55a4fa36b2d7a39c1bfb;p=oota-llvm.git diff --git a/utils/TableGen/CodeGenDAGPatterns.cpp b/utils/TableGen/CodeGenDAGPatterns.cpp index 882c7245f61..6e7dd1eae05 100644 --- a/utils/TableGen/CodeGenDAGPatterns.cpp +++ b/utils/TableGen/CodeGenDAGPatterns.cpp @@ -140,6 +140,33 @@ void DumpDepVars(MultipleUseVarSet &DepVars) { } } +//===----------------------------------------------------------------------===// +// PatternToMatch implementation +// + +/// getPredicateCheck - Return a single string containing all of this +/// pattern's predicates concatenated with "&&" operators. +/// +std::string PatternToMatch::getPredicateCheck() const { + std::string PredicateCheck; + for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { + if (DefInit *Pred = dynamic_cast(Predicates->getElement(i))) { + Record *Def = Pred->getDef(); + if (!Def->isSubClassOf("Predicate")) { +#ifndef NDEBUG + Def->dump(); +#endif + assert(0 && "Unknown predicate type!"); + } + if (!PredicateCheck.empty()) + PredicateCheck += " && "; + PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; + } + } + + return PredicateCheck; +} + //===----------------------------------------------------------------------===// // SDTypeConstraint implementation // @@ -401,6 +428,8 @@ SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { Properties |= 1 << SDNPMayLoad; } else if (PropList[i]->getName() == "SDNPSideEffect") { Properties |= 1 << SDNPSideEffect; + } else if (PropList[i]->getName() == "SDNPMemOperand") { + Properties |= 1 << SDNPMemOperand; } else { cerr << "Unknown SD Node property '" << PropList[i]->getName() << "' on node '" << R->getName() << "'!\n"; @@ -441,8 +470,9 @@ bool TreePatternNode::UpdateNodeType(const std::vector &ExtVTs, return true; } - if (getExtTypeNum(0) == MVT::iPTR) { - if (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == EMVT::isInt) + if (getExtTypeNum(0) == MVT::iPTR || getExtTypeNum(0) == MVT::iPTRAny) { + if (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny || + ExtVTs[0] == EMVT::isInt) return false; if (EMVT::isExtIntegerInVTs(ExtVTs)) { std::vector FVTs = FilterEVTs(ExtVTs, isInteger); @@ -452,8 +482,9 @@ bool TreePatternNode::UpdateNodeType(const std::vector &ExtVTs, } } } - - if (ExtVTs[0] == EMVT::isInt && EMVT::isExtIntegerInVTs(getExtTypes())) { + + if ((ExtVTs[0] == EMVT::isInt || ExtVTs[0] == MVT::iAny) && + EMVT::isExtIntegerInVTs(getExtTypes())) { assert(hasTypeSet() && "should be handled above!"); std::vector FVTs = FilterEVTs(getExtTypes(), isInteger); if (getExtTypes() == FVTs) @@ -461,7 +492,8 @@ bool TreePatternNode::UpdateNodeType(const std::vector &ExtVTs, setTypes(FVTs); return true; } - if (ExtVTs[0] == MVT::iPTR && EMVT::isExtIntegerInVTs(getExtTypes())) { + if ((ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny) && + EMVT::isExtIntegerInVTs(getExtTypes())) { //assert(hasTypeSet() && "should be handled above!"); std::vector FVTs = FilterEVTs(getExtTypes(), isInteger); if (getExtTypes() == FVTs) @@ -471,7 +503,8 @@ bool TreePatternNode::UpdateNodeType(const std::vector &ExtVTs, return true; } } - if (ExtVTs[0] == EMVT::isFP && EMVT::isExtFloatingPointInVTs(getExtTypes())) { + if ((ExtVTs[0] == EMVT::isFP || ExtVTs[0] == MVT::fAny) && + EMVT::isExtFloatingPointInVTs(getExtTypes())) { assert(hasTypeSet() && "should be handled above!"); std::vector FVTs = FilterEVTs(getExtTypes(), isFloatingPoint); @@ -486,14 +519,15 @@ bool TreePatternNode::UpdateNodeType(const std::vector &ExtVTs, // // Similarly, we should probably set the type here to the intersection of // {isInt|isFP} and ExtVTs - if ((getExtTypeNum(0) == EMVT::isInt && + if (((getExtTypeNum(0) == EMVT::isInt || getExtTypeNum(0) == MVT::iAny) && EMVT::isExtIntegerInVTs(ExtVTs)) || - (getExtTypeNum(0) == EMVT::isFP && + ((getExtTypeNum(0) == EMVT::isFP || getExtTypeNum(0) == MVT::fAny) && EMVT::isExtFloatingPointInVTs(ExtVTs))) { setTypes(ExtVTs); return true; } - if (getExtTypeNum(0) == EMVT::isInt && ExtVTs[0] == MVT::iPTR) { + if (getExtTypeNum(0) == EMVT::isInt && + (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny)) { setTypes(ExtVTs); return true; } @@ -525,6 +559,7 @@ void TreePatternNode::print(std::ostream &OS) const { case EMVT::isFP : OS << ":isFP"; break; case EMVT::isUnknown: ; /*OS << ":?";*/ break; case MVT::iPTR: OS << ":iPTR"; break; + case MVT::iPTRAny: OS << ":iPTRAny"; break; default: { std::string VTName = llvm::getName(getTypeNum(0)); // Strip off MVT:: prefix if present. @@ -547,8 +582,8 @@ void TreePatternNode::print(std::ostream &OS) const { OS << ")"; } - if (!PredicateFn.empty()) - OS << "<>"; + for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) + OS << "<>"; if (TransformFn) OS << "<getName() << ">>"; if (!getName().empty()) @@ -570,7 +605,7 @@ bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, const MultipleUseVarSet &DepVars) const { if (N == this) return true; if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || - getPredicateFn() != N->getPredicateFn() || + getPredicateFns() != N->getPredicateFns() || getTransformFn() != N->getTransformFn()) return false; @@ -608,7 +643,7 @@ TreePatternNode *TreePatternNode::clone() const { } New->setName(getName()); New->setTypes(getExtTypes()); - New->setPredicateFn(getPredicateFn()); + New->setPredicateFns(getPredicateFns()); New->setTransformFn(getTransformFn()); return New; } @@ -626,9 +661,12 @@ SubstituteFormalArguments(std::map &ArgMap) { if (dynamic_cast(Val) && static_cast(Val)->getDef()->getName() == "node") { // We found a use of a formal argument, replace it with its value. - Child = ArgMap[Child->getName()]; - assert(Child && "Couldn't find formal argument!"); - setChild(i, Child); + TreePatternNode *NewChild = ArgMap[Child->getName()]; + assert(NewChild && "Couldn't find formal argument!"); + assert((Child->getPredicateFns().empty() || + NewChild->getPredicateFns() == Child->getPredicateFns()) && + "Non-empty child predicate clobbered!"); + setChild(i, NewChild); } } else { getChild(i)->SubstituteFormalArguments(ArgMap); @@ -646,8 +684,16 @@ TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { if (!Op->isSubClassOf("PatFrag")) { // Just recursively inline children nodes. - for (unsigned i = 0, e = getNumChildren(); i != e; ++i) - setChild(i, getChild(i)->InlinePatternFragments(TP)); + for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { + TreePatternNode *Child = getChild(i); + TreePatternNode *NewChild = Child->InlinePatternFragments(TP); + + assert((Child->getPredicateFns().empty() || + NewChild->getPredicateFns() == Child->getPredicateFns()) && + "Non-empty child predicate clobbered!"); + + setChild(i, NewChild); + } return this; } @@ -662,6 +708,10 @@ TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); + std::string Code = Op->getValueAsCode("Predicate"); + if (!Code.empty()) + FragTree->addPredicateFn("Predicate_"+Op->getName()); + // Resolve formal arguments to their actual value. if (Frag->getNumArgs()) { // Compute the map of formal to actual arguments. @@ -674,10 +724,17 @@ TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { FragTree->setName(getName()); FragTree->UpdateNodeType(getExtTypes(), TP); - + + // Transfer in the old predicates. + for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) + FragTree->addPredicateFn(getPredicateFns()[i]); + // Get a new copy of this fragment to stitch into here. //delete this; // FIXME: implement refcounting! - return FragTree; + + // The fragment we inlined could have recursive inlining that is needed. See + // if there are any pattern fragments in it and inline them as needed. + return FragTree->InlinePatternFragments(TP); } /// getImplicitType - Check to see if the specified record has an implicit @@ -742,8 +799,17 @@ getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { return &CDP.getIntrinsicInfo(IID); } +/// isCommutativeIntrinsic - Return true if the node corresponds to a +/// commutative intrinsic. +bool +TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { + if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) + return Int->isCommutative; + return false; +} + -/// ApplyTypeConstraints - Apply all of the type constraints relevent to +/// ApplyTypeConstraints - Apply all of the type constraints relevant to /// this node and its children in the tree. This returns true if it makes a /// change, false otherwise. If a type contradiction is found, throw an /// exception. @@ -767,7 +833,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { assert(getTypeNum(i) == VT && "TreePattern has too many types!"); VT = getTypeNum(0); - if (VT != MVT::iPTR) { + if (VT != MVT::iPTR && VT != MVT::iPTRAny) { unsigned Size = MVT(VT).getSizeInBits(); // Make sure that the value is representable for this type. if (Size < 32) { @@ -776,7 +842,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { // If sign-extended doesn't fit, does it fit as unsigned? unsigned ValueMask; unsigned UnsignedVal; - ValueMask = unsigned(MVT(VT).getIntegerVTBitMask()); + ValueMask = unsigned(~uint32_t(0UL) >> (32-Size)); UnsignedVal = unsigned(II->getValue()); if ((ValueMask & UnsignedVal) != UnsignedVal) { @@ -822,18 +888,22 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { bool MadeChange = false; // Apply the result type to the node. - MadeChange = UpdateNodeType(Int->ArgVTs[0], TP); + unsigned NumRetVTs = Int->IS.RetVTs.size(); + unsigned NumParamVTs = Int->IS.ParamVTs.size(); - if (getNumChildren() != Int->ArgVTs.size()) + for (unsigned i = 0, e = NumRetVTs; i != e; ++i) + MadeChange |= UpdateNodeType(Int->IS.RetVTs[i], TP); + + if (getNumChildren() != NumParamVTs + NumRetVTs) TP.error("Intrinsic '" + Int->Name + "' expects " + - utostr(Int->ArgVTs.size()-1) + " operands, not " + - utostr(getNumChildren()-1) + " operands!"); + utostr(NumParamVTs + NumRetVTs - 1) + " operands, not " + + utostr(getNumChildren() - 1) + " operands!"); // Apply type info to the intrinsic ID. MadeChange |= getChild(0)->UpdateNodeType(MVT::iPTR, TP); - for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { - MVT::SimpleValueType OpVT = Int->ArgVTs[i]; + for (unsigned i = NumRetVTs, e = getNumChildren(); i != e; ++i) { + MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i - NumRetVTs]; MadeChange |= getChild(i)->UpdateNodeType(OpVT, TP); MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); } @@ -999,11 +1069,13 @@ bool TreePatternNode::canPatternMatch(std::string &Reason, // If this node is a commutative operator, check that the LHS isn't an // immediate. const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); - if (NodeInfo.hasProperty(SDNPCommutative)) { + bool isCommIntrinsic = isCommutativeIntrinsic(CDP); + if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { // Scan all of the operands of the node and make sure that only the last one // is a constant node, unless the RHS also is. if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { - for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) + bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. + for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) if (OnlyOnRHSOfCommutative(getChild(i))) { Reason="Immediate value must be on the RHS of commutative operators!"; return false; @@ -1167,7 +1239,7 @@ TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { // If this intrinsic returns void, it must have side-effects and thus a // chain. - if (Int.ArgVTs[0] == MVT::isVoid) { + if (Int.IS.RetVTs[0] == MVT::isVoid) { Operator = getDAGPatterns().get_intrinsic_void_sdnode(); } else if (Int.ModRef != CodeGenIntrinsic::NoMem) { // Has side-effects, requires chain. @@ -1231,7 +1303,8 @@ void TreePattern::dump() const { print(*cerr.stream()); } // FIXME: REMOVE OSTREAM ARGUMENT CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : Records(R) { - Intrinsics = LoadIntrinsics(Records); + Intrinsics = LoadIntrinsics(Records, false); + TgtIntrinsics = LoadIntrinsics(Records, true); ParseNodeInfo(); ParseNodeTransforms(); ParseComplexPatterns(); @@ -1359,7 +1432,7 @@ void CodeGenDAGPatterns::ParsePatternFragments() { // this fragment uses it. std::string Code = Fragments[i]->getValueAsCode("Predicate"); if (!Code.empty()) - P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); + P->getOnlyTree()->addPredicateFn("Predicate_"+Fragments[i]->getName()); // If there is a node transformation corresponding to this, keep track of // it. @@ -1370,9 +1443,8 @@ void CodeGenDAGPatterns::ParsePatternFragments() { // Now that we've parsed all of the tree fragments, do a closure on them so // that there are not references to PatFrags left inside of them. - for (std::map::iterator I = PatternFragments.begin(), - E = PatternFragments.end(); I != E; ++I) { - TreePattern *ThePat = I->second; + for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { + TreePattern *ThePat = PatternFragments[Fragments[i]]; ThePat->InlinePatternFragments(); // Infer as many types as possible. Don't worry about it if we don't infer @@ -1866,7 +1938,7 @@ void CodeGenDAGPatterns::ParseInstructions() { TreePatternNode *OpNode = InVal->clone(); // No predicate is useful on the result. - OpNode->setPredicateFn(""); + OpNode->clearPredicateFns(); // Promote the xform function to be an explicit node if set. if (Record *Xform = OpNode->getTransformFn()) { @@ -2093,7 +2165,7 @@ static void CombineChildVariants(TreePatternNode *Orig, // Copy over properties. R->setName(Orig->getName()); - R->setPredicateFn(Orig->getPredicateFn()); + R->setPredicateFns(Orig->getPredicateFns()); R->setTransformFn(Orig->getTransformFn()); R->setTypes(Orig->getExtTypes()); @@ -2155,7 +2227,7 @@ static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, Record *Operator = N->getOperator(); // Only permit raw nodes. - if (!N->getName().empty() || !N->getPredicateFn().empty() || + if (!N->getName().empty() || !N->getPredicateFns().empty() || N->getTransformFn()) { Children.push_back(N); return; @@ -2250,8 +2322,10 @@ static void GenerateVariantsOf(TreePatternNode *N, CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); // If this node is commutative, consider the commuted order. - if (NodeInfo.hasProperty(SDNPCommutative)) { - assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!"); + bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); + if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { + assert((N->getNumChildren()==2 || isCommIntrinsic) && + "Commutative but doesn't have 2 children!"); // Don't count children which are actually register references. unsigned NC = 0; for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { @@ -2265,7 +2339,20 @@ static void GenerateVariantsOf(TreePatternNode *N, NC++; } // Consider the commuted order. - if (NC == 2) + if (isCommIntrinsic) { + // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd + // operands are the commutative operands, and there might be more operands + // after those. + assert(NC >= 3 && + "Commutative intrinsic should have at least 3 childrean!"); + std::vector > Variants; + Variants.push_back(ChildVariants[0]); // Intrinsic id. + Variants.push_back(ChildVariants[2]); + Variants.push_back(ChildVariants[1]); + for (unsigned i = 3; i != NC; ++i) + Variants.push_back(ChildVariants[i]); + CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); + } else if (NC == 2) CombineChildVariants(N, ChildVariants[1], ChildVariants[0], OutVariants, CDP, DepVars); }