One might make the argument that anybody, prompted by religious beliefs, who’s willing to strap on a suicide belt or cut heads off or shoot people down on the street is mentally ill; however, mental illness is not,
per se, a defense against a charge of murder (let alone terrorism), at least not in the U.S. I believe you would have to display some form of severe psychosis that genuinely undermined your ability to distinguish between right and wrong in order to get off on an insanity plea (even then, of course, you’re not looking at being set loose on society; you go off to an asylum for the criminally insane). Subscribing to a religious belief system that not only authorizes, but exhorts, you to kill unbelievers
could generally be the result of parental abuse or a bed-wetting problem or premature baldness. But I think we err badly if we do not take into account the existence of evil in trying to understand why people, whatever the ostensible initial cause, turn to violence, particularly when they make a conscious choice that involves the dehumanization of others who do not share their values. And I think it is clear that militant Islam is an evil medium that brings out the worst in its adherents. Whether they are all mentally ill or not, their victims are still dead, and in no position to appreciate what, in my view, is an irrelevant distinction that should not deter the U.S. government from fulfilling its primary duty of protecting us from the depredations of our enemies - sane or otherwise.