Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Side by Side Diff: src/IceTargetLoweringX86BaseImpl.h

Issue 1406593003: Optimize 64-bit compares with zero (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Add isZero utility function, fix comments. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceInstX86BaseImpl.h ('k') | tests_lit/assembler/x86/jump_encodings.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/IceTargetLoweringX86BaseImpl.h - x86 lowering -*- C++ -*-==// 1 //===- subzero/src/IceTargetLoweringX86BaseImpl.h - x86 lowering -*- C++ -*-==//
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 /// \file 10 /// \file
(...skipping 2630 matching lines...) Expand 10 before | Expand all | Expand 10 after
2641 if (HasC2) { 2641 if (HasC2) {
2642 _br(Traits::TableFcmp[Index].C2, Label); 2642 _br(Traits::TableFcmp[Index].C2, Label);
2643 } 2643 }
2644 Constant *NonDefault = 2644 Constant *NonDefault =
2645 Ctx->getConstantInt32(!Traits::TableFcmp[Index].Default); 2645 Ctx->getConstantInt32(!Traits::TableFcmp[Index].Default);
2646 _mov_redefined(Dest, NonDefault); 2646 _mov_redefined(Dest, NonDefault);
2647 Context.insert(Label); 2647 Context.insert(Label);
2648 } 2648 }
2649 } 2649 }
2650 2650
2651 inline bool isZero(const Operand *Opnd) {
2652 if (auto *C64 = llvm::dyn_cast<ConstantInteger64>(Opnd))
2653 return C64->getValue() == 0;
2654 if (auto *C32 = llvm::dyn_cast<ConstantInteger32>(Opnd))
2655 return C32->getValue() == 0;
2656 return false;
2657 }
2658
2651 template <class Machine> 2659 template <class Machine>
2652 void TargetX86Base<Machine>::lowerIcmp(const InstIcmp *Inst) { 2660 void TargetX86Base<Machine>::lowerIcmp(const InstIcmp *Inst) {
2653 Operand *Src0 = legalize(Inst->getSrc(0)); 2661 Operand *Src0 = legalize(Inst->getSrc(0));
2654 Operand *Src1 = legalize(Inst->getSrc(1)); 2662 Operand *Src1 = legalize(Inst->getSrc(1));
2655 Variable *Dest = Inst->getDest(); 2663 Variable *Dest = Inst->getDest();
2656 2664
2657 if (isVectorType(Dest->getType())) { 2665 if (isVectorType(Dest->getType())) {
2658 Type Ty = Src0->getType(); 2666 Type Ty = Src0->getType();
2659 // Promote i1 vectors to 128 bit integer vector types. 2667 // Promote i1 vectors to 128 bit integer vector types.
2660 if (typeElementType(Ty) == IceType_i1) { 2668 if (typeElementType(Ty) == IceType_i1) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
2762 eliminateNextVectorSextInstruction(Dest); 2770 eliminateNextVectorSextInstruction(Dest);
2763 return; 2771 return;
2764 } 2772 }
2765 2773
2766 if (!Traits::Is64Bit && Src0->getType() == IceType_i64) { 2774 if (!Traits::Is64Bit && Src0->getType() == IceType_i64) {
2767 lowerIcmp64(Inst); 2775 lowerIcmp64(Inst);
2768 return; 2776 return;
2769 } 2777 }
2770 2778
2771 // cmp b, c 2779 // cmp b, c
2780 if (isZero(Src1)) {
2781 switch (Inst->getCondition()) {
2782 default:
2783 break;
2784 case InstIcmp::Uge:
2785 _mov(Dest, Ctx->getConstantInt(Dest->getType(), 1));
2786 return;
2787 case InstIcmp::Ult:
2788 _mov(Dest, Ctx->getConstantInt(Dest->getType(), 0));
2789 return;
2790 }
2791 }
2772 Operand *Src0RM = legalizeSrc0ForCmp(Src0, Src1); 2792 Operand *Src0RM = legalizeSrc0ForCmp(Src0, Src1);
2773 _cmp(Src0RM, Src1); 2793 _cmp(Src0RM, Src1);
2774 _setcc(Dest, Traits::getIcmp32Mapping(Inst->getCondition())); 2794 _setcc(Dest, Traits::getIcmp32Mapping(Inst->getCondition()));
2775 } 2795 }
2776 2796
2777 template <typename Machine> 2797 template <typename Machine>
2778 template <typename T> 2798 template <typename T>
2779 typename std::enable_if<!T::Is64Bit, void>::type 2799 typename std::enable_if<!T::Is64Bit, void>::type
2780 TargetX86Base<Machine>::lowerIcmp64(const InstIcmp *Inst) { 2800 TargetX86Base<Machine>::lowerIcmp64(const InstIcmp *Inst) {
2781 // a=icmp cond, b, c ==> cmp b,c; a=1; br cond,L1; FakeUse(a); a=0; L1: 2801 // a=icmp cond, b, c ==> cmp b,c; a=1; br cond,L1; FakeUse(a); a=0; L1:
2782 Operand *Src0 = legalize(Inst->getSrc(0)); 2802 Operand *Src0 = legalize(Inst->getSrc(0));
2783 Operand *Src1 = legalize(Inst->getSrc(1)); 2803 Operand *Src1 = legalize(Inst->getSrc(1));
2784 Variable *Dest = Inst->getDest(); 2804 Variable *Dest = Inst->getDest();
2785 InstIcmp::ICond Condition = Inst->getCondition(); 2805 InstIcmp::ICond Condition = Inst->getCondition();
2786 size_t Index = static_cast<size_t>(Condition); 2806 size_t Index = static_cast<size_t>(Condition);
2787 assert(Index < Traits::TableIcmp64Size); 2807 assert(Index < Traits::TableIcmp64Size);
2788 Operand *Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem); 2808 Constant *Zero = Ctx->getConstantZero(IceType_i32);
2789 Operand *Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem); 2809 Constant *One = Ctx->getConstantInt32(1);
2810 Operand *Src0LoRM = nullptr;
2811 Operand *Src0HiRM = nullptr;
2812 // Legalize the portions of Src0 that are going to be needed.
2813 if (isZero(Src1)) {
2814 switch (Condition) {
2815 default:
2816 llvm_unreachable("unexpected condition");
2817 break;
2818 // These two are not optimized, so we fall through to the general case,
2819 // which needs the upper and lower halves legalized.
2820 case InstIcmp::Sgt:
2821 case InstIcmp::Sle:
2822 // These four compare after performing an "or" of the high and low half, so they
2823 // need the upper and lower halves legalized.
2824 case InstIcmp::Eq:
2825 case InstIcmp::Ule:
2826 case InstIcmp::Ne:
2827 case InstIcmp::Ugt:
2828 Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem);
2829 // These two test only the high half's sign bit, so they need only
2830 // the upper half legalized.
2831 case InstIcmp::Sge:
2832 case InstIcmp::Slt:
2833 Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem);
2834 break;
2835
2836 // These two move constants and hence need no legalization.
2837 case InstIcmp::Uge:
2838 case InstIcmp::Ult:
2839 break;
2840 }
2841 } else {
2842 Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem);
2843 Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem);
2844 }
2845 // Optimize comparisons with zero.
2846 if (isZero(Src1)) {
2847 Constant *SignMask = Ctx->getConstantInt32(0x80000000);
2848 Variable *Temp = nullptr;
2849 switch (Condition) {
2850 default:
2851 llvm_unreachable("unexpected condition");
2852 break;
2853 case InstIcmp::Eq:
2854 case InstIcmp::Ule:
2855 _mov(Temp, Src0LoRM);
2856 _or(Temp, Src0HiRM);
2857 Context.insert(InstFakeUse::create(Func, Temp));
2858 _setcc(Dest, Traits::Cond::Br_e);
2859 return;
2860 case InstIcmp::Ne:
2861 case InstIcmp::Ugt:
2862 _mov(Temp, Src0LoRM);
2863 _or(Temp, Src0HiRM);
2864 Context.insert(InstFakeUse::create(Func, Temp));
2865 _setcc(Dest, Traits::Cond::Br_ne);
2866 return;
2867 case InstIcmp::Uge:
2868 _mov(Dest, Ctx->getConstantInt(Dest->getType(), 1));
2869 return;
2870 case InstIcmp::Ult:
2871 _mov(Dest, Ctx->getConstantInt(Dest->getType(), 0));
2872 return;
2873 case InstIcmp::Sgt:
2874 break;
2875 case InstIcmp::Sge:
2876 _test(Src0HiRM, SignMask);
2877 _setcc(Dest, Traits::Cond::Br_e);
2878 return;
2879 case InstIcmp::Slt:
2880 _test(Src0HiRM, SignMask);
2881 _setcc(Dest, Traits::Cond::Br_ne);
2882 return;
2883 case InstIcmp::Sle:
2884 break;
2885 }
2886 }
2887 // Handle general compares.
2790 Operand *Src1LoRI = legalize(loOperand(Src1), Legal_Reg | Legal_Imm); 2888 Operand *Src1LoRI = legalize(loOperand(Src1), Legal_Reg | Legal_Imm);
2791 Operand *Src1HiRI = legalize(hiOperand(Src1), Legal_Reg | Legal_Imm); 2889 Operand *Src1HiRI = legalize(hiOperand(Src1), Legal_Reg | Legal_Imm);
2792 Constant *Zero = Ctx->getConstantZero(IceType_i32);
2793 Constant *One = Ctx->getConstantInt32(1);
2794 typename Traits::Insts::Label *LabelFalse = 2890 typename Traits::Insts::Label *LabelFalse =
2795 Traits::Insts::Label::create(Func, this); 2891 Traits::Insts::Label::create(Func, this);
2796 typename Traits::Insts::Label *LabelTrue = 2892 typename Traits::Insts::Label *LabelTrue =
2797 Traits::Insts::Label::create(Func, this); 2893 Traits::Insts::Label::create(Func, this);
2798 _mov(Dest, One); 2894 _mov(Dest, One);
2799 _cmp(Src0HiRM, Src1HiRI); 2895 _cmp(Src0HiRM, Src1HiRI);
2800 if (Traits::TableIcmp64[Index].C1 != Traits::Cond::Br_None) 2896 if (Traits::TableIcmp64[Index].C1 != Traits::Cond::Br_None)
2801 _br(Traits::TableIcmp64[Index].C1, LabelTrue); 2897 _br(Traits::TableIcmp64[Index].C1, LabelTrue);
2802 if (Traits::TableIcmp64[Index].C2 != Traits::Cond::Br_None) 2898 if (Traits::TableIcmp64[Index].C2 != Traits::Cond::Br_None)
2803 _br(Traits::TableIcmp64[Index].C2, LabelFalse); 2899 _br(Traits::TableIcmp64[Index].C2, LabelFalse);
(...skipping 2705 matching lines...) Expand 10 before | Expand all | Expand 10 after
5509 } 5605 }
5510 // the offset is not eligible for blinding or pooling, return the original 5606 // the offset is not eligible for blinding or pooling, return the original
5511 // mem operand 5607 // mem operand
5512 return MemOperand; 5608 return MemOperand;
5513 } 5609 }
5514 5610
5515 } // end of namespace X86Internal 5611 } // end of namespace X86Internal
5516 } // end of namespace Ice 5612 } // end of namespace Ice
5517 5613
5518 #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H 5614 #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H
OLDNEW
« no previous file with comments | « src/IceInstX86BaseImpl.h ('k') | tests_lit/assembler/x86/jump_encodings.ll » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698