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

Commit 7aaca46

Browse files
author
Jan Voung
committed
SimplifyAllocas: search through more bitcasts to find alloca for llvm.dbg.declare.
The webp/snes9x build was generating more intermediate bitcasts than expected, so the fixup for making llvm.dbg.declare's first argument be an alloca was sometimes failing to find the alloca. I.e., some earlier pass like instcombine was already violating the contract that llvm.dbg.declare's first argument be an alloca or a function argument. Try to be more resilient and loop through more casts if needed. Technically llvm.dbg.declare's first argument doesn't *have* to be an alloca: http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-June/086647.html However, ReplacePtrsWithInts does not handle converting llvm.dbg.declare parameters which are bitcasts, and ends up erasing the bitcasts. This causes us to drop debug info. An alternative is to fix up ReplacePtrsWithInts (TODO), but we need to make that work with DCE. That is, the conversions inserted by ReplacePtrsWithInts must not be used solely by llvm.dbg.declare, otherwise DCE will delete those conversions and leave us with a null argument to llvm.dbg.declare. Example program state prior to SimplifyAllocas: ; Function Attrs: nounwind readonly define internal fastcc i32 @png_sig_cmp(i8* readonly %sig, i32 %start, i32 %num_to_check) #3 { entry: %png_signature = alloca i64, align 8, !dbg !68085 %tmpcast = bitcast i64* %png_signature to [8 x i8]*, !dbg !68085 call void @llvm.dbg.declare(metadata [8 x i8]* %tmpcast, metadata !68092, metadata !45244), !dbg !68085 store i64 727905341920923785, i64* %png_signature, align 8, !dbg !68085 %cmp = icmp ugt i32 %num_to_check, 8, !dbg !68093 br i1 %cmp, label %if.end3, label %if.else, !dbg !68095 // ... if.end6: // ... %gep_int6 = ptrtoint [8 x i8]* %tmpcast to i32, !dbg !68108 // ... BUG= https://code.google.com/p/nativeclient/issues/detail?id=4203 [email protected] Review URL: https://codereview.chromium.org/1166253003.
1 parent 25718c2 commit 7aaca46

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

