Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- Local.cpp - Functions to perform local transformations ------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 17 | #include "llvm/iTerminators.h" |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 18 | #include "llvm/iOperators.h" |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 19 | #include "llvm/iPHINode.h" |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 23 | // Local constant propagation... |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 24 | // |
| 25 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 26 | /// doConstantPropagation - If an instruction references constants, try to fold |
| 27 | /// them together... |
| 28 | /// |
| 29 | bool llvm::doConstantPropagation(BasicBlock::iterator &II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 30 | if (Constant *C = ConstantFoldInstruction(II)) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 31 | // 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] | 32 | II->replaceAllUsesWith(C); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 33 | |
| 34 | // Remove the instruction from the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 35 | II = II->getParent()->getInstList().erase(II); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 36 | return true; |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 42 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 43 | /// instruction. If successful, the constant result is returned, if not, null |
| 44 | /// is returned. Note that this function can only fail when attempting to fold |
| 45 | /// instructions like loads and stores, which have no constant expression form. |
| 46 | /// |
| 47 | Constant *llvm::ConstantFoldInstruction(Instruction *I) { |
| 48 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 49 | if (PN->getNumIncomingValues() == 0) |
| 50 | return Constant::getNullValue(PN->getType()); |
| 51 | |
| 52 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 53 | if (Result == 0) return 0; |
| 54 | |
| 55 | // Handle PHI nodes specially here... |
| 56 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 57 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 58 | return 0; // Not all the same incoming constants... |
| 59 | |
| 60 | // If we reach here, all incoming values are the same constant. |
| 61 | return Result; |
| 62 | } |
| 63 | |
| 64 | Constant *Op0 = 0, *Op1 = 0; |
| 65 | switch (I->getNumOperands()) { |
| 66 | default: |
| 67 | case 2: |
| 68 | Op1 = dyn_cast<Constant>(I->getOperand(1)); |
| 69 | if (Op1 == 0) return 0; // Not a constant?, can't fold |
| 70 | case 1: |
| 71 | Op0 = dyn_cast<Constant>(I->getOperand(0)); |
| 72 | if (Op0 == 0) return 0; // Not a constant?, can't fold |
| 73 | break; |
| 74 | case 0: return 0; |
| 75 | } |
| 76 | |
Chris Lattner | c6646eb | 2004-01-12 19:10:58 +0000 | [diff] [blame^] | 77 | if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 78 | return ConstantExpr::get(I->getOpcode(), Op0, Op1); |
| 79 | |
| 80 | switch (I->getOpcode()) { |
| 81 | default: return 0; |
| 82 | case Instruction::Cast: |
| 83 | return ConstantExpr::getCast(Op0, I->getType()); |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 84 | case Instruction::GetElementPtr: |
| 85 | std::vector<Constant*> IdxList; |
| 86 | IdxList.reserve(I->getNumOperands()-1); |
| 87 | if (Op1) IdxList.push_back(Op1); |
| 88 | for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i) |
| 89 | if (Constant *C = dyn_cast<Constant>(I->getOperand(i))) |
| 90 | IdxList.push_back(C); |
| 91 | else |
| 92 | return 0; // Non-constant operand |
| 93 | return ConstantExpr::getGetElementPtr(Op0, IdxList); |
| 94 | } |
| 95 | } |
| 96 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 97 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 98 | // constant value, convert it into an unconditional branch to the constant |
| 99 | // destination. |
| 100 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 101 | bool llvm::ConstantFoldTerminator(BasicBlock *BB) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 102 | TerminatorInst *T = BB->getTerminator(); |
| 103 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 104 | // Branch - See if we are conditional jumping on constant |
| 105 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 106 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
| 107 | BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0)); |
| 108 | BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1)); |
| 109 | |
| 110 | if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) { |
| 111 | // Are we branching on constant? |
| 112 | // YES. Change to unconditional branch... |
| 113 | BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2; |
| 114 | BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1; |
| 115 | |
| 116 | //cerr << "Function: " << T->getParent()->getParent() |
| 117 | // << "\nRemoving branch from " << T->getParent() |
| 118 | // << "\n\nTo: " << OldDest << endl; |
| 119 | |
| 120 | // Let the basic block know that we are letting go of it. Based on this, |
| 121 | // it will adjust it's PHI nodes. |
| 122 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 123 | OldDest->removePredecessor(BI->getParent()); |
| 124 | |
| 125 | // Set the unconditional destination, and change the insn to be an |
| 126 | // unconditional branch. |
| 127 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 128 | return true; |
Chris Lattner | 342a9d1 | 2003-08-17 19:34:55 +0000 | [diff] [blame] | 129 | } else if (Dest2 == Dest1) { // Conditional branch to same location? |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 130 | // This branch matches something like this: |
| 131 | // br bool %cond, label %Dest, label %Dest |
| 132 | // and changes it into: br label %Dest |
| 133 | |
| 134 | // Let the basic block know that we are letting go of one copy of it. |
| 135 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 136 | Dest1->removePredecessor(BI->getParent()); |
| 137 | |
| 138 | // Change a conditional branch to unconditional. |
| 139 | BI->setUnconditionalDest(Dest1); |
| 140 | return true; |
| 141 | } |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 142 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
| 143 | // If we are switching on a constant, we can convert the switch into a |
| 144 | // single branch instruction! |
| 145 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 146 | BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 147 | BasicBlock *DefaultDest = TheOnlyDest; |
| 148 | assert(TheOnlyDest == SI->getDefaultDest() && |
| 149 | "Default destination is not successor #0?"); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 151 | // Figure out which case it goes to... |
| 152 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 153 | // Found case matching a constant operand? |
| 154 | if (SI->getSuccessorValue(i) == CI) { |
| 155 | TheOnlyDest = SI->getSuccessor(i); |
| 156 | break; |
| 157 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 159 | // Check to see if this branch is going to the same place as the default |
| 160 | // dest. If so, eliminate it as an explicit compare. |
| 161 | if (SI->getSuccessor(i) == DefaultDest) { |
| 162 | // Remove this entry... |
| 163 | DefaultDest->removePredecessor(SI->getParent()); |
| 164 | SI->removeCase(i); |
| 165 | --i; --e; // Don't skip an entry... |
| 166 | continue; |
| 167 | } |
| 168 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 169 | // Otherwise, check to see if the switch only branches to one destination. |
| 170 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 171 | // destinations. |
| 172 | if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 175 | if (CI && !TheOnlyDest) { |
| 176 | // Branching on a constant, but not any of the cases, go to the default |
| 177 | // successor. |
| 178 | TheOnlyDest = SI->getDefaultDest(); |
| 179 | } |
| 180 | |
| 181 | // If we found a single destination that we can fold the switch into, do so |
| 182 | // now. |
| 183 | if (TheOnlyDest) { |
| 184 | // Insert the new branch.. |
| 185 | new BranchInst(TheOnlyDest, SI); |
| 186 | BasicBlock *BB = SI->getParent(); |
| 187 | |
| 188 | // Remove entries from PHI nodes which we no longer branch to... |
| 189 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 190 | // Found case matching a constant operand? |
| 191 | BasicBlock *Succ = SI->getSuccessor(i); |
| 192 | if (Succ == TheOnlyDest) |
| 193 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 194 | else |
| 195 | Succ->removePredecessor(BB); |
| 196 | } |
| 197 | |
| 198 | // Delete the old switch... |
| 199 | BB->getInstList().erase(SI); |
| 200 | return true; |
| 201 | } else if (SI->getNumSuccessors() == 2) { |
| 202 | // Otherwise, we can fold this switch into a conditional branch |
| 203 | // instruction if it has only one non-default destination. |
| 204 | Value *Cond = new SetCondInst(Instruction::SetEQ, SI->getCondition(), |
| 205 | SI->getSuccessorValue(1), "cond", SI); |
| 206 | // Insert the new branch... |
| 207 | new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); |
| 208 | |
| 209 | // Delete the old switch... |
| 210 | SI->getParent()->getInstList().erase(SI); |
| 211 | return true; |
| 212 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | // Local dead code elimination... |
| 221 | // |
| 222 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 223 | bool llvm::isInstructionTriviallyDead(Instruction *I) { |
Chris Lattner | f0a93ed | 2003-02-24 20:48:32 +0000 | [diff] [blame] | 224 | return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // dceInstruction - Inspect the instruction at *BBI and figure out if it's |
| 228 | // [trivially] dead. If so, remove the instruction and update the iterator |
| 229 | // to point to the instruction that immediately succeeded the original |
| 230 | // instruction. |
| 231 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 232 | bool llvm::dceInstruction(BasicBlock::iterator &BBI) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 233 | // Look for un"used" definitions... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 234 | if (isInstructionTriviallyDead(BBI)) { |
| 235 | BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | return false; |
| 239 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 240 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 241 | //===----------------------------------------------------------------------===// |
| 242 | // PHI Instruction Simplification |
| 243 | // |
| 244 | |
| 245 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 246 | /// value, return the value, otherwise return null. |
| 247 | /// |
| 248 | Value *llvm::hasConstantValue(PHINode *PN) { |
| 249 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 250 | if (PN->getNumIncomingValues() == 1) |
| 251 | return PN->getIncomingValue(0); |
| 252 | |
| 253 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 254 | // the PHI node with the incoming value. |
| 255 | // |
| 256 | Value *InVal = 0; |
| 257 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 258 | if (PN->getIncomingValue(i) != PN) // Not the PHI node itself... |
| 259 | if (InVal && PN->getIncomingValue(i) != InVal) |
| 260 | return 0; // Not the same, bail out. |
| 261 | else |
| 262 | InVal = PN->getIncomingValue(i); |
| 263 | |
| 264 | // The only case that could cause InVal to be null is if we have a PHI node |
| 265 | // that only has entries for itself. In this case, there is no entry into the |
| 266 | // loop, so kill the PHI. |
| 267 | // |
| 268 | if (InVal == 0) InVal = Constant::getNullValue(PN->getType()); |
| 269 | |
| 270 | // All of the incoming values are the same, return the value now. |
| 271 | return InVal; |
| 272 | } |