CodeGen: Refactor linkage/visibility calculation

It turns out that linkage and visibility have rather similar logic for
both functions and non-variable globals.  Split the calculation out so
that both sides may share this code.

No functionality change.
PR19554: Fix some memory leaks in DIEHashTest.cpp