LLVM API Documentation
00001 //===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the PowerPC implementation of the TargetInstrInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "PPCInstrInfo.h" 00015 #include "PPCInstrBuilder.h" 00016 #include "PPCMachineFunctionInfo.h" 00017 #include "PPCPredicates.h" 00018 #include "PPCGenInstrInfo.inc" 00019 #include "PPCTargetMachine.h" 00020 #include "llvm/ADT/STLExtras.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/Support/CommandLine.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 #include "llvm/Target/TargetAsmInfo.h" 00026 using namespace llvm; 00027 00028 extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp. 00029 extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp. 00030 00031 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm) 00032 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm), 00033 RI(*TM.getSubtargetImpl(), *this) {} 00034 00035 bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI, 00036 unsigned& sourceReg, 00037 unsigned& destReg, 00038 unsigned& sourceSubIdx, 00039 unsigned& destSubIdx) const { 00040 sourceSubIdx = destSubIdx = 0; // No sub-registers. 00041 00042 unsigned oc = MI.getOpcode(); 00043 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR || 00044 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2 00045 assert(MI.getNumOperands() >= 3 && 00046 MI.getOperand(0).isReg() && 00047 MI.getOperand(1).isReg() && 00048 MI.getOperand(2).isReg() && 00049 "invalid PPC OR instruction!"); 00050 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) { 00051 sourceReg = MI.getOperand(1).getReg(); 00052 destReg = MI.getOperand(0).getReg(); 00053 return true; 00054 } 00055 } else if (oc == PPC::ADDI) { // addi r1, r2, 0 00056 assert(MI.getNumOperands() >= 3 && 00057 MI.getOperand(0).isReg() && 00058 MI.getOperand(2).isImm() && 00059 "invalid PPC ADDI instruction!"); 00060 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) { 00061 sourceReg = MI.getOperand(1).getReg(); 00062 destReg = MI.getOperand(0).getReg(); 00063 return true; 00064 } 00065 } else if (oc == PPC::ORI) { // ori r1, r2, 0 00066 assert(MI.getNumOperands() >= 3 && 00067 MI.getOperand(0).isReg() && 00068 MI.getOperand(1).isReg() && 00069 MI.getOperand(2).isImm() && 00070 "invalid PPC ORI instruction!"); 00071 if (MI.getOperand(2).getImm() == 0) { 00072 sourceReg = MI.getOperand(1).getReg(); 00073 destReg = MI.getOperand(0).getReg(); 00074 return true; 00075 } 00076 } else if (oc == PPC::FMRS || oc == PPC::FMRD || 00077 oc == PPC::FMRSD) { // fmr r1, r2 00078 assert(MI.getNumOperands() >= 2 && 00079 MI.getOperand(0).isReg() && 00080 MI.getOperand(1).isReg() && 00081 "invalid PPC FMR instruction"); 00082 sourceReg = MI.getOperand(1).getReg(); 00083 destReg = MI.getOperand(0).getReg(); 00084 return true; 00085 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2 00086 assert(MI.getNumOperands() >= 2 && 00087 MI.getOperand(0).isReg() && 00088 MI.getOperand(1).isReg() && 00089 "invalid PPC MCRF instruction"); 00090 sourceReg = MI.getOperand(1).getReg(); 00091 destReg = MI.getOperand(0).getReg(); 00092 return true; 00093 } 00094 return false; 00095 } 00096 00097 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 00098 int &FrameIndex) const { 00099 switch (MI->getOpcode()) { 00100 default: break; 00101 case PPC::LD: 00102 case PPC::LWZ: 00103 case PPC::LFS: 00104 case PPC::LFD: 00105 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && 00106 MI->getOperand(2).isFI()) { 00107 FrameIndex = MI->getOperand(2).getIndex(); 00108 return MI->getOperand(0).getReg(); 00109 } 00110 break; 00111 } 00112 return 0; 00113 } 00114 00115 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 00116 int &FrameIndex) const { 00117 switch (MI->getOpcode()) { 00118 default: break; 00119 case PPC::STD: 00120 case PPC::STW: 00121 case PPC::STFS: 00122 case PPC::STFD: 00123 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && 00124 MI->getOperand(2).isFI()) { 00125 FrameIndex = MI->getOperand(2).getIndex(); 00126 return MI->getOperand(0).getReg(); 00127 } 00128 break; 00129 } 00130 return 0; 00131 } 00132 00133 // commuteInstruction - We can commute rlwimi instructions, but only if the 00134 // rotate amt is zero. We also have to munge the immediates a bit. 00135 MachineInstr * 00136 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const { 00137 MachineFunction &MF = *MI->getParent()->getParent(); 00138 00139 // Normal instructions can be commuted the obvious way. 00140 if (MI->getOpcode() != PPC::RLWIMI) 00141 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI); 00142 00143 // Cannot commute if it has a non-zero rotate count. 00144 if (MI->getOperand(3).getImm() != 0) 00145 return 0; 00146 00147 // If we have a zero rotate count, we have: 00148 // M = mask(MB,ME) 00149 // Op0 = (Op1 & ~M) | (Op2 & M) 00150 // Change this to: 00151 // M = mask((ME+1)&31, (MB-1)&31) 00152 // Op0 = (Op2 & ~M) | (Op1 & M) 00153 00154 // Swap op1/op2 00155 unsigned Reg0 = MI->getOperand(0).getReg(); 00156 unsigned Reg1 = MI->getOperand(1).getReg(); 00157 unsigned Reg2 = MI->getOperand(2).getReg(); 00158 bool Reg1IsKill = MI->getOperand(1).isKill(); 00159 bool Reg2IsKill = MI->getOperand(2).isKill(); 00160 bool ChangeReg0 = false; 00161 // If machine instrs are no longer in two-address forms, update 00162 // destination register as well. 00163 if (Reg0 == Reg1) { 00164 // Must be two address instruction! 00165 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && 00166 "Expecting a two-address instruction!"); 00167 Reg2IsKill = false; 00168 ChangeReg0 = true; 00169 } 00170 00171 // Masks. 00172 unsigned MB = MI->getOperand(4).getImm(); 00173 unsigned ME = MI->getOperand(5).getImm(); 00174 00175 if (NewMI) { 00176 // Create a new instruction. 00177 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg(); 00178 bool Reg0IsDead = MI->getOperand(0).isDead(); 00179 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) 00180 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) 00181 .addReg(Reg2, getKillRegState(Reg2IsKill)) 00182 .addReg(Reg1, getKillRegState(Reg1IsKill)) 00183 .addImm((ME+1) & 31) 00184 .addImm((MB-1) & 31); 00185 } 00186 00187 if (ChangeReg0) 00188 MI->getOperand(0).setReg(Reg2); 00189 MI->getOperand(2).setReg(Reg1); 00190 MI->getOperand(1).setReg(Reg2); 00191 MI->getOperand(2).setIsKill(Reg1IsKill); 00192 MI->getOperand(1).setIsKill(Reg2IsKill); 00193 00194 // Swap the mask around. 00195 MI->getOperand(4).setImm((ME+1) & 31); 00196 MI->getOperand(5).setImm((MB-1) & 31); 00197 return MI; 00198 } 00199 00200 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 00201 MachineBasicBlock::iterator MI) const { 00202 DebugLoc DL = DebugLoc::getUnknownLoc(); 00203 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00204 00205 BuildMI(MBB, MI, DL, get(PPC::NOP)); 00206 } 00207 00208 00209 // Branch analysis. 00210 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, 00211 MachineBasicBlock *&FBB, 00212 SmallVectorImpl<MachineOperand> &Cond, 00213 bool AllowModify) const { 00214 // If the block has no terminators, it just falls into the block after it. 00215 MachineBasicBlock::iterator I = MBB.end(); 00216 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) 00217 return false; 00218 00219 // Get the last instruction in the block. 00220 MachineInstr *LastInst = I; 00221 00222 // If there is only one terminator instruction, process it. 00223 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { 00224 if (LastInst->getOpcode() == PPC::B) { 00225 if (!LastInst->getOperand(0).isMBB()) 00226 return true; 00227 TBB = LastInst->getOperand(0).getMBB(); 00228 return false; 00229 } else if (LastInst->getOpcode() == PPC::BCC) { 00230 if (!LastInst->getOperand(2).isMBB()) 00231 return true; 00232 // Block ends with fall-through condbranch. 00233 TBB = LastInst->getOperand(2).getMBB(); 00234 Cond.push_back(LastInst->getOperand(0)); 00235 Cond.push_back(LastInst->getOperand(1)); 00236 return false; 00237 } 00238 // Otherwise, don't know what this is. 00239 return true; 00240 } 00241 00242 // Get the instruction before it if it's a terminator. 00243 MachineInstr *SecondLastInst = I; 00244 00245 // If there are three terminators, we don't know what sort of block this is. 00246 if (SecondLastInst && I != MBB.begin() && 00247 isUnpredicatedTerminator(--I)) 00248 return true; 00249 00250 // If the block ends with PPC::B and PPC:BCC, handle it. 00251 if (SecondLastInst->getOpcode() == PPC::BCC && 00252 LastInst->getOpcode() == PPC::B) { 00253 if (!SecondLastInst->getOperand(2).isMBB() || 00254 !LastInst->getOperand(0).isMBB()) 00255 return true; 00256 TBB = SecondLastInst->getOperand(2).getMBB(); 00257 Cond.push_back(SecondLastInst->getOperand(0)); 00258 Cond.push_back(SecondLastInst->getOperand(1)); 00259 FBB = LastInst->getOperand(0).getMBB(); 00260 return false; 00261 } 00262 00263 // If the block ends with two PPC:Bs, handle it. The second one is not 00264 // executed, so remove it. 00265 if (SecondLastInst->getOpcode() == PPC::B && 00266 LastInst->getOpcode() == PPC::B) { 00267 if (!SecondLastInst->getOperand(0).isMBB()) 00268 return true; 00269 TBB = SecondLastInst->getOperand(0).getMBB(); 00270 I = LastInst; 00271 if (AllowModify) 00272 I->eraseFromParent(); 00273 return false; 00274 } 00275 00276 // Otherwise, can't handle this. 00277 return true; 00278 } 00279 00280 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { 00281 MachineBasicBlock::iterator I = MBB.end(); 00282 if (I == MBB.begin()) return 0; 00283 --I; 00284 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC) 00285 return 0; 00286 00287 // Remove the branch. 00288 I->eraseFromParent(); 00289 00290 I = MBB.end(); 00291 00292 if (I == MBB.begin()) return 1; 00293 --I; 00294 if (I->getOpcode() != PPC::BCC) 00295 return 1; 00296 00297 // Remove the branch. 00298 I->eraseFromParent(); 00299 return 2; 00300 } 00301 00302 unsigned 00303 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 00304 MachineBasicBlock *FBB, 00305 const SmallVectorImpl<MachineOperand> &Cond) const { 00306 // FIXME this should probably have a DebugLoc argument 00307 DebugLoc dl = DebugLoc::getUnknownLoc(); 00308 // Shouldn't be a fall through. 00309 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 00310 assert((Cond.size() == 2 || Cond.size() == 0) && 00311 "PPC branch conditions have two components!"); 00312 00313 // One-way branch. 00314 if (FBB == 0) { 00315 if (Cond.empty()) // Unconditional branch 00316 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB); 00317 else // Conditional branch 00318 BuildMI(&MBB, dl, get(PPC::BCC)) 00319 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00320 return 1; 00321 } 00322 00323 // Two-way Conditional Branch. 00324 BuildMI(&MBB, dl, get(PPC::BCC)) 00325 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00326 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB); 00327 return 2; 00328 } 00329 00330 bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB, 00331 MachineBasicBlock::iterator MI, 00332 unsigned DestReg, unsigned SrcReg, 00333 const TargetRegisterClass *DestRC, 00334 const TargetRegisterClass *SrcRC) const { 00335 if (DestRC != SrcRC) { 00336 // Not yet supported! 00337 return false; 00338 } 00339 00340 DebugLoc DL = DebugLoc::getUnknownLoc(); 00341 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00342 00343 if (DestRC == PPC::GPRCRegisterClass) { 00344 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg); 00345 } else if (DestRC == PPC::G8RCRegisterClass) { 00346 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg); 00347 } else if (DestRC == PPC::F4RCRegisterClass) { 00348 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg); 00349 } else if (DestRC == PPC::F8RCRegisterClass) { 00350 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg); 00351 } else if (DestRC == PPC::CRRCRegisterClass) { 00352 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg); 00353 } else if (DestRC == PPC::VRRCRegisterClass) { 00354 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg); 00355 } else if (DestRC == PPC::CRBITRCRegisterClass) { 00356 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg); 00357 } else { 00358 // Attempt to copy register that is not GPR or FPR 00359 return false; 00360 } 00361 00362 return true; 00363 } 00364 00365 bool 00366 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, 00367 unsigned SrcReg, bool isKill, 00368 int FrameIdx, 00369 const TargetRegisterClass *RC, 00370 SmallVectorImpl<MachineInstr*> &NewMIs) const{ 00371 DebugLoc DL = DebugLoc::getUnknownLoc(); 00372 if (RC == PPC::GPRCRegisterClass) { 00373 if (SrcReg != PPC::LR) { 00374 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 00375 .addReg(SrcReg, 00376 getKillRegState(isKill)), 00377 FrameIdx)); 00378 } else { 00379 // FIXME: this spills LR immediately to memory in one step. To do this, 00380 // we use R11, which we know cannot be used in the prolog/epilog. This is 00381 // a hack. 00382 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11)); 00383 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 00384 .addReg(PPC::R11, 00385 getKillRegState(isKill)), 00386 FrameIdx)); 00387 } 00388 } else if (RC == PPC::G8RCRegisterClass) { 00389 if (SrcReg != PPC::LR8) { 00390 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) 00391 .addReg(SrcReg, 00392 getKillRegState(isKill)), 00393 FrameIdx)); 00394 } else { 00395 // FIXME: this spills LR immediately to memory in one step. To do this, 00396 // we use R11, which we know cannot be used in the prolog/epilog. This is 00397 // a hack. 00398 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11)); 00399 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) 00400 .addReg(PPC::X11, 00401 getKillRegState(isKill)), 00402 FrameIdx)); 00403 } 00404 } else if (RC == PPC::F8RCRegisterClass) { 00405 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) 00406 .addReg(SrcReg, 00407 getKillRegState(isKill)), 00408 FrameIdx)); 00409 } else if (RC == PPC::F4RCRegisterClass) { 00410 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) 00411 .addReg(SrcReg, 00412 getKillRegState(isKill)), 00413 FrameIdx)); 00414 } else if (RC == PPC::CRRCRegisterClass) { 00415 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) || 00416 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) { 00417 // FIXME (64-bit): Enable 00418 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) 00419 .addReg(SrcReg, 00420 getKillRegState(isKill)), 00421 FrameIdx)); 00422 return true; 00423 } else { 00424 // FIXME: We use R0 here, because it isn't available for RA. We need to 00425 // store the CR in the low 4-bits of the saved value. First, issue a MFCR 00426 // to save all of the CRBits. 00427 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0)); 00428 00429 // If the saved register wasn't CR0, shift the bits left so that they are 00430 // in CR0's slot. 00431 if (SrcReg != PPC::CR0) { 00432 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4; 00433 // rlwinm r0, r0, ShiftBits, 0, 31. 00434 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0) 00435 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31)); 00436 } 00437 00438 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) 00439 .addReg(PPC::R0, 00440 getKillRegState(isKill)), 00441 FrameIdx)); 00442 } 00443 } else if (RC == PPC::CRBITRCRegisterClass) { 00444 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the 00445 // backend currently only uses CR1EQ as an individual bit, this should 00446 // not cause any bug. If we need other uses of CR bits, the following 00447 // code may be invalid. 00448 unsigned Reg = 0; 00449 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT || 00450 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN) 00451 Reg = PPC::CR0; 00452 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT || 00453 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN) 00454 Reg = PPC::CR1; 00455 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT || 00456 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN) 00457 Reg = PPC::CR2; 00458 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT || 00459 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN) 00460 Reg = PPC::CR3; 00461 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT || 00462 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN) 00463 Reg = PPC::CR4; 00464 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT || 00465 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN) 00466 Reg = PPC::CR5; 00467 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT || 00468 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN) 00469 Reg = PPC::CR6; 00470 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT || 00471 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN) 00472 Reg = PPC::CR7; 00473 00474 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx, 00475 PPC::CRRCRegisterClass, NewMIs); 00476 00477 } else if (RC == PPC::VRRCRegisterClass) { 00478 // We don't have indexed addressing for vector loads. Emit: 00479 // R0 = ADDI FI# 00480 // STVX VAL, 0, R0 00481 // 00482 // FIXME: We use R0 here, because it isn't available for RA. 00483 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0), 00484 FrameIdx, 0, 0)); 00485 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX)) 00486 .addReg(SrcReg, getKillRegState(isKill)) 00487 .addReg(PPC::R0) 00488 .addReg(PPC::R0)); 00489 } else { 00490 llvm_unreachable("Unknown regclass!"); 00491 } 00492 00493 return false; 00494 } 00495 00496 void 00497 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 00498 MachineBasicBlock::iterator MI, 00499 unsigned SrcReg, bool isKill, int FrameIdx, 00500 const TargetRegisterClass *RC) const { 00501 MachineFunction &MF = *MBB.getParent(); 00502 SmallVector<MachineInstr*, 4> NewMIs; 00503 00504 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) { 00505 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 00506 FuncInfo->setSpillsCR(); 00507 } 00508 00509 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 00510 MBB.insert(MI, NewMIs[i]); 00511 } 00512 00513 void 00514 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, 00515 unsigned DestReg, int FrameIdx, 00516 const TargetRegisterClass *RC, 00517 SmallVectorImpl<MachineInstr*> &NewMIs)const{ 00518 if (RC == PPC::GPRCRegisterClass) { 00519 if (DestReg != PPC::LR) { 00520 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 00521 DestReg), FrameIdx)); 00522 } else { 00523 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 00524 PPC::R11), FrameIdx)); 00525 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11)); 00526 } 00527 } else if (RC == PPC::G8RCRegisterClass) { 00528 if (DestReg != PPC::LR8) { 00529 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), 00530 FrameIdx)); 00531 } else { 00532 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), 00533 PPC::R11), FrameIdx)); 00534 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11)); 00535 } 00536 } else if (RC == PPC::F8RCRegisterClass) { 00537 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), 00538 FrameIdx)); 00539 } else if (RC == PPC::F4RCRegisterClass) { 00540 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), 00541 FrameIdx)); 00542 } else if (RC == PPC::CRRCRegisterClass) { 00543 // FIXME: We use R0 here, because it isn't available for RA. 00544 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0), 00545 FrameIdx)); 00546 00547 // If the reloaded register isn't CR0, shift the bits right so that they are 00548 // in the right CR's slot. 00549 if (DestReg != PPC::CR0) { 00550 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4; 00551 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 00552 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0) 00553 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31)); 00554 } 00555 00556 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0)); 00557 } else if (RC == PPC::CRBITRCRegisterClass) { 00558 00559 unsigned Reg = 0; 00560 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT || 00561 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN) 00562 Reg = PPC::CR0; 00563 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT || 00564 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN) 00565 Reg = PPC::CR1; 00566 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT || 00567 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN) 00568 Reg = PPC::CR2; 00569 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT || 00570 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN) 00571 Reg = PPC::CR3; 00572 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT || 00573 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN) 00574 Reg = PPC::CR4; 00575 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT || 00576 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN) 00577 Reg = PPC::CR5; 00578 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT || 00579 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN) 00580 Reg = PPC::CR6; 00581 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT || 00582 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN) 00583 Reg = PPC::CR7; 00584 00585 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx, 00586 PPC::CRRCRegisterClass, NewMIs); 00587 00588 } else if (RC == PPC::VRRCRegisterClass) { 00589 // We don't have indexed addressing for vector loads. Emit: 00590 // R0 = ADDI FI# 00591 // Dest = LVX 0, R0 00592 // 00593 // FIXME: We use R0 here, because it isn't available for RA. 00594 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0), 00595 FrameIdx, 0, 0)); 00596 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0) 00597 .addReg(PPC::R0)); 00598 } else { 00599 llvm_unreachable("Unknown regclass!"); 00600 } 00601 } 00602 00603 void 00604 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 00605 MachineBasicBlock::iterator MI, 00606 unsigned DestReg, int FrameIdx, 00607 const TargetRegisterClass *RC) const { 00608 MachineFunction &MF = *MBB.getParent(); 00609 SmallVector<MachineInstr*, 4> NewMIs; 00610 DebugLoc DL = DebugLoc::getUnknownLoc(); 00611 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00612 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs); 00613 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) 00614 MBB.insert(MI, NewMIs[i]); 00615 } 00616 00617 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into 00618 /// copy instructions, turning them into load/store instructions. 00619 MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, 00620 MachineInstr *MI, 00621 const SmallVectorImpl<unsigned> &Ops, 00622 int FrameIndex) const { 00623 if (Ops.size() != 1) return NULL; 00624 00625 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because 00626 // it takes more than one instruction to store it. 00627 unsigned Opc = MI->getOpcode(); 00628 unsigned OpNum = Ops[0]; 00629 00630 MachineInstr *NewMI = NULL; 00631 if ((Opc == PPC::OR && 00632 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) { 00633 if (OpNum == 0) { // move -> store 00634 unsigned InReg = MI->getOperand(1).getReg(); 00635 bool isKill = MI->getOperand(1).isKill(); 00636 bool isUndef = MI->getOperand(1).isUndef(); 00637 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW)) 00638 .addReg(InReg, 00639 getKillRegState(isKill) | 00640 getUndefRegState(isUndef)), 00641 FrameIndex); 00642 } else { // move -> load 00643 unsigned OutReg = MI->getOperand(0).getReg(); 00644 bool isDead = MI->getOperand(0).isDead(); 00645 bool isUndef = MI->getOperand(0).isUndef(); 00646 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ)) 00647 .addReg(OutReg, 00648 RegState::Define | 00649 getDeadRegState(isDead) | 00650 getUndefRegState(isUndef)), 00651 FrameIndex); 00652 } 00653 } else if ((Opc == PPC::OR8 && 00654 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) { 00655 if (OpNum == 0) { // move -> store 00656 unsigned InReg = MI->getOperand(1).getReg(); 00657 bool isKill = MI->getOperand(1).isKill(); 00658 bool isUndef = MI->getOperand(1).isUndef(); 00659 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD)) 00660 .addReg(InReg, 00661 getKillRegState(isKill) | 00662 getUndefRegState(isUndef)), 00663 FrameIndex); 00664 } else { // move -> load 00665 unsigned OutReg = MI->getOperand(0).getReg(); 00666 bool isDead = MI->getOperand(0).isDead(); 00667 bool isUndef = MI->getOperand(0).isUndef(); 00668 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD)) 00669 .addReg(OutReg, 00670 RegState::Define | 00671 getDeadRegState(isDead) | 00672 getUndefRegState(isUndef)), 00673 FrameIndex); 00674 } 00675 } else if (Opc == PPC::FMRD) { 00676 if (OpNum == 0) { // move -> store 00677 unsigned InReg = MI->getOperand(1).getReg(); 00678 bool isKill = MI->getOperand(1).isKill(); 00679 bool isUndef = MI->getOperand(1).isUndef(); 00680 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD)) 00681 .addReg(InReg, 00682 getKillRegState(isKill) | 00683 getUndefRegState(isUndef)), 00684 FrameIndex); 00685 } else { // move -> load 00686 unsigned OutReg = MI->getOperand(0).getReg(); 00687 bool isDead = MI->getOperand(0).isDead(); 00688 bool isUndef = MI->getOperand(0).isUndef(); 00689 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD)) 00690 .addReg(OutReg, 00691 RegState::Define | 00692 getDeadRegState(isDead) | 00693 getUndefRegState(isUndef)), 00694 FrameIndex); 00695 } 00696 } else if (Opc == PPC::FMRS) { 00697 if (OpNum == 0) { // move -> store 00698 unsigned InReg = MI->getOperand(1).getReg(); 00699 bool isKill = MI->getOperand(1).isKill(); 00700 bool isUndef = MI->getOperand(1).isUndef(); 00701 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS)) 00702 .addReg(InReg, 00703 getKillRegState(isKill) | 00704 getUndefRegState(isUndef)), 00705 FrameIndex); 00706 } else { // move -> load 00707 unsigned OutReg = MI->getOperand(0).getReg(); 00708 bool isDead = MI->getOperand(0).isDead(); 00709 bool isUndef = MI->getOperand(0).isUndef(); 00710 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS)) 00711 .addReg(OutReg, 00712 RegState::Define | 00713 getDeadRegState(isDead) | 00714 getUndefRegState(isUndef)), 00715 FrameIndex); 00716 } 00717 } 00718 00719 return NewMI; 00720 } 00721 00722 bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI, 00723 const SmallVectorImpl<unsigned> &Ops) const { 00724 if (Ops.size() != 1) return false; 00725 00726 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because 00727 // it takes more than one instruction to store it. 00728 unsigned Opc = MI->getOpcode(); 00729 00730 if ((Opc == PPC::OR && 00731 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) 00732 return true; 00733 else if ((Opc == PPC::OR8 && 00734 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) 00735 return true; 00736 else if (Opc == PPC::FMRD || Opc == PPC::FMRS) 00737 return true; 00738 00739 return false; 00740 } 00741 00742 00743 bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const { 00744 if (MBB.empty()) return false; 00745 00746 switch (MBB.back().getOpcode()) { 00747 case PPC::BLR: // Return. 00748 case PPC::B: // Uncond branch. 00749 case PPC::BCTR: // Indirect branch. 00750 return true; 00751 default: return false; 00752 } 00753 } 00754 00755 bool PPCInstrInfo:: 00756 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 00757 assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); 00758 // Leave the CR# the same, but invert the condition. 00759 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); 00760 return false; 00761 } 00762 00763 /// GetInstSize - Return the number of bytes of code the specified 00764 /// instruction may be. This returns the maximum number of bytes. 00765 /// 00766 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { 00767 switch (MI->getOpcode()) { 00768 case PPC::INLINEASM: { // Inline Asm: Variable size. 00769 const MachineFunction *MF = MI->getParent()->getParent(); 00770 const char *AsmStr = MI->getOperand(0).getSymbolName(); 00771 return getInlineAsmLength(AsmStr, *MF->getTarget().getTargetAsmInfo()); 00772 } 00773 case PPC::DBG_LABEL: 00774 case PPC::EH_LABEL: 00775 case PPC::GC_LABEL: 00776 return 0; 00777 default: 00778 return 4; // PowerPC instructions are all 4 bytes 00779 } 00780 }