1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the X86 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "X86InstrInfo.h"
16 #include "X86GenInstrInfo.inc"
17 #include "X86InstrBuilder.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/LiveVariables.h"
23 #include "llvm/CodeGen/SSARegMap.h"
24 #include "llvm/Target/TargetOptions.h"
27 X86InstrInfo::X86InstrInfo(X86TargetMachine
&tm
)
28 : TargetInstrInfo(X86Insts
, array_lengthof(X86Insts
)),
29 TM(tm
), RI(tm
, *this) {
32 bool X86InstrInfo::isMoveInstr(const MachineInstr
& MI
,
34 unsigned& destReg
) const {
35 MachineOpCode oc
= MI
.getOpcode();
36 if (oc
== X86::MOV8rr
|| oc
== X86::MOV16rr
||
37 oc
== X86::MOV32rr
|| oc
== X86::MOV64rr
||
38 oc
== X86::MOV16to16_
|| oc
== X86::MOV32to32_
||
39 oc
== X86::MOV_Fp3232
|| oc
== X86::MOVSSrr
|| oc
== X86::MOVSDrr
||
40 oc
== X86::MOV_Fp3264
|| oc
== X86::MOV_Fp6432
|| oc
== X86::MOV_Fp6464
||
41 oc
== X86::FsMOVAPSrr
|| oc
== X86::FsMOVAPDrr
||
42 oc
== X86::MOVAPSrr
|| oc
== X86::MOVAPDrr
||
43 oc
== X86::MOVSS2PSrr
|| oc
== X86::MOVSD2PDrr
||
44 oc
== X86::MOVPS2SSrr
|| oc
== X86::MOVPD2SDrr
||
45 oc
== X86::MMX_MOVD64rr
|| oc
== X86::MMX_MOVQ64rr
) {
46 assert(MI
.getNumOperands() >= 2 &&
47 MI
.getOperand(0).isRegister() &&
48 MI
.getOperand(1).isRegister() &&
49 "invalid register-register move instruction");
50 sourceReg
= MI
.getOperand(1).getReg();
51 destReg
= MI
.getOperand(0).getReg();
57 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr
*MI
,
58 int &FrameIndex
) const {
59 switch (MI
->getOpcode()) {
72 case X86::MMX_MOVD64rm
:
73 case X86::MMX_MOVQ64rm
:
74 if (MI
->getOperand(1).isFrameIndex() && MI
->getOperand(2).isImmediate() &&
75 MI
->getOperand(3).isRegister() && MI
->getOperand(4).isImmediate() &&
76 MI
->getOperand(2).getImmedValue() == 1 &&
77 MI
->getOperand(3).getReg() == 0 &&
78 MI
->getOperand(4).getImmedValue() == 0) {
79 FrameIndex
= MI
->getOperand(1).getFrameIndex();
80 return MI
->getOperand(0).getReg();
87 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr
*MI
,
88 int &FrameIndex
) const {
89 switch (MI
->getOpcode()) {
102 case X86::MMX_MOVD64mr
:
103 case X86::MMX_MOVQ64mr
:
104 case X86::MMX_MOVNTQmr
:
105 if (MI
->getOperand(0).isFrameIndex() && MI
->getOperand(1).isImmediate() &&
106 MI
->getOperand(2).isRegister() && MI
->getOperand(3).isImmediate() &&
107 MI
->getOperand(1).getImmedValue() == 1 &&
108 MI
->getOperand(2).getReg() == 0 &&
109 MI
->getOperand(3).getImmedValue() == 0) {
110 FrameIndex
= MI
->getOperand(0).getFrameIndex();
111 return MI
->getOperand(4).getReg();
119 bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr
*MI
) const {
120 switch (MI
->getOpcode()) {
133 case X86::MMX_MOVD64rm
:
134 case X86::MMX_MOVQ64rm
:
135 // Loads from constant pools are trivially rematerializable.
136 return MI
->getOperand(1).isRegister() && MI
->getOperand(2).isImmediate() &&
137 MI
->getOperand(3).isRegister() && MI
->getOperand(4).isConstantPoolIndex() &&
138 MI
->getOperand(1).getReg() == 0 &&
139 MI
->getOperand(2).getImmedValue() == 1 &&
140 MI
->getOperand(3).getReg() == 0;
142 // All other instructions marked M_REMATERIALIZABLE are always trivially
147 /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
148 /// is not marked dead.
149 static bool hasLiveCondCodeDef(MachineInstr
*MI
) {
150 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
151 MachineOperand
&MO
= MI
->getOperand(i
);
152 if (MO
.isRegister() && MO
.isDef() &&
153 MO
.getReg() == X86::EFLAGS
&& !MO
.isDead()) {
160 /// convertToThreeAddress - This method must be implemented by targets that
161 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
162 /// may be able to convert a two-address instruction into a true
163 /// three-address instruction on demand. This allows the X86 target (for
164 /// example) to convert ADD and SHL instructions into LEA instructions if they
165 /// would require register copies due to two-addressness.
167 /// This method returns a null pointer if the transformation cannot be
168 /// performed, otherwise it returns the new instruction.
171 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator
&MFI
,
172 MachineBasicBlock::iterator
&MBBI
,
173 LiveVariables
&LV
) const {
174 MachineInstr
*MI
= MBBI
;
175 // All instructions input are two-addr instructions. Get the known operands.
176 unsigned Dest
= MI
->getOperand(0).getReg();
177 unsigned Src
= MI
->getOperand(1).getReg();
179 MachineInstr
*NewMI
= NULL
;
180 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
181 // we have better subtarget support, enable the 16-bit LEA generation here.
182 bool DisableLEA16
= true;
184 unsigned MIOpc
= MI
->getOpcode();
186 case X86::SHUFPSrri
: {
187 assert(MI
->getNumOperands() == 4 && "Unknown shufps instruction!");
188 if (!TM
.getSubtarget
<X86Subtarget
>().hasSSE2()) return 0;
190 unsigned A
= MI
->getOperand(0).getReg();
191 unsigned B
= MI
->getOperand(1).getReg();
192 unsigned C
= MI
->getOperand(2).getReg();
193 unsigned M
= MI
->getOperand(3).getImm();
194 if (B
!= C
) return 0;
195 NewMI
= BuildMI(get(X86::PSHUFDri
), A
).addReg(B
).addImm(M
);
199 assert(MI
->getNumOperands() >= 3 && "Unknown shift instruction!");
200 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
201 // the flags produced by a shift yet, so this is safe.
202 unsigned Dest
= MI
->getOperand(0).getReg();
203 unsigned Src
= MI
->getOperand(1).getReg();
204 unsigned ShAmt
= MI
->getOperand(2).getImm();
205 if (ShAmt
== 0 || ShAmt
>= 4) return 0;
207 NewMI
= BuildMI(get(X86::LEA64r
), Dest
)
208 .addReg(0).addImm(1 << ShAmt
).addReg(Src
).addImm(0);
212 assert(MI
->getNumOperands() >= 3 && "Unknown shift instruction!");
213 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
214 // the flags produced by a shift yet, so this is safe.
215 unsigned Dest
= MI
->getOperand(0).getReg();
216 unsigned Src
= MI
->getOperand(1).getReg();
217 unsigned ShAmt
= MI
->getOperand(2).getImm();
218 if (ShAmt
== 0 || ShAmt
>= 4) return 0;
220 unsigned Opc
= TM
.getSubtarget
<X86Subtarget
>().is64Bit() ?
221 X86::LEA64_32r
: X86::LEA32r
;
222 NewMI
= BuildMI(get(Opc
), Dest
)
223 .addReg(0).addImm(1 << ShAmt
).addReg(Src
).addImm(0);
227 assert(MI
->getNumOperands() >= 3 && "Unknown shift instruction!");
228 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
229 // the flags produced by a shift yet, so this is safe.
230 unsigned Dest
= MI
->getOperand(0).getReg();
231 unsigned Src
= MI
->getOperand(1).getReg();
232 unsigned ShAmt
= MI
->getOperand(2).getImm();
233 if (ShAmt
== 0 || ShAmt
>= 4) return 0;
236 // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
237 SSARegMap
*RegMap
= MFI
->getParent()->getSSARegMap();
238 unsigned Opc
= TM
.getSubtarget
<X86Subtarget
>().is64Bit()
239 ? X86::LEA64_32r
: X86::LEA32r
;
240 unsigned leaInReg
= RegMap
->createVirtualRegister(&X86::GR32RegClass
);
241 unsigned leaOutReg
= RegMap
->createVirtualRegister(&X86::GR32RegClass
);
244 BuildMI(get(X86::INSERT_SUBREG
), leaInReg
).addReg(Src
).addImm(2);
245 Ins
->copyKillDeadInfo(MI
);
247 NewMI
= BuildMI(get(Opc
), leaOutReg
)
248 .addReg(0).addImm(1 << ShAmt
).addReg(leaInReg
).addImm(0);
251 BuildMI(get(X86::EXTRACT_SUBREG
), Dest
).addReg(leaOutReg
).addImm(2);
252 Ext
->copyKillDeadInfo(MI
);
254 MFI
->insert(MBBI
, Ins
); // Insert the insert_subreg
255 LV
.instructionChanged(MI
, NewMI
); // Update live variables
256 LV
.addVirtualRegisterKilled(leaInReg
, NewMI
);
257 MFI
->insert(MBBI
, NewMI
); // Insert the new inst
258 LV
.addVirtualRegisterKilled(leaOutReg
, Ext
);
259 MFI
->insert(MBBI
, Ext
); // Insert the extract_subreg
262 NewMI
= BuildMI(get(X86::LEA16r
), Dest
)
263 .addReg(0).addImm(1 << ShAmt
).addReg(Src
).addImm(0);
268 // The following opcodes also sets the condition code register(s). Only
269 // convert them to equivalent lea if the condition code register def's
271 if (hasLiveCondCodeDef(MI
))
274 bool is64Bit
= TM
.getSubtarget
<X86Subtarget
>().is64Bit();
279 assert(MI
->getNumOperands() >= 2 && "Unknown inc instruction!");
280 unsigned Opc
= MIOpc
== X86::INC64r
? X86::LEA64r
281 : (is64Bit
? X86::LEA64_32r
: X86::LEA32r
);
282 NewMI
= addRegOffset(BuildMI(get(Opc
), Dest
), Src
, 1);
287 if (DisableLEA16
) return 0;
288 assert(MI
->getNumOperands() >= 2 && "Unknown inc instruction!");
289 NewMI
= addRegOffset(BuildMI(get(X86::LEA16r
), Dest
), Src
, 1);
293 assert(MI
->getNumOperands() >= 2 && "Unknown dec instruction!");
294 unsigned Opc
= MIOpc
== X86::DEC64r
? X86::LEA64r
295 : (is64Bit
? X86::LEA64_32r
: X86::LEA32r
);
296 NewMI
= addRegOffset(BuildMI(get(Opc
), Dest
), Src
, -1);
301 if (DisableLEA16
) return 0;
302 assert(MI
->getNumOperands() >= 2 && "Unknown dec instruction!");
303 NewMI
= addRegOffset(BuildMI(get(X86::LEA16r
), Dest
), Src
, -1);
307 assert(MI
->getNumOperands() >= 3 && "Unknown add instruction!");
308 unsigned Opc
= MIOpc
== X86::ADD64rr
? X86::LEA64r
309 : (is64Bit
? X86::LEA64_32r
: X86::LEA32r
);
310 NewMI
= addRegReg(BuildMI(get(Opc
), Dest
), Src
,
311 MI
->getOperand(2).getReg());
315 if (DisableLEA16
) return 0;
316 assert(MI
->getNumOperands() >= 3 && "Unknown add instruction!");
317 NewMI
= addRegReg(BuildMI(get(X86::LEA16r
), Dest
), Src
,
318 MI
->getOperand(2).getReg());
322 assert(MI
->getNumOperands() >= 3 && "Unknown add instruction!");
323 if (MI
->getOperand(2).isImmediate())
324 NewMI
= addRegOffset(BuildMI(get(X86::LEA64r
), Dest
), Src
,
325 MI
->getOperand(2).getImmedValue());
329 assert(MI
->getNumOperands() >= 3 && "Unknown add instruction!");
330 if (MI
->getOperand(2).isImmediate()) {
331 unsigned Opc
= is64Bit
? X86::LEA64_32r
: X86::LEA32r
;
332 NewMI
= addRegOffset(BuildMI(get(Opc
), Dest
), Src
,
333 MI
->getOperand(2).getImmedValue());
338 if (DisableLEA16
) return 0;
339 assert(MI
->getNumOperands() >= 3 && "Unknown add instruction!");
340 if (MI
->getOperand(2).isImmediate())
341 NewMI
= addRegOffset(BuildMI(get(X86::LEA16r
), Dest
), Src
,
342 MI
->getOperand(2).getImmedValue());
345 if (DisableLEA16
) return 0;
348 assert(MI
->getNumOperands() >= 3 && MI
->getOperand(2).isImmediate() &&
349 "Unknown shl instruction!");
350 unsigned ShAmt
= MI
->getOperand(2).getImmedValue();
351 if (ShAmt
== 1 || ShAmt
== 2 || ShAmt
== 3) {
353 AM
.Scale
= 1 << ShAmt
;
355 unsigned Opc
= MIOpc
== X86::SHL64ri
? X86::LEA64r
356 : (MIOpc
== X86::SHL32ri
357 ? (is64Bit
? X86::LEA64_32r
: X86::LEA32r
) : X86::LEA16r
);
358 NewMI
= addFullAddress(BuildMI(get(Opc
), Dest
), AM
);
366 NewMI
->copyKillDeadInfo(MI
);
367 LV
.instructionChanged(MI
, NewMI
); // Update live variables
368 MFI
->insert(MBBI
, NewMI
); // Insert the new inst
372 /// commuteInstruction - We have a few instructions that must be hacked on to
375 MachineInstr
*X86InstrInfo::commuteInstruction(MachineInstr
*MI
) const {
376 switch (MI
->getOpcode()) {
377 case X86::SHRD16rri8
: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
378 case X86::SHLD16rri8
: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
379 case X86::SHRD32rri8
: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
380 case X86::SHLD32rri8
: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
381 case X86::SHRD64rri8
: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
382 case X86::SHLD64rri8
:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
385 switch (MI
->getOpcode()) {
386 default: assert(0 && "Unreachable!");
387 case X86::SHRD16rri8
: Size
= 16; Opc
= X86::SHLD16rri8
; break;
388 case X86::SHLD16rri8
: Size
= 16; Opc
= X86::SHRD16rri8
; break;
389 case X86::SHRD32rri8
: Size
= 32; Opc
= X86::SHLD32rri8
; break;
390 case X86::SHLD32rri8
: Size
= 32; Opc
= X86::SHRD32rri8
; break;
391 case X86::SHRD64rri8
: Size
= 64; Opc
= X86::SHLD64rri8
; break;
392 case X86::SHLD64rri8
: Size
= 64; Opc
= X86::SHRD64rri8
; break;
394 unsigned Amt
= MI
->getOperand(3).getImmedValue();
395 unsigned A
= MI
->getOperand(0).getReg();
396 unsigned B
= MI
->getOperand(1).getReg();
397 unsigned C
= MI
->getOperand(2).getReg();
398 bool BisKill
= MI
->getOperand(1).isKill();
399 bool CisKill
= MI
->getOperand(2).isKill();
400 return BuildMI(get(Opc
), A
).addReg(C
, false, false, CisKill
)
401 .addReg(B
, false, false, BisKill
).addImm(Size
-Amt
);
406 case X86::CMOVAE16rr
:
407 case X86::CMOVAE32rr
:
408 case X86::CMOVAE64rr
:
412 case X86::CMOVNE16rr
:
413 case X86::CMOVNE32rr
:
414 case X86::CMOVNE64rr
:
415 case X86::CMOVBE16rr
:
416 case X86::CMOVBE32rr
:
417 case X86::CMOVBE64rr
:
424 case X86::CMOVGE16rr
:
425 case X86::CMOVGE32rr
:
426 case X86::CMOVGE64rr
:
427 case X86::CMOVLE16rr
:
428 case X86::CMOVLE32rr
:
429 case X86::CMOVLE64rr
:
436 case X86::CMOVNS16rr
:
437 case X86::CMOVNS32rr
:
438 case X86::CMOVNS64rr
:
442 case X86::CMOVNP16rr
:
443 case X86::CMOVNP32rr
:
444 case X86::CMOVNP64rr
: {
446 switch (MI
->getOpcode()) {
448 case X86::CMOVB16rr
: Opc
= X86::CMOVAE16rr
; break;
449 case X86::CMOVB32rr
: Opc
= X86::CMOVAE32rr
; break;
450 case X86::CMOVB64rr
: Opc
= X86::CMOVAE64rr
; break;
451 case X86::CMOVAE16rr
: Opc
= X86::CMOVB16rr
; break;
452 case X86::CMOVAE32rr
: Opc
= X86::CMOVB32rr
; break;
453 case X86::CMOVAE64rr
: Opc
= X86::CMOVB64rr
; break;
454 case X86::CMOVE16rr
: Opc
= X86::CMOVNE16rr
; break;
455 case X86::CMOVE32rr
: Opc
= X86::CMOVNE32rr
; break;
456 case X86::CMOVE64rr
: Opc
= X86::CMOVNE64rr
; break;
457 case X86::CMOVNE16rr
: Opc
= X86::CMOVE16rr
; break;
458 case X86::CMOVNE32rr
: Opc
= X86::CMOVE32rr
; break;
459 case X86::CMOVNE64rr
: Opc
= X86::CMOVE64rr
; break;
460 case X86::CMOVBE16rr
: Opc
= X86::CMOVA16rr
; break;
461 case X86::CMOVBE32rr
: Opc
= X86::CMOVA32rr
; break;
462 case X86::CMOVBE64rr
: Opc
= X86::CMOVA64rr
; break;
463 case X86::CMOVA16rr
: Opc
= X86::CMOVBE16rr
; break;
464 case X86::CMOVA32rr
: Opc
= X86::CMOVBE32rr
; break;
465 case X86::CMOVA64rr
: Opc
= X86::CMOVBE64rr
; break;
466 case X86::CMOVL16rr
: Opc
= X86::CMOVGE16rr
; break;
467 case X86::CMOVL32rr
: Opc
= X86::CMOVGE32rr
; break;
468 case X86::CMOVL64rr
: Opc
= X86::CMOVGE64rr
; break;
469 case X86::CMOVGE16rr
: Opc
= X86::CMOVL16rr
; break;
470 case X86::CMOVGE32rr
: Opc
= X86::CMOVL32rr
; break;
471 case X86::CMOVGE64rr
: Opc
= X86::CMOVL64rr
; break;
472 case X86::CMOVLE16rr
: Opc
= X86::CMOVG16rr
; break;
473 case X86::CMOVLE32rr
: Opc
= X86::CMOVG32rr
; break;
474 case X86::CMOVLE64rr
: Opc
= X86::CMOVG64rr
; break;
475 case X86::CMOVG16rr
: Opc
= X86::CMOVLE16rr
; break;
476 case X86::CMOVG32rr
: Opc
= X86::CMOVLE32rr
; break;
477 case X86::CMOVG64rr
: Opc
= X86::CMOVLE64rr
; break;
478 case X86::CMOVS16rr
: Opc
= X86::CMOVNS16rr
; break;
479 case X86::CMOVS32rr
: Opc
= X86::CMOVNS32rr
; break;
480 case X86::CMOVS64rr
: Opc
= X86::CMOVNS32rr
; break;
481 case X86::CMOVNS16rr
: Opc
= X86::CMOVS16rr
; break;
482 case X86::CMOVNS32rr
: Opc
= X86::CMOVS32rr
; break;
483 case X86::CMOVNS64rr
: Opc
= X86::CMOVS64rr
; break;
484 case X86::CMOVP16rr
: Opc
= X86::CMOVNP16rr
; break;
485 case X86::CMOVP32rr
: Opc
= X86::CMOVNP32rr
; break;
486 case X86::CMOVP64rr
: Opc
= X86::CMOVNP32rr
; break;
487 case X86::CMOVNP16rr
: Opc
= X86::CMOVP16rr
; break;
488 case X86::CMOVNP32rr
: Opc
= X86::CMOVP32rr
; break;
489 case X86::CMOVNP64rr
: Opc
= X86::CMOVP64rr
; break;
492 MI
->setInstrDescriptor(get(Opc
));
493 // Fallthrough intended.
496 return TargetInstrInfo::commuteInstruction(MI
);
500 static X86::CondCode
GetCondFromBranchOpc(unsigned BrOpc
) {
502 default: return X86::COND_INVALID
;
503 case X86::JE
: return X86::COND_E
;
504 case X86::JNE
: return X86::COND_NE
;
505 case X86::JL
: return X86::COND_L
;
506 case X86::JLE
: return X86::COND_LE
;
507 case X86::JG
: return X86::COND_G
;
508 case X86::JGE
: return X86::COND_GE
;
509 case X86::JB
: return X86::COND_B
;
510 case X86::JBE
: return X86::COND_BE
;
511 case X86::JA
: return X86::COND_A
;
512 case X86::JAE
: return X86::COND_AE
;
513 case X86::JS
: return X86::COND_S
;
514 case X86::JNS
: return X86::COND_NS
;
515 case X86::JP
: return X86::COND_P
;
516 case X86::JNP
: return X86::COND_NP
;
517 case X86::JO
: return X86::COND_O
;
518 case X86::JNO
: return X86::COND_NO
;
522 unsigned X86::GetCondBranchFromCond(X86::CondCode CC
) {
524 default: assert(0 && "Illegal condition code!");
525 case X86::COND_E
: return X86::JE
;
526 case X86::COND_NE
: return X86::JNE
;
527 case X86::COND_L
: return X86::JL
;
528 case X86::COND_LE
: return X86::JLE
;
529 case X86::COND_G
: return X86::JG
;
530 case X86::COND_GE
: return X86::JGE
;
531 case X86::COND_B
: return X86::JB
;
532 case X86::COND_BE
: return X86::JBE
;
533 case X86::COND_A
: return X86::JA
;
534 case X86::COND_AE
: return X86::JAE
;
535 case X86::COND_S
: return X86::JS
;
536 case X86::COND_NS
: return X86::JNS
;
537 case X86::COND_P
: return X86::JP
;
538 case X86::COND_NP
: return X86::JNP
;
539 case X86::COND_O
: return X86::JO
;
540 case X86::COND_NO
: return X86::JNO
;
544 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
545 /// e.g. turning COND_E to COND_NE.
546 X86::CondCode
X86::GetOppositeBranchCondition(X86::CondCode CC
) {
548 default: assert(0 && "Illegal condition code!");
549 case X86::COND_E
: return X86::COND_NE
;
550 case X86::COND_NE
: return X86::COND_E
;
551 case X86::COND_L
: return X86::COND_GE
;
552 case X86::COND_LE
: return X86::COND_G
;
553 case X86::COND_G
: return X86::COND_LE
;
554 case X86::COND_GE
: return X86::COND_L
;
555 case X86::COND_B
: return X86::COND_AE
;
556 case X86::COND_BE
: return X86::COND_A
;
557 case X86::COND_A
: return X86::COND_BE
;
558 case X86::COND_AE
: return X86::COND_B
;
559 case X86::COND_S
: return X86::COND_NS
;
560 case X86::COND_NS
: return X86::COND_S
;
561 case X86::COND_P
: return X86::COND_NP
;
562 case X86::COND_NP
: return X86::COND_P
;
563 case X86::COND_O
: return X86::COND_NO
;
564 case X86::COND_NO
: return X86::COND_O
;
568 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr
*MI
) const {
569 const TargetInstrDescriptor
*TID
= MI
->getInstrDescriptor();
570 if (TID
->Flags
& M_TERMINATOR_FLAG
) {
571 // Conditional branch is a special case.
572 if ((TID
->Flags
& M_BRANCH_FLAG
) != 0 && (TID
->Flags
& M_BARRIER_FLAG
) == 0)
574 if ((TID
->Flags
& M_PREDICABLE
) == 0)
576 return !isPredicated(MI
);
581 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
582 static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr
*MI
,
583 const X86InstrInfo
&TII
) {
584 if (MI
->getOpcode() == X86::FP_REG_KILL
)
586 return TII
.isUnpredicatedTerminator(MI
);
589 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock
&MBB
,
590 MachineBasicBlock
*&TBB
,
591 MachineBasicBlock
*&FBB
,
592 std::vector
<MachineOperand
> &Cond
) const {
593 // If the block has no terminators, it just falls into the block after it.
594 MachineBasicBlock::iterator I
= MBB
.end();
595 if (I
== MBB
.begin() || !isBrAnalysisUnpredicatedTerminator(--I
, *this))
598 // Get the last instruction in the block.
599 MachineInstr
*LastInst
= I
;
601 // If there is only one terminator instruction, process it.
602 if (I
== MBB
.begin() || !isBrAnalysisUnpredicatedTerminator(--I
, *this)) {
603 if (!isBranch(LastInst
->getOpcode()))
606 // If the block ends with a branch there are 3 possibilities:
607 // it's an unconditional, conditional, or indirect branch.
609 if (LastInst
->getOpcode() == X86::JMP
) {
610 TBB
= LastInst
->getOperand(0).getMachineBasicBlock();
613 X86::CondCode BranchCode
= GetCondFromBranchOpc(LastInst
->getOpcode());
614 if (BranchCode
== X86::COND_INVALID
)
615 return true; // Can't handle indirect branch.
617 // Otherwise, block ends with fall-through condbranch.
618 TBB
= LastInst
->getOperand(0).getMachineBasicBlock();
619 Cond
.push_back(MachineOperand::CreateImm(BranchCode
));
623 // Get the instruction before it if it's a terminator.
624 MachineInstr
*SecondLastInst
= I
;
626 // If there are three terminators, we don't know what sort of block this is.
627 if (SecondLastInst
&& I
!= MBB
.begin() &&
628 isBrAnalysisUnpredicatedTerminator(--I
, *this))
631 // If the block ends with X86::JMP and a conditional branch, handle it.
632 X86::CondCode BranchCode
= GetCondFromBranchOpc(SecondLastInst
->getOpcode());
633 if (BranchCode
!= X86::COND_INVALID
&& LastInst
->getOpcode() == X86::JMP
) {
634 TBB
= SecondLastInst
->getOperand(0).getMachineBasicBlock();
635 Cond
.push_back(MachineOperand::CreateImm(BranchCode
));
636 FBB
= LastInst
->getOperand(0).getMachineBasicBlock();
640 // If the block ends with two X86::JMPs, handle it. The second one is not
641 // executed, so remove it.
642 if (SecondLastInst
->getOpcode() == X86::JMP
&&
643 LastInst
->getOpcode() == X86::JMP
) {
644 TBB
= SecondLastInst
->getOperand(0).getMachineBasicBlock();
646 I
->eraseFromParent();
650 // Otherwise, can't handle this.
654 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock
&MBB
) const {
655 MachineBasicBlock::iterator I
= MBB
.end();
656 if (I
== MBB
.begin()) return 0;
658 if (I
->getOpcode() != X86::JMP
&&
659 GetCondFromBranchOpc(I
->getOpcode()) == X86::COND_INVALID
)
662 // Remove the branch.
663 I
->eraseFromParent();
667 if (I
== MBB
.begin()) return 1;
669 if (GetCondFromBranchOpc(I
->getOpcode()) == X86::COND_INVALID
)
672 // Remove the branch.
673 I
->eraseFromParent();
678 X86InstrInfo::InsertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
679 MachineBasicBlock
*FBB
,
680 const std::vector
<MachineOperand
> &Cond
) const {
681 // Shouldn't be a fall through.
682 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
683 assert((Cond
.size() == 1 || Cond
.size() == 0) &&
684 "X86 branch conditions have one component!");
686 if (FBB
== 0) { // One way branch.
688 // Unconditional branch?
689 BuildMI(&MBB
, get(X86::JMP
)).addMBB(TBB
);
691 // Conditional branch.
692 unsigned Opc
= GetCondBranchFromCond((X86::CondCode
)Cond
[0].getImm());
693 BuildMI(&MBB
, get(Opc
)).addMBB(TBB
);
698 // Two-way Conditional branch.
699 unsigned Opc
= GetCondBranchFromCond((X86::CondCode
)Cond
[0].getImm());
700 BuildMI(&MBB
, get(Opc
)).addMBB(TBB
);
701 BuildMI(&MBB
, get(X86::JMP
)).addMBB(FBB
);
705 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock
&MBB
) const {
706 if (MBB
.empty()) return false;
708 switch (MBB
.back().getOpcode()) {
709 case X86::TCRETURNri
:
710 case X86::TCRETURNdi
:
711 case X86::RET
: // Return.
716 case X86::JMP
: // Uncond branch.
717 case X86::JMP32r
: // Indirect branch.
718 case X86::JMP64r
: // Indirect branch (64-bit).
719 case X86::JMP32m
: // Indirect branch through mem.
720 case X86::JMP64m
: // Indirect branch through mem (64-bit).
722 default: return false;
727 ReverseBranchCondition(std::vector
<MachineOperand
> &Cond
) const {
728 assert(Cond
.size() == 1 && "Invalid X86 branch condition!");
729 Cond
[0].setImm(GetOppositeBranchCondition((X86::CondCode
)Cond
[0].getImm()));
733 const TargetRegisterClass
*X86InstrInfo::getPointerRegClass() const {
734 const X86Subtarget
*Subtarget
= &TM
.getSubtarget
<X86Subtarget
>();
735 if (Subtarget
->is64Bit())
736 return &X86::GR64RegClass
;
738 return &X86::GR32RegClass
;