There are no such things as tax loopholes. There is compliance with the law as Congress has written with all of their agendas, and there is noncompliance.
Loopholes are the side effect crappily written laws. And in many cases, loopholes are unearthed by an army of high-paid law firms. Unintentionally created, but very intentionally exploited.
As these loopholes are closed(again, because they're unintentional), these mega corporations are moving around their money from tax haven to tax haven; they're actively exploiting these loopholes in various countries.
It is depriving these countries of very real income. Ya know to pay for the roads, emergency systems, mental health clinics, etc. of ordinary people.
So, sure, it's legal in the same way the many multitudes of immoral things of history were legal.