10b57cec5SDimitry Andric //===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 9*0fca6ea1SDimitry Andric #include "Basic/SDNodeProperties.h" 10*0fca6ea1SDimitry Andric #include "Common/CodeGenDAGPatterns.h" 11*0fca6ea1SDimitry Andric #include "Common/CodeGenInstruction.h" 12*0fca6ea1SDimitry Andric #include "Common/CodeGenRegisters.h" 13*0fca6ea1SDimitry Andric #include "Common/CodeGenTarget.h" 14*0fca6ea1SDimitry Andric #include "Common/DAGISelMatcher.h" 15*0fca6ea1SDimitry Andric #include "Common/InfoByHwMode.h" 160b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h" 180b57cec5SDimitry Andric #include "llvm/TableGen/Error.h" 190b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 200b57cec5SDimitry Andric #include <utility> 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric /// getRegisterValueType - Look up and return the ValueType of the specified 2406c3fb27SDimitry Andric /// register. If the register is a member of multiple register classes, they 2506c3fb27SDimitry Andric /// must all have the same type. 260b57cec5SDimitry Andric static MVT::SimpleValueType getRegisterValueType(Record *R, 270b57cec5SDimitry Andric const CodeGenTarget &T) { 280b57cec5SDimitry Andric bool FoundRC = false; 290b57cec5SDimitry Andric MVT::SimpleValueType VT = MVT::Other; 300b57cec5SDimitry Andric const CodeGenRegister *Reg = T.getRegBank().getReg(R); 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric for (const auto &RC : T.getRegBank().getRegClasses()) { 330b57cec5SDimitry Andric if (!RC.contains(Reg)) 340b57cec5SDimitry Andric continue; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric if (!FoundRC) { 370b57cec5SDimitry Andric FoundRC = true; 380b57cec5SDimitry Andric const ValueTypeByHwMode &VVT = RC.getValueTypeNum(0); 3906c3fb27SDimitry Andric assert(VVT.isSimple()); 400b57cec5SDimitry Andric VT = VVT.getSimple().SimpleTy; 410b57cec5SDimitry Andric continue; 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric #ifndef NDEBUG 450b57cec5SDimitry Andric // If this occurs in multiple register classes, they all have to agree. 4606c3fb27SDimitry Andric const ValueTypeByHwMode &VVT = RC.getValueTypeNum(0); 4706c3fb27SDimitry Andric assert(VVT.isSimple() && VVT.getSimple().SimpleTy == VT && 480b57cec5SDimitry Andric "ValueType mismatch between register classes for this register"); 490b57cec5SDimitry Andric #endif 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric return VT; 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric namespace { 550b57cec5SDimitry Andric class MatcherGen { 560b57cec5SDimitry Andric const PatternToMatch &Pattern; 570b57cec5SDimitry Andric const CodeGenDAGPatterns &CGP; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts 600b57cec5SDimitry Andric /// out with all of the types removed. This allows us to insert type checks 610b57cec5SDimitry Andric /// as we scan the tree. 620b57cec5SDimitry Andric TreePatternNodePtr PatWithNoTypes; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric /// VariableMap - A map from variable names ('$dst') to the recorded operand 650b57cec5SDimitry Andric /// number that they were captured as. These are biased by 1 to make 660b57cec5SDimitry Andric /// insertion easier. 670b57cec5SDimitry Andric StringMap<unsigned> VariableMap; 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric /// This maintains the recorded operand number that OPC_CheckComplexPattern 700b57cec5SDimitry Andric /// drops each sub-operand into. We don't want to insert these into 710b57cec5SDimitry Andric /// VariableMap because that leads to identity checking if they are 720b57cec5SDimitry Andric /// encountered multiple times. Biased by 1 like VariableMap for 730b57cec5SDimitry Andric /// consistency. 740b57cec5SDimitry Andric StringMap<unsigned> NamedComplexPatternOperands; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /// NextRecordedOperandNo - As we emit opcodes to record matched values in 770b57cec5SDimitry Andric /// the RecordedNodes array, this keeps track of which slot will be next to 780b57cec5SDimitry Andric /// record into. 790b57cec5SDimitry Andric unsigned NextRecordedOperandNo; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric /// MatchedChainNodes - This maintains the position in the recorded nodes 820b57cec5SDimitry Andric /// array of all of the recorded input nodes that have chains. 830b57cec5SDimitry Andric SmallVector<unsigned, 2> MatchedChainNodes; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric /// MatchedComplexPatterns - This maintains a list of all of the 860b57cec5SDimitry Andric /// ComplexPatterns that we need to check. The second element of each pair 870b57cec5SDimitry Andric /// is the recorded operand number of the input node. 88*0fca6ea1SDimitry Andric SmallVector<std::pair<const TreePatternNode *, unsigned>, 2> 89*0fca6ea1SDimitry Andric MatchedComplexPatterns; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric /// PhysRegInputs - List list has an entry for each explicitly specified 920b57cec5SDimitry Andric /// physreg input to the pattern. The first elt is the Register node, the 930b57cec5SDimitry Andric /// second is the recorded slot number the input pattern match saved it in. 940b57cec5SDimitry Andric SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric /// Matcher - This is the top level of the generated matcher, the result. 970b57cec5SDimitry Andric Matcher *TheMatcher; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric /// CurPredicate - As we emit matcher nodes, this points to the latest check 1000b57cec5SDimitry Andric /// which should have future checks stuck into its Next position. 1010b57cec5SDimitry Andric Matcher *CurPredicate; 102*0fca6ea1SDimitry Andric 1030b57cec5SDimitry Andric public: 1040b57cec5SDimitry Andric MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp); 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric bool EmitMatcherCode(unsigned Variant); 1070b57cec5SDimitry Andric void EmitResultCode(); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric Matcher *GetMatcher() const { return TheMatcher; } 110*0fca6ea1SDimitry Andric 1110b57cec5SDimitry Andric private: 1120b57cec5SDimitry Andric void AddMatcher(Matcher *NewNode); 11306c3fb27SDimitry Andric void InferPossibleTypes(); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric // Matcher Generation. 116*0fca6ea1SDimitry Andric void EmitMatchCode(const TreePatternNode &N, TreePatternNode &NodeNoTypes); 117*0fca6ea1SDimitry Andric void EmitLeafMatchCode(const TreePatternNode &N); 118*0fca6ea1SDimitry Andric void EmitOperatorMatchCode(const TreePatternNode &N, 119*0fca6ea1SDimitry Andric TreePatternNode &NodeNoTypes); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric /// If this is the first time a node with unique identifier Name has been 1220b57cec5SDimitry Andric /// seen, record it. Otherwise, emit a check to make sure this is the same 1230b57cec5SDimitry Andric /// node. Returns true if this is the first encounter. 1240b57cec5SDimitry Andric bool recordUniqueNode(ArrayRef<std::string> Names); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // Result Code Generation. 1270b57cec5SDimitry Andric unsigned getNamedArgumentSlot(StringRef Name) { 1280b57cec5SDimitry Andric unsigned VarMapEntry = VariableMap[Name]; 1290b57cec5SDimitry Andric assert(VarMapEntry != 0 && 1300b57cec5SDimitry Andric "Variable referenced but not defined and not caught earlier!"); 1310b57cec5SDimitry Andric return VarMapEntry - 1; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 134*0fca6ea1SDimitry Andric void EmitResultOperand(const TreePatternNode &N, 1350b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps); 136*0fca6ea1SDimitry Andric void EmitResultOfNamedOperand(const TreePatternNode &N, 1370b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps); 138*0fca6ea1SDimitry Andric void EmitResultLeafAsOperand(const TreePatternNode &N, 1390b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps); 140*0fca6ea1SDimitry Andric void EmitResultInstructionAsOperand(const TreePatternNode &N, 1410b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps); 142*0fca6ea1SDimitry Andric void EmitResultSDNodeXFormAsOperand(const TreePatternNode &N, 1430b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps); 1440b57cec5SDimitry Andric }; 1450b57cec5SDimitry Andric 1468bcb0991SDimitry Andric } // end anonymous namespace 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric MatcherGen::MatcherGen(const PatternToMatch &pattern, 1490b57cec5SDimitry Andric const CodeGenDAGPatterns &cgp) 1505f757f3fSDimitry Andric : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0), TheMatcher(nullptr), 1515f757f3fSDimitry Andric CurPredicate(nullptr) { 1525f757f3fSDimitry Andric // We need to produce the matcher tree for the patterns source pattern. To 1535f757f3fSDimitry Andric // do this we need to match the structure as well as the types. To do the 1545f757f3fSDimitry Andric // type matching, we want to figure out the fewest number of type checks we 1555f757f3fSDimitry Andric // need to emit. For example, if there is only one integer type supported 1565f757f3fSDimitry Andric // by a target, there should be no type comparisons at all for integer 1575f757f3fSDimitry Andric // patterns! 1580b57cec5SDimitry Andric // 1590b57cec5SDimitry Andric // To figure out the fewest number of type checks needed, clone the pattern, 1600b57cec5SDimitry Andric // remove the types, then perform type inference on the pattern as a whole. 1610b57cec5SDimitry Andric // If there are unresolved types, emit an explicit check for those types, 1620b57cec5SDimitry Andric // apply the type to the tree, then rerun type inference. Iterate until all 1630b57cec5SDimitry Andric // types are resolved. 1640b57cec5SDimitry Andric // 165*0fca6ea1SDimitry Andric PatWithNoTypes = Pattern.getSrcPattern().clone(); 1660b57cec5SDimitry Andric PatWithNoTypes->RemoveAllTypes(); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // If there are types that are manifestly known, infer them. 16906c3fb27SDimitry Andric InferPossibleTypes(); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric /// InferPossibleTypes - As we emit the pattern, we end up generating type 1730b57cec5SDimitry Andric /// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we 1740b57cec5SDimitry Andric /// want to propagate implied types as far throughout the tree as possible so 1750b57cec5SDimitry Andric /// that we avoid doing redundant type checks. This does the type propagation. 17606c3fb27SDimitry Andric void MatcherGen::InferPossibleTypes() { 1770b57cec5SDimitry Andric // TP - Get *SOME* tree pattern, we don't care which. It is only used for 1780b57cec5SDimitry Andric // diagnostics, which we know are impossible at this point. 1790b57cec5SDimitry Andric TreePattern &TP = *CGP.pf_begin()->second; 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric bool MadeChange = true; 1820b57cec5SDimitry Andric while (MadeChange) 183*0fca6ea1SDimitry Andric MadeChange = PatWithNoTypes->ApplyTypeConstraints( 184*0fca6ea1SDimitry Andric TP, true /*Ignore reg constraints*/); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric /// AddMatcher - Add a matcher node to the current graph we're building. 1880b57cec5SDimitry Andric void MatcherGen::AddMatcher(Matcher *NewNode) { 1890b57cec5SDimitry Andric if (CurPredicate) 1900b57cec5SDimitry Andric CurPredicate->setNext(NewNode); 1910b57cec5SDimitry Andric else 1920b57cec5SDimitry Andric TheMatcher = NewNode; 1930b57cec5SDimitry Andric CurPredicate = NewNode; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1970b57cec5SDimitry Andric // Pattern Match Generation 1980b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric /// EmitLeafMatchCode - Generate matching code for leaf nodes. 201*0fca6ea1SDimitry Andric void MatcherGen::EmitLeafMatchCode(const TreePatternNode &N) { 202*0fca6ea1SDimitry Andric assert(N.isLeaf() && "Not a leaf?"); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Direct match against an integer constant. 205*0fca6ea1SDimitry Andric if (IntInit *II = dyn_cast<IntInit>(N.getLeafValue())) { 2060b57cec5SDimitry Andric // If this is the root of the dag we're matching, we emit a redundant opcode 2070b57cec5SDimitry Andric // check to ensure that this gets folded into the normal top-level 2080b57cec5SDimitry Andric // OpcodeSwitch. 209*0fca6ea1SDimitry Andric if (&N == &Pattern.getSrcPattern()) { 2100b57cec5SDimitry Andric const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm")); 2110b57cec5SDimitry Andric AddMatcher(new CheckOpcodeMatcher(NI)); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric return AddMatcher(new CheckIntegerMatcher(II->getValue())); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // An UnsetInit represents a named node without any constraints. 218*0fca6ea1SDimitry Andric if (isa<UnsetInit>(N.getLeafValue())) { 219*0fca6ea1SDimitry Andric assert(N.hasName() && "Unnamed ? leaf"); 2200b57cec5SDimitry Andric return; 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 223*0fca6ea1SDimitry Andric DefInit *DI = dyn_cast<DefInit>(N.getLeafValue()); 2240b57cec5SDimitry Andric if (!DI) { 225*0fca6ea1SDimitry Andric errs() << "Unknown leaf kind: " << N << "\n"; 2260b57cec5SDimitry Andric abort(); 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric Record *LeafRec = DI->getDef(); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // A ValueType leaf node can represent a register when named, or itself when 2320b57cec5SDimitry Andric // unnamed. 2330b57cec5SDimitry Andric if (LeafRec->isSubClassOf("ValueType")) { 2340b57cec5SDimitry Andric // A named ValueType leaf always matches: (add i32:$a, i32:$b). 235*0fca6ea1SDimitry Andric if (N.hasName()) 2360b57cec5SDimitry Andric return; 2370b57cec5SDimitry Andric // An unnamed ValueType as in (sext_inreg GPR:$foo, i8). 238*0fca6ea1SDimitry Andric return AddMatcher(new CheckValueTypeMatcher(llvm::getValueType(LeafRec))); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric if ( // Handle register references. Nothing to do here, they always match. 2420b57cec5SDimitry Andric LeafRec->isSubClassOf("RegisterClass") || 2430b57cec5SDimitry Andric LeafRec->isSubClassOf("RegisterOperand") || 2440b57cec5SDimitry Andric LeafRec->isSubClassOf("PointerLikeRegClass") || 2450b57cec5SDimitry Andric LeafRec->isSubClassOf("SubRegIndex") || 2460b57cec5SDimitry Andric // Place holder for SRCVALUE nodes. Nothing to do here. 2470b57cec5SDimitry Andric LeafRec->getName() == "srcvalue") 2480b57cec5SDimitry Andric return; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // If we have a physreg reference like (mul gpr:$src, EAX) then we need to 2510b57cec5SDimitry Andric // record the register 2520b57cec5SDimitry Andric if (LeafRec->isSubClassOf("Register")) { 2530b57cec5SDimitry Andric AddMatcher(new RecordMatcher("physreg input " + LeafRec->getName().str(), 2540b57cec5SDimitry Andric NextRecordedOperandNo)); 255*0fca6ea1SDimitry Andric PhysRegInputs.push_back(std::pair(LeafRec, NextRecordedOperandNo++)); 2560b57cec5SDimitry Andric return; 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric if (LeafRec->isSubClassOf("CondCode")) 2600b57cec5SDimitry Andric return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName())); 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric if (LeafRec->isSubClassOf("ComplexPattern")) { 2630b57cec5SDimitry Andric // We can't model ComplexPattern uses that don't have their name taken yet. 2640b57cec5SDimitry Andric // The OPC_CheckComplexPattern operation implicitly records the results. 265*0fca6ea1SDimitry Andric if (N.getName().empty()) { 2660b57cec5SDimitry Andric std::string S; 2670b57cec5SDimitry Andric raw_string_ostream OS(S); 268*0fca6ea1SDimitry Andric OS << "We expect complex pattern uses to have names: " << N; 2690eae32dcSDimitry Andric PrintFatalError(S); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // Remember this ComplexPattern so that we can emit it after all the other 2730b57cec5SDimitry Andric // structural matches are done. 274*0fca6ea1SDimitry Andric unsigned InputOperand = VariableMap[N.getName()] - 1; 275*0fca6ea1SDimitry Andric MatchedComplexPatterns.push_back(std::pair(&N, InputOperand)); 2760b57cec5SDimitry Andric return; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 27906c3fb27SDimitry Andric if (LeafRec->getName() == "immAllOnesV" || 28006c3fb27SDimitry Andric LeafRec->getName() == "immAllZerosV") { 2810b57cec5SDimitry Andric // If this is the root of the dag we're matching, we emit a redundant opcode 2820b57cec5SDimitry Andric // check to ensure that this gets folded into the normal top-level 2830b57cec5SDimitry Andric // OpcodeSwitch. 284*0fca6ea1SDimitry Andric if (&N == &Pattern.getSrcPattern()) { 285*0fca6ea1SDimitry Andric MVT VT = N.getSimpleType(0); 286e8d8bef9SDimitry Andric StringRef Name = VT.isScalableVector() ? "splat_vector" : "build_vector"; 287e8d8bef9SDimitry Andric const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed(Name)); 2880b57cec5SDimitry Andric AddMatcher(new CheckOpcodeMatcher(NI)); 2890b57cec5SDimitry Andric } 29006c3fb27SDimitry Andric if (LeafRec->getName() == "immAllOnesV") 29106c3fb27SDimitry Andric AddMatcher(new CheckImmAllOnesVMatcher()); 29206c3fb27SDimitry Andric else 29306c3fb27SDimitry Andric AddMatcher(new CheckImmAllZerosVMatcher()); 29406c3fb27SDimitry Andric return; 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric 297*0fca6ea1SDimitry Andric errs() << "Unknown leaf kind: " << N << "\n"; 2980b57cec5SDimitry Andric abort(); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 301*0fca6ea1SDimitry Andric void MatcherGen::EmitOperatorMatchCode(const TreePatternNode &N, 302*0fca6ea1SDimitry Andric TreePatternNode &NodeNoTypes) { 303*0fca6ea1SDimitry Andric assert(!N.isLeaf() && "Not an operator?"); 3040b57cec5SDimitry Andric 305*0fca6ea1SDimitry Andric if (N.getOperator()->isSubClassOf("ComplexPattern")) { 3060b57cec5SDimitry Andric // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is 3070b57cec5SDimitry Andric // "MY_PAT:op1:op2". We should already have validated that the uses are 3080b57cec5SDimitry Andric // consistent. 309*0fca6ea1SDimitry Andric std::string PatternName = std::string(N.getOperator()->getName()); 310*0fca6ea1SDimitry Andric for (unsigned i = 0; i < N.getNumChildren(); ++i) { 3110b57cec5SDimitry Andric PatternName += ":"; 312*0fca6ea1SDimitry Andric PatternName += N.getChild(i).getName(); 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric if (recordUniqueNode(PatternName)) { 316*0fca6ea1SDimitry Andric auto NodeAndOpNum = std::pair(&N, NextRecordedOperandNo - 1); 3170b57cec5SDimitry Andric MatchedComplexPatterns.push_back(NodeAndOpNum); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric return; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 323*0fca6ea1SDimitry Andric const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N.getOperator()); 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is 3260b57cec5SDimitry Andric // a constant without a predicate fn that has more than one bit set, handle 3270b57cec5SDimitry Andric // this as a special case. This is usually for targets that have special 3280b57cec5SDimitry Andric // handling of certain large constants (e.g. alpha with it's 8/16/32-bit 3290b57cec5SDimitry Andric // handling stuff). Using these instructions is often far more efficient 3300b57cec5SDimitry Andric // than materializing the constant. Unfortunately, both the instcombiner 3310b57cec5SDimitry Andric // and the dag combiner can often infer that bits are dead, and thus drop 3320b57cec5SDimitry Andric // them from the mask in the dag. For example, it might turn 'AND X, 255' 3330b57cec5SDimitry Andric // into 'AND X, 254' if it knows the low bit is set. Emit code that checks 3340b57cec5SDimitry Andric // to handle this. 335*0fca6ea1SDimitry Andric if ((N.getOperator()->getName() == "and" || 336*0fca6ea1SDimitry Andric N.getOperator()->getName() == "or") && 337*0fca6ea1SDimitry Andric N.getChild(1).isLeaf() && N.getChild(1).getPredicateCalls().empty() && 338*0fca6ea1SDimitry Andric N.getPredicateCalls().empty()) { 339*0fca6ea1SDimitry Andric if (IntInit *II = dyn_cast<IntInit>(N.getChild(1).getLeafValue())) { 34006c3fb27SDimitry Andric if (!llvm::has_single_bit<uint32_t>( 34106c3fb27SDimitry Andric II->getValue())) { // Don't bother with single bits. 3420b57cec5SDimitry Andric // If this is at the root of the pattern, we emit a redundant 3430b57cec5SDimitry Andric // CheckOpcode so that the following checks get factored properly under 3440b57cec5SDimitry Andric // a single opcode check. 345*0fca6ea1SDimitry Andric if (&N == &Pattern.getSrcPattern()) 3460b57cec5SDimitry Andric AddMatcher(new CheckOpcodeMatcher(CInfo)); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Emit the CheckAndImm/CheckOrImm node. 349*0fca6ea1SDimitry Andric if (N.getOperator()->getName() == "and") 3500b57cec5SDimitry Andric AddMatcher(new CheckAndImmMatcher(II->getValue())); 3510b57cec5SDimitry Andric else 3520b57cec5SDimitry Andric AddMatcher(new CheckOrImmMatcher(II->getValue())); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Match the LHS of the AND as appropriate. 3550b57cec5SDimitry Andric AddMatcher(new MoveChildMatcher(0)); 356*0fca6ea1SDimitry Andric EmitMatchCode(N.getChild(0), NodeNoTypes.getChild(0)); 3570b57cec5SDimitry Andric AddMatcher(new MoveParentMatcher()); 3580b57cec5SDimitry Andric return; 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // Check that the current opcode lines up. 3640b57cec5SDimitry Andric AddMatcher(new CheckOpcodeMatcher(CInfo)); 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // If this node has memory references (i.e. is a load or store), tell the 3670b57cec5SDimitry Andric // interpreter to capture them in the memref array. 368*0fca6ea1SDimitry Andric if (N.NodeHasProperty(SDNPMemOperand, CGP)) 3690b57cec5SDimitry Andric AddMatcher(new RecordMemRefMatcher()); 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric // If this node has a chain, then the chain is operand #0 is the SDNode, and 3720b57cec5SDimitry Andric // the child numbers of the node are all offset by one. 3730b57cec5SDimitry Andric unsigned OpNo = 0; 374*0fca6ea1SDimitry Andric if (N.NodeHasProperty(SDNPHasChain, CGP)) { 3750b57cec5SDimitry Andric // Record the node and remember it in our chained nodes list. 376*0fca6ea1SDimitry Andric AddMatcher(new RecordMatcher("'" + N.getOperator()->getName().str() + 3770b57cec5SDimitry Andric "' chained node", 3780b57cec5SDimitry Andric NextRecordedOperandNo)); 3790b57cec5SDimitry Andric // Remember all of the input chains our pattern will match. 3800b57cec5SDimitry Andric MatchedChainNodes.push_back(NextRecordedOperandNo++); 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric // Don't look at the input chain when matching the tree pattern to the 3830b57cec5SDimitry Andric // SDNode. 3840b57cec5SDimitry Andric OpNo = 1; 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric // If this node is not the root and the subtree underneath it produces a 3870b57cec5SDimitry Andric // chain, then the result of matching the node is also produce a chain. 3880b57cec5SDimitry Andric // Beyond that, this means that we're also folding (at least) the root node 3890b57cec5SDimitry Andric // into the node that produce the chain (for example, matching 3900b57cec5SDimitry Andric // "(add reg, (load ptr))" as a add_with_memory on X86). This is 3910b57cec5SDimitry Andric // problematic, if the 'reg' node also uses the load (say, its chain). 3920b57cec5SDimitry Andric // Graphically: 3930b57cec5SDimitry Andric // 3940b57cec5SDimitry Andric // [LD] 3950b57cec5SDimitry Andric // ^ ^ 3960b57cec5SDimitry Andric // | \ DAG's like cheese. 3970b57cec5SDimitry Andric // / | 3980b57cec5SDimitry Andric // / [YY] 3990b57cec5SDimitry Andric // | ^ 4000b57cec5SDimitry Andric // [XX]--/ 4010b57cec5SDimitry Andric // 4020b57cec5SDimitry Andric // It would be invalid to fold XX and LD. In this case, folding the two 4030b57cec5SDimitry Andric // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG' 4040b57cec5SDimitry Andric // To prevent this, we emit a dynamic check for legality before allowing 4050b57cec5SDimitry Andric // this to be folded. 4060b57cec5SDimitry Andric // 407*0fca6ea1SDimitry Andric const TreePatternNode &Root = Pattern.getSrcPattern(); 408*0fca6ea1SDimitry Andric if (&N != &Root) { // Not the root of the pattern. 4090b57cec5SDimitry Andric // If there is a node between the root and this node, then we definitely 4100b57cec5SDimitry Andric // need to emit the check. 411*0fca6ea1SDimitry Andric bool NeedCheck = !Root.hasChild(&N); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // If it *is* an immediate child of the root, we can still need a check if 4140b57cec5SDimitry Andric // the root SDNode has multiple inputs. For us, this means that it is an 4150b57cec5SDimitry Andric // intrinsic, has multiple operands, or has other inputs like chain or 4160b57cec5SDimitry Andric // glue). 4170b57cec5SDimitry Andric if (!NeedCheck) { 418*0fca6ea1SDimitry Andric const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root.getOperator()); 4190b57cec5SDimitry Andric NeedCheck = 420*0fca6ea1SDimitry Andric Root.getOperator() == CGP.get_intrinsic_void_sdnode() || 421*0fca6ea1SDimitry Andric Root.getOperator() == CGP.get_intrinsic_w_chain_sdnode() || 422*0fca6ea1SDimitry Andric Root.getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || 423*0fca6ea1SDimitry Andric PInfo.getNumOperands() > 1 || PInfo.hasProperty(SDNPHasChain) || 424*0fca6ea1SDimitry Andric PInfo.hasProperty(SDNPInGlue) || PInfo.hasProperty(SDNPOptInGlue); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric if (NeedCheck) 4280b57cec5SDimitry Andric AddMatcher(new CheckFoldableChainNodeMatcher()); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric // If this node has an output glue and isn't the root, remember it. 433*0fca6ea1SDimitry Andric if (N.NodeHasProperty(SDNPOutGlue, CGP) && &N != &Pattern.getSrcPattern()) { 4340b57cec5SDimitry Andric // TODO: This redundantly records nodes with both glues and chains. 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric // Record the node and remember it in our chained nodes list. 437*0fca6ea1SDimitry Andric AddMatcher(new RecordMatcher("'" + N.getOperator()->getName().str() + 4380b57cec5SDimitry Andric "' glue output node", 4390b57cec5SDimitry Andric NextRecordedOperandNo)); 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric // If this node is known to have an input glue or if it *might* have an input 4430b57cec5SDimitry Andric // glue, capture it as the glue input of the pattern. 444*0fca6ea1SDimitry Andric if (N.NodeHasProperty(SDNPOptInGlue, CGP) || 445*0fca6ea1SDimitry Andric N.NodeHasProperty(SDNPInGlue, CGP)) 4460b57cec5SDimitry Andric AddMatcher(new CaptureGlueInputMatcher()); 4470b57cec5SDimitry Andric 448*0fca6ea1SDimitry Andric for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i, ++OpNo) { 4490b57cec5SDimitry Andric // Get the code suitable for matching this child. Move to the child, check 4500b57cec5SDimitry Andric // it then move back to the parent. 4510b57cec5SDimitry Andric AddMatcher(new MoveChildMatcher(OpNo)); 452*0fca6ea1SDimitry Andric EmitMatchCode(N.getChild(i), NodeNoTypes.getChild(i)); 4530b57cec5SDimitry Andric AddMatcher(new MoveParentMatcher()); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric bool MatcherGen::recordUniqueNode(ArrayRef<std::string> Names) { 4580b57cec5SDimitry Andric unsigned Entry = 0; 4590b57cec5SDimitry Andric for (const std::string &Name : Names) { 4600b57cec5SDimitry Andric unsigned &VarMapEntry = VariableMap[Name]; 4610b57cec5SDimitry Andric if (!Entry) 4620b57cec5SDimitry Andric Entry = VarMapEntry; 4630b57cec5SDimitry Andric assert(Entry == VarMapEntry); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric bool NewRecord = false; 4670b57cec5SDimitry Andric if (Entry == 0) { 4680b57cec5SDimitry Andric // If it is a named node, we must emit a 'Record' opcode. 4690b57cec5SDimitry Andric std::string WhatFor; 4700b57cec5SDimitry Andric for (const std::string &Name : Names) { 4710b57cec5SDimitry Andric if (!WhatFor.empty()) 4720b57cec5SDimitry Andric WhatFor += ','; 4730b57cec5SDimitry Andric WhatFor += "$" + Name; 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric AddMatcher(new RecordMatcher(WhatFor, NextRecordedOperandNo)); 4760b57cec5SDimitry Andric Entry = ++NextRecordedOperandNo; 4770b57cec5SDimitry Andric NewRecord = true; 4780b57cec5SDimitry Andric } else { 4790b57cec5SDimitry Andric // If we get here, this is a second reference to a specific name. Since 4800b57cec5SDimitry Andric // we already have checked that the first reference is valid, we don't 4810b57cec5SDimitry Andric // have to recursively match it, just check that it's the same as the 4820b57cec5SDimitry Andric // previously named thing. 4830b57cec5SDimitry Andric AddMatcher(new CheckSameMatcher(Entry - 1)); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric for (const std::string &Name : Names) 4870b57cec5SDimitry Andric VariableMap[Name] = Entry; 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric return NewRecord; 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric 492*0fca6ea1SDimitry Andric void MatcherGen::EmitMatchCode(const TreePatternNode &N, 493*0fca6ea1SDimitry Andric TreePatternNode &NodeNoTypes) { 4940b57cec5SDimitry Andric // If N and NodeNoTypes don't agree on a type, then this is a case where we 4950b57cec5SDimitry Andric // need to do a type check. Emit the check, apply the type to NodeNoTypes and 4960b57cec5SDimitry Andric // reinfer any correlated types. 4970b57cec5SDimitry Andric SmallVector<unsigned, 2> ResultsToTypeCheck; 4980b57cec5SDimitry Andric 499*0fca6ea1SDimitry Andric for (unsigned i = 0, e = NodeNoTypes.getNumTypes(); i != e; ++i) { 500*0fca6ea1SDimitry Andric if (NodeNoTypes.getExtType(i) == N.getExtType(i)) 501*0fca6ea1SDimitry Andric continue; 502*0fca6ea1SDimitry Andric NodeNoTypes.setType(i, N.getExtType(i)); 50306c3fb27SDimitry Andric InferPossibleTypes(); 5040b57cec5SDimitry Andric ResultsToTypeCheck.push_back(i); 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric // If this node has a name associated with it, capture it in VariableMap. If 5080b57cec5SDimitry Andric // we already saw this in the pattern, emit code to verify dagness. 5090b57cec5SDimitry Andric SmallVector<std::string, 4> Names; 510*0fca6ea1SDimitry Andric if (!N.getName().empty()) 511*0fca6ea1SDimitry Andric Names.push_back(N.getName()); 5120b57cec5SDimitry Andric 513*0fca6ea1SDimitry Andric for (const ScopedName &Name : N.getNamesAsPredicateArg()) { 514*0fca6ea1SDimitry Andric Names.push_back( 515*0fca6ea1SDimitry Andric ("pred:" + Twine(Name.getScope()) + ":" + Name.getIdentifier()).str()); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric if (!Names.empty()) { 5190b57cec5SDimitry Andric if (!recordUniqueNode(Names)) 5200b57cec5SDimitry Andric return; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 523*0fca6ea1SDimitry Andric if (N.isLeaf()) 5240b57cec5SDimitry Andric EmitLeafMatchCode(N); 5250b57cec5SDimitry Andric else 52606c3fb27SDimitry Andric EmitOperatorMatchCode(N, NodeNoTypes); 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // If there are node predicates for this node, generate their checks. 529*0fca6ea1SDimitry Andric for (unsigned i = 0, e = N.getPredicateCalls().size(); i != e; ++i) { 530*0fca6ea1SDimitry Andric const TreePredicateCall &Pred = N.getPredicateCalls()[i]; 5310b57cec5SDimitry Andric SmallVector<unsigned, 4> Operands; 5320b57cec5SDimitry Andric if (Pred.Fn.usesOperands()) { 5330b57cec5SDimitry Andric TreePattern *TP = Pred.Fn.getOrigPatFragRecord(); 5340b57cec5SDimitry Andric for (unsigned i = 0; i < TP->getNumArgs(); ++i) { 5350b57cec5SDimitry Andric std::string Name = 5360b57cec5SDimitry Andric ("pred:" + Twine(Pred.Scope) + ":" + TP->getArgName(i)).str(); 5370b57cec5SDimitry Andric Operands.push_back(getNamedArgumentSlot(Name)); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric AddMatcher(new CheckPredicateMatcher(Pred.Fn, Operands)); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i) 544*0fca6ea1SDimitry Andric AddMatcher(new CheckTypeMatcher(N.getSimpleType(ResultsToTypeCheck[i]), 5450b57cec5SDimitry Andric ResultsToTypeCheck[i])); 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric /// EmitMatcherCode - Generate the code that matches the predicate of this 5490b57cec5SDimitry Andric /// pattern for the specified Variant. If the variant is invalid this returns 5500b57cec5SDimitry Andric /// true and does not generate code, if it is valid, it returns false. 5510b57cec5SDimitry Andric bool MatcherGen::EmitMatcherCode(unsigned Variant) { 5520b57cec5SDimitry Andric // If the root of the pattern is a ComplexPattern and if it is specified to 5530b57cec5SDimitry Andric // match some number of root opcodes, these are considered to be our variants. 5540b57cec5SDimitry Andric // Depending on which variant we're generating code for, emit the root opcode 5550b57cec5SDimitry Andric // check. 5560b57cec5SDimitry Andric if (const ComplexPattern *CP = 557*0fca6ea1SDimitry Andric Pattern.getSrcPattern().getComplexPatternInfo(CGP)) { 5580b57cec5SDimitry Andric const std::vector<Record *> &OpNodes = CP->getRootNodes(); 559*0fca6ea1SDimitry Andric assert(!OpNodes.empty() && 560*0fca6ea1SDimitry Andric "Complex Pattern must specify what it can match"); 561*0fca6ea1SDimitry Andric if (Variant >= OpNodes.size()) 562*0fca6ea1SDimitry Andric return true; 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant]))); 5650b57cec5SDimitry Andric } else { 566*0fca6ea1SDimitry Andric if (Variant != 0) 567*0fca6ea1SDimitry Andric return true; 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric // Emit the matcher for the pattern structure and types. 571*0fca6ea1SDimitry Andric EmitMatchCode(Pattern.getSrcPattern(), *PatWithNoTypes); 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // If the pattern has a predicate on it (e.g. only enabled when a subtarget 5740b57cec5SDimitry Andric // feature is around, do the check). 57506c3fb27SDimitry Andric std::string PredicateCheck = Pattern.getPredicateCheck(); 57606c3fb27SDimitry Andric if (!PredicateCheck.empty()) 57706c3fb27SDimitry Andric AddMatcher(new CheckPatternPredicateMatcher(PredicateCheck)); 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric // Now that we've completed the structural type match, emit any ComplexPattern 5800b57cec5SDimitry Andric // checks (e.g. addrmode matches). We emit this after the structural match 5810b57cec5SDimitry Andric // because they are generally more expensive to evaluate and more difficult to 5820b57cec5SDimitry Andric // factor. 5830b57cec5SDimitry Andric for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) { 584*0fca6ea1SDimitry Andric auto &N = *MatchedComplexPatterns[i].first; 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric // Remember where the results of this match get stuck. 587*0fca6ea1SDimitry Andric if (N.isLeaf()) { 588*0fca6ea1SDimitry Andric NamedComplexPatternOperands[N.getName()] = NextRecordedOperandNo + 1; 5890b57cec5SDimitry Andric } else { 5900b57cec5SDimitry Andric unsigned CurOp = NextRecordedOperandNo; 591*0fca6ea1SDimitry Andric for (unsigned i = 0; i < N.getNumChildren(); ++i) { 592*0fca6ea1SDimitry Andric NamedComplexPatternOperands[N.getChild(i).getName()] = CurOp + 1; 593*0fca6ea1SDimitry Andric CurOp += N.getChild(i).getNumMIResults(CGP); 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric // Get the slot we recorded the value in from the name on the node. 5980b57cec5SDimitry Andric unsigned RecNodeEntry = MatchedComplexPatterns[i].second; 5990b57cec5SDimitry Andric 600*0fca6ea1SDimitry Andric const ComplexPattern *CP = N.getComplexPatternInfo(CGP); 60106c3fb27SDimitry Andric assert(CP && "Not a valid ComplexPattern!"); 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric // Emit a CheckComplexPat operation, which does the match (aborting if it 6040b57cec5SDimitry Andric // fails) and pushes the matched operands onto the recorded nodes list. 605*0fca6ea1SDimitry Andric AddMatcher(new CheckComplexPatMatcher(*CP, RecNodeEntry, N.getName(), 60606c3fb27SDimitry Andric NextRecordedOperandNo)); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // Record the right number of operands. 60906c3fb27SDimitry Andric NextRecordedOperandNo += CP->getNumOperands(); 61006c3fb27SDimitry Andric if (CP->hasProperty(SDNPHasChain)) { 6110b57cec5SDimitry Andric // If the complex pattern has a chain, then we need to keep track of the 6120b57cec5SDimitry Andric // fact that we just recorded a chain input. The chain input will be 6130b57cec5SDimitry Andric // matched as the last operand of the predicate if it was successful. 6140b57cec5SDimitry Andric ++NextRecordedOperandNo; // Chained node operand. 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric // It is the last operand recorded. 6170b57cec5SDimitry Andric assert(NextRecordedOperandNo > 1 && 6180b57cec5SDimitry Andric "Should have recorded input/result chains at least!"); 6190b57cec5SDimitry Andric MatchedChainNodes.push_back(NextRecordedOperandNo - 1); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric // TODO: Complex patterns can't have output glues, if they did, we'd want 6230b57cec5SDimitry Andric // to record them. 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric return false; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6300b57cec5SDimitry Andric // Node Result Generation 6310b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6320b57cec5SDimitry Andric 633*0fca6ea1SDimitry Andric void MatcherGen::EmitResultOfNamedOperand( 634*0fca6ea1SDimitry Andric const TreePatternNode &N, SmallVectorImpl<unsigned> &ResultOps) { 635*0fca6ea1SDimitry Andric assert(!N.getName().empty() && "Operand not named!"); 6360b57cec5SDimitry Andric 637*0fca6ea1SDimitry Andric if (unsigned SlotNo = NamedComplexPatternOperands[N.getName()]) { 6380b57cec5SDimitry Andric // Complex operands have already been completely selected, just find the 6390b57cec5SDimitry Andric // right slot ant add the arguments directly. 640*0fca6ea1SDimitry Andric for (unsigned i = 0; i < N.getNumMIResults(CGP); ++i) 6410b57cec5SDimitry Andric ResultOps.push_back(SlotNo - 1 + i); 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric return; 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 646*0fca6ea1SDimitry Andric unsigned SlotNo = getNamedArgumentSlot(N.getName()); 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target 6490b57cec5SDimitry Andric // version of the immediate so that it doesn't get selected due to some other 6500b57cec5SDimitry Andric // node use. 651*0fca6ea1SDimitry Andric if (!N.isLeaf()) { 652*0fca6ea1SDimitry Andric StringRef OperatorName = N.getOperator()->getName(); 6530b57cec5SDimitry Andric if (OperatorName == "imm" || OperatorName == "fpimm") { 6540b57cec5SDimitry Andric AddMatcher(new EmitConvertToTargetMatcher(SlotNo)); 6550b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 6560b57cec5SDimitry Andric return; 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 660*0fca6ea1SDimitry Andric for (unsigned i = 0; i < N.getNumMIResults(CGP); ++i) 6610b57cec5SDimitry Andric ResultOps.push_back(SlotNo + i); 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 664*0fca6ea1SDimitry Andric void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode &N, 6650b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps) { 666*0fca6ea1SDimitry Andric assert(N.isLeaf() && "Must be a leaf"); 6670b57cec5SDimitry Andric 668*0fca6ea1SDimitry Andric if (IntInit *II = dyn_cast<IntInit>(N.getLeafValue())) { 669*0fca6ea1SDimitry Andric AddMatcher(new EmitIntegerMatcher(II->getValue(), N.getSimpleType(0))); 6700b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 6710b57cec5SDimitry Andric return; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric // If this is an explicit register reference, handle it. 675*0fca6ea1SDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(N.getLeafValue())) { 6760b57cec5SDimitry Andric Record *Def = DI->getDef(); 6770b57cec5SDimitry Andric if (Def->isSubClassOf("Register")) { 678*0fca6ea1SDimitry Andric const CodeGenRegister *Reg = CGP.getTargetInfo().getRegBank().getReg(Def); 679*0fca6ea1SDimitry Andric AddMatcher(new EmitRegisterMatcher(Reg, N.getSimpleType(0))); 6800b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 6810b57cec5SDimitry Andric return; 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric if (Def->getName() == "zero_reg") { 685*0fca6ea1SDimitry Andric AddMatcher(new EmitRegisterMatcher(nullptr, N.getSimpleType(0))); 6860b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 6870b57cec5SDimitry Andric return; 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric if (Def->getName() == "undef_tied_input") { 691*0fca6ea1SDimitry Andric MVT::SimpleValueType ResultVT = N.getSimpleType(0); 6920b57cec5SDimitry Andric auto IDOperandNo = NextRecordedOperandNo++; 69306c3fb27SDimitry Andric Record *ImpDef = Def->getRecords().getDef("IMPLICIT_DEF"); 69406c3fb27SDimitry Andric CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(ImpDef); 69506c3fb27SDimitry Andric AddMatcher(new EmitNodeMatcher(II, ResultVT, std::nullopt, false, false, 69606c3fb27SDimitry Andric false, false, -1, IDOperandNo)); 6970b57cec5SDimitry Andric ResultOps.push_back(IDOperandNo); 6980b57cec5SDimitry Andric return; 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric // Handle a reference to a register class. This is used 7020b57cec5SDimitry Andric // in COPY_TO_SUBREG instructions. 7030b57cec5SDimitry Andric if (Def->isSubClassOf("RegisterOperand")) 7040b57cec5SDimitry Andric Def = Def->getValueAsDef("RegClass"); 7050b57cec5SDimitry Andric if (Def->isSubClassOf("RegisterClass")) { 7065ffd83dbSDimitry Andric // If the register class has an enum integer value greater than 127, the 7075ffd83dbSDimitry Andric // encoding overflows the limit of 7 bits, which precludes the use of 7085ffd83dbSDimitry Andric // StringIntegerMatcher. In this case, fallback to using IntegerMatcher. 7095ffd83dbSDimitry Andric const CodeGenRegisterClass &RC = 7105ffd83dbSDimitry Andric CGP.getTargetInfo().getRegisterClass(Def); 7115ffd83dbSDimitry Andric if (RC.EnumValue <= 127) { 7125f757f3fSDimitry Andric std::string Value = RC.getQualifiedIdName(); 7130b57cec5SDimitry Andric AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); 7140b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 7155ffd83dbSDimitry Andric } else { 7165ffd83dbSDimitry Andric AddMatcher(new EmitIntegerMatcher(RC.EnumValue, MVT::i32)); 7175ffd83dbSDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 7185ffd83dbSDimitry Andric } 7190b57cec5SDimitry Andric return; 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric // Handle a subregister index. This is used for INSERT_SUBREG etc. 7230b57cec5SDimitry Andric if (Def->isSubClassOf("SubRegIndex")) { 7245ffd83dbSDimitry Andric const CodeGenRegBank &RB = CGP.getTargetInfo().getRegBank(); 7255ffd83dbSDimitry Andric // If we have more than 127 subreg indices the encoding can overflow 7265ffd83dbSDimitry Andric // 7 bit and we cannot use StringInteger. 7275ffd83dbSDimitry Andric if (RB.getSubRegIndices().size() > 127) { 7285ffd83dbSDimitry Andric const CodeGenSubRegIndex *I = RB.findSubRegIdx(Def); 7295ffd83dbSDimitry Andric assert(I && "Cannot find subreg index by name!"); 7305ffd83dbSDimitry Andric if (I->EnumValue > 127) { 7315ffd83dbSDimitry Andric AddMatcher(new EmitIntegerMatcher(I->EnumValue, MVT::i32)); 7325ffd83dbSDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 7335ffd83dbSDimitry Andric return; 7345ffd83dbSDimitry Andric } 7355ffd83dbSDimitry Andric } 7360b57cec5SDimitry Andric std::string Value = getQualifiedName(Def); 7370b57cec5SDimitry Andric AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); 7380b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 7390b57cec5SDimitry Andric return; 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric errs() << "unhandled leaf node:\n"; 744*0fca6ea1SDimitry Andric N.dump(); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 747*0fca6ea1SDimitry Andric static bool mayInstNodeLoadOrStore(const TreePatternNode &N, 7480b57cec5SDimitry Andric const CodeGenDAGPatterns &CGP) { 749*0fca6ea1SDimitry Andric Record *Op = N.getOperator(); 7500b57cec5SDimitry Andric const CodeGenTarget &CGT = CGP.getTargetInfo(); 7510b57cec5SDimitry Andric CodeGenInstruction &II = CGT.getInstruction(Op); 7520b57cec5SDimitry Andric return II.mayLoad || II.mayStore; 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric 755*0fca6ea1SDimitry Andric static unsigned numNodesThatMayLoadOrStore(const TreePatternNode &N, 7560b57cec5SDimitry Andric const CodeGenDAGPatterns &CGP) { 757*0fca6ea1SDimitry Andric if (N.isLeaf()) 7580b57cec5SDimitry Andric return 0; 7590b57cec5SDimitry Andric 760*0fca6ea1SDimitry Andric Record *OpRec = N.getOperator(); 7610b57cec5SDimitry Andric if (!OpRec->isSubClassOf("Instruction")) 7620b57cec5SDimitry Andric return 0; 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric unsigned Count = 0; 7650b57cec5SDimitry Andric if (mayInstNodeLoadOrStore(N, CGP)) 7660b57cec5SDimitry Andric ++Count; 7670b57cec5SDimitry Andric 768*0fca6ea1SDimitry Andric for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i) 769*0fca6ea1SDimitry Andric Count += numNodesThatMayLoadOrStore(N.getChild(i), CGP); 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric return Count; 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 774*0fca6ea1SDimitry Andric void MatcherGen::EmitResultInstructionAsOperand( 775*0fca6ea1SDimitry Andric const TreePatternNode &N, SmallVectorImpl<unsigned> &OutputOps) { 776*0fca6ea1SDimitry Andric Record *Op = N.getOperator(); 7770b57cec5SDimitry Andric const CodeGenTarget &CGT = CGP.getTargetInfo(); 7780b57cec5SDimitry Andric CodeGenInstruction &II = CGT.getInstruction(Op); 7790b57cec5SDimitry Andric const DAGInstruction &Inst = CGP.getInstruction(Op); 7800b57cec5SDimitry Andric 781*0fca6ea1SDimitry Andric bool isRoot = &N == &Pattern.getDstPattern(); 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric // TreeHasOutGlue - True if this tree has glue. 7840b57cec5SDimitry Andric bool TreeHasInGlue = false, TreeHasOutGlue = false; 7850b57cec5SDimitry Andric if (isRoot) { 786*0fca6ea1SDimitry Andric const TreePatternNode &SrcPat = Pattern.getSrcPattern(); 787*0fca6ea1SDimitry Andric TreeHasInGlue = SrcPat.TreeHasProperty(SDNPOptInGlue, CGP) || 788*0fca6ea1SDimitry Andric SrcPat.TreeHasProperty(SDNPInGlue, CGP); 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // FIXME2: this is checking the entire pattern, not just the node in 7910b57cec5SDimitry Andric // question, doing this just for the root seems like a total hack. 792*0fca6ea1SDimitry Andric TreeHasOutGlue = SrcPat.TreeHasProperty(SDNPOutGlue, CGP); 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric // NumResults - This is the number of results produced by the instruction in 7960b57cec5SDimitry Andric // the "outs" list. 7970b57cec5SDimitry Andric unsigned NumResults = Inst.getNumResults(); 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric // Number of operands we know the output instruction must have. If it is 8000b57cec5SDimitry Andric // variadic, we could have more operands. 8010b57cec5SDimitry Andric unsigned NumFixedOperands = II.Operands.size(); 8020b57cec5SDimitry Andric 8030b57cec5SDimitry Andric SmallVector<unsigned, 8> InstOps; 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric // Loop over all of the fixed operands of the instruction pattern, emitting 8060b57cec5SDimitry Andric // code to fill them all in. The node 'N' usually has number children equal to 8070b57cec5SDimitry Andric // the number of input operands of the instruction. However, in cases where 8080b57cec5SDimitry Andric // there are predicate operands for an instruction, we need to fill in the 8090b57cec5SDimitry Andric // 'execute always' values. Match up the node operands to the instruction 8100b57cec5SDimitry Andric // operands to do this. 8110b57cec5SDimitry Andric unsigned ChildNo = 0; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric // Similarly to the code in TreePatternNode::ApplyTypeConstraints, count the 8140b57cec5SDimitry Andric // number of operands at the end of the list which have default values. 8150b57cec5SDimitry Andric // Those can come from the pattern if it provides enough arguments, or be 8160b57cec5SDimitry Andric // filled in with the default if the pattern hasn't provided them. But any 8170b57cec5SDimitry Andric // operand with a default value _before_ the last mandatory one will be 8180b57cec5SDimitry Andric // filled in with their defaults unconditionally. 8190b57cec5SDimitry Andric unsigned NonOverridableOperands = NumFixedOperands; 8200b57cec5SDimitry Andric while (NonOverridableOperands > NumResults && 8210b57cec5SDimitry Andric CGP.operandHasDefault(II.Operands[NonOverridableOperands - 1].Rec)) 8220b57cec5SDimitry Andric --NonOverridableOperands; 8230b57cec5SDimitry Andric 824*0fca6ea1SDimitry Andric for (unsigned InstOpNo = NumResults, e = NumFixedOperands; InstOpNo != e; 825*0fca6ea1SDimitry Andric ++InstOpNo) { 8260b57cec5SDimitry Andric // Determine what to emit for this operand. 8270b57cec5SDimitry Andric Record *OperandNode = II.Operands[InstOpNo].Rec; 8280b57cec5SDimitry Andric if (CGP.operandHasDefault(OperandNode) && 829*0fca6ea1SDimitry Andric (InstOpNo < NonOverridableOperands || ChildNo >= N.getNumChildren())) { 8300b57cec5SDimitry Andric // This is a predicate or optional def operand which the pattern has not 8310b57cec5SDimitry Andric // overridden, or which we aren't letting it override; emit the 'default 8320b57cec5SDimitry Andric // ops' operands. 833*0fca6ea1SDimitry Andric const DAGDefaultOperand &DefaultOp = CGP.getDefaultOperand(OperandNode); 8340b57cec5SDimitry Andric for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) 835*0fca6ea1SDimitry Andric EmitResultOperand(*DefaultOp.DefaultOps[i], InstOps); 8360b57cec5SDimitry Andric continue; 8370b57cec5SDimitry Andric } 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric // Otherwise this is a normal operand or a predicate operand without 8400b57cec5SDimitry Andric // 'execute always'; emit it. 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric // For operands with multiple sub-operands we may need to emit 8430b57cec5SDimitry Andric // multiple child patterns to cover them all. However, ComplexPattern 8440b57cec5SDimitry Andric // children may themselves emit multiple MI operands. 8450b57cec5SDimitry Andric unsigned NumSubOps = 1; 8460b57cec5SDimitry Andric if (OperandNode->isSubClassOf("Operand")) { 8470b57cec5SDimitry Andric DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); 8480b57cec5SDimitry Andric if (unsigned NumArgs = MIOpInfo->getNumArgs()) 8490b57cec5SDimitry Andric NumSubOps = NumArgs; 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric unsigned FinalNumOps = InstOps.size() + NumSubOps; 8530b57cec5SDimitry Andric while (InstOps.size() < FinalNumOps) { 854*0fca6ea1SDimitry Andric const TreePatternNode &Child = N.getChild(ChildNo); 8550b57cec5SDimitry Andric unsigned BeforeAddingNumOps = InstOps.size(); 8560b57cec5SDimitry Andric EmitResultOperand(Child, InstOps); 8570b57cec5SDimitry Andric assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands"); 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric // If the operand is an instruction and it produced multiple results, just 8600b57cec5SDimitry Andric // take the first one. 861*0fca6ea1SDimitry Andric if (!Child.isLeaf() && Child.getOperator()->isSubClassOf("Instruction")) 8620b57cec5SDimitry Andric InstOps.resize(BeforeAddingNumOps + 1); 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric ++ChildNo; 8650b57cec5SDimitry Andric } 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric // If this is a variadic output instruction (i.e. REG_SEQUENCE), we can't 8690b57cec5SDimitry Andric // expand suboperands, use default operands, or other features determined from 8700b57cec5SDimitry Andric // the CodeGenInstruction after the fixed operands, which were handled 8710b57cec5SDimitry Andric // above. Emit the remaining instructions implicitly added by the use for 8720b57cec5SDimitry Andric // variable_ops. 8730b57cec5SDimitry Andric if (II.Operands.isVariadic) { 874*0fca6ea1SDimitry Andric for (unsigned I = ChildNo, E = N.getNumChildren(); I < E; ++I) 875*0fca6ea1SDimitry Andric EmitResultOperand(N.getChild(I), InstOps); 8760b57cec5SDimitry Andric } 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric // If this node has input glue or explicitly specified input physregs, we 8790b57cec5SDimitry Andric // need to add chained and glued copyfromreg nodes and materialize the glue 8800b57cec5SDimitry Andric // input. 8810b57cec5SDimitry Andric if (isRoot && !PhysRegInputs.empty()) { 8820b57cec5SDimitry Andric // Emit all of the CopyToReg nodes for the input physical registers. These 8830b57cec5SDimitry Andric // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src). 8848bcb0991SDimitry Andric for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i) { 8858bcb0991SDimitry Andric const CodeGenRegister *Reg = 8868bcb0991SDimitry Andric CGP.getTargetInfo().getRegBank().getReg(PhysRegInputs[i].first); 887*0fca6ea1SDimitry Andric AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second, Reg)); 8888bcb0991SDimitry Andric } 8898bcb0991SDimitry Andric 8900b57cec5SDimitry Andric // Even if the node has no other glue inputs, the resultant node must be 8910b57cec5SDimitry Andric // glued to the CopyFromReg nodes we just generated. 8920b57cec5SDimitry Andric TreeHasInGlue = true; 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric // Result order: node results, chain, glue 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric // Determine the result types. 8980b57cec5SDimitry Andric SmallVector<MVT::SimpleValueType, 4> ResultVTs; 899*0fca6ea1SDimitry Andric for (unsigned i = 0, e = N.getNumTypes(); i != e; ++i) 900*0fca6ea1SDimitry Andric ResultVTs.push_back(N.getSimpleType(i)); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric // If this is the root instruction of a pattern that has physical registers in 9030b57cec5SDimitry Andric // its result pattern, add output VTs for them. For example, X86 has: 9040b57cec5SDimitry Andric // (set AL, (mul ...)) 9050b57cec5SDimitry Andric // This also handles implicit results like: 9060b57cec5SDimitry Andric // (implicit EFLAGS) 9070b57cec5SDimitry Andric if (isRoot && !Pattern.getDstRegs().empty()) { 9080b57cec5SDimitry Andric // If the root came from an implicit def in the instruction handling stuff, 9090b57cec5SDimitry Andric // don't re-add it. 9100b57cec5SDimitry Andric Record *HandledReg = nullptr; 9110b57cec5SDimitry Andric if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) 9120b57cec5SDimitry Andric HandledReg = II.ImplicitDefs[0]; 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric for (Record *Reg : Pattern.getDstRegs()) { 915*0fca6ea1SDimitry Andric if (!Reg->isSubClassOf("Register") || Reg == HandledReg) 916*0fca6ea1SDimitry Andric continue; 9170b57cec5SDimitry Andric ResultVTs.push_back(getRegisterValueType(Reg, CGT)); 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric // If this is the root of the pattern and the pattern we're matching includes 9220b57cec5SDimitry Andric // a node that is variadic, mark the generated node as variadic so that it 9230b57cec5SDimitry Andric // gets the excess operands from the input DAG. 9240b57cec5SDimitry Andric int NumFixedArityOperands = -1; 925*0fca6ea1SDimitry Andric if (isRoot && Pattern.getSrcPattern().NodeHasProperty(SDNPVariadic, CGP)) 926*0fca6ea1SDimitry Andric NumFixedArityOperands = Pattern.getSrcPattern().getNumChildren(); 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric // If this is the root node and multiple matched nodes in the input pattern 9290b57cec5SDimitry Andric // have MemRefs in them, have the interpreter collect them and plop them onto 9300b57cec5SDimitry Andric // this node. If there is just one node with MemRefs, leave them on that node 9310b57cec5SDimitry Andric // even if it is not the root. 9320b57cec5SDimitry Andric // 9330b57cec5SDimitry Andric // FIXME3: This is actively incorrect for result patterns with multiple 9340b57cec5SDimitry Andric // memory-referencing instructions. 9350b57cec5SDimitry Andric bool PatternHasMemOperands = 936*0fca6ea1SDimitry Andric Pattern.getSrcPattern().TreeHasProperty(SDNPMemOperand, CGP); 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric bool NodeHasMemRefs = false; 9390b57cec5SDimitry Andric if (PatternHasMemOperands) { 9400b57cec5SDimitry Andric unsigned NumNodesThatLoadOrStore = 9410b57cec5SDimitry Andric numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP); 942*0fca6ea1SDimitry Andric bool NodeIsUniqueLoadOrStore = 943*0fca6ea1SDimitry Andric mayInstNodeLoadOrStore(N, CGP) && NumNodesThatLoadOrStore == 1; 9440b57cec5SDimitry Andric NodeHasMemRefs = 9450b57cec5SDimitry Andric NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) || 9460b57cec5SDimitry Andric NumNodesThatLoadOrStore != 1)); 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric // Determine whether we need to attach a chain to this node. 9500b57cec5SDimitry Andric bool NodeHasChain = false; 951*0fca6ea1SDimitry Andric if (Pattern.getSrcPattern().TreeHasProperty(SDNPHasChain, CGP)) { 9520b57cec5SDimitry Andric // For some instructions, we were able to infer from the pattern whether 9530b57cec5SDimitry Andric // they should have a chain. Otherwise, attach the chain to the root. 9540b57cec5SDimitry Andric // 9550b57cec5SDimitry Andric // FIXME2: This is extremely dubious for several reasons, not the least of 9560b57cec5SDimitry Andric // which it gives special status to instructions with patterns that Pat<> 9570b57cec5SDimitry Andric // nodes can't duplicate. 9580b57cec5SDimitry Andric if (II.hasChain_Inferred) 9590b57cec5SDimitry Andric NodeHasChain = II.hasChain; 9600b57cec5SDimitry Andric else 9610b57cec5SDimitry Andric NodeHasChain = isRoot; 9620b57cec5SDimitry Andric // Instructions which load and store from memory should have a chain, 9630b57cec5SDimitry Andric // regardless of whether they happen to have a pattern saying so. 9640b57cec5SDimitry Andric if (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad || 9650b57cec5SDimitry Andric II.hasSideEffects) 9660b57cec5SDimitry Andric NodeHasChain = true; 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) && 9700b57cec5SDimitry Andric "Node has no result"); 9710b57cec5SDimitry Andric 97206c3fb27SDimitry Andric AddMatcher(new EmitNodeMatcher(II, ResultVTs, InstOps, NodeHasChain, 97306c3fb27SDimitry Andric TreeHasInGlue, TreeHasOutGlue, NodeHasMemRefs, 97406c3fb27SDimitry Andric NumFixedArityOperands, NextRecordedOperandNo)); 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric // The non-chain and non-glue results of the newly emitted node get recorded. 9770b57cec5SDimitry Andric for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) { 978*0fca6ea1SDimitry Andric if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) 979*0fca6ea1SDimitry Andric break; 9800b57cec5SDimitry Andric OutputOps.push_back(NextRecordedOperandNo++); 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric 984*0fca6ea1SDimitry Andric void MatcherGen::EmitResultSDNodeXFormAsOperand( 985*0fca6ea1SDimitry Andric const TreePatternNode &N, SmallVectorImpl<unsigned> &ResultOps) { 986*0fca6ea1SDimitry Andric assert(N.getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?"); 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric // Emit the operand. 9890b57cec5SDimitry Andric SmallVector<unsigned, 8> InputOps; 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric // FIXME2: Could easily generalize this to support multiple inputs and outputs 9920b57cec5SDimitry Andric // to the SDNodeXForm. For now we just support one input and one output like 9930b57cec5SDimitry Andric // the old instruction selector. 994*0fca6ea1SDimitry Andric assert(N.getNumChildren() == 1); 995*0fca6ea1SDimitry Andric EmitResultOperand(N.getChild(0), InputOps); 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric // The input currently must have produced exactly one result. 9980b57cec5SDimitry Andric assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm"); 9990b57cec5SDimitry Andric 1000*0fca6ea1SDimitry Andric AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N.getOperator())); 10010b57cec5SDimitry Andric ResultOps.push_back(NextRecordedOperandNo++); 10020b57cec5SDimitry Andric } 10030b57cec5SDimitry Andric 1004*0fca6ea1SDimitry Andric void MatcherGen::EmitResultOperand(const TreePatternNode &N, 10050b57cec5SDimitry Andric SmallVectorImpl<unsigned> &ResultOps) { 10060b57cec5SDimitry Andric // This is something selected from the pattern we matched. 1007*0fca6ea1SDimitry Andric if (!N.getName().empty()) 10080b57cec5SDimitry Andric return EmitResultOfNamedOperand(N, ResultOps); 10090b57cec5SDimitry Andric 1010*0fca6ea1SDimitry Andric if (N.isLeaf()) 10110b57cec5SDimitry Andric return EmitResultLeafAsOperand(N, ResultOps); 10120b57cec5SDimitry Andric 1013*0fca6ea1SDimitry Andric Record *OpRec = N.getOperator(); 10140b57cec5SDimitry Andric if (OpRec->isSubClassOf("Instruction")) 10150b57cec5SDimitry Andric return EmitResultInstructionAsOperand(N, ResultOps); 10160b57cec5SDimitry Andric if (OpRec->isSubClassOf("SDNodeXForm")) 10170b57cec5SDimitry Andric return EmitResultSDNodeXFormAsOperand(N, ResultOps); 1018*0fca6ea1SDimitry Andric errs() << "Unknown result node to emit code for: " << N << '\n'; 10190b57cec5SDimitry Andric PrintFatalError("Unknown node in result pattern!"); 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric void MatcherGen::EmitResultCode() { 10230b57cec5SDimitry Andric // Patterns that match nodes with (potentially multiple) chain inputs have to 10240b57cec5SDimitry Andric // merge them together into a token factor. This informs the generated code 10250b57cec5SDimitry Andric // what all the chained nodes are. 10260b57cec5SDimitry Andric if (!MatchedChainNodes.empty()) 10270b57cec5SDimitry Andric AddMatcher(new EmitMergeInputChainsMatcher(MatchedChainNodes)); 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric // Codegen the root of the result pattern, capturing the resulting values. 10300b57cec5SDimitry Andric SmallVector<unsigned, 8> Ops; 10310b57cec5SDimitry Andric EmitResultOperand(Pattern.getDstPattern(), Ops); 10320b57cec5SDimitry Andric 10330b57cec5SDimitry Andric // At this point, we have however many values the result pattern produces. 10340b57cec5SDimitry Andric // However, the input pattern might not need all of these. If there are 10350b57cec5SDimitry Andric // excess values at the end (such as implicit defs of condition codes etc) 10360b57cec5SDimitry Andric // just lop them off. This doesn't need to worry about glue or chains, just 10370b57cec5SDimitry Andric // explicit results. 10380b57cec5SDimitry Andric // 1039*0fca6ea1SDimitry Andric unsigned NumSrcResults = Pattern.getSrcPattern().getNumTypes(); 10400b57cec5SDimitry Andric 10410b57cec5SDimitry Andric // If the pattern also has (implicit) results, count them as well. 10420b57cec5SDimitry Andric if (!Pattern.getDstRegs().empty()) { 10430b57cec5SDimitry Andric // If the root came from an implicit def in the instruction handling stuff, 10440b57cec5SDimitry Andric // don't re-add it. 10450b57cec5SDimitry Andric Record *HandledReg = nullptr; 1046*0fca6ea1SDimitry Andric const TreePatternNode &DstPat = Pattern.getDstPattern(); 1047*0fca6ea1SDimitry Andric if (!DstPat.isLeaf() && DstPat.getOperator()->isSubClassOf("Instruction")) { 10480b57cec5SDimitry Andric const CodeGenTarget &CGT = CGP.getTargetInfo(); 1049*0fca6ea1SDimitry Andric CodeGenInstruction &II = CGT.getInstruction(DstPat.getOperator()); 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andric if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) 10520b57cec5SDimitry Andric HandledReg = II.ImplicitDefs[0]; 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric for (Record *Reg : Pattern.getDstRegs()) { 1056*0fca6ea1SDimitry Andric if (!Reg->isSubClassOf("Register") || Reg == HandledReg) 1057*0fca6ea1SDimitry Andric continue; 10580b57cec5SDimitry Andric ++NumSrcResults; 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric } 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric SmallVector<unsigned, 8> Results(Ops); 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric // Apply result permutation. 1065*0fca6ea1SDimitry Andric for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern().getNumResults(); 10660b57cec5SDimitry Andric ++ResNo) { 1067*0fca6ea1SDimitry Andric Results[ResNo] = Ops[Pattern.getDstPattern().getResultIndex(ResNo)]; 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric Results.resize(NumSrcResults); 10710b57cec5SDimitry Andric AddMatcher(new CompleteMatchMatcher(Results, Pattern)); 10720b57cec5SDimitry Andric } 10730b57cec5SDimitry Andric 10740b57cec5SDimitry Andric /// ConvertPatternToMatcher - Create the matcher for the specified pattern with 10750b57cec5SDimitry Andric /// the specified variant. If the variant number is invalid, this returns null. 10760b57cec5SDimitry Andric Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern, 10770b57cec5SDimitry Andric unsigned Variant, 10780b57cec5SDimitry Andric const CodeGenDAGPatterns &CGP) { 10790b57cec5SDimitry Andric MatcherGen Gen(Pattern, CGP); 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric // Generate the code for the matcher. 10820b57cec5SDimitry Andric if (Gen.EmitMatcherCode(Variant)) 10830b57cec5SDimitry Andric return nullptr; 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence. 10860b57cec5SDimitry Andric // FIXME2: Split result code out to another table, and make the matcher end 10870b57cec5SDimitry Andric // with an "Emit <index>" command. This allows result generation stuff to be 10880b57cec5SDimitry Andric // shared and factored? 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric // If the match succeeds, then we generate Pattern. 10910b57cec5SDimitry Andric Gen.EmitResultCode(); 10920b57cec5SDimitry Andric 10930b57cec5SDimitry Andric // Unconditional match. 10940b57cec5SDimitry Andric return Gen.GetMatcher(); 10950b57cec5SDimitry Andric } 1096