Ideally the Romans would attempt to flank the phalanx as this is one of the weaknesses of the formation. This is easier for a legion to do as they have better mobility and they need this edge as the phalanx is stronger head on.
Other things the Romans would do is to launch their pilum before engaging in melee combat in an disrupt the Macedonian lines, make the Greeks drop their shields (as they have pilum stuck in them) and hopefully create a gap in the Macedonian line that the Romans could exploit.
The best example of this that I can think of is the battle of Cynocephalae during the Second Macedonian War where Roman legions fought a Macedonian phalanx.
"The strength of the phalanx is irresistible when it is close-packed and bristling with extended spears; but if by attacks at different points you force the troops to swing round their spears, unwieldy as they are by reason of their length and weight, they become entangled in a disorderly mass" Livy XLIV.41
So somehow at Pydna the Romans broke up the phalanx and then attacked into the gaps. Where they attacked an unbroken phalanx they would suffer heavy losses. This could lead into questions about the triplex acies formation and whether they fought with the gaps or whether the 2nd line closed up right away.
At Cynoscephalae 30 years earlier, a Roman legion withdrew before an advancing phalanx when at an uphill disadvantage until they reached the valley and were compelled to fight. Then the action was decided then the phalanx was taken in behind by the successful Roman right wing.
The phalanx could also be drawn into broken terrain which would break up the solid line and make them very vulnerable.
So it appears the legions would try to break up the cohesion of the phalanx either by pinprick attacks or by drawing them into bad terrain. Or utilize their superior command and control and tactical maneuverability and flank them.
This may be an unpopular view. But the fact is we don't even know what a phalanx really was. In terms of primary sources we have very little actual evidence for how they fought by people who directly witnessed the phalanxes.
There are two schools of though on the matter - Traditionalist (such as Victor Hanson) use the description of Egyptians fighting in Xenophon's historical novel The education of Cyrus to assume that is a description of Greek warfare. Which is a big assumption. For example compare Polybious (who is actually describing a Greek Phalanx) who states that hoplites needed 6 feet of space in all direction in order to fight.
Revisionists (foremost among them Hans Van Wees. Also look at (2013) Men of Bronze edited by Donald Kagan) have in the past 30 years over turned many of the popular assumptions concerning Hoplite warfare.
So recently Peter Krentz (in Men of Bronze) restates many of the arguments against the plausibility of the hoplite mass-shove and the heavy weight of hoplite equipment. Of more interest was how he traced the scholarly history of orthodox ideas, showing that some ideas (such as the weight of the hoplite shield and the compact shoving) have been passed on from generation to generation perhaps with too little critical evaluation.
So what I'm saying is that any answer to your question is going to be highly speculative because we just don't even know what one of the elements was.