LLVM API Documentation
00001 //=== A15SDOptimizerPass.cpp - Optimize DPR and SPR register accesses on A15==// 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 // The Cortex-A15 processor employs a tracking scheme in its register renaming 00011 // in order to process each instruction's micro-ops speculatively and 00012 // out-of-order with appropriate forwarding. The ARM architecture allows VFP 00013 // instructions to read and write 32-bit S-registers. Each S-register 00014 // corresponds to one half (upper or lower) of an overlaid 64-bit D-register. 00015 // 00016 // There are several instruction patterns which can be used to provide this 00017 // capability which can provide higher performance than other, potentially more 00018 // direct patterns, specifically around when one micro-op reads a D-register 00019 // operand that has recently been written as one or more S-register results. 00020 // 00021 // This file defines a pre-regalloc pass which looks for SPR producers which 00022 // are going to be used by a DPR (or QPR) consumers and creates the more 00023 // optimized access pattern. 00024 // 00025 //===----------------------------------------------------------------------===// 00026 00027 #include "ARM.h" 00028 #include "ARMBaseInstrInfo.h" 00029 #include "ARMBaseRegisterInfo.h" 00030 #include "llvm/ADT/Statistic.h" 00031 #include "llvm/CodeGen/MachineFunctionPass.h" 00032 #include "llvm/CodeGen/MachineInstr.h" 00033 #include "llvm/CodeGen/MachineInstrBuilder.h" 00034 #include "llvm/CodeGen/MachineRegisterInfo.h" 00035 #include "llvm/Support/Debug.h" 00036 #include "llvm/Target/TargetRegisterInfo.h" 00037 #include "llvm/Target/TargetSubtargetInfo.h" 00038 #include <set> 00039 00040 using namespace llvm; 00041 00042 #define DEBUG_TYPE "a15-sd-optimizer" 00043 00044 namespace { 00045 struct A15SDOptimizer : public MachineFunctionPass { 00046 static char ID; 00047 A15SDOptimizer() : MachineFunctionPass(ID) {} 00048 00049 bool runOnMachineFunction(MachineFunction &Fn) override; 00050 00051 const char *getPassName() const override { 00052 return "ARM A15 S->D optimizer"; 00053 } 00054 00055 private: 00056 const ARMBaseInstrInfo *TII; 00057 const TargetRegisterInfo *TRI; 00058 MachineRegisterInfo *MRI; 00059 00060 bool runOnInstruction(MachineInstr *MI); 00061 00062 // 00063 // Instruction builder helpers 00064 // 00065 unsigned createDupLane(MachineBasicBlock &MBB, 00066 MachineBasicBlock::iterator InsertBefore, 00067 DebugLoc DL, 00068 unsigned Reg, unsigned Lane, 00069 bool QPR=false); 00070 00071 unsigned createExtractSubreg(MachineBasicBlock &MBB, 00072 MachineBasicBlock::iterator InsertBefore, 00073 DebugLoc DL, 00074 unsigned DReg, unsigned Lane, 00075 const TargetRegisterClass *TRC); 00076 00077 unsigned createVExt(MachineBasicBlock &MBB, 00078 MachineBasicBlock::iterator InsertBefore, 00079 DebugLoc DL, 00080 unsigned Ssub0, unsigned Ssub1); 00081 00082 unsigned createRegSequence(MachineBasicBlock &MBB, 00083 MachineBasicBlock::iterator InsertBefore, 00084 DebugLoc DL, 00085 unsigned Reg1, unsigned Reg2); 00086 00087 unsigned createInsertSubreg(MachineBasicBlock &MBB, 00088 MachineBasicBlock::iterator InsertBefore, 00089 DebugLoc DL, unsigned DReg, unsigned Lane, 00090 unsigned ToInsert); 00091 00092 unsigned createImplicitDef(MachineBasicBlock &MBB, 00093 MachineBasicBlock::iterator InsertBefore, 00094 DebugLoc DL); 00095 00096 // 00097 // Various property checkers 00098 // 00099 bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC); 00100 bool hasPartialWrite(MachineInstr *MI); 00101 SmallVector<unsigned, 8> getReadDPRs(MachineInstr *MI); 00102 unsigned getDPRLaneFromSPR(unsigned SReg); 00103 00104 // 00105 // Methods used for getting the definitions of partial registers 00106 // 00107 00108 MachineInstr *elideCopies(MachineInstr *MI); 00109 void elideCopiesAndPHIs(MachineInstr *MI, 00110 SmallVectorImpl<MachineInstr*> &Outs); 00111 00112 // 00113 // Pattern optimization methods 00114 // 00115 unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg); 00116 unsigned optimizeSDPattern(MachineInstr *MI); 00117 unsigned getPrefSPRLane(unsigned SReg); 00118 00119 // 00120 // Sanitizing method - used to make sure if don't leave dead code around. 00121 // 00122 void eraseInstrWithNoUses(MachineInstr *MI); 00123 00124 // 00125 // A map used to track the changes done by this pass. 00126 // 00127 std::map<MachineInstr*, unsigned> Replacements; 00128 std::set<MachineInstr *> DeadInstr; 00129 }; 00130 char A15SDOptimizer::ID = 0; 00131 } // end anonymous namespace 00132 00133 // Returns true if this is a use of a SPR register. 00134 bool A15SDOptimizer::usesRegClass(MachineOperand &MO, 00135 const TargetRegisterClass *TRC) { 00136 if (!MO.isReg()) 00137 return false; 00138 unsigned Reg = MO.getReg(); 00139 00140 if (TargetRegisterInfo::isVirtualRegister(Reg)) 00141 return MRI->getRegClass(Reg)->hasSuperClassEq(TRC); 00142 else 00143 return TRC->contains(Reg); 00144 } 00145 00146 unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) { 00147 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, 00148 &ARM::DPRRegClass); 00149 if (DReg != ARM::NoRegister) return ARM::ssub_1; 00150 return ARM::ssub_0; 00151 } 00152 00153 // Get the subreg type that is most likely to be coalesced 00154 // for an SPR register that will be used in VDUP32d pseudo. 00155 unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) { 00156 if (!TRI->isVirtualRegister(SReg)) 00157 return getDPRLaneFromSPR(SReg); 00158 00159 MachineInstr *MI = MRI->getVRegDef(SReg); 00160 if (!MI) return ARM::ssub_0; 00161 MachineOperand *MO = MI->findRegisterDefOperand(SReg); 00162 00163 assert(MO->isReg() && "Non-register operand found!"); 00164 if (!MO) return ARM::ssub_0; 00165 00166 if (MI->isCopy() && usesRegClass(MI->getOperand(1), 00167 &ARM::SPRRegClass)) { 00168 SReg = MI->getOperand(1).getReg(); 00169 } 00170 00171 if (TargetRegisterInfo::isVirtualRegister(SReg)) { 00172 if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1; 00173 return ARM::ssub_0; 00174 } 00175 return getDPRLaneFromSPR(SReg); 00176 } 00177 00178 // MI is known to be dead. Figure out what instructions 00179 // are also made dead by this and mark them for removal. 00180 void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) { 00181 SmallVector<MachineInstr *, 8> Front; 00182 DeadInstr.insert(MI); 00183 00184 DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n"); 00185 Front.push_back(MI); 00186 00187 while (Front.size() != 0) { 00188 MI = Front.back(); 00189 Front.pop_back(); 00190 00191 // MI is already known to be dead. We need to see 00192 // if other instructions can also be removed. 00193 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) { 00194 MachineOperand &MO = MI->getOperand(i); 00195 if ((!MO.isReg()) || (!MO.isUse())) 00196 continue; 00197 unsigned Reg = MO.getReg(); 00198 if (!TRI->isVirtualRegister(Reg)) 00199 continue; 00200 MachineOperand *Op = MI->findRegisterDefOperand(Reg); 00201 00202 if (!Op) 00203 continue; 00204 00205 MachineInstr *Def = Op->getParent(); 00206 00207 // We don't need to do anything if we have already marked 00208 // this instruction as being dead. 00209 if (DeadInstr.find(Def) != DeadInstr.end()) 00210 continue; 00211 00212 // Check if all the uses of this instruction are marked as 00213 // dead. If so, we can also mark this instruction as being 00214 // dead. 00215 bool IsDead = true; 00216 for (unsigned int j = 0; j < Def->getNumOperands(); ++j) { 00217 MachineOperand &MODef = Def->getOperand(j); 00218 if ((!MODef.isReg()) || (!MODef.isDef())) 00219 continue; 00220 unsigned DefReg = MODef.getReg(); 00221 if (!TRI->isVirtualRegister(DefReg)) { 00222 IsDead = false; 00223 break; 00224 } 00225 for (MachineRegisterInfo::use_instr_iterator 00226 II = MRI->use_instr_begin(Reg), EE = MRI->use_instr_end(); 00227 II != EE; ++II) { 00228 // We don't care about self references. 00229 if (&*II == Def) 00230 continue; 00231 if (DeadInstr.find(&*II) == DeadInstr.end()) { 00232 IsDead = false; 00233 break; 00234 } 00235 } 00236 } 00237 00238 if (!IsDead) continue; 00239 00240 DEBUG(dbgs() << "Deleting instruction " << *Def << "\n"); 00241 DeadInstr.insert(Def); 00242 } 00243 } 00244 } 00245 00246 // Creates the more optimized patterns and generally does all the code 00247 // transformations in this pass. 00248 unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) { 00249 if (MI->isCopy()) { 00250 return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg()); 00251 } 00252 00253 if (MI->isInsertSubreg()) { 00254 unsigned DPRReg = MI->getOperand(1).getReg(); 00255 unsigned SPRReg = MI->getOperand(2).getReg(); 00256 00257 if (TRI->isVirtualRegister(DPRReg) && TRI->isVirtualRegister(SPRReg)) { 00258 MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg()); 00259 MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg()); 00260 00261 if (DPRMI && SPRMI) { 00262 // See if the first operand of this insert_subreg is IMPLICIT_DEF 00263 MachineInstr *ECDef = elideCopies(DPRMI); 00264 if (ECDef && ECDef->isImplicitDef()) { 00265 // Another corner case - if we're inserting something that is purely 00266 // a subreg copy of a DPR, just use that DPR. 00267 00268 MachineInstr *EC = elideCopies(SPRMI); 00269 // Is it a subreg copy of ssub_0? 00270 if (EC && EC->isCopy() && 00271 EC->getOperand(1).getSubReg() == ARM::ssub_0) { 00272 DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI); 00273 00274 // Find the thing we're subreg copying out of - is it of the same 00275 // regclass as DPRMI? (i.e. a DPR or QPR). 00276 unsigned FullReg = SPRMI->getOperand(1).getReg(); 00277 const TargetRegisterClass *TRC = 00278 MRI->getRegClass(MI->getOperand(1).getReg()); 00279 if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) { 00280 DEBUG(dbgs() << "Subreg copy is compatible - returning "); 00281 DEBUG(dbgs() << PrintReg(FullReg) << "\n"); 00282 eraseInstrWithNoUses(MI); 00283 return FullReg; 00284 } 00285 } 00286 00287 return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg()); 00288 } 00289 } 00290 } 00291 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg()); 00292 } 00293 00294 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), 00295 &ARM::SPRRegClass)) { 00296 // See if all bar one of the operands are IMPLICIT_DEF and insert the 00297 // optimizer pattern accordingly. 00298 unsigned NumImplicit = 0, NumTotal = 0; 00299 unsigned NonImplicitReg = ~0U; 00300 00301 for (unsigned I = 1; I < MI->getNumExplicitOperands(); ++I) { 00302 if (!MI->getOperand(I).isReg()) 00303 continue; 00304 ++NumTotal; 00305 unsigned OpReg = MI->getOperand(I).getReg(); 00306 00307 if (!TRI->isVirtualRegister(OpReg)) 00308 break; 00309 00310 MachineInstr *Def = MRI->getVRegDef(OpReg); 00311 if (!Def) 00312 break; 00313 if (Def->isImplicitDef()) 00314 ++NumImplicit; 00315 else 00316 NonImplicitReg = MI->getOperand(I).getReg(); 00317 } 00318 00319 if (NumImplicit == NumTotal - 1) 00320 return optimizeAllLanesPattern(MI, NonImplicitReg); 00321 else 00322 return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg()); 00323 } 00324 00325 llvm_unreachable("Unhandled update pattern!"); 00326 } 00327 00328 // Return true if this MachineInstr inserts a scalar (SPR) value into 00329 // a D or Q register. 00330 bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) { 00331 // The only way we can do a partial register update is through a COPY, 00332 // INSERT_SUBREG or REG_SEQUENCE. 00333 if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass)) 00334 return true; 00335 00336 if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2), 00337 &ARM::SPRRegClass)) 00338 return true; 00339 00340 if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass)) 00341 return true; 00342 00343 return false; 00344 } 00345 00346 // Looks through full copies to get the instruction that defines the input 00347 // operand for MI. 00348 MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) { 00349 if (!MI->isFullCopy()) 00350 return MI; 00351 if (!TRI->isVirtualRegister(MI->getOperand(1).getReg())) 00352 return nullptr; 00353 MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg()); 00354 if (!Def) 00355 return nullptr; 00356 return elideCopies(Def); 00357 } 00358 00359 // Look through full copies and PHIs to get the set of non-copy MachineInstrs 00360 // that can produce MI. 00361 void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI, 00362 SmallVectorImpl<MachineInstr*> &Outs) { 00363 // Looking through PHIs may create loops so we need to track what 00364 // instructions we have visited before. 00365 std::set<MachineInstr *> Reached; 00366 SmallVector<MachineInstr *, 8> Front; 00367 Front.push_back(MI); 00368 while (Front.size() != 0) { 00369 MI = Front.back(); 00370 Front.pop_back(); 00371 00372 // If we have already explored this MachineInstr, ignore it. 00373 if (Reached.find(MI) != Reached.end()) 00374 continue; 00375 Reached.insert(MI); 00376 if (MI->isPHI()) { 00377 for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) { 00378 unsigned Reg = MI->getOperand(I).getReg(); 00379 if (!TRI->isVirtualRegister(Reg)) { 00380 continue; 00381 } 00382 MachineInstr *NewMI = MRI->getVRegDef(Reg); 00383 if (!NewMI) 00384 continue; 00385 Front.push_back(NewMI); 00386 } 00387 } else if (MI->isFullCopy()) { 00388 if (!TRI->isVirtualRegister(MI->getOperand(1).getReg())) 00389 continue; 00390 MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg()); 00391 if (!NewMI) 00392 continue; 00393 Front.push_back(NewMI); 00394 } else { 00395 DEBUG(dbgs() << "Found partial copy" << *MI <<"\n"); 00396 Outs.push_back(MI); 00397 } 00398 } 00399 } 00400 00401 // Return the DPR virtual registers that are read by this machine instruction 00402 // (if any). 00403 SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) { 00404 if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() || 00405 MI->isKill()) 00406 return SmallVector<unsigned, 8>(); 00407 00408 SmallVector<unsigned, 8> Defs; 00409 for (unsigned i = 0; i < MI->getNumOperands(); ++i) { 00410 MachineOperand &MO = MI->getOperand(i); 00411 00412 if (!MO.isReg() || !MO.isUse()) 00413 continue; 00414 if (!usesRegClass(MO, &ARM::DPRRegClass) && 00415 !usesRegClass(MO, &ARM::QPRRegClass) && 00416 !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR 00417 continue; 00418 00419 Defs.push_back(MO.getReg()); 00420 } 00421 return Defs; 00422 } 00423 00424 // Creates a DPR register from an SPR one by using a VDUP. 00425 unsigned 00426 A15SDOptimizer::createDupLane(MachineBasicBlock &MBB, 00427 MachineBasicBlock::iterator InsertBefore, 00428 DebugLoc DL, 00429 unsigned Reg, unsigned Lane, bool QPR) { 00430 unsigned Out = MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : 00431 &ARM::DPRRegClass); 00432 AddDefaultPred(BuildMI(MBB, 00433 InsertBefore, 00434 DL, 00435 TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), 00436 Out) 00437 .addReg(Reg) 00438 .addImm(Lane)); 00439 00440 return Out; 00441 } 00442 00443 // Creates a SPR register from a DPR by copying the value in lane 0. 00444 unsigned 00445 A15SDOptimizer::createExtractSubreg(MachineBasicBlock &MBB, 00446 MachineBasicBlock::iterator InsertBefore, 00447 DebugLoc DL, 00448 unsigned DReg, unsigned Lane, 00449 const TargetRegisterClass *TRC) { 00450 unsigned Out = MRI->createVirtualRegister(TRC); 00451 BuildMI(MBB, 00452 InsertBefore, 00453 DL, 00454 TII->get(TargetOpcode::COPY), Out) 00455 .addReg(DReg, 0, Lane); 00456 00457 return Out; 00458 } 00459 00460 // Takes two SPR registers and creates a DPR by using a REG_SEQUENCE. 00461 unsigned 00462 A15SDOptimizer::createRegSequence(MachineBasicBlock &MBB, 00463 MachineBasicBlock::iterator InsertBefore, 00464 DebugLoc DL, 00465 unsigned Reg1, unsigned Reg2) { 00466 unsigned Out = MRI->createVirtualRegister(&ARM::QPRRegClass); 00467 BuildMI(MBB, 00468 InsertBefore, 00469 DL, 00470 TII->get(TargetOpcode::REG_SEQUENCE), Out) 00471 .addReg(Reg1) 00472 .addImm(ARM::dsub_0) 00473 .addReg(Reg2) 00474 .addImm(ARM::dsub_1); 00475 return Out; 00476 } 00477 00478 // Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1) 00479 // and merges them into one DPR register. 00480 unsigned 00481 A15SDOptimizer::createVExt(MachineBasicBlock &MBB, 00482 MachineBasicBlock::iterator InsertBefore, 00483 DebugLoc DL, 00484 unsigned Ssub0, unsigned Ssub1) { 00485 unsigned Out = MRI->createVirtualRegister(&ARM::DPRRegClass); 00486 AddDefaultPred(BuildMI(MBB, 00487 InsertBefore, 00488 DL, 00489 TII->get(ARM::VEXTd32), Out) 00490 .addReg(Ssub0) 00491 .addReg(Ssub1) 00492 .addImm(1)); 00493 return Out; 00494 } 00495 00496 unsigned 00497 A15SDOptimizer::createInsertSubreg(MachineBasicBlock &MBB, 00498 MachineBasicBlock::iterator InsertBefore, 00499 DebugLoc DL, unsigned DReg, unsigned Lane, 00500 unsigned ToInsert) { 00501 unsigned Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass); 00502 BuildMI(MBB, 00503 InsertBefore, 00504 DL, 00505 TII->get(TargetOpcode::INSERT_SUBREG), Out) 00506 .addReg(DReg) 00507 .addReg(ToInsert) 00508 .addImm(Lane); 00509 00510 return Out; 00511 } 00512 00513 unsigned 00514 A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB, 00515 MachineBasicBlock::iterator InsertBefore, 00516 DebugLoc DL) { 00517 unsigned Out = MRI->createVirtualRegister(&ARM::DPRRegClass); 00518 BuildMI(MBB, 00519 InsertBefore, 00520 DL, 00521 TII->get(TargetOpcode::IMPLICIT_DEF), Out); 00522 return Out; 00523 } 00524 00525 // This function inserts instructions in order to optimize interactions between 00526 // SPR registers and DPR/QPR registers. It does so by performing VDUPs on all 00527 // lanes, and the using VEXT instructions to recompose the result. 00528 unsigned 00529 A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) { 00530 MachineBasicBlock::iterator InsertPt(MI); 00531 DebugLoc DL = MI->getDebugLoc(); 00532 MachineBasicBlock &MBB = *MI->getParent(); 00533 InsertPt++; 00534 unsigned Out; 00535 00536 // DPair has the same length as QPR and also has two DPRs as subreg. 00537 // Treat DPair as QPR. 00538 if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) || 00539 MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) { 00540 unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg, 00541 ARM::dsub_0, &ARM::DPRRegClass); 00542 unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg, 00543 ARM::dsub_1, &ARM::DPRRegClass); 00544 00545 unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0); 00546 unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1); 00547 Out = createVExt(MBB, InsertPt, DL, Out1, Out2); 00548 00549 unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0); 00550 unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1); 00551 Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4); 00552 00553 Out = createRegSequence(MBB, InsertPt, DL, Out, Out2); 00554 00555 } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) { 00556 unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0); 00557 unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1); 00558 Out = createVExt(MBB, InsertPt, DL, Out1, Out2); 00559 00560 } else { 00561 assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) && 00562 "Found unexpected regclass!"); 00563 00564 unsigned PrefLane = getPrefSPRLane(Reg); 00565 unsigned Lane; 00566 switch (PrefLane) { 00567 case ARM::ssub_0: Lane = 0; break; 00568 case ARM::ssub_1: Lane = 1; break; 00569 default: llvm_unreachable("Unknown preferred lane!"); 00570 } 00571 00572 // Treat DPair as QPR 00573 bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) || 00574 usesRegClass(MI->getOperand(0), &ARM::DPairRegClass); 00575 00576 Out = createImplicitDef(MBB, InsertPt, DL); 00577 Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg); 00578 Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR); 00579 eraseInstrWithNoUses(MI); 00580 } 00581 return Out; 00582 } 00583 00584 bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) { 00585 // We look for instructions that write S registers that are then read as 00586 // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and 00587 // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or 00588 // merge two SPR values to form a DPR register. In order avoid false 00589 // positives we make sure that there is an SPR producer so we look past 00590 // COPY and PHI nodes to find it. 00591 // 00592 // The best code pattern for when an SPR producer is going to be used by a 00593 // DPR or QPR consumer depends on whether the other lanes of the 00594 // corresponding DPR/QPR are currently defined. 00595 // 00596 // We can handle these efficiently, depending on the type of 00597 // pseudo-instruction that is producing the pattern 00598 // 00599 // * COPY: * VDUP all lanes and merge the results together 00600 // using VEXTs. 00601 // 00602 // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR 00603 // lane, and the other lane(s) of the DPR/QPR register 00604 // that we are inserting in are undefined, use the 00605 // original DPR/QPR value. 00606 // * Otherwise, fall back on the same stategy as COPY. 00607 // 00608 // * REG_SEQUENCE: * If all except one of the input operands are 00609 // IMPLICIT_DEFs, insert the VDUP pattern for just the 00610 // defined input operand 00611 // * Otherwise, fall back on the same stategy as COPY. 00612 // 00613 00614 // First, get all the reads of D-registers done by this instruction. 00615 SmallVector<unsigned, 8> Defs = getReadDPRs(MI); 00616 bool Modified = false; 00617 00618 for (SmallVectorImpl<unsigned>::iterator I = Defs.begin(), E = Defs.end(); 00619 I != E; ++I) { 00620 // Follow the def-use chain for this DPR through COPYs, and also through 00621 // PHIs (which are essentially multi-way COPYs). It is because of PHIs that 00622 // we can end up with multiple defs of this DPR. 00623 00624 SmallVector<MachineInstr *, 8> DefSrcs; 00625 if (!TRI->isVirtualRegister(*I)) 00626 continue; 00627 MachineInstr *Def = MRI->getVRegDef(*I); 00628 if (!Def) 00629 continue; 00630 00631 elideCopiesAndPHIs(Def, DefSrcs); 00632 00633 for (SmallVectorImpl<MachineInstr *>::iterator II = DefSrcs.begin(), 00634 EE = DefSrcs.end(); II != EE; ++II) { 00635 MachineInstr *MI = *II; 00636 00637 // If we've already analyzed and replaced this operand, don't do 00638 // anything. 00639 if (Replacements.find(MI) != Replacements.end()) 00640 continue; 00641 00642 // Now, work out if the instruction causes a SPR->DPR dependency. 00643 if (!hasPartialWrite(MI)) 00644 continue; 00645 00646 // Collect all the uses of this MI's DPR def for updating later. 00647 SmallVector<MachineOperand*, 8> Uses; 00648 unsigned DPRDefReg = MI->getOperand(0).getReg(); 00649 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DPRDefReg), 00650 E = MRI->use_end(); I != E; ++I) 00651 Uses.push_back(&*I); 00652 00653 // We can optimize this. 00654 unsigned NewReg = optimizeSDPattern(MI); 00655 00656 if (NewReg != 0) { 00657 Modified = true; 00658 for (SmallVectorImpl<MachineOperand *>::const_iterator I = Uses.begin(), 00659 E = Uses.end(); I != E; ++I) { 00660 // Make sure to constrain the register class of the new register to 00661 // match what we're replacing. Otherwise we can optimize a DPR_VFP2 00662 // reference into a plain DPR, and that will end poorly. NewReg is 00663 // always virtual here, so there will always be a matching subclass 00664 // to find. 00665 MRI->constrainRegClass(NewReg, MRI->getRegClass((*I)->getReg())); 00666 00667 DEBUG(dbgs() << "Replacing operand " 00668 << **I << " with " 00669 << PrintReg(NewReg) << "\n"); 00670 (*I)->substVirtReg(NewReg, 0, *TRI); 00671 } 00672 } 00673 Replacements[MI] = NewReg; 00674 } 00675 } 00676 return Modified; 00677 } 00678 00679 bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) { 00680 TII = static_cast<const ARMBaseInstrInfo *>(Fn.getSubtarget().getInstrInfo()); 00681 TRI = Fn.getSubtarget().getRegisterInfo(); 00682 MRI = &Fn.getRegInfo(); 00683 bool Modified = false; 00684 00685 DEBUG(dbgs() << "Running on function " << Fn.getName()<< "\n"); 00686 00687 DeadInstr.clear(); 00688 Replacements.clear(); 00689 00690 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 00691 ++MFI) { 00692 00693 for (MachineBasicBlock::iterator MI = MFI->begin(), ME = MFI->end(); 00694 MI != ME;) { 00695 Modified |= runOnInstruction(MI++); 00696 } 00697 00698 } 00699 00700 for (std::set<MachineInstr *>::iterator I = DeadInstr.begin(), 00701 E = DeadInstr.end(); 00702 I != E; ++I) { 00703 (*I)->eraseFromParent(); 00704 } 00705 00706 return Modified; 00707 } 00708 00709 FunctionPass *llvm::createA15SDOptimizerPass() { 00710 return new A15SDOptimizer(); 00711 }