If f and g are ints, then yes, compilers will in general do this type of optimization where it makes sense.
If f and g are floats, then multiplies/divides by powers of two cannot be done by bit-shifting. However, as it happens, some GPUs have hardware that can do a free multiplication by certain powers of two (such as 0.5, 2, or 4) at the same time as some other operation. So, for instance, (f + g) / 2 could be translated to (f + g) * 0.5 and done for free along with the addition.
Shader compiler implementation details are, unfortunately, not readily available. Both NVIDIA and AMD tend to be fairly secretive about this stuff. However, you can often look at the disassembly produced by the compiler for a given shader. Each vendor provides some tools to do that, such as NVIDIA ShaderPerf and AMD GPUShaderAnalyzer.
Note that the details will vary by GPU vendor and by chipset within a vendor, but will generally be the same for OpenGL and DirectX as those are just two different front-ends to the same back-end instruction set.