Chromium Code Reviews

Side by Side Diff: src/IceTargetLoweringX8632.cpp

Issue 557953007: Subzero: Fix incorrect address mode inference involving Phi temporaries. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Remove accidental garbage Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « src/IceOperand.h ('k') | tests_lit/llvm2ice_tests/phi.ll » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 3529 matching lines...)
3540 else 3540 else
3541 Str << "<null>"; 3541 Str << "<null>";
3542 Str << ", Index="; 3542 Str << ", Index=";
3543 if (Index) 3543 if (Index)
3544 Index->dump(Func); 3544 Index->dump(Func);
3545 else 3545 else
3546 Str << "<null>"; 3546 Str << "<null>";
3547 Str << ", Shift=" << Shift << ", Offset=" << Offset << "\n"; 3547 Str << ", Shift=" << Shift << ", Offset=" << Offset << "\n";
3548 } 3548 }
3549 3549
3550 bool matchTransitiveAssign(Variable *&Var, const Inst *&Reason) {
3551 // Var originates from Var=SrcVar ==>
3552 // set Var:=SrcVar
3553 if (Var == NULL)
3554 return false;
3555 if (const Inst *VarAssign = Var->getDefinition()) {
3556 if (llvm::isa<InstAssign>(VarAssign)) {
3557 Operand *SrcOp = VarAssign->getSrc(0);
3558 assert(SrcOp);
3559 if (Variable *SrcVar = llvm::dyn_cast<Variable>(SrcOp)) {
3560 if (!SrcVar->getIsMultidef() &&
3561 // TODO: ensure SrcVar stays single-BB
3562 true) {
3563 Var = SrcVar;
3564 Reason = VarAssign;
3565 return true;
3566 }
3567 }
3568 }
3569 }
3570 return false;
3571 }
3572
3573 bool matchCombinedBaseIndex(Variable *&Base, Variable *&Index, uint16_t &Shift,
3574 const Inst *&Reason) {
3575 // Index==NULL && Base is Base=Var1+Var2 ==>
3576 // set Base=Var1, Index=Var2, Shift=0
3577 if (Base == NULL)
3578 return false;
3579 if (Index != NULL)
3580 return false;
3581 const Inst *BaseInst = Base->getDefinition();
3582 if (BaseInst == NULL)
3583 return false;
3584 if (BaseInst->getSrcSize() < 2)
3585 return false;
3586 if (Variable *Var1 = llvm::dyn_cast<Variable>(BaseInst->getSrc(0))) {
3587 if (Variable *Var2 = llvm::dyn_cast<Variable>(BaseInst->getSrc(1))) {
3588 if (isAdd(BaseInst) &&
3589 // TODO: ensure Var1 and Var2 stay single-BB
3590 !Var2->getIsMultidef() && true) {
jvoung (off chromium) 2014/09/15 16:37:04 Is there a reason that only Var2 is checked for ge
Jim Stichnoth 2014/09/15 17:26:54 Nice catch, thanks! Done.
3591 Base = Var1;
3592 Index = Var2;
3593 Shift = 0; // should already have been 0
3594 Reason = BaseInst;
3595 return true;
3596 }
3597 }
3598 }
3599 return false;
3600 }
3601
3602 bool matchShiftedIndex(Variable *&Index, uint16_t &Shift, const Inst *&Reason) {
3603 // Index is Index=Var*Const && log2(Const)+Shift<=3 ==>
3604 // Index=Var, Shift+=log2(Const)
3605 if (Index == NULL)
3606 return false;
3607 const Inst *IndexInst = Index->getDefinition();
3608 if (IndexInst == NULL)
3609 return false;
3610 if (IndexInst->getSrcSize() < 2)
3611 return false;
3612 if (const InstArithmetic *ArithInst =
3613 llvm::dyn_cast<InstArithmetic>(IndexInst)) {
3614 if (Variable *Var = llvm::dyn_cast<Variable>(ArithInst->getSrc(0))) {
3615 if (ConstantInteger *Const =
3616 llvm::dyn_cast<ConstantInteger>(ArithInst->getSrc(1))) {
3617 if (ArithInst->getOp() == InstArithmetic::Mul &&
3618 !Var->getIsMultidef() && Const->getType() == IceType_i32) {
3619 uint64_t Mult = Const->getValue();
3620 uint32_t LogMult;
3621 switch (Mult) {
3622 case 1:
3623 LogMult = 0;
3624 break;
3625 case 2:
3626 LogMult = 1;
3627 break;
3628 case 4:
3629 LogMult = 2;
3630 break;
3631 case 8:
3632 LogMult = 3;
3633 break;
3634 default:
3635 return false;
3636 }
3637 if (Shift + LogMult <= 3) {
3638 Index = Var;
3639 Shift += LogMult;
3640 Reason = IndexInst;
3641 return true;
3642 }
3643 }
3644 }
3645 }
3646 }
3647 return false;
3648 }
3649
3650 bool matchOffsetBase(Variable *&Base, int32_t &Offset, const Inst *&Reason) {
3651 // Base is Base=Var+Const || Base is Base=Const+Var ==>
3652 // set Base=Var, Offset+=Const
3653 // Base is Base=Var-Const ==>
3654 // set Base=Var, Offset-=Const
3655 if (Base == NULL)
3656 return false;
3657 const Inst *BaseInst = Base->getDefinition();
3658 if (BaseInst == NULL)
3659 return false;
3660 if (const InstArithmetic *ArithInst =
3661 llvm::dyn_cast<const InstArithmetic>(BaseInst)) {
3662 if (ArithInst->getOp() != InstArithmetic::Add &&
3663 ArithInst->getOp() != InstArithmetic::Sub)
3664 return false;
3665 bool IsAdd = ArithInst->getOp() == InstArithmetic::Add;
3666 Variable *Var = NULL;
3667 ConstantInteger *Const = NULL;
3668 if (Variable *VariableOperand =
3669 llvm::dyn_cast<Variable>(ArithInst->getSrc(0))) {
3670 Var = VariableOperand;
3671 Const = llvm::dyn_cast<ConstantInteger>(ArithInst->getSrc(1));
3672 } else if (IsAdd) {
3673 Const = llvm::dyn_cast<ConstantInteger>(ArithInst->getSrc(0));
3674 Var = llvm::dyn_cast<Variable>(ArithInst->getSrc(1));
3675 }
3676 if (Var == NULL || Const == NULL || Var->getIsMultidef())
3677 return false;
3678 Base = Var;
3679 Offset += IsAdd ? Const->getValue() : -Const->getValue();
3680 Reason = BaseInst;
3681 return true;
3682 }
3683 return false;
3684 }
3685
3550 void computeAddressOpt(Cfg *Func, const Inst *Instr, Variable *&Base, 3686 void computeAddressOpt(Cfg *Func, const Inst *Instr, Variable *&Base,
3551 Variable *&Index, uint16_t &Shift, int32_t &Offset) { 3687 Variable *&Index, uint16_t &Shift, int32_t &Offset) {
3552 Func->setCurrentNode(NULL); 3688 Func->setCurrentNode(NULL);
3553 if (Func->getContext()->isVerbose(IceV_AddrOpt)) { 3689 if (Func->getContext()->isVerbose(IceV_AddrOpt)) {
3554 Ostream &Str = Func->getContext()->getStrDump(); 3690 Ostream &Str = Func->getContext()->getStrDump();
3555 Str << "\nStarting computeAddressOpt for instruction:\n "; 3691 Str << "\nStarting computeAddressOpt for instruction:\n ";
3556 Instr->dumpDecorated(Func); 3692 Instr->dumpDecorated(Func);
3557 } 3693 }
3558 (void)Offset; // TODO: pattern-match for non-zero offsets. 3694 (void)Offset; // TODO: pattern-match for non-zero offsets.
3559 if (Base == NULL) 3695 if (Base == NULL)
3560 return; 3696 return;
3561 // If the Base has more than one use or is live across multiple 3697 // If the Base has more than one use or is live across multiple
3562 // blocks, then don't go further. Alternatively (?), never consider 3698 // blocks, then don't go further. Alternatively (?), never consider
3563 // a transformation that would change a variable that is currently 3699 // a transformation that would change a variable that is currently
3564 // *not* live across basic block boundaries into one that *is*. 3700 // *not* live across basic block boundaries into one that *is*.
3565 if (Base->isMultiblockLife() /* || Base->getUseCount() > 1*/) 3701 if (Base->isMultiblockLife() /* || Base->getUseCount() > 1*/)
3566 return; 3702 return;
3567 3703
3568 while (true) { 3704 bool Continue = true;
3569 // Base is Base=Var ==> 3705 while (Continue) {
3570 // set Base=Var 3706 const Inst *Reason = NULL;
3571 const Inst *BaseInst = Base->getDefinition(); 3707 if (matchTransitiveAssign(Base, Reason) ||
3572 Operand *BaseOperand0 = BaseInst ? BaseInst->getSrc(0) : NULL; 3708 matchTransitiveAssign(Index, Reason) ||
3573 Variable *BaseVariable0 = llvm::dyn_cast_or_null<Variable>(BaseOperand0); 3709 matchCombinedBaseIndex(Base, Index, Shift, Reason) ||
3574 // TODO: Helper function for all instances of assignment 3710 matchShiftedIndex(Index, Shift, Reason) ||
3575 // transitivity. 3711 matchOffsetBase(Base, Offset, Reason)) {
3576 if (BaseInst && llvm::isa<InstAssign>(BaseInst) && BaseVariable0 && 3712 dumpAddressOpt(Func, Base, Index, Shift, Offset, Reason);
3577 // TODO: ensure BaseVariable0 stays single-BB 3713 } else {
3578 true) { 3714 Continue = false;
3579 Base = BaseVariable0;
3580 dumpAddressOpt(Func, Base, Index, Shift, Offset, BaseInst);
3581 continue;
3582 }
3583
3584 // Index is Index=Var ==>
3585 // set Index=Var
3586
3587 // Index==NULL && Base is Base=Var1+Var2 ==>
3588 // set Base=Var1, Index=Var2, Shift=0
3589 Operand *BaseOperand1 =
3590 BaseInst && BaseInst->getSrcSize() >= 2 ? BaseInst->getSrc(1) : NULL;
3591 Variable *BaseVariable1 = llvm::dyn_cast_or_null<Variable>(BaseOperand1);
3592 if (Index == NULL && isAdd(BaseInst) && BaseVariable0 && BaseVariable1 &&
3593 // TODO: ensure BaseVariable0 and BaseVariable1 stay single-BB
3594 true) {
3595 Base = BaseVariable0;
3596 Index = BaseVariable1;
3597 Shift = 0; // should already have been 0
3598 dumpAddressOpt(Func, Base, Index, Shift, Offset, BaseInst);
3599 continue;
3600 }
3601
3602 // Index is Index=Var*Const && log2(Const)+Shift<=3 ==>
3603 // Index=Var, Shift+=log2(Const)
3604 const Inst *IndexInst = Index ? Index->getDefinition() : NULL;
3605 if (const InstArithmetic *ArithInst =
3606 llvm::dyn_cast_or_null<InstArithmetic>(IndexInst)) {
3607 Operand *IndexOperand0 = ArithInst->getSrc(0);
3608 Variable *IndexVariable0 = llvm::dyn_cast<Variable>(IndexOperand0);
3609 Operand *IndexOperand1 = ArithInst->getSrc(1);
3610 ConstantInteger *IndexConstant1 =
3611 llvm::dyn_cast<ConstantInteger>(IndexOperand1);
3612 if (ArithInst->getOp() == InstArithmetic::Mul && IndexVariable0 &&
3613 IndexOperand1->getType() == IceType_i32 && IndexConstant1) {
3614 uint64_t Mult = IndexConstant1->getValue();
3615 uint32_t LogMult;
3616 switch (Mult) {
3617 case 1:
3618 LogMult = 0;
3619 break;
3620 case 2:
3621 LogMult = 1;
3622 break;
3623 case 4:
3624 LogMult = 2;
3625 break;
3626 case 8:
3627 LogMult = 3;
3628 break;
3629 default:
3630 LogMult = 4;
3631 break;
3632 }
3633 if (Shift + LogMult <= 3) {
3634 Index = IndexVariable0;
3635 Shift += LogMult;
3636 dumpAddressOpt(Func, Base, Index, Shift, Offset, IndexInst);
3637 continue;
3638 }
3639 }
3640 }
3641
3642 // Base is Base=Var+Const || Base is Base=Const+Var ==>
3643 // set Base=Var, Offset+=Const
3644 // Base is Base=Var-Const ==>
3645 // set Base=Var, Offset-=Const
3646 const InstArithmetic *ArithInst =
3647 llvm::dyn_cast_or_null<const InstArithmetic>(BaseInst);
3648 if (ArithInst && (ArithInst->getOp() == InstArithmetic::Add ||
3649 ArithInst->getOp() == InstArithmetic::Sub)) {
3650 bool IsAdd = ArithInst->getOp() == InstArithmetic::Add;
3651 Variable *Var = NULL;
3652 ConstantInteger *Const = NULL;
3653 if (Variable *VariableOperand =
3654 llvm::dyn_cast<Variable>(ArithInst->getSrc(0))) {
3655 Var = VariableOperand;
3656 Const = llvm::dyn_cast<ConstantInteger>(ArithInst->getSrc(1));
3657 } else if (IsAdd) {
3658 Const = llvm::dyn_cast<ConstantInteger>(ArithInst->getSrc(0));
3659 Var = llvm::dyn_cast<Variable>(ArithInst->getSrc(1));
3660 }
3661 if (!(Const && Var)) {
3662 break;
3663 }
3664 Base = Var;
3665 Offset += IsAdd ? Const->getValue() : -Const->getValue();
3666 dumpAddressOpt(Func, Base, Index, Shift, Offset, BaseInst);
3667 continue;
3668 } 3715 }
3669 3716
3670 // Index is Index=Var<<Const && Const+Shift<=3 ==> 3717 // Index is Index=Var<<Const && Const+Shift<=3 ==>
3671 // Index=Var, Shift+=Const 3718 // Index=Var, Shift+=Const
3672 3719
3673 // Index is Index=Const*Var && log2(Const)+Shift<=3 ==> 3720 // Index is Index=Const*Var && log2(Const)+Shift<=3 ==>
3674 // Index=Var, Shift+=log2(Const) 3721 // Index=Var, Shift+=log2(Const)
3675 3722
3676 // Index && Shift==0 && Base is Base=Var*Const && log2(Const)+Shift<=3 ==> 3723 // Index && Shift==0 && Base is Base=Var*Const && log2(Const)+Shift<=3 ==>
3677 // swap(Index,Base) 3724 // swap(Index,Base)
3678 // Similar for Base=Const*Var and Base=Var<<Const 3725 // Similar for Base=Const*Var and Base=Var<<Const
3679 3726
3680 // Index is Index=Var+Const ==> 3727 // Index is Index=Var+Const ==>
3681 // set Index=Var, Offset+=(Const<<Shift) 3728 // set Index=Var, Offset+=(Const<<Shift)
3682 3729
3683 // Index is Index=Const+Var ==> 3730 // Index is Index=Const+Var ==>
3684 // set Index=Var, Offset+=(Const<<Shift) 3731 // set Index=Var, Offset+=(Const<<Shift)
3685 3732
3686 // Index is Index=Var-Const ==> 3733 // Index is Index=Var-Const ==>
3687 // set Index=Var, Offset-=(Const<<Shift) 3734 // set Index=Var, Offset-=(Const<<Shift)
3688 3735
3689 // TODO: consider overflow issues with respect to Offset. 3736 // TODO: consider overflow issues with respect to Offset.
3690 // TODO: handle symbolic constants. 3737 // TODO: handle symbolic constants.
3691 break;
3692 } 3738 }
3693 } 3739 }
3694 3740
3695 } // anonymous namespace 3741 } // anonymous namespace
3696 3742
3697 void TargetX8632::lowerLoad(const InstLoad *Inst) { 3743 void TargetX8632::lowerLoad(const InstLoad *Inst) {
3698 // A Load instruction can be treated the same as an Assign 3744 // A Load instruction can be treated the same as an Assign
3699 // instruction, after the source operand is transformed into an 3745 // instruction, after the source operand is transformed into an
3700 // OperandX8632Mem operand. Note that the address mode 3746 // OperandX8632Mem operand. Note that the address mode
3701 // optimization already creates an OperandX8632Mem operand, so it 3747 // optimization already creates an OperandX8632Mem operand, so it
(...skipping 761 matching lines...)
4463 Str << "\t.align\t" << Align << "\n"; 4509 Str << "\t.align\t" << Align << "\n";
4464 Str << MangledName << ":\n"; 4510 Str << MangledName << ":\n";
4465 for (SizeT i = 0; i < Size; ++i) { 4511 for (SizeT i = 0; i < Size; ++i) {
4466 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; 4512 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n";
4467 } 4513 }
4468 Str << "\t.size\t" << MangledName << ", " << Size << "\n"; 4514 Str << "\t.size\t" << MangledName << ", " << Size << "\n";
4469 } 4515 }
4470 } 4516 }
4471 4517
4472 } // end of namespace Ice 4518 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceOperand.h ('k') | tests_lit/llvm2ice_tests/phi.ll » ('j') | no next file with comments »

Powered by Google App Engine