Remove support forNOT instruction
[oota-llvm.git] / lib / VMCore / BasicBlock.cpp
1 //===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
2 //
3 // This file implements the BasicBlock class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/BasicBlock.h"
8 #include "llvm/iTerminators.h"
9 #include "llvm/Type.h"
10 #include "llvm/Support/CFG.h"
11 #include "llvm/Constant.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/SymbolTable.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include <algorithm>
16
17 // DummyInst - An instance of this class is used to mark the end of the
18 // instruction list.  This is not a real instruction.
19 //
20 struct DummyInst : public Instruction {
21   DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {}
22
23   virtual Instruction *clone() const {
24     assert(0 && "Cannot clone EOL");abort();
25     return 0;
26   }
27   virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
28
29   // Methods for support type inquiry through isa, cast, and dyn_cast...
30   static inline bool classof(const DummyInst *) { return true; }
31   static inline bool classof(const Instruction *I) {
32     return I->getOpcode() == NumOtherOps;
33   }
34   static inline bool classof(const Value *V) {
35     return isa<Instruction>(V) && classof(cast<Instruction>(V));
36   }
37 };
38
39 Instruction *ilist_traits<Instruction>::createNode() {
40   return new DummyInst();
41 }
42 iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
43   return BB->getInstList();
44 }
45
46 // Explicit instantiation of SymbolTableListTraits since some of the methods
47 // are not in the public header file...
48 template SymbolTableListTraits<Instruction, BasicBlock, Function>;
49
50
51 BasicBlock::BasicBlock(const std::string &name, Function *Parent)
52   : Value(Type::LabelTy, Value::BasicBlockVal, name) {
53   // Initialize the instlist...
54   InstList.setItemParent(this);
55
56   if (Parent)
57     Parent->getBasicBlockList().push_back(this);
58 }
59
60 BasicBlock::~BasicBlock() {
61   dropAllReferences();
62   InstList.clear();
63 }
64
65 // Specialize setName to take care of symbol table majik
66 void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
67   Function *P;
68   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
69          "Invalid symtab argument!");
70   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
71   Value::setName(name);
72   if (P && hasName()) P->getSymbolTable()->insert(this);
73 }
74
75 TerminatorInst *BasicBlock::getTerminator() {
76   if (InstList.empty()) return 0;
77   return dyn_cast<TerminatorInst>(&InstList.back());
78 }
79
80 const TerminatorInst *const BasicBlock::getTerminator() const {
81   if (InstList.empty()) return 0;
82   return dyn_cast<TerminatorInst>(&InstList.back());
83 }
84
85 void BasicBlock::dropAllReferences() {
86   for(iterator I = begin(), E = end(); I != E; ++I)
87     I->dropAllReferences();
88 }
89
90 // hasConstantReferences() - This predicate is true if there is a 
91 // reference to this basic block in the constant pool for this method.  For
92 // example, if a block is reached through a switch table, that table resides
93 // in the constant pool, and the basic block is reference from it.
94 //
95 bool BasicBlock::hasConstantReferences() const {
96   for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
97     if (::isa<Constant>((Value*)*I))
98       return true;
99
100   return false;
101 }
102
103 // removePredecessor - This method is used to notify a BasicBlock that the
104 // specified Predecessor of the block is no longer able to reach it.  This is
105 // actually not used to update the Predecessor list, but is actually used to 
106 // update the PHI nodes that reside in the block.  Note that this should be
107 // called while the predecessor still refers to this block.
108 //
109 void BasicBlock::removePredecessor(BasicBlock *Pred) {
110   assert(find(pred_begin(this), pred_end(this), Pred) != pred_end(this) &&
111          "removePredecessor: BB is not a predecessor!");
112   if (!isa<PHINode>(front())) return;   // Quick exit.
113
114   pred_iterator PI(pred_begin(this)), EI(pred_end(this));
115   unsigned max_idx;
116
117   // Loop over the rest of the predecessors until we run out, or until we find
118   // out that there are more than 2 predecessors.
119   for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
120
121   // If there are exactly two predecessors, then we want to nuke the PHI nodes
122   // altogether.  We cannot do this, however if this in this case however:
123   //
124   //  Loop:
125   //    %x = phi [X, Loop]
126   //    %x2 = add %x, 1         ;; This would become %x2 = add %x2, 1
127   //    br Loop                 ;; %x2 does not dominate all uses
128   //
129   // This is because the PHI node input is actually taken from the predecessor
130   // basic block.  The only case this can happen is with a self loop, so we 
131   // check for this case explicitly now.
132   // 
133   assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
134   if (max_idx == 2) {
135     PI = pred_begin(this);
136     BasicBlock *Other = *PI == Pred ? *++PI : *PI;
137
138     // Disable PHI elimination!
139     if (this == Other) max_idx = 3;
140   }
141
142   if (max_idx <= 2) {                // <= Two predecessors BEFORE I remove one?
143     // Yup, loop through and nuke the PHI nodes
144     while (PHINode *PN = dyn_cast<PHINode>(&front())) {
145       PN->removeIncomingValue(Pred); // Remove the predecessor first...
146       
147       assert(PN->getNumIncomingValues() == max_idx-1 && 
148              "PHI node shouldn't have this many values!!!");
149
150       // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
151       if (max_idx == 2)
152         PN->replaceAllUsesWith(PN->getOperand(0));
153       else // Otherwise there are no incoming values/edges, replace with dummy
154         PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
155       getInstList().pop_front();    // Remove the PHI node
156     }
157   } else {
158     // Okay, now we know that we need to remove predecessor #pred_idx from all
159     // PHI nodes.  Iterate over each PHI node fixing them up
160     for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(&*II); ++II)
161       PN->removeIncomingValue(Pred);
162   }
163 }
164
165
166 // splitBasicBlock - This splits a basic block into two at the specified
167 // instruction.  Note that all instructions BEFORE the specified iterator stay
168 // as part of the original basic block, an unconditional branch is added to 
169 // the new BB, and the rest of the instructions in the BB are moved to the new
170 // BB, including the old terminator.  This invalidates the iterator.
171 //
172 // Note that this only works on well formed basic blocks (must have a 
173 // terminator), and 'I' must not be the end of instruction list (which would
174 // cause a degenerate basic block to be formed, having a terminator inside of
175 // the basic block). 
176 //
177 BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
178   assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
179   assert(I != InstList.end() && 
180          "Trying to get me to create degenerate basic block!");
181
182   BasicBlock *New = new BasicBlock("", getParent());
183
184   // Go from the end of the basic block through to the iterator pointer, moving
185   // to the new basic block...
186   Instruction *Inst = 0;
187   do {
188     iterator EndIt = end();
189     Inst = InstList.remove(--EndIt);                  // Remove from end
190     New->InstList.push_front(Inst);                   // Add to front
191   } while (Inst != &*I);   // Loop until we move the specified instruction.
192
193   // Add a branch instruction to the newly formed basic block.
194   InstList.push_back(new BranchInst(New));
195
196   // Now we must loop through all of the successors of the New block (which
197   // _were_ the successors of the 'this' block), and update any PHI nodes in
198   // successors.  If there were PHI nodes in the successors, then they need to
199   // know that incoming branches will be from New, not from Old.
200   //
201   for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
202        I != E; ++I) {
203     // Loop over any phi nodes in the basic block, updating the BB field of
204     // incoming values...
205     BasicBlock *Successor = *I;
206     for (BasicBlock::iterator II = Successor->begin();
207          PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
208       int IDX = PN->getBasicBlockIndex(this);
209       while (IDX != -1) {
210         PN->setIncomingBlock((unsigned)IDX, New);
211         IDX = PN->getBasicBlockIndex(this);
212       }
213     }
214   }
215   return New;
216 }