I forgot this. If you are serious about staying with the company, move into or work with a group that is visible and known to the higher ups. Usually this happens when the manger is very good at managing upwards.

In my last company many good people who got fired were in groups that did great work, but did not have a strong manager. Strong managers advocate for the group and show off what the group does. They make the group visible in the company and bring interesting work to the group.

If you feel like you are just an ant in a group that nobody knows about, it's easy for somebody else (probably somebody who have never even heard about) to draw a line through your name in an excel sheet defining who to keep and who to cut.