blob: 3809b8054952334640beb8f9e2757cfb9b95847f [file] [log] [blame]
Misha Brukmana85d6bc2002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3501fea2003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner055c9652002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner4ce42a72002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattnerabf05b22003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengaa3c1412006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
18#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng258ff672006-12-01 21:52:41 +000021#include "llvm/CodeGen/LiveVariables.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000022using namespace llvm;
23
Evan Chengaa3c1412006-05-30 21:45:53 +000024X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
Evan Cheng25ab6902006-09-08 06:48:29 +000026 TM(tm), RI(tm, *this) {
Chris Lattner72614082002-10-25 22:55:53 +000027}
28
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000029bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
30 unsigned& sourceReg,
31 unsigned& destReg) const {
32 MachineOpCode oc = MI.getOpcode();
Evan Cheng25ab6902006-09-08 06:48:29 +000033 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
34 oc == X86::MOV32rr || oc == X86::MOV64rr ||
Evan Cheng403be7e2006-05-08 08:01:26 +000035 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
Evan Chengbda54cd2006-02-01 23:03:16 +000036 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
Evan Chengfe5cb192006-02-16 22:45:17 +000037 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng82521dd2006-03-21 07:09:35 +000038 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Cheng11e15b32006-04-03 20:53:28 +000039 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
Bill Wendling2f88dcd2007-03-08 22:09:11 +000040 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
41 oc == X86::MOVD64rr || oc == X86::MOVQ64rr) {
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000042 assert(MI.getNumOperands() == 2 &&
43 MI.getOperand(0).isRegister() &&
44 MI.getOperand(1).isRegister() &&
45 "invalid register-register move instruction");
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000046 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000048 return true;
49 }
50 return false;
51}
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +000052
Chris Lattner40839602006-02-02 20:12:32 +000053unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
54 int &FrameIndex) const {
55 switch (MI->getOpcode()) {
56 default: break;
57 case X86::MOV8rm:
58 case X86::MOV16rm:
Evan Chengf4df6802006-05-11 07:33:49 +000059 case X86::MOV16_rm:
Chris Lattner40839602006-02-02 20:12:32 +000060 case X86::MOV32rm:
Evan Chengf4df6802006-05-11 07:33:49 +000061 case X86::MOV32_rm:
Evan Cheng25ab6902006-09-08 06:48:29 +000062 case X86::MOV64rm:
Chris Lattner40839602006-02-02 20:12:32 +000063 case X86::FpLD64m:
64 case X86::MOVSSrm:
65 case X86::MOVSDrm:
Chris Lattner993c8972006-04-18 16:44:51 +000066 case X86::MOVAPSrm:
67 case X86::MOVAPDrm:
Bill Wendling2f88dcd2007-03-08 22:09:11 +000068 case X86::MOVD64rm:
69 case X86::MOVQ64rm:
Chris Lattner40839602006-02-02 20:12:32 +000070 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
71 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
72 MI->getOperand(2).getImmedValue() == 1 &&
73 MI->getOperand(3).getReg() == 0 &&
74 MI->getOperand(4).getImmedValue() == 0) {
75 FrameIndex = MI->getOperand(1).getFrameIndex();
76 return MI->getOperand(0).getReg();
77 }
78 break;
79 }
80 return 0;
81}
82
83unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
84 int &FrameIndex) const {
85 switch (MI->getOpcode()) {
86 default: break;
87 case X86::MOV8mr:
88 case X86::MOV16mr:
Evan Chengf4df6802006-05-11 07:33:49 +000089 case X86::MOV16_mr:
Chris Lattner40839602006-02-02 20:12:32 +000090 case X86::MOV32mr:
Evan Chengf4df6802006-05-11 07:33:49 +000091 case X86::MOV32_mr:
Evan Cheng25ab6902006-09-08 06:48:29 +000092 case X86::MOV64mr:
Chris Lattner40839602006-02-02 20:12:32 +000093 case X86::FpSTP64m:
94 case X86::MOVSSmr:
95 case X86::MOVSDmr:
Chris Lattner993c8972006-04-18 16:44:51 +000096 case X86::MOVAPSmr:
97 case X86::MOVAPDmr:
Bill Wendling2f88dcd2007-03-08 22:09:11 +000098 case X86::MOVD64mr:
99 case X86::MOVQ64mr:
Chris Lattner40839602006-02-02 20:12:32 +0000100 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
101 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
Chris Lattner1c07e722006-02-02 20:38:12 +0000102 MI->getOperand(1).getImmedValue() == 1 &&
103 MI->getOperand(2).getReg() == 0 &&
104 MI->getOperand(3).getImmedValue() == 0) {
105 FrameIndex = MI->getOperand(0).getFrameIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000106 return MI->getOperand(4).getReg();
107 }
108 break;
109 }
110 return 0;
111}
112
113
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000114/// convertToThreeAddress - This method must be implemented by targets that
115/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
116/// may be able to convert a two-address instruction into a true
117/// three-address instruction on demand. This allows the X86 target (for
118/// example) to convert ADD and SHL instructions into LEA instructions if they
119/// would require register copies due to two-addressness.
120///
121/// This method returns a null pointer if the transformation cannot be
122/// performed, otherwise it returns the new instruction.
123///
Evan Cheng258ff672006-12-01 21:52:41 +0000124MachineInstr *
125X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
126 MachineBasicBlock::iterator &MBBI,
127 LiveVariables &LV) const {
128 MachineInstr *MI = MBBI;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000129 // All instructions input are two-addr instructions. Get the known operands.
130 unsigned Dest = MI->getOperand(0).getReg();
131 unsigned Src = MI->getOperand(1).getReg();
132
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000133 MachineInstr *NewMI = NULL;
Evan Cheng258ff672006-12-01 21:52:41 +0000134 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000135 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng258ff672006-12-01 21:52:41 +0000136 bool DisableLEA16 = true;
137
Evan Chengccba76b2006-05-30 20:26:50 +0000138 switch (MI->getOpcode()) {
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000139 default: return 0;
Evan Chengccba76b2006-05-30 20:26:50 +0000140 case X86::SHUFPSrri: {
141 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000142 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
143
Evan Chengaa3c1412006-05-30 21:45:53 +0000144 unsigned A = MI->getOperand(0).getReg();
145 unsigned B = MI->getOperand(1).getReg();
146 unsigned C = MI->getOperand(2).getReg();
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000147 unsigned M = MI->getOperand(3).getImm();
148 if (B != C) return 0;
Evan Chengc0f64ff2006-11-27 23:37:22 +0000149 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000150 break;
151 }
Chris Lattner995f5502007-03-28 18:12:31 +0000152 case X86::SHL64ri: {
153 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
154 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
155 // the flags produced by a shift yet, so this is safe.
156 unsigned Dest = MI->getOperand(0).getReg();
157 unsigned Src = MI->getOperand(1).getReg();
158 unsigned ShAmt = MI->getOperand(2).getImm();
159 if (ShAmt == 0 || ShAmt >= 4) return 0;
160
161 NewMI = BuildMI(get(X86::LEA64r), Dest)
162 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
163 break;
164 }
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000165 case X86::SHL32ri: {
166 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
167 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
168 // the flags produced by a shift yet, so this is safe.
169 unsigned Dest = MI->getOperand(0).getReg();
170 unsigned Src = MI->getOperand(1).getReg();
171 unsigned ShAmt = MI->getOperand(2).getImm();
172 if (ShAmt == 0 || ShAmt >= 4) return 0;
173
Chris Lattnerf2177b82007-03-28 00:58:40 +0000174 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
175 X86::LEA64_32r : X86::LEA32r;
176 NewMI = BuildMI(get(Opc), Dest)
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000177 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
178 break;
179 }
180 case X86::SHL16ri: {
181 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
182 if (DisableLEA16) return 0;
183
184 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
185 // the flags produced by a shift yet, so this is safe.
186 unsigned Dest = MI->getOperand(0).getReg();
187 unsigned Src = MI->getOperand(1).getReg();
188 unsigned ShAmt = MI->getOperand(2).getImm();
189 if (ShAmt == 0 || ShAmt >= 4) return 0;
190
191 NewMI = BuildMI(get(X86::LEA16r), Dest)
192 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
193 break;
Evan Chengccba76b2006-05-30 20:26:50 +0000194 }
195 }
196
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000197 // FIXME: None of these instructions are promotable to LEAs without
198 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner5aee0b92005-01-02 04:18:17 +0000199 // add and inc do. :(
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000200 if (0)
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000201 switch (MI->getOpcode()) {
202 case X86::INC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 case X86::INC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000204 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000205 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000206 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000207 case X86::INC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 case X86::INC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000209 if (DisableLEA16) return 0;
210 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000211 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000212 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000213 case X86::DEC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 case X86::DEC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000215 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000216 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000217 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000218 case X86::DEC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 case X86::DEC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000220 if (DisableLEA16) return 0;
221 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000222 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000223 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000224 case X86::ADD32rr:
225 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000226 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000227 MI->getOperand(2).getReg());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000228 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000229 case X86::ADD16rr:
230 if (DisableLEA16) return 0;
231 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000232 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000233 MI->getOperand(2).getReg());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000234 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000235 case X86::ADD32ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000236 case X86::ADD32ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000237 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
238 if (MI->getOperand(2).isImmediate())
Evan Chengc0f64ff2006-11-27 23:37:22 +0000239 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000240 MI->getOperand(2).getImmedValue());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000241 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000242 case X86::ADD16ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000243 case X86::ADD16ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000244 if (DisableLEA16) return 0;
245 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
246 if (MI->getOperand(2).isImmediate())
Evan Chengc0f64ff2006-11-27 23:37:22 +0000247 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000248 MI->getOperand(2).getImmedValue());
249 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000250 case X86::SHL16ri:
251 if (DisableLEA16) return 0;
252 case X86::SHL32ri:
253 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
254 "Unknown shl instruction!");
255 unsigned ShAmt = MI->getOperand(2).getImmedValue();
256 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
257 X86AddressMode AM;
258 AM.Scale = 1 << ShAmt;
259 AM.IndexReg = Src;
260 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
Evan Chengc0f64ff2006-11-27 23:37:22 +0000261 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000262 }
263 break;
264 }
265
Evan Cheng258ff672006-12-01 21:52:41 +0000266 if (NewMI) {
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000267 NewMI->copyKillDeadInfo(MI);
Evan Cheng258ff672006-12-01 21:52:41 +0000268 LV.instructionChanged(MI, NewMI); // Update live variables
269 MFI->insert(MBBI, NewMI); // Insert the new inst
270 }
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000271 return NewMI;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000272}
273
Chris Lattner41e431b2005-01-19 07:11:01 +0000274/// commuteInstruction - We have a few instructions that must be hacked on to
275/// commute them.
276///
277MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattner6458f182006-09-28 23:33:12 +0000278 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner41e431b2005-01-19 07:11:01 +0000279 switch (MI->getOpcode()) {
Chris Lattner0df53d22005-01-19 07:31:24 +0000280 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
281 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner41e431b2005-01-19 07:11:01 +0000282 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
283 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattner0df53d22005-01-19 07:31:24 +0000284 unsigned Opc;
285 unsigned Size;
286 switch (MI->getOpcode()) {
287 default: assert(0 && "Unreachable!");
288 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
289 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
290 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
291 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
292 }
Chris Lattner41e431b2005-01-19 07:11:01 +0000293 unsigned Amt = MI->getOperand(3).getImmedValue();
294 unsigned A = MI->getOperand(0).getReg();
295 unsigned B = MI->getOperand(1).getReg();
296 unsigned C = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000297 bool BisKill = MI->getOperand(1).isKill();
298 bool CisKill = MI->getOperand(2).isKill();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000299 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000300 .addReg(B, false, false, BisKill).addImm(Size-Amt);
Chris Lattner41e431b2005-01-19 07:11:01 +0000301 }
302 default:
303 return TargetInstrInfo::commuteInstruction(MI);
304 }
305}
306
Chris Lattner7fbe9722006-10-20 17:42:20 +0000307static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
308 switch (BrOpc) {
309 default: return X86::COND_INVALID;
310 case X86::JE: return X86::COND_E;
311 case X86::JNE: return X86::COND_NE;
312 case X86::JL: return X86::COND_L;
313 case X86::JLE: return X86::COND_LE;
314 case X86::JG: return X86::COND_G;
315 case X86::JGE: return X86::COND_GE;
316 case X86::JB: return X86::COND_B;
317 case X86::JBE: return X86::COND_BE;
318 case X86::JA: return X86::COND_A;
319 case X86::JAE: return X86::COND_AE;
320 case X86::JS: return X86::COND_S;
321 case X86::JNS: return X86::COND_NS;
322 case X86::JP: return X86::COND_P;
323 case X86::JNP: return X86::COND_NP;
324 case X86::JO: return X86::COND_O;
325 case X86::JNO: return X86::COND_NO;
326 }
327}
328
329unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
330 switch (CC) {
331 default: assert(0 && "Illegal condition code!");
332 case X86::COND_E: return X86::JE;
333 case X86::COND_NE: return X86::JNE;
334 case X86::COND_L: return X86::JL;
335 case X86::COND_LE: return X86::JLE;
336 case X86::COND_G: return X86::JG;
337 case X86::COND_GE: return X86::JGE;
338 case X86::COND_B: return X86::JB;
339 case X86::COND_BE: return X86::JBE;
340 case X86::COND_A: return X86::JA;
341 case X86::COND_AE: return X86::JAE;
342 case X86::COND_S: return X86::JS;
343 case X86::COND_NS: return X86::JNS;
344 case X86::COND_P: return X86::JP;
345 case X86::COND_NP: return X86::JNP;
346 case X86::COND_O: return X86::JO;
347 case X86::COND_NO: return X86::JNO;
348 }
349}
350
Chris Lattner9cd68752006-10-21 05:52:40 +0000351/// GetOppositeBranchCondition - Return the inverse of the specified condition,
352/// e.g. turning COND_E to COND_NE.
353X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
354 switch (CC) {
355 default: assert(0 && "Illegal condition code!");
356 case X86::COND_E: return X86::COND_NE;
357 case X86::COND_NE: return X86::COND_E;
358 case X86::COND_L: return X86::COND_GE;
359 case X86::COND_LE: return X86::COND_G;
360 case X86::COND_G: return X86::COND_LE;
361 case X86::COND_GE: return X86::COND_L;
362 case X86::COND_B: return X86::COND_AE;
363 case X86::COND_BE: return X86::COND_A;
364 case X86::COND_A: return X86::COND_BE;
365 case X86::COND_AE: return X86::COND_B;
366 case X86::COND_S: return X86::COND_NS;
367 case X86::COND_NS: return X86::COND_S;
368 case X86::COND_P: return X86::COND_NP;
369 case X86::COND_NP: return X86::COND_P;
370 case X86::COND_O: return X86::COND_NO;
371 case X86::COND_NO: return X86::COND_O;
372 }
373}
374
375
Chris Lattner7fbe9722006-10-20 17:42:20 +0000376bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
377 MachineBasicBlock *&TBB,
378 MachineBasicBlock *&FBB,
379 std::vector<MachineOperand> &Cond) const {
380 // TODO: If FP_REG_KILL is around, ignore it.
381
382 // If the block has no terminators, it just falls into the block after it.
383 MachineBasicBlock::iterator I = MBB.end();
384 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
385 return false;
386
387 // Get the last instruction in the block.
388 MachineInstr *LastInst = I;
389
390 // If there is only one terminator instruction, process it.
391 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
392 if (!isBranch(LastInst->getOpcode()))
393 return true;
394
395 // If the block ends with a branch there are 3 possibilities:
396 // it's an unconditional, conditional, or indirect branch.
397
398 if (LastInst->getOpcode() == X86::JMP) {
399 TBB = LastInst->getOperand(0).getMachineBasicBlock();
400 return false;
401 }
402 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
403 if (BranchCode == X86::COND_INVALID)
404 return true; // Can't handle indirect branch.
405
406 // Otherwise, block ends with fall-through condbranch.
407 TBB = LastInst->getOperand(0).getMachineBasicBlock();
408 Cond.push_back(MachineOperand::CreateImm(BranchCode));
409 return false;
410 }
411
412 // Get the instruction before it if it's a terminator.
413 MachineInstr *SecondLastInst = I;
414
415 // If there are three terminators, we don't know what sort of block this is.
416 if (SecondLastInst && I != MBB.begin() &&
417 isTerminatorInstr((--I)->getOpcode()))
418 return true;
419
Chris Lattner6ce64432006-10-30 22:27:23 +0000420 // If the block ends with X86::JMP and a conditional branch, handle it.
Chris Lattner7fbe9722006-10-20 17:42:20 +0000421 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
422 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner6ce64432006-10-30 22:27:23 +0000423 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
424 Cond.push_back(MachineOperand::CreateImm(BranchCode));
425 FBB = LastInst->getOperand(0).getMachineBasicBlock();
426 return false;
427 }
Chris Lattner7fbe9722006-10-20 17:42:20 +0000428
429 // Otherwise, can't handle this.
430 return true;
431}
432
433void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
434 MachineBasicBlock::iterator I = MBB.end();
435 if (I == MBB.begin()) return;
436 --I;
437 if (I->getOpcode() != X86::JMP &&
438 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
439 return;
440
441 // Remove the branch.
442 I->eraseFromParent();
443
444 I = MBB.end();
445
446 if (I == MBB.begin()) return;
447 --I;
448 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
449 return;
450
451 // Remove the branch.
452 I->eraseFromParent();
453}
454
455void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
456 MachineBasicBlock *FBB,
457 const std::vector<MachineOperand> &Cond) const {
458 // Shouldn't be a fall through.
459 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner34a84ac2006-10-21 05:34:23 +0000460 assert((Cond.size() == 1 || Cond.size() == 0) &&
461 "X86 branch conditions have one component!");
462
463 if (FBB == 0) { // One way branch.
464 if (Cond.empty()) {
465 // Unconditional branch?
Evan Chengc0f64ff2006-11-27 23:37:22 +0000466 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
Chris Lattner34a84ac2006-10-21 05:34:23 +0000467 } else {
468 // Conditional branch.
469 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +0000470 BuildMI(&MBB, get(Opc)).addMBB(TBB);
Chris Lattner34a84ac2006-10-21 05:34:23 +0000471 }
Chris Lattner7fbe9722006-10-20 17:42:20 +0000472 return;
473 }
474
Chris Lattner879d09c2006-10-21 05:42:09 +0000475 // Two-way Conditional branch.
Chris Lattner7fbe9722006-10-20 17:42:20 +0000476 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +0000477 BuildMI(&MBB, get(Opc)).addMBB(TBB);
478 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
Chris Lattner7fbe9722006-10-20 17:42:20 +0000479}
480
Chris Lattnerc24ff8e2006-10-28 17:29:57 +0000481bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
482 if (MBB.empty()) return false;
483
484 switch (MBB.back().getOpcode()) {
485 case X86::JMP: // Uncond branch.
486 case X86::JMP32r: // Indirect branch.
487 case X86::JMP32m: // Indirect branch through mem.
488 return true;
489 default: return false;
490 }
491}
492
Chris Lattner7fbe9722006-10-20 17:42:20 +0000493bool X86InstrInfo::
494ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner9cd68752006-10-21 05:52:40 +0000495 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
496 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
497 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +0000498}
499
Evan Cheng25ab6902006-09-08 06:48:29 +0000500const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
501 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
502 if (Subtarget->is64Bit())
503 return &X86::GR64RegClass;
504 else
505 return &X86::GR32RegClass;
506}