Hello all: I apologize for what I imagine is a rather silly question, but I'm still somewhat new to LLVM and am stuck. I am reworking some code that was originally in the backend involving MachineInstructions and MachineOperands, and I now need for it to function as an LLVM IR pass, using just Instructions, etc, and nothing related to 'Machine'. However, I am not sure how to get the information I need at the IR level. I need to iterate over all operands of an instruction, analyzing each operand to get a register number (if available), see if the operand is a register or an immediate, get the operand def, and some other stuff. I also need to be able to do implicitDef/Use determination on instructions. I don't see a way to do this at present. So, any guidance would be much appreciated. Thanks! Griffin Wright
LLVM IR operands *are* other instructions. For example, if you want to use the result of a multiply in an add, the operand of the add is simply a pointer to the multiply instruction. This is possible because the IR is always in SSA form, so there is only one def. Reid On Thu, Jun 2, 2011 at 12:46 PM, Griffin Wright <grwright at umich.edu> wrote:> > Hello all: > > I apologize for what I imagine is a rather silly question, but I'm still > somewhat new to LLVM and am stuck. I am reworking some code that was > originally in the backend involving MachineInstructions and > MachineOperands, and I now need for it to function as an LLVM IR pass, > using just Instructions, etc, and nothing related to 'Machine'. > > However, I am not sure how to get the information I need at the IR level. > I need to iterate over all operands of an instruction, analyzing each > operand to get a register number (if available), see if the operand is a > register or an immediate, get the operand def, and some other stuff. I > also need to be able to do implicitDef/Use determination on instructions. > I don't see a way to do this at present. > > So, any guidance would be much appreciated. > > Thanks! > Griffin Wright > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
On Jun 2, 2011, at 9:46 AM, Griffin Wright wrote:> > Hello all: > > I apologize for what I imagine is a rather silly question, but I'm still > somewhat new to LLVM and am stuck. I am reworking some code that was > originally in the backend involving MachineInstructions and > MachineOperands, and I now need for it to function as an LLVM IR pass, > using just Instructions, etc, and nothing related to 'Machine'. > > However, I am not sure how to get the information I need at the IR level. > I need to iterate over all operands of an instruction, analyzing each > operand to get a register number (if available), see if the operand is a > register or an immediate, get the operand def, and some other stuff. I > also need to be able to do implicitDef/Use determination on instructions. > I don't see a way to do this at present.If your pass really needs to analyze the use of physical registers and whether individual machine instructions are using immediate operands or not, then it needs to stay at the machine level. If you only really care about higher-level things like (1) what operation is being performed, (2) whether particular operands are constant or not, and (3) if not, how and when they were produced, then it's far better to work on LLVM IR. On IR, you test for specific kinds of values using isa or dyn_cast. For example, I can check whether a value is a 'mul' instruction: if (BinaryOperator *op = dyn_cast<BinaryOperator>(val)) { if (op->getOpcode() == Instruction::Mul) { and now I can check whether the LHS is a call: if (isa<CallInst>(op->getOperand(0))) { etc. John.
That is very helpful, John, thank you. Reid as well, your response cleared some things up :) Part <n> of my question series: Say I have a bunch of defs that I'm iterating through, and I want to find the uses for each def. How should I have my parameters set up? Right now I'm trying to use the following, where "defs" is defined in the header file as "std::vector<Instruction*> defs": for (std::vector<Instruction*>::iterator def_iter = defs.begin(); def_iter!= defs.end(); def_iter++) { Instruction* defInsn = dyn_cast<Instruction>(*def_iter); for (Instruction::use_iterator use_iter = defInsn->use_begin(); use_iter!= defInsn->use_end(); use_iter++) { Instruction *pUseInstr = dyn_cast<Instruction>(*use_iter); Do stuff with the newly found use instruction! } } I feel like having the use_begin() based on defInsn is grossly incorrect (and the rest of the code here might be as well), so I was hoping to run it past someone with [much] more knowledge than I. Thanks again for tolerating a poor newbie's questions. -Griffin On Thu, 02 Jun 2011 10:37:47 -0700, John McCall <rjmccall at apple.com> wrote:> On Jun 2, 2011, at 9:46 AM, Griffin Wright wrote: > >> >> Hello all: >> >> I apologize for what I imagine is a rather silly question, but I'm still >> somewhat new to LLVM and am stuck. I am reworking some code that was >> originally in the backend involving MachineInstructions and >> MachineOperands, and I now need for it to function as an LLVM IR pass, >> using just Instructions, etc, and nothing related to 'Machine'. >> >> However, I am not sure how to get the information I need at the IRlevel.>> >> I need to iterate over all operands of an instruction, analyzing each >> operand to get a register number (if available), see if the operand is a >> register or an immediate, get the operand def, and some other stuff. I >> also need to be able to do implicitDef/Use determination oninstructions.>> >> I don't see a way to do this at present. > > If your pass really needs to analyze the use of physical registers and > whether individual machine instructions are using immediate operands > or not, then it needs to stay at the machine level. If you only really > care > about higher-level things like (1) what operation is being performed, > (2) whether particular operands are constant or not, and (3) if not, how > and when they were produced, then it's far better to work on LLVM IR. > > On IR, you test for specific kinds of values using isa or dyn_cast. > For example, I can check whether a value is a 'mul' instruction: > if (BinaryOperator *op = dyn_cast<BinaryOperator>(val)) { > if (op->getOpcode() == Instruction::Mul) { > and now I can check whether the LHS is a call: > if (isa<CallInst>(op->getOperand(0))) { > etc. > > John.