The phase margin (PM) is detrmined based on the loop gain of a circuit with feedback.
The loop gain LG is the gain that can be measured/simulated by opening the loop and injecting a test signal into the loop.
Now the PM is detrmined at the point where the LG is 0 dB. If the phase at this frequency is -360 deg. the PM is zero.
Thus the PM is the distance to -360 deg. (Example: -320 deg gives a PM=40 deg).
The above mentioned threshold of -180 deg (post of so.nia) is used for theoretical calculations only and does NOT include the phase shift of 180 deg caused by the negative feedback action of the opamp.
That means: Only the phase contributions of the various components are considered (amplifier and feedback elements) WITHOUT the negative sign of feedback.
(Hint: Special care is to be taken by opening the loop because the dc operating point may be lost and/or the load condition are altered; therefore, some specific methods for loop gain determination exist).