| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H |
| 15 |
#define LLVM_CODEGEN_CALLINGCONVLOWER_H |
| 16 |
|
| 17 |
#include "llvm/ADT/SmallVector.h" |
| 18 |
#include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 |
#include "llvm/CodeGen/Register.h" |
| 20 |
#include "llvm/CodeGen/TargetCallingConv.h" |
| 21 |
#include "llvm/IR/CallingConv.h" |
| 22 |
#include "llvm/MC/MCRegisterInfo.h" |
| 23 |
#include "llvm/Support/Alignment.h" |
| 24 |
|
| 25 |
namespace llvm { |
| 26 |
|
| 27 |
class CCState; |
| 28 |
class MachineFunction; |
| 29 |
class MVT; |
| 30 |
class TargetRegisterInfo; |
| 31 |
|
| 32 |
|
| 33 |
class CCValAssign { |
| 34 |
public: |
| 35 |
enum LocInfo { |
| 36 |
Full, |
| 37 |
SExt, |
| 38 |
ZExt, |
| 39 |
AExt, |
| 40 |
SExtUpper, |
| 41 |
|
| 42 |
ZExtUpper, |
| 43 |
|
| 44 |
AExtUpper, |
| 45 |
|
| 46 |
BCvt, |
| 47 |
Trunc, |
| 48 |
VExt, |
| 49 |
|
| 50 |
|
| 51 |
FPExt, |
| 52 |
Indirect |
| 53 |
|
| 54 |
}; |
| 55 |
|
| 56 |
private: |
| 57 |
|
| 58 |
unsigned ValNo; |
| 59 |
|
| 60 |
|
| 61 |
unsigned Loc; |
| 62 |
|
| 63 |
|
| 64 |
unsigned isMem : 1; |
| 65 |
|
| 66 |
|
| 67 |
unsigned isCustom : 1; |
| 68 |
|
| 69 |
|
| 70 |
LocInfo HTP : 6; |
| 71 |
|
| 72 |
|
| 73 |
MVT ValVT; |
| 74 |
|
| 75 |
|
| 76 |
MVT LocVT; |
| 77 |
public: |
| 78 |
|
| 79 |
static CCValAssign getReg(unsigned ValNo, MVT ValVT, |
| 80 |
unsigned RegNo, MVT LocVT, |
| 81 |
LocInfo HTP) { |
| 82 |
CCValAssign Ret; |
| 83 |
Ret.ValNo = ValNo; |
| 84 |
Ret.Loc = RegNo; |
| 85 |
Ret.isMem = false; |
| 86 |
Ret.isCustom = false; |
| 87 |
Ret.HTP = HTP; |
| 88 |
Ret.ValVT = ValVT; |
| 89 |
Ret.LocVT = LocVT; |
| 90 |
return Ret; |
| 91 |
} |
| 92 |
|
| 93 |
static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT, |
| 94 |
unsigned RegNo, MVT LocVT, |
| 95 |
LocInfo HTP) { |
| 96 |
CCValAssign Ret; |
| 97 |
Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP); |
| 98 |
Ret.isCustom = true; |
| 99 |
return Ret; |
| 100 |
} |
| 101 |
|
| 102 |
static CCValAssign getMem(unsigned ValNo, MVT ValVT, |
| 103 |
unsigned Offset, MVT LocVT, |
| 104 |
LocInfo HTP) { |
| 105 |
CCValAssign Ret; |
| 106 |
Ret.ValNo = ValNo; |
| 107 |
Ret.Loc = Offset; |
| 108 |
Ret.isMem = true; |
| 109 |
Ret.isCustom = false; |
| 110 |
Ret.HTP = HTP; |
| 111 |
Ret.ValVT = ValVT; |
| 112 |
Ret.LocVT = LocVT; |
| 113 |
return Ret; |
| 114 |
} |
| 115 |
|
| 116 |
static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT, |
| 117 |
unsigned Offset, MVT LocVT, |
| 118 |
LocInfo HTP) { |
| 119 |
CCValAssign Ret; |
| 120 |
Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP); |
| 121 |
Ret.isCustom = true; |
| 122 |
return Ret; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
static CCValAssign getPending(unsigned ValNo, MVT ValVT, MVT LocVT, |
| 128 |
LocInfo HTP, unsigned ExtraInfo = 0) { |
| 129 |
return getReg(ValNo, ValVT, ExtraInfo, LocVT, HTP); |
| 130 |
} |
| 131 |
|
| 132 |
void convertToReg(unsigned RegNo) { |
| 133 |
Loc = RegNo; |
| 134 |
isMem = false; |
| 135 |
} |
| 136 |
|
| 137 |
void convertToMem(unsigned Offset) { |
| 138 |
Loc = Offset; |
| 139 |
isMem = true; |
| 140 |
} |
| 141 |
|
| 142 |
unsigned getValNo() const { return ValNo; } |
| 143 |
MVT getValVT() const { return ValVT; } |
| 144 |
|
| 145 |
bool isRegLoc() const { return !isMem; } |
| 146 |
bool isMemLoc() const { return isMem; } |
| 147 |
|
| 148 |
bool needsCustom() const { return isCustom; } |
| 149 |
|
| 150 |
Register getLocReg() const { assert(isRegLoc()); return Loc; } |
| 151 |
unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; } |
| 152 |
unsigned getExtraInfo() const { return Loc; } |
| 153 |
MVT getLocVT() const { return LocVT; } |
| 154 |
|
| 155 |
LocInfo getLocInfo() const { return HTP; } |
| 156 |
bool isExtInLoc() const { |
| 157 |
return (HTP == AExt || HTP == SExt || HTP == ZExt); |
| 158 |
} |
| 159 |
|
| 160 |
bool isUpperBitsInLoc() const { |
| 161 |
return HTP == AExtUpper || HTP == SExtUpper || HTP == ZExtUpper; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
struct ForwardedRegister { |
| 168 |
ForwardedRegister(Register VReg, MCPhysReg PReg, MVT VT) |
| 169 |
: VReg(VReg), PReg(PReg), VT(VT) {} |
| 170 |
Register VReg; |
| 171 |
MCPhysReg PReg; |
| 172 |
MVT VT; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
typedef bool CCAssignFn(unsigned ValNo, MVT ValVT, |
| 178 |
MVT LocVT, CCValAssign::LocInfo LocInfo, |
| 179 |
ISD::ArgFlagsTy ArgFlags, CCState &State); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT, |
| 185 |
MVT &LocVT, CCValAssign::LocInfo &LocInfo, |
| 186 |
ISD::ArgFlagsTy &ArgFlags, CCState &State); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
class CCState { |
| 192 |
private: |
| 193 |
CallingConv::ID CallingConv; |
| 194 |
bool IsVarArg; |
| 195 |
bool AnalyzingMustTailForwardedRegs = false; |
| 196 |
MachineFunction &MF; |
| 197 |
const TargetRegisterInfo &TRI; |
| 198 |
SmallVectorImpl<CCValAssign> &Locs; |
| 199 |
LLVMContext &Context; |
| 200 |
|
| 201 |
unsigned StackOffset; |
| 202 |
Align MaxStackArgAlign; |
| 203 |
SmallVector<uint32_t, 16> UsedRegs; |
| 204 |
SmallVector<CCValAssign, 4> PendingLocs; |
| 205 |
SmallVector<ISD::ArgFlagsTy, 4> PendingArgFlags; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
struct ByValInfo { |
| 235 |
ByValInfo(unsigned B, unsigned E) : Begin(B), End(E) {} |
| 236 |
|
| 237 |
|
| 238 |
unsigned Begin; |
| 239 |
|
| 240 |
|
| 241 |
unsigned End; |
| 242 |
}; |
| 243 |
SmallVector<ByValInfo, 4 > ByValRegs; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
unsigned InRegsParamsProcessed; |
| 248 |
|
| 249 |
public: |
| 250 |
CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, |
| 251 |
SmallVectorImpl<CCValAssign> &locs, LLVMContext &C); |
| 252 |
|
| 253 |
void addLoc(const CCValAssign &V) { |
| 254 |
Locs.push_back(V); |
| 255 |
} |
| 256 |
|
| 257 |
LLVMContext &getContext() const { return Context; } |
| 258 |
MachineFunction &getMachineFunction() const { return MF; } |
| 259 |
CallingConv::ID getCallingConv() const { return CallingConv; } |
| 260 |
bool isVarArg() const { return IsVarArg; } |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
unsigned getNextStackOffset() const { |
| 265 |
return StackOffset; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
unsigned getAlignedCallFrameSize() const { |
| 272 |
return alignTo(StackOffset, MaxStackArgAlign); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
bool isAllocated(MCRegister Reg) const { |
| 278 |
return UsedRegs[Reg / 32] & (1 << (Reg & 31)); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins, |
| 284 |
CCAssignFn Fn); |
| 285 |
|
| 286 |
|
| 287 |
void AnalyzeArguments(const SmallVectorImpl<ISD::InputArg> &Ins, |
| 288 |
CCAssignFn Fn) { |
| 289 |
AnalyzeFormalArguments(Ins, Fn); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 295 |
CCAssignFn Fn); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 301 |
CCAssignFn Fn); |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 306 |
CCAssignFn Fn); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs, |
| 311 |
SmallVectorImpl<ISD::ArgFlagsTy> &Flags, |
| 312 |
CCAssignFn Fn); |
| 313 |
|
| 314 |
|
| 315 |
void AnalyzeArguments(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 316 |
CCAssignFn Fn) { |
| 317 |
AnalyzeCallOperands(Outs, Fn); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, |
| 323 |
CCAssignFn Fn); |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
bool IsShadowAllocatedReg(MCRegister Reg) const; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
void AnalyzeCallResult(MVT VT, CCAssignFn Fn); |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
unsigned getFirstUnallocated(ArrayRef<MCPhysReg> Regs) const { |
| 337 |
for (unsigned i = 0; i < Regs.size(); ++i) |
| 338 |
if (!isAllocated(Regs[i])) |
| 339 |
return i; |
| 340 |
return Regs.size(); |
| 341 |
} |
| 342 |
|
| 343 |
void DeallocateReg(MCPhysReg Reg) { |
| 344 |
assert(isAllocated(Reg) && "Trying to deallocate an unallocated register"); |
| 345 |
MarkUnallocated(Reg); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
MCRegister AllocateReg(MCPhysReg Reg) { |
| 352 |
if (isAllocated(Reg)) |
| 353 |
return MCRegister(); |
| 354 |
MarkAllocated(Reg); |
| 355 |
return Reg; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
MCRegister AllocateReg(MCPhysReg Reg, MCPhysReg ShadowReg) { |
| 360 |
if (isAllocated(Reg)) |
| 361 |
return MCRegister(); |
| 362 |
MarkAllocated(Reg); |
| 363 |
MarkAllocated(ShadowReg); |
| 364 |
return Reg; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
MCPhysReg AllocateReg(ArrayRef<MCPhysReg> Regs) { |
| 371 |
unsigned FirstUnalloc = getFirstUnallocated(Regs); |
| 372 |
if (FirstUnalloc == Regs.size()) |
| 373 |
return MCRegister(); |
| 374 |
|
| 375 |
|
| 376 |
MCPhysReg Reg = Regs[FirstUnalloc]; |
| 377 |
MarkAllocated(Reg); |
| 378 |
return Reg; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
MCPhysReg AllocateRegBlock(ArrayRef<MCPhysReg> Regs, unsigned RegsRequired) { |
| 385 |
if (RegsRequired > Regs.size()) |
| 386 |
return 0; |
| 387 |
|
| 388 |
for (unsigned StartIdx = 0; StartIdx <= Regs.size() - RegsRequired; |
| 389 |
++StartIdx) { |
| 390 |
bool BlockAvailable = true; |
| 391 |
|
| 392 |
for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) { |
| 393 |
if (isAllocated(Regs[StartIdx + BlockIdx])) { |
| 394 |
BlockAvailable = false; |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
if (BlockAvailable) { |
| 399 |
|
| 400 |
for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) { |
| 401 |
MarkAllocated(Regs[StartIdx + BlockIdx]); |
| 402 |
} |
| 403 |
return Regs[StartIdx]; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
MCRegister AllocateReg(ArrayRef<MCPhysReg> Regs, const MCPhysReg *ShadowRegs) { |
| 412 |
unsigned FirstUnalloc = getFirstUnallocated(Regs); |
| 413 |
if (FirstUnalloc == Regs.size()) |
| 414 |
return MCRegister(); |
| 415 |
|
| 416 |
|
| 417 |
MCRegister Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc]; |
| 418 |
MarkAllocated(Reg); |
| 419 |
MarkAllocated(ShadowReg); |
| 420 |
return Reg; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
unsigned AllocateStack(unsigned Size, Align Alignment) { |
| 426 |
StackOffset = alignTo(StackOffset, Alignment); |
| 427 |
unsigned Result = StackOffset; |
| 428 |
StackOffset += Size; |
| 429 |
MaxStackArgAlign = std::max(Alignment, MaxStackArgAlign); |
| 430 |
ensureMaxAlignment(Alignment); |
| 431 |
return Result; |
| 432 |
} |
| 433 |
|
| 434 |
void ensureMaxAlignment(Align Alignment); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
unsigned AllocateStack(unsigned Size, Align Alignment, |
| 439 |
ArrayRef<MCPhysReg> ShadowRegs) { |
| 440 |
for (unsigned i = 0; i < ShadowRegs.size(); ++i) |
| 441 |
MarkAllocated(ShadowRegs[i]); |
| 442 |
return AllocateStack(Size, Alignment); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
void HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT, |
| 449 |
CCValAssign::LocInfo LocInfo, int MinSize, Align MinAlign, |
| 450 |
ISD::ArgFlagsTy ArgFlags); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
unsigned getInRegsParamsCount() const { return ByValRegs.size(); } |
| 455 |
|
| 456 |
|
| 457 |
unsigned getInRegsParamsProcessed() const { return InRegsParamsProcessed; } |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
void getInRegsParamInfo(unsigned InRegsParamRecordIndex, |
| 462 |
unsigned& BeginReg, unsigned& EndReg) const { |
| 463 |
assert(InRegsParamRecordIndex < ByValRegs.size() && |
| 464 |
"Wrong ByVal parameter index"); |
| 465 |
|
| 466 |
const ByValInfo& info = ByValRegs[InRegsParamRecordIndex]; |
| 467 |
BeginReg = info.Begin; |
| 468 |
EndReg = info.End; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) { |
| 473 |
ByValRegs.push_back(ByValInfo(RegBegin, RegEnd)); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
bool nextInRegsParam() { |
| 480 |
unsigned e = ByValRegs.size(); |
| 481 |
if (InRegsParamsProcessed < e) |
| 482 |
++InRegsParamsProcessed; |
| 483 |
return InRegsParamsProcessed < e; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
void clearByValRegsInfo() { |
| 488 |
InRegsParamsProcessed = 0; |
| 489 |
ByValRegs.clear(); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
void rewindByValRegsInfo() { |
| 494 |
InRegsParamsProcessed = 0; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
SmallVectorImpl<CCValAssign> &getPendingLocs() { |
| 499 |
return PendingLocs; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
SmallVectorImpl<ISD::ArgFlagsTy> &getPendingArgFlags() { |
| 504 |
return PendingArgFlags; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
void getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs, MVT VT, |
| 512 |
CCAssignFn Fn); |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
void analyzeMustTailForwardedRegisters( |
| 517 |
SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes, |
| 518 |
CCAssignFn Fn); |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static bool resultsCompatible(CallingConv::ID CalleeCC, |
| 523 |
CallingConv::ID CallerCC, MachineFunction &MF, |
| 524 |
LLVMContext &C, |
| 525 |
const SmallVectorImpl<ISD::InputArg> &Ins, |
| 526 |
CCAssignFn CalleeFn, CCAssignFn CallerFn); |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
template <class T> |
| 532 |
void AnalyzeArgumentsSecondPass(const SmallVectorImpl<T> &Args, |
| 533 |
CCAssignFn Fn) { |
| 534 |
unsigned NumFirstPassLocs = Locs.size(); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
SmallVector<T, 16> SecPassArg; |
| 539 |
|
| 540 |
for (auto Arg : Args) { |
| 541 |
Arg.Flags.setSecArgPass(); |
| 542 |
SecPassArg.push_back(Arg); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
AnalyzeArguments(SecPassArg, Fn); |
| 547 |
|
| 548 |
|
| 549 |
SmallVector<CCValAssign, 16> TmpArgLocs; |
| 550 |
TmpArgLocs.swap(Locs); |
| 551 |
auto B = TmpArgLocs.begin(), E = TmpArgLocs.end(); |
| 552 |
std::merge(B, B + NumFirstPassLocs, B + NumFirstPassLocs, E, |
| 553 |
std::back_inserter(Locs), |
| 554 |
[](const CCValAssign &A, const CCValAssign &B) -> bool { |
| 555 |
return A.getValNo() < B.getValNo(); |
| 556 |
}); |
| 557 |
} |
| 558 |
|
| 559 |
private: |
| 560 |
|
| 561 |
void MarkAllocated(MCPhysReg Reg); |
| 562 |
|
| 563 |
void MarkUnallocated(MCPhysReg Reg); |
| 564 |
}; |
| 565 |
|
| 566 |
} |
| 567 |
|
| 568 |
#endif |