I would like to extract the shared variables between two arbitrary subroutines (with nested calls to other subroutines with a long depth) in a Fortran program. One solution that sound intuitive is to find the intersection of the global variables of the two subroutines. I wonder if there is any compiler option that can generate the list of global variables used by a subroutine?
What does "global" mean, in the context of a Fortran program? Certainly not the global variables that one can have in C or assembler. In Fortran 77, members of shared COMMON blocks could be considered "global" in a restricted sense -- the block name was shared, but variable names could be different in different subprograms that access a common block. In modern Fortran, module variables could be considered global to all subprograms that use that module.
Intel Fortran can generate a cross-reference listing, but you will have to do some post processing of the listing files to correlate variables shared across subprograms and source files.
Take a look at source code analysis tools such as Hyperkwic from Polyhedron (https://polyhedron.com/?product=plusfort#hy ) to see if they are suitable.
I assume you are referring to named or blank COMMON BLOCKs. I was given here a very useful advice which helped me a lot. Move all COMMON BLOCK declarations to one or more modules and then share them with your subroutines. You will discover very quickly if you have a variable named differently in one of the subroutines which can be one of the problems you are encountering. For the second possible problem of mismatched variable sizes or shapes, the compiler should catch some of them. Size mismatch will cause run time error. You may have to do some work, but it is really worth it.