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