// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, 0x58(%rsp)
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x10(%rsp)
movaps %xmm0, (%rsp)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
cmpq %rax, %rcx
je 0x6cc3d
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x6cc2b
movq %rbx, %r15
shlq $0x4, %r15
movq %rbx, %r12
xorl %r13d, %r13d
movq (%r14), %rax
movq (%rax,%r15), %rdi
movq (%rax,%r13), %rsi
callq 0x3f9a0
testb %al, %al
je 0x6cc1b
movq (%r14), %rax
movq 0x8(%rax,%r15), %rsi
movq 0x8(%rax,%r13), %rdx
movq 0x58(%rsp), %rdi
callq 0x422a0
addq $0x10, %r13
decq %r12
jne 0x6cbf0
movq (%r14), %rax
movq 0x8(%r14), %rcx
incq %rbx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x4, %rdx
cmpq %rbx, %rdx
ja 0x6cbde
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x40370
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x6cdc4
xorl %r8d, %r8d
xorl %ebx, %ebx
movq %r8, 0x60(%rsp)
movl $0x0, %r12d
testq %rbx, %rbx
je 0x6cd67
movq %rbx, %rax
shlq $0x4, %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbx, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx), %rbp
movq (%r14), %rax
movq (%rax,%r15), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x40fb0
testb %al, %al
jne 0x6cd64
movq %r13, %rdi
movq %rbp, %rsi
callq 0x40fb0
testb %al, %al
jne 0x6cd4c
movq 0x40(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rax,%rcx), %rax
movq (%r14), %rcx
movq 0x8(%rcx,%r15), %rbx
cmpq %rbx, %rax
je 0x6cd4c
movq %rbp, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x6cd0c
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rsp), %rsi
addq $0x10, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x6cd1e
movq %rsp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x41660
movq 0x8(%rsp), %rsi
movq %r13, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
cmpq 0x10(%rsp), %rsi
je 0x6cd3f
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rsp)
jmp 0x6cd4c
movq %rsp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x41660
incq %r12
addq $0x10, %r15
movq 0x28(%rsp), %rbx
cmpq %r12, %rbx
jne 0x6cc87
movq %rbx, %r12
movq (%r14), %rax
movl %r12d, %ecx
cmpq %rcx, %rbx
movq 0x60(%rsp), %r8
jbe 0x6cd91
movslq %ecx, %rdx
leaq (%rax,%r8), %rsi
movq %rbx, %rdi
decq %rdi
movups -0x10(%rsi), %xmm0
movups %xmm0, (%rsi)
addq $-0x10, %rsi
cmpq %rdx, %rdi
jg 0x6cd7e
movq 0x40(%rsp), %rdx
movq %rbx, %rsi
shlq $0x4, %rsi
shlq $0x4, %rcx
movups (%rdx,%rsi), %xmm0
movups %xmm0, (%rax,%rcx)
incq %rbx
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x10, %r8
cmpq %rbx, %rcx
ja 0x6cc5c
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0x6ce86
xorl %ebx, %ebx
movq %rbx, %rcx
shlq $0x4, %rcx
movq 0x8(%rdi,%rcx), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rdi,%rcx), %rax
movq %rax, 0x28(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x6ce55
movq (%rdi,%rcx), %r13
movq 0x10(%rdi,%rcx), %rbp
movl $0x8, %r12d
xorl %r15d, %r15d
movq -0x8(%rax,%r12), %rdi
movq (%rax,%r12), %rax
cmpq %r13, %rdi
jne 0x6ce20
cmpq 0x20(%rsp), %rax
je 0x6ce55
cmpq %rbp, %rdi
jne 0x6ce2c
cmpq 0x28(%rsp), %rax
je 0x6ce55
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3f920
testb %al, %al
jne 0x6ce69
incq %r15
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x10, %r12
cmpq %r15, %rcx
ja 0x6ce0b
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x41cc0
addq $0x2, %rbx
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
sarq $0x4, %rax
cmpq %rbx, %rax
ja 0x6cdd5
testq %rdi, %rdi
je 0x6ce98
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x40880
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6ceaf
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x40880
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6ceca
jmp 0x6ceca
jmp 0x6ceca
jmp 0x6ceca
jmp 0x6ceca
jmp 0x6ceca
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x6cee3
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x40880
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6cefa
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x40880
movq %rbx, %rdi
callq 0x41f10