The S&P 500 has overcome worries about artificial intelligence and the fallout from the war in Iran to stage one of its strongest two-month showings on record.