Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1 | //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
Alkis Evlogimenos | 50c047d | 2004-01-04 23:09:24 +0000 | [diff] [blame] | 10 | // This file implements the TwoAddress instruction pass which is used |
| 11 | // by most register allocators. Two-Address instructions are rewritten |
| 12 | // from: |
| 13 | // |
| 14 | // A = B op C |
| 15 | // |
| 16 | // to: |
| 17 | // |
| 18 | // A = B |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 19 | // A op= C |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 20 | // |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 21 | // Note that if a register allocator chooses to use this pass, that it |
| 22 | // has to be capable of handling the non-SSA nature of these rewritten |
| 23 | // virtual registers. |
| 24 | // |
| 25 | // It is also worth noting that the duplicate operand of the two |
| 26 | // address instruction is removed. |
Chris Lattner | bd91c1c | 2004-01-31 21:07:15 +0000 | [diff] [blame] | 27 | // |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #define DEBUG_TYPE "twoaddrinstr" |
Chris Lattner | bd91c1c | 2004-01-31 21:07:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 32 | #include "llvm/Function.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/LiveVariables.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 35 | #include "llvm/CodeGen/MachineInstr.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/SSARegMap.h" |
| 37 | #include "llvm/Target/MRegisterInfo.h" |
| 38 | #include "llvm/Target/TargetInstrInfo.h" |
| 39 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/Statistic.h" |
| 43 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 46 | STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions"); |
| 47 | STATISTIC(NumCommuted , "Number of instructions commuted to coalesce"); |
| 48 | STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); |
Chris Lattner | bd91c1c | 2004-01-31 21:07:15 +0000 | [diff] [blame] | 49 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 50 | namespace { |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 51 | struct VISIBILITY_HIDDEN TwoAddressInstructionPass |
| 52 | : public MachineFunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 53 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 54 | TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {} |
| 55 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 57 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 58 | /// runOnMachineFunction - pass entry point |
| 59 | bool runOnMachineFunction(MachineFunction&); |
| 60 | }; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 61 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 62 | char TwoAddressInstructionPass::ID = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 63 | RegisterPass<TwoAddressInstructionPass> |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 64 | X("twoaddressinstruction", "Two-Address instruction pass"); |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 65 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 66 | |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 67 | const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo(); |
| 68 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 69 | void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 70 | AU.addRequired<LiveVariables>(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 71 | AU.addPreserved<LiveVariables>(); |
| 72 | AU.addPreservedID(PHIEliminationID); |
| 73 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// runOnMachineFunction - Reduce two-address instructions to two |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 77 | /// operands. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 78 | /// |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 79 | bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 80 | DOUT << "Machine Function\n"; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 81 | const TargetMachine &TM = MF.getTarget(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 82 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Evan Cheng | ba59a1e | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 83 | const MRegisterInfo &MRI = *TM.getRegisterInfo(); |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 84 | LiveVariables &LV = getAnalysis<LiveVariables>(); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 85 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 86 | bool MadeChange = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 87 | |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 88 | DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n"; |
| 89 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
Alkis Evlogimenos | 3a9986f | 2004-02-18 00:35:06 +0000 | [diff] [blame] | 90 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 91 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 92 | mbbi != mbbe; ++mbbi) { |
| 93 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
| 94 | mi != me; ++mi) { |
Evan Cheng | ba59a1e | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 95 | const TargetInstrDescriptor *TID = mi->getInstrDescriptor(); |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 96 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 97 | bool FirstTied = true; |
Evan Cheng | ba59a1e | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 98 | for (unsigned si = 1, e = TID->numOperands; si < e; ++si) { |
| 99 | int ti = TID->getOperandConstraint(si, TOI::TIED_TO); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 100 | if (ti == -1) |
| 101 | continue; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 102 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 103 | if (FirstTied) { |
| 104 | ++NumTwoAddressInstrs; |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 105 | DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 106 | } |
| 107 | FirstTied = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 108 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 109 | assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() && |
| 110 | mi->getOperand(si).isUse() && "two address instruction invalid"); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 111 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 112 | // if the two operands are the same we just remove the use |
| 113 | // and mark the def as def&use, otherwise we have to insert a copy. |
| 114 | if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) { |
| 115 | // rewrite: |
| 116 | // a = b op c |
| 117 | // to: |
| 118 | // a = b |
| 119 | // a = a op c |
| 120 | unsigned regA = mi->getOperand(ti).getReg(); |
| 121 | unsigned regB = mi->getOperand(si).getReg(); |
| 122 | |
| 123 | assert(MRegisterInfo::isVirtualRegister(regA) && |
| 124 | MRegisterInfo::isVirtualRegister(regB) && |
| 125 | "cannot update physical register live information"); |
Chris Lattner | 6b50767 | 2004-01-31 21:21:43 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 127 | #ifndef NDEBUG |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 128 | // First, verify that we don't have a use of a in the instruction (a = |
| 129 | // b + a for example) because our transformation will not work. This |
| 130 | // should never occur because we are in SSA form. |
| 131 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) |
| 132 | assert((int)i == ti || |
| 133 | !mi->getOperand(i).isRegister() || |
| 134 | mi->getOperand(i).getReg() != regA); |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 135 | #endif |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 136 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 137 | // If this instruction is not the killing user of B, see if we can |
| 138 | // rearrange the code to make it so. Making it the killing user will |
| 139 | // allow us to coalesce A and B together, eliminating the copy we are |
| 140 | // about to insert. |
| 141 | if (!LV.KillsRegister(mi, regB)) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 142 | // If this instruction is commutative, check to see if C dies. If |
| 143 | // so, swap the B and C operands. This makes the live ranges of A |
| 144 | // and C joinable. |
| 145 | // FIXME: This code also works for A := B op C instructions. |
Evan Cheng | 7bb175b | 2007-10-23 20:14:40 +0000 | [diff] [blame] | 146 | if ((TID->Flags & M_COMMUTABLE) && mi->getNumOperands() >= 3) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 147 | assert(mi->getOperand(3-si).isRegister() && |
| 148 | "Not a proper commutative instruction!"); |
| 149 | unsigned regC = mi->getOperand(3-si).getReg(); |
| 150 | if (LV.KillsRegister(mi, regC)) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 151 | DOUT << "2addr: COMMUTING : " << *mi; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 152 | MachineInstr *NewMI = TII.commuteInstruction(mi); |
| 153 | if (NewMI == 0) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 154 | DOUT << "2addr: COMMUTING FAILED!\n"; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 155 | } else { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 156 | DOUT << "2addr: COMMUTED TO: " << *NewMI; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 157 | // If the instruction changed to commute it, update livevar. |
| 158 | if (NewMI != mi) { |
| 159 | LV.instructionChanged(mi, NewMI); // Update live variables |
| 160 | mbbi->insert(mi, NewMI); // Insert the new inst |
| 161 | mbbi->erase(mi); // Nuke the old inst. |
| 162 | mi = NewMI; |
| 163 | } |
| 164 | |
| 165 | ++NumCommuted; |
| 166 | regB = regC; |
| 167 | goto InstructionRearranged; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | c71d694 | 2005-01-19 07:08:42 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 170 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 171 | |
| 172 | // If this instruction is potentially convertible to a true |
| 173 | // three-address instruction, |
Evan Cheng | b9d5e7c | 2007-10-20 04:01:47 +0000 | [diff] [blame] | 174 | if (TID->Flags & M_CONVERTIBLE_TO_3_ADDR) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 175 | // FIXME: This assumes there are no more operands which are tied |
| 176 | // to another register. |
| 177 | #ifndef NDEBUG |
Evan Cheng | ba59a1e | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 178 | for (unsigned i = si+1, e = TID->numOperands; i < e; ++i) |
| 179 | assert(TID->getOperandConstraint(i, TOI::TIED_TO) == -1); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Evan Cheng | ba59a1e | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 182 | if (MachineInstr *New = TII.convertToThreeAddress(mbbi, mi, LV)) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 183 | DOUT << "2addr: CONVERTING 2-ADDR: " << *mi; |
| 184 | DOUT << "2addr: TO 3-ADDR: " << *New; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 185 | mbbi->erase(mi); // Nuke the old inst. |
| 186 | mi = New; |
| 187 | ++NumConvertedTo3Addr; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 188 | // Done with this instruction. |
| 189 | break; |
| 190 | } |
Evan Cheng | b9d5e7c | 2007-10-20 04:01:47 +0000 | [diff] [blame] | 191 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 192 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 193 | |
| 194 | InstructionRearranged: |
| 195 | const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA); |
Evan Cheng | 9efce63 | 2007-09-26 06:25:56 +0000 | [diff] [blame] | 196 | MRI.copyRegToReg(*mbbi, mi, regA, regB, rc, rc); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 197 | |
| 198 | MachineBasicBlock::iterator prevMi = prior(mi); |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 199 | DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 200 | |
| 201 | // Update live variables for regA |
| 202 | LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA); |
| 203 | varInfo.DefInst = prevMi; |
| 204 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 205 | if (LV.removeVirtualRegisterKilled(regB, mbbi, mi)) |
| 206 | LV.addVirtualRegisterKilled(regB, prevMi); |
| 207 | |
| 208 | if (LV.removeVirtualRegisterDead(regB, mbbi, mi)) |
| 209 | LV.addVirtualRegisterDead(regB, prevMi); |
| 210 | |
| 211 | // replace all occurences of regB with regA |
| 212 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 213 | if (mi->getOperand(i).isRegister() && |
| 214 | mi->getOperand(i).getReg() == regB) |
| 215 | mi->getOperand(i).setReg(regA); |
| 216 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 217 | } |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 218 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 219 | assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse()); |
| 220 | mi->getOperand(ti).setReg(mi->getOperand(si).getReg()); |
| 221 | MadeChange = true; |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 222 | |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 223 | DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM)); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 224 | } |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | return MadeChange; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 229 | } |