Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 9 | // |
| 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 Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 18 | #include "llvm/Constant.h" |
| 19 | #include "llvm/Type.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 23 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 24 | // with a value, then remove and delete the original instruction. |
| 25 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 26 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 27 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 28 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 29 | // Replaces all of the uses of the instruction with uses of the value |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 30 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 32 | std::string OldName = I.getName(); |
| 33 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 34 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 35 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 36 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 37 | // Make sure to propagate a name if there is one already... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 38 | if (OldName.size() && !V->hasName()) |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 39 | V->setName(OldName, &BIL.getParent()->getSymbolTable()); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 44 | // instruction specified by I. The original instruction is deleted and BI is |
| 45 | // updated to point to the new instruction. |
| 46 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 47 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 48 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 49 | assert(I->getParent() == 0 && |
| 50 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 51 | |
| 52 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 53 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 54 | |
| 55 | // Replace all uses of the old instruction, and delete it. |
| 56 | ReplaceInstWithValue(BIL, BI, I); |
| 57 | |
| 58 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 59 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // ReplaceInstWithInst - Replace the instruction specified by From with the |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 63 | // instruction specified by To. |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 64 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 65 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 66 | BasicBlock::iterator BI(From); |
| 67 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 69 | |
| 70 | // RemoveSuccessor - Change the specified terminator instruction such that its |
| 71 | // successor #SuccNum no longer exists. Because this reduces the outgoing |
| 72 | // degree of the current basic block, the actual terminator instruction itself |
| 73 | // may have to be changed. In the case where the last successor of the block is |
| 74 | // deleted, a return instruction is inserted in its place which can cause a |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 75 | // surprising change in program behavior if it is not expected. |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 76 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 77 | void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 78 | assert(SuccNum < TI->getNumSuccessors() && |
| 79 | "Trying to remove a nonexistant successor!"); |
| 80 | |
| 81 | // If our old successor block contains any PHI nodes, remove the entry in the |
| 82 | // PHI nodes that comes from this branch... |
| 83 | // |
| 84 | BasicBlock *BB = TI->getParent(); |
| 85 | TI->getSuccessor(SuccNum)->removePredecessor(BB); |
| 86 | |
| 87 | TerminatorInst *NewTI = 0; |
| 88 | switch (TI->getOpcode()) { |
| 89 | case Instruction::Br: |
| 90 | // If this is a conditional branch... convert to unconditional branch. |
| 91 | if (TI->getNumSuccessors() == 2) { |
| 92 | cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); |
| 93 | } else { // Otherwise convert to a return instruction... |
| 94 | Value *RetVal = 0; |
| 95 | |
| 96 | // Create a value to return... if the function doesn't return null... |
| 97 | if (BB->getParent()->getReturnType() != Type::VoidTy) |
| 98 | RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); |
| 99 | |
| 100 | // Create the return... |
| 101 | NewTI = new ReturnInst(RetVal); |
| 102 | } |
| 103 | break; |
| 104 | |
| 105 | case Instruction::Invoke: // Should convert to call |
| 106 | case Instruction::Switch: // Should remove entry |
| 107 | default: |
| 108 | case Instruction::Ret: // Cannot happen, has no successors! |
| 109 | assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!"); |
| 110 | abort(); |
| 111 | } |
| 112 | |
| 113 | if (NewTI) // If it's a different instruction, replace. |
| 114 | ReplaceInstWithInst(TI, NewTI); |
| 115 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 116 | |