Skip to content

Commit 34be781

Browse files
author
Felix Kellenbenz
committed
Check if dyncast was succesful
1 parent c123427 commit 34be781

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,41 +1897,46 @@ Instruction *InstCombinerImpl::visitFDiv(BinaryOperator &I) {
18971897
CallBase *Op1AsCallBase = dyn_cast<CallBase>(Op1);
18981898
LibFunc Op0LibFunc, Op1LibFunc;
18991899

1900-
TLI.getLibFunc(*Op1AsCallBase, Op1LibFunc);
1901-
TLI.getLibFunc(*Op0AsCallBase, Op0LibFunc);
1900+
if (Op1AsCallBase && Op0AsCallBase) {
1901+
TLI.getLibFunc(*Op1AsCallBase, Op1LibFunc);
1902+
TLI.getLibFunc(*Op0AsCallBase, Op0LibFunc);
19021903

1903-
bool ArgsMatch = match(Op0AsCallBase->getArgOperand(0), m_Value(Y)) &&
1904-
match(Op1AsCallBase->getArgOperand(0), m_Specific(Y));
1904+
bool ArgsMatch = match(Op0AsCallBase->getArgOperand(0), m_Value(Y)) &&
1905+
match(Op1AsCallBase->getArgOperand(0), m_Specific(Y));
19051906

1906-
bool IsTanH =
1907-
ArgsMatch &&
1908-
((Op0LibFunc == LibFunc_sinh && Op1LibFunc == LibFunc_cosh) ||
1909-
(Op0LibFunc == LibFunc_sinhf && Op1LibFunc == LibFunc_coshf) ||
1910-
(Op0LibFunc == LibFunc_sinhl && Op1LibFunc == LibFunc_coshl));
1907+
bool IsTanH =
1908+
ArgsMatch &&
1909+
((Op0LibFunc == LibFunc_sinh && Op1LibFunc == LibFunc_cosh) ||
1910+
(Op0LibFunc == LibFunc_sinhf && Op1LibFunc == LibFunc_coshf) ||
1911+
(Op0LibFunc == LibFunc_sinhl && Op1LibFunc == LibFunc_coshl));
19111912

1912-
bool IsCotH =
1913-
!IsTanH && ArgsMatch &&
1914-
((Op1LibFunc == LibFunc_sinh && Op0LibFunc == LibFunc_cosh) ||
1915-
(Op1LibFunc == LibFunc_sinhf && Op0LibFunc == LibFunc_coshf) ||
1916-
(Op1LibFunc == LibFunc_sinhl && Op0LibFunc == LibFunc_coshl));
1913+
bool IsCotH =
1914+
!IsTanH && ArgsMatch &&
1915+
((Op1LibFunc == LibFunc_sinh && Op0LibFunc == LibFunc_cosh) ||
1916+
(Op1LibFunc == LibFunc_sinhf && Op0LibFunc == LibFunc_coshf) ||
1917+
(Op1LibFunc == LibFunc_sinhl && Op0LibFunc == LibFunc_coshl));
19171918

1918-
if ((IsTanH || IsCotH) && hasFloatFn(M, &TLI, I.getType(), LibFunc_tanh,
1919-
LibFunc_tanhf, LibFunc_tanhl)) {
1919+
if ((IsTanH || IsCotH) && hasFloatFn(M, &TLI, I.getType(), LibFunc_tanh,
1920+
LibFunc_tanhf, LibFunc_tanhl)) {
19201921

1921-
Value *Res =
1922-
GetReplacement(Y, IsCotH, LibFunc_tanh, LibFunc_tanhf, LibFunc_tanhl);
1922+
Value *Res = GetReplacement(Y, IsCotH, LibFunc_tanh, LibFunc_tanhf,
1923+
LibFunc_tanhl);
1924+
1925+
Instruction *Replacement = replaceInstUsesWith(I, Res);
19231926

1924-
Instruction *Replacement = replaceInstUsesWith(I, Res);
1927+
if (!Op0AsCallBase->use_empty())
1928+
Op0AsCallBase->replaceAllUsesWith(
1929+
PoisonValue::get(Op0AsCallBase->getType()));
19251930

1926-
Op0AsCallBase->replaceAllUsesWith(
1927-
PoisonValue::get(Op0AsCallBase->getType()));
1928-
Op1AsCallBase->replaceAllUsesWith(
1929-
PoisonValue::get(Op1AsCallBase->getType()));
1931+
if (!Op1AsCallBase->use_empty())
1932+
Op1AsCallBase->replaceAllUsesWith(
1933+
PoisonValue::get(Op1AsCallBase->getType()));
19301934

1931-
Op0AsCallBase->eraseFromParent();
1932-
Op1AsCallBase->eraseFromParent();
1935+
Op0AsCallBase->eraseFromParent();
1936+
Op1AsCallBase->eraseFromParent();
19331937

1934-
return Replacement;
1938+
return Replacement;
1939+
}
19351940
}
19361941
}
19371942

llvm/test/Transforms/InstCombine/fdiv-cosh-sinh.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ define double @fdiv_reassoc_cosh_strict_sinh_strict(double %a, ptr dereferenceab
3535
;
3636
%cosh = call double @cosh(double %a)
3737
%sinh = call double @sinh(double %a)
38-
%div = fdiv reassoc double %1, %2
39-
ret double %div %cosh, %sinh
38+
%div = fdiv reassoc double %cosh, %sinh
39+
ret double %div
4040
}
4141

4242
define double @fdiv_reassoc_cosh_reassoc_sinh_strict(double %a) {

llvm/test/Transforms/InstCombine/fdiv-sinh-cosh.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ define double @fdiv_sin_cos_reassoc_multiple_uses_sinh(double %a) {
4747
%sinh = call reassoc double @sinh(double %a)
4848
%cosh = call reassoc double @cosh(double %a)
4949
%div = fdiv reassoc double %sinh, %cosh
50-
call void @use(double %cosh)
50+
call void @use(double %sinh)
5151
ret double %div
5252
}
5353

@@ -62,7 +62,7 @@ define double @fdiv_sin_cos_reassoc_multiple_uses_cosh(double %a) {
6262
%sinh = call reassoc double @sinh(double %a)
6363
%cosh = call reassoc double @cosh(double %a)
6464
%div = fdiv reassoc double %sinh, %cosh
65-
call void @use(double %2)
65+
call void @use(double %cosh)
6666
ret double %div
6767
}
6868

0 commit comments

Comments
 (0)