// File: rossy[P]crossc/SPIRV-Cross/spirv_cross.cpp
// Function: spirv_cross::Compiler::block_is_loop_candidate(spirv_cross::SPIRBlock const&, spirv_cross::SPIRBlock::Method) const
bool Compiler::block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const
{
	// Tried and failed.
	if (block.disable_block_optimization || block.complex_continue)
		return false;

	if (method == SPIRBlock::MergeToSelectForLoop || method == SPIRBlock::MergeToSelectContinueForLoop)
	{
		// Try to detect common for loop pattern
		// which the code backend can use to create cleaner code.
		// for(;;) { if (cond) { some_body; } else { break; } }
		// is the pattern we're looking for.
		bool ret = block.terminator == SPIRBlock::Select && block.merge == SPIRBlock::MergeLoop &&
		           block.true_block != block.merge_block && block.true_block != block.self &&
		           block.false_block == block.merge_block;

		if (ret && method == SPIRBlock::MergeToSelectContinueForLoop)
			ret = block.true_block == block.continue_block;

		// If we have OpPhi which depends on branches which came from our own block,
		// we need to flush phi variables in else block instead of a trivial break,
		// so we cannot assume this is a for loop candidate.
		if (ret)
		{
			for (auto &phi : block.phi_variables)
				if (phi.parent == block.self)
					return false;

			auto *merge = maybe_get<SPIRBlock>(block.merge_block);
			if (merge)
				for (auto &phi : merge->phi_variables)
					if (phi.parent == block.self)
						return false;
		}
		return ret;
	}
	else if (method == SPIRBlock::MergeToDirectForLoop)
	{
		// Empty loop header that just sets up merge target
		// and branches to loop body.
		bool ret = block.terminator == SPIRBlock::Direct && block.merge == SPIRBlock::MergeLoop && block.ops.empty();

		if (!ret)
			return false;

		auto &child = get<SPIRBlock>(block.next_block);
		ret = child.terminator == SPIRBlock::Select && child.merge == SPIRBlock::MergeNone &&
		      child.false_block == block.merge_block && child.true_block != block.merge_block &&
		      child.true_block != block.self;

		// If we have OpPhi which depends on branches which came from our own block,
		// we need to flush phi variables in else block instead of a trivial break,
		// so we cannot assume this is a for loop candidate.
		if (ret)
		{
			for (auto &phi : block.phi_variables)
				if (phi.parent == block.self || phi.parent == child.self)
					return false;

			for (auto &phi : child.phi_variables)
				if (phi.parent == block.self)
					return false;

			auto *merge = maybe_get<SPIRBlock>(block.merge_block);
			if (merge)
				for (auto &phi : merge->phi_variables)
					if (phi.parent == block.self || phi.parent == child.false_block)
						return false;
		}

		return ret;
	}
	else
		return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rax
testb $0x1, 0xb0(%rax)
jne 0x2365b
movq -0x18(%rbp), %rax
testb $0x1, 0xb1(%rax)
je 0x23664
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
cmpl $0x0, -0x1c(%rbp)
je 0x23674
cmpl $0x2, -0x1c(%rbp)
jne 0x2381d
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x2, 0xc(%rcx)
movb %al, -0xe9(%rbp)
jne 0x236df
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x1, 0x10(%rcx)
movb %al, -0xe9(%rbp)
jne 0x236df
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpl 0x1c(%rdx), %ecx
movb %al, -0xe9(%rbp)
je 0x236df
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpl 0x8(%rdx), %ecx
movb %al, -0xe9(%rbp)
je 0x236df
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
sete %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
testb $0x1, -0x1d(%rbp)
je 0x2370c
cmpl $0x2, -0x1c(%rbp)
jne 0x2370c
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
sete %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
testb $0x1, -0x1d(%rbp)
je 0x23810
movq -0x18(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2fc30
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2fc60
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2fc90
testb $0x1, %al
jne 0x2374f
jmp 0x23782
leaq -0x30(%rbp), %rdi
callq 0x2fcd0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x23775
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
jmp 0x23777
leaq -0x30(%rbp), %rdi
callq 0x2fcf0
jmp 0x2373c
movq -0xe8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %esi
callq 0x2fd10
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2380e
movq -0x48(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x2fc30
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x2fc60
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2fc90
testb $0x1, %al
jne 0x237d9
jmp 0x2380c
leaq -0x58(%rbp), %rdi
callq 0x2fcd0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x237ff
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
jmp 0x23801
leaq -0x58(%rbp), %rdi
callq 0x2fcf0
jmp 0x237c6
jmp 0x2380e
jmp 0x23810
movb -0x1d(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x23b0d
cmpl $0x1, -0x1c(%rbp)
jne 0x23b09
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x1, 0xc(%rcx)
movb %al, -0xea(%rbp)
jne 0x2385e
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x1, 0x10(%rcx)
movb %al, -0xea(%rbp)
jne 0x2385e
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x2fd70
movb %al, -0xea(%rbp)
movb -0xea(%rbp), %al
andb $0x1, %al
movb %al, -0x69(%rbp)
testb $0x1, -0x69(%rbp)
jne 0x23878
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
movq -0xe8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x18(%rax), %esi
callq 0x8a70
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rcx
xorl %eax, %eax
cmpl $0x2, 0xc(%rcx)
movb %al, -0xeb(%rbp)
jne 0x238fa
movq -0x78(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x10(%rcx)
movb %al, -0xeb(%rbp)
jne 0x238fa
movq -0x78(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpl 0x1c(%rdx), %ecx
movb %al, -0xeb(%rbp)
jne 0x238fa
movq -0x78(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpl 0x1c(%rdx), %ecx
movb %al, -0xeb(%rbp)
je 0x238fa
movq -0x78(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
setne %al
movb %al, -0xeb(%rbp)
movb -0xeb(%rbp), %al
andb $0x1, %al
movb %al, -0x69(%rbp)
testb $0x1, -0x69(%rbp)
je 0x23aff
movq -0x18(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x2fc30
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x2fc60
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2fc90
testb $0x1, %al
jne 0x23954
jmp 0x239a6
leaq -0x88(%rbp), %rdi
callq 0x2fcd0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0x2398d
movq -0x98(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x23996
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
jmp 0x23998
leaq -0x88(%rbp), %rdi
callq 0x2fcf0
jmp 0x2393b
movq -0x78(%rbp), %rax
addq $0x50, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x2fc30
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x2fc60
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2fc90
testb $0x1, %al
jne 0x239f4
jmp 0x23a33
leaq -0xa8(%rbp), %rdi
callq 0x2fcd0
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x23a23
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
jmp 0x23a25
leaq -0xa8(%rbp), %rdi
callq 0x2fcf0
jmp 0x239db
movq -0xe8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %esi
callq 0x2fd10
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x23afd
movq -0xc0(%rbp), %rax
addq $0x50, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x2fc30
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x2fc60
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x2fc90
testb $0x1, %al
jne 0x23aac
jmp 0x23afb
leaq -0xd0(%rbp), %rdi
callq 0x2fcd0
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0x23ae5
movq -0xe0(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jne 0x23aeb
movb $0x0, -0x1(%rbp)
jmp 0x23b0d
jmp 0x23aed
leaq -0xd0(%rbp), %rdi
callq 0x2fcf0
jmp 0x23a93
jmp 0x23afd
jmp 0x23aff
movb -0x69(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x23b0d
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)