<div dir="ltr"><span style="font-size:12.8px">Hi,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">What I am trying to do is to check duplicate arithmetic instructions, if found any, I want to delete that duplicate instruction and needed to add a <b>Store </b>Instruction.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Code:</div><div style="font-size:12.8px">        Instruction* temp_Ins;</div><div style="font-size:12.8px">        for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {</div><div style="font-size:12.8px">           for (BasicBlock::iterator i = b->begin(), ie = b->end(); i != ie; ++i) {</div><div style="font-size:12.8px">                     </div><div style="font-size:12.8px">                     switch(curr_Ins->getOpcode()){</div><div style="font-size:12.8px">                             </div><div style="font-size:12.8px">                              case Instruction::Add: {</div><div style="font-size:12.8px">                                     // Here i am keeping expression and its address to a <i>Map. </i>Eg: <i><b>mymap[a+b]=i</b></i></div><div style="font-size:12.8px"><b style="font-style:italic">                                     </b>// If 'a+b' is already in the list then i am trying to delete that new duplicate instruction from LLVM IR.</div><div style="font-size:12.8px">                                     // following is the code i wrote for deleting duplicate <i style="font-weight:bold">add </i>instruction:</div><div style="font-size:12.8px">                                     temp_Ins = cast<Instruction>(i);</div><div style="font-size:12.8px"><span style="white-space:pre-wrap">                             </span>temp_Ins->eraseFromParent();</div><div style="font-size:12.8px">                              }</div><div style="font-size:12.8px">                     }</div><div style="font-size:12.8px">           }</div><div style="font-size:12.8px">        }</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">What I am doing wrong here ?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 26 November 2015 at 09:22, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi,<div><br><div><span class=""><blockquote type="cite"><div>On Nov 25, 2015, at 7:05 PM, Ansar K.A. via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br><div><div dir="ltr"><span style="font-size:12.8px">Hi,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I was trying to create a new  <b>Store</b> instruction and inserting it to LLVM IR (.ll) file. I found the following constructor in LLVM Manual:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><table style="font-family:'Lucida Grande',Verdana,Geneva,Arial,sans-serif;font-size:13px;line-height:16.9px;white-space:nowrap;font-weight:bold;margin-left:6px;color:rgb(37,53,85)"><tbody><tr></tr><tr><td style="margin-left:6px"><a href="http://llvm.org/docs/doxygen/html/classllvm_1_1StoreInst.html#aa2a72f9a51b317f5b4ab8695adf59025" style="color:rgb(70,101,162)" target="_blank">StoreInst::StoreInst</a></td><td>(</td><td><a href="http://llvm.org/docs/doxygen/html/classllvm_1_1Value.html" style="color:rgb(70,101,162)" target="_blank">Value</a> * </td><td style="color:rgb(96,32,32)">Val,</td></tr><tr><td style="text-align:right"></td><td></td><td><a href="http://llvm.org/docs/doxygen/html/classllvm_1_1Value.html" style="color:rgb(70,101,162)" target="_blank">Value</a> * </td><td style="color:rgb(96,32,32)">Ptr,</td></tr><tr><td style="text-align:right"></td><td></td><td><a href="http://llvm.org/docs/doxygen/html/classllvm_1_1Instruction.html" style="color:rgb(70,101,162)" target="_blank">Instruction</a> * </td><td style="color:rgb(96,32,32)">InsertBefore </td></tr><tr><td></td><td>)<br><br></td></tr></tbody></table></div><div style="font-size:12.8px">I thought of using this. I don't know the meaning of the constructors arguments. What all values should be passed for creating a new <b>Store </b>Instruction (with an <i>example) </i>?</div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">How to insert this to LLVM IR? ( I saw the code snippet in LLVM Programming manual, I couldn't understand it. Any examples on doing the same? )</div></div></div></blockquote><div><br></div><div><br></div></span><div><div>- Val is the value you want to store.</div><div>- Ptr is the location where you want to store the value Val.</div><div>- InsertBefore is an optional pointer to an existing instruction, if it is provided the newly created instruction will be just before the provided one.</div><div><br></div><div>Also you may want to use the IRBuilder in general to create IR, you may want to follow the Kaleidoscope tutorial at least once (Creating a Store is done chapter 7: <a href="http://llvm.org/docs/tutorial/LangImpl7.html" target="_blank">http://llvm.org/docs/tutorial/LangImpl7.html</a> )</div></div><span class=""><br><blockquote type="cite"><div><div dir="ltr"><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I tried to delete an instruction using:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">                        i->eraseFromParent();</div></div></div></blockquote><div><br></div><div><br></div></span><div>From the trace it does not seem that this call is leading to the assert. You’re not giving enough information about what you’re doing here. It could be some iterator invalidation for example.</div><div><br></div><div>— </div><div>Mehdi</div><div><br></div><div><br></div><div><br></div><div><br></div><br><blockquote type="cite"><div><div><div class="h5"><div dir="ltr"><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">But it throws the following error/exception:</div><div style="font-size:12.8px"><br></div><div><div><span style="font-size:12.8px"><b>opt -load /home/mtech2/Documents/llvm/build/Debug+Asserts/lib/CSE.so -CSE <test.ll> /dev/null</b></span></div><div><span style="font-size:12.8px"><b>opt: /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:81: static bool llvm::isa_impl_cl<To, From*>::doit(const From*) [with To = llvm::Instruction; From = llvm::Instruction]: Assertion `Val && "isa<> used on a null pointer"' failed.</b></span></div><div><span style="font-size:12.8px"><b>#0 0x29635a5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/mtech2/Documents/llvm/llvm/lib/Support/Unix/Signals.inc:405:0</b></span></div><div><span style="font-size:12.8px"><b>#1 0x29638ba PrintStackTraceSignalHandler(void*) /home/mtech2/Documents/llvm/llvm/lib/Support/Unix/Signals.inc:463:0</b></span></div><div><span style="font-size:12.8px"><b>#2 0x2961eb9 llvm::sys::RunSignalHandlers() /home/mtech2/Documents/llvm/llvm/lib/Support/Signals.cpp:34:0</b></span></div><div><span style="font-size:12.8px"><b>#3 0x29623b1 SignalHandler(int) /home/mtech2/Documents/llvm/llvm/lib/Support/Unix/Signals.inc:211:0</b></span></div><div><span style="font-size:12.8px"><b>#4 0x7f190977a340 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10340)</b></span></div><div><span style="font-size:12.8px"><b>#5 0x7f1908715cc9 gsignal /build/buildd/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56:0</b></span></div><div><span style="font-size:12.8px"><b>#6 0x7f19087190d8 abort /build/buildd/eglibc-2.19/stdlib/abort.c:91:0</b></span></div><div><span style="font-size:12.8px"><b>#7 0x7f190870eb86 __assert_fail_base /build/buildd/eglibc-2.19/assert/assert.c:92:0</b></span></div><div><span style="font-size:12.8px"><b>#8 0x7f190870ec32 (/lib/x86_64-linux-gnu/libc.so.6+0x2fc32)</b></span></div><div><span style="font-size:12.8px"><b>#9 0x7f19084d9970 llvm::isa_impl_cl<llvm::Instruction, llvm::Instruction*>::doit(llvm::Instruction const*) /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:82:0</b></span></div><div><span style="font-size:12.8px"><b>#10 0x7f19084d9450 llvm::isa_impl_wrap<llvm::Instruction, llvm::Instruction*, llvm::Instruction*>::doit(llvm::Instruction* const&) /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:123:0</b></span></div><div><span style="font-size:12.8px"><b>#11 0x7f19084d89e2 llvm::isa_impl_wrap<llvm::Instruction, llvm::ilist_iterator<llvm::Instruction> const, llvm::Instruction*>::doit(llvm::ilist_iterator<llvm::Instruction> const&) /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:115:0</b></span></div><div><span style="font-size:12.8px"><b>#12 0x7f19084d8371 bool llvm::isa<llvm::Instruction, llvm::ilist_iterator<llvm::Instruction> >(llvm::ilist_iterator<llvm::Instruction> const&) /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:135:0</b></span></div><div><span style="font-size:12.8px"><b>#13 0x7f19084d7ba1 llvm::cast_retty<llvm::Instruction, llvm::ilist_iterator<llvm::Instruction> >::ret_type llvm::cast<llvm::Instruction, llvm::ilist_iterator<llvm::Instruction> >(llvm::ilist_iterator<llvm::Instruction>&) /home/mtech2/Documents/llvm/llvm/include/llvm/Support/Casting.h:230:0</b></span></div><div><span style="font-size:12.8px"><b>#14 0x7f19084d73fd (anonymous namespace)::CSE::runOnFunction(llvm::Function&) /home/mtech2/Documents/llvm/llvm/lib/Transforms/CSE/CSE.cpp:48:0</b></span></div><div><span style="font-size:12.8px"><b>#15 0x288fd1c llvm::FPPassManager::runOnFunction(llvm::Function&) /home/mtech2/Documents/llvm/llvm/lib/IR/LegacyPassManager.cpp:1528:0</b></span></div><div><span style="font-size:12.8px"><b>#16 0x288feaf llvm::FPPassManager::runOnModule(llvm::Module&) /home/mtech2/Documents/llvm/llvm/lib/IR/LegacyPassManager.cpp:1549:0</b></span></div><div><span style="font-size:12.8px"><b>#17 0x289024a (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/mtech2/Documents/llvm/llvm/lib/IR/LegacyPassManager.cpp:1605:0</b></span></div><div><span style="font-size:12.8px"><b>#18 0x28909bb llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/mtech2/Documents/llvm/llvm/lib/IR/LegacyPassManager.cpp:1708:0</b></span></div><div><span style="font-size:12.8px"><b>#19 0x2890bfb llvm::legacy::PassManager::run(llvm::Module&) /home/mtech2/Documents/llvm/llvm/lib/IR/LegacyPassManager.cpp:1740:0</b></span></div><div><span style="font-size:12.8px"><b>#20 0xee3bd2 main /home/mtech2/Documents/llvm/llvm/tools/opt/opt.cpp:602:0</b></span></div><div><span style="font-size:12.8px"><b>#21 0x7f1908700ec5 __libc_start_main /build/buildd/eglibc-2.19/csu/libc-start.c:321:0</b></span></div><div><span style="font-size:12.8px"><b>#22 0xec1739 _start (/home/mtech2/Documents/llvm/build/Debug+Asserts/bin/opt+0xec1739)</b></span></div><div><span style="font-size:12.8px"><b>Stack dump:</b></span></div><div><span style="font-size:12.8px"><b>0.<span style="white-space:pre-wrap">   </span>Program arguments: opt -load /home/mtech2/Documents/llvm/build/Debug+Asserts/lib/CSE.so -CSE </b></span></div><div><span style="font-size:12.8px"><b>1.<span style="white-space:pre-wrap">  </span>Running pass 'Function Pass Manager' on module '<stdin>'.</b></span></div><div><span style="font-size:12.8px"><b>2.<span style="white-space:pre-wrap"> </span>Running pass 'Print Instructions' on function '@main'</b></span></div><div><span style="font-size:12.8px"><b>Aborted (core dumped)</b></span></div><div style="font-size:12.8px"><br></div></div><div style="font-size:12.8px">Any help will be appreciated.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- Ansar K.A.</div></div></div></div>
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>