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" |
Chris Lattner | 81ebc30 | 2004-01-12 18:35:03 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Devang Patel | c79e118 | 2009-03-06 00:19:37 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7822c2a | 2004-01-12 19:56:36 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | cf11035 | 2004-06-11 06:16:23 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 741c0ae | 2007-12-29 00:59:12 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | cbbc6b7 | 2005-10-27 16:34:00 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ConstantFolding.h" |
Devang Patel | c79e118 | 2009-03-06 00:19:37 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/DebugInfo.h" |
Chris Lattner | 9fa038d | 2007-01-30 23:13:49 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 31 | // Local constant propagation. |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 32 | // |
| 33 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 34 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 35 | // constant value, convert it into an unconditional branch to the constant |
| 36 | // destination. |
| 37 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 38 | bool llvm::ConstantFoldTerminator(BasicBlock *BB) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 39 | TerminatorInst *T = BB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 41 | // Branch - See if we are conditional jumping on constant |
| 42 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 43 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
Gabor Greif | c1bb13f | 2009-01-30 18:21:13 +0000 | [diff] [blame] | 44 | BasicBlock *Dest1 = BI->getSuccessor(0); |
| 45 | BasicBlock *Dest2 = BI->getSuccessor(1); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 46 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 47 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 48 | // Are we branching on constant? |
| 49 | // YES. Change to unconditional branch... |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 50 | BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; |
| 51 | BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 52 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 53 | //cerr << "Function: " << T->getParent()->getParent() |
| 54 | // << "\nRemoving branch from " << T->getParent() |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 55 | // << "\n\nTo: " << OldDest << endl; |
| 56 | |
| 57 | // Let the basic block know that we are letting go of it. Based on this, |
| 58 | // it will adjust it's PHI nodes. |
| 59 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 60 | OldDest->removePredecessor(BI->getParent()); |
| 61 | |
| 62 | // Set the unconditional destination, and change the insn to be an |
| 63 | // unconditional branch. |
| 64 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 65 | return true; |
Chris Lattner | 342a9d1 | 2003-08-17 19:34:55 +0000 | [diff] [blame] | 66 | } else if (Dest2 == Dest1) { // Conditional branch to same location? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 67 | // This branch matches something like this: |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 68 | // br bool %cond, label %Dest, label %Dest |
| 69 | // and changes it into: br label %Dest |
| 70 | |
| 71 | // Let the basic block know that we are letting go of one copy of it. |
| 72 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 73 | Dest1->removePredecessor(BI->getParent()); |
| 74 | |
| 75 | // Change a conditional branch to unconditional. |
| 76 | BI->setUnconditionalDest(Dest1); |
| 77 | return true; |
| 78 | } |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 79 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
| 80 | // If we are switching on a constant, we can convert the switch into a |
| 81 | // single branch instruction! |
| 82 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 83 | BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 84 | BasicBlock *DefaultDest = TheOnlyDest; |
| 85 | assert(TheOnlyDest == SI->getDefaultDest() && |
| 86 | "Default destination is not successor #0?"); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 88 | // Figure out which case it goes to... |
| 89 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 90 | // Found case matching a constant operand? |
| 91 | if (SI->getSuccessorValue(i) == CI) { |
| 92 | TheOnlyDest = SI->getSuccessor(i); |
| 93 | break; |
| 94 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 96 | // Check to see if this branch is going to the same place as the default |
| 97 | // dest. If so, eliminate it as an explicit compare. |
| 98 | if (SI->getSuccessor(i) == DefaultDest) { |
| 99 | // Remove this entry... |
| 100 | DefaultDest->removePredecessor(SI->getParent()); |
| 101 | SI->removeCase(i); |
| 102 | --i; --e; // Don't skip an entry... |
| 103 | continue; |
| 104 | } |
| 105 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 106 | // Otherwise, check to see if the switch only branches to one destination. |
| 107 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 108 | // destinations. |
| 109 | if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 112 | if (CI && !TheOnlyDest) { |
| 113 | // Branching on a constant, but not any of the cases, go to the default |
| 114 | // successor. |
| 115 | TheOnlyDest = SI->getDefaultDest(); |
| 116 | } |
| 117 | |
| 118 | // If we found a single destination that we can fold the switch into, do so |
| 119 | // now. |
| 120 | if (TheOnlyDest) { |
| 121 | // Insert the new branch.. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 122 | BranchInst::Create(TheOnlyDest, SI); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 123 | BasicBlock *BB = SI->getParent(); |
| 124 | |
| 125 | // Remove entries from PHI nodes which we no longer branch to... |
| 126 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 127 | // Found case matching a constant operand? |
| 128 | BasicBlock *Succ = SI->getSuccessor(i); |
| 129 | if (Succ == TheOnlyDest) |
| 130 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 131 | else |
| 132 | Succ->removePredecessor(BB); |
| 133 | } |
| 134 | |
| 135 | // Delete the old switch... |
| 136 | BB->getInstList().erase(SI); |
| 137 | return true; |
| 138 | } else if (SI->getNumSuccessors() == 2) { |
| 139 | // Otherwise, we can fold this switch into a conditional branch |
| 140 | // instruction if it has only one non-default destination. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 141 | Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(), |
| 142 | SI->getSuccessorValue(1), "cond", SI); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 143 | // Insert the new branch... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 144 | BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 145 | |
| 146 | // Delete the old switch... |
Dan Gohman | 1adec83 | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 147 | SI->eraseFromParent(); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 154 | |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | // Local dead code elimination... |
| 157 | // |
| 158 | |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 159 | /// isInstructionTriviallyDead - Return true if the result produced by the |
| 160 | /// instruction is not used, and the instruction has no side effects. |
| 161 | /// |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 162 | bool llvm::isInstructionTriviallyDead(Instruction *I) { |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 163 | if (!I->use_empty() || isa<TerminatorInst>(I)) return false; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 164 | |
Dale Johannesen | 127a793 | 2009-03-03 23:30:00 +0000 | [diff] [blame] | 165 | // We don't want debug info removed by anything this general. |
| 166 | if (isa<DbgInfoIntrinsic>(I)) return false; |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 167 | |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 168 | if (!I->mayHaveSideEffects()) return true; |
| 169 | |
| 170 | // Special case intrinsics that "may have side effects" but can be deleted |
| 171 | // when dead. |
Chris Lattner | 741c0ae | 2007-12-29 00:59:12 +0000 | [diff] [blame] | 172 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 173 | // Safe to delete llvm.stacksave if dead. |
| 174 | if (II->getIntrinsicID() == Intrinsic::stacksave) |
| 175 | return true; |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 176 | return false; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 179 | /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a |
| 180 | /// trivially dead instruction, delete it. If that makes any of its operands |
| 181 | /// trivially dead, delete them too, recursively. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 182 | void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) { |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 183 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 184 | if (!I || !I->use_empty() || !isInstructionTriviallyDead(I)) |
| 185 | return; |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 187 | SmallVector<Instruction*, 16> DeadInsts; |
| 188 | DeadInsts.push_back(I); |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 190 | while (!DeadInsts.empty()) { |
Dan Gohman | e9d87f4 | 2009-05-06 17:22:41 +0000 | [diff] [blame^] | 191 | I = DeadInsts.pop_back_val(); |
Chris Lattner | 2872177 | 2008-11-28 00:58:15 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 7605730 | 2008-11-28 01:20:46 +0000 | [diff] [blame] | 193 | // Null out all of the instruction's operands to see if any operand becomes |
| 194 | // dead as we go. |
| 195 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 196 | Value *OpV = I->getOperand(i); |
| 197 | I->setOperand(i, 0); |
| 198 | |
| 199 | if (!OpV->use_empty()) continue; |
| 200 | |
| 201 | // If the operand is an instruction that became dead as we nulled out the |
| 202 | // operand, and if it is 'trivially' dead, delete it in a future loop |
| 203 | // iteration. |
| 204 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) |
| 205 | if (isInstructionTriviallyDead(OpI)) |
| 206 | DeadInsts.push_back(OpI); |
| 207 | } |
| 208 | |
| 209 | I->eraseFromParent(); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 211 | } |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 212 | |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 213 | /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively |
| 214 | /// dead PHI node, due to being a def-use chain of single-use nodes that |
| 215 | /// either forms a cycle or is terminated by a trivially dead instruction, |
| 216 | /// delete it. If that makes any of its operands trivially dead, delete them |
| 217 | /// too, recursively. |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 218 | void |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 219 | llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) { |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 220 | |
| 221 | // We can remove a PHI if it is on a cycle in the def-use graph |
| 222 | // where each node in the cycle has degree one, i.e. only one use, |
| 223 | // and is an instruction with no side effects. |
| 224 | if (!PN->hasOneUse()) |
| 225 | return; |
| 226 | |
| 227 | SmallPtrSet<PHINode *, 4> PHIs; |
| 228 | PHIs.insert(PN); |
| 229 | for (Instruction *J = cast<Instruction>(*PN->use_begin()); |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 230 | J->hasOneUse() && !J->mayHaveSideEffects(); |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 231 | J = cast<Instruction>(*J->use_begin())) |
| 232 | // If we find a PHI more than once, we're on a cycle that |
| 233 | // won't prove fruitful. |
| 234 | if (PHINode *JP = dyn_cast<PHINode>(J)) |
| 235 | if (!PHIs.insert(cast<PHINode>(JP))) { |
| 236 | // Break the cycle and delete the PHI and its operands. |
| 237 | JP->replaceAllUsesWith(UndefValue::get(JP->getType())); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 238 | RecursivelyDeleteTriviallyDeadInstructions(JP); |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 242 | |
Chris Lattner | b29714a | 2008-11-27 07:43:12 +0000 | [diff] [blame] | 243 | //===----------------------------------------------------------------------===// |
| 244 | // Control Flow Graph Restructuring... |
| 245 | // |
| 246 | |
| 247 | /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its |
| 248 | /// predecessor is known to have one successor (DestBB!). Eliminate the edge |
| 249 | /// between them, moving the instructions in the predecessor into DestBB and |
| 250 | /// deleting the predecessor block. |
| 251 | /// |
| 252 | void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB) { |
| 253 | // If BB has single-entry PHI nodes, fold them. |
| 254 | while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { |
| 255 | Value *NewVal = PN->getIncomingValue(0); |
| 256 | // Replace self referencing PHI with undef, it must be dead. |
| 257 | if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); |
| 258 | PN->replaceAllUsesWith(NewVal); |
| 259 | PN->eraseFromParent(); |
| 260 | } |
| 261 | |
| 262 | BasicBlock *PredBB = DestBB->getSinglePredecessor(); |
| 263 | assert(PredBB && "Block doesn't have a single predecessor!"); |
| 264 | |
| 265 | // Splice all the instructions from PredBB to DestBB. |
| 266 | PredBB->getTerminator()->eraseFromParent(); |
| 267 | DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); |
| 268 | |
| 269 | // Anything that branched to PredBB now branches to DestBB. |
| 270 | PredBB->replaceAllUsesWith(DestBB); |
| 271 | |
| 272 | // Nuke BB. |
| 273 | PredBB->eraseFromParent(); |
| 274 | } |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 275 | |
| 276 | /// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used |
| 277 | /// by DbgIntrinsics. If DbgInUses is specified then the vector is filled |
| 278 | /// with the DbgInfoIntrinsic that use the instruction I. |
| 279 | bool llvm::OnlyUsedByDbgInfoIntrinsics(Instruction *I, |
| 280 | SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses) { |
| 281 | if (DbgInUses) |
| 282 | DbgInUses->clear(); |
| 283 | |
| 284 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE; |
| 285 | ++UI) { |
| 286 | if (DbgInfoIntrinsic *DI = dyn_cast<DbgInfoIntrinsic>(*UI)) { |
| 287 | if (DbgInUses) |
| 288 | DbgInUses->push_back(DI); |
| 289 | } else { |
| 290 | if (DbgInUses) |
| 291 | DbgInUses->clear(); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | return true; |
| 296 | } |
Devang Patel | c79e118 | 2009-03-06 00:19:37 +0000 | [diff] [blame] | 297 | |
| 298 | /// UserIsDebugInfo - Return true if U is a constant expr used by |
| 299 | /// llvm.dbg.variable or llvm.dbg.global_variable |
| 300 | bool llvm::UserIsDebugInfo(User *U) { |
| 301 | ConstantExpr *CE = dyn_cast<ConstantExpr>(U); |
| 302 | |
| 303 | if (!CE || CE->getNumUses() != 1) |
| 304 | return false; |
| 305 | |
| 306 | Constant *Init = dyn_cast<Constant>(CE->use_back()); |
| 307 | if (!Init || Init->getNumUses() != 1) |
| 308 | return false; |
| 309 | |
| 310 | GlobalVariable *GV = dyn_cast<GlobalVariable>(Init->use_back()); |
| 311 | if (!GV || !GV->hasInitializer() || GV->getInitializer() != Init) |
| 312 | return false; |
| 313 | |
| 314 | DIVariable DV(GV); |
| 315 | if (!DV.isNull()) |
| 316 | return true; // User is llvm.dbg.variable |
| 317 | |
| 318 | DIGlobalVariable DGV(GV); |
| 319 | if (!DGV.isNull()) |
| 320 | return true; // User is llvm.dbg.global_variable |
| 321 | |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | /// RemoveDbgInfoUser - Remove an User which is representing debug info. |
| 326 | void llvm::RemoveDbgInfoUser(User *U) { |
| 327 | assert (UserIsDebugInfo(U) && "Unexpected User!"); |
| 328 | ConstantExpr *CE = cast<ConstantExpr>(U); |
| 329 | while (!CE->use_empty()) { |
| 330 | Constant *C = cast<Constant>(CE->use_back()); |
| 331 | while (!C->use_empty()) { |
| 332 | GlobalVariable *GV = cast<GlobalVariable>(C->use_back()); |
| 333 | GV->eraseFromParent(); |
| 334 | } |
| 335 | C->destroyConstant(); |
| 336 | } |
| 337 | CE->destroyConstant(); |
| 338 | } |