Have never owned a 540i but have driven a few....
As an 8 year owner of a '91 M5 I can tell you for maintenance and parts costs the 540i is less expensive.The 540 has more low end torque and IMO is a better daily driver but for sheer driving pleasure or on the track-there aren't many cars that will give you more driving pleasure than the M5.
The howl of the S38 at full throttle is unbeatable.
If you can find a '95 540i with the Motorsport package (suspension,ground effects etc.)at a good price it is quite close to the handling of and possibly rarer than the E34 M5.