llvm.org GIT mirror llvm / bccfc24
Change PHINode::hasConstantValue to have a DominatorTree argument instead of a bool argument, and to do the dominator check itself. This makes it eaiser to use when DominatorTree information is available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80920 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
7 changed file(s) with 49 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
3030 class ConstantRange;
3131 class APInt;
3232 class LLVMContext;
33 class DominatorTree;
3334
3435 //===----------------------------------------------------------------------===//
3536 // AllocationInst Class
19491950 /// hasConstantValue - If the specified PHI node always merges together the
19501951 /// same value, return the value, otherwise return null.
19511952 ///
1952 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1953 /// If the PHI has undef operands, but all the rest of the operands are
1954 /// some unique value, return that value if it can be proved that the
1955 /// value dominates the PHI. If DT is null, use a conservative check,
1956 /// otherwise use DT to test for dominance.
1957 ///
1958 Value *hasConstantValue(DominatorTree *DT = 0) const;
19531959
19541960 /// Methods for support type inquiry through isa, cast, and dyn_cast:
19551961 static inline bool classof(const PHINode *) { return true; }
768768 }
769769
770770 Value* GVN::CollapsePhi(PHINode* p) {
771 Value* constVal = p->hasConstantValue();
771 Value* constVal = p->hasConstantValue(DT);
772772 if (!constVal) return 0;
773773
774774 Instruction* inst = dyn_cast(constVal);
428428 PN->addIncoming(InVal, NewBB);
429429
430430 // Check to see if we can eliminate this phi node.
431 if (Value *V = PN->hasConstantValue(DT != 0)) {
432 Instruction *I = dyn_cast(V);
433 if (!I || DT == 0 || DT->dominates(I, PN)) {
434 PN->replaceAllUsesWith(V);
435 if (AA) AA->deleteValue(PN);
436 PN->eraseFromParent();
437 }
431 if (Value *V = PN->hasConstantValue(DT)) {
432 PN->replaceAllUsesWith(V);
433 if (AA) AA->deleteValue(PN);
434 PN->eraseFromParent();
438435 }
439436 }
440437
254254 PHINode *PN;
255255 for (BasicBlock::iterator I = L->getHeader()->begin();
256256 (PN = dyn_cast(I++)); )
257 if (Value *V = PN->hasConstantValue()) {
257 if (Value *V = PN->hasConstantValue(DT)) {
258258 if (AA) AA->deleteValue(PN);
259259 PN->replaceAllUsesWith(V);
260260 PN->eraseFromParent();
416416 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ) {
417417 PHINode *PN = cast(I);
418418 ++I;
419 if (Value *V = PN->hasConstantValue())
420 if (!isa(V) || DT->dominates(cast(V), PN)) {
421 // This is a degenerate PHI already, don't modify it!
422 PN->replaceAllUsesWith(V);
423 if (AA) AA->deleteValue(PN);
424 PN->eraseFromParent();
425 continue;
426 }
419 if (Value *V = PN->hasConstantValue(DT)) {
420 // This is a degenerate PHI already, don't modify it!
421 PN->replaceAllUsesWith(V);
422 if (AA) AA->deleteValue(PN);
423 PN->eraseFromParent();
424 continue;
425 }
427426
428427 // Scan this PHI node looking for a use of the PHI node by itself.
429428 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
493493 PHINode *PN = I->second;
494494
495495 // If this PHI node merges one value and/or undefs, get the value.
496 if (Value *V = PN->hasConstantValue(true)) {
497 if (!isa(V) ||
498 properlyDominates(cast(V), PN)) {
499 if (AST && isa(PN->getType()))
500 AST->deleteValue(PN);
501 PN->replaceAllUsesWith(V);
502 PN->eraseFromParent();
503 NewPhiNodes.erase(I++);
504 EliminatedAPHI = true;
505 continue;
506 }
496 if (Value *V = PN->hasConstantValue(&DT)) {
497 if (AST && isa(PN->getType()))
498 AST->deleteValue(PN);
499 PN->replaceAllUsesWith(V);
500 PN->eraseFromParent();
501 NewPhiNodes.erase(I++);
502 EliminatedAPHI = true;
503 continue;
507504 }
508505 ++I;
509506 }
1616 #include "llvm/Function.h"
1717 #include "llvm/Instructions.h"
1818 #include "llvm/Operator.h"
19 #include "llvm/Analysis/Dominators.h"
1920 #include "llvm/Support/ErrorHandling.h"
2021 #include "llvm/Support/CallSite.h"
2122 #include "llvm/Support/ConstantRange.h"
225226 /// hasConstantValue - If the specified PHI node always merges together the same
226227 /// value, return the value, otherwise return null.
227228 ///
228 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
229 /// If the PHI has undef operands, but all the rest of the operands are
230 /// some unique value, return that value if it can be proved that the
231 /// value dominates the PHI. If DT is null, use a conservative check,
232 /// otherwise use DT to test for dominance.
233 ///
234 Value *PHINode::hasConstantValue(DominatorTree *DT) const {
229235 // If the PHI node only has one incoming value, eliminate the PHI node...
230236 if (getNumIncomingValues() == 1) {
231237 if (getIncomingValue(0) != this) // not X = phi X
259265 // instruction, we cannot always return X as the result of the PHI node. Only
260266 // do this if X is not an instruction (thus it must dominate the PHI block),
261267 // or if the client is prepared to deal with this possibility.
262 if (HasUndefInput && !AllowNonDominatingInstruction)
263 if (Instruction *IV = dyn_cast(InVal))
264 // If it's in the entry block, it dominates everything.
265 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
266 isa(IV))
267 return 0; // Cannot guarantee that InVal dominates this PHINode.
268 if (HasUndefInput)
269 if (Instruction *IV = dyn_cast(InVal)) {
270 if (DT) {
271 // We have a DominatorTree. Do a precise test.
272 if (!DT->dominates(IV, this))
273 return 0;
274 } else {
275 // If it's in the entry block, it dominates everything.
276 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
277 isa(IV))
278 return 0; // Cannot guarantee that InVal dominates this PHINode.
279 }
280 }
268281
269282 // All of the incoming values are the same, return the value now.
270283 return InVal;
None ; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep undef
0 ; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {ret i8 \[%\]tmp3}
11 ; PR2503
22
33 @g_3 = external global i8 ; [#uses=2]