| OLD | NEW |
| 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 // This file implements the TargetLoweringX86Base class, which | 10 /// \file |
| 11 // consists almost entirely of the lowering sequence for each | 11 /// This file implements the TargetLoweringX86Base class, which |
| 12 // high-level instruction. | 12 /// consists almost entirely of the lowering sequence for each |
| 13 // | 13 /// high-level instruction. |
| 14 /// |
| 14 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
| 15 | 16 |
| 16 #ifndef SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H | 17 #ifndef SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |
| 17 #define SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H | 18 #define SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |
| 18 | 19 |
| 19 #include "IceCfg.h" | 20 #include "IceCfg.h" |
| 20 #include "IceCfgNode.h" | 21 #include "IceCfgNode.h" |
| 21 #include "IceClFlags.h" | 22 #include "IceClFlags.h" |
| 22 #include "IceDefs.h" | 23 #include "IceDefs.h" |
| 23 #include "IceELFObjectWriter.h" | 24 #include "IceELFObjectWriter.h" |
| 24 #include "IceGlobalInits.h" | 25 #include "IceGlobalInits.h" |
| 25 #include "IceInstX8632.h" | 26 #include "IceInstX8632.h" |
| 26 #include "IceLiveness.h" | 27 #include "IceLiveness.h" |
| 27 #include "IceOperand.h" | 28 #include "IceOperand.h" |
| 28 #include "IceRegistersX8632.h" | 29 #include "IceRegistersX8632.h" |
| 29 #include "IceTargetLoweringX8632.def" | 30 #include "IceTargetLoweringX8632.def" |
| 30 #include "IceTargetLoweringX8632.h" | 31 #include "IceTargetLoweringX8632.h" |
| 31 #include "IceUtils.h" | 32 #include "IceUtils.h" |
| 32 #include "llvm/Support/MathExtras.h" | 33 #include "llvm/Support/MathExtras.h" |
| 33 | 34 |
| 34 namespace Ice { | 35 namespace Ice { |
| 35 namespace X86Internal { | 36 namespace X86Internal { |
| 36 | 37 |
| 37 // A helper class to ease the settings of RandomizationPoolingPause | 38 /// A helper class to ease the settings of RandomizationPoolingPause |
| 38 // to disable constant blinding or pooling for some translation phases. | 39 /// to disable constant blinding or pooling for some translation phases. |
| 39 class BoolFlagSaver { | 40 class BoolFlagSaver { |
| 40 BoolFlagSaver() = delete; | 41 BoolFlagSaver() = delete; |
| 41 BoolFlagSaver(const BoolFlagSaver &) = delete; | 42 BoolFlagSaver(const BoolFlagSaver &) = delete; |
| 42 BoolFlagSaver &operator=(const BoolFlagSaver &) = delete; | 43 BoolFlagSaver &operator=(const BoolFlagSaver &) = delete; |
| 43 | 44 |
| 44 public: | 45 public: |
| 45 BoolFlagSaver(bool &F, bool NewValue) : OldValue(F), Flag(F) { F = NewValue; } | 46 BoolFlagSaver(bool &F, bool NewValue) : OldValue(F), Flag(F) { F = NewValue; } |
| 46 ~BoolFlagSaver() { Flag = OldValue; } | 47 ~BoolFlagSaver() { Flag = OldValue; } |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 const bool OldValue; | 50 const bool OldValue; |
| 50 bool &Flag; | 51 bool &Flag; |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 template <class MachineTraits> class BoolFoldingEntry { | 54 template <class MachineTraits> class BoolFoldingEntry { |
| 54 BoolFoldingEntry(const BoolFoldingEntry &) = delete; | 55 BoolFoldingEntry(const BoolFoldingEntry &) = delete; |
| 55 | 56 |
| 56 public: | 57 public: |
| 57 BoolFoldingEntry() = default; | 58 BoolFoldingEntry() = default; |
| 58 explicit BoolFoldingEntry(Inst *I); | 59 explicit BoolFoldingEntry(Inst *I); |
| 59 BoolFoldingEntry &operator=(const BoolFoldingEntry &) = default; | 60 BoolFoldingEntry &operator=(const BoolFoldingEntry &) = default; |
| 60 // Instr is the instruction producing the i1-type variable of interest. | 61 /// Instr is the instruction producing the i1-type variable of interest. |
| 61 Inst *Instr = nullptr; | 62 Inst *Instr = nullptr; |
| 62 // IsComplex is the cached result of BoolFolding::hasComplexLowering(Instr). | 63 /// IsComplex is the cached result of BoolFolding::hasComplexLowering(Instr). |
| 63 bool IsComplex = false; | 64 bool IsComplex = false; |
| 64 // IsLiveOut is initialized conservatively to true, and is set to false when | 65 /// IsLiveOut is initialized conservatively to true, and is set to false when |
| 65 // we encounter an instruction that ends Var's live range. We disable the | 66 /// we encounter an instruction that ends Var's live range. We disable the |
| 66 // folding optimization when Var is live beyond this basic block. Note that | 67 /// folding optimization when Var is live beyond this basic block. Note that |
| 67 // if liveness analysis is not performed (e.g. in Om1 mode), IsLiveOut will | 68 /// if liveness analysis is not performed (e.g. in Om1 mode), IsLiveOut will |
| 68 // always be true and the folding optimization will never be performed. | 69 /// always be true and the folding optimization will never be performed. |
| 69 bool IsLiveOut = true; | 70 bool IsLiveOut = true; |
| 70 // NumUses counts the number of times Var is used as a source operand in the | 71 // NumUses counts the number of times Var is used as a source operand in the |
| 71 // basic block. If IsComplex is true and there is more than one use of Var, | 72 // basic block. If IsComplex is true and there is more than one use of Var, |
| 72 // then the folding optimization is disabled for Var. | 73 // then the folding optimization is disabled for Var. |
| 73 uint32_t NumUses = 0; | 74 uint32_t NumUses = 0; |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 template <class MachineTraits> class BoolFolding { | 77 template <class MachineTraits> class BoolFolding { |
| 77 public: | 78 public: |
| 78 enum BoolFoldingProducerKind { | 79 enum BoolFoldingProducerKind { |
| 79 PK_None, | 80 PK_None, |
| 80 PK_Icmp32, | 81 PK_Icmp32, |
| 81 PK_Icmp64, | 82 PK_Icmp64, |
| 82 PK_Fcmp, | 83 PK_Fcmp, |
| 83 PK_Trunc | 84 PK_Trunc |
| 84 }; | 85 }; |
| 85 | 86 |
| 86 // Currently the actual enum values are not used (other than CK_None), but we | 87 /// Currently the actual enum values are not used (other than CK_None), but we |
| 87 // go | 88 /// go |
| 88 // ahead and produce them anyway for symmetry with the | 89 /// ahead and produce them anyway for symmetry with the |
| 89 // BoolFoldingProducerKind. | 90 /// BoolFoldingProducerKind. |
| 90 enum BoolFoldingConsumerKind { CK_None, CK_Br, CK_Select, CK_Sext, CK_Zext }; | 91 enum BoolFoldingConsumerKind { CK_None, CK_Br, CK_Select, CK_Sext, CK_Zext }; |
| 91 | 92 |
| 92 private: | 93 private: |
| 93 BoolFolding(const BoolFolding &) = delete; | 94 BoolFolding(const BoolFolding &) = delete; |
| 94 BoolFolding &operator=(const BoolFolding &) = delete; | 95 BoolFolding &operator=(const BoolFolding &) = delete; |
| 95 | 96 |
| 96 public: | 97 public: |
| 97 BoolFolding() = default; | 98 BoolFolding() = default; |
| 98 static BoolFoldingProducerKind getProducerKind(const Inst *Instr); | 99 static BoolFoldingProducerKind getProducerKind(const Inst *Instr); |
| 99 static BoolFoldingConsumerKind getConsumerKind(const Inst *Instr); | 100 static BoolFoldingConsumerKind getConsumerKind(const Inst *Instr); |
| 100 static bool hasComplexLowering(const Inst *Instr); | 101 static bool hasComplexLowering(const Inst *Instr); |
| 101 void init(CfgNode *Node); | 102 void init(CfgNode *Node); |
| 102 const Inst *getProducerFor(const Operand *Opnd) const; | 103 const Inst *getProducerFor(const Operand *Opnd) const; |
| 103 void dump(const Cfg *Func) const; | 104 void dump(const Cfg *Func) const; |
| 104 | 105 |
| 105 private: | 106 private: |
| 106 // Returns true if Producers contains a valid entry for the given VarNum. | 107 /// Returns true if Producers contains a valid entry for the given VarNum. |
| 107 bool containsValid(SizeT VarNum) const { | 108 bool containsValid(SizeT VarNum) const { |
| 108 auto Element = Producers.find(VarNum); | 109 auto Element = Producers.find(VarNum); |
| 109 return Element != Producers.end() && Element->second.Instr != nullptr; | 110 return Element != Producers.end() && Element->second.Instr != nullptr; |
| 110 } | 111 } |
| 111 void setInvalid(SizeT VarNum) { Producers[VarNum].Instr = nullptr; } | 112 void setInvalid(SizeT VarNum) { Producers[VarNum].Instr = nullptr; } |
| 112 // Producers maps Variable::Number to a BoolFoldingEntry. | 113 /// Producers maps Variable::Number to a BoolFoldingEntry. |
| 113 std::unordered_map<SizeT, BoolFoldingEntry<MachineTraits>> Producers; | 114 std::unordered_map<SizeT, BoolFoldingEntry<MachineTraits>> Producers; |
| 114 }; | 115 }; |
| 115 | 116 |
| 116 template <class MachineTraits> | 117 template <class MachineTraits> |
| 117 BoolFoldingEntry<MachineTraits>::BoolFoldingEntry(Inst *I) | 118 BoolFoldingEntry<MachineTraits>::BoolFoldingEntry(Inst *I) |
| 118 : Instr(I), IsComplex(BoolFolding<MachineTraits>::hasComplexLowering(I)) {} | 119 : Instr(I), IsComplex(BoolFolding<MachineTraits>::hasComplexLowering(I)) {} |
| 119 | 120 |
| 120 template <class MachineTraits> | 121 template <class MachineTraits> |
| 121 typename BoolFolding<MachineTraits>::BoolFoldingProducerKind | 122 typename BoolFolding<MachineTraits>::BoolFoldingProducerKind |
| 122 BoolFolding<MachineTraits>::getProducerKind(const Inst *Instr) { | 123 BoolFolding<MachineTraits>::getProducerKind(const Inst *Instr) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 return CK_None; | 156 return CK_None; |
| 156 case InstCast::Sext: | 157 case InstCast::Sext: |
| 157 return CK_Sext; | 158 return CK_Sext; |
| 158 case InstCast::Zext: | 159 case InstCast::Zext: |
| 159 return CK_Zext; | 160 return CK_Zext; |
| 160 } | 161 } |
| 161 } | 162 } |
| 162 return CK_None; | 163 return CK_None; |
| 163 } | 164 } |
| 164 | 165 |
| 165 // Returns true if the producing instruction has a "complex" lowering | 166 /// Returns true if the producing instruction has a "complex" lowering |
| 166 // sequence. This generally means that its lowering sequence requires | 167 /// sequence. This generally means that its lowering sequence requires |
| 167 // more than one conditional branch, namely 64-bit integer compares | 168 /// more than one conditional branch, namely 64-bit integer compares |
| 168 // and some floating-point compares. When this is true, and there is | 169 /// and some floating-point compares. When this is true, and there is |
| 169 // more than one consumer, we prefer to disable the folding | 170 /// more than one consumer, we prefer to disable the folding |
| 170 // optimization because it minimizes branches. | 171 /// optimization because it minimizes branches. |
| 171 template <class MachineTraits> | 172 template <class MachineTraits> |
| 172 bool BoolFolding<MachineTraits>::hasComplexLowering(const Inst *Instr) { | 173 bool BoolFolding<MachineTraits>::hasComplexLowering(const Inst *Instr) { |
| 173 switch (getProducerKind(Instr)) { | 174 switch (getProducerKind(Instr)) { |
| 174 default: | 175 default: |
| 175 return false; | 176 return false; |
| 176 case PK_Icmp64: | 177 case PK_Icmp64: |
| 177 return true; | 178 return true; |
| 178 case PK_Fcmp: | 179 case PK_Fcmp: |
| 179 return MachineTraits::TableFcmp[llvm::cast<InstFcmp>(Instr)->getCondition()] | 180 return MachineTraits::TableFcmp[llvm::cast<InstFcmp>(Instr)->getCondition()] |
| 180 .C2 != CondX86::Br_None; | 181 .C2 != CondX86::Br_None; |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 } | 604 } |
| 604 | 605 |
| 605 // Converts a ConstantInteger32 operand into its constant value, or | 606 // Converts a ConstantInteger32 operand into its constant value, or |
| 606 // MemoryOrderInvalid if the operand is not a ConstantInteger32. | 607 // MemoryOrderInvalid if the operand is not a ConstantInteger32. |
| 607 uint64_t getConstantMemoryOrder(Operand *Opnd) { | 608 uint64_t getConstantMemoryOrder(Operand *Opnd) { |
| 608 if (auto Integer = llvm::dyn_cast<ConstantInteger32>(Opnd)) | 609 if (auto Integer = llvm::dyn_cast<ConstantInteger32>(Opnd)) |
| 609 return Integer->getValue(); | 610 return Integer->getValue(); |
| 610 return Intrinsics::MemoryOrderInvalid; | 611 return Intrinsics::MemoryOrderInvalid; |
| 611 } | 612 } |
| 612 | 613 |
| 613 // Determines whether the dest of a Load instruction can be folded | 614 /// Determines whether the dest of a Load instruction can be folded |
| 614 // into one of the src operands of a 2-operand instruction. This is | 615 /// into one of the src operands of a 2-operand instruction. This is |
| 615 // true as long as the load dest matches exactly one of the binary | 616 /// true as long as the load dest matches exactly one of the binary |
| 616 // instruction's src operands. Replaces Src0 or Src1 with LoadSrc if | 617 /// instruction's src operands. Replaces Src0 or Src1 with LoadSrc if |
| 617 // the answer is true. | 618 /// the answer is true. |
| 618 bool canFoldLoadIntoBinaryInst(Operand *LoadSrc, Variable *LoadDest, | 619 bool canFoldLoadIntoBinaryInst(Operand *LoadSrc, Variable *LoadDest, |
| 619 Operand *&Src0, Operand *&Src1) { | 620 Operand *&Src0, Operand *&Src1) { |
| 620 if (Src0 == LoadDest && Src1 != LoadDest) { | 621 if (Src0 == LoadDest && Src1 != LoadDest) { |
| 621 Src0 = LoadSrc; | 622 Src0 = LoadSrc; |
| 622 return true; | 623 return true; |
| 623 } | 624 } |
| 624 if (Src0 != LoadDest && Src1 == LoadDest) { | 625 if (Src0 != LoadDest && Src1 == LoadDest) { |
| 625 Src1 = LoadSrc; | 626 Src1 = LoadSrc; |
| 626 return true; | 627 return true; |
| 627 } | 628 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 RegisterArg->setName(Func, "home_reg:" + Arg->getName(Func)); | 845 RegisterArg->setName(Func, "home_reg:" + Arg->getName(Func)); |
| 845 RegisterArg->setRegNum(RegNum); | 846 RegisterArg->setRegNum(RegNum); |
| 846 RegisterArg->setIsArg(); | 847 RegisterArg->setIsArg(); |
| 847 Arg->setIsArg(false); | 848 Arg->setIsArg(false); |
| 848 | 849 |
| 849 Args[I] = RegisterArg; | 850 Args[I] = RegisterArg; |
| 850 Context.insert(InstAssign::create(Func, Arg, RegisterArg)); | 851 Context.insert(InstAssign::create(Func, Arg, RegisterArg)); |
| 851 } | 852 } |
| 852 } | 853 } |
| 853 | 854 |
| 854 // Helper function for addProlog(). | 855 /// Helper function for addProlog(). |
| 855 // | 856 /// |
| 856 // This assumes Arg is an argument passed on the stack. This sets the | 857 /// This assumes Arg is an argument passed on the stack. This sets the |
| 857 // frame offset for Arg and updates InArgsSizeBytes according to Arg's | 858 /// frame offset for Arg and updates InArgsSizeBytes according to Arg's |
| 858 // width. For an I64 arg that has been split into Lo and Hi components, | 859 /// width. For an I64 arg that has been split into Lo and Hi components, |
| 859 // it calls itself recursively on the components, taking care to handle | 860 /// it calls itself recursively on the components, taking care to handle |
| 860 // Lo first because of the little-endian architecture. Lastly, this | 861 /// Lo first because of the little-endian architecture. Lastly, this |
| 861 // function generates an instruction to copy Arg into its assigned | 862 /// function generates an instruction to copy Arg into its assigned |
| 862 // register if applicable. | 863 /// register if applicable. |
| 863 template <class Machine> | 864 template <class Machine> |
| 864 void TargetX86Base<Machine>::finishArgumentLowering(Variable *Arg, | 865 void TargetX86Base<Machine>::finishArgumentLowering(Variable *Arg, |
| 865 Variable *FramePtr, | 866 Variable *FramePtr, |
| 866 size_t BasicFrameOffset, | 867 size_t BasicFrameOffset, |
| 867 size_t &InArgsSizeBytes) { | 868 size_t &InArgsSizeBytes) { |
| 868 Variable *Lo = Arg->getLo(); | 869 Variable *Lo = Arg->getLo(); |
| 869 Variable *Hi = Arg->getHi(); | 870 Variable *Hi = Arg->getHi(); |
| 870 Type Ty = Arg->getType(); | 871 Type Ty = Arg->getType(); |
| 871 if (Lo && Hi && Ty == IceType_i64) { | 872 if (Lo && Hi && Ty == IceType_i64) { |
| 872 assert(Lo->getType() != IceType_i64); // don't want infinite recursion | 873 assert(Lo->getType() != IceType_i64); // don't want infinite recursion |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1339 // multiple of the required alignment at runtime. | 1340 // multiple of the required alignment at runtime. |
| 1340 Variable *T = makeReg(IceType_i32); | 1341 Variable *T = makeReg(IceType_i32); |
| 1341 _mov(T, TotalSize); | 1342 _mov(T, TotalSize); |
| 1342 _add(T, Ctx->getConstantInt32(Alignment - 1)); | 1343 _add(T, Ctx->getConstantInt32(Alignment - 1)); |
| 1343 _and(T, Ctx->getConstantInt32(-Alignment)); | 1344 _and(T, Ctx->getConstantInt32(-Alignment)); |
| 1344 _sub(esp, T); | 1345 _sub(esp, T); |
| 1345 } | 1346 } |
| 1346 _mov(Dest, esp); | 1347 _mov(Dest, esp); |
| 1347 } | 1348 } |
| 1348 | 1349 |
| 1349 // Strength-reduce scalar integer multiplication by a constant (for | 1350 /// Strength-reduce scalar integer multiplication by a constant (for |
| 1350 // i32 or narrower) for certain constants. The lea instruction can be | 1351 /// i32 or narrower) for certain constants. The lea instruction can be |
| 1351 // used to multiply by 3, 5, or 9, and the lsh instruction can be used | 1352 /// used to multiply by 3, 5, or 9, and the lsh instruction can be used |
| 1352 // to multiply by powers of 2. These can be combined such that | 1353 /// to multiply by powers of 2. These can be combined such that |
| 1353 // e.g. multiplying by 100 can be done as 2 lea-based multiplies by 5, | 1354 /// e.g. multiplying by 100 can be done as 2 lea-based multiplies by 5, |
| 1354 // combined with left-shifting by 2. | 1355 /// combined with left-shifting by 2. |
| 1355 template <class Machine> | 1356 template <class Machine> |
| 1356 bool TargetX86Base<Machine>::optimizeScalarMul(Variable *Dest, Operand *Src0, | 1357 bool TargetX86Base<Machine>::optimizeScalarMul(Variable *Dest, Operand *Src0, |
| 1357 int32_t Src1) { | 1358 int32_t Src1) { |
| 1358 // Disable this optimization for Om1 and O0, just to keep things | 1359 // Disable this optimization for Om1 and O0, just to keep things |
| 1359 // simple there. | 1360 // simple there. |
| 1360 if (Ctx->getFlags().getOptLevel() < Opt_1) | 1361 if (Ctx->getFlags().getOptLevel() < Opt_1) |
| 1361 return false; | 1362 return false; |
| 1362 Type Ty = Dest->getType(); | 1363 Type Ty = Dest->getType(); |
| 1363 Variable *T = nullptr; | 1364 Variable *T = nullptr; |
| 1364 if (Src1 == -1) { | 1365 if (Src1 == -1) { |
| (...skipping 1015 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2380 if (DestTy == IceType_v16i8) { | 2381 if (DestTy == IceType_v16i8) { |
| 2381 // onemask = materialize(1,1,...); dst = (src & onemask) > 0 | 2382 // onemask = materialize(1,1,...); dst = (src & onemask) > 0 |
| 2382 Variable *OneMask = makeVectorOfOnes(Dest->getType()); | 2383 Variable *OneMask = makeVectorOfOnes(Dest->getType()); |
| 2383 Variable *T = makeReg(DestTy); | 2384 Variable *T = makeReg(DestTy); |
| 2384 _movp(T, Src0RM); | 2385 _movp(T, Src0RM); |
| 2385 _pand(T, OneMask); | 2386 _pand(T, OneMask); |
| 2386 Variable *Zeros = makeVectorOfZeros(Dest->getType()); | 2387 Variable *Zeros = makeVectorOfZeros(Dest->getType()); |
| 2387 _pcmpgt(T, Zeros); | 2388 _pcmpgt(T, Zeros); |
| 2388 _movp(Dest, T); | 2389 _movp(Dest, T); |
| 2389 } else { | 2390 } else { |
| 2390 // width = width(elty) - 1; dest = (src << width) >> width | 2391 /// width = width(elty) - 1; dest = (src << width) >> width |
| 2391 SizeT ShiftAmount = | 2392 SizeT ShiftAmount = |
| 2392 Traits::X86_CHAR_BIT * typeWidthInBytes(typeElementType(DestTy)) - | 2393 Traits::X86_CHAR_BIT * typeWidthInBytes(typeElementType(DestTy)) - |
| 2393 1; | 2394 1; |
| 2394 Constant *ShiftConstant = Ctx->getConstantInt8(ShiftAmount); | 2395 Constant *ShiftConstant = Ctx->getConstantInt8(ShiftAmount); |
| 2395 Variable *T = makeReg(DestTy); | 2396 Variable *T = makeReg(DestTy); |
| 2396 _movp(T, Src0RM); | 2397 _movp(T, Src0RM); |
| 2397 _psll(T, ShiftConstant); | 2398 _psll(T, ShiftConstant); |
| 2398 _psra(T, ShiftConstant); | 2399 _psra(T, ShiftConstant); |
| 2399 _movp(Dest, T); | 2400 _movp(Dest, T); |
| 2400 } | 2401 } |
| (...skipping 1523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3924 // the end of the loop, since it will be re-used by the loop. | 3925 // the end of the loop, since it will be re-used by the loop. |
| 3925 if (Variable *ValVar = llvm::dyn_cast<Variable>(Val)) { | 3926 if (Variable *ValVar = llvm::dyn_cast<Variable>(Val)) { |
| 3926 Context.insert(InstFakeUse::create(Func, ValVar)); | 3927 Context.insert(InstFakeUse::create(Func, ValVar)); |
| 3927 } | 3928 } |
| 3928 // The address base (if any) is also reused in the loop. | 3929 // The address base (if any) is also reused in the loop. |
| 3929 if (Variable *Base = Addr->getBase()) | 3930 if (Variable *Base = Addr->getBase()) |
| 3930 Context.insert(InstFakeUse::create(Func, Base)); | 3931 Context.insert(InstFakeUse::create(Func, Base)); |
| 3931 _mov(Dest, T_eax); | 3932 _mov(Dest, T_eax); |
| 3932 } | 3933 } |
| 3933 | 3934 |
| 3934 // Lowers count {trailing, leading} zeros intrinsic. | 3935 /// Lowers count {trailing, leading} zeros intrinsic. |
| 3935 // | 3936 /// |
| 3936 // We could do constant folding here, but that should have | 3937 /// We could do constant folding here, but that should have |
| 3937 // been done by the front-end/middle-end optimizations. | 3938 /// been done by the front-end/middle-end optimizations. |
| 3938 template <class Machine> | 3939 template <class Machine> |
| 3939 void TargetX86Base<Machine>::lowerCountZeros(bool Cttz, Type Ty, Variable *Dest, | 3940 void TargetX86Base<Machine>::lowerCountZeros(bool Cttz, Type Ty, Variable *Dest, |
| 3940 Operand *FirstVal, | 3941 Operand *FirstVal, |
| 3941 Operand *SecondVal) { | 3942 Operand *SecondVal) { |
| 3942 // TODO(jvoung): Determine if the user CPU supports LZCNT (BMI). | 3943 // TODO(jvoung): Determine if the user CPU supports LZCNT (BMI). |
| 3943 // Then the instructions will handle the Val == 0 case much more simply | 3944 // Then the instructions will handle the Val == 0 case much more simply |
| 3944 // and won't require conversion from bit position to number of zeros. | 3945 // and won't require conversion from bit position to number of zeros. |
| 3945 // | 3946 // |
| 3946 // Otherwise: | 3947 // Otherwise: |
| 3947 // bsr IF_NOT_ZERO, Val | 3948 // bsr IF_NOT_ZERO, Val |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4590 | 4591 |
| 4591 // Insert the result into position. | 4592 // Insert the result into position. |
| 4592 Variable *DestT = Func->template makeVariable(Ty); | 4593 Variable *DestT = Func->template makeVariable(Ty); |
| 4593 lowerInsertElement(InstInsertElement::create(Func, DestT, T, Res, Index)); | 4594 lowerInsertElement(InstInsertElement::create(Func, DestT, T, Res, Index)); |
| 4594 T = DestT; | 4595 T = DestT; |
| 4595 } | 4596 } |
| 4596 | 4597 |
| 4597 lowerAssign(InstAssign::create(Func, Dest, T)); | 4598 lowerAssign(InstAssign::create(Func, Dest, T)); |
| 4598 } | 4599 } |
| 4599 | 4600 |
| 4600 // The following pattern occurs often in lowered C and C++ code: | 4601 /// The following pattern occurs often in lowered C and C++ code: |
| 4601 // | 4602 /// |
| 4602 // %cmp = fcmp/icmp pred <n x ty> %src0, %src1 | 4603 /// %cmp = fcmp/icmp pred <n x ty> %src0, %src1 |
| 4603 // %cmp.ext = sext <n x i1> %cmp to <n x ty> | 4604 /// %cmp.ext = sext <n x i1> %cmp to <n x ty> |
| 4604 // | 4605 /// |
| 4605 // We can eliminate the sext operation by copying the result of pcmpeqd, | 4606 /// We can eliminate the sext operation by copying the result of pcmpeqd, |
| 4606 // pcmpgtd, or cmpps (which produce sign extended results) to the result | 4607 /// pcmpgtd, or cmpps (which produce sign extended results) to the result |
| 4607 // of the sext operation. | 4608 /// of the sext operation. |
| 4608 template <class Machine> | 4609 template <class Machine> |
| 4609 void TargetX86Base<Machine>::eliminateNextVectorSextInstruction( | 4610 void TargetX86Base<Machine>::eliminateNextVectorSextInstruction( |
| 4610 Variable *SignExtendedResult) { | 4611 Variable *SignExtendedResult) { |
| 4611 if (InstCast *NextCast = | 4612 if (InstCast *NextCast = |
| 4612 llvm::dyn_cast_or_null<InstCast>(Context.getNextInst())) { | 4613 llvm::dyn_cast_or_null<InstCast>(Context.getNextInst())) { |
| 4613 if (NextCast->getCastKind() == InstCast::Sext && | 4614 if (NextCast->getCastKind() == InstCast::Sext && |
| 4614 NextCast->getSrc(0) == SignExtendedResult) { | 4615 NextCast->getSrc(0) == SignExtendedResult) { |
| 4615 NextCast->setDeleted(); | 4616 NextCast->setDeleted(); |
| 4616 _movp(NextCast->getDest(), legalizeToVar(SignExtendedResult)); | 4617 _movp(NextCast->getDest(), legalizeToVar(SignExtendedResult)); |
| 4617 // Skip over the instruction. | 4618 // Skip over the instruction. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4703 | 4704 |
| 4704 template <class Machine> | 4705 template <class Machine> |
| 4705 void TargetX86Base<Machine>::lowerOther(const Inst *Instr) { | 4706 void TargetX86Base<Machine>::lowerOther(const Inst *Instr) { |
| 4706 if (const auto *RMW = llvm::dyn_cast<InstX8632FakeRMW>(Instr)) { | 4707 if (const auto *RMW = llvm::dyn_cast<InstX8632FakeRMW>(Instr)) { |
| 4707 lowerRMW(RMW); | 4708 lowerRMW(RMW); |
| 4708 } else { | 4709 } else { |
| 4709 TargetLowering::lowerOther(Instr); | 4710 TargetLowering::lowerOther(Instr); |
| 4710 } | 4711 } |
| 4711 } | 4712 } |
| 4712 | 4713 |
| 4713 // Turn an i64 Phi instruction into a pair of i32 Phi instructions, to | 4714 /// Turn an i64 Phi instruction into a pair of i32 Phi instructions, to |
| 4714 // preserve integrity of liveness analysis. Undef values are also | 4715 /// preserve integrity of liveness analysis. Undef values are also |
| 4715 // turned into zeroes, since loOperand() and hiOperand() don't expect | 4716 /// turned into zeroes, since loOperand() and hiOperand() don't expect |
| 4716 // Undef input. | 4717 /// Undef input. |
| 4717 template <class Machine> void TargetX86Base<Machine>::prelowerPhis() { | 4718 template <class Machine> void TargetX86Base<Machine>::prelowerPhis() { |
| 4718 // Pause constant blinding or pooling, blinding or pooling will be done later | 4719 // Pause constant blinding or pooling, blinding or pooling will be done later |
| 4719 // during phi lowering assignments | 4720 // during phi lowering assignments |
| 4720 BoolFlagSaver B(RandomizationPoolingPaused, true); | 4721 BoolFlagSaver B(RandomizationPoolingPaused, true); |
| 4721 | 4722 |
| 4722 CfgNode *Node = Context.getNode(); | 4723 CfgNode *Node = Context.getNode(); |
| 4723 for (Inst &I : Node->getPhis()) { | 4724 for (Inst &I : Node->getPhis()) { |
| 4724 auto Phi = llvm::dyn_cast<InstPhi>(&I); | 4725 auto Phi = llvm::dyn_cast<InstPhi>(&I); |
| 4725 if (Phi->isDeleted()) | 4726 if (Phi->isDeleted()) |
| 4726 continue; | 4727 continue; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 4752 // because they do in fact need a register to materialize the vector | 4753 // because they do in fact need a register to materialize the vector |
| 4753 // of zeroes into. | 4754 // of zeroes into. |
| 4754 if (llvm::isa<ConstantUndef>(Opnd)) | 4755 if (llvm::isa<ConstantUndef>(Opnd)) |
| 4755 return isScalarFloatingType(Opnd->getType()) || | 4756 return isScalarFloatingType(Opnd->getType()) || |
| 4756 isVectorType(Opnd->getType()); | 4757 isVectorType(Opnd->getType()); |
| 4757 if (llvm::isa<Constant>(Opnd)) | 4758 if (llvm::isa<Constant>(Opnd)) |
| 4758 return isScalarFloatingType(Opnd->getType()); | 4759 return isScalarFloatingType(Opnd->getType()); |
| 4759 return true; | 4760 return true; |
| 4760 } | 4761 } |
| 4761 | 4762 |
| 4762 // Lower the pre-ordered list of assignments into mov instructions. | 4763 /// Lower the pre-ordered list of assignments into mov instructions. |
| 4763 // Also has to do some ad-hoc register allocation as necessary. | 4764 /// Also has to do some ad-hoc register allocation as necessary. |
| 4764 template <class Machine> | 4765 template <class Machine> |
| 4765 void TargetX86Base<Machine>::lowerPhiAssignments( | 4766 void TargetX86Base<Machine>::lowerPhiAssignments( |
| 4766 CfgNode *Node, const AssignList &Assignments) { | 4767 CfgNode *Node, const AssignList &Assignments) { |
| 4767 // Check that this is a properly initialized shell of a node. | 4768 // Check that this is a properly initialized shell of a node. |
| 4768 assert(Node->getOutEdges().size() == 1); | 4769 assert(Node->getOutEdges().size() == 1); |
| 4769 assert(Node->getInsts().empty()); | 4770 assert(Node->getInsts().empty()); |
| 4770 assert(Node->getPhis().empty()); | 4771 assert(Node->getPhis().empty()); |
| 4771 CfgNode *Succ = Node->getOutEdges().front(); | 4772 CfgNode *Succ = Node->getOutEdges().front(); |
| 4772 getContext().init(Node); | 4773 getContext().init(Node); |
| 4773 // Register set setup similar to regAlloc(). | 4774 // Register set setup similar to regAlloc(). |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4957 // SSE has no left shift operation for vectors of 8 bit integers. | 4958 // SSE has no left shift operation for vectors of 8 bit integers. |
| 4958 const uint32_t HIGH_ORDER_BITS_MASK = 0x80808080; | 4959 const uint32_t HIGH_ORDER_BITS_MASK = 0x80808080; |
| 4959 Constant *ConstantMask = Ctx->getConstantInt32(HIGH_ORDER_BITS_MASK); | 4960 Constant *ConstantMask = Ctx->getConstantInt32(HIGH_ORDER_BITS_MASK); |
| 4960 Variable *Reg = makeReg(Ty, RegNum); | 4961 Variable *Reg = makeReg(Ty, RegNum); |
| 4961 _movd(Reg, legalize(ConstantMask, Legal_Reg | Legal_Mem)); | 4962 _movd(Reg, legalize(ConstantMask, Legal_Reg | Legal_Mem)); |
| 4962 _pshufd(Reg, Reg, Ctx->getConstantZero(IceType_i8)); | 4963 _pshufd(Reg, Reg, Ctx->getConstantZero(IceType_i8)); |
| 4963 return Reg; | 4964 return Reg; |
| 4964 } | 4965 } |
| 4965 } | 4966 } |
| 4966 | 4967 |
| 4967 // Construct a mask in a register that can be and'ed with a | 4968 /// Construct a mask in a register that can be and'ed with a |
| 4968 // floating-point value to mask off its sign bit. The value will be | 4969 /// floating-point value to mask off its sign bit. The value will be |
| 4969 // <4 x 0x7fffffff> for f32 and v4f32, and <2 x 0x7fffffffffffffff> | 4970 /// <4 x 0x7fffffff> for f32 and v4f32, and <2 x 0x7fffffffffffffff> |
| 4970 // for f64. Construct it as vector of ones logically right shifted | 4971 /// for f64. Construct it as vector of ones logically right shifted |
| 4971 // one bit. TODO(stichnot): Fix the wala TODO above, to represent | 4972 /// one bit. TODO(stichnot): Fix the wala TODO above, to represent |
| 4972 // vector constants in memory. | 4973 /// vector constants in memory. |
| 4973 template <class Machine> | 4974 template <class Machine> |
| 4974 Variable *TargetX86Base<Machine>::makeVectorOfFabsMask(Type Ty, | 4975 Variable *TargetX86Base<Machine>::makeVectorOfFabsMask(Type Ty, |
| 4975 int32_t RegNum) { | 4976 int32_t RegNum) { |
| 4976 Variable *Reg = makeVectorOfMinusOnes(Ty, RegNum); | 4977 Variable *Reg = makeVectorOfMinusOnes(Ty, RegNum); |
| 4977 _psrl(Reg, Ctx->getConstantInt8(1)); | 4978 _psrl(Reg, Ctx->getConstantInt8(1)); |
| 4978 return Reg; | 4979 return Reg; |
| 4979 } | 4980 } |
| 4980 | 4981 |
| 4981 template <class Machine> | 4982 template <class Machine> |
| 4982 OperandX8632Mem * | 4983 OperandX8632Mem * |
| 4983 TargetX86Base<Machine>::getMemoryOperandForStackSlot(Type Ty, Variable *Slot, | 4984 TargetX86Base<Machine>::getMemoryOperandForStackSlot(Type Ty, Variable *Slot, |
| 4984 uint32_t Offset) { | 4985 uint32_t Offset) { |
| 4985 // Ensure that Loc is a stack slot. | 4986 // Ensure that Loc is a stack slot. |
| 4986 assert(Slot->getWeight().isZero()); | 4987 assert(Slot->getWeight().isZero()); |
| 4987 assert(Slot->getRegNum() == Variable::NoRegister); | 4988 assert(Slot->getRegNum() == Variable::NoRegister); |
| 4988 // Compute the location of Loc in memory. | 4989 // Compute the location of Loc in memory. |
| 4989 // TODO(wala,stichnot): lea should not be required. The address of | 4990 // TODO(wala,stichnot): lea should not be required. The address of |
| 4990 // the stack slot is known at compile time (although not until after | 4991 // the stack slot is known at compile time (although not until after |
| 4991 // addProlog()). | 4992 // addProlog()). |
| 4992 const Type PointerType = IceType_i32; | 4993 const Type PointerType = IceType_i32; |
| 4993 Variable *Loc = makeReg(PointerType); | 4994 Variable *Loc = makeReg(PointerType); |
| 4994 _lea(Loc, Slot); | 4995 _lea(Loc, Slot); |
| 4995 Constant *ConstantOffset = Ctx->getConstantInt32(Offset); | 4996 Constant *ConstantOffset = Ctx->getConstantInt32(Offset); |
| 4996 return OperandX8632Mem::create(Func, Ty, Loc, ConstantOffset); | 4997 return OperandX8632Mem::create(Func, Ty, Loc, ConstantOffset); |
| 4997 } | 4998 } |
| 4998 | 4999 |
| 4999 // Helper for legalize() to emit the right code to lower an operand to a | 5000 /// Helper for legalize() to emit the right code to lower an operand to a |
| 5000 // register of the appropriate type. | 5001 /// register of the appropriate type. |
| 5001 template <class Machine> | 5002 template <class Machine> |
| 5002 Variable *TargetX86Base<Machine>::copyToReg(Operand *Src, int32_t RegNum) { | 5003 Variable *TargetX86Base<Machine>::copyToReg(Operand *Src, int32_t RegNum) { |
| 5003 Type Ty = Src->getType(); | 5004 Type Ty = Src->getType(); |
| 5004 Variable *Reg = makeReg(Ty, RegNum); | 5005 Variable *Reg = makeReg(Ty, RegNum); |
| 5005 if (isVectorType(Ty)) { | 5006 if (isVectorType(Ty)) { |
| 5006 _movp(Reg, Src); | 5007 _movp(Reg, Src); |
| 5007 } else { | 5008 } else { |
| 5008 _mov(Reg, Src); | 5009 _mov(Reg, Src); |
| 5009 } | 5010 } |
| 5010 return Reg; | 5011 return Reg; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5117 if ((!(Allowed & Legal_Mem) && !MustHaveRegister) || | 5118 if ((!(Allowed & Legal_Mem) && !MustHaveRegister) || |
| 5118 (RegNum != Variable::NoRegister && RegNum != Var->getRegNum())) { | 5119 (RegNum != Variable::NoRegister && RegNum != Var->getRegNum())) { |
| 5119 From = copyToReg(From, RegNum); | 5120 From = copyToReg(From, RegNum); |
| 5120 } | 5121 } |
| 5121 return From; | 5122 return From; |
| 5122 } | 5123 } |
| 5123 llvm_unreachable("Unhandled operand kind in legalize()"); | 5124 llvm_unreachable("Unhandled operand kind in legalize()"); |
| 5124 return From; | 5125 return From; |
| 5125 } | 5126 } |
| 5126 | 5127 |
| 5127 // Provide a trivial wrapper to legalize() for this common usage. | 5128 /// Provide a trivial wrapper to legalize() for this common usage. |
| 5128 template <class Machine> | 5129 template <class Machine> |
| 5129 Variable *TargetX86Base<Machine>::legalizeToVar(Operand *From, int32_t RegNum) { | 5130 Variable *TargetX86Base<Machine>::legalizeToVar(Operand *From, int32_t RegNum) { |
| 5130 return llvm::cast<Variable>(legalize(From, Legal_Reg, RegNum)); | 5131 return llvm::cast<Variable>(legalize(From, Legal_Reg, RegNum)); |
| 5131 } | 5132 } |
| 5132 | 5133 |
| 5133 // For the cmp instruction, if Src1 is an immediate, or known to be a | 5134 /// For the cmp instruction, if Src1 is an immediate, or known to be a |
| 5134 // physical register, we can allow Src0 to be a memory operand. | 5135 /// physical register, we can allow Src0 to be a memory operand. |
| 5135 // Otherwise, Src0 must be copied into a physical register. | 5136 /// Otherwise, Src0 must be copied into a physical register. |
| 5136 // (Actually, either Src0 or Src1 can be chosen for the physical | 5137 /// (Actually, either Src0 or Src1 can be chosen for the physical |
| 5137 // register, but unfortunately we have to commit to one or the other | 5138 /// register, but unfortunately we have to commit to one or the other |
| 5138 // before register allocation.) | 5139 /// before register allocation.) |
| 5139 template <class Machine> | 5140 template <class Machine> |
| 5140 Operand *TargetX86Base<Machine>::legalizeSrc0ForCmp(Operand *Src0, | 5141 Operand *TargetX86Base<Machine>::legalizeSrc0ForCmp(Operand *Src0, |
| 5141 Operand *Src1) { | 5142 Operand *Src1) { |
| 5142 bool IsSrc1ImmOrReg = false; | 5143 bool IsSrc1ImmOrReg = false; |
| 5143 if (llvm::isa<Constant>(Src1)) { | 5144 if (llvm::isa<Constant>(Src1)) { |
| 5144 IsSrc1ImmOrReg = true; | 5145 IsSrc1ImmOrReg = true; |
| 5145 } else if (Variable *Var = llvm::dyn_cast<Variable>(Src1)) { | 5146 } else if (Variable *Var = llvm::dyn_cast<Variable>(Src1)) { |
| 5146 if (Var->hasReg()) | 5147 if (Var->hasReg()) |
| 5147 IsSrc1ImmOrReg = true; | 5148 IsSrc1ImmOrReg = true; |
| 5148 } | 5149 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5297 return; | 5298 return; |
| 5298 Ostream &Str = Ctx->getStrEmit(); | 5299 Ostream &Str = Ctx->getStrEmit(); |
| 5299 C->emitPoolLabel(Str); | 5300 C->emitPoolLabel(Str); |
| 5300 } | 5301 } |
| 5301 | 5302 |
| 5302 template <class Machine> | 5303 template <class Machine> |
| 5303 void TargetX86Base<Machine>::emit(const ConstantUndef *) const { | 5304 void TargetX86Base<Machine>::emit(const ConstantUndef *) const { |
| 5304 llvm::report_fatal_error("undef value encountered by emitter."); | 5305 llvm::report_fatal_error("undef value encountered by emitter."); |
| 5305 } | 5306 } |
| 5306 | 5307 |
| 5307 // Randomize or pool an Immediate. | 5308 /// Randomize or pool an Immediate. |
| 5308 template <class Machine> | 5309 template <class Machine> |
| 5309 Operand *TargetX86Base<Machine>::randomizeOrPoolImmediate(Constant *Immediate, | 5310 Operand *TargetX86Base<Machine>::randomizeOrPoolImmediate(Constant *Immediate, |
| 5310 int32_t RegNum) { | 5311 int32_t RegNum) { |
| 5311 assert(llvm::isa<ConstantInteger32>(Immediate) || | 5312 assert(llvm::isa<ConstantInteger32>(Immediate) || |
| 5312 llvm::isa<ConstantRelocatable>(Immediate)); | 5313 llvm::isa<ConstantRelocatable>(Immediate)); |
| 5313 if (Ctx->getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || | 5314 if (Ctx->getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || |
| 5314 RandomizationPoolingPaused == true) { | 5315 RandomizationPoolingPaused == true) { |
| 5315 // Immediates randomization/pooling off or paused | 5316 // Immediates randomization/pooling off or paused |
| 5316 return Immediate; | 5317 return Immediate; |
| 5317 } | 5318 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5496 } | 5497 } |
| 5497 // the offset is not eligible for blinding or pooling, return the original | 5498 // the offset is not eligible for blinding or pooling, return the original |
| 5498 // mem operand | 5499 // mem operand |
| 5499 return MemOperand; | 5500 return MemOperand; |
| 5500 } | 5501 } |
| 5501 | 5502 |
| 5502 } // end of namespace X86Internal | 5503 } // end of namespace X86Internal |
| 5503 } // end of namespace Ice | 5504 } // end of namespace Ice |
| 5504 | 5505 |
| 5505 #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H | 5506 #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |
| OLD | NEW |