Skip to content

Commit 8ec819e

Browse files
Only guard loop metadata that has non-debug info in it
1 parent 2df48fa commit 8ec819e

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

llvm/include/llvm/IR/Instruction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ class Instruction : public User,
367367
/// Return true if this instruction has any metadata attached to it.
368368
bool hasMetadata() const { return DbgLoc || Value::hasMetadata(); }
369369

370+
// Return true if this instruction contains loop metadata other than
371+
// a debug location
372+
bool hasLoopMetadataOtherThanDebugLoc() const;
373+
370374
/// Return true if this instruction has metadata attached to it other than a
371375
/// debug location.
372376
bool hasMetadataOtherThanDebugLoc() const { return Value::hasMetadata(); }

llvm/lib/IR/Instruction.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
#include "llvm/IR/Instruction.h"
1414
#include "llvm/ADT/DenseSet.h"
15+
#include "llvm/ADT/STLExtras.h"
1516
#include "llvm/IR/AttributeMask.h"
1617
#include "llvm/IR/Attributes.h"
1718
#include "llvm/IR/Constants.h"
1819
#include "llvm/IR/InstrTypes.h"
1920
#include "llvm/IR/Instructions.h"
2021
#include "llvm/IR/IntrinsicInst.h"
2122
#include "llvm/IR/Intrinsics.h"
23+
#include "llvm/IR/LLVMContext.h"
2224
#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
2325
#include "llvm/IR/Module.h"
2426
#include "llvm/IR/Operator.h"
@@ -461,6 +463,29 @@ bool Instruction::hasPoisonGeneratingMetadata() const {
461463
hasMetadata(LLVMContext::MD_align);
462464
}
463465

466+
bool Instruction::hasLoopMetadataOtherThanDebugLoc() const {
467+
// If there is no loop metadata at all, we also don't have
468+
// non-debug loop metadata, obviously.
469+
if (!hasMetadata(LLVMContext::MD_loop))
470+
return false;
471+
472+
// If we do have loop metadata, retrieve it.
473+
MDNode *LoopMD = getMetadata(LLVMContext::MD_loop);
474+
475+
// Check if the existing operands are debug locations. This loop
476+
// should terminate after at most three iterations. Skip
477+
// the first item because it is a self-reference.
478+
for (const MDOperand &Op : llvm::drop_begin(LoopMD->operands())) {
479+
// check for debug location type by attempting a cast.
480+
if (!dyn_cast<DILocation>(Op)) {
481+
return true;
482+
}
483+
}
484+
485+
// If we get here, then all we have is debug locations in the loop metadata.
486+
return false;
487+
}
488+
464489
void Instruction::dropPoisonGeneratingMetadata() {
465490
eraseMetadata(LLVMContext::MD_range);
466491
eraseMetadata(LLVMContext::MD_nonnull);

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
12791279
// | for.body <---- (md2)
12801280
// |_______| |______|
12811281
if (Instruction *TI = BB->getTerminator())
1282-
if (TI->hasMetadata(LLVMContext::MD_loop))
1282+
if (TI->hasLoopMetadataOtherThanDebugLoc())
12831283
for (BasicBlock *Pred : predecessors(BB))
12841284
if (Instruction *PredTI = Pred->getTerminator())
12851285
if (PredTI->hasMetadata(LLVMContext::MD_loop))

0 commit comments

Comments
 (0)