LLVM API Documentation
00001 //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This family of functions perform manipulations on basic blocks, and 00011 // instructions contained within basic blocks. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00016 #include "llvm/Function.h" 00017 #include "llvm/Instructions.h" 00018 #include "llvm/Constant.h" 00019 #include "llvm/Type.h" 00020 #include <algorithm> 00021 using namespace llvm; 00022 00023 /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 00024 /// with a value, then remove and delete the original instruction. 00025 /// 00026 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, 00027 BasicBlock::iterator &BI, Value *V) { 00028 Instruction &I = *BI; 00029 // Replaces all of the uses of the instruction with uses of the value 00030 I.replaceAllUsesWith(V); 00031 00032 std::string OldName = I.getName(); 00033 00034 // Delete the unnecessary instruction now... 00035 BI = BIL.erase(BI); 00036 00037 // Make sure to propagate a name if there is one already. 00038 if (!OldName.empty() && !V->hasName()) 00039 V->setName(OldName); 00040 } 00041 00042 00043 /// ReplaceInstWithInst - Replace the instruction specified by BI with the 00044 /// instruction specified by I. The original instruction is deleted and BI is 00045 /// updated to point to the new instruction. 00046 /// 00047 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, 00048 BasicBlock::iterator &BI, Instruction *I) { 00049 assert(I->getParent() == 0 && 00050 "ReplaceInstWithInst: Instruction already inserted into basic block!"); 00051 00052 // Insert the new instruction into the basic block... 00053 BasicBlock::iterator New = BIL.insert(BI, I); 00054 00055 // Replace all uses of the old instruction, and delete it. 00056 ReplaceInstWithValue(BIL, BI, I); 00057 00058 // Move BI back to point to the newly inserted instruction 00059 BI = New; 00060 } 00061 00062 /// ReplaceInstWithInst - Replace the instruction specified by From with the 00063 /// instruction specified by To. 00064 /// 00065 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { 00066 BasicBlock::iterator BI(From); 00067 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); 00068 } 00069 00070 /// RemoveSuccessor - Change the specified terminator instruction such that its 00071 /// successor #SuccNum no longer exists. Because this reduces the outgoing 00072 /// degree of the current basic block, the actual terminator instruction itself 00073 /// may have to be changed. In the case where the last successor of the block is 00074 /// deleted, a return instruction is inserted in its place which can cause a 00075 /// surprising change in program behavior if it is not expected. 00076 /// 00077 void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { 00078 assert(SuccNum < TI->getNumSuccessors() && 00079 "Trying to remove a nonexistant successor!"); 00080 00081 // If our old successor block contains any PHI nodes, remove the entry in the 00082 // PHI nodes that comes from this branch... 00083 // 00084 BasicBlock *BB = TI->getParent(); 00085 TI->getSuccessor(SuccNum)->removePredecessor(BB); 00086 00087 TerminatorInst *NewTI = 0; 00088 switch (TI->getOpcode()) { 00089 case Instruction::Br: 00090 // If this is a conditional branch... convert to unconditional branch. 00091 if (TI->getNumSuccessors() == 2) { 00092 cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); 00093 } else { // Otherwise convert to a return instruction... 00094 Value *RetVal = 0; 00095 00096 // Create a value to return... if the function doesn't return null... 00097 if (BB->getParent()->getReturnType() != Type::VoidTy) 00098 RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); 00099 00100 // Create the return... 00101 NewTI = new ReturnInst(RetVal); 00102 } 00103 break; 00104 00105 case Instruction::Invoke: // Should convert to call 00106 case Instruction::Switch: // Should remove entry 00107 default: 00108 case Instruction::Ret: // Cannot happen, has no successors! 00109 assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!"); 00110 abort(); 00111 } 00112 00113 if (NewTI) // If it's a different instruction, replace. 00114 ReplaceInstWithInst(TI, NewTI); 00115 } 00116