Based on Krister Walfridsson's blog, the way to do it could be:
- Add an command line option to the
machine.opt
file, so to create a global variable.
- Find the instruction node in
machine.md
or other files, that emits the instruction you want to disable. Change the condition to be on the new variable you'v added. When the condition is not met, gcc would emit a call to a function that you'll supply in your .c
file or lib file.
As a simpler example, take a look at the ft32
architecture directory. It creates an global variable NODIV
based on a -mnodiv
command line option. The instruction node in the ft32.md
file contains:
(define_insn "divsi3"
[(set (match_operand:SI 0 "register_operand" "=r,r")
(div:SI
(match_operand:SI 1 "register_operand" "r,r")
(match_operand:SI 2 "ft32_rimm_operand" "r,KA")))]
"!TARGET_NODIV"
"div.l %0,%1,%2")
In this ft32 case, when the variable is not set, gcc emits div.l
assembly code. When it is set, it would make a call to a function named __divsi3
.
Though I have not tried this out. Will update with exact information when I get a chance to try it out.