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