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