Skip to content

Commit b8da3a2

Browse files
committed
Reinstate r273711
r273711 was reverted by r273743. The inliner needs to know about any call sites in the inlined function. These were obscured if we replaced a call to undef with an undef but kept the call around. This fixes PR28298. llvm-svn: 273753
1 parent 580e754 commit b8da3a2

File tree

10 files changed

+103
-33
lines changed

10 files changed

+103
-33
lines changed

llvm/lib/Transforms/Scalar/EarlyCSE.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,18 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
582582
// its simpler value.
583583
if (Value *V = SimplifyInstruction(Inst, DL, &TLI, &DT, &AC)) {
584584
DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
585-
Inst->replaceAllUsesWith(V);
586-
Inst->eraseFromParent();
587-
Changed = true;
588-
++NumSimplify;
589-
continue;
585+
if (!Inst->use_empty()) {
586+
Inst->replaceAllUsesWith(V);
587+
Changed = true;
588+
}
589+
if (isInstructionTriviallyDead(Inst, &TLI)) {
590+
Inst->eraseFromParent();
591+
Changed = true;
592+
}
593+
if (Changed) {
594+
++NumSimplify;
595+
continue;
596+
}
590597
}
591598

592599
// If this is a simple instruction that we can value number, process it.

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,12 +2056,21 @@ bool GVN::processInstruction(Instruction *I) {
20562056
// "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
20572057
const DataLayout &DL = I->getModule()->getDataLayout();
20582058
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
2059-
I->replaceAllUsesWith(V);
2060-
if (MD && V->getType()->getScalarType()->isPointerTy())
2061-
MD->invalidateCachedPointerInfo(V);
2062-
markInstructionForDeletion(I);
2063-
++NumGVNSimpl;
2064-
return true;
2059+
bool Changed = false;
2060+
if (!I->use_empty()) {
2061+
I->replaceAllUsesWith(V);
2062+
Changed = true;
2063+
}
2064+
if (isInstructionTriviallyDead(I, TLI)) {
2065+
markInstructionForDeletion(I);
2066+
Changed = true;
2067+
}
2068+
if (Changed) {
2069+
if (MD && V->getType()->getScalarType()->isPointerTy())
2070+
MD->invalidateCachedPointerInfo(V);
2071+
++NumGVNSimpl;
2072+
return true;
2073+
}
20652074
}
20662075

20672076
if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,13 +1746,18 @@ bool JumpThreadingPass::DuplicateCondBranchOnPHIIntoPred(
17461746
// phi translation.
17471747
if (Value *IV =
17481748
SimplifyInstruction(New, BB->getModule()->getDataLayout())) {
1749-
delete New;
17501749
ValueMapping[&*BI] = IV;
1750+
if (!New->mayHaveSideEffects()) {
1751+
delete New;
1752+
New = nullptr;
1753+
}
17511754
} else {
1755+
ValueMapping[&*BI] = New;
1756+
}
1757+
if (New) {
17521758
// Otherwise, insert the new instruction into the block.
17531759
New->setName(BI->getName());
17541760
PredBB->getInstList().insert(OldPredBranch->getIterator(), New);
1755-
ValueMapping[&*BI] = New;
17561761
}
17571762
}
17581763

llvm/lib/Transforms/Scalar/LoopRotation.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,18 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
312312
if (V && LI->replacementPreservesLCSSAForm(C, V)) {
313313
// If so, then delete the temporary instruction and stick the folded value
314314
// in the map.
315-
delete C;
316315
ValueMap[Inst] = V;
316+
if (!C->mayHaveSideEffects()) {
317+
delete C;
318+
C = nullptr;
319+
}
317320
} else {
321+
ValueMap[Inst] = C;
322+
}
323+
if (C) {
318324
// Otherwise, stick the new instruction into the new block!
319325
C->setName(Inst->getName());
320326
C->insertBefore(LoopEntryBranch);
321-
ValueMap[Inst] = C;
322327
}
323328
}
324329

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,11 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
296296
if (Value *MappedV = VMap.lookup(V))
297297
V = MappedV;
298298

