Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceTargetLoweringX8632.cpp - x86-32 lowering -----------===// | 1 //===- subzero/src/IceTargetLoweringX8632.cpp - x86-32 lowering -----------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the TargetLoweringX8632 class, which | 10 // This file implements the TargetLoweringX8632 class, which |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 if (Func->hasError()) | 240 if (Func->hasError()) |
| 241 return; | 241 return; |
| 242 T_deletePhis.printElapsedUs(Context, "deletePhis()"); | 242 T_deletePhis.printElapsedUs(Context, "deletePhis()"); |
| 243 Func->dump("After Phi lowering"); | 243 Func->dump("After Phi lowering"); |
| 244 | 244 |
| 245 // Address mode optimization. | 245 // Address mode optimization. |
| 246 Timer T_doAddressOpt; | 246 Timer T_doAddressOpt; |
| 247 Func->doAddressOpt(); | 247 Func->doAddressOpt(); |
| 248 T_doAddressOpt.printElapsedUs(Context, "doAddressOpt()"); | 248 T_doAddressOpt.printElapsedUs(Context, "doAddressOpt()"); |
| 249 | 249 |
| 250 // Argument lowering | |
| 251 Timer T_argLowering; | |
| 252 Func->doArgLowering(); | |
| 253 T_argLowering.printElapsedUs(Context, "lowerArguments()"); | |
| 254 | |
| 250 // Target lowering. This requires liveness analysis for some parts | 255 // Target lowering. This requires liveness analysis for some parts |
| 251 // of the lowering decisions, such as compare/branch fusing. If | 256 // of the lowering decisions, such as compare/branch fusing. If |
| 252 // non-lightweight liveness analysis is used, the instructions need | 257 // non-lightweight liveness analysis is used, the instructions need |
| 253 // to be renumbered first. TODO: This renumbering should only be | 258 // to be renumbered first. TODO: This renumbering should only be |
| 254 // necessary if we're actually calculating live intervals, which we | 259 // necessary if we're actually calculating live intervals, which we |
| 255 // only do for register allocation. | 260 // only do for register allocation. |
| 256 Timer T_renumber1; | 261 Timer T_renumber1; |
| 257 Func->renumberInstructions(); | 262 Func->renumberInstructions(); |
| 258 if (Func->hasError()) | 263 if (Func->hasError()) |
| 259 return; | 264 return; |
| 260 T_renumber1.printElapsedUs(Context, "renumberInstructions()"); | 265 T_renumber1.printElapsedUs(Context, "renumberInstructions()"); |
| 266 | |
| 261 // TODO: It should be sufficient to use the fastest liveness | 267 // TODO: It should be sufficient to use the fastest liveness |
| 262 // calculation, i.e. livenessLightweight(). However, for some | 268 // calculation, i.e. livenessLightweight(). However, for some |
| 263 // reason that slows down the rest of the translation. Investigate. | 269 // reason that slows down the rest of the translation. Investigate. |
| 264 Timer T_liveness1; | 270 Timer T_liveness1; |
| 265 Func->liveness(Liveness_Basic); | 271 Func->liveness(Liveness_Basic); |
| 266 if (Func->hasError()) | 272 if (Func->hasError()) |
| 267 return; | 273 return; |
| 268 T_liveness1.printElapsedUs(Context, "liveness()"); | 274 T_liveness1.printElapsedUs(Context, "liveness()"); |
| 269 Func->dump("After x86 address mode opt"); | 275 Func->dump("After x86 address mode opt"); |
| 276 | |
| 270 Timer T_genCode; | 277 Timer T_genCode; |
| 271 Func->genCode(); | 278 Func->genCode(); |
| 272 if (Func->hasError()) | 279 if (Func->hasError()) |
| 273 return; | 280 return; |
| 274 T_genCode.printElapsedUs(Context, "genCode()"); | 281 T_genCode.printElapsedUs(Context, "genCode()"); |
| 275 | 282 |
| 276 // Register allocation. This requires instruction renumbering and | 283 // Register allocation. This requires instruction renumbering and |
| 277 // full liveness analysis. | 284 // full liveness analysis. |
| 278 Timer T_renumber2; | 285 Timer T_renumber2; |
| 279 Func->renumberInstructions(); | 286 Func->renumberInstructions(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 if (Func->hasError()) | 329 if (Func->hasError()) |
| 323 return; | 330 return; |
| 324 T_placePhiStores.printElapsedUs(Context, "placePhiStores()"); | 331 T_placePhiStores.printElapsedUs(Context, "placePhiStores()"); |
| 325 Timer T_deletePhis; | 332 Timer T_deletePhis; |
| 326 Func->deletePhis(); | 333 Func->deletePhis(); |
| 327 if (Func->hasError()) | 334 if (Func->hasError()) |
| 328 return; | 335 return; |
| 329 T_deletePhis.printElapsedUs(Context, "deletePhis()"); | 336 T_deletePhis.printElapsedUs(Context, "deletePhis()"); |
| 330 Func->dump("After Phi lowering"); | 337 Func->dump("After Phi lowering"); |
| 331 | 338 |
| 339 Timer T_argLowering; | |
| 340 Func->doArgLowering(); | |
| 341 T_argLowering.printElapsedUs(Context, "lowerArguments()"); | |
| 342 | |
| 332 Timer T_genCode; | 343 Timer T_genCode; |
| 333 Func->genCode(); | 344 Func->genCode(); |
| 334 if (Func->hasError()) | 345 if (Func->hasError()) |
| 335 return; | 346 return; |
| 336 T_genCode.printElapsedUs(Context, "genCode()"); | 347 T_genCode.printElapsedUs(Context, "genCode()"); |
| 337 Func->dump("After initial x8632 codegen"); | 348 Func->dump("After initial x8632 codegen"); |
| 338 | 349 |
| 339 Timer T_genFrame; | 350 Timer T_genFrame; |
| 340 Func->genFrame(); | 351 Func->genFrame(); |
| 341 if (Func->hasError()) | 352 if (Func->hasError()) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 if (!hasFramePointer()) | 416 if (!hasFramePointer()) |
| 406 Offset += getStackAdjustment(); | 417 Offset += getStackAdjustment(); |
| 407 if (Offset) { | 418 if (Offset) { |
| 408 if (Offset > 0) | 419 if (Offset > 0) |
| 409 Str << "+"; | 420 Str << "+"; |
| 410 Str << Offset; | 421 Str << Offset; |
| 411 } | 422 } |
| 412 Str << "]"; | 423 Str << "]"; |
| 413 } | 424 } |
| 414 | 425 |
| 415 // Helper function for addProlog(). Sets the frame offset for Arg, | 426 void TargetX8632::lowerArguments() { |
| 416 // updates InArgsSizeBytes according to Arg's width, and generates an | 427 VarList &Args = Func->getArgs(); |
| 417 // instruction to copy Arg into its assigned register if applicable. | 428 // The first four arguments of vector type, regardless of their |
| 418 // For an I64 arg that has been split into Lo and Hi components, it | 429 // position relative to the other arguments in the argument list, are |
| 419 // calls itself recursively on the components, taking care to handle | 430 // passed in registers xmm0 - xmm3. |
| 420 // Lo first because of the little-endian architecture. | 431 unsigned NumXmmArgs = 0; |
| 421 void TargetX8632::setArgOffsetAndCopy(Variable *Arg, Variable *FramePtr, | 432 |
| 422 size_t BasicFrameOffset, | 433 Context.init(Func->getEntryNode()); |
| 423 size_t &InArgsSizeBytes) { | 434 Context.setInsertPoint(Context.getCur()); |
| 435 | |
| 436 for (SizeT I = 0, E = Args.size(); I < E && NumXmmArgs < 4; ++I) { | |
|
jvoung (off chromium)
2014/07/09 04:26:53
Might be worth making the "4" a symbolic constant
Jim Stichnoth
2014/07/09 16:56:13
I agree, except that between x86-32 and x86-64 low
wala
2014/07/09 19:05:12
Done.
| |
| 437 Variable *Arg = Args[I]; | |
| 438 Type Ty = Arg->getType(); | |
| 439 if (!isVectorType(Ty)) | |
| 440 continue; | |
| 441 // Replace Arg in the argument list with the home register. Then | |
| 442 // generate an instruction in the prolog to copy the home register | |
| 443 // to the assigned location of Arg. | |
| 444 int32_t RegNum = Reg_xmm0 + NumXmmArgs; | |
| 445 ++NumXmmArgs; | |
| 446 IceString Name = "home_reg:" + Arg->getName(); | |
| 447 const CfgNode *DefNode = NULL; | |
| 448 Variable *RegisterArg = Func->makeVariable(Ty, DefNode, Name); | |
| 449 RegisterArg->setRegNum(RegNum); | |
| 450 RegisterArg->setIsArg(Func); | |
| 451 Arg->setIsArg(Func, false); | |
| 452 | |
| 453 Args[I] = RegisterArg; | |
| 454 Context.insert(InstAssign::create(Func, Arg, RegisterArg)); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 // Helper function for addProlog(). | |
| 459 // | |
| 460 // This assumes Arg is an argument passed on the stack. This sets the | |
| 461 // frame offset for Arg and updates InArgsSizeBytes according to Arg's | |
| 462 // width. For an I64 arg that has been split into Lo and Hi components, | |
| 463 // it calls itself recursively on the components, taking care to handle | |
| 464 // Lo first because of the little-endian architecture. Lastly, this | |
| 465 // function generates an instruction to copy Arg into its assigned | |
| 466 // register if applicable. | |
| 467 void TargetX8632::finishArgumentLowering(Variable *Arg, Variable *FramePtr, | |
| 468 size_t BasicFrameOffset, | |
| 469 size_t &InArgsSizeBytes) { | |
| 424 Variable *Lo = Arg->getLo(); | 470 Variable *Lo = Arg->getLo(); |
| 425 Variable *Hi = Arg->getHi(); | 471 Variable *Hi = Arg->getHi(); |
| 426 Type Ty = Arg->getType(); | 472 Type Ty = Arg->getType(); |
| 427 if (Lo && Hi && Ty == IceType_i64) { | 473 if (Lo && Hi && Ty == IceType_i64) { |
| 428 assert(Lo->getType() != IceType_i64); // don't want infinite recursion | 474 assert(Lo->getType() != IceType_i64); // don't want infinite recursion |
| 429 assert(Hi->getType() != IceType_i64); // don't want infinite recursion | 475 assert(Hi->getType() != IceType_i64); // don't want infinite recursion |
| 430 setArgOffsetAndCopy(Lo, FramePtr, BasicFrameOffset, InArgsSizeBytes); | 476 finishArgumentLowering(Lo, FramePtr, BasicFrameOffset, InArgsSizeBytes); |
| 431 setArgOffsetAndCopy(Hi, FramePtr, BasicFrameOffset, InArgsSizeBytes); | 477 finishArgumentLowering(Hi, FramePtr, BasicFrameOffset, InArgsSizeBytes); |
| 432 return; | 478 return; |
| 433 } | 479 } |
| 434 Arg->setStackOffset(BasicFrameOffset + InArgsSizeBytes); | 480 Arg->setStackOffset(BasicFrameOffset + InArgsSizeBytes); |
| 481 InArgsSizeBytes += typeWidthInBytesOnStack(Ty); | |
| 435 if (Arg->hasReg()) { | 482 if (Arg->hasReg()) { |
| 436 assert(Ty != IceType_i64); | 483 assert(Ty != IceType_i64); |
| 437 OperandX8632Mem *Mem = OperandX8632Mem::create( | 484 OperandX8632Mem *Mem = OperandX8632Mem::create( |
| 438 Func, Ty, FramePtr, | 485 Func, Ty, FramePtr, |
| 439 Ctx->getConstantInt(IceType_i32, Arg->getStackOffset())); | 486 Ctx->getConstantInt(IceType_i32, Arg->getStackOffset())); |
| 440 _mov(Arg, Mem); | 487 if (isVectorType(Arg->getType())) { |
| 488 _movp(Arg, Mem); | |
| 489 } else { | |
| 490 _mov(Arg, Mem); | |
| 491 } | |
| 441 } | 492 } |
| 442 InArgsSizeBytes += typeWidthInBytesOnStack(Ty); | |
| 443 } | 493 } |
| 444 | 494 |
| 445 Type TargetX8632::stackSlotType() { return IceType_i32; } | 495 Type TargetX8632::stackSlotType() { return IceType_i32; } |
| 446 | 496 |
| 447 void TargetX8632::addProlog(CfgNode *Node) { | 497 void TargetX8632::addProlog(CfgNode *Node) { |
| 448 // If SimpleCoalescing is false, each variable without a register | 498 // If SimpleCoalescing is false, each variable without a register |
| 449 // gets its own unique stack slot, which leads to large stack | 499 // gets its own unique stack slot, which leads to large stack |
| 450 // frames. If SimpleCoalescing is true, then each "global" variable | 500 // frames. If SimpleCoalescing is true, then each "global" variable |
| 451 // without a register gets its own slot, but "local" variable slots | 501 // without a register gets its own slot, but "local" variable slots |
| 452 // are reused across basic blocks. E.g., if A and B are local to | 502 // are reused across basic blocks. E.g., if A and B are local to |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 482 RegsUsed = llvm::SmallBitVector(CalleeSaves.size()); | 532 RegsUsed = llvm::SmallBitVector(CalleeSaves.size()); |
| 483 const VarList &Variables = Func->getVariables(); | 533 const VarList &Variables = Func->getVariables(); |
| 484 const VarList &Args = Func->getArgs(); | 534 const VarList &Args = Func->getArgs(); |
| 485 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); | 535 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); |
| 486 I != E; ++I) { | 536 I != E; ++I) { |
| 487 Variable *Var = *I; | 537 Variable *Var = *I; |
| 488 if (Var->hasReg()) { | 538 if (Var->hasReg()) { |
| 489 RegsUsed[Var->getRegNum()] = true; | 539 RegsUsed[Var->getRegNum()] = true; |
| 490 continue; | 540 continue; |
| 491 } | 541 } |
| 492 // An argument passed on the stack already has a stack slot. | 542 // An argument either does not need a stack slot (if passed in a |
| 543 // register) or already has one (if passed on the stack). | |
| 493 if (Var->getIsArg()) | 544 if (Var->getIsArg()) |
| 494 continue; | 545 continue; |
| 495 // An unreferenced variable doesn't need a stack slot. | 546 // An unreferenced variable doesn't need a stack slot. |
| 496 if (ComputedLiveRanges && Var->getLiveRange().isEmpty()) | 547 if (ComputedLiveRanges && Var->getLiveRange().isEmpty()) |
| 497 continue; | 548 continue; |
| 498 // A spill slot linked to a variable with a stack slot should reuse | 549 // A spill slot linked to a variable with a stack slot should reuse |
| 499 // that stack slot. | 550 // that stack slot. |
| 500 if (Var->getWeight() == RegWeight::Zero && Var->getRegisterOverlap()) { | 551 if (Var->getWeight() == RegWeight::Zero && Var->getRegisterOverlap()) { |
| 501 if (Variable *Linked = Var->getPreferredRegister()) { | 552 if (Variable *Linked = Var->getPreferredRegister()) { |
| 502 if (!Linked->hasReg()) | 553 if (!Linked->hasReg()) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 540 _mov(ebp, esp); | 591 _mov(ebp, esp); |
| 541 } | 592 } |
| 542 | 593 |
| 543 // Generate "sub esp, LocalsSizeBytes" | 594 // Generate "sub esp, LocalsSizeBytes" |
| 544 if (LocalsSizeBytes) | 595 if (LocalsSizeBytes) |
| 545 _sub(getPhysicalRegister(Reg_esp), | 596 _sub(getPhysicalRegister(Reg_esp), |
| 546 Ctx->getConstantInt(IceType_i32, LocalsSizeBytes)); | 597 Ctx->getConstantInt(IceType_i32, LocalsSizeBytes)); |
| 547 | 598 |
| 548 resetStackAdjustment(); | 599 resetStackAdjustment(); |
| 549 | 600 |
| 550 // Fill in stack offsets for args, and copy args into registers for | 601 // Fill in stack offsets for stack args, and copy args into registers |
| 551 // those that were register-allocated. Args are pushed right to | 602 // for those that were register-allocated. Args are pushed right to |
| 552 // left, so Arg[0] is closest to the stack/frame pointer. | 603 // left, so Arg[0] is closest to the stack/frame pointer. |
| 553 // | |
| 554 // TODO: Make this right for different width args, calling | |
| 555 // conventions, etc. For one thing, args passed in registers will | |
| 556 // need to be copied/shuffled to their home registers (the | |
| 557 // RegManager code may have some permutation logic to leverage), | |
| 558 // and if they have no home register, home space will need to be | |
| 559 // allocated on the stack to copy into. | |
| 560 Variable *FramePtr = getPhysicalRegister(getFrameOrStackReg()); | 604 Variable *FramePtr = getPhysicalRegister(getFrameOrStackReg()); |
| 561 size_t BasicFrameOffset = PreservedRegsSizeBytes + RetIpSizeBytes; | 605 size_t BasicFrameOffset = PreservedRegsSizeBytes + RetIpSizeBytes; |
| 562 if (!IsEbpBasedFrame) | 606 if (!IsEbpBasedFrame) |
| 563 BasicFrameOffset += LocalsSizeBytes; | 607 BasicFrameOffset += LocalsSizeBytes; |
| 608 | |
| 609 unsigned NumXmmArgs = 0; | |
| 564 for (SizeT i = 0; i < Args.size(); ++i) { | 610 for (SizeT i = 0; i < Args.size(); ++i) { |
| 565 Variable *Arg = Args[i]; | 611 Variable *Arg = Args[i]; |
| 566 setArgOffsetAndCopy(Arg, FramePtr, BasicFrameOffset, InArgsSizeBytes); | 612 // Skip arguments passed in registers. |
| 613 if (isVectorType(Arg->getType()) && NumXmmArgs < 4) { | |
| 614 ++NumXmmArgs; | |
| 615 continue; | |
| 616 } | |
| 617 finishArgumentLowering(Arg, FramePtr, BasicFrameOffset, InArgsSizeBytes); | |
| 567 } | 618 } |
| 568 | 619 |
| 569 // Fill in stack offsets for locals. | 620 // Fill in stack offsets for locals. |
| 570 size_t TotalGlobalsSize = GlobalsSize; | 621 size_t TotalGlobalsSize = GlobalsSize; |
| 571 GlobalsSize = 0; | 622 GlobalsSize = 0; |
| 572 LocalsSize.assign(LocalsSize.size(), 0); | 623 LocalsSize.assign(LocalsSize.size(), 0); |
| 573 size_t NextStackOffset = 0; | 624 size_t NextStackOffset = 0; |
| 574 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); | 625 for (VarList::const_iterator I = Variables.begin(), E = Variables.end(); |
| 575 I != E; ++I) { | 626 I != E; ++I) { |
| 576 Variable *Var = *I; | 627 Variable *Var = *I; |
| (...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1243 Operand *Src0Lo = loOperand(Src0); | 1294 Operand *Src0Lo = loOperand(Src0); |
| 1244 Operand *Src0Hi = hiOperand(Src0); | 1295 Operand *Src0Hi = hiOperand(Src0); |
| 1245 Variable *DestLo = llvm::cast<Variable>(loOperand(Dest)); | 1296 Variable *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 1246 Variable *DestHi = llvm::cast<Variable>(hiOperand(Dest)); | 1297 Variable *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
| 1247 Variable *T_Lo = NULL, *T_Hi = NULL; | 1298 Variable *T_Lo = NULL, *T_Hi = NULL; |
| 1248 _mov(T_Lo, Src0Lo); | 1299 _mov(T_Lo, Src0Lo); |
| 1249 _mov(DestLo, T_Lo); | 1300 _mov(DestLo, T_Lo); |
| 1250 _mov(T_Hi, Src0Hi); | 1301 _mov(T_Hi, Src0Hi); |
| 1251 _mov(DestHi, T_Hi); | 1302 _mov(DestHi, T_Hi); |
| 1252 } else { | 1303 } else { |
| 1253 const bool AllowOverlap = true; | 1304 const bool AllowOverlap = false; |
| 1254 // RI is either a physical register or an immediate. | 1305 // RI is either a physical register or an immediate. |
| 1255 Operand *RI = legalize(Src0, Legal_Reg | Legal_Imm, AllowOverlap); | 1306 Operand *RI = legalize(Src0, Legal_Reg | Legal_Imm, AllowOverlap); |
| 1256 _mov(Dest, RI); | 1307 if (isVectorType(Dest->getType())) |
| 1308 _movp(Dest, RI); | |
| 1309 else | |
| 1310 _mov(Dest, RI); | |
| 1257 } | 1311 } |
| 1258 } | 1312 } |
| 1259 | 1313 |
| 1260 void TargetX8632::lowerBr(const InstBr *Inst) { | 1314 void TargetX8632::lowerBr(const InstBr *Inst) { |
| 1261 if (Inst->isUnconditional()) { | 1315 if (Inst->isUnconditional()) { |
| 1262 _br(Inst->getTargetUnconditional()); | 1316 _br(Inst->getTargetUnconditional()); |
| 1263 } else { | 1317 } else { |
| 1264 Operand *Src0 = legalize(Inst->getCondition()); | 1318 Operand *Src0 = legalize(Inst->getCondition()); |
| 1265 Constant *Zero = Ctx->getConstantZero(IceType_i32); | 1319 Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 1266 _cmp(Src0, Zero); | 1320 _cmp(Src0, Zero); |
| 1267 _br(InstX8632Br::Br_ne, Inst->getTargetTrue(), Inst->getTargetFalse()); | 1321 _br(InstX8632Br::Br_ne, Inst->getTargetTrue(), Inst->getTargetFalse()); |
| 1268 } | 1322 } |
| 1269 } | 1323 } |
| 1270 | 1324 |
| 1271 void TargetX8632::lowerCall(const InstCall *Instr) { | 1325 void TargetX8632::lowerCall(const InstCall *Instr) { |
| 1272 // Generate a sequence of push instructions, pushing right to left, | 1326 // For stack arguments, generate a sequence of push instructions, |
| 1273 // keeping track of stack offsets in case a push involves a stack | 1327 // pushing right to left, keeping track of stack offsets in case a |
| 1274 // operand and we are using an esp-based frame. | 1328 // push involves a stack operand and we are using an esp-based frame. |
| 1275 uint32_t StackOffset = 0; | 1329 uint32_t StackOffset = 0; |
| 1330 // Keep track of the number of xmm registers that get used to pass | |
| 1331 // arguments. | |
| 1332 unsigned NumXmmArgs = 0; | |
| 1333 VarList RegisterArgs; | |
| 1276 // TODO: If for some reason the call instruction gets dead-code | 1334 // TODO: If for some reason the call instruction gets dead-code |
| 1277 // eliminated after lowering, we would need to ensure that the | 1335 // eliminated after lowering, we would need to ensure that the |
| 1278 // pre-call push instructions and the post-call esp adjustment get | 1336 // pre-call push instructions and the post-call esp adjustment get |
| 1279 // eliminated as well. | 1337 // eliminated as well. |
| 1280 for (SizeT NumArgs = Instr->getNumArgs(), i = 0; i < NumArgs; ++i) { | 1338 for (SizeT NumArgs = Instr->getNumArgs(), i = 0; i < NumArgs; ++i) { |
| 1281 Operand *Arg = legalize(Instr->getArg(NumArgs - i - 1)); | 1339 Operand *Arg = legalize(Instr->getArg(NumArgs - i - 1)); |
| 1340 bool ArgInRegister = false; | |
| 1282 if (Arg->getType() == IceType_i64) { | 1341 if (Arg->getType() == IceType_i64) { |
| 1283 _push(hiOperand(Arg)); | 1342 _push(hiOperand(Arg)); |
| 1284 _push(loOperand(Arg)); | 1343 _push(loOperand(Arg)); |
| 1285 } else if (Arg->getType() == IceType_f64) { | 1344 } else if (Arg->getType() == IceType_f64) { |
| 1286 // If the Arg turns out to be a memory operand, we need to push | 1345 // If the Arg turns out to be a memory operand, we need to push |
| 1287 // 8 bytes, which requires two push instructions. This ends up | 1346 // 8 bytes, which requires two push instructions. This ends up |
| 1288 // being somewhat clumsy in the current IR, so we use a | 1347 // being somewhat clumsy in the current IR, so we use a |
| 1289 // workaround. Force the operand into a (xmm) register, and | 1348 // workaround. Force the operand into a (xmm) register, and |
| 1290 // then push the register. An xmm register push is actually not | 1349 // then push the register. An xmm register push is actually not |
| 1291 // possible in x86, but the Push instruction emitter handles | 1350 // possible in x86, but the Push instruction emitter handles |
| 1292 // this by decrementing the stack pointer and directly writing | 1351 // this by decrementing the stack pointer and directly writing |
| 1293 // the xmm register value. | 1352 // the xmm register value. |
| 1294 Variable *T = NULL; | 1353 Variable *T = NULL; |
| 1295 _mov(T, Arg); | 1354 _mov(T, Arg); |
| 1296 _push(T); | 1355 _push(T); |
| 1356 } else if (isVectorType(Arg->getType())) { | |
| 1357 if (NumXmmArgs < 4) { | |
| 1358 Variable *Reg = legalizeToVar(Arg, false, Reg_xmm0 + NumXmmArgs); | |
| 1359 ++NumXmmArgs; | |
| 1360 ArgInRegister = true; | |
| 1361 RegisterArgs.push_back(Reg); | |
| 1362 } else { | |
| 1363 // sub esp, 16 | |
| 1364 // movups [esp], legalize_to_reg(Arg) | |
| 1365 Variable *esp = getPhysicalRegister(Reg_esp); | |
| 1366 size_t Width = typeWidthInBytesOnStack(Arg->getType()); | |
| 1367 _sub(esp, Ctx->getConstantInt(IceType_i8, Width)); | |
| 1368 Constant *Zero = Ctx->getConstantZero(IceType_i8); | |
| 1369 OperandX8632Mem *Dest = | |
| 1370 OperandX8632Mem::create(Func, Arg->getType(), esp, Zero); | |
| 1371 _storep(legalize(Arg, Legal_Reg), Dest); | |
| 1372 } | |
| 1297 } else { | 1373 } else { |
| 1298 // Otherwise PNaCl requires parameter types to be at least 32-bits. | 1374 // Otherwise PNaCl requires parameter types to be at least 32-bits. |
| 1299 assert(Arg->getType() == IceType_f32 || Arg->getType() == IceType_i32); | 1375 assert(Arg->getType() == IceType_f32 || Arg->getType() == IceType_i32); |
| 1300 _push(Arg); | 1376 _push(Arg); |
| 1301 } | 1377 } |
| 1302 StackOffset += typeWidthInBytesOnStack(Arg->getType()); | 1378 if (!ArgInRegister) { |
| 1379 StackOffset += typeWidthInBytesOnStack(Arg->getType()); | |
| 1380 } | |
| 1381 } | |
| 1382 // Generate a FakeUse of all register arguments so that they do not | |
| 1383 // get dead code eliminated as a result of the FakeKill of scratch | |
| 1384 // registers after the call. | |
|
Jim Stichnoth
2014/07/09 16:56:13
I think it wouldn't hurt to assert(RegisterArgs.si
wala
2014/07/09 19:05:12
Unnecessary in the revised code.
| |
| 1385 for (VarList::const_iterator I = RegisterArgs.begin(), E = RegisterArgs.end(); | |
| 1386 I != E; ++I) { | |
| 1387 Context.insert(InstFakeUse::create(Func, *I)); | |
| 1303 } | 1388 } |
| 1304 // Generate the call instruction. Assign its result to a temporary | 1389 // Generate the call instruction. Assign its result to a temporary |
| 1305 // with high register allocation weight. | 1390 // with high register allocation weight. |
| 1306 Variable *Dest = Instr->getDest(); | 1391 Variable *Dest = Instr->getDest(); |
| 1307 Variable *eax = NULL; // doubles as RegLo as necessary | 1392 // ReturnReg doubles as ReturnRegLo as necessary. |
| 1308 Variable *edx = NULL; | 1393 Variable *ReturnReg = NULL; |
| 1394 Variable *ReturnRegHi = NULL; | |
| 1309 if (Dest) { | 1395 if (Dest) { |
| 1310 switch (Dest->getType()) { | 1396 switch (Dest->getType()) { |
| 1311 case IceType_NUM: | 1397 case IceType_NUM: |
| 1312 llvm_unreachable("Invalid Call dest type"); | 1398 llvm_unreachable("Invalid Call dest type"); |
| 1313 break; | 1399 break; |
| 1314 case IceType_void: | 1400 case IceType_void: |
| 1315 break; | 1401 break; |
| 1316 case IceType_i1: | 1402 case IceType_i1: |
| 1317 case IceType_i8: | 1403 case IceType_i8: |
| 1318 case IceType_i16: | 1404 case IceType_i16: |
| 1319 case IceType_i32: | 1405 case IceType_i32: |
| 1320 eax = makeReg(Dest->getType(), Reg_eax); | 1406 ReturnReg = makeReg(Dest->getType(), Reg_eax); |
| 1321 break; | 1407 break; |
| 1322 case IceType_i64: | 1408 case IceType_i64: |
| 1323 eax = makeReg(IceType_i32, Reg_eax); | 1409 ReturnReg = makeReg(IceType_i32, Reg_eax); |
| 1324 edx = makeReg(IceType_i32, Reg_edx); | 1410 ReturnRegHi = makeReg(IceType_i32, Reg_edx); |
| 1325 break; | 1411 break; |
| 1326 case IceType_f32: | 1412 case IceType_f32: |
| 1327 case IceType_f64: | 1413 case IceType_f64: |
| 1328 // Leave eax==edx==NULL, and capture the result with the fstp | 1414 // Leave ReturnReg==ReturnRegHi==NULL, and capture the result with |
| 1329 // instruction. | 1415 // the fstp instruction. |
| 1330 break; | 1416 break; |
| 1331 case IceType_v4i1: | 1417 case IceType_v4i1: |
| 1332 case IceType_v8i1: | 1418 case IceType_v8i1: |
| 1333 case IceType_v16i1: | 1419 case IceType_v16i1: |
| 1334 case IceType_v16i8: | 1420 case IceType_v16i8: |
| 1335 case IceType_v8i16: | 1421 case IceType_v8i16: |
| 1336 case IceType_v4i32: | 1422 case IceType_v4i32: |
| 1337 case IceType_v4f32: { | 1423 case IceType_v4f32: |
| 1338 // TODO(wala): Handle return values of vector type in the caller. | 1424 ReturnReg = makeReg(Dest->getType(), Reg_xmm0); |
| 1339 IceString Ty; | 1425 break; |
| 1340 llvm::raw_string_ostream BaseOS(Ty); | |
| 1341 Ostream OS(&BaseOS); | |
| 1342 OS << Dest->getType(); | |
| 1343 Func->setError("Unhandled dest type: " + BaseOS.str()); | |
| 1344 return; | |
| 1345 } | |
| 1346 } | 1426 } |
| 1347 } | 1427 } |
| 1348 // TODO(stichnot): LEAHACK: remove Legal_All (and use default) once | 1428 // TODO(stichnot): LEAHACK: remove Legal_All (and use default) once |
| 1349 // a proper emitter is used. | 1429 // a proper emitter is used. |
| 1350 Operand *CallTarget = legalize(Instr->getCallTarget(), Legal_All); | 1430 Operand *CallTarget = legalize(Instr->getCallTarget(), Legal_All); |
| 1351 Inst *NewCall = InstX8632Call::create(Func, eax, CallTarget); | 1431 Inst *NewCall = InstX8632Call::create(Func, ReturnReg, CallTarget); |
| 1352 Context.insert(NewCall); | 1432 Context.insert(NewCall); |
| 1353 if (edx) | 1433 if (ReturnRegHi) |
| 1354 Context.insert(InstFakeDef::create(Func, edx)); | 1434 Context.insert(InstFakeDef::create(Func, ReturnRegHi)); |
| 1355 | 1435 |
| 1356 // Add the appropriate offset to esp. | 1436 // Add the appropriate offset to esp. |
| 1357 if (StackOffset) { | 1437 if (StackOffset) { |
| 1358 Variable *esp = Func->getTarget()->getPhysicalRegister(Reg_esp); | 1438 Variable *esp = Func->getTarget()->getPhysicalRegister(Reg_esp); |
| 1359 _add(esp, Ctx->getConstantInt(IceType_i32, StackOffset)); | 1439 _add(esp, Ctx->getConstantInt(IceType_i32, StackOffset)); |
| 1360 } | 1440 } |
| 1361 | 1441 |
| 1362 // Insert a register-kill pseudo instruction. | 1442 // Insert a register-kill pseudo instruction. |
| 1363 VarList KilledRegs; | 1443 VarList KilledRegs; |
| 1364 for (SizeT i = 0; i < ScratchRegs.size(); ++i) { | 1444 for (SizeT i = 0; i < ScratchRegs.size(); ++i) { |
| 1365 if (ScratchRegs[i]) | 1445 if (ScratchRegs[i]) |
| 1366 KilledRegs.push_back(Func->getTarget()->getPhysicalRegister(i)); | 1446 KilledRegs.push_back(Func->getTarget()->getPhysicalRegister(i)); |
| 1367 } | 1447 } |
| 1368 Context.insert(InstFakeKill::create(Func, KilledRegs, NewCall)); | 1448 Context.insert(InstFakeKill::create(Func, KilledRegs, NewCall)); |
| 1369 | 1449 |
| 1370 // Generate a FakeUse to keep the call live if necessary. | 1450 // Generate a FakeUse to keep the call live if necessary. |
| 1371 if (Instr->hasSideEffects() && eax) { | 1451 if (Instr->hasSideEffects() && ReturnReg) { |
| 1372 Inst *FakeUse = InstFakeUse::create(Func, eax); | 1452 Inst *FakeUse = InstFakeUse::create(Func, ReturnReg); |
| 1373 Context.insert(FakeUse); | 1453 Context.insert(FakeUse); |
| 1374 } | 1454 } |
| 1375 | 1455 |
| 1376 // Generate Dest=eax assignment. | 1456 // Assign the result of the call to Dest. |
| 1377 if (Dest && eax) { | 1457 if (!Dest) { |
| 1378 if (edx) { | 1458 return; |
| 1459 } else if (ReturnReg) { | |
|
Jim Stichnoth
2014/07/09 16:56:13
Don't use "else if" here.
http://llvm.org/docs/Cod
wala
2014/07/09 19:05:12
Done.
| |
| 1460 if (ReturnRegHi) { | |
| 1461 assert(Dest->getType() == IceType_i64); | |
| 1379 split64(Dest); | 1462 split64(Dest); |
| 1380 Variable *DestLo = Dest->getLo(); | 1463 Variable *DestLo = Dest->getLo(); |
| 1381 Variable *DestHi = Dest->getHi(); | 1464 Variable *DestHi = Dest->getHi(); |
| 1382 DestLo->setPreferredRegister(eax, false); | 1465 DestLo->setPreferredRegister(ReturnReg, false); |
| 1383 DestHi->setPreferredRegister(edx, false); | 1466 DestHi->setPreferredRegister(ReturnRegHi, false); |
| 1384 _mov(DestLo, eax); | 1467 _mov(DestLo, ReturnReg); |
| 1385 _mov(DestHi, edx); | 1468 _mov(DestHi, ReturnRegHi); |
| 1386 } else { | 1469 } else { |
| 1387 Dest->setPreferredRegister(eax, false); | 1470 assert(Dest->getType() == IceType_i32 || Dest->getType() == IceType_i16 || |
| 1388 _mov(Dest, eax); | 1471 Dest->getType() == IceType_i8 || Dest->getType() == IceType_i1 || |
| 1472 isVectorType(Dest->getType())); | |
| 1473 Dest->setPreferredRegister(ReturnReg, false); | |
| 1474 if (isVectorType(Dest->getType())) { | |
| 1475 _movp(Dest, ReturnReg); | |
| 1476 } else { | |
| 1477 _mov(Dest, ReturnReg); | |
| 1478 } | |
| 1389 } | 1479 } |
| 1390 } | 1480 } else if (Dest->getType() == IceType_f32 || Dest->getType() == IceType_f64) { |
| 1391 | 1481 // Special treatment for an FP function which returns its result in |
| 1392 // Special treatment for an FP function which returns its result in | 1482 // st(0). |
| 1393 // st(0). | |
| 1394 if (Dest && | |
| 1395 (Dest->getType() == IceType_f32 || Dest->getType() == IceType_f64)) { | |
| 1396 _fstp(Dest); | 1483 _fstp(Dest); |
| 1397 // If Dest ends up being a physical xmm register, the fstp emit | 1484 // If Dest ends up being a physical xmm register, the fstp emit code |
| 1398 // code will route st(0) through a temporary stack slot. | 1485 // will route st(0) through a temporary stack slot. |
| 1399 } | 1486 } |
| 1400 } | 1487 } |
| 1401 | 1488 |
| 1402 void TargetX8632::lowerCast(const InstCast *Inst) { | 1489 void TargetX8632::lowerCast(const InstCast *Inst) { |
| 1403 // a = cast(b) ==> t=cast(b); a=t; (link t->b, link a->t, no overlap) | 1490 // a = cast(b) ==> t=cast(b); a=t; (link t->b, link a->t, no overlap) |
| 1404 InstCast::OpKind CastKind = Inst->getCastKind(); | 1491 InstCast::OpKind CastKind = Inst->getCastKind(); |
| 1405 Variable *Dest = Inst->getDest(); | 1492 Variable *Dest = Inst->getDest(); |
| 1406 switch (CastKind) { | 1493 switch (CastKind) { |
| 1407 default: | 1494 default: |
| 1408 Func->setError("Cast type not supported"); | 1495 Func->setError("Cast type not supported"); |
| (...skipping 1325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2734 for (SizeT i = 0; i < Size; ++i) { | 2821 for (SizeT i = 0; i < Size; ++i) { |
| 2735 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; | 2822 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; |
| 2736 } | 2823 } |
| 2737 Str << "\t.size\t" << MangledName << ", " << Size << "\n"; | 2824 Str << "\t.size\t" << MangledName << ", " << Size << "\n"; |
| 2738 } | 2825 } |
| 2739 Str << "\t" << (IsInternal ? ".local" : ".global") << "\t" << MangledName | 2826 Str << "\t" << (IsInternal ? ".local" : ".global") << "\t" << MangledName |
| 2740 << "\n"; | 2827 << "\n"; |
| 2741 } | 2828 } |
| 2742 | 2829 |
| 2743 } // end of namespace Ice | 2830 } // end of namespace Ice |
| OLD | NEW |