Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- Local.cpp - Functions to perform local transformations ------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This family of functions perform various local transformations to the |
| 11 | // program. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 58a2cbe | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Dominators.h" |
| 21 | #include "llvm/Analysis/InstructionSimplify.h" |
| 22 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 24 | #include "llvm/DIBuilder.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/GlobalAlias.h" |
| 30 | #include "llvm/IR/GlobalVariable.h" |
| 31 | #include "llvm/IR/IRBuilder.h" |
| 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/Intrinsics.h" |
| 35 | #include "llvm/IR/MDBuilder.h" |
| 36 | #include "llvm/IR/Metadata.h" |
| 37 | #include "llvm/IR/Operator.h" |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CFG.h" |
| 39 | #include "llvm/Support/Debug.h" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 40 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 41 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 19f2dc4 | 2009-12-29 09:12:29 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 44 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 45 | |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 46 | STATISTIC(NumRemoved, "Number of unreachable basic blocks removed"); |
| 47 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 49 | // Local constant propagation. |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 50 | // |
| 51 | |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 52 | /// ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 53 | /// constant value, convert it into an unconditional branch to the constant |
| 54 | /// destination. This is a nontrivial operation because the successors of this |
| 55 | /// basic block must have their PHI nodes updated. |
| 56 | /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch |
| 57 | /// conditions and indirectbr addresses this might make dead if |
| 58 | /// DeleteDeadConditions is true. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 59 | bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, |
| 60 | const TargetLibraryInfo *TLI) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 61 | TerminatorInst *T = BB->getTerminator(); |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 62 | IRBuilder<> Builder(T); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 64 | // Branch - See if we are conditional jumping on constant |
| 65 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 66 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
Gabor Greif | c1bb13f | 2009-01-30 18:21:13 +0000 | [diff] [blame] | 67 | BasicBlock *Dest1 = BI->getSuccessor(0); |
| 68 | BasicBlock *Dest2 = BI->getSuccessor(1); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 69 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 70 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 71 | // Are we branching on constant? |
| 72 | // YES. Change to unconditional branch... |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 73 | BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; |
| 74 | BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 75 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 76 | //cerr << "Function: " << T->getParent()->getParent() |
| 77 | // << "\nRemoving branch from " << T->getParent() |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 78 | // << "\n\nTo: " << OldDest << endl; |
| 79 | |
| 80 | // Let the basic block know that we are letting go of it. Based on this, |
| 81 | // it will adjust it's PHI nodes. |
Jay Foad | 1a03902 | 2011-04-19 15:23:29 +0000 | [diff] [blame] | 82 | OldDest->removePredecessor(BB); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 83 | |
Jay Foad | 8f9ffbd | 2011-01-07 20:25:56 +0000 | [diff] [blame] | 84 | // Replace the conditional branch with an unconditional one. |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 85 | Builder.CreateBr(Destination); |
Jay Foad | 8f9ffbd | 2011-01-07 20:25:56 +0000 | [diff] [blame] | 86 | BI->eraseFromParent(); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 87 | return true; |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 88 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 90 | if (Dest2 == Dest1) { // Conditional branch to same location? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | // This branch matches something like this: |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 92 | // br bool %cond, label %Dest, label %Dest |
| 93 | // and changes it into: br label %Dest |
| 94 | |
| 95 | // Let the basic block know that we are letting go of one copy of it. |
| 96 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 97 | Dest1->removePredecessor(BI->getParent()); |
| 98 | |
Jay Foad | 8f9ffbd | 2011-01-07 20:25:56 +0000 | [diff] [blame] | 99 | // Replace the conditional branch with an unconditional one. |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 100 | Builder.CreateBr(Dest1); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 101 | Value *Cond = BI->getCondition(); |
Jay Foad | 8f9ffbd | 2011-01-07 20:25:56 +0000 | [diff] [blame] | 102 | BI->eraseFromParent(); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 103 | if (DeleteDeadConditions) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 104 | RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 110 | if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 111 | // If we are switching on a constant, we can convert the switch into a |
| 112 | // single branch instruction! |
| 113 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 114 | BasicBlock *TheOnlyDest = SI->getDefaultDest(); |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 115 | BasicBlock *DefaultDest = TheOnlyDest; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 117 | // Figure out which case it goes to. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 118 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 119 | i != e; ++i) { |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 120 | // Found case matching a constant operand? |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 121 | if (i.getCaseValue() == CI) { |
| 122 | TheOnlyDest = i.getCaseSuccessor(); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 126 | // Check to see if this branch is going to the same place as the default |
| 127 | // dest. If so, eliminate it as an explicit compare. |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 128 | if (i.getCaseSuccessor() == DefaultDest) { |
Manman Ren | ee99c7f | 2012-09-12 17:04:11 +0000 | [diff] [blame] | 129 | MDNode* MD = SI->getMetadata(LLVMContext::MD_prof); |
| 130 | // MD should have 2 + NumCases operands. |
| 131 | if (MD && MD->getNumOperands() == 2 + SI->getNumCases()) { |
| 132 | // Collect branch weights into a vector. |
| 133 | SmallVector<uint32_t, 8> Weights; |
| 134 | for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; |
| 135 | ++MD_i) { |
| 136 | ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i)); |
| 137 | assert(CI); |
| 138 | Weights.push_back(CI->getValue().getZExtValue()); |
| 139 | } |
| 140 | // Merge weight of this case to the default weight. |
| 141 | unsigned idx = i.getCaseIndex(); |
| 142 | Weights[0] += Weights[idx+1]; |
| 143 | // Remove weight for this case. |
| 144 | std::swap(Weights[idx+1], Weights.back()); |
| 145 | Weights.pop_back(); |
| 146 | SI->setMetadata(LLVMContext::MD_prof, |
| 147 | MDBuilder(BB->getContext()). |
| 148 | createBranchWeights(Weights)); |
| 149 | } |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 150 | // Remove this entry. |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 151 | DefaultDest->removePredecessor(SI->getParent()); |
| 152 | SI->removeCase(i); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 153 | --i; --e; |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 154 | continue; |
| 155 | } |
| 156 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 157 | // Otherwise, check to see if the switch only branches to one destination. |
| 158 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 159 | // destinations. |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 160 | if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 163 | if (CI && !TheOnlyDest) { |
| 164 | // Branching on a constant, but not any of the cases, go to the default |
| 165 | // successor. |
| 166 | TheOnlyDest = SI->getDefaultDest(); |
| 167 | } |
| 168 | |
| 169 | // If we found a single destination that we can fold the switch into, do so |
| 170 | // now. |
| 171 | if (TheOnlyDest) { |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 172 | // Insert the new branch. |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 173 | Builder.CreateBr(TheOnlyDest); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 174 | BasicBlock *BB = SI->getParent(); |
| 175 | |
| 176 | // Remove entries from PHI nodes which we no longer branch to... |
| 177 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 178 | // Found case matching a constant operand? |
| 179 | BasicBlock *Succ = SI->getSuccessor(i); |
| 180 | if (Succ == TheOnlyDest) |
| 181 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 182 | else |
| 183 | Succ->removePredecessor(BB); |
| 184 | } |
| 185 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 186 | // Delete the old switch. |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 187 | Value *Cond = SI->getCondition(); |
| 188 | SI->eraseFromParent(); |
| 189 | if (DeleteDeadConditions) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 190 | RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 191 | return true; |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 192 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 193 | |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 194 | if (SI->getNumCases() == 1) { |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 195 | // Otherwise, we can fold this switch into a conditional branch |
| 196 | // instruction if it has only one non-default destination. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 197 | SwitchInst::CaseIt FirstCase = SI->case_begin(); |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 198 | Value *Cond = Builder.CreateICmpEQ(SI->getCondition(), |
| 199 | FirstCase.getCaseValue(), "cond"); |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 200 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 201 | // Insert the new branch. |
| 202 | BranchInst *NewBr = Builder.CreateCondBr(Cond, |
| 203 | FirstCase.getCaseSuccessor(), |
| 204 | SI->getDefaultDest()); |
| 205 | MDNode* MD = SI->getMetadata(LLVMContext::MD_prof); |
| 206 | if (MD && MD->getNumOperands() == 3) { |
| 207 | ConstantInt *SICase = dyn_cast<ConstantInt>(MD->getOperand(2)); |
| 208 | ConstantInt *SIDef = dyn_cast<ConstantInt>(MD->getOperand(1)); |
| 209 | assert(SICase && SIDef); |
| 210 | // The TrueWeight should be the weight for the single case of SI. |
| 211 | NewBr->setMetadata(LLVMContext::MD_prof, |
| 212 | MDBuilder(BB->getContext()). |
| 213 | createBranchWeights(SICase->getValue().getZExtValue(), |
| 214 | SIDef->getValue().getZExtValue())); |
Stepan Dyatkovskiy | a2067fb | 2012-05-23 08:18:26 +0000 | [diff] [blame] | 215 | } |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 216 | |
| 217 | // Delete the old switch. |
| 218 | SI->eraseFromParent(); |
| 219 | return true; |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 221 | return false; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 223 | |
| 224 | if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) { |
| 225 | // indirectbr blockaddress(@F, @BB) -> br label @BB |
| 226 | if (BlockAddress *BA = |
| 227 | dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { |
| 228 | BasicBlock *TheOnlyDest = BA->getBasicBlock(); |
| 229 | // Insert the new branch. |
Devang Patel | 62fb355 | 2011-05-18 17:26:46 +0000 | [diff] [blame] | 230 | Builder.CreateBr(TheOnlyDest); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 232 | for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { |
| 233 | if (IBI->getDestination(i) == TheOnlyDest) |
| 234 | TheOnlyDest = 0; |
| 235 | else |
| 236 | IBI->getDestination(i)->removePredecessor(IBI->getParent()); |
| 237 | } |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 238 | Value *Address = IBI->getAddress(); |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 239 | IBI->eraseFromParent(); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 240 | if (DeleteDeadConditions) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 241 | RecursivelyDeleteTriviallyDeadInstructions(Address, TLI); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 243 | // If we didn't find our destination in the IBI successor list, then we |
| 244 | // have undefined behavior. Replace the unconditional branch with an |
| 245 | // 'unreachable' instruction. |
| 246 | if (TheOnlyDest) { |
| 247 | BB->getTerminator()->eraseFromParent(); |
| 248 | new UnreachableInst(BB->getContext(), BB); |
| 249 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 0a4c678 | 2009-11-01 03:40:38 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 258 | |
| 259 | //===----------------------------------------------------------------------===// |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 260 | // Local dead code elimination. |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 261 | // |
| 262 | |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 263 | /// isInstructionTriviallyDead - Return true if the result produced by the |
| 264 | /// instruction is not used, and the instruction has no side effects. |
| 265 | /// |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 266 | bool llvm::isInstructionTriviallyDead(Instruction *I, |
| 267 | const TargetLibraryInfo *TLI) { |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 268 | if (!I->use_empty() || isa<TerminatorInst>(I)) return false; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 269 | |
Bill Wendling | 187b192 | 2011-08-15 20:10:51 +0000 | [diff] [blame] | 270 | // We don't want the landingpad instruction removed by anything this general. |
| 271 | if (isa<LandingPadInst>(I)) |
| 272 | return false; |
| 273 | |
Devang Patel | 9c5822a | 2011-03-18 23:28:02 +0000 | [diff] [blame] | 274 | // We don't want debug info removed by anything this general, unless |
| 275 | // debug info is empty. |
| 276 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { |
Nick Lewycky | 3e69c13 | 2011-08-02 21:19:27 +0000 | [diff] [blame] | 277 | if (DDI->getAddress()) |
Devang Patel | 9c5822a | 2011-03-18 23:28:02 +0000 | [diff] [blame] | 278 | return false; |
Devang Patel | b994621 | 2011-03-21 22:04:45 +0000 | [diff] [blame] | 279 | return true; |
Nick Lewycky | 3e69c13 | 2011-08-02 21:19:27 +0000 | [diff] [blame] | 280 | } |
Devang Patel | b994621 | 2011-03-21 22:04:45 +0000 | [diff] [blame] | 281 | if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { |
Devang Patel | 9c5822a | 2011-03-18 23:28:02 +0000 | [diff] [blame] | 282 | if (DVI->getValue()) |
| 283 | return false; |
Devang Patel | b994621 | 2011-03-21 22:04:45 +0000 | [diff] [blame] | 284 | return true; |
Devang Patel | 9c5822a | 2011-03-18 23:28:02 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 287 | if (!I->mayHaveSideEffects()) return true; |
| 288 | |
| 289 | // Special case intrinsics that "may have side effects" but can be deleted |
| 290 | // when dead. |
Nick Lewycky | 3e69c13 | 2011-08-02 21:19:27 +0000 | [diff] [blame] | 291 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Chris Lattner | 741c0ae | 2007-12-29 00:59:12 +0000 | [diff] [blame] | 292 | // Safe to delete llvm.stacksave if dead. |
| 293 | if (II->getIntrinsicID() == Intrinsic::stacksave) |
| 294 | return true; |
Nick Lewycky | 3e69c13 | 2011-08-02 21:19:27 +0000 | [diff] [blame] | 295 | |
| 296 | // Lifetime intrinsics are dead when their right-hand is undef. |
| 297 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 298 | II->getIntrinsicID() == Intrinsic::lifetime_end) |
| 299 | return isa<UndefValue>(II->getArgOperand(1)); |
| 300 | } |
Nick Lewycky | 4a3935c | 2011-10-24 04:35:36 +0000 | [diff] [blame] | 301 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 302 | if (isAllocLikeFn(I, TLI)) return true; |
Nick Lewycky | 4a3935c | 2011-10-24 04:35:36 +0000 | [diff] [blame] | 303 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 304 | if (CallInst *CI = isFreeCall(I, TLI)) |
Nick Lewycky | 4a3935c | 2011-10-24 04:35:36 +0000 | [diff] [blame] | 305 | if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) |
| 306 | return C->isNullValue() || isa<UndefValue>(C); |
| 307 | |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 308 | return false; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 311 | /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a |
| 312 | /// trivially dead instruction, delete it. If that makes any of its operands |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 313 | /// trivially dead, delete them too, recursively. Return true if any |
| 314 | /// instructions were deleted. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 315 | bool |
| 316 | llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, |
| 317 | const TargetLibraryInfo *TLI) { |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 318 | Instruction *I = dyn_cast<Instruction>(V); |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 319 | if (!I || !I->use_empty() || !isInstructionTriviallyDead(I, TLI)) |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 320 | return false; |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 322 | SmallVector<Instruction*, 16> DeadInsts; |
| 323 | DeadInsts.push_back(I); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 324 | |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 325 | do { |
Dan Gohman | e9d87f4 | 2009-05-06 17:22:41 +0000 | [diff] [blame] | 326 | I = DeadInsts.pop_back_val(); |
Chris Lattner | 2872177 | 2008-11-28 00:58:15 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 328 | // Null out all of the instruction's operands to see if any operand becomes |
| 329 | // dead as we go. |
| 330 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 331 | Value *OpV = I->getOperand(i); |
| 332 | I->setOperand(i, 0); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 334 | if (!OpV->use_empty()) continue; |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 336 | // If the operand is an instruction that became dead as we nulled out the |
| 337 | // operand, and if it is 'trivially' dead, delete it in a future loop |
| 338 | // iteration. |
| 339 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 340 | if (isInstructionTriviallyDead(OpI, TLI)) |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 341 | DeadInsts.push_back(OpI); |
| 342 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 344 | I->eraseFromParent(); |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 345 | } while (!DeadInsts.empty()); |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 346 | |
| 347 | return true; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 349 | |
Nick Lewycky | 1a4021a | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 350 | /// areAllUsesEqual - Check whether the uses of a value are all the same. |
| 351 | /// This is similar to Instruction::hasOneUse() except this will also return |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 352 | /// true when there are no uses or multiple uses that all refer to the same |
| 353 | /// value. |
Nick Lewycky | 1a4021a | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 354 | static bool areAllUsesEqual(Instruction *I) { |
| 355 | Value::use_iterator UI = I->use_begin(); |
| 356 | Value::use_iterator UE = I->use_end(); |
| 357 | if (UI == UE) |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 358 | return true; |
Nick Lewycky | 1a4021a | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 359 | |
| 360 | User *TheUse = *UI; |
| 361 | for (++UI; UI != UE; ++UI) { |
| 362 | if (*UI != TheUse) |
| 363 | return false; |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 368 | /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively |
| 369 | /// dead PHI node, due to being a def-use chain of single-use nodes that |
| 370 | /// either forms a cycle or is terminated by a trivially dead instruction, |
| 371 | /// delete it. If that makes any of its operands trivially dead, delete them |
Duncan Sands | 2cfbf01 | 2011-02-21 17:32:05 +0000 | [diff] [blame] | 372 | /// too, recursively. Return true if a change was made. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 373 | bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, |
| 374 | const TargetLibraryInfo *TLI) { |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 375 | SmallPtrSet<Instruction*, 4> Visited; |
| 376 | for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects(); |
| 377 | I = cast<Instruction>(*I->use_begin())) { |
| 378 | if (I->use_empty()) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 379 | return RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
Nick Lewycky | eff5e69 | 2011-02-20 18:05:56 +0000 | [diff] [blame] | 380 | |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 381 | // If we find an instruction more than once, we're on a cycle that |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 382 | // won't prove fruitful. |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 383 | if (!Visited.insert(I)) { |
| 384 | // Break the cycle and delete the instruction and its operands. |
| 385 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 386 | (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
Duncan Sands | 2cfbf01 | 2011-02-21 17:32:05 +0000 | [diff] [blame] | 387 | return true; |
Duncan Sands | b4098ba | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | return false; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 391 | } |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 392 | |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 393 | /// SimplifyInstructionsInBlock - Scan the specified basic block and try to |
| 394 | /// simplify any instructions in it and recursively delete dead instructions. |
| 395 | /// |
| 396 | /// This returns true if it changed the code, note that it can delete |
| 397 | /// instructions in other blocks as well in this block. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 398 | bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const DataLayout *TD, |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 399 | const TargetLibraryInfo *TLI) { |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 400 | bool MadeChange = false; |
Chandler Carruth | acdae3e | 2012-03-25 03:29:25 +0000 | [diff] [blame] | 401 | |
| 402 | #ifndef NDEBUG |
| 403 | // In debug builds, ensure that the terminator of the block is never replaced |
| 404 | // or deleted by these simplifications. The idea of simplification is that it |
| 405 | // cannot introduce new instructions, and there is no way to replace the |
| 406 | // terminator of a block without introducing a new instruction. |
| 407 | AssertingVH<Instruction> TerminatorVH(--BB->end()); |
| 408 | #endif |
| 409 | |
Chandler Carruth | 858cd1c | 2012-03-24 23:03:27 +0000 | [diff] [blame] | 410 | for (BasicBlock::iterator BI = BB->begin(), E = --BB->end(); BI != E; ) { |
| 411 | assert(!BI->isTerminator()); |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 412 | Instruction *Inst = BI++; |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 413 | |
| 414 | WeakVH BIHandle(BI); |
Benjamin Kramer | d721520 | 2013-09-24 16:37:40 +0000 | [diff] [blame] | 415 | if (recursivelySimplifyInstruction(Inst, TD, TLI)) { |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 416 | MadeChange = true; |
Chris Lattner | 35a939b | 2010-07-15 06:06:04 +0000 | [diff] [blame] | 417 | if (BIHandle != BI) |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 418 | BI = BB->begin(); |
| 419 | continue; |
| 420 | } |
Eli Friedman | 71ad2c9 | 2011-04-02 22:45:17 +0000 | [diff] [blame] | 421 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 422 | MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI); |
Eli Friedman | 71ad2c9 | 2011-04-02 22:45:17 +0000 | [diff] [blame] | 423 | if (BIHandle != BI) |
| 424 | BI = BB->begin(); |
Chris Lattner | e234a30 | 2010-01-12 19:40:54 +0000 | [diff] [blame] | 425 | } |
| 426 | return MadeChange; |
| 427 | } |
| 428 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 429 | //===----------------------------------------------------------------------===// |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 430 | // Control Flow Graph Restructuring. |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 431 | // |
| 432 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 433 | |
| 434 | /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this |
| 435 | /// method is called when we're about to delete Pred as a predecessor of BB. If |
| 436 | /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. |
| 437 | /// |
| 438 | /// Unlike the removePredecessor method, this attempts to simplify uses of PHI |
| 439 | /// nodes that collapse into identity values. For example, if we have: |
| 440 | /// x = phi(1, 0, 0, 0) |
| 441 | /// y = and x, z |
| 442 | /// |
| 443 | /// .. and delete the predecessor corresponding to the '1', this will attempt to |
| 444 | /// recursively fold the and to 0. |
| 445 | void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 446 | DataLayout *TD) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 447 | // This only adjusts blocks with PHI nodes. |
| 448 | if (!isa<PHINode>(BB->begin())) |
| 449 | return; |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 451 | // Remove the entries for Pred from the PHI nodes in BB, but do not simplify |
| 452 | // them down. This will leave us with single entry phi nodes and other phis |
| 453 | // that can be removed. |
| 454 | BB->removePredecessor(Pred, true); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 456 | WeakVH PhiIt = &BB->front(); |
| 457 | while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) { |
| 458 | PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt)); |
Chris Lattner | 35a939b | 2010-07-15 06:06:04 +0000 | [diff] [blame] | 459 | Value *OldPhiIt = PhiIt; |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 460 | |
| 461 | if (!recursivelySimplifyInstruction(PN, TD)) |
| 462 | continue; |
| 463 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 464 | // If recursive simplification ended up deleting the next PHI node we would |
| 465 | // iterate to, then our iterator is invalid, restart scanning from the top |
| 466 | // of the block. |
Chris Lattner | 35a939b | 2010-07-15 06:06:04 +0000 | [diff] [blame] | 467 | if (PhiIt != OldPhiIt) PhiIt = &BB->front(); |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 472 | /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its |
| 473 | /// predecessor is known to have one successor (DestBB!). Eliminate the edge |
| 474 | /// between them, moving the instructions in the predecessor into DestBB and |
| 475 | /// deleting the predecessor block. |
| 476 | /// |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 477 | void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) { |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 478 | // If BB has single-entry PHI nodes, fold them. |
| 479 | while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { |
| 480 | Value *NewVal = PN->getIncomingValue(0); |
| 481 | // Replace self referencing PHI with undef, it must be dead. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 482 | if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 483 | PN->replaceAllUsesWith(NewVal); |
| 484 | PN->eraseFromParent(); |
| 485 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 486 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 487 | BasicBlock *PredBB = DestBB->getSinglePredecessor(); |
| 488 | assert(PredBB && "Block doesn't have a single predecessor!"); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 37914c8 | 2010-02-15 20:47:49 +0000 | [diff] [blame] | 490 | // Zap anything that took the address of DestBB. Not doing this will give the |
| 491 | // address an invalid value. |
| 492 | if (DestBB->hasAddressTaken()) { |
| 493 | BlockAddress *BA = BlockAddress::get(DestBB); |
| 494 | Constant *Replacement = |
| 495 | ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1); |
| 496 | BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, |
| 497 | BA->getType())); |
| 498 | BA->destroyConstant(); |
| 499 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 500 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 501 | // Anything that branched to PredBB now branches to DestBB. |
| 502 | PredBB->replaceAllUsesWith(DestBB); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 503 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 504 | // Splice all the instructions from PredBB to DestBB. |
| 505 | PredBB->getTerminator()->eraseFromParent(); |
Bill Wendling | d5b7f2b | 2013-10-19 11:27:12 +0000 | [diff] [blame] | 506 | |
| 507 | // First splice over the PHI nodes. |
| 508 | BasicBlock::iterator PI = PredBB->begin(); |
| 509 | while (isa<PHINode>(PI)) |
| 510 | ++PI; |
| 511 | |
| 512 | if (PI != PredBB->begin()) |
| 513 | DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList(), |
| 514 | PredBB->begin(), PI); |
| 515 | |
| 516 | // Now splice over the rest of the instructions. |
| 517 | DestBB->getInstList().splice(DestBB->getFirstInsertionPt(), |
| 518 | PredBB->getInstList(), PI, PredBB->end()); |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 519 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 520 | if (P) { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 521 | DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); |
| 522 | if (DT) { |
| 523 | BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock(); |
| 524 | DT->changeImmediateDominator(DestBB, PredBBIDom); |
| 525 | DT->eraseNode(PredBB); |
| 526 | } |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 527 | } |
Bill Wendling | d5b7f2b | 2013-10-19 11:27:12 +0000 | [diff] [blame] | 528 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 529 | // Nuke BB. |
| 530 | PredBB->eraseFromParent(); |
| 531 | } |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 532 | |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 533 | /// CanMergeValues - Return true if we can choose one of these values to use |
| 534 | /// in place of the other. Note that we will always choose the non-undef |
| 535 | /// value to keep. |
| 536 | static bool CanMergeValues(Value *First, Value *Second) { |
| 537 | return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second); |
| 538 | } |
| 539 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 540 | /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an |
Mark Lacey | 1b6e10f | 2013-08-14 22:11:42 +0000 | [diff] [blame] | 541 | /// almost-empty BB ending in an unconditional branch to Succ, into Succ. |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 542 | /// |
| 543 | /// Assumption: Succ is the single successor for BB. |
| 544 | /// |
| 545 | static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
| 546 | assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); |
| 547 | |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 548 | DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 549 | << Succ->getName() << "\n"); |
| 550 | // Shortcut, if there is only a single predecessor it must be BB and merging |
| 551 | // is always safe |
| 552 | if (Succ->getSinglePredecessor()) return true; |
| 553 | |
| 554 | // Make a list of the predecessors of BB |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 555 | SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 556 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 557 | // Look at all the phi nodes in Succ, to see if they present a conflict when |
| 558 | // merging these blocks |
| 559 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 560 | PHINode *PN = cast<PHINode>(I); |
| 561 | |
| 562 | // If the incoming value from BB is again a PHINode in |
| 563 | // BB which has the same incoming value for *PI as PN does, we can |
| 564 | // merge the phi nodes and then the blocks can still be merged |
| 565 | PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); |
| 566 | if (BBPN && BBPN->getParent() == BB) { |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 567 | for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { |
| 568 | BasicBlock *IBB = PN->getIncomingBlock(PI); |
| 569 | if (BBPreds.count(IBB) && |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 570 | !CanMergeValues(BBPN->getIncomingValueForBlock(IBB), |
| 571 | PN->getIncomingValue(PI))) { |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 572 | DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " |
| 573 | << Succ->getName() << " is conflicting with " |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 574 | << BBPN->getName() << " with regard to common predecessor " |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 575 | << IBB->getName() << "\n"); |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | } |
| 579 | } else { |
| 580 | Value* Val = PN->getIncomingValueForBlock(BB); |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 581 | for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 582 | // See if the incoming value for the common predecessor is equal to the |
| 583 | // one for BB, in which case this phi node will not prevent the merging |
| 584 | // of the block. |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 585 | BasicBlock *IBB = PN->getIncomingBlock(PI); |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 586 | if (BBPreds.count(IBB) && |
| 587 | !CanMergeValues(Val, PN->getIncomingValue(PI))) { |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 588 | DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 589 | << Succ->getName() << " is conflicting with regard to common " |
Benjamin Kramer | 88c0914 | 2011-12-06 16:14:29 +0000 | [diff] [blame] | 590 | << "predecessor " << IBB->getName() << "\n"); |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return true; |
| 598 | } |
| 599 | |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 600 | typedef SmallVector<BasicBlock *, 16> PredBlockVector; |
| 601 | typedef DenseMap<BasicBlock *, Value *> IncomingValueMap; |
| 602 | |
| 603 | /// \brief Determines the value to use as the phi node input for a block. |
| 604 | /// |
| 605 | /// Select between \p OldVal any value that we know flows from \p BB |
| 606 | /// to a particular phi on the basis of which one (if either) is not |
| 607 | /// undef. Update IncomingValues based on the selected value. |
| 608 | /// |
| 609 | /// \param OldVal The value we are considering selecting. |
| 610 | /// \param BB The block that the value flows in from. |
| 611 | /// \param IncomingValues A map from block-to-value for other phi inputs |
| 612 | /// that we have examined. |
| 613 | /// |
| 614 | /// \returns the selected value. |
| 615 | static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB, |
| 616 | IncomingValueMap &IncomingValues) { |
| 617 | if (!isa<UndefValue>(OldVal)) { |
| 618 | assert((!IncomingValues.count(BB) || |
| 619 | IncomingValues.find(BB)->second == OldVal) && |
| 620 | "Expected OldVal to match incoming value from BB!"); |
| 621 | |
| 622 | IncomingValues.insert(std::make_pair(BB, OldVal)); |
| 623 | return OldVal; |
| 624 | } |
| 625 | |
| 626 | IncomingValueMap::const_iterator It = IncomingValues.find(BB); |
| 627 | if (It != IncomingValues.end()) return It->second; |
| 628 | |
| 629 | return OldVal; |
| 630 | } |
| 631 | |
| 632 | /// \brief Create a map from block to value for the operands of a |
| 633 | /// given phi. |
| 634 | /// |
| 635 | /// Create a map from block to value for each non-undef value flowing |
| 636 | /// into \p PN. |
| 637 | /// |
| 638 | /// \param PN The phi we are collecting the map for. |
| 639 | /// \param IncomingValues [out] The map from block to value for this phi. |
| 640 | static void gatherIncomingValuesToPhi(PHINode *PN, |
| 641 | IncomingValueMap &IncomingValues) { |
| 642 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 643 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 644 | Value *V = PN->getIncomingValue(i); |
| 645 | |
| 646 | if (!isa<UndefValue>(V)) |
| 647 | IncomingValues.insert(std::make_pair(BB, V)); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /// \brief Replace the incoming undef values to a phi with the values |
| 652 | /// from a block-to-value map. |
| 653 | /// |
| 654 | /// \param PN The phi we are replacing the undefs in. |
| 655 | /// \param IncomingValues A map from block to value. |
| 656 | static void replaceUndefValuesInPhi(PHINode *PN, |
| 657 | const IncomingValueMap &IncomingValues) { |
| 658 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 659 | Value *V = PN->getIncomingValue(i); |
| 660 | |
| 661 | if (!isa<UndefValue>(V)) continue; |
| 662 | |
| 663 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 664 | IncomingValueMap::const_iterator It = IncomingValues.find(BB); |
| 665 | if (It == IncomingValues.end()) continue; |
| 666 | |
| 667 | PN->setIncomingValue(i, It->second); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /// \brief Replace a value flowing from a block to a phi with |
| 672 | /// potentially multiple instances of that value flowing from the |
| 673 | /// block's predecessors to the phi. |
| 674 | /// |
| 675 | /// \param BB The block with the value flowing into the phi. |
| 676 | /// \param BBPreds The predecessors of BB. |
| 677 | /// \param PN The phi that we are updating. |
| 678 | static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB, |
| 679 | const PredBlockVector &BBPreds, |
| 680 | PHINode *PN) { |
| 681 | Value *OldVal = PN->removeIncomingValue(BB, false); |
| 682 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 683 | |
| 684 | IncomingValueMap IncomingValues; |
| 685 | |
| 686 | // We are merging two blocks - BB, and the block containing PN - and |
| 687 | // as a result we need to redirect edges from the predecessors of BB |
| 688 | // to go to the block containing PN, and update PN |
| 689 | // accordingly. Since we allow merging blocks in the case where the |
| 690 | // predecessor and successor blocks both share some predecessors, |
| 691 | // and where some of those common predecessors might have undef |
| 692 | // values flowing into PN, we want to rewrite those values to be |
| 693 | // consistent with the non-undef values. |
| 694 | |
| 695 | gatherIncomingValuesToPhi(PN, IncomingValues); |
| 696 | |
| 697 | // If this incoming value is one of the PHI nodes in BB, the new entries |
| 698 | // in the PHI node are the entries from the old PHI. |
| 699 | if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { |
| 700 | PHINode *OldValPN = cast<PHINode>(OldVal); |
| 701 | for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) { |
| 702 | // Note that, since we are merging phi nodes and BB and Succ might |
| 703 | // have common predecessors, we could end up with a phi node with |
| 704 | // identical incoming branches. This will be cleaned up later (and |
| 705 | // will trigger asserts if we try to clean it up now, without also |
| 706 | // simplifying the corresponding conditional branch). |
| 707 | BasicBlock *PredBB = OldValPN->getIncomingBlock(i); |
| 708 | Value *PredVal = OldValPN->getIncomingValue(i); |
| 709 | Value *Selected = selectIncomingValueForBlock(PredVal, PredBB, |
| 710 | IncomingValues); |
| 711 | |
| 712 | // And add a new incoming value for this predecessor for the |
| 713 | // newly retargeted branch. |
| 714 | PN->addIncoming(Selected, PredBB); |
| 715 | } |
| 716 | } else { |
| 717 | for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) { |
| 718 | // Update existing incoming values in PN for this |
| 719 | // predecessor of BB. |
| 720 | BasicBlock *PredBB = BBPreds[i]; |
| 721 | Value *Selected = selectIncomingValueForBlock(OldVal, PredBB, |
| 722 | IncomingValues); |
| 723 | |
| 724 | // And add a new incoming value for this predecessor for the |
| 725 | // newly retargeted branch. |
| 726 | PN->addIncoming(Selected, PredBB); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | replaceUndefValuesInPhi(PN, IncomingValues); |
| 731 | } |
| 732 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 733 | /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an |
| 734 | /// unconditional branch, and contains no instructions other than PHI nodes, |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 735 | /// potential side-effect free intrinsics and the branch. If possible, |
| 736 | /// eliminate BB by rewriting all the predecessors to branch to the successor |
| 737 | /// block and return true. If we can't transform, return false. |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 738 | bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) { |
Dan Gohman | e2c6d13 | 2010-08-14 00:29:42 +0000 | [diff] [blame] | 739 | assert(BB != &BB->getParent()->getEntryBlock() && |
| 740 | "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); |
| 741 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 742 | // We can't eliminate infinite loops. |
| 743 | BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); |
| 744 | if (BB == Succ) return false; |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 745 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 746 | // Check to see if merging these blocks would cause conflicts for any of the |
| 747 | // phi nodes in BB or Succ. If not, we can safely merge. |
| 748 | if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; |
| 749 | |
| 750 | // Check for cases where Succ has multiple predecessors and a PHI node in BB |
| 751 | // has uses which will not disappear when the PHI nodes are merged. It is |
| 752 | // possible to handle such cases, but difficult: it requires checking whether |
| 753 | // BB dominates Succ, which is non-trivial to calculate in the case where |
| 754 | // Succ has multiple predecessors. Also, it requires checking whether |
Alexey Samsonov | 3a19999 | 2012-12-24 08:52:53 +0000 | [diff] [blame] | 755 | // constructing the necessary self-referential PHI node doesn't introduce any |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 756 | // conflicts; this isn't too difficult, but the previous code for doing this |
| 757 | // was incorrect. |
| 758 | // |
| 759 | // Note that if this check finds a live use, BB dominates Succ, so BB is |
| 760 | // something like a loop pre-header (or rarely, a part of an irreducible CFG); |
| 761 | // folding the branch isn't profitable in that case anyway. |
| 762 | if (!Succ->getSinglePredecessor()) { |
| 763 | BasicBlock::iterator BBI = BB->begin(); |
| 764 | while (isa<PHINode>(*BBI)) { |
| 765 | for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end(); |
| 766 | UI != E; ++UI) { |
| 767 | if (PHINode* PN = dyn_cast<PHINode>(*UI)) { |
| 768 | if (PN->getIncomingBlock(UI) != BB) |
| 769 | return false; |
| 770 | } else { |
| 771 | return false; |
| 772 | } |
| 773 | } |
| 774 | ++BBI; |
| 775 | } |
| 776 | } |
| 777 | |
David Greene | fae7706 | 2010-01-05 01:26:57 +0000 | [diff] [blame] | 778 | DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 779 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 780 | if (isa<PHINode>(Succ->begin())) { |
| 781 | // If there is more than one pred of succ, and there are PHI nodes in |
| 782 | // the successor, then we need to add incoming edges for the PHI nodes |
| 783 | // |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 784 | const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB)); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 785 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 786 | // Loop over all of the PHI nodes in the successor of BB. |
| 787 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 788 | PHINode *PN = cast<PHINode>(I); |
Duncan Sands | c48b55a | 2013-07-11 08:28:20 +0000 | [diff] [blame] | 789 | |
| 790 | redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN); |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 793 | |
Rafael Espindola | 77a2c37 | 2011-06-30 20:14:24 +0000 | [diff] [blame] | 794 | if (Succ->getSinglePredecessor()) { |
| 795 | // BB is the only predecessor of Succ, so Succ will end up with exactly |
| 796 | // the same predecessors BB had. |
| 797 | |
| 798 | // Copy over any phi, debug or lifetime instruction. |
| 799 | BB->getTerminator()->eraseFromParent(); |
| 800 | Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList()); |
| 801 | } else { |
| 802 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 803 | // We explicitly check for such uses in CanPropagatePredecessorsForPHIs. |
| 804 | assert(PN->use_empty() && "There shouldn't be any uses here!"); |
| 805 | PN->eraseFromParent(); |
| 806 | } |
| 807 | } |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 808 | |
Chris Lattner | dce94d9 | 2009-11-10 05:59:26 +0000 | [diff] [blame] | 809 | // Everything that jumped to BB now goes to Succ. |
| 810 | BB->replaceAllUsesWith(Succ); |
| 811 | if (!Succ->hasName()) Succ->takeName(BB); |
| 812 | BB->eraseFromParent(); // Delete the old basic block. |
| 813 | return true; |
| 814 | } |
| 815 | |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 816 | /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI |
| 817 | /// nodes in this block. This doesn't try to be clever about PHI nodes |
| 818 | /// which differ only in the order of the incoming values, but instcombine |
| 819 | /// orders them so it usually won't matter. |
| 820 | /// |
| 821 | bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { |
| 822 | bool Changed = false; |
| 823 | |
| 824 | // This implementation doesn't currently consider undef operands |
Nick Lewycky | 89991d4 | 2011-06-28 03:57:31 +0000 | [diff] [blame] | 825 | // specially. Theoretically, two phis which are identical except for |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 826 | // one having an undef where the other doesn't could be collapsed. |
| 827 | |
| 828 | // Map from PHI hash values to PHI nodes. If multiple PHIs have |
| 829 | // the same hash value, the element is the first PHI in the |
| 830 | // linked list in CollisionMap. |
| 831 | DenseMap<uintptr_t, PHINode *> HashMap; |
| 832 | |
| 833 | // Maintain linked lists of PHI nodes with common hash values. |
| 834 | DenseMap<PHINode *, PHINode *> CollisionMap; |
| 835 | |
| 836 | // Examine each PHI. |
| 837 | for (BasicBlock::iterator I = BB->begin(); |
| 838 | PHINode *PN = dyn_cast<PHINode>(I++); ) { |
| 839 | // Compute a hash value on the operands. Instcombine will likely have sorted |
| 840 | // them, which helps expose duplicates, but we have to check all the |
| 841 | // operands to be safe in case instcombine hasn't run. |
| 842 | uintptr_t Hash = 0; |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 843 | // This hash algorithm is quite weak as hash functions go, but it seems |
| 844 | // to do a good enough job for this particular purpose, and is very quick. |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 845 | for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) { |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 846 | Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I)); |
| 847 | Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); |
| 848 | } |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 849 | for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end(); |
| 850 | I != E; ++I) { |
| 851 | Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I)); |
| 852 | Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); |
| 853 | } |
Jakob Stoklund Olesen | 2bc2a08 | 2011-03-04 02:48:56 +0000 | [diff] [blame] | 854 | // Avoid colliding with the DenseMap sentinels ~0 and ~0-1. |
| 855 | Hash >>= 1; |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 856 | // If we've never seen this hash value before, it's a unique PHI. |
| 857 | std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair = |
| 858 | HashMap.insert(std::make_pair(Hash, PN)); |
| 859 | if (Pair.second) continue; |
| 860 | // Otherwise it's either a duplicate or a hash collision. |
| 861 | for (PHINode *OtherPN = Pair.first->second; ; ) { |
| 862 | if (OtherPN->isIdenticalTo(PN)) { |
| 863 | // A duplicate. Replace this PHI with its duplicate. |
| 864 | PN->replaceAllUsesWith(OtherPN); |
| 865 | PN->eraseFromParent(); |
| 866 | Changed = true; |
| 867 | break; |
| 868 | } |
| 869 | // A non-duplicate hash collision. |
| 870 | DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN); |
| 871 | if (I == CollisionMap.end()) { |
| 872 | // Set this PHI to be the head of the linked list of colliding PHIs. |
| 873 | PHINode *Old = Pair.first->second; |
| 874 | Pair.first->second = PN; |
| 875 | CollisionMap[PN] = Old; |
| 876 | break; |
| 877 | } |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 878 | // Proceed to the next PHI in the list. |
Jim Grosbach | 43a8241 | 2009-12-02 17:06:45 +0000 | [diff] [blame] | 879 | OtherPN = I->second; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return Changed; |
| 884 | } |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 885 | |
| 886 | /// enforceKnownAlignment - If the specified pointer points to an object that |
| 887 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 888 | /// often possible though. If alignment is important, a more reliable approach |
| 889 | /// is to simply align all global variables and allocation instructions to |
| 890 | /// their preferred alignment from the beginning. |
| 891 | /// |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 892 | static unsigned enforceKnownAlignment(Value *V, unsigned Align, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 893 | unsigned PrefAlign, const DataLayout *TD) { |
Eli Friedman | b53c793 | 2011-06-15 21:08:25 +0000 | [diff] [blame] | 894 | V = V->stripPointerCasts(); |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 895 | |
Eli Friedman | b53c793 | 2011-06-15 21:08:25 +0000 | [diff] [blame] | 896 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
Lang Hames | bb5b3f3 | 2011-10-10 23:42:08 +0000 | [diff] [blame] | 897 | // If the preferred alignment is greater than the natural stack alignment |
| 898 | // then don't round up. This avoids dynamic stack realignment. |
| 899 | if (TD && TD->exceedsNaturalStackAlignment(PrefAlign)) |
| 900 | return Align; |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 901 | // If there is a requested alignment and if this is an alloca, round up. |
| 902 | if (AI->getAlignment() >= PrefAlign) |
| 903 | return AI->getAlignment(); |
| 904 | AI->setAlignment(PrefAlign); |
| 905 | return PrefAlign; |
| 906 | } |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 907 | |
| 908 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 909 | // If there is a large requested alignment and we can, bump up the alignment |
| 910 | // of the global. |
| 911 | if (GV->isDeclaration()) return Align; |
Duncan Sands | d3a38cc | 2011-11-29 18:26:38 +0000 | [diff] [blame] | 912 | // If the memory we set aside for the global may not be the memory used by |
| 913 | // the final program then it is impossible for us to reliably enforce the |
| 914 | // preferred alignment. |
| 915 | if (GV->isWeakForLinker()) return Align; |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 917 | if (GV->getAlignment() >= PrefAlign) |
| 918 | return GV->getAlignment(); |
| 919 | // We can only increase the alignment of the global if it has no alignment |
| 920 | // specified or if it is not assigned a section. If it is assigned a |
| 921 | // section, the global could be densely packed with other objects in the |
| 922 | // section, increasing the alignment could cause padding issues. |
| 923 | if (!GV->hasSection() || GV->getAlignment() == 0) |
| 924 | GV->setAlignment(PrefAlign); |
| 925 | return GV->getAlignment(); |
| 926 | } |
| 927 | |
| 928 | return Align; |
| 929 | } |
| 930 | |
| 931 | /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 932 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 933 | /// and it is more than the alignment of the ultimate object, see if we can |
| 934 | /// increase the alignment of the ultimate object, making this check succeed. |
| 935 | unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, |
Matt Arsenault | 186f8f9 | 2013-08-01 22:42:18 +0000 | [diff] [blame] | 936 | const DataLayout *DL) { |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 937 | assert(V->getType()->isPointerTy() && |
| 938 | "getOrEnforceKnownAlignment expects a pointer!"); |
Matt Arsenault | 186f8f9 | 2013-08-01 22:42:18 +0000 | [diff] [blame] | 939 | unsigned BitWidth = DL ? DL->getPointerTypeSizeInBits(V->getType()) : 64; |
| 940 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 941 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Matt Arsenault | 186f8f9 | 2013-08-01 22:42:18 +0000 | [diff] [blame] | 942 | ComputeMaskedBits(V, KnownZero, KnownOne, DL); |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 943 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 944 | |
Matt Arsenault | 59a3878 | 2013-07-23 22:20:57 +0000 | [diff] [blame] | 945 | // Avoid trouble with ridiculously large TrailZ values, such as |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 946 | // those computed from a null pointer. |
| 947 | TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1)); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 949 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 951 | // LLVM doesn't support alignments larger than this currently. |
| 952 | Align = std::min(Align, +Value::MaximumAlignment); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 953 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 954 | if (PrefAlign > Align) |
Matt Arsenault | 186f8f9 | 2013-08-01 22:42:18 +0000 | [diff] [blame] | 955 | Align = enforceKnownAlignment(V, Align, PrefAlign, DL); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 957 | // We don't need to make any adjustment. |
| 958 | return Align; |
| 959 | } |
| 960 | |
Devang Patel | 5ee2068 | 2011-03-17 21:58:19 +0000 | [diff] [blame] | 961 | ///===---------------------------------------------------------------------===// |
| 962 | /// Dbg Intrinsic utilities |
| 963 | /// |
| 964 | |
Adrian Prantl | 163da93 | 2013-04-26 17:48:33 +0000 | [diff] [blame] | 965 | /// See if there is a dbg.value intrinsic for DIVar before I. |
| 966 | static bool LdStHasDebugValue(DIVariable &DIVar, Instruction *I) { |
| 967 | // Since we can't guarantee that the original dbg.declare instrinsic |
| 968 | // is removed by LowerDbgDeclare(), we need to make sure that we are |
| 969 | // not inserting the same dbg.value intrinsic over and over. |
| 970 | llvm::BasicBlock::InstListType::iterator PrevI(I); |
| 971 | if (PrevI != I->getParent()->getInstList().begin()) { |
| 972 | --PrevI; |
| 973 | if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI)) |
| 974 | if (DVI->getValue() == I->getOperand(0) && |
| 975 | DVI->getOffset() == 0 && |
| 976 | DVI->getVariable() == DIVar) |
| 977 | return true; |
| 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
Adrian Prantl | 9d5d58a | 2013-04-26 18:10:50 +0000 | [diff] [blame] | 982 | /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value |
Devang Patel | 5ee2068 | 2011-03-17 21:58:19 +0000 | [diff] [blame] | 983 | /// that has an associated llvm.dbg.decl intrinsic. |
| 984 | bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, |
| 985 | StoreInst *SI, DIBuilder &Builder) { |
| 986 | DIVariable DIVar(DDI->getVariable()); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 987 | assert((!DIVar || DIVar.isVariable()) && |
| 988 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
| 989 | if (!DIVar) |
Devang Patel | 5ee2068 | 2011-03-17 21:58:19 +0000 | [diff] [blame] | 990 | return false; |
| 991 | |
Adrian Prantl | 163da93 | 2013-04-26 17:48:33 +0000 | [diff] [blame] | 992 | if (LdStHasDebugValue(DIVar, SI)) |
| 993 | return true; |
| 994 | |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 995 | Instruction *DbgVal = NULL; |
| 996 | // If an argument is zero extended then use argument directly. The ZExt |
| 997 | // may be zapped by an optimization pass in future. |
| 998 | Argument *ExtendedArg = NULL; |
| 999 | if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) |
| 1000 | ExtendedArg = dyn_cast<Argument>(ZExt->getOperand(0)); |
| 1001 | if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) |
| 1002 | ExtendedArg = dyn_cast<Argument>(SExt->getOperand(0)); |
| 1003 | if (ExtendedArg) |
| 1004 | DbgVal = Builder.insertDbgValueIntrinsic(ExtendedArg, 0, DIVar, SI); |
| 1005 | else |
| 1006 | DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar, SI); |
| 1007 | |
Devang Patel | 5ee2068 | 2011-03-17 21:58:19 +0000 | [diff] [blame] | 1008 | // Propagate any debug metadata from the store onto the dbg.value. |
| 1009 | DebugLoc SIDL = SI->getDebugLoc(); |
| 1010 | if (!SIDL.isUnknown()) |
| 1011 | DbgVal->setDebugLoc(SIDL); |
| 1012 | // Otherwise propagate debug metadata from dbg.declare. |
| 1013 | else |
| 1014 | DbgVal->setDebugLoc(DDI->getDebugLoc()); |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
Adrian Prantl | 9d5d58a | 2013-04-26 18:10:50 +0000 | [diff] [blame] | 1018 | /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value |
Devang Patel | 36fae67 | 2011-03-18 23:45:43 +0000 | [diff] [blame] | 1019 | /// that has an associated llvm.dbg.decl intrinsic. |
| 1020 | bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, |
| 1021 | LoadInst *LI, DIBuilder &Builder) { |
| 1022 | DIVariable DIVar(DDI->getVariable()); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 1023 | assert((!DIVar || DIVar.isVariable()) && |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 1024 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
| 1025 | if (!DIVar) |
Devang Patel | 36fae67 | 2011-03-18 23:45:43 +0000 | [diff] [blame] | 1026 | return false; |
| 1027 | |
Adrian Prantl | 163da93 | 2013-04-26 17:48:33 +0000 | [diff] [blame] | 1028 | if (LdStHasDebugValue(DIVar, LI)) |
| 1029 | return true; |
| 1030 | |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 1031 | Instruction *DbgVal = |
Devang Patel | 36fae67 | 2011-03-18 23:45:43 +0000 | [diff] [blame] | 1032 | Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, |
| 1033 | DIVar, LI); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 1034 | |
Devang Patel | 36fae67 | 2011-03-18 23:45:43 +0000 | [diff] [blame] | 1035 | // Propagate any debug metadata from the store onto the dbg.value. |
| 1036 | DebugLoc LIDL = LI->getDebugLoc(); |
| 1037 | if (!LIDL.isUnknown()) |
| 1038 | DbgVal->setDebugLoc(LIDL); |
| 1039 | // Otherwise propagate debug metadata from dbg.declare. |
| 1040 | else |
| 1041 | DbgVal->setDebugLoc(DDI->getDebugLoc()); |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
Devang Patel | 813c9a0 | 2011-03-17 22:18:16 +0000 | [diff] [blame] | 1045 | /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set |
| 1046 | /// of llvm.dbg.value intrinsics. |
| 1047 | bool llvm::LowerDbgDeclare(Function &F) { |
| 1048 | DIBuilder DIB(*F.getParent()); |
| 1049 | SmallVector<DbgDeclareInst *, 4> Dbgs; |
| 1050 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 1051 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) { |
| 1052 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) |
| 1053 | Dbgs.push_back(DDI); |
| 1054 | } |
| 1055 | if (Dbgs.empty()) |
| 1056 | return false; |
| 1057 | |
Craig Topper | 6227d5c | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 1058 | for (SmallVectorImpl<DbgDeclareInst *>::iterator I = Dbgs.begin(), |
Devang Patel | 813c9a0 | 2011-03-17 22:18:16 +0000 | [diff] [blame] | 1059 | E = Dbgs.end(); I != E; ++I) { |
| 1060 | DbgDeclareInst *DDI = *I; |
| 1061 | if (AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress())) { |
Adrian Prantl | 163da93 | 2013-04-26 17:48:33 +0000 | [diff] [blame] | 1062 | // We only remove the dbg.declare intrinsic if all uses are |
| 1063 | // converted to dbg.value intrinsics. |
Devang Patel | 81ad03c | 2011-04-28 20:32:02 +0000 | [diff] [blame] | 1064 | bool RemoveDDI = true; |
Devang Patel | 813c9a0 | 2011-03-17 22:18:16 +0000 | [diff] [blame] | 1065 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 1066 | UI != E; ++UI) |
| 1067 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) |
| 1068 | ConvertDebugDeclareToDebugValue(DDI, SI, DIB); |
Devang Patel | 36fae67 | 2011-03-18 23:45:43 +0000 | [diff] [blame] | 1069 | else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) |
| 1070 | ConvertDebugDeclareToDebugValue(DDI, LI, DIB); |
Devang Patel | 81ad03c | 2011-04-28 20:32:02 +0000 | [diff] [blame] | 1071 | else |
| 1072 | RemoveDDI = false; |
| 1073 | if (RemoveDDI) |
| 1074 | DDI->eraseFromParent(); |
Devang Patel | 813c9a0 | 2011-03-17 22:18:16 +0000 | [diff] [blame] | 1075 | } |
Devang Patel | 813c9a0 | 2011-03-17 22:18:16 +0000 | [diff] [blame] | 1076 | } |
| 1077 | return true; |
| 1078 | } |
Cameron Zwarich | c827939 | 2011-05-24 03:10:43 +0000 | [diff] [blame] | 1079 | |
| 1080 | /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the |
| 1081 | /// alloca 'V', if any. |
| 1082 | DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) { |
| 1083 | if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V)) |
| 1084 | for (Value::use_iterator UI = DebugNode->use_begin(), |
| 1085 | E = DebugNode->use_end(); UI != E; ++UI) |
| 1086 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(*UI)) |
| 1087 | return DDI; |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
Alexey Samsonov | 1afbb51 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 1091 | |
| 1092 | bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, |
| 1093 | DIBuilder &Builder) { |
| 1094 | DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI); |
| 1095 | if (!DDI) |
| 1096 | return false; |
| 1097 | DIVariable DIVar(DDI->getVariable()); |
Jakub Staszak | a18c574 | 2013-07-22 23:16:36 +0000 | [diff] [blame] | 1098 | assert((!DIVar || DIVar.isVariable()) && |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 1099 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
| 1100 | if (!DIVar) |
Alexey Samsonov | 1afbb51 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 1101 | return false; |
| 1102 | |
| 1103 | // Create a copy of the original DIDescriptor for user variable, appending |
| 1104 | // "deref" operation to a list of address elements, as new llvm.dbg.declare |
| 1105 | // will take a value storing address of the memory for variable, not |
| 1106 | // alloca itself. |
| 1107 | Type *Int64Ty = Type::getInt64Ty(AI->getContext()); |
| 1108 | SmallVector<Value*, 4> NewDIVarAddress; |
| 1109 | if (DIVar.hasComplexAddress()) { |
| 1110 | for (unsigned i = 0, n = DIVar.getNumAddrElements(); i < n; ++i) { |
| 1111 | NewDIVarAddress.push_back( |
| 1112 | ConstantInt::get(Int64Ty, DIVar.getAddrElement(i))); |
| 1113 | } |
| 1114 | } |
| 1115 | NewDIVarAddress.push_back(ConstantInt::get(Int64Ty, DIBuilder::OpDeref)); |
| 1116 | DIVariable NewDIVar = Builder.createComplexVariable( |
| 1117 | DIVar.getTag(), DIVar.getContext(), DIVar.getName(), |
| 1118 | DIVar.getFile(), DIVar.getLineNumber(), DIVar.getType(), |
| 1119 | NewDIVarAddress, DIVar.getArgNumber()); |
| 1120 | |
| 1121 | // Insert llvm.dbg.declare in the same basic block as the original alloca, |
| 1122 | // and remove old llvm.dbg.declare. |
| 1123 | BasicBlock *BB = AI->getParent(); |
| 1124 | Builder.insertDeclare(NewAllocaAddress, NewDIVar, BB); |
| 1125 | DDI->eraseFromParent(); |
| 1126 | return true; |
| 1127 | } |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1128 | |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1129 | /// changeToUnreachable - Insert an unreachable instruction before the specified |
| 1130 | /// instruction, making it and the rest of the code in the block dead. |
| 1131 | static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) { |
| 1132 | BasicBlock *BB = I->getParent(); |
| 1133 | // Loop over all of the successors, removing BB's entry from any PHI |
| 1134 | // nodes. |
| 1135 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 1136 | (*SI)->removePredecessor(BB); |
| 1137 | |
| 1138 | // Insert a call to llvm.trap right before this. This turns the undefined |
| 1139 | // behavior into a hard fail instead of falling through into random code. |
| 1140 | if (UseLLVMTrap) { |
| 1141 | Function *TrapFn = |
| 1142 | Intrinsic::getDeclaration(BB->getParent()->getParent(), Intrinsic::trap); |
| 1143 | CallInst *CallTrap = CallInst::Create(TrapFn, "", I); |
| 1144 | CallTrap->setDebugLoc(I->getDebugLoc()); |
| 1145 | } |
| 1146 | new UnreachableInst(I->getContext(), I); |
| 1147 | |
| 1148 | // All instructions after this are dead. |
| 1149 | BasicBlock::iterator BBI = I, BBE = BB->end(); |
| 1150 | while (BBI != BBE) { |
| 1151 | if (!BBI->use_empty()) |
| 1152 | BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); |
| 1153 | BB->getInstList().erase(BBI++); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /// changeToCall - Convert the specified invoke into a normal call. |
| 1158 | static void changeToCall(InvokeInst *II) { |
| 1159 | SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3); |
| 1160 | CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, "", II); |
| 1161 | NewCall->takeName(II); |
| 1162 | NewCall->setCallingConv(II->getCallingConv()); |
| 1163 | NewCall->setAttributes(II->getAttributes()); |
| 1164 | NewCall->setDebugLoc(II->getDebugLoc()); |
| 1165 | II->replaceAllUsesWith(NewCall); |
| 1166 | |
| 1167 | // Follow the call by a branch to the normal destination. |
| 1168 | BranchInst::Create(II->getNormalDest(), II); |
| 1169 | |
| 1170 | // Update PHI nodes in the unwind destination |
| 1171 | II->getUnwindDest()->removePredecessor(II->getParent()); |
| 1172 | II->eraseFromParent(); |
| 1173 | } |
| 1174 | |
| 1175 | static bool markAliveBlocks(BasicBlock *BB, |
| 1176 | SmallPtrSet<BasicBlock*, 128> &Reachable) { |
| 1177 | |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1178 | SmallVector<BasicBlock*, 128> Worklist; |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1179 | Worklist.push_back(BB); |
| 1180 | Reachable.insert(BB); |
| 1181 | bool Changed = false; |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1182 | do { |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1183 | BB = Worklist.pop_back_val(); |
| 1184 | |
| 1185 | // Do a quick scan of the basic block, turning any obviously unreachable |
| 1186 | // instructions into LLVM unreachable insts. The instruction combining pass |
| 1187 | // canonicalizes unreachable insts into stores to null or undef. |
| 1188 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){ |
| 1189 | if (CallInst *CI = dyn_cast<CallInst>(BBI)) { |
| 1190 | if (CI->doesNotReturn()) { |
| 1191 | // If we found a call to a no-return function, insert an unreachable |
| 1192 | // instruction after it. Make sure there isn't *already* one there |
| 1193 | // though. |
| 1194 | ++BBI; |
| 1195 | if (!isa<UnreachableInst>(BBI)) { |
| 1196 | // Don't insert a call to llvm.trap right before the unreachable. |
| 1197 | changeToUnreachable(BBI, false); |
| 1198 | Changed = true; |
| 1199 | } |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | // Store to undef and store to null are undefined and used to signal that |
| 1205 | // they should be changed to unreachable by passes that can't modify the |
| 1206 | // CFG. |
| 1207 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
| 1208 | // Don't touch volatile stores. |
| 1209 | if (SI->isVolatile()) continue; |
| 1210 | |
| 1211 | Value *Ptr = SI->getOperand(1); |
| 1212 | |
| 1213 | if (isa<UndefValue>(Ptr) || |
| 1214 | (isa<ConstantPointerNull>(Ptr) && |
| 1215 | SI->getPointerAddressSpace() == 0)) { |
| 1216 | changeToUnreachable(SI, true); |
| 1217 | Changed = true; |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // Turn invokes that call 'nounwind' functions into ordinary calls. |
| 1224 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 1225 | Value *Callee = II->getCalledValue(); |
| 1226 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 1227 | changeToUnreachable(II, true); |
| 1228 | Changed = true; |
| 1229 | } else if (II->doesNotThrow()) { |
| 1230 | if (II->use_empty() && II->onlyReadsMemory()) { |
| 1231 | // jump to the normal destination branch. |
| 1232 | BranchInst::Create(II->getNormalDest(), II); |
| 1233 | II->getUnwindDest()->removePredecessor(II->getParent()); |
| 1234 | II->eraseFromParent(); |
| 1235 | } else |
| 1236 | changeToCall(II); |
| 1237 | Changed = true; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | Changed |= ConstantFoldTerminator(BB, true); |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1242 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 1243 | if (Reachable.insert(*SI)) |
| 1244 | Worklist.push_back(*SI); |
| 1245 | } while (!Worklist.empty()); |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1246 | return Changed; |
| 1247 | } |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1248 | |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1249 | /// removeUnreachableBlocksFromFn - Remove blocks that are not reachable, even |
| 1250 | /// if they are in a dead cycle. Return true if a change was made, false |
| 1251 | /// otherwise. |
| 1252 | bool llvm::removeUnreachableBlocks(Function &F) { |
| 1253 | SmallPtrSet<BasicBlock*, 128> Reachable; |
| 1254 | bool Changed = markAliveBlocks(F.begin(), Reachable); |
| 1255 | |
| 1256 | // If there are unreachable blocks in the CFG... |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1257 | if (Reachable.size() == F.size()) |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1258 | return Changed; |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1259 | |
| 1260 | assert(Reachable.size() < F.size()); |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1261 | NumRemoved += F.size()-Reachable.size(); |
| 1262 | |
| 1263 | // Loop over all of the basic blocks that are not reachable, dropping all of |
| 1264 | // their internal references... |
| 1265 | for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) { |
| 1266 | if (Reachable.count(BB)) |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1267 | continue; |
| 1268 | |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1269 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1270 | if (Reachable.count(*SI)) |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1271 | (*SI)->removePredecessor(BB); |
| 1272 | BB->dropAllReferences(); |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1273 | } |
Evgeniy Stepanov | 7541cd3 | 2013-03-22 08:43:04 +0000 | [diff] [blame] | 1274 | |
Peter Collingbourne | 4f96b7e | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 1275 | for (Function::iterator I = ++F.begin(); I != F.end();) |
Evgeniy Stepanov | 7541cd3 | 2013-03-22 08:43:04 +0000 | [diff] [blame] | 1276 | if (!Reachable.count(I)) |
| 1277 | I = F.getBasicBlockList().erase(I); |
| 1278 | else |
| 1279 | ++I; |
| 1280 | |
Evgeniy Stepanov | 3333e66 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 1281 | return true; |
| 1282 | } |