From 92fd2e397c035b4863850753eed1a8a22f990e1d Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Tue, 3 Oct 2017 20:36:40 +0000 Subject: [PATCH] Refactor DIBuilder dbg intrinsic insertion, NFC Both dbg.declare and dbg.value insertion had duplicate code for the two overloads with different insertion point conventions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314839 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/DIBuilder.h | 11 ++++ lib/IR/DIBuilder.cpp | 115 ++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/include/llvm/IR/DIBuilder.h b/include/llvm/IR/DIBuilder.h index dd6cc44c946..eac48d9f727 100644 --- a/include/llvm/IR/DIBuilder.h +++ b/include/llvm/IR/DIBuilder.h @@ -74,6 +74,17 @@ namespace llvm { /// Create an \a temporary node and track it in \a UnresolvedNodes. void trackIfUnresolved(MDNode *N); + /// Internal helper for insertDeclare. + Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, + DIExpression *Expr, const DILocation *DL, + BasicBlock *InsertBB, Instruction *InsertBefore); + + /// Internal helper for insertDbgValueIntrinsic. + Instruction * + insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo, + DIExpression *Expr, const DILocation *DL, + BasicBlock *InsertBB, Instruction *InsertBefore); + public: /// Construct a builder for a module. /// diff --git a/lib/IR/DIBuilder.cpp b/lib/IR/DIBuilder.cpp index 88f5b36dd58..18979a8d5cf 100644 --- a/lib/IR/DIBuilder.cpp +++ b/lib/IR/DIBuilder.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/IR/DIBuilder.h" +#include "llvm/IR/IRBuilder.h" #include "LLVMContextImpl.h" #include "llvm/ADT/STLExtras.h" #include "llvm/BinaryFormat/Dwarf.h" @@ -771,16 +772,59 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File, File, Line, Col); } +Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, + DIExpression *Expr, const DILocation *DL, + Instruction *InsertBefore) { + return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(), + InsertBefore); +} + +Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, + DIExpression *Expr, const DILocation *DL, + BasicBlock *InsertAtEnd) { + // If this block already has a terminator then insert this intrinsic before + // the terminator. Otherwise, put it at the end of the block. + Instruction *InsertBefore = InsertAtEnd->getTerminator(); + return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore); +} + +Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, + DILocalVariable *VarInfo, + DIExpression *Expr, + const DILocation *DL, + Instruction *InsertBefore) { + return insertDbgValueIntrinsic( + V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr, + InsertBefore); +} + +Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, + DILocalVariable *VarInfo, + DIExpression *Expr, + const DILocation *DL, + BasicBlock *InsertAtEnd) { + return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr); +} + +/// Return an IRBuilder for inserting dbg.declare and dbg.value intrinsics. This +/// abstracts over the various ways to specify an insert position. +static IRBuilder<> getIRBForDbgInsertion(const DILocation *DL, + BasicBlock *InsertBB, + Instruction *InsertBefore) { + IRBuilder<> B(DL->getContext()); + if (InsertBefore) + B.SetInsertPoint(InsertBefore); + else if (InsertBB) + B.SetInsertPoint(InsertBB); + B.SetCurrentDebugLocation(DL); + return B; +} + static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) { assert(V && "no value passed to dbg intrinsic"); return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V)); } -static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) { - I->setDebugLoc(const_cast(DL)); - return I; -} - static Function *getDeclareIntrin(Module &M) { return Intrinsic::getDeclaration(&M, UseDbgAddr ? Intrinsic::dbg_addr : Intrinsic::dbg_declare); @@ -788,7 +832,7 @@ static Function *getDeclareIntrin(Module &M) { Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - Instruction *InsertBefore) { + BasicBlock *InsertBB, Instruction *InsertBefore) { assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare"); assert(DL && "Expected debug loc"); assert(DL->getScope()->getSubprogram() == @@ -802,60 +846,14 @@ Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), MetadataAsValue::get(VMContext, VarInfo), MetadataAsValue::get(VMContext, Expr)}; - return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL); -} -Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertAtEnd) { - assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare"); - assert(DL && "Expected debug loc"); - assert(DL->getScope()->getSubprogram() == - VarInfo->getScope()->getSubprogram() && - "Expected matching subprograms"); - if (!DeclareFn) - DeclareFn = getDeclareIntrin(M); - - trackIfUnresolved(VarInfo); - trackIfUnresolved(Expr); - Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), - MetadataAsValue::get(VMContext, VarInfo), - MetadataAsValue::get(VMContext, Expr)}; - - // If this block already has a terminator then insert this intrinsic - // before the terminator. - if (TerminatorInst *T = InsertAtEnd->getTerminator()) - return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL); - return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL); -} - -Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, - DILocalVariable *VarInfo, - DIExpression *Expr, - const DILocation *DL, - Instruction *InsertBefore) { - assert(V && "no value passed to dbg.value"); - assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value"); - assert(DL && "Expected debug loc"); - assert(DL->getScope()->getSubprogram() == - VarInfo->getScope()->getSubprogram() && - "Expected matching subprograms"); - if (!ValueFn) - ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); - - trackIfUnresolved(VarInfo); - trackIfUnresolved(Expr); - Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), - MetadataAsValue::get(VMContext, VarInfo), - MetadataAsValue::get(VMContext, Expr)}; - return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL); + IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); + return B.CreateCall(DeclareFn, Args); } -Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, - DILocalVariable *VarInfo, - DIExpression *Expr, - const DILocation *DL, - BasicBlock *InsertAtEnd) { +Instruction *DIBuilder::insertDbgValueIntrinsic( + Value *V, DILocalVariable *VarInfo, DIExpression *Expr, + const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) { assert(V && "no value passed to dbg.value"); assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value"); assert(DL && "Expected debug loc"); @@ -871,7 +869,8 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, MetadataAsValue::get(VMContext, VarInfo), MetadataAsValue::get(VMContext, Expr)}; - return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL); + IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); + return B.CreateCall(ValueFn, Args); } void DIBuilder::replaceVTableHolder(DICompositeType *&T, -- 2.40.0