Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 406cfb6

Browse files
committed
[CloneFunction] Don't remove side effecting calls
We were able to figure out that the result of a call is some constant. While propagating that fact, we added the constant to the value map. This is problematic because it results in us losing the call site when processing the value map. This fixes PR28802. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277611 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d619aa8 commit 406cfb6

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

lib/Analysis/InstructionSimplify.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,7 +4356,8 @@ static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV,
43564356

43574357
// Gracefully handle edge cases where the instruction is not wired into any
43584358
// parent block.
4359-
if (I->getParent())
4359+
if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) &&
4360+
!I->mayHaveSideEffects())
43604361
I->eraseFromParent();
43614362
} else {
43624363
Worklist.insert(I);
@@ -4384,7 +4385,8 @@ static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV,
43844385

43854386
// Gracefully handle edge cases where the instruction is not wired into any
43864387
// parent block.
4387-
if (I->getParent())
4388+
if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) &&
4389+
!I->mayHaveSideEffects())
43884390
I->eraseFromParent();
43894391
}
43904392
return Simplified;

lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "llvm/Transforms/Utils/Cloning.h"
17+
#include "llvm/ADT/SetVector.h"
1718
#include "llvm/ADT/SmallVector.h"
1819
#include "llvm/Analysis/ConstantFolding.h"
1920
#include "llvm/Analysis/InstructionSimplify.h"
@@ -552,9 +553,39 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
552553
// two PHINodes, the iteration over the old PHIs remains valid, and the
553554
// mapping will just map us to the new node (which may not even be a PHI
554555
// node).
556+
const DataLayout &DL = NewFunc->getParent()->getDataLayout();
557+
SmallSetVector<const Value *, 8> Worklist;
555558
for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
556-
if (PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]]))
557-
recursivelySimplifyInstruction(PN);
559+
if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
560+
Worklist.insert(PHIToResolve[Idx]);
561+
562+
// Note that we must test the size on each iteration, the worklist can grow.
563+
for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
564+
const Value *OrigV = Worklist[Idx];
565+
auto *I = cast<Instruction>(VMap.lookup(OrigV));
566+
if (!I)
567+
continue;
568+
569+
// See if this instruction simplifies.
570+
Value *SimpleV = SimplifyInstruction(I, DL);
571+
if (!SimpleV)
572+
continue;
573+
574+
// Stash away all the uses of the old instruction so we can check them for
575+
// recursive simplifications after a RAUW. This is cheaper than checking all
576+
// uses of To on the recursive step in most cases.
577+
for (const User *U : OrigV->users())
578+
Worklist.insert(cast<Instruction>(U));
579+
580+
// Replace the instruction with its simplified value.
581+
I->replaceAllUsesWith(SimpleV);
582+
583+
// If the original instruction had no side effects, remove it.
584+
if (isInstructionTriviallyDead(I))
585+
I->eraseFromParent();
586+
else
587+
VMap[OrigV] = I;
588+
}
558589

559590
// Now that the inlined function body has been fully constructed, go through
560591
// and zap unconditional fall-through branches. This happens all the time when

test/Transforms/Inline/inline_constprop.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,25 @@ return:
279279
%retval.0 = phi i32* [ %b, %if.end3 ], [ %a, %if.then ]
280280
ret i32* %retval.0
281281
}
282+
283+
declare i32 @PR28802.external(i32 returned %p1)
284+
285+
define internal i32 @PR28802.callee() {
286+
entry:
287+
br label %cont
288+
289+
cont:
290+
%0 = phi i32 [ 0, %entry ]
291+
%call = call i32 @PR28802.external(i32 %0)
292+
ret i32 %call
293+
}
294+
295+
define i32 @PR28802() {
296+
entry:
297+
%call = call i32 @PR28802.callee()
298+
ret i32 %call
299+
}
300+
301+
; CHECK-LABEL: define i32 @PR28802(
302+
; CHECK: call i32 @PR28802.external(i32 0)
303+
; CHECK: ret i32 0

0 commit comments

Comments
 (0)