2003-09-30 18:37:50 +00:00
|
|
|
//===-- llvm/iPHINode.h - PHI instruction definition ------------*- C++ -*-===//
|
2003-10-20 20:19:47 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 18:02:31 +00:00
|
|
|
//
|
|
|
|
// This file defines the PHINode class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_IPHINODE_H
|
|
|
|
#define LLVM_IPHINODE_H
|
|
|
|
|
|
|
|
#include "llvm/Instruction.h"
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2004-06-08 17:44:21 +00:00
|
|
|
struct BasicBlock;
|
2001-12-03 18:02:31 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PHINode Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// PHINode - The PHINode class is used to represent the magical mystical PHI
|
|
|
|
// node, that can not exist in nature, but can be synthesized in a computer
|
|
|
|
// scientist's overactive imagination.
|
|
|
|
//
|
|
|
|
class PHINode : public Instruction {
|
|
|
|
PHINode(const PHINode &PN);
|
|
|
|
public:
|
2002-09-10 15:36:11 +00:00
|
|
|
PHINode(const Type *Ty, const std::string &Name = "",
|
|
|
|
Instruction *InsertBefore = 0)
|
2003-10-19 21:34:28 +00:00
|
|
|
: Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
|
2002-09-10 15:36:11 +00:00
|
|
|
}
|
2001-12-03 18:02:31 +00:00
|
|
|
|
2004-05-27 00:15:23 +00:00
|
|
|
PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
|
|
|
|
: Instruction(Ty, Instruction::PHI, Name, InsertAtEnd) {
|
|
|
|
}
|
|
|
|
|
2001-12-03 18:02:31 +00:00
|
|
|
virtual Instruction *clone() const { return new PHINode(*this); }
|
|
|
|
|
2004-02-26 23:20:08 +00:00
|
|
|
/// getNumIncomingValues - Return the number of incoming edges
|
|
|
|
///
|
2002-09-10 15:36:11 +00:00
|
|
|
unsigned getNumIncomingValues() const { return Operands.size()/2; }
|
2001-12-03 18:02:31 +00:00
|
|
|
|
2002-09-10 15:36:11 +00:00
|
|
|
/// getIncomingValue - Return incoming value #x
|
2004-02-26 23:20:08 +00:00
|
|
|
///
|
2003-03-06 16:36:28 +00:00
|
|
|
Value *getIncomingValue(unsigned i) const {
|
|
|
|
assert(i*2 < Operands.size() && "Invalid value number!");
|
2001-12-03 18:02:31 +00:00
|
|
|
return Operands[i*2];
|
|
|
|
}
|
2002-09-10 15:36:11 +00:00
|
|
|
void setIncomingValue(unsigned i, Value *V) {
|
2003-03-06 16:36:28 +00:00
|
|
|
assert(i*2 < Operands.size() && "Invalid value number!");
|
2001-12-03 18:02:31 +00:00
|
|
|
Operands[i*2] = V;
|
|
|
|
}
|
2002-09-16 16:06:12 +00:00
|
|
|
inline unsigned getOperandNumForIncomingValue(unsigned i) {
|
|
|
|
return i*2;
|
|
|
|
}
|
2001-12-03 18:02:31 +00:00
|
|
|
|
2002-09-10 15:36:11 +00:00
|
|
|
/// getIncomingBlock - Return incoming basic block #x
|
2004-02-26 23:20:08 +00:00
|
|
|
///
|
2003-03-06 16:36:28 +00:00
|
|
|
BasicBlock *getIncomingBlock(unsigned i) const {
|
|
|
|
assert(i*2+1 < Operands.size() && "Invalid value number!");
|
2003-11-16 20:21:15 +00:00
|
|
|
return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
|
2001-12-03 18:02:31 +00:00
|
|
|
}
|
2003-03-06 16:36:28 +00:00
|
|
|
void setIncomingBlock(unsigned i, BasicBlock *BB) {
|
|
|
|
assert(i*2+1 < Operands.size() && "Invalid value number!");
|
2003-11-16 20:21:15 +00:00
|
|
|
Operands[i*2+1] = reinterpret_cast<Value*>(BB);
|
2001-12-03 18:02:31 +00:00
|
|
|
}
|
2003-03-06 16:36:28 +00:00
|
|
|
unsigned getOperandNumForIncomingBlock(unsigned i) {
|
2002-09-16 16:06:12 +00:00
|
|
|
return i*2+1;
|
|
|
|
}
|
2001-12-03 18:02:31 +00:00
|
|
|
|
2002-09-10 15:36:11 +00:00
|
|
|
/// addIncoming - Add an incoming value to the end of the PHI list
|
2004-02-26 23:20:08 +00:00
|
|
|
///
|
2004-06-11 21:12:22 +00:00
|
|
|
void addIncoming(Value *V, BasicBlock *BB) {
|
|
|
|
assert(getType() == V->getType() &&
|
2003-03-05 21:15:12 +00:00
|
|
|
"All operands to PHI node must be the same type as the PHI node!");
|
2004-06-11 21:12:22 +00:00
|
|
|
Operands.push_back(Use(V, this));
|
2003-11-16 20:21:15 +00:00
|
|
|
Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
|
2003-03-05 21:15:12 +00:00
|
|
|
}
|
2002-09-10 15:36:11 +00:00
|
|
|
|
|
|
|
/// removeIncomingValue - Remove an incoming value. This is useful if a
|
|
|
|
/// predecessor basic block is deleted. The value removed is returned.
|
2002-10-08 21:31:56 +00:00
|
|
|
///
|
|
|
|
/// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
|
|
|
|
/// is true), the PHI node is destroyed and any uses of it are replaced with
|
|
|
|
/// dummy values. The only time there should be zero incoming values to a PHI
|
|
|
|
/// node is when the block is dead, so this strategy is sound.
|
|
|
|
///
|
2003-03-05 21:15:12 +00:00
|
|
|
Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
|
|
|
|
|
|
|
|
Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
|
|
|
|
int Idx = getBasicBlockIndex(BB);
|
|
|
|
assert(Idx >= 0 && "Invalid basic block argument to remove!");
|
|
|
|
return removeIncomingValue(Idx, DeletePHIIfEmpty);
|
|
|
|
}
|
2001-12-03 18:02:31 +00:00
|
|
|
|
2002-09-10 15:36:11 +00:00
|
|
|
/// getBasicBlockIndex - Return the first index of the specified basic
|
|
|
|
/// block in the value list for this PHI. Returns -1 if no instance.
|
|
|
|
///
|
2001-12-03 18:02:31 +00:00
|
|
|
int getBasicBlockIndex(const BasicBlock *BB) const {
|
|
|
|
for (unsigned i = 0; i < Operands.size()/2; ++i)
|
|
|
|
if (getIncomingBlock(i) == BB) return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-06 16:36:28 +00:00
|
|
|
Value *getIncomingValueForBlock(const BasicBlock *BB) const {
|
|
|
|
return getIncomingValue(getBasicBlockIndex(BB));
|
|
|
|
}
|
|
|
|
|
2002-09-10 15:36:11 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2001-12-03 18:02:31 +00:00
|
|
|
static inline bool classof(const PHINode *) { return true; }
|
|
|
|
static inline bool classof(const Instruction *I) {
|
2003-10-19 21:34:28 +00:00
|
|
|
return I->getOpcode() == Instruction::PHI;
|
2001-12-03 18:02:31 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return isa<Instruction>(V) && classof(cast<Instruction>(V));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-12-03 18:02:31 +00:00
|
|
|
#endif
|