Today I just bought a T-Mobile HTC One M8 and it prompted me to download Android 6.0 marshmallow and when I did it optimize all of its apps approximately 182 and now it's stuck saying Android is upgrading with a swirly thing on the screen
The MM update has had some issues for sure! My suggestion, is that you put the onus, on T-Mobile. Take it back and demand one that doesn't have Marshmallow, if possible. And wait to upgrade until they have fixed the bugs.
Now, if you want to venture further. Try this: Press and hold the POWER and VOLUME UP buttons for at least 10 seconds. HTC One (M8) then restarts. See if the forced restart helps. If not, then I would recommend to take it back.
Also....until such time that the bugs are worked out, assuming you get another phone, I would suggest you download an app called Package Disabler Pro, from the Play store and put the check mark in the T-Mobile Software Update entry. This will prevent it from auto-update. DO NOT TURN OFF ANY SYSTEM ENTRIES IN THAT PROGRAM. If you elect to use it, be sure to read up on it.