Skip to content

Commit 2bb2ea8

Browse files
author
James Molloy
committed
Reapply r237520 with another fix for infinite looping
SimplifyDemandedBits was "simplifying" a constant by removing just sign bits. This caused a canonicalization race between different parts of instcombine. Fix and regression test added - third time lucky? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237539 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a0eb803 commit 2bb2ea8

File tree

6 files changed

+162
-7
lines changed

6 files changed

+162
-7
lines changed

lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,15 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
435435
if (Instruction *Result = commonCastTransforms(CI))
436436
return Result;
437437

438+
// Test if the trunc is the user of a select which is part of a
439+
// minimum or maximum operation. If so, don't do any more simplification.
440+
// Even simplifying demanded bits can break the canonical form of a
441+
// min/max.
442+
Value *LHS, *RHS;
443+
if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
444+
if (matchSelectPattern(SI, LHS, RHS) != SPF_UNKNOWN)
445+
return nullptr;
446+
438447
// See if we can simplify any instructions used by the input whose sole
439448
// purpose is to compute bits we don't care about.
440449
if (SimplifyDemandedInstructionBits(CI))

lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,6 +3970,19 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
39703970
}
39713971
}
39723972

3973+
// Test if the FCmpInst instruction is used exclusively by a select as
3974+
// part of a minimum or maximum operation. If so, refrain from doing
3975+
// any other folding. This helps out other analyses which understand
3976+
// non-obfuscated minimum and maximum idioms, such as ScalarEvolution
3977+
// and CodeGen. And in this case, at least one of the comparison
3978+
// operands has at least one user besides the compare (the select),
3979+
// which would often largely negate the benefit of folding anyway.
3980+
if (I.hasOneUse())
3981+
if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
3982+
if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
3983+
(SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
3984+
return nullptr;
3985+
39733986
// Handle fcmp with constant RHS
39743987
if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
39753988
if (Instruction *LHSI = dyn_cast<Instruction>(Op0))

lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,18 +1154,30 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
11541154
}
11551155

11561156
// See if we can fold the select into one of our operands.
1157-
if (SI.getType()->isIntegerTy()) {
1157+
if (SI.getType()->isIntOrIntVectorTy()) {
11581158
if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
11591159
return FoldI;
11601160

11611161
Value *LHS, *RHS, *LHS2, *RHS2;
1162-
SelectPatternFlavor SPF = matchSelectPattern(&SI, LHS, RHS);
1162+
Instruction::CastOps CastOp;
1163+
SelectPatternFlavor SPF = matchSelectPattern(&SI, LHS, RHS, &CastOp);
11631164

1164-
// MAX(MAX(a, b), a) -> MAX(a, b)
1165-
// MIN(MIN(a, b), a) -> MIN(a, b)
1166-
// MAX(MIN(a, b), a) -> a
1167-
// MIN(MAX(a, b), a) -> a
11681165
if (SPF) {
1166+
// Canonicalize so that type casts are outside select patterns.
1167+
if (LHS->getType()->getPrimitiveSizeInBits() !=
1168+
SI.getType()->getPrimitiveSizeInBits()) {
1169+
CmpInst::Predicate Pred = getICmpPredicateForMinMax(SPF);
1170+
Value *Cmp = Builder->CreateICmp(Pred, LHS, RHS);
1171+
Value *NewSI = Builder->CreateCast(CastOp,
1172+
Builder->CreateSelect(Cmp, LHS, RHS),
1173+
SI.getType());
1174+
return ReplaceInstUsesWith(SI, NewSI);
1175+
}
1176+
1177+
// MAX(MAX(a, b), a) -> MAX(a, b)
1178+
// MIN(MIN(a, b), a) -> MIN(a, b)
1179+
// MAX(MIN(a, b), a) -> a
1180+
// MIN(MAX(a, b), a) -> a
11691181
if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2))
11701182
if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
11711183
SI, SPF, RHS))

lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "InstCombineInternal.h"
16+
#include "llvm/Analysis/ValueTracking.h"
1617
#include "llvm/IR/IntrinsicInst.h"
1718
#include "llvm/IR/PatternMatch.h"
1819

@@ -406,6 +407,12 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
406407
break;
407408
}
408409
case Instruction::Select:
410+
// If this is a select as part of a min/max pattern, don't simplify any
411+
// further in case we break the structure.
412+
Value *LHS, *RHS;
413+
if (matchSelectPattern(I, LHS, RHS) != SPF_UNKNOWN)
414+
return nullptr;
415+
409416
if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, RHSKnownZero,
410417
RHSKnownOne, Depth + 1) ||
411418
SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, LHSKnownZero,

lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,22 @@ Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
714714
return nullptr;
715715
}
716716

