blob: e93c51d12465637346534801039f81c2237b9d7c [file] [log] [blame]
Chris Lattner6b379da2003-09-23 15:13:04 +00001//===-- PhyRegAlloc.h - Graph Coloring Register Allocator -------*- c++ -*-===//
John Criswell29265fe2003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner6b379da2003-09-23 15:13:04 +00009//
10// This is the main entry point for register allocation.
11//
12// Notes:
13// * RegisterClasses: Each RegClass accepts a
14// TargetRegClass which contains machine specific info about that register
15// class. The code in the RegClass is machine independent and they use
16// access functions in the TargetRegClass object passed into it to get
17// machine specific info.
18//
19// * Machine dependent work: All parts of the register coloring algorithm
20// except coloring of an individual node are machine independent.
21//
22//===----------------------------------------------------------------------===//
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000023
Brian Gaeke58dabb42003-09-21 02:31:25 +000024#ifndef PHYREGALLOC_H
25#define PHYREGALLOC_H
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000026
Chris Lattnere80612a2003-09-01 20:12:17 +000027#include "LiveRangeInfo.h"
Brian Gaekee1061012003-09-21 01:23:46 +000028#include "llvm/Pass.h"
Vikram S. Adve91e75d82003-07-29 19:37:41 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
Brian Gaekee1061012003-09-21 01:23:46 +000030#include "llvm/Target/TargetMachine.h"
Misha Brukmandc077752003-10-23 18:06:27 +000031#include "llvm/Target/TargetRegInfo.h"
Chris Lattner50cf8f12002-04-28 20:40:16 +000032#include <map>
33
Misha Brukman7ae7f842002-10-28 00:28:31 +000034class MachineFunction;
Chris Lattnerf9986852002-04-27 07:27:19 +000035class FunctionLiveVarInfo;
Chris Lattnerb0da8b22002-02-04 05:52:08 +000036class MachineInstr;
Chris Lattner002958c2002-04-28 16:19:42 +000037class LoopInfo;
Chris Lattner19a7cb22003-01-15 19:56:21 +000038class RegClass;
Brian Gaeke1542a8b2003-09-24 18:08:54 +000039class Constant;
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000040
41//----------------------------------------------------------------------------
42// Class AddedInstrns:
43// When register allocator inserts new instructions in to the existing
44// instruction stream, it does NOT directly modify the instruction stream.
45// Rather, it creates an object of AddedInstrns and stick it in the
46// AddedInstrMap for an existing instruction. This class contains two vectors
47// to store such instructions added before and after an existing instruction.
48//----------------------------------------------------------------------------
49
Chris Lattner30e23da2002-04-09 05:13:04 +000050struct AddedInstrns {
Chris Lattnerb1e39b52002-10-28 19:43:23 +000051 std::vector<MachineInstr*> InstrnsBefore;//Insts added BEFORE an existing inst
52 std::vector<MachineInstr*> InstrnsAfter; //Insts added AFTER an existing inst
Brian Gaekee1061012003-09-21 01:23:46 +000053 inline void clear () { InstrnsBefore.clear (); InstrnsAfter.clear (); }
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000054};
55
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000056//----------------------------------------------------------------------------
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000057// class PhyRegAlloc:
Brian Gaekee1061012003-09-21 01:23:46 +000058// Main class the register allocator. Call runOnFunction() to allocate
Chris Lattnerf739fa82002-04-08 22:03:57 +000059// registers for a Function.
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000060//----------------------------------------------------------------------------
61
Brian Gaekee1061012003-09-21 01:23:46 +000062class PhyRegAlloc : public FunctionPass {
Chris Lattner7f74a562002-01-20 22:54:45 +000063 std::vector<RegClass *> RegClassList; // vector of register classes
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000064 const TargetMachine &TM; // target machine
Chris Lattnerb1e39b52002-10-28 19:43:23 +000065 const Function *Fn; // name of the function we work on
Brian Gaekee1061012003-09-21 01:23:46 +000066 MachineFunction *MF; // descriptor for method's native code
67 FunctionLiveVarInfo *LVI; // LV information for this method
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000068 // (already computed for BBs)
Brian Gaekee1061012003-09-21 01:23:46 +000069 LiveRangeInfo *LRI; // LR info (will be computed)
Chris Lattnerf9781b52002-12-29 03:13:05 +000070 const TargetRegInfo &MRI; // Machine Register information
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000071 const unsigned NumOfRegClasses; // recorded here for efficiency
72
Brian Gaekec8a9ec02003-09-16 15:36:50 +000073 // Map to indicate whether operands of each MachineInstr have been
74 // updated according to their assigned colors. This is only used in
75 // assertion checking (debug builds).
Vikram S. Adve24ce4d82003-05-31 07:41:54 +000076 std::map<const MachineInstr *, bool> OperandsColoredMap;
Ruchira Sasankaca632ed2001-11-03 17:14:44 +000077
Chris Lattner76014b92002-10-29 17:08:05 +000078 // AddedInstrMap - Used to store instrns added in this phase
79 std::map<const MachineInstr *, AddedInstrns> AddedInstrMap;
80
Chris Lattner92f5fb52003-08-05 22:09:31 +000081 // ScratchRegsUsed - Contains scratch register uses for a particular MI.
82 typedef std::multimap<const MachineInstr*, int> ScratchRegsUsedTy;
83 ScratchRegsUsedTy ScratchRegsUsed;
84
Vikram S. Adve40221aa2002-04-25 04:46:28 +000085 AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
Brian Gaekee1061012003-09-21 01:23:46 +000086 const LoopInfo *LoopDepthCalc; // to calculate loop depths
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000087
Brian Gaekef140b282003-10-22 20:44:29 +000088 std::map<const Function *, std::vector<Constant *> > FnAllocState;
Brian Gaeke1542a8b2003-09-24 18:08:54 +000089
Chris Lattnere62a2a72003-08-05 22:03:27 +000090 PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT
91 void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT
Chris Lattner669a74c2002-02-04 17:38:48 +000092public:
Brian Gaekee1061012003-09-21 01:23:46 +000093 inline PhyRegAlloc (const TargetMachine &TM_) :
94 TM (TM_), MRI (TM.getRegInfo ()),
95 NumOfRegClasses (MRI.getNumOfRegClasses ()) { }
96 virtual ~PhyRegAlloc() { }
Chris Lattner669a74c2002-02-04 17:38:48 +000097
Brian Gaekee1061012003-09-21 01:23:46 +000098 /// runOnFunction - Main method called for allocating registers.
99 ///
100 virtual bool runOnFunction (Function &F);
Vikram S. Advececde712002-03-18 03:26:48 +0000101
Brian Gaeke1542a8b2003-09-24 18:08:54 +0000102 virtual bool doFinalization (Module &M);
103
Chris Lattner6b379da2003-09-23 15:13:04 +0000104 virtual void getAnalysisUsage (AnalysisUsage &AU) const;
Brian Gaekee1061012003-09-21 01:23:46 +0000105
106 const char *getPassName () const {
107 return "Traditional graph-coloring reg. allocator";
108 }
Brian Gaeke43593b82003-09-21 02:24:09 +0000109
110 inline const RegClass* getRegClassByID(unsigned id) const {
Vikram S. Adveabcd8d72003-07-25 21:00:13 +0000111 return RegClassList[id];
Vikram S. Advececde712002-03-18 03:26:48 +0000112 }
Brian Gaeke43593b82003-09-21 02:24:09 +0000113 inline RegClass *getRegClassByID(unsigned id) { return RegClassList[id]; }
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000114
Chris Lattner669a74c2002-02-04 17:38:48 +0000115private:
Chris Lattnerb1def732002-02-05 02:51:01 +0000116 void addInterference(const Value *Def, const ValueSet *LVSet,
117 bool isCallInst);
Brian Gaekee1061012003-09-21 01:23:46 +0000118 bool markAllocatedRegs(MachineInstr* MInst);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000119
120 void addInterferencesForArgs();
121 void createIGNodeListsAndIGs();
122 void buildInterferenceGraphs();
Brian Gaeke1542a8b2003-09-24 18:08:54 +0000123 void saveState();
Brian Gaekef29231ec32003-10-22 20:23:13 +0000124 void verifySavedState();
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000125
Chris Lattnere62a2a72003-08-05 22:03:27 +0000126 void setCallInterferences(const MachineInstr *MI,
127 const ValueSet *LVSetAft);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000128
Ruchira Sasanka33b0d852001-10-23 21:38:42 +0000129 void move2DelayedInstr(const MachineInstr *OrigMI,
Chris Lattnere62a2a72003-08-05 22:03:27 +0000130 const MachineInstr *DelayedMI);
Ruchira Sasanka33b0d852001-10-23 21:38:42 +0000131
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000132 void markUnusableSugColors();
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000133 void allocateStackSpace4SpilledLRs();
134
Chris Lattnere62a2a72003-08-05 22:03:27 +0000135 void insertCode4SpilledLR(const LiveRange *LR,
136 MachineBasicBlock::iterator& MII,
137 MachineBasicBlock &MBB, unsigned OpNum);
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000138
Misha Brukmandc077752003-10-23 18:06:27 +0000139 /// Method for inserting caller saving code. The caller must save all the
140 /// volatile registers live across a call.
141 ///
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000142 void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
143 std::vector<MachineInstr*>& instrnsAfter,
144 MachineInstr *CallMI,
145 const BasicBlock *BB);
146
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000147 void colorIncomingArgs();
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000148 void colorCallRetArgs();
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000149 void updateMachineCode();
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000150 void updateInstruction(MachineBasicBlock::iterator& MII,
151 MachineBasicBlock &MBB);
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000152
Chris Lattnere62a2a72003-08-05 22:03:27 +0000153 int getUsableUniRegAtMI(int RegType, const ValueSet *LVSetBef,
154 MachineInstr *MI,
Vikram S. Advebeb36402002-07-08 22:39:36 +0000155 std::vector<MachineInstr*>& MIBef,
156 std::vector<MachineInstr*>& MIAft);
157
Misha Brukmandc077752003-10-23 18:06:27 +0000158 /// Callback method used to find unused registers.
159 /// LVSetBef is the live variable set to search for an unused register.
160 /// If it is not specified, the LV set before the current MI is used.
161 /// This is sufficient as long as no new copy instructions are generated
162 /// to copy the free register to memory.
163 ///
Chris Lattnere62a2a72003-08-05 22:03:27 +0000164 int getUnusedUniRegAtMI(RegClass *RC, int RegType,
165 const MachineInstr *MI,
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000166 const ValueSet *LVSetBef = 0);
167
Chris Lattnere62a2a72003-08-05 22:03:27 +0000168 void setRelRegsUsedByThisInst(RegClass *RC, int RegType,
169 const MachineInstr *MI);
Vikram S. Adveabcd8d72003-07-25 21:00:13 +0000170
Chris Lattnere62a2a72003-08-05 22:03:27 +0000171 int getUniRegNotUsedByThisInst(RegClass *RC, int RegType,
172 const MachineInstr *MI);
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000173
Chris Lattnere62a2a72003-08-05 22:03:27 +0000174 void addInterf4PseudoInstr(const MachineInstr *MI);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000175};
176
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000177#endif