void Variables::updateMetadata(Module& module, Value* oldTarget, Value* newTarget, Type* newType) { vector<Instruction*> to_remove; if (newTarget) { errs() << "\tChanging metadata for: " << newTarget->getName() << "\n"; bool changed = false; for(Module::iterator f = module.begin(), fe = module.end(); f != fe; f++) { for(Function::iterator b = f->begin(), be = f->end(); b != be; b++) { for(BasicBlock::iterator i = b->begin(), ie = b->end(); i != ie; i++) { if (DbgDeclareInst *oldDeclare = dyn_cast<DbgDeclareInst>(i)) { if (Value *address = oldDeclare->getAddress()) { if (AllocaInst *allocaInst = dyn_cast<AllocaInst>(address)) { if (allocaInst == oldTarget) { // the alloca we are looking for DIVariable oldDIVar(oldDeclare->getVariable()); MDNode* newDIType = getTypeMetadata(module, oldDIVar, newType); // construct new DIVariable with new type descriptor vector<Value*> doperands; for(unsigned i = 0; i < oldDIVar->getNumOperands(); i++) { if (i == 5) { // the argument that corresponds to the type descriptor doperands.push_back(newDIType); } else { doperands.push_back(oldDIVar->getOperand(i)); // preserve other descriptors } } ArrayRef<Value*> *arrayRefDOperands = new ArrayRef<Value*>(doperands); MDNode* newMDNode = MDNode::get(module.getContext(), *arrayRefDOperands); DIVariable newDIVar(newMDNode); // insert new declare instruction DIBuilder* builder = new DIBuilder(module); Instruction *newDeclare = builder->insertDeclare(newTarget, newDIVar, oldDeclare); // make sure the declare instruction preserves its own metadata unsigned id = 0; if (oldDeclare->getMetadata(id)) { newDeclare->setMetadata(id, oldDeclare->getMetadata(id)); } to_remove.push_back(oldDeclare); // can't erase while iterating through instructions changed = true; } } } } } } } for(unsigned i = 0; i < to_remove.size(); i++) { to_remove[i]->eraseFromParent(); } if (!changed) { errs() << "\tNo metadata to change\n"; } } return; }
LLVMValueRef DIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef dref, LLVMValueRef storage, LLVMValueRef varInfo, LLVMBasicBlockRef block) { DIBuilder *d = unwrap(dref); Instruction *instr = d->insertDeclare( unwrap(storage), unwrapDI<DIVariable>(varInfo), unwrap(block)); return wrap(instr); }
LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, LLVMValueRef Storage, LLVMValueRef VarInfo, LLVMValueRef Expr, LLVMBasicBlockRef Block) { DIBuilder *D = unwrap(Dref); Instruction *Instr = D->insertDeclare(unwrap(Storage), unwrapDI<DIVariable>(VarInfo), unwrapDI<DIExpression>(Expr), unwrap(Block)); return wrap(Instr); }
//could do the before equivs of the next 2 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd( LLVMDIBuilderRef D, LLVMValueRef Storage, LLVMValueRef VarInfo, LLVMBasicBlockRef Block) { DIBuilder *db = unwrap(D); Instruction *Instr = db->insertDeclare( unwrap(Storage), unwrapDI<DIVariable>(VarInfo), unwrap(Block)); return wrap(Instr); }
LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, LLVMValueRef Storage, LLVMMetadataRef VarInfo, LLVMMetadataRef Expr, LLVMBasicBlockRef Block) { // Fail immediately here until the llgo folks update their bindings. The // called function is going to assert out anyway. llvm_unreachable("DIBuilder API change requires a DebugLoc"); DIBuilder *D = unwrap(Dref); Instruction *Instr = D->insertDeclare( unwrap(Storage), unwrap<DILocalVariable>(VarInfo), unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block)); return wrap(Instr); }