1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the X86 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "X86InstrInfo.h"
16 #include "X86GenInstrInfo.inc"
17 #include "X86InstrBuilder.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
24 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
25 TM(tm), RI(tm, *this) {
28 /// 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.
31 unsigned X86InstrInfo::getDWARF_LABELOpcode() const {
32 return X86::DWARF_LABEL;
36 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
38 unsigned& destReg) const {
39 MachineOpCode oc = MI.getOpcode();
40 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
41 oc == X86::MOV32rr || oc == X86::MOV64rr ||
42 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
43 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
44 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
45 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
46 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
47 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
48 oc == X86::MOVDI2PDIrr || oc == X86::MOVQI2PQIrr ||
49 oc == X86::MOVPDI2DIrr) {
50 assert(MI.getNumOperands() == 2 &&
51 MI.getOperand(0).isRegister() &&
52 MI.getOperand(1).isRegister() &&
53 "invalid register-register move instruction");
54 sourceReg = MI.getOperand(1).getReg();
55 destReg = MI.getOperand(0).getReg();
61 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
62 int &FrameIndex) const {
63 switch (MI->getOpcode()) {
76 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
77 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
78 MI->getOperand(2).getImmedValue() == 1 &&
79 MI->getOperand(3).getReg() == 0 &&
80 MI->getOperand(4).getImmedValue() == 0) {
81 FrameIndex = MI->getOperand(1).getFrameIndex();
82 return MI->getOperand(0).getReg();
89 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
90 int &FrameIndex) const {
91 switch (MI->getOpcode()) {
104 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
105 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
106 MI->getOperand(1).getImmedValue() == 1 &&
107 MI->getOperand(2).getReg() == 0 &&
108 MI->getOperand(3).getImmedValue() == 0) {
109 FrameIndex = MI->getOperand(0).getFrameIndex();
110 return MI->getOperand(4).getReg();
118 /// convertToThreeAddress - This method must be implemented by targets that
119 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
120 /// may be able to convert a two-address instruction into a true
121 /// three-address instruction on demand. This allows the X86 target (for
122 /// example) to convert ADD and SHL instructions into LEA instructions if they
123 /// would require register copies due to two-addressness.
125 /// This method returns a null pointer if the transformation cannot be
126 /// performed, otherwise it returns the new instruction.
128 MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
129 // 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();
133 MachineInstr *NewMI = NULL;
134 switch (MI->getOpcode()) {
136 case X86::SHUFPSrri: {
137 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 unsigned A = MI->getOperand(0).getReg();
140 unsigned B = MI->getOperand(1).getReg();
141 unsigned C = MI->getOperand(2).getReg();
142 unsigned M = MI->getOperand(3).getImmedValue();
143 if (!Subtarget->hasSSE2() || B != C) return 0;
144 NewMI = BuildMI(*this, X86::PSHUFDri, 2, A).addReg(B).addImm(M);
145 NewMI->copyKillDeadInfo(MI);
150 // FIXME: None of these instructions are promotable to LEAs without
151 // additional information. In particular, LEA doesn't set the flags that
152 // add and inc do. :(
155 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
156 // we have subtarget support, enable the 16-bit LEA generation here.
157 bool DisableLEA16 = true;
159 switch (MI->getOpcode()) {
162 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
163 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, 1);
167 if (DisableLEA16) return 0;
168 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
169 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, 1);
173 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
174 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, -1);
178 if (DisableLEA16) return 0;
179 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
180 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, -1);
183 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
184 NewMI = addRegReg(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
185 MI->getOperand(2).getReg());
188 if (DisableLEA16) return 0;
189 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
190 NewMI = addRegReg(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
191 MI->getOperand(2).getReg());
195 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
196 if (MI->getOperand(2).isImmediate())
197 NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
198 MI->getOperand(2).getImmedValue());
202 if (DisableLEA16) return 0;
203 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
204 if (MI->getOperand(2).isImmediate())
205 NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
206 MI->getOperand(2).getImmedValue());
209 if (DisableLEA16) return 0;
211 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
212 "Unknown shl instruction!");
213 unsigned ShAmt = MI->getOperand(2).getImmedValue();
214 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
216 AM.Scale = 1 << ShAmt;
218 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
219 NewMI = addFullAddress(BuildMI(*this, Opc, 5, Dest), AM);
225 NewMI->copyKillDeadInfo(MI);
229 /// commuteInstruction - We have a few instructions that must be hacked on to
232 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
233 // FIXME: Can commute cmoves by changing the condition!
234 switch (MI->getOpcode()) {
235 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
236 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
237 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
238 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
241 switch (MI->getOpcode()) {
242 default: assert(0 && "Unreachable!");
243 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
244 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
245 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
246 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
248 unsigned Amt = MI->getOperand(3).getImmedValue();
249 unsigned A = MI->getOperand(0).getReg();
250 unsigned B = MI->getOperand(1).getReg();
251 unsigned C = MI->getOperand(2).getReg();
252 bool BisKill = MI->getOperand(1).isKill();
253 bool CisKill = MI->getOperand(2).isKill();
254 return BuildMI(*this, Opc, 3, A).addReg(C, false, false, CisKill)
255 .addReg(B, false, false, BisKill).addImm(Size-Amt);
258 return TargetInstrInfo::commuteInstruction(MI);
262 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
264 default: return X86::COND_INVALID;
265 case X86::JE: return X86::COND_E;
266 case X86::JNE: return X86::COND_NE;
267 case X86::JL: return X86::COND_L;
268 case X86::JLE: return X86::COND_LE;
269 case X86::JG: return X86::COND_G;
270 case X86::JGE: return X86::COND_GE;
271 case X86::JB: return X86::COND_B;
272 case X86::JBE: return X86::COND_BE;
273 case X86::JA: return X86::COND_A;
274 case X86::JAE: return X86::COND_AE;
275 case X86::JS: return X86::COND_S;
276 case X86::JNS: return X86::COND_NS;
277 case X86::JP: return X86::COND_P;
278 case X86::JNP: return X86::COND_NP;
279 case X86::JO: return X86::COND_O;
280 case X86::JNO: return X86::COND_NO;
284 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
286 default: assert(0 && "Illegal condition code!");
287 case X86::COND_E: return X86::JE;
288 case X86::COND_NE: return X86::JNE;
289 case X86::COND_L: return X86::JL;
290 case X86::COND_LE: return X86::JLE;
291 case X86::COND_G: return X86::JG;
292 case X86::COND_GE: return X86::JGE;
293 case X86::COND_B: return X86::JB;
294 case X86::COND_BE: return X86::JBE;
295 case X86::COND_A: return X86::JA;
296 case X86::COND_AE: return X86::JAE;
297 case X86::COND_S: return X86::JS;
298 case X86::COND_NS: return X86::JNS;
299 case X86::COND_P: return X86::JP;
300 case X86::COND_NP: return X86::JNP;
301 case X86::COND_O: return X86::JO;
302 case X86::COND_NO: return X86::JNO;
306 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
307 /// e.g. turning COND_E to COND_NE.
308 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
310 default: assert(0 && "Illegal condition code!");
311 case X86::COND_E: return X86::COND_NE;
312 case X86::COND_NE: return X86::COND_E;
313 case X86::COND_L: return X86::COND_GE;
314 case X86::COND_LE: return X86::COND_G;
315 case X86::COND_G: return X86::COND_LE;
316 case X86::COND_GE: return X86::COND_L;
317 case X86::COND_B: return X86::COND_AE;
318 case X86::COND_BE: return X86::COND_A;
319 case X86::COND_A: return X86::COND_BE;
320 case X86::COND_AE: return X86::COND_B;
321 case X86::COND_S: return X86::COND_NS;
322 case X86::COND_NS: return X86::COND_S;
323 case X86::COND_P: return X86::COND_NP;
324 case X86::COND_NP: return X86::COND_P;
325 case X86::COND_O: return X86::COND_NO;
326 case X86::COND_NO: return X86::COND_O;
331 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
332 MachineBasicBlock *&TBB,
333 MachineBasicBlock *&FBB,
334 std::vector<MachineOperand> &Cond) const {
335 // TODO: If FP_REG_KILL is around, ignore it.
337 // If the block has no terminators, it just falls into the block after it.
338 MachineBasicBlock::iterator I = MBB.end();
339 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
342 // Get the last instruction in the block.
343 MachineInstr *LastInst = I;
345 // If there is only one terminator instruction, process it.
346 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
347 if (!isBranch(LastInst->getOpcode()))
350 // If the block ends with a branch there are 3 possibilities:
351 // it's an unconditional, conditional, or indirect branch.
353 if (LastInst->getOpcode() == X86::JMP) {
354 TBB = LastInst->getOperand(0).getMachineBasicBlock();
357 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
358 if (BranchCode == X86::COND_INVALID)
359 return true; // Can't handle indirect branch.
361 // Otherwise, block ends with fall-through condbranch.
362 TBB = LastInst->getOperand(0).getMachineBasicBlock();
363 Cond.push_back(MachineOperand::CreateImm(BranchCode));
367 // Get the instruction before it if it's a terminator.
368 MachineInstr *SecondLastInst = I;
370 // If there are three terminators, we don't know what sort of block this is.
371 if (SecondLastInst && I != MBB.begin() &&
372 isTerminatorInstr((--I)->getOpcode()))
375 // If the block ends with X86::JMP and a conditional branch, handle it.
376 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
377 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
378 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
379 Cond.push_back(MachineOperand::CreateImm(BranchCode));
380 FBB = LastInst->getOperand(0).getMachineBasicBlock();
384 // Otherwise, can't handle this.
388 void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
389 MachineBasicBlock::iterator I = MBB.end();
390 if (I == MBB.begin()) return;
392 if (I->getOpcode() != X86::JMP &&
393 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
396 // Remove the branch.
397 I->eraseFromParent();
401 if (I == MBB.begin()) return;
403 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
406 // Remove the branch.
407 I->eraseFromParent();
410 void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
411 MachineBasicBlock *FBB,
412 const std::vector<MachineOperand> &Cond) const {
413 // Shouldn't be a fall through.
414 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
415 assert((Cond.size() == 1 || Cond.size() == 0) &&
416 "X86 branch conditions have one component!");
418 if (FBB == 0) { // One way branch.
420 // Unconditional branch?
421 BuildMI(&MBB, X86::JMP, 1).addMBB(TBB);
423 // Conditional branch.
424 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
425 BuildMI(&MBB, Opc, 1).addMBB(TBB);
430 // Two-way Conditional branch.
431 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
432 BuildMI(&MBB, Opc, 1).addMBB(TBB);
433 BuildMI(&MBB, X86::JMP, 1).addMBB(FBB);
436 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
437 if (MBB.empty()) return false;
439 switch (MBB.back().getOpcode()) {
440 case X86::JMP: // Uncond branch.
441 case X86::JMP32r: // Indirect branch.
442 case X86::JMP32m: // Indirect branch through mem.
444 default: return false;
449 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
450 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
451 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
455 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
456 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
457 if (Subtarget->is64Bit())
458 return &X86::GR64RegClass;
460 return &X86::GR32RegClass;