1 //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
3 // This family of functions perform manipulations on basic blocks, and
4 // instructions contained within basic blocks.
6 //===----------------------------------------------------------------------===//
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"
15 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
16 // with a value, then remove and delete the original instruction.
18 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
19 BasicBlock::iterator &BI, Value *V) {
21 // Replaces all of the uses of the instruction with uses of the value
22 I.replaceAllUsesWith(V);
24 std::string OldName = I.getName();
26 // Delete the unnecessary instruction now...
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());
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.
39 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
40 BasicBlock::iterator &BI, Instruction *I) {
41 assert(I->getParent() == 0 &&
42 "ReplaceInstWithInst: Instruction already inserted into basic block!");
44 // Insert the new instruction into the basic block...
45 BasicBlock::iterator New = BIL.insert(BI, I);
47 // Replace all uses of the old instruction, and delete it.
48 ReplaceInstWithValue(BIL, BI, I);
50 // Move BI back to point to the newly inserted instruction
54 // ReplaceInstWithInst - Replace the instruction specified by From with the
55 // instruction specified by To.
57 void ReplaceInstWithInst(Instruction *From, Instruction *To) {
58 BasicBlock::iterator BI(From);
59 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
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.
69 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
70 assert(SuccNum < TI->getNumSuccessors() &&
71 "Trying to remove a nonexistant successor!");
73 // If our old successor block contains any PHI nodes, remove the entry in the
74 // PHI nodes that comes from this branch...
76 BasicBlock *BB = TI->getParent();
77 TI->getSuccessor(SuccNum)->removePredecessor(BB);
79 TerminatorInst *NewTI = 0;
80 switch (TI->getOpcode()) {
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...
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());
92 // Create the return...
93 NewTI = new ReturnInst(RetVal);
97 case Instruction::Invoke: // Should convert to call
98 case Instruction::Switch: // Should remove entry
100 case Instruction::Ret: // Cannot happen, has no successors!
101 assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
105 if (NewTI) // If it's a different instruction, replace.
106 ReplaceInstWithInst(TI, NewTI);