This file is no longer used.
[oota-llvm.git] / include / llvm / iTerminators.h
index 312519eb45f6b27f36f5c7254afd143c4a1b3a6e..f6e1c362cfdc291dbf993f2dbc09511f7021133a 100644 (file)
@@ -1,4 +1,11 @@
 //===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file contains the declarations for all the subclasses of the Instruction
 // class which represent "terminator" instructions.  Terminator instructions are
 #ifndef LLVM_ITERMINATORS_H
 #define LLVM_ITERMINATORS_H
 
-#include <assert.h>
-
 #include "llvm/InstrTypes.h"
 
+namespace llvm {
+
 //===---------------------------------------------------------------------------
-// ReturnInst - Return a value (possibly void), from a method.  Execution does
-//              not continue in this method any longer.
-//
+/// ReturnInst - Return a value (possibly void), from a function.  Execution
+/// does not continue in this function any longer.
+///
 class ReturnInst : public TerminatorInst {
   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
     if (RI.Operands.size()) {
@@ -26,6 +33,13 @@ class ReturnInst : public TerminatorInst {
     }
   }
 public:
+  // ReturnInst constructors:
+  // ReturnInst()                  - 'ret void' instruction
+  // ReturnInst(Value* X)          - 'ret X'    instruction
+  // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
+  // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
+  // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
+  // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
   ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
     : TerminatorInst(Instruction::Ret, InsertBefore) {
     if (RetVal) {
@@ -33,6 +47,13 @@ public:
       Operands.push_back(Use(RetVal, this));
     }
   }
+  ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
+    : TerminatorInst(Instruction::Ret, InsertAtEnd) {
+    if (RetVal) {
+      Operands.reserve(1);
+      Operands.push_back(Use(RetVal, this));
+    }
+  }
 
   virtual Instruction *clone() const { return new ReturnInst(*this); }
 
@@ -48,9 +69,7 @@ public:
     abort();
     return 0;
   }
-  virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
-    assert(0 && "ReturnInst has no successors!");
-  }
+  virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
   virtual unsigned getNumSuccessors() const { return 0; }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -63,17 +82,25 @@ public:
   }
 };
 
-
 //===---------------------------------------------------------------------------
-// BranchInst - Conditional or Unconditional Branch instruction.
-//
+/// BranchInst - Conditional or Unconditional Branch instruction.
+///
 class BranchInst : public TerminatorInst {
   BranchInst(const BranchInst &BI);
 public:
-  // If cond = null, then is an unconditional br...
-  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond = 0,
-             Instruction *InsertBefore = 0);
+  // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
+  // BranchInst(BB *B)                           - 'br B'
+  // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
+  // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
+  // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
+  // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
+  // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
+  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
+             Instruction *InsertBefore = 0);
+  BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
+  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
+             BasicBlock *InsertAtEnd);
 
   virtual Instruction *clone() const { return new BranchInst(*this); }
 
@@ -81,7 +108,7 @@ public:
   inline bool isConditional()   const { return Operands.size() == 3; }
 
   inline Value *getCondition() const {
-    return isUnconditional() ? 0 : (Value*)Operands[2].get();
+    return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
   }
 
   void setCondition(Value *V) {
@@ -94,7 +121,7 @@ public:
   //
   void setUnconditionalDest(BasicBlock *Dest) {
     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
-    Operands[0] = (Value*)Dest;
+    Operands[0] = reinterpret_cast<Value*>(Dest);
   }
 
   virtual const BasicBlock *getSuccessor(unsigned i) const {
@@ -103,12 +130,13 @@ public:
                       cast<BasicBlock>(Operands[1].get());
   }
   inline BasicBlock *getSuccessor(unsigned idx) {
-    return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
+    const BranchInst *BI = this;
+    return const_cast<BasicBlock*>(BI->getSuccessor(idx));
   }
 
   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
-    Operands[idx] = (Value*)NewSucc;
+    Operands[idx] = reinterpret_cast<Value*>(NewSucc);
   }
 
   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
@@ -125,8 +153,8 @@ public:
 
 
 //===---------------------------------------------------------------------------
-// SwitchInst - Multiway switch
-//
+/// SwitchInst - Multiway switch
+///
 class SwitchInst : public TerminatorInst {
   // Operand[0]    = Value to switch on
   // Operand[1]    = Default basic block destination
@@ -135,6 +163,7 @@ class SwitchInst : public TerminatorInst {
   SwitchInst(const SwitchInst &RI);
 public:
   SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
+  SwitchInst(Value *Value, BasicBlock *Default, BasicBlock  *InsertAtEnd);
 
   virtual Instruction *clone() const { return new SwitchInst(*this); }
 
@@ -149,7 +178,45 @@ public:
     return cast<BasicBlock>(Operands[1].get());
   }
 
-  void dest_push_back(Constant *OnVal, BasicBlock *Dest);
+  /// getNumCases - return the number of 'cases' in this switch instruction.
+  /// Note that case #0 is always the default case.
+  unsigned getNumCases() const {
+    return Operands.size()/2;
+  }
+
+  /// getCaseValue - Return the specified case value.  Note that case #0, the
+  /// default destination, does not have a case value.
+  Constant *getCaseValue(unsigned i) {
+    assert(i && i < getNumCases() && "Illegal case value to get!");
+    return getSuccessorValue(i);
+  }
+
+  /// getCaseValue - Return the specified case value.  Note that case #0, the
+  /// default destination, does not have a case value.
+  const Constant *getCaseValue(unsigned i) const {
+    assert(i && i < getNumCases() && "Illegal case value to get!");
+    return getSuccessorValue(i);
+  }
+
+  /// findCaseValue - Search all of the case values for the specified constant.
+  /// If it is explicitly handled, return the case number of it, otherwise
+  /// return 0 to indicate that it is handled by the default handler.
+  unsigned findCaseValue(const Constant *C) const {
+    for (unsigned i = 1, e = getNumCases(); i != e; ++i)
+      if (getCaseValue(i) == C)
+        return i;
+    return 0;
+  }
+
+  /// addCase - Add an entry to the switch instruction...
+  ///
+  void addCase(Constant *OnVal, BasicBlock *Dest);
+
+  /// removeCase - This method removes the specified successor from the switch
+  /// instruction.  Note that this cannot be used to remove the default
+  /// destination (successor #0).
+  ///
+  void removeCase(unsigned idx);
 
   virtual const BasicBlock *getSuccessor(unsigned idx) const {
     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
@@ -162,7 +229,7 @@ public:
 
   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
-    Operands[idx*2+1] = (Value*)NewSucc;
+    Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
   }
 
   // getSuccessorValue - Return the value associated with the specified
@@ -187,32 +254,32 @@ public:
   }
 };
 
