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 bool X86InstrInfo::isOtherReMaterializableLoad(MachineInstr *MI) const {
116 switch (MI->getOpcode()) {
129 case X86::MMX_MOVD64rm:
130 case X86::MMX_MOVQ64rm:
131 return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() &&
132 MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() &&
133 MI->getOperand(1).getReg() == 0 &&
134 MI->getOperand(2).getImmedValue() == 1 &&
135 MI->getOperand(3).getReg() == 0;
140 /// convertToThreeAddress - This method must be implemented by targets that
141 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
142 /// may be able to convert a two-address instruction into a true
143 /// three-address instruction on demand. This allows the X86 target (for
144 /// example) to convert ADD and SHL instructions into LEA instructions if they
145 /// would require register copies due to two-addressness.
147 /// This method returns a null pointer if the transformation cannot be
148 /// performed, otherwise it returns the new instruction.
151 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
152 MachineBasicBlock::iterator &MBBI,
153 LiveVariables &LV) const {
154 MachineInstr *MI = MBBI;
155 // All instructions input are two-addr instructions. Get the known operands.
156 unsigned Dest = MI->getOperand(0).getReg();
157 unsigned Src = MI->getOperand(1).getReg();
159 MachineInstr *NewMI = NULL;
160 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
161 // we have better subtarget support, enable the 16-bit LEA generation here.
162 bool DisableLEA16 = true;
164 switch (MI->getOpcode()) {
166 case X86::SHUFPSrri: {
167 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
168 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
170 unsigned A = MI->getOperand(0).getReg();
171 unsigned B = MI->getOperand(1).getReg();
172 unsigned C = MI->getOperand(2).getReg();
173 unsigned M = MI->getOperand(3).getImm();
174 if (B != C) return 0;
175 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
179 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
180 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
181 // the flags produced by a shift yet, so this is safe.
182 unsigned Dest = MI->getOperand(0).getReg();
183 unsigned Src = MI->getOperand(1).getReg();
184 unsigned ShAmt = MI->getOperand(2).getImm();
185 if (ShAmt == 0 || ShAmt >= 4) return 0;
187 NewMI = BuildMI(get(X86::LEA64r), Dest)
188 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
192 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
193 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
194 // the flags produced by a shift yet, so this is safe.
195 unsigned Dest = MI->getOperand(0).getReg();
196 unsigned Src = MI->getOperand(1).getReg();
197 unsigned ShAmt = MI->getOperand(2).getImm();
198 if (ShAmt == 0 || ShAmt >= 4) return 0;
200 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
201 X86::LEA64_32r : X86::LEA32r;
202 NewMI = BuildMI(get(Opc), Dest)
203 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
207 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
208 if (DisableLEA16) return 0;
210 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
211 // the flags produced by a shift yet, so this is safe.
212 unsigned Dest = MI->getOperand(0).getReg();
213 unsigned Src = MI->getOperand(1).getReg();
214 unsigned ShAmt = MI->getOperand(2).getImm();
215 if (ShAmt == 0 || ShAmt >= 4) return 0;
217 NewMI = BuildMI(get(X86::LEA16r), Dest)
218 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
223 // FIXME: None of these instructions are promotable to LEAs without
224 // additional information. In particular, LEA doesn't set the flags that
225 // add and inc do. :(
227 switch (MI->getOpcode()) {
230 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
231 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
235 if (DisableLEA16) return 0;
236 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
237 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
241 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
242 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
246 if (DisableLEA16) return 0;
247 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
248 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
251 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
252 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
253 MI->getOperand(2).getReg());
256 if (DisableLEA16) return 0;
257 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
258 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
259 MI->getOperand(2).getReg());
263 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
264 if (MI->getOperand(2).isImmediate())
265 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
266 MI->getOperand(2).getImmedValue());
270 if (DisableLEA16) return 0;
271 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
272 if (MI->getOperand(2).isImmediate())
273 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
274 MI->getOperand(2).getImmedValue());
277 if (DisableLEA16) return 0;
279 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
280 "Unknown shl instruction!");
281 unsigned ShAmt = MI->getOperand(2).getImmedValue();
282 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
284 AM.Scale = 1 << ShAmt;
286 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
287 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
293 NewMI->copyKillDeadInfo(MI);
294 LV.instructionChanged(MI, NewMI); // Update live variables
295 MFI->insert(MBBI, NewMI); // Insert the new inst
300 /// commuteInstruction - We have a few instructions that must be hacked on to
303 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
304 // FIXME: Can commute cmoves by changing the condition!
305 switch (MI->getOpcode()) {
306 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
307 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
308 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
309 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
312 switch (MI->getOpcode()) {
313 default: assert(0 && "Unreachable!");
314 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
315 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
316 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
317 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
319 unsigned Amt = MI->getOperand(3).getImmedValue();
320 unsigned A = MI->getOperand(0).getReg();
321 unsigned B = MI->getOperand(1).getReg();
322 unsigned C = MI->getOperand(2).getReg();
323 bool BisKill = MI->getOperand(1).isKill();
324 bool CisKill = MI->getOperand(2).isKill();
325 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
326 .addReg(B, false, false, BisKill).addImm(Size-Amt);
329 return TargetInstrInfo::commuteInstruction(MI);
333 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
335 default: return X86::COND_INVALID;
336 case X86::JE: return X86::COND_E;
337 case X86::JNE: return X86::COND_NE;
338 case X86::JL: return X86::COND_L;
339 case X86::JLE: return X86::COND_LE;
340 case X86::JG: return X86::COND_G;
341 case X86::JGE: return X86::COND_GE;
342 case X86::JB: return X86::COND_B;
343 case X86::JBE: return X86::COND_BE;
344 case X86::JA: return X86::COND_A;
345 case X86::JAE: return X86::COND_AE;
346 case X86::JS: return X86::COND_S;
347 case X86::JNS: return X86::COND_NS;
348 case X86::JP: return X86::COND_P;
349 case X86::JNP: return X86::COND_NP;
350 case X86::JO: return X86::COND_O;
351 case X86::JNO: return X86::COND_NO;
355 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
357 default: assert(0 && "Illegal condition code!");
358 case X86::COND_E: return X86::JE;
359 case X86::COND_NE: return X86::JNE;
360 case X86::COND_L: return X86::JL;
361 case X86::COND_LE: return X86::JLE;
362 case X86::COND_G: return X86::JG;
363 case X86::COND_GE: return X86::JGE;
364 case X86::COND_B: return X86::JB;
365 case X86::COND_BE: return X86::JBE;
366 case X86::COND_A: return X86::JA;
367 case X86::COND_AE: return X86::JAE;
368 case X86::COND_S: return X86::JS;
369 case X86::COND_NS: return X86::JNS;
370 case X86::COND_P: return X86::JP;
371 case X86::COND_NP: return X86::JNP;
372 case X86::COND_O: return X86::JO;
373 case X86::COND_NO: return X86::JNO;
377 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
378 /// e.g. turning COND_E to COND_NE.
379 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
381 default: assert(0 && "Illegal condition code!");
382 case X86::COND_E: return X86::COND_NE;
383 case X86::COND_NE: return X86::COND_E;
384 case X86::COND_L: return X86::COND_GE;
385 case X86::COND_LE: return X86::COND_G;
386 case X86::COND_G: return X86::COND_LE;
387 case X86::COND_GE: return X86::COND_L;
388 case X86::COND_B: return X86::COND_AE;
389 case X86::COND_BE: return X86::COND_A;
390 case X86::COND_A: return X86::COND_BE;
391 case X86::COND_AE: return X86::COND_B;
392 case X86::COND_S: return X86::COND_NS;
393 case X86::COND_NS: return X86::COND_S;
394 case X86::COND_P: return X86::COND_NP;
395 case X86::COND_NP: return X86::COND_P;
396 case X86::COND_O: return X86::COND_NO;
397 case X86::COND_NO: return X86::COND_O;
401 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
402 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
403 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
404 if (MI->getOpcode() == X86::FP_REG_KILL)
406 if (TID->Flags & M_TERMINATOR_FLAG)
407 return !isPredicated(MI);
411 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
412 MachineBasicBlock *&TBB,
413 MachineBasicBlock *&FBB,
414 std::vector<MachineOperand> &Cond) const {
415 // If the block has no terminators, it just falls into the block after it.
416 MachineBasicBlock::iterator I = MBB.end();
417 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
420 // Get the last instruction in the block.
421 MachineInstr *LastInst = I;
423 // If there is only one terminator instruction, process it.
424 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
425 if (!isBranch(LastInst->getOpcode()))
428 // If the block ends with a branch there are 3 possibilities:
429 // it's an unconditional, conditional, or indirect branch.
431 if (LastInst->getOpcode() == X86::JMP) {
432 TBB = LastInst->getOperand(0).getMachineBasicBlock();
435 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
436 if (BranchCode == X86::COND_INVALID)
437 return true; // Can't handle indirect branch.
439 // Otherwise, block ends with fall-through condbranch.
440 TBB = LastInst->getOperand(0).getMachineBasicBlock();
441 Cond.push_back(MachineOperand::CreateImm(BranchCode));
445 // Get the instruction before it if it's a terminator.
446 MachineInstr *SecondLastInst = I;
448 // If there are three terminators, we don't know what sort of block this is.
449 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
452 // If the block ends with X86::JMP and a conditional branch, handle it.
453 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
454 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
455 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
456 Cond.push_back(MachineOperand::CreateImm(BranchCode));
457 FBB = LastInst->getOperand(0).getMachineBasicBlock();
461 // If the block ends with two X86::JMPs, handle it. The second one is not
462 // executed, so remove it.
463 if (SecondLastInst->getOpcode() == X86::JMP &&
464 LastInst->getOpcode() == X86::JMP) {
465 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
467 I->eraseFromParent();
471 // Otherwise, can't handle this.
475 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
476 MachineBasicBlock::iterator I = MBB.end();
477 if (I == MBB.begin()) return 0;
479 if (I->getOpcode() != X86::JMP &&
480 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
483 // Remove the branch.
484 I->eraseFromParent();
488 if (I == MBB.begin()) return 1;
490 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
493 // Remove the branch.
494 I->eraseFromParent();
499 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
500 MachineBasicBlock *FBB,
501 const std::vector<MachineOperand> &Cond) const {
502 // Shouldn't be a fall through.
503 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
504 assert((Cond.size() == 1 || Cond.size() == 0) &&
505 "X86 branch conditions have one component!");
507 if (FBB == 0) { // One way branch.
509 // Unconditional branch?
510 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
512 // Conditional branch.
513 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
514 BuildMI(&MBB, get(Opc)).addMBB(TBB);
519 // Two-way Conditional branch.
520 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
521 BuildMI(&MBB, get(Opc)).addMBB(TBB);
522 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
526 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
527 if (MBB.empty()) return false;
529 switch (MBB.back().getOpcode()) {
530 case X86::RET: // Return.
535 case X86::JMP: // Uncond branch.
536 case X86::JMP32r: // Indirect branch.
537 case X86::JMP32m: // Indirect branch through mem.
539 default: return false;
544 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
545 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
546 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
550 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
551 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
552 if (Subtarget->is64Bit())
553 return &X86::GR64RegClass;
555 return &X86::GR32RegClass;