lib/Transforms/NaCl/SimplifyAllocas.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "llvm/IR/IntrinsicInst.h"
1919
#include "llvm/IR/Module.h"
2020
#include "llvm/Transforms/NaCl.h"
21+
#include "llvm/Support/Debug.h"
2122
#include "llvm/Support/raw_ostream.h"
23+
2224
using namespace llvm;
2325
namespace {
2426
class SimplifyAllocas : public BasicBlockPass {
@@ -50,6 +52,21 @@ class SimplifyAllocas : public BasicBlockPass {
5052
return false;
5153
}
5254

55+
AllocaInst *findAllocaFromBC(BitCastInst *BCInst) {
56+
Value *Op0 = BCInst->getOperand(0);
57+
while (!llvm::isa<AllocaInst>(Op0)) {
58+
if (auto *NextBC = llvm::dyn_cast<BitCastInst>(Op0)) {
59+
Op0 = NextBC->getOperand(0);
60+
} else {
61+
dbgs() << "findAllocaFromBC encountered a non-bitcast intermediate val "
62+
<< *Op0 << " starting w/ BCInst " << *BCInst << "\n";
63+
report_fatal_error(
64+
"findAllocaFromBC encountered a non-bitcast intermediate");
65+
}
66+
}
67+
return llvm::cast<AllocaInst>(Op0);
68+
}
69+
5370
bool runOnBasicBlock(BasicBlock &BB) override {
5471
bool Changed = false;
5572
for (BasicBlock::iterator I = BB.getFirstInsertionPt(), E = BB.end();
@@ -106,12 +123,11 @@ class SimplifyAllocas : public BasicBlockPass {
106123
// Sometimes dbg.declare points to an argument instead of an alloca.
107124
if (auto *VM = dyn_cast<ValueAsMetadata>(MV->getMetadata())) {
108125
if (auto *BCInst = dyn_cast<BitCastInst>(VM->getValue())) {
109-
Value *CastSrc = BCInst->getOperand(0);
110-
assert(isa<AllocaInst>(CastSrc));
126+
AllocaInst *Alloca = findAllocaFromBC(BCInst);
111127
Call->setArgOperand(
112128
0,
113129
MetadataAsValue::get(Inst->getContext(),
114-
ValueAsMetadata::get(CastSrc)));
130+
ValueAsMetadata::get(Alloca)));
115131
Changed = true;
116132
}
117133
}

test/Transforms/NaCl/simplify-allocas.ll

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,31 +138,46 @@ define void @dyn_inst_alloca_array_zext(i8 %a) {
138138
declare void @llvm.dbg.declare(metadata, metadata, metadata)
139139
define void @debug_declare() {
140140
%var = alloca i32
141-
call void @llvm.dbg.declare(metadata i32* %var, metadata !11, metadata !12), !dbg !13
141+
call void @llvm.dbg.declare(metadata i32* %var, metadata !12, metadata !13), !dbg !14
142142
unreachable
143143
}
144144
; Ensure that the first arg to dbg.declare points to the alloca, not the bitcast
145145
; CHECK-LABEL: define void @debug_declare
146146
; CHECK-NEXT: %var = alloca i8, i32 4
147-
; CHECK: call void @llvm.dbg.declare(metadata i8* %var, metadata !11, metadata !12), !dbg !13
147+
; CHECK: call void @llvm.dbg.declare(metadata i8* %var, metadata !12, metadata !13), !dbg !14
148+
149+
define void @debug_declare_morecasts() {
150+
%var = alloca i32, i32 2, align 8
151+
%other_bc = bitcast i32* %var to i64*
152+
%other_bc2 = bitcast i64* %other_bc to i16*
153+
call void @llvm.dbg.declare(metadata i16* %other_bc2, metadata !15, metadata !13), !dbg !16
154+
unreachable
155+
}
156+
; Ensure that the first arg to dbg.declare points to the alloca, not bitcasts
157+
; CHECK-LABEL: define void @debug_declare_morecasts
158+
; CHECK-NEXT: %var = alloca i8, i32 8, align 8
159+
; CHECK: call void @llvm.dbg.declare(metadata i8* %var, metadata !15, metadata !13), !dbg !16
148160

149161
!llvm.dbg.cu = !{!0}
150-
!llvm.module.flags = !{!8, !9}
151-
!llvm.ident = !{!10}
162+
!llvm.module.flags = !{!9, !10}
163+
!llvm.ident = !{!11}
152164

153165
; 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)
154166

155167
!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)
156168
!1 = !MDFile(filename: "foo.c", directory: "/s/llvm/cmakebuild")
157169
!2 = !{}
158-
!3 = !{!4}
170+
!3 = !{!4, !8}
159171
!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)
160172
!5 = !MDSubroutineType(types: !6)
161173
!6 = !{null, !7}
162174
!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)
175+
!8 = !MDSubprogram(name: "debug_declare_morecasts", scope: !1, file: !1, line: 8, type: !5, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false, function: void ()* @debug_declare_morecasts, variables: !2)
176+
!9 = !{i32 2, !"Dwarf Version", i32 4}
177+
!10 = !{i32 2, !"Debug Info Version", i32 3}
178+
!11 = !{!"clang version 3.7.0 (trunk 235150) (llvm/trunk 235152)"}
179+
!12 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "val", arg: 1, scope: !4, file: !1, line: 1, type: !7)
180+
!13 = !MDExpression()
181+
!14 = !MDLocation(line: 1, column: 24, scope: !4)
182+
!15 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "var", arg: 1, scope: !8, file: !1, line: 9, type: !7)
183+
!16 = !MDLocation(line: 9, column: 24, scope: !8)

0 commit comments

Comments
 (0)