Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===// |
| 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 | // |
| 10 | // This file contains the X86 implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86InstrInfo.h" |
| 15 | #include "X86.h" |
| 16 | #include "X86GenInstrInfo.inc" |
| 17 | #include "X86InstrBuilder.h" |
| 18 | #include "X86Subtarget.h" |
| 19 | #include "X86TargetMachine.h" |
Owen Anderson | 1636de9 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/LiveVariables.h" |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/SSARegMap.h" |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | fa1a495 | 2007-10-05 08:04:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Evan Cheng | fa1a495 | 2007-10-05 08:04:01 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | cl::opt<bool> |
| 30 | EnableConvert3Addr("enable-x86-conv-3-addr", |
| 31 | cl::desc("Enable convertToThreeAddress for X86")); |
| 32 | } |
| 33 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | X86InstrInfo::X86InstrInfo(X86TargetMachine &tm) |
Owen Anderson | 1636de9 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 35 | : TargetInstrInfo(X86Insts, array_lengthof(X86Insts)), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | TM(tm), RI(tm, *this) { |
| 37 | } |
| 38 | |
| 39 | bool X86InstrInfo::isMoveInstr(const MachineInstr& MI, |
| 40 | unsigned& sourceReg, |
| 41 | unsigned& destReg) const { |
| 42 | MachineOpCode oc = MI.getOpcode(); |
| 43 | if (oc == X86::MOV8rr || oc == X86::MOV16rr || |
| 44 | oc == X86::MOV32rr || oc == X86::MOV64rr || |
| 45 | oc == X86::MOV16to16_ || oc == X86::MOV32to32_ || |
| 46 | oc == X86::MOV_Fp3232 || oc == X86::MOVSSrr || oc == X86::MOVSDrr || |
| 47 | oc == X86::MOV_Fp3264 || oc == X86::MOV_Fp6432 || oc == X86::MOV_Fp6464 || |
| 48 | oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr || |
| 49 | oc == X86::MOVAPSrr || oc == X86::MOVAPDrr || |
| 50 | oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr || |
| 51 | oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr || |
| 52 | oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) { |
| 53 | assert(MI.getNumOperands() >= 2 && |
| 54 | MI.getOperand(0).isRegister() && |
| 55 | MI.getOperand(1).isRegister() && |
| 56 | "invalid register-register move instruction"); |
| 57 | sourceReg = MI.getOperand(1).getReg(); |
| 58 | destReg = MI.getOperand(0).getReg(); |
| 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI, |
| 65 | int &FrameIndex) const { |
| 66 | switch (MI->getOpcode()) { |
| 67 | default: break; |
| 68 | case X86::MOV8rm: |
| 69 | case X86::MOV16rm: |
| 70 | case X86::MOV16_rm: |
| 71 | case X86::MOV32rm: |
| 72 | case X86::MOV32_rm: |
| 73 | case X86::MOV64rm: |
| 74 | case X86::LD_Fp64m: |
| 75 | case X86::MOVSSrm: |
| 76 | case X86::MOVSDrm: |
| 77 | case X86::MOVAPSrm: |
| 78 | case X86::MOVAPDrm: |
| 79 | case X86::MMX_MOVD64rm: |
| 80 | case X86::MMX_MOVQ64rm: |
| 81 | if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() && |
| 82 | MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() && |
| 83 | MI->getOperand(2).getImmedValue() == 1 && |
| 84 | MI->getOperand(3).getReg() == 0 && |
| 85 | MI->getOperand(4).getImmedValue() == 0) { |
| 86 | FrameIndex = MI->getOperand(1).getFrameIndex(); |
| 87 | return MI->getOperand(0).getReg(); |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI, |
| 95 | int &FrameIndex) const { |
| 96 | switch (MI->getOpcode()) { |
| 97 | default: break; |
| 98 | case X86::MOV8mr: |
| 99 | case X86::MOV16mr: |
| 100 | case X86::MOV16_mr: |
| 101 | case X86::MOV32mr: |
| 102 | case X86::MOV32_mr: |
| 103 | case X86::MOV64mr: |
| 104 | case X86::ST_FpP64m: |
| 105 | case X86::MOVSSmr: |
| 106 | case X86::MOVSDmr: |
| 107 | case X86::MOVAPSmr: |
| 108 | case X86::MOVAPDmr: |
| 109 | case X86::MMX_MOVD64mr: |
| 110 | case X86::MMX_MOVQ64mr: |
| 111 | case X86::MMX_MOVNTQmr: |
| 112 | if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() && |
| 113 | MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() && |
| 114 | MI->getOperand(1).getImmedValue() == 1 && |
| 115 | MI->getOperand(2).getReg() == 0 && |
| 116 | MI->getOperand(3).getImmedValue() == 0) { |
| 117 | FrameIndex = MI->getOperand(0).getFrameIndex(); |
| 118 | return MI->getOperand(4).getReg(); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const { |
| 127 | switch (MI->getOpcode()) { |
| 128 | default: break; |
| 129 | case X86::MOV8rm: |
| 130 | case X86::MOV16rm: |
| 131 | case X86::MOV16_rm: |
| 132 | case X86::MOV32rm: |
| 133 | case X86::MOV32_rm: |
| 134 | case X86::MOV64rm: |
| 135 | case X86::LD_Fp64m: |
| 136 | case X86::MOVSSrm: |
| 137 | case X86::MOVSDrm: |
| 138 | case X86::MOVAPSrm: |
| 139 | case X86::MOVAPDrm: |
| 140 | case X86::MMX_MOVD64rm: |
| 141 | case X86::MMX_MOVQ64rm: |
| 142 | // Loads from constant pools are trivially rematerializable. |
| 143 | return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() && |
| 144 | MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() && |
| 145 | MI->getOperand(1).getReg() == 0 && |
| 146 | MI->getOperand(2).getImmedValue() == 1 && |
| 147 | MI->getOperand(3).getReg() == 0; |
| 148 | } |
| 149 | // All other instructions marked M_REMATERIALIZABLE are always trivially |
| 150 | // rematerializable. |
| 151 | return true; |
| 152 | } |
| 153 | |
Evan Cheng | fa1a495 | 2007-10-05 08:04:01 +0000 | [diff] [blame] | 154 | /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that |
| 155 | /// is not marked dead. |
| 156 | static bool hasLiveCondCodeDef(MachineInstr *MI) { |
| 157 | if (!EnableConvert3Addr) |
| 158 | return true; |
| 159 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 160 | MachineOperand &MO = MI->getOperand(i); |
| 161 | if (MO.isRegister() && MO.isDef() && |
| 162 | MO.getReg() == X86::EFLAGS && !MO.isDead()) { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | /// convertToThreeAddress - This method must be implemented by targets that |
| 170 | /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target |
| 171 | /// may be able to convert a two-address instruction into a true |
| 172 | /// three-address instruction on demand. This allows the X86 target (for |
| 173 | /// example) to convert ADD and SHL instructions into LEA instructions if they |
| 174 | /// would require register copies due to two-addressness. |
| 175 | /// |
| 176 | /// This method returns a null pointer if the transformation cannot be |
| 177 | /// performed, otherwise it returns the new instruction. |
| 178 | /// |
| 179 | MachineInstr * |
| 180 | X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI, |
| 181 | MachineBasicBlock::iterator &MBBI, |
| 182 | LiveVariables &LV) const { |
| 183 | MachineInstr *MI = MBBI; |
| 184 | // All instructions input are two-addr instructions. Get the known operands. |
| 185 | unsigned Dest = MI->getOperand(0).getReg(); |
| 186 | unsigned Src = MI->getOperand(1).getReg(); |
| 187 | |
| 188 | MachineInstr *NewMI = NULL; |
| 189 | // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When |
| 190 | // we have better subtarget support, enable the 16-bit LEA generation here. |
| 191 | bool DisableLEA16 = true; |
| 192 | |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 193 | unsigned MIOpc = MI->getOpcode(); |
| 194 | switch (MIOpc) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | case X86::SHUFPSrri: { |
| 196 | assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!"); |
| 197 | if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0; |
| 198 | |
| 199 | unsigned A = MI->getOperand(0).getReg(); |
| 200 | unsigned B = MI->getOperand(1).getReg(); |
| 201 | unsigned C = MI->getOperand(2).getReg(); |
| 202 | unsigned M = MI->getOperand(3).getImm(); |
| 203 | if (B != C) return 0; |
| 204 | NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M); |
| 205 | break; |
| 206 | } |
| 207 | case X86::SHL64ri: { |
Evan Cheng | 5568707 | 2007-09-14 21:48:26 +0000 | [diff] [blame] | 208 | assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 209 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 210 | // the flags produced by a shift yet, so this is safe. |
| 211 | unsigned Dest = MI->getOperand(0).getReg(); |
| 212 | unsigned Src = MI->getOperand(1).getReg(); |
| 213 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 214 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
| 215 | |
| 216 | NewMI = BuildMI(get(X86::LEA64r), Dest) |
| 217 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 218 | break; |
| 219 | } |
| 220 | case X86::SHL32ri: { |
Evan Cheng | 5568707 | 2007-09-14 21:48:26 +0000 | [diff] [blame] | 221 | assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 222 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 223 | // the flags produced by a shift yet, so this is safe. |
| 224 | unsigned Dest = MI->getOperand(0).getReg(); |
| 225 | unsigned Src = MI->getOperand(1).getReg(); |
| 226 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 227 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
| 228 | |
| 229 | unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ? |
| 230 | X86::LEA64_32r : X86::LEA32r; |
| 231 | NewMI = BuildMI(get(Opc), Dest) |
| 232 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 233 | break; |
| 234 | } |
| 235 | case X86::SHL16ri: { |
Evan Cheng | 5568707 | 2007-09-14 21:48:26 +0000 | [diff] [blame] | 236 | assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!"); |
Evan Cheng | 0b1e871 | 2007-09-06 00:14:41 +0000 | [diff] [blame] | 237 | // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses |
| 238 | // the flags produced by a shift yet, so this is safe. |
| 239 | unsigned Dest = MI->getOperand(0).getReg(); |
| 240 | unsigned Src = MI->getOperand(1).getReg(); |
| 241 | unsigned ShAmt = MI->getOperand(2).getImm(); |
| 242 | if (ShAmt == 0 || ShAmt >= 4) return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 243 | |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 244 | if (DisableLEA16) { |
| 245 | // If 16-bit LEA is disabled, use 32-bit LEA via subregisters. |
| 246 | SSARegMap *RegMap = MFI->getParent()->getSSARegMap(); |
Evan Cheng | 0b1e871 | 2007-09-06 00:14:41 +0000 | [diff] [blame] | 247 | unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() |
| 248 | ? X86::LEA64_32r : X86::LEA32r; |
| 249 | unsigned leaInReg = RegMap->createVirtualRegister(&X86::GR32RegClass); |
| 250 | unsigned leaOutReg = RegMap->createVirtualRegister(&X86::GR32RegClass); |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 251 | |
Evan Cheng | 0b1e871 | 2007-09-06 00:14:41 +0000 | [diff] [blame] | 252 | MachineInstr *Ins = |
| 253 | BuildMI(get(X86::INSERT_SUBREG), leaInReg).addReg(Src).addImm(2); |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 254 | Ins->copyKillDeadInfo(MI); |
| 255 | |
| 256 | NewMI = BuildMI(get(Opc), leaOutReg) |
| 257 | .addReg(0).addImm(1 << ShAmt).addReg(leaInReg).addImm(0); |
| 258 | |
Evan Cheng | 0b1e871 | 2007-09-06 00:14:41 +0000 | [diff] [blame] | 259 | MachineInstr *Ext = |
| 260 | BuildMI(get(X86::EXTRACT_SUBREG), Dest).addReg(leaOutReg).addImm(2); |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 261 | Ext->copyKillDeadInfo(MI); |
| 262 | |
| 263 | MFI->insert(MBBI, Ins); // Insert the insert_subreg |
| 264 | LV.instructionChanged(MI, NewMI); // Update live variables |
| 265 | LV.addVirtualRegisterKilled(leaInReg, NewMI); |
| 266 | MFI->insert(MBBI, NewMI); // Insert the new inst |
| 267 | LV.addVirtualRegisterKilled(leaOutReg, Ext); |
Evan Cheng | 0b1e871 | 2007-09-06 00:14:41 +0000 | [diff] [blame] | 268 | MFI->insert(MBBI, Ext); // Insert the extract_subreg |
Christopher Lamb | 380c627 | 2007-08-10 21:18:25 +0000 | [diff] [blame] | 269 | return Ext; |
| 270 | } else { |
| 271 | NewMI = BuildMI(get(X86::LEA16r), Dest) |
| 272 | .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0); |
| 273 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 276 | default: { |
| 277 | // The following opcodes also sets the condition code register(s). Only |
| 278 | // convert them to equivalent lea if the condition code register def's |
| 279 | // are dead! |
| 280 | if (hasLiveCondCodeDef(MI)) |
| 281 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 283 | switch (MIOpc) { |
| 284 | default: return 0; |
| 285 | case X86::INC64r: |
Evan Cheng | 3cdc719 | 2007-10-05 21:55:32 +0000 | [diff] [blame^] | 286 | case X86::INC32r: { |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 287 | assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!"); |
Evan Cheng | 3cdc719 | 2007-10-05 21:55:32 +0000 | [diff] [blame^] | 288 | unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r : X86::LEA32r; |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 289 | NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, 1); |
| 290 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 291 | } |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 292 | case X86::INC16r: |
| 293 | case X86::INC64_16r: |
| 294 | if (DisableLEA16) return 0; |
| 295 | assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!"); |
| 296 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1); |
| 297 | break; |
| 298 | case X86::DEC64r: |
Evan Cheng | 3cdc719 | 2007-10-05 21:55:32 +0000 | [diff] [blame^] | 299 | case X86::DEC32r: { |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 300 | assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!"); |
Evan Cheng | 3cdc719 | 2007-10-05 21:55:32 +0000 | [diff] [blame^] | 301 | unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r : X86::LEA32r; |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 302 | NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, -1); |
| 303 | break; |
| 304 | } |
| 305 | case X86::DEC16r: |
| 306 | case X86::DEC64_16r: |
| 307 | if (DisableLEA16) return 0; |
| 308 | assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!"); |
| 309 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1); |
| 310 | break; |
| 311 | case X86::ADD64rr: |
| 312 | case X86::ADD32rr: { |
| 313 | assert(MI->getNumOperands() >= 3 && "Unknown add instruction!"); |
| 314 | unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r : X86::LEA32r; |
| 315 | NewMI = addRegReg(BuildMI(get(Opc), Dest), Src, |
| 316 | MI->getOperand(2).getReg()); |
| 317 | break; |
| 318 | } |
| 319 | case X86::ADD16rr: |
| 320 | if (DisableLEA16) return 0; |
| 321 | assert(MI->getNumOperands() >= 3 && "Unknown add instruction!"); |
| 322 | NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src, |
| 323 | MI->getOperand(2).getReg()); |
| 324 | break; |
| 325 | case X86::ADD64ri32: |
| 326 | case X86::ADD64ri8: |
| 327 | assert(MI->getNumOperands() >= 3 && "Unknown add instruction!"); |
| 328 | if (MI->getOperand(2).isImmediate()) |
| 329 | NewMI = addRegOffset(BuildMI(get(X86::LEA64r), Dest), Src, |
| 330 | MI->getOperand(2).getImmedValue()); |
| 331 | break; |
| 332 | case X86::ADD32ri: |
| 333 | case X86::ADD32ri8: |
| 334 | assert(MI->getNumOperands() >= 3 && "Unknown add instruction!"); |
| 335 | if (MI->getOperand(2).isImmediate()) |
| 336 | NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, |
| 337 | MI->getOperand(2).getImmedValue()); |
| 338 | break; |
| 339 | case X86::ADD16ri: |
| 340 | case X86::ADD16ri8: |
| 341 | if (DisableLEA16) return 0; |
| 342 | assert(MI->getNumOperands() >= 3 && "Unknown add instruction!"); |
| 343 | if (MI->getOperand(2).isImmediate()) |
| 344 | NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, |
| 345 | MI->getOperand(2).getImmedValue()); |
| 346 | break; |
| 347 | case X86::SHL16ri: |
| 348 | if (DisableLEA16) return 0; |
| 349 | case X86::SHL32ri: |
| 350 | case X86::SHL64ri: { |
| 351 | assert(MI->getNumOperands() >= 3 && MI->getOperand(2).isImmediate() && |
| 352 | "Unknown shl instruction!"); |
| 353 | unsigned ShAmt = MI->getOperand(2).getImmedValue(); |
| 354 | if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) { |
| 355 | X86AddressMode AM; |
| 356 | AM.Scale = 1 << ShAmt; |
| 357 | AM.IndexReg = Src; |
| 358 | unsigned Opc = MIOpc == X86::SHL64ri ? X86::LEA64r |
| 359 | : (MIOpc == X86::SHL32ri ? X86::LEA32r : X86::LEA16r); |
| 360 | NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM); |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Evan Cheng | 6b96ed3 | 2007-10-05 20:34:26 +0000 | [diff] [blame] | 368 | NewMI->copyKillDeadInfo(MI); |
| 369 | LV.instructionChanged(MI, NewMI); // Update live variables |
| 370 | MFI->insert(MBBI, NewMI); // Insert the new inst |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | return NewMI; |
| 372 | } |
| 373 | |
| 374 | /// commuteInstruction - We have a few instructions that must be hacked on to |
| 375 | /// commute them. |
| 376 | /// |
| 377 | MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const { |
| 378 | // FIXME: Can commute cmoves by changing the condition! |
| 379 | switch (MI->getOpcode()) { |
| 380 | case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I) |
| 381 | case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I) |
| 382 | case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I) |
Dan Gohman | 4d9fc4a | 2007-09-14 23:17:45 +0000 | [diff] [blame] | 383 | case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I) |
| 384 | case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I) |
| 385 | case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 386 | unsigned Opc; |
| 387 | unsigned Size; |
| 388 | switch (MI->getOpcode()) { |
| 389 | default: assert(0 && "Unreachable!"); |
| 390 | case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break; |
| 391 | case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break; |
| 392 | case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break; |
| 393 | case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break; |
Dan Gohman | 4d9fc4a | 2007-09-14 23:17:45 +0000 | [diff] [blame] | 394 | case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break; |
| 395 | case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 396 | } |
| 397 | unsigned Amt = MI->getOperand(3).getImmedValue(); |
| 398 | unsigned A = MI->getOperand(0).getReg(); |
| 399 | unsigned B = MI->getOperand(1).getReg(); |
| 400 | unsigned C = MI->getOperand(2).getReg(); |
| 401 | bool BisKill = MI->getOperand(1).isKill(); |
| 402 | bool CisKill = MI->getOperand(2).isKill(); |
| 403 | return BuildMI(get(Opc), A).addReg(C, false, false, CisKill) |
| 404 | .addReg(B, false, false, BisKill).addImm(Size-Amt); |
| 405 | } |
| 406 | default: |
| 407 | return TargetInstrInfo::commuteInstruction(MI); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) { |
| 412 | switch (BrOpc) { |
| 413 | default: return X86::COND_INVALID; |
| 414 | case X86::JE: return X86::COND_E; |
| 415 | case X86::JNE: return X86::COND_NE; |
| 416 | case X86::JL: return X86::COND_L; |
| 417 | case X86::JLE: return X86::COND_LE; |
| 418 | case X86::JG: return X86::COND_G; |
| 419 | case X86::JGE: return X86::COND_GE; |
| 420 | case X86::JB: return X86::COND_B; |
| 421 | case X86::JBE: return X86::COND_BE; |
| 422 | case X86::JA: return X86::COND_A; |
| 423 | case X86::JAE: return X86::COND_AE; |
| 424 | case X86::JS: return X86::COND_S; |
| 425 | case X86::JNS: return X86::COND_NS; |
| 426 | case X86::JP: return X86::COND_P; |
| 427 | case X86::JNP: return X86::COND_NP; |
| 428 | case X86::JO: return X86::COND_O; |
| 429 | case X86::JNO: return X86::COND_NO; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | unsigned X86::GetCondBranchFromCond(X86::CondCode CC) { |
| 434 | switch (CC) { |
| 435 | default: assert(0 && "Illegal condition code!"); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 436 | case X86::COND_E: return X86::JE; |
| 437 | case X86::COND_NE: return X86::JNE; |
| 438 | case X86::COND_L: return X86::JL; |
| 439 | case X86::COND_LE: return X86::JLE; |
| 440 | case X86::COND_G: return X86::JG; |
| 441 | case X86::COND_GE: return X86::JGE; |
| 442 | case X86::COND_B: return X86::JB; |
| 443 | case X86::COND_BE: return X86::JBE; |
| 444 | case X86::COND_A: return X86::JA; |
| 445 | case X86::COND_AE: return X86::JAE; |
| 446 | case X86::COND_S: return X86::JS; |
| 447 | case X86::COND_NS: return X86::JNS; |
| 448 | case X86::COND_P: return X86::JP; |
| 449 | case X86::COND_NP: return X86::JNP; |
| 450 | case X86::COND_O: return X86::JO; |
| 451 | case X86::COND_NO: return X86::JNO; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | /// GetOppositeBranchCondition - Return the inverse of the specified condition, |
| 456 | /// e.g. turning COND_E to COND_NE. |
| 457 | X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) { |
| 458 | switch (CC) { |
| 459 | default: assert(0 && "Illegal condition code!"); |
| 460 | case X86::COND_E: return X86::COND_NE; |
| 461 | case X86::COND_NE: return X86::COND_E; |
| 462 | case X86::COND_L: return X86::COND_GE; |
| 463 | case X86::COND_LE: return X86::COND_G; |
| 464 | case X86::COND_G: return X86::COND_LE; |
| 465 | case X86::COND_GE: return X86::COND_L; |
| 466 | case X86::COND_B: return X86::COND_AE; |
| 467 | case X86::COND_BE: return X86::COND_A; |
| 468 | case X86::COND_A: return X86::COND_BE; |
| 469 | case X86::COND_AE: return X86::COND_B; |
| 470 | case X86::COND_S: return X86::COND_NS; |
| 471 | case X86::COND_NS: return X86::COND_S; |
| 472 | case X86::COND_P: return X86::COND_NP; |
| 473 | case X86::COND_NP: return X86::COND_P; |
| 474 | case X86::COND_O: return X86::COND_NO; |
| 475 | case X86::COND_NO: return X86::COND_O; |
| 476 | } |
| 477 | } |
| 478 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 479 | bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 480 | const TargetInstrDescriptor *TID = MI->getInstrDescriptor(); |
| 481 | if (TID->Flags & M_TERMINATOR_FLAG) { |
| 482 | // Conditional branch is a special case. |
| 483 | if ((TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0) |
| 484 | return true; |
| 485 | if ((TID->Flags & M_PREDICABLE) == 0) |
| 486 | return true; |
| 487 | return !isPredicated(MI); |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
Evan Cheng | 1251579 | 2007-07-26 17:32:14 +0000 | [diff] [blame] | 492 | // For purposes of branch analysis do not count FP_REG_KILL as a terminator. |
| 493 | static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI, |
| 494 | const X86InstrInfo &TII) { |
| 495 | if (MI->getOpcode() == X86::FP_REG_KILL) |
| 496 | return false; |
| 497 | return TII.isUnpredicatedTerminator(MI); |
| 498 | } |
| 499 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 500 | bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 501 | MachineBasicBlock *&TBB, |
| 502 | MachineBasicBlock *&FBB, |
| 503 | std::vector<MachineOperand> &Cond) const { |
| 504 | // If the block has no terminators, it just falls into the block after it. |
| 505 | MachineBasicBlock::iterator I = MBB.end(); |
Evan Cheng | 1251579 | 2007-07-26 17:32:14 +0000 | [diff] [blame] | 506 | if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 507 | return false; |
| 508 | |
| 509 | // Get the last instruction in the block. |
| 510 | MachineInstr *LastInst = I; |
| 511 | |
| 512 | // If there is only one terminator instruction, process it. |
Evan Cheng | 1251579 | 2007-07-26 17:32:14 +0000 | [diff] [blame] | 513 | if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 514 | if (!isBranch(LastInst->getOpcode())) |
| 515 | return true; |
| 516 | |
| 517 | // If the block ends with a branch there are 3 possibilities: |
| 518 | // it's an unconditional, conditional, or indirect branch. |
| 519 | |
| 520 | if (LastInst->getOpcode() == X86::JMP) { |
| 521 | TBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 522 | return false; |
| 523 | } |
| 524 | X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode()); |
| 525 | if (BranchCode == X86::COND_INVALID) |
| 526 | return true; // Can't handle indirect branch. |
| 527 | |
| 528 | // Otherwise, block ends with fall-through condbranch. |
| 529 | TBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 530 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | // Get the instruction before it if it's a terminator. |
| 535 | MachineInstr *SecondLastInst = I; |
| 536 | |
| 537 | // If there are three terminators, we don't know what sort of block this is. |
Evan Cheng | 1251579 | 2007-07-26 17:32:14 +0000 | [diff] [blame] | 538 | if (SecondLastInst && I != MBB.begin() && |
| 539 | isBrAnalysisUnpredicatedTerminator(--I, *this)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 540 | return true; |
| 541 | |
| 542 | // If the block ends with X86::JMP and a conditional branch, handle it. |
| 543 | X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode()); |
| 544 | if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) { |
| 545 | TBB = SecondLastInst->getOperand(0).getMachineBasicBlock(); |
| 546 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 547 | FBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | // If the block ends with two X86::JMPs, handle it. The second one is not |
| 552 | // executed, so remove it. |
| 553 | if (SecondLastInst->getOpcode() == X86::JMP && |
| 554 | LastInst->getOpcode() == X86::JMP) { |
| 555 | TBB = SecondLastInst->getOperand(0).getMachineBasicBlock(); |
| 556 | I = LastInst; |
| 557 | I->eraseFromParent(); |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | // Otherwise, can't handle this. |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 566 | MachineBasicBlock::iterator I = MBB.end(); |
| 567 | if (I == MBB.begin()) return 0; |
| 568 | --I; |
| 569 | if (I->getOpcode() != X86::JMP && |
| 570 | GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID) |
| 571 | return 0; |
| 572 | |
| 573 | // Remove the branch. |
| 574 | I->eraseFromParent(); |
| 575 | |
| 576 | I = MBB.end(); |
| 577 | |
| 578 | if (I == MBB.begin()) return 1; |
| 579 | --I; |
| 580 | if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID) |
| 581 | return 1; |
| 582 | |
| 583 | // Remove the branch. |
| 584 | I->eraseFromParent(); |
| 585 | return 2; |
| 586 | } |
| 587 | |
| 588 | unsigned |
| 589 | X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 590 | MachineBasicBlock *FBB, |
| 591 | const std::vector<MachineOperand> &Cond) const { |
| 592 | // Shouldn't be a fall through. |
| 593 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 594 | assert((Cond.size() == 1 || Cond.size() == 0) && |
| 595 | "X86 branch conditions have one component!"); |
| 596 | |
| 597 | if (FBB == 0) { // One way branch. |
| 598 | if (Cond.empty()) { |
| 599 | // Unconditional branch? |
| 600 | BuildMI(&MBB, get(X86::JMP)).addMBB(TBB); |
| 601 | } else { |
| 602 | // Conditional branch. |
| 603 | unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm()); |
| 604 | BuildMI(&MBB, get(Opc)).addMBB(TBB); |
| 605 | } |
| 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | // Two-way Conditional branch. |
| 610 | unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm()); |
| 611 | BuildMI(&MBB, get(Opc)).addMBB(TBB); |
| 612 | BuildMI(&MBB, get(X86::JMP)).addMBB(FBB); |
| 613 | return 2; |
| 614 | } |
| 615 | |
| 616 | bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const { |
| 617 | if (MBB.empty()) return false; |
| 618 | |
| 619 | switch (MBB.back().getOpcode()) { |
| 620 | case X86::RET: // Return. |
| 621 | case X86::RETI: |
| 622 | case X86::TAILJMPd: |
| 623 | case X86::TAILJMPr: |
| 624 | case X86::TAILJMPm: |
| 625 | case X86::JMP: // Uncond branch. |
| 626 | case X86::JMP32r: // Indirect branch. |
Dan Gohman | b15b6b5 | 2007-09-17 15:19:08 +0000 | [diff] [blame] | 627 | case X86::JMP64r: // Indirect branch (64-bit). |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 628 | case X86::JMP32m: // Indirect branch through mem. |
Dan Gohman | b15b6b5 | 2007-09-17 15:19:08 +0000 | [diff] [blame] | 629 | case X86::JMP64m: // Indirect branch through mem (64-bit). |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 630 | return true; |
| 631 | default: return false; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | bool X86InstrInfo:: |
| 636 | ReverseBranchCondition(std::vector<MachineOperand> &Cond) const { |
| 637 | assert(Cond.size() == 1 && "Invalid X86 branch condition!"); |
| 638 | Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm())); |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const { |
| 643 | const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 644 | if (Subtarget->is64Bit()) |
| 645 | return &X86::GR64RegClass; |
| 646 | else |
| 647 | return &X86::GR32RegClass; |
| 648 | } |