BasicBlockUtils.cpp revision 6e6026b46569b01f8f6d4dcdb6c899c3a9c76b3e
1//===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
2//
3// This family of functions perform manipulations on basic blocks, and
4// instructions contained within basic blocks.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Transforms/Utils/BasicBlockUtils.h"
9#include "llvm/Function.h"
10#include "llvm/iTerminators.h"
11#include "llvm/Constant.h"
12#include "llvm/Type.h"
13#include <algorithm>
14
15// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
16// with a value, then remove and delete the original instruction.
17//
18void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
19                          BasicBlock::iterator &BI, Value *V) {
20  Instruction &I = *BI;
21  // Replaces all of the uses of the instruction with uses of the value
22  I.replaceAllUsesWith(V);
23
24  std::string OldName = I.getName();
25
26  // Delete the unneccesary instruction now...
27  BI = BIL.erase(BI);
28
29  // Make sure to propagate a name if there is one already...
30  if (OldName.size() && !V->hasName())
31    V->setName(OldName, &BIL.getParent()->getSymbolTable());
32}
33
34
35// ReplaceInstWithInst - Replace the instruction specified by BI with the
36// instruction specified by I.  The original instruction is deleted and BI is
37// updated to point to the new instruction.
38//
39void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
40                         BasicBlock::iterator &BI, Instruction *I) {
41  assert(I->getParent() == 0 &&
42         "ReplaceInstWithInst: Instruction already inserted into basic block!");
43
44  // Insert the new instruction into the basic block...
45  BasicBlock::iterator New = BIL.insert(BI, I);
46
47  // Replace all uses of the old instruction, and delete it.
48  ReplaceInstWithValue(BIL, BI, I);
49
50  // Move BI back to point to the newly inserted instruction
51  BI = New;
52}
53
54// ReplaceInstWithInst - Replace the instruction specified by From with the
55// instruction specified by To.
56//
57void ReplaceInstWithInst(Instruction *From, Instruction *To) {
58  BasicBlock::iterator BI(From);
59  ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
60}
61
62// RemoveSuccessor - Change the specified terminator instruction such that its
63// successor #SuccNum no longer exists.  Because this reduces the outgoing
64// degree of the current basic block, the actual terminator instruction itself
65// may have to be changed.  In the case where the last successor of the block is
66// deleted, a return instruction is inserted in its place which can cause a
67// suprising change in program behavior if it is not expected.
68//
69void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
70  assert(SuccNum < TI->getNumSuccessors() &&
71         "Trying to remove a nonexistant successor!");
72
73  // If our old successor block contains any PHI nodes, remove the entry in the
74  // PHI nodes that comes from this branch...
75  //
76  BasicBlock *BB = TI->getParent();
77  TI->getSuccessor(SuccNum)->removePredecessor(BB);
78
79  TerminatorInst *NewTI = 0;
80  switch (TI->getOpcode()) {
81  case Instruction::Br:
82    // If this is a conditional branch... convert to unconditional branch.
83    if (TI->getNumSuccessors() == 2) {
84      cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
85    } else {                    // Otherwise convert to a return instruction...
86      Value *RetVal = 0;
87
88      // Create a value to return... if the function doesn't return null...
89      if (BB->getParent()->getReturnType() != Type::VoidTy)
90        RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
91
92      // Create the return...
93      NewTI = new ReturnInst(RetVal);
94    }
95    break;
96
97  case Instruction::Invoke:    // Should convert to call
98  case Instruction::Switch:    // Should remove entry
99  default:
100  case Instruction::Ret:       // Cannot happen, has no successors!
101    assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
102    abort();
103  }
104
105  if (NewTI)   // If it's a different instruction, replace.
106    ReplaceInstWithInst(TI, NewTI);
107}
108