]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/IntrinsicInst.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / IntrinsicInst.h
1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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_IR_INTRINSICINST_H
25 #define LLVM_IR_INTRINSICINST_H
26
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include <cassert>
37 #include <cstdint>
38
39 namespace llvm {
40
41   /// A wrapper class for inspecting calls to intrinsic functions.
42   /// This allows the standard isa/dyncast/cast functionality to work with calls
43   /// to intrinsic functions.
44   class IntrinsicInst : public CallInst {
45   public:
46     IntrinsicInst() = delete;
47     IntrinsicInst(const IntrinsicInst &) = delete;
48     IntrinsicInst &operator=(const IntrinsicInst &) = delete;
49
50     /// Return the intrinsic ID of this intrinsic.
51     Intrinsic::ID getIntrinsicID() const {
52       return getCalledFunction()->getIntrinsicID();
53     }
54
55     // Methods for support type inquiry through isa, cast, and dyn_cast:
56     static inline bool classof(const CallInst *I) {
57       if (const Function *CF = I->getCalledFunction())
58         return CF->isIntrinsic();
59       return false;
60     }
61     static inline bool classof(const Value *V) {
62       return isa<CallInst>(V) && classof(cast<CallInst>(V));
63     }
64   };
65
66   /// This is the common base class for debug info intrinsics.
67   class DbgInfoIntrinsic : public IntrinsicInst {
68   public:
69     /// Get the location corresponding to the variable referenced by the debug
70     /// info intrinsic.  Depending on the intrinsic, this could be the
71     /// variable's value or its address.
72     Value *getVariableLocation(bool AllowNullOp = true) const;
73
74     // Methods for support type inquiry through isa, cast, and dyn_cast:
75     static inline bool classof(const IntrinsicInst *I) {
76       switch (I->getIntrinsicID()) {
77       case Intrinsic::dbg_declare:
78       case Intrinsic::dbg_value:
79         return true;
80       default: return false;
81       }
82     }
83     static inline bool classof(const Value *V) {
84       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
85     }
86   };
87
88   /// This represents the llvm.dbg.declare instruction.
89   class DbgDeclareInst : public DbgInfoIntrinsic {
90   public:
91     Value *getAddress() const { return getVariableLocation(); }
92
93     DILocalVariable *getVariable() const {
94       return cast<DILocalVariable>(getRawVariable());
95     }
96
97     DIExpression *getExpression() const {
98       return cast<DIExpression>(getRawExpression());
99     }
100
101     Metadata *getRawVariable() const {
102       return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
103     }
104
105     Metadata *getRawExpression() const {
106       return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
107     }
108
109     // Methods for support type inquiry through isa, cast, and dyn_cast:
110     static inline bool classof(const IntrinsicInst *I) {
111       return I->getIntrinsicID() == Intrinsic::dbg_declare;
112     }
113     static inline bool classof(const Value *V) {
114       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
115     }
116   };
117
118   /// This represents the llvm.dbg.value instruction.
119   class DbgValueInst : public DbgInfoIntrinsic {
120   public:
121     Value *getValue() const {
122       return getVariableLocation(/* AllowNullOp = */ false);
123     }
124
125     uint64_t getOffset() const {
126       return cast<ConstantInt>(
127                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
128     }
129
130     DILocalVariable *getVariable() const {
131       return cast<DILocalVariable>(getRawVariable());
132     }
133
134     DIExpression *getExpression() const {
135       return cast<DIExpression>(getRawExpression());
136     }
137
138     Metadata *getRawVariable() const {
139       return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
140     }
141
142     Metadata *getRawExpression() const {
143       return cast<MetadataAsValue>(getArgOperand(3))->getMetadata();
144     }
145
146     // Methods for support type inquiry through isa, cast, and dyn_cast:
147     static inline bool classof(const IntrinsicInst *I) {
148       return I->getIntrinsicID() == Intrinsic::dbg_value;
149     }
150     static inline bool classof(const Value *V) {
151       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
152     }
153   };
154
155   /// This is the common base class for constrained floating point intrinsics.
156   class ConstrainedFPIntrinsic : public IntrinsicInst {
157   public:
158     enum RoundingMode {
159       rmInvalid,
160       rmDynamic,
161       rmToNearest,
162       rmDownward,
163       rmUpward,
164       rmTowardZero
165     };
166
167     enum ExceptionBehavior {
168       ebInvalid,
169       ebIgnore,
170       ebMayTrap,
171       ebStrict
172     };
173
174     bool isUnaryOp() const;
175     RoundingMode getRoundingMode() const;
176     ExceptionBehavior getExceptionBehavior() const;
177
178     // Methods for support type inquiry through isa, cast, and dyn_cast:
179     static inline bool classof(const IntrinsicInst *I) {
180       switch (I->getIntrinsicID()) {
181       case Intrinsic::experimental_constrained_fadd:
182       case Intrinsic::experimental_constrained_fsub:
183       case Intrinsic::experimental_constrained_fmul:
184       case Intrinsic::experimental_constrained_fdiv:
185       case Intrinsic::experimental_constrained_frem:
186       case Intrinsic::experimental_constrained_sqrt:
187       case Intrinsic::experimental_constrained_pow:
188       case Intrinsic::experimental_constrained_powi:
189       case Intrinsic::experimental_constrained_sin:
190       case Intrinsic::experimental_constrained_cos:
191       case Intrinsic::experimental_constrained_exp:
192       case Intrinsic::experimental_constrained_exp2:
193       case Intrinsic::experimental_constrained_log:
194       case Intrinsic::experimental_constrained_log10:
195       case Intrinsic::experimental_constrained_log2:
196       case Intrinsic::experimental_constrained_rint:
197       case Intrinsic::experimental_constrained_nearbyint:
198         return true;
199       default: return false;
200       }
201     }
202     static inline bool classof(const Value *V) {
203       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
204     }
205   };
206
207   /// This class represents atomic memcpy intrinsic
208   /// TODO: Integrate this class into MemIntrinsic hierarchy.
209   class ElementAtomicMemCpyInst : public IntrinsicInst {
210   public:
211     Value *getRawDest() const { return getArgOperand(0); }
212     Value *getRawSource() const { return getArgOperand(1); }
213
214     Value *getNumElements() const { return getArgOperand(2); }
215     void setNumElements(Value *V) { setArgOperand(2, V); }
216
217     uint64_t getSrcAlignment() const { return getParamAlignment(0); }
218     uint64_t getDstAlignment() const { return getParamAlignment(1); }
219
220     uint64_t getElementSizeInBytes() const {
221       Value *Arg = getArgOperand(3);
222       return cast<ConstantInt>(Arg)->getZExtValue();
223     }
224
225     static inline bool classof(const IntrinsicInst *I) {
226       return I->getIntrinsicID() == Intrinsic::memcpy_element_atomic;
227     }
228     static inline bool classof(const Value *V) {
229       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
230     }
231   };
232
233   /// This is the common base class for memset/memcpy/memmove.
234   class MemIntrinsic : public IntrinsicInst {
235   public:
236     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
237     const Use &getRawDestUse() const { return getArgOperandUse(0); }
238     Use &getRawDestUse() { return getArgOperandUse(0); }
239
240     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
241     const Use &getLengthUse() const { return getArgOperandUse(2); }
242     Use &getLengthUse() { return getArgOperandUse(2); }
243
244     ConstantInt *getAlignmentCst() const {
245       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
246     }
247
248     unsigned getAlignment() const {
249       return getAlignmentCst()->getZExtValue();
250     }
251
252     ConstantInt *getVolatileCst() const {
253       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
254     }
255
256     bool isVolatile() const {
257       return !getVolatileCst()->isZero();
258     }
259
260     unsigned getDestAddressSpace() const {
261       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
262     }
263
264     /// This is just like getRawDest, but it strips off any cast
265     /// instructions that feed it, giving the original input.  The returned
266     /// value is guaranteed to be a pointer.
267     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
268
269     /// Set the specified arguments of the instruction.
270     void setDest(Value *Ptr) {
271       assert(getRawDest()->getType() == Ptr->getType() &&
272              "setDest called with pointer of wrong type!");
273       setArgOperand(0, Ptr);
274     }
275
276     void setLength(Value *L) {
277       assert(getLength()->getType() == L->getType() &&
278              "setLength called with value of wrong type!");
279       setArgOperand(2, L);
280     }
281
282     void setAlignment(Constant* A) {
283       setArgOperand(3, A);
284     }
285
286     void setVolatile(Constant* V) {
287       setArgOperand(4, V);
288     }
289
290     Type *getAlignmentType() const {
291       return getArgOperand(3)->getType();
292     }
293
294     // Methods for support type inquiry through isa, cast, and dyn_cast:
295     static inline bool classof(const IntrinsicInst *I) {
296       switch (I->getIntrinsicID()) {
297       case Intrinsic::memcpy:
298       case Intrinsic::memmove:
299       case Intrinsic::memset:
300         return true;
301       default: return false;
302       }
303     }
304     static inline bool classof(const Value *V) {
305       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
306     }
307   };
308
309   /// This class wraps the llvm.memset intrinsic.
310   class MemSetInst : public MemIntrinsic {
311   public:
312     /// Return the arguments to the instruction.
313     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
314     const Use &getValueUse() const { return getArgOperandUse(1); }
315     Use &getValueUse() { return getArgOperandUse(1); }
316
317     void setValue(Value *Val) {
318       assert(getValue()->getType() == Val->getType() &&
319              "setValue called with value of wrong type!");
320       setArgOperand(1, Val);
321     }
322
323     // Methods for support type inquiry through isa, cast, and dyn_cast:
324     static inline bool classof(const IntrinsicInst *I) {
325       return I->getIntrinsicID() == Intrinsic::memset;
326     }
327     static inline bool classof(const Value *V) {
328       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
329     }
330   };
331
332   /// This class wraps the llvm.memcpy/memmove intrinsics.
333   class MemTransferInst : public MemIntrinsic {
334   public:
335     /// Return the arguments to the instruction.
336     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
337     const Use &getRawSourceUse() const { return getArgOperandUse(1); }
338     Use &getRawSourceUse() { return getArgOperandUse(1); }
339
340     /// This is just like getRawSource, but it strips off any cast
341     /// instructions that feed it, giving the original input.  The returned
342     /// value is guaranteed to be a pointer.
343     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
344
345     unsigned getSourceAddressSpace() const {
346       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
347     }
348
349     void setSource(Value *Ptr) {
350       assert(getRawSource()->getType() == Ptr->getType() &&
351              "setSource called with pointer of wrong type!");
352       setArgOperand(1, Ptr);
353     }
354
355     // Methods for support type inquiry through isa, cast, and dyn_cast:
356     static inline bool classof(const IntrinsicInst *I) {
357       return I->getIntrinsicID() == Intrinsic::memcpy ||
358              I->getIntrinsicID() == Intrinsic::memmove;
359     }
360     static inline bool classof(const Value *V) {
361       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
362     }
363   };
364
365   /// This class wraps the llvm.memcpy intrinsic.
366   class MemCpyInst : public MemTransferInst {
367   public:
368     // Methods for support type inquiry through isa, cast, and dyn_cast:
369     static inline bool classof(const IntrinsicInst *I) {
370       return I->getIntrinsicID() == Intrinsic::memcpy;
371     }
372     static inline bool classof(const Value *V) {
373       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
374     }
375   };
376
377   /// This class wraps the llvm.memmove intrinsic.
378   class MemMoveInst : public MemTransferInst {
379   public:
380     // Methods for support type inquiry through isa, cast, and dyn_cast:
381     static inline bool classof(const IntrinsicInst *I) {
382       return I->getIntrinsicID() == Intrinsic::memmove;
383     }
384     static inline bool classof(const Value *V) {
385       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
386     }
387   };
388
389   /// This represents the llvm.va_start intrinsic.
390   class VAStartInst : public IntrinsicInst {
391   public:
392     static inline bool classof(const IntrinsicInst *I) {
393       return I->getIntrinsicID() == Intrinsic::vastart;
394     }
395     static inline bool classof(const Value *V) {
396       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
397     }
398
399     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
400   };
401
402   /// This represents the llvm.va_end intrinsic.
403   class VAEndInst : public IntrinsicInst {
404   public:
405     static inline bool classof(const IntrinsicInst *I) {
406       return I->getIntrinsicID() == Intrinsic::vaend;
407     }
408     static inline bool classof(const Value *V) {
409       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
410     }
411
412     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
413   };
414
415   /// This represents the llvm.va_copy intrinsic.
416   class VACopyInst : public IntrinsicInst {
417   public:
418     static inline bool classof(const IntrinsicInst *I) {
419       return I->getIntrinsicID() == Intrinsic::vacopy;
420     }
421     static inline bool classof(const Value *V) {
422       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
423     }
424
425     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
426     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
427   };
428
429   /// This represents the llvm.instrprof_increment intrinsic.
430   class InstrProfIncrementInst : public IntrinsicInst {
431   public:
432     static inline bool classof(const IntrinsicInst *I) {
433       return I->getIntrinsicID() == Intrinsic::instrprof_increment;
434     }
435     static inline bool classof(const Value *V) {
436       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
437     }
438
439     GlobalVariable *getName() const {
440       return cast<GlobalVariable>(
441           const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
442     }
443
444     ConstantInt *getHash() const {
445       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
446     }
447
448     ConstantInt *getNumCounters() const {
449       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
450     }
451
452     ConstantInt *getIndex() const {
453       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
454     }
455
456     Value *getStep() const;
457   };
458
459   class InstrProfIncrementInstStep : public InstrProfIncrementInst {
460   public:
461     static inline bool classof(const IntrinsicInst *I) {
462       return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
463     }
464     static inline bool classof(const Value *V) {
465       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
466     }
467   };
468
469   /// This represents the llvm.instrprof_value_profile intrinsic.
470   class InstrProfValueProfileInst : public IntrinsicInst {
471   public:
472     static inline bool classof(const IntrinsicInst *I) {
473       return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
474     }
475     static inline bool classof(const Value *V) {
476       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
477     }
478
479     GlobalVariable *getName() const {
480       return cast<GlobalVariable>(
481           const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
482     }
483
484     ConstantInt *getHash() const {
485       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
486     }
487
488     Value *getTargetValue() const {
489       return cast<Value>(const_cast<Value *>(getArgOperand(2)));
490     }
491
492     ConstantInt *getValueKind() const {
493       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
494     }
495
496     // Returns the value site index.
497     ConstantInt *getIndex() const {
498       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
499     }
500   };
501
502 } // end namespace llvm
503
504 #endif // LLVM_IR_INTRINSICINST_H