299-
VMap[&*II] = V;
300-
delete NewInst;
301-
continue;
299+
if (!NewInst->mayHaveSideEffects()) {
300+
VMap[&*II] = V;
301+
delete NewInst;
302+
continue;
303+
}
302304
}
303305
}
304306

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,23 @@ simplifyAndDCEInstruction(Instruction *I,
456456
if (Value *SimpleV = SimplifyInstruction(I, DL)) {
457457
// Add the users to the worklist. CAREFUL: an instruction can use itself,
458458
// in the case of a phi node.
459-
for (User *U : I->users())
460-
if (U != I)
459+
for (User *U : I->users()) {
460+
if (U != I) {
461461
WorkList.insert(cast<Instruction>(U));
462+
}
463+
}
462464

463465
// Replace the instruction with its simplified value.
464-
I->replaceAllUsesWith(SimpleV);
465-
I->eraseFromParent();
466-
return true;
466+
bool Changed = false;
467+
if (!I->use_empty()) {
468+
I->replaceAllUsesWith(SimpleV);
469+
Changed = true;
470+
}
471+
if (isInstructionTriviallyDead(I, TLI)) {
472+
I->eraseFromParent();
473+
Changed = true;
474+
}
475+
return Changed;
467476
}
468477
return false;
469478
}

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,18 +623,17 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool Force,
623623
// go.
624624
const DataLayout &DL = Header->getModule()->getDataLayout();
625625
const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
626-
for (BasicBlock *BB : NewLoopBlocks)
626+
for (BasicBlock *BB : NewLoopBlocks) {
627627
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
628628
Instruction *Inst = &*I++;
629629

630+
if (Value *V = SimplifyInstruction(Inst, DL))
631+
if (LI->replacementPreservesLCSSAForm(Inst, V))
632+
Inst->replaceAllUsesWith(V);
630633
if (isInstructionTriviallyDead(Inst))
631634
BB->getInstList().erase(Inst);
632-
else if (Value *V = SimplifyInstruction(Inst, DL))
633-
if (LI->replacementPreservesLCSSAForm(Inst, V)) {
634-
Inst->replaceAllUsesWith(V);
635-
BB->getInstList().erase(Inst);
636-
}
637635
}
636+
}
638637

639638
NumCompletelyUnrolled += CompletelyUnroll;
640639
++NumUnrolled;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,14 +1885,19 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL) {
18851885

18861886
// Check for trivial simplification.
18871887
if (Value *V = SimplifyInstruction(N, DL)) {
1888-
TranslateMap[&*BBI] = V;
1889-
delete N; // Instruction folded away, don't need actual inst
1888+
if (!BBI->use_empty())
1889+
TranslateMap[&*BBI] = V;
1890+
if (!N->mayHaveSideEffects()) {
1891+
delete N; // Instruction folded away, don't need actual inst
1892+
N = nullptr;
1893+
}
18901894
} else {
1891-
// Insert the new instruction into its new home.
1892-
EdgeBB->getInstList().insert(InsertPt, N);
18931895
if (!BBI->use_empty())
18941896
TranslateMap[&*BBI] = N;
18951897
}
1898+
// Insert the new instruction into its new home.
1899+
if (N)
1900+
EdgeBB->getInstList().insert(InsertPt, N);
18961901
}
18971902

18981903
// Loop over all of the edges from PredBB to BB, changing them to branch

llvm/test/Transforms/GVN/volatile.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ exit:
152152
ret i32 %add
153153
}
154154

155+
define i32 @test9(i32* %V) {
156+
entry:
157+
%load = load volatile i32, i32* %V, !range !0
158+
ret i32 %load
159+
}
160+
; CHECK-LABEL: test9
161+
; CHECK: load volatile
162+
; CHECK: ret i32 0
163+
155164
declare void @use(i32) readonly
156165
declare void @clobber(i32* %p, i32* %q)
157166

167+
!0 = !{ i32 0, i32 1 }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt -S -inline < %s | FileCheck %s
2+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
define void @test1() {
6+
entry:
7+
call void @test2()
8+
ret void
9+
}
10+
11+
define internal void @test2() {
12+
entry:
13+
call void undef()
14+
ret void
15+
}
16+
17+
; CHECK-LABEL: define void @test1(
18+
; CHECK: call void undef(
19+
; CHECK: ret void

0 commit comments

Comments
 (0)