llvm.org GIT mirror llvm / 506a3c2
InstructionSimplify: don't speculate about Constants changing. When presented with an icmp/select pair, we can end up asking what would happen if we replaced one constant with another in an instruction. This is a mistake, while non-constant Values could become a constant, constants cannot change and trying to do so can lead to completely invalid IR (a GEP referencing a non-existant field in the original case). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303580 91177308-0d34-0410-b5e6-96231b3b80d8 Tim Northover 2 years ago
2 changed file(s) with 43 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
35383538 if (V == Op)
35393539 return RepOp;
35403540
3541 // We cannot replace a constant, and shouldn't even try.
3542 if (isa(Op))
3543 return nullptr;
3544
35413545 auto *I = dyn_cast(V);
35423546 if (!I)
35433547 return nullptr;
0 ; RUN: opt -early-cse -S %s | FileCheck %s
1
2 %mystruct = type { i32 }
3
4 ; @var is global so that *every* GEP argument is Constant.
5 @var = external global %mystruct
6
7 ; Control flow is to make the dominance tree consider the final icmp before it
8 ; gets to simplify the purely constant one (%tst). Since that icmp uses the
9 ; select that gets considered next. Finally the select simplification looks at
10 ; the %tst icmp and we don't want it to speculate about what happens if "i32 0"
11 ; is actually "i32 1", broken universes are automatic UB.
12 ;
13 ; In this case doing the speculation would create an invalid GEP(@var, 0, 1) and
14 ; crash.
15
16 define i1 @test_constant_speculation() {
17 ; CHECK-LABEL: define i1 @test_constant_speculation
18 entry:
19 br i1 undef, label %end, label %select
20
21 select:
22 ; CHECK: select:
23 ; CHECK-NOT: icmp
24 ; CHECK-NOT: getelementptr
25 ; CHECK-NOT: select
26
27 %tst = icmp eq i32 1, 0
28 %elt = getelementptr %mystruct, %mystruct* @var, i64 0, i32 0
29 %sel = select i1 %tst, i32* null, i32* %elt
30 br label %end
31
32 end:
33 ; CHECK: end:
34 ; CHECK: %tmp = phi i32* [ null, %entry ], [ getelementptr inbounds (%mystruct, %mystruct* @var, i64 0, i32 0), %select ]
35 %tmp = phi i32* [null, %entry], [%sel, %select]
36 %res = icmp eq i32* %tmp, null
37 ret i1 %res
38 }