IntrinsicInst.h revision 1085548d35ccaf5e90e001b5c2af8cc311872f15
1//===-- llvm/InstrinsicInst.h - Intrinsic Instruction Wrappers --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines classes that make it really easy to deal with intrinsic
11// functions with the isa/dyncast family of functions.  In particular, this
12// allows you to do things like:
13//
14//     if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
15//        ... MCI->getDest() ... MCI->getSource() ...
16//
17// All intrinsic function calls are instances of the call instruction, so these
18// are all subclasses of the CallInst class.  Note that none of these classes
19// has state or virtual methods, which is an important part of this gross/neat
20// hack working.
21//
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_INTRINSICINST_H
25#define LLVM_INTRINSICINST_H
26
27#include "llvm/Constants.h"
28#include "llvm/Function.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31
32namespace llvm {
33  class IntrinsicInst : public CallInst {
34    IntrinsicInst();                      // DO NOT IMPLEMENT
35    IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
36    void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
37  public:
38
39    /// StripPointerCasts - This static method strips off any unneeded pointer
40    /// casts from the specified value, returning the original uncasted value.
41    /// Note that the returned value is guaranteed to have pointer type.
42    static Value *StripPointerCasts(Value *Ptr);
43  };
44
45  /// DbgStopPointInst - This represent llvm.dbg.stoppoint instructions.
46  ///
47  struct DbgStopPointInst : public IntrinsicInst {
48
49    Value *getChain() const { return const_cast<Value*>(getOperand(1)); }
50    unsigned getLineNo() const {
51      return cast<ConstantInt>(getOperand(2))->getRawValue();
52    }
53    unsigned getColNo() const {
54      return cast<ConstantInt>(getOperand(3))->getRawValue();
55    }
56    Value *getContext() const { return const_cast<Value*>(getOperand(4)); }
57
58
59    // Methods for support type inquiry through isa, cast, and dyn_cast:
60    static inline bool classof(const DbgStopPointInst *) { return true; }
61    static inline bool classof(const CallInst *I) {
62      if (const Function *CF = I->getCalledFunction())
63        return CF->getIntrinsicID() == Intrinsic::dbg_stoppoint;
64      return false;
65    }
66    static inline bool classof(const Value *V) {
67      return isa<CallInst>(V) && classof(cast<CallInst>(V));
68    }
69  };
70
71
72
73  /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
74  ///
75  struct MemIntrinsic : public IntrinsicInst {
76    Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
77
78    Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
79    ConstantInt *getAlignment() const {
80      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
81    }
82
83    /// getDest - This is just like getRawDest, but it strips off any cast
84    /// instructions that feed it, giving the original input.  The returned
85    /// value is guaranteed to be a pointer.
86    Value *getDest() const { return StripPointerCasts(getRawDest()); }
87
88    /// set* - Set the specified arguments of the instruction.
89    ///
90    void setDest(Value *Ptr) {
91      assert(getRawDest()->getType() == Ptr->getType() &&
92             "setDest called with pointer of wrong type!");
93      setOperand(1, Ptr);
94    }
95
96    void setLength(Value *L) {
97      assert(getLength()->getType() == L->getType() &&
98             "setLength called with value of wrong type!");
99      setOperand(3, L);
100    }
101    void setAlignment(ConstantInt *A) {
102      assert(getAlignment()->getType() == A->getType() &&
103             "setAlignment called with value of wrong type!");
104      setOperand(4, A);
105    }
106
107    // Methods for support type inquiry through isa, cast, and dyn_cast:
108    static inline bool classof(const MemIntrinsic *) { return true; }
109    static inline bool classof(const CallInst *I) {
110      if (const Function *CF = I->getCalledFunction())
111        switch (CF->getIntrinsicID()) {
112        case Intrinsic::memcpy:
113        case Intrinsic::memmove:
114        case Intrinsic::memset:
115          return true;
116        default: break;
117        }
118      return false;
119    }
120    static inline bool classof(const Value *V) {
121      return isa<CallInst>(V) && classof(cast<CallInst>(V));
122    }
123  };
124
125
126  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
127  ///
128  struct MemCpyInst : public MemIntrinsic {
129    /// get* - Return the arguments to the instruction.
130    ///
131    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
132
133    /// getSource - This is just like getRawSource, but it strips off any cast
134    /// instructions that feed it, giving the original input.  The returned
135    /// value is guaranteed to be a pointer.
136    Value *getSource() const { return StripPointerCasts(getRawSource()); }
137
138
139    void setSource(Value *Ptr) {
140      assert(getRawSource()->getType() == Ptr->getType() &&
141             "setSource called with pointer of wrong type!");
142      setOperand(2, Ptr);
143    }
144
145    // Methods for support type inquiry through isa, cast, and dyn_cast:
146    static inline bool classof(const MemCpyInst *) { return true; }
147    static inline bool classof(const MemIntrinsic *I) {
148      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memcpy;
149    }
150    static inline bool classof(const CallInst *I) {
151      if (const Function *CF = I->getCalledFunction())
152        if (CF->getIntrinsicID() == Intrinsic::memcpy)
153          return true;
154      return false;
155    }
156    static inline bool classof(const Value *V) {
157      return isa<CallInst>(V) && classof(cast<CallInst>(V));
158    }
159  };
160
161  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
162  ///
163  struct MemMoveInst : public MemIntrinsic {
164    /// get* - Return the arguments to the instruction.
165    ///
166    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
167
168    /// getSource - This is just like getRawSource, but it strips off any cast
169    /// instructions that feed it, giving the original input.  The returned
170    /// value is guaranteed to be a pointer.
171    Value *getSource() const { return StripPointerCasts(getRawSource()); }
172
173    void setSource(Value *Ptr) {
174      assert(getRawSource()->getType() == Ptr->getType() &&
175             "setSource called with pointer of wrong type!");
176      setOperand(2, Ptr);
177    }
178
179    // Methods for support type inquiry through isa, cast, and dyn_cast:
180    static inline bool classof(const MemMoveInst *) { return true; }
181    static inline bool classof(const MemIntrinsic *I) {
182      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memmove;
183    }
184    static inline bool classof(const CallInst *I) {
185      if (const Function *CF = I->getCalledFunction())
186        if (CF->getIntrinsicID() == Intrinsic::memmove)
187          return true;
188      return false;
189    }
190    static inline bool classof(const Value *V) {
191      return isa<CallInst>(V) && classof(cast<CallInst>(V));
192    }
193  };
194
195  /// MemSetInst - This class wraps the llvm.memcpy intrinsic.
196  ///
197  struct MemSetInst : public MemIntrinsic {
198    /// get* - Return the arguments to the instruction.
199    ///
200    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
201
202    void setValue(Value *Val) {
203      assert(getValue()->getType() == Val->getType() &&
204             "setSource called with pointer of wrong type!");
205      setOperand(2, Val);
206    }
207
208    // Methods for support type inquiry through isa, cast, and dyn_cast:
209    static inline bool classof(const MemSetInst *) { return true; }
210    static inline bool classof(const MemIntrinsic *I) {
211      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memset;
212    }
213    static inline bool classof(const CallInst *I) {
214      if (const Function *CF = I->getCalledFunction())
215        if (CF->getIntrinsicID() == Intrinsic::memset)
216          return true;
217      return false;
218    }
219    static inline bool classof(const Value *V) {
220      return isa<CallInst>(V) && classof(cast<CallInst>(V));
221    }
222  };
223}
224
225#endif
226