Local.cpp revision 36fae67831517f132255118b45b21a8cf199a012
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" 2319f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/ADT/DenseMap.h" 24afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman#include "llvm/ADT/SmallPtrSet.h" 255ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DebugInfo.h" 265ee20680c7ebc765950983633e19fafab5235245Devang Patel#include "llvm/Analysis/DIBuilder.h" 2780f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich#include "llvm/Analysis/Dominators.h" 28cbbc6b74e357afbf8fb37fdeb177ed78021092d3Chris Lattner#include "llvm/Analysis/ConstantFolding.h" 2940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner#include "llvm/Analysis/InstructionSimplify.h" 30ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter#include "llvm/Analysis/ProfileInfo.h" 31687140c818ba4b896329a83324714140b6580ef8Chris Lattner#include "llvm/Analysis/ValueTracking.h" 329fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner#include "llvm/Target/TargetData.h" 33dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/CFG.h" 34dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/Debug.h" 35c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/GetElementPtrTypeIterator.h" 36c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/MathExtras.h" 3719f2dc436df4f768484287a478973e83efd4202aChris Lattner#include "llvm/Support/ValueHandle.h" 38dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner#include "llvm/Support/raw_ostream.h" 39abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerusing namespace llvm; 40d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 414d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===// 423481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local constant propagation. 434d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 454d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldTerminator - If a terminator instruction is predicated on a 464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// constant value, convert it into an unconditional branch to the constant 474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// destination. 484d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 49abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::ConstantFoldTerminator(BasicBlock *BB) { 5076ae3445f81164aaff9f95123426109c119f27c0Chris Lattner TerminatorInst *T = BB->getTerminator(); 51fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 524d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Branch - See if we are conditional jumping on constant 534d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner if (BranchInst *BI = dyn_cast<BranchInst>(T)) { 544d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner if (BI->isUnconditional()) return false; // Can't optimize uncond branch 55c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif BasicBlock *Dest1 = BI->getSuccessor(0); 56c1bb13f1b8794aa6f3219b3ac567f569ad78a6d1Gabor Greif BasicBlock *Dest2 = BI->getSuccessor(1); 574d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 586b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { 594d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Are we branching on constant? 604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // YES. Change to unconditional branch... 61579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; 62579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; 634d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 64fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman //cerr << "Function: " << T->getParent()->getParent() 65fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman // << "\nRemoving branch from " << T->getParent() 664d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // << "\n\nTo: " << OldDest << endl; 674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 684d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Let the basic block know that we are letting go of it. Based on this, 694d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // it will adjust it's PHI nodes. 704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner assert(BI->getParent() && "Terminator not inserted in block!"); 714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner OldDest->removePredecessor(BI->getParent()); 724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 738f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad // Replace the conditional branch with an unconditional one. 748f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BranchInst::Create(Destination, BI); 758f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BI->eraseFromParent(); 764d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return true; 770a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 780a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 790a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (Dest2 == Dest1) { // Conditional branch to same location? 80fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman // This branch matches something like this: 814d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // br bool %cond, label %Dest, label %Dest 824d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // and changes it into: br label %Dest 834d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 844d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner // Let the basic block know that we are letting go of one copy of it. 854d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner assert(BI->getParent() && "Terminator not inserted in block!"); 864d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner Dest1->removePredecessor(BI->getParent()); 874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 888f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad // Replace the conditional branch with an unconditional one. 898f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BranchInst::Create(Dest1, BI); 908f9ffbd056172da470bcd3a9f1d5b4c2414fce59Jay Foad BI->eraseFromParent(); 914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return true; 924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner } 930a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return false; 940a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 950a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 960a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { 9710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // If we are switching on a constant, we can convert the switch into a 9810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // single branch instruction! 9910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); 10010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest 1017d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner BasicBlock *DefaultDest = TheOnlyDest; 1027d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner assert(TheOnlyDest == SI->getDefaultDest() && 1037d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner "Default destination is not successor #0?"); 10410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1050a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Figure out which case it goes to. 10610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { 10710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Found case matching a constant operand? 10810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (SI->getSuccessorValue(i) == CI) { 10910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner TheOnlyDest = SI->getSuccessor(i); 11010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner break; 11110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 11210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1137d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner // Check to see if this branch is going to the same place as the default 1147d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner // dest. If so, eliminate it as an explicit compare. 1157d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner if (SI->getSuccessor(i) == DefaultDest) { 1160a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Remove this entry. 1177d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner DefaultDest->removePredecessor(SI->getParent()); 1187d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner SI->removeCase(i); 1197d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner --i; --e; // Don't skip an entry... 1207d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner continue; 1217d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner } 1227d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner 12310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Otherwise, check to see if the switch only branches to one destination. 12410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // We do this by reseting "TheOnlyDest" to null when we find two non-equal 12510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // destinations. 12610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; 12710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 128694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner 12910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (CI && !TheOnlyDest) { 13010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Branching on a constant, but not any of the cases, go to the default 13110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // successor. 13210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner TheOnlyDest = SI->getDefaultDest(); 133694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner } 134694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner 13510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // If we found a single destination that we can fold the switch into, do so 13610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // now. 13710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (TheOnlyDest) { 1380a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 139051a950000e21935165db56695e35bade668193bGabor Greif BranchInst::Create(TheOnlyDest, SI); 14010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BasicBlock *BB = SI->getParent(); 14110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 14210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Remove entries from PHI nodes which we no longer branch to... 14310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { 14410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Found case matching a constant operand? 14510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BasicBlock *Succ = SI->getSuccessor(i); 14610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner if (Succ == TheOnlyDest) 14710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest 14810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner else 14910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner Succ->removePredecessor(BB); 15010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 15110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1520a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Delete the old switch. 15310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner BB->getInstList().erase(SI); 15410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner return true; 1550a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1560a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1570a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (SI->getNumSuccessors() == 2) { 15810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // Otherwise, we can fold this switch into a conditional branch 15910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner // instruction if it has only one non-default destination. 160333c40096561218bc3597cf153c0a3895274414cOwen Anderson Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(), 161333c40096561218bc3597cf153c0a3895274414cOwen Anderson SI->getSuccessorValue(1), "cond"); 1620a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 163051a950000e21935165db56695e35bade668193bGabor Greif BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); 16410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner 1650a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Delete the old switch. 1661adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman SI->eraseFromParent(); 16710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner return true; 16810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner } 1690a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return false; 1700a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1710a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1720a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) { 1730a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // indirectbr blockaddress(@F, @BB) -> br label @BB 1740a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (BlockAddress *BA = 1750a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { 1760a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner BasicBlock *TheOnlyDest = BA->getBasicBlock(); 1770a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // Insert the new branch. 1780a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner BranchInst::Create(TheOnlyDest, IBI); 1790a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1800a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 1810a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (IBI->getDestination(i) == TheOnlyDest) 1820a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner TheOnlyDest = 0; 1830a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner else 1840a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner IBI->getDestination(i)->removePredecessor(IBI->getParent()); 1850a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1860a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner IBI->eraseFromParent(); 1870a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1880a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // If we didn't find our destination in the IBI successor list, then we 1890a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // have undefined behavior. Replace the unconditional branch with an 1900a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner // 'unreachable' instruction. 1910a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner if (TheOnlyDest) { 1920a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner BB->getTerminator()->eraseFromParent(); 1930a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner new UnreachableInst(BB->getContext(), BB); 1940a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1950a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 1960a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner return true; 1970a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner } 1984d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner } 1990a4c6789d5adafb6eb33080fe1833b416a152d7cChris Lattner 2004d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner return false; 2014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner} 2024d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2034d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2044d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===// 20540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner// Local dead code elimination. 2064d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// 2074d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner 2083481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the 2093481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// instruction is not used, and the instruction has no side effects. 2103481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// 211abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::isInstructionTriviallyDead(Instruction *I) { 212ec710c5b12af647ae90f53917122726269c18738Chris Lattner if (!I->use_empty() || isa<TerminatorInst>(I)) return false; 21300b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 2149c5822a966572ea78f4e818870d4229c7a855749Devang Patel // We don't want debug info removed by anything this general, unless 2159c5822a966572ea78f4e818870d4229c7a855749Devang Patel // debug info is empty. 2169c5822a966572ea78f4e818870d4229c7a855749Devang Patel if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 2179c5822a966572ea78f4e818870d4229c7a855749Devang Patel if (DDI->getAddress()) 2189c5822a966572ea78f4e818870d4229c7a855749Devang Patel return false; 2199c5822a966572ea78f4e818870d4229c7a855749Devang Patel else 2209c5822a966572ea78f4e818870d4229c7a855749Devang Patel return true; 2219c5822a966572ea78f4e818870d4229c7a855749Devang Patel } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 2229c5822a966572ea78f4e818870d4229c7a855749Devang Patel if (DVI->getValue()) 2239c5822a966572ea78f4e818870d4229c7a855749Devang Patel return false; 2249c5822a966572ea78f4e818870d4229c7a855749Devang Patel else 2259c5822a966572ea78f4e818870d4229c7a855749Devang 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 } 336e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner 337e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst); 338e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner } 339e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner return MadeChange; 340e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner} 341e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner 342b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner//===----------------------------------------------------------------------===// 34340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner// Control Flow Graph Restructuring. 344b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner// 345b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 34640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 34740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 34840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// method is called when we're about to delete Pred as a predecessor of BB. If 34940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 35040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 35140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// Unlike the removePredecessor method, this attempts to simplify uses of PHI 35240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// nodes that collapse into identity values. For example, if we have: 35340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// x = phi(1, 0, 0, 0) 35440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// y = and x, z 35540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 35640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// .. and delete the predecessor corresponding to the '1', this will attempt to 35740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// recursively fold the and to 0. 35840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattnervoid llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 35940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner TargetData *TD) { 36040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // This only adjusts blocks with PHI nodes. 36140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner if (!isa<PHINode>(BB->begin())) 36240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner return; 36340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 36440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // Remove the entries for Pred from the PHI nodes in BB, but do not simplify 36540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // them down. This will leave us with single entry phi nodes and other phis 36640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // that can be removed. 36740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner BB->removePredecessor(Pred, true); 36840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 36940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner WeakVH PhiIt = &BB->front(); 37040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) { 37140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt)); 3726ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands 3736ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands Value *PNV = SimplifyInstruction(PN, TD); 37440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner if (PNV == 0) continue; 3756ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands 37640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // If we're able to simplify the phi to a single value, substitute the new 37740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // value into all of its uses. 3786ac3386e100db376895dbc4a324d56d0ecd666d2Duncan Sands assert(PNV != PN && "SimplifyInstruction broken!"); 37940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 38035a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner Value *OldPhiIt = PhiIt; 38140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner ReplaceAndSimplifyAllUses(PN, PNV, TD); 38240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 38340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // If recursive simplification ended up deleting the next PHI node we would 38440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // iterate to, then our iterator is invalid, restart scanning from the top 38540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner // of the block. 38635a939b97dba538bfc12010b7ac26bffbf5ec4cbChris Lattner if (PhiIt != OldPhiIt) PhiIt = &BB->front(); 38740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner } 38840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner} 38940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 39040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 391b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its 392b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (DestBB!). Eliminate the edge 393b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into DestBB and 394b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deleting the predecessor block. 395b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// 396ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustiftervoid llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) { 397b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // If BB has single-entry PHI nodes, fold them. 398b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { 399b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner Value *NewVal = PN->getIncomingValue(0); 400b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Replace self referencing PHI with undef, it must be dead. 4019e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); 402b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PN->replaceAllUsesWith(NewVal); 403b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PN->eraseFromParent(); 404b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner } 405b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 406b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner BasicBlock *PredBB = DestBB->getSinglePredecessor(); 407b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner assert(PredBB && "Block doesn't have a single predecessor!"); 408b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 409b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Splice all the instructions from PredBB to DestBB. 410b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PredBB->getTerminator()->eraseFromParent(); 411b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); 41237914c8e83c43d710925263b66014159f03fa355Chris Lattner 41337914c8e83c43d710925263b66014159f03fa355Chris Lattner // Zap anything that took the address of DestBB. Not doing this will give the 41437914c8e83c43d710925263b66014159f03fa355Chris Lattner // address an invalid value. 41537914c8e83c43d710925263b66014159f03fa355Chris Lattner if (DestBB->hasAddressTaken()) { 41637914c8e83c43d710925263b66014159f03fa355Chris Lattner BlockAddress *BA = BlockAddress::get(DestBB); 41737914c8e83c43d710925263b66014159f03fa355Chris Lattner Constant *Replacement = 41837914c8e83c43d710925263b66014159f03fa355Chris Lattner ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1); 41937914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, 42037914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->getType())); 42137914c8e83c43d710925263b66014159f03fa355Chris Lattner BA->destroyConstant(); 42237914c8e83c43d710925263b66014159f03fa355Chris Lattner } 423b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 424b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Anything that branched to PredBB now branches to DestBB. 425b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PredBB->replaceAllUsesWith(DestBB); 426b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 427ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter if (P) { 42880f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); 42980f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich if (DT) { 43080f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock(); 43180f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DT->changeImmediateDominator(DestBB, PredBBIDom); 43280f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich DT->eraseNode(PredBB); 43380f6a507d4e11ba066ad0e53e12ad25ad8cf07baCameron Zwarich } 434ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>(); 435ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter if (PI) { 436ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter PI->replaceAllUses(PredBB, DestBB); 437ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB)); 438ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter } 439ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustifter } 440b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner // Nuke BB. 441b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner PredBB->eraseFromParent(); 442b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner} 4434afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel 444dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an 445dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// almost-empty BB ending in an unconditional branch to Succ, into succ. 446dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// 447dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// Assumption: Succ is the single successor for BB. 448dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// 449dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerstatic bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { 450dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); 451dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 452fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 453dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << "\n"); 454dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Shortcut, if there is only a single predecessor it must be BB and merging 455dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // is always safe 456dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (Succ->getSinglePredecessor()) return true; 457dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 458dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Make a list of the predecessors of BB 459dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner typedef SmallPtrSet<BasicBlock*, 16> BlockSet; 460dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BlockSet BBPreds(pred_begin(BB), pred_end(BB)); 461dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 462dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Use that list to make another list of common predecessors of BB and Succ 463dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BlockSet CommonPreds; 464dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); 465f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif PI != PE; ++PI) { 466f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif BasicBlock *P = *PI; 467f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif if (BBPreds.count(P)) 468f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif CommonPreds.insert(P); 469f1b2874c68e4b5561972f77ffa7c8ff7fccb6132Gabor Greif } 470dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 471dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Shortcut, if there are no common predecessors, merging is always safe 472dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (CommonPreds.empty()) 473dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return true; 474dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 475dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Look at all the phi nodes in Succ, to see if they present a conflict when 476dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // merging these blocks 477dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 478dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *PN = cast<PHINode>(I); 479dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 480dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If the incoming value from BB is again a PHINode in 481dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // BB which has the same incoming value for *PI as PN does, we can 482dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // merge the phi nodes and then the blocks can still be merged 483dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); 484dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (BBPN && BBPN->getParent() == BB) { 485dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end(); 486dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PI != PE; PI++) { 487dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (BBPN->getIncomingValueForBlock(*PI) 488dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner != PN->getIncomingValueForBlock(*PI)) { 489fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 490dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << " is conflicting with " 491dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << BBPN->getName() << " with regard to common predecessor " 492dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << (*PI)->getName() << "\n"); 493dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 494dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 495dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 496dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 497dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner Value* Val = PN->getIncomingValueForBlock(BB); 498dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end(); 499dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PI != PE; PI++) { 500dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // See if the incoming value for the common predecessor is equal to the 501dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // one for BB, in which case this phi node will not prevent the merging 502dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // of the block. 503dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (Val != PN->getIncomingValueForBlock(*PI)) { 504fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 505dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << Succ->getName() << " is conflicting with regard to common " 506dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner << "predecessor " << (*PI)->getName() << "\n"); 507dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 508dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 509dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 510dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 511dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 512dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 513dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return true; 514dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner} 515dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 516dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 517dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// unconditional branch, and contains no instructions other than PHI nodes, 518dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// potential debug intrinsics and the branch. If possible, eliminate BB by 519dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// rewriting all the predecessors to branch to the successor block and return 520dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// true. If we can't transform, return false. 521dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerbool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) { 522e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman assert(BB != &BB->getParent()->getEntryBlock() && 523e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); 524e2c6d131d12c779a410740e0a90545def75e0f48Dan Gohman 525dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // We can't eliminate infinite loops. 526dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); 527dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (BB == Succ) return false; 528dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 529dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Check to see if merging these blocks would cause conflicts for any of the 530dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // phi nodes in BB or Succ. If not, we can safely merge. 531dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; 532dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 533dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Check for cases where Succ has multiple predecessors and a PHI node in BB 534dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // has uses which will not disappear when the PHI nodes are merged. It is 535dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // possible to handle such cases, but difficult: it requires checking whether 536dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // BB dominates Succ, which is non-trivial to calculate in the case where 537dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Succ has multiple predecessors. Also, it requires checking whether 538dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // constructing the necessary self-referential PHI node doesn't intoduce any 539dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // conflicts; this isn't too difficult, but the previous code for doing this 540dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // was incorrect. 541dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // 542dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Note that if this check finds a live use, BB dominates Succ, so BB is 543dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // something like a loop pre-header (or rarely, a part of an irreducible CFG); 544dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // folding the branch isn't profitable in that case anyway. 545dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!Succ->getSinglePredecessor()) { 546dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BasicBlock::iterator BBI = BB->begin(); 547dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner while (isa<PHINode>(*BBI)) { 548dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end(); 549dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner UI != E; ++UI) { 550dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (PHINode* PN = dyn_cast<PHINode>(*UI)) { 551dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (PN->getIncomingBlock(UI) != BB) 552dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 553dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 554dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return false; 555dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 556dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 557dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner ++BBI; 558dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 559dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 560dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 561fae7706dfd3591391c03ed1439850edaed9d291cDavid Greene DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); 562dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 563dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (isa<PHINode>(Succ->begin())) { 564dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If there is more than one pred of succ, and there are PHI nodes in 565dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // the successor, then we need to add incoming edges for the PHI nodes 566dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // 567dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); 568dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 569dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Loop over all of the PHI nodes in the successor of BB. 570dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 571dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *PN = cast<PHINode>(I); 572dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner Value *OldVal = PN->removeIncomingValue(BB, false); 573dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(OldVal && "No entry in PHI for Pred BB!"); 574dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 575dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // If this incoming value is one of the PHI nodes in BB, the new entries 576dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // in the PHI node are the entries from the old PHI. 577dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { 578dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PHINode *OldValPN = cast<PHINode>(OldVal); 579dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) 580dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Note that, since we are merging phi nodes and BB and Succ might 581dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // have common predecessors, we could end up with a phi node with 582dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // identical incoming branches. This will be cleaned up later (and 583dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // will trigger asserts if we try to clean it up now, without also 584dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // simplifying the corresponding conditional branch). 585dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->addIncoming(OldValPN->getIncomingValue(i), 586dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner OldValPN->getIncomingBlock(i)); 587dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 588dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Add an incoming value for each of the new incoming values. 589dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) 590dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->addIncoming(OldVal, BBPreds[i]); 591dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 592dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 593dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 594dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 595dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { 596dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (Succ->getSinglePredecessor()) { 597dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // BB is the only predecessor of Succ, so Succ will end up with exactly 598dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // the same predecessors BB had. 599dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner Succ->getInstList().splice(Succ->begin(), 600dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BB->getInstList(), BB->begin()); 601dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } else { 602dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // We explicitly check for such uses in CanPropagatePredecessorsForPHIs. 603dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner assert(PN->use_empty() && "There shouldn't be any uses here!"); 604dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner PN->eraseFromParent(); 605dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 606dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner } 607dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 608dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner // Everything that jumped to BB now goes to Succ. 609dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BB->replaceAllUsesWith(Succ); 610dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner if (!Succ->hasName()) Succ->takeName(BB); 611dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner BB->eraseFromParent(); // Delete the old basic block. 612dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner return true; 613dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner} 614dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 61543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 61643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// nodes in this block. This doesn't try to be clever about PHI nodes 61743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// which differ only in the order of the incoming values, but instcombine 61843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// orders them so it usually won't matter. 61943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach/// 62043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbachbool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { 62143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach bool Changed = false; 62243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 62343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // This implementation doesn't currently consider undef operands 62443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // specially. Theroetically, two phis which are identical except for 62543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // one having an undef where the other doesn't could be collapsed. 62643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 62743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Map from PHI hash values to PHI nodes. If multiple PHIs have 62843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // the same hash value, the element is the first PHI in the 62943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // linked list in CollisionMap. 63043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<uintptr_t, PHINode *> HashMap; 63143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 63243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Maintain linked lists of PHI nodes with common hash values. 63343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<PHINode *, PHINode *> CollisionMap; 63443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 63543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Examine each PHI. 63643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (BasicBlock::iterator I = BB->begin(); 63743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PHINode *PN = dyn_cast<PHINode>(I++); ) { 63843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Compute a hash value on the operands. Instcombine will likely have sorted 63943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // them, which helps expose duplicates, but we have to check all the 64043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // operands to be safe in case instcombine hasn't run. 64143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach uintptr_t Hash = 0; 64243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) { 64343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // This hash algorithm is quite weak as hash functions go, but it seems 64443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // to do a good enough job for this particular purpose, and is very quick. 64543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I)); 64643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); 64743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 6482bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen // Avoid colliding with the DenseMap sentinels ~0 and ~0-1. 6492bc2a08b1bf6b5dcbfa515acc85999d6f884ec1aJakob Stoklund Olesen Hash >>= 1; 65043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // If we've never seen this hash value before, it's a unique PHI. 65143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair = 65243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach HashMap.insert(std::make_pair(Hash, PN)); 65343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (Pair.second) continue; 65443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Otherwise it's either a duplicate or a hash collision. 65543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach for (PHINode *OtherPN = Pair.first->second; ; ) { 65643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (OtherPN->isIdenticalTo(PN)) { 65743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // A duplicate. Replace this PHI with its duplicate. 65843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PN->replaceAllUsesWith(OtherPN); 65943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PN->eraseFromParent(); 66043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Changed = true; 66143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach break; 66243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 66343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // A non-duplicate hash collision. 66443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN); 66543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach if (I == CollisionMap.end()) { 66643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Set this PHI to be the head of the linked list of colliding PHIs. 66743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach PHINode *Old = Pair.first->second; 66843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach Pair.first->second = PN; 66943a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach CollisionMap[PN] = Old; 67043a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach break; 67143a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 67243a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach // Procede to the next PHI in the list. 67343a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach OtherPN = I->second; 67443a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 67543a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach } 67643a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach 67743a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach return Changed; 67843a8241b65b70ded3a87fb26852719633908a1e4Jim Grosbach} 679687140c818ba4b896329a83324714140b6580ef8Chris Lattner 680687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// enforceKnownAlignment - If the specified pointer points to an object that 681687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we control, modify the object's alignment to PrefAlign. This isn't 682687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// often possible though. If alignment is important, a more reliable approach 683687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// is to simply align all global variables and allocation instructions to 684687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// their preferred alignment from the beginning. 685687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// 68619282365147f498a60463d250dbed2f8e1b81861Benjamin Kramerstatic unsigned enforceKnownAlignment(Value *V, unsigned Align, 68719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer unsigned PrefAlign) { 688687140c818ba4b896329a83324714140b6580ef8Chris Lattner 689687140c818ba4b896329a83324714140b6580ef8Chris Lattner User *U = dyn_cast<User>(V); 690687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (!U) return Align; 691687140c818ba4b896329a83324714140b6580ef8Chris Lattner 692687140c818ba4b896329a83324714140b6580ef8Chris Lattner switch (Operator::getOpcode(U)) { 693687140c818ba4b896329a83324714140b6580ef8Chris Lattner default: break; 694687140c818ba4b896329a83324714140b6580ef8Chris Lattner case Instruction::BitCast: 695687140c818ba4b896329a83324714140b6580ef8Chris Lattner return enforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 696687140c818ba4b896329a83324714140b6580ef8Chris Lattner case Instruction::GetElementPtr: { 697687140c818ba4b896329a83324714140b6580ef8Chris Lattner // If all indexes are zero, it is just the alignment of the base pointer. 698687140c818ba4b896329a83324714140b6580ef8Chris Lattner bool AllZeroOperands = true; 699687140c818ba4b896329a83324714140b6580ef8Chris Lattner for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) 700687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (!isa<Constant>(*i) || 701687140c818ba4b896329a83324714140b6580ef8Chris Lattner !cast<Constant>(*i)->isNullValue()) { 702687140c818ba4b896329a83324714140b6580ef8Chris Lattner AllZeroOperands = false; 703687140c818ba4b896329a83324714140b6580ef8Chris Lattner break; 704687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 705687140c818ba4b896329a83324714140b6580ef8Chris Lattner 706687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (AllZeroOperands) { 707687140c818ba4b896329a83324714140b6580ef8Chris Lattner // Treat this like a bitcast. 708687140c818ba4b896329a83324714140b6580ef8Chris Lattner return enforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 709687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 710687140c818ba4b896329a83324714140b6580ef8Chris Lattner return Align; 711687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 712687140c818ba4b896329a83324714140b6580ef8Chris Lattner case Instruction::Alloca: { 713687140c818ba4b896329a83324714140b6580ef8Chris Lattner AllocaInst *AI = cast<AllocaInst>(V); 714687140c818ba4b896329a83324714140b6580ef8Chris Lattner // If there is a requested alignment and if this is an alloca, round up. 715687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (AI->getAlignment() >= PrefAlign) 716687140c818ba4b896329a83324714140b6580ef8Chris Lattner return AI->getAlignment(); 717687140c818ba4b896329a83324714140b6580ef8Chris Lattner AI->setAlignment(PrefAlign); 718687140c818ba4b896329a83324714140b6580ef8Chris Lattner return PrefAlign; 719687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 720687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 721687140c818ba4b896329a83324714140b6580ef8Chris Lattner 722687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 723687140c818ba4b896329a83324714140b6580ef8Chris Lattner // If there is a large requested alignment and we can, bump up the alignment 724687140c818ba4b896329a83324714140b6580ef8Chris Lattner // of the global. 725687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GV->isDeclaration()) return Align; 726687140c818ba4b896329a83324714140b6580ef8Chris Lattner 727687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (GV->getAlignment() >= PrefAlign) 728687140c818ba4b896329a83324714140b6580ef8Chris Lattner return GV->getAlignment(); 729687140c818ba4b896329a83324714140b6580ef8Chris Lattner // We can only increase the alignment of the global if it has no alignment 730687140c818ba4b896329a83324714140b6580ef8Chris Lattner // specified or if it is not assigned a section. If it is assigned a 731687140c818ba4b896329a83324714140b6580ef8Chris Lattner // section, the global could be densely packed with other objects in the 732687140c818ba4b896329a83324714140b6580ef8Chris Lattner // section, increasing the alignment could cause padding issues. 733687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (!GV->hasSection() || GV->getAlignment() == 0) 734687140c818ba4b896329a83324714140b6580ef8Chris Lattner GV->setAlignment(PrefAlign); 735687140c818ba4b896329a83324714140b6580ef8Chris Lattner return GV->getAlignment(); 736687140c818ba4b896329a83324714140b6580ef8Chris Lattner } 737687140c818ba4b896329a83324714140b6580ef8Chris Lattner 738687140c818ba4b896329a83324714140b6580ef8Chris Lattner return Align; 739687140c818ba4b896329a83324714140b6580ef8Chris Lattner} 740687140c818ba4b896329a83324714140b6580ef8Chris Lattner 741687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// getOrEnforceKnownAlignment - If the specified pointer has an alignment that 742687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we can determine, return it, otherwise return 0. If PrefAlign is specified, 743687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// and it is more than the alignment of the ultimate object, see if we can 744687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// increase the alignment of the ultimate object, making this check succeed. 745687140c818ba4b896329a83324714140b6580ef8Chris Lattnerunsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, 746687140c818ba4b896329a83324714140b6580ef8Chris Lattner const TargetData *TD) { 747687140c818ba4b896329a83324714140b6580ef8Chris Lattner assert(V->getType()->isPointerTy() && 748687140c818ba4b896329a83324714140b6580ef8Chris Lattner "getOrEnforceKnownAlignment expects a pointer!"); 749687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned BitWidth = TD ? TD->getPointerSizeInBits() : 64; 750687140c818ba4b896329a83324714140b6580ef8Chris Lattner APInt Mask = APInt::getAllOnesValue(BitWidth); 751687140c818ba4b896329a83324714140b6580ef8Chris Lattner APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 752ae47be1ea023e4b1e6bbbdc4687333eea54c84c8Chris Lattner ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD); 753687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned TrailZ = KnownZero.countTrailingOnes(); 754687140c818ba4b896329a83324714140b6580ef8Chris Lattner 755687140c818ba4b896329a83324714140b6580ef8Chris Lattner // Avoid trouble with rediculously large TrailZ values, such as 756687140c818ba4b896329a83324714140b6580ef8Chris Lattner // those computed from a null pointer. 757687140c818ba4b896329a83324714140b6580ef8Chris Lattner TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1)); 758687140c818ba4b896329a83324714140b6580ef8Chris Lattner 759687140c818ba4b896329a83324714140b6580ef8Chris Lattner unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); 760687140c818ba4b896329a83324714140b6580ef8Chris Lattner 761687140c818ba4b896329a83324714140b6580ef8Chris Lattner // LLVM doesn't support alignments larger than this currently. 762687140c818ba4b896329a83324714140b6580ef8Chris Lattner Align = std::min(Align, +Value::MaximumAlignment); 763687140c818ba4b896329a83324714140b6580ef8Chris Lattner 764687140c818ba4b896329a83324714140b6580ef8Chris Lattner if (PrefAlign > Align) 765687140c818ba4b896329a83324714140b6580ef8Chris Lattner Align = enforceKnownAlignment(V, Align, PrefAlign); 766687140c818ba4b896329a83324714140b6580ef8Chris Lattner 767687140c818ba4b896329a83324714140b6580ef8Chris Lattner // We don't need to make any adjustment. 768687140c818ba4b896329a83324714140b6580ef8Chris Lattner return Align; 769687140c818ba4b896329a83324714140b6580ef8Chris Lattner} 770687140c818ba4b896329a83324714140b6580ef8Chris Lattner 7715ee20680c7ebc765950983633e19fafab5235245Devang Patel///===---------------------------------------------------------------------===// 7725ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Dbg Intrinsic utilities 7735ee20680c7ebc765950983633e19fafab5235245Devang Patel/// 7745ee20680c7ebc765950983633e19fafab5235245Devang Patel 7755ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 7765ee20680c7ebc765950983633e19fafab5235245Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 7775ee20680c7ebc765950983633e19fafab5235245Devang Patelbool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 7785ee20680c7ebc765950983633e19fafab5235245Devang Patel StoreInst *SI, DIBuilder &Builder) { 7795ee20680c7ebc765950983633e19fafab5235245Devang Patel DIVariable DIVar(DDI->getVariable()); 7805ee20680c7ebc765950983633e19fafab5235245Devang Patel if (!DIVar.Verify()) 7815ee20680c7ebc765950983633e19fafab5235245Devang Patel return false; 7825ee20680c7ebc765950983633e19fafab5235245Devang Patel 7835ee20680c7ebc765950983633e19fafab5235245Devang Patel Instruction *DbgVal = 7845ee20680c7ebc765950983633e19fafab5235245Devang Patel Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, 7855ee20680c7ebc765950983633e19fafab5235245Devang Patel DIVar, SI); 7865ee20680c7ebc765950983633e19fafab5235245Devang Patel 7875ee20680c7ebc765950983633e19fafab5235245Devang Patel // Propagate any debug metadata from the store onto the dbg.value. 7885ee20680c7ebc765950983633e19fafab5235245Devang Patel DebugLoc SIDL = SI->getDebugLoc(); 7895ee20680c7ebc765950983633e19fafab5235245Devang Patel if (!SIDL.isUnknown()) 7905ee20680c7ebc765950983633e19fafab5235245Devang Patel DbgVal->setDebugLoc(SIDL); 7915ee20680c7ebc765950983633e19fafab5235245Devang Patel // Otherwise propagate debug metadata from dbg.declare. 7925ee20680c7ebc765950983633e19fafab5235245Devang Patel else 7935ee20680c7ebc765950983633e19fafab5235245Devang Patel DbgVal->setDebugLoc(DDI->getDebugLoc()); 7945ee20680c7ebc765950983633e19fafab5235245Devang Patel return true; 7955ee20680c7ebc765950983633e19fafab5235245Devang Patel} 7965ee20680c7ebc765950983633e19fafab5235245Devang Patel 79736fae67831517f132255118b45b21a8cf199a012Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 79836fae67831517f132255118b45b21a8cf199a012Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 79936fae67831517f132255118b45b21a8cf199a012Devang Patelbool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 80036fae67831517f132255118b45b21a8cf199a012Devang Patel LoadInst *LI, DIBuilder &Builder) { 80136fae67831517f132255118b45b21a8cf199a012Devang Patel DIVariable DIVar(DDI->getVariable()); 80236fae67831517f132255118b45b21a8cf199a012Devang Patel if (!DIVar.Verify()) 80336fae67831517f132255118b45b21a8cf199a012Devang Patel return false; 80436fae67831517f132255118b45b21a8cf199a012Devang Patel 80536fae67831517f132255118b45b21a8cf199a012Devang Patel Instruction *DbgVal = 80636fae67831517f132255118b45b21a8cf199a012Devang Patel Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, 80736fae67831517f132255118b45b21a8cf199a012Devang Patel DIVar, LI); 80836fae67831517f132255118b45b21a8cf199a012Devang Patel 80936fae67831517f132255118b45b21a8cf199a012Devang Patel // Propagate any debug metadata from the store onto the dbg.value. 81036fae67831517f132255118b45b21a8cf199a012Devang Patel DebugLoc LIDL = LI->getDebugLoc(); 81136fae67831517f132255118b45b21a8cf199a012Devang Patel if (!LIDL.isUnknown()) 81236fae67831517f132255118b45b21a8cf199a012Devang Patel DbgVal->setDebugLoc(LIDL); 81336fae67831517f132255118b45b21a8cf199a012Devang Patel // Otherwise propagate debug metadata from dbg.declare. 81436fae67831517f132255118b45b21a8cf199a012Devang Patel else 81536fae67831517f132255118b45b21a8cf199a012Devang Patel DbgVal->setDebugLoc(DDI->getDebugLoc()); 81636fae67831517f132255118b45b21a8cf199a012Devang Patel return true; 81736fae67831517f132255118b45b21a8cf199a012Devang Patel} 81836fae67831517f132255118b45b21a8cf199a012Devang Patel 819813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 820813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// of llvm.dbg.value intrinsics. 821813c9a0f19c0d27085a3ea81eb44033747007741Devang Patelbool llvm::LowerDbgDeclare(Function &F) { 822813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel DIBuilder DIB(*F.getParent()); 823813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel SmallVector<DbgDeclareInst *, 4> Dbgs; 824813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) 825813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) { 826813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) 827813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel Dbgs.push_back(DDI); 828813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 829813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (Dbgs.empty()) 830813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel return false; 831813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel 832813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (SmallVector<DbgDeclareInst *, 4>::iterator I = Dbgs.begin(), 833813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel E = Dbgs.end(); I != E; ++I) { 834813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel DbgDeclareInst *DDI = *I; 835813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress())) { 836813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); 837813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel UI != E; ++UI) 838813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) 839813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel ConvertDebugDeclareToDebugValue(DDI, SI, DIB); 84036fae67831517f132255118b45b21a8cf199a012Devang Patel else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) 84136fae67831517f132255118b45b21a8cf199a012Devang Patel ConvertDebugDeclareToDebugValue(DDI, LI, DIB); 842813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 843813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel DDI->eraseFromParent(); 844813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel } 845813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel return true; 846813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel} 847