+template <>
+struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
+
+//===----------------------------------------------------------------------===//
+// ExtractValueInst Class
+//===----------------------------------------------------------------------===//
+
+/// ExtractValueInst - This instruction extracts a struct member or array
+/// element value from an aggregate value.
+///
+class ExtractValueInst : public UnaryInstruction {
+ SmallVector<unsigned, 4> Indices;
+
+ ExtractValueInst(const ExtractValueInst &EVI);
+ void init(const unsigned *Idx, unsigned NumIdx,
+ const std::string &NameStr);
+ void init(unsigned Idx, const std::string &NameStr);
+
+ template<typename InputIterator>
+ void init(InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr,
+ // This argument ensures that we have an iterator we can
+ // do arithmetic on in constant time
+ std::random_access_iterator_tag) {
+ unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
+
+ // There's no fundamental reason why we require at least one index
+ // (other than weirdness with &*IdxBegin being invalid; see
+ // getelementptr's init routine for example). But there's no
+ // present need to support it.
+ assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
+
+ // This requires that the iterator points to contiguous memory.
+ init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
+ // we have to build an array here
+ }
+
+ /// getIndexedType - Returns the type of the element that would be extracted
+ /// with an extractvalue instruction with the specified parameters.
+ ///
+ /// Null is returned if the indices are invalid for the specified
+ /// pointer type.
+ ///
+ static const Type *getIndexedType(const Type *Agg,
+ const unsigned *Idx, unsigned NumIdx);
+
+ template<typename InputIterator>
+ static const Type *getIndexedType(const Type *Ptr,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ // This argument ensures that we
+ // have an iterator we can do
+ // arithmetic on in constant time
+ std::random_access_iterator_tag) {
+ unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
+
+ if (NumIdx > 0)
+ // This requires that the iterator points to contiguous memory.
+ return getIndexedType(Ptr, &*IdxBegin, NumIdx);
+ else
+ return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
+ }
+
+ /// Constructors - Create a extractvalue instruction with a base aggregate
+ /// value and a list of indices. The first ctor can optionally insert before
+ /// an existing instruction, the second appends the new instruction to the
+ /// specified BasicBlock.
+ template<typename InputIterator>
+ inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ Instruction *InsertBefore);
+ template<typename InputIterator>
+ inline ExtractValueInst(Value *Agg,
+ InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr, BasicBlock *InsertAtEnd);
+
+ // allocate space for exactly one operand
+ void *operator new(size_t s) {
+ return User::operator new(s, 1);
+ }
+
+public:
+ template<typename InputIterator>
+ static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr = "",
+ Instruction *InsertBefore = 0) {
+ return new
+ ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
+ }
+ template<typename InputIterator>
+ static ExtractValueInst *Create(Value *Agg,
+ InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd) {
+ return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
+ }
+
+ /// Constructors - These two creators are convenience methods because one
+ /// index extractvalue instructions are much more common than those with
+ /// more than one.
+ static ExtractValueInst *Create(Value *Agg, unsigned Idx,
+ const std::string &NameStr = "",
+ Instruction *InsertBefore = 0) {
+ unsigned Idxs[1] = { Idx };
+ return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
+ }
+ static ExtractValueInst *Create(Value *Agg, unsigned Idx,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd) {
+ unsigned Idxs[1] = { Idx };
+ return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
+ }
+
+ virtual ExtractValueInst *clone(LLVMContext &Context) const;
+
+ /// getIndexedType - Returns the type of the element that would be extracted
+ /// with an extractvalue instruction with the specified parameters.
+ ///
+ /// Null is returned if the indices are invalid for the specified
+ /// pointer type.
+ ///
+ template<typename InputIterator>
+ static const Type *getIndexedType(const Type *Ptr,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd) {
+ return getIndexedType(Ptr, IdxBegin, IdxEnd,
+ typename std::iterator_traits<InputIterator>::
+ iterator_category());
+ }
+ static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
+
+ typedef const unsigned* idx_iterator;
+ inline idx_iterator idx_begin() const { return Indices.begin(); }
+ inline idx_iterator idx_end() const { return Indices.end(); }
+
+ Value *getAggregateOperand() {
+ return getOperand(0);
+ }
+ const Value *getAggregateOperand() const {
+ return getOperand(0);
+ }
+ static unsigned getAggregateOperandIndex() {
+ return 0U; // get index for modifying correct operand
+ }
+
+ unsigned getNumIndices() const { // Note: always non-negative
+ return (unsigned)Indices.size();
+ }
+
+ bool hasIndices() const {
+ return true;
+ }
+
+ // Methods for support type inquiry through isa, cast, and dyn_cast:
+ static inline bool classof(const ExtractValueInst *) { return true; }
+ static inline bool classof(const Instruction *I) {
+ return I->getOpcode() == Instruction::ExtractValue;
+ }
+ static inline bool classof(const Value *V) {
+ return isa<Instruction>(V) && classof(cast<Instruction>(V));
+ }
+};
+
+template<typename InputIterator>
+ExtractValueInst::ExtractValueInst(Value *Agg,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ Instruction *InsertBefore)
+ : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
+ IdxBegin, IdxEnd)),
+ ExtractValue, Agg, InsertBefore) {
+ init(IdxBegin, IdxEnd, NameStr,
+ typename std::iterator_traits<InputIterator>::iterator_category());
+}
+template<typename InputIterator>
+ExtractValueInst::ExtractValueInst(Value *Agg,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd)
+ : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
+ IdxBegin, IdxEnd)),
+ ExtractValue, Agg, InsertAtEnd) {
+ init(IdxBegin, IdxEnd, NameStr,
+ typename std::iterator_traits<InputIterator>::iterator_category());
+}
+
+
+//===----------------------------------------------------------------------===//
+// InsertValueInst Class
+//===----------------------------------------------------------------------===//
+
+/// InsertValueInst - This instruction inserts a struct field of array element
+/// value into an aggregate value.
+///
+class InsertValueInst : public Instruction {
+ SmallVector<unsigned, 4> Indices;
+
+ void *operator new(size_t, unsigned); // Do not implement
+ InsertValueInst(const InsertValueInst &IVI);
+ void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
+ const std::string &NameStr);
+ void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
+
+ template<typename InputIterator>
+ void init(Value *Agg, Value *Val,
+ InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr,
+ // This argument ensures that we have an iterator we can
+ // do arithmetic on in constant time
+ std::random_access_iterator_tag) {
+ unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
+
+ // There's no fundamental reason why we require at least one index
+ // (other than weirdness with &*IdxBegin being invalid; see
+ // getelementptr's init routine for example). But there's no
+ // present need to support it.
+ assert(NumIdx > 0 && "InsertValueInst must have at least one index");
+
+ // This requires that the iterator points to contiguous memory.
+ init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
+ // we have to build an array here
+ }
+
+ /// Constructors - Create a insertvalue instruction with a base aggregate
+ /// value, a value to insert, and a list of indices. The first ctor can
+ /// optionally insert before an existing instruction, the second appends
+ /// the new instruction to the specified BasicBlock.
+ template<typename InputIterator>
+ inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ Instruction *InsertBefore);
+ template<typename InputIterator>
+ inline InsertValueInst(Value *Agg, Value *Val,
+ InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr, BasicBlock *InsertAtEnd);
+
+ /// Constructors - These two constructors are convenience methods because one
+ /// and two index insertvalue instructions are so common.
+ InsertValueInst(Value *Agg, Value *Val,
+ unsigned Idx, const std::string &NameStr = "",
+ Instruction *InsertBefore = 0);
+ InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
+ const std::string &NameStr, BasicBlock *InsertAtEnd);
+public:
+ // allocate space for exactly two operands
+ void *operator new(size_t s) {
+ return User::operator new(s, 2);
+ }
+
+ template<typename InputIterator>
+ static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr = "",
+ Instruction *InsertBefore = 0) {
+ return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
+ NameStr, InsertBefore);
+ }
+ template<typename InputIterator>
+ static InsertValueInst *Create(Value *Agg, Value *Val,
+ InputIterator IdxBegin, InputIterator IdxEnd,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd) {
+ return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
+ NameStr, InsertAtEnd);
+ }
+
+ /// Constructors - These two creators are convenience methods because one
+ /// index insertvalue instructions are much more common than those with
+ /// more than one.
+ static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
+ const std::string &NameStr = "",
+ Instruction *InsertBefore = 0) {
+ return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
+ }
+ static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd) {
+ return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
+ }
+
+ virtual InsertValueInst *clone(LLVMContext &Context) const;
+
+ /// Transparently provide more efficient getOperand methods.
+ DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
+ typedef const unsigned* idx_iterator;
+ inline idx_iterator idx_begin() const { return Indices.begin(); }
+ inline idx_iterator idx_end() const { return Indices.end(); }
+
+ Value *getAggregateOperand() {
+ return getOperand(0);
+ }
+ const Value *getAggregateOperand() const {
+ return getOperand(0);
+ }
+ static unsigned getAggregateOperandIndex() {
+ return 0U; // get index for modifying correct operand
+ }
+
+ Value *getInsertedValueOperand() {
+ return getOperand(1);
+ }
+ const Value *getInsertedValueOperand() const {
+ return getOperand(1);
+ }
+ static unsigned getInsertedValueOperandIndex() {
+ return 1U; // get index for modifying correct operand
+ }
+
+ unsigned getNumIndices() const { // Note: always non-negative
+ return (unsigned)Indices.size();
+ }
+
+ bool hasIndices() const {
+ return true;
+ }
+
+ // Methods for support type inquiry through isa, cast, and dyn_cast:
+ static inline bool classof(const InsertValueInst *) { return true; }
+ static inline bool classof(const Instruction *I) {
+ return I->getOpcode() == Instruction::InsertValue;
+ }
+ static inline bool classof(const Value *V) {
+ return isa<Instruction>(V) && classof(cast<Instruction>(V));
+ }
+};
+
+template <>
+struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
+};
+
+template<typename InputIterator>
+InsertValueInst::InsertValueInst(Value *Agg,
+ Value *Val,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ Instruction *InsertBefore)
+ : Instruction(Agg->getType(), InsertValue,
+ OperandTraits<InsertValueInst>::op_begin(this),
+ 2, InsertBefore) {
+ init(Agg, Val, IdxBegin, IdxEnd, NameStr,
+ typename std::iterator_traits<InputIterator>::iterator_category());
+}
+template<typename InputIterator>
+InsertValueInst::InsertValueInst(Value *Agg,
+ Value *Val,
+ InputIterator IdxBegin,
+ InputIterator IdxEnd,
+ const std::string &NameStr,
+ BasicBlock *InsertAtEnd)
+ : Instruction(Agg->getType(), InsertValue,
+ OperandTraits<InsertValueInst>::op_begin(this),
+ 2, InsertAtEnd) {
+ init(Agg, Val, IdxBegin, IdxEnd, NameStr,
+ typename std::iterator_traits<InputIterator>::iterator_category());
+}
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)