blob: 5fb5469332538e1f2a89e2f6d469af43c6f56691 [file] [log] [blame]
Chris Lattner4d1e46e2002-05-07 18:07:59 +00001//===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner4d1e46e2002-05-07 18:07:59 +00009//
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"
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000017#include "llvm/iTerminators.h"
18#include "llvm/Constant.h"
19#include "llvm/Type.h"
Chris Lattner4d1e46e2002-05-07 18:07:59 +000020#include <algorithm>
21
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattner4d1e46e2002-05-07 18:07:59 +000024// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
25// with a value, then remove and delete the original instruction.
26//
27void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
28 BasicBlock::iterator &BI, Value *V) {
Chris Lattner18961502002-06-25 16:12:52 +000029 Instruction &I = *BI;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000030 // Replaces all of the uses of the instruction with uses of the value
Chris Lattner18961502002-06-25 16:12:52 +000031 I.replaceAllUsesWith(V);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000032
Chris Lattner18961502002-06-25 16:12:52 +000033 std::string OldName = I.getName();
34
Misha Brukman5560c9d2003-08-18 14:43:39 +000035 // Delete the unnecessary instruction now...
Chris Lattner18961502002-06-25 16:12:52 +000036 BI = BIL.erase(BI);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000037
Misha Brukmana3bbcb52002-10-29 23:06:16 +000038 // Make sure to propagate a name if there is one already...
Chris Lattner18961502002-06-25 16:12:52 +000039 if (OldName.size() && !V->hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000040 V->setName(OldName, &BIL.getParent()->getSymbolTable());
Chris Lattner4d1e46e2002-05-07 18:07:59 +000041}
42
43
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.
47//
48void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
49 BasicBlock::iterator &BI, Instruction *I) {
50 assert(I->getParent() == 0 &&
51 "ReplaceInstWithInst: Instruction already inserted into basic block!");
52
53 // Insert the new instruction into the basic block...
Chris Lattner18961502002-06-25 16:12:52 +000054 BasicBlock::iterator New = BIL.insert(BI, I);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000055
56 // Replace all uses of the old instruction, and delete it.
57 ReplaceInstWithValue(BIL, BI, I);
58
59 // Move BI back to point to the newly inserted instruction
Chris Lattner18961502002-06-25 16:12:52 +000060 BI = New;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000061}
62
63// ReplaceInstWithInst - Replace the instruction specified by From with the
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000064// instruction specified by To.
Chris Lattner4d1e46e2002-05-07 18:07:59 +000065//
66void ReplaceInstWithInst(Instruction *From, Instruction *To) {
Chris Lattner18961502002-06-25 16:12:52 +000067 BasicBlock::iterator BI(From);
68 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000069}
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000070
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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000076// surprising change in program behavior if it is not expected.
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000077//
78void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
79 assert(SuccNum < TI->getNumSuccessors() &&
80 "Trying to remove a nonexistant successor!");
81
82 // If our old successor block contains any PHI nodes, remove the entry in the
83 // PHI nodes that comes from this branch...
84 //
85 BasicBlock *BB = TI->getParent();
86 TI->getSuccessor(SuccNum)->removePredecessor(BB);
87
88 TerminatorInst *NewTI = 0;
89 switch (TI->getOpcode()) {
90 case Instruction::Br:
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...
95 Value *RetVal = 0;
96
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());
100
101 // Create the return...
102 NewTI = new ReturnInst(RetVal);
103 }
104 break;
105
106 case Instruction::Invoke: // Should convert to call
107 case Instruction::Switch: // Should remove entry
108 default:
109 case Instruction::Ret: // Cannot happen, has no successors!
110 assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
111 abort();
112 }
113
114 if (NewTI) // If it's a different instruction, replace.
115 ReplaceInstWithInst(TI, NewTI);
116}
Brian Gaeked0fde302003-11-11 22:41:34 +0000117
118} // End llvm namespace