Index: llvm/lib/IR/Verifier.cpp =================================================================== --- llvm/lib/IR/Verifier.cpp +++ llvm/lib/IR/Verifier.cpp @@ -269,6 +269,11 @@ /// Keep track which DISubprogram is attached to which function. DenseMap DISubprogramAttachments; + /// For each DILocation, store a list of the first Instruction with that !dbg + /// attachment in each basic block. + DenseMap> + FirstDILocationPerBlock; + /// Track all DICompileUnits visited. SmallPtrSet CUVisited; @@ -468,6 +473,7 @@ void visitLoadInst(LoadInst &LI); void visitStoreInst(StoreInst &SI); void verifyDominatesUse(Instruction &I, unsigned i); + void verifyDominateUseSourceLoc(Instruction &I, unsigned i); void visitInstruction(Instruction &I); void visitTerminator(Instruction &I); void visitBranchInst(BranchInst &BI); @@ -2506,6 +2512,22 @@ if (BrokenDebugInfo) return; } + + // For each DILocation, store a list of the first Instruction with that !dbg + // attachment in each basic block. + FirstDILocationPerBlock.clear(); + for (auto &BB : F) { + Seen.clear(); + for (auto &I : BB) { + const DILocation *Loc = I.getDebugLoc().get(); + if (!Loc) + continue; + auto it_inserted = Seen.insert(Loc); + if (!it_inserted.second) + continue; + FirstDILocationPerBlock[Loc].push_back(&I); + } + } } // verifyBasicBlock - Verify that a basic block is well formed... @@ -4179,6 +4201,35 @@ const Use &U = I.getOperandUse(i); Assert(DT.dominates(Op, U), "Instruction does not dominate all uses!", Op, &I); + + verifyDominateUseSourceLoc(I, i); +} + +void Verifier::verifyDominateUseSourceLoc(Instruction &I, unsigned i) { + MDNode *N = I.getDebugLoc().getAsMDNode(); + if (!N) + return; + AssertDI(isa(N), "invalid !dbg metadata attachment", &I, N); + // If an instruction has a source location, then all occurrences of + // each operand's source location must dominate this instruction or + // be in the same BB as the instruction. + Instruction *Op = cast(I.getOperand(i)); + + MDNode *OpN = Op->getDebugLoc().getAsMDNode(); + if (!OpN) + return; + AssertDI(isa(OpN), "invalid !dbg metadata attachment", &I, N); + DILocation *OpLoc = cast(N); + if (OpLoc->getLine() == 0) + return; + // Verify that the first occurrance of the operand's !dbg location + // in each basic block dominates I. + for (auto *FirstOccurence : FirstDILocationPerBlock[OpLoc]) + AssertDI(FirstOccurence->getParent() == I.getParent() || + DT.dominates(FirstOccurence, &I), + "Occurence of source location from an operand does not dominate " + "instruction.", + FirstOccurence, OpLoc, &I); } void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {