blob: 8a22e632734da0bd7d8cb8e55db3745f9a4766cd [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
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// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000016#include "RegisterClassInfo.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000017#include "llvm/BasicBlock.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
Devang Patel459a36b2010-08-04 18:42:02 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/CodeGen/RegAllocRegistry.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/IndexedMap.h"
33#include "llvm/ADT/SmallSet.h"
34#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000035#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/STLExtras.h"
38#include <algorithm>
39using namespace llvm;
40
41STATISTIC(NumStores, "Number of stores added");
42STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000043STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000044
45static RegisterRegAlloc
46 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
47
48namespace {
49 class RAFast : public MachineFunctionPass {
50 public:
51 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000052 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trick8dd26252012-02-10 04:10:36 +000053 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000054 private:
55 const TargetMachine *TM;
56 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000057 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000058 const TargetRegisterInfo *TRI;
59 const TargetInstrInfo *TII;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000060 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000061
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000062 // Basic block currently being allocated.
63 MachineBasicBlock *MBB;
64
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000065 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
66 // values are spilled.
67 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
68
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000069 // Everything we know about a live virtual register.
70 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000071 MachineInstr *LastUse; // Last instr to use reg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000072 unsigned VirtReg; // Virtual register number.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000073 unsigned PhysReg; // Currently held here.
74 unsigned short LastOpNum; // OpNum on LastUse.
75 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000076
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000077 explicit LiveReg(unsigned v)
78 : LastUse(0), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false) {}
79
Andrew Trickc0ccb8b2012-04-20 20:05:28 +000080 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000081 return TargetRegisterInfo::virtReg2Index(VirtReg);
82 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000083 };
84
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000085 typedef SparseSet<LiveReg> LiveRegMap;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000086
87 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000088 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000089 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000090
Devang Patel72d9b0e2011-06-21 22:36:03 +000091 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Patel459a36b2010-08-04 18:42:02 +000092
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000093 // RegState - Track the state of a physical register.
94 enum RegState {
95 // A disabled register is not available for allocation, but an alias may
96 // be in use. A register can only be moved out of the disabled state if
97 // all aliases are disabled.
98 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000099
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000100 // A free register is not currently in use and can be allocated
101 // immediately without checking aliases.
102 regFree,
103
Evan Chengd8a16242011-04-22 01:40:20 +0000104 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000105 // call parameter), and it remains reserved until it is used.
106 regReserved
107
108 // A register state may also be a virtual register number, indication that
109 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000110 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000111 };
112
113 // PhysRegState - One of the RegState enums, or a virtreg.
114 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115
116 // UsedInInstr - BitVector of physregs that are used in the current
117 // instruction, and so cannot be allocated.
118 BitVector UsedInInstr;
119
Jim Grosbach07cb6892010-09-01 19:16:29 +0000120 // SkippedInstrs - Descriptors of instructions whose clobber list was
121 // ignored because all registers were spilled. It is still necessary to
122 // mark all the clobbered registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +0000123 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000124
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000125 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
126 // completely after spilling all live registers. LiveRegMap entries should
127 // not be erased.
128 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000129
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000130 enum {
131 spillClean = 1,
132 spillDirty = 100,
133 spillImpossible = ~0u
134 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000135 public:
136 virtual const char *getPassName() const {
137 return "Fast Register Allocator";
138 }
139
140 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesCFG();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000142 MachineFunctionPass::getAnalysisUsage(AU);
143 }
144
145 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000146 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000147 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000148 void handleThroughOperands(MachineInstr *MI,
149 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000150 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000151 bool isLastUseOfLocalReg(MachineOperand&);
152
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000153 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000154 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000155 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000156 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000157 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000158
159 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000160 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000161 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000162 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
163 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
164 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
165 }
166 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
167 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
168 }
169 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
170 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
171 unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000172 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
173 unsigned VirtReg, unsigned Hint);
174 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
175 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000176 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000177 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Andrew Trickb3d58472012-01-31 05:55:32 +0000178 void addRetOperands(MachineBasicBlock *MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000179 };
180 char RAFast::ID = 0;
181}
182
183/// getStackSpaceFor - This allocates space for the specified virtual register
184/// to be held on the stack.
185int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
186 // Find the location Reg would belong...
187 int SS = StackSlotForVirtReg[VirtReg];
188 if (SS != -1)
189 return SS; // Already has space allocated?
190
191 // Allocate a new stack object for this spill location...
192 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
193 RC->getAlignment());
194
195 // Assign the slot.
196 StackSlotForVirtReg[VirtReg] = FrameIdx;
197 return FrameIdx;
198}
199
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000200/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
201/// its virtual register, and it is guaranteed to be a block-local register.
202///
203bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
204 // Check for non-debug uses or defs following MO.
205 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000206 MachineOperand *Next = &MO;
207 while ((Next = Next->getNextOperandForReg()))
208 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000209 return false;
210
211 // If the register has ever been spilled or reloaded, we conservatively assume
212 // it is a global register used in multiple blocks.
213 if (StackSlotForVirtReg[MO.getReg()] != -1)
214 return false;
215
216 // Check that the use/def chain has exactly one operand - MO.
217 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
218}
219
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000220/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000221void RAFast::addKillFlag(const LiveReg &LR) {
222 if (!LR.LastUse) return;
223 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000224 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
225 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000226 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000227 else
228 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
229 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000230}
231
232/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000233void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000234 addKillFlag(*LRI);
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000235 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
236 "Broken RegState mapping");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000237 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000238 // Erase from LiveVirtRegs unless we're spilling in bulk.
239 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000240 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000241}
242
243/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000244void RAFast::killVirtReg(unsigned VirtReg) {
245 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
246 "killVirtReg needs a virtual register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000247 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000248 if (LRI != LiveVirtRegs.end())
249 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000250}
251
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000252/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000253/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000254void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000255 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
256 "Spilling a physical register is illegal!");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000257 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
259 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000260}
261
262/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000263void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000264 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000265 LiveReg &LR = *LRI;
266 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000267
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000268 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000269 // If this physreg is used by the instruction, we want to kill it on the
270 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000271 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000272 LR.Dirty = false;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000273 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000274 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000275 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
276 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000277 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000278 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000279 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000280
Jim Grosbach07cb6892010-09-01 19:16:29 +0000281 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000282 // identify spilled location as the place to find corresponding variable's
283 // value.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000284 SmallVector<MachineInstr *, 4> &LRIDbgValues =
285 LiveDbgValueMap[LRI->VirtReg];
Devang Patel72d9b0e2011-06-21 22:36:03 +0000286 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
287 MachineInstr *DBG = LRIDbgValues[li];
Jim Grosbach07cb6892010-09-01 19:16:29 +0000288 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000289 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
290 int64_t Offset = 0;
291 if (DBG->getOperand(1).isImm())
292 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000293 DebugLoc DL;
294 if (MI == MBB->end()) {
295 // If MI is at basic block end then use last instruction's location.
296 MachineBasicBlock::iterator EI = MI;
297 DL = (--EI)->getDebugLoc();
298 }
299 else
300 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000301 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000302 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
303 MachineBasicBlock *MBB = DBG->getParent();
304 MBB->insert(MI, NewDV);
305 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel459a36b2010-08-04 18:42:02 +0000306 }
307 }
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000308 // Now this register is spilled there is should not be any DBG_VALUE
309 // pointing to this register because they are all pointing to spilled value
310 // now.
Devang Patel6f373a82011-06-21 23:02:36 +0000311 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000312 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000313 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000314 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000315 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000316}
317
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000318/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000319void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000320 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000321 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000322 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
323 // of spilling here is deterministic, if arbitrary.
324 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
325 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000326 spillVirtReg(MI, i);
327 LiveVirtRegs.clear();
328 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000329}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000330
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000331/// usePhysReg - Handle the direct use of a physical register.
332/// Check that the register is not used by a virtreg.
333/// Kill the physreg, marking it free.
334/// This may add implicit kills to MO->getParent() and invalidate MO.
335void RAFast::usePhysReg(MachineOperand &MO) {
336 unsigned PhysReg = MO.getReg();
337 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
338 "Bad usePhysReg operand");
339
340 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000341 case regDisabled:
342 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 case regReserved:
344 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345 // Fall through
346 case regFree:
347 UsedInInstr.set(PhysReg);
348 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000349 return;
350 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000351 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000352 // wanted has been clobbered.
353 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000354 }
355
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 // Maybe a superregister is reserved?
Craig Toppere4fd9072012-03-04 10:43:23 +0000357 for (const uint16_t *AS = TRI->getAliasSet(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000358 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000359 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000360 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000361 break;
362 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000363 assert(TRI->isSuperRegister(PhysReg, Alias) &&
364 "Instruction is not using a subregister of a reserved register");
365 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000366 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000367 UsedInInstr.set(Alias);
368 MO.getParent()->addRegisterKilled(Alias, TRI, true);
369 return;
370 case regFree:
371 if (TRI->isSuperRegister(PhysReg, Alias)) {
372 // Leave the superregister in the working set.
373 UsedInInstr.set(Alias);
374 MO.getParent()->addRegisterKilled(Alias, TRI, true);
375 return;
376 }
377 // Some other alias was in the working set - clear it.
378 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000379 break;
380 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000381 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000382 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000383 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000384
385 // All aliases are disabled, bring register into working set.
386 PhysRegState[PhysReg] = regFree;
387 UsedInInstr.set(PhysReg);
388 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000389}
390
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000391/// definePhysReg - Mark PhysReg as reserved or free after spilling any
392/// virtregs. This is very similar to defineVirtReg except the physreg is
393/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000394void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
395 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000396 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
398 case regDisabled:
399 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000401 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000402 // Fall through.
403 case regFree:
404 case regReserved:
405 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000406 return;
407 }
408
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000409 // This is a disabled register, disable all aliases.
410 PhysRegState[PhysReg] = NewState;
Craig Toppere4fd9072012-03-04 10:43:23 +0000411 for (const uint16_t *AS = TRI->getAliasSet(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000412 unsigned Alias = *AS; ++AS) {
413 switch (unsigned VirtReg = PhysRegState[Alias]) {
414 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000415 break;
416 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000417 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000418 // Fall through.
419 case regFree:
420 case regReserved:
421 PhysRegState[Alias] = regDisabled;
422 if (TRI->isSuperRegister(PhysReg, Alias))
423 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000424 break;
425 }
426 }
427}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000428
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000429
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000430// calcSpillCost - Return the cost of spilling clearing out PhysReg and
431// aliases so it is free for allocation.
432// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
433// can be allocated directly.
434// Returns spillImpossible when PhysReg or an alias can't be spilled.
435unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000436 if (UsedInInstr.test(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000437 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000438 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000439 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000440 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
441 case regDisabled:
442 break;
443 case regFree:
444 return 0;
445 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000446 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
447 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000448 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000449 default: {
450 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
451 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
452 return I->Dirty ? spillDirty : spillClean;
453 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000454 }
455
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000456 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000457 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000458 unsigned Cost = 0;
Craig Toppere4fd9072012-03-04 10:43:23 +0000459 for (const uint16_t *AS = TRI->getAliasSet(PhysReg);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000460 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000461 if (UsedInInstr.test(Alias))
462 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000463 switch (unsigned VirtReg = PhysRegState[Alias]) {
464 case regDisabled:
465 break;
466 case regFree:
467 ++Cost;
468 break;
469 case regReserved:
470 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000471 default: {
472 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
473 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
474 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000475 break;
476 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000477 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000478 }
479 return Cost;
480}
481
482
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000483/// assignVirtToPhysReg - This method updates local state so that we know
484/// that PhysReg is the proper container for VirtReg now. The physical
485/// register must not be used for anything else when this is called.
486///
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000487void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
488 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000489 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000490 PhysRegState[PhysReg] = LR.VirtReg;
491 assert(!LR.PhysReg && "Already assigned a physreg");
492 LR.PhysReg = PhysReg;
493}
494
495RAFast::LiveRegMap::iterator
496RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
497 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
498 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
499 assignVirtToPhysReg(*LRI, PhysReg);
500 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000501}
502
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000504RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
505 LiveRegMap::iterator LRI,
506 unsigned Hint) {
507 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000508
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000509 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
510 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000511
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000512 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000513
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000514 // Ignore invalid hints.
515 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000516 !RC->contains(Hint) || !RegClassInfo.isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000517 Hint = 0;
518
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000519 // Take hint when possible.
520 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000521 // Ignore the hint if we would have to spill a dirty register.
522 unsigned Cost = calcSpillCost(Hint);
523 if (Cost < spillDirty) {
524 if (Cost)
525 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000526 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
527 // That invalidates LRI, so run a new lookup for VirtReg.
528 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000529 }
530 }
531
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000532 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000533
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000534 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000535 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000536 unsigned PhysReg = *I;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000537 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg)) {
538 assignVirtToPhysReg(*LRI, PhysReg);
539 return LRI;
540 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000541 }
542
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000543 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
544 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000545
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000546 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000547 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000548 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000549 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000550 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
551 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000552 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000553 if (Cost == 0) {
554 assignVirtToPhysReg(*LRI, *I);
555 return LRI;
556 }
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000557 if (Cost < BestCost)
558 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000559 }
560
561 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000562 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000563 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
564 // That invalidates LRI, so run a new lookup for VirtReg.
565 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000566 }
567
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000568 // Nothing we can do. Report an error and keep going with a bad allocation.
569 MI->emitError("ran out of registers during register allocation");
570 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000571 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000572}
573
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000575RAFast::LiveRegMap::iterator
576RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
577 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000578 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
579 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000580 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000581 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000582 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000583 if (New) {
584 // If there is no hint, peek at the only use of this register.
585 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
586 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000587 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000588 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000589 if (UseMI.isCopyLike())
590 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000591 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000592 LRI = allocVirtReg(MI, LRI, Hint);
593 } else if (LRI->LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000594 // Redefining a live register - kill at the last use, unless it is this
595 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000596 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
597 addKillFlag(*LRI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000598 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000599 assert(LRI->PhysReg && "Register not assigned");
600 LRI->LastUse = MI;
601 LRI->LastOpNum = OpNum;
602 LRI->Dirty = true;
603 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000604 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000605}
606
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000607/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000608RAFast::LiveRegMap::iterator
609RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
610 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000611 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
612 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000613 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000614 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000615 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000616 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000617 if (New) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000618 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000619 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000621 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000622 << PrintReg(LRI->PhysReg, TRI) << "\n");
623 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000624 ++NumLoads;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000625 } else if (LRI->Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000626 if (isLastUseOfLocalReg(MO)) {
627 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000628 if (MO.isUse())
629 MO.setIsKill();
630 else
631 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000632 } else if (MO.isKill()) {
633 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
634 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000635 } else if (MO.isDead()) {
636 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
637 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000638 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000639 } else if (MO.isKill()) {
640 // We must remove kill flags from uses of reloaded registers because the
641 // register would be killed immediately, and there might be a second use:
642 // %foo = OR %x<kill>, %x
643 // This would cause a second reload of %x into a different register.
644 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
645 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000646 } else if (MO.isDead()) {
647 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
648 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000649 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000650 assert(LRI->PhysReg && "Register not assigned");
651 LRI->LastUse = MI;
652 LRI->LastOpNum = OpNum;
653 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000654 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000655}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000656
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000657// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
658// subregs. This may invalidate any operand pointers.
659// Return true if the operand kills its register.
660bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
661 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000662 bool Dead = MO.isDead();
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000663 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000664 MO.setReg(PhysReg);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000665 return MO.isKill() || Dead;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000666 }
667
668 // Handle subregister index.
669 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
670 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000671
672 // A kill flag implies killing the full register. Add corresponding super
673 // register kill.
674 if (MO.isKill()) {
675 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000676 return true;
677 }
Jakob Stoklund Olesen4d108292012-05-14 21:10:25 +0000678
679 // A <def,read-undef> of a sub-register requires an implicit def of the full
680 // register.
681 if (MO.isDef() && MO.isUndef())
682 MI->addRegisterDefined(PhysReg, TRI);
683
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000684 return Dead;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000685}
686
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000687// Handle special instruction operand like early clobbers and tied ops when
688// there are additional physreg defines.
689void RAFast::handleThroughOperands(MachineInstr *MI,
690 SmallVectorImpl<unsigned> &VirtDead) {
691 DEBUG(dbgs() << "Scanning for through registers:");
692 SmallSet<unsigned, 8> ThroughRegs;
693 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
694 MachineOperand &MO = MI->getOperand(i);
695 if (!MO.isReg()) continue;
696 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000697 if (!TargetRegisterInfo::isVirtualRegister(Reg))
698 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000699 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
700 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000701 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000702 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000703 }
704 }
705
706 // If any physreg defines collide with preallocated through registers,
707 // we must spill and reallocate.
708 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
709 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
710 MachineOperand &MO = MI->getOperand(i);
711 if (!MO.isReg() || !MO.isDef()) continue;
712 unsigned Reg = MO.getReg();
713 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000714 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
715 UsedInInstr.set(*AI);
716 if (ThroughRegs.count(PhysRegState[*AI]))
717 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000718 }
719 }
720
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000721 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000722 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000723 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
724 MachineOperand &MO = MI->getOperand(i);
725 if (!MO.isReg()) continue;
726 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000727 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000728 if (MO.isUse()) {
729 unsigned DefIdx = 0;
730 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
731 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
732 << DefIdx << ".\n");
733 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000734 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000735 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000736 // Note: we don't update the def operand yet. That would cause the normal
737 // def-scan to attempt spilling.
738 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
739 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
740 // Reload the register, but don't assign to the operand just yet.
741 // That would confuse the later phys-def processing pass.
742 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000743 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000744 }
745 }
746
Rafael Espindola254a1322011-11-22 06:27:18 +0000747 DEBUG(dbgs() << "Allocating early clobbers.\n");
748 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
749 MachineOperand &MO = MI->getOperand(i);
750 if (!MO.isReg()) continue;
751 unsigned Reg = MO.getReg();
752 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
753 if (!MO.isEarlyClobber())
754 continue;
755 // Note: defineVirtReg may invalidate MO.
756 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000757 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola254a1322011-11-22 06:27:18 +0000758 if (setPhysReg(MI, i, PhysReg))
759 VirtDead.push_back(Reg);
760 }
761
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000762 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000763 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000764 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
765 MachineOperand &MO = MI->getOperand(i);
766 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
767 unsigned Reg = MO.getReg();
768 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000769 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
770 << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000771 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000772 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000773
774 // Also mark PartialDefs as used to avoid reallocation.
775 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
776 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000777}
778
Andrew Trickb3d58472012-01-31 05:55:32 +0000779/// addRetOperand - ensure that a return instruction has an operand for each
780/// value live out of the function.
781///
782/// Things marked both call and return are tail calls; do not do this for them.
783/// The tail callee need not take the same registers as input that it produces
784/// as output, and there are dependencies for its input registers elsewhere.
785///
786/// FIXME: This should be done as part of instruction selection, and this helper
787/// should be deleted. Until then, we use custom logic here to create the proper
788/// operand under all circumstances. We can't use addRegisterKilled because that
789/// doesn't make sense for undefined values. We can't simply avoid calling it
790/// for undefined values, because we must ensure that the operand always exists.
791void RAFast::addRetOperands(MachineBasicBlock *MBB) {
792 if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall())
793 return;
794
795 MachineInstr *MI = &MBB->back();
796
797 for (MachineRegisterInfo::liveout_iterator
798 I = MBB->getParent()->getRegInfo().liveout_begin(),
799 E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) {
800 unsigned Reg = *I;
801 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
802 "Cannot have a live-out virtual register.");
803
804 bool hasDef = PhysRegState[Reg] == regReserved;
805
806 // Check if this register already has an operand.
807 bool Found = false;
808 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
809 MachineOperand &MO = MI->getOperand(i);
810 if (!MO.isReg() || !MO.isUse())
811 continue;
812
813 unsigned OperReg = MO.getReg();
Andrew Trickab78e202012-01-31 18:54:19 +0000814 if (!TargetRegisterInfo::isPhysicalRegister(OperReg))
815 continue;
816
817 if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) {
818 // If the ret already has an operand for this physreg or a superset,
819 // don't duplicate it. Set the kill flag if the value is defined.
820 if (hasDef && !MO.isKill())
821 MO.setIsKill();
822 Found = true;
823 break;
Andrew Trickb3d58472012-01-31 05:55:32 +0000824 }
825 }
826 if (!Found)
827 MI->addOperand(MachineOperand::CreateReg(Reg,
828 false /*IsDef*/,
829 true /*IsImp*/,
830 hasDef/*IsKill*/));
831 }
832}
833
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000834void RAFast::AllocateBasicBlock() {
835 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000836
837 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000838 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000839
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000840 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000841
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000842 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000843 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
844 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000845 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000846 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000847
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000848 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000849 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000850
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000851 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000852 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000853 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000854 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000855 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000856 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000857 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
858 if (PhysRegState[Reg] == regDisabled) continue;
859 dbgs() << " " << TRI->getName(Reg);
860 switch(PhysRegState[Reg]) {
861 case regFree:
862 break;
863 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000864 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000865 break;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000866 default: {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000867 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000868 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
869 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
870 if (I->Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000871 dbgs() << "*";
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000872 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000873 break;
874 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000875 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000876 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000877 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000878 // Check that LiveVirtRegs is the inverse.
879 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
880 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000881 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000882 "Bad map key");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000883 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000884 "Bad map value");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000885 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000886 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000887 });
888
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000889 // Debug values are not allowed to change codegen in any way.
890 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000891 bool ScanDbgValue = true;
892 while (ScanDbgValue) {
893 ScanDbgValue = false;
894 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
895 MachineOperand &MO = MI->getOperand(i);
896 if (!MO.isReg()) continue;
897 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000898 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000899 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Patel58b81762010-07-19 23:25:39 +0000900 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000901 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000902 else {
Devang Patel58b81762010-07-19 23:25:39 +0000903 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000904 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000905 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000906 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000907 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000908 }
Devang Patel58b81762010-07-19 23:25:39 +0000909 else {
910 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000911 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000912 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000913 MI->getOperand(MI->getNumOperands()-1).getMetadata();
914 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000915 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000916 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000917 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
918 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000919 MachineBasicBlock *MBB = MI->getParent();
920 MBB->insert(MBB->erase(MI), NewDV);
921 // Scan NewDV operands from the beginning.
922 MI = NewDV;
923 ScanDbgValue = true;
924 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000925 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000926 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000927 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000928 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000929 }
Devang Patel58b81762010-07-19 23:25:39 +0000930 }
Devang Patel7a029b62010-07-09 21:48:31 +0000931 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000932 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000933 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000934 }
935 // Next instruction.
936 continue;
937 }
938
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000939 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000940 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000941 if (MI->isCopy()) {
942 CopyDst = MI->getOperand(0).getReg();
943 CopySrc = MI->getOperand(1).getReg();
944 CopyDstSub = MI->getOperand(0).getSubReg();
945 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000946 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000947
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000948 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000949 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000950
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000951 // First scan.
952 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000953 // Find the end of the virtreg operands
954 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000955 bool hasTiedOps = false;
956 bool hasEarlyClobbers = false;
957 bool hasPartialRedefs = false;
958 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000959 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
960 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000961 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000962 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000963 if (!Reg) continue;
964 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
965 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000966 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000967 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000968 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000969 } else {
970 if (MO.isEarlyClobber())
971 hasEarlyClobbers = true;
972 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
973 hasPartialRedefs = true;
974 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000975 continue;
976 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000977 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000978 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000979 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000980 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000981 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
982 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000983 hasEarlyClobbers = true;
984 } else
985 hasPhysDefs = true;
986 }
987
988 // The instruction may have virtual register operands that must be allocated
989 // the same register at use-time and def-time: early clobbers and tied
990 // operands. If there are also physical defs, these registers must avoid
991 // both physical defs and uses, making them more constrained than normal
992 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000993 // Similarly, if there are multiple defs and tied operands, we must make
994 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000995 // We didn't detect inline asm tied operands above, so just make this extra
996 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000997 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000998 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000999 handleThroughOperands(MI, VirtDead);
1000 // Don't attempt coalescing when we have funny stuff going on.
1001 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001002 // Pretend we have early clobbers so the use operands get marked below.
1003 // This is not necessary for the common case of a single tied use.
1004 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001005 }
1006
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001007 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001008 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +00001009 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001010 MachineOperand &MO = MI->getOperand(i);
1011 if (!MO.isReg()) continue;
1012 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001013 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001014 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001015 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001016 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001017 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001018 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001019 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001020 }
1021 }
1022
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001023 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001024
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001025 // Track registers defined by instruction - early clobbers and tied uses at
1026 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +00001027 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001028 if (hasEarlyClobbers) {
1029 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1030 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001031 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001032 unsigned Reg = MO.getReg();
1033 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001034 // Look for physreg defs and tied uses.
1035 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +00001036 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
1037 UsedInInstr.set(*AI);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001038 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001039 }
1040
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001041 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001042 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001043 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001044 // exception is thrown, the landing pad is going to expect to find
1045 // registers in their spill slots, and 2. we don't have to wade through
1046 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001047 DefOpEnd = VirtOpEnd;
1048 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1049 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001050
1051 // The imp-defs are skipped below, but we still need to mark those
1052 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001053 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001054 }
1055
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001056 // Third scan.
1057 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001058 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001059 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001060 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1061 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001062 unsigned Reg = MO.getReg();
1063
1064 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +00001065 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001066 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1067 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001068 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001069 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001070 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001071 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001072 if (setPhysReg(MI, i, PhysReg)) {
1073 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001074 CopyDst = 0; // cancel coalescing;
1075 } else
1076 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001077 }
1078
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001079 // Kill dead defs after the scan to ensure that multiple defs of the same
1080 // register are allocated identically. We didn't need to do this for uses
1081 // because we are crerating our own kill flags, and they are always at the
1082 // last use.
1083 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1084 killVirtReg(VirtDead[i]);
1085 VirtDead.clear();
1086
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001087 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001088
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001089 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1090 DEBUG(dbgs() << "-- coalescing: " << *MI);
1091 Coalesced.push_back(MI);
1092 } else {
1093 DEBUG(dbgs() << "<< " << *MI);
1094 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001095 }
1096
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001097 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001098 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1099 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001100
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001101 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001102 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001103 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001104 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001105 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001106
Andrew Trickb3d58472012-01-31 05:55:32 +00001107 // addRetOperands must run after we've seen all defs in this block.
1108 addRetOperands(MBB);
1109
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001110 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001111}
1112
1113/// runOnMachineFunction - Register allocate the whole function
1114///
1115bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001116 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1117 << "********** Function: "
1118 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001119 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001120 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001121 TM = &Fn.getTarget();
1122 TRI = TM->getRegisterInfo();
1123 TII = TM->getInstrInfo();
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +00001124 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001125 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001126 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001127
Andrew Trick8dd26252012-02-10 04:10:36 +00001128 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1129
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001130 // initialize the virtual->physical register map to have a 'null'
1131 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001132 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001133 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001134
1135 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001136 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1137 MBBi != MBBe; ++MBBi) {
1138 MBB = &*MBBi;
1139 AllocateBasicBlock();
1140 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001141
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001142 // Add the clobber lists for all the instructions we skipped earlier.
Evan Chenge837dea2011-06-28 19:10:37 +00001143 for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001144 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
Craig Topperfac25982012-03-08 08:22:45 +00001145 if (const uint16_t *Defs = (*I)->getImplicitDefs())
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001146 while (*Defs)
1147 MRI->setPhysRegUsed(*Defs++);
1148
Andrew Trick19273ae2012-02-21 04:51:23 +00001149 // All machine operands and other references to virtual registers have been
1150 // replaced. Remove the virtual registers.
1151 MRI->clearVirtRegs();
1152
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001153 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001154 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001155 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001156 return true;
1157}
1158
1159FunctionPass *llvm::createFastRegisterAllocator() {
1160 return new RAFast();
1161}