Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 9449db4

Browse files
committed
Make SimplifyAllocas keep dbg.declare intrinsics pointed to their original
allocas instead of the bitcast that the other users use.
1 parent 1ce51a0 commit 9449db4

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/Transforms/NaCl/SimplifyAllocas.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
#include "llvm/IR/DataLayout.h"
1616
#include "llvm/IR/Function.h"
1717
#include "llvm/IR/Instructions.h"
18+
#include "llvm/IR/IntrinsicInst.h"
1819
#include "llvm/IR/Module.h"
1920
#include "llvm/Transforms/NaCl.h"
20-
21+
#include "llvm/Support/raw_ostream.h"
2122
using namespace llvm;
2223
namespace {
2324
class SimplifyAllocas : public BasicBlockPass {
@@ -53,7 +54,8 @@ class SimplifyAllocas : public BasicBlockPass {
5354
bool Changed = false;
5455
for (BasicBlock::iterator I = BB.getFirstInsertionPt(), E = BB.end();
5556
I != E;) {
56-
if (AllocaInst *Alloca = dyn_cast<AllocaInst>(I++)) {
57+
Instruction *Inst = &*I++;
58+
if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Inst)) {
5759
Changed = true;
5860
Type *ElementTy = Alloca->getType()->getPointerElementType();
5961
Constant *ElementSize =
@@ -93,6 +95,28 @@ class SimplifyAllocas : public BasicBlockPass {
9395
Alloca->replaceAllUsesWith(BC);
9496
Alloca->eraseFromParent();
9597
}
98+
else if (auto *Call = dyn_cast<IntrinsicInst>(Inst)) {
99+
if (Call->getIntrinsicID() == Intrinsic::dbg_declare) {
100+
// dbg.declare's first argument is a special metadata that wraps a
101+
// value, and RAUW works on those. It is supposed to refer to the
102+
// alloca that represents the variable's storage, but the alloca
103+
// simplification may have RAUWed it to use the bitcast.
104+
// Fix it up here by recreating the metadata to use the new alloca.
105+
auto *MV = cast<MetadataAsValue>(Call->getArgOperand(0));
106+
// Sometimes dbg.declare points to an argument instead of an alloca.
107+
if (auto *VM = dyn_cast<ValueAsMetadata>(MV->getMetadata())) {
108+
if (auto *BCInst = dyn_cast<BitCastInst>(VM->getValue())) {
109+
Value *CastSrc = BCInst->getOperand(0);
110+
assert(isa<AllocaInst>(CastSrc));
111+
Call->setArgOperand(
112+
0,
113+
MetadataAsValue::get(Inst->getContext(),
114+
ValueAsMetadata::get(CastSrc)));
115+
Changed = true;
116+
}
117+
}
118+
}
119+
}
96120
}
97121
return Changed;
98122
}

test/Transforms/NaCl/simplify-allocas.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,35 @@ define void @dyn_inst_alloca_array_zext(i8 %a) {
134134
; CHECK-NEXT: zext i8 %b to i32
135135
; CHECK-NEXT: mul i32 4,
136136
; CHECK-NEXT: %c = alloca i8, i32
137+
138+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
139+
define void @debug_declare() {
140+
%var = alloca i32
141+
call void @llvm.dbg.declare(metadata i32* %var, metadata !11, metadata !12), !dbg !13
142+
unreachable
143+
}
144+
; Ensure that the first arg to dbg.declare points to the alloca, not the bitcast
145+
; CHECK-LABEL: define void @debug_declare
146+
; CHECK-NEXT: %var = alloca i8, i32 4
147+
; CHECK: call void @llvm.dbg.declare(metadata i8* %var, metadata !11, metadata !12), !dbg !13
148+
149+
!llvm.dbg.cu = !{!0}
150+
!llvm.module.flags = !{!8, !9}
151+
!llvm.ident = !{!10}
152+
153+
; CHECK: !4 = !MDSubprogram(name: "debug_declare", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, function: void ()* @debug_declare, variables: !2)
154+
155+
!0 = !MDCompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (trunk 235150) (llvm/trunk 235152)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
156+
!1 = !MDFile(filename: "foo.c", directory: "/s/llvm/cmakebuild")
157+
!2 = !{}
158+
!3 = !{!4}
159+
!4 = !MDSubprogram(name: "debug_declare", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, function: void ()* @debug_declare, variables: !2)
160+
!5 = !MDSubroutineType(types: !6)
161+
!6 = !{null, !7}
162+
!7 = !MDBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
163+
!8 = !{i32 2, !"Dwarf Version", i32 4}
164+
!9 = !{i32 2, !"Debug Info Version", i32 3}
165+
!10 = !{!"clang version 3.7.0 (trunk 235150) (llvm/trunk 235152)"}
166+
!11 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "val", arg: 1, scope: !4, file: !1, line: 1, type: !7)
167+
!12 = !MDExpression()
168+
!13 = !MDLocation(line: 1, column: 24, scope: !4)

0 commit comments

Comments
 (0)