Local.cpp revision 858cd1c33c6ba47cf3401b1e00862aa22302af10
14d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===-- Local.cpp - Functions to perform local transformations ------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===// 94d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 104d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// This family of functions perform various local transformations to the 114d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// program. 124d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 134d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===// 144d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 154d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#include "llvm/Transforms/Utils/Local.h" 1681ebc300891a81c305258aed980567514dff952dChris Lattner#include "llvm/Constants.h" 176cc8a93c486f889c5767278508bc655942ba408eChris Lattner#include "llvm/GlobalAlias.h" 18c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel#include "llvm/GlobalVariable.h" 19c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/DerivedTypes.h" 207822c2ae077429d7bf6eb3f6ebf99d61f359b601Chris Lattner#include "llvm/Instructions.h" 21cf11035a6f9973d68d8eaf837d71dcf272d36b79Chris Lattner#include "llvm/Intrinsics.h" 22741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner#include "llvm/IntrinsicInst.h" 23c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich#include "llvm/Metadata.h" 24562b84b3aea359d1f918184e355da82bf05eb290Jay Foad#include "llvm/Operator.h" 2519f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/ADT/DenseMap.h" 26afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman#include "llvm/ADT/SmallPtrSet.h" 275ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DebugInfo.h" 285ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DIBuilder.h" 2980f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich#include "llvm/Analysis/Dominators.h" 3040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner#include "llvm/Analysis/InstructionSimplify.h" 314a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky#include "llvm/Analysis/MemoryBuiltins.h" 32ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter#include "llvm/Analysis/ProfileInfo.h" 33687140c818ba4b896329a83324714140b6580ef8Chris Lattner#include "llvm/Analysis/ValueTracking.h" 349fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner#include "llvm/Target/TargetData.h" 35dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/CFG.h" 36dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/Debug.h" 37c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/GetElementPtrTypeIterator.h" 38f88ad9aeeeb861a82f590f44484a055fcaf25b5cMatt Beaumont-Gay#include "llvm/Support/IRBuilder.h" 39c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/MathExtras.h" 4019f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/Support/ValueHandle.h" 41dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/raw_ostream.h" 42abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerusing namespace llvm; 43d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===// 453481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local constant propagation. 464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 485649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// ConstantFoldTerminator - If a terminator instruction is predicated on a 495649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// constant value, convert it into an unconditional branch to the constant 505649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// destination. This is a nontrivial operation because the successors of this 515649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// basic block must have their PHI nodes updated. 525649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 535649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// conditions and indirectbr addresses this might make dead if 545649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// DeleteDeadConditions is true. 555649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommelbool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions) { 5676ae3445f81164aaff9f95123426109c119f27c0Chris Lattner TerminatorInst *T = BB->getTerminator(); 5762fb3556eab41d9d66994e92d15e3e707c181988Devang Patel IRBuilder<> Builder(T); 58fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 594d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Branch - See if we are conditional jumping on constant 604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner if (BranchInst *BI = dyn_cast<BranchInst>(T)) { 614d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner if (BI->isUnconditional()) return false; // Can't optimize uncond branch 62c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif BasicBlock *Dest1 = BI->getSuccessor(0); 63c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif BasicBlock *Dest2 = BI->getSuccessor(1); 644d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 656b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { 664d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Are we branching on constant? 674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // YES. Change to unconditional branch... 68579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; 69579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; 704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 71fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman //cerr << "Function: " << T->getParent()->getParent() 72fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman // << "\nRemoving branch from " << T->getParent() 734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // << "\n\nTo: " << OldDest << endl; 744d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 754d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Let the basic block know that we are letting go of it. Based on this, 764d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // it will adjust it's PHI nodes. 771a0390253b3e7c2327139d81e5a8c16d5bf85aa8Jay Foad OldDest->removePredecessor(BB); 784d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 798f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad // Replace the conditional branch with an unconditional one. 8062fb3556eab41d9d66994e92d15e3e707c181988Devang Patel Builder.CreateBr(Destination); 818f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BI->eraseFromParent(); 824d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return true; 830a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 840a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 850a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (Dest2 == Dest1) { // Conditional branch to same location? 86fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman // This branch matches something like this: 874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // br bool %cond, label %Dest, label %Dest 884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // and changes it into: br label %Dest 894d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 904d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Let the basic block know that we are letting go of one copy of it. 914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner assert(BI->getParent() && "Terminator not inserted in block!"); 924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner Dest1->removePredecessor(BI->getParent()); 934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 948f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad // Replace the conditional branch with an unconditional one. 9562fb3556eab41d9d66994e92d15e3e707c181988Devang Patel Builder.CreateBr(Dest1); 965649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel Value *Cond = BI->getCondition(); 978f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BI->eraseFromParent(); 985649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel if (DeleteDeadConditions) 995649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel RecursivelyDeleteTriviallyDeadInstructions(Cond); 1004d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return true; 1014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner } 1020a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return false; 1030a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1040a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1050a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { 10610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // If we are switching on a constant, we can convert the switch into a 10710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // single branch instruction! 10810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); 109c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy BasicBlock *TheOnlyDest = SI->getDefaultDest(); 1107d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner BasicBlock *DefaultDest = TheOnlyDest; 11110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1120a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Figure out which case it goes to. 1133d3abe0852d5f499bed7ab014519dd582a0a795dStepan Dyatkovskiy for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 114c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy i != e; ++i) { 11510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Found case matching a constant operand? 116c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy if (i.getCaseValue() == CI) { 117c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy TheOnlyDest = i.getCaseSuccessor(); 11810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner break; 11910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 12010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1217d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner // Check to see if this branch is going to the same place as the default 1227d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner // dest. If so, eliminate it as an explicit compare. 123c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy if (i.getCaseSuccessor() == DefaultDest) { 1240a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Remove this entry. 1257d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner DefaultDest->removePredecessor(SI->getParent()); 1267d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner SI->removeCase(i); 127c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy --i; --e; 1287d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner continue; 1297d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner } 1307d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner 13110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Otherwise, check to see if the switch only branches to one destination. 13210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // We do this by reseting "TheOnlyDest" to null when we find two non-equal 13310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // destinations. 134c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = 0; 13510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 136694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner 13710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (CI && !TheOnlyDest) { 13810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Branching on a constant, but not any of the cases, go to the default 13910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // successor. 14010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner TheOnlyDest = SI->getDefaultDest(); 141694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner } 142694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner 14310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // If we found a single destination that we can fold the switch into, do so 14410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // now. 14510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (TheOnlyDest) { 1460a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 14762fb3556eab41d9d66994e92d15e3e707c181988Devang Patel Builder.CreateBr(TheOnlyDest); 14810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BasicBlock *BB = SI->getParent(); 14910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 15010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Remove entries from PHI nodes which we no longer branch to... 15110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { 15210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Found case matching a constant operand? 15310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BasicBlock *Succ = SI->getSuccessor(i); 15410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (Succ == TheOnlyDest) 15510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest 15610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner else 15710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner Succ->removePredecessor(BB); 15810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 15910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1600a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Delete the old switch. 1615649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel Value *Cond = SI->getCondition(); 1625649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel SI->eraseFromParent(); 1635649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel if (DeleteDeadConditions) 1645649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel RecursivelyDeleteTriviallyDeadInstructions(Cond); 16510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner return true; 1660a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1670a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 16824473120a253a05f3601cd3373403b47e6d03d41Stepan Dyatkovskiy if (SI->getNumCases() == 1) { 16910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Otherwise, we can fold this switch into a conditional branch 17010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // instruction if it has only one non-default destination. 1713d3abe0852d5f499bed7ab014519dd582a0a795dStepan Dyatkovskiy SwitchInst::CaseIt FirstCase = SI->case_begin(); 17262fb3556eab41d9d66994e92d15e3e707c181988Devang Patel Value *Cond = Builder.CreateICmpEQ(SI->getCondition(), 173c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy FirstCase.getCaseValue(), "cond"); 17462fb3556eab41d9d66994e92d15e3e707c181988Devang Patel 1750a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 176c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy Builder.CreateCondBr(Cond, FirstCase.getCaseSuccessor(), 177c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy SI->getDefaultDest()); 17810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1790a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Delete the old switch. 1801adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman SI->eraseFromParent(); 18110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner return true; 18210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 1830a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return false; 1840a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1850a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1860a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) { 1870a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // indirectbr blockaddress(@F, @BB) -> br label @BB 1880a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (BlockAddress *BA = 1890a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { 1900a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner BasicBlock *TheOnlyDest = BA->getBasicBlock(); 1910a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 19262fb3556eab41d9d66994e92d15e3e707c181988Devang Patel Builder.CreateBr(TheOnlyDest); 1930a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1940a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 1950a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (IBI->getDestination(i) == TheOnlyDest) 1960a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner TheOnlyDest = 0; 1970a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner else 1980a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner IBI->getDestination(i)->removePredecessor(IBI->getParent()); 1990a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 2005649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel Value *Address = IBI->getAddress(); 2010a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner IBI->eraseFromParent(); 2025649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel if (DeleteDeadConditions) 2035649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel RecursivelyDeleteTriviallyDeadInstructions(Address); 2040a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 2050a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // If we didn't find our destination in the IBI successor list, then we 2060a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // have undefined behavior. Replace the unconditional branch with an 2070a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // 'unreachable' instruction. 2080a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (TheOnlyDest) { 2090a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner BB->getTerminator()->eraseFromParent(); 2100a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner new UnreachableInst(BB->getContext(), BB); 2110a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 2120a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 2130a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return true; 2140a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 2154d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner } 2160a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 2174d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return false; 2184d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner} 2194d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2204d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2214d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===// 22240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner// Local dead code elimination. 2234d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 2244d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2253481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the 2263481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// instruction is not used, and the instruction has no side effects. 2273481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// 228abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::isInstructionTriviallyDead(Instruction *I) { 229ec710c5b12af647ae90f53917122726269c18738Chris Lattner if (!I->use_empty() || isa<TerminatorInst>(I)) return false; 23000b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 231187b1924a4b68350a6492b116db0fb19c659222fBill Wendling // We don't want the landingpad instruction removed by anything this general. 232187b1924a4b68350a6492b116db0fb19c659222fBill Wendling if (isa<LandingPadInst>(I)) 233187b1924a4b68350a6492b116db0fb19c659222fBill Wendling return false; 234187b1924a4b68350a6492b116db0fb19c659222fBill Wendling 2359c5822a966572ea78f4e818870d4229c7a855749Devang Patel // We don't want debug info removed by anything this general, unless 2369c5822a966572ea78f4e818870d4229c7a855749Devang Patel // debug info is empty. 2379c5822a966572ea78f4e818870d4229c7a855749Devang Patel if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 2383e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky if (DDI->getAddress()) 2399c5822a966572ea78f4e818870d4229c7a855749Devang Patel return false; 240b99462117ebd4be41788346246d7935fc90a11eeDevang Patel return true; 2413e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky } 242b99462117ebd4be41788346246d7935fc90a11eeDevang Patel if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 2439c5822a966572ea78f4e818870d4229c7a855749Devang Patel if (DVI->getValue()) 2449c5822a966572ea78f4e818870d4229c7a855749Devang Patel return false; 245b99462117ebd4be41788346246d7935fc90a11eeDevang Patel return true; 2469c5822a966572ea78f4e818870d4229c7a855749Devang Patel } 2479c5822a966572ea78f4e818870d4229c7a855749Devang Patel 2487af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands if (!I->mayHaveSideEffects()) return true; 2497af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands 2507af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands // Special case intrinsics that "may have side effects" but can be deleted 2517af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands // when dead. 2523e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 253741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner // Safe to delete llvm.stacksave if dead. 254741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner if (II->getIntrinsicID() == Intrinsic::stacksave) 255741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner return true; 2563e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky 2573e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky // Lifetime intrinsics are dead when their right-hand is undef. 2583e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky if (II->getIntrinsicID() == Intrinsic::lifetime_start || 2593e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky II->getIntrinsicID() == Intrinsic::lifetime_end) 2603e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky return isa<UndefValue>(II->getArgOperand(1)); 2613e69c13c301acee99c8bde7e692777bf856a6362Nick Lewycky } 2624a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky 2634a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky if (extractMallocCall(I)) return true; 2644a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky 2654a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky if (CallInst *CI = isFreeCall(I)) 2664a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) 2674a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky return C->isNullValue() || isa<UndefValue>(C); 2684a3935c27e5315081844a5b7ae1f7097efc234b0Nick Lewycky 269ec710c5b12af647ae90f53917122726269c18738Chris Lattner return false; 2704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner} 2714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2723481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 2733481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead instruction, delete it. If that makes any of its operands 27490fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// trivially dead, delete them too, recursively. Return true if any 27590fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// instructions were deleted. 27690fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) { 2773481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner Instruction *I = dyn_cast<Instruction>(V); 2787605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner if (!I || !I->use_empty() || !isInstructionTriviallyDead(I)) 27990fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman return false; 2803481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner 2817605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner SmallVector<Instruction*, 16> DeadInsts; 2827605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner DeadInsts.push_back(I); 2833481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner 284321a813c536e2f1f2f05bbe78a7fbf64046f0557Dan Gohman do { 285e9d87f49063cb1bd213d8e9c339b9b63393cc2d9Dan Gohman I = DeadInsts.pop_back_val(); 2862872177834d83b42cd042a37299cb7089965f36bChris Lattner 2877605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner // Null out all of the instruction's operands to see if any operand becomes 2887605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner // dead as we go. 2897605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 2907605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner Value *OpV = I->getOperand(i); 2917605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner I->setOperand(i, 0); 2927605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner 2937605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner if (!OpV->use_empty()) continue; 2947605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner 2957605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner // If the operand is an instruction that became dead as we nulled out the 2967605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner // operand, and if it is 'trivially' dead, delete it in a future loop 2977605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner // iteration. 2987605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner if (Instruction *OpI = dyn_cast<Instruction>(OpV)) 2997605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner if (isInstructionTriviallyDead(OpI)) 3007605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner DeadInsts.push_back(OpI); 3017605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner } 3027605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner 3037605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner I->eraseFromParent(); 304321a813c536e2f1f2f05bbe78a7fbf64046f0557Dan Gohman } while (!DeadInsts.empty()); 30590fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman 30690fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman return true; 3074d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner} 308b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 3091a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky/// areAllUsesEqual - Check whether the uses of a value are all the same. 3101a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky/// This is similar to Instruction::hasOneUse() except this will also return 311b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands/// true when there are no uses or multiple uses that all refer to the same 312b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands/// value. 3131a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewyckystatic bool areAllUsesEqual(Instruction *I) { 3141a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky Value::use_iterator UI = I->use_begin(); 3151a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky Value::use_iterator UE = I->use_end(); 3161a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky if (UI == UE) 317b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands return true; 3181a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky 3191a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky User *TheUse = *UI; 3201a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky for (++UI; UI != UE; ++UI) { 3211a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky if (*UI != TheUse) 3221a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky return false; 3231a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky } 3241a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky return true; 3251a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky} 3261a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky 327afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 328afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// dead PHI node, due to being a def-use chain of single-use nodes that 329afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// either forms a cycle or is terminated by a trivially dead instruction, 330afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// delete it. If that makes any of its operands trivially dead, delete them 3312cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands/// too, recursively. Return true if a change was made. 3321a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewyckybool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) { 333b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands SmallPtrSet<Instruction*, 4> Visited; 334b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects(); 335b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands I = cast<Instruction>(*I->use_begin())) { 336b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands if (I->use_empty()) 337b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands return RecursivelyDeleteTriviallyDeadInstructions(I); 338afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 339b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands // If we find an instruction more than once, we're on a cycle that 340afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman // won't prove fruitful. 341b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands if (!Visited.insert(I)) { 342b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands // Break the cycle and delete the instruction and its operands. 343b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands I->replaceAllUsesWith(UndefValue::get(I->getType())); 3442cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands (void)RecursivelyDeleteTriviallyDeadInstructions(I); 3452cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands return true; 346b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands } 347b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands } 348b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands return false; 349afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman} 3503481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner 351e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// SimplifyInstructionsInBlock - Scan the specified basic block and try to 352e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// simplify any instructions in it and recursively delete dead instructions. 353e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// 354e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// This returns true if it changed the code, note that it can delete 355e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// instructions in other blocks as well in this block. 356e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattnerbool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD) { 357e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner bool MadeChange = false; 358858cd1c33c6ba47cf3401b1e00862aa22302af10Chandler Carruth for (BasicBlock::iterator BI = BB->begin(), E = --BB->end(); BI != E; ) { 359858cd1c33c6ba47cf3401b1e00862aa22302af10Chandler Carruth assert(!BI->isTerminator()); 360e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner Instruction *Inst = BI++; 3616b980541df5846ad335c377c8803b517968daee2Chandler Carruth 3626b980541df5846ad335c377c8803b517968daee2Chandler Carruth WeakVH BIHandle(BI); 3636b980541df5846ad335c377c8803b517968daee2Chandler Carruth if (recursivelySimplifyInstruction(Inst, TD)) { 364e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner MadeChange = true; 36535a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner if (BIHandle != BI) 366e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner BI = BB->begin(); 367e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner continue; 368e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner } 36971ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman 370e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst); 37171ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman if (BIHandle != BI) 37271ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman BI = BB->begin(); 373e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner } 374e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner return MadeChange; 375e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner} 376e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner 377b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner//===----------------------------------------------------------------------===// 37840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner// Control Flow Graph Restructuring. 379b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner// 380b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 38140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 38240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 38340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// method is called when we're about to delete Pred as a predecessor of BB. If 38440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 38540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 38640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// Unlike the removePredecessor method, this attempts to simplify uses of PHI 38740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// nodes that collapse into identity values. For example, if we have: 38840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// x = phi(1, 0, 0, 0) 38940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// y = and x, z 39040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 39140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// .. and delete the predecessor corresponding to the '1', this will attempt to 39240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// recursively fold the and to 0. 39340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattnervoid llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 39440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner TargetData *TD) { 39540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // This only adjusts blocks with PHI nodes. 39640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner if (!isa<PHINode>(BB->begin())) 39740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner return; 39840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 39940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // Remove the entries for Pred from the PHI nodes in BB, but do not simplify 40040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // them down. This will leave us with single entry phi nodes and other phis 40140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // that can be removed. 40240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner BB->removePredecessor(Pred, true); 40340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 40440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner WeakVH PhiIt = &BB->front(); 40540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) { 40640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt)); 4076b980541df5846ad335c377c8803b517968daee2Chandler Carruth Value *OldPhiIt = PhiIt; 4086ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands 4096b980541df5846ad335c377c8803b517968daee2Chandler Carruth if (!recursivelySimplifyInstruction(PN, TD)) 4106b980541df5846ad335c377c8803b517968daee2Chandler Carruth continue; 4116ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands 41240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // If recursive simplification ended up deleting the next PHI node we would 41340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // iterate to, then our iterator is invalid, restart scanning from the top 41440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // of the block. 41535a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner if (PhiIt != OldPhiIt) PhiIt = &BB->front(); 41640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner } 41740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner} 41840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 41940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 420b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its 421b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (DestBB!). Eliminate the edge 422b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into DestBB and 423b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deleting the predecessor block. 424b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// 425ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustiftervoid llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) { 426b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // If BB has single-entry PHI nodes, fold them. 427b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { 428b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner Value *NewVal = PN->getIncomingValue(0); 429b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Replace self referencing PHI with undef, it must be dead. 4309e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); 431b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PN->replaceAllUsesWith(NewVal); 432b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PN->eraseFromParent(); 433b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner } 434b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 435b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner BasicBlock *PredBB = DestBB->getSinglePredecessor(); 436b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner assert(PredBB && "Block doesn't have a single predecessor!"); 437b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 43837914c8e83c43d710925263b66014159f03fa355Chris Lattner // Zap anything that took the address of DestBB. Not doing this will give the 43937914c8e83c43d710925263b66014159f03fa355Chris Lattner // address an invalid value. 44037914c8e83c43d710925263b66014159f03fa355Chris Lattner if (DestBB->hasAddressTaken()) { 44137914c8e83c43d710925263b66014159f03fa355Chris Lattner BlockAddress *BA = BlockAddress::get(DestBB); 44237914c8e83c43d710925263b66014159f03fa355Chris Lattner Constant *Replacement = 44337914c8e83c43d710925263b66014159f03fa355Chris Lattner ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1); 44437914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, 44537914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->getType())); 44637914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->destroyConstant(); 44737914c8e83c43d710925263b66014159f03fa355Chris Lattner } 448b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 449b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Anything that branched to PredBB now branches to DestBB. 450b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PredBB->replaceAllUsesWith(DestBB); 451b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 45295c3e48f9557adb6064d580684bb14cacec2f826Jay Foad // Splice all the instructions from PredBB to DestBB. 45395c3e48f9557adb6064d580684bb14cacec2f826Jay Foad PredBB->getTerminator()->eraseFromParent(); 45495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); 45595c3e48f9557adb6064d580684bb14cacec2f826Jay Foad 456ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter if (P) { 45780f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); 45880f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich if (DT) { 45980f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock(); 46080f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DT->changeImmediateDominator(DestBB, PredBBIDom); 46180f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DT->eraseNode(PredBB); 46280f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich } 463ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>(); 464ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter if (PI) { 465ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter PI->replaceAllUses(PredBB, DestBB); 466ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB)); 467ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter } 468ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter } 469b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Nuke BB. 470b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PredBB->eraseFromParent(); 471b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner} 4724afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel 473dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an 474dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// almost-empty BB ending in an unconditional branch to Succ, into succ. 475dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// 476dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// Assumption: Succ is the single successor for BB. 477dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// 478dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerstatic bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { 479dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); 480dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 481fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 482dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << "\n"); 483dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Shortcut, if there is only a single predecessor it must be BB and merging 484dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // is always safe 485dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (Succ->getSinglePredecessor()) return true; 486dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 487dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Make a list of the predecessors of BB 48888c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); 489dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 490dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Look at all the phi nodes in Succ, to see if they present a conflict when 491dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // merging these blocks 492dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 493dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *PN = cast<PHINode>(I); 494dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 495dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If the incoming value from BB is again a PHINode in 496dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // BB which has the same incoming value for *PI as PN does, we can 497dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // merge the phi nodes and then the blocks can still be merged 498dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); 499dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (BBPN && BBPN->getParent() == BB) { 50088c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 50188c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer BasicBlock *IBB = PN->getIncomingBlock(PI); 50288c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer if (BBPreds.count(IBB) && 50388c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer BBPN->getIncomingValueForBlock(IBB) != PN->getIncomingValue(PI)) { 504fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 505dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << " is conflicting with " 506dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << BBPN->getName() << " with regard to common predecessor " 50788c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer << IBB->getName() << "\n"); 508dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 509dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 510dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 511dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 512dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner Value* Val = PN->getIncomingValueForBlock(BB); 51388c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 514dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // See if the incoming value for the common predecessor is equal to the 515dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // one for BB, in which case this phi node will not prevent the merging 516dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // of the block. 51788c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer BasicBlock *IBB = PN->getIncomingBlock(PI); 51888c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer if (BBPreds.count(IBB) && Val != PN->getIncomingValue(PI)) { 519fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 520dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << " is conflicting with regard to common " 52188c09143b6af07ed7b16381885a03c4b10886f96Benjamin Kramer << "predecessor " << IBB->getName() << "\n"); 522dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 523dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 524dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 525dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 526dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 527dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 528dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return true; 529dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner} 530dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 531dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 532dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// unconditional branch, and contains no instructions other than PHI nodes, 53377a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola/// potential side-effect free intrinsics and the branch. If possible, 53477a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola/// eliminate BB by rewriting all the predecessors to branch to the successor 53577a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola/// block and return true. If we can't transform, return false. 536dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerbool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) { 537e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman assert(BB != &BB->getParent()->getEntryBlock() && 538e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); 539e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman 540dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // We can't eliminate infinite loops. 541dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); 542dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (BB == Succ) return false; 543dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 544dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Check to see if merging these blocks would cause conflicts for any of the 545dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // phi nodes in BB or Succ. If not, we can safely merge. 546dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; 547dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 548dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Check for cases where Succ has multiple predecessors and a PHI node in BB 549dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // has uses which will not disappear when the PHI nodes are merged. It is 550dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // possible to handle such cases, but difficult: it requires checking whether 551dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // BB dominates Succ, which is non-trivial to calculate in the case where 552dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Succ has multiple predecessors. Also, it requires checking whether 553dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // constructing the necessary self-referential PHI node doesn't intoduce any 554dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // conflicts; this isn't too difficult, but the previous code for doing this 555dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // was incorrect. 556dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // 557dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Note that if this check finds a live use, BB dominates Succ, so BB is 558dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // something like a loop pre-header (or rarely, a part of an irreducible CFG); 559dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // folding the branch isn't profitable in that case anyway. 560dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!Succ->getSinglePredecessor()) { 561dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BasicBlock::iterator BBI = BB->begin(); 562dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner while (isa<PHINode>(*BBI)) { 563dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end(); 564dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner UI != E; ++UI) { 565dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (PHINode* PN = dyn_cast<PHINode>(*UI)) { 566dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (PN->getIncomingBlock(UI) != BB) 567dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 568dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 569dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 570dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 571dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 572dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner ++BBI; 573dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 574dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 575dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 576fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); 577dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 578dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (isa<PHINode>(Succ->begin())) { 579dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If there is more than one pred of succ, and there are PHI nodes in 580dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // the successor, then we need to add incoming edges for the PHI nodes 581dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // 582dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); 583dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 584dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Loop over all of the PHI nodes in the successor of BB. 585dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 586dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *PN = cast<PHINode>(I); 587dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner Value *OldVal = PN->removeIncomingValue(BB, false); 588dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(OldVal && "No entry in PHI for Pred BB!"); 589dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 590dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If this incoming value is one of the PHI nodes in BB, the new entries 591dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // in the PHI node are the entries from the old PHI. 592dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { 593dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *OldValPN = cast<PHINode>(OldVal); 594dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) 595dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Note that, since we are merging phi nodes and BB and Succ might 596dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // have common predecessors, we could end up with a phi node with 597dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // identical incoming branches. This will be cleaned up later (and 598dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // will trigger asserts if we try to clean it up now, without also 599dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // simplifying the corresponding conditional branch). 600dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->addIncoming(OldValPN->getIncomingValue(i), 601dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner OldValPN->getIncomingBlock(i)); 602dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 603dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Add an incoming value for each of the new incoming values. 604dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) 605dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->addIncoming(OldVal, BBPreds[i]); 606dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 607dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 608dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 609dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 61077a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola if (Succ->getSinglePredecessor()) { 61177a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola // BB is the only predecessor of Succ, so Succ will end up with exactly 61277a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola // the same predecessors BB had. 61377a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola 61477a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola // Copy over any phi, debug or lifetime instruction. 61577a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola BB->getTerminator()->eraseFromParent(); 61677a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList()); 61777a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola } else { 61877a2c372face15a302f4c9e5cb9acc035b8b3bd3Rafael Espindola while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { 619dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // We explicitly check for such uses in CanPropagatePredecessorsForPHIs. 620dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(PN->use_empty() && "There shouldn't be any uses here!"); 621dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->eraseFromParent(); 622dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 623dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 624dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 625dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Everything that jumped to BB now goes to Succ. 626dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BB->replaceAllUsesWith(Succ); 627dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!Succ->hasName()) Succ->takeName(BB); 628dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BB->eraseFromParent(); // Delete the old basic block. 629dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return true; 630dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner} 631dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 63243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 63343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// nodes in this block. This doesn't try to be clever about PHI nodes 63443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// which differ only in the order of the incoming values, but instcombine 63543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// orders them so it usually won't matter. 63643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// 63743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbachbool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { 63843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach bool Changed = false; 63943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 64043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // This implementation doesn't currently consider undef operands 64189991d44136414c4c74eee7c6dfbdbeab287b881Nick Lewycky // specially. Theoretically, two phis which are identical except for 64243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // one having an undef where the other doesn't could be collapsed. 64343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 64443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Map from PHI hash values to PHI nodes. If multiple PHIs have 64543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // the same hash value, the element is the first PHI in the 64643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // linked list in CollisionMap. 64743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<uintptr_t, PHINode *> HashMap; 64843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 64943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Maintain linked lists of PHI nodes with common hash values. 65043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<PHINode *, PHINode *> CollisionMap; 65143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 65243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Examine each PHI. 65343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (BasicBlock::iterator I = BB->begin(); 65443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PHINode *PN = dyn_cast<PHINode>(I++); ) { 65543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Compute a hash value on the operands. Instcombine will likely have sorted 65643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // them, which helps expose duplicates, but we have to check all the 65743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // operands to be safe in case instcombine hasn't run. 65843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach uintptr_t Hash = 0; 65995c3e48f9557adb6064d580684bb14cacec2f826Jay Foad // This hash algorithm is quite weak as hash functions go, but it seems 66095c3e48f9557adb6064d580684bb14cacec2f826Jay Foad // to do a good enough job for this particular purpose, and is very quick. 66143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) { 66243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I)); 66343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); 66443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 66595c3e48f9557adb6064d580684bb14cacec2f826Jay Foad for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end(); 66695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad I != E; ++I) { 66795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I)); 66895c3e48f9557adb6064d580684bb14cacec2f826Jay Foad Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); 66995c3e48f9557adb6064d580684bb14cacec2f826Jay Foad } 6702bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen // Avoid colliding with the DenseMap sentinels ~0 and ~0-1. 6712bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen Hash >>= 1; 67243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // If we've never seen this hash value before, it's a unique PHI. 67343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair = 67443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach HashMap.insert(std::make_pair(Hash, PN)); 67543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (Pair.second) continue; 67643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Otherwise it's either a duplicate or a hash collision. 67743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (PHINode *OtherPN = Pair.first->second; ; ) { 67843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (OtherPN->isIdenticalTo(PN)) { 67943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // A duplicate. Replace this PHI with its duplicate. 68043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PN->replaceAllUsesWith(OtherPN); 68143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PN->eraseFromParent(); 68243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Changed = true; 68343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach break; 68443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 68543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // A non-duplicate hash collision. 68643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN); 68743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (I == CollisionMap.end()) { 68843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Set this PHI to be the head of the linked list of colliding PHIs. 68943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PHINode *Old = Pair.first->second; 69043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Pair.first->second = PN; 69143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach CollisionMap[PN] = Old; 69243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach break; 69343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 69443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Procede to the next PHI in the list. 69543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach OtherPN = I->second; 69643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 69743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 69843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 69943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach return Changed; 70043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach} 701687140c818ba4b896329a83324714140b6580ef8Chris Lattner 702687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// enforceKnownAlignment - If the specified pointer points to an object that 703687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we control, modify the object's alignment to PrefAlign. This isn't 704687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// often possible though. If alignment is important, a more reliable approach 705687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// is to simply align all global variables and allocation instructions to 706687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// their preferred alignment from the beginning. 707687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// 70819282365147f498a60463d250dbed2f8e1b81861Benjamin Kramerstatic unsigned enforceKnownAlignment(Value *V, unsigned Align, 709bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames unsigned PrefAlign, const TargetData *TD) { 710b53c7937c5651e5500b554186657b27838137365Eli Friedman V = V->stripPointerCasts(); 711687140c818ba4b896329a83324714140b6580ef8Chris Lattner 712b53c7937c5651e5500b554186657b27838137365Eli Friedman if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 713bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames // If the preferred alignment is greater than the natural stack alignment 714bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames // then don't round up. This avoids dynamic stack realignment. 715bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames if (TD && TD->exceedsNaturalStackAlignment(PrefAlign)) 716bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames return Align; 717687140c818ba4b896329a83324714140b6580ef8Chris Lattner // If there is a requested alignment and if this is an alloca, round up. 718687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (AI->getAlignment() >= PrefAlign) 719687140c818ba4b896329a83324714140b6580ef8Chris Lattner return AI->getAlignment(); 720687140c818ba4b896329a83324714140b6580ef8Chris Lattner AI->setAlignment(PrefAlign); 721687140c818ba4b896329a83324714140b6580ef8Chris Lattner return PrefAlign; 722687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 723687140c818ba4b896329a83324714140b6580ef8Chris Lattner 724687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 725687140c818ba4b896329a83324714140b6580ef8Chris Lattner // If there is a large requested alignment and we can, bump up the alignment 726687140c818ba4b896329a83324714140b6580ef8Chris Lattner // of the global. 727687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GV->isDeclaration()) return Align; 728d3a38ccfbb6be0edad037961df77649db2cb9597Duncan Sands // If the memory we set aside for the global may not be the memory used by 729d3a38ccfbb6be0edad037961df77649db2cb9597Duncan Sands // the final program then it is impossible for us to reliably enforce the 730d3a38ccfbb6be0edad037961df77649db2cb9597Duncan Sands // preferred alignment. 731d3a38ccfbb6be0edad037961df77649db2cb9597Duncan Sands if (GV->isWeakForLinker()) return Align; 732687140c818ba4b896329a83324714140b6580ef8Chris Lattner 733687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GV->getAlignment() >= PrefAlign) 734687140c818ba4b896329a83324714140b6580ef8Chris Lattner return GV->getAlignment(); 735687140c818ba4b896329a83324714140b6580ef8Chris Lattner // We can only increase the alignment of the global if it has no alignment 736687140c818ba4b896329a83324714140b6580ef8Chris Lattner // specified or if it is not assigned a section. If it is assigned a 737687140c818ba4b896329a83324714140b6580ef8Chris Lattner // section, the global could be densely packed with other objects in the 738687140c818ba4b896329a83324714140b6580ef8Chris Lattner // section, increasing the alignment could cause padding issues. 739687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (!GV->hasSection() || GV->getAlignment() == 0) 740687140c818ba4b896329a83324714140b6580ef8Chris Lattner GV->setAlignment(PrefAlign); 741687140c818ba4b896329a83324714140b6580ef8Chris Lattner return GV->getAlignment(); 742687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 743687140c818ba4b896329a83324714140b6580ef8Chris Lattner 744687140c818ba4b896329a83324714140b6580ef8Chris Lattner return Align; 745687140c818ba4b896329a83324714140b6580ef8Chris Lattner} 746687140c818ba4b896329a83324714140b6580ef8Chris Lattner 747687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// getOrEnforceKnownAlignment - If the specified pointer has an alignment that 748687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we can determine, return it, otherwise return 0. If PrefAlign is specified, 749687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// and it is more than the alignment of the ultimate object, see if we can 750687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// increase the alignment of the ultimate object, making this check succeed. 751687140c818ba4b896329a83324714140b6580ef8Chris Lattnerunsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, 752687140c818ba4b896329a83324714140b6580ef8Chris Lattner const TargetData *TD) { 753687140c818ba4b896329a83324714140b6580ef8Chris Lattner assert(V->getType()->isPointerTy() && 754687140c818ba4b896329a83324714140b6580ef8Chris Lattner "getOrEnforceKnownAlignment expects a pointer!"); 755687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned BitWidth = TD ? TD->getPointerSizeInBits() : 64; 756687140c818ba4b896329a83324714140b6580ef8Chris Lattner APInt Mask = APInt::getAllOnesValue(BitWidth); 757687140c818ba4b896329a83324714140b6580ef8Chris Lattner APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 758ae47be1ea023e4b1e6bbbdc4687333eea54c84c8Chris Lattner ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD); 759687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned TrailZ = KnownZero.countTrailingOnes(); 760687140c818ba4b896329a83324714140b6580ef8Chris Lattner 761687140c818ba4b896329a83324714140b6580ef8Chris Lattner // Avoid trouble with rediculously large TrailZ values, such as 762687140c818ba4b896329a83324714140b6580ef8Chris Lattner // those computed from a null pointer. 763687140c818ba4b896329a83324714140b6580ef8Chris Lattner TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1)); 764687140c818ba4b896329a83324714140b6580ef8Chris Lattner 765687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); 766687140c818ba4b896329a83324714140b6580ef8Chris Lattner 767687140c818ba4b896329a83324714140b6580ef8Chris Lattner // LLVM doesn't support alignments larger than this currently. 768687140c818ba4b896329a83324714140b6580ef8Chris Lattner Align = std::min(Align, +Value::MaximumAlignment); 769687140c818ba4b896329a83324714140b6580ef8Chris Lattner 770687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (PrefAlign > Align) 771bb5b3f33594cfa40e9f53bf9a71af359b080a697Lang Hames Align = enforceKnownAlignment(V, Align, PrefAlign, TD); 772687140c818ba4b896329a83324714140b6580ef8Chris Lattner 773687140c818ba4b896329a83324714140b6580ef8Chris Lattner // We don't need to make any adjustment. 774687140c818ba4b896329a83324714140b6580ef8Chris Lattner return Align; 775687140c818ba4b896329a83324714140b6580ef8Chris Lattner} 776687140c818ba4b896329a83324714140b6580ef8Chris Lattner 7775ee20680c7ebc765950983633e19fafab5235245Devang Patel///===---------------------------------------------------------------------===// 7785ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Dbg Intrinsic utilities 7795ee20680c7ebc765950983633e19fafab5235245Devang Patel/// 7805ee20680c7ebc765950983633e19fafab5235245Devang Patel 7815ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 7825ee20680c7ebc765950983633e19fafab5235245Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 7835ee20680c7ebc765950983633e19fafab5235245Devang Patelbool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 7845ee20680c7ebc765950983633e19fafab5235245Devang Patel StoreInst *SI, DIBuilder &Builder) { 7855ee20680c7ebc765950983633e19fafab5235245Devang Patel DIVariable DIVar(DDI->getVariable()); 7865ee20680c7ebc765950983633e19fafab5235245Devang Patel if (!DIVar.Verify()) 7875ee20680c7ebc765950983633e19fafab5235245Devang Patel return false; 7885ee20680c7ebc765950983633e19fafab5235245Devang Patel 789227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel Instruction *DbgVal = NULL; 790227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel // If an argument is zero extended then use argument directly. The ZExt 791227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel // may be zapped by an optimization pass in future. 792227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel Argument *ExtendedArg = NULL; 793227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) 794227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel ExtendedArg = dyn_cast<Argument>(ZExt->getOperand(0)); 795227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) 796227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel ExtendedArg = dyn_cast<Argument>(SExt->getOperand(0)); 797227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel if (ExtendedArg) 798227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel DbgVal = Builder.insertDbgValueIntrinsic(ExtendedArg, 0, DIVar, SI); 799227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel else 800227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar, SI); 801227dfdb3c44c5cc5ec140b4be89f618bdc59a133Devang Patel 8025ee20680c7ebc765950983633e19fafab5235245Devang Patel // Propagate any debug metadata from the store onto the dbg.value. 8035ee20680c7ebc765950983633e19fafab5235245Devang Patel DebugLoc SIDL = SI->getDebugLoc(); 8045ee20680c7ebc765950983633e19fafab5235245Devang Patel if (!SIDL.isUnknown()) 8055ee20680c7ebc765950983633e19fafab5235245Devang Patel DbgVal->setDebugLoc(SIDL); 8065ee20680c7ebc765950983633e19fafab5235245Devang Patel // Otherwise propagate debug metadata from dbg.declare. 8075ee20680c7ebc765950983633e19fafab5235245Devang Patel else 8085ee20680c7ebc765950983633e19fafab5235245Devang Patel DbgVal->setDebugLoc(DDI->getDebugLoc()); 8095ee20680c7ebc765950983633e19fafab5235245Devang Patel return true; 8105ee20680c7ebc765950983633e19fafab5235245Devang Patel} 8115ee20680c7ebc765950983633e19fafab5235245Devang Patel 81236fae67831517f132255118b45b21a8cf199a012Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 81336fae67831517f132255118b45b21a8cf199a012Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 81436fae67831517f132255118b45b21a8cf199a012Devang Patelbool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 81536fae67831517f132255118b45b21a8cf199a012Devang Patel LoadInst *LI, DIBuilder &Builder) { 81636fae67831517f132255118b45b21a8cf199a012Devang Patel DIVariable DIVar(DDI->getVariable()); 81736fae67831517f132255118b45b21a8cf199a012Devang Patel if (!DIVar.Verify()) 81836fae67831517f132255118b45b21a8cf199a012Devang Patel return false; 81936fae67831517f132255118b45b21a8cf199a012Devang Patel 82036fae67831517f132255118b45b21a8cf199a012Devang Patel Instruction *DbgVal = 82136fae67831517f132255118b45b21a8cf199a012Devang Patel Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, 82236fae67831517f132255118b45b21a8cf199a012Devang Patel DIVar, LI); 82336fae67831517f132255118b45b21a8cf199a012Devang Patel 82436fae67831517f132255118b45b21a8cf199a012Devang Patel // Propagate any debug metadata from the store onto the dbg.value. 82536fae67831517f132255118b45b21a8cf199a012Devang Patel DebugLoc LIDL = LI->getDebugLoc(); 82636fae67831517f132255118b45b21a8cf199a012Devang Patel if (!LIDL.isUnknown()) 82736fae67831517f132255118b45b21a8cf199a012Devang Patel DbgVal->setDebugLoc(LIDL); 82836fae67831517f132255118b45b21a8cf199a012Devang Patel // Otherwise propagate debug metadata from dbg.declare. 82936fae67831517f132255118b45b21a8cf199a012Devang Patel else 83036fae67831517f132255118b45b21a8cf199a012Devang Patel DbgVal->setDebugLoc(DDI->getDebugLoc()); 83136fae67831517f132255118b45b21a8cf199a012Devang Patel return true; 83236fae67831517f132255118b45b21a8cf199a012Devang Patel} 83336fae67831517f132255118b45b21a8cf199a012Devang Patel 834813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 835813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// of llvm.dbg.value intrinsics. 836813c9a0f19c0d27085a3ea81eb44033747007741Devang Patelbool llvm::LowerDbgDeclare(Function &F) { 837813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel DIBuilder DIB(*F.getParent()); 838813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel SmallVector<DbgDeclareInst *, 4> Dbgs; 839813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) 840813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) { 841813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) 842813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel Dbgs.push_back(DDI); 843813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 844813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (Dbgs.empty()) 845813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel return false; 846813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel 847813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (SmallVector<DbgDeclareInst *, 4>::iterator I = Dbgs.begin(), 848813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel E = Dbgs.end(); I != E; ++I) { 849813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel DbgDeclareInst *DDI = *I; 850813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress())) { 85181ad03c020d7c1f73a78ce41ca713f1e425efe3dDevang Patel bool RemoveDDI = true; 852813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); 853813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel UI != E; ++UI) 854813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) 855813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel ConvertDebugDeclareToDebugValue(DDI, SI, DIB); 85636fae67831517f132255118b45b21a8cf199a012Devang Patel else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) 85736fae67831517f132255118b45b21a8cf199a012Devang Patel ConvertDebugDeclareToDebugValue(DDI, LI, DIB); 85881ad03c020d7c1f73a78ce41ca713f1e425efe3dDevang Patel else 85981ad03c020d7c1f73a78ce41ca713f1e425efe3dDevang Patel RemoveDDI = false; 86081ad03c020d7c1f73a78ce41ca713f1e425efe3dDevang Patel if (RemoveDDI) 86181ad03c020d7c1f73a78ce41ca713f1e425efe3dDevang Patel DDI->eraseFromParent(); 862813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 863813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 864813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel return true; 865813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel} 866c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich 867c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich/// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the 868c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich/// alloca 'V', if any. 869c827939046670a9800659b83e2048f1d3a79a531Cameron ZwarichDbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) { 870c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V)) 871c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich for (Value::use_iterator UI = DebugNode->use_begin(), 872c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich E = DebugNode->use_end(); UI != E; ++UI) 873c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(*UI)) 874c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich return DDI; 875c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich 876c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich return 0; 877c827939046670a9800659b83e2048f1d3a79a531Cameron Zwarich} 878