Fix comment.
[oota-llvm.git] / lib / Target / X86 / X86InstrInfo.cpp
1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "X86MachineFunctionInfo.h"
19 #include "X86Subtarget.h"
20 #include "X86TargetMachine.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/LiveVariables.h"
26 #include "llvm/Target/TargetOptions.h"
27 using namespace llvm;
28
29 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
30   : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
31     TM(tm), RI(tm, *this) {
32 }
33
34 bool 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
59 unsigned 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:
76     if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
77         MI->getOperand(3).isReg() && MI->getOperand(4).isImm() &&
78         MI->getOperand(2).getImm() == 1 &&
79         MI->getOperand(3).getReg() == 0 &&
80         MI->getOperand(4).getImm() == 0) {
81       FrameIndex = MI->getOperand(1).getIndex();
82       return MI->getOperand(0).getReg();
83     }
84     break;
85   }
86   return 0;
87 }
88
89 unsigned 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:
107     if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
108         MI->getOperand(2).isReg() && MI->getOperand(3).isImm() &&
109         MI->getOperand(1).getImm() == 1 &&
110         MI->getOperand(2).getReg() == 0 &&
111         MI->getOperand(3).getImm() == 0) {
112       FrameIndex = MI->getOperand(0).getIndex();
113       return MI->getOperand(4).getReg();
114     }
115     break;
116   }
117   return 0;
118 }
119
120
121 bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
122   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.
138     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;
144       
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       
156     return false;
157   }
158   // All other instructions marked M_REMATERIALIZABLE are always trivially
159   // rematerializable.
160   return true;
161 }
162
163 /// 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.
168 bool X86InstrInfo::isReallySideEffectFree(MachineInstr *MI) const {
169   switch (MI->getOpcode()) {
170   default: break;
171   case X86::MOV32rm:
172     if (MI->getOperand(1).isRegister()) {
173       unsigned Reg = MI->getOperand(1).getReg();
174       const X86Subtarget &ST = TM.getSubtarget<X86Subtarget>();
175
176       // Loads from stubs of global addresses are side effect free.
177       if (Reg != 0 && MRegisterInfo::isVirtualRegister(Reg) &&
178           MI->getOperand(2).isImm() && MI->getOperand(3).isReg() &&
179           MI->getOperand(4).isGlobal() &&
180           ST.GVRequiresExtraLoad(MI->getOperand(4).getGlobal(), TM, false) &&
181           MI->getOperand(2).getImm() == 1 &&
182           MI->getOperand(3).getReg() == 0)
183         return true;
184     }
185     // FALLTHROUGH
186   case X86::MOV8rm:
187   case X86::MOV16rm:
188   case X86::MOV16_rm:
189   case X86::MOV32_rm:
190   case X86::MOV64rm:
191   case X86::LD_Fp64m:
192   case X86::MOVSSrm:
193   case X86::MOVSDrm:
194   case X86::MOVAPSrm:
195   case X86::MOVAPDrm:
196   case X86::MMX_MOVD64rm:
197   case X86::MMX_MOVQ64rm:
198     // Loads from constant pools are trivially rematerializable.
199     if (MI->getOperand(1).isReg() && MI->getOperand(2).isImm() &&
200         MI->getOperand(3).isReg() && MI->getOperand(4).isCPI() &&
201         MI->getOperand(1).getReg() == 0 &&
202         MI->getOperand(2).getImm() == 1 &&
203         MI->getOperand(3).getReg() == 0)
204       return true;
205       
206     // If this is a load from a fixed argument slot, we know the value is
207     // invariant across the whole function, because we don't redefine argument
208     // values.
209     MachineFunction *MF = MI->getParent()->getParent();
210     if (MI->getOperand(1).isFI() && 
211         MF->getFrameInfo()->isFixedObjectIndex(MI->getOperand(1).getIndex()))
212       return true;
213       
214     return false;
215   }
216
217   // All other instances of these instructions are presumed to have side
218   // effects.
219   return false;
220 }
221
222 /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
223 /// is not marked dead.
224 static bool hasLiveCondCodeDef(MachineInstr *MI) {
225   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
226     MachineOperand &MO = MI->getOperand(i);
227     if (MO.isRegister() && MO.isDef() &&
228         MO.getReg() == X86::EFLAGS && !MO.isDead()) {
229       return true;
230     }
231   }
232   return false;
233 }
234
235 /// convertToThreeAddress - This method must be implemented by targets that
236 /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
237 /// may be able to convert a two-address instruction into a true
238 /// three-address instruction on demand.  This allows the X86 target (for
239 /// example) to convert ADD and SHL instructions into LEA instructions if they
240 /// would require register copies due to two-addressness.
241 ///
242 /// This method returns a null pointer if the transformation cannot be
243 /// performed, otherwise it returns the new instruction.
244 ///
245 MachineInstr *
246 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
247                                     MachineBasicBlock::iterator &MBBI,
248                                     LiveVariables &LV) const {
249   MachineInstr *MI = MBBI;
250   // All instructions input are two-addr instructions.  Get the known operands.
251   unsigned Dest = MI->getOperand(0).getReg();
252   unsigned Src = MI->getOperand(1).getReg();
253
254   MachineInstr *NewMI = NULL;
255   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
256   // we have better subtarget support, enable the 16-bit LEA generation here.
257   bool DisableLEA16 = true;
258
259   unsigned MIOpc = MI->getOpcode();
260   switch (MIOpc) {
261   case X86::SHUFPSrri: {
262     assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
263     if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
264     
265     unsigned A = MI->getOperand(0).getReg();
266     unsigned B = MI->getOperand(1).getReg();
267     unsigned C = MI->getOperand(2).getReg();
268     unsigned M = MI->getOperand(3).getImm();
269     if (B != C) return 0;
270     NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
271     break;
272   }
273   case X86::SHL64ri: {
274     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
275     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
276     // the flags produced by a shift yet, so this is safe.
277     unsigned Dest = MI->getOperand(0).getReg();
278     unsigned Src = MI->getOperand(1).getReg();
279     unsigned ShAmt = MI->getOperand(2).getImm();
280     if (ShAmt == 0 || ShAmt >= 4) return 0;
281     
282     NewMI = BuildMI(get(X86::LEA64r), Dest)
283       .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
284     break;
285   }
286   case X86::SHL32ri: {
287     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
288     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
289     // the flags produced by a shift yet, so this is safe.
290     unsigned Dest = MI->getOperand(0).getReg();
291     unsigned Src = MI->getOperand(1).getReg();
292     unsigned ShAmt = MI->getOperand(2).getImm();
293     if (ShAmt == 0 || ShAmt >= 4) return 0;
294     
295     unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
296       X86::LEA64_32r : X86::LEA32r;
297     NewMI = BuildMI(get(Opc), Dest)
298       .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
299     break;
300   }
301   case X86::SHL16ri: {
302     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
303     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
304     // the flags produced by a shift yet, so this is safe.
305     unsigned Dest = MI->getOperand(0).getReg();
306     unsigned Src = MI->getOperand(1).getReg();
307     unsigned ShAmt = MI->getOperand(2).getImm();
308     if (ShAmt == 0 || ShAmt >= 4) return 0;
309     
310     if (DisableLEA16) {
311       // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
312       MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
313       unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
314         ? X86::LEA64_32r : X86::LEA32r;
315       unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
316       unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
317             
318       MachineInstr *Ins =
319         BuildMI(get(X86::INSERT_SUBREG), leaInReg).addReg(Src).addImm(2);
320       Ins->copyKillDeadInfo(MI);
321       
322       NewMI = BuildMI(get(Opc), leaOutReg)
323         .addReg(0).addImm(1 << ShAmt).addReg(leaInReg).addImm(0);
324       
325       MachineInstr *Ext =
326         BuildMI(get(X86::EXTRACT_SUBREG), Dest).addReg(leaOutReg).addImm(2);
327       Ext->copyKillDeadInfo(MI);
328       
329       MFI->insert(MBBI, Ins);            // Insert the insert_subreg
330       LV.instructionChanged(MI, NewMI);  // Update live variables
331       LV.addVirtualRegisterKilled(leaInReg, NewMI);
332       MFI->insert(MBBI, NewMI);          // Insert the new inst
333       LV.addVirtualRegisterKilled(leaOutReg, Ext);
334       MFI->insert(MBBI, Ext);            // Insert the extract_subreg      
335       return Ext;
336     } else {
337       NewMI = BuildMI(get(X86::LEA16r), Dest)
338         .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
339     }
340     break;
341   }
342   default: {
343     // The following opcodes also sets the condition code register(s). Only
344     // convert them to equivalent lea if the condition code register def's
345     // are dead!
346     if (hasLiveCondCodeDef(MI))
347       return 0;
348
349     bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
350     switch (MIOpc) {
351     default: return 0;
352     case X86::INC64r:
353     case X86::INC32r: {
354       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
355       unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
356         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
357       NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, 1);
358       break;
359     }
360     case X86::INC16r:
361     case X86::INC64_16r:
362       if (DisableLEA16) return 0;
363       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
364       NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
365       break;
366     case X86::DEC64r:
367     case X86::DEC32r: {
368       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
369       unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
370         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
371       NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src, -1);
372       break;
373     }
374     case X86::DEC16r:
375     case X86::DEC64_16r:
376       if (DisableLEA16) return 0;
377       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
378       NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
379       break;
380     case X86::ADD64rr:
381     case X86::ADD32rr: {
382       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
383       unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
384         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
385       NewMI = addRegReg(BuildMI(get(Opc), Dest), Src,
386                         MI->getOperand(2).getReg());
387       break;
388     }
389     case X86::ADD16rr:
390       if (DisableLEA16) return 0;
391       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
392       NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
393                         MI->getOperand(2).getReg());
394       break;
395     case X86::ADD64ri32:
396     case X86::ADD64ri8:
397       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
398       if (MI->getOperand(2).isImmediate())
399         NewMI = addRegOffset(BuildMI(get(X86::LEA64r), Dest), Src,
400                              MI->getOperand(2).getImm());
401       break;
402     case X86::ADD32ri:
403     case X86::ADD32ri8:
404       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
405       if (MI->getOperand(2).isImmediate()) {
406         unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
407         NewMI = addRegOffset(BuildMI(get(Opc), Dest), Src,
408                              MI->getOperand(2).getImm());
409       }
410       break;
411     case X86::ADD16ri:
412     case X86::ADD16ri8:
413       if (DisableLEA16) return 0;
414       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
415       if (MI->getOperand(2).isImmediate())
416         NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
417                              MI->getOperand(2).getImm());
418       break;
419     case X86::SHL16ri:
420       if (DisableLEA16) return 0;
421     case X86::SHL32ri:
422     case X86::SHL64ri: {
423       assert(MI->getNumOperands() >= 3 && MI->getOperand(2).isImmediate() &&
424              "Unknown shl instruction!");
425       unsigned ShAmt = MI->getOperand(2).getImm();
426       if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
427         X86AddressMode AM;
428         AM.Scale = 1 << ShAmt;
429         AM.IndexReg = Src;
430         unsigned Opc = MIOpc == X86::SHL64ri ? X86::LEA64r
431           : (MIOpc == X86::SHL32ri
432              ? (is64Bit ? X86::LEA64_32r : X86::LEA32r) : X86::LEA16r);
433         NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
434       }
435       break;
436     }
437     }
438   }
439   }
440
441   NewMI->copyKillDeadInfo(MI);
442   LV.instructionChanged(MI, NewMI);  // Update live variables
443   MFI->insert(MBBI, NewMI);          // Insert the new inst    
444   return NewMI;
445 }
446
447 /// commuteInstruction - We have a few instructions that must be hacked on to
448 /// commute them.
449 ///
450 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
451   switch (MI->getOpcode()) {
452   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
453   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
454   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
455   case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
456   case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
457   case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
458     unsigned Opc;
459     unsigned Size;
460     switch (MI->getOpcode()) {
461     default: assert(0 && "Unreachable!");
462     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
463     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
464     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
465     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
466     case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
467     case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
468     }
469     unsigned Amt = MI->getOperand(3).getImm();
470     unsigned A = MI->getOperand(0).getReg();
471     unsigned B = MI->getOperand(1).getReg();
472     unsigned C = MI->getOperand(2).getReg();
473     bool BisKill = MI->getOperand(1).isKill();
474     bool CisKill = MI->getOperand(2).isKill();
475     return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
476       .addReg(B, false, false, BisKill).addImm(Size-Amt);
477   }
478   case X86::CMOVB16rr:
479   case X86::CMOVB32rr:
480   case X86::CMOVB64rr:
481   case X86::CMOVAE16rr:
482   case X86::CMOVAE32rr:
483   case X86::CMOVAE64rr:
484   case X86::CMOVE16rr:
485   case X86::CMOVE32rr:
486   case X86::CMOVE64rr:
487   case X86::CMOVNE16rr:
488   case X86::CMOVNE32rr:
489   case X86::CMOVNE64rr:
490   case X86::CMOVBE16rr:
491   case X86::CMOVBE32rr:
492   case X86::CMOVBE64rr:
493   case X86::CMOVA16rr:
494   case X86::CMOVA32rr:
495   case X86::CMOVA64rr:
496   case X86::CMOVL16rr:
497   case X86::CMOVL32rr:
498   case X86::CMOVL64rr:
499   case X86::CMOVGE16rr:
500   case X86::CMOVGE32rr:
501   case X86::CMOVGE64rr:
502   case X86::CMOVLE16rr:
503   case X86::CMOVLE32rr:
504   case X86::CMOVLE64rr:
505   case X86::CMOVG16rr:
506   case X86::CMOVG32rr:
507   case X86::CMOVG64rr:
508   case X86::CMOVS16rr:
509   case X86::CMOVS32rr:
510   case X86::CMOVS64rr:
511   case X86::CMOVNS16rr:
512   case X86::CMOVNS32rr:
513   case X86::CMOVNS64rr:
514   case X86::CMOVP16rr:
515   case X86::CMOVP32rr:
516   case X86::CMOVP64rr:
517   case X86::CMOVNP16rr:
518   case X86::CMOVNP32rr:
519   case X86::CMOVNP64rr: {
520     unsigned Opc = 0;
521     switch (MI->getOpcode()) {
522     default: break;
523     case X86::CMOVB16rr:  Opc = X86::CMOVAE16rr; break;
524     case X86::CMOVB32rr:  Opc = X86::CMOVAE32rr; break;
525     case X86::CMOVB64rr:  Opc = X86::CMOVAE64rr; break;
526     case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
527     case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
528     case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
529     case X86::CMOVE16rr:  Opc = X86::CMOVNE16rr; break;
530     case X86::CMOVE32rr:  Opc = X86::CMOVNE32rr; break;
531     case X86::CMOVE64rr:  Opc = X86::CMOVNE64rr; break;
532     case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
533     case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
534     case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
535     case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
536     case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
537     case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
538     case X86::CMOVA16rr:  Opc = X86::CMOVBE16rr; break;
539     case X86::CMOVA32rr:  Opc = X86::CMOVBE32rr; break;
540     case X86::CMOVA64rr:  Opc = X86::CMOVBE64rr; break;
541     case X86::CMOVL16rr:  Opc = X86::CMOVGE16rr; break;
542     case X86::CMOVL32rr:  Opc = X86::CMOVGE32rr; break;
543     case X86::CMOVL64rr:  Opc = X86::CMOVGE64rr; break;
544     case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
545     case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
546     case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
547     case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
548     case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
549     case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
550     case X86::CMOVG16rr:  Opc = X86::CMOVLE16rr; break;
551     case X86::CMOVG32rr:  Opc = X86::CMOVLE32rr; break;
552     case X86::CMOVG64rr:  Opc = X86::CMOVLE64rr; break;
553     case X86::CMOVS16rr:  Opc = X86::CMOVNS16rr; break;
554     case X86::CMOVS32rr:  Opc = X86::CMOVNS32rr; break;
555     case X86::CMOVS64rr:  Opc = X86::CMOVNS32rr; break;
556     case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
557     case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
558     case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
559     case X86::CMOVP16rr:  Opc = X86::CMOVNP16rr; break;
560     case X86::CMOVP32rr:  Opc = X86::CMOVNP32rr; break;
561     case X86::CMOVP64rr:  Opc = X86::CMOVNP32rr; break;
562     case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
563     case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
564     case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
565     }
566
567     MI->setInstrDescriptor(get(Opc));
568     // Fallthrough intended.
569   }
570   default:
571     return TargetInstrInfoImpl::commuteInstruction(MI);
572   }
573 }
574
575 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
576   switch (BrOpc) {
577   default: return X86::COND_INVALID;
578   case X86::JE:  return X86::COND_E;
579   case X86::JNE: return X86::COND_NE;
580   case X86::JL:  return X86::COND_L;
581   case X86::JLE: return X86::COND_LE;
582   case X86::JG:  return X86::COND_G;
583   case X86::JGE: return X86::COND_GE;
584   case X86::JB:  return X86::COND_B;
585   case X86::JBE: return X86::COND_BE;
586   case X86::JA:  return X86::COND_A;
587   case X86::JAE: return X86::COND_AE;
588   case X86::JS:  return X86::COND_S;
589   case X86::JNS: return X86::COND_NS;
590   case X86::JP:  return X86::COND_P;
591   case X86::JNP: return X86::COND_NP;
592   case X86::JO:  return X86::COND_O;
593   case X86::JNO: return X86::COND_NO;
594   }
595 }
596
597 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
598   switch (CC) {
599   default: assert(0 && "Illegal condition code!");
600   case X86::COND_E:  return X86::JE;
601   case X86::COND_NE: return X86::JNE;
602   case X86::COND_L:  return X86::JL;
603   case X86::COND_LE: return X86::JLE;
604   case X86::COND_G:  return X86::JG;
605   case X86::COND_GE: return X86::JGE;
606   case X86::COND_B:  return X86::JB;
607   case X86::COND_BE: return X86::JBE;
608   case X86::COND_A:  return X86::JA;
609   case X86::COND_AE: return X86::JAE;
610   case X86::COND_S:  return X86::JS;
611   case X86::COND_NS: return X86::JNS;
612   case X86::COND_P:  return X86::JP;
613   case X86::COND_NP: return X86::JNP;
614   case X86::COND_O:  return X86::JO;
615   case X86::COND_NO: return X86::JNO;
616   }
617 }
618
619 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
620 /// e.g. turning COND_E to COND_NE.
621 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
622   switch (CC) {
623   default: assert(0 && "Illegal condition code!");
624   case X86::COND_E:  return X86::COND_NE;
625   case X86::COND_NE: return X86::COND_E;
626   case X86::COND_L:  return X86::COND_GE;
627   case X86::COND_LE: return X86::COND_G;
628   case X86::COND_G:  return X86::COND_LE;
629   case X86::COND_GE: return X86::COND_L;
630   case X86::COND_B:  return X86::COND_AE;
631   case X86::COND_BE: return X86::COND_A;
632   case X86::COND_A:  return X86::COND_BE;
633   case X86::COND_AE: return X86::COND_B;
634   case X86::COND_S:  return X86::COND_NS;
635   case X86::COND_NS: return X86::COND_S;
636   case X86::COND_P:  return X86::COND_NP;
637   case X86::COND_NP: return X86::COND_P;
638   case X86::COND_O:  return X86::COND_NO;
639   case X86::COND_NO: return X86::COND_O;
640   }
641 }
642
643 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
644   const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
645   if (TID->Flags & M_TERMINATOR_FLAG) {
646     // Conditional branch is a special case.
647     if ((TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0)
648       return true;
649     if ((TID->Flags & M_PREDICABLE) == 0)
650       return true;
651     return !isPredicated(MI);
652   }
653   return false;
654 }
655
656 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
657 static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
658                                                const X86InstrInfo &TII) {
659   if (MI->getOpcode() == X86::FP_REG_KILL)
660     return false;
661   return TII.isUnpredicatedTerminator(MI);
662 }
663
664 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
665                                  MachineBasicBlock *&TBB,
666                                  MachineBasicBlock *&FBB,
667                                  std::vector<MachineOperand> &Cond) const {
668   // If the block has no terminators, it just falls into the block after it.
669   MachineBasicBlock::iterator I = MBB.end();
670   if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this))
671     return false;
672
673   // Get the last instruction in the block.
674   MachineInstr *LastInst = I;
675   
676   // If there is only one terminator instruction, process it.
677   if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this)) {
678     if (!isBranch(LastInst->getOpcode()))
679       return true;
680     
681     // If the block ends with a branch there are 3 possibilities:
682     // it's an unconditional, conditional, or indirect branch.
683     
684     if (LastInst->getOpcode() == X86::JMP) {
685       TBB = LastInst->getOperand(0).getMBB();
686       return false;
687     }
688     X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
689     if (BranchCode == X86::COND_INVALID)
690       return true;  // Can't handle indirect branch.
691
692     // Otherwise, block ends with fall-through condbranch.
693     TBB = LastInst->getOperand(0).getMBB();
694     Cond.push_back(MachineOperand::CreateImm(BranchCode));
695     return false;
696   }
697   
698   // Get the instruction before it if it's a terminator.
699   MachineInstr *SecondLastInst = I;
700   
701   // If there are three terminators, we don't know what sort of block this is.
702   if (SecondLastInst && I != MBB.begin() &&
703       isBrAnalysisUnpredicatedTerminator(--I, *this))
704     return true;
705
706   // If the block ends with X86::JMP and a conditional branch, handle it.
707   X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
708   if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
709     TBB = SecondLastInst->getOperand(0).getMBB();
710     Cond.push_back(MachineOperand::CreateImm(BranchCode));
711     FBB = LastInst->getOperand(0).getMBB();
712     return false;
713   }
714
715   // If the block ends with two X86::JMPs, handle it.  The second one is not
716   // executed, so remove it.
717   if (SecondLastInst->getOpcode() == X86::JMP && 
718       LastInst->getOpcode() == X86::JMP) {
719     TBB = SecondLastInst->getOperand(0).getMBB();
720     I = LastInst;
721     I->eraseFromParent();
722     return false;
723   }
724
725   // Otherwise, can't handle this.
726   return true;
727 }
728
729 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
730   MachineBasicBlock::iterator I = MBB.end();
731   if (I == MBB.begin()) return 0;
732   --I;
733   if (I->getOpcode() != X86::JMP && 
734       GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
735     return 0;
736   
737   // Remove the branch.
738   I->eraseFromParent();
739   
740   I = MBB.end();
741   
742   if (I == MBB.begin()) return 1;
743   --I;
744   if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
745     return 1;
746   
747   // Remove the branch.
748   I->eraseFromParent();
749   return 2;
750 }
751
752 static const MachineInstrBuilder &X86InstrAddOperand(MachineInstrBuilder &MIB,
753                                                      MachineOperand &MO) {
754   if (MO.isRegister())
755     MIB = MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit(),
756                      false, false, MO.getSubReg());
757   else if (MO.isImmediate())
758     MIB = MIB.addImm(MO.getImm());
759   else if (MO.isFrameIndex())
760     MIB = MIB.addFrameIndex(MO.getIndex());
761   else if (MO.isGlobalAddress())
762     MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset());
763   else if (MO.isConstantPoolIndex())
764     MIB = MIB.addConstantPoolIndex(MO.getIndex(), MO.getOffset());
765   else if (MO.isJumpTableIndex())
766     MIB = MIB.addJumpTableIndex(MO.getIndex());
767   else if (MO.isExternalSymbol())
768     MIB = MIB.addExternalSymbol(MO.getSymbolName());
769   else
770     assert(0 && "Unknown operand for X86InstrAddOperand!");
771
772   return MIB;
773 }
774
775 unsigned
776 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
777                            MachineBasicBlock *FBB,
778                            const std::vector<MachineOperand> &Cond) const {
779   // Shouldn't be a fall through.
780   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
781   assert((Cond.size() == 1 || Cond.size() == 0) &&
782          "X86 branch conditions have one component!");
783
784   if (FBB == 0) { // One way branch.
785     if (Cond.empty()) {
786       // Unconditional branch?
787       BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
788     } else {
789       // Conditional branch.
790       unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
791       BuildMI(&MBB, get(Opc)).addMBB(TBB);
792     }
793     return 1;
794   }
795   
796   // Two-way Conditional branch.
797   unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
798   BuildMI(&MBB, get(Opc)).addMBB(TBB);
799   BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
800   return 2;
801 }
802
803 void X86InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
804                                    MachineBasicBlock::iterator MI,
805                                    unsigned DestReg, unsigned SrcReg,
806                                    const TargetRegisterClass *DestRC,
807                                    const TargetRegisterClass *SrcRC) const {
808   if (DestRC != SrcRC) {
809     // Moving EFLAGS to / from another register requires a push and a pop.
810     if (SrcRC == &X86::CCRRegClass) {
811       assert(SrcReg == X86::EFLAGS);
812       if (DestRC == &X86::GR64RegClass) {
813         BuildMI(MBB, MI, get(X86::PUSHFQ));
814         BuildMI(MBB, MI, get(X86::POP64r), DestReg);
815         return;
816       } else if (DestRC == &X86::GR32RegClass) {
817         BuildMI(MBB, MI, get(X86::PUSHFD));
818         BuildMI(MBB, MI, get(X86::POP32r), DestReg);
819         return;
820       }
821     } else if (DestRC == &X86::CCRRegClass) {
822       assert(DestReg == X86::EFLAGS);
823       if (SrcRC == &X86::GR64RegClass) {
824         BuildMI(MBB, MI, get(X86::PUSH64r)).addReg(SrcReg);
825         BuildMI(MBB, MI, get(X86::POPFQ));
826         return;
827       } else if (SrcRC == &X86::GR32RegClass) {
828         BuildMI(MBB, MI, get(X86::PUSH32r)).addReg(SrcReg);
829         BuildMI(MBB, MI, get(X86::POPFD));
830         return;
831       }
832     }
833     cerr << "Not yet supported!";
834     abort();
835   }
836
837   unsigned Opc;
838   if (DestRC == &X86::GR64RegClass) {
839     Opc = X86::MOV64rr;
840   } else if (DestRC == &X86::GR32RegClass) {
841     Opc = X86::MOV32rr;
842   } else if (DestRC == &X86::GR16RegClass) {
843     Opc = X86::MOV16rr;
844   } else if (DestRC == &X86::GR8RegClass) {
845     Opc = X86::MOV8rr;
846   } else if (DestRC == &X86::GR32_RegClass) {
847     Opc = X86::MOV32_rr;
848   } else if (DestRC == &X86::GR16_RegClass) {
849     Opc = X86::MOV16_rr;
850   } else if (DestRC == &X86::RFP32RegClass) {
851     Opc = X86::MOV_Fp3232;
852   } else if (DestRC == &X86::RFP64RegClass || DestRC == &X86::RSTRegClass) {
853     Opc = X86::MOV_Fp6464;
854   } else if (DestRC == &X86::RFP80RegClass) {
855     Opc = X86::MOV_Fp8080;
856   } else if (DestRC == &X86::FR32RegClass) {
857     Opc = X86::FsMOVAPSrr;
858   } else if (DestRC == &X86::FR64RegClass) {
859     Opc = X86::FsMOVAPDrr;
860   } else if (DestRC == &X86::VR128RegClass) {
861     Opc = X86::MOVAPSrr;
862   } else if (DestRC == &X86::VR64RegClass) {
863     Opc = X86::MMX_MOVQ64rr;
864   } else {
865     assert(0 && "Unknown regclass");
866     abort();
867   }
868   BuildMI(MBB, MI, get(Opc), DestReg).addReg(SrcReg);
869 }
870
871 static unsigned getStoreRegOpcode(const TargetRegisterClass *RC,
872                                   unsigned StackAlign) {
873   unsigned Opc = 0;
874   if (RC == &X86::GR64RegClass) {
875     Opc = X86::MOV64mr;
876   } else if (RC == &X86::GR32RegClass) {
877     Opc = X86::MOV32mr;
878   } else if (RC == &X86::GR16RegClass) {
879     Opc = X86::MOV16mr;
880   } else if (RC == &X86::GR8RegClass) {
881     Opc = X86::MOV8mr;
882   } else if (RC == &X86::GR32_RegClass) {
883     Opc = X86::MOV32_mr;
884   } else if (RC == &X86::GR16_RegClass) {
885     Opc = X86::MOV16_mr;
886   } else if (RC == &X86::RFP80RegClass) {
887     Opc = X86::ST_FpP80m;   // pops
888   } else if (RC == &X86::RFP64RegClass) {
889     Opc = X86::ST_Fp64m;
890   } else if (RC == &X86::RFP32RegClass) {
891     Opc = X86::ST_Fp32m;
892   } else if (RC == &X86::FR32RegClass) {
893     Opc = X86::MOVSSmr;
894   } else if (RC == &X86::FR64RegClass) {
895     Opc = X86::MOVSDmr;
896   } else if (RC == &X86::VR128RegClass) {
897     // FIXME: Use movaps once we are capable of selectively
898     // aligning functions that spill SSE registers on 16-byte boundaries.
899     Opc = StackAlign >= 16 ? X86::MOVAPSmr : X86::MOVUPSmr;
900   } else if (RC == &X86::VR64RegClass) {
901     Opc = X86::MMX_MOVQ64mr;
902   } else {
903     assert(0 && "Unknown regclass");
904     abort();
905   }
906
907   return Opc;
908 }
909
910 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
911                                        MachineBasicBlock::iterator MI,
912                                        unsigned SrcReg, bool isKill, int FrameIdx,
913                                        const TargetRegisterClass *RC) const {
914   unsigned Opc = getStoreRegOpcode(RC, RI.getStackAlignment());
915   addFrameReference(BuildMI(MBB, MI, get(Opc)), FrameIdx)
916     .addReg(SrcReg, false, false, isKill);
917 }
918
919 void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
920                                   bool isKill,
921                                   SmallVectorImpl<MachineOperand> &Addr,
922                                   const TargetRegisterClass *RC,
923                                   SmallVectorImpl<MachineInstr*> &NewMIs) const {
924   unsigned Opc = getStoreRegOpcode(RC, RI.getStackAlignment());
925   MachineInstrBuilder MIB = BuildMI(get(Opc));
926   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
927     MIB = X86InstrAddOperand(MIB, Addr[i]);
928   MIB.addReg(SrcReg, false, false, isKill);
929   NewMIs.push_back(MIB);
930 }
931
932 static unsigned getLoadRegOpcode(const TargetRegisterClass *RC,
933                                  unsigned StackAlign) {
934   unsigned Opc = 0;
935   if (RC == &X86::GR64RegClass) {
936     Opc = X86::MOV64rm;
937   } else if (RC == &X86::GR32RegClass) {
938     Opc = X86::MOV32rm;
939   } else if (RC == &X86::GR16RegClass) {
940     Opc = X86::MOV16rm;
941   } else if (RC == &X86::GR8RegClass) {
942     Opc = X86::MOV8rm;
943   } else if (RC == &X86::GR32_RegClass) {
944     Opc = X86::MOV32_rm;
945   } else if (RC == &X86::GR16_RegClass) {
946     Opc = X86::MOV16_rm;
947   } else if (RC == &X86::RFP80RegClass) {
948     Opc = X86::LD_Fp80m;
949   } else if (RC == &X86::RFP64RegClass) {
950     Opc = X86::LD_Fp64m;
951   } else if (RC == &X86::RFP32RegClass) {
952     Opc = X86::LD_Fp32m;
953   } else if (RC == &X86::FR32RegClass) {
954     Opc = X86::MOVSSrm;
955   } else if (RC == &X86::FR64RegClass) {
956     Opc = X86::MOVSDrm;
957   } else if (RC == &X86::VR128RegClass) {
958     // FIXME: Use movaps once we are capable of selectively
959     // aligning functions that spill SSE registers on 16-byte boundaries.
960     Opc = StackAlign >= 16 ? X86::MOVAPSrm : X86::MOVUPSrm;
961   } else if (RC == &X86::VR64RegClass) {
962     Opc = X86::MMX_MOVQ64rm;
963   } else {
964     assert(0 && "Unknown regclass");
965     abort();
966   }
967
968   return Opc;
969 }
970
971 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
972                                            MachineBasicBlock::iterator MI,
973                                            unsigned DestReg, int FrameIdx,
974                                            const TargetRegisterClass *RC) const{
975   unsigned Opc = getLoadRegOpcode(RC, RI.getStackAlignment());
976   addFrameReference(BuildMI(MBB, MI, get(Opc), DestReg), FrameIdx);
977 }
978
979 void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
980                                       SmallVectorImpl<MachineOperand> &Addr,
981                                       const TargetRegisterClass *RC,
982                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
983   unsigned Opc = getLoadRegOpcode(RC, RI.getStackAlignment());
984   MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
985   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
986     MIB = X86InstrAddOperand(MIB, Addr[i]);
987   NewMIs.push_back(MIB);
988 }
989
990 bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
991                                                 MachineBasicBlock::iterator MI,
992                                 const std::vector<CalleeSavedInfo> &CSI) const {
993   if (CSI.empty())
994     return false;
995
996   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
997   unsigned SlotSize = is64Bit ? 8 : 4;
998
999   MachineFunction &MF = *MBB.getParent();
1000   X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1001   X86FI->setCalleeSavedFrameSize(CSI.size() * SlotSize);
1002   
1003   unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
1004   for (unsigned i = CSI.size(); i != 0; --i) {
1005     unsigned Reg = CSI[i-1].getReg();
1006     // Add the callee-saved register as live-in. It's killed at the spill.
1007     MBB.addLiveIn(Reg);
1008     BuildMI(MBB, MI, get(Opc)).addReg(Reg);
1009   }
1010   return true;
1011 }
1012
1013 bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1014                                                  MachineBasicBlock::iterator MI,
1015                                 const std::vector<CalleeSavedInfo> &CSI) const {
1016   if (CSI.empty())
1017     return false;
1018     
1019   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
1020
1021   unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
1022   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1023     unsigned Reg = CSI[i].getReg();
1024     BuildMI(MBB, MI, get(Opc), Reg);
1025   }
1026   return true;
1027 }
1028
1029 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
1030   if (MBB.empty()) return false;
1031   
1032   switch (MBB.back().getOpcode()) {
1033   case X86::TCRETURNri:
1034   case X86::TCRETURNdi:
1035   case X86::RET:     // Return.
1036   case X86::RETI:
1037   case X86::TAILJMPd:
1038   case X86::TAILJMPr:
1039   case X86::TAILJMPm:
1040   case X86::JMP:     // Uncond branch.
1041   case X86::JMP32r:  // Indirect branch.
1042   case X86::JMP64r:  // Indirect branch (64-bit).
1043   case X86::JMP32m:  // Indirect branch through mem.
1044   case X86::JMP64m:  // Indirect branch through mem (64-bit).
1045     return true;
1046   default: return false;
1047   }
1048 }
1049
1050 bool X86InstrInfo::
1051 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
1052   assert(Cond.size() == 1 && "Invalid X86 branch condition!");
1053   Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
1054   return false;
1055 }
1056
1057 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
1058   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
1059   if (Subtarget->is64Bit())
1060     return &X86::GR64RegClass;
1061   else
1062     return &X86::GR32RegClass;
1063 }