Here are a few initial thoughts on your plan:
Time/Distance - I think you will find that one month is not nearly enough to visit all that you want to, just tallying it up you'll probably more around 50-70 days. Don't underestimate distances too, you'll probably spend 1/4 of your vacation time just driving, Portugal and Spain are not so easy to combine as one would think.
Car - Having a car is good for shorter trips of a week or two focusing mainly in the countryside and not big cities. You have only listed big cities on your wish list, so I do not see any advantage to hiring a car, you'll find parking and navigating the big cities a nightmare with a car. Gasoline is significantly more expense in Spain than in the states, currently in Spain average for US gallon of gas is $6.87.
Weather - Late June into July are some of the hottest days in Spain, in the south it will average around 100 degrees F. Not a good time to visit the area. You will have to cut sightseeing time in half in order to accommodate for the high temps in the mid day.
I think you need to seriously reconsider the feasibility of your current plan so it does not become The Amazing Race.
My recommendation is to instead head north from Barcelona and do a roadtrip in Northern Spain before swing back down to Madrid. Northern Spain aka "Green Spain" is quite the contrast compared to the south of the country, and looks more like Ireland mixed in with the Alps. The weather is much more temperate. From medieval villages, to the picturesque wine regions, the emerald waters of Bay of Biscay, the culinary delights of the Basque country, the Celtic culture of Galicia, and finally the towering jagged peaks of the Picos de Europa.
Here are few more reasons why you should consider the north of Spain: https://theculturetrip.com/europe/spain/articles/15-reasons-to-visit-green-spain-at-least-once-in-your-lifetime/