717+
// Test if a CmpInst instruction is used exclusively by a select as
718+
// part of a minimum or maximum operation. If so, refrain from doing
719+
// any other folding. This helps out other analyses which understand
720+
// non-obfuscated minimum and maximum idioms, such as ScalarEvolution
721+
// and CodeGen. And in this case, at least one of the comparison
722+
// operands has at least one user besides the compare (the select),
723+
// which would often largely negate the benefit of folding anyway.
724+
if (auto *CI = dyn_cast<CmpInst>(SI->getCondition())) {
725+
if (CI->hasOneUse()) {
726+
Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
727+
if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
728+
(SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
729+
return nullptr;
730+
}
731+
}
732+
717733
Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
718734
Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
719735

@@ -723,7 +739,6 @@ Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
723739
return nullptr;
724740
}
725741

726-
727742
/// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which
728743
/// has a PHI node as operand #0, see if we can fold the instruction into the
729744
/// PHI (which is only possible if all operands to the PHI are constants).
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
; RUN: opt -S -instcombine < %s | FileCheck %s
2+
3+
; CHECK-LABEL: @t1
4+
; CHECK-NEXT: icmp
5+
; CHECK-NEXT: select
6+
; CHECK-NEXT: sext
7+
define i64 @t1(i32 %a) {
8+
; This is the canonical form for a type-changing min/max.
9+
%1 = icmp slt i32 %a, 5
10+
%2 = select i1 %1, i32 %a, i32 5
11+
%3 = sext i32 %2 to i64
12+
ret i64 %3
13+
}
14+
15+
; CHECK-LABEL: @t2
16+
; CHECK-NEXT: icmp
17+
; CHECK-NEXT: select
18+
; CHECK-NEXT: sext
19+
define i64 @t2(i32 %a) {
20+
; Check this is converted into canonical form, as above.
21+
%1 = icmp slt i32 %a, 5
22+
%2 = sext i32 %a to i64
23+
%3 = select i1 %1, i64 %2, i64 5
24+
ret i64 %3
25+
}
26+
27+
; CHECK-LABEL: @t3
28+
; CHECK-NEXT: icmp
29+
; CHECK-NEXT: select
30+
; CHECK-NEXT: zext
31+
define i64 @t3(i32 %a) {
32+
; Same as @t2, with flipped operands and zext instead of sext.
33+
%1 = icmp ult i32 %a, 5
34+
%2 = zext i32 %a to i64
35+
%3 = select i1 %1, i64 5, i64 %2
36+
ret i64 %3
37+
}
38+
39+
; CHECK-LABEL: @t4
40+
; CHECK-NEXT: icmp
41+
; CHECK-NEXT: select
42+
; CHECK-NEXT: trunc
43+
define i32 @t4(i64 %a) {
44+
; Same again, with trunc.
45+
%1 = icmp slt i64 %a, 5
46+
%2 = trunc i64 %a to i32
47+
%3 = select i1 %1, i32 %2, i32 5
48+
ret i32 %3
49+
}
50+
51+
; CHECK-LABEL: @t5
52+
; CHECK-NEXT: icmp
53+
; CHECK-NEXT: zext
54+
; CHECK-NEXT: select
55+
define i64 @t5(i32 %a) {
56+
; Same as @t3, but with mismatched signedness between icmp and zext.
57+
; InstCombine should leave this alone.
58+
%1 = icmp slt i32 %a, 5
59+
%2 = zext i32 %a to i64
60+
%3 = select i1 %1, i64 5, i64 %2
61+
ret i64 %3
62+
}
63+
64+
; CHECK-LABEL: @t6
65+
; CHECK-NEXT: icmp
66+
; CHECK-NEXT: select
67+
; CHECK-NEXT: sitofp
68+
define float @t6(i32 %a) {
69+
%1 = icmp slt i32 %a, 0
70+
%2 = select i1 %1, i32 %a, i32 0
71+
%3 = sitofp i32 %2 to float
72+
ret float %3
73+
}
74+
75+
; CHECK-LABEL: @t7
76+
; CHECK-NEXT: icmp
77+
; CHECK-NEXT: select
78+
; CHECK-NEXT: trunc
79+
define i16 @t7(i32 %a) {
80+
%1 = icmp slt i32 %a, -32768
81+
%2 = trunc i32 %a to i16
82+
%3 = select i1 %1, i16 %2, i16 -32768
83+
ret i16 %3
84+
}
85+
86+
; Just check for no infinite loop. InstSimplify liked to
87+
; "simplify" -32767 by removing all the sign bits,
88+
; which led to a canonicalization fight between different
89+
; parts of instcombine.
90+
define i32 @t8(i64 %a, i32 %b) {
91+
%1 = icmp slt i64 %a, -32767
92+
%2 = select i1 %1, i64 %a, i64 -32767
93+
%3 = trunc i64 %2 to i32
94+
%4 = icmp slt i32 %b, 42
95+
%5 = select i1 %4, i32 42, i32 %3
96+
%6 = icmp ne i32 %5, %b
97+
%7 = zext i1 %6 to i32
98+
ret i32 %7
99+
}

0 commit comments

Comments
 (0)