Is it illegal to flash high beams to signal other cars?
If police see you doing so is it a valid pretext for a traffic stop?
Generally yes, but it depends on both context and jurisdiction.
For example, if you arrived from a different street segment at 4-way stop at the same time as another car, and give a flash to indicate "go ahead," that by itself is not likely to be sustained as sufficient to justify a stop.
If you come up close behind another car and flash your high beams as part of aggressive driving (e.g. message "get out of my way") then that may form a key contribution to a justification for pulling you over for aggressive driving.
Many states also prohibit use of high beams when other cars are around because they have a tendency to blind other drivers (and blinded drivers are more likely to be in an accident), so if the officer observes you committing that offense it would be probable cause to pull you over for that.
Here's an example from NY State where flashing high beams, alone, did not provide probable cause for an officer to pull over a vehicle. NY's law about high beams blinding other drivers specifies that the high-beams have to interfere with the other driver's operation of the vehicle for it to be an offense. See also citations near the end of this decision, which says:
The Court of Appeals has indicated: "The mere flashing of lights, alone, does not constitute a violation of the statute (see People v. Meola, 7 NY2d 391, 397 ; People v. Hines, 155 AD2d 722, 724 , lv denied 76 NY2d 736 ; People v. Lauber, 162 Misc 2d 19, 20 ).
Also, if you are flashing the police car, the officer may think you are trying to get his/her attention for some reason and that you are initiating a traffic stop.
However, do be careful. This driver in Texas flashed his high beams at another car (which turned out to be a police car) because he thought the other car had its high beams on (the officer says it was just a new car; at least two other drivers had apparently flashed the officer for the same reason). The officer then applied the same law that driver was concerned about, forbidding the use of high beams that blind others. The driver was tased, shot, and killed as the officer applied the instant death penalty for his offenses, and the penalty was ruled justified. A dead driver cannot practically contest that stop later on, even though the family might try.
See also Headlight Flashing: Legality on Wikipedia.