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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 #undef X | 78 #undef X |
79 }; | 79 }; |
80 const size_t TableIcmp64Size = llvm::array_lengthof(TableIcmp64); | 80 const size_t TableIcmp64Size = llvm::array_lengthof(TableIcmp64); |
81 | 81 |
82 InstX8632Br::BrCond getIcmp32Mapping(InstIcmp::ICond Cond) { | 82 InstX8632Br::BrCond getIcmp32Mapping(InstIcmp::ICond Cond) { |
83 size_t Index = static_cast<size_t>(Cond); | 83 size_t Index = static_cast<size_t>(Cond); |
84 assert(Index < TableIcmp32Size); | 84 assert(Index < TableIcmp32Size); |
85 return TableIcmp32[Index].Mapping; | 85 return TableIcmp32[Index].Mapping; |
86 } | 86 } |
87 | 87 |
88 // Returns the type name as a valid assembly label. | |
89 IceString typeAsmLabel(Type Ty) { | |
90 IceString Result; | |
91 llvm::raw_string_ostream BaseOS(Result); | |
92 Ostream OS(&BaseOS); | |
93 unsigned NumElements = typeNumElements(Ty); | |
94 if (NumElements > 1) { | |
95 OS << "v" << NumElements; | |
96 } | |
97 OS << typeElementType(Ty); | |
98 return BaseOS.str(); | |
99 } | |
100 | |
88 // In some cases, there are x-macros tables for both high-level and | 101 // In some cases, there are x-macros tables for both high-level and |
89 // low-level instructions/operands that use the same enum key value. | 102 // low-level instructions/operands that use the same enum key value. |
90 // The tables are kept separate to maintain a proper separation | 103 // The tables are kept separate to maintain a proper separation |
91 // between abstraction layers. There is a risk that the tables | 104 // between abstraction layers. There is a risk that the tables |
92 // could get out of sync if enum values are reordered or if entries | 105 // could get out of sync if enum values are reordered or if entries |
93 // are added or deleted. This dummy function uses static_assert to | 106 // are added or deleted. This dummy function uses static_assert to |
94 // ensure everything is kept in sync. | 107 // ensure everything is kept in sync. |
95 void xMacroIntegrityCheck() { | 108 void xMacroIntegrityCheck() { |
96 // Validate the enum values in FCMPX8632_TABLE. | 109 // Validate the enum values in FCMPX8632_TABLE. |
97 { | 110 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 { | 166 { |
154 // Define a temporary set of enum values based on low-level | 167 // Define a temporary set of enum values based on low-level |
155 // table entries. | 168 // table entries. |
156 enum _tmp_enum { | 169 enum _tmp_enum { |
157 #define X(tag, cvt, sdss, width) _tmp_##tag, | 170 #define X(tag, cvt, sdss, width) _tmp_##tag, |
158 ICETYPEX8632_TABLE | 171 ICETYPEX8632_TABLE |
159 #undef X | 172 #undef X |
160 _num | 173 _num |
161 }; | 174 }; |
162 // Define a set of constants based on high-level table entries. | 175 // Define a set of constants based on high-level table entries. |
163 #define X(tag, size, align, str) static const int _table1_##tag = tag; | 176 #define X(tag, size, align, elts, elty, str) \ |
177 static const int _table1_##tag = tag; | |
164 ICETYPE_TABLE; | 178 ICETYPE_TABLE; |
165 #undef X | 179 #undef X |
166 // Define a set of constants based on low-level table entries, | 180 // Define a set of constants based on low-level table entries, |
167 // and ensure the table entry keys are consistent. | 181 // and ensure the table entry keys are consistent. |
168 #define X(tag, cvt, sdss, width) \ | 182 #define X(tag, cvt, sdss, width) \ |
169 static const int _table2_##tag = _tmp_##tag; \ | 183 static const int _table2_##tag = _tmp_##tag; \ |
170 STATIC_ASSERT(_table1_##tag == _table2_##tag); | 184 STATIC_ASSERT(_table1_##tag == _table2_##tag); |
171 ICETYPEX8632_TABLE; | 185 ICETYPEX8632_TABLE; |
172 #undef X | 186 #undef X |
173 // Repeat the static asserts with respect to the high-level | 187 // Repeat the static asserts with respect to the high-level |
174 // table entries in case the high-level table has extra entries. | 188 // table entries in case the high-level table has extra entries. |
175 #define X(tag, size, align, str) STATIC_ASSERT(_table1_##tag == _table2_##tag); | 189 #define X(tag, size, align, elts, elty, str) \ |
190 STATIC_ASSERT(_table1_##tag == _table2_##tag); | |
176 ICETYPE_TABLE; | 191 ICETYPE_TABLE; |
177 #undef X | 192 #undef X |
178 } | 193 } |
179 } | 194 } |
180 | 195 |
181 } // end of anonymous namespace | 196 } // end of anonymous namespace |
182 | 197 |
183 TargetX8632::TargetX8632(Cfg *Func) | 198 TargetX8632::TargetX8632(Cfg *Func) |
184 : TargetLowering(Func), IsEbpBasedFrame(false), FrameSizeLocals(0), | 199 : TargetLowering(Func), IsEbpBasedFrame(false), FrameSizeLocals(0), |
185 LocalsSizeBytes(0), NextLabelNumber(0), ComputedLiveRanges(false), | 200 LocalsSizeBytes(0), NextLabelNumber(0), ComputedLiveRanges(false), |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
674 typedef ConstantDouble IceType; | 689 typedef ConstantDouble IceType; |
675 static const Type Ty = IceType_f64; | 690 static const Type Ty = IceType_f64; |
676 static const char *TypeName; | 691 static const char *TypeName; |
677 static const char *AsmTag; | 692 static const char *AsmTag; |
678 static const char *PrintfString; | 693 static const char *PrintfString; |
679 }; | 694 }; |
680 const char *PoolTypeConverter<double>::TypeName = "double"; | 695 const char *PoolTypeConverter<double>::TypeName = "double"; |
681 const char *PoolTypeConverter<double>::AsmTag = ".quad"; | 696 const char *PoolTypeConverter<double>::AsmTag = ".quad"; |
682 const char *PoolTypeConverter<double>::PrintfString = "0x%llx"; | 697 const char *PoolTypeConverter<double>::PrintfString = "0x%llx"; |
683 | 698 |
684 template <typename T> void TargetX8632::emitConstantPool() const { | 699 template <typename T> void TargetX8632::emitScalarConstantPool() const { |
685 Ostream &Str = Ctx->getStrEmit(); | 700 Ostream &Str = Ctx->getStrEmit(); |
686 Type Ty = T::Ty; | 701 Type Ty = T::Ty; |
687 SizeT Align = typeAlignInBytes(Ty); | 702 SizeT Align = typeAlignInBytes(Ty); |
688 ConstantList Pool = Ctx->getConstantPool(Ty); | 703 ConstantList Pool = Ctx->getConstantPool(Ty); |
689 | 704 |
690 Str << "\t.section\t.rodata.cst" << Align << ",\"aM\",@progbits," << Align | 705 Str << "\t.section\t.rodata.cst" << Align << ",\"aM\",@progbits," << Align |
691 << "\n"; | 706 << "\n"; |
692 Str << "\t.align\t" << Align << "\n"; | 707 Str << "\t.align\t" << Align << "\n"; |
693 for (ConstantList::const_iterator I = Pool.begin(), E = Pool.end(); I != E; | 708 for (ConstantList::const_iterator I = Pool.begin(), E = Pool.end(); I != E; |
694 ++I) { | 709 ++I) { |
695 typename T::IceType *Const = llvm::cast<typename T::IceType>(*I); | 710 typename T::IceType *Const = llvm::cast<typename T::IceType>(*I); |
696 typename T::PrimitiveFpType Value = Const->getValue(); | 711 typename T::PrimitiveFpType Value = Const->getValue(); |
697 // Use memcpy() to copy bits from Value into RawValue in a way | 712 // Use memcpy() to copy bits from Value into RawValue in a way |
698 // that avoids breaking strict-aliasing rules. | 713 // that avoids breaking strict-aliasing rules. |
699 typename T::PrimitiveIntType RawValue; | 714 typename T::PrimitiveIntType RawValue; |
700 memcpy(&RawValue, &Value, sizeof(Value)); | 715 memcpy(&RawValue, &Value, sizeof(Value)); |
701 char buf[30]; | 716 char buf[30]; |
702 int CharsPrinted = | 717 int CharsPrinted = |
703 snprintf(buf, llvm::array_lengthof(buf), T::PrintfString, RawValue); | 718 snprintf(buf, llvm::array_lengthof(buf), T::PrintfString, RawValue); |
704 assert(CharsPrinted >= 0 && | 719 assert(CharsPrinted >= 0 && |
705 (size_t)CharsPrinted < llvm::array_lengthof(buf)); | 720 (size_t)CharsPrinted < llvm::array_lengthof(buf)); |
706 (void)CharsPrinted; // avoid warnings if asserts are disabled | 721 (void)CharsPrinted; // avoid warnings if asserts are disabled |
707 Str << "L$" << Ty << "$" << Const->getPoolEntryID() << ":\n"; | 722 Str << "L$" << Ty << "$" << Const->getPoolEntryID() << ":\n"; |
708 Str << "\t" << T::AsmTag << "\t" << buf << "\t# " << T::TypeName << " " | 723 Str << "\t" << T::AsmTag << "\t" << buf << "\t# " << T::TypeName << " " |
709 << Value << "\n"; | 724 << Value << "\n"; |
710 } | 725 } |
711 } | 726 } |
712 | 727 |
728 void TargetX8632::emitVectorConstantPool() const { | |
729 Ostream &Str = Ctx->getStrEmit(); | |
730 Str << "\t.section\t.rodata.cst" << 16 << ",\"aM\",@progbits," << 16 << "\n"; | |
731 Str << "\t.align\t" << 16 << "\n"; | |
732 | |
733 // Emit each (128 bit) vector. | |
734 ConstantList Vectors = Ctx->getConstantPool(IceType_v8i16); | |
735 for (ConstantList::const_iterator I = Vectors.begin(), E = Vectors.end(); | |
736 I != E; ++I) { | |
737 ConstantVector *Vector = llvm::cast<ConstantVector>(*I); | |
738 Vect128 Value = Vector->getValue(); | |
739 assert(Value.size() == 16); | |
740 const char *Data = Value.data(); | |
741 Str << "L$" << typeAsmLabel(Vector->getType()) << "$" | |
742 << Vector->getPoolEntryID() << ":\n"; | |
743 for (unsigned Element = 0; Element != 4; ++Element) { | |
744 uint32_t RawValue; | |
745 memcpy(&RawValue, &Data[4 * Element], 4); | |
746 char buf[30]; | |
747 int CharsPrinted = | |
748 snprintf(buf, llvm::array_lengthof(buf), "0x%x", RawValue); | |
749 assert(CharsPrinted >= 0 && | |
750 (size_t)CharsPrinted < llvm::array_lengthof(buf)); | |
751 Str << "\t" | |
752 << ".long" | |
753 << "\t" << buf << "\t" | |
754 << "\n"; | |
755 } | |
756 } | |
757 | |
758 // Emit each I1 vector expanded to a 128 bit constant. | |
759 ConstantList BitVectors = Ctx->getConstantPool(IceType_v4i1); | |
760 for (ConstantList::const_iterator I = BitVectors.begin(), | |
761 E = BitVectors.end(); | |
762 I != E; ++I) { | |
763 ConstantBitVector *BitVector = llvm::cast<ConstantBitVector>(*I); | |
764 BitVect Value = BitVector->getValue(); | |
765 Str << "L$" << typeAsmLabel(BitVector->getType()) << "$" | |
766 << BitVector->getPoolEntryID() << ":\n"; | |
767 const char *AsmString = NULL; | |
768 switch (BitVector->getType()) { | |
769 default: | |
770 llvm_unreachable("Unknown type"); | |
771 case IceType_v4i1: | |
772 AsmString = ".long"; | |
773 break; | |
774 case IceType_v8i1: | |
775 AsmString = ".short"; | |
776 break; | |
777 case IceType_v16i1: | |
778 AsmString = ".byte"; | |
779 break; | |
780 } | |
781 unsigned NumElements = Value.size(); | |
782 for (unsigned Element = 0; Element != NumElements; ++Element) { | |
783 Str << "\t" << AsmString << "\t" | |
784 << "0x"; | |
785 Str << (Value[Element] ? "1" : "0"); | |
786 Str << "\t" | |
787 << "\n"; | |
788 } | |
789 } | |
790 } | |
791 | |
713 void TargetX8632::emitConstants() const { | 792 void TargetX8632::emitConstants() const { |
714 emitConstantPool<PoolTypeConverter<float> >(); | 793 emitScalarConstantPool<PoolTypeConverter<float> >(); |
715 emitConstantPool<PoolTypeConverter<double> >(); | 794 emitScalarConstantPool<PoolTypeConverter<double> >(); |
795 emitVectorConstantPool(); | |
716 | 796 |
717 // No need to emit constants from the int pool since (for x86) they | 797 // No need to emit constants from the int pool since (for x86) they |
718 // are embedded as immediates in the instructions. | 798 // are embedded as immediates in the instructions. |
719 } | 799 } |
720 | 800 |
721 void TargetX8632::split64(Variable *Var) { | 801 void TargetX8632::split64(Variable *Var) { |
722 switch (Var->getType()) { | 802 switch (Var->getType()) { |
723 default: | 803 default: |
724 return; | 804 return; |
725 case IceType_i64: | 805 case IceType_i64: |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1290 } | 1370 } |
1291 StackOffset += typeWidthInBytesOnStack(Arg->getType()); | 1371 StackOffset += typeWidthInBytesOnStack(Arg->getType()); |
1292 } | 1372 } |
1293 // Generate the call instruction. Assign its result to a temporary | 1373 // Generate the call instruction. Assign its result to a temporary |
1294 // with high register allocation weight. | 1374 // with high register allocation weight. |
1295 Variable *Dest = Instr->getDest(); | 1375 Variable *Dest = Instr->getDest(); |
1296 Variable *eax = NULL; // doubles as RegLo as necessary | 1376 Variable *eax = NULL; // doubles as RegLo as necessary |
1297 Variable *edx = NULL; | 1377 Variable *edx = NULL; |
1298 if (Dest) { | 1378 if (Dest) { |
1299 switch (Dest->getType()) { | 1379 switch (Dest->getType()) { |
1380 case IceType_v4i1: | |
1381 case IceType_v8i1: | |
1382 case IceType_v16i1: | |
1383 case IceType_v16i8: | |
1384 case IceType_v8i16: | |
1385 case IceType_v4i32: | |
1386 case IceType_v4f32: | |
1300 case IceType_NUM: | 1387 case IceType_NUM: |
1301 llvm_unreachable("Invalid Call dest type"); | 1388 llvm_unreachable("Invalid Call dest type"); |
1302 break; | 1389 break; |
1303 case IceType_void: | 1390 case IceType_void: |
1304 break; | 1391 break; |
1305 case IceType_i1: | 1392 case IceType_i1: |
1306 case IceType_i8: | 1393 case IceType_i8: |
1307 case IceType_i16: | 1394 case IceType_i16: |
1308 case IceType_i32: | 1395 case IceType_i32: |
1309 eax = makeReg(Dest->getType(), Reg_eax); | 1396 eax = makeReg(Dest->getType(), Reg_eax); |
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2264 if (Inst->hasRetValue()) { | 2351 if (Inst->hasRetValue()) { |
2265 Operand *Src0 = legalize(Inst->getRetValue()); | 2352 Operand *Src0 = legalize(Inst->getRetValue()); |
2266 if (Src0->getType() == IceType_i64) { | 2353 if (Src0->getType() == IceType_i64) { |
2267 Variable *eax = legalizeToVar(loOperand(Src0), false, Reg_eax); | 2354 Variable *eax = legalizeToVar(loOperand(Src0), false, Reg_eax); |
2268 Variable *edx = legalizeToVar(hiOperand(Src0), false, Reg_edx); | 2355 Variable *edx = legalizeToVar(hiOperand(Src0), false, Reg_edx); |
2269 Reg = eax; | 2356 Reg = eax; |
2270 Context.insert(InstFakeUse::create(Func, edx)); | 2357 Context.insert(InstFakeUse::create(Func, edx)); |
2271 } else if (Src0->getType() == IceType_f32 || | 2358 } else if (Src0->getType() == IceType_f32 || |
2272 Src0->getType() == IceType_f64) { | 2359 Src0->getType() == IceType_f64) { |
2273 _fld(Src0); | 2360 _fld(Src0); |
2361 } else if (typeNumElements(Src0->getType()) > 1) { | |
2362 _mov(Reg, Src0, Reg_xmm0); | |
jvoung (off chromium)
2014/06/26 00:45:41
See question about reusing _mov or having a separa
| |
2274 } else { | 2363 } else { |
2275 _mov(Reg, Src0, Reg_eax); | 2364 _mov(Reg, Src0, Reg_eax); |
2276 } | 2365 } |
2277 } | 2366 } |
2278 _ret(Reg); | 2367 _ret(Reg); |
2279 // Add a fake use of esp to make sure esp stays alive for the entire | 2368 // Add a fake use of esp to make sure esp stays alive for the entire |
2280 // function. Otherwise post-call esp adjustments get dead-code | 2369 // function. Otherwise post-call esp adjustments get dead-code |
2281 // eliminated. TODO: Are there more places where the fake use | 2370 // eliminated. TODO: Are there more places where the fake use |
2282 // should be inserted? E.g. "void f(int n){while(1) g(n);}" may not | 2371 // should be inserted? E.g. "void f(int n){while(1) g(n);}" may not |
2283 // have a ret instruction. | 2372 // have a ret instruction. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2440 // values to uninitialized registers, a FakeDef will be needed: | 2529 // values to uninitialized registers, a FakeDef will be needed: |
2441 // Context.insert(InstFakeDef::create(Func, Reg)); | 2530 // Context.insert(InstFakeDef::create(Func, Reg)); |
2442 // This is in order to ensure that the live range of Reg is not | 2531 // This is in order to ensure that the live range of Reg is not |
2443 // overestimated. If the constant being lowered is a 64 bit value, | 2532 // overestimated. If the constant being lowered is a 64 bit value, |
2444 // then the result should be split and the lo and hi components will | 2533 // then the result should be split and the lo and hi components will |
2445 // need to go in uninitialized registers. | 2534 // need to go in uninitialized registers. |
2446 From = Ctx->getConstantZero(From->getType()); | 2535 From = Ctx->getConstantZero(From->getType()); |
2447 } | 2536 } |
2448 bool NeedsReg = | 2537 bool NeedsReg = |
2449 !(Allowed & Legal_Imm) || | 2538 !(Allowed & Legal_Imm) || |
2450 // ConstantFloat and ConstantDouble are actually memory operands. | 2539 // ConstantFloat, ConstantDouble, and vector constants are |
2540 // actually memory operands. | |
2451 (!(Allowed & Legal_Mem) && | 2541 (!(Allowed & Legal_Mem) && |
2452 (From->getType() == IceType_f32 || From->getType() == IceType_f64)); | 2542 (From->getType() == IceType_f32 || From->getType() == IceType_f64 || |
2543 typeNumElements(From->getType()) > 1)); | |
2453 if (NeedsReg) { | 2544 if (NeedsReg) { |
2454 Variable *Reg = makeReg(From->getType(), RegNum); | 2545 Variable *Reg = makeReg(From->getType(), RegNum); |
2455 _mov(Reg, From); | 2546 _mov(Reg, From); |
2456 From = Reg; | 2547 From = Reg; |
2457 } | 2548 } |
2458 return From; | 2549 return From; |
2459 } | 2550 } |
2460 if (Variable *Var = llvm::dyn_cast<Variable>(From)) { | 2551 if (Variable *Var = llvm::dyn_cast<Variable>(From)) { |
2461 // We need a new physical register for the operand if: | 2552 // We need a new physical register for the operand if: |
2462 // Mem is not allowed and Var->getRegNum() is unknown, or | 2553 // Mem is not allowed and Var->getRegNum() is unknown, or |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2562 } | 2653 } |
2563 assert(AvailableTypedRegisters.any()); | 2654 assert(AvailableTypedRegisters.any()); |
2564 int32_t RegNum = AvailableTypedRegisters.find_first(); | 2655 int32_t RegNum = AvailableTypedRegisters.find_first(); |
2565 Var->setRegNum(RegNum); | 2656 Var->setRegNum(RegNum); |
2566 AvailableRegisters[RegNum] = false; | 2657 AvailableRegisters[RegNum] = false; |
2567 } | 2658 } |
2568 } | 2659 } |
2569 } | 2660 } |
2570 } | 2661 } |
2571 | 2662 |
2663 template <> void ConstantInteger::emit(GlobalContext *Ctx) const { | |
2664 Ostream &Str = Ctx->getStrEmit(); | |
2665 Str << getValue(); | |
2666 } | |
2667 | |
2572 template <> void ConstantFloat::emit(GlobalContext *Ctx) const { | 2668 template <> void ConstantFloat::emit(GlobalContext *Ctx) const { |
2573 Ostream &Str = Ctx->getStrEmit(); | 2669 Ostream &Str = Ctx->getStrEmit(); |
2574 // It would be better to prefix with ".L$" instead of "L$", but | 2670 // It would be better to prefix with ".L$" instead of "L$", but |
2575 // llvm-mc doesn't parse "dword ptr [.L$foo]". | 2671 // llvm-mc doesn't parse "dword ptr [.L$foo]". |
2576 Str << "dword ptr [L$" << IceType_f32 << "$" << getPoolEntryID() << "]"; | 2672 Str << "dword ptr [L$" << IceType_f32 << "$" << getPoolEntryID() << "]"; |
2577 } | 2673 } |
2578 | 2674 |
2579 template <> void ConstantDouble::emit(GlobalContext *Ctx) const { | 2675 template <> void ConstantDouble::emit(GlobalContext *Ctx) const { |
2580 Ostream &Str = Ctx->getStrEmit(); | 2676 Ostream &Str = Ctx->getStrEmit(); |
2581 Str << "qword ptr [L$" << IceType_f64 << "$" << getPoolEntryID() << "]"; | 2677 Str << "qword ptr [L$" << IceType_f64 << "$" << getPoolEntryID() << "]"; |
2582 } | 2678 } |
2583 | 2679 |
2680 template <> void ConstantVector::emit(GlobalContext *Ctx) const { | |
2681 Ostream &Str = Ctx->getStrEmit(); | |
2682 Str << "xmmword ptr [L$" << typeAsmLabel(getType()) << "$" << getPoolEntryID() | |
2683 << "]"; | |
2684 } | |
2685 | |
2686 template <> void ConstantBitVector::emit(GlobalContext *Ctx) const { | |
2687 Ostream &Str = Ctx->getStrEmit(); | |
2688 Str << "xmmword ptr [L$" << typeAsmLabel(getType()) << "$" << getPoolEntryID() | |
2689 << "]"; | |
2690 } | |
2691 | |
2584 } // end of namespace Ice | 2692 } // end of namespace Ice |
OLD | NEW |