Misha Brukman | a85d6bc | 2002-11-22 22:42:50 +0000 | [diff] [blame] | 1 | //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===// |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +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 | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 10 | // This file contains the X86 implementation of the TargetInstrInfo class. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 055c965 | 2002-10-29 21:05:24 +0000 | [diff] [blame] | 14 | #include "X86InstrInfo.h" |
Chris Lattner | 4ce42a7 | 2002-12-03 05:42:53 +0000 | [diff] [blame] | 15 | #include "X86.h" |
Chris Lattner | abf05b2 | 2003-08-03 21:55:55 +0000 | [diff] [blame] | 16 | #include "X86GenInstrInfo.inc" |
Evan Cheng | aa3c141 | 2006-05-30 21:45:53 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
| 18 | #include "X86Subtarget.h" |
| 19 | #include "X86TargetMachine.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveVariables.h" |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Evan Cheng | aa3c141 | 2006-05-30 21:45:53 +0000 | [diff] [blame] | 24 | X86InstrInfo::X86InstrInfo(X86TargetMachine &tm) |
| 25 | : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])), |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 26 | TM(tm), RI(tm, *this) { |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Alkis Evlogimenos | 5e30002 | 2003-12-28 17:35:08 +0000 | [diff] [blame] | 29 | bool X86InstrInfo::isMoveInstr(const MachineInstr& MI, |
| 30 | unsigned& sourceReg, |
| 31 | unsigned& destReg) const { |
| 32 | MachineOpCode oc = MI.getOpcode(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 33 | if (oc == X86::MOV8rr || oc == X86::MOV16rr || |
| 34 | oc == X86::MOV32rr || oc == X86::MOV64rr || |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 35 | oc == X86::MOV16to16_ || oc == X86::MOV32to32_ || |
Evan Cheng | bda54cd | 2006-02-01 23:03:16 +0000 | [diff] [blame] | 36 | oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr || |
Evan Cheng | fe5cb19 | 2006-02-16 22:45:17 +0000 | [diff] [blame] | 37 | oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr || |
Evan Cheng | 82521dd | 2006-03-21 07:09:35 +0000 | [diff] [blame] | 38 | oc == X86::MOVAPSrr || oc == X86::MOVAPDrr || |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 39 | oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr || |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 40 | oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr || |
Bill Wendling | 823efee | 2007-04-03 06:00:37 +0000 | [diff] [blame^] | 41 | oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) { |
Alkis Evlogimenos | 5e30002 | 2003-12-28 17:35:08 +0000 | [diff] [blame] | 42 | assert(MI.getNumOperands() == 2 && |
| 43 | MI.getOperand(0).isRegister() && |
| 44 | MI.getOperand(1).isRegister() && |
| 45 | "invalid register-register move instruction"); |
Alkis Evlogimenos | be766c7 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 46 | sourceReg = MI.getOperand(1).getReg(); |
| 47 | destReg = MI.getOperand(0).getReg(); |
Alkis Evlogimenos | 5e30002 | 2003-12-28 17:35:08 +0000 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
Alkis Evlogimenos | 36f506e | 2004-07-31 09:38:47 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 53 | unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI, |
| 54 | int &FrameIndex) const { |
| 55 | switch (MI->getOpcode()) { |
| 56 | default: break; |
| 57 | case X86::MOV8rm: |
| 58 | case X86::MOV16rm: |
Evan Cheng | f4df680 | 2006-05-11 07:33:49 +0000 | [diff] [blame] | 59 | case X86::MOV16_rm: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 60 | case X86::MOV32rm: |
Evan Cheng | f4df680 | 2006-05-11 07:33:49 +0000 | [diff] [blame] | 61 | case X86::MOV32_rm: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 62 | case X86::MOV64rm: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 63 | case X86::FpLD64m: |
| 64 | case X86::MOVSSrm: |
| 65 | case X86::MOVSDrm: |
Chris Lattner | 993c897 | 2006-04-18 16:44:51 +0000 | [diff] [blame] | 66 | case X86::MOVAPSrm: |
| 67 | case X86::MOVAPDrm: |
Bill Wendling | 823efee | 2007-04-03 06:00:37 +0000 | [diff] [blame^] | 68 | case X86::MMX_MOVD64rm: |
| 69 | case X86::MMX_MOVQ64rm: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 70 | if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() && |
| 71 | MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() && |
| 72 | MI->getOperand(2).getImmedValue() == 1 && |
| 73 | MI->getOperand(3).getReg() == 0 && |
| 74 | MI->getOperand(4).getImmedValue() == 0) { |
| 75 | FrameIndex = MI->getOperand(1).getFrameIndex(); |
| 76 | return MI->getOperand(0).getReg(); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI, |
| 84 | int &FrameIndex) const { |
| 85 | switch (MI->getOpcode()) { |
| 86 | default: break; |
| 87 | case X86::MOV8mr: |
| 88 | case X86::MOV16mr: |
Evan Cheng | f4df680 | 2006-05-11 07:33:49 +0000 | [diff] [blame] | 89 | case X86::MOV16_mr: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 90 | case X86::MOV32mr: |
Evan Cheng | f4df680 | 2006-05-11 07:33:49 +0000 | [diff] [blame] | 91 | case X86::MOV32_mr: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 92 | case X86::MOV64mr: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 93 | case X86::FpSTP64m: |
| 94 | case X86::MOVSSmr: |
| 95 | case X86::MOVSDmr: |
Chris Lattner | 993c897 | 2006-04-18 16:44:51 +0000 | [diff] [blame] | 96 | case X86::MOVAPSmr: |
| 97 | case X86::MOVAPDmr: |
Bill Wendling | 823efee | 2007-04-03 06:00:37 +0000 | [diff] [blame^] | 98 | case X86::MMX_MOVD64mr: |
| 99 | case X86::MMX_MOVQ64mr: |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 100 | if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() && |
| 101 | MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() && |
Chris Lattner | 1c07e72 | 2006-02-02 20:38:12 +0000 | [diff] [blame] | 102 | MI->getOperand(1).getImmedValue() == 1 && |
| 103 | MI->getOperand(2).getReg() == 0 && |
| 104 | MI->getOperand(3).getImmedValue() == 0) { |
| 105 | FrameIndex = MI->getOperand(0).getFrameIndex(); |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 106 | return MI->getOperand(4).getReg(); |
| 107 | } |
| 108 | break; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 114 | /// convertToThreeAddress - This method must be implemented by targets that |
| 115 | /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target |
| 116 | /// may be able to convert a two-address instruction into a true |
| 117 | /// three-address instruction on demand. This allows the X86 target (for |
| 118 | /// example) to convert ADD and SHL instructions into LEA instructions if they |
| 119 | /// would require register copies due to two-addressness. |
| 120 | /// |
| 121 | /// This method returns a null pointer if the transformation cannot be |
| 122 | /// performed, otherwise it returns the new instruction. |
| 123 | /// |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 124 | MachineInstr * |
| 125 | X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI, |
| 126 | MachineBasicBlock::iterator &MBBI, |
| 127 | LiveVariables &LV) const { |
| 128 | MachineInstr *MI = MBBI; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 129 | // All instructions input are two-addr instructions. Get the known operands. |
| 130 | unsigned Dest = MI->getOperand(0).getReg(); |
| 131 | unsigned Src = MI->getOperand(1).getReg(); |
| 132 | |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 133 | MachineInstr *NewMI = NULL; |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 134 | // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 135 | // we have better subtarget support, enable the 16-bit LEA generation here. |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 136 | bool DisableLEA16 = true; |
| 137 | |
Evan Cheng | ccba76b | 2006-05-30 20:26:50 +0000 | [diff] [blame] | 138 | switch (MI->getOpcode()) { |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 139 | default: return 0; |
Evan Cheng | ccba76b | 2006-05-30 20:26:50 +0000 | [diff] [blame] | 140 | case X86::SHUFPSrri: { |
| 141 | assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!"); |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 142 | if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0; |
| 143 | |
Evan Cheng | aa3c141 | 2006-05-30 21:45:53 +0000 | [diff] [blame] | 144 | unsigned A = MI->getOperand(0).getReg(); |
| 145 | unsigned B = MI->getOperand(1).getReg(); |
| 146 | unsigned C = MI->getOperand(2).getReg(); |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 147 | unsigned M = MI->getOperand(3).getImm(); |
| 148 | if (B != C) return 0; |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 149 | NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M); |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
Chris Lattner | 995f550 | 2007-03-28 18:12:31 +0000 | [diff] [blame] | 152 | case X86::SHL64ri: { |
| 153 | assert(MI->getNumOperands() == 3 && "Unknown shift instruction!"); |
| 154 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 155 | // the flags produced by a shift yet, so this is safe. |
| 156 | unsigned Dest = MI->getOperand(0).getReg(); |
| 157 | unsigned Src = MI->getOperand(1).getReg(); |
| 158 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 159 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
| 160 | |
| 161 | NewMI = BuildMI(get(X86::LEA64r), Dest) |
| 162 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 163 | break; |
| 164 | } |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 165 | case X86::SHL32ri: { |
| 166 | assert(MI->getNumOperands() == 3 && "Unknown shift instruction!"); |
| 167 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 168 | // the flags produced by a shift yet, so this is safe. |
| 169 | unsigned Dest = MI->getOperand(0).getReg(); |
| 170 | unsigned Src = MI->getOperand(1).getReg(); |
| 171 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 172 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
| 173 | |
Chris Lattner | f2177b8 | 2007-03-28 00:58:40 +0000 | [diff] [blame] | 174 | unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ? |
| 175 | X86::LEA64_32r : X86::LEA32r; |
| 176 | NewMI = BuildMI(get(Opc), Dest) |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 177 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 178 | break; |
| 179 | } |
| 180 | case X86::SHL16ri: { |
| 181 | assert(MI->getNumOperands() == 3 && "Unknown shift instruction!"); |
| 182 | if (DisableLEA16) return 0; |
| 183 | |
| 184 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 185 | // the flags produced by a shift yet, so this is safe. |
| 186 | unsigned Dest = MI->getOperand(0).getReg(); |
| 187 | unsigned Src = MI->getOperand(1).getReg(); |
| 188 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 189 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
| 190 | |
| 191 | NewMI = BuildMI(get(X86::LEA16r), Dest) |
| 192 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 193 | break; |
Evan Cheng | ccba76b | 2006-05-30 20:26:50 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 197 | // FIXME: None of these instructions are promotable to LEAs without |
| 198 | // additional information. In particular, LEA doesn't set the flags that |
Chris Lattner | 5aee0b9 | 2005-01-02 04:18:17 +0000 | [diff] [blame] | 199 | // add and inc do. :( |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 200 | if (0) |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 201 | switch (MI->getOpcode()) { |
| 202 | case X86::INC32r: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 203 | case X86::INC64_32r: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 204 | assert(MI->getNumOperands() == 2 && "Unknown inc instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 205 | NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 206 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 207 | case X86::INC16r: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 208 | case X86::INC64_16r: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 209 | if (DisableLEA16) return 0; |
| 210 | assert(MI->getNumOperands() == 2 && "Unknown inc instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 211 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 212 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 213 | case X86::DEC32r: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 214 | case X86::DEC64_32r: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 215 | assert(MI->getNumOperands() == 2 && "Unknown dec instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 216 | NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 217 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 218 | case X86::DEC16r: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 219 | case X86::DEC64_16r: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 220 | if (DisableLEA16) return 0; |
| 221 | assert(MI->getNumOperands() == 2 && "Unknown dec instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 222 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 223 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 224 | case X86::ADD32rr: |
| 225 | assert(MI->getNumOperands() == 3 && "Unknown add instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 226 | NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src, |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 227 | MI->getOperand(2).getReg()); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 228 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 229 | case X86::ADD16rr: |
| 230 | if (DisableLEA16) return 0; |
| 231 | assert(MI->getNumOperands() == 3 && "Unknown add instruction!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 232 | NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src, |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 233 | MI->getOperand(2).getReg()); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 234 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 235 | case X86::ADD32ri: |
Evan Cheng | 6de0163 | 2006-05-19 18:43:41 +0000 | [diff] [blame] | 236 | case X86::ADD32ri8: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 237 | assert(MI->getNumOperands() == 3 && "Unknown add instruction!"); |
| 238 | if (MI->getOperand(2).isImmediate()) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 239 | NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 240 | MI->getOperand(2).getImmedValue()); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 241 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 242 | case X86::ADD16ri: |
Evan Cheng | 6de0163 | 2006-05-19 18:43:41 +0000 | [diff] [blame] | 243 | case X86::ADD16ri8: |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 244 | if (DisableLEA16) return 0; |
| 245 | assert(MI->getNumOperands() == 3 && "Unknown add instruction!"); |
| 246 | if (MI->getOperand(2).isImmediate()) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 247 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 248 | MI->getOperand(2).getImmedValue()); |
| 249 | break; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 250 | case X86::SHL16ri: |
| 251 | if (DisableLEA16) return 0; |
| 252 | case X86::SHL32ri: |
| 253 | assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() && |
| 254 | "Unknown shl instruction!"); |
| 255 | unsigned ShAmt = MI->getOperand(2).getImmedValue(); |
| 256 | if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) { |
| 257 | X86AddressMode AM; |
| 258 | AM.Scale = 1 << ShAmt; |
| 259 | AM.IndexReg = Src; |
| 260 | unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r; |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 261 | NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM); |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | } |
| 265 | |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 266 | if (NewMI) { |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 267 | NewMI->copyKillDeadInfo(MI); |
Evan Cheng | 258ff67 | 2006-12-01 21:52:41 +0000 | [diff] [blame] | 268 | LV.instructionChanged(MI, NewMI); // Update live variables |
| 269 | MFI->insert(MBBI, NewMI); // Insert the new inst |
| 270 | } |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 271 | return NewMI; |
Chris Lattner | bcea4d6 | 2005-01-02 02:37:07 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | 41e431b | 2005-01-19 07:11:01 +0000 | [diff] [blame] | 274 | /// commuteInstruction - We have a few instructions that must be hacked on to |
| 275 | /// commute them. |
| 276 | /// |
| 277 | MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const { |
Chris Lattner | 6458f18 | 2006-09-28 23:33:12 +0000 | [diff] [blame] | 278 | // FIXME: Can commute cmoves by changing the condition! |
Chris Lattner | 41e431b | 2005-01-19 07:11:01 +0000 | [diff] [blame] | 279 | switch (MI->getOpcode()) { |
Chris Lattner | 0df53d2 | 2005-01-19 07:31:24 +0000 | [diff] [blame] | 280 | case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I) |
| 281 | case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I) |
Chris Lattner | 41e431b | 2005-01-19 07:11:01 +0000 | [diff] [blame] | 282 | case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I) |
| 283 | case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I) |
Chris Lattner | 0df53d2 | 2005-01-19 07:31:24 +0000 | [diff] [blame] | 284 | unsigned Opc; |
| 285 | unsigned Size; |
| 286 | switch (MI->getOpcode()) { |
| 287 | default: assert(0 && "Unreachable!"); |
| 288 | case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break; |
| 289 | case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break; |
| 290 | case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break; |
| 291 | case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break; |
| 292 | } |
Chris Lattner | 41e431b | 2005-01-19 07:11:01 +0000 | [diff] [blame] | 293 | unsigned Amt = MI->getOperand(3).getImmedValue(); |
| 294 | unsigned A = MI->getOperand(0).getReg(); |
| 295 | unsigned B = MI->getOperand(1).getReg(); |
| 296 | unsigned C = MI->getOperand(2).getReg(); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 297 | bool BisKill = MI->getOperand(1).isKill(); |
| 298 | bool CisKill = MI->getOperand(2).isKill(); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 299 | return BuildMI(get(Opc), A).addReg(C, false, false, CisKill) |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 300 | .addReg(B, false, false, BisKill).addImm(Size-Amt); |
Chris Lattner | 41e431b | 2005-01-19 07:11:01 +0000 | [diff] [blame] | 301 | } |
| 302 | default: |
| 303 | return TargetInstrInfo::commuteInstruction(MI); |
| 304 | } |
| 305 | } |
| 306 | |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 307 | static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) { |
| 308 | switch (BrOpc) { |
| 309 | default: return X86::COND_INVALID; |
| 310 | case X86::JE: return X86::COND_E; |
| 311 | case X86::JNE: return X86::COND_NE; |
| 312 | case X86::JL: return X86::COND_L; |
| 313 | case X86::JLE: return X86::COND_LE; |
| 314 | case X86::JG: return X86::COND_G; |
| 315 | case X86::JGE: return X86::COND_GE; |
| 316 | case X86::JB: return X86::COND_B; |
| 317 | case X86::JBE: return X86::COND_BE; |
| 318 | case X86::JA: return X86::COND_A; |
| 319 | case X86::JAE: return X86::COND_AE; |
| 320 | case X86::JS: return X86::COND_S; |
| 321 | case X86::JNS: return X86::COND_NS; |
| 322 | case X86::JP: return X86::COND_P; |
| 323 | case X86::JNP: return X86::COND_NP; |
| 324 | case X86::JO: return X86::COND_O; |
| 325 | case X86::JNO: return X86::COND_NO; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | unsigned X86::GetCondBranchFromCond(X86::CondCode CC) { |
| 330 | switch (CC) { |
| 331 | default: assert(0 && "Illegal condition code!"); |
| 332 | case X86::COND_E: return X86::JE; |
| 333 | case X86::COND_NE: return X86::JNE; |
| 334 | case X86::COND_L: return X86::JL; |
| 335 | case X86::COND_LE: return X86::JLE; |
| 336 | case X86::COND_G: return X86::JG; |
| 337 | case X86::COND_GE: return X86::JGE; |
| 338 | case X86::COND_B: return X86::JB; |
| 339 | case X86::COND_BE: return X86::JBE; |
| 340 | case X86::COND_A: return X86::JA; |
| 341 | case X86::COND_AE: return X86::JAE; |
| 342 | case X86::COND_S: return X86::JS; |
| 343 | case X86::COND_NS: return X86::JNS; |
| 344 | case X86::COND_P: return X86::JP; |
| 345 | case X86::COND_NP: return X86::JNP; |
| 346 | case X86::COND_O: return X86::JO; |
| 347 | case X86::COND_NO: return X86::JNO; |
| 348 | } |
| 349 | } |
| 350 | |
Chris Lattner | 9cd6875 | 2006-10-21 05:52:40 +0000 | [diff] [blame] | 351 | /// GetOppositeBranchCondition - Return the inverse of the specified condition, |
| 352 | /// e.g. turning COND_E to COND_NE. |
| 353 | X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) { |
| 354 | switch (CC) { |
| 355 | default: assert(0 && "Illegal condition code!"); |
| 356 | case X86::COND_E: return X86::COND_NE; |
| 357 | case X86::COND_NE: return X86::COND_E; |
| 358 | case X86::COND_L: return X86::COND_GE; |
| 359 | case X86::COND_LE: return X86::COND_G; |
| 360 | case X86::COND_G: return X86::COND_LE; |
| 361 | case X86::COND_GE: return X86::COND_L; |
| 362 | case X86::COND_B: return X86::COND_AE; |
| 363 | case X86::COND_BE: return X86::COND_A; |
| 364 | case X86::COND_A: return X86::COND_BE; |
| 365 | case X86::COND_AE: return X86::COND_B; |
| 366 | case X86::COND_S: return X86::COND_NS; |
| 367 | case X86::COND_NS: return X86::COND_S; |
| 368 | case X86::COND_P: return X86::COND_NP; |
| 369 | case X86::COND_NP: return X86::COND_P; |
| 370 | case X86::COND_O: return X86::COND_NO; |
| 371 | case X86::COND_NO: return X86::COND_O; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 376 | bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 377 | MachineBasicBlock *&TBB, |
| 378 | MachineBasicBlock *&FBB, |
| 379 | std::vector<MachineOperand> &Cond) const { |
| 380 | // TODO: If FP_REG_KILL is around, ignore it. |
| 381 | |
| 382 | // If the block has no terminators, it just falls into the block after it. |
| 383 | MachineBasicBlock::iterator I = MBB.end(); |
| 384 | if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) |
| 385 | return false; |
| 386 | |
| 387 | // Get the last instruction in the block. |
| 388 | MachineInstr *LastInst = I; |
| 389 | |
| 390 | // If there is only one terminator instruction, process it. |
| 391 | if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) { |
| 392 | if (!isBranch(LastInst->getOpcode())) |
| 393 | return true; |
| 394 | |
| 395 | // If the block ends with a branch there are 3 possibilities: |
| 396 | // it's an unconditional, conditional, or indirect branch. |
| 397 | |
| 398 | if (LastInst->getOpcode() == X86::JMP) { |
| 399 | TBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 400 | return false; |
| 401 | } |
| 402 | X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode()); |
| 403 | if (BranchCode == X86::COND_INVALID) |
| 404 | return true; // Can't handle indirect branch. |
| 405 | |
| 406 | // Otherwise, block ends with fall-through condbranch. |
| 407 | TBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 408 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | // Get the instruction before it if it's a terminator. |
| 413 | MachineInstr *SecondLastInst = I; |
| 414 | |
| 415 | // If there are three terminators, we don't know what sort of block this is. |
| 416 | if (SecondLastInst && I != MBB.begin() && |
| 417 | isTerminatorInstr((--I)->getOpcode())) |
| 418 | return true; |
| 419 | |
Chris Lattner | 6ce6443 | 2006-10-30 22:27:23 +0000 | [diff] [blame] | 420 | // If the block ends with X86::JMP and a conditional branch, handle it. |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 421 | X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode()); |
| 422 | if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) { |
Chris Lattner | 6ce6443 | 2006-10-30 22:27:23 +0000 | [diff] [blame] | 423 | TBB = SecondLastInst->getOperand(0).getMachineBasicBlock(); |
| 424 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 425 | FBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 426 | return false; |
| 427 | } |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 428 | |
| 429 | // Otherwise, can't handle this. |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 434 | MachineBasicBlock::iterator I = MBB.end(); |
| 435 | if (I == MBB.begin()) return; |
| 436 | --I; |
| 437 | if (I->getOpcode() != X86::JMP && |
| 438 | GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID) |
| 439 | return; |
| 440 | |
| 441 | // Remove the branch. |
| 442 | I->eraseFromParent(); |
| 443 | |
| 444 | I = MBB.end(); |
| 445 | |
| 446 | if (I == MBB.begin()) return; |
| 447 | --I; |
| 448 | if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID) |
| 449 | return; |
| 450 | |
| 451 | // Remove the branch. |
| 452 | I->eraseFromParent(); |
| 453 | } |
| 454 | |
| 455 | void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 456 | MachineBasicBlock *FBB, |
| 457 | const std::vector<MachineOperand> &Cond) const { |
| 458 | // Shouldn't be a fall through. |
| 459 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Chris Lattner | 34a84ac | 2006-10-21 05:34:23 +0000 | [diff] [blame] | 460 | assert((Cond.size() == 1 || Cond.size() == 0) && |
| 461 | "X86 branch conditions have one component!"); |
| 462 | |
| 463 | if (FBB == 0) { // One way branch. |
| 464 | if (Cond.empty()) { |
| 465 | // Unconditional branch? |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 466 | BuildMI(&MBB, get(X86::JMP)).addMBB(TBB); |
Chris Lattner | 34a84ac | 2006-10-21 05:34:23 +0000 | [diff] [blame] | 467 | } else { |
| 468 | // Conditional branch. |
| 469 | unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm()); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 470 | BuildMI(&MBB, get(Opc)).addMBB(TBB); |
Chris Lattner | 34a84ac | 2006-10-21 05:34:23 +0000 | [diff] [blame] | 471 | } |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | |
Chris Lattner | 879d09c | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 475 | // Two-way Conditional branch. |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 476 | unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm()); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 477 | BuildMI(&MBB, get(Opc)).addMBB(TBB); |
| 478 | BuildMI(&MBB, get(X86::JMP)).addMBB(FBB); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chris Lattner | c24ff8e | 2006-10-28 17:29:57 +0000 | [diff] [blame] | 481 | bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const { |
| 482 | if (MBB.empty()) return false; |
| 483 | |
| 484 | switch (MBB.back().getOpcode()) { |
| 485 | case X86::JMP: // Uncond branch. |
| 486 | case X86::JMP32r: // Indirect branch. |
| 487 | case X86::JMP32m: // Indirect branch through mem. |
| 488 | return true; |
| 489 | default: return false; |
| 490 | } |
| 491 | } |
| 492 | |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 493 | bool X86InstrInfo:: |
| 494 | ReverseBranchCondition(std::vector<MachineOperand> &Cond) const { |
Chris Lattner | 9cd6875 | 2006-10-21 05:52:40 +0000 | [diff] [blame] | 495 | assert(Cond.size() == 1 && "Invalid X86 branch condition!"); |
| 496 | Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm())); |
| 497 | return false; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 500 | const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const { |
| 501 | const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 502 | if (Subtarget->is64Bit()) |
| 503 | return &X86::GR64RegClass; |
| 504 | else |
| 505 | return &X86::GR32RegClass; |
| 506 | } |