blob: f9bcd39e41c55f4dff3bdb1a38cc2061b4884307 [file] [log] [blame]
Chris Lattner4d1e46e2002-05-07 18:07:59 +00001//===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000018#include "llvm/Constant.h"
19#include "llvm/Type.h"
Chris Lattner4d1e46e2002-05-07 18:07:59 +000020#include <algorithm>
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner0f67dd62005-04-21 16:04:49 +000023/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
24/// with a value, then remove and delete the original instruction.
25///
Chris Lattnerf7703df2004-01-09 06:12:26 +000026void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
27 BasicBlock::iterator &BI, Value *V) {
Chris Lattner18961502002-06-25 16:12:52 +000028 Instruction &I = *BI;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000029 // Replaces all of the uses of the instruction with uses of the value
Chris Lattner18961502002-06-25 16:12:52 +000030 I.replaceAllUsesWith(V);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000031
Chris Lattner86cc4232007-02-11 01:37:51 +000032 // Make sure to propagate a name if there is one already.
33 if (I.hasName() && !V->hasName())
34 V->takeName(&I);
Misha Brukmanfd939082005-04-21 23:48:37 +000035
Misha Brukman5560c9d2003-08-18 14:43:39 +000036 // Delete the unnecessary instruction now...
Chris Lattner18961502002-06-25 16:12:52 +000037 BI = BIL.erase(BI);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000038}
39
40
Chris Lattner0f67dd62005-04-21 16:04:49 +000041/// ReplaceInstWithInst - Replace the instruction specified by BI with the
42/// instruction specified by I. The original instruction is deleted and BI is
43/// updated to point to the new instruction.
44///
Chris Lattnerf7703df2004-01-09 06:12:26 +000045void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
46 BasicBlock::iterator &BI, Instruction *I) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000047 assert(I->getParent() == 0 &&
48 "ReplaceInstWithInst: Instruction already inserted into basic block!");
49
50 // Insert the new instruction into the basic block...
Chris Lattner18961502002-06-25 16:12:52 +000051 BasicBlock::iterator New = BIL.insert(BI, I);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000052
53 // Replace all uses of the old instruction, and delete it.
54 ReplaceInstWithValue(BIL, BI, I);
55
56 // Move BI back to point to the newly inserted instruction
Chris Lattner18961502002-06-25 16:12:52 +000057 BI = New;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000058}
59
Chris Lattner0f67dd62005-04-21 16:04:49 +000060/// ReplaceInstWithInst - Replace the instruction specified by From with the
61/// instruction specified by To.
62///
Chris Lattnerf7703df2004-01-09 06:12:26 +000063void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
Chris Lattner18961502002-06-25 16:12:52 +000064 BasicBlock::iterator BI(From);
65 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000066}
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000067
Chris Lattner0f67dd62005-04-21 16:04:49 +000068/// RemoveSuccessor - Change the specified terminator instruction such that its
Reid Spencerbc2eba12006-05-19 19:09:46 +000069/// successor SuccNum no longer exists. Because this reduces the outgoing
Chris Lattner0f67dd62005-04-21 16:04:49 +000070/// degree of the current basic block, the actual terminator instruction itself
Reid Spencerbc2eba12006-05-19 19:09:46 +000071/// may have to be changed. In the case where the last successor of the block
72/// is deleted, a return instruction is inserted in its place which can cause a
Chris Lattner0f67dd62005-04-21 16:04:49 +000073/// surprising change in program behavior if it is not expected.
74///
Chris Lattnerf7703df2004-01-09 06:12:26 +000075void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000076 assert(SuccNum < TI->getNumSuccessors() &&
77 "Trying to remove a nonexistant successor!");
78
79 // If our old successor block contains any PHI nodes, remove the entry in the
80 // PHI nodes that comes from this branch...
81 //
82 BasicBlock *BB = TI->getParent();
83 TI->getSuccessor(SuccNum)->removePredecessor(BB);
84
85 TerminatorInst *NewTI = 0;
86 switch (TI->getOpcode()) {
87 case Instruction::Br:
88 // If this is a conditional branch... convert to unconditional branch.
89 if (TI->getNumSuccessors() == 2) {
90 cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
91 } else { // Otherwise convert to a return instruction...
92 Value *RetVal = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000093
Chris Lattnerb0f0ef82002-07-29 22:32:08 +000094 // Create a value to return... if the function doesn't return null...
95 if (BB->getParent()->getReturnType() != Type::VoidTy)
96 RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
97
98 // Create the return...
99 NewTI = new ReturnInst(RetVal);
100 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000101 break;
Chris Lattnerb0f0ef82002-07-29 22:32:08 +0000102
103 case Instruction::Invoke: // Should convert to call
104 case Instruction::Switch: // Should remove entry
105 default:
106 case Instruction::Ret: // Cannot happen, has no successors!
107 assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
108 abort();
109 }
110
111 if (NewTI) // If it's a different instruction, replace.
112 ReplaceInstWithInst(TI, NewTI);
113}
Brian Gaeked0fde302003-11-11 22:41:34 +0000114