blob: 9d714279684fdd4de86036bcc1055c7d15b6176e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Owen Anderson6690c7f2008-01-04 23:57:37 +000018#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Owen Anderson1636de92007-09-07 04:06:50 +000021#include "llvm/ADT/STLExtras.h"
Owen Anderson6690c7f2008-01-04 23:57:37 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CodeGen/LiveVariables.h"
Evan Cheng950aac02007-09-25 01:57:46 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
29X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000030 : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 TM(tm), RI(tm, *this) {
32}
33
34bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
35 unsigned& sourceReg,
36 unsigned& destReg) const {
37 MachineOpCode oc = MI.getOpcode();
38 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
39 oc == X86::MOV32rr || oc == X86::MOV64rr ||
40 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
41 oc == X86::MOV_Fp3232 || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
42 oc == X86::MOV_Fp3264 || oc == X86::MOV_Fp6432 || oc == X86::MOV_Fp6464 ||
43 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
44 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
45 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
46 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
47 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
48 assert(MI.getNumOperands() >= 2 &&
49 MI.getOperand(0).isRegister() &&
50 MI.getOperand(1).isRegister() &&
51 "invalid register-register move instruction");
52 sourceReg = MI.getOperand(1).getReg();
53 destReg = MI.getOperand(0).getReg();
54 return true;
55 }
56 return false;
57}
58
59unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
60 int &FrameIndex) const {
61 switch (MI->getOpcode()) {
62 default: break;
63 case X86::MOV8rm:
64 case X86::MOV16rm:
65 case X86::MOV16_rm:
66 case X86::MOV32rm:
67 case X86::MOV32_rm:
68 case X86::MOV64rm:
69 case X86::LD_Fp64m:
70 case X86::MOVSSrm:
71 case X86::MOVSDrm:
72 case X86::MOVAPSrm:
73 case X86::MOVAPDrm:
74 case X86::MMX_MOVD64rm:
75 case X86::MMX_MOVQ64rm:
Chris Lattner6017d482007-12-30 23:10:15 +000076 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
77 MI->getOperand(3).isReg() && MI->getOperand(4).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +000078 MI->getOperand(2).getImm() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 MI->getOperand(3).getReg() == 0 &&
Chris Lattnera96056a2007-12-30 20:49:49 +000080 MI->getOperand(4).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +000081 FrameIndex = MI->getOperand(1).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 return MI->getOperand(0).getReg();
83 }
84 break;
85 }
86 return 0;
87}
88
89unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
90 int &FrameIndex) const {
91 switch (MI->getOpcode()) {
92 default: break;
93 case X86::MOV8mr:
94 case X86::MOV16mr:
95 case X86::MOV16_mr:
96 case X86::MOV32mr:
97 case X86::MOV32_mr:
98 case X86::MOV64mr:
99 case X86::ST_FpP64m:
100 case X86::MOVSSmr:
101 case X86::MOVSDmr:
102 case X86::MOVAPSmr:
103 case X86::MOVAPDmr:
104 case X86::MMX_MOVD64mr:
105 case X86::MMX_MOVQ64mr:
106 case X86::MMX_MOVNTQmr:
Chris Lattner6017d482007-12-30 23:10:15 +0000107 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
108 MI->getOperand(2).isReg() && MI->getOperand(3).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000109 MI->getOperand(1).getImm() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 MI->getOperand(2).getReg() == 0 &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000111 MI->getOperand(3).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +0000112 FrameIndex = MI->getOperand(0).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return MI->getOperand(4).getReg();
114 }
115 break;
116 }
117 return 0;
118}
119
120
Bill Wendling0fe34c22007-12-08 23:58:46 +0000121bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 switch (MI->getOpcode()) {
123 default: break;
124 case X86::MOV8rm:
125 case X86::MOV16rm:
126 case X86::MOV16_rm:
127 case X86::MOV32rm:
128 case X86::MOV32_rm:
129 case X86::MOV64rm:
130 case X86::LD_Fp64m:
131 case X86::MOVSSrm:
132 case X86::MOVSDrm:
133 case X86::MOVAPSrm:
134 case X86::MOVAPDrm:
135 case X86::MMX_MOVD64rm:
136 case X86::MMX_MOVQ64rm:
137 // Loads from constant pools are trivially rematerializable.
Chris Lattner00e46fa2008-01-05 05:28:30 +0000138 if (MI->getOperand(1).isReg() && MI->getOperand(2).isImm() &&
139 MI->getOperand(3).isReg() && MI->getOperand(4).isCPI() &&
140 MI->getOperand(1).getReg() == 0 &&
141 MI->getOperand(2).getImm() == 1 &&
142 MI->getOperand(3).getReg() == 0)
143 return true;
Chris Lattner5c6ee7a2008-01-05 06:10:42 +0000144
145 // If this is a load from a fixed argument slot, we know the value is
146 // invariant across the whole function, because we don't redefine argument
147 // values.
148#if 0
149 // FIXME: This is disabled due to a remat bug. rdar://5671644
150 MachineFunction *MF = MI->getParent()->getParent();
151 if (MI->getOperand(1).isFI() &&
152 MF->getFrameInfo()->isFixedObjectIndex(MI->getOperand(1).getIndex()))
153 return true;
154#endif
155
Chris Lattner00e46fa2008-01-05 05:28:30 +0000156 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
158 // All other instructions marked M_REMATERIALIZABLE are always trivially
159 // rematerializable.
160 return true;
161}
162
Bill Wendling57e31d62007-12-17 23:07:56 +0000163/// isReallySideEffectFree - If the M_MAY_HAVE_SIDE_EFFECTS flag is set, this
164/// method is called to determine if the specific instance of this instruction
165/// has side effects. This is useful in cases of instructions, like loads, which
166/// generally always have side effects. A load from a constant pool doesn't have
167/// side effects, though. So we need to differentiate it from the general case.
168bool X86InstrInfo::isReallySideEffectFree(MachineInstr *MI) const {
169 switch (MI->getOpcode()) {
170 default: break;
Bill Wendling0e3410c2007-12-30 03:18:58 +0000171 case X86::MOV32rm:
172 if (MI->getOperand(1).isRegister()) {
173 unsigned Reg = MI->getOperand(1).getReg();
Bill Wendlingc81e5b12008-01-05 09:18:04 +0000174 const X86Subtarget &ST = TM.getSubtarget<X86Subtarget>();
Bill Wendling0e3410c2007-12-30 03:18:58 +0000175
176 // Loads from global addresses which aren't redefined in the function are
177 // side effect free.
Bill Wendlingcb5043d2008-01-02 21:10:40 +0000178 if (Reg != 0 && MRegisterInfo::isVirtualRegister(Reg) &&
Chris Lattner00e46fa2008-01-05 05:28:30 +0000179 MI->getOperand(2).isImm() && MI->getOperand(3).isReg() &&
Bill Wendlingc81e5b12008-01-05 09:18:04 +0000180 MI->getOperand(4).isGlobal() &&
181 ST.GVRequiresExtraLoad(MI->getOperand(4).getGlobal(), TM, false) &&
182 MI->getOperand(2).getImm() == 1 &&
Bill Wendling0e3410c2007-12-30 03:18:58 +0000183 MI->getOperand(3).getReg() == 0)
184 return true;
185 }
Chris Lattnereb0f16f2008-01-05 05:26:26 +0000186 // FALLTHROUGH
187 case X86::MOV8rm:
188 case X86::MOV16rm:
189 case X86::MOV16_rm:
190 case X86::MOV32_rm:
191 case X86::MOV64rm:
192 case X86::LD_Fp64m:
193 case X86::MOVSSrm:
194 case X86::MOVSDrm:
195 case X86::MOVAPSrm:
196 case X86::MOVAPDrm:
197 case X86::MMX_MOVD64rm:
198 case X86::MMX_MOVQ64rm:
Chris Lattner00e46fa2008-01-05 05:28:30 +0000199 // Loads from constant pools are trivially rematerializable.
200 if (MI->getOperand(1).isReg() && MI->getOperand(2).isImm() &&
201 MI->getOperand(3).isReg() && MI->getOperand(4).isCPI() &&
202 MI->getOperand(1).getReg() == 0 &&
203 MI->getOperand(2).getImm() == 1 &&
204 MI->getOperand(3).getReg() == 0)
205 return true;
Chris Lattner5c6ee7a2008-01-05 06:10:42 +0000206
207 // If this is a load from a fixed argument slot, we know the value is
208 // invariant across the whole function, because we don't redefine argument
209 // values.
210 MachineFunction *MF = MI->getParent()->getParent();
211 if (MI->getOperand(1).isFI() &&
212 MF->getFrameInfo()->isFixedObjectIndex(MI->getOperand(1).getIndex()))
213 return true;
214
Chris Lattner00e46fa2008-01-05 05:28:30 +0000215 return false;
Bill Wendling57e31d62007-12-17 23:07:56 +0000216 }
217
Chris Lattnereb0f16f2008-01-05 05:26:26 +0000218 // All other instances of these instructions are presumed to have side
219 // effects.
220 return false;
Bill Wendling57e31d62007-12-17 23:07:56 +0000221}
222
Evan Chengfa1a4952007-10-05 08:04:01 +0000223/// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
224/// is not marked dead.
225static bool hasLiveCondCodeDef(MachineInstr *MI) {
Evan Chengfa1a4952007-10-05 08:04:01 +0000226 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
227 MachineOperand &MO = MI->getOperand(i);
228 if (MO.isRegister() && MO.isDef() &&
229 MO.getReg() == X86::EFLAGS && !MO.isDead()) {
230 return true;
231 }
232 }
233 return false;
234}
235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236/// convertToThreeAddress - This method must be implemented by targets that
237/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
238/// may be able to convert a two-address instruction into a true
239/// three-address instruction on demand. This allows the X86 target (for
240/// example) to convert ADD and SHL instructions into LEA instructions if they
241/// would require register copies due to two-addressness.
242///
243/// This method returns a null pointer if the transformation cannot be
244/// performed, otherwise it returns the new instruction.
245///
246MachineInstr *
247X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
248 MachineBasicBlock::iterator &MBBI,
249 LiveVariables &LV) const {
250 MachineInstr *MI = MBBI;
251 // All instructions input are two-addr instructions. Get the known operands.
252 unsigned Dest = MI->getOperand(0).getReg();
253 unsigned Src = MI->getOperand(1).getReg();
254
255 MachineInstr *NewMI = NULL;
256 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
257 // we have better subtarget support, enable the 16-bit LEA generation here.
258 bool DisableLEA16 = true;
259
Evan Cheng6b96ed32007-10-05 20:34:26 +0000260 unsigned MIOpc = MI->getOpcode();
261 switch (MIOpc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 case X86::SHUFPSrri: {
263 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
264 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
265
266 unsigned A = MI->getOperand(0).getReg();
267 unsigned B = MI->getOperand(1).getReg();
268 unsigned C = MI->getOperand(2).getReg();
269 unsigned M = MI->getOperand(3).getImm();
270 if (B != C) return 0;
271 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
272 break;
273 }
274 case X86::SHL64ri: {
Evan Cheng55687072007-09-14 21:48:26 +0000275 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
277 // the flags produced by a shift yet, so this is safe.
278 unsigned Dest = MI->getOperand(0).getReg();
279 unsigned Src = MI->getOperand(1).getReg();
280 unsigned ShAmt = MI->getOperand(2).getImm();
281 if (ShAmt == 0 || ShAmt >= 4) return 0;
282
283 NewMI = BuildMI(get(X86::LEA64r), Dest)
284 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
285 break;
286 }
287 case X86::SHL32ri: {
Evan Cheng55687072007-09-14 21:48:26 +0000288 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
290 // the flags produced by a shift yet, so this is safe.
291 unsigned Dest = MI->getOperand(0).getReg();
292 unsigned Src = MI->getOperand(1).getReg();
293 unsigned ShAmt = MI->getOperand(2).getImm();
294 if (ShAmt == 0 || ShAmt >= 4) return 0;
295
296 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
297 X86::LEA64_32r : X86::LEA32r;
298 NewMI = BuildMI(get(Opc), Dest)
299 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
300 break;
301 }
302 case X86::SHL16ri: {
Evan Cheng55687072007-09-14 21:48:26 +0000303 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Evan Cheng0b1e8712007-09-06 00:14:41 +0000304 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
305 // the flags produced by a shift yet, so this is safe.
306 unsigned Dest = MI->getOperand(0).getReg();
307 unsigned Src = MI->getOperand(1).getReg();
308 unsigned ShAmt = MI->getOperand(2).getImm();
309 if (ShAmt == 0 || ShAmt >= 4) return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
Christopher Lamb380c6272007-08-10 21:18:25 +0000311 if (DisableLEA16) {
312 // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
Chris Lattner1b989192007-12-31 04:13:23 +0000313 MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
Evan Cheng0b1e8712007-09-06 00:14:41 +0000314 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
315 ? X86::LEA64_32r : X86::LEA32r;
Chris Lattner1b989192007-12-31 04:13:23 +0000316 unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
317 unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
Christopher Lamb380c6272007-08-10 21:18:25 +0000318
Evan Cheng0b1e8712007-09-06 00:14:41 +0000319 MachineInstr *Ins =
320 BuildMI(get(X86::INSERT_SUBREG), leaInReg).addReg(Src).addImm(2);
Christopher Lamb380c6272007-08-10 21:18:25 +0000321 Ins->copyKillDeadInfo(MI);
322
323 NewMI = BuildMI(get(Opc), leaOutReg)
324 .addReg(0).addImm(1 << ShAmt).addReg(leaInReg).addImm(0);
325
Evan Cheng0b1e8712007-09-06 00:14:41 +0000326 MachineInstr *Ext =
327 BuildMI(get(X86::EXTRACT_SUBREG), Dest).addReg(leaOutReg).addImm(2);
Christopher Lamb380c6272007-08-10 21:18:25 +0000328 Ext->copyKillDeadInfo(MI);
329
330 MFI->insert(MBBI, Ins); // Insert the insert_subreg
331 LV.instructionChanged(MI, NewMI); // Update live variables
332 LV.addVirtualRegisterKilled(leaInReg, NewMI);
333 MFI->insert(MBBI, NewMI); // Insert the new inst
334 LV.addVirtualRegisterKilled(leaOutReg, Ext);
Evan Cheng0b1e8712007-09-06 00:14:41 +0000335 MFI->insert(MBBI, Ext); // Insert the extract_subreg
Christopher Lamb380c6272007-08-10 21:18:25 +0000336 return Ext;
337 } else {
338 NewMI = BuildMI(get(X86::LEA16r), Dest)
339 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
340 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 break;
342 }
Evan Cheng6b96ed32007-10-05 20:34:26 +0000343 default: {
344 // The following opcodes also sets the condition code register(s). Only
345 // convert them to equivalent lea if the condition code register def's
346 // are dead!
347 if (hasLiveCondCodeDef(MI))
348 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
Evan Chenga28a9562007-10-09 07:14:53 +0000350 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng6b96ed32007-10-05 20:34:26 +0000351 switch (MIOpc) {
352 default: return 0;
353 case X86::INC64r:
Evan Cheng3cdc7192007-10-05 21:55:32 +0000354 case X86::INC32r: {
Evan Cheng6b96ed32007-10-05 20:34:26 +0000355 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +0000356 unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
357 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Cheng6b96ed32007-10-05 20:34:26 +0000358 NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, 1);
359 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 }
Evan Cheng6b96ed32007-10-05 20:34:26 +0000361 case X86::INC16r:
362 case X86::INC64_16r:
363 if (DisableLEA16) return 0;
364 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
365 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
366 break;
367 case X86::DEC64r:
Evan Cheng3cdc7192007-10-05 21:55:32 +0000368 case X86::DEC32r: {
Evan Cheng6b96ed32007-10-05 20:34:26 +0000369 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +0000370 unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
371 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Cheng6b96ed32007-10-05 20:34:26 +0000372 NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, -1);
373 break;
374 }
375 case X86::DEC16r:
376 case X86::DEC64_16r:
377 if (DisableLEA16) return 0;
378 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
379 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
380 break;
381 case X86::ADD64rr:
382 case X86::ADD32rr: {
383 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +0000384 unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
385 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Cheng6b96ed32007-10-05 20:34:26 +0000386 NewMI = addRegReg(BuildMI(get(Opc), Dest), Src,
387 MI->getOperand(2).getReg());
388 break;
389 }
390 case X86::ADD16rr:
391 if (DisableLEA16) return 0;
392 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
393 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
394 MI->getOperand(2).getReg());
395 break;
396 case X86::ADD64ri32:
397 case X86::ADD64ri8:
398 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
399 if (MI->getOperand(2).isImmediate())
400 NewMI = addRegOffset(BuildMI(get(X86::LEA64r), Dest), Src,
Chris Lattnera96056a2007-12-30 20:49:49 +0000401 MI->getOperand(2).getImm());
Evan Cheng6b96ed32007-10-05 20:34:26 +0000402 break;
403 case X86::ADD32ri:
404 case X86::ADD32ri8:
405 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +0000406 if (MI->getOperand(2).isImmediate()) {
407 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
408 NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src,
Chris Lattnera96056a2007-12-30 20:49:49 +0000409 MI->getOperand(2).getImm());
Evan Chenga28a9562007-10-09 07:14:53 +0000410 }
Evan Cheng6b96ed32007-10-05 20:34:26 +0000411 break;
412 case X86::ADD16ri:
413 case X86::ADD16ri8:
414 if (DisableLEA16) return 0;
415 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
416 if (MI->getOperand(2).isImmediate())
417 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnera96056a2007-12-30 20:49:49 +0000418 MI->getOperand(2).getImm());
Evan Cheng6b96ed32007-10-05 20:34:26 +0000419 break;
420 case X86::SHL16ri:
421 if (DisableLEA16) return 0;
422 case X86::SHL32ri:
423 case X86::SHL64ri: {
424 assert(MI->getNumOperands() >= 3 && MI->getOperand(2).isImmediate() &&
425 "Unknown shl instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000426 unsigned ShAmt = MI->getOperand(2).getImm();
Evan Cheng6b96ed32007-10-05 20:34:26 +0000427 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
428 X86AddressMode AM;
429 AM.Scale = 1 << ShAmt;
430 AM.IndexReg = Src;
431 unsigned Opc = MIOpc == X86::SHL64ri ? X86::LEA64r
Evan Chenga28a9562007-10-09 07:14:53 +0000432 : (MIOpc == X86::SHL32ri
433 ? (is64Bit ? X86::LEA64_32r : X86::LEA32r) : X86::LEA16r);
Evan Cheng6b96ed32007-10-05 20:34:26 +0000434 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
435 }
436 break;
437 }
438 }
439 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 }
441
Evan Cheng6b96ed32007-10-05 20:34:26 +0000442 NewMI->copyKillDeadInfo(MI);
443 LV.instructionChanged(MI, NewMI); // Update live variables
444 MFI->insert(MBBI, NewMI); // Insert the new inst
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 return NewMI;
446}
447
448/// commuteInstruction - We have a few instructions that must be hacked on to
449/// commute them.
450///
451MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 switch (MI->getOpcode()) {
453 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
454 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
455 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
Dan Gohman4d9fc4a2007-09-14 23:17:45 +0000456 case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
457 case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
458 case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 unsigned Opc;
460 unsigned Size;
461 switch (MI->getOpcode()) {
462 default: assert(0 && "Unreachable!");
463 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
464 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
465 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
466 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
Dan Gohman4d9fc4a2007-09-14 23:17:45 +0000467 case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
468 case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 }
Chris Lattnera96056a2007-12-30 20:49:49 +0000470 unsigned Amt = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 unsigned A = MI->getOperand(0).getReg();
472 unsigned B = MI->getOperand(1).getReg();
473 unsigned C = MI->getOperand(2).getReg();
474 bool BisKill = MI->getOperand(1).isKill();
475 bool CisKill = MI->getOperand(2).isKill();
476 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
477 .addReg(B, false, false, BisKill).addImm(Size-Amt);
478 }
Evan Cheng926658c2007-10-05 23:13:21 +0000479 case X86::CMOVB16rr:
480 case X86::CMOVB32rr:
481 case X86::CMOVB64rr:
482 case X86::CMOVAE16rr:
483 case X86::CMOVAE32rr:
484 case X86::CMOVAE64rr:
485 case X86::CMOVE16rr:
486 case X86::CMOVE32rr:
487 case X86::CMOVE64rr:
488 case X86::CMOVNE16rr:
489 case X86::CMOVNE32rr:
490 case X86::CMOVNE64rr:
491 case X86::CMOVBE16rr:
492 case X86::CMOVBE32rr:
493 case X86::CMOVBE64rr:
494 case X86::CMOVA16rr:
495 case X86::CMOVA32rr:
496 case X86::CMOVA64rr:
497 case X86::CMOVL16rr:
498 case X86::CMOVL32rr:
499 case X86::CMOVL64rr:
500 case X86::CMOVGE16rr:
501 case X86::CMOVGE32rr:
502 case X86::CMOVGE64rr:
503 case X86::CMOVLE16rr:
504 case X86::CMOVLE32rr:
505 case X86::CMOVLE64rr:
506 case X86::CMOVG16rr:
507 case X86::CMOVG32rr:
508 case X86::CMOVG64rr:
509 case X86::CMOVS16rr:
510 case X86::CMOVS32rr:
511 case X86::CMOVS64rr:
512 case X86::CMOVNS16rr:
513 case X86::CMOVNS32rr:
514 case X86::CMOVNS64rr:
515 case X86::CMOVP16rr:
516 case X86::CMOVP32rr:
517 case X86::CMOVP64rr:
518 case X86::CMOVNP16rr:
519 case X86::CMOVNP32rr:
520 case X86::CMOVNP64rr: {
Evan Cheng926658c2007-10-05 23:13:21 +0000521 unsigned Opc = 0;
522 switch (MI->getOpcode()) {
523 default: break;
524 case X86::CMOVB16rr: Opc = X86::CMOVAE16rr; break;
525 case X86::CMOVB32rr: Opc = X86::CMOVAE32rr; break;
526 case X86::CMOVB64rr: Opc = X86::CMOVAE64rr; break;
527 case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
528 case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
529 case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
530 case X86::CMOVE16rr: Opc = X86::CMOVNE16rr; break;
531 case X86::CMOVE32rr: Opc = X86::CMOVNE32rr; break;
532 case X86::CMOVE64rr: Opc = X86::CMOVNE64rr; break;
533 case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
534 case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
535 case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
536 case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
537 case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
538 case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
539 case X86::CMOVA16rr: Opc = X86::CMOVBE16rr; break;
540 case X86::CMOVA32rr: Opc = X86::CMOVBE32rr; break;
541 case X86::CMOVA64rr: Opc = X86::CMOVBE64rr; break;
542 case X86::CMOVL16rr: Opc = X86::CMOVGE16rr; break;
543 case X86::CMOVL32rr: Opc = X86::CMOVGE32rr; break;
544 case X86::CMOVL64rr: Opc = X86::CMOVGE64rr; break;
545 case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
546 case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
547 case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
548 case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
549 case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
550 case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
551 case X86::CMOVG16rr: Opc = X86::CMOVLE16rr; break;
552 case X86::CMOVG32rr: Opc = X86::CMOVLE32rr; break;
553 case X86::CMOVG64rr: Opc = X86::CMOVLE64rr; break;
554 case X86::CMOVS16rr: Opc = X86::CMOVNS16rr; break;
555 case X86::CMOVS32rr: Opc = X86::CMOVNS32rr; break;
556 case X86::CMOVS64rr: Opc = X86::CMOVNS32rr; break;
557 case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
558 case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
559 case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
560 case X86::CMOVP16rr: Opc = X86::CMOVNP16rr; break;
561 case X86::CMOVP32rr: Opc = X86::CMOVNP32rr; break;
562 case X86::CMOVP64rr: Opc = X86::CMOVNP32rr; break;
563 case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
564 case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
565 case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
566 }
567
568 MI->setInstrDescriptor(get(Opc));
569 // Fallthrough intended.
570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 default:
Chris Lattner6ca3a8e2008-01-01 01:05:34 +0000572 return TargetInstrInfoImpl::commuteInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
574}
575
576static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
577 switch (BrOpc) {
578 default: return X86::COND_INVALID;
579 case X86::JE: return X86::COND_E;
580 case X86::JNE: return X86::COND_NE;
581 case X86::JL: return X86::COND_L;
582 case X86::JLE: return X86::COND_LE;
583 case X86::JG: return X86::COND_G;
584 case X86::JGE: return X86::COND_GE;
585 case X86::JB: return X86::COND_B;
586 case X86::JBE: return X86::COND_BE;
587 case X86::JA: return X86::COND_A;
588 case X86::JAE: return X86::COND_AE;
589 case X86::JS: return X86::COND_S;
590 case X86::JNS: return X86::COND_NS;
591 case X86::JP: return X86::COND_P;
592 case X86::JNP: return X86::COND_NP;
593 case X86::JO: return X86::COND_O;
594 case X86::JNO: return X86::COND_NO;
595 }
596}
597
598unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
599 switch (CC) {
600 default: assert(0 && "Illegal condition code!");
Evan Cheng621216e2007-09-29 00:00:36 +0000601 case X86::COND_E: return X86::JE;
602 case X86::COND_NE: return X86::JNE;
603 case X86::COND_L: return X86::JL;
604 case X86::COND_LE: return X86::JLE;
605 case X86::COND_G: return X86::JG;
606 case X86::COND_GE: return X86::JGE;
607 case X86::COND_B: return X86::JB;
608 case X86::COND_BE: return X86::JBE;
609 case X86::COND_A: return X86::JA;
610 case X86::COND_AE: return X86::JAE;
611 case X86::COND_S: return X86::JS;
612 case X86::COND_NS: return X86::JNS;
613 case X86::COND_P: return X86::JP;
614 case X86::COND_NP: return X86::JNP;
615 case X86::COND_O: return X86::JO;
616 case X86::COND_NO: return X86::JNO;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 }
618}
619
620/// GetOppositeBranchCondition - Return the inverse of the specified condition,
621/// e.g. turning COND_E to COND_NE.
622X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
623 switch (CC) {
624 default: assert(0 && "Illegal condition code!");
625 case X86::COND_E: return X86::COND_NE;
626 case X86::COND_NE: return X86::COND_E;
627 case X86::COND_L: return X86::COND_GE;
628 case X86::COND_LE: return X86::COND_G;
629 case X86::COND_G: return X86::COND_LE;
630 case X86::COND_GE: return X86::COND_L;
631 case X86::COND_B: return X86::COND_AE;
632 case X86::COND_BE: return X86::COND_A;
633 case X86::COND_A: return X86::COND_BE;
634 case X86::COND_AE: return X86::COND_B;
635 case X86::COND_S: return X86::COND_NS;
636 case X86::COND_NS: return X86::COND_S;
637 case X86::COND_P: return X86::COND_NP;
638 case X86::COND_NP: return X86::COND_P;
639 case X86::COND_O: return X86::COND_NO;
640 case X86::COND_NO: return X86::COND_O;
641 }
642}
643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
646 if (TID->Flags & M_TERMINATOR_FLAG) {
647 // Conditional branch is a special case.
648 if ((TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0)
649 return true;
650 if ((TID->Flags & M_PREDICABLE) == 0)
651 return true;
652 return !isPredicated(MI);
653 }
654 return false;
655}
656
Evan Cheng12515792007-07-26 17:32:14 +0000657// For purposes of branch analysis do not count FP_REG_KILL as a terminator.
658static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
659 const X86InstrInfo &TII) {
660 if (MI->getOpcode() == X86::FP_REG_KILL)
661 return false;
662 return TII.isUnpredicatedTerminator(MI);
663}
664
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
666 MachineBasicBlock *&TBB,
667 MachineBasicBlock *&FBB,
668 std::vector<MachineOperand> &Cond) const {
669 // If the block has no terminators, it just falls into the block after it.
670 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng12515792007-07-26 17:32:14 +0000671 if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 return false;
673
674 // Get the last instruction in the block.
675 MachineInstr *LastInst = I;
676
677 // If there is only one terminator instruction, process it.
Evan Cheng12515792007-07-26 17:32:14 +0000678 if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 if (!isBranch(LastInst->getOpcode()))
680 return true;
681
682 // If the block ends with a branch there are 3 possibilities:
683 // it's an unconditional, conditional, or indirect branch.
684
685 if (LastInst->getOpcode() == X86::JMP) {
Chris Lattner6017d482007-12-30 23:10:15 +0000686 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 return false;
688 }
689 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
690 if (BranchCode == X86::COND_INVALID)
691 return true; // Can't handle indirect branch.
692
693 // Otherwise, block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000694 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 Cond.push_back(MachineOperand::CreateImm(BranchCode));
696 return false;
697 }
698
699 // Get the instruction before it if it's a terminator.
700 MachineInstr *SecondLastInst = I;
701
702 // If there are three terminators, we don't know what sort of block this is.
Evan Cheng12515792007-07-26 17:32:14 +0000703 if (SecondLastInst && I != MBB.begin() &&
704 isBrAnalysisUnpredicatedTerminator(--I, *this))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 return true;
706
707 // If the block ends with X86::JMP and a conditional branch, handle it.
708 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
709 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner6017d482007-12-30 23:10:15 +0000710 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 Cond.push_back(MachineOperand::CreateImm(BranchCode));
Chris Lattner6017d482007-12-30 23:10:15 +0000712 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 return false;
714 }
715
716 // If the block ends with two X86::JMPs, handle it. The second one is not
717 // executed, so remove it.
718 if (SecondLastInst->getOpcode() == X86::JMP &&
719 LastInst->getOpcode() == X86::JMP) {
Chris Lattner6017d482007-12-30 23:10:15 +0000720 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 I = LastInst;
722 I->eraseFromParent();
723 return false;
724 }
725
726 // Otherwise, can't handle this.
727 return true;
728}
729
730unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
731 MachineBasicBlock::iterator I = MBB.end();
732 if (I == MBB.begin()) return 0;
733 --I;
734 if (I->getOpcode() != X86::JMP &&
735 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
736 return 0;
737
738 // Remove the branch.
739 I->eraseFromParent();
740
741 I = MBB.end();
742
743 if (I == MBB.begin()) return 1;
744 --I;
745 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
746 return 1;
747
748 // Remove the branch.
749 I->eraseFromParent();
750 return 2;
751}
752
Owen Anderson81875432008-01-01 21:11:32 +0000753static const MachineInstrBuilder &X86InstrAddOperand(MachineInstrBuilder &MIB,
754 MachineOperand &MO) {
755 if (MO.isRegister())
756 MIB = MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit(),
757 false, false, MO.getSubReg());
758 else if (MO.isImmediate())
759 MIB = MIB.addImm(MO.getImm());
760 else if (MO.isFrameIndex())
761 MIB = MIB.addFrameIndex(MO.getIndex());
762 else if (MO.isGlobalAddress())
763 MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset());
764 else if (MO.isConstantPoolIndex())
765 MIB = MIB.addConstantPoolIndex(MO.getIndex(), MO.getOffset());
766 else if (MO.isJumpTableIndex())
767 MIB = MIB.addJumpTableIndex(MO.getIndex());
768 else if (MO.isExternalSymbol())
769 MIB = MIB.addExternalSymbol(MO.getSymbolName());
770 else
771 assert(0 && "Unknown operand for X86InstrAddOperand!");
772
773 return MIB;
774}
775
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776unsigned
777X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
778 MachineBasicBlock *FBB,
779 const std::vector<MachineOperand> &Cond) const {
780 // Shouldn't be a fall through.
781 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
782 assert((Cond.size() == 1 || Cond.size() == 0) &&
783 "X86 branch conditions have one component!");
784
785 if (FBB == 0) { // One way branch.
786 if (Cond.empty()) {
787 // Unconditional branch?
788 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
789 } else {
790 // Conditional branch.
791 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
792 BuildMI(&MBB, get(Opc)).addMBB(TBB);
793 }
794 return 1;
795 }
796
797 // Two-way Conditional branch.
798 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
799 BuildMI(&MBB, get(Opc)).addMBB(TBB);
800 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
801 return 2;
802}
803
Owen Anderson8f2c8932007-12-31 06:32:00 +0000804void X86InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
805 MachineBasicBlock::iterator MI,
806 unsigned DestReg, unsigned SrcReg,
807 const TargetRegisterClass *DestRC,
808 const TargetRegisterClass *SrcRC) const {
809 if (DestRC != SrcRC) {
810 // Moving EFLAGS to / from another register requires a push and a pop.
811 if (SrcRC == &X86::CCRRegClass) {
812 assert(SrcReg == X86::EFLAGS);
813 if (DestRC == &X86::GR64RegClass) {
814 BuildMI(MBB, MI, get(X86::PUSHFQ));
815 BuildMI(MBB, MI, get(X86::POP64r), DestReg);
816 return;
817 } else if (DestRC == &X86::GR32RegClass) {
818 BuildMI(MBB, MI, get(X86::PUSHFD));
819 BuildMI(MBB, MI, get(X86::POP32r), DestReg);
820 return;
821 }
822 } else if (DestRC == &X86::CCRRegClass) {
823 assert(DestReg == X86::EFLAGS);
824 if (SrcRC == &X86::GR64RegClass) {
825 BuildMI(MBB, MI, get(X86::PUSH64r)).addReg(SrcReg);
826 BuildMI(MBB, MI, get(X86::POPFQ));
827 return;
828 } else if (SrcRC == &X86::GR32RegClass) {
829 BuildMI(MBB, MI, get(X86::PUSH32r)).addReg(SrcReg);
830 BuildMI(MBB, MI, get(X86::POPFD));
831 return;
832 }
833 }
834 cerr << "Not yet supported!";
835 abort();
836 }
837
838 unsigned Opc;
839 if (DestRC == &X86::GR64RegClass) {
840 Opc = X86::MOV64rr;
841 } else if (DestRC == &X86::GR32RegClass) {
842 Opc = X86::MOV32rr;
843 } else if (DestRC == &X86::GR16RegClass) {
844 Opc = X86::MOV16rr;
845 } else if (DestRC == &X86::GR8RegClass) {
846 Opc = X86::MOV8rr;
847 } else if (DestRC == &X86::GR32_RegClass) {
848 Opc = X86::MOV32_rr;
849 } else if (DestRC == &X86::GR16_RegClass) {
850 Opc = X86::MOV16_rr;
851 } else if (DestRC == &X86::RFP32RegClass) {
852 Opc = X86::MOV_Fp3232;
853 } else if (DestRC == &X86::RFP64RegClass || DestRC == &X86::RSTRegClass) {
854 Opc = X86::MOV_Fp6464;
855 } else if (DestRC == &X86::RFP80RegClass) {
856 Opc = X86::MOV_Fp8080;
857 } else if (DestRC == &X86::FR32RegClass) {
858 Opc = X86::FsMOVAPSrr;
859 } else if (DestRC == &X86::FR64RegClass) {
860 Opc = X86::FsMOVAPDrr;
861 } else if (DestRC == &X86::VR128RegClass) {
862 Opc = X86::MOVAPSrr;
863 } else if (DestRC == &X86::VR64RegClass) {
864 Opc = X86::MMX_MOVQ64rr;
865 } else {
866 assert(0 && "Unknown regclass");
867 abort();
868 }
869 BuildMI(MBB, MI, get(Opc), DestReg).addReg(SrcReg);
870}
871
Owen Anderson81875432008-01-01 21:11:32 +0000872static unsigned getStoreRegOpcode(const TargetRegisterClass *RC,
873 unsigned StackAlign) {
874 unsigned Opc = 0;
875 if (RC == &X86::GR64RegClass) {
876 Opc = X86::MOV64mr;
877 } else if (RC == &X86::GR32RegClass) {
878 Opc = X86::MOV32mr;
879 } else if (RC == &X86::GR16RegClass) {
880 Opc = X86::MOV16mr;
881 } else if (RC == &X86::GR8RegClass) {
882 Opc = X86::MOV8mr;
883 } else if (RC == &X86::GR32_RegClass) {
884 Opc = X86::MOV32_mr;
885 } else if (RC == &X86::GR16_RegClass) {
886 Opc = X86::MOV16_mr;
887 } else if (RC == &X86::RFP80RegClass) {
888 Opc = X86::ST_FpP80m; // pops
889 } else if (RC == &X86::RFP64RegClass) {
890 Opc = X86::ST_Fp64m;
891 } else if (RC == &X86::RFP32RegClass) {
892 Opc = X86::ST_Fp32m;
893 } else if (RC == &X86::FR32RegClass) {
894 Opc = X86::MOVSSmr;
895 } else if (RC == &X86::FR64RegClass) {
896 Opc = X86::MOVSDmr;
897 } else if (RC == &X86::VR128RegClass) {
898 // FIXME: Use movaps once we are capable of selectively
899 // aligning functions that spill SSE registers on 16-byte boundaries.
900 Opc = StackAlign >= 16 ? X86::MOVAPSmr : X86::MOVUPSmr;
901 } else if (RC == &X86::VR64RegClass) {
902 Opc = X86::MMX_MOVQ64mr;
903 } else {
904 assert(0 && "Unknown regclass");
905 abort();
906 }
907
908 return Opc;
909}
910
911void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
912 MachineBasicBlock::iterator MI,
913 unsigned SrcReg, bool isKill, int FrameIdx,
914 const TargetRegisterClass *RC) const {
915 unsigned Opc = getStoreRegOpcode(RC, RI.getStackAlignment());
916 addFrameReference(BuildMI(MBB, MI, get(Opc)), FrameIdx)
917 .addReg(SrcReg, false, false, isKill);
918}
919
920void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
921 bool isKill,
922 SmallVectorImpl<MachineOperand> &Addr,
923 const TargetRegisterClass *RC,
924 SmallVectorImpl<MachineInstr*> &NewMIs) const {
925 unsigned Opc = getStoreRegOpcode(RC, RI.getStackAlignment());
926 MachineInstrBuilder MIB = BuildMI(get(Opc));
927 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
928 MIB = X86InstrAddOperand(MIB, Addr[i]);
929 MIB.addReg(SrcReg, false, false, isKill);
930 NewMIs.push_back(MIB);
931}
932
933static unsigned getLoadRegOpcode(const TargetRegisterClass *RC,
934 unsigned StackAlign) {
935 unsigned Opc = 0;
936 if (RC == &X86::GR64RegClass) {
937 Opc = X86::MOV64rm;
938 } else if (RC == &X86::GR32RegClass) {
939 Opc = X86::MOV32rm;
940 } else if (RC == &X86::GR16RegClass) {
941 Opc = X86::MOV16rm;
942 } else if (RC == &X86::GR8RegClass) {
943 Opc = X86::MOV8rm;
944 } else if (RC == &X86::GR32_RegClass) {
945 Opc = X86::MOV32_rm;
946 } else if (RC == &X86::GR16_RegClass) {
947 Opc = X86::MOV16_rm;
948 } else if (RC == &X86::RFP80RegClass) {
949 Opc = X86::LD_Fp80m;
950 } else if (RC == &X86::RFP64RegClass) {
951 Opc = X86::LD_Fp64m;
952 } else if (RC == &X86::RFP32RegClass) {
953 Opc = X86::LD_Fp32m;
954 } else if (RC == &X86::FR32RegClass) {
955 Opc = X86::MOVSSrm;
956 } else if (RC == &X86::FR64RegClass) {
957 Opc = X86::MOVSDrm;
958 } else if (RC == &X86::VR128RegClass) {
959 // FIXME: Use movaps once we are capable of selectively
960 // aligning functions that spill SSE registers on 16-byte boundaries.
961 Opc = StackAlign >= 16 ? X86::MOVAPSrm : X86::MOVUPSrm;
962 } else if (RC == &X86::VR64RegClass) {
963 Opc = X86::MMX_MOVQ64rm;
964 } else {
965 assert(0 && "Unknown regclass");
966 abort();
967 }
968
969 return Opc;
970}
971
972void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
973 MachineBasicBlock::iterator MI,
974 unsigned DestReg, int FrameIdx,
975 const TargetRegisterClass *RC) const{
976 unsigned Opc = getLoadRegOpcode(RC, RI.getStackAlignment());
977 addFrameReference(BuildMI(MBB, MI, get(Opc), DestReg), FrameIdx);
978}
979
980void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
981 SmallVectorImpl<MachineOperand> &Addr,
982 const TargetRegisterClass *RC,
983 SmallVectorImpl<MachineInstr*> &NewMIs) const {
984 unsigned Opc = getLoadRegOpcode(RC, RI.getStackAlignment());
985 MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
986 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
987 MIB = X86InstrAddOperand(MIB, Addr[i]);
988 NewMIs.push_back(MIB);
989}
990
Owen Anderson6690c7f2008-01-04 23:57:37 +0000991bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
992 MachineBasicBlock::iterator MI,
993 const std::vector<CalleeSavedInfo> &CSI) const {
994 if (CSI.empty())
995 return false;
996
997 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
998 unsigned SlotSize = is64Bit ? 8 : 4;
999
1000 MachineFunction &MF = *MBB.getParent();
1001 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1002 X86FI->setCalleeSavedFrameSize(CSI.size() * SlotSize);
1003
1004 unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
1005 for (unsigned i = CSI.size(); i != 0; --i) {
1006 unsigned Reg = CSI[i-1].getReg();
1007 // Add the callee-saved register as live-in. It's killed at the spill.
1008 MBB.addLiveIn(Reg);
1009 BuildMI(MBB, MI, get(Opc)).addReg(Reg);
1010 }
1011 return true;
1012}
1013
1014bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1015 MachineBasicBlock::iterator MI,
1016 const std::vector<CalleeSavedInfo> &CSI) const {
1017 if (CSI.empty())
1018 return false;
1019
1020 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
1021
1022 unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
1023 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1024 unsigned Reg = CSI[i].getReg();
1025 BuildMI(MBB, MI, get(Opc), Reg);
1026 }
1027 return true;
1028}
1029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
1031 if (MBB.empty()) return false;
1032
1033 switch (MBB.back().getOpcode()) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001034 case X86::TCRETURNri:
1035 case X86::TCRETURNdi:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 case X86::RET: // Return.
1037 case X86::RETI:
1038 case X86::TAILJMPd:
1039 case X86::TAILJMPr:
1040 case X86::TAILJMPm:
1041 case X86::JMP: // Uncond branch.
1042 case X86::JMP32r: // Indirect branch.
Dan Gohmanb15b6b52007-09-17 15:19:08 +00001043 case X86::JMP64r: // Indirect branch (64-bit).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 case X86::JMP32m: // Indirect branch through mem.
Dan Gohmanb15b6b52007-09-17 15:19:08 +00001045 case X86::JMP64m: // Indirect branch through mem (64-bit).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 return true;
1047 default: return false;
1048 }
1049}
1050
1051bool X86InstrInfo::
1052ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
1053 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
1054 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
1055 return false;
1056}
1057
1058const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
1059 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
1060 if (Subtarget->is64Bit())
1061 return &X86::GR64RegClass;
1062 else
1063 return &X86::GR32RegClass;
1064}