llvm.org GIT mirror llvm / afc36a9
Previously, RecursivelyDeleteDeadInstructions provided an option of returning a list of pointers to Values that are deleted. This was unsafe, because the pointers in the list are, by nature of what RecursivelyDeleteDeadInstructions does, always dangling. Replace this with a simple callback mechanism. This may eventually be removed if all clients can reasonably be expected to use CallbackVH. Use this to factor out the dead-phi-cycle-elimination code from LSR utility function, and generalize it to use the RecursivelyDeleteTriviallyDeadInstructions utility function. This makes LSR more aggressive about eliminating dead PHI cycles; adjust tests to either be less trivial or to simply expect fewer instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70636 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
9 changed file(s) with 122 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
2424 class Instruction;
2525 class Pass;
2626 class AliasAnalysis;
27 class ValueDeletionListener;
2728
2829 /// DeleteDeadBlock - Delete the specified block, which must have no
2930 /// predecessors.
3536 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
3637 /// when the block has exactly one predecessor.
3738 void FoldSingleEntryPHINodes(BasicBlock *BB);
38
39
39
40 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
41 /// is dead. Also recursively delete any operands that become dead as
42 /// a result. This includes tracing the def-use list from the PHI to see if
43 /// it is ultimately unused or if it reaches an unused cycle. If a
44 /// ValueDeletionListener is specified, it is notified of the deletions.
45 void DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL = 0);
46
4047 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
4148 /// if possible. The return value indicates success or failure.
4249 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
4949 ///
5050 bool isInstructionTriviallyDead(Instruction *I);
5151
52
52 /// ValueDeletionListener - A simple abstract interface for delivering
53 /// notifications when Values are deleted.
54 ///
55 /// @todo Consider whether ValueDeletionListener can be made obsolete by
56 /// requiring clients to use CallbackVH instead.
57 class ValueDeletionListener {
58 public:
59 /// ValueWillBeDeleted - This method is called shortly before the specified
60 /// value will be deleted.
61 virtual void ValueWillBeDeleted(Value *V) = 0;
62
63 protected:
64 virtual ~ValueDeletionListener();
65 };
66
5367 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
5468 /// trivially dead instruction, delete it. If that makes any of its operands
5569 /// trivially dead, delete them too, recursively.
5670 ///
57 /// If DeadInst is specified, the vector is filled with the instructions that
58 /// are actually deleted.
71 /// If a ValueDeletionListener is specified, it is notified of instructions that
72 /// are actually deleted (before they are actually deleted).
5973 void RecursivelyDeleteTriviallyDeadInstructions(Value *V,
60 SmallVectorImpl *DeadInst = 0);
61
74 ValueDeletionListener *VDL = 0);
75
76 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
77 /// dead PHI node, due to being a def-use chain of single-use nodes that
78 /// either forms a cycle or is terminated by a trivially dead instruction,
79 /// delete it. If that makes any of its operands trivially dead, delete them
80 /// too, recursively.
81 ///
82 /// If a ValueDeletionListener is specified, it is notified of instructions that
83 /// are actually deleted (before they are actually deleted).
84 void RecursivelyDeleteDeadPHINode(PHINode *PN,
85 ValueDeletionListener *VDL = 0);
86
6287 //===----------------------------------------------------------------------===//
6388 // Control Flow Graph Restructuring.
6489 //
3131 #include "llvm/Support/Debug.h"
3232 #include "llvm/Support/Compiler.h"
3333 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ValueHandle.h"
3435 #include "llvm/Target/TargetLowering.h"
3536 #include
3637 using namespace llvm;
21372138 CondUse = &IVUsesByStride[*NewStride].Users.back();
21382139 CondStride = NewStride;
21392140 ++NumEliminated;
2141 Changed = true;
21402142 }
21412143
21422144 return Cond;
25002502 StrideOrder.clear();
25012503
25022504 // Clean up after ourselves
2503 if (!DeadInsts.empty()) {
2505 if (!DeadInsts.empty())
25042506 DeleteTriviallyDeadInstructions();
25052507
2506 BasicBlock::iterator I = L->getHeader()->begin();
2507 while (PHINode *PN = dyn_cast(I++)) {
2508 // At this point, we know that we have killed one or more IV users.
2509 // It is worth checking to see if the cannonical indvar is also
2510 // dead, so that we can remove it as well.
2511 //
2512 // We can remove a PHI if it is on a cycle in the def-use graph
2513 // where each node in the cycle has degree one, i.e. only one use,
2514 // and is an instruction with no side effects.
2515 //
2516 // FIXME: this needs to eliminate an induction variable even if it's being
2517 // compared against some value to decide loop termination.
2518 if (!PN->hasOneUse())
2519 continue;
2520
2521 SmallPtrSet PHIs;
2522 for (Instruction *J = dyn_cast(*PN->use_begin());
2523 J && J->hasOneUse() && !J->mayWriteToMemory();
2524 J = dyn_cast(*J->use_begin())) {
2525 // If we find the original PHI, we've discovered a cycle.
2526 if (J == PN) {
2527 // Break the cycle and mark the PHI for deletion.
2528 SE->deleteValueFromRecords(PN);
2529 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
2530 DeadInsts.push_back(PN);
2531 Changed = true;
2532 break;
2533 }
2534 // If we find a PHI more than once, we're on a cycle that
2535 // won't prove fruitful.
2536 if (isa(J) && !PHIs.insert(cast(J)))
2537 break;
2538 }
2539 }
2540 DeleteTriviallyDeadInstructions();
2541 }
2508 // At this point, it is worth checking to see if any recurrence PHIs are also
2509 // dead, so that we can remove them as well. To keep ScalarEvolution
2510 // current, use a ValueDeletionListener class.
2511 struct LSRListener : public ValueDeletionListener {
2512 ScalarEvolution &SE;
2513 explicit LSRListener(ScalarEvolution &se) : SE(se) {}
2514
2515 virtual void ValueWillBeDeleted(Value *V) {
2516 SE.deleteValueFromRecords(V);
2517 }
2518 } VDL(*SE);
2519 DeleteDeadPHIs(L->getHeader(), &VDL);
2520
25422521 return Changed;
25432522 }
2121 #include "llvm/Analysis/LoopInfo.h"
2222 #include "llvm/Analysis/Dominators.h"
2323 #include "llvm/Target/TargetData.h"
24 #include "llvm/Transforms/Utils/Local.h"
25 #include "llvm/Support/ValueHandle.h"
2426 #include
2527 using namespace llvm;
2628
7173 }
7274 }
7375
76
77 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
78 /// is dead. Also recursively delete any operands that become dead as
79 /// a result. This includes tracing the def-use list from the PHI to see if
80 /// it is ultimately unused or if it reaches an unused cycle. If a
81 /// ValueDeletionListener is specified, it is notified of the deletions.
82 void llvm::DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL) {
83 // Recursively deleting a PHI may cause multiple PHIs to be deleted
84 // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
85 SmallVector PHIs;
86 for (BasicBlock::iterator I = BB->begin();
87 PHINode *PN = dyn_cast(I); ++I)
88 PHIs.push_back(PN);
89
90 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
91 if (PHINode *PN = dyn_cast_or_null(PHIs[i].operator Value*()))
92 RecursivelyDeleteDeadPHINode(PN, VDL);
93 }
7494
7595 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
7696 /// if possible. The return value indicates success or failure.
1818 #include "llvm/Instructions.h"
1919 #include "llvm/Intrinsics.h"
2020 #include "llvm/IntrinsicInst.h"
21 #include "llvm/ADT/SmallPtrSet.h"
2122 #include "llvm/Analysis/ConstantFolding.h"
2223 #include "llvm/Analysis/DebugInfo.h"
2324 #include "llvm/Target/TargetData.h"
176177 return false;
177178 }
178179
180 /// ~ValueDeletionListener - A trivial dtor, defined out of line to give the
181 /// class a home.
182 llvm::ValueDeletionListener::~ValueDeletionListener() {}
183
179184 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
180185 /// trivially dead instruction, delete it. If that makes any of its operands
181186 /// trivially dead, delete them too, recursively.
182187 ///
183 /// If DeadInst is specified, the vector is filled with the instructions that
184 /// are actually deleted.
188 /// If a ValueDeletionListener is specified, it is notified of instructions that
189 /// are actually deleted (before they are actually deleted).
185190 void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V,
186 SmallVectorImpl *DeadInst) {
191 ValueDeletionListener *VDL) {
187192 Instruction *I = dyn_cast(V);
188193 if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
189194 return;
196201 DeadInsts.pop_back();
197202
198203 // If the client wanted to know, tell it about deleted instructions.
199 if (DeadInst)
200 DeadInst->push_back(I);
204 if (VDL)
205 VDL->ValueWillBeDeleted(I);
201206
202207 // Null out all of the instruction's operands to see if any operand becomes
203208 // dead as we go.
219224 }
220225 }
221226
227 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
228 /// dead PHI node, due to being a def-use chain of single-use nodes that
229 /// either forms a cycle or is terminated by a trivially dead instruction,
230 /// delete it. If that makes any of its operands trivially dead, delete them
231 /// too, recursively.
232 ///
233 /// If a ValueDeletionListener is specified, it is notified of instructions that
234 /// are actually deleted (before they are actually deleted).
235 void
236 llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, ValueDeletionListener *VDL) {
237
238 // We can remove a PHI if it is on a cycle in the def-use graph
239 // where each node in the cycle has degree one, i.e. only one use,
240 // and is an instruction with no side effects.
241 if (!PN->hasOneUse())
242 return;
243
244 SmallPtrSet PHIs;
245 PHIs.insert(PN);
246 for (Instruction *J = cast(*PN->use_begin());
247 J->hasOneUse() && !J->mayWriteToMemory();
248 J = cast(*J->use_begin()))
249 // If we find a PHI more than once, we're on a cycle that
250 // won't prove fruitful.
251 if (PHINode *JP = dyn_cast(J))
252 if (!PHIs.insert(cast(JP))) {
253 // Break the cycle and delete the PHI and its operands.
254 JP->replaceAllUsesWith(UndefValue::get(JP->getType()));
255 RecursivelyDeleteTriviallyDeadInstructions(JP, VDL);
256 break;
257 }
258 }
222259
223260 //===----------------------------------------------------------------------===//
224261 // Control Flow Graph Restructuring...
None ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin9
1 ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin9 -stats |& grep asm-printer | grep 186
0 ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin9 -stats |& grep asm-printer | grep 184
21
32 %"struct.Adv5::Ekin<3>" = type <{ i8 }>
43 %"struct.Adv5::X::Energyflux<3>" = type { double }
1616 %tmp555 = and <2 x i32> %1, < i32 -1, i32 0 > ; <<2 x i32>> [#uses=1]
1717 %2 = bitcast <2 x i32> %tmp555 to <1 x i64> ; <<1 x i64>> [#uses=1]
1818 %3 = call <1 x i64> @llvm.x86.mmx.psrli.q(<1 x i64> %0, i32 32) nounwind readnone ; <<1 x i64>> [#uses=1]
19 store <1 x i64> %sum.0.reg2mem.0, <1 x i64>* null
1920 %tmp558 = add <1 x i64> %sum.0.reg2mem.0, %2 ; <<1 x i64>> [#uses=1]
2021 %4 = call <1 x i64> @llvm.x86.mmx.psrli.q(<1 x i64> %tmp558, i32 32) nounwind readnone ; <<1 x i64>> [#uses=1]
2122 %tmp562 = add <1 x i64> %4, %3 ; <<1 x i64>> [#uses=1]
1313 %2 = tail call double @sin(double %k.0.reg2mem.0) nounwind readonly ; [#uses=1]
1414 %3 = mul double 0.000000e+00, %2 ; [#uses=1]
1515 %4 = fdiv double 1.000000e+00, %3 ; [#uses=1]
16 store double %Flint.0.reg2mem.0, double* null
17 store double %twoThrd.0.reg2mem.0, double* null
1618 %5 = add double %4, %Flint.0.reg2mem.0 ; [#uses=1]
1719 %6 = add double %k.0.reg2mem.0, 1.000000e+00 ; [#uses=1]
1820 br label %bb
None ; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis | grep {phi\\>} | count 14
0 ; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis | grep {phi\\>} | count 10
11 ; PR2570
22
33 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"