Regardless of what you think about the prison system, if someone has bad enough dementia that they can’t properly communicate, then prison is not the appropriate place for them.
Prison should not be used as torture, and someone that is terminally ill should have the option of being moved to a more appropriate place. I’m sure there are a few cases where it wouldn’t be appropriate to release them, but those should be the exception, not the rule.
It should be extremely rare for someone to die in prison.