llvm.org GIT mirror llvm / b4098ba
Simplify RecursivelyDeleteDeadPHINode. The only functionality change should be that if the phi is used by a side-effect free instruction with no uses then the phi and the instruction now get zapped (checked by the unittest). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126124 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 9 years ago
2 changed file(s) with 23 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
261261
262262 /// areAllUsesEqual - Check whether the uses of a value are all the same.
263263 /// This is similar to Instruction::hasOneUse() except this will also return
264 /// true when there are multiple uses that all refer to the same value.
264 /// true when there are no uses or multiple uses that all refer to the same
265 /// value.
265266 static bool areAllUsesEqual(Instruction *I) {
266267 Value::use_iterator UI = I->use_begin();
267268 Value::use_iterator UE = I->use_end();
268269 if (UI == UE)
269 return false;
270 return true;
270271
271272 User *TheUse = *UI;
272273 for (++UI; UI != UE; ++UI) {
282283 /// delete it. If that makes any of its operands trivially dead, delete them
283284 /// too, recursively. Return true if the PHI node is actually deleted.
284285 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
285 if (PN->use_empty()) {
286 PN->eraseFromParent();
287 return true;
288 }
289
290 // We can remove a PHI if it is on a cycle in the def-use graph
291 // where each node in the cycle has degree one, i.e. only one use,
292 // and is an instruction with no side effects.
293 if (!areAllUsesEqual(PN))
294 return false;
295
296 bool Changed = false;
297 SmallPtrSet PHIs;
298 PHIs.insert(PN);
299 for (Instruction *I = cast(*PN->use_begin());
300 areAllUsesEqual(I) && !I->mayHaveSideEffects();
301 I = cast(*I->use_begin()))
302 // If we find a PHI more than once, we're on a cycle that
286 SmallPtrSet Visited;
287 for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
288 I = cast(*I->use_begin())) {
289 if (I->use_empty())
290 return RecursivelyDeleteTriviallyDeadInstructions(I);
291
292 // If we find an instruction more than once, we're on a cycle that
303293 // won't prove fruitful.
304 if (PHINode *IP = dyn_cast(I))
305 if (!PHIs.insert(IP)) {
306 // Break the cycle and delete the PHI and its operands.
307 IP->replaceAllUsesWith(UndefValue::get(IP->getType()));
308 (void)RecursivelyDeleteTriviallyDeadInstructions(IP);
309 Changed = true;
310 break;
311 }
312 return Changed;
294 if (!Visited.insert(I)) {
295 // Break the cycle and delete the instruction and its operands.
296 I->replaceAllUsesWith(UndefValue::get(I->getType()));
297 return RecursivelyDeleteTriviallyDeadInstructions(I);
298 }
299 }
300 return false;
313301 }
314302
315303 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
4646
4747 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
4848
49 builder.SetInsertPoint(bb0);
50 phi = builder.CreatePHI(Type::getInt32Ty(C));
51 builder.CreateAdd(phi, phi);
52
53 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
54
4955 bb0->dropAllReferences();
5056 bb1->dropAllReferences();
5157 delete bb0;