Skip to content

Commit 6e9b997

Browse files
committed
[LSR] Don't try to fixup uses in 'EH pad' instructions
The added test case crashes before this fix: ``` opt: /repositories/llvm-project/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:5172: BasicBlock::iterator (anonymous namespace)::LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator, const (anonymous namespace)::LSRFixup &, const (anonymous namespace)::LSRUse &, llvm::SCEVExpander &) const: Assertion `!isa<PHINode>(LowestIP) && !LowestIP->isEHPad() && !isa<DbgInfoIntrinsic>(LowestIP) && "Insertion point must be a normal instruction"' failed. ``` This is fully analogous to the previous commit, with the pointer constant replaced to be something non-null. The comparison here can be strength-reduced, but the second operand of the comparison happens to be identical to the constant pointer in the `catch` case of `landingpad`. While LSRInstance::CollectLoopInvariantFixupsAndFormulae() already gave up on uses in blocks ending up with EH pads, it didn't consider this case. Eventually, `LSRInstance::AdjustInsertPositionForExpand()` will be called, but the original insertion point it will get is the user instruction itself, and it doesn't want to deal with EH pads, and asserts as much. It would seem that this basically never happens in-the-wild, otherwise it would have been reported already, so it seems safe to take the cautious approach, and just not deal with such users.
1 parent 23d591e commit 6e9b997

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,6 +3430,9 @@ LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
34303430
// Ignore non-instructions.
34313431
if (!UserInst)
34323432
continue;
3433+
// Don't bother if the instruction is an EHPad.
3434+
if (UserInst->isEHPad())
3435+
continue;
34333436
// Ignore instructions in other functions (as can happen with
34343437
// Constants).
34353438
if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -loop-reduce < %s | FileCheck %s
3+
4+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
declare void @maybe_throws()
8+
declare void @use1(i1)
9+
10+
define void @is_not_42(i8* %baseptr, i8* %finalptr) local_unnamed_addr align 2 personality i8* undef {
11+
; CHECK-LABEL: @is_not_42(
12+
; CHECK-NEXT: preheader:
13+
; CHECK-NEXT: [[BASEPTR1:%.*]] = ptrtoint i8* [[BASEPTR:%.*]] to i64
14+
; CHECK-NEXT: [[TMP0:%.*]] = sub i64 0, [[BASEPTR1]]
15+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, i8* inttoptr (i64 42 to i8*), i64 [[TMP0]]
16+
; CHECK-NEXT: br label [[HEADER:%.*]]
17+
; CHECK: header:
18+
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i8* [ [[SCEVGEP2:%.*]], [[LATCH:%.*]] ], [ [[SCEVGEP]], [[PREHEADER:%.*]] ]
19+
; CHECK-NEXT: invoke void @maybe_throws()
20+
; CHECK-NEXT: to label [[LATCH]] unwind label [[LPAD:%.*]]
21+
; CHECK: lpad:
22+
; CHECK-NEXT: [[TMP1:%.*]] = landingpad { i8*, i32 }
23+
; CHECK-NEXT: catch i8* inttoptr (i64 42 to i8*)
24+
; CHECK-NEXT: [[PTR_IS_NOT_42:%.*]] = icmp ne i8* [[LSR_IV]], null
25+
; CHECK-NEXT: call void @use1(i1 [[PTR_IS_NOT_42]])
26+
; CHECK-NEXT: ret void
27+
; CHECK: latch:
28+
; CHECK-NEXT: [[SCEVGEP2]] = getelementptr i8, i8* [[LSR_IV]], i64 -1
29+
; CHECK-NEXT: br label [[HEADER]]
30+
;
31+
preheader:
32+
br label %header
33+
34+
header:
35+
%ptr = phi i8* [ %incptr, %latch ], [ %baseptr, %preheader ]
36+
invoke void @maybe_throws() to label %latch unwind label %lpad
37+
38+
lpad:
39+
landingpad { i8*, i32 } catch i8* inttoptr (i64 42 to i8*)
40+
%ptr_is_not_42 = icmp ne i8* %ptr, inttoptr (i64 42 to i8*)
41+
call void @use1(i1 %ptr_is_not_42)
42+
ret void
43+
44+
latch:
45+
%incptr = getelementptr inbounds i8, i8* %ptr, i64 1
46+
br label %header
47+
}

0 commit comments

Comments
 (0)