-
 //===---------------------------------------------------------------------------
-// InvokeInst - Invoke instruction
-//
+/// InvokeInst - Invoke instruction
+///
 class InvokeInst : public TerminatorInst {
   InvokeInst(const InvokeInst &BI);
 public:
-  InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
+  InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
             const std::vector<Value*> &Params, const std::string &Name = "",
              Instruction *InsertBefore = 0);
+  InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
+            const std::vector<Value*> &Params, const std::string &Name,
+             BasicBlock *InsertAtEnd);
 
   virtual Instruction *clone() const { return new InvokeInst(*this); }
 
   bool mayWriteToMemory() const { return true; }
 
-  // getCalledFunction - Return the function called, or null if this is an
-  // indirect function invocation...
-  //
-  inline const Function *getCalledFunction() const {
-    return dyn_cast<Function>(Operands[0].get());
-  }
-  inline Function *getCalledFunction() {
-    return dyn_cast<Function>(Operands[0].get());
-  }
+  /// getCalledFunction - Return the function called, or null if this is an
+  /// indirect function invocation... 
+  ///
+  /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
+  ///
+  const Function *getCalledFunction() const;
+  Function *getCalledFunction();
 
-  // getCalledValue - Get a pointer to a method that is invoked by this inst.
+  // getCalledValue - Get a pointer to a function that is invoked by this inst.
   inline const Value *getCalledValue() const { return Operands[0]; }
   inline       Value *getCalledValue()       { return Operands[0]; }
 
@@ -223,33 +290,33 @@ public:
   inline       BasicBlock *getNormalDest() {
     return cast<BasicBlock>(Operands[1].get());
   }
-  inline const BasicBlock *getExceptionalDest() const {
+  inline const BasicBlock *getUnwindDest() const {
     return cast<BasicBlock>(Operands[2].get());
   }
-  inline       BasicBlock *getExceptionalDest() {
+  inline       BasicBlock *getUnwindDest() {
     return cast<BasicBlock>(Operands[2].get());
   }
 
   inline void setNormalDest(BasicBlock *B){
-    Operands[1] = (Value*)B;
+    Operands[1] = reinterpret_cast<Value*>(B);
   }
 
-  inline void setExceptionalDest(BasicBlock *B){
-    Operands[2] = (Value*)B;
+  inline void setUnwindDest(BasicBlock *B){
+    Operands[2] = reinterpret_cast<Value*>(B);
   }
 
   virtual const BasicBlock *getSuccessor(unsigned i) const {
     assert(i < 2 && "Successor # out of range for invoke!");
-    return i == 0 ? getNormalDest() : getExceptionalDest();
+    return i == 0 ? getNormalDest() : getUnwindDest();
   }
   inline BasicBlock *getSuccessor(unsigned i) {
     assert(i < 2 && "Successor # out of range for invoke!");
-    return i == 0 ? getNormalDest() : getExceptionalDest();
+    return i == 0 ? getNormalDest() : getUnwindDest();
   }
 
   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
     assert(idx < 2 && "Successor # out of range for invoke!");
-    Operands[idx+1] = (Value*)NewSucc;
+    Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
   }
 
   virtual unsigned getNumSuccessors() const { return 2; }
@@ -264,4 +331,39 @@ public:
   }
 };
 
+
+//===---------------------------------------------------------------------------
+/// UnwindInst - Immediately exit the current function, unwinding the stack
+/// until an invoke instruction is found.
+///
+struct UnwindInst : public TerminatorInst {
+  UnwindInst(Instruction *InsertBefore = 0)
+    : TerminatorInst(Instruction::Unwind, InsertBefore) {
+  }
+  UnwindInst(BasicBlock *InsertAtEnd)
+    : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
+  }
+
+  virtual Instruction *clone() const { return new UnwindInst(); }
+
+  virtual const BasicBlock *getSuccessor(unsigned idx) const {
+    assert(0 && "UnwindInst has no successors!");
+    abort();
+    return 0;
+  }
+  virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
+  virtual unsigned getNumSuccessors() const { return 0; }
+
+  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  static inline bool classof(const UnwindInst *) { return true; }
+  static inline bool classof(const Instruction *I) {
+    return I->getOpcode() == Instruction::Unwind;
+  }
+  static inline bool classof(const Value *V) {
+    return isa<Instruction>(V) && classof(cast<Instruction>(V));
+  }
+};
+
+} // End llvm namespace
+
 #endif