Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/Rewriter.cpp - Rewriter -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #define DEBUG_TYPE "virtregrewriter" |
| 11 | #include "VirtRegRewriter.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 12 | #include "llvm/Function.h" |
| 13 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 14 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 15 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetLowering.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DepthFirstIterator.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumDSE , "Number of dead stores elided"); |
| 29 | STATISTIC(NumDSS , "Number of dead spill slots removed"); |
| 30 | STATISTIC(NumCommutes, "Number of instructions commuted"); |
| 31 | STATISTIC(NumDRM , "Number of re-materializable defs elided"); |
| 32 | STATISTIC(NumStores , "Number of stores added"); |
| 33 | STATISTIC(NumPSpills , "Number of physical register spills"); |
| 34 | STATISTIC(NumOmitted , "Number of reloads omited"); |
| 35 | STATISTIC(NumAvoided , "Number of reloads deemed unnecessary"); |
| 36 | STATISTIC(NumCopified, "Number of available reloads turned into copies"); |
| 37 | STATISTIC(NumReMats , "Number of re-materialization"); |
| 38 | STATISTIC(NumLoads , "Number of loads added"); |
| 39 | STATISTIC(NumReused , "Number of values reused"); |
| 40 | STATISTIC(NumDCE , "Number of copies elided"); |
| 41 | STATISTIC(NumSUnfold , "Number of stores unfolded"); |
| 42 | STATISTIC(NumModRefUnfold, "Number of modref unfolded"); |
| 43 | |
| 44 | namespace { |
Lang Hames | ac27640 | 2009-06-04 18:45:36 +0000 | [diff] [blame] | 45 | enum RewriterName { local, trivial }; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static cl::opt<RewriterName> |
| 49 | RewriterOpt("rewriter", |
| 50 | cl::desc("Rewriter to use: (default: local)"), |
| 51 | cl::Prefix, |
Lang Hames | ac27640 | 2009-06-04 18:45:36 +0000 | [diff] [blame] | 52 | cl::values(clEnumVal(local, "local rewriter"), |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 53 | clEnumVal(trivial, "trivial rewriter"), |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 54 | clEnumValEnd), |
| 55 | cl::init(local)); |
| 56 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 57 | static cl::opt<bool> |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 58 | ScheduleSpills("schedule-spills", |
| 59 | cl::desc("Schedule spill code"), |
| 60 | cl::init(false)); |
| 61 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 62 | VirtRegRewriter::~VirtRegRewriter() {} |
| 63 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 64 | namespace { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 65 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 66 | /// This class is intended for use with the new spilling framework only. It |
| 67 | /// rewrites vreg def/uses to use the assigned preg, but does not insert any |
| 68 | /// spill code. |
| 69 | struct VISIBILITY_HIDDEN TrivialRewriter : public VirtRegRewriter { |
| 70 | |
| 71 | bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM, |
| 72 | LiveIntervals* LIs) { |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 73 | DEBUG(errs() << "********** REWRITE MACHINE CODE **********\n"); |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 74 | DEBUG(errs() << "********** Function: " |
| 75 | << MF.getFunction()->getName() << '\n'); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 76 | DEBUG(errs() << "**** Machine Instrs" |
| 77 | << "(NOTE! Does not include spills and reloads!) ****\n"); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 78 | DEBUG(MF.dump()); |
| 79 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 80 | MachineRegisterInfo *mri = &MF.getRegInfo(); |
| 81 | |
| 82 | bool changed = false; |
| 83 | |
| 84 | for (LiveIntervals::iterator liItr = LIs->begin(), liEnd = LIs->end(); |
| 85 | liItr != liEnd; ++liItr) { |
| 86 | |
| 87 | if (TargetRegisterInfo::isVirtualRegister(liItr->first)) { |
| 88 | if (VRM.hasPhys(liItr->first)) { |
| 89 | unsigned preg = VRM.getPhys(liItr->first); |
| 90 | mri->replaceRegWith(liItr->first, preg); |
| 91 | mri->setPhysRegUsed(preg); |
| 92 | changed = true; |
| 93 | } |
| 94 | } |
| 95 | else { |
| 96 | if (!liItr->second->empty()) { |
| 97 | mri->setPhysRegUsed(liItr->first); |
| 98 | } |
| 99 | } |
| 100 | } |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 101 | |
| 102 | |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 103 | DEBUG(errs() << "**** Post Machine Instrs ****\n"); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 104 | DEBUG(MF.dump()); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 105 | |
| 106 | return changed; |
| 107 | } |
| 108 | |
| 109 | }; |
| 110 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 113 | // ************************************************************************ // |
| 114 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 115 | namespace { |
| 116 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 117 | /// AvailableSpills - As the local rewriter is scanning and rewriting an MBB |
| 118 | /// from top down, keep track of which spill slots or remat are available in |
| 119 | /// each register. |
| 120 | /// |
| 121 | /// Note that not all physregs are created equal here. In particular, some |
| 122 | /// physregs are reloads that we are allowed to clobber or ignore at any time. |
| 123 | /// Other physregs are values that the register allocated program is using |
| 124 | /// that we cannot CHANGE, but we can read if we like. We keep track of this |
| 125 | /// on a per-stack-slot / remat id basis as the low bit in the value of the |
| 126 | /// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks |
| 127 | /// this bit and addAvailable sets it if. |
| 128 | class VISIBILITY_HIDDEN AvailableSpills { |
| 129 | const TargetRegisterInfo *TRI; |
| 130 | const TargetInstrInfo *TII; |
| 131 | |
| 132 | // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled |
| 133 | // or remat'ed virtual register values that are still available, due to |
| 134 | // being loaded or stored to, but not invalidated yet. |
| 135 | std::map<int, unsigned> SpillSlotsOrReMatsAvailable; |
| 136 | |
| 137 | // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable, |
| 138 | // indicating which stack slot values are currently held by a physreg. This |
| 139 | // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a |
| 140 | // physreg is modified. |
| 141 | std::multimap<unsigned, int> PhysRegsAvailable; |
| 142 | |
| 143 | void disallowClobberPhysRegOnly(unsigned PhysReg); |
| 144 | |
| 145 | void ClobberPhysRegOnly(unsigned PhysReg); |
| 146 | public: |
| 147 | AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii) |
| 148 | : TRI(tri), TII(tii) { |
| 149 | } |
| 150 | |
| 151 | /// clear - Reset the state. |
| 152 | void clear() { |
| 153 | SpillSlotsOrReMatsAvailable.clear(); |
| 154 | PhysRegsAvailable.clear(); |
| 155 | } |
| 156 | |
| 157 | const TargetRegisterInfo *getRegInfo() const { return TRI; } |
| 158 | |
| 159 | /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is |
| 160 | /// available in a physical register, return that PhysReg, otherwise |
| 161 | /// return 0. |
| 162 | unsigned getSpillSlotOrReMatPhysReg(int Slot) const { |
| 163 | std::map<int, unsigned>::const_iterator I = |
| 164 | SpillSlotsOrReMatsAvailable.find(Slot); |
| 165 | if (I != SpillSlotsOrReMatsAvailable.end()) { |
| 166 | return I->second >> 1; // Remove the CanClobber bit. |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /// addAvailable - Mark that the specified stack slot / remat is available |
| 172 | /// in the specified physreg. If CanClobber is true, the physreg can be |
| 173 | /// modified at any time without changing the semantics of the program. |
| 174 | void addAvailable(int SlotOrReMat, unsigned Reg, bool CanClobber = true) { |
| 175 | // If this stack slot is thought to be available in some other physreg, |
| 176 | // remove its record. |
| 177 | ModifyStackSlotOrReMat(SlotOrReMat); |
| 178 | |
| 179 | PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat)); |
| 180 | SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | |
| 181 | (unsigned)CanClobber; |
| 182 | |
| 183 | if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 184 | DEBUG(errs() << "Remembering RM#" |
| 185 | << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 186 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 187 | DEBUG(errs() << "Remembering SS#" << SlotOrReMat); |
| 188 | DEBUG(errs() << " in physreg " << TRI->getName(Reg) << "\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /// canClobberPhysRegForSS - Return true if the spiller is allowed to change |
| 192 | /// the value of the specified stackslot register if it desires. The |
| 193 | /// specified stack slot must be available in a physreg for this query to |
| 194 | /// make sense. |
| 195 | bool canClobberPhysRegForSS(int SlotOrReMat) const { |
| 196 | assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) && |
| 197 | "Value not available!"); |
| 198 | return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1; |
| 199 | } |
| 200 | |
| 201 | /// canClobberPhysReg - Return true if the spiller is allowed to clobber the |
| 202 | /// physical register where values for some stack slot(s) might be |
| 203 | /// available. |
| 204 | bool canClobberPhysReg(unsigned PhysReg) const { |
| 205 | std::multimap<unsigned, int>::const_iterator I = |
| 206 | PhysRegsAvailable.lower_bound(PhysReg); |
| 207 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
| 208 | int SlotOrReMat = I->second; |
| 209 | I++; |
| 210 | if (!canClobberPhysRegForSS(SlotOrReMat)) |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | /// disallowClobberPhysReg - Unset the CanClobber bit of the specified |
| 217 | /// stackslot register. The register is still available but is no longer |
| 218 | /// allowed to be modifed. |
| 219 | void disallowClobberPhysReg(unsigned PhysReg); |
| 220 | |
| 221 | /// ClobberPhysReg - This is called when the specified physreg changes |
| 222 | /// value. We use this to invalidate any info about stuff that lives in |
| 223 | /// it and any of its aliases. |
| 224 | void ClobberPhysReg(unsigned PhysReg); |
| 225 | |
| 226 | /// ModifyStackSlotOrReMat - This method is called when the value in a stack |
| 227 | /// slot changes. This removes information about which register the |
| 228 | /// previous value for this slot lives in (as the previous value is dead |
| 229 | /// now). |
| 230 | void ModifyStackSlotOrReMat(int SlotOrReMat); |
| 231 | |
| 232 | /// AddAvailableRegsToLiveIn - Availability information is being kept coming |
| 233 | /// into the specified MBB. Add available physical registers as potential |
| 234 | /// live-in's. If they are reused in the MBB, they will be added to the |
| 235 | /// live-in set to make register scavenger and post-allocation scheduler. |
| 236 | void AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, BitVector &RegKills, |
| 237 | std::vector<MachineOperand*> &KillOps); |
| 238 | }; |
| 239 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 242 | // ************************************************************************ // |
| 243 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 244 | // Given a location where a reload of a spilled register or a remat of |
| 245 | // a constant is to be inserted, attempt to find a safe location to |
| 246 | // insert the load at an earlier point in the basic-block, to hide |
| 247 | // latency of the load and to avoid address-generation interlock |
| 248 | // issues. |
| 249 | static MachineBasicBlock::iterator |
| 250 | ComputeReloadLoc(MachineBasicBlock::iterator const InsertLoc, |
| 251 | MachineBasicBlock::iterator const Begin, |
| 252 | unsigned PhysReg, |
| 253 | const TargetRegisterInfo *TRI, |
| 254 | bool DoReMat, |
| 255 | int SSorRMId, |
| 256 | const TargetInstrInfo *TII, |
| 257 | const MachineFunction &MF) |
| 258 | { |
| 259 | if (!ScheduleSpills) |
| 260 | return InsertLoc; |
| 261 | |
| 262 | // Spill backscheduling is of primary interest to addresses, so |
| 263 | // don't do anything if the register isn't in the register class |
| 264 | // used for pointers. |
| 265 | |
| 266 | const TargetLowering *TL = MF.getTarget().getTargetLowering(); |
| 267 | |
| 268 | if (!TL->isTypeLegal(TL->getPointerTy())) |
| 269 | // Believe it or not, this is true on PIC16. |
| 270 | return InsertLoc; |
| 271 | |
| 272 | const TargetRegisterClass *ptrRegClass = |
| 273 | TL->getRegClassFor(TL->getPointerTy()); |
| 274 | if (!ptrRegClass->contains(PhysReg)) |
| 275 | return InsertLoc; |
| 276 | |
| 277 | // Scan upwards through the preceding instructions. If an instruction doesn't |
| 278 | // reference the stack slot or the register we're loading, we can |
| 279 | // backschedule the reload up past it. |
| 280 | MachineBasicBlock::iterator NewInsertLoc = InsertLoc; |
| 281 | while (NewInsertLoc != Begin) { |
| 282 | MachineBasicBlock::iterator Prev = prior(NewInsertLoc); |
| 283 | for (unsigned i = 0; i < Prev->getNumOperands(); ++i) { |
| 284 | MachineOperand &Op = Prev->getOperand(i); |
| 285 | if (!DoReMat && Op.isFI() && Op.getIndex() == SSorRMId) |
| 286 | goto stop; |
| 287 | } |
| 288 | if (Prev->findRegisterUseOperandIdx(PhysReg) != -1 || |
| 289 | Prev->findRegisterDefOperand(PhysReg)) |
| 290 | goto stop; |
| 291 | for (const unsigned *Alias = TRI->getAliasSet(PhysReg); *Alias; ++Alias) |
| 292 | if (Prev->findRegisterUseOperandIdx(*Alias) != -1 || |
| 293 | Prev->findRegisterDefOperand(*Alias)) |
| 294 | goto stop; |
| 295 | NewInsertLoc = Prev; |
| 296 | } |
| 297 | stop:; |
| 298 | |
| 299 | // If we made it to the beginning of the block, turn around and move back |
| 300 | // down just past any existing reloads. They're likely to be reloads/remats |
| 301 | // for instructions earlier than what our current reload/remat is for, so |
| 302 | // they should be scheduled earlier. |
| 303 | if (NewInsertLoc == Begin) { |
| 304 | int FrameIdx; |
| 305 | while (InsertLoc != NewInsertLoc && |
| 306 | (TII->isLoadFromStackSlot(NewInsertLoc, FrameIdx) || |
| 307 | TII->isTriviallyReMaterializable(NewInsertLoc))) |
| 308 | ++NewInsertLoc; |
| 309 | } |
| 310 | |
| 311 | return NewInsertLoc; |
| 312 | } |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 313 | |
| 314 | namespace { |
| 315 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 316 | // ReusedOp - For each reused operand, we keep track of a bit of information, |
| 317 | // in case we need to rollback upon processing a new operand. See comments |
| 318 | // below. |
| 319 | struct ReusedOp { |
| 320 | // The MachineInstr operand that reused an available value. |
| 321 | unsigned Operand; |
| 322 | |
| 323 | // StackSlotOrReMat - The spill slot or remat id of the value being reused. |
| 324 | unsigned StackSlotOrReMat; |
| 325 | |
| 326 | // PhysRegReused - The physical register the value was available in. |
| 327 | unsigned PhysRegReused; |
| 328 | |
| 329 | // AssignedPhysReg - The physreg that was assigned for use by the reload. |
| 330 | unsigned AssignedPhysReg; |
| 331 | |
| 332 | // VirtReg - The virtual register itself. |
| 333 | unsigned VirtReg; |
| 334 | |
| 335 | ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr, |
| 336 | unsigned vreg) |
| 337 | : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr), |
| 338 | AssignedPhysReg(apr), VirtReg(vreg) {} |
| 339 | }; |
| 340 | |
| 341 | /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that |
| 342 | /// is reused instead of reloaded. |
| 343 | class VISIBILITY_HIDDEN ReuseInfo { |
| 344 | MachineInstr &MI; |
| 345 | std::vector<ReusedOp> Reuses; |
| 346 | BitVector PhysRegsClobbered; |
| 347 | public: |
| 348 | ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) { |
| 349 | PhysRegsClobbered.resize(tri->getNumRegs()); |
| 350 | } |
| 351 | |
| 352 | bool hasReuses() const { |
| 353 | return !Reuses.empty(); |
| 354 | } |
| 355 | |
| 356 | /// addReuse - If we choose to reuse a virtual register that is already |
| 357 | /// available instead of reloading it, remember that we did so. |
| 358 | void addReuse(unsigned OpNo, unsigned StackSlotOrReMat, |
| 359 | unsigned PhysRegReused, unsigned AssignedPhysReg, |
| 360 | unsigned VirtReg) { |
| 361 | // If the reload is to the assigned register anyway, no undo will be |
| 362 | // required. |
| 363 | if (PhysRegReused == AssignedPhysReg) return; |
| 364 | |
| 365 | // Otherwise, remember this. |
| 366 | Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused, |
| 367 | AssignedPhysReg, VirtReg)); |
| 368 | } |
| 369 | |
| 370 | void markClobbered(unsigned PhysReg) { |
| 371 | PhysRegsClobbered.set(PhysReg); |
| 372 | } |
| 373 | |
| 374 | bool isClobbered(unsigned PhysReg) const { |
| 375 | return PhysRegsClobbered.test(PhysReg); |
| 376 | } |
| 377 | |
| 378 | /// GetRegForReload - We are about to emit a reload into PhysReg. If there |
| 379 | /// is some other operand that is using the specified register, either pick |
| 380 | /// a new register to use, or evict the previous reload and use this reg. |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 381 | unsigned GetRegForReload(const TargetRegisterClass *RC, unsigned PhysReg, |
| 382 | MachineFunction &MF, MachineInstr *MI, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 383 | AvailableSpills &Spills, |
| 384 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 385 | SmallSet<unsigned, 8> &Rejected, |
| 386 | BitVector &RegKills, |
| 387 | std::vector<MachineOperand*> &KillOps, |
| 388 | VirtRegMap &VRM); |
| 389 | |
| 390 | /// GetRegForReload - Helper for the above GetRegForReload(). Add a |
| 391 | /// 'Rejected' set to remember which registers have been considered and |
| 392 | /// rejected for the reload. This avoids infinite looping in case like |
| 393 | /// this: |
| 394 | /// t1 := op t2, t3 |
| 395 | /// t2 <- assigned r0 for use by the reload but ended up reuse r1 |
| 396 | /// t3 <- assigned r1 for use by the reload but ended up reuse r0 |
| 397 | /// t1 <- desires r1 |
| 398 | /// sees r1 is taken by t2, tries t2's reload register r0 |
| 399 | /// sees r0 is taken by t3, tries t3's reload register r1 |
| 400 | /// sees r1 is taken by t2, tries t2's reload register r0 ... |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 401 | unsigned GetRegForReload(unsigned VirtReg, unsigned PhysReg, MachineInstr *MI, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 402 | AvailableSpills &Spills, |
| 403 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 404 | BitVector &RegKills, |
| 405 | std::vector<MachineOperand*> &KillOps, |
| 406 | VirtRegMap &VRM) { |
| 407 | SmallSet<unsigned, 8> Rejected; |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 408 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 409 | const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg); |
| 410 | return GetRegForReload(RC, PhysReg, MF, MI, Spills, MaybeDeadStores, |
| 411 | Rejected, RegKills, KillOps, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 412 | } |
| 413 | }; |
| 414 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 415 | } |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 416 | |
| 417 | // ****************** // |
| 418 | // Utility Functions // |
| 419 | // ****************** // |
| 420 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 421 | /// findSinglePredSuccessor - Return via reference a vector of machine basic |
| 422 | /// blocks each of which is a successor of the specified BB and has no other |
| 423 | /// predecessor. |
| 424 | static void findSinglePredSuccessor(MachineBasicBlock *MBB, |
| 425 | SmallVectorImpl<MachineBasicBlock *> &Succs) { |
| 426 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 427 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 428 | MachineBasicBlock *SuccMBB = *SI; |
| 429 | if (SuccMBB->pred_size() == 1) |
| 430 | Succs.push_back(SuccMBB); |
| 431 | } |
| 432 | } |
| 433 | |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 434 | /// InvalidateKill - Invalidate register kill information for a specific |
| 435 | /// register. This also unsets the kills marker on the last kill operand. |
| 436 | static void InvalidateKill(unsigned Reg, |
| 437 | const TargetRegisterInfo* TRI, |
| 438 | BitVector &RegKills, |
| 439 | std::vector<MachineOperand*> &KillOps) { |
| 440 | if (RegKills[Reg]) { |
| 441 | KillOps[Reg]->setIsKill(false); |
Evan Cheng | 2c48fe6 | 2009-06-03 09:00:27 +0000 | [diff] [blame] | 442 | // KillOps[Reg] might be a def of a super-register. |
| 443 | unsigned KReg = KillOps[Reg]->getReg(); |
| 444 | KillOps[KReg] = NULL; |
| 445 | RegKills.reset(KReg); |
| 446 | for (const unsigned *SR = TRI->getSubRegisters(KReg); *SR; ++SR) { |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 447 | if (RegKills[*SR]) { |
| 448 | KillOps[*SR]->setIsKill(false); |
| 449 | KillOps[*SR] = NULL; |
| 450 | RegKills.reset(*SR); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 456 | /// InvalidateKills - MI is going to be deleted. If any of its operands are |
| 457 | /// marked kill, then invalidate the information. |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 458 | static void InvalidateKills(MachineInstr &MI, |
| 459 | const TargetRegisterInfo* TRI, |
| 460 | BitVector &RegKills, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 461 | std::vector<MachineOperand*> &KillOps, |
| 462 | SmallVector<unsigned, 2> *KillRegs = NULL) { |
| 463 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 464 | MachineOperand &MO = MI.getOperand(i); |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 465 | if (!MO.isReg() || !MO.isUse() || !MO.isKill() || MO.isUndef()) |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 466 | continue; |
| 467 | unsigned Reg = MO.getReg(); |
| 468 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 469 | continue; |
| 470 | if (KillRegs) |
| 471 | KillRegs->push_back(Reg); |
| 472 | assert(Reg < KillOps.size()); |
| 473 | if (KillOps[Reg] == &MO) { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 474 | KillOps[Reg] = NULL; |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 475 | RegKills.reset(Reg); |
| 476 | for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { |
| 477 | if (RegKills[*SR]) { |
| 478 | KillOps[*SR] = NULL; |
| 479 | RegKills.reset(*SR); |
| 480 | } |
| 481 | } |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /// InvalidateRegDef - If the def operand of the specified def MI is now dead |
| 487 | /// (since it's spill instruction is removed), mark it isDead. Also checks if |
| 488 | /// the def MI has other definition operands that are not dead. Returns it by |
| 489 | /// reference. |
| 490 | static bool InvalidateRegDef(MachineBasicBlock::iterator I, |
| 491 | MachineInstr &NewDef, unsigned Reg, |
| 492 | bool &HasLiveDef) { |
| 493 | // Due to remat, it's possible this reg isn't being reused. That is, |
| 494 | // the def of this reg (by prev MI) is now dead. |
| 495 | MachineInstr *DefMI = I; |
| 496 | MachineOperand *DefOp = NULL; |
| 497 | for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) { |
| 498 | MachineOperand &MO = DefMI->getOperand(i); |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 499 | if (!MO.isReg() || !MO.isUse() || !MO.isKill() || MO.isUndef()) |
| 500 | continue; |
| 501 | if (MO.getReg() == Reg) |
| 502 | DefOp = &MO; |
| 503 | else if (!MO.isDead()) |
| 504 | HasLiveDef = true; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 505 | } |
| 506 | if (!DefOp) |
| 507 | return false; |
| 508 | |
| 509 | bool FoundUse = false, Done = false; |
| 510 | MachineBasicBlock::iterator E = &NewDef; |
| 511 | ++I; ++E; |
| 512 | for (; !Done && I != E; ++I) { |
| 513 | MachineInstr *NMI = I; |
| 514 | for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) { |
| 515 | MachineOperand &MO = NMI->getOperand(j); |
| 516 | if (!MO.isReg() || MO.getReg() != Reg) |
| 517 | continue; |
| 518 | if (MO.isUse()) |
| 519 | FoundUse = true; |
| 520 | Done = true; // Stop after scanning all the operands of this MI. |
| 521 | } |
| 522 | } |
| 523 | if (!FoundUse) { |
| 524 | // Def is dead! |
| 525 | DefOp->setIsDead(); |
| 526 | return true; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | /// UpdateKills - Track and update kill info. If a MI reads a register that is |
| 532 | /// marked kill, then it must be due to register reuse. Transfer the kill info |
| 533 | /// over. |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 534 | static void UpdateKills(MachineInstr &MI, const TargetRegisterInfo* TRI, |
| 535 | BitVector &RegKills, |
| 536 | std::vector<MachineOperand*> &KillOps) { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 537 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 538 | MachineOperand &MO = MI.getOperand(i); |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 539 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 540 | continue; |
| 541 | unsigned Reg = MO.getReg(); |
| 542 | if (Reg == 0) |
| 543 | continue; |
| 544 | |
| 545 | if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) { |
| 546 | // That can't be right. Register is killed but not re-defined and it's |
| 547 | // being reused. Let's fix that. |
| 548 | KillOps[Reg]->setIsKill(false); |
Evan Cheng | 2c48fe6 | 2009-06-03 09:00:27 +0000 | [diff] [blame] | 549 | // KillOps[Reg] might be a def of a super-register. |
| 550 | unsigned KReg = KillOps[Reg]->getReg(); |
| 551 | KillOps[KReg] = NULL; |
| 552 | RegKills.reset(KReg); |
| 553 | |
| 554 | // Must be a def of a super-register. Its other sub-regsters are no |
| 555 | // longer killed as well. |
| 556 | for (const unsigned *SR = TRI->getSubRegisters(KReg); *SR; ++SR) { |
| 557 | KillOps[*SR] = NULL; |
| 558 | RegKills.reset(*SR); |
| 559 | } |
| 560 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 561 | if (!MI.isRegTiedToDefOperand(i)) |
| 562 | // Unless it's a two-address operand, this is the new kill. |
| 563 | MO.setIsKill(); |
| 564 | } |
| 565 | if (MO.isKill()) { |
| 566 | RegKills.set(Reg); |
| 567 | KillOps[Reg] = &MO; |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 568 | for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { |
| 569 | RegKills.set(*SR); |
| 570 | KillOps[*SR] = &MO; |
| 571 | } |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 576 | const MachineOperand &MO = MI.getOperand(i); |
| 577 | if (!MO.isReg() || !MO.isDef()) |
| 578 | continue; |
| 579 | unsigned Reg = MO.getReg(); |
| 580 | RegKills.reset(Reg); |
| 581 | KillOps[Reg] = NULL; |
| 582 | // It also defines (or partially define) aliases. |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 583 | for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) { |
| 584 | RegKills.reset(*SR); |
| 585 | KillOps[*SR] = NULL; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /// ReMaterialize - Re-materialize definition for Reg targetting DestReg. |
| 591 | /// |
| 592 | static void ReMaterialize(MachineBasicBlock &MBB, |
| 593 | MachineBasicBlock::iterator &MII, |
| 594 | unsigned DestReg, unsigned Reg, |
| 595 | const TargetInstrInfo *TII, |
| 596 | const TargetRegisterInfo *TRI, |
| 597 | VirtRegMap &VRM) { |
Evan Cheng | 5f15992 | 2009-07-16 20:15:00 +0000 | [diff] [blame] | 598 | MachineInstr *ReMatDefMI = VRM.getReMaterializedMI(Reg); |
Daniel Dunbar | 24cd3c4 | 2009-07-16 22:08:25 +0000 | [diff] [blame] | 599 | #ifndef NDEBUG |
Evan Cheng | 5f15992 | 2009-07-16 20:15:00 +0000 | [diff] [blame] | 600 | const TargetInstrDesc &TID = ReMatDefMI->getDesc(); |
Evan Cheng | c1b46f9 | 2009-07-17 00:32:06 +0000 | [diff] [blame] | 601 | assert(TID.getNumDefs() == 1 && |
Evan Cheng | 5f15992 | 2009-07-16 20:15:00 +0000 | [diff] [blame] | 602 | "Don't know how to remat instructions that define > 1 values!"); |
| 603 | #endif |
| 604 | TII->reMaterialize(MBB, MII, DestReg, |
| 605 | ReMatDefMI->getOperand(0).getSubReg(), ReMatDefMI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 606 | MachineInstr *NewMI = prior(MII); |
| 607 | for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { |
| 608 | MachineOperand &MO = NewMI->getOperand(i); |
| 609 | if (!MO.isReg() || MO.getReg() == 0) |
| 610 | continue; |
| 611 | unsigned VirtReg = MO.getReg(); |
| 612 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) |
| 613 | continue; |
| 614 | assert(MO.isUse()); |
| 615 | unsigned SubIdx = MO.getSubReg(); |
| 616 | unsigned Phys = VRM.getPhys(VirtReg); |
| 617 | assert(Phys); |
| 618 | unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys; |
| 619 | MO.setReg(RReg); |
| 620 | MO.setSubReg(0); |
| 621 | } |
| 622 | ++NumReMats; |
| 623 | } |
| 624 | |
| 625 | /// findSuperReg - Find the SubReg's super-register of given register class |
| 626 | /// where its SubIdx sub-register is SubReg. |
| 627 | static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg, |
| 628 | unsigned SubIdx, const TargetRegisterInfo *TRI) { |
| 629 | for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); |
| 630 | I != E; ++I) { |
| 631 | unsigned Reg = *I; |
| 632 | if (TRI->getSubReg(Reg, SubIdx) == SubReg) |
| 633 | return Reg; |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | // ******************************** // |
| 639 | // Available Spills Implementation // |
| 640 | // ******************************** // |
| 641 | |
| 642 | /// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified |
| 643 | /// stackslot register. The register is still available but is no longer |
| 644 | /// allowed to be modifed. |
| 645 | void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) { |
| 646 | std::multimap<unsigned, int>::iterator I = |
| 647 | PhysRegsAvailable.lower_bound(PhysReg); |
| 648 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
| 649 | int SlotOrReMat = I->second; |
| 650 | I++; |
| 651 | assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && |
| 652 | "Bidirectional map mismatch!"); |
| 653 | SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1; |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 654 | DEBUG(errs() << "PhysReg " << TRI->getName(PhysReg) |
| 655 | << " copied, it is available for use but can no longer be modified\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | /// disallowClobberPhysReg - Unset the CanClobber bit of the specified |
| 660 | /// stackslot register and its aliases. The register and its aliases may |
| 661 | /// still available but is no longer allowed to be modifed. |
| 662 | void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) { |
| 663 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) |
| 664 | disallowClobberPhysRegOnly(*AS); |
| 665 | disallowClobberPhysRegOnly(PhysReg); |
| 666 | } |
| 667 | |
| 668 | /// ClobberPhysRegOnly - This is called when the specified physreg changes |
| 669 | /// value. We use this to invalidate any info about stuff we thing lives in it. |
| 670 | void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) { |
| 671 | std::multimap<unsigned, int>::iterator I = |
| 672 | PhysRegsAvailable.lower_bound(PhysReg); |
| 673 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
| 674 | int SlotOrReMat = I->second; |
| 675 | PhysRegsAvailable.erase(I++); |
| 676 | assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && |
| 677 | "Bidirectional map mismatch!"); |
| 678 | SpillSlotsOrReMatsAvailable.erase(SlotOrReMat); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 679 | DEBUG(errs() << "PhysReg " << TRI->getName(PhysReg) |
| 680 | << " clobbered, invalidating "); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 681 | if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 682 | DEBUG(errs() << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 <<"\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 683 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 684 | DEBUG(errs() << "SS#" << SlotOrReMat << "\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
| 688 | /// ClobberPhysReg - This is called when the specified physreg changes |
| 689 | /// value. We use this to invalidate any info about stuff we thing lives in |
| 690 | /// it and any of its aliases. |
| 691 | void AvailableSpills::ClobberPhysReg(unsigned PhysReg) { |
| 692 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) |
| 693 | ClobberPhysRegOnly(*AS); |
| 694 | ClobberPhysRegOnly(PhysReg); |
| 695 | } |
| 696 | |
| 697 | /// AddAvailableRegsToLiveIn - Availability information is being kept coming |
| 698 | /// into the specified MBB. Add available physical registers as potential |
| 699 | /// live-in's. If they are reused in the MBB, they will be added to the |
| 700 | /// live-in set to make register scavenger and post-allocation scheduler. |
| 701 | void AvailableSpills::AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, |
| 702 | BitVector &RegKills, |
| 703 | std::vector<MachineOperand*> &KillOps) { |
| 704 | std::set<unsigned> NotAvailable; |
| 705 | for (std::multimap<unsigned, int>::iterator |
| 706 | I = PhysRegsAvailable.begin(), E = PhysRegsAvailable.end(); |
| 707 | I != E; ++I) { |
| 708 | unsigned Reg = I->first; |
| 709 | const TargetRegisterClass* RC = TRI->getPhysicalRegisterRegClass(Reg); |
| 710 | // FIXME: A temporary workaround. We can't reuse available value if it's |
| 711 | // not safe to move the def of the virtual register's class. e.g. |
| 712 | // X86::RFP* register classes. Do not add it as a live-in. |
| 713 | if (!TII->isSafeToMoveRegClassDefs(RC)) |
| 714 | // This is no longer available. |
| 715 | NotAvailable.insert(Reg); |
| 716 | else { |
| 717 | MBB.addLiveIn(Reg); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 718 | InvalidateKill(Reg, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // Skip over the same register. |
| 722 | std::multimap<unsigned, int>::iterator NI = next(I); |
| 723 | while (NI != E && NI->first == Reg) { |
| 724 | ++I; |
| 725 | ++NI; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | for (std::set<unsigned>::iterator I = NotAvailable.begin(), |
| 730 | E = NotAvailable.end(); I != E; ++I) { |
| 731 | ClobberPhysReg(*I); |
| 732 | for (const unsigned *SubRegs = TRI->getSubRegisters(*I); |
| 733 | *SubRegs; ++SubRegs) |
| 734 | ClobberPhysReg(*SubRegs); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /// ModifyStackSlotOrReMat - This method is called when the value in a stack |
| 739 | /// slot changes. This removes information about which register the previous |
| 740 | /// value for this slot lives in (as the previous value is dead now). |
| 741 | void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) { |
| 742 | std::map<int, unsigned>::iterator It = |
| 743 | SpillSlotsOrReMatsAvailable.find(SlotOrReMat); |
| 744 | if (It == SpillSlotsOrReMatsAvailable.end()) return; |
| 745 | unsigned Reg = It->second >> 1; |
| 746 | SpillSlotsOrReMatsAvailable.erase(It); |
| 747 | |
| 748 | // This register may hold the value of multiple stack slots, only remove this |
| 749 | // stack slot from the set of values the register contains. |
| 750 | std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg); |
| 751 | for (; ; ++I) { |
| 752 | assert(I != PhysRegsAvailable.end() && I->first == Reg && |
| 753 | "Map inverse broken!"); |
| 754 | if (I->second == SlotOrReMat) break; |
| 755 | } |
| 756 | PhysRegsAvailable.erase(I); |
| 757 | } |
| 758 | |
| 759 | // ************************** // |
| 760 | // Reuse Info Implementation // |
| 761 | // ************************** // |
| 762 | |
| 763 | /// GetRegForReload - We are about to emit a reload into PhysReg. If there |
| 764 | /// is some other operand that is using the specified register, either pick |
| 765 | /// a new register to use, or evict the previous reload and use this reg. |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 766 | unsigned ReuseInfo::GetRegForReload(const TargetRegisterClass *RC, |
| 767 | unsigned PhysReg, |
| 768 | MachineFunction &MF, |
| 769 | MachineInstr *MI, AvailableSpills &Spills, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 770 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 771 | SmallSet<unsigned, 8> &Rejected, |
| 772 | BitVector &RegKills, |
| 773 | std::vector<MachineOperand*> &KillOps, |
| 774 | VirtRegMap &VRM) { |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 775 | const TargetInstrInfo* TII = MF.getTarget().getInstrInfo(); |
| 776 | const TargetRegisterInfo *TRI = Spills.getRegInfo(); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 777 | |
| 778 | if (Reuses.empty()) return PhysReg; // This is most often empty. |
| 779 | |
| 780 | for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) { |
| 781 | ReusedOp &Op = Reuses[ro]; |
| 782 | // If we find some other reuse that was supposed to use this register |
| 783 | // exactly for its reload, we can change this reload to use ITS reload |
| 784 | // register. That is, unless its reload register has already been |
| 785 | // considered and subsequently rejected because it has also been reused |
| 786 | // by another operand. |
| 787 | if (Op.PhysRegReused == PhysReg && |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 788 | Rejected.count(Op.AssignedPhysReg) == 0 && |
| 789 | RC->contains(Op.AssignedPhysReg)) { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 790 | // Yup, use the reload register that we didn't use before. |
| 791 | unsigned NewReg = Op.AssignedPhysReg; |
| 792 | Rejected.insert(PhysReg); |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 793 | return GetRegForReload(RC, NewReg, MF, MI, Spills, MaybeDeadStores, Rejected, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 794 | RegKills, KillOps, VRM); |
| 795 | } else { |
| 796 | // Otherwise, we might also have a problem if a previously reused |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 797 | // value aliases the new register. If so, codegen the previous reload |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 798 | // and use this one. |
| 799 | unsigned PRRU = Op.PhysRegReused; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 800 | if (TRI->areAliases(PRRU, PhysReg)) { |
| 801 | // Okay, we found out that an alias of a reused register |
| 802 | // was used. This isn't good because it means we have |
| 803 | // to undo a previous reuse. |
| 804 | MachineBasicBlock *MBB = MI->getParent(); |
| 805 | const TargetRegisterClass *AliasRC = |
| 806 | MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg); |
| 807 | |
| 808 | // Copy Op out of the vector and remove it, we're going to insert an |
| 809 | // explicit load for it. |
| 810 | ReusedOp NewOp = Op; |
| 811 | Reuses.erase(Reuses.begin()+ro); |
| 812 | |
| 813 | // Ok, we're going to try to reload the assigned physreg into the |
| 814 | // slot that we were supposed to in the first place. However, that |
| 815 | // register could hold a reuse. Check to see if it conflicts or |
| 816 | // would prefer us to use a different register. |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 817 | unsigned NewPhysReg = GetRegForReload(RC, NewOp.AssignedPhysReg, |
| 818 | MF, MI, Spills, MaybeDeadStores, |
| 819 | Rejected, RegKills, KillOps, VRM); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 820 | |
| 821 | bool DoReMat = NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT; |
| 822 | int SSorRMId = DoReMat |
| 823 | ? VRM.getReMatId(NewOp.VirtReg) : NewOp.StackSlotOrReMat; |
| 824 | |
| 825 | // Back-schedule reloads and remats. |
| 826 | MachineBasicBlock::iterator InsertLoc = |
| 827 | ComputeReloadLoc(MI, MBB->begin(), PhysReg, TRI, |
| 828 | DoReMat, SSorRMId, TII, MF); |
| 829 | |
| 830 | if (DoReMat) { |
| 831 | ReMaterialize(*MBB, InsertLoc, NewPhysReg, NewOp.VirtReg, TII, |
| 832 | TRI, VRM); |
| 833 | } else { |
| 834 | TII->loadRegFromStackSlot(*MBB, InsertLoc, NewPhysReg, |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 835 | NewOp.StackSlotOrReMat, AliasRC); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 836 | MachineInstr *LoadMI = prior(InsertLoc); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 837 | VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI); |
| 838 | // Any stores to this stack slot are not dead anymore. |
| 839 | MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL; |
| 840 | ++NumLoads; |
| 841 | } |
| 842 | Spills.ClobberPhysReg(NewPhysReg); |
| 843 | Spills.ClobberPhysReg(NewOp.PhysRegReused); |
| 844 | |
| 845 | unsigned SubIdx = MI->getOperand(NewOp.Operand).getSubReg(); |
| 846 | unsigned RReg = SubIdx ? TRI->getSubReg(NewPhysReg, SubIdx) : NewPhysReg; |
| 847 | MI->getOperand(NewOp.Operand).setReg(RReg); |
| 848 | MI->getOperand(NewOp.Operand).setSubReg(0); |
| 849 | |
| 850 | Spills.addAvailable(NewOp.StackSlotOrReMat, NewPhysReg); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 851 | UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 852 | DEBUG(errs() << '\t' << *prior(InsertLoc)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 854 | DEBUG(errs() << "Reuse undone!\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 855 | --NumReused; |
| 856 | |
| 857 | // Finally, PhysReg is now available, go ahead and use it. |
| 858 | return PhysReg; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | return PhysReg; |
| 863 | } |
| 864 | |
| 865 | // ************************************************************************ // |
| 866 | |
| 867 | /// FoldsStackSlotModRef - Return true if the specified MI folds the specified |
| 868 | /// stack slot mod/ref. It also checks if it's possible to unfold the |
| 869 | /// instruction by having it define a specified physical register instead. |
| 870 | static bool FoldsStackSlotModRef(MachineInstr &MI, int SS, unsigned PhysReg, |
| 871 | const TargetInstrInfo *TII, |
| 872 | const TargetRegisterInfo *TRI, |
| 873 | VirtRegMap &VRM) { |
| 874 | if (VRM.hasEmergencySpills(&MI) || VRM.isSpillPt(&MI)) |
| 875 | return false; |
| 876 | |
| 877 | bool Found = false; |
| 878 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
| 879 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) { |
| 880 | unsigned VirtReg = I->second.first; |
| 881 | VirtRegMap::ModRef MR = I->second.second; |
| 882 | if (MR & VirtRegMap::isModRef) |
| 883 | if (VRM.getStackSlot(VirtReg) == SS) { |
| 884 | Found= TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), true, true) != 0; |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | if (!Found) |
| 889 | return false; |
| 890 | |
| 891 | // Does the instruction uses a register that overlaps the scratch register? |
| 892 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 893 | MachineOperand &MO = MI.getOperand(i); |
| 894 | if (!MO.isReg() || MO.getReg() == 0) |
| 895 | continue; |
| 896 | unsigned Reg = MO.getReg(); |
| 897 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 898 | if (!VRM.hasPhys(Reg)) |
| 899 | continue; |
| 900 | Reg = VRM.getPhys(Reg); |
| 901 | } |
| 902 | if (TRI->regsOverlap(PhysReg, Reg)) |
| 903 | return false; |
| 904 | } |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | /// FindFreeRegister - Find a free register of a given register class by looking |
| 909 | /// at (at most) the last two machine instructions. |
| 910 | static unsigned FindFreeRegister(MachineBasicBlock::iterator MII, |
| 911 | MachineBasicBlock &MBB, |
| 912 | const TargetRegisterClass *RC, |
| 913 | const TargetRegisterInfo *TRI, |
| 914 | BitVector &AllocatableRegs) { |
| 915 | BitVector Defs(TRI->getNumRegs()); |
| 916 | BitVector Uses(TRI->getNumRegs()); |
| 917 | SmallVector<unsigned, 4> LocalUses; |
| 918 | SmallVector<unsigned, 4> Kills; |
| 919 | |
| 920 | // Take a look at 2 instructions at most. |
| 921 | for (unsigned Count = 0; Count < 2; ++Count) { |
| 922 | if (MII == MBB.begin()) |
| 923 | break; |
| 924 | MachineInstr *PrevMI = prior(MII); |
| 925 | for (unsigned i = 0, e = PrevMI->getNumOperands(); i != e; ++i) { |
| 926 | MachineOperand &MO = PrevMI->getOperand(i); |
| 927 | if (!MO.isReg() || MO.getReg() == 0) |
| 928 | continue; |
| 929 | unsigned Reg = MO.getReg(); |
| 930 | if (MO.isDef()) { |
| 931 | Defs.set(Reg); |
| 932 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 933 | Defs.set(*AS); |
| 934 | } else { |
| 935 | LocalUses.push_back(Reg); |
| 936 | if (MO.isKill() && AllocatableRegs[Reg]) |
| 937 | Kills.push_back(Reg); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) { |
| 942 | unsigned Kill = Kills[i]; |
| 943 | if (!Defs[Kill] && !Uses[Kill] && |
| 944 | TRI->getPhysicalRegisterRegClass(Kill) == RC) |
| 945 | return Kill; |
| 946 | } |
| 947 | for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) { |
| 948 | unsigned Reg = LocalUses[i]; |
| 949 | Uses.set(Reg); |
| 950 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 951 | Uses.set(*AS); |
| 952 | } |
| 953 | |
| 954 | MII = PrevMI; |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | static |
| 961 | void AssignPhysToVirtReg(MachineInstr *MI, unsigned VirtReg, unsigned PhysReg) { |
| 962 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 963 | MachineOperand &MO = MI->getOperand(i); |
| 964 | if (MO.isReg() && MO.getReg() == VirtReg) |
| 965 | MO.setReg(PhysReg); |
| 966 | } |
| 967 | } |
| 968 | |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 969 | namespace { |
| 970 | struct RefSorter { |
| 971 | bool operator()(const std::pair<MachineInstr*, int> &A, |
| 972 | const std::pair<MachineInstr*, int> &B) { |
| 973 | return A.second < B.second; |
| 974 | } |
| 975 | }; |
| 976 | } |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 977 | |
| 978 | // ***************************** // |
| 979 | // Local Spiller Implementation // |
| 980 | // ***************************** // |
| 981 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 982 | namespace { |
| 983 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 984 | class VISIBILITY_HIDDEN LocalRewriter : public VirtRegRewriter { |
| 985 | MachineRegisterInfo *RegInfo; |
| 986 | const TargetRegisterInfo *TRI; |
| 987 | const TargetInstrInfo *TII; |
| 988 | BitVector AllocatableRegs; |
| 989 | DenseMap<MachineInstr*, unsigned> DistanceMap; |
| 990 | public: |
| 991 | |
| 992 | bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM, |
| 993 | LiveIntervals* LIs) { |
| 994 | RegInfo = &MF.getRegInfo(); |
| 995 | TRI = MF.getTarget().getRegisterInfo(); |
| 996 | TII = MF.getTarget().getInstrInfo(); |
| 997 | AllocatableRegs = TRI->getAllocatableSet(MF); |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 998 | DEBUG(errs() << "\n**** Local spiller rewriting function '" |
| 999 | << MF.getFunction()->getName() << "':\n"); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1000 | DEBUG(errs() << "**** Machine Instrs (NOTE! Does not include spills and" |
| 1001 | " reloads!) ****\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1002 | DEBUG(MF.dump()); |
| 1003 | |
| 1004 | // Spills - Keep track of which spilled values are available in physregs |
| 1005 | // so that we can choose to reuse the physregs instead of emitting |
| 1006 | // reloads. This is usually refreshed per basic block. |
| 1007 | AvailableSpills Spills(TRI, TII); |
| 1008 | |
| 1009 | // Keep track of kill information. |
| 1010 | BitVector RegKills(TRI->getNumRegs()); |
| 1011 | std::vector<MachineOperand*> KillOps; |
| 1012 | KillOps.resize(TRI->getNumRegs(), NULL); |
| 1013 | |
| 1014 | // SingleEntrySuccs - Successor blocks which have a single predecessor. |
| 1015 | SmallVector<MachineBasicBlock*, 4> SinglePredSuccs; |
| 1016 | SmallPtrSet<MachineBasicBlock*,16> EarlyVisited; |
| 1017 | |
| 1018 | // Traverse the basic blocks depth first. |
| 1019 | MachineBasicBlock *Entry = MF.begin(); |
| 1020 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
| 1021 | for (df_ext_iterator<MachineBasicBlock*, |
| 1022 | SmallPtrSet<MachineBasicBlock*,16> > |
| 1023 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 1024 | DFI != E; ++DFI) { |
| 1025 | MachineBasicBlock *MBB = *DFI; |
| 1026 | if (!EarlyVisited.count(MBB)) |
| 1027 | RewriteMBB(*MBB, VRM, LIs, Spills, RegKills, KillOps); |
| 1028 | |
| 1029 | // If this MBB is the only predecessor of a successor. Keep the |
| 1030 | // availability information and visit it next. |
| 1031 | do { |
| 1032 | // Keep visiting single predecessor successor as long as possible. |
| 1033 | SinglePredSuccs.clear(); |
| 1034 | findSinglePredSuccessor(MBB, SinglePredSuccs); |
| 1035 | if (SinglePredSuccs.empty()) |
| 1036 | MBB = 0; |
| 1037 | else { |
| 1038 | // FIXME: More than one successors, each of which has MBB has |
| 1039 | // the only predecessor. |
| 1040 | MBB = SinglePredSuccs[0]; |
| 1041 | if (!Visited.count(MBB) && EarlyVisited.insert(MBB)) { |
| 1042 | Spills.AddAvailableRegsToLiveIn(*MBB, RegKills, KillOps); |
| 1043 | RewriteMBB(*MBB, VRM, LIs, Spills, RegKills, KillOps); |
| 1044 | } |
| 1045 | } |
| 1046 | } while (MBB); |
| 1047 | |
| 1048 | // Clear the availability info. |
| 1049 | Spills.clear(); |
| 1050 | } |
| 1051 | |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1052 | DEBUG(errs() << "**** Post Machine Instrs ****\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1053 | DEBUG(MF.dump()); |
| 1054 | |
| 1055 | // Mark unused spill slots. |
| 1056 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1057 | int SS = VRM.getLowSpillSlot(); |
| 1058 | if (SS != VirtRegMap::NO_STACK_SLOT) |
| 1059 | for (int e = VRM.getHighSpillSlot(); SS <= e; ++SS) |
| 1060 | if (!VRM.isSpillSlotUsed(SS)) { |
| 1061 | MFI->RemoveStackObject(SS); |
| 1062 | ++NumDSS; |
| 1063 | } |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | private: |
| 1069 | |
| 1070 | /// OptimizeByUnfold2 - Unfold a series of load / store folding instructions if |
| 1071 | /// a scratch register is available. |
| 1072 | /// xorq %r12<kill>, %r13 |
| 1073 | /// addq %rax, -184(%rbp) |
| 1074 | /// addq %r13, -184(%rbp) |
| 1075 | /// ==> |
| 1076 | /// xorq %r12<kill>, %r13 |
| 1077 | /// movq -184(%rbp), %r12 |
| 1078 | /// addq %rax, %r12 |
| 1079 | /// addq %r13, %r12 |
| 1080 | /// movq %r12, -184(%rbp) |
| 1081 | bool OptimizeByUnfold2(unsigned VirtReg, int SS, |
| 1082 | MachineBasicBlock &MBB, |
| 1083 | MachineBasicBlock::iterator &MII, |
| 1084 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 1085 | AvailableSpills &Spills, |
| 1086 | BitVector &RegKills, |
| 1087 | std::vector<MachineOperand*> &KillOps, |
| 1088 | VirtRegMap &VRM) { |
| 1089 | |
| 1090 | MachineBasicBlock::iterator NextMII = next(MII); |
| 1091 | if (NextMII == MBB.end()) |
| 1092 | return false; |
| 1093 | |
| 1094 | if (TII->getOpcodeAfterMemoryUnfold(MII->getOpcode(), true, true) == 0) |
| 1095 | return false; |
| 1096 | |
| 1097 | // Now let's see if the last couple of instructions happens to have freed up |
| 1098 | // a register. |
| 1099 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1100 | unsigned PhysReg = FindFreeRegister(MII, MBB, RC, TRI, AllocatableRegs); |
| 1101 | if (!PhysReg) |
| 1102 | return false; |
| 1103 | |
| 1104 | MachineFunction &MF = *MBB.getParent(); |
| 1105 | TRI = MF.getTarget().getRegisterInfo(); |
| 1106 | MachineInstr &MI = *MII; |
| 1107 | if (!FoldsStackSlotModRef(MI, SS, PhysReg, TII, TRI, VRM)) |
| 1108 | return false; |
| 1109 | |
| 1110 | // If the next instruction also folds the same SS modref and can be unfoled, |
| 1111 | // then it's worthwhile to issue a load from SS into the free register and |
| 1112 | // then unfold these instructions. |
| 1113 | if (!FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, VRM)) |
| 1114 | return false; |
| 1115 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1116 | // Back-schedule reloads and remats. |
| 1117 | MachineBasicBlock::iterator InsertLoc = |
| 1118 | ComputeReloadLoc(MII, MBB.begin(), PhysReg, TRI, false, SS, TII, MF); |
| 1119 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1120 | // Load from SS to the spare physical register. |
| 1121 | TII->loadRegFromStackSlot(MBB, MII, PhysReg, SS, RC); |
| 1122 | // This invalidates Phys. |
| 1123 | Spills.ClobberPhysReg(PhysReg); |
| 1124 | // Remember it's available. |
| 1125 | Spills.addAvailable(SS, PhysReg); |
| 1126 | MaybeDeadStores[SS] = NULL; |
| 1127 | |
| 1128 | // Unfold current MI. |
| 1129 | SmallVector<MachineInstr*, 4> NewMIs; |
| 1130 | if (!TII->unfoldMemoryOperand(MF, &MI, VirtReg, false, false, NewMIs)) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1131 | llvm_unreachable("Unable unfold the load / store folding instruction!"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1132 | assert(NewMIs.size() == 1); |
| 1133 | AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg); |
| 1134 | VRM.transferRestorePts(&MI, NewMIs[0]); |
| 1135 | MII = MBB.insert(MII, NewMIs[0]); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1136 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1137 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 1138 | MBB.erase(&MI); |
| 1139 | ++NumModRefUnfold; |
| 1140 | |
| 1141 | // Unfold next instructions that fold the same SS. |
| 1142 | do { |
| 1143 | MachineInstr &NextMI = *NextMII; |
| 1144 | NextMII = next(NextMII); |
| 1145 | NewMIs.clear(); |
| 1146 | if (!TII->unfoldMemoryOperand(MF, &NextMI, VirtReg, false, false, NewMIs)) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1147 | llvm_unreachable("Unable unfold the load / store folding instruction!"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1148 | assert(NewMIs.size() == 1); |
| 1149 | AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg); |
| 1150 | VRM.transferRestorePts(&NextMI, NewMIs[0]); |
| 1151 | MBB.insert(NextMII, NewMIs[0]); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1152 | InvalidateKills(NextMI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1153 | VRM.RemoveMachineInstrFromMaps(&NextMI); |
| 1154 | MBB.erase(&NextMI); |
| 1155 | ++NumModRefUnfold; |
Evan Cheng | 2c48fe6 | 2009-06-03 09:00:27 +0000 | [diff] [blame] | 1156 | if (NextMII == MBB.end()) |
| 1157 | break; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1158 | } while (FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, VRM)); |
| 1159 | |
| 1160 | // Store the value back into SS. |
| 1161 | TII->storeRegToStackSlot(MBB, NextMII, PhysReg, true, SS, RC); |
| 1162 | MachineInstr *StoreMI = prior(NextMII); |
| 1163 | VRM.addSpillSlotUse(SS, StoreMI); |
| 1164 | VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); |
| 1165 | |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
| 1169 | /// OptimizeByUnfold - Turn a store folding instruction into a load folding |
| 1170 | /// instruction. e.g. |
| 1171 | /// xorl %edi, %eax |
| 1172 | /// movl %eax, -32(%ebp) |
| 1173 | /// movl -36(%ebp), %eax |
| 1174 | /// orl %eax, -32(%ebp) |
| 1175 | /// ==> |
| 1176 | /// xorl %edi, %eax |
| 1177 | /// orl -36(%ebp), %eax |
| 1178 | /// mov %eax, -32(%ebp) |
| 1179 | /// This enables unfolding optimization for a subsequent instruction which will |
| 1180 | /// also eliminate the newly introduced store instruction. |
| 1181 | bool OptimizeByUnfold(MachineBasicBlock &MBB, |
| 1182 | MachineBasicBlock::iterator &MII, |
| 1183 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 1184 | AvailableSpills &Spills, |
| 1185 | BitVector &RegKills, |
| 1186 | std::vector<MachineOperand*> &KillOps, |
| 1187 | VirtRegMap &VRM) { |
| 1188 | MachineFunction &MF = *MBB.getParent(); |
| 1189 | MachineInstr &MI = *MII; |
| 1190 | unsigned UnfoldedOpc = 0; |
| 1191 | unsigned UnfoldPR = 0; |
| 1192 | unsigned UnfoldVR = 0; |
| 1193 | int FoldedSS = VirtRegMap::NO_STACK_SLOT; |
| 1194 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
| 1195 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) { |
| 1196 | // Only transform a MI that folds a single register. |
| 1197 | if (UnfoldedOpc) |
| 1198 | return false; |
| 1199 | UnfoldVR = I->second.first; |
| 1200 | VirtRegMap::ModRef MR = I->second.second; |
| 1201 | // MI2VirtMap be can updated which invalidate the iterator. |
| 1202 | // Increment the iterator first. |
| 1203 | ++I; |
| 1204 | if (VRM.isAssignedReg(UnfoldVR)) |
| 1205 | continue; |
| 1206 | // If this reference is not a use, any previous store is now dead. |
| 1207 | // Otherwise, the store to this stack slot is not dead anymore. |
| 1208 | FoldedSS = VRM.getStackSlot(UnfoldVR); |
| 1209 | MachineInstr* DeadStore = MaybeDeadStores[FoldedSS]; |
| 1210 | if (DeadStore && (MR & VirtRegMap::isModRef)) { |
| 1211 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS); |
| 1212 | if (!PhysReg || !DeadStore->readsRegister(PhysReg)) |
| 1213 | continue; |
| 1214 | UnfoldPR = PhysReg; |
| 1215 | UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), |
| 1216 | false, true); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (!UnfoldedOpc) { |
| 1221 | if (!UnfoldVR) |
| 1222 | return false; |
| 1223 | |
| 1224 | // Look for other unfolding opportunities. |
| 1225 | return OptimizeByUnfold2(UnfoldVR, FoldedSS, MBB, MII, |
| 1226 | MaybeDeadStores, Spills, RegKills, KillOps, VRM); |
| 1227 | } |
| 1228 | |
| 1229 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1230 | MachineOperand &MO = MI.getOperand(i); |
| 1231 | if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse()) |
| 1232 | continue; |
| 1233 | unsigned VirtReg = MO.getReg(); |
| 1234 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg()) |
| 1235 | continue; |
| 1236 | if (VRM.isAssignedReg(VirtReg)) { |
| 1237 | unsigned PhysReg = VRM.getPhys(VirtReg); |
| 1238 | if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR)) |
| 1239 | return false; |
| 1240 | } else if (VRM.isReMaterialized(VirtReg)) |
| 1241 | continue; |
| 1242 | int SS = VRM.getStackSlot(VirtReg); |
| 1243 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 1244 | if (PhysReg) { |
| 1245 | if (TRI->regsOverlap(PhysReg, UnfoldPR)) |
| 1246 | return false; |
| 1247 | continue; |
| 1248 | } |
| 1249 | if (VRM.hasPhys(VirtReg)) { |
| 1250 | PhysReg = VRM.getPhys(VirtReg); |
| 1251 | if (!TRI->regsOverlap(PhysReg, UnfoldPR)) |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | // Ok, we'll need to reload the value into a register which makes |
| 1256 | // it impossible to perform the store unfolding optimization later. |
| 1257 | // Let's see if it is possible to fold the load if the store is |
| 1258 | // unfolded. This allows us to perform the store unfolding |
| 1259 | // optimization. |
| 1260 | SmallVector<MachineInstr*, 4> NewMIs; |
| 1261 | if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) { |
| 1262 | assert(NewMIs.size() == 1); |
| 1263 | MachineInstr *NewMI = NewMIs.back(); |
| 1264 | NewMIs.clear(); |
| 1265 | int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false); |
| 1266 | assert(Idx != -1); |
| 1267 | SmallVector<unsigned, 1> Ops; |
| 1268 | Ops.push_back(Idx); |
| 1269 | MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, NewMI, Ops, SS); |
| 1270 | if (FoldedMI) { |
| 1271 | VRM.addSpillSlotUse(SS, FoldedMI); |
| 1272 | if (!VRM.hasPhys(UnfoldVR)) |
| 1273 | VRM.assignVirt2Phys(UnfoldVR, UnfoldPR); |
| 1274 | VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); |
| 1275 | MII = MBB.insert(MII, FoldedMI); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1276 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1277 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 1278 | MBB.erase(&MI); |
| 1279 | MF.DeleteMachineInstr(NewMI); |
| 1280 | return true; |
| 1281 | } |
| 1282 | MF.DeleteMachineInstr(NewMI); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 1289 | /// CommuteChangesDestination - We are looking for r0 = op r1, r2 and |
| 1290 | /// where SrcReg is r1 and it is tied to r0. Return true if after |
| 1291 | /// commuting this instruction it will be r0 = op r2, r1. |
| 1292 | static bool CommuteChangesDestination(MachineInstr *DefMI, |
| 1293 | const TargetInstrDesc &TID, |
| 1294 | unsigned SrcReg, |
| 1295 | const TargetInstrInfo *TII, |
| 1296 | unsigned &DstIdx) { |
| 1297 | if (TID.getNumDefs() != 1 && TID.getNumOperands() != 3) |
| 1298 | return false; |
| 1299 | if (!DefMI->getOperand(1).isReg() || |
| 1300 | DefMI->getOperand(1).getReg() != SrcReg) |
| 1301 | return false; |
| 1302 | unsigned DefIdx; |
| 1303 | if (!DefMI->isRegTiedToDefOperand(1, &DefIdx) || DefIdx != 0) |
| 1304 | return false; |
| 1305 | unsigned SrcIdx1, SrcIdx2; |
| 1306 | if (!TII->findCommutedOpIndices(DefMI, SrcIdx1, SrcIdx2)) |
| 1307 | return false; |
| 1308 | if (SrcIdx1 == 1 && SrcIdx2 == 2) { |
| 1309 | DstIdx = 2; |
| 1310 | return true; |
| 1311 | } |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1315 | /// CommuteToFoldReload - |
| 1316 | /// Look for |
| 1317 | /// r1 = load fi#1 |
| 1318 | /// r1 = op r1, r2<kill> |
| 1319 | /// store r1, fi#1 |
| 1320 | /// |
| 1321 | /// If op is commutable and r2 is killed, then we can xform these to |
| 1322 | /// r2 = op r2, fi#1 |
| 1323 | /// store r2, fi#1 |
| 1324 | bool CommuteToFoldReload(MachineBasicBlock &MBB, |
| 1325 | MachineBasicBlock::iterator &MII, |
| 1326 | unsigned VirtReg, unsigned SrcReg, int SS, |
| 1327 | AvailableSpills &Spills, |
| 1328 | BitVector &RegKills, |
| 1329 | std::vector<MachineOperand*> &KillOps, |
| 1330 | const TargetRegisterInfo *TRI, |
| 1331 | VirtRegMap &VRM) { |
| 1332 | if (MII == MBB.begin() || !MII->killsRegister(SrcReg)) |
| 1333 | return false; |
| 1334 | |
| 1335 | MachineFunction &MF = *MBB.getParent(); |
| 1336 | MachineInstr &MI = *MII; |
| 1337 | MachineBasicBlock::iterator DefMII = prior(MII); |
| 1338 | MachineInstr *DefMI = DefMII; |
| 1339 | const TargetInstrDesc &TID = DefMI->getDesc(); |
| 1340 | unsigned NewDstIdx; |
| 1341 | if (DefMII != MBB.begin() && |
| 1342 | TID.isCommutable() && |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 1343 | CommuteChangesDestination(DefMI, TID, SrcReg, TII, NewDstIdx)) { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1344 | MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx); |
| 1345 | unsigned NewReg = NewDstMO.getReg(); |
| 1346 | if (!NewDstMO.isKill() || TRI->regsOverlap(NewReg, SrcReg)) |
| 1347 | return false; |
| 1348 | MachineInstr *ReloadMI = prior(DefMII); |
| 1349 | int FrameIdx; |
| 1350 | unsigned DestReg = TII->isLoadFromStackSlot(ReloadMI, FrameIdx); |
| 1351 | if (DestReg != SrcReg || FrameIdx != SS) |
| 1352 | return false; |
| 1353 | int UseIdx = DefMI->findRegisterUseOperandIdx(DestReg, false); |
| 1354 | if (UseIdx == -1) |
| 1355 | return false; |
| 1356 | unsigned DefIdx; |
| 1357 | if (!MI.isRegTiedToDefOperand(UseIdx, &DefIdx)) |
| 1358 | return false; |
| 1359 | assert(DefMI->getOperand(DefIdx).isReg() && |
| 1360 | DefMI->getOperand(DefIdx).getReg() == SrcReg); |
| 1361 | |
| 1362 | // Now commute def instruction. |
| 1363 | MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true); |
| 1364 | if (!CommutedMI) |
| 1365 | return false; |
| 1366 | SmallVector<unsigned, 1> Ops; |
| 1367 | Ops.push_back(NewDstIdx); |
| 1368 | MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS); |
| 1369 | // Not needed since foldMemoryOperand returns new MI. |
| 1370 | MF.DeleteMachineInstr(CommutedMI); |
| 1371 | if (!FoldedMI) |
| 1372 | return false; |
| 1373 | |
| 1374 | VRM.addSpillSlotUse(SS, FoldedMI); |
| 1375 | VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); |
| 1376 | // Insert new def MI and spill MI. |
| 1377 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1378 | TII->storeRegToStackSlot(MBB, &MI, NewReg, true, SS, RC); |
| 1379 | MII = prior(MII); |
| 1380 | MachineInstr *StoreMI = MII; |
| 1381 | VRM.addSpillSlotUse(SS, StoreMI); |
| 1382 | VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); |
| 1383 | MII = MBB.insert(MII, FoldedMI); // Update MII to backtrack. |
| 1384 | |
| 1385 | // Delete all 3 old instructions. |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1386 | InvalidateKills(*ReloadMI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1387 | VRM.RemoveMachineInstrFromMaps(ReloadMI); |
| 1388 | MBB.erase(ReloadMI); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1389 | InvalidateKills(*DefMI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1390 | VRM.RemoveMachineInstrFromMaps(DefMI); |
| 1391 | MBB.erase(DefMI); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1392 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1393 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 1394 | MBB.erase(&MI); |
| 1395 | |
| 1396 | // If NewReg was previously holding value of some SS, it's now clobbered. |
| 1397 | // This has to be done now because it's a physical register. When this |
| 1398 | // instruction is re-visited, it's ignored. |
| 1399 | Spills.ClobberPhysReg(NewReg); |
| 1400 | |
| 1401 | ++NumCommutes; |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
| 1408 | /// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if |
| 1409 | /// the last store to the same slot is now dead. If so, remove the last store. |
| 1410 | void SpillRegToStackSlot(MachineBasicBlock &MBB, |
| 1411 | MachineBasicBlock::iterator &MII, |
| 1412 | int Idx, unsigned PhysReg, int StackSlot, |
| 1413 | const TargetRegisterClass *RC, |
| 1414 | bool isAvailable, MachineInstr *&LastStore, |
| 1415 | AvailableSpills &Spills, |
| 1416 | SmallSet<MachineInstr*, 4> &ReMatDefs, |
| 1417 | BitVector &RegKills, |
| 1418 | std::vector<MachineOperand*> &KillOps, |
| 1419 | VirtRegMap &VRM) { |
| 1420 | |
| 1421 | TII->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC); |
| 1422 | MachineInstr *StoreMI = next(MII); |
| 1423 | VRM.addSpillSlotUse(StackSlot, StoreMI); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1424 | DEBUG(errs() << "Store:\t" << *StoreMI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1425 | |
| 1426 | // If there is a dead store to this stack slot, nuke it now. |
| 1427 | if (LastStore) { |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1428 | DEBUG(errs() << "Removed dead store:\t" << *LastStore); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1429 | ++NumDSE; |
| 1430 | SmallVector<unsigned, 2> KillRegs; |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1431 | InvalidateKills(*LastStore, TRI, RegKills, KillOps, &KillRegs); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1432 | MachineBasicBlock::iterator PrevMII = LastStore; |
| 1433 | bool CheckDef = PrevMII != MBB.begin(); |
| 1434 | if (CheckDef) |
| 1435 | --PrevMII; |
| 1436 | VRM.RemoveMachineInstrFromMaps(LastStore); |
| 1437 | MBB.erase(LastStore); |
| 1438 | if (CheckDef) { |
| 1439 | // Look at defs of killed registers on the store. Mark the defs |
| 1440 | // as dead since the store has been deleted and they aren't |
| 1441 | // being reused. |
| 1442 | for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) { |
| 1443 | bool HasOtherDef = false; |
| 1444 | if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) { |
| 1445 | MachineInstr *DeadDef = PrevMII; |
| 1446 | if (ReMatDefs.count(DeadDef) && !HasOtherDef) { |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 1447 | // FIXME: This assumes a remat def does not have side effects. |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1448 | VRM.RemoveMachineInstrFromMaps(DeadDef); |
| 1449 | MBB.erase(DeadDef); |
| 1450 | ++NumDRM; |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | LastStore = next(MII); |
| 1458 | |
| 1459 | // If the stack slot value was previously available in some other |
| 1460 | // register, change it now. Otherwise, make the register available, |
| 1461 | // in PhysReg. |
| 1462 | Spills.ModifyStackSlotOrReMat(StackSlot); |
| 1463 | Spills.ClobberPhysReg(PhysReg); |
| 1464 | Spills.addAvailable(StackSlot, PhysReg, isAvailable); |
| 1465 | ++NumStores; |
| 1466 | } |
| 1467 | |
| 1468 | /// TransferDeadness - A identity copy definition is dead and it's being |
| 1469 | /// removed. Find the last def or use and mark it as dead / kill. |
| 1470 | void TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist, |
| 1471 | unsigned Reg, BitVector &RegKills, |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1472 | std::vector<MachineOperand*> &KillOps, |
| 1473 | VirtRegMap &VRM) { |
| 1474 | SmallPtrSet<MachineInstr*, 4> Seens; |
| 1475 | SmallVector<std::pair<MachineInstr*, int>,8> Refs; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1476 | for (MachineRegisterInfo::reg_iterator RI = RegInfo->reg_begin(Reg), |
| 1477 | RE = RegInfo->reg_end(); RI != RE; ++RI) { |
| 1478 | MachineInstr *UDMI = &*RI; |
| 1479 | if (UDMI->getParent() != MBB) |
| 1480 | continue; |
| 1481 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI); |
| 1482 | if (DI == DistanceMap.end() || DI->second > CurDist) |
| 1483 | continue; |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1484 | if (Seens.insert(UDMI)) |
| 1485 | Refs.push_back(std::make_pair(UDMI, DI->second)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1488 | if (Refs.empty()) |
| 1489 | return; |
| 1490 | std::sort(Refs.begin(), Refs.end(), RefSorter()); |
| 1491 | |
| 1492 | while (!Refs.empty()) { |
| 1493 | MachineInstr *LastUDMI = Refs.back().first; |
| 1494 | Refs.pop_back(); |
| 1495 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1496 | MachineOperand *LastUD = NULL; |
| 1497 | for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) { |
| 1498 | MachineOperand &MO = LastUDMI->getOperand(i); |
| 1499 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1500 | continue; |
| 1501 | if (!LastUD || (LastUD->isUse() && MO.isDef())) |
| 1502 | LastUD = &MO; |
| 1503 | if (LastUDMI->isRegTiedToDefOperand(i)) |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1504 | break; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1505 | } |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1506 | if (LastUD->isDef()) { |
| 1507 | // If the instruction has no side effect, delete it and propagate |
| 1508 | // backward further. Otherwise, mark is dead and we are done. |
Evan Cheng | fc6ad40 | 2009-07-22 00:25:27 +0000 | [diff] [blame] | 1509 | if (!TII->isDeadInstruction(LastUDMI)) { |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1510 | LastUD->setIsDead(); |
| 1511 | break; |
| 1512 | } |
| 1513 | VRM.RemoveMachineInstrFromMaps(LastUDMI); |
| 1514 | MBB->erase(LastUDMI); |
| 1515 | } else { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1516 | LastUD->setIsKill(); |
| 1517 | RegKills.set(Reg); |
| 1518 | KillOps[Reg] = LastUD; |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 1519 | break; |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /// rewriteMBB - Keep track of which spills are available even after the |
| 1525 | /// register allocator is done with them. If possible, avid reloading vregs. |
| 1526 | void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM, |
| 1527 | LiveIntervals *LIs, |
| 1528 | AvailableSpills &Spills, BitVector &RegKills, |
| 1529 | std::vector<MachineOperand*> &KillOps) { |
| 1530 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 1531 | DEBUG(errs() << "\n**** Local spiller rewriting MBB '" |
| 1532 | << MBB.getBasicBlock()->getName() << "':\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1533 | |
| 1534 | MachineFunction &MF = *MBB.getParent(); |
| 1535 | |
| 1536 | // MaybeDeadStores - When we need to write a value back into a stack slot, |
| 1537 | // keep track of the inserted store. If the stack slot value is never read |
| 1538 | // (because the value was used from some available register, for example), and |
| 1539 | // subsequently stored to, the original store is dead. This map keeps track |
| 1540 | // of inserted stores that are not used. If we see a subsequent store to the |
| 1541 | // same stack slot, the original store is deleted. |
| 1542 | std::vector<MachineInstr*> MaybeDeadStores; |
| 1543 | MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL); |
| 1544 | |
| 1545 | // ReMatDefs - These are rematerializable def MIs which are not deleted. |
| 1546 | SmallSet<MachineInstr*, 4> ReMatDefs; |
| 1547 | |
| 1548 | // Clear kill info. |
| 1549 | SmallSet<unsigned, 2> KilledMIRegs; |
| 1550 | RegKills.reset(); |
| 1551 | KillOps.clear(); |
| 1552 | KillOps.resize(TRI->getNumRegs(), NULL); |
| 1553 | |
| 1554 | unsigned Dist = 0; |
| 1555 | DistanceMap.clear(); |
| 1556 | for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end(); |
| 1557 | MII != E; ) { |
| 1558 | MachineBasicBlock::iterator NextMII = next(MII); |
| 1559 | |
| 1560 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
| 1561 | bool Erased = false; |
| 1562 | bool BackTracked = false; |
| 1563 | if (OptimizeByUnfold(MBB, MII, |
| 1564 | MaybeDeadStores, Spills, RegKills, KillOps, VRM)) |
| 1565 | NextMII = next(MII); |
| 1566 | |
| 1567 | MachineInstr &MI = *MII; |
| 1568 | |
| 1569 | if (VRM.hasEmergencySpills(&MI)) { |
| 1570 | // Spill physical register(s) in the rare case the allocator has run out |
| 1571 | // of registers to allocate. |
| 1572 | SmallSet<int, 4> UsedSS; |
| 1573 | std::vector<unsigned> &EmSpills = VRM.getEmergencySpills(&MI); |
| 1574 | for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) { |
| 1575 | unsigned PhysReg = EmSpills[i]; |
| 1576 | const TargetRegisterClass *RC = |
| 1577 | TRI->getPhysicalRegisterRegClass(PhysReg); |
| 1578 | assert(RC && "Unable to determine register class!"); |
| 1579 | int SS = VRM.getEmergencySpillSlot(RC); |
| 1580 | if (UsedSS.count(SS)) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1581 | llvm_unreachable("Need to spill more than one physical registers!"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1582 | UsedSS.insert(SS); |
| 1583 | TII->storeRegToStackSlot(MBB, MII, PhysReg, true, SS, RC); |
| 1584 | MachineInstr *StoreMI = prior(MII); |
| 1585 | VRM.addSpillSlotUse(SS, StoreMI); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Back-schedule reloads and remats. |
| 1588 | MachineBasicBlock::iterator InsertLoc = |
| 1589 | ComputeReloadLoc(next(MII), MBB.begin(), PhysReg, TRI, false, |
| 1590 | SS, TII, MF); |
| 1591 | |
| 1592 | TII->loadRegFromStackSlot(MBB, InsertLoc, PhysReg, SS, RC); |
| 1593 | |
| 1594 | MachineInstr *LoadMI = prior(InsertLoc); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1595 | VRM.addSpillSlotUse(SS, LoadMI); |
| 1596 | ++NumPSpills; |
Jakob Stoklund Olesen | 7a1e872 | 2009-08-15 11:03:03 +0000 | [diff] [blame] | 1597 | DistanceMap.insert(std::make_pair(LoadMI, Dist++)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1598 | } |
| 1599 | NextMII = next(MII); |
| 1600 | } |
| 1601 | |
| 1602 | // Insert restores here if asked to. |
| 1603 | if (VRM.isRestorePt(&MI)) { |
| 1604 | std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI); |
| 1605 | for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) { |
| 1606 | unsigned VirtReg = RestoreRegs[e-i-1]; // Reverse order. |
| 1607 | if (!VRM.getPreSplitReg(VirtReg)) |
| 1608 | continue; // Split interval spilled again. |
| 1609 | unsigned Phys = VRM.getPhys(VirtReg); |
| 1610 | RegInfo->setPhysRegUsed(Phys); |
| 1611 | |
| 1612 | // Check if the value being restored if available. If so, it must be |
| 1613 | // from a predecessor BB that fallthrough into this BB. We do not |
| 1614 | // expect: |
| 1615 | // BB1: |
| 1616 | // r1 = load fi#1 |
| 1617 | // ... |
| 1618 | // = r1<kill> |
| 1619 | // ... # r1 not clobbered |
| 1620 | // ... |
| 1621 | // = load fi#1 |
| 1622 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 1623 | int SSorRMId = DoReMat |
| 1624 | ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg); |
| 1625 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1626 | unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId); |
| 1627 | if (InReg == Phys) { |
| 1628 | // If the value is already available in the expected register, save |
| 1629 | // a reload / remat. |
| 1630 | if (SSorRMId) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1631 | DEBUG(errs() << "Reusing RM#" |
| 1632 | << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1633 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1634 | DEBUG(errs() << "Reusing SS#" << SSorRMId); |
| 1635 | DEBUG(errs() << " from physreg " |
| 1636 | << TRI->getName(InReg) << " for vreg" |
| 1637 | << VirtReg <<" instead of reloading into physreg " |
| 1638 | << TRI->getName(Phys) << '\n'); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1639 | ++NumOmitted; |
| 1640 | continue; |
| 1641 | } else if (InReg && InReg != Phys) { |
| 1642 | if (SSorRMId) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1643 | DEBUG(errs() << "Reusing RM#" |
| 1644 | << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1645 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1646 | DEBUG(errs() << "Reusing SS#" << SSorRMId); |
| 1647 | DEBUG(errs() << " from physreg " |
| 1648 | << TRI->getName(InReg) << " for vreg" |
| 1649 | << VirtReg <<" by copying it into physreg " |
| 1650 | << TRI->getName(Phys) << '\n'); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1651 | |
| 1652 | // If the reloaded / remat value is available in another register, |
| 1653 | // copy it to the desired register. |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1654 | |
| 1655 | // Back-schedule reloads and remats. |
| 1656 | MachineBasicBlock::iterator InsertLoc = |
| 1657 | ComputeReloadLoc(MII, MBB.begin(), Phys, TRI, DoReMat, |
| 1658 | SSorRMId, TII, MF); |
| 1659 | |
| 1660 | TII->copyRegToReg(MBB, InsertLoc, Phys, InReg, RC, RC); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1661 | |
| 1662 | // This invalidates Phys. |
| 1663 | Spills.ClobberPhysReg(Phys); |
| 1664 | // Remember it's available. |
| 1665 | Spills.addAvailable(SSorRMId, Phys); |
| 1666 | |
| 1667 | // Mark is killed. |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1668 | MachineInstr *CopyMI = prior(InsertLoc); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1669 | MachineOperand *KillOpnd = CopyMI->findRegisterUseOperand(InReg); |
| 1670 | KillOpnd->setIsKill(); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1671 | UpdateKills(*CopyMI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1673 | DEBUG(errs() << '\t' << *CopyMI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1674 | ++NumCopified; |
| 1675 | continue; |
| 1676 | } |
| 1677 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1678 | // Back-schedule reloads and remats. |
| 1679 | MachineBasicBlock::iterator InsertLoc = |
| 1680 | ComputeReloadLoc(MII, MBB.begin(), Phys, TRI, DoReMat, |
| 1681 | SSorRMId, TII, MF); |
| 1682 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1683 | if (VRM.isReMaterialized(VirtReg)) { |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1684 | ReMaterialize(MBB, InsertLoc, Phys, VirtReg, TII, TRI, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1685 | } else { |
| 1686 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1687 | TII->loadRegFromStackSlot(MBB, InsertLoc, Phys, SSorRMId, RC); |
| 1688 | MachineInstr *LoadMI = prior(InsertLoc); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1689 | VRM.addSpillSlotUse(SSorRMId, LoadMI); |
| 1690 | ++NumLoads; |
Jakob Stoklund Olesen | 7a1e872 | 2009-08-15 11:03:03 +0000 | [diff] [blame] | 1691 | DistanceMap.insert(std::make_pair(LoadMI, Dist++)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | // This invalidates Phys. |
| 1695 | Spills.ClobberPhysReg(Phys); |
| 1696 | // Remember it's available. |
| 1697 | Spills.addAvailable(SSorRMId, Phys); |
| 1698 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1699 | UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1700 | DEBUG(errs() << '\t' << *prior(MII)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | // Insert spills here if asked to. |
| 1705 | if (VRM.isSpillPt(&MI)) { |
| 1706 | std::vector<std::pair<unsigned,bool> > &SpillRegs = |
| 1707 | VRM.getSpillPtSpills(&MI); |
| 1708 | for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) { |
| 1709 | unsigned VirtReg = SpillRegs[i].first; |
| 1710 | bool isKill = SpillRegs[i].second; |
| 1711 | if (!VRM.getPreSplitReg(VirtReg)) |
| 1712 | continue; // Split interval spilled again. |
| 1713 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
| 1714 | unsigned Phys = VRM.getPhys(VirtReg); |
| 1715 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 1716 | TII->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC); |
| 1717 | MachineInstr *StoreMI = next(MII); |
| 1718 | VRM.addSpillSlotUse(StackSlot, StoreMI); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1719 | DEBUG(errs() << "Store:\t" << *StoreMI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1720 | VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); |
| 1721 | } |
| 1722 | NextMII = next(MII); |
| 1723 | } |
| 1724 | |
| 1725 | /// ReusedOperands - Keep track of operand reuse in case we need to undo |
| 1726 | /// reuse. |
| 1727 | ReuseInfo ReusedOperands(MI, TRI); |
| 1728 | SmallVector<unsigned, 4> VirtUseOps; |
| 1729 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1730 | MachineOperand &MO = MI.getOperand(i); |
| 1731 | if (!MO.isReg() || MO.getReg() == 0) |
| 1732 | continue; // Ignore non-register operands. |
| 1733 | |
| 1734 | unsigned VirtReg = MO.getReg(); |
| 1735 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) { |
| 1736 | // Ignore physregs for spilling, but remember that it is used by this |
| 1737 | // function. |
| 1738 | RegInfo->setPhysRegUsed(VirtReg); |
| 1739 | continue; |
| 1740 | } |
| 1741 | |
| 1742 | // We want to process implicit virtual register uses first. |
| 1743 | if (MO.isImplicit()) |
| 1744 | // If the virtual register is implicitly defined, emit a implicit_def |
| 1745 | // before so scavenger knows it's "defined". |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 1746 | // FIXME: This is a horrible hack done the by register allocator to |
| 1747 | // remat a definition with virtual register operand. |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1748 | VirtUseOps.insert(VirtUseOps.begin(), i); |
| 1749 | else |
| 1750 | VirtUseOps.push_back(i); |
| 1751 | } |
| 1752 | |
| 1753 | // Process all of the spilled uses and all non spilled reg references. |
| 1754 | SmallVector<int, 2> PotentialDeadStoreSlots; |
| 1755 | KilledMIRegs.clear(); |
| 1756 | for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) { |
| 1757 | unsigned i = VirtUseOps[j]; |
| 1758 | MachineOperand &MO = MI.getOperand(i); |
| 1759 | unsigned VirtReg = MO.getReg(); |
| 1760 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 1761 | "Not a virtual register?"); |
| 1762 | |
| 1763 | unsigned SubIdx = MO.getSubReg(); |
| 1764 | if (VRM.isAssignedReg(VirtReg)) { |
| 1765 | // This virtual register was assigned a physreg! |
| 1766 | unsigned Phys = VRM.getPhys(VirtReg); |
| 1767 | RegInfo->setPhysRegUsed(Phys); |
| 1768 | if (MO.isDef()) |
| 1769 | ReusedOperands.markClobbered(Phys); |
| 1770 | unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys; |
| 1771 | MI.getOperand(i).setReg(RReg); |
| 1772 | MI.getOperand(i).setSubReg(0); |
| 1773 | if (VRM.isImplicitlyDefined(VirtReg)) |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 1774 | // FIXME: Is this needed? |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1775 | BuildMI(MBB, &MI, MI.getDebugLoc(), |
| 1776 | TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg); |
| 1777 | continue; |
| 1778 | } |
| 1779 | |
| 1780 | // This virtual register is now known to be a spilled value. |
| 1781 | if (!MO.isUse()) |
| 1782 | continue; // Handle defs in the loop below (handle use&def here though) |
| 1783 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 1784 | bool AvoidReload = MO.isUndef(); |
| 1785 | // Check if it is defined by an implicit def. It should not be spilled. |
| 1786 | // Note, this is for correctness reason. e.g. |
| 1787 | // 8 %reg1024<def> = IMPLICIT_DEF |
| 1788 | // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2 |
| 1789 | // The live range [12, 14) are not part of the r1024 live interval since |
| 1790 | // it's defined by an implicit def. It will not conflicts with live |
| 1791 | // interval of r1025. Now suppose both registers are spilled, you can |
| 1792 | // easily see a situation where both registers are reloaded before |
| 1793 | // the INSERT_SUBREG and both target registers that would overlap. |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1794 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 1795 | int SSorRMId = DoReMat |
| 1796 | ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg); |
| 1797 | int ReuseSlot = SSorRMId; |
| 1798 | |
| 1799 | // Check to see if this stack slot is available. |
| 1800 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId); |
| 1801 | |
| 1802 | // If this is a sub-register use, make sure the reuse register is in the |
| 1803 | // right register class. For example, for x86 not all of the 32-bit |
| 1804 | // registers have accessible sub-registers. |
| 1805 | // Similarly so for EXTRACT_SUBREG. Consider this: |
| 1806 | // EDI = op |
| 1807 | // MOV32_mr fi#1, EDI |
| 1808 | // ... |
| 1809 | // = EXTRACT_SUBREG fi#1 |
| 1810 | // fi#1 is available in EDI, but it cannot be reused because it's not in |
| 1811 | // the right register file. |
| 1812 | if (PhysReg && !AvoidReload && |
| 1813 | (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) { |
| 1814 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1815 | if (!RC->contains(PhysReg)) |
| 1816 | PhysReg = 0; |
| 1817 | } |
| 1818 | |
| 1819 | if (PhysReg && !AvoidReload) { |
| 1820 | // This spilled operand might be part of a two-address operand. If this |
| 1821 | // is the case, then changing it will necessarily require changing the |
| 1822 | // def part of the instruction as well. However, in some cases, we |
| 1823 | // aren't allowed to modify the reused register. If none of these cases |
| 1824 | // apply, reuse it. |
| 1825 | bool CanReuse = true; |
| 1826 | bool isTied = MI.isRegTiedToDefOperand(i); |
| 1827 | if (isTied) { |
| 1828 | // Okay, we have a two address operand. We can reuse this physreg as |
| 1829 | // long as we are allowed to clobber the value and there isn't an |
| 1830 | // earlier def that has already clobbered the physreg. |
| 1831 | CanReuse = !ReusedOperands.isClobbered(PhysReg) && |
| 1832 | Spills.canClobberPhysReg(PhysReg); |
| 1833 | } |
| 1834 | |
| 1835 | if (CanReuse) { |
| 1836 | // If this stack slot value is already available, reuse it! |
| 1837 | if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1838 | DEBUG(errs() << "Reusing RM#" |
| 1839 | << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1840 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1841 | DEBUG(errs() << "Reusing SS#" << ReuseSlot); |
| 1842 | DEBUG(errs() << " from physreg " |
| 1843 | << TRI->getName(PhysReg) << " for vreg" |
| 1844 | << VirtReg <<" instead of reloading into physreg " |
| 1845 | << TRI->getName(VRM.getPhys(VirtReg)) << '\n'); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1846 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
| 1847 | MI.getOperand(i).setReg(RReg); |
| 1848 | MI.getOperand(i).setSubReg(0); |
| 1849 | |
| 1850 | // The only technical detail we have is that we don't know that |
| 1851 | // PhysReg won't be clobbered by a reloaded stack slot that occurs |
| 1852 | // later in the instruction. In particular, consider 'op V1, V2'. |
| 1853 | // If V1 is available in physreg R0, we would choose to reuse it |
| 1854 | // here, instead of reloading it into the register the allocator |
| 1855 | // indicated (say R1). However, V2 might have to be reloaded |
| 1856 | // later, and it might indicate that it needs to live in R0. When |
| 1857 | // this occurs, we need to have information available that |
| 1858 | // indicates it is safe to use R1 for the reload instead of R0. |
| 1859 | // |
| 1860 | // To further complicate matters, we might conflict with an alias, |
| 1861 | // or R0 and R1 might not be compatible with each other. In this |
| 1862 | // case, we actually insert a reload for V1 in R1, ensuring that |
| 1863 | // we can get at R0 or its alias. |
| 1864 | ReusedOperands.addReuse(i, ReuseSlot, PhysReg, |
| 1865 | VRM.getPhys(VirtReg), VirtReg); |
| 1866 | if (isTied) |
| 1867 | // Only mark it clobbered if this is a use&def operand. |
| 1868 | ReusedOperands.markClobbered(PhysReg); |
| 1869 | ++NumReused; |
| 1870 | |
| 1871 | if (MI.getOperand(i).isKill() && |
| 1872 | ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) { |
| 1873 | |
| 1874 | // The store of this spilled value is potentially dead, but we |
| 1875 | // won't know for certain until we've confirmed that the re-use |
| 1876 | // above is valid, which means waiting until the other operands |
| 1877 | // are processed. For now we just track the spill slot, we'll |
| 1878 | // remove it after the other operands are processed if valid. |
| 1879 | |
| 1880 | PotentialDeadStoreSlots.push_back(ReuseSlot); |
| 1881 | } |
| 1882 | |
| 1883 | // Mark is isKill if it's there no other uses of the same virtual |
| 1884 | // register and it's not a two-address operand. IsKill will be |
| 1885 | // unset if reg is reused. |
| 1886 | if (!isTied && KilledMIRegs.count(VirtReg) == 0) { |
| 1887 | MI.getOperand(i).setIsKill(); |
| 1888 | KilledMIRegs.insert(VirtReg); |
| 1889 | } |
| 1890 | |
| 1891 | continue; |
| 1892 | } // CanReuse |
| 1893 | |
| 1894 | // Otherwise we have a situation where we have a two-address instruction |
| 1895 | // whose mod/ref operand needs to be reloaded. This reload is already |
| 1896 | // available in some register "PhysReg", but if we used PhysReg as the |
| 1897 | // operand to our 2-addr instruction, the instruction would modify |
| 1898 | // PhysReg. This isn't cool if something later uses PhysReg and expects |
| 1899 | // to get its initial value. |
| 1900 | // |
| 1901 | // To avoid this problem, and to avoid doing a load right after a store, |
| 1902 | // we emit a copy from PhysReg into the designated register for this |
| 1903 | // operand. |
| 1904 | unsigned DesignatedReg = VRM.getPhys(VirtReg); |
| 1905 | assert(DesignatedReg && "Must map virtreg to physreg!"); |
| 1906 | |
| 1907 | // Note that, if we reused a register for a previous operand, the |
| 1908 | // register we want to reload into might not actually be |
| 1909 | // available. If this occurs, use the register indicated by the |
| 1910 | // reuser. |
| 1911 | if (ReusedOperands.hasReuses()) |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 1912 | DesignatedReg = ReusedOperands.GetRegForReload(VirtReg, |
| 1913 | DesignatedReg, &MI, |
| 1914 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1915 | |
| 1916 | // If the mapped designated register is actually the physreg we have |
| 1917 | // incoming, we don't need to inserted a dead copy. |
| 1918 | if (DesignatedReg == PhysReg) { |
| 1919 | // If this stack slot value is already available, reuse it! |
| 1920 | if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1921 | DEBUG(errs() << "Reusing RM#" |
| 1922 | << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1923 | else |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1924 | DEBUG(errs() << "Reusing SS#" << ReuseSlot); |
| 1925 | DEBUG(errs() << " from physreg " << TRI->getName(PhysReg) |
| 1926 | << " for vreg" << VirtReg |
| 1927 | << " instead of reloading into same physreg.\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1928 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
| 1929 | MI.getOperand(i).setReg(RReg); |
| 1930 | MI.getOperand(i).setSubReg(0); |
| 1931 | ReusedOperands.markClobbered(RReg); |
| 1932 | ++NumReused; |
| 1933 | continue; |
| 1934 | } |
| 1935 | |
| 1936 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1937 | RegInfo->setPhysRegUsed(DesignatedReg); |
| 1938 | ReusedOperands.markClobbered(DesignatedReg); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1939 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1940 | // Back-schedule reloads and remats. |
| 1941 | MachineBasicBlock::iterator InsertLoc = |
| 1942 | ComputeReloadLoc(&MI, MBB.begin(), PhysReg, TRI, DoReMat, |
| 1943 | SSorRMId, TII, MF); |
| 1944 | |
| 1945 | TII->copyRegToReg(MBB, InsertLoc, DesignatedReg, PhysReg, RC, RC); |
| 1946 | |
| 1947 | MachineInstr *CopyMI = prior(InsertLoc); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 1948 | UpdateKills(*CopyMI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1949 | |
| 1950 | // This invalidates DesignatedReg. |
| 1951 | Spills.ClobberPhysReg(DesignatedReg); |
| 1952 | |
| 1953 | Spills.addAvailable(ReuseSlot, DesignatedReg); |
| 1954 | unsigned RReg = |
| 1955 | SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg; |
| 1956 | MI.getOperand(i).setReg(RReg); |
| 1957 | MI.getOperand(i).setSubReg(0); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 1958 | DEBUG(errs() << '\t' << *prior(MII)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1959 | ++NumReused; |
| 1960 | continue; |
| 1961 | } // if (PhysReg) |
| 1962 | |
| 1963 | // Otherwise, reload it and remember that we have it. |
| 1964 | PhysReg = VRM.getPhys(VirtReg); |
| 1965 | assert(PhysReg && "Must map virtreg to physreg!"); |
| 1966 | |
| 1967 | // Note that, if we reused a register for a previous operand, the |
| 1968 | // register we want to reload into might not actually be |
| 1969 | // available. If this occurs, use the register indicated by the |
| 1970 | // reuser. |
| 1971 | if (ReusedOperands.hasReuses()) |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 1972 | PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, |
| 1973 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1974 | |
| 1975 | RegInfo->setPhysRegUsed(PhysReg); |
| 1976 | ReusedOperands.markClobbered(PhysReg); |
| 1977 | if (AvoidReload) |
| 1978 | ++NumAvoided; |
| 1979 | else { |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1980 | // Back-schedule reloads and remats. |
| 1981 | MachineBasicBlock::iterator InsertLoc = |
| 1982 | ComputeReloadLoc(MII, MBB.begin(), PhysReg, TRI, DoReMat, |
| 1983 | SSorRMId, TII, MF); |
| 1984 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1985 | if (DoReMat) { |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1986 | ReMaterialize(MBB, InsertLoc, PhysReg, VirtReg, TII, TRI, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1987 | } else { |
| 1988 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 1989 | TII->loadRegFromStackSlot(MBB, InsertLoc, PhysReg, SSorRMId, RC); |
| 1990 | MachineInstr *LoadMI = prior(InsertLoc); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1991 | VRM.addSpillSlotUse(SSorRMId, LoadMI); |
| 1992 | ++NumLoads; |
Jakob Stoklund Olesen | 7a1e872 | 2009-08-15 11:03:03 +0000 | [diff] [blame] | 1993 | DistanceMap.insert(std::make_pair(LoadMI, Dist++)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1994 | } |
| 1995 | // This invalidates PhysReg. |
| 1996 | Spills.ClobberPhysReg(PhysReg); |
| 1997 | |
| 1998 | // Any stores to this stack slot are not dead anymore. |
| 1999 | if (!DoReMat) |
| 2000 | MaybeDeadStores[SSorRMId] = NULL; |
| 2001 | Spills.addAvailable(SSorRMId, PhysReg); |
| 2002 | // Assumes this is the last use. IsKill will be unset if reg is reused |
| 2003 | // unless it's a two-address operand. |
| 2004 | if (!MI.isRegTiedToDefOperand(i) && |
| 2005 | KilledMIRegs.count(VirtReg) == 0) { |
| 2006 | MI.getOperand(i).setIsKill(); |
| 2007 | KilledMIRegs.insert(VirtReg); |
| 2008 | } |
| 2009 | |
David Greene | 2d4e6d3 | 2009-07-28 16:49:24 +0000 | [diff] [blame] | 2010 | UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2011 | DEBUG(errs() << '\t' << *prior(InsertLoc)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2012 | } |
| 2013 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
| 2014 | MI.getOperand(i).setReg(RReg); |
| 2015 | MI.getOperand(i).setSubReg(0); |
| 2016 | } |
| 2017 | |
| 2018 | // Ok - now we can remove stores that have been confirmed dead. |
| 2019 | for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) { |
| 2020 | // This was the last use and the spilled value is still available |
| 2021 | // for reuse. That means the spill was unnecessary! |
| 2022 | int PDSSlot = PotentialDeadStoreSlots[j]; |
| 2023 | MachineInstr* DeadStore = MaybeDeadStores[PDSSlot]; |
| 2024 | if (DeadStore) { |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2025 | DEBUG(errs() << "Removed dead store:\t" << *DeadStore); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2026 | InvalidateKills(*DeadStore, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2027 | VRM.RemoveMachineInstrFromMaps(DeadStore); |
| 2028 | MBB.erase(DeadStore); |
| 2029 | MaybeDeadStores[PDSSlot] = NULL; |
| 2030 | ++NumDSE; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2035 | DEBUG(errs() << '\t' << MI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2036 | |
| 2037 | |
| 2038 | // If we have folded references to memory operands, make sure we clear all |
| 2039 | // physical registers that may contain the value of the spilled virtual |
| 2040 | // register |
| 2041 | SmallSet<int, 2> FoldedSS; |
| 2042 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) { |
| 2043 | unsigned VirtReg = I->second.first; |
| 2044 | VirtRegMap::ModRef MR = I->second.second; |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2045 | DEBUG(errs() << "Folded vreg: " << VirtReg << " MR: " << MR); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2046 | |
| 2047 | // MI2VirtMap be can updated which invalidate the iterator. |
| 2048 | // Increment the iterator first. |
| 2049 | ++I; |
| 2050 | int SS = VRM.getStackSlot(VirtReg); |
| 2051 | if (SS == VirtRegMap::NO_STACK_SLOT) |
| 2052 | continue; |
| 2053 | FoldedSS.insert(SS); |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2054 | DEBUG(errs() << " - StackSlot: " << SS << "\n"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2055 | |
| 2056 | // If this folded instruction is just a use, check to see if it's a |
| 2057 | // straight load from the virt reg slot. |
| 2058 | if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) { |
| 2059 | int FrameIdx; |
| 2060 | unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx); |
| 2061 | if (DestReg && FrameIdx == SS) { |
| 2062 | // If this spill slot is available, turn it into a copy (or nothing) |
| 2063 | // instead of leaving it as a load! |
| 2064 | if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) { |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2065 | DEBUG(errs() << "Promoted Load To Copy: " << MI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2066 | if (DestReg != InReg) { |
| 2067 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
| 2068 | TII->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC); |
| 2069 | MachineOperand *DefMO = MI.findRegisterDefOperand(DestReg); |
| 2070 | unsigned SubIdx = DefMO->getSubReg(); |
| 2071 | // Revisit the copy so we make sure to notice the effects of the |
| 2072 | // operation on the destreg (either needing to RA it if it's |
| 2073 | // virtual or needing to clobber any values if it's physical). |
| 2074 | NextMII = &MI; |
| 2075 | --NextMII; // backtrack to the copy. |
| 2076 | // Propagate the sub-register index over. |
| 2077 | if (SubIdx) { |
| 2078 | DefMO = NextMII->findRegisterDefOperand(DestReg); |
| 2079 | DefMO->setSubReg(SubIdx); |
| 2080 | } |
| 2081 | |
| 2082 | // Mark is killed. |
| 2083 | MachineOperand *KillOpnd = NextMII->findRegisterUseOperand(InReg); |
| 2084 | KillOpnd->setIsKill(); |
| 2085 | |
| 2086 | BackTracked = true; |
| 2087 | } else { |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2088 | DEBUG(errs() << "Removing now-noop copy: " << MI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2089 | // Unset last kill since it's being reused. |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2090 | InvalidateKill(InReg, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2091 | Spills.disallowClobberPhysReg(InReg); |
| 2092 | } |
| 2093 | |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2094 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2095 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2096 | MBB.erase(&MI); |
| 2097 | Erased = true; |
| 2098 | goto ProcessNextInst; |
| 2099 | } |
| 2100 | } else { |
| 2101 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 2102 | SmallVector<MachineInstr*, 4> NewMIs; |
| 2103 | if (PhysReg && |
| 2104 | TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) { |
| 2105 | MBB.insert(MII, NewMIs[0]); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2106 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2107 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2108 | MBB.erase(&MI); |
| 2109 | Erased = true; |
| 2110 | --NextMII; // backtrack to the unfolded instruction. |
| 2111 | BackTracked = true; |
| 2112 | goto ProcessNextInst; |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | // If this reference is not a use, any previous store is now dead. |
| 2118 | // Otherwise, the store to this stack slot is not dead anymore. |
| 2119 | MachineInstr* DeadStore = MaybeDeadStores[SS]; |
| 2120 | if (DeadStore) { |
| 2121 | bool isDead = !(MR & VirtRegMap::isRef); |
| 2122 | MachineInstr *NewStore = NULL; |
| 2123 | if (MR & VirtRegMap::isModRef) { |
| 2124 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 2125 | SmallVector<MachineInstr*, 4> NewMIs; |
| 2126 | // We can reuse this physreg as long as we are allowed to clobber |
| 2127 | // the value and there isn't an earlier def that has already clobbered |
| 2128 | // the physreg. |
| 2129 | if (PhysReg && |
| 2130 | !ReusedOperands.isClobbered(PhysReg) && |
| 2131 | Spills.canClobberPhysReg(PhysReg) && |
| 2132 | !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable! |
| 2133 | MachineOperand *KillOpnd = |
| 2134 | DeadStore->findRegisterUseOperand(PhysReg, true); |
| 2135 | // Note, if the store is storing a sub-register, it's possible the |
| 2136 | // super-register is needed below. |
| 2137 | if (KillOpnd && !KillOpnd->getSubReg() && |
| 2138 | TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){ |
| 2139 | MBB.insert(MII, NewMIs[0]); |
| 2140 | NewStore = NewMIs[1]; |
| 2141 | MBB.insert(MII, NewStore); |
| 2142 | VRM.addSpillSlotUse(SS, NewStore); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2143 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2144 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2145 | MBB.erase(&MI); |
| 2146 | Erased = true; |
| 2147 | --NextMII; |
| 2148 | --NextMII; // backtrack to the unfolded instruction. |
| 2149 | BackTracked = true; |
| 2150 | isDead = true; |
| 2151 | ++NumSUnfold; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | if (isDead) { // Previous store is dead. |
| 2157 | // If we get here, the store is dead, nuke it now. |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2158 | DEBUG(errs() << "Removed dead store:\t" << *DeadStore); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2159 | InvalidateKills(*DeadStore, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2160 | VRM.RemoveMachineInstrFromMaps(DeadStore); |
| 2161 | MBB.erase(DeadStore); |
| 2162 | if (!NewStore) |
| 2163 | ++NumDSE; |
| 2164 | } |
| 2165 | |
| 2166 | MaybeDeadStores[SS] = NULL; |
| 2167 | if (NewStore) { |
| 2168 | // Treat this store as a spill merged into a copy. That makes the |
| 2169 | // stack slot value available. |
| 2170 | VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod); |
| 2171 | goto ProcessNextInst; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | // If the spill slot value is available, and this is a new definition of |
| 2176 | // the value, the value is not available anymore. |
| 2177 | if (MR & VirtRegMap::isMod) { |
| 2178 | // Notice that the value in this stack slot has been modified. |
| 2179 | Spills.ModifyStackSlotOrReMat(SS); |
| 2180 | |
| 2181 | // If this is *just* a mod of the value, check to see if this is just a |
| 2182 | // store to the spill slot (i.e. the spill got merged into the copy). If |
| 2183 | // so, realize that the vreg is available now, and add the store to the |
| 2184 | // MaybeDeadStore info. |
| 2185 | int StackSlot; |
| 2186 | if (!(MR & VirtRegMap::isRef)) { |
| 2187 | if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) { |
| 2188 | assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
| 2189 | "Src hasn't been allocated yet?"); |
| 2190 | |
| 2191 | if (CommuteToFoldReload(MBB, MII, VirtReg, SrcReg, StackSlot, |
| 2192 | Spills, RegKills, KillOps, TRI, VRM)) { |
| 2193 | NextMII = next(MII); |
| 2194 | BackTracked = true; |
| 2195 | goto ProcessNextInst; |
| 2196 | } |
| 2197 | |
| 2198 | // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark |
| 2199 | // this as a potentially dead store in case there is a subsequent |
| 2200 | // store into the stack slot without a read from it. |
| 2201 | MaybeDeadStores[StackSlot] = &MI; |
| 2202 | |
| 2203 | // If the stack slot value was previously available in some other |
| 2204 | // register, change it now. Otherwise, make the register |
| 2205 | // available in PhysReg. |
| 2206 | Spills.addAvailable(StackSlot, SrcReg, MI.killsRegister(SrcReg)); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | // Process all of the spilled defs. |
| 2213 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 2214 | MachineOperand &MO = MI.getOperand(i); |
| 2215 | if (!(MO.isReg() && MO.getReg() && MO.isDef())) |
| 2216 | continue; |
| 2217 | |
| 2218 | unsigned VirtReg = MO.getReg(); |
| 2219 | if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) { |
| 2220 | // Check to see if this is a noop copy. If so, eliminate the |
| 2221 | // instruction before considering the dest reg to be changed. |
Evan Cheng | 2578ba2 | 2009-07-01 01:59:31 +0000 | [diff] [blame] | 2222 | // Also check if it's copying from an "undef", if so, we can't |
| 2223 | // eliminate this or else the undef marker is lost and it will |
| 2224 | // confuses the scavenger. This is extremely rare. |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2225 | unsigned Src, Dst, SrcSR, DstSR; |
Evan Cheng | 2578ba2 | 2009-07-01 01:59:31 +0000 | [diff] [blame] | 2226 | if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst && |
| 2227 | !MI.findRegisterUseOperand(Src)->isUndef()) { |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2228 | ++NumDCE; |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2229 | DEBUG(errs() << "Removing now-noop copy: " << MI); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2230 | SmallVector<unsigned, 2> KillRegs; |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2231 | InvalidateKills(MI, TRI, RegKills, KillOps, &KillRegs); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2232 | if (MO.isDead() && !KillRegs.empty()) { |
| 2233 | // Source register or an implicit super/sub-register use is killed. |
| 2234 | assert(KillRegs[0] == Dst || |
| 2235 | TRI->isSubRegister(KillRegs[0], Dst) || |
| 2236 | TRI->isSuperRegister(KillRegs[0], Dst)); |
| 2237 | // Last def is now dead. |
Evan Cheng | eca24fb | 2009-05-12 23:07:00 +0000 | [diff] [blame] | 2238 | TransferDeadness(&MBB, Dist, Src, RegKills, KillOps, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2239 | } |
| 2240 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2241 | MBB.erase(&MI); |
| 2242 | Erased = true; |
| 2243 | Spills.disallowClobberPhysReg(VirtReg); |
| 2244 | goto ProcessNextInst; |
| 2245 | } |
Evan Cheng | 2578ba2 | 2009-07-01 01:59:31 +0000 | [diff] [blame] | 2246 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2247 | // If it's not a no-op copy, it clobbers the value in the destreg. |
| 2248 | Spills.ClobberPhysReg(VirtReg); |
| 2249 | ReusedOperands.markClobbered(VirtReg); |
| 2250 | |
| 2251 | // Check to see if this instruction is a load from a stack slot into |
| 2252 | // a register. If so, this provides the stack slot value in the reg. |
| 2253 | int FrameIdx; |
| 2254 | if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) { |
| 2255 | assert(DestReg == VirtReg && "Unknown load situation!"); |
| 2256 | |
| 2257 | // If it is a folded reference, then it's not safe to clobber. |
| 2258 | bool Folded = FoldedSS.count(FrameIdx); |
| 2259 | // Otherwise, if it wasn't available, remember that it is now! |
| 2260 | Spills.addAvailable(FrameIdx, DestReg, !Folded); |
| 2261 | goto ProcessNextInst; |
| 2262 | } |
| 2263 | |
| 2264 | continue; |
| 2265 | } |
| 2266 | |
| 2267 | unsigned SubIdx = MO.getSubReg(); |
| 2268 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 2269 | if (DoReMat) |
| 2270 | ReMatDefs.insert(&MI); |
| 2271 | |
| 2272 | // The only vregs left are stack slot definitions. |
| 2273 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 2274 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
| 2275 | |
| 2276 | // If this def is part of a two-address operand, make sure to execute |
| 2277 | // the store from the correct physical register. |
| 2278 | unsigned PhysReg; |
| 2279 | unsigned TiedOp; |
| 2280 | if (MI.isRegTiedToUseOperand(i, &TiedOp)) { |
| 2281 | PhysReg = MI.getOperand(TiedOp).getReg(); |
| 2282 | if (SubIdx) { |
| 2283 | unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI); |
| 2284 | assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg && |
| 2285 | "Can't find corresponding super-register!"); |
| 2286 | PhysReg = SuperReg; |
| 2287 | } |
| 2288 | } else { |
| 2289 | PhysReg = VRM.getPhys(VirtReg); |
| 2290 | if (ReusedOperands.isClobbered(PhysReg)) { |
| 2291 | // Another def has taken the assigned physreg. It must have been a |
| 2292 | // use&def which got it due to reuse. Undo the reuse! |
Evan Cheng | 5d88502 | 2009-07-21 09:15:00 +0000 | [diff] [blame] | 2293 | PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, |
| 2294 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | assert(PhysReg && "VR not assigned a physical register?"); |
| 2299 | RegInfo->setPhysRegUsed(PhysReg); |
| 2300 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
| 2301 | ReusedOperands.markClobbered(RReg); |
| 2302 | MI.getOperand(i).setReg(RReg); |
| 2303 | MI.getOperand(i).setSubReg(0); |
| 2304 | |
| 2305 | if (!MO.isDead()) { |
| 2306 | MachineInstr *&LastStore = MaybeDeadStores[StackSlot]; |
| 2307 | SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true, |
| 2308 | LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM); |
| 2309 | NextMII = next(MII); |
| 2310 | |
| 2311 | // Check to see if this is a noop copy. If so, eliminate the |
| 2312 | // instruction before considering the dest reg to be changed. |
| 2313 | { |
| 2314 | unsigned Src, Dst, SrcSR, DstSR; |
| 2315 | if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) { |
| 2316 | ++NumDCE; |
Chris Lattner | 6456d38 | 2009-08-23 03:20:44 +0000 | [diff] [blame] | 2317 | DEBUG(errs() << "Removing now-noop copy: " << MI); |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2318 | InvalidateKills(MI, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2319 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2320 | MBB.erase(&MI); |
| 2321 | Erased = true; |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2322 | UpdateKills(*LastStore, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2323 | goto ProcessNextInst; |
| 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | ProcessNextInst: |
Evan Cheng | 5248468 | 2009-07-18 02:10:10 +0000 | [diff] [blame] | 2329 | // Delete dead instructions without side effects. |
Evan Cheng | fc6ad40 | 2009-07-22 00:25:27 +0000 | [diff] [blame] | 2330 | if (!Erased && !BackTracked && TII->isDeadInstruction(&MI)) { |
Evan Cheng | 5248468 | 2009-07-18 02:10:10 +0000 | [diff] [blame] | 2331 | InvalidateKills(MI, TRI, RegKills, KillOps); |
| 2332 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 2333 | MBB.erase(&MI); |
| 2334 | Erased = true; |
| 2335 | } |
| 2336 | if (!Erased) |
| 2337 | DistanceMap.insert(std::make_pair(&MI, Dist++)); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2338 | if (!Erased && !BackTracked) { |
| 2339 | for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II) |
Evan Cheng | 427a6b6 | 2009-05-15 06:48:19 +0000 | [diff] [blame] | 2340 | UpdateKills(*II, TRI, RegKills, KillOps); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2341 | } |
| 2342 | MII = NextMII; |
| 2343 | } |
| 2344 | |
| 2345 | } |
| 2346 | |
| 2347 | }; |
| 2348 | |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2351 | llvm::VirtRegRewriter* llvm::createVirtRegRewriter() { |
| 2352 | switch (RewriterOpt) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2353 | default: llvm_unreachable("Unreachable!"); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2354 | case local: |
| 2355 | return new LocalRewriter(); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 2356 | case trivial: |
| 2357 | return new TrivialRewriter(); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |