LLVM API Documentation
00001 //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "PHIEliminationUtils.h" 00011 #include "llvm/ADT/SmallPtrSet.h" 00012 #include "llvm/CodeGen/MachineBasicBlock.h" 00013 #include "llvm/CodeGen/MachineFunction.h" 00014 #include "llvm/CodeGen/MachineRegisterInfo.h" 00015 using namespace llvm; 00016 00017 // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg 00018 // when following the CFG edge to SuccMBB. This needs to be after any def of 00019 // SrcReg, but before any subsequent point where control flow might jump out of 00020 // the basic block. 00021 MachineBasicBlock::iterator 00022 llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB, 00023 unsigned SrcReg) { 00024 // Handle the trivial case trivially. 00025 if (MBB->empty()) 00026 return MBB->begin(); 00027 00028 // Usually, we just want to insert the copy before the first terminator 00029 // instruction. However, for the edge going to a landing pad, we must insert 00030 // the copy before the call/invoke instruction. 00031 if (!SuccMBB->isLandingPad()) 00032 return MBB->getFirstTerminator(); 00033 00034 // Discover any defs/uses in this basic block. 00035 SmallPtrSet<MachineInstr*, 8> DefUsesInMBB; 00036 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo(); 00037 for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) { 00038 if (RI.getParent() == MBB) 00039 DefUsesInMBB.insert(&RI); 00040 } 00041 00042 MachineBasicBlock::iterator InsertPoint; 00043 if (DefUsesInMBB.empty()) { 00044 // No defs. Insert the copy at the start of the basic block. 00045 InsertPoint = MBB->begin(); 00046 } else if (DefUsesInMBB.size() == 1) { 00047 // Insert the copy immediately after the def/use. 00048 InsertPoint = *DefUsesInMBB.begin(); 00049 ++InsertPoint; 00050 } else { 00051 // Insert the copy immediately after the last def/use. 00052 InsertPoint = MBB->end(); 00053 while (!DefUsesInMBB.count(&*--InsertPoint)) {} 00054 ++InsertPoint; 00055 } 00056 00057 // Make sure the copy goes after any phi nodes however. 00058 return MBB->SkipPHIsAndLabels(InsertPoint); 00059 }