1 //===-- iBranch.cpp - Implement the Branch instruction -----------*- C++ -*--=//
3 // This file implements the 'br' instruction, which can represent either a
4 // conditional or unconditional branch.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/iTerminators.h"
9 #include "llvm/BasicBlock.h"
11 #include "llvm/Type.h" // Only used for assertions...
12 #include "llvm/Assembly/Writer.h"
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!!!");
21 if (Cond != 0 && Cond->getType() != Type::BoolTy)
22 cerr << "Bad Condition: " << Cond << endl;
24 assert((Cond == 0 || Cond->getType() == Type::BoolTy) &&
25 "May only branch on boolean predicates!!!!");
28 BranchInst::BranchInst(const BranchInst &BI)
29 : TerminatorInst(Instruction::Br), TrueDest(BI.TrueDest, this),
30 FalseDest(BI.FalseDest, this), Condition(BI.Condition, this) {
34 void BranchInst::dropAllReferences() {
36 TrueDest = FalseDest = 0;
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));
45 const BasicBlock *BranchInst::getSuccessor(unsigned i) const {
46 return (i == 0) ? (const BasicBlock*)TrueDest :
47 ((i == 1) ? (const BasicBlock*)FalseDest : 0);
50 bool BranchInst::setOperand(unsigned i, Value *Val) {
53 assert(Val && "Can't change primary direction to 0!");
54 assert(Val->getType() == Type::LabelTy);
55 TrueDest = (BasicBlock*)Val;
58 assert(Val == 0 || Val->getType() == Type::LabelTy);
59 FalseDest = (BasicBlock*)Val;
63 assert(!Condition || Condition->getType() == Type::BoolTy &&
64 "Condition expr must be a boolean expression!");