llvm.org GIT mirror llvm / 13513b7
Minor refactoring of CC Lowering interfaces git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34656 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
3 changed file(s) with 108 addition(s) and 79 deletion(s). Raw diff Collapse all Expand all
1919
2020 namespace llvm {
2121 class MRegisterInfo;
22 class TargetMachine;
2223
23 /// CCState - This class holds information needed while lowering arguments and
24 /// return values. It captures which registers are already assigned and which
25 /// stack slots are used. It provides accessors to allocate these values.
26 class CCState {
27 unsigned StackOffset;
28 const MRegisterInfo &MRI;
29 SmallVector UsedRegs;
30 public:
31 CCState(const MRegisterInfo &mri);
32
33 unsigned getNextStackOffset() const { return StackOffset; }
34
35 /// isAllocated - Return true if the specified register (or an alias) is
36 /// allocated.
37 bool isAllocated(unsigned Reg) const {
38 return UsedRegs[Reg/32] & (1 << (Reg&31));
39 }
40
41 /// getFirstUnallocated - Return the first unallocated register in the set, or
42 /// NumRegs if they are all allocated.
43 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
44 for (unsigned i = 0; i != NumRegs; ++i)
45 if (!isAllocated(Regs[i]))
46 return i;
47 return NumRegs;
48 }
49
50 /// AllocateReg - Attempt to allocate one of the specified registers. If none
51 /// are available, return zero. Otherwise, return the first one available,
52 /// marking it and any aliases as allocated.
53 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
54 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
55 if (FirstUnalloc == NumRegs)
56 return 0; // Didn't find the reg.
57
58 // Mark the register and any aliases as allocated.
59 unsigned Reg = Regs[FirstUnalloc];
60 MarkAllocated(Reg);
61 return Reg;
62 }
63
64 /// AllocateStack - Allocate a chunk of stack space with the specified size
65 /// and alignment.
66 unsigned AllocateStack(unsigned Size, unsigned Align) {
67 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
68 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
69 unsigned Result = StackOffset;
70 StackOffset += Size;
71 return Result;
72 }
73 private:
74 /// MarkAllocated - Mark a register and all of its aliases as allocated.
75 void MarkAllocated(unsigned Reg);
76 };
77
7824 /// CCValAssign - Represent assignment of one arg/retval to a location.
7925 class CCValAssign {
8026 public:
14389 LocInfo getLocInfo() const { return HTP; }
14490 };
14591
92
93
94 /// CCState - This class holds information needed while lowering arguments and
95 /// return values. It captures which registers are already assigned and which
96 /// stack slots are used. It provides accessors to allocate these values.
97 class CCState {
98 unsigned CallingConv;
99 const TargetMachine &TM;
100 const MRegisterInfo &MRI;
101 SmallVector &Locs;
102
103 unsigned StackOffset;
104 SmallVector UsedRegs;
105 public:
106 CCState(unsigned CC, const TargetMachine &TM,
107 SmallVector &locs);
108
109 void addLoc(const CCValAssign &V) {
110 Locs.push_back(V);
111 }
112
113 const TargetMachine &getTarget() const { return TM; }
114 unsigned getCallingConv() const { return CallingConv; }
115
116 unsigned getNextStackOffset() const { return StackOffset; }
117
118 /// isAllocated - Return true if the specified register (or an alias) is
119 /// allocated.
120 bool isAllocated(unsigned Reg) const {
121 return UsedRegs[Reg/32] & (1 << (Reg&31));
122 }
123
124 /// getFirstUnallocated - Return the first unallocated register in the set, or
125 /// NumRegs if they are all allocated.
126 unsigned getFirstUnallocated(const unsigned *Regs, unsigned NumRegs) const {
127 for (unsigned i = 0; i != NumRegs; ++i)
128 if (!isAllocated(Regs[i]))
129 return i;
130 return NumRegs;
131 }
132
133 /// AllocateReg - Attempt to allocate one register. If it is not available,
134 /// return zero. Otherwise, return the register, marking it and any aliases
135 /// as allocated.
136 unsigned AllocateReg(unsigned Reg) {
137 if (isAllocated(Reg)) return 0;
138 MarkAllocated(Reg);
139 return Reg;
140 }
141
142 /// AllocateReg - Attempt to allocate one of the specified registers. If none
143 /// are available, return zero. Otherwise, return the first one available,
144 /// marking it and any aliases as allocated.
145 unsigned AllocateReg(const unsigned *Regs, unsigned NumRegs) {
146 unsigned FirstUnalloc = getFirstUnallocated(Regs, NumRegs);
147 if (FirstUnalloc == NumRegs)
148 return 0; // Didn't find the reg.
149
150 // Mark the register and any aliases as allocated.
151 unsigned Reg = Regs[FirstUnalloc];
152 MarkAllocated(Reg);
153 return Reg;
154 }
155
156 /// AllocateStack - Allocate a chunk of stack space with the specified size
157 /// and alignment.
158 unsigned AllocateStack(unsigned Size, unsigned Align) {
159 assert(Align && ((Align-1) & Align) == 0); // Align is power of 2.
160 StackOffset = ((StackOffset + Align-1) & ~(Align-1));
161 unsigned Result = StackOffset;
162 StackOffset += Size;
163 return Result;
164 }
165 private:
166 /// MarkAllocated - Mark a register and all of its aliases as allocated.
167 void MarkAllocated(unsigned Reg);
168 };
169
146170 } // end namespace llvm
147171
148172 #endif
1313
1414 #include "llvm/CodeGen/CallingConvLower.h"
1515 #include "llvm/Target/MRegisterInfo.h"
16 #include "llvm/Target/TargetMachine.h"
1617 using namespace llvm;
1718
18 CCState::CCState(const MRegisterInfo &mri) : MRI(mri) {
19 CCState::CCState(unsigned CC, const TargetMachine &tm,
20 SmallVector &locs)
21 : CallingConv(CC), TM(tm), MRI(*TM.getRegisterInfo()), Locs(locs) {
1922 // No stack is used.
2023 StackOffset = 0;
2124
427427 //===----------------------------------------------------------------------===//
428428 // Return Value Calling Convention Implementation
429429 //===----------------------------------------------------------------------===//
430
430431
431432 /// GetRetValueLocs - If we are returning a set of values with the specified
432433 /// value types, determine the set of registers each one will land in. This
10591060 //===----------------------------------------------------------------------===//
10601061
10611062
1062 /// X86_64_CCC_AssignArgument - Implement the X86-64 C Calling Convention.
1063 static void X86_64_CCC_AssignArgument(unsigned ValNo,
1063 /// X86_64_CCC_AssignArgument - Implement the X86-64 C Calling Convention. This
1064 /// returns true if the value was not handled by this calling convention.
1065 static bool X86_64_CCC_AssignArgument(unsigned ValNo,
10641066 MVT::ValueType ArgVT, unsigned ArgFlags,
1065 CCState &State,
1066 SmallVector &Locs) {
1067 CCState &State) {
10671068 MVT::ValueType LocVT = ArgVT;
10681069 CCValAssign::LocInfo LocInfo = CCValAssign::Full;
10691070
10811082 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
10821083 };
10831084 if (unsigned Reg = State.AllocateReg(GPR32ArgRegs, 6)) {
1084 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1085 return;
1085 State.addLoc(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1086 return false;
10861087 }
10871088 }
10881089
10931094 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
10941095 };
10951096 if (unsigned Reg = State.AllocateReg(GPR64ArgRegs, 6)) {
1096 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1097 return;
1097 State.addLoc(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1098 return false;
10981099 }
10991100 }
11001101
11061107 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
11071108 };
11081109 if (unsigned Reg = State.AllocateReg(XMMArgRegs, 8)) {
1109 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1110 return;
1110 State.addLoc(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo));
1111 return false;
11111112 }
11121113 }
11131114
11161117 if (LocVT == MVT::i32 || LocVT == MVT::i64 ||
11171118 LocVT == MVT::f32 || LocVT == MVT::f64) {
11181119 unsigned Offset = State.AllocateStack(8, 8);
1119 Locs.push_back(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo));
1120 return;
1120 State.addLoc(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo));
1121 return false;
11211122 }
11221123
11231124 // Vectors get 16-byte stack slots that are 16-byte aligned.
11241125 if (MVT::isVector(LocVT)) {
11251126 unsigned Offset = State.AllocateStack(16, 16);
1126 Locs.push_back(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo));
1127 return;
1128 }
1129 assert(0 && "Unknown argument type!");
1127 State.addLoc(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo));
1128 return false;
1129 }
1130 return true;
11301131 }
11311132
11321133
11461147 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
11471148 };
11481149
1149 SmallVector ArgValues;
1150
1151
1152 CCState CCInfo(*getTargetMachine().getRegisterInfo());
11531150 SmallVector ArgLocs;
1151 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1152 ArgLocs);
11541153
11551154 for (unsigned i = 0; i != NumArgs; ++i) {
11561155 MVT::ValueType ArgVT = Op.getValue(i).getValueType();
11571156 unsigned ArgFlags = cast(Op.getOperand(3+i))->getValue();
1158 X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo, ArgLocs);
1157 if (X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo))
1158 assert(0 && "Unhandled argument type!");
11591159 }
11601160
1161 SmallVector ArgValues;
11611162 unsigned LastVal = ~0U;
11621163 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
11631164 CCValAssign &VA = ArgLocs[i];
12791280 SDOperand Callee = Op.getOperand(4);
12801281 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
12811282
1282 CCState CCInfo(*getTargetMachine().getRegisterInfo());
12831283 SmallVector ArgLocs;
1284 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
12841285
12851286 for (unsigned i = 0; i != NumOps; ++i) {
12861287 MVT::ValueType ArgVT = Op.getOperand(5+2*i).getValueType();
12871288 unsigned ArgFlags =cast(Op.getOperand(5+2*i+1))->getValue();
1288 X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo, ArgLocs);
1289 if (X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo))
1290 assert(0 && "Unhandled argument type!");
12891291 }
12901292
12911293 // Get a count of how many bytes are to be pushed on the stack.