summaryrefslogtreecommitdiff
path: root/lib/VMCore/iBranch.cpp
blob: 05f35059a546e2007f571fb007b97a258da49470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//===-- iBranch.cpp - Implement the Branch instruction -----------*- C++ -*--=//
//
// This file implements the 'br' instruction, which can represent either a 
// conditional or unconditional branch.
//
//===----------------------------------------------------------------------===//

#include "llvm/iTerminators.h"
#include "llvm/BasicBlock.h"
#ifndef NDEBUG
#include "llvm/Type.h"       // Only used for assertions...
#include "llvm/Assembly/Writer.h"
#endif

BranchInst::BranchInst(BasicBlock *True, BasicBlock *False, Value *Cond) 
  : TerminatorInst(Instruction::Br), TrueDest(True, this), 
    FalseDest(False, this), Condition(Cond, this) {
  assert(True != 0 && "True branch destination may not be null!!!");

#ifndef NDEBUG
  if (Cond != 0 && Cond->getType() != Type::BoolTy)
    cerr << "Bad Condition: " << Cond << endl;
#endif
  assert((Cond == 0 || Cond->getType() == Type::BoolTy) && 
         "May only branch on boolean predicates!!!!");
}

BranchInst::BranchInst(const BranchInst &BI) 
  : TerminatorInst(Instruction::Br), TrueDest(BI.TrueDest, this), 
    FalseDest(BI.FalseDest, this), Condition(BI.Condition, this) {
}


void BranchInst::dropAllReferences() {
  Condition = 0;
  TrueDest = FalseDest = 0;
}

const Value *BranchInst::getOperand(unsigned i) const {
    return (i == 0) ? (Value*)TrueDest : 
          ((i == 1) ? (Value*)FalseDest : 
          ((i == 2) ? (Value*)Condition : 0));
}

const BasicBlock *BranchInst::getSuccessor(unsigned i) const {
  return (i == 0) ? (const BasicBlock*)TrueDest : 
        ((i == 1) ? (const BasicBlock*)FalseDest : 0);
}

bool BranchInst::setOperand(unsigned i, Value *Val) { 
  switch (i) {
  case 0:
    assert(Val && "Can't change primary direction to 0!");
    assert(Val->getType() == Type::LabelTy);
    TrueDest = (BasicBlock*)Val;
    return true;
  case 1:
    assert(Val == 0 || Val->getType() == Type::LabelTy);
    FalseDest = (BasicBlock*)Val;
    return true;
  case 2:
    Condition = Val;
    assert(!Condition || Condition->getType() == Type::BoolTy && 
           "Condition expr must be a boolean expression!");
    return true;
  } 

  return false;
}