Skip to content

Commit ff76627

Browse files
authored
[InstCombine] Fix type mismatch between cond and value in foldSelectToCopysign (#76343)
This patch fixes the miscompilation when we try to bitcast a floating point vector into an integer scalar.
1 parent 6452395 commit ff76627

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,9 @@ static Instruction *foldSelectToCopysign(SelectInst &Sel,
23632363
Value *FVal = Sel.getFalseValue();
23642364
Type *SelType = Sel.getType();
23652365

2366+
if (ICmpInst::makeCmpResultType(TVal->getType()) != Cond->getType())
2367+
return nullptr;
2368+
23662369
// Match select ?, TC, FC where the constants are equal but negated.
23672370
// TODO: Generalize to handle a negated variable operand?
23682371
const APFloat *TC, *FC;

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,21 @@ define float @copysign_type_mismatch(double %x) {
17351735

17361736
; Negative test
17371737

1738+
define <2 x float> @copysign_type_mismatch2(<2 x float> %x) {
1739+
; CHECK-LABEL: @copysign_type_mismatch2(
1740+
; CHECK-NEXT: [[I:%.*]] = bitcast <2 x float> [[X:%.*]] to i64
1741+
; CHECK-NEXT: [[ISPOS:%.*]] = icmp sgt i64 [[I]], -1
1742+
; CHECK-NEXT: [[R:%.*]] = select i1 [[ISPOS]], <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> <float -1.000000e+00, float -1.000000e+00>
1743+
; CHECK-NEXT: ret <2 x float> [[R]]
1744+
;
1745+
%i = bitcast <2 x float> %x to i64
1746+
%ispos = icmp sgt i64 %i, -1
1747+
%r = select i1 %ispos, <2 x float> <float 1.0, float 1.0>, <2 x float> <float -1.0, float -1.0>
1748+
ret <2 x float> %r
1749+
}
1750+
1751+
; Negative test
1752+
17381753
define float @copysign_wrong_cmp(float %x) {
17391754
; CHECK-LABEL: @copysign_wrong_cmp(
17401755
; CHECK-NEXT: [[I:%.*]] = bitcast float [[X:%.*]] to i32

0 commit comments

Comments
 (0)