diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -627,11 +627,16 @@ /// Return true if the instruction may have side effects. /// + /// Side effects are: + /// * Writing to memory. + /// * Unwinding. + /// * Not returning (e.g. an infinite loop). + /// /// Note that this does not consider malloc and alloca to have side /// effects because the newly allocated memory is completely invisible to /// instructions which don't use the returned value. For cases where this /// matters, isSafeToSpeculativelyExecute may be more appropriate. - bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); } + bool mayHaveSideEffects() const; /// Return true if the instruction can be removed if the result is unused. /// diff --git a/llvm/lib/Analysis/DemandedBits.cpp b/llvm/lib/Analysis/DemandedBits.cpp --- a/llvm/lib/Analysis/DemandedBits.cpp +++ b/llvm/lib/Analysis/DemandedBits.cpp @@ -80,7 +80,7 @@ static bool isAlwaysLive(Instruction *I) { return I->isTerminator() || isa(I) || I->isEHPad() || - I->mayHaveSideEffects() || !I->willReturn(); + I->mayHaveSideEffects(); } void DemandedBits::determineLiveOperandBits( diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -6677,7 +6677,7 @@ if (auto *SI = dyn_cast(I)) return !SI->isSimple(); - return I->mayHaveSideEffects(); + return I->mayThrow() || I->mayWriteToMemory(); }; LoopProperties LP = {/* HasNoAbnormalExits */ true, diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -663,6 +663,10 @@ return isa(this); } +bool Instruction::mayHaveSideEffects() const { + return mayWriteToMemory() || mayThrow() || !willReturn(); +} + bool Instruction::isSafeToRemove() const { return (!isa(this) || !this->mayHaveSideEffects()) && !this->isTerminator(); diff --git a/llvm/lib/Transforms/Scalar/ADCE.cpp b/llvm/lib/Transforms/Scalar/ADCE.cpp --- a/llvm/lib/Transforms/Scalar/ADCE.cpp +++ b/llvm/lib/Transforms/Scalar/ADCE.cpp @@ -326,7 +326,7 @@ bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) { // TODO -- use llvm::isInstructionTriviallyDead - if (I.isEHPad() || I.mayHaveSideEffects() || !I.willReturn()) { + if (I.isEHPad() || I.mayHaveSideEffects()) { // Skip any value profile instrumentation calls if they are // instrumenting constants. if (isInstrumentsConstant(I)) diff --git a/llvm/test/Transforms/LICM/sinking.ll b/llvm/test/Transforms/LICM/sinking.ll --- a/llvm/test/Transforms/LICM/sinking.ll +++ b/llvm/test/Transforms/LICM/sinking.ll @@ -979,16 +979,16 @@ ret void } -; TODO: Should not get sunk. define i32 @not_willreturn(i8* %p) { ; CHECK-LABEL: @not_willreturn( +; CHECK-NEXT: [[X:%.*]] = call i32 @getv() #[[ATTR5:[0-9]+]] ; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK: loop: ; CHECK-NEXT: store volatile i8 0, i8* [[P:%.*]], align 1 ; CHECK-NEXT: br i1 true, label [[LOOP]], label [[OUT:%.*]] ; CHECK: out: -; CHECK-NEXT: [[X_LE:%.*]] = call i32 @getv() #[[ATTR5:[0-9]+]] -; CHECK-NEXT: ret i32 [[X_LE]] +; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X]], [[LOOP]] ] +; CHECK-NEXT: ret i32 [[X_LCSSA]] ; br label %loop diff --git a/llvm/test/Transforms/LoopDeletion/noop-loops-with-subloops.ll b/llvm/test/Transforms/LoopDeletion/noop-loops-with-subloops.ll --- a/llvm/test/Transforms/LoopDeletion/noop-loops-with-subloops.ll +++ b/llvm/test/Transforms/LoopDeletion/noop-loops-with-subloops.ll @@ -395,11 +395,16 @@ } ; Inner infinite loop hidden behind a call. -; TODO: Loop should not get deleted. define void @not_willreturn() { ; CHECK-LABEL: @not_willreturn( ; CHECK-NEXT: entry: -; CHECK-NEXT: br label [[EXIT:%.*]] +; CHECK-NEXT: br label [[LOOP:%.*]] +; CHECK: loop: +; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ] +; CHECK-NEXT: call void @sideeffect() #[[ATTR2:[0-9]+]] +; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1 +; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[IV]], 100 +; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]] ; CHECK: exit: ; CHECK-NEXT: ret void ; diff --git a/llvm/test/Transforms/SCCP/calltest.ll b/llvm/test/Transforms/SCCP/calltest.ll --- a/llvm/test/Transforms/SCCP/calltest.ll +++ b/llvm/test/Transforms/SCCP/calltest.ll @@ -34,9 +34,9 @@ ret i32 0 } -; TODO: Call should not get dropped. define i32 @test_not_willreturn() { ; CHECK-LABEL: @test_not_willreturn( +; CHECK-NEXT: [[TMP1:%.*]] = call [[EMPTY:%.*]] @has_side_effects() #[[ATTR1:[0-9]+]] ; CHECK-NEXT: ret i32 0 ; %1 = call %empty @has_side_effects() nounwind readonly