我是 LLVM 的初学者。我试图在 BasicBlock 的指令中移动,但我不能。在这个特定示例中,我尝试获取结束指令的前一条指令。我正在尝试两种方法:
我有以下代码序列:
bool patternDC::runOnBasicBlock(BasicBlock &BB) { ... if (BB.getTerminator()) { Instruction* current = BB.getTerminator(); errs() << "\n LAST: "<<*current<<"\n"; Instruction* prev = &BB.back(); errs() << "\n PENULTIMATE: "<<*prev<<"\n"; ...
终端打印相同的指令。我不知道 back() 是如何工作的。(定义在文件 BasicBlock.h 的第 199 行。)
我也试过:
bool patternDC::runOnBasicBlock(BasicBlock &BB) { ... BasicBlock::const_iterator I = BB.begin(); BasicBlock::const_iterator E = BB.end(); BasicBlock::const_iterator prev_iter,last_iter; prev_iter = NULL; last_iter = NULL; for(;I!=E;I++){ prev_iter = last_iter; last_iter = I; } if(prev_iter){ errs() << "prev_iter: " << *(dyn_cast<Instruction>(prev_iter)) << "\n"; } if(last_iter){ errs() << "last_iter: " << *(dyn_cast<Instruction>(last_iter)) << "\n"; } // not related to the main question: uncomment the next line for an unusual //behavior: lastlast is DIFFERENT from last.lastlast=section(BasicBlock) // errs() << "lastlast: " << *(dyn_cast<Instruction>(I)) << "\n"; ... Instruction* prev = *(dyn_cast<Instruction*>(prev_iter)); errs() << "\n prev: "<<*prev<<"\n";
终端可以很好地打印出 prev 和 last,但是在尝试分配到 Instruction* prev 时出现编译错误 Clang 错误是:“..... /home/alex/llvm/include/llvm/Support/Casting.h: 51:28:错误:'classof' 不是 'llvm::Instruction*' 的成员
如果有人知道使用基本块中任何元素的更好方法或知道为什么这些元素不起作用,请告诉我:)
谢谢你,亚历克斯