blob: a2043a64810351f5dc200e18efc8240b52467ceb [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"
Brian Gaeked0fde302003-11-11 22:41:34 +000021using namespace llvm;
22
Evan Chengaa3c1412006-05-30 21:45:53 +000023X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
24 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
Evan Cheng25ab6902006-09-08 06:48:29 +000025 TM(tm), RI(tm, *this) {
Chris Lattner72614082002-10-25 22:55:53 +000026}
27
Chris Lattnerae1dc402006-10-17 22:41:45 +000028/// getDWARF_LABELOpcode - Return the opcode of the target's DWARF_LABEL
29/// instruction if it has one. This is used by codegen passes that update
30/// DWARF line number info as they modify the code.
31unsigned X86InstrInfo::getDWARF_LABELOpcode() const {
32 return X86::DWARF_LABEL;
33}
34
Chris Lattner72614082002-10-25 22:55:53 +000035
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000036bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
37 unsigned& sourceReg,
38 unsigned& destReg) const {
39 MachineOpCode oc = MI.getOpcode();
Evan Cheng25ab6902006-09-08 06:48:29 +000040 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
41 oc == X86::MOV32rr || oc == X86::MOV64rr ||
Evan Cheng403be7e2006-05-08 08:01:26 +000042 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
Evan Chengbda54cd2006-02-01 23:03:16 +000043 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
Evan Chengfe5cb192006-02-16 22:45:17 +000044 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng82521dd2006-03-21 07:09:35 +000045 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Cheng11e15b32006-04-03 20:53:28 +000046 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
Evan Chenge23fc602006-11-16 23:22:26 +000047 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr) {
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000048 assert(MI.getNumOperands() == 2 &&
49 MI.getOperand(0).isRegister() &&
50 MI.getOperand(1).isRegister() &&
51 "invalid register-register move instruction");
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000052 sourceReg = MI.getOperand(1).getReg();
53 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000054 return true;
55 }
56 return false;
57}
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +000058
Chris Lattner40839602006-02-02 20:12:32 +000059unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
60 int &FrameIndex) const {
61 switch (MI->getOpcode()) {
62 default: break;
63 case X86::MOV8rm:
64 case X86::MOV16rm:
Evan Chengf4df6802006-05-11 07:33:49 +000065 case X86::MOV16_rm:
Chris Lattner40839602006-02-02 20:12:32 +000066 case X86::MOV32rm:
Evan Chengf4df6802006-05-11 07:33:49 +000067 case X86::MOV32_rm:
Evan Cheng25ab6902006-09-08 06:48:29 +000068 case X86::MOV64rm:
Chris Lattner40839602006-02-02 20:12:32 +000069 case X86::FpLD64m:
70 case X86::MOVSSrm:
71 case X86::MOVSDrm:
Chris Lattner993c8972006-04-18 16:44:51 +000072 case X86::MOVAPSrm:
73 case X86::MOVAPDrm:
Chris Lattner40839602006-02-02 20:12:32 +000074 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();
81 }
82 break;
83 }
84 return 0;
85}
86
87unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
88 int &FrameIndex) const {
89 switch (MI->getOpcode()) {
90 default: break;
91 case X86::MOV8mr:
92 case X86::MOV16mr:
Evan Chengf4df6802006-05-11 07:33:49 +000093 case X86::MOV16_mr:
Chris Lattner40839602006-02-02 20:12:32 +000094 case X86::MOV32mr:
Evan Chengf4df6802006-05-11 07:33:49 +000095 case X86::MOV32_mr:
Evan Cheng25ab6902006-09-08 06:48:29 +000096 case X86::MOV64mr:
Chris Lattner40839602006-02-02 20:12:32 +000097 case X86::FpSTP64m:
98 case X86::MOVSSmr:
99 case X86::MOVSDmr:
Chris Lattner993c8972006-04-18 16:44:51 +0000100 case X86::MOVAPSmr:
101 case X86::MOVAPDmr:
Chris Lattner40839602006-02-02 20:12:32 +0000102 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
103 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
Chris Lattner1c07e722006-02-02 20:38:12 +0000104 MI->getOperand(1).getImmedValue() == 1 &&
105 MI->getOperand(2).getReg() == 0 &&
106 MI->getOperand(3).getImmedValue() == 0) {
107 FrameIndex = MI->getOperand(0).getFrameIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000108 return MI->getOperand(4).getReg();
109 }
110 break;
111 }
112 return 0;
113}
114
115
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000116/// convertToThreeAddress - This method must be implemented by targets that
117/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
118/// may be able to convert a two-address instruction into a true
119/// three-address instruction on demand. This allows the X86 target (for
120/// example) to convert ADD and SHL instructions into LEA instructions if they
121/// would require register copies due to two-addressness.
122///
123/// This method returns a null pointer if the transformation cannot be
124/// performed, otherwise it returns the new instruction.
125///
126MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
127 // All instructions input are two-addr instructions. Get the known operands.
128 unsigned Dest = MI->getOperand(0).getReg();
129 unsigned Src = MI->getOperand(1).getReg();
130
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000131 MachineInstr *NewMI = NULL;
Evan Chengccba76b2006-05-30 20:26:50 +0000132 switch (MI->getOpcode()) {
133 default: break;
134 case X86::SHUFPSrri: {
135 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Evan Cheng51da42c2006-05-30 21:30:59 +0000136 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengaa3c1412006-05-30 21:45:53 +0000137 unsigned A = MI->getOperand(0).getReg();
138 unsigned B = MI->getOperand(1).getReg();
139 unsigned C = MI->getOperand(2).getReg();
140 unsigned M = MI->getOperand(3).getImmedValue();
Evan Chenga0eaf2d2006-05-30 22:13:36 +0000141 if (!Subtarget->hasSSE2() || B != C) return 0;
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000142 NewMI = BuildMI(*this, X86::PSHUFDri, 2, A).addReg(B).addImm(M);
143 NewMI->copyKillDeadInfo(MI);
144 return NewMI;
Evan Chengccba76b2006-05-30 20:26:50 +0000145 }
146 }
147
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000148 // FIXME: None of these instructions are promotable to LEAs without
149 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner5aee0b92005-01-02 04:18:17 +0000150 // add and inc do. :(
151 return 0;
152
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000153 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
154 // we have subtarget support, enable the 16-bit LEA generation here.
155 bool DisableLEA16 = true;
156
157 switch (MI->getOpcode()) {
158 case X86::INC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000159 case X86::INC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000160 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000161 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, 1);
162 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000163 case X86::INC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000164 case X86::INC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000165 if (DisableLEA16) return 0;
166 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000167 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, 1);
168 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000169 case X86::DEC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000170 case X86::DEC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000171 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000172 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, -1);
173 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000174 case X86::DEC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000175 case X86::DEC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000176 if (DisableLEA16) return 0;
177 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000178 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, -1);
179 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000180 case X86::ADD32rr:
181 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000182 NewMI = addRegReg(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000183 MI->getOperand(2).getReg());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000184 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000185 case X86::ADD16rr:
186 if (DisableLEA16) return 0;
187 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000188 NewMI = addRegReg(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000189 MI->getOperand(2).getReg());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000190 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000191 case X86::ADD32ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000192 case X86::ADD32ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000193 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
194 if (MI->getOperand(2).isImmediate())
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000195 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000196 MI->getOperand(2).getImmedValue());
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000197 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000198 case X86::ADD16ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000199 case X86::ADD16ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000200 if (DisableLEA16) return 0;
201 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
202 if (MI->getOperand(2).isImmediate())
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000203 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000204 MI->getOperand(2).getImmedValue());
205 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000206 case X86::SHL16ri:
207 if (DisableLEA16) return 0;
208 case X86::SHL32ri:
209 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
210 "Unknown shl instruction!");
211 unsigned ShAmt = MI->getOperand(2).getImmedValue();
212 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
213 X86AddressMode AM;
214 AM.Scale = 1 << ShAmt;
215 AM.IndexReg = Src;
216 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000217 NewMI = addFullAddress(BuildMI(*this, Opc, 5, Dest), AM);
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000218 }
219 break;
220 }
221
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000222 if (NewMI)
223 NewMI->copyKillDeadInfo(MI);
224 return NewMI;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000225}
226
Chris Lattner41e431b2005-01-19 07:11:01 +0000227/// commuteInstruction - We have a few instructions that must be hacked on to
228/// commute them.
229///
230MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattner6458f182006-09-28 23:33:12 +0000231 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner41e431b2005-01-19 07:11:01 +0000232 switch (MI->getOpcode()) {
Chris Lattner0df53d22005-01-19 07:31:24 +0000233 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
234 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner41e431b2005-01-19 07:11:01 +0000235 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
236 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattner0df53d22005-01-19 07:31:24 +0000237 unsigned Opc;
238 unsigned Size;
239 switch (MI->getOpcode()) {
240 default: assert(0 && "Unreachable!");
241 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
242 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
243 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
244 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
245 }
Chris Lattner41e431b2005-01-19 07:11:01 +0000246 unsigned Amt = MI->getOperand(3).getImmedValue();
247 unsigned A = MI->getOperand(0).getReg();
248 unsigned B = MI->getOperand(1).getReg();
249 unsigned C = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000250 bool BisKill = MI->getOperand(1).isKill();
251 bool CisKill = MI->getOperand(2).isKill();
252 return BuildMI(*this, Opc, 3, A).addReg(C, false, false, CisKill)
253 .addReg(B, false, false, BisKill).addImm(Size-Amt);
Chris Lattner41e431b2005-01-19 07:11:01 +0000254 }
255 default:
256 return TargetInstrInfo::commuteInstruction(MI);
257 }
258}
259
Chris Lattner7fbe9722006-10-20 17:42:20 +0000260static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
261 switch (BrOpc) {
262 default: return X86::COND_INVALID;
263 case X86::JE: return X86::COND_E;
264 case X86::JNE: return X86::COND_NE;
265 case X86::JL: return X86::COND_L;
266 case X86::JLE: return X86::COND_LE;
267 case X86::JG: return X86::COND_G;
268 case X86::JGE: return X86::COND_GE;
269 case X86::JB: return X86::COND_B;
270 case X86::JBE: return X86::COND_BE;
271 case X86::JA: return X86::COND_A;
272 case X86::JAE: return X86::COND_AE;
273 case X86::JS: return X86::COND_S;
274 case X86::JNS: return X86::COND_NS;
275 case X86::JP: return X86::COND_P;
276 case X86::JNP: return X86::COND_NP;
277 case X86::JO: return X86::COND_O;
278 case X86::JNO: return X86::COND_NO;
279 }
280}
281
282unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
283 switch (CC) {
284 default: assert(0 && "Illegal condition code!");
285 case X86::COND_E: return X86::JE;
286 case X86::COND_NE: return X86::JNE;
287 case X86::COND_L: return X86::JL;
288 case X86::COND_LE: return X86::JLE;
289 case X86::COND_G: return X86::JG;
290 case X86::COND_GE: return X86::JGE;
291 case X86::COND_B: return X86::JB;
292 case X86::COND_BE: return X86::JBE;
293 case X86::COND_A: return X86::JA;
294 case X86::COND_AE: return X86::JAE;
295 case X86::COND_S: return X86::JS;
296 case X86::COND_NS: return X86::JNS;
297 case X86::COND_P: return X86::JP;
298 case X86::COND_NP: return X86::JNP;
299 case X86::COND_O: return X86::JO;
300 case X86::COND_NO: return X86::JNO;
301 }
302}
303
Chris Lattner9cd68752006-10-21 05:52:40 +0000304/// GetOppositeBranchCondition - Return the inverse of the specified condition,
305/// e.g. turning COND_E to COND_NE.
306X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
307 switch (CC) {
308 default: assert(0 && "Illegal condition code!");
309 case X86::COND_E: return X86::COND_NE;
310 case X86::COND_NE: return X86::COND_E;
311 case X86::COND_L: return X86::COND_GE;
312 case X86::COND_LE: return X86::COND_G;
313 case X86::COND_G: return X86::COND_LE;
314 case X86::COND_GE: return X86::COND_L;
315 case X86::COND_B: return X86::COND_AE;
316 case X86::COND_BE: return X86::COND_A;
317 case X86::COND_A: return X86::COND_BE;
318 case X86::COND_AE: return X86::COND_B;
319 case X86::COND_S: return X86::COND_NS;
320 case X86::COND_NS: return X86::COND_S;
321 case X86::COND_P: return X86::COND_NP;
322 case X86::COND_NP: return X86::COND_P;
323 case X86::COND_O: return X86::COND_NO;
324 case X86::COND_NO: return X86::COND_O;
325 }
326}
327
328
Chris Lattner7fbe9722006-10-20 17:42:20 +0000329bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
330 MachineBasicBlock *&TBB,
331 MachineBasicBlock *&FBB,
332 std::vector<MachineOperand> &Cond) const {
333 // TODO: If FP_REG_KILL is around, ignore it.
334
335 // If the block has no terminators, it just falls into the block after it.
336 MachineBasicBlock::iterator I = MBB.end();
337 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
338 return false;
339
340 // Get the last instruction in the block.
341 MachineInstr *LastInst = I;
342
343 // If there is only one terminator instruction, process it.
344 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
345 if (!isBranch(LastInst->getOpcode()))
346 return true;
347
348 // If the block ends with a branch there are 3 possibilities:
349 // it's an unconditional, conditional, or indirect branch.
350
351 if (LastInst->getOpcode() == X86::JMP) {
352 TBB = LastInst->getOperand(0).getMachineBasicBlock();
353 return false;
354 }
355 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
356 if (BranchCode == X86::COND_INVALID)
357 return true; // Can't handle indirect branch.
358
359 // Otherwise, block ends with fall-through condbranch.
360 TBB = LastInst->getOperand(0).getMachineBasicBlock();
361 Cond.push_back(MachineOperand::CreateImm(BranchCode));
362 return false;
363 }
364
365 // Get the instruction before it if it's a terminator.
366 MachineInstr *SecondLastInst = I;
367
368 // If there are three terminators, we don't know what sort of block this is.
369 if (SecondLastInst && I != MBB.begin() &&
370 isTerminatorInstr((--I)->getOpcode()))
371 return true;
372
Chris Lattner6ce64432006-10-30 22:27:23 +0000373 // If the block ends with X86::JMP and a conditional branch, handle it.
Chris Lattner7fbe9722006-10-20 17:42:20 +0000374 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
375 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner6ce64432006-10-30 22:27:23 +0000376 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
377 Cond.push_back(MachineOperand::CreateImm(BranchCode));
378 FBB = LastInst->getOperand(0).getMachineBasicBlock();
379 return false;
380 }
Chris Lattner7fbe9722006-10-20 17:42:20 +0000381
382 // Otherwise, can't handle this.
383 return true;
384}
385
386void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
387 MachineBasicBlock::iterator I = MBB.end();
388 if (I == MBB.begin()) return;
389 --I;
390 if (I->getOpcode() != X86::JMP &&
391 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
392 return;
393
394 // Remove the branch.
395 I->eraseFromParent();
396
397 I = MBB.end();
398
399 if (I == MBB.begin()) return;
400 --I;
401 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
402 return;
403
404 // Remove the branch.
405 I->eraseFromParent();
406}
407
408void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
409 MachineBasicBlock *FBB,
410 const std::vector<MachineOperand> &Cond) const {
411 // Shouldn't be a fall through.
412 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner34a84ac2006-10-21 05:34:23 +0000413 assert((Cond.size() == 1 || Cond.size() == 0) &&
414 "X86 branch conditions have one component!");
415
416 if (FBB == 0) { // One way branch.
417 if (Cond.empty()) {
418 // Unconditional branch?
419 BuildMI(&MBB, X86::JMP, 1).addMBB(TBB);
420 } else {
421 // Conditional branch.
422 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
423 BuildMI(&MBB, Opc, 1).addMBB(TBB);
424 }
Chris Lattner7fbe9722006-10-20 17:42:20 +0000425 return;
426 }
427
Chris Lattner879d09c2006-10-21 05:42:09 +0000428 // Two-way Conditional branch.
Chris Lattner7fbe9722006-10-20 17:42:20 +0000429 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
430 BuildMI(&MBB, Opc, 1).addMBB(TBB);
Chris Lattner879d09c2006-10-21 05:42:09 +0000431 BuildMI(&MBB, X86::JMP, 1).addMBB(FBB);
Chris Lattner7fbe9722006-10-20 17:42:20 +0000432}
433
Chris Lattnerc24ff8e2006-10-28 17:29:57 +0000434bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
435 if (MBB.empty()) return false;
436
437 switch (MBB.back().getOpcode()) {
438 case X86::JMP: // Uncond branch.
439 case X86::JMP32r: // Indirect branch.
440 case X86::JMP32m: // Indirect branch through mem.
441 return true;
442 default: return false;
443 }
444}
445
Chris Lattner7fbe9722006-10-20 17:42:20 +0000446bool X86InstrInfo::
447ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner9cd68752006-10-21 05:52:40 +0000448 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
449 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
450 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +0000451}
452
Evan Cheng25ab6902006-09-08 06:48:29 +0000453const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
454 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
455 if (Subtarget->is64Bit())
456 return &X86::GR64RegClass;
457 else
458 return &X86::GR32RegClass;
459}