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"
21 #include "llvm/CodeGen/LiveVariables.h"
24 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
26 TM(tm), RI(tm, *this) {
29 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
31 unsigned& destReg) const {
32 MachineOpCode oc = MI.getOpcode();
33 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
34 oc == X86::MOV32rr || oc == X86::MOV64rr ||
35 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
36 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
37 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
38 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
39 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
40 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
41 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
42 assert(MI.getNumOperands() >= 2 &&
43 MI.getOperand(0).isRegister() &&
44 MI.getOperand(1).isRegister() &&
45 "invalid register-register move instruction");
46 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
53 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
54 int &FrameIndex) const {
55 switch (MI->getOpcode()) {
68 case X86::MMX_MOVD64rm:
69 case X86::MMX_MOVQ64rm:
70 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();
83 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
84 int &FrameIndex) const {
85 switch (MI->getOpcode()) {
98 case X86::MMX_MOVD64mr:
99 case X86::MMX_MOVQ64mr:
100 case X86::MMX_MOVNTQmr:
101 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
102 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
103 MI->getOperand(1).getImmedValue() == 1 &&
104 MI->getOperand(2).getReg() == 0 &&
105 MI->getOperand(3).getImmedValue() == 0) {
106 FrameIndex = MI->getOperand(0).getFrameIndex();
107 return MI->getOperand(4).getReg();
115 /// convertToThreeAddress - This method must be implemented by targets that
116 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
117 /// may be able to convert a two-address instruction into a true
118 /// three-address instruction on demand. This allows the X86 target (for
119 /// example) to convert ADD and SHL instructions into LEA instructions if they
120 /// would require register copies due to two-addressness.
122 /// This method returns a null pointer if the transformation cannot be
123 /// performed, otherwise it returns the new instruction.
126 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
127 MachineBasicBlock::iterator &MBBI,
128 LiveVariables &LV) const {
129 MachineInstr *MI = MBBI;
130 // All instructions input are two-addr instructions. Get the known operands.
131 unsigned Dest = MI->getOperand(0).getReg();
132 unsigned Src = MI->getOperand(1).getReg();
134 MachineInstr *NewMI = NULL;
135 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
136 // we have better subtarget support, enable the 16-bit LEA generation here.
137 bool DisableLEA16 = true;
139 switch (MI->getOpcode()) {
141 case X86::SHUFPSrri: {
142 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
143 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
145 unsigned A = MI->getOperand(0).getReg();
146 unsigned B = MI->getOperand(1).getReg();
147 unsigned C = MI->getOperand(2).getReg();
148 unsigned M = MI->getOperand(3).getImm();
149 if (B != C) return 0;
150 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
154 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
155 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
156 // the flags produced by a shift yet, so this is safe.
157 unsigned Dest = MI->getOperand(0).getReg();
158 unsigned Src = MI->getOperand(1).getReg();
159 unsigned ShAmt = MI->getOperand(2).getImm();
160 if (ShAmt == 0 || ShAmt >= 4) return 0;
162 NewMI = BuildMI(get(X86::LEA64r), Dest)
163 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
167 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
168 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
169 // the flags produced by a shift yet, so this is safe.
170 unsigned Dest = MI->getOperand(0).getReg();
171 unsigned Src = MI->getOperand(1).getReg();
172 unsigned ShAmt = MI->getOperand(2).getImm();
173 if (ShAmt == 0 || ShAmt >= 4) return 0;
175 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
176 X86::LEA64_32r : X86::LEA32r;
177 NewMI = BuildMI(get(Opc), Dest)
178 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
182 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
183 if (DisableLEA16) return 0;
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;
192 NewMI = BuildMI(get(X86::LEA16r), Dest)
193 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
198 // FIXME: None of these instructions are promotable to LEAs without
199 // additional information. In particular, LEA doesn't set the flags that
200 // add and inc do. :(
202 switch (MI->getOpcode()) {
205 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
206 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
210 if (DisableLEA16) return 0;
211 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
212 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
216 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
217 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
221 if (DisableLEA16) return 0;
222 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
223 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
226 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
227 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
228 MI->getOperand(2).getReg());
231 if (DisableLEA16) return 0;
232 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
233 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
234 MI->getOperand(2).getReg());
238 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
239 if (MI->getOperand(2).isImmediate())
240 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
241 MI->getOperand(2).getImmedValue());
245 if (DisableLEA16) return 0;
246 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
247 if (MI->getOperand(2).isImmediate())
248 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
249 MI->getOperand(2).getImmedValue());
252 if (DisableLEA16) return 0;
254 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
255 "Unknown shl instruction!");
256 unsigned ShAmt = MI->getOperand(2).getImmedValue();
257 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
259 AM.Scale = 1 << ShAmt;
261 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
262 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
268 NewMI->copyKillDeadInfo(MI);
269 LV.instructionChanged(MI, NewMI); // Update live variables
270 MFI->insert(MBBI, NewMI); // Insert the new inst
275 /// commuteInstruction - We have a few instructions that must be hacked on to
278 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
279 // FIXME: Can commute cmoves by changing the condition!
280 switch (MI->getOpcode()) {
281 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
282 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
283 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
284 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
287 switch (MI->getOpcode()) {
288 default: assert(0 && "Unreachable!");
289 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
290 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
291 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
292 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
294 unsigned Amt = MI->getOperand(3).getImmedValue();
295 unsigned A = MI->getOperand(0).getReg();
296 unsigned B = MI->getOperand(1).getReg();
297 unsigned C = MI->getOperand(2).getReg();
298 bool BisKill = MI->getOperand(1).isKill();
299 bool CisKill = MI->getOperand(2).isKill();
300 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
301 .addReg(B, false, false, BisKill).addImm(Size-Amt);
304 return TargetInstrInfo::commuteInstruction(MI);
308 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
310 default: return X86::COND_INVALID;
311 case X86::JE: return X86::COND_E;
312 case X86::JNE: return X86::COND_NE;
313 case X86::JL: return X86::COND_L;
314 case X86::JLE: return X86::COND_LE;
315 case X86::JG: return X86::COND_G;
316 case X86::JGE: return X86::COND_GE;
317 case X86::JB: return X86::COND_B;
318 case X86::JBE: return X86::COND_BE;
319 case X86::JA: return X86::COND_A;
320 case X86::JAE: return X86::COND_AE;
321 case X86::JS: return X86::COND_S;
322 case X86::JNS: return X86::COND_NS;
323 case X86::JP: return X86::COND_P;
324 case X86::JNP: return X86::COND_NP;
325 case X86::JO: return X86::COND_O;
326 case X86::JNO: return X86::COND_NO;
330 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
332 default: assert(0 && "Illegal condition code!");
333 case X86::COND_E: return X86::JE;
334 case X86::COND_NE: return X86::JNE;
335 case X86::COND_L: return X86::JL;
336 case X86::COND_LE: return X86::JLE;
337 case X86::COND_G: return X86::JG;
338 case X86::COND_GE: return X86::JGE;
339 case X86::COND_B: return X86::JB;
340 case X86::COND_BE: return X86::JBE;
341 case X86::COND_A: return X86::JA;
342 case X86::COND_AE: return X86::JAE;
343 case X86::COND_S: return X86::JS;
344 case X86::COND_NS: return X86::JNS;
345 case X86::COND_P: return X86::JP;
346 case X86::COND_NP: return X86::JNP;
347 case X86::COND_O: return X86::JO;
348 case X86::COND_NO: return X86::JNO;
352 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
353 /// e.g. turning COND_E to COND_NE.
354 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
356 default: assert(0 && "Illegal condition code!");
357 case X86::COND_E: return X86::COND_NE;
358 case X86::COND_NE: return X86::COND_E;
359 case X86::COND_L: return X86::COND_GE;
360 case X86::COND_LE: return X86::COND_G;
361 case X86::COND_G: return X86::COND_LE;
362 case X86::COND_GE: return X86::COND_L;
363 case X86::COND_B: return X86::COND_AE;
364 case X86::COND_BE: return X86::COND_A;
365 case X86::COND_A: return X86::COND_BE;
366 case X86::COND_AE: return X86::COND_B;
367 case X86::COND_S: return X86::COND_NS;
368 case X86::COND_NS: return X86::COND_S;
369 case X86::COND_P: return X86::COND_NP;
370 case X86::COND_NP: return X86::COND_P;
371 case X86::COND_O: return X86::COND_NO;
372 case X86::COND_NO: return X86::COND_O;
377 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
378 MachineBasicBlock *&TBB,
379 MachineBasicBlock *&FBB,
380 std::vector<MachineOperand> &Cond) const {
381 // TODO: If FP_REG_KILL is around, ignore it.
383 // If the block has no terminators, it just falls into the block after it.
384 MachineBasicBlock::iterator I = MBB.end();
385 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
388 // Get the last instruction in the block.
389 MachineInstr *LastInst = I;
391 // If there is only one terminator instruction, process it.
392 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
393 if (!isBranch(LastInst->getOpcode()))
396 // If the block ends with a branch there are 3 possibilities:
397 // it's an unconditional, conditional, or indirect branch.
399 if (LastInst->getOpcode() == X86::JMP) {
400 TBB = LastInst->getOperand(0).getMachineBasicBlock();
403 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
404 if (BranchCode == X86::COND_INVALID)
405 return true; // Can't handle indirect branch.
407 // Otherwise, block ends with fall-through condbranch.
408 TBB = LastInst->getOperand(0).getMachineBasicBlock();
409 Cond.push_back(MachineOperand::CreateImm(BranchCode));
413 // Get the instruction before it if it's a terminator.
414 MachineInstr *SecondLastInst = I;
416 // If there are three terminators, we don't know what sort of block this is.
417 if (SecondLastInst && I != MBB.begin() &&
418 isTerminatorInstr((--I)->getOpcode()))
421 // If the block ends with X86::JMP and a conditional branch, handle it.
422 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
423 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
424 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
425 Cond.push_back(MachineOperand::CreateImm(BranchCode));
426 FBB = LastInst->getOperand(0).getMachineBasicBlock();
430 // Otherwise, can't handle this.
434 void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
435 MachineBasicBlock::iterator I = MBB.end();
436 if (I == MBB.begin()) return;
438 if (I->getOpcode() != X86::JMP &&
439 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
442 // Remove the branch.
443 I->eraseFromParent();
447 if (I == MBB.begin()) return;
449 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
452 // Remove the branch.
453 I->eraseFromParent();
456 void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
457 MachineBasicBlock *FBB,
458 const std::vector<MachineOperand> &Cond) const {
459 // Shouldn't be a fall through.
460 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
461 assert((Cond.size() == 1 || Cond.size() == 0) &&
462 "X86 branch conditions have one component!");
464 if (FBB == 0) { // One way branch.
466 // Unconditional branch?
467 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
469 // Conditional branch.
470 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
471 BuildMI(&MBB, get(Opc)).addMBB(TBB);
476 // Two-way Conditional branch.
477 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
478 BuildMI(&MBB, get(Opc)).addMBB(TBB);
479 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
482 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
483 if (MBB.empty()) return false;
485 switch (MBB.back().getOpcode()) {
486 case X86::JMP: // Uncond branch.
487 case X86::JMP32r: // Indirect branch.
488 case X86::JMP32m: // Indirect branch through mem.
490 default: return false;
495 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
496 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
497 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
501 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
502 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
503 if (Subtarget->is64Bit())
504 return &X86::GR64RegClass;
506 return &X86::GR32RegClass;