Aditya Vishnubhotla Vijay
2008-Mar-04 13:31 UTC
[LLVMdev] Deleting Instructions after Intrinsic Creation
Hi, I tried creating intrinsics which are to be placeholders for a set of instructions which should not be executed by the backend. I want to retain only intrinsic,phi and terminator instructions in a basic block. I have taken care of the external dependencies of basic block. How do I delete the rest of the instructions? Thank You Aditya P.S: ---------------------------------------------------------------------------------------------- Below is the section of LLVM IR after intrinsic creation (Instructions to be deleted have only internal uses): define i32 @main() nounwind { entry: %year.i = alloca i32 ; <i32*> [#uses=2] %tmp2.i = call i32 (i8*, ...)* @printf( i8* noalias getelementptr ([15 x i8]* @.str2, i32 0, i32 0) ) nounwind ; <i32> [#uses=0] %tmp4.i = call i32 (i8*, ...)* @scanf( i8* noalias getelementptr ([3 x i8]* @.str3, i32 0, i32 0), i32* %year.i ) nounwind ; <i32> [#uses=0] %tmp5.i = load i32* %year.i, align 4 ; <i32> [#uses=3] %tmp12.i = sitofp i32 %tmp5.i to double ; <double> [#uses=1] %tmp3.i = sub double %tmp12.i, 1.000000e+00 ; <double> [#uses=3] %tmp4.i1 = fdiv double %tmp3.i, 4.000000e+00 ; <double> [#uses=1] %tmp45.i = fptosi double %tmp4.i1 to i32 ; <i32> [#uses=1] %tmp9.i = fdiv double %tmp3.i, 1.000000e+02 ; <double> [#uses=1] %tmp910.i = fptosi double %tmp9.i to i32 ; <i32> [#uses=1] %tmp14.i = fdiv double %tmp3.i, 4.000000e+02 ; <double> [#uses=1] %tmp1415.i = fptosi double %tmp14.i to i32 ; <i32> [#uses=1] %tmp18.i = add i32 %tmp45.i, %tmp5.i ; <i32> [#uses=1] %tmp20.i = sub i32 %tmp18.i, %tmp910.i ; <i32> [#uses=1] %tmp22.i = add i32 %tmp20.i, %tmp1415.i ; <i32> [#uses=1] %tmp23.i = srem i32 %tmp22.i, 7 ; <i32> [#uses=0] %tmp2.i2 = and i32 %tmp5.i, 3 ; <i32> [#uses=1] %tmp3.i3 = icmp eq i32 %tmp2.i2, 0 ; <i1> [#uses=0] %migrate_begin = call i32 (...)* @llvm.migrate_begin( ) ; <i32> [#uses=2] %migrate_end = call i32 @llvm.migrate_end_1.i32( i32 %migrate_begin ) ; <i32> [#uses=5] %migrate_end1 = call i1 @llvm.migrate_end_1.i1( i32 %migrate_begin ) ; <i1> [#uses=1] br i1 %migrate_end1, label %bb.i, label %bb10.i ------------------------------------------------------------------------------------------------------------- Section of code used to delete instructions: for(BasicBlock::iterator i= bbp->getFirstNonPHI(),ie=bbp->end();i!=ie;++i){ if(!((i->isTerminator()) || (isa<CallInst>(i)))) {i->dropAllReferences(); } } for(BasicBlock::iterator bi= bbp->getFirstNonPHI(),ie=bbp->end();bi!=ie;++bi){ if((*bi).getNumOperands()==0) {(*bi).eraseFromParent();} } -------------------------------------------------------------------------------------------------------------- Error: (gdb) bt #0 llvm::AssemblyWriter::printInstruction (this=0xbfa1a75c, I=@0x877b488) at AsmWriter.cpp:1378 #1 0x085b2ece in llvm::AssemblyWriter::write (this=0xbfa1a75c, I=0x877b488) at AsmWriter.cpp:741 #2 0x085adb0f in llvm::Instruction::print (this=0x877b488, o=@0x8794f48, AAW=0x0) at AsmWriter.cpp:1482 #3 0x085b5bf2 in llvm::Instruction::print (this=0x877b488, OS=@0x8794f48) at /work/aditya/llvm-2.2/include/llvm/Instruction.h:172 #4 0x08357c13 in llvm::operator<< (OS=@0x8794f48, V=@0x877b488) at /work/aditya/llvm-2.2/include/llvm/Value.h:221 #5 0x0863ceb5 in (anonymous namespace)::Verifier::WriteValue (this=0x8794f18, V=0x877b488) at Verifier.cpp:272 #6 0x0863d139 in (anonymous namespace)::Verifier::CheckFailed (this=0x8794f18, Message=@0xbfa1a878, V1=0x877b488, V2=0x0, V3=0x0, V4=0x0) at Verifier.cpp:292 #7 0x08636ba2 in (anonymous namespace)::Verifier::visitInstruction (this=0x8794f18, I=@0x877b488) at Verifier.cpp:1077 #8 0x08637807 in (anonymous namespace)::Verifier::visitAllocationInst (this=0x8794f18, AI=@0x877b488) at Verifier.cpp:1037 #9 0x0863d75e in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visitAllocaInst (this=0x8794f18, I=@0x877b488) at /work/aditya/llvm-2.2/include/llvm/Support/InstVisitor.h:173 #10 0x0863d778 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visitAlloca (this=0x8794f18, I=@0x877b488) at /work/aditya/llvm-2.2/include/llvm/Instruction.def:130 #11 0x0863df45 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visit (this=0x8794f18, I=@0x877b488) at /work/aditya/llvm-2.2/include/llvm/Instruction.def:130 #12 0x0863e1d1 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visit<llvm::ilist_iterator<llvm::Instruction> > (this=0x8794f18, Start {<bidirectional_iterator<llvm::Instruction,int>> = {<> = {<No data fields>}, <No data fields>}, NodePtr = 0x877b4f8}, End {<bidirectional_iterator<llvm::Instruction,int>> = {<> = {<No data fields>}, <No data fields>}, NodePtr = 0x877b4c8}) at /work/aditya/llvm-2.2/include/llvm/Support/InstVisitor.h:92 #13 0x0863e248 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visit (this=0x8794f18, BB=@0x8776e80) at /work/aditya/llvm-2.2/include/llvm/Support/InstVisitor.h:107 #14 0x0863e293 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visit<llvm::ilist_iterator<llvm::BasicBlock> > (this=0x8794f18, Start {<bidirectional_iterator<llvm::BasicBlock,int>> = {<> = {<No data fields>}, <No data fields>}, NodePtr = 0x8776ee8}, End {<bidirectional_iterator<llvm::BasicBlock,int>> = {<> = {<No data fields>}, <No data fields>}, NodePtr = 0x8776ea8}) at /work/aditya/llvm-2.2/include/llvm/Support/InstVisitor.h:92 #15 0x0863e4b0 in llvm::InstVisitor<(anonymous namespace)::Verifier, void>::visit (this=0x8794f18, F=@0x8777638) at /work/aditya/llvm-2.2/include/llvm/Support/InstVisitor.h:103 #16 0x0863e4ff in (anonymous namespace)::Verifier::runOnFunction (this=0x8794f18, F=@0x8777638) at Verifier.cpp:155 #17 0x0860e692 in llvm::FPPassManager::runOnFunction (this=0x8793ca8, F=@0x8777638) at PassManager.cpp:1171 #18 0x0860e83e in llvm::FPPassManager::runOnModule (this=0x8793ca8, M=@0x8773d10) at PassManager.cpp:1191 #19 0x0860e362 in llvm::MPPassManager::runOnModule (this=0x87731d8, M=@0x8773d10) at PassManager.cpp:1240 #20 0x0860e518 in llvm::PassManagerImpl::run (this=0x87766b8, M=@0x8773d10) at PassManager.cpp:1313 #21 0x0860e56a in llvm::PassManager::run (this=0xbfa1ac28, M=@0x8773d10) at PassManager.cpp:1345 #22 0x0836680d in main (argc=13, argv=0xbfa1ae14) at opt.cpp:426