Refactor common initialization code in private init() functions.
[oota-llvm.git] / lib / VMCore / iBranch.cpp
1 //===-- iBranch.cpp - Implement the Branch instruction --------------------===//
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 implements the 'br' instruction, which can represent either a 
11 // conditional or unconditional branch.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/iTerminators.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/Type.h"
18 using namespace llvm;
19
20 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
21 // emit the vtable for the class in this translation unit.
22 void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
23   assert(0 && "ReturnInst has no successors!");
24 }
25
26 // Likewise for UnwindInst
27 void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
28   assert(0 && "UnwindInst has no successors!");
29 }
30
31 void BranchInst::init(BasicBlock *IfTrue)
32 {
33   assert(IfTrue != 0 && "Branch destination may not be null!");
34   Operands.reserve(1);
35   Operands.push_back(Use(IfTrue, this));
36 }
37
38 void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
39 {
40   assert(IfTrue && IfFalse && Cond &&
41          "Branch destinations and condition may not be null!");
42   assert(Cond && Cond->getType() == Type::BoolTy && 
43          "May only branch on boolean predicates!");
44   Operands.reserve(3);
45   Operands.push_back(Use(IfTrue, this));
46   Operands.push_back(Use(IfFalse, this));
47   Operands.push_back(Use(Cond, this));
48 }
49
50 BranchInst::BranchInst(BasicBlock *True, BasicBlock *False, Value *Cond,
51                        Instruction *InsertBefore) 
52   : TerminatorInst(Instruction::Br, InsertBefore) {
53   init(True, False, Cond);
54 }
55
56 BranchInst::BranchInst(BasicBlock *True, BasicBlock *False, Value *Cond,
57                        BasicBlock *InsertAtEnd) 
58   : TerminatorInst(Instruction::Br, InsertAtEnd) {
59   init(True, False, Cond);
60 }
61
62 BranchInst::BranchInst(BasicBlock *True, Instruction *InsertBefore) 
63   : TerminatorInst(Instruction::Br, InsertBefore) {
64   init(True);
65 }
66
67 BranchInst::BranchInst(BasicBlock *True, BasicBlock *InsertAtEnd) 
68   : TerminatorInst(Instruction::Br, InsertAtEnd) {
69   init(True);
70 }
71
72 BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
73   Operands.reserve(BI.Operands.size());
74   Operands.push_back(Use(BI.Operands[0], this));
75   if (BI.Operands.size() != 1) {
76     assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
77     Operands.push_back(Use(BI.Operands[1], this));
78     Operands.push_back(Use(BI.Operands[2], this));
79   }
80 }