I’m trying to figure out a way to assign initial values to function arguments. For a function in IR:
define i32 @main (i32 %0, i32 %1)
%tmp = add i32 %0, %1
I would like to make sure %0 has some initial value (e.g. i32 0) under some circumstances. Is there any easy way to do this? I understand that %0 comes from a live-in value which is defined from outside of the function. I could, at the ISelLowering stage when seeing an ADD, replace the “%0” with a i32 constant 0, but I don’t feel like this is the right approach.
Should I try to look into SUBREG_TO_REG/INSERT_REG here? Or any other thoughts?
Can you clarify what you mean by ensuring that "%0 has a default value?"
Are you trying to ensure that the function's arguments have a default value if the function is called with too few arguments?
The only way that %0 is going to have an undefined value is if a) a function passes in an undefined value (like undef) or b) the function is called with few two arguments. Unless you're trying to address behavior that is undefined at the LLVM IR level, your question doesn't make much sense.
I see. Then my question is probably better put as - if there's a good way in LowerFormalArgument() one can overwrite the input argument's default value?
Now the backend I have does a CopyFromReg(), which reads from a virtual register mapping to the input argument. I'd like to overwrite its value, so that all further IR instructions using this register can see the updated value.
Are you just trying to replace the contents of an argument with another value? If so, I would just do this at the LLVM IR level before doing any code generation, and I'd just replace all uses of the argument with the desired value (if you just want to change some of the uses of the argument, that is easy enough to do, too, at the LLVM IR level).