Douglas do Couto Teixeira
2011-Jan-21 20:56 UTC
[LLVMdev] How to change the type of an Instruction?
Hello guys, I wonder how I can change the type of an integer variable. For instance, given the instruction "%3 = add i32 %1, %2" I would like to alter the instruction to "%3 = add i16 %1, %2". Is there any way to do this? Best wishes, Douglas -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110121/c0007572/attachment.html>
On 21 January 2011 12:56, Douglas do Couto Teixeira < douglasdocouto at gmail.com> wrote:> Hello guys, > > I wonder how I can change the type of an integer variable. For instance, > given the instruction "%3 = add i32 %1, %2" I would like to alter the > instruction to "%3 = add i16 %1, %2". Is there any way to do this? >No. Instead you create a new Instruction, in this case with BinaryOperator::CreateAdd, then OldInst->replaceAllUsesWith(NewInst) to update all the users, then OldInst->eraseFromParent() since it's now dead code. Also, all values have types immutably assigned at creation, so you'll need to insert casts (trunc instructions in your case) to cast %1 and %2 from i32 to i16 for the smaller add. Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110121/91e43425/attachment.html>
Douglas do Couto Teixeira
2011-Jan-24 12:41 UTC
[LLVMdev] How to change the type of an Instruction?
Hi, Nick, thanks for the reply. I still have a problem: I only need to "clone" an Instruction, changing its type. That is, I would like to keep all characteristics of the old Instruction and create a new one only with a different type. I am trying create a new Instruction thus: %3 = add nsw i32 %1, %2 ; <i16> [#uses=2] //Old Instruction Value* Op0 = I->getOperand(0); Value* Op1 = I->getOperand(1); Value* V0 = new Value(Type::getInt16Ty(Op0->getContext()), Op0->getValueID()); Value* V1 = new Value(Type::getInt16Ty(Op1->getContext()), Op1->getValueID()); Instruction* newInst = BinaryOperator::CreateNSWAdd(V0, V1, "test"); errs() << "NewInst:\n" << *newInst << "\n"; But I get something like this: %test = add nsw i16 <badref>, <badref> ; <i16> [#uses=0] What I am doing wrong? Best, Douglas On Fri, Jan 21, 2011 at 8:25 PM, Nick Lewycky <nlewycky at google.com> wrote:> On 21 January 2011 12:56, Douglas do Couto Teixeira < > douglasdocouto at gmail.com> wrote: > >> Hello guys, >> >> I wonder how I can change the type of an integer variable. For instance, >> given the instruction "%3 = add i32 %1, %2" I would like to alter the >> instruction to "%3 = add i16 %1, %2". Is there any way to do this? >> > > No. Instead you create a new Instruction, in this case with > BinaryOperator::CreateAdd, then OldInst->replaceAllUsesWith(NewInst) to > update all the users, then OldInst->eraseFromParent() since it's now dead > code. > > Also, all values have types immutably assigned at creation, so you'll need > to insert casts (trunc instructions in your case) to cast %1 and %2 from i32 > to i16 for the smaller add. > > Nick > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110124/039591fc/attachment.html>