Protect classIsDerivedFrom from run-out-of-stack crash and infinity loop.
Rewrite recursion to loop-over-stack and add checks for complexity to avoid crash on deep definition or infinify recursion.
This fixes https://github.com/llvm/llvm-project/issues/55614