(טו) וּמַכֵּ֥ה אָבִ֛יו וְאִמּ֖וֹ מ֥וֹת יוּמָֽת׃
(א) ומכה אביו ואמו...וְאֵינוֹ חַיָּב אֶלָּא בְהַכָּאָה שֶׁיֵּשׁ בָּהּ חַבּוּרָה
He is, however, not punishable with death except for a blow which causes a wound
(ז) הַמַּקִּיז דָּם לְאָבִיו אוֹ שֶׁהָיָה רוֹפֵא וְחָתַךְ לוֹ בָּשָׂר אוֹ אֵבָר פָּטוּר. אַף עַל פִּי שֶׁהוּא פָּטוּר לְכַתְּחִלָּה לֹא יַעֲשֶׂה. אוֹ לְהוֹצִיא סִלּוֹן מִבְּשַׂר אָבִיו אוֹ אִמּוֹ לֹא יוֹצִיא שֶׁמָּא יַעֲשֶׂה חַבּוּרָה. בַּמֶּה דְּבָרִים אֲמוּרִים כְּשֶׁיֵּשׁ שָׁם אַחֵר לַעֲשׂוֹת. אֲבָל אִם אֵין שָׁם מִי שֶׁיַּעֲשֶׂה אֶלָּא הוּא וַהֲרֵי הֵן מִצְטַעֲרִין הֲרֵי זֶה מַקִּיז וְחוֹתֵךְ כְּפִי מַה שֶּׁיַּרְשֵׁהוּ לַעֲשׂוֹת:
When a person lets blood for his father, or if he was a doctor and amputated flesh or a limb, he is not liable. Even though he is not liable, the initial and preferred option is for him not to perform the operation. Nor should he remove a thorn from the flesh of his father or mother lest he cause a bruise.
When does the above apply? When there is another person there who is capable of performing these actions. If, however, there is no one else there capable of doing this but him and they are suffering, he may let blood or amputate according to the license that they grant him.
Aruch Hashulchan, Yoreh Deah (241:6)
A doctor may perform the procedure if he is the most qualified and his parents would prefer him over another doctor.
הַצּוֹעֵק לְשֶׁעָבַר, הֲרֵי זוֹ תְּפִלַּת שָׁוְא. כֵּיצַד. הָיְתָה אִשְׁתּוֹ מְעֻבֶּרֶת, וְאָמַר, יְהִי רָצוֹן שֶׁתֵּלֵד אִשְׁתִּי זָכָר, הֲרֵי זוֹ תְּפִלַּת שָׁוְא. הָיָה בָא בַדֶּרֶךְ וְשָׁמַע קוֹל צְוָחָה בָּעִיר, וְאָמַר יְהִי רָצוֹן שֶׁלֹּא יִהְיוּ אֵלּוּ בְּנֵי בֵיתִי, הֲרֵי זוֹ תְּפִלַּת שָׁוְא:
He who prays over what has already happened, this prayer is in vain. How? If his wife was pregnant and he said, “May it be your will that my wife give birth to a boy," this prayer is in vain. If he was coming on the way and heard the sound of screaming in the city, and he said, “May it be your will that these are not the children of my house," this is a prayer in vain.
Paraphrasing R' Aryeh Lebowitz - YU Torah:
R'Shlomo Zalman Auerbach (Nishmas Avraham, Yoreh Deah, 4) talks about this Mishna and says it's an Aveirah to daven when someone is terminally ill as it weakens their emunah when they see that their tefillos don't work. But you can still hope as Hashem does have the power.
The Steipler (Toldos Yaakov 118) gives 5 reasons why one can still daven:
1) Can help to reduce his suffering
2) Can still extend his life by hours or days
3) Can always still daven even if it seems like there's no hope (see next source)
4) Could be a zechus for the person in the next world
5) May result in a rephuah for someone else, never know what happens with your tefillos.
Steipler even says that this is an ideal time to daven when the doctors give up hope, as you're not contradicting the Mishnah in Brachos because here you're davening that the doctors are wrong or that a cure should be discovered rather than for an actual miracle.
Hezekiah continued: I have received a tradition from the house of my father’s father, from King David, Even if a sharp sword rests upon a person’s neck, he should not prevent himself from praying for mercy. One may still hold out hope that his prayers will be answered, as was David himself when he saw the Angel of Destruction, but nonetheless prayed for mercy and his prayers were answered.