llvm.org GIT mirror llvm / 6148a49
Merging r323331: ------------------------------------------------------------------------ r323331 | spatel | 2018-01-24 16:20:37 +0100 (Wed, 24 Jan 2018) | 21 lines [ValueTracking] add recursion depth param to matchSelectPattern We're getting bug reports: https://bugs.llvm.org/show_bug.cgi?id=35807 https://bugs.llvm.org/show_bug.cgi?id=35840 https://bugs.llvm.org/show_bug.cgi?id=36045 ...where we blow up the stack in value tracking because other passes are sending in selects that have an operand that is itself the select. We don't currently have a reliable way to avoid analyzing dead code that may take non-standard forms, so bail out when things go too far. This mimics the recursion depth limitations in other parts of value tracking. Unfortunately, this pushes the underlying problems for other passes (jump-threading, simplifycfg, correlated-propagation) into hiding. If someone wants to uncover those again, the first draft of this patch on Phab would do that (it would assert rather than bail out). Differential Revision: https://reviews.llvm.org/D42442 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@323737 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 2 years ago
3 changed file(s) with 66 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
507507 /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
508508 ///
509509 SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
510 Instruction::CastOps *CastOp = nullptr);
510 Instruction::CastOps *CastOp = nullptr,
511 unsigned Depth = 0);
511512 inline SelectPatternResult
512513 matchSelectPattern(const Value *V, const Value *&LHS, const Value *&RHS,
513514 Instruction::CastOps *CastOp = nullptr) {
41644164 /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
41654165 static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
41664166 Value *CmpLHS, Value *CmpRHS,
4167 Value *TrueVal, Value *FalseVal) {
4167 Value *TVal, Value *FVal,
4168 unsigned Depth) {
41684169 // TODO: Allow FP min/max with nnan/nsz.
41694170 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
41704171
41714172 Value *A, *B;
4172 SelectPatternResult L = matchSelectPattern(TrueVal, A, B);
4173 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
41734174 if (!SelectPatternResult::isMinOrMax(L.Flavor))
41744175 return {SPF_UNKNOWN, SPNB_NA, false};
41754176
41764177 Value *C, *D;
4177 SelectPatternResult R = matchSelectPattern(FalseVal, C, D);
4178 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
41784179 if (L.Flavor != R.Flavor)
41794180 return {SPF_UNKNOWN, SPNB_NA, false};
41804181
42394240 static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
42404241 Value *CmpLHS, Value *CmpRHS,
42414242 Value *TrueVal, Value *FalseVal,
4242 Value *&LHS, Value *&RHS) {
4243 Value *&LHS, Value *&RHS,
4244 unsigned Depth) {
42434245 // Assume success. If there's no match, callers should not use these anyway.
42444246 LHS = TrueVal;
42454247 RHS = FalseVal;
42484250 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
42494251 return SPR;
42504252
4251 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
4253 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
42524254 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
42534255 return SPR;
42544256
43124314 FastMathFlags FMF,
43134315 Value *CmpLHS, Value *CmpRHS,
43144316 Value *TrueVal, Value *FalseVal,
4315 Value *&LHS, Value *&RHS) {
4317 Value *&LHS, Value *&RHS,
4318 unsigned Depth) {
43164319 LHS = CmpLHS;
43174320 RHS = CmpRHS;
43184321
44284431 }
44294432
44304433 if (CmpInst::isIntPredicate(Pred))
4431 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
4434 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
44324435
44334436 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
44344437 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
45494552 }
45504553
45514554 SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
4552 Instruction::CastOps *CastOp) {
4555 Instruction::CastOps *CastOp,
4556 unsigned Depth) {
4557 if (Depth >= MaxDepth)
4558 return {SPF_UNKNOWN, SPNB_NA, false};
4559
45534560 SelectInst *SI = dyn_cast(V);
45544561 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
45554562
45784585 FMF.setNoSignedZeros();
45794586 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
45804587 cast(TrueVal)->getOperand(0), C,
4581 LHS, RHS);
4588 LHS, RHS, Depth);
45824589 }
45834590 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
45844591 // If this is a potential fmin/fmax with a cast to integer, then ignore
45874594 FMF.setNoSignedZeros();
45884595 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
45894596 C, cast(FalseVal)->getOperand(0),
4590 LHS, RHS);
4597 LHS, RHS, Depth);
45914598 }
45924599 }
45934600 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
4594 LHS, RHS);
4601 LHS, RHS, Depth);
45954602 }
45964603
45974604 /// Return true if "icmp Pred LHS RHS" is always true.
0 ; RUN: opt -simplifycfg < %s -S | FileCheck %s
1
2 ; The dead code would cause a select that had itself
3 ; as an operand to be analyzed. This would then cause
4 ; infinite recursion and eventual crash.
5
6 define void @PR36045(i1 %t, i32* %b) {
7 ; CHECK-LABEL: @PR36045(
8 ; CHECK-NEXT: entry:
9 ; CHECK-NEXT: ret void
10 ;
11 entry:
12 br i1 %t, label %if, label %end
13
14 if:
15 br i1 %t, label %unreach, label %pre
16
17 unreach:
18 unreachable
19
20 pre:
21 %p = phi i32 [ 70, %if ], [ %sel, %for ]
22 br label %for
23
24 for:
25 %cmp = icmp sgt i32 %p, 8
26 %add = add i32 %p, 2
27 %sel = select i1 %cmp, i32 %p, i32 %add
28 %cmp21 = icmp ult i32 %sel, 21
29 br i1 %cmp21, label %pre, label %for.end
30
31 for.end:
32 br i1 %t, label %unreach2, label %then12
33
34 then12:
35 store i32 0, i32* %b
36 br label %unreach2
37
38 unreach2:
39 %spec = phi i32 [ %sel, %for.end ], [ 42, %then12 ]
40 unreachable
41
42 end:
43 ret void
44 }
45