6811626ccc63e71b1558b8bb3205286fb4abb57a
[oota-llvm.git] / include / llvm / Support / CallSite.h
1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- 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 the CallSite class, which is a handy wrapper for code that
11 // wants to treat Call and Invoke instructions in a generic way.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_CALLSITE_H
16 #define LLVM_SUPPORT_CALLSITE_H
17
18 #include "llvm/Instruction.h"
19
20 class CallInst;
21 class InvokeInst;
22
23 class CallSite {
24   Instruction *I;
25 public:
26   CallSite() : I(0) {}
27   CallSite(CallInst *CI) : I((Instruction*)CI) {}
28   CallSite(InvokeInst *II) : I((Instruction*)II) {}
29   CallSite(const CallSite &CS) : I(CS.I) {}
30   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
31
32   /// CallSite::get - This static method is sort of like a constructor.  It will
33   /// create an appropriate call site for a Call or Invoke instruction, but it
34   /// can also create a null initialized CallSite object for something which is
35   /// NOT a call site.
36   ///
37   static CallSite get(Value *V) {
38     if (Instruction *I = dyn_cast<Instruction>(V)) {
39       if (I->getOpcode() == Instruction::Call)
40         return CallSite((CallInst*)I);
41       else if (I->getOpcode() == Instruction::Invoke)
42         return CallSite((InvokeInst*)I);
43     }
44     return CallSite();
45   }
46
47   /// getType - Return the type of the instruction that generated this call site
48   ///
49   const Type *getType () const { return I->getType (); }
50
51   /// getInstruction - Return the instruction this call site corresponds to
52   ///
53   Instruction *getInstruction() const { return I; }
54
55   /// getCalledValue - Return the pointer to function that is being called...
56   ///
57   Value *getCalledValue() const {
58     assert(I && "Not a call or invoke instruction!");
59     return I->getOperand(0);
60   }
61
62   /// getCalledFunction - Return the function being called if this is a direct
63   /// call, otherwise return null (if it's an indirect call).
64   ///
65   /// FIXME: This should be inlined once ConstantPointerRefs are gone.  :(
66   Function *getCalledFunction() const;
67
68   /// setCalledFunction - Set the callee to the specified value...
69   ///
70   void setCalledFunction(Value *V) {
71     assert(I && "Not a call or invoke instruction!");
72     I->setOperand(0, V);
73   }
74
75   /// arg_iterator - The type of iterator to use when looping over actual
76   /// arguments at this call site...
77   typedef User::op_iterator arg_iterator;
78
79   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
80   /// list for a call site.
81   ///
82   arg_iterator arg_begin() const {
83     assert(I && "Not a call or invoke instruction!");
84     if (I->getOpcode() == Instruction::Call)
85       return I->op_begin()+1; // Skip Function
86     else
87       return I->op_begin()+3; // Skip Function, BB, BB
88   }
89   arg_iterator arg_end() const { return I->op_end(); }
90   unsigned arg_size() const { return arg_end() - arg_begin(); }
91
92   bool operator<(const CallSite &CS) const {
93     return getInstruction() < CS.getInstruction();
94   }
95 };
96
97 #endif