// File: /workspace/github_cpp_makefile_star2_O2/jstaursky[P]hutch/src/Sleigh/slghsymbol.cc
// Function: DecisionNode::orderPatterns(DecisionProperties&)
void DecisionNode::orderPatterns(DecisionProperties &props)

{
  // This is a tricky routine.  When this routine is called, the patterns remaining in the
  // the decision node can no longer be distinguished by examining additional bits. The basic
  // idea here is that the patterns should be ordered so that the most specialized should come
  // first in the list. Pattern 1 is a specialization of pattern 2, if the set of instructions
  // matching 1 is contained in the set matching 2.  So in the simplest case, the pattern order
  // should represent a strict nesting.  Unfortunately, there are many potential situations where
  // patterns don't necessarily nest.
  //   1) An "or" of two patterns.  This can be an explicit '|' operator in the Constructor, in
  //      which case this can be detected because the two patterns point to the same constructor
  //      But the "or" can be implied across two constructors that do the same thing.  This should
  //      probably be flagged as an error except in the following case.
  //   2) Two patterns aren't properly nested, but they are "resolved" by a third pattern which
  //      covers the intersection of the first two patterns.  Sometimes its easier to specify
  //      three cases that need to be distinguished in this way.
  //   3) Recursive constructors that use a "guard" context bit.  The guard bit is used to prevent
  //      the recursive constructor from matching repeatedly, but it's too much work to put a
  //      constraint an the bit for every other pattern.
  //   4) Other situations where the ability to distinguish between constructors is hidden in
  //      the subconstructors.
  // This routine can determine if an intersection results from case 1) or case 2)
  int4 i,j,k;
  vector<pair<DisjointPattern *,Constructor *> > newlist;
  vector<pair<DisjointPattern *,Constructor *> > conflictlist;

  // Check for identical patterns
  for(i=0;i<list.size();++i) {
    for(j=0;j<i;++j) {
      DisjointPattern *ipat = list[i].first;
      DisjointPattern *jpat = list[j].first;
      if (ipat->identical(jpat))
	props.identicalPattern(list[i].second,list[j].second);
    }
  }

  newlist = list;
  for(i=0;i<list.size();++i) {
    for(j=0;j<i;++j) {
      DisjointPattern *ipat = newlist[i].first;
      DisjointPattern *jpat = list[j].first;
      if (ipat->specializes(jpat))
	break;
      if (!jpat->specializes(ipat)) { // We have a potential conflict
	Constructor *iconst = newlist[i].second;
	Constructor *jconst = list[j].second;
	if (iconst == jconst) { // This is an OR in the pattern for ONE constructor
	  // So there is no conflict
	}
	else {			// A true conflict that needs to be resolved
	  conflictlist.push_back(pair<DisjointPattern *,Constructor *>(ipat,iconst));
	  conflictlist.push_back(pair<DisjointPattern *,Constructor *>(jpat,jconst));
	}
      }
    }
    for(k=i-1;k>=j;--k)
      list[k+1] = list[k];
    list[j] = newlist[i];
  }
  
  // Check if intersection patterns are present, which resolve conflicts
  for(i=0;i<conflictlist.size();i+=2) {
    DisjointPattern *pat1,*pat2;
    Constructor *const1,*const2;
    pat1 = conflictlist[i].first;
    const1 = conflictlist[i].second;
    pat2 = conflictlist[i+1].first;
    const2 = conflictlist[i+1].second;
    bool resolved = false;
    for(j=0;j<list.size();++j) {
      DisjointPattern *tpat = list[j].first;
      Constructor *tconst = list[j].second;
      if ((tpat == pat1)&&(tconst==const1)) break; // Ran out of possible specializations
      if ((tpat == pat2)&&(tconst==const2)) break;
      if (tpat->resolvesIntersect(pat1,pat2)) {
	resolved = true;
	break;
      }
    }
    if (!resolved)
      props.conflictingPattern(const1,const2);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
xorl %ebx, %ebx
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x4, %rax
cmpq %rbx, %rax
jbe 0x75576
movq %rbx, %r15
shlq $0x4, %r15
xorl %r12d, %r12d
movq %rbx, %r13
subq $0x1, %r13
jb 0x75571
movq (%r14), %rax
movq (%rax,%r15), %rdi
movq (%rax,%r12), %rsi
callq 0x4df30
testb %al, %al
je 0x7556b
movq (%r14), %rax
movq 0x8(%rax,%r15), %rsi
movq 0x8(%rax,%r12), %rdx
movq 0x18(%rsp), %rdi
callq 0x51de0
addq $0x10, %r12
jmp 0x7553a
incq %rbx
jmp 0x7551d
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4ef10
movq (%r14), %rax
movq $0x0, 0x10(%rsp)
xorl %esi, %esi
movq %r14, 0x70(%rsp)
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rsi, %rcx
jbe 0x756bc
movq %rsi, %rax
shlq $0x4, %rax
movq %rax, (%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rsi, 0x8(%rsp)
movq (%r14), %rax
cmpq %r15, %rsi
je 0x75665
movq %rsi, %r12
movq 0x20(%rsp), %rcx
movq (%rsp), %rdx
movq (%rcx,%rdx), %rbp
movq (%rax,%rbx), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x50270
testb %al, %al
jne 0x7566a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x50270
testb %al, %al
movq %r12, %rsi
jne 0x75659
movq 0x20(%rsp), %rax
movq (%rsp), %rcx
movq 0x8(%rax,%rcx), %rax
movq (%r14), %rcx
movq 0x8(%rcx,%rbx), %r12
cmpq %r12, %rax
je 0x75659
movq %rbp, 0x40(%rsp)
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
leaq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x4f6b0
movq %r13, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4f6b0
movq 0x70(%rsp), %r14
movq 0x8(%rsp), %rsi
incq %r15
addq $0x10, %rbx
jmp 0x755bf
movq %rsi, %r15
jmp 0x75670
movq (%r14), %rax
movq %r12, %rsi
movslq %r15d, %rcx
movq 0x10(%rsp), %rdx
addq %rax, %rdx
movq %rsi, %rdi
cmpq %rcx, %rsi
jle 0x75693
decq %rsi
movups -0x10(%rdx), %xmm0
movups %xmm0, (%rdx)
addq $-0x10, %rdx
jmp 0x7567e
movq 0x20(%rsp), %rcx
movl %r15d, %edx
shlq $0x4, %rdx
movq (%rsp), %rsi
movups (%rcx,%rsi), %xmm0
movups %xmm0, (%rax,%rdx)
movq %rdi, %rsi
incq %rsi
addq $0x10, 0x10(%rsp)
jmp 0x75596
xorl %edx, %edx
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rdx, %rcx
jbe 0x7576a
movq %rdx, 0x8(%rsp)
movq %rdx, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %r13
movq 0x8(%rax,%rcx), %r15
movq 0x10(%rax,%rcx), %rbp
movq 0x18(%rax,%rcx), %rax
movq %rax, (%rsp)
pushq $0x8
popq %rbx
pushq $-0x1
popq %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
incq %r12
cmpq %r12, %rcx
jbe 0x7574b
movq -0x8(%rax,%rbx), %rdi
movq (%rax,%rbx), %rax
cmpq %r13, %rdi
jne 0x7572b
cmpq %r15, %rax
je 0x7574b
cmpq %rbp, %rdi
jne 0x75736
cmpq (%rsp), %rax
je 0x7574b
movq %r13, %rsi
movq %rbp, %rdx
callq 0x4de90
addq $0x10, %rbx
testb %al, %al
je 0x75702
jmp 0x7575c
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movq (%rsp), %rdx
callq 0x51550
movq 0x8(%rsp), %rdx
addq $0x2, %rdx
jmp 0x756be
leaq 0x50(%rsp), %rdi
callq 0x50040
leaq 0x20(%rsp), %rdi
callq 0x50040
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x75799
jmp 0x75799
jmp 0x75799
jmp 0x75799
jmp 0x75799
jmp 0x75799
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x50040
leaq 0x20(%rsp), %rdi
callq 0x50040
movq %rbx, %rdi
callq 0x518c0