Index: llvm/include/llvm/Analysis/LoopInfo.h =================================================================== --- llvm/include/llvm/Analysis/LoopInfo.h +++ llvm/include/llvm/Analysis/LoopInfo.h @@ -441,7 +441,9 @@ /// header then the node is returned. If any latch instruction does not /// contain llvm.loop or or if multiple latches contain different nodes then /// 0 is returned. - MDNode *getLoopID() const; + std::pair getLoopIDWithInstr() const; + MDNode *getLoopID() const { return getLoopIDWithInstr().first; } + /// Set the llvm.loop loop id metadata for this loop. /// /// The LoopID metadata node will be added to each terminator instruction in Index: llvm/lib/Analysis/LoopInfo.cpp =================================================================== --- llvm/lib/Analysis/LoopInfo.cpp +++ llvm/lib/Analysis/LoopInfo.cpp @@ -203,10 +203,12 @@ return true; } -MDNode *Loop::getLoopID() const { +std::pair Loop::getLoopIDWithInstr() const { MDNode *LoopID = nullptr; + Instruction *I = nullptr; if (isLoopSimplifyForm()) { - LoopID = getLoopLatch()->getTerminator()->getMetadata(LLVMContext::MD_loop); + I = getLoopLatch()->getTerminator(); + LoopID = I->getMetadata(LLVMContext::MD_loop); } else { // Go through each predecessor of the loop header and check the // terminator for the metadata. @@ -218,17 +220,19 @@ TerminatorInst *TI = BB->getTerminator(); if (MDNode *MD = TI->getMetadata(LLVMContext::MD_loop)) { - if (!LoopID) + if (!LoopID) { LoopID = MD; + I = TI; + } else if (MD != LoopID) // Multiple MD_loop found => corrupt metadata. - return nullptr; + return { nullptr, nullptr }; } } } if (!LoopID || LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID) - return nullptr; - return LoopID; + return { nullptr, nullptr }; + return { LoopID, I }; } void Loop::setLoopID(MDNode *LoopID) const {