blob: d5f7f1f03eb69c86921f40246ab6b8d968bff482 [file] [log] [blame]
Chris Lattner4d1e46e2002-05-07 18:07:59 +00001//===-- 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"
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000010#include "llvm/iTerminators.h"
11#include "llvm/Constant.h"
12#include "llvm/Type.h"
Chris Lattner4d1e46e2002-05-07 18:07:59 +000013#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) {
Chris Lattner18961502002-06-25 16:12:52 +000020 Instruction &I = *BI;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000021 // Replaces all of the uses of the instruction with uses of the value
Chris Lattner18961502002-06-25 16:12:52 +000022 I.replaceAllUsesWith(V);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000023
Chris Lattner18961502002-06-25 16:12:52 +000024 std::string OldName = I.getName();
25
Misha Brukman5560c9d2003-08-18 14:43:39 +000026 // Delete the unnecessary instruction now...
Chris Lattner18961502002-06-25 16:12:52 +000027 BI = BIL.erase(BI);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000028
Misha Brukmana3bbcb52002-10-29 23:06:16 +000029 // Make sure to propagate a name if there is one already...
Chris Lattner18961502002-06-25 16:12:52 +000030 if (OldName.size() && !V->hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000031 V->setName(OldName, &BIL.getParent()->getSymbolTable());
Chris Lattner4d1e46e2002-05-07 18:07:59 +000032}
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...
Chris Lattner18961502002-06-25 16:12:52 +000045 BasicBlock::iterator New = BIL.insert(BI, I);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000046
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
Chris Lattner18961502002-06-25 16:12:52 +000051 BI = New;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000052}
53
54// ReplaceInstWithInst - Replace the instruction specified by From with the
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000055// instruction specified by To.
Chris Lattner4d1e46e2002-05-07 18:07:59 +000056//
57void ReplaceInstWithInst(Instruction *From, Instruction *To) {
Chris Lattner18961502002-06-25 16:12:52 +000058 BasicBlock::iterator BI(From);
59 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000060}
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000061
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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000067// surprising change in program behavior if it is not expected.
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000068//
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}