[PATCH] D76457: [InstCombine] Don't replace musttail result based on known bits
Nikita Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 19 15:22:32 PDT 2020
nikic created this revision.
nikic added reviewers: jdoerfert, spatel, lebedev.ri.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This is the same change as D75824 <https://reviews.llvm.org/D75824>, but for two cases where InstCombine performs the same optimization: Replacing an instructions whose bits are fully known with a constant. This is not (generally) legal for musttail calls.
I was wondering whether we should be preventing this somehow directly in computeKnownBits(), but it seemed pretty odd to me to add a check in the analysis to return a worse result if musttail is used...
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D76457
Files:
lib/Transforms/InstCombine/InstructionCombining.cpp
test/Transforms/InstCombine/call-returned.ll
Index: test/Transforms/InstCombine/call-returned.ll
===================================================================
--- /dev/null
+++ 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
+}
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2760,6 +2760,12 @@
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 @@
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 @@
// 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());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76457.251482.patch
Type: text/x-patch
Size: 3320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200319/2599107a/attachment.bin>
More information about the llvm-commits
mailing list