1
在ir中有一個分支我想完全刪除(condtion + branch + true_basic_block + false_basic_block)。它看起來像這樣:從llvm刪除完整分支ir
%4 = icmp sge i32 %2, %3
br i1 %4, label %5, label %7
; <label>:5 ; preds = %0
%6 = load i32* %x, align 4
store i32 %6, i32* %z, align 4
br label %9
; <label>:7 ; preds = %0
%8 = load i32* %y, align 4
store i32 %8, i32* %z, align 4
br label %9
; <label>:9 ; preds = %7, %5
%10 = call dereferenceable(140) %"class.std::basic_ostream"*@_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(140) @_ZSt4cout, i8* getelementptr inbounds ([5 x i8]* @.str, i32 0, i32 0))
%11 = load i32* %z, align 4
%12 = call dereferenceable(140) %"class.std::basic_ostream"* @_ZNSolsEi(%"class.std::basic_ostream"* %10, i32 %11)
%13 = call dereferenceable(140) %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %12, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
ret i32 0
現在把它刪除,是否有removeBranch功能,或者我需要刪除的指令一個接一個。我一直在嘗試後一種方式,但我已經看到從「基本塊在主沒有終止符」的每個錯誤「使用仍然當def被破壞」,以及更多..我使用了erasefromparent,replaceinstwithvalue,replaceinstwithinst,removefromparent等
任何人都可以足以指出我在正確的方向嗎?
這是我function_pass:
bool runOnFunction(Function &F) override {
for (auto& B : F)
for (auto& I : B)
if(auto* brn = dyn_cast<BranchInst>(&I))
if(brn->isConditional()){
Instruction* cond = dyn_cast<Instruction>(brn->getCondition());
if(cond->getOpcode() == Instruction::ICmp){
branch_vector.push_back(brn);
//removeConditionalBranch(dyn_cast<BranchInst>(brn));
}
}
/*For now just delete the branches in the vector.*/
for(auto b : branch_vector)
removeConditionalBranch(dyn_cast<BranchInst>(b));
return true;
}
我應該在遍歷指令時還是在迭代循環之外調用removeConditionalBranch(在迭代過程中將其存儲在向量中)?我在兩種方法中都遇到了分段錯誤... –
@GauravSharma一般來說,在迭代它們時,不應該添加/刪除指令。我只是測試了這個,我沒有收到段錯誤,所以你必須提供更多的信息。 –
@ Ismail Badawi:我已經添加了pass和它的輸出...請參閱.... –