1 //==-llvm/CodeGen/DAGISelHeader.h - Common DAG ISel definitions -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides definitions of the common, target-independent methods and
11 // data, which is used by SelectionDAG-based instruction selectors.
13 // *** NOTE: This file is #included into the middle of the target
14 // instruction selector class. These functions are really methods.
15 // This is a little awkward, but it allows this code to be shared
16 // by all the targets while still being able to call into
17 // target-specific code without using a virtual function call.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CODEGEN_DAGISEL_HEADER_H
22 #define LLVM_CODEGEN_DAGISEL_HEADER_H
24 /// ISelPosition - Node iterator marking the current position of
25 /// instruction selection as it procedes through the topologically-sorted
27 SelectionDAG::allnodes_iterator ISelPosition;
29 /// ChainNotReachable - Returns true if Chain does not reach Op.
30 static bool ChainNotReachable(SDNode *Chain, SDNode *Op) {
31 if (Chain->getOpcode() == ISD::EntryToken)
33 if (Chain->getOpcode() == ISD::TokenFactor)
35 if (Chain->getNumOperands() > 0) {
36 SDValue C0 = Chain->getOperand(0);
37 if (C0.getValueType() == MVT::Other)
38 return C0.getNode() != Op && ChainNotReachable(C0.getNode(), Op);
43 /// IsChainCompatible - Returns true if Chain is Op or Chain does not reach Op.
44 /// This is used to ensure that there are no nodes trapped between Chain, which
45 /// is the first chain node discovered in a pattern and Op, a later node, that
46 /// will not be selected into the pattern.
47 static bool IsChainCompatible(SDNode *Chain, SDNode *Op) {
48 return Chain == Op || ChainNotReachable(Chain, Op);
52 /// ISelUpdater - helper class to handle updates of the
53 /// instruciton selection graph.
54 class VISIBILITY_HIDDEN ISelUpdater : public SelectionDAG::DAGUpdateListener {
55 SelectionDAG::allnodes_iterator &ISelPosition;
57 explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
58 : ISelPosition(isp) {}
60 /// NodeDeleted - Handle nodes deleted from the graph. If the
61 /// node being deleted is the current ISelPosition node, update
64 virtual void NodeDeleted(SDNode *N, SDNode *E) {
65 if (ISelPosition == SelectionDAG::allnodes_iterator(N))
69 /// NodeUpdated - Ignore updates for now.
70 virtual void NodeUpdated(SDNode *N) {}
73 /// ReplaceUses - replace all uses of the old node F with the use
74 /// of the new node T.
75 DISABLE_INLINE void ReplaceUses(SDValue F, SDValue T) {
76 ISelUpdater ISU(ISelPosition);
77 CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISU);
80 /// ReplaceUses - replace all uses of the old nodes F with the use
81 /// of the new nodes T.
82 DISABLE_INLINE void ReplaceUses(const SDValue *F, const SDValue *T,
84 ISelUpdater ISU(ISelPosition);
85 CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num, &ISU);
88 /// ReplaceUses - replace all uses of the old node F with the use
89 /// of the new node T.
90 DISABLE_INLINE void ReplaceUses(SDNode *F, SDNode *T) {
91 ISelUpdater ISU(ISelPosition);
92 CurDAG->ReplaceAllUsesWith(F, T, &ISU);
95 /// SelectRoot - Top level entry to DAG instruction selector.
96 /// Selects instructions starting at the root of the current DAG.
97 void SelectRoot(SelectionDAG &DAG) {
100 // Create a dummy node (which is not added to allnodes), that adds
101 // a reference to the root node, preventing it from being deleted,
102 // and tracking any changes of the root.
103 HandleSDNode Dummy(CurDAG->getRoot());
104 ISelPosition = SelectionDAG::allnodes_iterator(CurDAG->getRoot().getNode());
107 // The AllNodes list is now topological-sorted. Visit the
108 // nodes by starting at the end of the list (the root of the
109 // graph) and preceding back toward the beginning (the entry
111 while (ISelPosition != CurDAG->allnodes_begin()) {
112 SDNode *Node = --ISelPosition;
113 // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
114 // but there are currently some corner cases that it misses. Also, this
115 // makes it theoretically possible to disable the DAGCombiner.
116 if (Node->use_empty())
119 SDNode *ResNode = Select(Node);
120 // If node should not be replaced, continue with the next one.
125 ReplaceUses(Node, ResNode);
127 // If after the replacement this node is not used any more,
128 // remove this dead node.
129 if (Node->use_empty()) { // Don't delete EntryToken, etc.
130 ISelUpdater ISU(ISelPosition);
131 CurDAG->RemoveDeadNode(Node, &ISU);
135 CurDAG->setRoot(Dummy.getValue());
139 /// CheckInteger - Return true if the specified node is not a ConstantSDNode or
140 /// if it doesn't have the specified value.
141 static bool CheckInteger(SDValue V, int64_t Val) {
142 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
143 return C == 0 || C->getSExtValue() != Val;
146 /// CheckAndImmediate - Check to see if the specified node is an and with an
147 /// immediate returning true on failure.
149 /// FIXME: Inline this gunk into CheckAndMask.
150 bool CheckAndImmediate(SDValue V, int64_t Val) {
151 if (V->getOpcode() == ISD::AND)
152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(V->getOperand(1)))
153 if (CheckAndMask(V.getOperand(0), C, Val))
158 /// CheckOrImmediate - Check to see if the specified node is an or with an
159 /// immediate returning true on failure.
161 /// FIXME: Inline this gunk into CheckOrMask.
162 bool CheckOrImmediate(SDValue V, int64_t Val) {
163 if (V->getOpcode() == ISD::OR)
164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(V->getOperand(1)))
165 if (CheckOrMask(V.getOperand(0), C, Val))
170 void EmitInteger(int64_t Val, MVT::SimpleValueType VT,
171 SmallVectorImpl<SDValue> &RecordedNodes) {
172 RecordedNodes.push_back(CurDAG->getTargetConstant(Val, VT));
175 // These functions are marked always inline so that Idx doesn't get pinned to
177 ALWAYS_INLINE static int8_t
178 GetInt1(const unsigned char *MatcherTable, unsigned &Idx) {
179 return MatcherTable[Idx++];
182 ALWAYS_INLINE static int16_t
183 GetInt2(const unsigned char *MatcherTable, unsigned &Idx) {
184 int16_t Val = (uint8_t)GetInt1(MatcherTable, Idx);
185 Val |= int16_t(GetInt1(MatcherTable, Idx)) << 8;
189 ALWAYS_INLINE static int32_t
190 GetInt4(const unsigned char *MatcherTable, unsigned &Idx) {
191 int32_t Val = (uint16_t)GetInt2(MatcherTable, Idx);
192 Val |= int32_t(GetInt2(MatcherTable, Idx)) << 16;
196 ALWAYS_INLINE static int64_t
197 GetInt8(const unsigned char *MatcherTable, unsigned &Idx) {
198 int64_t Val = (uint32_t)GetInt4(MatcherTable, Idx);
199 Val |= int64_t(GetInt4(MatcherTable, Idx)) << 32;
203 enum BuiltinOpcodes {
207 OPC_CaptureFlagInput,
211 OPC_CheckPatternPredicate,
215 OPC_CheckInteger1, OPC_CheckInteger2, OPC_CheckInteger4, OPC_CheckInteger8,
219 OPC_CheckAndImm1, OPC_CheckAndImm2, OPC_CheckAndImm4, OPC_CheckAndImm8,
220 OPC_CheckOrImm1, OPC_CheckOrImm2, OPC_CheckOrImm4, OPC_CheckOrImm8,
221 OPC_CheckFoldableChainNode,
222 OPC_CheckChainCompatible,
224 OPC_EmitInteger1, OPC_EmitInteger2, OPC_EmitInteger4, OPC_EmitInteger8,
226 OPC_EmitConvertToTarget,
227 OPC_EmitMergeInputChains,
235 OPFL_None = 0, // Node has no chain or flag input and isn't variadic.
236 OPFL_Chain = 1, // Node has a chain input.
237 OPFL_Flag = 2, // Node has a flag input.
238 OPFL_MemRefs = 4, // Node gets accumulated MemRefs.
239 OPFL_Variadic0 = 1<<3, // Node is variadic, root has 0 fixed inputs.
240 OPFL_Variadic1 = 2<<3, // Node is variadic, root has 1 fixed inputs.
241 OPFL_Variadic2 = 3<<3, // Node is variadic, root has 2 fixed inputs.
242 OPFL_Variadic3 = 4<<3, // Node is variadic, root has 3 fixed inputs.
243 OPFL_Variadic4 = 5<<3, // Node is variadic, root has 4 fixed inputs.
244 OPFL_Variadic5 = 6<<3, // Node is variadic, root has 5 fixed inputs.
245 OPFL_Variadic6 = 7<<3, // Node is variadic, root has 6 fixed inputs.
247 OPFL_VariadicInfo = OPFL_Variadic6
250 /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
251 /// number of fixed arity values that should be skipped when copying from the
253 static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
254 return ((Flags&OPFL_VariadicInfo) >> 3)-1;
258 /// FailIndex - If this match fails, this is the index to continue with.
261 /// NodeStackSize - The size of the node stack when the scope was formed.
262 unsigned NodeStackSize;
264 /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
265 unsigned NumRecordedNodes;
267 /// NumMatchedMemRefs - The number of matched memref entries.
268 unsigned NumMatchedMemRefs;
270 /// InputChain/InputFlag - The current chain/flag
271 SDValue InputChain, InputFlag;
273 /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
274 bool HasChainNodesMatched;
277 SDNode *SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
278 unsigned TableSize) {
279 // FIXME: Should these even be selected? Handle these cases in the caller?
280 switch (NodeToMatch->getOpcode()) {
283 case ISD::EntryToken: // These nodes remain the same.
284 case ISD::BasicBlock:
286 case ISD::HANDLENODE:
287 case ISD::TargetConstant:
288 case ISD::TargetConstantFP:
289 case ISD::TargetConstantPool:
290 case ISD::TargetFrameIndex:
291 case ISD::TargetExternalSymbol:
292 case ISD::TargetBlockAddress:
293 case ISD::TargetJumpTable:
294 case ISD::TargetGlobalTLSAddress:
295 case ISD::TargetGlobalAddress:
296 case ISD::TokenFactor:
297 case ISD::CopyFromReg:
300 case ISD::AssertSext:
301 case ISD::AssertZext:
302 ReplaceUses(SDValue(NodeToMatch, 0), NodeToMatch->getOperand(0));
304 case ISD::INLINEASM: return Select_INLINEASM(NodeToMatch);
305 case ISD::EH_LABEL: return Select_EH_LABEL(NodeToMatch);
306 case ISD::UNDEF: return Select_UNDEF(NodeToMatch);
309 assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
311 // Set up the node stack with NodeToMatch as the only node on the stack.
312 SmallVector<SDValue, 8> NodeStack;
313 SDValue N = SDValue(NodeToMatch, 0);
314 NodeStack.push_back(N);
316 // MatchScopes - Scopes used when matching, if a match failure happens, this
317 // indicates where to continue checking.
318 SmallVector<MatchScope, 8> MatchScopes;
320 // RecordedNodes - This is the set of nodes that have been recorded by the
322 SmallVector<SDValue, 8> RecordedNodes;
324 // MatchedMemRefs - This is the set of MemRef's we've seen in the input
326 SmallVector<MachineMemOperand*, 2> MatchedMemRefs;
328 // These are the current input chain and flag for use when generating nodes.
329 // Various Emit operations change these. For example, emitting a copytoreg
330 // uses and updates these.
331 SDValue InputChain, InputFlag;
333 // ChainNodesMatched - If a pattern matches nodes that have input/output
334 // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
335 // which ones they are. The result is captured into this list so that we can
336 // update the chain results when the pattern is complete.
337 SmallVector<SDNode*, 3> ChainNodesMatched;
339 DEBUG(errs() << "ISEL: Starting pattern match on root node: ";
340 NodeToMatch->dump(CurDAG);
343 // Interpreter starts at opcode #0.
344 unsigned MatcherIndex = 0;
346 assert(MatcherIndex < TableSize && "Invalid index");
347 switch ((BuiltinOpcodes)MatcherTable[MatcherIndex++]) {
349 unsigned NumToSkip = MatcherTable[MatcherIndex++];
351 NewEntry.FailIndex = MatcherIndex+NumToSkip;
352 NewEntry.NodeStackSize = NodeStack.size();
353 NewEntry.NumRecordedNodes = RecordedNodes.size();
354 NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
355 NewEntry.InputChain = InputChain;
356 NewEntry.InputFlag = InputFlag;
357 NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
358 MatchScopes.push_back(NewEntry);
362 // Remember this node, it may end up being an operand in the pattern.
363 RecordedNodes.push_back(N);
365 case OPC_RecordMemRef:
366 MatchedMemRefs.push_back(cast<MemSDNode>(N)->getMemOperand());
369 case OPC_CaptureFlagInput:
370 // If the current node has an input flag, capture it in InputFlag.
371 if (N->getNumOperands() != 0 &&
372 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag)
373 InputFlag = N->getOperand(N->getNumOperands()-1);
376 case OPC_MoveChild: {
377 unsigned Child = MatcherTable[MatcherIndex++];
378 if (Child >= N.getNumOperands())
379 break; // Match fails if out of range child #.
380 N = N.getOperand(Child);
381 NodeStack.push_back(N);
386 // Pop the current node off the NodeStack.
387 NodeStack.pop_back();
388 assert(!NodeStack.empty() && "Node stack imbalance!");
389 N = NodeStack.back();
392 case OPC_CheckSame: {
393 // Accept if it is exactly the same as a previously recorded node.
394 unsigned RecNo = MatcherTable[MatcherIndex++];
395 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
396 if (N != RecordedNodes[RecNo]) break;
399 case OPC_CheckPatternPredicate:
400 if (!CheckPatternPredicate(MatcherTable[MatcherIndex++])) break;
402 case OPC_CheckPredicate:
403 if (!CheckNodePredicate(N.getNode(), MatcherTable[MatcherIndex++])) break;
405 case OPC_CheckComplexPat:
406 if (!CheckComplexPattern(NodeToMatch, N,
407 MatcherTable[MatcherIndex++], RecordedNodes))
410 case OPC_CheckOpcode:
411 if (N->getOpcode() != MatcherTable[MatcherIndex++]) break;
413 case OPC_CheckType: {
414 MVT::SimpleValueType VT =
415 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
416 if (N.getValueType() != VT) {
417 // Handle the case when VT is iPTR.
418 if (VT != MVT::iPTR || N.getValueType() != TLI.getPointerTy())
423 case OPC_CheckCondCode:
424 if (cast<CondCodeSDNode>(N)->get() !=
425 (ISD::CondCode)MatcherTable[MatcherIndex++]) break;
427 case OPC_CheckValueType: {
428 MVT::SimpleValueType VT =
429 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
430 if (cast<VTSDNode>(N)->getVT() != VT) {
431 // Handle the case when VT is iPTR.
432 if (VT != MVT::iPTR || cast<VTSDNode>(N)->getVT() != TLI.getPointerTy())
437 case OPC_CheckInteger1:
438 if (CheckInteger(N, GetInt1(MatcherTable, MatcherIndex))) break;
440 case OPC_CheckInteger2:
441 if (CheckInteger(N, GetInt2(MatcherTable, MatcherIndex))) break;
443 case OPC_CheckInteger4:
444 if (CheckInteger(N, GetInt4(MatcherTable, MatcherIndex))) break;
446 case OPC_CheckInteger8:
447 if (CheckInteger(N, GetInt8(MatcherTable, MatcherIndex))) break;
450 case OPC_CheckAndImm1:
451 if (CheckAndImmediate(N, GetInt1(MatcherTable, MatcherIndex))) break;
453 case OPC_CheckAndImm2:
454 if (CheckAndImmediate(N, GetInt2(MatcherTable, MatcherIndex))) break;
456 case OPC_CheckAndImm4:
457 if (CheckAndImmediate(N, GetInt4(MatcherTable, MatcherIndex))) break;
459 case OPC_CheckAndImm8:
460 if (CheckAndImmediate(N, GetInt8(MatcherTable, MatcherIndex))) break;
463 case OPC_CheckOrImm1:
464 if (CheckOrImmediate(N, GetInt1(MatcherTable, MatcherIndex))) break;
466 case OPC_CheckOrImm2:
467 if (CheckOrImmediate(N, GetInt2(MatcherTable, MatcherIndex))) break;
469 case OPC_CheckOrImm4:
470 if (CheckOrImmediate(N, GetInt4(MatcherTable, MatcherIndex))) break;
472 case OPC_CheckOrImm8:
473 if (CheckOrImmediate(N, GetInt8(MatcherTable, MatcherIndex))) break;
476 case OPC_CheckFoldableChainNode: {
477 assert(NodeStack.size() != 1 && "No parent node");
478 // Verify that all intermediate nodes between the root and this one have
480 bool HasMultipleUses = false;
481 for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i)
482 if (!NodeStack[i].hasOneUse()) {
483 HasMultipleUses = true;
486 if (HasMultipleUses) break;
488 // Check to see that the target thinks this is profitable to fold and that
489 // we can fold it without inducing cycles in the graph.
490 if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
492 !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
498 case OPC_CheckChainCompatible: {
499 unsigned PrevNode = MatcherTable[MatcherIndex++];
500 assert(PrevNode < RecordedNodes.size() && "Invalid CheckChainCompatible");
501 SDValue PrevChainedNode = RecordedNodes[PrevNode];
502 SDValue ThisChainedNode = RecordedNodes.back();
504 // We have two nodes with chains, verify that their input chains are good.
505 assert(PrevChainedNode.getOperand(0).getValueType() == MVT::Other &&
506 ThisChainedNode.getOperand(0).getValueType() == MVT::Other &&
507 "Invalid chained nodes");
509 if (!IsChainCompatible(// Input chain of the previous node.
510 PrevChainedNode.getOperand(0).getNode(),
512 ThisChainedNode.getNode()))
517 case OPC_EmitInteger1: {
518 MVT::SimpleValueType VT =
519 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
520 EmitInteger(GetInt1(MatcherTable, MatcherIndex), VT, RecordedNodes);
523 case OPC_EmitInteger2: {
524 MVT::SimpleValueType VT =
525 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
526 EmitInteger(GetInt2(MatcherTable, MatcherIndex), VT, RecordedNodes);
529 case OPC_EmitInteger4: {
530 MVT::SimpleValueType VT =
531 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
532 EmitInteger(GetInt4(MatcherTable, MatcherIndex), VT, RecordedNodes);
535 case OPC_EmitInteger8: {
536 MVT::SimpleValueType VT =
537 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
538 EmitInteger(GetInt8(MatcherTable, MatcherIndex), VT, RecordedNodes);
542 case OPC_EmitRegister: {
543 MVT::SimpleValueType VT =
544 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
545 unsigned RegNo = MatcherTable[MatcherIndex++];
546 RecordedNodes.push_back(CurDAG->getRegister(RegNo, VT));
550 case OPC_EmitConvertToTarget: {
551 // Convert from IMM/FPIMM to target version.
552 unsigned RecNo = MatcherTable[MatcherIndex++];
553 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
554 SDValue Imm = RecordedNodes[RecNo];
556 if (Imm->getOpcode() == ISD::Constant) {
557 int64_t Val = cast<ConstantSDNode>(Imm)->getZExtValue();
558 Imm = CurDAG->getTargetConstant(Val, Imm.getValueType());
559 } else if (Imm->getOpcode() == ISD::ConstantFP) {
560 const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
561 Imm = CurDAG->getTargetConstantFP(*Val, Imm.getValueType());
564 RecordedNodes.push_back(Imm);
568 case OPC_EmitMergeInputChains: {
569 assert(InputChain.getNode() == 0 &&
570 "EmitMergeInputChains should be the first chain producing node");
571 // This node gets a list of nodes we matched in the input that have
572 // chains. We want to token factor all of the input chains to these nodes
573 // together. However, if any of the input chains is actually one of the
574 // nodes matched in this pattern, then we have an intra-match reference.
575 // Ignore these because the newly token factored chain should not refer to
577 unsigned NumChains = MatcherTable[MatcherIndex++];
578 assert(NumChains != 0 && "Can't TF zero chains");
580 // The common case here is that we have exactly one chain, which is really
581 // cheap to handle, just do it.
582 if (NumChains == 1) {
583 unsigned RecNo = MatcherTable[MatcherIndex++];
584 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
585 ChainNodesMatched.push_back(RecordedNodes[RecNo].getNode());
586 InputChain = RecordedNodes[RecNo].getOperand(0);
587 assert(InputChain.getValueType() == MVT::Other && "Not a chain");
591 // Read all of the chained nodes.
592 assert(ChainNodesMatched.empty() &&
593 "Should only have one EmitMergeInputChains per match");
594 for (unsigned i = 0; i != NumChains; ++i) {
595 unsigned RecNo = MatcherTable[MatcherIndex++];
596 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
597 ChainNodesMatched.push_back(RecordedNodes[RecNo].getNode());
600 // Walk all the chained nodes, adding the input chains if they are not in
601 // ChainedNodes (and this, not in the matched pattern). This is an N^2
602 // algorithm, but # chains is usually 2 here, at most 3 for MSP430.
603 SmallVector<SDValue, 3> InputChains;
604 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
605 SDValue InChain = ChainNodesMatched[i]->getOperand(0);
606 assert(InChain.getValueType() == MVT::Other && "Not a chain");
607 bool Invalid = false;
608 for (unsigned j = 0; j != e; ++j)
609 Invalid |= ChainNodesMatched[j] == InChain.getNode();
611 InputChains.push_back(InChain);
615 if (InputChains.size() == 1)
616 InputChain = InputChains[0];
618 InputChain = CurDAG->getNode(ISD::TokenFactor,
619 NodeToMatch->getDebugLoc(), MVT::Other,
620 &InputChains[0], InputChains.size());
624 case OPC_EmitCopyToReg: {
625 unsigned RecNo = MatcherTable[MatcherIndex++];
626 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
627 unsigned DestPhysReg = MatcherTable[MatcherIndex++];
629 if (InputChain.getNode() == 0)
630 InputChain = CurDAG->getEntryNode();
632 InputChain = CurDAG->getCopyToReg(InputChain, NodeToMatch->getDebugLoc(),
633 DestPhysReg, RecordedNodes[RecNo],
636 InputFlag = InputChain.getValue(1);
640 case OPC_EmitNodeXForm: {
641 unsigned XFormNo = MatcherTable[MatcherIndex++];
642 unsigned RecNo = MatcherTable[MatcherIndex++];
643 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
644 RecordedNodes.push_back(RunSDNodeXForm(RecordedNodes[RecNo], XFormNo));
649 uint16_t TargetOpc = GetInt2(MatcherTable, MatcherIndex);
650 unsigned EmitNodeInfo = MatcherTable[MatcherIndex++];
651 // Get the result VT list.
652 unsigned NumVTs = MatcherTable[MatcherIndex++];
653 assert(NumVTs != 0 && "Invalid node result");
654 SmallVector<EVT, 4> VTs;
655 for (unsigned i = 0; i != NumVTs; ++i) {
656 MVT::SimpleValueType VT =
657 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
658 if (VT == MVT::iPTR) VT = TLI.getPointerTy().SimpleTy;
662 // FIXME: Use faster version for the common 'one VT' case?
663 SDVTList VTList = CurDAG->getVTList(VTs.data(), VTs.size());
665 // Get the operand list.
666 unsigned NumOps = MatcherTable[MatcherIndex++];
667 SmallVector<SDValue, 8> Ops;
668 for (unsigned i = 0; i != NumOps; ++i) {
669 unsigned RecNo = MatcherTable[MatcherIndex++];
670 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
671 Ops.push_back(RecordedNodes[RecNo]);
674 // If there are variadic operands to add, handle them now.
675 if (EmitNodeInfo & OPFL_VariadicInfo) {
676 // Determine the start index to copy from.
677 unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
678 FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
679 assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
680 "Invalid variadic node");
681 // Copy all of the variadic operands, not including a potential flag
683 for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
685 SDValue V = NodeToMatch->getOperand(i);
686 if (V.getValueType() == MVT::Flag) break;
691 // If this has chain/flag inputs, add them.
692 if (EmitNodeInfo & OPFL_Chain)
693 Ops.push_back(InputChain);
694 if ((EmitNodeInfo & OPFL_Flag) && InputFlag.getNode() != 0)
695 Ops.push_back(InputFlag);
698 MachineSDNode *Res = CurDAG->getMachineNode(TargetOpc,
699 NodeToMatch->getDebugLoc(),
701 Ops.data(), Ops.size());
702 // Add all the non-flag/non-chain results to the RecordedNodes list.
703 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
704 if (VTs[i] == MVT::Other || VTs[i] == MVT::Flag) break;
705 RecordedNodes.push_back(SDValue(Res, i));
708 // If the node had chain/flag results, update our notion of the current
710 if (VTs.back() == MVT::Flag) {
711 InputFlag = SDValue(Res, VTs.size()-1);
712 if (EmitNodeInfo & OPFL_Chain)
713 InputChain = SDValue(Res, VTs.size()-2);
714 } else if (EmitNodeInfo & OPFL_Chain)
715 InputChain = SDValue(Res, VTs.size()-1);
717 // If the OPFL_MemRefs flag is set on this node, slap all of the
718 // accumulated memrefs onto it.
720 // FIXME: This is vastly incorrect for patterns with multiple outputs
721 // instructions that access memory and for ComplexPatterns that match
723 if (EmitNodeInfo & OPFL_MemRefs) {
724 MachineSDNode::mmo_iterator MemRefs =
725 MF->allocateMemRefsArray(MatchedMemRefs.size());
726 std::copy(MatchedMemRefs.begin(), MatchedMemRefs.end(), MemRefs);
727 Res->setMemRefs(MemRefs, MemRefs + MatchedMemRefs.size());
730 DEBUG(errs() << " Created node: "; Res->dump(CurDAG); errs() << "\n");
734 case OPC_CompleteMatch: {
735 // The match has been completed, and any new nodes (if any) have been
736 // created. Patch up references to the matched dag to use the newly
738 unsigned NumResults = MatcherTable[MatcherIndex++];
740 for (unsigned i = 0; i != NumResults; ++i) {
741 unsigned ResSlot = MatcherTable[MatcherIndex++];
742 assert(ResSlot < RecordedNodes.size() && "Invalid CheckSame");
743 SDValue Res = RecordedNodes[ResSlot];
745 // FIXME2: Eliminate this horrible hack by fixing the 'Gen' program
746 // after (parallel) on input patterns are removed. This would also
747 // allow us to stop encoding #results in OPC_CompleteMatch's table
749 if (NodeToMatch->getNumValues() <= i ||
750 NodeToMatch->getValueType(i) == MVT::Other)
752 assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
753 NodeToMatch->getValueType(i) == MVT::iPTR ||
754 Res.getValueType() == MVT::iPTR ||
755 NodeToMatch->getValueType(i).getSizeInBits() ==
756 Res.getValueType().getSizeInBits()) &&
757 "invalid replacement");
758 ReplaceUses(SDValue(NodeToMatch, i), Res);
761 // Now that all the normal results are replaced, we replace the chain and
762 // flag results if present.
763 if (!ChainNodesMatched.empty()) {
764 assert(InputChain.getNode() != 0 &&
765 "Matched input chains but didn't produce a chain");
766 // Loop over all of the nodes we matched that produced a chain result.
767 // Replace all the chain results with the final chain we ended up with.
768 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
769 SDNode *ChainNode = ChainNodesMatched[i];
770 SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
771 if (ChainVal.getValueType() == MVT::Flag)
772 ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
773 assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
774 ReplaceUses(ChainVal, InputChain);
777 // If the root node produces a flag, make sure to replace its flag
778 // result with the resultant flag.
779 if (NodeToMatch->getValueType(NodeToMatch->getNumValues()-1) ==
781 ReplaceUses(SDValue(NodeToMatch, NodeToMatch->getNumValues()-1),
784 assert(NodeToMatch->use_empty() &&
785 "Didn't replace all uses of the node?");
787 DEBUG(errs() << "ISEL: Match complete!\n");
789 // FIXME: We just return here, which interacts correctly with SelectRoot
790 // above. We should fix this to not return an SDNode* anymore.
795 // If the code reached this point, then the match failed pop out to the next
797 if (MatchScopes.empty()) {
798 CannotYetSelect(NodeToMatch);
802 const MatchScope &LastScope = MatchScopes.back();
803 RecordedNodes.resize(LastScope.NumRecordedNodes);
804 NodeStack.resize(LastScope.NodeStackSize);
805 N = NodeStack.back();
807 DEBUG(errs() << " Match failed at index " << MatcherIndex
808 << " continuing at " << LastScope.FailIndex << "\n");
810 if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
811 MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
812 MatcherIndex = LastScope.FailIndex;
814 InputChain = LastScope.InputChain;
815 InputFlag = LastScope.InputFlag;
816 if (!LastScope.HasChainNodesMatched)
817 ChainNodesMatched.clear();
819 MatchScopes.pop_back();
824 #endif /* LLVM_CODEGEN_DAGISEL_HEADER_H */