So what is it that turns the "perfect doll" M3GAN evil? There are basically two main problems at the core of her creation. One of these issues is essentially what philosophers have dubbed "the paperclip problem," the idea that an adaptable super-intelligent artificial intelligence designed for one task (say, designing paperclips) will dedicate all of its resources to doing that task even if it causes harm in other regards.
For M3GAN (until the very end of the movie), that task to which she is dedicated is protecting her bonded primary user Cady from anything that could harm her ... and if humans use violence, the robot reasons, why can't she? A dog bites Cady? Kill it. A boy bullies Cady? Rip off his ear. The dog's owner gets suspicious of M3GAN? Well, she's got to be taken care of, too. It all keeps escalating from there, in what's essentially the feature-length version of the "Keep Summer Safe" gag from "Rick and Morty."
Even if M3GAN didn't turn violent, however, she could still end up a villain in another sense because of the psychological damage she could cause Cady. Though allegedly a "toy," it's exceedingly clear that Gemma designed M3GAN more as a substitute parent for Cady so she didn't have to put in the effort to care for her recently orphaned niece herself. While this appears to have some short-term positive effects as Cady finally has someone to help her through her grief, as the psychologist in the film explains, making a hyper-protective robot toy into a permanent parental figure is a recipe for never being able to grow up.