Local.cpp revision 562b84b3aea359d1f918184e355da82bf05eb290
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"
23562b84b3aea359d1f918184e355da82bf05eb290Jay Foad#include "llvm/Operator.h"
2419f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/ADT/DenseMap.h"
25afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman#include "llvm/ADT/SmallPtrSet.h"
265ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DebugInfo.h"
275ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DIBuilder.h"
2880f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich#include "llvm/Analysis/Dominators.h"
29cbbc6b74e357afbf8fb37fdeb177ed78021092d3Chris Lattner#include "llvm/Analysis/ConstantFolding.h"
3040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner#include "llvm/Analysis/InstructionSimplify.h"
31ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter#include "llvm/Analysis/ProfileInfo.h"
32687140c818ba4b896329a83324714140b6580ef8Chris Lattner#include "llvm/Analysis/ValueTracking.h"
339fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner#include "llvm/Target/TargetData.h"
34dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/CFG.h"
35dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/Debug.h"
36c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/GetElementPtrTypeIterator.h"
37c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/MathExtras.h"
3819f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/Support/ValueHandle.h"
39dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/raw_ostream.h"
40abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerusing namespace llvm;
41d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
424d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
433481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner//  Local constant propagation.
444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
454d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldTerminator - If a terminator instruction is predicated on a
474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// constant value, convert it into an unconditional branch to the constant
484d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// destination.
494d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
50abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::ConstantFoldTerminator(BasicBlock *BB) {
5176ae3445f81164aaff9f95123426109c119f27c0Chris Lattner  TerminatorInst *T = BB->getTerminator();
52fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
534d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Branch - See if we are conditional jumping on constant
544d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
554d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
56c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif    BasicBlock *Dest1 = BI->getSuccessor(0);
57c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif    BasicBlock *Dest2 = BI->getSuccessor(1);
584d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
596b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Are we branching on constant?
614d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // YES.  Change to unconditional branch...
62579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer      BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
63579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer      BasicBlock *OldDest     = Cond->getZExtValue() ? Dest2 : Dest1;
644d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
65fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      //cerr << "Function: " << T->getParent()->getParent()
66fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      //     << "\nRemoving branch from " << T->getParent()
674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\n\nTo: " << OldDest << endl;
684d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
694d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of it.  Based on this,
704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // it will adjust it's PHI nodes.
714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      OldDest->removePredecessor(BI->getParent());
734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
748f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      // Replace the conditional branch with an unconditional one.
758f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      BranchInst::Create(Destination, BI);
768f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      BI->eraseFromParent();
774d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
780a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    }
790a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
800a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    if (Dest2 == Dest1) {       // Conditional branch to same location?
81fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      // This branch matches something like this:
824d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     br bool %cond, label %Dest, label %Dest
834d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // and changes it into:  br label %Dest
844d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
854d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of one copy of it.
864d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      Dest1->removePredecessor(BI->getParent());
884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
898f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      // Replace the conditional branch with an unconditional one.
908f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      BranchInst::Create(Dest1, BI);
918f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad      BI->eraseFromParent();
924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
940a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    return false;
950a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner  }
960a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
970a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner  if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
9810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // If we are switching on a constant, we can convert the switch into a
9910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // single branch instruction!
10010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
10110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    BasicBlock *TheOnlyDest = SI->getSuccessor(0);  // The default dest
1027d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner    BasicBlock *DefaultDest = TheOnlyDest;
1037d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner    assert(TheOnlyDest == SI->getDefaultDest() &&
1047d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner           "Default destination is not successor #0?");
10510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
1060a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    // Figure out which case it goes to.
10710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
10810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Found case matching a constant operand?
10910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      if (SI->getSuccessorValue(i) == CI) {
11010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        TheOnlyDest = SI->getSuccessor(i);
11110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        break;
11210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      }
11310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
1147d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      // Check to see if this branch is going to the same place as the default
1157d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      // dest.  If so, eliminate it as an explicit compare.
1167d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      if (SI->getSuccessor(i) == DefaultDest) {
1170a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner        // Remove this entry.
1187d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        DefaultDest->removePredecessor(SI->getParent());
1197d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        SI->removeCase(i);
1207d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        --i; --e;  // Don't skip an entry...
1217d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        continue;
1227d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      }
1237d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner
12410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Otherwise, check to see if the switch only branches to one destination.
12510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // We do this by reseting "TheOnlyDest" to null when we find two non-equal
12610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // destinations.
12710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
12810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    }
129694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner
13010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    if (CI && !TheOnlyDest) {
13110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Branching on a constant, but not any of the cases, go to the default
13210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // successor.
13310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      TheOnlyDest = SI->getDefaultDest();
134694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner    }
135694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner
13610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // If we found a single destination that we can fold the switch into, do so
13710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // now.
13810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    if (TheOnlyDest) {
1390a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // Insert the new branch.
140051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(TheOnlyDest, SI);
14110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      BasicBlock *BB = SI->getParent();
14210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
14310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Remove entries from PHI nodes which we no longer branch to...
14410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
14510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        // Found case matching a constant operand?
14610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        BasicBlock *Succ = SI->getSuccessor(i);
14710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        if (Succ == TheOnlyDest)
14810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner          TheOnlyDest = 0;  // Don't modify the first branch to TheOnlyDest
14910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        else
15010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner          Succ->removePredecessor(BB);
15110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      }
15210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
1530a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // Delete the old switch.
15410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      BB->getInstList().erase(SI);
15510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      return true;
1560a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    }
1570a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
1580a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    if (SI->getNumSuccessors() == 2) {
15910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Otherwise, we can fold this switch into a conditional branch
16010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // instruction if it has only one non-default destination.
161333c40096561218bc3597cf153c0a3895274414cOwen Anderson      Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(),
162333c40096561218bc3597cf153c0a3895274414cOwen Anderson                                 SI->getSuccessorValue(1), "cond");
1630a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // Insert the new branch.
164051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
16510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
1660a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // Delete the old switch.
1671adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman      SI->eraseFromParent();
16810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      return true;
16910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    }
1700a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    return false;
1710a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner  }
1720a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
1730a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner  if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
1740a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    // indirectbr blockaddress(@F, @BB) -> br label @BB
1750a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    if (BlockAddress *BA =
1760a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner          dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
1770a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      BasicBlock *TheOnlyDest = BA->getBasicBlock();
1780a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // Insert the new branch.
1790a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      BranchInst::Create(TheOnlyDest, IBI);
1800a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
1810a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
1820a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner        if (IBI->getDestination(i) == TheOnlyDest)
1830a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner          TheOnlyDest = 0;
1840a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner        else
1850a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner          IBI->getDestination(i)->removePredecessor(IBI->getParent());
1860a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      }
1870a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      IBI->eraseFromParent();
1880a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
1890a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // If we didn't find our destination in the IBI successor list, then we
1900a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // have undefined behavior.  Replace the unconditional branch with an
1910a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      // 'unreachable' instruction.
1920a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      if (TheOnlyDest) {
1930a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner        BB->getTerminator()->eraseFromParent();
1940a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner        new UnreachableInst(BB->getContext(), BB);
1950a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      }
1960a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
1970a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner      return true;
1980a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner    }
1994d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
2000a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner
2014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
2024d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
2034d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
2044d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
2054d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
20640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner//  Local dead code elimination.
2074d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
2084d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
2093481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the
2103481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// instruction is not used, and the instruction has no side effects.
2113481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner///
212abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::isInstructionTriviallyDead(Instruction *I) {
213ec710c5b12af647ae90f53917122726269c18738Chris Lattner  if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
21400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
2159c5822a966572ea78f4e818870d4229c7a855749Devang Patel  // We don't want debug info removed by anything this general, unless
2169c5822a966572ea78f4e818870d4229c7a855749Devang Patel  // debug info is empty.
2179c5822a966572ea78f4e818870d4229c7a855749Devang Patel  if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
2189c5822a966572ea78f4e818870d4229c7a855749Devang Patel    if (DDI->getAddress())
2199c5822a966572ea78f4e818870d4229c7a855749Devang Patel      return false;
220b99462117ebd4be41788346246d7935fc90a11eeDevang Patel    return true;
221b99462117ebd4be41788346246d7935fc90a11eeDevang Patel  }
222b99462117ebd4be41788346246d7935fc90a11eeDevang Patel  if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
2239c5822a966572ea78f4e818870d4229c7a855749Devang Patel    if (DVI->getValue())
2249c5822a966572ea78f4e818870d4229c7a855749Devang Patel      return false;
225b99462117ebd4be41788346246d7935fc90a11eeDevang Patel    return true;
2269c5822a966572ea78f4e818870d4229c7a855749Devang Patel  }
2279c5822a966572ea78f4e818870d4229c7a855749Devang Patel
2287af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  if (!I->mayHaveSideEffects()) return true;
2297af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands
2307af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  // Special case intrinsics that "may have side effects" but can be deleted
2317af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  // when dead.
232741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
233741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner    // Safe to delete llvm.stacksave if dead.
234741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner    if (II->getIntrinsicID() == Intrinsic::stacksave)
235741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner      return true;
236ec710c5b12af647ae90f53917122726269c18738Chris Lattner  return false;
2374d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
2384d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
2393481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
2403481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead instruction, delete it.  If that makes any of its operands
24190fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// trivially dead, delete them too, recursively.  Return true if any
24290fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// instructions were deleted.
24390fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) {
2443481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner  Instruction *I = dyn_cast<Instruction>(V);
2457605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner  if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
24690fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman    return false;
2473481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner
2487605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner  SmallVector<Instruction*, 16> DeadInsts;
2497605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner  DeadInsts.push_back(I);
2503481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner
251321a813c536e2f1f2f05bbe78a7fbf64046f0557Dan Gohman  do {
252e9d87f49063cb1bd213d8e9c339b9b63393cc2d9Dan Gohman    I = DeadInsts.pop_back_val();
2532872177834d83b42cd042a37299cb7089965f36bChris Lattner
2547605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner    // Null out all of the instruction's operands to see if any operand becomes
2557605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner    // dead as we go.
2567605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner    for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
2577605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      Value *OpV = I->getOperand(i);
2587605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      I->setOperand(i, 0);
2597605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner
2607605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      if (!OpV->use_empty()) continue;
2617605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner
2627605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      // If the operand is an instruction that became dead as we nulled out the
2637605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      // operand, and if it is 'trivially' dead, delete it in a future loop
2647605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      // iteration.
2657605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner      if (Instruction *OpI = dyn_cast<Instruction>(OpV))
2667605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner        if (isInstructionTriviallyDead(OpI))
2677605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner          DeadInsts.push_back(OpI);
2687605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner    }
2697605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner
2707605730ba8eaf248a8285bb2055e131f13c15b63Chris Lattner    I->eraseFromParent();
271321a813c536e2f1f2f05bbe78a7fbf64046f0557Dan Gohman  } while (!DeadInsts.empty());
27290fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman
27390fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman  return true;
2744d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
275b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
2761a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky/// areAllUsesEqual - Check whether the uses of a value are all the same.
2771a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky/// This is similar to Instruction::hasOneUse() except this will also return
278b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands/// true when there are no uses or multiple uses that all refer to the same
279b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands/// value.
2801a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewyckystatic bool areAllUsesEqual(Instruction *I) {
2811a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  Value::use_iterator UI = I->use_begin();
2821a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  Value::use_iterator UE = I->use_end();
2831a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  if (UI == UE)
284b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands    return true;
2851a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky
2861a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  User *TheUse = *UI;
2871a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  for (++UI; UI != UE; ++UI) {
2881a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky    if (*UI != TheUse)
2891a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky      return false;
2901a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  }
2911a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky  return true;
2921a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky}
2931a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewycky
294afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
295afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// dead PHI node, due to being a def-use chain of single-use nodes that
296afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// either forms a cycle or is terminated by a trivially dead instruction,
297afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// delete it.  If that makes any of its operands trivially dead, delete them
2982cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands/// too, recursively.  Return true if a change was made.
2991a4021a2be4a59e9f9010776cb6f72107241aeb5Nick Lewyckybool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
300b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands  SmallPtrSet<Instruction*, 4> Visited;
301b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands  for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
302b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands       I = cast<Instruction>(*I->use_begin())) {
303b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands    if (I->use_empty())
304b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands      return RecursivelyDeleteTriviallyDeadInstructions(I);
305afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman
306b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands    // If we find an instruction more than once, we're on a cycle that
307afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman    // won't prove fruitful.
308b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands    if (!Visited.insert(I)) {
309b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands      // Break the cycle and delete the instruction and its operands.
310b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands      I->replaceAllUsesWith(UndefValue::get(I->getType()));
3112cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands      (void)RecursivelyDeleteTriviallyDeadInstructions(I);
3122cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands      return true;
313b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands    }
314b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands  }
315b4098ba03d51a3845bde5aeb4fca893d1a90d3f8Duncan Sands  return false;
316afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman}
3173481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner
318e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// SimplifyInstructionsInBlock - Scan the specified basic block and try to
319e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// simplify any instructions in it and recursively delete dead instructions.
320e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner///
321e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// This returns true if it changed the code, note that it can delete
322e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// instructions in other blocks as well in this block.
323e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattnerbool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD) {
324e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner  bool MadeChange = false;
325e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner  for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
326e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner    Instruction *Inst = BI++;
327e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner
328e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner    if (Value *V = SimplifyInstruction(Inst, TD)) {
329e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner      WeakVH BIHandle(BI);
330e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner      ReplaceAndSimplifyAllUses(Inst, V, TD);
331e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner      MadeChange = true;
33235a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner      if (BIHandle != BI)
333e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner        BI = BB->begin();
334e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner      continue;
335e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner    }
33671ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman
337eeb7f4ce2dca47453d5e39448a4aa61c1c7d72d1Eli Friedman    if (Inst->isTerminator())
338eeb7f4ce2dca47453d5e39448a4aa61c1c7d72d1Eli Friedman      break;
339eeb7f4ce2dca47453d5e39448a4aa61c1c7d72d1Eli Friedman
34071ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman    WeakVH BIHandle(BI);
341e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner    MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst);
34271ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman    if (BIHandle != BI)
34371ad2c9eda710bc26ec1621a9afefad11dd7fad2Eli Friedman      BI = BB->begin();
344e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner  }
345e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner  return MadeChange;
346e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner}
347e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner
348b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner//===----------------------------------------------------------------------===//
34940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner//  Control Flow Graph Restructuring.
350b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner//
351b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
35240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
35340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
35440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// method is called when we're about to delete Pred as a predecessor of BB.  If
35540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
35640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner///
35740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// Unlike the removePredecessor method, this attempts to simplify uses of PHI
35840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// nodes that collapse into identity values.  For example, if we have:
35940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner///   x = phi(1, 0, 0, 0)
36040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner///   y = and x, z
36140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner///
36240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// .. and delete the predecessor corresponding to the '1', this will attempt to
36340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// recursively fold the and to 0.
36440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattnervoid llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
36540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner                                        TargetData *TD) {
36640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  // This only adjusts blocks with PHI nodes.
36740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  if (!isa<PHINode>(BB->begin()))
36840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    return;
36940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
37040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
37140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  // them down.  This will leave us with single entry phi nodes and other phis
37240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  // that can be removed.
37340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  BB->removePredecessor(Pred, true);
37440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
37540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  WeakVH PhiIt = &BB->front();
37640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
37740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
3786ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands
3796ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands    Value *PNV = SimplifyInstruction(PN, TD);
38040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    if (PNV == 0) continue;
3816ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands
38240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    // If we're able to simplify the phi to a single value, substitute the new
38340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    // value into all of its uses.
3846ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands    assert(PNV != PN && "SimplifyInstruction broken!");
38540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
38635a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner    Value *OldPhiIt = PhiIt;
38740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    ReplaceAndSimplifyAllUses(PN, PNV, TD);
38840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
38940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    // If recursive simplification ended up deleting the next PHI node we would
39040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    // iterate to, then our iterator is invalid, restart scanning from the top
39140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner    // of the block.
39235a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner    if (PhiIt != OldPhiIt) PhiIt = &BB->front();
39340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner  }
39440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner}
39540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
39640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner
397b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
398b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (DestBB!).  Eliminate the edge
399b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into DestBB and
400b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deleting the predecessor block.
401b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner///
402ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustiftervoid llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
403b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  // If BB has single-entry PHI nodes, fold them.
404b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
405b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner    Value *NewVal = PN->getIncomingValue(0);
406b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner    // Replace self referencing PHI with undef, it must be dead.
4079e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
408b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner    PN->replaceAllUsesWith(NewVal);
409b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner    PN->eraseFromParent();
410b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  }
411b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
412b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  BasicBlock *PredBB = DestBB->getSinglePredecessor();
413b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  assert(PredBB && "Block doesn't have a single predecessor!");
414b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
415b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  // Splice all the instructions from PredBB to DestBB.
416b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  PredBB->getTerminator()->eraseFromParent();
417b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
41837914c8e83c43d710925263b66014159f03fa355Chris Lattner
41937914c8e83c43d710925263b66014159f03fa355Chris Lattner  // Zap anything that took the address of DestBB.  Not doing this will give the
42037914c8e83c43d710925263b66014159f03fa355Chris Lattner  // address an invalid value.
42137914c8e83c43d710925263b66014159f03fa355Chris Lattner  if (DestBB->hasAddressTaken()) {
42237914c8e83c43d710925263b66014159f03fa355Chris Lattner    BlockAddress *BA = BlockAddress::get(DestBB);
42337914c8e83c43d710925263b66014159f03fa355Chris Lattner    Constant *Replacement =
42437914c8e83c43d710925263b66014159f03fa355Chris Lattner      ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1);
42537914c8e83c43d710925263b66014159f03fa355Chris Lattner    BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
42637914c8e83c43d710925263b66014159f03fa355Chris Lattner                                                     BA->getType()));
42737914c8e83c43d710925263b66014159f03fa355Chris Lattner    BA->destroyConstant();
42837914c8e83c43d710925263b66014159f03fa355Chris Lattner  }
429b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
430b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  // Anything that branched to PredBB now branches to DestBB.
431b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  PredBB->replaceAllUsesWith(DestBB);
432b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner
433ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter  if (P) {
43480f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich    DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
43580f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich    if (DT) {
43680f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich      BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock();
43780f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich      DT->changeImmediateDominator(DestBB, PredBBIDom);
43880f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich      DT->eraseNode(PredBB);
43980f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich    }
440ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter    ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
441ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter    if (PI) {
442ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter      PI->replaceAllUses(PredBB, DestBB);
443ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter      PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB));
444ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter    }
445ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter  }
446b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  // Nuke BB.
447b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner  PredBB->eraseFromParent();
448b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner}
4494afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel
450dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
451dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// almost-empty BB ending in an unconditional branch to Succ, into succ.
452dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner///
453dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// Assumption: Succ is the single successor for BB.
454dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner///
455dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerstatic bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
456dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
457dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
458fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene  DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
459dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        << Succ->getName() << "\n");
460dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Shortcut, if there is only a single predecessor it must be BB and merging
461dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // is always safe
462dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (Succ->getSinglePredecessor()) return true;
463dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
464dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Make a list of the predecessors of BB
465dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  typedef SmallPtrSet<BasicBlock*, 16> BlockSet;
466dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  BlockSet BBPreds(pred_begin(BB), pred_end(BB));
467dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
468dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Use that list to make another list of common predecessors of BB and Succ
469dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  BlockSet CommonPreds;
470dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
471f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif       PI != PE; ++PI) {
472f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif    BasicBlock *P = *PI;
473f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif    if (BBPreds.count(P))
474f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif      CommonPreds.insert(P);
475f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif  }
476dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
477dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Shortcut, if there are no common predecessors, merging is always safe
478dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (CommonPreds.empty())
479dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    return true;
480dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
481dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Look at all the phi nodes in Succ, to see if they present a conflict when
482dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // merging these blocks
483dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
484dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    PHINode *PN = cast<PHINode>(I);
485dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
486dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // If the incoming value from BB is again a PHINode in
487dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // BB which has the same incoming value for *PI as PN does, we can
488dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // merge the phi nodes and then the blocks can still be merged
489dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
490dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    if (BBPN && BBPN->getParent() == BB) {
491dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
492dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner            PI != PE; PI++) {
493dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        if (BBPN->getIncomingValueForBlock(*PI)
494dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner              != PN->getIncomingValueForBlock(*PI)) {
495fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene          DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
496dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                << Succ->getName() << " is conflicting with "
497dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                << BBPN->getName() << " with regard to common predecessor "
498dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                << (*PI)->getName() << "\n");
499dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          return false;
500dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        }
501dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      }
502dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    } else {
503dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      Value* Val = PN->getIncomingValueForBlock(BB);
504dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
505dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner            PI != PE; PI++) {
506dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        // See if the incoming value for the common predecessor is equal to the
507dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        // one for BB, in which case this phi node will not prevent the merging
508dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        // of the block.
509dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        if (Val != PN->getIncomingValueForBlock(*PI)) {
510fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene          DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
511dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                << Succ->getName() << " is conflicting with regard to common "
512dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                << "predecessor " << (*PI)->getName() << "\n");
513dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          return false;
514dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        }
515dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      }
516dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    }
517dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  }
518dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
519dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  return true;
520dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner}
521dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
522dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
523dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// unconditional branch, and contains no instructions other than PHI nodes,
524dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// potential debug intrinsics and the branch.  If possible, eliminate BB by
525dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// rewriting all the predecessors to branch to the successor block and return
526dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// true.  If we can't transform, return false.
527dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerbool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
528e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman  assert(BB != &BB->getParent()->getEntryBlock() &&
529e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman         "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
530e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman
531dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // We can't eliminate infinite loops.
532dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
533dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (BB == Succ) return false;
534dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
535dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Check to see if merging these blocks would cause conflicts for any of the
536dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // phi nodes in BB or Succ. If not, we can safely merge.
537dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
538dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
539dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Check for cases where Succ has multiple predecessors and a PHI node in BB
540dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // has uses which will not disappear when the PHI nodes are merged.  It is
541dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // possible to handle such cases, but difficult: it requires checking whether
542dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // BB dominates Succ, which is non-trivial to calculate in the case where
543dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Succ has multiple predecessors.  Also, it requires checking whether
544dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // constructing the necessary self-referential PHI node doesn't intoduce any
545dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // conflicts; this isn't too difficult, but the previous code for doing this
546dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // was incorrect.
547dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  //
548dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Note that if this check finds a live use, BB dominates Succ, so BB is
549dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // something like a loop pre-header (or rarely, a part of an irreducible CFG);
550dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // folding the branch isn't profitable in that case anyway.
551dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (!Succ->getSinglePredecessor()) {
552dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    BasicBlock::iterator BBI = BB->begin();
553dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    while (isa<PHINode>(*BBI)) {
554dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
555dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner           UI != E; ++UI) {
556dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        if (PHINode* PN = dyn_cast<PHINode>(*UI)) {
557dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          if (PN->getIncomingBlock(UI) != BB)
558dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner            return false;
559dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        } else {
560dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          return false;
561dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        }
562dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      }
563dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      ++BBI;
564dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    }
565dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  }
566dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
567fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene  DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
568dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
569dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (isa<PHINode>(Succ->begin())) {
570dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // If there is more than one pred of succ, and there are PHI nodes in
571dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // the successor, then we need to add incoming edges for the PHI nodes
572dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    //
573dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
574dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
575dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    // Loop over all of the PHI nodes in the successor of BB.
576dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
577dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      PHINode *PN = cast<PHINode>(I);
578dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      Value *OldVal = PN->removeIncomingValue(BB, false);
579dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      assert(OldVal && "No entry in PHI for Pred BB!");
580dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
581dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      // If this incoming value is one of the PHI nodes in BB, the new entries
582dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      // in the PHI node are the entries from the old PHI.
583dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
584dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        PHINode *OldValPN = cast<PHINode>(OldVal);
585dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
586dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          // Note that, since we are merging phi nodes and BB and Succ might
587dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          // have common predecessors, we could end up with a phi node with
588dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          // identical incoming branches. This will be cleaned up later (and
589dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          // will trigger asserts if we try to clean it up now, without also
590dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          // simplifying the corresponding conditional branch).
591dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          PN->addIncoming(OldValPN->getIncomingValue(i),
592dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                          OldValPN->getIncomingBlock(i));
593dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      } else {
594dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        // Add an incoming value for each of the new incoming values.
595dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner        for (unsigned i = 0, e = BBPreds.size(); i != e; ++i)
596dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner          PN->addIncoming(OldVal, BBPreds[i]);
597dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      }
598dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    }
599dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  }
600dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
601dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
602dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    if (Succ->getSinglePredecessor()) {
603dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      // BB is the only predecessor of Succ, so Succ will end up with exactly
604dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      // the same predecessors BB had.
605dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      Succ->getInstList().splice(Succ->begin(),
606dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner                                 BB->getInstList(), BB->begin());
607dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    } else {
608dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
609dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      assert(PN->use_empty() && "There shouldn't be any uses here!");
610dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner      PN->eraseFromParent();
611dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner    }
612dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  }
613dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
614dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  // Everything that jumped to BB now goes to Succ.
615dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  BB->replaceAllUsesWith(Succ);
616dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  if (!Succ->hasName()) Succ->takeName(BB);
617dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  BB->eraseFromParent();              // Delete the old basic block.
618dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner  return true;
619dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner}
620dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner
62143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
62243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// nodes in this block. This doesn't try to be clever about PHI nodes
62343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// which differ only in the order of the incoming values, but instcombine
62443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// orders them so it usually won't matter.
62543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach///
62643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbachbool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
62743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  bool Changed = false;
62843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach
62943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // This implementation doesn't currently consider undef operands
63043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // specially. Theroetically, two phis which are identical except for
63143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // one having an undef where the other doesn't could be collapsed.
63243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach
63343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // Map from PHI hash values to PHI nodes. If multiple PHIs have
63443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // the same hash value, the element is the first PHI in the
63543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // linked list in CollisionMap.
63643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  DenseMap<uintptr_t, PHINode *> HashMap;
63743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach
63843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // Maintain linked lists of PHI nodes with common hash values.
63943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  DenseMap<PHINode *, PHINode *> CollisionMap;
64043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach
64143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  // Examine each PHI.
64243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  for (BasicBlock::iterator I = BB->begin();
64343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach       PHINode *PN = dyn_cast<PHINode>(I++); ) {
64443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    // Compute a hash value on the operands. Instcombine will likely have sorted
64543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    // them, which helps expose duplicates, but we have to check all the
64643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    // operands to be safe in case instcombine hasn't run.
64743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    uintptr_t Hash = 0;
64843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
64943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      // This hash algorithm is quite weak as hash functions go, but it seems
65043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      // to do a good enough job for this particular purpose, and is very quick.
65143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
65243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
65343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    }
6542bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen    // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
6552bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen    Hash >>= 1;
65643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    // If we've never seen this hash value before, it's a unique PHI.
65743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
65843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      HashMap.insert(std::make_pair(Hash, PN));
65943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    if (Pair.second) continue;
66043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    // Otherwise it's either a duplicate or a hash collision.
66143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    for (PHINode *OtherPN = Pair.first->second; ; ) {
66243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      if (OtherPN->isIdenticalTo(PN)) {
66343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        // A duplicate. Replace this PHI with its duplicate.
66443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        PN->replaceAllUsesWith(OtherPN);
66543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        PN->eraseFromParent();
66643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        Changed = true;
66743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        break;
66843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      }
66943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      // A non-duplicate hash collision.
67043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
67143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      if (I == CollisionMap.end()) {
67243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        // Set this PHI to be the head of the linked list of colliding PHIs.
67343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        PHINode *Old = Pair.first->second;
67443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        Pair.first->second = PN;
67543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        CollisionMap[PN] = Old;
67643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach        break;
67743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      }
67843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      // Procede to the next PHI in the list.
67943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach      OtherPN = I->second;
68043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach    }
68143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  }
68243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach
68343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach  return Changed;
68443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach}
685687140c818ba4b896329a83324714140b6580ef8Chris Lattner
686687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// enforceKnownAlignment - If the specified pointer points to an object that
687687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we control, modify the object's alignment to PrefAlign. This isn't
688687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// often possible though. If alignment is important, a more reliable approach
689687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// is to simply align all global variables and allocation instructions to
690687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// their preferred alignment from the beginning.
691687140c818ba4b896329a83324714140b6580ef8Chris Lattner///
69219282365147f498a60463d250dbed2f8e1b81861Benjamin Kramerstatic unsigned enforceKnownAlignment(Value *V, unsigned Align,
69319282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer                                      unsigned PrefAlign) {
694687140c818ba4b896329a83324714140b6580ef8Chris Lattner
695687140c818ba4b896329a83324714140b6580ef8Chris Lattner  User *U = dyn_cast<User>(V);
696687140c818ba4b896329a83324714140b6580ef8Chris Lattner  if (!U) return Align;
697687140c818ba4b896329a83324714140b6580ef8Chris Lattner
698687140c818ba4b896329a83324714140b6580ef8Chris Lattner  switch (Operator::getOpcode(U)) {
699687140c818ba4b896329a83324714140b6580ef8Chris Lattner  default: break;
700687140c818ba4b896329a83324714140b6580ef8Chris Lattner  case Instruction::BitCast:
701687140c818ba4b896329a83324714140b6580ef8Chris Lattner    return enforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
702687140c818ba4b896329a83324714140b6580ef8Chris Lattner  case Instruction::GetElementPtr: {
703687140c818ba4b896329a83324714140b6580ef8Chris Lattner    // If all indexes are zero, it is just the alignment of the base pointer.
704687140c818ba4b896329a83324714140b6580ef8Chris Lattner    bool AllZeroOperands = true;
705687140c818ba4b896329a83324714140b6580ef8Chris Lattner    for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
706687140c818ba4b896329a83324714140b6580ef8Chris Lattner      if (!isa<Constant>(*i) ||
707687140c818ba4b896329a83324714140b6580ef8Chris Lattner          !cast<Constant>(*i)->isNullValue()) {
708687140c818ba4b896329a83324714140b6580ef8Chris Lattner        AllZeroOperands = false;
709687140c818ba4b896329a83324714140b6580ef8Chris Lattner        break;
710687140c818ba4b896329a83324714140b6580ef8Chris Lattner      }
711687140c818ba4b896329a83324714140b6580ef8Chris Lattner
712687140c818ba4b896329a83324714140b6580ef8Chris Lattner    if (AllZeroOperands) {
713687140c818ba4b896329a83324714140b6580ef8Chris Lattner      // Treat this like a bitcast.
714687140c818ba4b896329a83324714140b6580ef8Chris Lattner      return enforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
715687140c818ba4b896329a83324714140b6580ef8Chris Lattner    }
716687140c818ba4b896329a83324714140b6580ef8Chris Lattner    return Align;
717687140c818ba4b896329a83324714140b6580ef8Chris Lattner  }
718687140c818ba4b896329a83324714140b6580ef8Chris Lattner  case Instruction::Alloca: {
719687140c818ba4b896329a83324714140b6580ef8Chris Lattner    AllocaInst *AI = cast<AllocaInst>(V);
720687140c818ba4b896329a83324714140b6580ef8Chris Lattner    // If there is a requested alignment and if this is an alloca, round up.
721687140c818ba4b896329a83324714140b6580ef8Chris Lattner    if (AI->getAlignment() >= PrefAlign)
722687140c818ba4b896329a83324714140b6580ef8Chris Lattner      return AI->getAlignment();
723687140c818ba4b896329a83324714140b6580ef8Chris Lattner    AI->setAlignment(PrefAlign);
724687140c818ba4b896329a83324714140b6580ef8Chris Lattner    return PrefAlign;
725687140c818ba4b896329a83324714140b6580ef8Chris Lattner  }
726687140c818ba4b896329a83324714140b6580ef8Chris Lattner  }
727687140c818ba4b896329a83324714140b6580ef8Chris Lattner
728687140c818ba4b896329a83324714140b6580ef8Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
729687140c818ba4b896329a83324714140b6580ef8Chris Lattner    // If there is a large requested alignment and we can, bump up the alignment
730687140c818ba4b896329a83324714140b6580ef8Chris Lattner    // of the global.
731687140c818ba4b896329a83324714140b6580ef8Chris Lattner    if (GV->isDeclaration()) 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)
771687140c818ba4b896329a83324714140b6580ef8Chris Lattner    Align = enforceKnownAlignment(V, Align, PrefAlign);
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
7895ee20680c7ebc765950983633e19fafab5235245Devang Patel  Instruction *DbgVal =
7905ee20680c7ebc765950983633e19fafab5235245Devang Patel    Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0,
7915ee20680c7ebc765950983633e19fafab5235245Devang Patel                                    DIVar, SI);
7925ee20680c7ebc765950983633e19fafab5235245Devang Patel
7935ee20680c7ebc765950983633e19fafab5235245Devang Patel  // Propagate any debug metadata from the store onto the dbg.value.
7945ee20680c7ebc765950983633e19fafab5235245Devang Patel  DebugLoc SIDL = SI->getDebugLoc();
7955ee20680c7ebc765950983633e19fafab5235245Devang Patel  if (!SIDL.isUnknown())
7965ee20680c7ebc765950983633e19fafab5235245Devang Patel    DbgVal->setDebugLoc(SIDL);
7975ee20680c7ebc765950983633e19fafab5235245Devang Patel  // Otherwise propagate debug metadata from dbg.declare.
7985ee20680c7ebc765950983633e19fafab5235245Devang Patel  else
7995ee20680c7ebc765950983633e19fafab5235245Devang Patel    DbgVal->setDebugLoc(DDI->getDebugLoc());
8005ee20680c7ebc765950983633e19fafab5235245Devang Patel  return true;
8015ee20680c7ebc765950983633e19fafab5235245Devang Patel}
8025ee20680c7ebc765950983633e19fafab5235245Devang Patel
80336fae67831517f132255118b45b21a8cf199a012Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value
80436fae67831517f132255118b45b21a8cf199a012Devang Patel/// that has an associated llvm.dbg.decl intrinsic.
80536fae67831517f132255118b45b21a8cf199a012Devang Patelbool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
80636fae67831517f132255118b45b21a8cf199a012Devang Patel                                           LoadInst *LI, DIBuilder &Builder) {
80736fae67831517f132255118b45b21a8cf199a012Devang Patel  DIVariable DIVar(DDI->getVariable());
80836fae67831517f132255118b45b21a8cf199a012Devang Patel  if (!DIVar.Verify())
80936fae67831517f132255118b45b21a8cf199a012Devang Patel    return false;
81036fae67831517f132255118b45b21a8cf199a012Devang Patel
81136fae67831517f132255118b45b21a8cf199a012Devang Patel  Instruction *DbgVal =
81236fae67831517f132255118b45b21a8cf199a012Devang Patel    Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0,
81336fae67831517f132255118b45b21a8cf199a012Devang Patel                                    DIVar, LI);
81436fae67831517f132255118b45b21a8cf199a012Devang Patel
81536fae67831517f132255118b45b21a8cf199a012Devang Patel  // Propagate any debug metadata from the store onto the dbg.value.
81636fae67831517f132255118b45b21a8cf199a012Devang Patel  DebugLoc LIDL = LI->getDebugLoc();
81736fae67831517f132255118b45b21a8cf199a012Devang Patel  if (!LIDL.isUnknown())
81836fae67831517f132255118b45b21a8cf199a012Devang Patel    DbgVal->setDebugLoc(LIDL);
81936fae67831517f132255118b45b21a8cf199a012Devang Patel  // Otherwise propagate debug metadata from dbg.declare.
82036fae67831517f132255118b45b21a8cf199a012Devang Patel  else
82136fae67831517f132255118b45b21a8cf199a012Devang Patel    DbgVal->setDebugLoc(DDI->getDebugLoc());
82236fae67831517f132255118b45b21a8cf199a012Devang Patel  return true;
82336fae67831517f132255118b45b21a8cf199a012Devang Patel}
82436fae67831517f132255118b45b21a8cf199a012Devang Patel
825813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
826813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// of llvm.dbg.value intrinsics.
827813c9a0f19c0d27085a3ea81eb44033747007741Devang Patelbool llvm::LowerDbgDeclare(Function &F) {
828813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  DIBuilder DIB(*F.getParent());
829813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  SmallVector<DbgDeclareInst *, 4> Dbgs;
830813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
831813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) {
832813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel      if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
833813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel        Dbgs.push_back(DDI);
834813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    }
835813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  if (Dbgs.empty())
836813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    return false;
837813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel
838813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  for (SmallVector<DbgDeclareInst *, 4>::iterator I = Dbgs.begin(),
839813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel         E = Dbgs.end(); I != E; ++I) {
840813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    DbgDeclareInst *DDI = *I;
841813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    if (AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress())) {
842813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel      for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
843813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel           UI != E; ++UI)
844813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel        if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
845813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel          ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
84636fae67831517f132255118b45b21a8cf199a012Devang Patel        else if (LoadInst *LI = dyn_cast<LoadInst>(*UI))
84736fae67831517f132255118b45b21a8cf199a012Devang Patel          ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
848813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    }
849813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel    DDI->eraseFromParent();
850813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  }
851813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel  return true;
852813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel}
853