blob: 24e434cca15be967595046661efe56eebf20ef70 [file] [log] [blame]
Misha Brukman1a72c632002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd92fb002002-10-25 22:55:53 +00009//
Chris Lattnerb4d58d72003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattnerd92fb002002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner27d24792002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner0d808742002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattner05e2f382003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengc8c172e2006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
18#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng07fc1072006-12-01 21:52:41 +000021#include "llvm/CodeGen/LiveVariables.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000022using namespace llvm;
23
Evan Chengc8c172e2006-05-30 21:45:53 +000024X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
Evan Cheng11b0a5d2006-09-08 06:48:29 +000026 TM(tm), RI(tm, *this) {
Chris Lattnerd92fb002002-10-25 22:55:53 +000027}
28
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000029bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
30 unsigned& sourceReg,
31 unsigned& destReg) const {
32 MachineOpCode oc = MI.getOpcode();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000033 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
34 oc == X86::MOV32rr || oc == X86::MOV64rr ||
Evan Cheng9733bde2006-05-08 08:01:26 +000035 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
Evan Chengb3ea2672006-02-01 23:03:16 +000036 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
Evan Cheng24c461b2006-02-16 22:45:17 +000037 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng1208d9172006-03-21 07:09:35 +000038 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Chengebf10062006-04-03 20:53:28 +000039 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
Bill Wendling6092ce22007-03-08 22:09:11 +000040 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
41 oc == X86::MOVD64rr || oc == X86::MOVQ64rr) {
Alkis Evlogimenos52564b22003-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 Evlogimenos8cdd0212004-02-13 21:01:20 +000046 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000048 return true;
49 }
50 return false;
51}
Alkis Evlogimenosf57d78a2004-07-31 09:38:47 +000052
Chris Lattnerbb53acd2006-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 Chengdd7230c2006-05-11 07:33:49 +000059 case X86::MOV16_rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000060 case X86::MOV32rm:
Evan Chengdd7230c2006-05-11 07:33:49 +000061 case X86::MOV32_rm:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000062 case X86::MOV64rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000063 case X86::FpLD64m:
64 case X86::MOVSSrm:
65 case X86::MOVSDrm:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000066 case X86::MOVAPSrm:
67 case X86::MOVAPDrm:
Bill Wendling6092ce22007-03-08 22:09:11 +000068 case X86::MOVD64rm:
69 case X86::MOVQ64rm:
Chris Lattnerbb53acd2006-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 Chengdd7230c2006-05-11 07:33:49 +000089 case X86::MOV16_mr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000090 case X86::MOV32mr:
Evan Chengdd7230c2006-05-11 07:33:49 +000091 case X86::MOV32_mr:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000092 case X86::MOV64mr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000093 case X86::FpSTP64m:
94 case X86::MOVSSmr:
95 case X86::MOVSDmr:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000096 case X86::MOVAPSmr:
97 case X86::MOVAPDmr:
Bill Wendling6092ce22007-03-08 22:09:11 +000098 case X86::MOVD64mr:
99 case X86::MOVQ64mr:
Chris Lattnerbb53acd2006-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 Lattner512334672006-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 Lattnerbb53acd2006-02-02 20:12:32 +0000106 return MI->getOperand(4).getReg();
107 }
108 break;
109 }
110 return 0;
111}
112
113
Chris Lattnerb7782d72005-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 Cheng07fc1072006-12-01 21:52:41 +0000124MachineInstr *
125X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
126 MachineBasicBlock::iterator &MBBI,
127 LiveVariables &LV) const {
128 MachineInstr *MI = MBBI;
Chris Lattnerb7782d72005-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 Chengdc2c8742006-11-15 20:58:11 +0000133 MachineInstr *NewMI = NULL;
Evan Cheng07fc1072006-12-01 21:52:41 +0000134 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
135 // we have subtarget support, enable the 16-bit LEA generation here.
136 bool DisableLEA16 = true;
137
Evan Cheng66f849b2006-05-30 20:26:50 +0000138 switch (MI->getOpcode()) {
139 default: break;
140 case X86::SHUFPSrri: {
141 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Evan Chenga4fc5b82006-05-30 21:30:59 +0000142 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengc8c172e2006-05-30 21:45:53 +0000143 unsigned A = MI->getOperand(0).getReg();
144 unsigned B = MI->getOperand(1).getReg();
145 unsigned C = MI->getOperand(2).getReg();
146 unsigned M = MI->getOperand(3).getImmedValue();
Evan Chengf21045a2006-05-30 22:13:36 +0000147 if (!Subtarget->hasSSE2() || B != C) return 0;
Evan Cheng20350c42006-11-27 23:37:22 +0000148 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
Evan Cheng07fc1072006-12-01 21:52:41 +0000149 goto Done;
Evan Cheng66f849b2006-05-30 20:26:50 +0000150 }
151 }
152
Misha Brukmanc88330a2005-04-21 23:38:14 +0000153 // FIXME: None of these instructions are promotable to LEAs without
154 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner733aac12005-01-02 04:18:17 +0000155 // add and inc do. :(
156 return 0;
157
Chris Lattnerb7782d72005-01-02 02:37:07 +0000158 switch (MI->getOpcode()) {
159 case X86::INC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000160 case X86::INC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000161 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000162 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000163 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000164 case X86::INC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000165 case X86::INC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000166 if (DisableLEA16) return 0;
167 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000168 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000169 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000170 case X86::DEC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000171 case X86::DEC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000172 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000173 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000174 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000175 case X86::DEC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000176 case X86::DEC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000177 if (DisableLEA16) return 0;
178 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000179 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000180 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000181 case X86::ADD32rr:
182 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000183 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000184 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000185 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000186 case X86::ADD16rr:
187 if (DisableLEA16) return 0;
188 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000189 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000190 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000191 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000192 case X86::ADD32ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000193 case X86::ADD32ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000194 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
195 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000196 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000197 MI->getOperand(2).getImmedValue());
Evan Chengdc2c8742006-11-15 20:58:11 +0000198 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000199 case X86::ADD16ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000200 case X86::ADD16ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000201 if (DisableLEA16) return 0;
202 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
203 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000204 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000205 MI->getOperand(2).getImmedValue());
206 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000207 case X86::SHL16ri:
208 if (DisableLEA16) return 0;
209 case X86::SHL32ri:
210 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
211 "Unknown shl instruction!");
212 unsigned ShAmt = MI->getOperand(2).getImmedValue();
213 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
214 X86AddressMode AM;
215 AM.Scale = 1 << ShAmt;
216 AM.IndexReg = Src;
217 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
Evan Cheng20350c42006-11-27 23:37:22 +0000218 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
Chris Lattnerb7782d72005-01-02 02:37:07 +0000219 }
220 break;
221 }
222
Evan Cheng07fc1072006-12-01 21:52:41 +0000223Done:
224 if (NewMI) {
Evan Chengdc2c8742006-11-15 20:58:11 +0000225 NewMI->copyKillDeadInfo(MI);
Evan Cheng07fc1072006-12-01 21:52:41 +0000226 LV.instructionChanged(MI, NewMI); // Update live variables
227 MFI->insert(MBBI, NewMI); // Insert the new inst
228 }
Evan Chengdc2c8742006-11-15 20:58:11 +0000229 return NewMI;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000230}
231
Chris Lattner29478012005-01-19 07:11:01 +0000232/// commuteInstruction - We have a few instructions that must be hacked on to
233/// commute them.
234///
235MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattnerd9e4bf52006-09-28 23:33:12 +0000236 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner29478012005-01-19 07:11:01 +0000237 switch (MI->getOpcode()) {
Chris Lattnerd54845f2005-01-19 07:31:24 +0000238 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
239 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner29478012005-01-19 07:11:01 +0000240 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
241 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattnerd54845f2005-01-19 07:31:24 +0000242 unsigned Opc;
243 unsigned Size;
244 switch (MI->getOpcode()) {
245 default: assert(0 && "Unreachable!");
246 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
247 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
248 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
249 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
250 }
Chris Lattner29478012005-01-19 07:11:01 +0000251 unsigned Amt = MI->getOperand(3).getImmedValue();
252 unsigned A = MI->getOperand(0).getReg();
253 unsigned B = MI->getOperand(1).getReg();
254 unsigned C = MI->getOperand(2).getReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000255 bool BisKill = MI->getOperand(1).isKill();
256 bool CisKill = MI->getOperand(2).isKill();
Evan Cheng20350c42006-11-27 23:37:22 +0000257 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
Evan Chengdc2c8742006-11-15 20:58:11 +0000258 .addReg(B, false, false, BisKill).addImm(Size-Amt);
Chris Lattner29478012005-01-19 07:11:01 +0000259 }
260 default:
261 return TargetInstrInfo::commuteInstruction(MI);
262 }
263}
264
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000265static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
266 switch (BrOpc) {
267 default: return X86::COND_INVALID;
268 case X86::JE: return X86::COND_E;
269 case X86::JNE: return X86::COND_NE;
270 case X86::JL: return X86::COND_L;
271 case X86::JLE: return X86::COND_LE;
272 case X86::JG: return X86::COND_G;
273 case X86::JGE: return X86::COND_GE;
274 case X86::JB: return X86::COND_B;
275 case X86::JBE: return X86::COND_BE;
276 case X86::JA: return X86::COND_A;
277 case X86::JAE: return X86::COND_AE;
278 case X86::JS: return X86::COND_S;
279 case X86::JNS: return X86::COND_NS;
280 case X86::JP: return X86::COND_P;
281 case X86::JNP: return X86::COND_NP;
282 case X86::JO: return X86::COND_O;
283 case X86::JNO: return X86::COND_NO;
284 }
285}
286
287unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
288 switch (CC) {
289 default: assert(0 && "Illegal condition code!");
290 case X86::COND_E: return X86::JE;
291 case X86::COND_NE: return X86::JNE;
292 case X86::COND_L: return X86::JL;
293 case X86::COND_LE: return X86::JLE;
294 case X86::COND_G: return X86::JG;
295 case X86::COND_GE: return X86::JGE;
296 case X86::COND_B: return X86::JB;
297 case X86::COND_BE: return X86::JBE;
298 case X86::COND_A: return X86::JA;
299 case X86::COND_AE: return X86::JAE;
300 case X86::COND_S: return X86::JS;
301 case X86::COND_NS: return X86::JNS;
302 case X86::COND_P: return X86::JP;
303 case X86::COND_NP: return X86::JNP;
304 case X86::COND_O: return X86::JO;
305 case X86::COND_NO: return X86::JNO;
306 }
307}
308
Chris Lattner3a897f32006-10-21 05:52:40 +0000309/// GetOppositeBranchCondition - Return the inverse of the specified condition,
310/// e.g. turning COND_E to COND_NE.
311X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
312 switch (CC) {
313 default: assert(0 && "Illegal condition code!");
314 case X86::COND_E: return X86::COND_NE;
315 case X86::COND_NE: return X86::COND_E;
316 case X86::COND_L: return X86::COND_GE;
317 case X86::COND_LE: return X86::COND_G;
318 case X86::COND_G: return X86::COND_LE;
319 case X86::COND_GE: return X86::COND_L;
320 case X86::COND_B: return X86::COND_AE;
321 case X86::COND_BE: return X86::COND_A;
322 case X86::COND_A: return X86::COND_BE;
323 case X86::COND_AE: return X86::COND_B;
324 case X86::COND_S: return X86::COND_NS;
325 case X86::COND_NS: return X86::COND_S;
326 case X86::COND_P: return X86::COND_NP;
327 case X86::COND_NP: return X86::COND_P;
328 case X86::COND_O: return X86::COND_NO;
329 case X86::COND_NO: return X86::COND_O;
330 }
331}
332
333
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000334bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
335 MachineBasicBlock *&TBB,
336 MachineBasicBlock *&FBB,
337 std::vector<MachineOperand> &Cond) const {
338 // TODO: If FP_REG_KILL is around, ignore it.
339
340 // If the block has no terminators, it just falls into the block after it.
341 MachineBasicBlock::iterator I = MBB.end();
342 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
343 return false;
344
345 // Get the last instruction in the block.
346 MachineInstr *LastInst = I;
347
348 // If there is only one terminator instruction, process it.
349 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
350 if (!isBranch(LastInst->getOpcode()))
351 return true;
352
353 // If the block ends with a branch there are 3 possibilities:
354 // it's an unconditional, conditional, or indirect branch.
355
356 if (LastInst->getOpcode() == X86::JMP) {
357 TBB = LastInst->getOperand(0).getMachineBasicBlock();
358 return false;
359 }
360 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
361 if (BranchCode == X86::COND_INVALID)
362 return true; // Can't handle indirect branch.
363
364 // Otherwise, block ends with fall-through condbranch.
365 TBB = LastInst->getOperand(0).getMachineBasicBlock();
366 Cond.push_back(MachineOperand::CreateImm(BranchCode));
367 return false;
368 }
369
370 // Get the instruction before it if it's a terminator.
371 MachineInstr *SecondLastInst = I;
372
373 // If there are three terminators, we don't know what sort of block this is.
374 if (SecondLastInst && I != MBB.begin() &&
375 isTerminatorInstr((--I)->getOpcode()))
376 return true;
377
Chris Lattner74436002006-10-30 22:27:23 +0000378 // If the block ends with X86::JMP and a conditional branch, handle it.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000379 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
380 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner74436002006-10-30 22:27:23 +0000381 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
382 Cond.push_back(MachineOperand::CreateImm(BranchCode));
383 FBB = LastInst->getOperand(0).getMachineBasicBlock();
384 return false;
385 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000386
387 // Otherwise, can't handle this.
388 return true;
389}
390
391void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
392 MachineBasicBlock::iterator I = MBB.end();
393 if (I == MBB.begin()) return;
394 --I;
395 if (I->getOpcode() != X86::JMP &&
396 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
397 return;
398
399 // Remove the branch.
400 I->eraseFromParent();
401
402 I = MBB.end();
403
404 if (I == MBB.begin()) return;
405 --I;
406 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
407 return;
408
409 // Remove the branch.
410 I->eraseFromParent();
411}
412
413void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
414 MachineBasicBlock *FBB,
415 const std::vector<MachineOperand> &Cond) const {
416 // Shouldn't be a fall through.
417 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner6fca75e2006-10-21 05:34:23 +0000418 assert((Cond.size() == 1 || Cond.size() == 0) &&
419 "X86 branch conditions have one component!");
420
421 if (FBB == 0) { // One way branch.
422 if (Cond.empty()) {
423 // Unconditional branch?
Evan Cheng20350c42006-11-27 23:37:22 +0000424 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000425 } else {
426 // Conditional branch.
427 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000428 BuildMI(&MBB, get(Opc)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000429 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000430 return;
431 }
432
Chris Lattnerd8816602006-10-21 05:42:09 +0000433 // Two-way Conditional branch.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000434 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000435 BuildMI(&MBB, get(Opc)).addMBB(TBB);
436 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000437}
438
Chris Lattner53ebf202006-10-28 17:29:57 +0000439bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
440 if (MBB.empty()) return false;
441
442 switch (MBB.back().getOpcode()) {
443 case X86::JMP: // Uncond branch.
444 case X86::JMP32r: // Indirect branch.
445 case X86::JMP32m: // Indirect branch through mem.
446 return true;
447 default: return false;
448 }
449}
450
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000451bool X86InstrInfo::
452ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner3a897f32006-10-21 05:52:40 +0000453 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
454 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
455 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000456}
457
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000458const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
459 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
460 if (Subtarget->is64Bit())
461 return &X86::GR64RegClass;
462 else
463 return &X86::GR32RegClass;
464}