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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7822c2a | 2004-01-12 19:56:36 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | cf11035 | 2004-06-11 06:16:23 +0000 | [diff] [blame] | 19 | #include "llvm/Intrinsics.h" |
Chris Lattner | cbbc6b7 | 2005-10-27 16:34:00 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 22 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 09233fb | 2004-04-21 16:11:40 +0000 | [diff] [blame] | 23 | #include <cerrno> |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 27 | // Local constant propagation... |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 28 | // |
| 29 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 30 | /// doConstantPropagation - If an instruction references constants, try to fold |
| 31 | /// them together... |
| 32 | /// |
| 33 | bool llvm::doConstantPropagation(BasicBlock::iterator &II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 34 | if (Constant *C = ConstantFoldInstruction(II)) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 35 | // Replaces all of the uses of a variable with uses of the constant. |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 36 | II->replaceAllUsesWith(C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 38 | // Remove the instruction from the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 39 | II = II->getParent()->getInstList().erase(II); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 40 | return true; |
| 41 | } |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 46 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 47 | /// instruction. If successful, the constant result is returned, if not, null |
| 48 | /// is returned. Note that this function can only fail when attempting to fold |
| 49 | /// instructions like loads and stores, which have no constant expression form. |
| 50 | /// |
| 51 | Constant *llvm::ConstantFoldInstruction(Instruction *I) { |
| 52 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 53 | if (PN->getNumIncomingValues() == 0) |
| 54 | return Constant::getNullValue(PN->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 56 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 57 | if (Result == 0) return 0; |
| 58 | |
| 59 | // Handle PHI nodes specially here... |
| 60 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 61 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 62 | return 0; // Not all the same incoming constants... |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 64 | // If we reach here, all incoming values are the same constant. |
| 65 | return Result; |
| 66 | } |
| 67 | |
| 68 | Constant *Op0 = 0, *Op1 = 0; |
| 69 | switch (I->getNumOperands()) { |
| 70 | default: |
| 71 | case 2: |
| 72 | Op1 = dyn_cast<Constant>(I->getOperand(1)); |
| 73 | if (Op1 == 0) return 0; // Not a constant?, can't fold |
| 74 | case 1: |
| 75 | Op0 = dyn_cast<Constant>(I->getOperand(0)); |
| 76 | if (Op0 == 0) return 0; // Not a constant?, can't fold |
| 77 | break; |
| 78 | case 0: return 0; |
| 79 | } |
| 80 | |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 81 | if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) { |
| 82 | if (Constant *Op0 = dyn_cast<Constant>(I->getOperand(0))) |
| 83 | if (Constant *Op1 = dyn_cast<Constant>(I->getOperand(1))) |
| 84 | return ConstantExpr::get(I->getOpcode(), Op0, Op1); |
| 85 | return 0; // Operands not constants. |
| 86 | } |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 88 | // Scan the operand list, checking to see if the are all constants, if so, |
| 89 | // hand off to ConstantFoldInstOperands. |
| 90 | std::vector<Constant*> Ops; |
| 91 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 92 | if (Constant *Op = dyn_cast<Constant>(I->getOperand(i))) |
| 93 | Ops.push_back(Op); |
| 94 | else |
| 95 | return 0; // All operands not constant! |
| 96 | |
| 97 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops); |
| 98 | } |
| 99 | |
| 100 | /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the |
| 101 | /// specified opcode and operands. If successful, the constant result is |
| 102 | /// returned, if not, null is returned. Note that this function can fail when |
| 103 | /// attempting to fold instructions like loads and stores, which have no |
| 104 | /// constant expression form. |
| 105 | /// |
| 106 | Constant *llvm::ConstantFoldInstOperands(unsigned Opc, const Type *DestTy, |
| 107 | const std::vector<Constant*> &Ops) { |
| 108 | if (Opc >= Instruction::BinaryOpsBegin && Opc < Instruction::BinaryOpsEnd) |
| 109 | return ConstantExpr::get(Opc, Ops[0], Ops[1]); |
| 110 | |
| 111 | switch (Opc) { |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 112 | default: return 0; |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 113 | case Instruction::Call: |
| 114 | if (Function *F = dyn_cast<Function>(Ops[0])) { |
| 115 | if (canConstantFoldCallTo(F)) { |
| 116 | std::vector<Constant*> Args(Ops.begin()+1, Ops.end()); |
| 117 | return ConstantFoldCall(F, Args); |
| 118 | } |
| 119 | } |
| 120 | return 0; |
| 121 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 122 | case Instruction::LShr: |
| 123 | case Instruction::AShr: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 124 | return ConstantExpr::get(Opc, Ops[0], Ops[1]); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 125 | case Instruction::Trunc: |
| 126 | case Instruction::ZExt: |
| 127 | case Instruction::SExt: |
| 128 | case Instruction::FPTrunc: |
| 129 | case Instruction::FPExt: |
| 130 | case Instruction::UIToFP: |
| 131 | case Instruction::SIToFP: |
| 132 | case Instruction::FPToUI: |
| 133 | case Instruction::FPToSI: |
| 134 | case Instruction::PtrToInt: |
| 135 | case Instruction::IntToPtr: |
| 136 | case Instruction::BitCast: |
| 137 | return ConstantExpr::getCast(Opc, Ops[0], DestTy); |
Chris Lattner | 17fd273 | 2004-03-12 05:53:03 +0000 | [diff] [blame] | 138 | case Instruction::Select: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 139 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 140 | case Instruction::ExtractElement: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 141 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
Robert Bocchino | 956fd72 | 2006-01-17 20:07:07 +0000 | [diff] [blame] | 142 | case Instruction::InsertElement: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 143 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | 543abdf | 2006-04-08 01:19:12 +0000 | [diff] [blame] | 144 | case Instruction::ShuffleVector: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 145 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 146 | case Instruction::GetElementPtr: |
Chris Lattner | 4b30fcb | 2006-05-27 01:18:04 +0000 | [diff] [blame] | 147 | return ConstantExpr::getGetElementPtr(Ops[0], |
| 148 | std::vector<Constant*>(Ops.begin()+1, |
| 149 | Ops.end())); |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 153 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 154 | // constant value, convert it into an unconditional branch to the constant |
| 155 | // destination. |
| 156 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 157 | bool llvm::ConstantFoldTerminator(BasicBlock *BB) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 158 | TerminatorInst *T = BB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 160 | // Branch - See if we are conditional jumping on constant |
| 161 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 162 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
| 163 | BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0)); |
| 164 | BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1)); |
| 165 | |
| 166 | if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) { |
| 167 | // Are we branching on constant? |
| 168 | // YES. Change to unconditional branch... |
| 169 | BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2; |
| 170 | BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1; |
| 171 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 172 | //cerr << "Function: " << T->getParent()->getParent() |
| 173 | // << "\nRemoving branch from " << T->getParent() |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 174 | // << "\n\nTo: " << OldDest << endl; |
| 175 | |
| 176 | // Let the basic block know that we are letting go of it. Based on this, |
| 177 | // it will adjust it's PHI nodes. |
| 178 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 179 | OldDest->removePredecessor(BI->getParent()); |
| 180 | |
| 181 | // Set the unconditional destination, and change the insn to be an |
| 182 | // unconditional branch. |
| 183 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 184 | return true; |
Chris Lattner | 342a9d1 | 2003-08-17 19:34:55 +0000 | [diff] [blame] | 185 | } else if (Dest2 == Dest1) { // Conditional branch to same location? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 186 | // This branch matches something like this: |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 187 | // br bool %cond, label %Dest, label %Dest |
| 188 | // and changes it into: br label %Dest |
| 189 | |
| 190 | // Let the basic block know that we are letting go of one copy of it. |
| 191 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 192 | Dest1->removePredecessor(BI->getParent()); |
| 193 | |
| 194 | // Change a conditional branch to unconditional. |
| 195 | BI->setUnconditionalDest(Dest1); |
| 196 | return true; |
| 197 | } |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 198 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
| 199 | // If we are switching on a constant, we can convert the switch into a |
| 200 | // single branch instruction! |
| 201 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 202 | BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 203 | BasicBlock *DefaultDest = TheOnlyDest; |
| 204 | assert(TheOnlyDest == SI->getDefaultDest() && |
| 205 | "Default destination is not successor #0?"); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 207 | // Figure out which case it goes to... |
| 208 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 209 | // Found case matching a constant operand? |
| 210 | if (SI->getSuccessorValue(i) == CI) { |
| 211 | TheOnlyDest = SI->getSuccessor(i); |
| 212 | break; |
| 213 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 215 | // Check to see if this branch is going to the same place as the default |
| 216 | // dest. If so, eliminate it as an explicit compare. |
| 217 | if (SI->getSuccessor(i) == DefaultDest) { |
| 218 | // Remove this entry... |
| 219 | DefaultDest->removePredecessor(SI->getParent()); |
| 220 | SI->removeCase(i); |
| 221 | --i; --e; // Don't skip an entry... |
| 222 | continue; |
| 223 | } |
| 224 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 225 | // Otherwise, check to see if the switch only branches to one destination. |
| 226 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 227 | // destinations. |
| 228 | if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 231 | if (CI && !TheOnlyDest) { |
| 232 | // Branching on a constant, but not any of the cases, go to the default |
| 233 | // successor. |
| 234 | TheOnlyDest = SI->getDefaultDest(); |
| 235 | } |
| 236 | |
| 237 | // If we found a single destination that we can fold the switch into, do so |
| 238 | // now. |
| 239 | if (TheOnlyDest) { |
| 240 | // Insert the new branch.. |
| 241 | new BranchInst(TheOnlyDest, SI); |
| 242 | BasicBlock *BB = SI->getParent(); |
| 243 | |
| 244 | // Remove entries from PHI nodes which we no longer branch to... |
| 245 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 246 | // Found case matching a constant operand? |
| 247 | BasicBlock *Succ = SI->getSuccessor(i); |
| 248 | if (Succ == TheOnlyDest) |
| 249 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 250 | else |
| 251 | Succ->removePredecessor(BB); |
| 252 | } |
| 253 | |
| 254 | // Delete the old switch... |
| 255 | BB->getInstList().erase(SI); |
| 256 | return true; |
| 257 | } else if (SI->getNumSuccessors() == 2) { |
| 258 | // Otherwise, we can fold this switch into a conditional branch |
| 259 | // instruction if it has only one non-default destination. |
| 260 | Value *Cond = new SetCondInst(Instruction::SetEQ, SI->getCondition(), |
| 261 | SI->getSuccessorValue(1), "cond", SI); |
| 262 | // Insert the new branch... |
| 263 | new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); |
| 264 | |
| 265 | // Delete the old switch... |
| 266 | SI->getParent()->getInstList().erase(SI); |
| 267 | return true; |
| 268 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 273 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 274 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 275 | /// constant expression, or null if something is funny and we can't decide. |
| 276 | Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, |
| 277 | ConstantExpr *CE) { |
| 278 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
| 279 | return 0; // Do not allow stepping over the value! |
| 280 | |
| 281 | // Loop over all of the operands, tracking down which value we are |
| 282 | // addressing... |
| 283 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 284 | for (++I; I != E; ++I) |
| 285 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 286 | ConstantInt *CU = cast<ConstantInt>(I.getOperand()); |
| 287 | assert(CU->getZExtValue() < STy->getNumElements() && |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 288 | "Struct index out of range!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 289 | unsigned El = (unsigned)CU->getZExtValue(); |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 290 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 291 | C = CS->getOperand(El); |
| 292 | } else if (isa<ConstantAggregateZero>(C)) { |
| 293 | C = Constant::getNullValue(STy->getElementType(El)); |
| 294 | } else if (isa<UndefValue>(C)) { |
| 295 | C = UndefValue::get(STy->getElementType(El)); |
| 296 | } else { |
| 297 | return 0; |
| 298 | } |
| 299 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 300 | if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 301 | if (CI->getZExtValue() >= ATy->getNumElements()) |
Chris Lattner | 99f2af2 | 2006-05-25 21:25:12 +0000 | [diff] [blame] | 302 | return 0; |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 303 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 304 | C = CA->getOperand(CI->getZExtValue()); |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 305 | else if (isa<ConstantAggregateZero>(C)) |
| 306 | C = Constant::getNullValue(ATy->getElementType()); |
| 307 | else if (isa<UndefValue>(C)) |
| 308 | C = UndefValue::get(ATy->getElementType()); |
| 309 | else |
| 310 | return 0; |
| 311 | } else if (const PackedType *PTy = dyn_cast<PackedType>(*I)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 312 | if (CI->getZExtValue() >= PTy->getNumElements()) |
Chris Lattner | 99f2af2 | 2006-05-25 21:25:12 +0000 | [diff] [blame] | 313 | return 0; |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 314 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 315 | C = CP->getOperand(CI->getZExtValue()); |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 316 | else if (isa<ConstantAggregateZero>(C)) |
| 317 | C = Constant::getNullValue(PTy->getElementType()); |
| 318 | else if (isa<UndefValue>(C)) |
| 319 | C = UndefValue::get(PTy->getElementType()); |
| 320 | else |
| 321 | return 0; |
| 322 | } else { |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 323 | return 0; |
Robert Bocchino | d900c6a | 2006-01-19 23:53:23 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 325 | } else { |
| 326 | return 0; |
| 327 | } |
| 328 | return C; |
| 329 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // Local dead code elimination... |
| 334 | // |
| 335 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 336 | bool llvm::isInstructionTriviallyDead(Instruction *I) { |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 337 | if (!I->use_empty() || isa<TerminatorInst>(I)) return false; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 338 | |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 339 | if (!I->mayWriteToMemory()) return true; |
| 340 | |
| 341 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
Chris Lattner | aeebe7f | 2006-03-09 22:38:10 +0000 | [diff] [blame] | 342 | if (Function *F = CI->getCalledFunction()) { |
Chris Lattner | 7224f84 | 2006-04-02 03:35:01 +0000 | [diff] [blame] | 343 | unsigned IntrinsicID = F->getIntrinsicID(); |
Chris Lattner | aeebe7f | 2006-03-09 22:38:10 +0000 | [diff] [blame] | 344 | #define GET_SIDE_EFFECT_INFO |
| 345 | #include "llvm/Intrinsics.gen" |
| 346 | #undef GET_SIDE_EFFECT_INFO |
| 347 | } |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 348 | return false; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // dceInstruction - Inspect the instruction at *BBI and figure out if it's |
| 352 | // [trivially] dead. If so, remove the instruction and update the iterator |
| 353 | // to point to the instruction that immediately succeeded the original |
| 354 | // instruction. |
| 355 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 356 | bool llvm::dceInstruction(BasicBlock::iterator &BBI) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 357 | // Look for un"used" definitions... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 358 | if (isInstructionTriviallyDead(BBI)) { |
| 359 | BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |