[llvm] 5c10967 - [InstCombine] Don't replace musttail result based on known bits
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 20 02:17:27 PDT 2020
Author: Nikita Popov
Date: 2020-03-20T10:17:09+01:00
New Revision: 5c10967157d11a489de544b51349d12cfb3fe7de
URL: https://github.com/llvm/llvm-project/commit/5c10967157d11a489de544b51349d12cfb3fe7de
DIFF: https://github.com/llvm/llvm-project/commit/5c10967157d11a489de544b51349d12cfb3fe7de.diff
LOG: [InstCombine] Don't replace musttail result based on known bits
This is the same change as D75824, but for two cases where
InstCombine performs the same optimization: Replacing an instruction
whose bits are fully known with a constant. This is not (generally)
legal for musttail calls.
Differential Revision: https://reviews.llvm.org/D76457
Added:
llvm/test/Transforms/InstCombine/call-returned.ll
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 9d17e92eca20..9bcd6d79756a 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2760,6 +2760,12 @@ Instruction *InstCombiner::visitFree(CallInst &FI) {
return nullptr;
}
+static bool isMustTailCall(Value *V) {
+ if (auto *CI = dyn_cast<CallInst>(V))
+ return CI->isMustTailCall();
+ return false;
+}
+
Instruction *InstCombiner::visitReturnInst(ReturnInst &RI) {
if (RI.getNumOperands() == 0) // ret void
return nullptr;
@@ -2769,6 +2775,10 @@ Instruction *InstCombiner::visitReturnInst(ReturnInst &RI) {
if (!VTy->isIntegerTy() || isa<Constant>(ResultOp))
return nullptr;
+ // Don't replace result of musttail calls.
+ if (isMustTailCall(ResultOp))
+ return nullptr;
+
// There might be assume intrinsics dominating this return that completely
// determine the value. If so, constant fold it.
KnownBits Known = computeKnownBits(ResultOp, 0, &RI);
@@ -3484,7 +3494,8 @@ bool InstCombiner::run() {
// In general, it is possible for computeKnownBits to determine all bits in
// a value even when the operands are not all constants.
Type *Ty = I->getType();
- if (ExpensiveCombines && !I->use_empty() && Ty->isIntOrIntVectorTy()) {
+ if (ExpensiveCombines && !I->use_empty() && Ty->isIntOrIntVectorTy() &&
+ !isMustTailCall(I)) {
KnownBits Known = computeKnownBits(I, /*Depth*/0, I);
if (Known.isConstant()) {
Constant *C = ConstantInt::get(Ty, Known.getConstant());
diff --git a/llvm/test/Transforms/InstCombine/call-returned.ll b/llvm/test/Transforms/InstCombine/call-returned.ll
new file mode 100644
index 000000000000..6fc20079671b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/call-returned.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -instcombine -expensive-combines=0 < %s | FileCheck %s --check-prefixes=CHECK,EXPENSIVE-OFF
+; RUN: opt -S -instcombine -expensive-combines=1 < %s | FileCheck %s --check-prefixes=CHECK,EXPENSIVE-ON
+
+declare i32 @passthru_i32(i32 returned)
+declare i8* @passthru_p8(i8* returned)
+
+define i32 @returned_const_int_arg() {
+; CHECK-LABEL: @returned_const_int_arg(
+; CHECK-NEXT: [[X:%.*]] = call i32 @passthru_i32(i32 42)
+; CHECK-NEXT: ret i32 42
+;
+ %x = call i32 @passthru_i32(i32 42)
+ ret i32 %x
+}
+
+define i8* @returned_const_ptr_arg() {
+; CHECK-LABEL: @returned_const_ptr_arg(
+; CHECK-NEXT: [[X:%.*]] = call i8* @passthru_p8(i8* null)
+; CHECK-NEXT: ret i8* [[X]]
+;
+ %x = call i8* @passthru_p8(i8* null)
+ ret i8* %x
+}
+
+define i32 @returned_var_arg(i32 %arg) {
+; CHECK-LABEL: @returned_var_arg(
+; CHECK-NEXT: [[X:%.*]] = call i32 @passthru_i32(i32 [[ARG:%.*]])
+; CHECK-NEXT: ret i32 [[X]]
+;
+ %x = call i32 @passthru_i32(i32 %arg)
+ ret i32 %x
+}
+
+define i32 @returned_const_int_arg_musttail(i32 %arg) {
+; CHECK-LABEL: @returned_const_int_arg_musttail(
+; CHECK-NEXT: [[X:%.*]] = musttail call i32 @passthru_i32(i32 42)
+; CHECK-NEXT: ret i32 [[X]]
+;
+ %x = musttail call i32 @passthru_i32(i32 42)
+ ret i32 %x
+}
+
+define i32 @returned_var_arg_musttail(i32 %arg) {
+; CHECK-LABEL: @returned_var_arg_musttail(
+; CHECK-NEXT: [[X:%.*]] = musttail call i32 @passthru_i32(i32 [[ARG:%.*]])
+; CHECK-NEXT: ret i32 [[X]]
+;
+ %x = musttail call i32 @passthru_i32(i32 %arg)
+ ret i32 %x
+}
More information about the llvm-commits
mailing list