Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 10 | #include "llvm/iTerminators.h" |
| 11 | #include "llvm/Constant.h" |
| 12 | #include "llvm/Type.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 13 | #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 | // |
| 18 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 19 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 20 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 21 | // 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] | 22 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 24 | std::string OldName = I.getName(); |
| 25 | |
| 26 | // Delete the unneccesary instruction now... |
| 27 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 28 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 29 | // Make sure to propagate a name if there is one already... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 30 | if (OldName.size() && !V->hasName()) |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 31 | V->setName(OldName, &BIL.getParent()->getSymbolTable()); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 32 | } |
| 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 | // |
| 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!"); |
| 43 | |
| 44 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 45 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 46 | |
| 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 Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 51 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // ReplaceInstWithInst - Replace the instruction specified by From with the |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 55 | // instruction specified by To. |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 56 | // |
| 57 | void ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 58 | BasicBlock::iterator BI(From); |
| 59 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 61 | |
| 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. |
| 68 | // |
| 69 | void 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 | } |