LICM shouldn't sink/delete debug information. Fix this and add a testcase.

For now the metadata of sinked/hoisted instructions is still wrong, but that'll
be fixed when instructions will have debug metadata directly attached.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83786 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Torok Edwin 2009-10-11 19:15:54 +00:00
parent 95eb470ce1
commit 9289ae85b4
2 changed files with 94 additions and 1 deletions

View File

@ -35,6 +35,7 @@
#include "llvm/Transforms/Scalar.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Instructions.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Analysis/LoopInfo.h"
@ -140,6 +141,10 @@ namespace {
///
void HoistRegion(DomTreeNode *N);
// Cleanup debug information (remove stoppoints with no coressponding
// instructions).
void CleanupDbgInfoRegion(DomTreeNode *N);
/// inSubLoop - Little predicate that returns true if the specified basic
/// block is in a subloop of the current one, not the current one itself.
///
@ -287,6 +292,7 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
//
SinkRegion(DT->getNode(L->getHeader()));
HoistRegion(DT->getNode(L->getHeader()));
CleanupDbgInfoRegion(DT->getNode(L->getHeader()));
// Now that all loop invariants have been removed from the loop, promote any
// memory references to scalars that we can...
@ -338,6 +344,35 @@ void LICM::SinkRegion(DomTreeNode *N) {
}
}
void LICM::CleanupDbgInfoRegion(DomTreeNode *N) {
BasicBlock *BB = N->getBlock();
// If this subregion is not in the top level loop at all, exit.
if (!CurLoop->contains(BB)) return;
// We are processing blocks in reverse dfo, so process children first...
const std::vector<DomTreeNode*> &Children = N->getChildren();
for (unsigned i = 0, e = Children.size(); i != e; ++i)
CleanupDbgInfoRegion(Children[i]);
// Only need to process the contents of this block if it is not part of a
// subloop (which would already have been processed).
if (inSubLoop(BB)) return;
// We modify the basicblock, so don't cache end()
for (BasicBlock::iterator I=BB->begin(); I != BB->end();) {
Instruction *Last = 0;
// Remove consecutive dbgstoppoints, leave only last
do {
if (Last) {
Last->eraseFromParent();
Changed = true;
}
Last = I;
++I;
} while (isa<DbgStopPointInst>(Last) && isa<DbgStopPointInst>(I));
}
}
/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
/// dominated by the specified block, and that are in the current loop) in depth
@ -392,6 +427,10 @@ bool LICM::canSinkOrHoistInst(Instruction &I) {
Size = AA->getTypeStoreSize(LI->getType());
return !pointerInvalidatedByLoop(LI->getOperand(0), Size);
} else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
if (isa<DbgStopPointInst>(CI)) {
// Don't hoist/sink dbgstoppoints, we handle them separately
return false;
}
// Handle obvious cases efficiently.
AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
if (Behavior == AliasAnalysis::DoesNotAccessMemory)
@ -489,7 +528,6 @@ void LICM::sink(Instruction &I) {
// Move the instruction to the start of the exit block, after any PHI
// nodes in it.
I.removeFromParent();
BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI();
ExitBlocks[0]->getInstList().insert(InsertPt, &I);
}

View File

@ -0,0 +1,55 @@
; RUN: opt -licm -S <%s | FileCheck %s
; Test that licm doesn't sink/delete debug info.
define i32 @foo(i32 %a, i32 %j) nounwind {
entry:
;CHECK: entry:
call void @llvm.dbg.func.start(metadata !0)
call void @llvm.dbg.stoppoint(i32 3, i32 5, metadata !1)
;CHECK: %mul = mul i32 %j, %j
br label %for.cond
for.cond:
;CHECK: for.cond:
%i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
%s.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ]
call void @llvm.dbg.stoppoint(i32 4, i32 5, metadata !1)
; CHECK: call void @llvm.dbg.stoppoint(i32 4, i32 5, metadata !1)
%cmp = icmp slt i32 %i.0, %a
br i1 %cmp, label %for.body, label %for.end
for.body:
;CHECK: for.body:
call void @llvm.dbg.stoppoint(i32 5, i32 2, metadata !1)
;CHECK: call void @llvm.dbg.stoppoint(i32 5, i32 2, metadata !1)
%mul = mul i32 %j, %j
%add = add nsw i32 %s.0, %mul
br label %for.inc
for.inc:
;CHECK: for.inc:
call void @llvm.dbg.stoppoint(i32 4, i32 18, metadata !1)
;CHECK: call void @llvm.dbg.stoppoint(i32 4, i32 18, metadata !1)
%inc = add nsw i32 %i.0, 1
br label %for.cond
for.end:
call void @llvm.dbg.stoppoint(i32 7, i32 5, metadata !1)
br label %0
; <label>:0 ; preds = %for.end
call void @llvm.dbg.stoppoint(i32 8, i32 1, metadata !1)
call void @llvm.dbg.region.end(metadata !0)
ret i32 %s.0
}
declare void @llvm.dbg.func.start(metadata) nounwind readnone
declare void @llvm.dbg.declare({ }*, metadata) nounwind readnone
declare void @llvm.dbg.stoppoint(i32, i32, metadata) nounwind readnone
declare void @llvm.dbg.region.end(metadata) nounwind readnone
!0 = metadata !{i32 458798, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"foo", metadata !1, i32 2, metadata !2, i1 false, i1 true}; [DW_TAG_subprogram ]
!1 = metadata !{i32 458769, i32 0, i32 12, metadata !"licm.c", metadata !"/home/edwin", metadata !"clang 1.1", i1 true, i1 false, metadata !"", i32 0}; [DW_TAG_compile_unit ]
!2 = metadata !{i32 458788, metadata !1, metadata !"int", metadata !1, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5}; [DW_TAG_base_type ]