llvm.org GIT mirror llvm / efe6536
merge of use-diet branch to trunk git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50943 91177308-0d34-0410-b5e6-96231b3b80d8 Gabor Greif 11 years ago
18 changed file(s) with 1550 addition(s) and 606 deletion(s). Raw diff Collapse all Expand all
109109 D: Miscellaneous bug fixes
110110 D: Register allocation refactoring
111111
112 N: Gabor Greif
113 E: ggreif@gmail.com
114 D: Improvements for space efficiency
115
112116 N: Gordon Henriksen
113117 E: gordonhenriksen@mac.com
114118 D: Pluggable GC support
2121
2222 #include "llvm/Constant.h"
2323 #include "llvm/Type.h"
24 #include "llvm/OperandTraits.h"
2425 #include "llvm/ADT/APInt.h"
2526 #include "llvm/ADT/APFloat.h"
2627
317318 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
318319 protected:
319320 ConstantArray(const ArrayType *T, const std::vector &Val);
320 ~ConstantArray();
321321 public:
322322 /// get() - Static factory methods - Return objects of the specified value
323323 static Constant *get(const ArrayType *T, const std::vector &);
335335 /// null termination.
336336 static Constant *get(const std::string &Initializer, bool AddNull = true);
337337
338 /// Transparently provide more efficient getOperand methods.
339 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
340
338341 /// getType - Specialize the getType() method to always return an ArrayType,
339342 /// which reduces the amount of casting needed in parts of the compiler.
340343 ///
373376 }
374377 };
375378
379 template <>
380 struct OperandTraits : VariadicOperandTraits<> {
381 };
382
383 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
376384
377385 //===----------------------------------------------------------------------===//
378386 // ConstantStruct - Constant Struct Declarations
383391 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
384392 protected:
385393 ConstantStruct(const StructType *T, const std::vector &Val);
386 ~ConstantStruct();
387394 public:
388395 /// get() - Static factory methods - Return objects of the specified value
389396 ///
395402 return get(std::vector(Vals, Vals+NumVals), Packed);
396403 }
397404
405 /// Transparently provide more efficient getOperand methods.
406 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
407
398408 /// getType() specialization - Reduce amount of casting...
399409 ///
400410 inline const StructType *getType() const {
417427 return V->getValueID() == ConstantStructVal;
418428 }
419429 };
430
431 template <>
432 struct OperandTraits : VariadicOperandTraits<> {
433 };
434
435 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
420436
421437 //===----------------------------------------------------------------------===//
422438 /// ConstantVector - Constant Vector Declarations
427443 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
428444 protected:
429445 ConstantVector(const VectorType *T, const std::vector &Val);
430 ~ConstantVector();
431446 public:
432447 /// get() - Static factory methods - Return objects of the specified value
433448 static Constant *get(const VectorType *T, const std::vector &);
437452 return get(std::vector(Vals, Vals+NumVals));
438453 }
439454
455 /// Transparently provide more efficient getOperand methods.
456 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
457
440458 /// getType - Specialize the getType() method to always return a VectorType,
441459 /// which reduces the amount of casting needed in parts of the compiler.
442460 ///
473491 return V->getValueID() == ConstantVectorVal;
474492 }
475493 };
494
495 template <>
496 struct OperandTraits : VariadicOperandTraits<> {
497 };
498
499 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
476500
477501 //===----------------------------------------------------------------------===//
478502 /// ConstantPointerNull - a constant pointer value that points to null
571595 static Constant *getPtrToInt(Constant *C, const Type *Ty);
572596 static Constant *getIntToPtr(Constant *C, const Type *Ty);
573597 static Constant *getBitCast (Constant *C, const Type *Ty);
598
599 /// Transparently provide more efficient getOperand methods.
600 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
574601
575602 // @brief Convenience function for getting one of the casting operations
576603 // using a CastOps opcode.
708735 virtual void destroyConstant();
709736 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
710737
711 /// Override methods to provide more type information...
738 /* /// Override methods to provide more type information...
712739 inline Constant *getOperand(unsigned i) {
713740 return cast(User::getOperand(i));
714741 }
715742 inline Constant *getOperand(unsigned i) const {
716743 return const_cast(cast(User::getOperand(i)));
717 }
744 }*/
718745
719746
720747 /// Methods for support type inquiry through isa, cast, and dyn_cast:
724751 }
725752 };
726753
754 template <>
755 struct OperandTraits : VariadicOperandTraits<1> {
756 };
757
758 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
727759
728760 //===----------------------------------------------------------------------===//
729761 /// UndefValue - 'undef' values are things that do not have specified contents.
1515 #define LLVM_GLOBAL_ALIAS_H
1616
1717 #include "llvm/GlobalValue.h"
18 #include "llvm/OperandTraits.h"
1819
1920 namespace llvm {
2021
4142 GlobalAlias *getPrev() { return Prev; }
4243 const GlobalAlias *getPrev() const { return Prev; }
4344
44 Use Aliasee;
4545 public:
46 // allocate space for exactly zero operands
46 // allocate space for exactly one operand
4747 void *operator new(size_t s) {
48 return User::operator new(s, 0);
48 return User::operator new(s, 1);
4949 }
5050 /// GlobalAlias ctor - If a parent module is specified, the alias is
5151 /// automatically inserted into the end of the specified module's alias list.
5252 GlobalAlias(const Type *Ty, LinkageTypes Linkage, const std::string &Name = "",
5353 Constant* Aliasee = 0, Module *Parent = 0);
54
55 /// Provide fast operand accessors
56 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
5457
5558 /// isDeclaration - Is this global variable lacking an initializer? If so,
5659 /// the global variable is defined in some other translation unit, and is thus
9497 }
9598 };
9699
100 template <>
101 struct OperandTraits : FixedNumOperandTraits<1> {
102 };
103
104 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Value)
105
97106 } // End llvm namespace
98107
99108 #endif
2020 #define LLVM_GLOBAL_VARIABLE_H
2121
2222 #include "llvm/GlobalValue.h"
23 #include "llvm/OperandTraits.h"
2324
2425 namespace llvm {
2526
4344
4445 bool isConstantGlobal : 1; // Is this a global constant?
4546 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
46 Use Initializer;
4747
4848 public:
49 // allocate space for exactly zero operands
49 // allocate space for exactly one operand
5050 void *operator new(size_t s) {
51 return User::operator new(s, 0);
51 return User::operator new(s, 1);
5252 }
5353 /// GlobalVariable ctor - If a parent module is specified, the global is
5454 /// automatically inserted into the end of the specified modules global list.
5555 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
5656 Constant *Initializer = 0, const std::string &Name = "",
57 Module *Parent = 0, bool ThreadLocal = false,
57 Module *Parent = 0, bool ThreadLocal = false,
5858 unsigned AddressSpace = 0);
5959 /// GlobalVariable ctor - This creates a global and inserts it before the
6060 /// specified other global.
6161 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
6262 Constant *Initializer, const std::string &Name,
63 GlobalVariable *InsertBefore, bool ThreadLocal = false,
63 GlobalVariable *InsertBefore, bool ThreadLocal = false,
6464 unsigned AddressSpace = 0);
65
65
66 ~GlobalVariable() {
67 NumOperands = 1; // FIXME: needed by operator delete
68 }
69
70 /// Provide fast operand accessors
71 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
72
6673 /// isDeclaration - Is this global variable lacking an initializer? If so,
6774 /// the global variable is defined in some other translation unit, and is thus
6875 /// only a declaration here.
7885 /// illegal to call this method if the global is external, because we cannot
7986 /// tell what the value is initialized to!
8087 ///
81 inline Constant *getInitializer() const {
88 inline /*const FIXME*/ Constant *getInitializer() const {
8289 assert(hasInitializer() && "GV doesn't have initializer!");
83 return reinterpret_cast(Initializer.get());
90 return static_cast(Op<0>().get());
8491 }
8592 inline Constant *getInitializer() {
8693 assert(hasInitializer() && "GV doesn't have initializer!");
87 return reinterpret_cast(Initializer.get());
94 return static_cast(Op<0>().get());
8895 }
8996 inline void setInitializer(Constant *CPV) {
9097 if (CPV == 0) {
9198 if (hasInitializer()) {
92 Initializer.set(0);
99 Op<0>().set(0);
93100 NumOperands = 0;
94101 }
95102 } else {
96103 if (!hasInitializer())
97104 NumOperands = 1;
98 Initializer.set(CPV);
105 Op<0>().set(CPV);
99106 }
100107 }
101108
140147 const GlobalVariable *getPrev() const { return Prev; }
141148 };
142149
150 template <>
151 struct OperandTraits : OptionalOperandTraits<> {
152 };
153
154 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
155
143156 } // End llvm namespace
144157
145158 #endif
1616 #define LLVM_INSTRUCTION_TYPES_H
1717
1818 #include "llvm/Instruction.h"
19 #include "llvm/OperandTraits.h"
1920
2021 namespace llvm {
2122
7778 }
7879 };
7980
81
8082 //===----------------------------------------------------------------------===//
8183 // UnaryInstruction Class
8284 //===----------------------------------------------------------------------===//
8385
8486 class UnaryInstruction : public Instruction {
8587 void *operator new(size_t, unsigned); // Do not implement
86 Use Op;
8788
88 // avoiding warning: 'this' : used in base member initializer list
89 UnaryInstruction* this_() { return this; }
9089 protected:
91 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB =0)
92 : Instruction(Ty, iType, &Op, 1, IB), Op(V, this_()) {
90 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
91 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
92 Op<0>() = V;
9393 }
9494 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
95 : Instruction(Ty, iType, &Op, 1, IAE), Op(V, this_()) {
95 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
96 Op<0>() = V;
9697 }
9798 public:
9899 // allocate space for exactly one operand
103104 // Out of line virtual method, so the vtable, etc has a home.
104105 ~UnaryInstruction();
105106
106 // Transparently provide more efficient getOperand methods.
107 Value *getOperand(unsigned i) const {
108 assert(i == 0 && "getOperand() out of range!");
109 return Op;
110 }
111 void setOperand(unsigned i, Value *Val) {
112 assert(i == 0 && "setOperand() out of range!");
113 Op = Val;
114 }
115 unsigned getNumOperands() const { return 1; }
107 /// Transparently provide more efficient getOperand methods.
108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
116109
117110 // Methods for support type inquiry through isa, cast, and dyn_cast:
118111 static inline bool classof(const UnaryInstruction *) { return true; }
129122 }
130123 };
131124
125 template <>
126 struct OperandTraits : FixedNumOperandTraits<1> {
127 };
128
129 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
130
132131 //===----------------------------------------------------------------------===//
133132 // BinaryOperator Class
134133 //===----------------------------------------------------------------------===//
135134
136135 class BinaryOperator : public Instruction {
137136 void *operator new(size_t, unsigned); // Do not implement
138 Use Ops[2];
139137 protected:
140138 void init(BinaryOps iType);
141139 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
149147 }
150148
151149 /// Transparently provide more efficient getOperand methods.
152 Value *getOperand(unsigned i) const {
153 assert(i < 2 && "getOperand() out of range!");
154 return Ops[i];
155 }
156 void setOperand(unsigned i, Value *Val) {
157 assert(i < 2 && "setOperand() out of range!");
158 Ops[i] = Val;
159 }
160 unsigned getNumOperands() const { return 2; }
150 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
161151
162152 /// create() - Construct a binary instruction, given the opcode and the two
163153 /// operands. Optionally (if InstBefore is specified) insert the instruction
249239 return isa(V) && classof(cast(V));
250240 }
251241 };
242
243 template <>
244 struct OperandTraits : FixedNumOperandTraits<2> {
245 };
246
247 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
252248
253249 //===----------------------------------------------------------------------===//
254250 // CastInst Class
496492
497493 /// This class is the base class for the comparison instructions.
498494 /// @brief Abstract base class of comparison instructions.
495 // FIXME: why not derive from BinaryOperator?
499496 class CmpInst: public Instruction {
500497 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
501498 CmpInst(); // do not implement
506503 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
507504 const std::string &Name, BasicBlock *InsertAtEnd);
508505
509 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
510
511506 public:
512507 // allocate space for exactly two operands
513508 void *operator new(size_t s) {
547542 }
548543
549544 /// @brief Provide more efficient getOperand methods.
550 Value *getOperand(unsigned i) const {
551 assert(i < 2 && "getOperand() out of range!");
552 return Ops[i];
553 }
554 void setOperand(unsigned i, Value *Val) {
555 assert(i < 2 && "setOperand() out of range!");
556 Ops[i] = Val;
557 }
558
559 /// @brief CmpInst instructions always have 2 operands.
560 unsigned getNumOperands() const { return 2; }
545 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
561546
562547 /// This is just a convenience that dispatches to the subclasses.
563548 /// @brief Swap the operands and adjust predicate accordingly to retain
597582 }
598583 };
599584
585
586 // FIXME: these are redundant if CmpInst < BinaryOperator
587 template <>
588 struct OperandTraits : FixedNumOperandTraits<2> {
589 };
590
591 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
592
600593 } // End llvm namespace
601594
602595 #endif
1919 namespace llvm {
2020
2121 struct AssemblyAnnotationWriter;
22 class BinaryOperator;
2322
2423 template
2524 class SymbolTableListTraits;
2020 #include "llvm/InstrTypes.h"
2121 #include "llvm/DerivedTypes.h"
2222 #include "llvm/ParameterAttributes.h"
23 #include "llvm/BasicBlock.h"
2324
2425 namespace llvm {
2526
26 class BasicBlock;
2727 class ConstantInt;
2828 class PointerType;
2929 class VectorType;
287287 ///
288288 class StoreInst : public Instruction {
289289 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
290 Use Ops[2];
291290
292 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
293 Ops[0].init(SI.Ops[0], this);
294 Ops[1].init(SI.Ops[1], this);
291 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
292 &Op<0>(), 2) {
293 Op<0>().init(SI.Op<0>(), this);
294 Op<1>().init(SI.Op<1>(), this);
295295 setVolatile(SI.isVolatile());
296296 setAlignment(SI.getAlignment());
297297
328328 }
329329
330330 /// Transparently provide more efficient getOperand methods.
331 Value *getOperand(unsigned i) const {
332 assert(i < 2 && "getOperand() out of range!");
333 return Ops[i];
334 }
335 void setOperand(unsigned i, Value *Val) {
336 assert(i < 2 && "setOperand() out of range!");
337 Ops[i] = Val;
338 }
339 unsigned getNumOperands() const { return 2; }
331 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
340332
341333 /// getAlignment - Return the alignment of the access that is being performed
342334 ///
362354 }
363355 };
364356
357 template <>
358 struct OperandTraits : FixedNumOperandTraits<2> {
359 };
360
361 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
365362
366363 //===----------------------------------------------------------------------===//
367364 // GetElementPtrInst Class
379376 /// access elements of arrays and structs
380377 ///
381378 class GetElementPtrInst : public Instruction {
382 GetElementPtrInst(const GetElementPtrInst &GEPI)
383 : Instruction(reinterpret_cast(GEPI.getType()), GetElementPtr,
384 0, GEPI.getNumOperands()) {
385 Use *OL = OperandList = new Use[NumOperands];
386 Use *GEPIOL = GEPI.OperandList;
387 for (unsigned i = 0, E = NumOperands; i != E; ++i)
388 OL[i].init(GEPIOL[i], this);
389 }
379 GetElementPtrInst(const GetElementPtrInst &GEPI);
390380 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
391381 void init(Value *Ptr, Value *Idx);
392382
399389 unsigned NumIdx = static_cast(std::distance(IdxBegin, IdxEnd));
400390
401391 if (NumIdx > 0) {
402 // This requires that the itoerator points to contiguous memory.
403 init(Ptr, &*IdxBegin, NumIdx);
392 // This requires that the iterator points to contiguous memory.
393 init(Ptr, &*IdxBegin, NumIdx); // FIXME: for the general case
394 // we have to build an array here
404395 }
405396 else {
406397 init(Ptr, 0, NumIdx);
445436 /// instruction, the second appends the new instruction to the specified
446437 /// BasicBlock.
447438 template
448 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
449 InputIterator IdxEnd,
450 const std::string &Name = "",
451 Instruction *InsertBefore = 0)
452 : Instruction(PointerType::get(
453 checkType(getIndexedType(Ptr->getType(),
454 IdxBegin, IdxEnd, true)),
455 cast(Ptr->getType())->getAddressSpace()),
456 GetElementPtr, 0, 0, InsertBefore) {
457 init(Ptr, IdxBegin, IdxEnd, Name,
458 typename std::iterator_traits::iterator_category());
459 }
439 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
440 InputIterator IdxEnd,
441 unsigned Values,
442 const std::string &Name,
443 Instruction *InsertBefore);
460444 template
461 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
462 const std::string &Name, BasicBlock *InsertAtEnd)
463 : Instruction(PointerType::get(
464 checkType(getIndexedType(Ptr->getType(),
465 IdxBegin, IdxEnd, true)),
466 cast(Ptr->getType())->getAddressSpace()),
467 GetElementPtr, 0, 0, InsertAtEnd) {
468 init(Ptr, IdxBegin, IdxEnd, Name,
469 typename std::iterator_traits::iterator_category());
470 }
445 inline GetElementPtrInst(Value *Ptr,
446 InputIterator IdxBegin, InputIterator IdxEnd,
447 unsigned Values,
448 const std::string &Name, BasicBlock *InsertAtEnd);
471449
472450 /// Constructors - These two constructors are convenience methods because one
473451 /// and two index getelementptr instructions are so common.
474 GetElementPtrInst(Value *Ptr, Value *Idx,
475 const std::string &Name = "", Instruction *InsertBefore = 0);
452 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name = "",
453 Instruction *InsertBefore = 0);
476454 GetElementPtrInst(Value *Ptr, Value *Idx,
477455 const std::string &Name, BasicBlock *InsertAtEnd);
478456 public:
481459 InputIterator IdxEnd,
482460 const std::string &Name = "",
483461 Instruction *InsertBefore = 0) {
484 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertBefore);
462 typename std::iterator_traits::difference_type Values =
463 1 + std::distance(IdxBegin, IdxEnd);
464 return new(Values)
465 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertBefore);
485466 }
486467 template
487 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
488 const std::string &Name, BasicBlock *InsertAtEnd) {
489 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertAtEnd);
490 }
491
492 /// Constructors - These two constructors are convenience methods because one
493 /// and two index getelementptr instructions are so common.
468 static GetElementPtrInst *Create(Value *Ptr,
469 InputIterator IdxBegin, InputIterator IdxEnd,
470 const std::string &Name,
471 BasicBlock *InsertAtEnd) {
472 typename std::iterator_traits::difference_type Values =
473 1 + std::distance(IdxBegin, IdxEnd);
474 return new(Values)
475 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
476 }
477
478 /// Constructors - These two creators are convenience methods because one
479 /// index getelementptr instructions are so common.
494480 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
495 const std::string &Name = "", Instruction *InsertBefore = 0) {
496 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
481 const std::string &Name = "",
482 Instruction *InsertBefore = 0) {
483 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
497484 }
498485 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
499 const std::string &Name, BasicBlock *InsertAtEnd) {
500 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
501 }
502 ~GetElementPtrInst();
486 const std::string &Name,
487 BasicBlock *InsertAtEnd) {
488 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
489 }
503490
504491 virtual GetElementPtrInst *clone() const;
492
493 /// Transparently provide more efficient getOperand methods.
494 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
505495
506496 // getType - Overload to return most specific pointer type...
507497 const PointerType *getType() const {
568558 return isa(V) && classof(cast(V));
569559 }
570560 };
561
562 template <>
563 struct OperandTraits : VariadicOperandTraits<1> {
564 };
565
566 template
567 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
568 InputIterator IdxBegin,
569 InputIterator IdxEnd,
570 unsigned Values,
571 const std::string &Name,
572 Instruction *InsertBefore)
573 : Instruction(PointerType::get(checkType(
574 getIndexedType(Ptr->getType(),
575 IdxBegin, IdxEnd, true)),
576 cast(Ptr->getType())
577 ->getAddressSpace()),
578 GetElementPtr,
579 OperandTraits::op_end(this) - Values,
580 Values, InsertBefore) {
581 init(Ptr, IdxBegin, IdxEnd, Name,
582 typename std::iterator_traits::iterator_category());
583 }
584 template
585 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
586 InputIterator IdxBegin,
587 InputIterator IdxEnd,
588 unsigned Values,
589 const std::string &Name,
590 BasicBlock *InsertAtEnd)
591 : Instruction(PointerType::get(checkType(
592 getIndexedType(Ptr->getType(),
593 IdxBegin, IdxEnd, true)),
594 cast(Ptr->getType())
595 ->getAddressSpace()),
596 GetElementPtr,
597 OperandTraits::op_end(this) - Values,
598 Values, InsertAtEnd) {
599 init(Ptr, IdxBegin, IdxEnd, Name,
600 typename std::iterator_traits::iterator_category());
601 }
602
603
604 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
605
571606
572607 //===----------------------------------------------------------------------===//
573608 // ICmpInst Class
722757 /// @brief Swap operands and adjust predicate.
723758 void swapOperands() {
724759 SubclassData = getSwappedPredicate();
725 std::swap(Ops[0], Ops[1]);
760 std::swap(Op<0>(), Op<1>());
726761 }
727762
728763 virtual ICmpInst *clone() const;
846881 /// @brief Swap operands and adjust predicate.
847882 void swapOperands() {
848883 SubclassData = getSwappedPredicate();
849 std::swap(Ops[0], Ops[1]);
884 std::swap(Op<0>(), Op<1>());
850885 }
851886
852887 virtual FCmpInst *clone() const;
899934 /// @brief Construct a CallInst from a range of arguments
900935 template
901936 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
902 const std::string &Name = "", Instruction *InsertBefore = 0)
903 : Instruction(cast(cast(Func->getType())
904 ->getElementType())->getReturnType(),
905 Instruction::Call, 0, 0, InsertBefore) {
906 init(Func, ArgBegin, ArgEnd, Name,
907 typename std::iterator_traits::iterator_category());
908 }
937 const std::string &Name, Instruction *InsertBefore);
909938
910939 /// Construct a CallInst given a range of arguments. InputIterator
911940 /// must be a random-access iterator pointing to contiguous storage
914943 /// incur runtime overhead.
915944 /// @brief Construct a CallInst from a range of arguments
916945 template
917 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
918 const std::string &Name, BasicBlock *InsertAtEnd)
919 : Instruction(cast(cast(Func->getType())
920 ->getElementType())->getReturnType(),
921 Instruction::Call, 0, 0, InsertAtEnd) {
922 init(Func, ArgBegin, ArgEnd, Name,
923 typename std::iterator_traits::iterator_category());
924 }
925
926 CallInst(Value *F, Value *Actual, const std::string& Name = "",
927 Instruction *InsertBefore = 0);
946 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
947 const std::string &Name, BasicBlock *InsertAtEnd);
948
949 CallInst(Value *F, Value *Actual, const std::string& Name,
950 Instruction *InsertBefore);
928951 CallInst(Value *F, Value *Actual, const std::string& Name,
929952 BasicBlock *InsertAtEnd);
930 explicit CallInst(Value *F, const std::string &Name = "",
931 Instruction *InsertBefore = 0);
953 explicit CallInst(Value *F, const std::string &Name,
954 Instruction *InsertBefore);
932955 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
933956 public:
934957 template
935 static CallInst *Create(Value *Func, InputIterator ArgBegin,
936 InputIterator ArgEnd,
958 static CallInst *Create(Value *Func,
959 InputIterator ArgBegin, InputIterator ArgEnd,
937960 const std::string &Name = "",
938961 Instruction *InsertBefore = 0) {
939962 return new(ArgEnd - ArgBegin + 1)
940963 CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
941964 }
942965 template
943 static CallInst *Create(Value *Func, InputIterator ArgBegin,
944 InputIterator ArgEnd, const std::string &Name,
945 BasicBlock *InsertAtEnd) {
966 static CallInst *Create(Value *Func,
967 InputIterator ArgBegin, InputIterator ArgEnd,
968 const std::string &Name, BasicBlock *InsertAtEnd) {
946969 return new(ArgEnd - ArgBegin + 1)
947970 CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
948971 }
966989 ~CallInst();
967990
968991 virtual CallInst *clone() const;
992
993 /// Provide fast operand accessors
994 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
969995
970996 bool isTailCall() const { return SubclassData & 1; }
971997 void setTailCall(bool isTailCall = true) {
10491075 }
10501076 };
10511077
1078 template <>
1079 struct OperandTraits : VariadicOperandTraits<1> {
1080 };
1081
1082 template
1083 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1084 const std::string &Name, BasicBlock *InsertAtEnd)
1085 : Instruction(cast(cast(Func->getType())
1086 ->getElementType())->getReturnType(),
1087 Instruction::Call,
1088 OperandTraits::op_end(this) - (ArgEnd - ArgBegin + 1),
1089 ArgEnd - ArgBegin + 1, InsertAtEnd) {
1090 init(Func, ArgBegin, ArgEnd, Name,
1091 typename std::iterator_traits::iterator_category());
1092 }
1093
1094 template
1095 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1096 const std::string &Name, Instruction *InsertBefore)
1097 : Instruction(cast(cast(Func->getType())
1098 ->getElementType())->getReturnType(),
1099 Instruction::Call,
1100 OperandTraits::op_end(this) - (ArgEnd - ArgBegin + 1),
1101 ArgEnd - ArgBegin + 1, InsertBefore) {
1102 init(Func, ArgBegin, ArgEnd, Name,
1103 typename std::iterator_traits::iterator_category());
1104 }
1105
1106 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1107
10521108 //===----------------------------------------------------------------------===//
10531109 // SelectInst Class
10541110 //===----------------------------------------------------------------------===//
10561112 /// SelectInst - This class represents the LLVM 'select' instruction.
10571113 ///
10581114 class SelectInst : public Instruction {
1059 Use Ops[3];
1060
10611115 void init(Value *C, Value *S1, Value *S2) {
1062 Ops[0].init(C, this);
1063 Ops[1].init(S1, this);
1064 Ops[2].init(S2, this);
1116 Op<0>() = C;
1117 Op<1>() = S1;
1118 Op<2>() = S2;
10651119 }
10661120
10671121 SelectInst(const SelectInst &SI)
1068 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1069 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1070 }
1071 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1072 Instruction *InsertBefore = 0)
1073 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
1122 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1123 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1124 }
1125 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1126 Instruction *InsertBefore)
1127 : Instruction(S1->getType(), Instruction::Select,
1128 &Op<0>(), 3, InsertBefore) {
10741129 init(C, S1, S2);
10751130 setName(Name);
10761131 }
10771132 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
10781133 BasicBlock *InsertAtEnd)
1079 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
1134 : Instruction(S1->getType(), Instruction::Select,
1135 &Op<0>(), 3, InsertAtEnd) {
10801136 init(C, S1, S2);
10811137 setName(Name);
10821138 }
10911147 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
10921148 }
10931149
1094 Value *getCondition() const { return Ops[0]; }
1095 Value *getTrueValue() const { return Ops[1]; }
1096 Value *getFalseValue() const { return Ops[2]; }
1150 Value *getCondition() const { return Op<0>(); }
1151 Value *getTrueValue() const { return Op<1>(); }
1152 Value *getFalseValue() const { return Op<2>(); }
10971153
10981154 /// Transparently provide more efficient getOperand methods.
1099 Value *getOperand(unsigned i) const {
1100 assert(i < 3 && "getOperand() out of range!");
1101 return Ops[i];
1102 }
1103 void setOperand(unsigned i, Value *Val) {
1104 assert(i < 3 && "setOperand() out of range!");
1105 Ops[i] = Val;
1106 }
1107 unsigned getNumOperands() const { return 3; }
1155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
11081156
11091157 OtherOps getOpcode() const {
11101158 return static_cast(Instruction::getOpcode());
11211169 return isa(V) && classof(cast(V));
11221170 }
11231171 };
1172
1173 template <>
1174 struct OperandTraits : FixedNumOperandTraits<3> {
1175 };
1176
1177 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
11241178
11251179 //===----------------------------------------------------------------------===//
11261180 // VAArgInst Class
11641218 /// element from a VectorType value
11651219 ///
11661220 class ExtractElementInst : public Instruction {
1167 Use Ops[2];
11681221 ExtractElementInst(const ExtractElementInst &EE) :
1169 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1170 Ops[0].init(EE.Ops[0], this);
1171 Ops[1].init(EE.Ops[1], this);
1222 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1223 Op<0>().init(EE.Op<0>(), this);
1224 Op<1>().init(EE.Op<1>(), this);
11721225 }
11731226
11741227 public:
11751228 // allocate space for exactly two operands
11761229 void *operator new(size_t s) {
1177 return User::operator new(s, 2); // FIXME: unsigned Idx forms of constructor?
1230 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
11781231 }
11791232 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
11801233 Instruction *InsertBefore = 0);
11921245 virtual ExtractElementInst *clone() const;
11931246
11941247 /// Transparently provide more efficient getOperand methods.
1195 Value *getOperand(unsigned i) const {
1196 assert(i < 2 && "getOperand() out of range!");
1197 return Ops[i];
1198 }
1199 void setOperand(unsigned i, Value *Val) {
1200 assert(i < 2 && "setOperand() out of range!");
1201 Ops[i] = Val;
1202 }
1203 unsigned getNumOperands() const { return 2; }
1248 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
12041249
12051250 // Methods for support type inquiry through isa, cast, and dyn_cast:
12061251 static inline bool classof(const ExtractElementInst *) { return true; }
12111256 return isa(V) && classof(cast(V));
12121257 }
12131258 };
1259
1260 template <>
1261 struct OperandTraits : FixedNumOperandTraits<2> {
1262 };
1263
1264 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
12141265
12151266 //===----------------------------------------------------------------------===//
12161267 // InsertElementInst Class
12201271 /// element into a VectorType value
12211272 ///
12221273 class InsertElementInst : public Instruction {
1223 Use Ops[3];
12241274 InsertElementInst(const InsertElementInst &IE);
12251275 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
12261276 const std::string &Name = "",Instruction *InsertBefore = 0);
12351285 return new(IE.getNumOperands()) InsertElementInst(IE);
12361286 }
12371287 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1238 const std::string &Name = "",Instruction *InsertBefore = 0) {
1288 const std::string &Name = "",
1289 Instruction *InsertBefore = 0) {
12391290 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
12401291 }
12411292 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
12421293 const std::string &Name = "",
12431294 Instruction *InsertBefore = 0) {
1244 return new(3/*FIXME*/)
1245 InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1295 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
12461296 }
12471297 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
12481298 const std::string &Name,
12521302 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
12531303 const std::string &Name,
12541304 BasicBlock *InsertAtEnd) {
1255 return new(3/*FIXME*/)
1256 InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1305 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
12571306 }
12581307
12591308 /// isValidOperands - Return true if an insertelement instruction can be
12701319 }
12711320
12721321 /// Transparently provide more efficient getOperand methods.
1273 Value *getOperand(unsigned i) const {
1274 assert(i < 3 && "getOperand() out of range!");
1275 return Ops[i];
1276 }
1277 void setOperand(unsigned i, Value *Val) {
1278 assert(i < 3 && "setOperand() out of range!");
1279 Ops[i] = Val;
1280 }
1281 unsigned getNumOperands() const { return 3; }
1322 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
12821323
12831324 // Methods for support type inquiry through isa, cast, and dyn_cast:
12841325 static inline bool classof(const InsertElementInst *) { return true; }
12891330 return isa(V) && classof(cast(V));
12901331 }
12911332 };
1333
1334 template <>
1335 struct OperandTraits : FixedNumOperandTraits<3> {
1336 };
1337
1338 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
12921339
12931340 //===----------------------------------------------------------------------===//
12941341 // ShuffleVectorInst Class
12981345 /// input vectors.
12991346 ///
13001347 class ShuffleVectorInst : public Instruction {
1301 Use Ops[3];
13021348 ShuffleVectorInst(const ShuffleVectorInst &IE);
13031349 public:
13041350 // allocate space for exactly three operands
13241370 }
13251371
13261372 /// Transparently provide more efficient getOperand methods.
1327 const Value *getOperand(unsigned i) const {
1328 assert(i < 3 && "getOperand() out of range!");
1329 return Ops[i];
1330 }
1331 Value *getOperand(unsigned i) {
1332 assert(i < 3 && "getOperand() out of range!");
1333 return Ops[i];
1334 }
1335 void setOperand(unsigned i, Value *Val) {
1336 assert(i < 3 && "setOperand() out of range!");
1337 Ops[i] = Val;
1338 }
1339 unsigned getNumOperands() const { return 3; }
1373 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
13401374
13411375 /// getMaskValue - Return the index from the shuffle mask for the specified
13421376 /// output result. This is either -1 if the element is undef or a number less
13531387 }
13541388 };
13551389
1390 template <>
1391 struct OperandTraits : FixedNumOperandTraits<3> {
1392 };
1393
1394 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
13561395
13571396 //===----------------------------------------------------------------------===//
13581397 // PHINode Class
14051444
14061445 virtual PHINode *clone() const;
14071446
1447 /// Provide fast operand accessors
1448 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1449
14081450 /// getNumIncomingValues - Return the number of incoming edges
14091451 ///
14101452 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
14261468 /// getIncomingBlock - Return incoming basic block number x
14271469 ///
14281470 BasicBlock *getIncomingBlock(unsigned i) const {
1429 return reinterpret_cast(getOperand(i*2+1));
1471 return static_cast(getOperand(i*2+1));
14301472 }
14311473 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1432 setOperand(i*2+1, reinterpret_cast(BB));
1474 setOperand(i*2+1, BB);
14331475 }
14341476 unsigned getOperandNumForIncomingBlock(unsigned i) {
14351477 return i*2+1;
14481490 // Initialize some new operands.
14491491 NumOperands = OpNo+2;
14501492 OperandList[OpNo].init(V, this);
1451 OperandList[OpNo+1].init(reinterpret_cast(BB), this);
1493 OperandList[OpNo+1].init(BB, this);
14521494 }
14531495
14541496 /// removeIncomingValue - Remove an incoming value. This is useful if a
14611503 ///
14621504 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
14631505
1464 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1506 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
14651507 int Idx = getBasicBlockIndex(BB);
14661508 assert(Idx >= 0 && "Invalid basic block argument to remove!");
14671509 return removeIncomingValue(Idx, DeletePHIIfEmpty);
14731515 int getBasicBlockIndex(const BasicBlock *BB) const {
14741516 Use *OL = OperandList;
14751517 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1476 if (OL[i+1] == reinterpret_cast(BB)) return i/2;
1518 if (OL[i+1].get() == BB) return i/2;
14771519 return -1;
14781520 }
14791521
14971539 private:
14981540 void resizeOperands(unsigned NumOperands);
14991541 };
1542
1543 template <>
1544 struct OperandTraits : HungoffOperandTraits<2> {
1545 };
1546
1547 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1548
15001549
15011550 //===----------------------------------------------------------------------===//
15021551 // ReturnInst Class
15071556 /// does not continue in this function any longer.
15081557 ///
15091558 class ReturnInst : public TerminatorInst {
1510 Use RetVal;
15111559 ReturnInst(const ReturnInst &RI);
15121560 void init(Value * const* retVals, unsigned N);
15131561
15161564 // ReturnInst() - 'ret void' instruction
15171565 // ReturnInst( null) - 'ret void' instruction
15181566 // ReturnInst(Value* X) - 'ret X' instruction
1519 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1567 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
15201568 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1521 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1522 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1569 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1570 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
15231571 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1524 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1525 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
1572 // ReturnInst(Value* X, N, Inst *I) - 'ret X,X+1...X+N-1', insert before I
1573 // ReturnInst(Value* X, N, BB *B) - 'ret X,X+1...X+N-1', insert @ end of B
15261574 //
15271575 // NOTE: If the Value* passed is of type void then the constructor behaves as
15281576 // if it was passed NULL.
15291577 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
15301578 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1531 ReturnInst(Value * const* retVals, unsigned N);
1532 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1579 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore = 0);
15331580 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
15341581 explicit ReturnInst(BasicBlock *InsertAtEnd);
15351582 public:
15391586 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
15401587 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
15411588 }
1542 static ReturnInst* Create(Value * const* retVals, unsigned N) {
1543 return new(N) ReturnInst(retVals, N);
1544 }
15451589 static ReturnInst* Create(Value * const* retVals, unsigned N,
1546 Instruction *InsertBefore) {
1590 Instruction *InsertBefore = 0) {
15471591 return new(N) ReturnInst(retVals, N, InsertBefore);
15481592 }
15491593 static ReturnInst* Create(Value * const* retVals, unsigned N,
15541598 return new(0) ReturnInst(InsertAtEnd);
15551599 }
15561600 virtual ~ReturnInst();
1601 inline void operator delete(void*);
15571602
15581603 virtual ReturnInst *clone() const;
15591604
1560 Value *getOperand(unsigned n = 0) const {
1561 if (getNumOperands() > 1)
1562 return TerminatorInst::getOperand(n);
1563 else
1564 return RetVal;
1565 }
1566
1605 /// Provide fast operand accessors
1606 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1607
1608 /// Convenience accessor
15671609 Value *getReturnValue(unsigned n = 0) const {
1568 return getOperand(n);
1610 return n < getNumOperands()
1611 ? getOperand(n)
1612 : 0;
15691613 }
15701614
15711615 unsigned getNumSuccessors() const { return 0; }
15831627 virtual unsigned getNumSuccessorsV() const;
15841628 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
15851629 };
1630
1631 template <>
1632 struct OperandTraits : VariadicOperandTraits<> {
1633 };
1634
1635 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1636 void ReturnInst::operator delete(void *it) {
1637 ReturnInst* me(static_cast(it));
1638 Use::zap(OperandTraits::op_begin(me),
1639 OperandTraits::op_end(me),
1640 true);
1641 }
15861642
15871643 //===----------------------------------------------------------------------===//
15881644 // BranchInst Class
15951651 /// Ops list - Branches are strange. The operands are ordered:
15961652 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
15971653 /// they don't have to check for cond/uncond branchness.
1598 Use Ops[3];
15991654 BranchInst(const BranchInst &BI);
16001655 void AssertOK();
16011656 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
16151670 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
16161671 return new(1) BranchInst(IfTrue, InsertBefore);
16171672 }
1618 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1619 Instruction *InsertBefore = 0) {
1673 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1674 Value *Cond, Instruction *InsertBefore = 0) {
16201675 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
16211676 }
16221677 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
16231678 return new(1) BranchInst(IfTrue, InsertAtEnd);
16241679 }
1625 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1626 BasicBlock *InsertAtEnd) {
1680 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1681 Value *Cond, BasicBlock *InsertAtEnd) {
16271682 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
16281683 }
16291684
1685 ~BranchInst()
1686 {
1687 if (NumOperands == 1)
1688 {
1689 NumOperands = (Use*)this - OperandList;
1690 }
1691 }
1692
16301693 /// Transparently provide more efficient getOperand methods.
1631 Value *getOperand(unsigned i) const {
1632 assert(i < getNumOperands() && "getOperand() out of range!");
1633 return Ops[i];
1634 }
1635 void setOperand(unsigned i, Value *Val) {
1636 assert(i < getNumOperands() && "setOperand() out of range!");
1637 Ops[i] = Val;
1638 }
1694 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
16391695
16401696 virtual BranchInst *clone() const;
16411697
16561712 // targeting the specified block.
16571713 // FIXME: Eliminate this ugly method.
16581714 void setUnconditionalDest(BasicBlock *Dest) {
1715 Op<0>() = Dest;
16591716 if (isConditional()) { // Convert this to an uncond branch.
1717 Op<1>().set(0);
1718 Op<2>().set(0);
16601719 NumOperands = 1;
1661 Ops[1].set(0);
1662 Ops[2].set(0);
16631720 }
1664 setOperand(0, reinterpret_cast(Dest));
16651721 }
16661722
16671723 unsigned getNumSuccessors() const { return 1+isConditional(); }
16731729
16741730 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
16751731 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1676 setOperand(idx, reinterpret_cast(NewSucc));
1732 setOperand(idx, NewSucc);
16771733 }
16781734
16791735 // Methods for support type inquiry through isa, cast, and dyn_cast:
16901746 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
16911747 };
16921748
1749 template <>
1750 struct OperandTraits : HungoffOperandTraits<> {
1751 // we need to access operands via OperandList, since
1752 // the NumOperands may change from 3 to 1
1753 static inline void *allocate(unsigned); // FIXME
1754 };
1755
1756 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
1757
16931758 //===----------------------------------------------------------------------===//
16941759 // SwitchInst Class
16951760 //===----------------------------------------------------------------------===//
16981763 /// SwitchInst - Multiway switch
16991764 ///
17001765 class SwitchInst : public TerminatorInst {
1766 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
17011767 unsigned ReservedSpace;
17021768 // Operand[0] = Value to switch on
17031769 // Operand[1] = Default basic block destination
17061772 SwitchInst(const SwitchInst &RI);
17071773 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
17081774 void resizeOperands(unsigned No);
1775 // allocate space for exactly zero operands
1776 void *operator new(size_t s) {
1777 return User::operator new(s, 0);
1778 }
17091779 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
17101780 /// switch on and a default destination. The number of additional cases can
17111781 /// be specified here to make memory allocation more efficient. This
17201790 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
17211791 BasicBlock *InsertAtEnd);
17221792 public:
1723 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1724 Instruction *InsertBefore = 0) {
1725 return new(NumCases/*FIXME*/)
1726 SwitchInst(Value, Default, NumCases, InsertBefore);
1727 }
1728 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1729 BasicBlock *InsertAtEnd) {
1730 return new(NumCases/*FIXME*/)
1731 SwitchInst(Value, Default, NumCases, InsertAtEnd);
1793 static SwitchInst *Create(Value *Value, BasicBlock *Default,
1794 unsigned NumCases, Instruction *InsertBefore = 0) {
1795 return new SwitchInst(Value, Default, NumCases, InsertBefore);
1796 }
1797 static SwitchInst *Create(Value *Value, BasicBlock *Default,
1798 unsigned NumCases, BasicBlock *InsertAtEnd) {
1799 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
17321800 }
17331801 ~SwitchInst();
1802
1803 /// Provide fast operand accessors
1804 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
17341805
17351806 // Accessor Methods for Switch stmt
17361807 Value *getCondition() const { return getOperand(0); }
18041875 }
18051876 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
18061877 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1807 setOperand(idx*2+1, reinterpret_cast(NewSucc));
1878 setOperand(idx*2+1, NewSucc);
18081879 }
18091880
18101881 // getSuccessorValue - Return the value associated with the specified
18271898 virtual unsigned getNumSuccessorsV() const;
18281899 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
18291900 };
1901
1902 template <>
1903 struct OperandTraits : HungoffOperandTraits<2> {
1904 };
1905
1906 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
1907
18301908
18311909 //===----------------------------------------------------------------------===//
18321910 // InvokeInst Class
18651943 ///
18661944 /// @brief Construct an InvokeInst from a range of arguments
18671945 template
1868 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1869 InputIterator ArgBegin, InputIterator ArgEnd,
1870 const std::string &Name = "", Instruction *InsertBefore = 0)
1871 : TerminatorInst(cast(cast(Func->getType())
1872 ->getElementType())->getReturnType(),
1873 Instruction::Invoke, 0, 0, InsertBefore) {
1874 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1875 typename std::iterator_traits::iterator_category());
1876 }
1946 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1947 InputIterator ArgBegin, InputIterator ArgEnd,
1948 unsigned Values,
1949 const std::string &Name, Instruction *InsertBefore);
18771950
18781951 /// Construct an InvokeInst given a range of arguments.
18791952 /// InputIterator must be a random-access iterator pointing to
18831956 ///
18841957 /// @brief Construct an InvokeInst from a range of arguments
18851958 template
1886 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1887 InputIterator ArgBegin, InputIterator ArgEnd,
1888 const std::string &Name, BasicBlock *InsertAtEnd)
1889 : TerminatorInst(cast(cast(Func->getType())
1890 ->getElementType())->getReturnType(),
1891 Instruction::Invoke, 0, 0, InsertAtEnd) {
1892 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1893 typename std::iterator_traits::iterator_category());
1894 }
1959 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1960 InputIterator ArgBegin, InputIterator ArgEnd,
1961 unsigned Values,
1962 const std::string &Name, BasicBlock *InsertAtEnd);
18951963 public:
18961964 template
1897 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
1898 BasicBlock *IfException,
1965 static InvokeInst *Create(Value *Func,
1966 BasicBlock *IfNormal, BasicBlock *IfException,
18991967 InputIterator ArgBegin, InputIterator ArgEnd,
19001968 const std::string &Name = "",
19011969 Instruction *InsertBefore = 0) {
1902 return new(ArgEnd - ArgBegin + 3)
1903 InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertBefore);
1970 unsigned Values(ArgEnd - ArgBegin + 3);
1971 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
1972 Values, Name, InsertBefore);
19041973 }
19051974 template
1906 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
1907 BasicBlock *IfException,
1975 static InvokeInst *Create(Value *Func,
1976 BasicBlock *IfNormal, BasicBlock *IfException,
19081977 InputIterator ArgBegin, InputIterator ArgEnd,
19091978 const std::string &Name, BasicBlock *InsertAtEnd) {
1910 return new(ArgEnd - ArgBegin + 3)
1911 InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertAtEnd);
1912 }
1913
1914 ~InvokeInst();
1979 unsigned Values(ArgEnd - ArgBegin + 3);
1980 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
1981 Values, Name, InsertAtEnd);
1982 }
19151983
19161984 virtual InvokeInst *clone() const;
19171985
1986 /// Provide fast operand accessors
1987 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1988
19181989 /// getCallingConv/setCallingConv - Get or set the calling convention of this
19191990 /// function call.
19201991 unsigned getCallingConv() const { return SubclassData; }
19842055 return cast(getOperand(2));
19852056 }
19862057 void setNormalDest(BasicBlock *B) {
1987 setOperand(1, reinterpret_cast(B));
2058 setOperand(1, B);
19882059 }
19892060
19902061 void setUnwindDest(BasicBlock *B) {
1991 setOperand(2, reinterpret_cast(B));
2062 setOperand(2, B);
19922063 }
19932064
19942065 BasicBlock *getSuccessor(unsigned i) const {
19982069
19992070 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
20002071 assert(idx < 2 && "Successor # out of range for invoke!");
2001 setOperand(idx+1, reinterpret_cast(NewSucc));
2072 setOperand(idx+1, NewSucc);
20022073 }
20032074
20042075 unsigned getNumSuccessors() const { return 2; }
20172088 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
20182089 };
20192090
2091 template <>
2092 struct OperandTraits : VariadicOperandTraits<3> {
2093 };
2094
2095 template
2096 InvokeInst::InvokeInst(Value *Func,
2097 BasicBlock *IfNormal, BasicBlock *IfException,
2098 InputIterator ArgBegin, InputIterator ArgEnd,
2099 unsigned Values,
2100 const std::string &Name, Instruction *InsertBefore)
2101 : TerminatorInst(cast(cast(Func->getType())
2102 ->getElementType())->getReturnType(),
2103 Instruction::Invoke,
2104 OperandTraits::op_end(this) - Values,
2105 Values, InsertBefore) {
2106 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2107 typename std::iterator_traits::iterator_category());
2108 }
2109 template
2110 InvokeInst::InvokeInst(Value *Func,
2111 BasicBlock *IfNormal, BasicBlock *IfException,
2112 InputIterator ArgBegin, InputIterator ArgEnd,
2113 unsigned Values,
2114 const std::string &Name, BasicBlock *InsertAtEnd)
2115 : TerminatorInst(cast(cast(Func->getType())
2116 ->getElementType())->getReturnType(),
2117 Instruction::Invoke,
2118 OperandTraits::op_end(this) - Values,
2119 Values, InsertAtEnd) {
2120 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2121 typename std::iterator_traits::iterator_category());
2122 }
2123
2124 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
20202125
20212126 //===----------------------------------------------------------------------===//
20222127 // UnwindInst Class
25712676 ///
25722677 class GetResultInst : public /*FIXME: Unary*/Instruction {
25732678 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2574 Use Aggr;
25752679 unsigned Idx;
25762680 GetResultInst(const GetResultInst &GRI) :
2577 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2578 Aggr.init(GRI.Aggr, this);
2681 Instruction(GRI.getType(), Instruction::GetResult, &Op<0>(), 1) {
2682 Op<0>().init(GRI.Op<0>(), this);
25792683 Idx = GRI.Idx;
25802684 }
25812685
25842688 void *operator new(size_t s) {
25852689 return User::operator new(s, 1);
25862690 }
2587 explicit GetResultInst(Value *Aggr, unsigned index,
2588 const std::string &Name = "",
2589 Instruction *InsertBefore = 0);
2691 GetResultInst(Value *Aggr, unsigned index,
2692 const std::string &Name = "",
2693 Instruction *InsertBefore = 0);
25902694
25912695 /// isValidOperands - Return true if an getresult instruction can be
25922696 /// formed with the specified operands.
26062710 return Idx;
26072711 }
26082712
2609 unsigned getNumOperands() const { return 1; }
2713 /// Provide fast operand accessors
2714 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
26102715
26112716 // Methods for support type inquiry through isa, cast, and dyn_cast:
26122717 static inline bool classof(const GetResultInst *) { return true; }
26182723 }
26192724 };
26202725
2726 // FIXME: these are redundant if GetResultInst < UnaryInstruction
2727 template <>
2728 struct OperandTraits : FixedNumOperandTraits<1> {
2729 };
2730
2731 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetResultInst, Value)
2732
2733
26212734 } // End llvm namespace
26222735
26232736 #endif
0 //===-- llvm/OperandTraits.h - OperandTraits class definition ---------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the traits classes that are handy for enforcing the correct
10 // layout of various User subclasses. It also provides the means for accessing
11 // the operands in the most efficient manner.
12 //
13
14 #ifndef LLVM_OPERAND_TRAITS_H
15 #define LLVM_OPERAND_TRAITS_H
16
17 #include "llvm/User.h"
18
19 namespace llvm {
20
21 //===----------------------------------------------------------------------===//
22 // FixedNumOperands Trait Class
23 //===----------------------------------------------------------------------===//
24
25 template
26 struct FixedNumOperandTraits {
27 static Use *op_begin(User* U) {
28 return reinterpret_cast(U) - ARITY;
29 }
30 static Use *op_end(User* U) {
31 return reinterpret_cast(U);
32 }
33 static unsigned operands(const User*) {
34 return ARITY;
35 }
36 struct prefix {
37 Use Ops[ARITY];
38 prefix(); // DO NOT IMPLEMENT
39 };
40 template
41 struct Layout {
42 struct overlay : prefix, U {
43 overlay(); // DO NOT IMPLEMENT
44 };
45 };
46 static inline void *allocate(unsigned); // FIXME
47 };
48
49 //===----------------------------------------------------------------------===//
50 // OptionalOperands Trait Class
51 //===----------------------------------------------------------------------===//
52
53 template
54 struct OptionalOperandTraits : FixedNumOperandTraits {
55 static unsigned operands(const User *U) {
56 return U->getNumOperands();
57 }
58 };
59
60 //===----------------------------------------------------------------------===//
61 // VariadicOperand Trait Class
62 //===----------------------------------------------------------------------===//
63
64 template
65 struct VariadicOperandTraits {
66 static Use *op_begin(User* U) {
67 return reinterpret_cast(U) - U->getNumOperands();
68 }
69 static Use *op_end(User* U) {
70 return reinterpret_cast(U);
71 }
72 static unsigned operands(const User *U) {
73 return U->getNumOperands();
74 }
75 static inline void *allocate(unsigned); // FIXME
76 };
77
78 //===----------------------------------------------------------------------===//
79 // HungoffOperand Trait Class
80 //===----------------------------------------------------------------------===//
81
82 template
83 struct HungoffOperandTraits {
84 static Use *op_begin(User* U) {
85 return U->OperandList;
86 }
87 static Use *op_end(User* U) {
88 return U->OperandList + U->getNumOperands();
89 }
90 static unsigned operands(const User *U) {
91 return U->getNumOperands();
92 }
93 static inline void *allocate(unsigned); // FIXME
94 };
95
96 /// Macro for generating in-class operand accessor declarations.
97 /// It should only be called in the public section of the interface.
98 ///
99 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
100 public: \
101 inline VALUECLASS *getOperand(unsigned) const; \
102 inline void setOperand(unsigned, VALUECLASS*); \
103 protected: \
104 template inline Use &Op(); \
105 template inline const Use &Op() const; \
106 public: \
107 inline unsigned getNumOperands() const
108
109 /// Macro for generating out-of-class operand accessor definitions
110 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
111 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
112 assert(i_nocapture < OperandTraits::operands(this) \
113 && "getOperand() out of range!"); \
114 return static_cast( \
115 OperandTraits::op_begin(const_cast(this))[i_nocapture]); \
116 } \
117 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
118 assert(i_nocapture < OperandTraits::operands(this) \
119 && "setOperand() out of range!"); \
120 OperandTraits::op_begin(this)[i_nocapture] = Val_nocapture; \
121 } \
122 unsigned CLASS::getNumOperands() const { \
123 return OperandTraits::operands(this); \
124 } \
125 template Use &CLASS::Op() { \
126 return OperandTraits::op_begin(this)[Idx_nocapture]; \
127 } \
128 template const Use &CLASS::Op() const { \
129 return OperandTraits::op_begin( \
130 const_cast(this))[Idx_nocapture]; \
131 }
132
133
134 /// Macro for generating out-of-class operand accessor
135 /// definitions with casted result
136 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
137 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
138 assert(i_nocapture < OperandTraits::operands(this) \
139 && "getOperand() out of range!"); \
140 return cast( \
141 OperandTraits::op_begin(const_cast(this))[i_nocapture]); \
142 } \
143 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
144 assert(i_nocapture < OperandTraits::operands(this) \
145 && "setOperand() out of range!"); \
146 OperandTraits::op_begin(this)[i_nocapture] = Val_nocapture; \
147 } \
148 unsigned CLASS::getNumOperands() const { \
149 return OperandTraits::operands(this); \
150 } \
151 template Use &CLASS::Op() { \
152 return OperandTraits::op_begin(this)[Idx_nocapture]; \
153 } \
154 template const Use &CLASS::Op() const { \
155 return OperandTraits::op_begin( \
156 const_cast(this))[Idx_nocapture]; \
157 }
158
159
160 } // End llvm namespace
161
162 #endif
2525
2626
2727 //===----------------------------------------------------------------------===//
28 // Generic Tagging Functions
29 //===----------------------------------------------------------------------===//
30
31 /// Tag - generic tag type for (at least 32 bit) pointers
32 enum Tag { noTag, tagOne, tagTwo, tagThree };
33
34 /// addTag - insert tag bits into an (untagged) pointer
35 template
36 inline T *addTag(const T *P, TAG Tag) {
37 return reinterpret_cast(ptrdiff_t(P) | Tag);
38 }
39
40 /// stripTag - remove tag bits from a pointer,
41 /// making it dereferencable
42 template
43 inline T *stripTag(const T *P) {
44 return reinterpret_cast(ptrdiff_t(P) & ~MASK);
45 }
46
47 /// extractTag - extract tag bits from a pointer
48 template
49 inline TAG extractTag(const T *P) {
50 return TAG(ptrdiff_t(P) & MASK);
51 }
52
53 /// transferTag - transfer tag bits from a pointer,
54 /// to an untagged pointer
55 template
56 inline T *transferTag(const T *From, const T *To) {
57 return reinterpret_cast((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
58 }
59
60
61 //===----------------------------------------------------------------------===//
2862 // Use Class
2963 //===----------------------------------------------------------------------===//
3064
3468 public:
3569 inline void init(Value *V, User *U);
3670
37 Use(Value *V, User *U) { init(V, U); }
38 Use(const Use &U) { init(U.Val, U.U); }
71 private:
72 /// Allow std::swap some intimacy
73 template friend void std::swap(U&, U&);
74
75 /// Copy ctor - Only for std::swap
76 Use(const Use &U) { init(U.get(), 0); }
77
78 /// Destructor - Only for zap() and std::swap
3979 inline ~Use() {
40 if (Val) removeFromList();
41 }
42
43 /// Default ctor - This leaves the Use completely unitialized. The only thing
80 if (get()) removeFromList();
81 }
82
83 /// Default ctor - This leaves the Use completely uninitialized. The only thing
4484 /// that is valid to do with this use is to call the "init" method.
45 inline Use() : Val(0) {}
85
86 inline Use() {}
87 enum PrevPtrTag { zeroDigitTag = noTag
88 , oneDigitTag = tagOne
89 , stopTag = tagTwo
90 , fullStopTag = tagThree };
91
92 public:
4693
4794
4895 operator Value*() const { return Val; }
4996 Value *get() const { return Val; }
50 User *getUser() const { return U; }
97 User *getUser() const;
98 const Use* getImpliedUser() const;
99 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
100 static void zap(Use *Start, const Use *Stop, bool del = false);
51101
52102 inline void set(Value *Val);
53103
56106 return RHS;
57107 }
58108 const Use &operator=(const Use &RHS) {
59 set(RHS.Val);
109 set(RHS.get());
60110 return *this;
61111 }
62112
65115
66116 Use *getNext() const { return Next; }
67117 private:
118 Value *Val;
68119 Use *Next, **Prev;
69 Value *Val;
70 User *U;
71
120
121 void setPrev(Use **NewPrev) {
122 Prev = transferTag(Prev, NewPrev);
123 }
72124 void addToList(Use **List) {
73125 Next = *List;
74 if (Next) Next->Prev = &Next;
75 Prev = List;
126 if (Next) Next->setPrev(&Next);
127 setPrev(List);
76128 *List = this;
77129 }
78130 void removeFromList() {
79 *Prev = Next;
80 if (Next) Next->Prev = Prev;
131 Use **StrippedPrev = stripTag(Prev);
132 *StrippedPrev = Next;
133 if (Next) Next->setPrev(StrippedPrev);
81134 }
82135
83136 friend class Value;
137190
138191 // Retrieve a reference to the current User
139192 UserTy *operator*() const {
140 assert(U && "Cannot increment end iterator!");
193 assert(U && "Cannot dereference end iterator!");
141194 return U->getUser();
142195 }
143196
2222
2323 namespace llvm {
2424
25 /*==============================================================================
26
27
28 -----------------------------------------------------------------
29 --- Interaction and relationship between User and Use objects ---
30 -----------------------------------------------------------------
31
32
33 A subclass of User can choose between incorporating its Use objects
34 or refer to them out-of-line by means of a pointer. A mixed variant
35 (some Uses inline others hung off) is impractical and breaks the invariant
36 that the Use objects belonging to the same User form a contiguous array.
37
38 We have 2 different layouts in the User (sub)classes:
39
40 Layout a)
41 The Use object(s) are inside (resp. at fixed offset) of the User
42 object and there are a fixed number of them.
43
44 Layout b)
45 The Use object(s) are referenced by a pointer to an
46 array from the User object and there may be a variable
47 number of them.
48
49 Initially each layout will possess a direct pointer to the
50 start of the array of Uses. Though not mandatory for layout a),
51 we stick to this redundancy for the sake of simplicity.
52 The User object will also store the number of Use objects it
53 has. (Theoretically this information can also be calculated
54 given the scheme presented below.)
55
56 Special forms of allocation operators (operator new)
57 will enforce the following memory layouts:
58
59
60 # Layout a) will be modelled by prepending the User object
61 # by the Use[] array.
62 #
63 # ...---.---.---.---.-------...
64 # | P | P | P | P | User
65 # '''---'---'---'---'-------'''
66
67
68 # Layout b) will be modelled by pointing at the Use[] array.
69 #
70 # .-------...
71 # | User
72 # '-------'''
73 # |
74 # v
75 # .---.---.---.---...
76 # | P | P | P | P |
77 # '---'---'---'---'''
78
79 (In the above figures 'P' stands for the Use** that
80 is stored in each Use object in the member Use::Prev)
81
82
83 Since the Use objects will be deprived of the direct pointer to
84 their User objects, there must be a fast and exact method to
85 recover it. This is accomplished by the following scheme:
86
87 A bit-encoding in the 2 LSBits of the Use::Prev will allow to find the
88 start of the User object:
89
90 00 --> binary digit 0
91 01 --> binary digit 1
92 10 --> stop and calc (s)
93 11 --> full stop (S)
94
95 Given a Use*, all we have to do is to walk till we get
96 a stop and we either have a User immediately behind or
97 we have to walk to the next stop picking up digits
98 and calculating the offset:
99
100 .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.----------------
101 | 1 | s | 1 | 0 | 1 | 0 | s | 1 | 1 | 0 | s | 1 | 1 | s | 1 | S | User (or User*)
102 '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'----------------
103 |+15 |+10 |+6 |+3 |+1
104 | | | | |__>
105 | | | |__________>
106 | | |______________________>
107 | |______________________________________>
108 |__________________________________________________________>
109
110
111 Only the significant number of bits need to be stored between the
112 stops, so that the worst case is 20 memory accesses when there are
113 1000 Use objects.
114
115 The following literate Haskell fragment demonstrates the concept:
116
117 > import Test.QuickCheck
118 >
119 > digits :: Int -> [Char] -> [Char]
120 > digits 0 acc = '0' : acc
121 > digits 1 acc = '1' : acc
122 > digits n acc = digits (n `div` 2) $ digits (n `mod` 2) acc
123 >
124 > dist :: Int -> [Char] -> [Char]
125 > dist 0 [] = ['S']
126 > dist 0 acc = acc
127 > dist 1 acc = let r = dist 0 acc in 's' : digits (length r) r
128 > dist n acc = dist (n - 1) $ dist 1 acc
129 >
130 > takeLast n ss = reverse $ take n $ reverse ss
131 >
132 > test = takeLast 40 $ dist 20 []
133 >
134
135 Printing gives: "1s100000s11010s10100s1111s1010s110s11s1S"
136
137 The reverse algorithm computes the
138 length of the string just by examining
139 a certain prefix:
140
141 > pref :: [Char] -> Int
142 > pref "S" = 1
143 > pref ('s':'1':rest) = decode 2 1 rest
144 > pref (_:rest) = 1 + pref rest
145 >
146 > decode walk acc ('0':rest) = decode (walk + 1) (acc * 2) rest
147 > decode walk acc ('1':rest) = decode (walk + 1) (acc * 2 + 1) rest
148 > decode walk acc _ = walk + acc
149 >
150
151 Now, as expected, printing gives 40.
152
153 We can quickCheck this with following property:
154
155 > testcase = dist 2000 []
156 > testcaseLength = length testcase
157 >
158 > identityProp n = n > 0 && n <= testcaseLength ==> length arr == pref arr
159 > where arr = takeLast n testcase
160
161 As expected gives:
162
163 *Main> quickCheck identityProp
164 OK, passed 100 tests.
165
166 Let's be a bit more exhaustive:
167
168 >
169 > deepCheck p = check (defaultConfig { configMaxTest = 500 }) p
170 >
171
172 And here is the result of :
173
174 *Main> deepCheck identityProp
175 OK, passed 500 tests.
176
177
178 To maintain the invariant that the 2 LSBits of each Use** in Use
179 never change after being set up, setters of Use::Prev must re-tag the
180 new Use** on every modification. Accordingly getters must strip the
181 tag bits.
182
183 For layout b) instead of the User we will find a pointer (User* with LSBit set).
184 Following this pointer brings us to the User. A portable trick will ensure
185 that the first bytes of User (if interpreted as a pointer) will never have
186 the LSBit set.
187
188 ==============================================================================*/
189
190 /// OperandTraits - Compile-time customization of
191 /// operand-related allocators and accessors
192 /// for use of the User class
193 template
194 struct OperandTraits;
195
196 class User;
197
198 /// OperandTraits - specialization to User
199 template <>
200 struct OperandTraits {
201 static inline Use *op_begin(User*);
202 static inline Use *op_end(User*);
203 static inline unsigned operands(const User*);
204 template
205 struct Layout {
206 typedef U overlay;
207 };
208 static inline void *allocate(unsigned);
209 };
210
25211 class User : public Value {
26212 User(const User &); // Do not implement
27213 void *operator new(size_t); // Do not implement
214 template
215 friend struct HungoffOperandTraits;
28216 protected:
29217 /// OperandList - This is a pointer to the array of Users for this operand.
30218 /// For nodes of fixed arity (e.g. a binary operator) this array will live
31 /// embedded into the derived class. For nodes of variable arity
32 /// (e.g. ConstantArrays, CallInst, PHINodes, ReturnInst etc), this memory
33 /// will be dynamically allocated and should be destroyed by the classes
219 /// prefixed to the derived class. For nodes of resizable variable arity
220 /// (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
221 /// allocated and should be destroyed by the classes'
34222 /// virtual dtor.
35223 Use *OperandList;
36224
38226 ///
39227 unsigned NumOperands;
40228
41 void *operator new(size_t s, size_t) {
42 return ::operator new(s);
229 void *operator new(size_t s, unsigned Us) {
230 void *Storage = ::operator new(s + sizeof(Use) * Us);
231 Use *Start = static_cast(Storage);
232 Use *End = Start + Us;
233 User *Obj = reinterpret_cast(End);
234 Obj->OperandList = Start;
235 Obj->NumOperands = Us;
236 Use::initTags(Start, End);
237 return Obj;
43238 }
44239 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
45240 : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
46
241 Use *allocHungoffUses(unsigned) const;
242 void dropHungoffUses(Use *U) {
243 if (OperandList == U) {
244 OperandList = 0;
245 NumOperands = 0;
246 }
247 Use::zap(U, U->getImpliedUser(), true);
248 }
47249 public:
250 ~User() {
251 Use::zap(OperandList, OperandList + NumOperands);
252 }
253 void operator delete(void *Usr) {
254 User *Start = static_cast(Usr);
255 Use *Storage = static_cast(Usr) - Start->NumOperands;
256 ::operator delete(Storage == Start->OperandList
257 ? Storage
258 : Usr);
259 }
260 template Use &Op() {
261 return OperandTraits::op_begin(this)[Idx];
262 }
263 template const Use &Op() const {
264 return OperandTraits::op_begin(const_cast(this))[Idx];
265 }
48266 Value *getOperand(unsigned i) const {
49267 assert(i < NumOperands && "getOperand() out of range!");
50268 return OperandList[i];
92310 }
93311 };
94312
313 inline Use *OperandTraits::op_begin(User *U) {
314 return U->op_begin();
315 }
316
317 inline Use *OperandTraits::op_end(User *U) {
318 return U->op_end();
319 }
320
321 inline unsigned OperandTraits::operands(const User *U) {
322 return U->getNumOperands();
323 }
324
95325 template<> struct simplify_type {
96326 typedef Value* SimpleType;
97327
231231 return OS;
232232 }
233233
234 void Use::init(Value *v, User *user) {
235 Val = v;
236 U = user;
237 if (Val) Val->addUse(*this);
234 void Use::init(Value *V, User *user) {
235 Val = V;
236 if (V) V->addUse(*this);
238237 }
239238
240239 void Use::set(Value *V) {
2222 #include "llvm/ADT/SmallVector.h"
2323 #include "llvm/Support/MathExtras.h"
2424 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/OperandTraits.h"
2526 using namespace llvm;
2627
2728 void BitcodeReader::FreeState() {
114115 }
115116 }
116117
117
118 namespace llvm {
118119 namespace {
119120 /// @brief A class for maintaining the slot number definition
120121 /// as a placeholder for the actual definition for forward constants defs.
121122 class ConstantPlaceHolder : public ConstantExpr {
122123 ConstantPlaceHolder(); // DO NOT IMPLEMENT
123124 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
124 Use Op;
125125 public:
126126 // allocate space for exactly one operand
127127 void *operator new(size_t s) {
128128 return User::operator new(s, 1);
129129 }
130130 explicit ConstantPlaceHolder(const Type *Ty)
131 : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1),
132 Op(UndefValue::get(Type::Int32Ty), this) {
133 }
131 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
132 Op<0>() = UndefValue::get(Type::Int32Ty);
133 }
134 /// Provide fast operand accessors
135 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
134136 };
137 }
138
139
140 // FIXME: can we inherit this from ConstantExpr?
141 template <>
142 struct OperandTraits : FixedNumOperandTraits<1> {
143 };
144
145 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
146 }
147
148 void BitcodeReaderValueList::resize(unsigned Desired) {
149 if (Desired > Capacity) {
150 // Since we expect many values to come from the bitcode file we better
151 // allocate the double amount, so that the array size grows exponentially
152 // at each reallocation. Also, add a small amount of 100 extra elements
153 // each time, to reallocate less frequently when the array is still small.
154 //
155 Capacity = Desired * 2 + 100;
156 Use *New = allocHungoffUses(Capacity);
157 Use *Old = OperandList;
158 unsigned Ops = getNumOperands();
159 for (int i(Ops - 1); i >= 0; --i)
160 New[i] = Old[i].get();
161 OperandList = New;
162 if (Old) Use::zap(Old, Old + Ops, true);
163 }
135164 }
136165
137166 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
138167 const Type *Ty) {
139168 if (Idx >= size()) {
140169 // Insert a bunch of null values.
141 Uses.resize(Idx+1);
142 OperandList = &Uses[0];
170 resize(Idx + 1);
143171 NumOperands = Idx+1;
144172 }
145173
146 if (Value *V = Uses[Idx]) {
174 if (Value *V = OperandList[Idx]) {
147175 assert(Ty == V->getType() && "Type mismatch in constant table!");
148176 return cast(V);
149177 }
150178
151179 // Create and return a placeholder, which will later be RAUW'd.
152180 Constant *C = new ConstantPlaceHolder(Ty);
153 Uses[Idx].init(C, this);
181 OperandList[Idx].init(C, this);
154182 return C;
155183 }
156184
157185 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) {
158186 if (Idx >= size()) {
159187 // Insert a bunch of null values.
160 Uses.resize(Idx+1);
161 OperandList = &Uses[0];
188 resize(Idx + 1);
162189 NumOperands = Idx+1;
163190 }
164191
165 if (Value *V = Uses[Idx]) {
192 if (Value *V = OperandList[Idx]) {
166193 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
167194 return V;
168195 }
172199
173200 // Create and return a placeholder, which will later be RAUW'd.
174201 Value *V = new Argument(Ty);
175 Uses[Idx].init(V, this);
202 OperandList[Idx].init(V, this);
176203 return V;
177204 }
178205
1616 #include "llvm/ModuleProvider.h"
1717 #include "llvm/ParameterAttributes.h"
1818 #include "llvm/Type.h"
19 #include "llvm/User.h"
19 #include "llvm/OperandTraits.h"
2020 #include "llvm/Bitcode/BitstreamReader.h"
2121 #include "llvm/Bitcode/LLVMBitCodes.h"
2222 #include "llvm/ADT/DenseMap.h"
2525 namespace llvm {
2626 class MemoryBuffer;
2727
28 //===----------------------------------------------------------------------===//
29 // BitcodeReaderValueList Class
30 //===----------------------------------------------------------------------===//
31
2832 class BitcodeReaderValueList : public User {
29 std::vector Uses;
33 unsigned Capacity;
3034 public:
31 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
32
35 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
36 , Capacity(0) {}
37
38 /// Provide fast operand accessors
39 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
40
3341 // vector compatibility methods
3442 unsigned size() const { return getNumOperands(); }
43 void resize(unsigned);
3544 void push_back(Value *V) {
36 Uses.push_back(Use(V, this));
37 OperandList = &Uses[0];
38 ++NumOperands;
45 unsigned OldOps(NumOperands), NewOps(NumOperands + 1);
46 resize(NewOps);
47 NumOperands = NewOps;
48 OperandList[OldOps] = V;
3949 }
4050
4151 void clear() {
42 std::vector().swap(Uses);
52 if (OperandList) dropHungoffUses(OperandList);
53 Capacity = 0;
4354 }
4455
4556 Value *operator[](unsigned i) const { return getOperand(i); }
4657
47 Value *back() const { return Uses.back(); }
48 void pop_back() { Uses.pop_back(); --NumOperands; }
58 Value *back() const { return getOperand(size() - 1); }
59 void pop_back() { setOperand(size() - 1, 0); --NumOperands; }
4960 bool empty() const { return NumOperands == 0; }
5061 void shrinkTo(unsigned N) {
5162 assert(N <= NumOperands && "Invalid shrinkTo request!");
52 Uses.resize(N);
53 NumOperands = N;
63 while (NumOperands > N)
64 pop_back();
5465 }
5566 virtual void print(std::ostream&) const {}
5667
7283
7384 private:
7485 void initVal(unsigned Idx, Value *V) {
75 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
76 Uses[Idx].init(V, this);
86 if (Idx >= size()) {
87 // Insert a bunch of null values.
88 resize(Idx * 2 + 1);
89 }
90 assert(getOperand(Idx) == 0 && "Cannot init an already init'd Use!");
91 OperandList[Idx].init(V, this);
7792 }
7893 };
79
94
95 template <>
96 struct OperandTraits : HungoffOperandTraits {
97 };
98
99 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
80100
81101 class BitcodeReader : public ModuleProvider {
82102 MemoryBuffer *Buffer;
331331
332332 if (const ConstantVector *CVal = dyn_cast(Val)) {
333333 if (const ConstantInt *CIdx = dyn_cast(Idx)) {
334 return const_cast(CVal->getOperand(CIdx->getZExtValue()));
334 return CVal->getOperand(CIdx->getZExtValue());
335335 } else if (isa(Idx)) {
336336 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
337 return const_cast(CVal->getOperand(0));
337 return CVal->getOperand(0);
338338 }
339339 }
340340 return 0;
400400 /// return the specified element value. Otherwise return null.
401401 static Constant *GetVectorElement(const Constant *C, unsigned EltNo) {
402402 if (const ConstantVector *CV = dyn_cast(C))
403 return const_cast(CV->getOperand(EltNo));
403 return CV->getOperand(EltNo);
404404
405405 const Type *EltTy = cast(C->getType())->getElementType();
406406 if (isa(C))
12211221 if (const ConstantVector *CP2 = dyn_cast(C2)) {
12221222 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
12231223 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1224 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1225 const_cast(CP1->getOperand(i)),
1226 const_cast(CP2->getOperand(i)));
1224 Constant *C = ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1225 CP1->getOperand(i),
1226 CP2->getOperand(i));
12271227 if (ConstantInt *CB = dyn_cast(C))
12281228 return CB;
12291229 }
12321232 } else if (pred == ICmpInst::ICMP_EQ) {
12331233 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
12341234 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1235 const_cast(CP1->getOperand(i)),
1236 const_cast(CP2->getOperand(i)));
1235 CP1->getOperand(i),
1236 CP2->getOperand(i));
12371237 if (ConstantInt *CB = dyn_cast(C))
12381238 return CB;
12391239 }
353353
354354 ConstantArray::ConstantArray(const ArrayType *T,
355355 const std::vector &V)
356 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
356 : Constant(T, ConstantArrayVal,
357 OperandTraits::op_end(this) - V.size(),
358 V.size()) {
357359 assert(V.size() == T->getNumElements() &&
358360 "Invalid initializer vector for constant array");
359361 Use *OL = OperandList;
368370 }
369371 }
370372
371 ConstantArray::~ConstantArray() {
372 delete [] OperandList;
373 }
374373
375374 ConstantStruct::ConstantStruct(const StructType *T,
376375 const std::vector &V)
377 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
376 : Constant(T, ConstantStructVal,
377 OperandTraits::op_end(this) - V.size(),
378 V.size()) {
378379 assert(V.size() == T->getNumElements() &&
379380 "Invalid initializer vector for constant structure");
380381 Use *OL = OperandList;
391392 }
392393 }
393394
394 ConstantStruct::~ConstantStruct() {
395 delete [] OperandList;
396 }
397
398395
399396 ConstantVector::ConstantVector(const VectorType *T,
400397 const std::vector &V)
401 : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) {
398 : Constant(T, ConstantVectorVal,
399 OperandTraits::op_end(this) - V.size(),
400 V.size()) {
402401 Use *OL = OperandList;
403402 for (std::vector::const_iterator I = V.begin(), E = V.end();
404403 I != E; ++I, ++OL) {
411410 }
412411 }
413412
414 ConstantVector::~ConstantVector() {
415 delete [] OperandList;
416 }
417
413
414 namespace llvm {
418415 // We declare several classes private to this file, so use an anonymous
419416 // namespace
420417 namespace {
423420 /// behind the scenes to implement unary constant exprs.
424421 class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
425422 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
426 Use Op;
427423 public:
428424 // allocate space for exactly one operand
429425 void *operator new(size_t s) {
430426 return User::operator new(s, 1);
431427 }
432428 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
433 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
429 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
430 Op<0>() = C;
431 }
432 /// Transparently provide more efficient getOperand methods.
433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
434434 };
435435
436436 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
437437 /// behind the scenes to implement binary constant exprs.
438438 class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
439439 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
440 Use Ops[2];
441440 public:
442441 // allocate space for exactly two operands
443442 void *operator new(size_t s) {
444443 return User::operator new(s, 2);
445444 }
446445 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
447 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
448 Ops[0].init(C1, this);
449 Ops[1].init(C2, this);
450 }
446 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
447 Op<0>().init(C1, this);
448 Op<1>().init(C2, this);
449 }
450 /// Transparently provide more efficient getOperand methods.
451 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
451452 };
452453
453454 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
454455 /// behind the scenes to implement select constant exprs.
455456 class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
456457 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
457 Use Ops[3];
458458 public:
459459 // allocate space for exactly three operands
460460 void *operator new(size_t s) {
461461 return User::operator new(s, 3);
462462 }
463463 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
464 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
465 Ops[0].init(C1, this);
466 Ops[1].init(C2, this);
467 Ops[2].init(C3, this);
468 }
464 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
465 Op<0>().init(C1, this);
466 Op<1>().init(C2, this);
467 Op<2>().init(C3, this);
468 }
469 /// Transparently provide more efficient getOperand methods.
470 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
469471 };
470472
471473 /// ExtractElementConstantExpr - This class is private to
473475 /// extractelement constant exprs.
474476 class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
475477 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
476 Use Ops[2];
477478 public:
478479 // allocate space for exactly two operands
479480 void *operator new(size_t s) {
481482 }
482483 ExtractElementConstantExpr(Constant *C1, Constant *C2)
483484 : ConstantExpr(cast(C1->getType())->getElementType(),
484 Instruction::ExtractElement, Ops, 2) {
485 Ops[0].init(C1, this);
486 Ops[1].init(C2, this);
487 }
485 Instruction::ExtractElement, &Op<0>(), 2) {
486 Op<0>().init(C1, this);
487 Op<1>().init(C2, this);
488 }
489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
488491 };
489492
490493 /// InsertElementConstantExpr - This class is private to
492495 /// insertelement constant exprs.
493496 class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
494497 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
495 Use Ops[3];
496498 public:
497499 // allocate space for exactly three operands
498500 void *operator new(size_t s) {
500502 }
501503 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
502504 : ConstantExpr(C1->getType(), Instruction::InsertElement,
503 Ops, 3) {
504 Ops[0].init(C1, this);
505 Ops[1].init(C2, this);
506 Ops[2].init(C3, this);
507 }
505 &Op<0>(), 3) {
506 Op<0>().init(C1, this);
507 Op<1>().init(C2, this);
508 Op<2>().init(C3, this);
509 }
510 /// Transparently provide more efficient getOperand methods.
511 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
508512 };
509513
510514 /// ShuffleVectorConstantExpr - This class is private to
512516 /// shufflevector constant exprs.
513517 class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
514518 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
515 Use Ops[3];
516519 public:
517520 // allocate space for exactly three operands
518521 void *operator new(size_t s) {
520523 }
521524 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
522525 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
523 Ops, 3) {
524 Ops[0].init(C1, this);
525 Ops[1].init(C2, this);
526 Ops[2].init(C3, this);
527 }
526 &Op<0>(), 3) {
527 Op<0>().init(C1, this);
528 Op<1>().init(C2, this);
529 Op<2>().init(C3, this);
530 }
531 /// Transparently provide more efficient getOperand methods.
532 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
528533 };
529534
530535 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
531536 /// used behind the scenes to implement getelementpr constant exprs.
532537 class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
533538 GetElementPtrConstantExpr(Constant *C, const std::vector &IdxList,
534 const Type *DestTy)
535 : ConstantExpr(DestTy, Instruction::GetElementPtr,
536 new Use[IdxList.size()+1], IdxList.size()+1) {
537 OperandList[0].init(C, this);
538 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
539 OperandList[i+1].init(IdxList[i], this);
540 }
539 const Type *DestTy);
541540 public:
542541 static GetElementPtrConstantExpr *Create(Constant *C, const std::vector &IdxList,
543 const Type *DestTy) {
544 return new(IdxList.size() + 1/*FIXME*/) GetElementPtrConstantExpr(C, IdxList, DestTy);
545 }
546 ~GetElementPtrConstantExpr() {
547 delete [] OperandList;
548 }
542 const Type *DestTy) {
543 return new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
544 }
545 /// Transparently provide more efficient getOperand methods.
546 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
549547 };
550548
551549 // CompareConstantExpr - This class is private to Constants.cpp, and is used
558556 return User::operator new(s, 2);
559557 }
560558 unsigned short predicate;
561 Use Ops[2];
562559 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
563560 Constant* LHS, Constant* RHS)
564 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
565 OperandList[0].init(LHS, this);
566 OperandList[1].init(RHS, this);
567 }
561 : ConstantExpr(Type::Int1Ty, opc, &Op<0>(), 2), predicate(pred) {
562 Op<0>().init(LHS, this);
563 Op<1>().init(RHS, this);
564 }
565 /// Transparently provide more efficient getOperand methods.
566 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
568567 };
569568
570569 } // end anonymous namespace
570
571 template <>
572 struct OperandTraits : FixedNumOperandTraits<1> {
573 };
574 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
575
576 template <>
577 struct OperandTraits : FixedNumOperandTraits<2> {
578 };
579 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
580
581 template <>
582 struct OperandTraits : FixedNumOperandTraits<3> {
583 };
584 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
585
586 template <>
587 struct OperandTraits : FixedNumOperandTraits<2> {
588 };
589 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
590
591 template <>
592 struct OperandTraits : FixedNumOperandTraits<3> {
593 };
594 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
595
596 template <>
597 struct OperandTraits : FixedNumOperandTraits<3> {
598 };
599 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
600
601
602 template <>
603 struct OperandTraits : VariadicOperandTraits<1> {
604 };
605
606 GetElementPtrConstantExpr::GetElementPtrConstantExpr
607 (Constant *C,
608 const std::vector &IdxList,
609 const Type *DestTy)
610 : ConstantExpr(DestTy, Instruction::GetElementPtr,
611 OperandTraits::op_end(this)
612 - (IdxList.size()+1),
613 IdxList.size()+1) {
614 OperandList[0].init(C, this);
615 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
616 OperandList[i+1].init(IdxList[i], this);
617 }
618
619 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
620
621
622 template <>
623 struct OperandTraits : FixedNumOperandTraits<2> {
624 };
625 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
626
627
628 } // End llvm namespace
571629
572630
573631 // Utility function for determining if a ConstantExpr is a CastOp or not. This
814872 //===----------------------------------------------------------------------===//
815873 // Factory Function Implementation
816874
875
876 // The number of operands for each ConstantCreator::create method is
877 // determined by the ConstantTraits template.
817878 // ConstantCreator - A class that is used to create constants by
818879 // ValueMap*. This class should be partially specialized if there is
819880 // something strange that needs to be done to interface to the ctor for the
820881 // constant.
821882 //
822883 namespace llvm {
884 template
885 struct ConstantTraits;
886
887 template
888 struct VISIBILITY_HIDDEN ConstantTraits< std::vector > {
889 static unsigned uses(const std::vector& v) {
890 return v.size();
891 }
892 };
893
823894 template
824895 struct VISIBILITY_HIDDEN ConstantCreator {
825896 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
826 unsigned FIXME = 0; // = traits::uses(V)
827 return new(FIXME) ConstantClass(Ty, V);
897 return new(ConstantTraits::uses(V)) ConstantClass(Ty, V);
828898 }
829899 };
830900
8888 Module *ParentModule, bool ThreadLocal,
8989 unsigned AddressSpace)
9090 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
91 &Initializer, InitVal != 0, Link, Name),
91 OperandTraits::op_begin(this),
92 InitVal != 0, Link, Name),
9293 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
9394 if (InitVal) {
9495 assert(InitVal->getType() == Ty &&
9596 "Initializer should be the same type as the GlobalVariable!");
96 Initializer.init(InitVal, this);
97 } else {
98 Initializer.init(0, this);
97 Op<0>().init(InitVal, this);
9998 }
10099
101100 LeakDetector::addGarbageObject(this);
109108 GlobalVariable *Before, bool ThreadLocal,
110109 unsigned AddressSpace)
111110 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
112 &Initializer, InitVal != 0, Link, Name),
111 OperandTraits::op_begin(this),
112 InitVal != 0, Link, Name),
113113 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
114114 if (InitVal) {
115115 assert(InitVal->getType() == Ty &&
116116 "Initializer should be the same type as the GlobalVariable!");
117 Initializer.init(InitVal, this);
118 } else {
119 Initializer.init(0, this);
117 Op<0>().init(InitVal, this);
120118 }
121119
122120 LeakDetector::addGarbageObject(this);
168166 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
169167 const std::string &Name, Constant* aliasee,
170168 Module *ParentModule)
171 : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) {
169 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
172170 LeakDetector::addGarbageObject(this);
173171
174172 if (aliasee)
175173 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
176 Aliasee.init(aliasee, this);
174 Op<0>().init(aliasee, this);
177175
178176 if (ParentModule)
179177 ParentModule->getAliasList().push_back(this);
<
9999 TerminatorInst::~TerminatorInst() {
100100 }
101101
102 //===----------------------------------------------------------------------===//
103 // UnaryInstruction Class
104 //===----------------------------------------------------------------------===//
105
102106 // Out of line virtual method, so the vtable, etc has a home.
103107 UnaryInstruction::~UnaryInstruction() {
104108 }
105109
106
107110 //===----------------------------------------------------------------------===//
108111 // PHINode Class
109112 //===----------------------------------------------------------------------===//
110113
111114 PHINode::PHINode(const PHINode &PN)
112115 : Instruction(PN.getType(), Instruction::PHI,
113 new Use[PN.getNumOperands()], PN.getNumOperands()),
116 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
114117 ReservedSpace(PN.getNumOperands()) {
115118 Use *OL = OperandList;
116119 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
120123 }
121124
122125 PHINode::~PHINode() {
123 delete [] OperandList;
126 dropHungoffUses(OperandList);
124127 }
125128
126129 // removeIncomingValue - Remove an incoming value. This is useful if a
163166 /// 3. If NumOps == NumOperands, trim the reserved space.
164167 ///
165168 void PHINode::resizeOperands(unsigned NumOps) {
169 unsigned e = getNumOperands();
166170 if (NumOps == 0) {
167 NumOps = (getNumOperands())*3/2;
171 NumOps = e*3/2;
168172 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
169173 } else if (NumOps*2 > NumOperands) {
170174 // No resize needed.
176180 }
177181
178182 ReservedSpace = NumOps;
179 Use *NewOps = new Use[NumOps];
180183 Use *OldOps = OperandList;
181 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
184 Use *NewOps = allocHungoffUses(NumOps);
185 for (unsigned i = 0; i != e; ++i) {
182186 NewOps[i].init(OldOps[i], this);
183 OldOps[i].set(0);
184 }
185 delete [] OldOps;
187 }
186188 OperandList = NewOps;
189 if (OldOps) Use::zap(OldOps, OldOps + e, true);
187190 }
188191
189192 /// hasConstantValue - If the specified PHI node always merges together the same
240243 //===----------------------------------------------------------------------===//
241244
242245 CallInst::~CallInst() {
243 delete [] OperandList;
244246 }
245247
246248 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
247 NumOperands = NumParams+1;
248 Use *OL = OperandList = new Use[NumParams+1];
249 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
250 Use *OL = OperandList;
249251 OL[0].init(Func, this);
250252
251253 const FunctionType *FTy =
264266 }
265267
266268 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
267 NumOperands = 3;
268 Use *OL = OperandList = new Use[3];
269 assert(NumOperands == 3 && "NumOperands not set up?");
270 Use *OL = OperandList;
269271 OL[0].init(Func, this);
270272 OL[1].init(Actual1, this);
271273 OL[2].init(Actual2, this);
286288 }
287289
288290 void CallInst::init(Value *Func, Value *Actual) {
289 NumOperands = 2;
290 Use *OL = OperandList = new Use[2];
291 assert(NumOperands == 2 && "NumOperands not set up?");
292 Use *OL = OperandList;
291293 OL[0].init(Func, this);
292294 OL[1].init(Actual, this);
293295
304306 }
305307
306308 void CallInst::init(Value *Func) {
307 NumOperands = 1;
308 Use *OL = OperandList = new Use[1];
309 assert(NumOperands == 1 && "NumOperands not set up?");
310 Use *OL = OperandList;
309311 OL[0].init(Func, this);
310312
311313 const FunctionType *FTy =
319321 Instruction *InsertBefore)
320322 : Instruction(cast(cast(Func->getType())
321323 ->getElementType())->getReturnType(),
322 Instruction::Call, 0, 0, InsertBefore) {
324 Instruction::Call,
325 OperandTraits::op_end(this) - 2,
326 2, InsertBefore) {
323327 init(Func, Actual);
324328 setName(Name);
325329 }
328332 BasicBlock *InsertAtEnd)
329333 : Instruction(cast(cast(Func->getType())
330334 ->getElementType())->getReturnType(),
331 Instruction::Call, 0, 0, InsertAtEnd) {
335 Instruction::Call,
336 OperandTraits::op_end(this) - 2,
337 2, InsertAtEnd) {
332338 init(Func, Actual);
333339 setName(Name);
334340 }
336342 Instruction *InsertBefore)
337343 : Instruction(cast(cast(Func->getType())
338344 ->getElementType())->getReturnType(),
339 Instruction::Call, 0, 0, InsertBefore) {
345 Instruction::Call,
346 OperandTraits::op_end(this) - 1,
347 1, InsertBefore) {
340348 init(Func);
341349 setName(Name);
342350 }
345353 BasicBlock *InsertAtEnd)
346354 : Instruction(cast(cast(Func->getType())
347355 ->getElementType())->getReturnType(),
348 Instruction::Call, 0, 0, InsertAtEnd) {
356 Instruction::Call,
357 OperandTraits::op_end(this) - 1,
358 1, InsertAtEnd) {
349359 init(Func);
350360 setName(Name);
351361 }
352362
353363 CallInst::CallInst(const CallInst &CI)
354 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
364 : Instruction(CI.getType(), Instruction::Call,
365 OperandTraits::op_end(this) - CI.getNumOperands(),
355366 CI.getNumOperands()) {
356367 setParamAttrs(CI.getParamAttrs());
357368 SubclassData = CI.SubclassData;
383394 // InvokeInst Implementation
384395 //===----------------------------------------------------------------------===//
385396
386 InvokeInst::~InvokeInst() {
387 delete [] OperandList;
388 }
389
390397 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
391398 Value* const *Args, unsigned NumArgs) {
392 NumOperands = 3+NumArgs;
393 Use *OL = OperandList = new Use[3+NumArgs];
399 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
400 Use *OL = OperandList;
394401 OL[0].init(Fn, this);
395402 OL[1].init(IfNormal, this);
396403 OL[2].init(IfException, this);
413420
414421 InvokeInst::InvokeInst(const InvokeInst &II)
415422 : TerminatorInst(II.getType(), Instruction::Invoke,
416 new Use[II.getNumOperands()], II.getNumOperands()) {
423 OperandTraits::op_end(this) - II.getNumOperands(),
424 II.getNumOperands()) {
417425 setParamAttrs(II.getParamAttrs());
418426 SubclassData = II.SubclassData;
419427 Use *OL = OperandList, *InOL = II.OperandList;
455463
456464 ReturnInst::ReturnInst(const ReturnInst &RI)
457465 : TerminatorInst(Type::VoidTy, Instruction::Ret,
458 &RetVal, RI.getNumOperands()) {
466 OperandTraits::op_end(this) - RI.getNumOperands(),
467 RI.getNumOperands()) {
459468 unsigned N = RI.getNumOperands();
460 if (N == 1)
461 RetVal.init(RI.RetVal, this);
469 if (N == 1)
470 Op<0>().init(RI.Op<0>(), this);
462471 else if (N) {
463 Use *OL = OperandList = new Use[N];
472 Use *OL = OperandList;
464473 for (unsigned i = 0; i < N; ++i)
465474 OL[i].init(RI.getOperand(i), this);
466475 }
467476 }
468477
469478 ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
470 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
479 : TerminatorInst(Type::VoidTy, Instruction::Ret,
480 OperandTraits::op_end(this) - (retVal != 0),
481 retVal != 0, InsertBefore) {
471482 if (retVal)
472483 init(&retVal, 1);
473484 }
474485 ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
475 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
486 : TerminatorInst(Type::VoidTy, Instruction::Ret,
487 OperandTraits::op_end(this) - (retVal != 0),
488 retVal != 0, InsertAtEnd) {
476489 if (retVal)
477490 init(&retVal, 1);
478491 }
479492 ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
480 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
493 : TerminatorInst(Type::VoidTy, Instruction::Ret,
494 OperandTraits::op_end(this),
495 0, InsertAtEnd) {
481496 }
482497
483498 ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
484499 Instruction *InsertBefore)
485 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) {
500 : TerminatorInst(Type::VoidTy, Instruction::Ret,
501 OperandTraits::op_end(this) - N,
502 N, InsertBefore) {
486503 if (N != 0)
487504 init(retVals, N);
488505 }
489506 ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
490507 BasicBlock *InsertAtEnd)
491 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) {
492 if (N != 0)
493 init(retVals, N);
494 }
495 ReturnInst::ReturnInst(Value * const* retVals, unsigned N)
496 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) {
508 : TerminatorInst(Type::VoidTy, Instruction::Ret,
509 OperandTraits::op_end(this) - N,
510 N, InsertAtEnd) {
497511 if (N != 0)
498512 init(retVals, N);
499513 }
506520 Value *V = *retVals;
507521 if (V->getType() == Type::VoidTy)
508522 return;
509 RetVal.init(V, this);
523 Op<0>().init(V, this);
510524 return;
511525 }
512526
513 Use *OL = OperandList = new Use[NumOperands];
527 Use *OL = OperandList;
514528 for (unsigned i = 0; i < NumOperands; ++i) {
515529 Value *V = *retVals++;
516530 assert(!isa(V) &&
536550 }
537551
538552 ReturnInst::~ReturnInst() {
539 if (NumOperands > 1)
540 delete [] OperandList;
541553 }
542554
543555 //===----------------------------------------------------------------------===//
602614 }
603615
604616 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
605 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
617 : TerminatorInst(Type::VoidTy, Instruction::Br,
618 OperandTraits::op_end(this) - 1,
619 1, InsertBefore) {
606620 assert(IfTrue != 0 && "Branch destination may not be null!");
607 Ops[0].init(reinterpret_cast(IfTrue), this);
621 Op<0>().init(reinterpret_cast(IfTrue), this);
608622 }
609623 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
610624 Instruction *InsertBefore)
611 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
612 Ops[0].init(reinterpret_cast(IfTrue), this);
613 Ops[1].init(reinterpret_cast(IfFalse), this);
614 Ops[2].init(Cond, this);
625 : TerminatorInst(Type::VoidTy, Instruction::Br,
626 OperandTraits::op_end(this) - 3,
627 3, InsertBefore) {
628 Op<0>().init(reinterpret_cast(IfTrue), this);
629 Op<1>().init(reinterpret_cast(IfFalse), this);
630 Op<2>().init(Cond, this);
615631 #ifndef NDEBUG
616632 AssertOK();
617633 #endif
618634 }
619635
620636 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
621 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
637 : TerminatorInst(Type::VoidTy, Instruction::Br,
638 OperandTraits::op_end(this) - 1,
639 1, InsertAtEnd) {
622640 assert(IfTrue != 0 && "Branch destination may not be null!");
623 Ops[0].init(reinterpret_cast(IfTrue), this);
641 Op<0>().init(reinterpret_cast(IfTrue), this);
624642 }
625643
626644 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
627645 BasicBlock *InsertAtEnd)
628 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
629 Ops[0].init(reinterpret_cast(IfTrue), this);
630 Ops[1].init(reinterpret_cast(IfFalse), this);
631 Ops[2].init(Cond, this);
646 : TerminatorInst(Type::VoidTy, Instruction::Br,
647 OperandTraits::op_end(this) - 3,
648 3, InsertAtEnd) {
649 Op<0>().init(reinterpret_cast(IfTrue), this);
650 Op<1>().init(reinterpret_cast(IfFalse), this);
651 Op<2>().init(Cond, this);
632652 #ifndef NDEBUG
633653 AssertOK();
634654 #endif
636656
637657
638658 BranchInst::BranchInst(const BranchInst &BI) :
639 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
659 TerminatorInst(Type::VoidTy, Instruction::Br,
660 OperandTraits::op_end(this) - BI.getNumOperands(),
661 BI.getNumOperands()) {
640662 OperandList[0].init(BI.getOperand(0), this);
641663 if (BI.getNumOperands() != 1) {
642664 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
868890
869891
870892 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
871 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
872 Ops[0].init(val, this);
873 Ops[1].init(addr, this);
893 : Instruction(Type::VoidTy, Store,
894 OperandTraits::op_begin(this),
895 OperandTraits::operands(this),
896 InsertBefore) {
897 Op<0>().init(val, this);
898 Op<1>().init(addr, this);
874899 setVolatile(false);
875900 setAlignment(0);
876901 AssertOK();
877902 }
878903
879904 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
880 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
881 Ops[0].init(val, this);
882 Ops[1].init(addr, this);
905 : Instruction(Type::VoidTy, Store,
906 OperandTraits::op_begin(this),
907 OperandTraits::operands(this),
908 InsertAtEnd) {
909 Op<0>().init(val, this);
910 Op<1>().init(addr, this);
883911 setVolatile(false);
884912 setAlignment(0);
885913 AssertOK();
887915
888916 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
889917 Instruction *InsertBefore)
890 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
891 Ops[0].init(val, this);
892 Ops[1].init(addr, this);
918 : Instruction(Type::VoidTy, Store,
919 OperandTraits::op_begin(this),
920 OperandTraits::operands(this),
921 InsertBefore) {
922 Op<0>().init(val, this);
923 Op<1>().init(addr, this);
893924 setVolatile(isVolatile);
894925 setAlignment(0);
895926 AssertOK();
897928
898929 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
899930 unsigned Align, Instruction *InsertBefore)
900 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
901 Ops[0].init(val, this);
902 Ops[1].init(addr, this);
931 : Instruction(Type::VoidTy, Store,
932 OperandTraits::op_begin(this),
933 OperandTraits::operands(this),
934 InsertBefore) {
935 Op<0>().init(val, this);
936 Op<1>().init(addr, this);
903937 setVolatile(isVolatile);
904938 setAlignment(Align);
905939 AssertOK();
907941
908942 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
909943 unsigned Align, BasicBlock *InsertAtEnd)
910 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
911 Ops[0].init(val, this);
912 Ops[1].init(addr, this);
944 : Instruction(Type::VoidTy, Store,
945 OperandTraits::op_begin(this),
946 OperandTraits::operands(this),
947 InsertAtEnd) {
948 Op<0>().init(val, this);
949 Op<1>().init(addr, this);
913950 setVolatile(isVolatile);
914951 setAlignment(Align);
915952 AssertOK();
917954
918955 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
919956 BasicBlock *InsertAtEnd)
920 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
921 Ops[0].init(val, this);
922 Ops[1].init(addr, this);
957 : Instruction(Type::VoidTy, Store,
958 OperandTraits::op_begin(this),
959 OperandTraits::operands(this),
960 InsertAtEnd) {
961 Op<0>().init(val, this);
962 Op<1>().init(addr, this);
923963 setVolatile(isVolatile);
924964 setAlignment(0);
925965 AssertOK();
939979 }
940980
941981 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
942 NumOperands = 1+NumIdx;
943 Use *OL = OperandList = new Use[NumOperands];
982 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
983 Use *OL = OperandList;
944984 OL[0].init(Ptr, this);
945985
946986 for (unsigned i = 0; i != NumIdx; ++i)
948988 }
949989
950990 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
951 NumOperands = 2;
952 Use *OL = OperandList = new Use[2];
991 assert(NumOperands == 2 && "NumOperands not initialized?");
992 Use *OL = OperandList;
953993 OL[0].init(Ptr, this);
954994 OL[1].init(Idx, this);
995 }
996
997 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
998 : Instruction(reinterpret_cast(GEPI.getType()), GetElementPtr,
999 OperandTraits::op_end(this) - GEPI.getNumOperands(),
1000 GEPI.getNumOperands()) {
1001 Use *OL = OperandList;
1002 Use *GEPIOL = GEPI.OperandList;
1003 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1004 OL[i].init(GEPIOL[i], this);
9551005 }
9561006
9571007 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
9581008 const std::string &Name, Instruction *InBe)
9591009 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
9601010 retrieveAddrSpace(Ptr)),
961 GetElementPtr, 0, 0, InBe) {
1011 GetElementPtr,
1012 OperandTraits::op_end(this) - 2,
1013 2, InBe) {
9621014 init(Ptr, Idx);
9631015 setName(Name);
9641016 }
9671019 const std::string &Name, BasicBlock *IAE)
9681020 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
9691021 retrieveAddrSpace(Ptr)),
970 GetElementPtr, 0, 0, IAE) {
1022 GetElementPtr,
1023 OperandTraits::op_end(this) - 2,
1024 2, IAE) {
9711025 init(Ptr, Idx);
9721026 setName(Name);
973 }
974
975 GetElementPtrInst::~GetElementPtrInst() {
976 delete[] OperandList;
9771027 }
9781028
9791029 // getIndexedType - Returns the type of the element that would be loaded with
10661116 const std::string &Name,
10671117 Instruction *InsertBef)
10681118 : Instruction(cast(Val->getType())->getElementType(),
1069 ExtractElement, Ops, 2, InsertBef) {
1119 ExtractElement,
1120 OperandTraits::op_begin(this),
1121 2, InsertBef) {
10701122 assert(isValidOperands(Val, Index) &&
10711123 "Invalid extractelement instruction operands!");
1072 Ops[0].init(Val, this);
1073 Ops[1].init(Index, this);
1124 Op<0>().init(Val, this);
1125 Op<1>().init(Index, this);
10741126 setName(Name);
10751127 }
10761128
10781130 const std::string &Name,
10791131 Instruction *InsertBef)
10801132 : Instruction(cast(Val->getType())->getElementType(),
1081 ExtractElement, Ops, 2, InsertBef) {
1133 ExtractElement,
1134 OperandTraits::op_begin(this),
1135 2, InsertBef) {
10821136 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
10831137 assert(isValidOperands(Val, Index) &&
10841138 "Invalid extractelement instruction operands!");
1085 Ops[0].init(Val, this);
1086 Ops[1].init(Index, this);
1139 Op<0>().init(Val, this);
1140 Op<1>().init(Index, this);
10871141 setName(Name);
10881142 }
10891143
10921146 const std::string &Name,
10931147 BasicBlock *InsertAE)
10941148 : Instruction(cast(Val->getType())->getElementType(),
1095 ExtractElement, Ops, 2, InsertAE) {
1149 ExtractElement,
1150 OperandTraits::op_begin(this),
1151 2, InsertAE) {
10961152 assert(isValidOperands(Val, Index) &&
10971153 "Invalid extractelement instruction operands!");
10981154
1099 Ops[0].init(Val, this);
1100 Ops[1].init(Index, this);
1155 Op<0>().init(Val, this);
1156 Op<1>().init(Index, this);
11011157 setName(Name);
11021158 }
11031159
11051161 const std::string &Name,
11061162 BasicBlock *InsertAE)
11071163 : Instruction(cast(Val->getType())->getElementType(),
1108 ExtractElement, Ops, 2, InsertAE) {
1164 ExtractElement,
1165 OperandTraits::op_begin(this),
1166 2, InsertAE) {
11091167 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
11101168 assert(isValidOperands(Val, Index) &&
11111169 "Invalid extractelement instruction operands!");
11121170
1113 Ops[0].init(Val, this);
1114 Ops[1].init(Index, this);
1171 Op<0>().init(Val, this);
1172 Op<1>().init(Index, this);
11151173 setName(Name);
11161174 }
11171175
11281186 //===----------------------------------------------------------------------===//
11291187
11301188 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1131 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1132 Ops[0].init(IE.Ops[0], this);
1133 Ops[1].init(IE.Ops[1], this);
1134 Ops[2].init(IE.Ops[2], this);
1189 : Instruction(IE.getType(), InsertElement,
1190 OperandTraits::op_begin(this), 3) {
1191 Op<0>().init(IE.Op<0>(), this);
1192 Op<1>().init(IE.Op<1>(), this);
1193 Op<2>().init(IE.Op<2>(), this);
11351194 }
11361195 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
11371196 const std::string &Name,
11381197 Instruction *InsertBef)
1139 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
1198 : Instruction(Vec->getType(), InsertElement,
1199 OperandTraits::op_begin(this),
1200 3, InsertBef) {
11401201 assert(isValidOperands(Vec, Elt, Index) &&
11411202 "Invalid insertelement instruction operands!");
1142 Ops[0].init(Vec, this);
1143 Ops[1].init(Elt, this);
1144 Ops[2].init(Index, this);
1203 Op<0>().init(Vec, this);
1204 Op<1>().init(Elt, this);
1205 Op<2>().init(Index, this);
11451206 setName(Name);
11461207 }
11471208
11481209 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
11491210 const std::string &Name,
11501211 Instruction *InsertBef)
1151 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
1212 : Instruction(Vec->getType(), InsertElement,
1213 OperandTraits::op_begin(this),
1214 3, InsertBef) {
11521215 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
11531216 assert(isValidOperands(Vec, Elt, Index) &&
11541217 "Invalid insertelement instruction operands!");
1155