05f35059a546e2007f571fb007b97a258da49470
[oota-llvm.git] / lib / VMCore / iBranch.cpp
1 //===-- iBranch.cpp - Implement the Branch instruction -----------*- C++ -*--=//
2 //
3 // This file implements the 'br' instruction, which can represent either a 
4 // conditional or unconditional branch.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/iTerminators.h"
9 #include "llvm/BasicBlock.h"
10 #ifndef NDEBUG
11 #include "llvm/Type.h"       // Only used for assertions...
12 #include "llvm/Assembly/Writer.h"
13 #endif
14
15 BranchInst::BranchInst(BasicBlock *True, BasicBlock *False, Value *Cond) 
16   : TerminatorInst(Instruction::Br), TrueDest(True, this), 
17     FalseDest(False, this), Condition(Cond, this) {
18   assert(True != 0 && "True branch destination may not be null!!!");
19
20 #ifndef NDEBUG
21   if (Cond != 0 && Cond->getType() != Type::BoolTy)
22     cerr << "Bad Condition: " << Cond << endl;
23 #endif
24   assert((Cond == 0 || Cond->getType() == Type::BoolTy) && 
25          "May only branch on boolean predicates!!!!");
26 }
27
28 BranchInst::BranchInst(const BranchInst &BI) 
29   : TerminatorInst(Instruction::Br), TrueDest(BI.TrueDest, this), 
30     FalseDest(BI.FalseDest, this), Condition(BI.Condition, this) {
31 }
32
33
34 void BranchInst::dropAllReferences() {
35   Condition = 0;
36   TrueDest = FalseDest = 0;
37 }
38
39 const Value *BranchInst::getOperand(unsigned i) const {
40     return (i == 0) ? (Value*)TrueDest : 
41           ((i == 1) ? (Value*)FalseDest : 
42           ((i == 2) ? (Value*)Condition : 0));
43 }
44
45 const BasicBlock *BranchInst::getSuccessor(unsigned i) const {
46   return (i == 0) ? (const BasicBlock*)TrueDest : 
47         ((i == 1) ? (const BasicBlock*)FalseDest : 0);
48 }
49
50 bool BranchInst::setOperand(unsigned i, Value *Val) { 
51   switch (i) {
52   case 0:
53     assert(Val && "Can't change primary direction to 0!");
54     assert(Val->getType() == Type::LabelTy);
55     TrueDest = (BasicBlock*)Val;
56     return true;
57   case 1:
58     assert(Val == 0 || Val->getType() == Type::LabelTy);
59     FalseDest = (BasicBlock*)Val;
60     return true;
61   case 2:
62     Condition = Val;
63     assert(!Condition || Condition->getType() == Type::BoolTy && 
64            "Condition expr must be a boolean expression!");
65     return true;
66   } 
67
68   return false;
69 }