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 | 9fa038d | 2007-01-30 23:13:49 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c5f52e6 | 2005-09-26 05:27:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 09233fb | 2004-04-21 16:11:40 +0000 | [diff] [blame] | 24 | #include <cerrno> |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 28 | // Local constant propagation... |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 29 | // |
| 30 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 31 | /// doConstantPropagation - If an instruction references constants, try to fold |
| 32 | /// them together... |
| 33 | /// |
Chris Lattner | 9fa038d | 2007-01-30 23:13:49 +0000 | [diff] [blame] | 34 | bool llvm::doConstantPropagation(BasicBlock::iterator &II, |
| 35 | const TargetData *TD) { |
| 36 | if (Constant *C = ConstantFoldInstruction(II, TD)) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 37 | // 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] | 38 | II->replaceAllUsesWith(C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 40 | // Remove the instruction from the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 41 | II = II->getParent()->getInstList().erase(II); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 49 | // constant value, convert it into an unconditional branch to the constant |
| 50 | // destination. |
| 51 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 52 | bool llvm::ConstantFoldTerminator(BasicBlock *BB) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 53 | TerminatorInst *T = BB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 55 | // Branch - See if we are conditional jumping on constant |
| 56 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 57 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
| 58 | BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0)); |
| 59 | BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1)); |
| 60 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 61 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 62 | // Are we branching on constant? |
| 63 | // YES. Change to unconditional branch... |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 64 | BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; |
| 65 | BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 66 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 67 | //cerr << "Function: " << T->getParent()->getParent() |
| 68 | // << "\nRemoving branch from " << T->getParent() |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 69 | // << "\n\nTo: " << OldDest << endl; |
| 70 | |
| 71 | // Let the basic block know that we are letting go of it. Based on this, |
| 72 | // it will adjust it's PHI nodes. |
| 73 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 74 | OldDest->removePredecessor(BI->getParent()); |
| 75 | |
| 76 | // Set the unconditional destination, and change the insn to be an |
| 77 | // unconditional branch. |
| 78 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 79 | return true; |
Chris Lattner | 342a9d1 | 2003-08-17 19:34:55 +0000 | [diff] [blame] | 80 | } else if (Dest2 == Dest1) { // Conditional branch to same location? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 81 | // This branch matches something like this: |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 82 | // br bool %cond, label %Dest, label %Dest |
| 83 | // and changes it into: br label %Dest |
| 84 | |
| 85 | // Let the basic block know that we are letting go of one copy of it. |
| 86 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 87 | Dest1->removePredecessor(BI->getParent()); |
| 88 | |
| 89 | // Change a conditional branch to unconditional. |
| 90 | BI->setUnconditionalDest(Dest1); |
| 91 | return true; |
| 92 | } |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 93 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
| 94 | // If we are switching on a constant, we can convert the switch into a |
| 95 | // single branch instruction! |
| 96 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 97 | BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 98 | BasicBlock *DefaultDest = TheOnlyDest; |
| 99 | assert(TheOnlyDest == SI->getDefaultDest() && |
| 100 | "Default destination is not successor #0?"); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 102 | // Figure out which case it goes to... |
| 103 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 104 | // Found case matching a constant operand? |
| 105 | if (SI->getSuccessorValue(i) == CI) { |
| 106 | TheOnlyDest = SI->getSuccessor(i); |
| 107 | break; |
| 108 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 110 | // Check to see if this branch is going to the same place as the default |
| 111 | // dest. If so, eliminate it as an explicit compare. |
| 112 | if (SI->getSuccessor(i) == DefaultDest) { |
| 113 | // Remove this entry... |
| 114 | DefaultDest->removePredecessor(SI->getParent()); |
| 115 | SI->removeCase(i); |
| 116 | --i; --e; // Don't skip an entry... |
| 117 | continue; |
| 118 | } |
| 119 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 120 | // Otherwise, check to see if the switch only branches to one destination. |
| 121 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 122 | // destinations. |
| 123 | if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 126 | if (CI && !TheOnlyDest) { |
| 127 | // Branching on a constant, but not any of the cases, go to the default |
| 128 | // successor. |
| 129 | TheOnlyDest = SI->getDefaultDest(); |
| 130 | } |
| 131 | |
| 132 | // If we found a single destination that we can fold the switch into, do so |
| 133 | // now. |
| 134 | if (TheOnlyDest) { |
| 135 | // Insert the new branch.. |
| 136 | new BranchInst(TheOnlyDest, SI); |
| 137 | BasicBlock *BB = SI->getParent(); |
| 138 | |
| 139 | // Remove entries from PHI nodes which we no longer branch to... |
| 140 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 141 | // Found case matching a constant operand? |
| 142 | BasicBlock *Succ = SI->getSuccessor(i); |
| 143 | if (Succ == TheOnlyDest) |
| 144 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 145 | else |
| 146 | Succ->removePredecessor(BB); |
| 147 | } |
| 148 | |
| 149 | // Delete the old switch... |
| 150 | BB->getInstList().erase(SI); |
| 151 | return true; |
| 152 | } else if (SI->getNumSuccessors() == 2) { |
| 153 | // Otherwise, we can fold this switch into a conditional branch |
| 154 | // instruction if it has only one non-default destination. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 155 | Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(), |
| 156 | SI->getSuccessorValue(1), "cond", SI); |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 157 | // Insert the new branch... |
| 158 | new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); |
| 159 | |
| 160 | // Delete the old switch... |
| 161 | SI->getParent()->getInstList().erase(SI); |
| 162 | return true; |
| 163 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 168 | |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | // Local dead code elimination... |
| 171 | // |
| 172 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 173 | bool llvm::isInstructionTriviallyDead(Instruction *I) { |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 174 | if (!I->use_empty() || isa<TerminatorInst>(I)) return false; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 175 | |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 176 | if (!I->mayWriteToMemory()) return true; |
| 177 | |
Chris Lattner | ec710c5 | 2005-05-06 05:27:34 +0000 | [diff] [blame] | 178 | return false; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // dceInstruction - Inspect the instruction at *BBI and figure out if it's |
| 182 | // [trivially] dead. If so, remove the instruction and update the iterator |
| 183 | // to point to the instruction that immediately succeeded the original |
| 184 | // instruction. |
| 185 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 186 | bool llvm::dceInstruction(BasicBlock::iterator &BBI) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 187 | // Look for un"used" definitions... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 188 | if (isInstructionTriviallyDead(BBI)) { |
| 189 | BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | return false; |
| 193 | } |