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 oc == X86::MMX_MOVDQ2Qrr || oc == X86::MMX_MOVQ2DQrr) {
43 assert(MI.getNumOperands() == 2 &&
44 MI.getOperand(0).isRegister() &&
45 MI.getOperand(1).isRegister() &&
46 "invalid register-register move instruction");
47 sourceReg = MI.getOperand(1).getReg();
48 destReg = MI.getOperand(0).getReg();
54 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
55 int &FrameIndex) const {
56 switch (MI->getOpcode()) {
69 case X86::MMX_MOVD64rm:
70 case X86::MMX_MOVQ64rm:
71 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
72 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
73 MI->getOperand(2).getImmedValue() == 1 &&
74 MI->getOperand(3).getReg() == 0 &&
75 MI->getOperand(4).getImmedValue() == 0) {
76 FrameIndex = MI->getOperand(1).getFrameIndex();
77 return MI->getOperand(0).getReg();
84 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
85 int &FrameIndex) const {
86 switch (MI->getOpcode()) {
99 case X86::MMX_MOVD64mr:
100 case X86::MMX_MOVQ64mr:
101 case X86::MMX_MOVNTQmr:
102 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
103 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
104 MI->getOperand(1).getImmedValue() == 1 &&
105 MI->getOperand(2).getReg() == 0 &&
106 MI->getOperand(3).getImmedValue() == 0) {
107 FrameIndex = MI->getOperand(0).getFrameIndex();
108 return MI->getOperand(4).getReg();
116 /// 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.
123 /// This method returns a null pointer if the transformation cannot be
124 /// performed, otherwise it returns the new instruction.
127 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
128 MachineBasicBlock::iterator &MBBI,
129 LiveVariables &LV) const {
130 MachineInstr *MI = MBBI;
131 // All instructions input are two-addr instructions. Get the known operands.
132 unsigned Dest = MI->getOperand(0).getReg();
133 unsigned Src = MI->getOperand(1).getReg();
135 MachineInstr *NewMI = NULL;
136 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
137 // we have better subtarget support, enable the 16-bit LEA generation here.
138 bool DisableLEA16 = true;
140 switch (MI->getOpcode()) {
142 case X86::SHUFPSrri: {
143 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
144 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
146 unsigned A = MI->getOperand(0).getReg();
147 unsigned B = MI->getOperand(1).getReg();
148 unsigned C = MI->getOperand(2).getReg();
149 unsigned M = MI->getOperand(3).getImm();
150 if (B != C) return 0;
151 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
155 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
156 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
157 // the flags produced by a shift yet, so this is safe.
158 unsigned Dest = MI->getOperand(0).getReg();
159 unsigned Src = MI->getOperand(1).getReg();
160 unsigned ShAmt = MI->getOperand(2).getImm();
161 if (ShAmt == 0 || ShAmt >= 4) return 0;
163 NewMI = BuildMI(get(X86::LEA64r), Dest)
164 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
168 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
169 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
170 // the flags produced by a shift yet, so this is safe.
171 unsigned Dest = MI->getOperand(0).getReg();
172 unsigned Src = MI->getOperand(1).getReg();
173 unsigned ShAmt = MI->getOperand(2).getImm();
174 if (ShAmt == 0 || ShAmt >= 4) return 0;
176 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
177 X86::LEA64_32r : X86::LEA32r;
178 NewMI = BuildMI(get(Opc), Dest)
179 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
183 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
184 if (DisableLEA16) return 0;
186 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
187 // the flags produced by a shift yet, so this is safe.
188 unsigned Dest = MI->getOperand(0).getReg();
189 unsigned Src = MI->getOperand(1).getReg();
190 unsigned ShAmt = MI->getOperand(2).getImm();
191 if (ShAmt == 0 || ShAmt >= 4) return 0;
193 NewMI = BuildMI(get(X86::LEA16r), Dest)
194 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
199 // FIXME: None of these instructions are promotable to LEAs without
200 // additional information. In particular, LEA doesn't set the flags that
201 // add and inc do. :(
203 switch (MI->getOpcode()) {
206 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
207 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
211 if (DisableLEA16) return 0;
212 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
213 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
217 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
218 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
222 if (DisableLEA16) return 0;
223 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
224 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
227 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
228 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
229 MI->getOperand(2).getReg());
232 if (DisableLEA16) return 0;
233 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
234 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
235 MI->getOperand(2).getReg());
239 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
240 if (MI->getOperand(2).isImmediate())
241 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
242 MI->getOperand(2).getImmedValue());
246 if (DisableLEA16) return 0;
247 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
248 if (MI->getOperand(2).isImmediate())
249 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
250 MI->getOperand(2).getImmedValue());
253 if (DisableLEA16) return 0;
255 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
256 "Unknown shl instruction!");
257 unsigned ShAmt = MI->getOperand(2).getImmedValue();
258 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
260 AM.Scale = 1 << ShAmt;
262 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
263 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
269 NewMI->copyKillDeadInfo(MI);
270 LV.instructionChanged(MI, NewMI); // Update live variables
271 MFI->insert(MBBI, NewMI); // Insert the new inst
276 /// commuteInstruction - We have a few instructions that must be hacked on to
279 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
280 // FIXME: Can commute cmoves by changing the condition!
281 switch (MI->getOpcode()) {
282 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
283 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
284 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
285 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
288 switch (MI->getOpcode()) {
289 default: assert(0 && "Unreachable!");
290 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
291 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
292 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
293 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
295 unsigned Amt = MI->getOperand(3).getImmedValue();
296 unsigned A = MI->getOperand(0).getReg();
297 unsigned B = MI->getOperand(1).getReg();
298 unsigned C = MI->getOperand(2).getReg();
299 bool BisKill = MI->getOperand(1).isKill();
300 bool CisKill = MI->getOperand(2).isKill();
301 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
302 .addReg(B, false, false, BisKill).addImm(Size-Amt);
305 return TargetInstrInfo::commuteInstruction(MI);
309 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
311 default: return X86::COND_INVALID;
312 case X86::JE: return X86::COND_E;
313 case X86::JNE: return X86::COND_NE;
314 case X86::JL: return X86::COND_L;
315 case X86::JLE: return X86::COND_LE;
316 case X86::JG: return X86::COND_G;
317 case X86::JGE: return X86::COND_GE;
318 case X86::JB: return X86::COND_B;
319 case X86::JBE: return X86::COND_BE;
320 case X86::JA: return X86::COND_A;
321 case X86::JAE: return X86::COND_AE;
322 case X86::JS: return X86::COND_S;
323 case X86::JNS: return X86::COND_NS;
324 case X86::JP: return X86::COND_P;
325 case X86::JNP: return X86::COND_NP;
326 case X86::JO: return X86::COND_O;
327 case X86::JNO: return X86::COND_NO;
331 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
333 default: assert(0 && "Illegal condition code!");
334 case X86::COND_E: return X86::JE;
335 case X86::COND_NE: return X86::JNE;
336 case X86::COND_L: return X86::JL;
337 case X86::COND_LE: return X86::JLE;
338 case X86::COND_G: return X86::JG;
339 case X86::COND_GE: return X86::JGE;
340 case X86::COND_B: return X86::JB;
341 case X86::COND_BE: return X86::JBE;
342 case X86::COND_A: return X86::JA;
343 case X86::COND_AE: return X86::JAE;
344 case X86::COND_S: return X86::JS;
345 case X86::COND_NS: return X86::JNS;
346 case X86::COND_P: return X86::JP;
347 case X86::COND_NP: return X86::JNP;
348 case X86::COND_O: return X86::JO;
349 case X86::COND_NO: return X86::JNO;
353 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
354 /// e.g. turning COND_E to COND_NE.
355 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
357 default: assert(0 && "Illegal condition code!");
358 case X86::COND_E: return X86::COND_NE;
359 case X86::COND_NE: return X86::COND_E;
360 case X86::COND_L: return X86::COND_GE;
361 case X86::COND_LE: return X86::COND_G;
362 case X86::COND_G: return X86::COND_LE;
363 case X86::COND_GE: return X86::COND_L;
364 case X86::COND_B: return X86::COND_AE;
365 case X86::COND_BE: return X86::COND_A;
366 case X86::COND_A: return X86::COND_BE;
367 case X86::COND_AE: return X86::COND_B;
368 case X86::COND_S: return X86::COND_NS;
369 case X86::COND_NS: return X86::COND_S;
370 case X86::COND_P: return X86::COND_NP;
371 case X86::COND_NP: return X86::COND_P;
372 case X86::COND_O: return X86::COND_NO;
373 case X86::COND_NO: return X86::COND_O;
378 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
379 MachineBasicBlock *&TBB,
380 MachineBasicBlock *&FBB,
381 std::vector<MachineOperand> &Cond) const {
382 // TODO: If FP_REG_KILL is around, ignore it.
384 // If the block has no terminators, it just falls into the block after it.
385 MachineBasicBlock::iterator I = MBB.end();
386 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
389 // Get the last instruction in the block.
390 MachineInstr *LastInst = I;
392 // If there is only one terminator instruction, process it.
393 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
394 if (!isBranch(LastInst->getOpcode()))
397 // If the block ends with a branch there are 3 possibilities:
398 // it's an unconditional, conditional, or indirect branch.
400 if (LastInst->getOpcode() == X86::JMP) {
401 TBB = LastInst->getOperand(0).getMachineBasicBlock();
404 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
405 if (BranchCode == X86::COND_INVALID)
406 return true; // Can't handle indirect branch.
408 // Otherwise, block ends with fall-through condbranch.
409 TBB = LastInst->getOperand(0).getMachineBasicBlock();
410 Cond.push_back(MachineOperand::CreateImm(BranchCode));
414 // Get the instruction before it if it's a terminator.
415 MachineInstr *SecondLastInst = I;
417 // If there are three terminators, we don't know what sort of block this is.
418 if (SecondLastInst && I != MBB.begin() &&
419 isTerminatorInstr((--I)->getOpcode()))
422 // If the block ends with X86::JMP and a conditional branch, handle it.
423 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
424 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
425 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
426 Cond.push_back(MachineOperand::CreateImm(BranchCode));
427 FBB = LastInst->getOperand(0).getMachineBasicBlock();
431 // Otherwise, can't handle this.
435 void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
436 MachineBasicBlock::iterator I = MBB.end();
437 if (I == MBB.begin()) return;
439 if (I->getOpcode() != X86::JMP &&
440 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
443 // Remove the branch.
444 I->eraseFromParent();
448 if (I == MBB.begin()) return;
450 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
453 // Remove the branch.
454 I->eraseFromParent();
457 void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
458 MachineBasicBlock *FBB,
459 const std::vector<MachineOperand> &Cond) const {
460 // Shouldn't be a fall through.
461 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
462 assert((Cond.size() == 1 || Cond.size() == 0) &&
463 "X86 branch conditions have one component!");
465 if (FBB == 0) { // One way branch.
467 // Unconditional branch?
468 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
470 // Conditional branch.
471 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
472 BuildMI(&MBB, get(Opc)).addMBB(TBB);
477 // Two-way Conditional branch.
478 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
479 BuildMI(&MBB, get(Opc)).addMBB(TBB);
480 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
483 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
484 if (MBB.empty()) return false;
486 switch (MBB.back().getOpcode()) {
487 case X86::JMP: // Uncond branch.
488 case X86::JMP32r: // Indirect branch.
489 case X86::JMP32m: // Indirect branch through mem.
491 default: return false;
496 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
497 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
498 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
502 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
503 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
504 if (Subtarget->is64Bit())
505 return &X86::GR64RegClass;
507 return &X86::GR32RegClass;