0ef3ee107db8e13c4fa149bbf1631906c447d381
[oota-llvm.git] / include / llvm / iMemory.h
1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
2 //
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_IMEMORY_H
9 #define LLVM_IMEMORY_H
10
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
13
14 class AllocationInst : public Instruction {
15 public:
16   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
17                  const string &Name = "")
18     : Instruction(Ty, iTy, Name) {
19     assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
20
21     if (ArraySize) {
22       // Make sure they didn't try to specify a size for !(unsized array) type...
23       assert((getType()->getValueType()->isArrayType() && 
24               ((const ArrayType*)getType()->getValueType())->isUnsized()) && 
25             "Trying to allocate something other than unsized array, with size!");
26
27       Operands.reserve(1);
28       Operands.push_back(Use(ArraySize, this));
29     }
30   }
31
32   // getType - Overload to return most specific pointer type...
33   inline const PointerType *getType() const {
34     return (const PointerType*)Instruction::getType(); 
35   }
36
37   virtual Instruction *clone() const = 0;
38 };
39
40 class MallocInst : public AllocationInst {
41 public:
42   MallocInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "") 
43     : AllocationInst(Ty, ArraySize, Instruction::Malloc, Name) {}
44
45   virtual Instruction *clone() const { 
46     return new MallocInst(getType(), Operands.size() ? Operands[1] : 0);
47   }
48
49   virtual const char *getOpcodeName() const { return "malloc"; }
50 };
51
52 class AllocaInst : public AllocationInst {
53 public:
54   AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "") 
55     : AllocationInst(Ty, ArraySize, Instruction::Alloca, Name) {}
56
57   virtual Instruction *clone() const { 
58     return new AllocaInst(getType(), Operands.size() ? Operands[1] : 0);
59   }
60
61   virtual const char *getOpcodeName() const { return "alloca"; }
62 };
63
64
65
66 class FreeInst : public Instruction {
67 public:
68   FreeInst(Value *Ptr, const string &Name = "") 
69     : Instruction(Type::VoidTy, Instruction::Free, Name) {
70     assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
71     Operands.reserve(1);
72     Operands.push_back(Use(Ptr, this));
73   }
74   inline ~FreeInst() {}
75
76   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
77
78   virtual const char *getOpcodeName() const { return "free"; }
79 };
80
81 #endif // LLVM_IMEMORY_H