1 //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
13 //===----------------------------------------------------------------------===//
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"
24 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
25 // with a value, then remove and delete the original instruction.
27 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
28 BasicBlock::iterator &BI, Value *V) {
30 // Replaces all of the uses of the instruction with uses of the value
31 I.replaceAllUsesWith(V);
33 std::string OldName = I.getName();
35 // Delete the unnecessary instruction now...
38 // Make sure to propagate a name if there is one already...
39 if (OldName.size() && !V->hasName())
40 V->setName(OldName, &BIL.getParent()->getSymbolTable());
44 // ReplaceInstWithInst - Replace the instruction specified by BI with the
45 // instruction specified by I. The original instruction is deleted and BI is
46 // updated to point to the new instruction.
48 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
49 BasicBlock::iterator &BI, Instruction *I) {
50 assert(I->getParent() == 0 &&
51 "ReplaceInstWithInst: Instruction already inserted into basic block!");
53 // Insert the new instruction into the basic block...
54 BasicBlock::iterator New = BIL.insert(BI, I);
56 // Replace all uses of the old instruction, and delete it.
57 ReplaceInstWithValue(BIL, BI, I);
59 // Move BI back to point to the newly inserted instruction
63 // ReplaceInstWithInst - Replace the instruction specified by From with the
64 // instruction specified by To.
66 void ReplaceInstWithInst(Instruction *From, Instruction *To) {
67 BasicBlock::iterator BI(From);
68 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
71 // RemoveSuccessor - Change the specified terminator instruction such that its
72 // successor #SuccNum no longer exists. Because this reduces the outgoing
73 // degree of the current basic block, the actual terminator instruction itself
74 // may have to be changed. In the case where the last successor of the block is
75 // deleted, a return instruction is inserted in its place which can cause a
76 // surprising change in program behavior if it is not expected.
78 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
79 assert(SuccNum < TI->getNumSuccessors() &&
80 "Trying to remove a nonexistant successor!");
82 // If our old successor block contains any PHI nodes, remove the entry in the
83 // PHI nodes that comes from this branch...
85 BasicBlock *BB = TI->getParent();
86 TI->getSuccessor(SuccNum)->removePredecessor(BB);
88 TerminatorInst *NewTI = 0;
89 switch (TI->getOpcode()) {
91 // If this is a conditional branch... convert to unconditional branch.
92 if (TI->getNumSuccessors() == 2) {
93 cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
94 } else { // Otherwise convert to a return instruction...
97 // Create a value to return... if the function doesn't return null...
98 if (BB->getParent()->getReturnType() != Type::VoidTy)
99 RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
101 // Create the return...
102 NewTI = new ReturnInst(RetVal);
106 case Instruction::Invoke: // Should convert to call
107 case Instruction::Switch: // Should remove entry
109 case Instruction::Ret: // Cannot happen, has no successors!
110 assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
114 if (NewTI) // If it's a different instruction, replace.
115 ReplaceInstWithInst(TI, NewTI);
118 } // End llvm namespace