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

Side by Side Diff: src/IceTargetLoweringX8632.cpp

Issue 342763004: Add atomic load/store, fetch_add, fence, and is-lock-free lowering. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: change comment Created 6 years, 5 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/IceTargetLoweringX8632.h ('k') | src/llvm2ice.cpp » ('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 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 if (Arg->hasReg()) { 424 if (Arg->hasReg()) {
425 assert(Ty != IceType_i64); 425 assert(Ty != IceType_i64);
426 OperandX8632Mem *Mem = OperandX8632Mem::create( 426 OperandX8632Mem *Mem = OperandX8632Mem::create(
427 Func, Ty, FramePtr, 427 Func, Ty, FramePtr,
428 Ctx->getConstantInt(IceType_i32, Arg->getStackOffset())); 428 Ctx->getConstantInt(IceType_i32, Arg->getStackOffset()));
429 _mov(Arg, Mem); 429 _mov(Arg, Mem);
430 } 430 }
431 InArgsSizeBytes += typeWidthInBytesOnStack(Ty); 431 InArgsSizeBytes += typeWidthInBytesOnStack(Ty);
432 } 432 }
433 433
434 // static
435 Type TargetX8632::stackSlotType() { return IceType_i32; } 434 Type TargetX8632::stackSlotType() { return IceType_i32; }
436 435
437 void TargetX8632::addProlog(CfgNode *Node) { 436 void TargetX8632::addProlog(CfgNode *Node) {
438 // If SimpleCoalescing is false, each variable without a register 437 // If SimpleCoalescing is false, each variable without a register
439 // gets its own unique stack slot, which leads to large stack 438 // gets its own unique stack slot, which leads to large stack
440 // frames. If SimpleCoalescing is true, then each "global" variable 439 // frames. If SimpleCoalescing is true, then each "global" variable
441 // without a register gets its own slot, but "local" variable slots 440 // without a register gets its own slot, but "local" variable slots
442 // are reused across basic blocks. E.g., if A and B are local to 441 // are reused across basic blocks. E.g., if A and B are local to
443 // block 1 and C is local to block 2, then C may share a slot with A 442 // block 1 and C is local to block 2, then C may share a slot with A
444 // or B. 443 // or B.
(...skipping 1163 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 assert(Src0RM->getType() == IceType_f64); 1607 assert(Src0RM->getType() == IceType_f64);
1609 // a.i64 = bitcast b.f64 ==> 1608 // a.i64 = bitcast b.f64 ==>
1610 // s.f64 = spill b.f64 1609 // s.f64 = spill b.f64
1611 // t_lo.i32 = lo(s.f64) 1610 // t_lo.i32 = lo(s.f64)
1612 // a_lo.i32 = t_lo.i32 1611 // a_lo.i32 = t_lo.i32
1613 // t_hi.i32 = hi(s.f64) 1612 // t_hi.i32 = hi(s.f64)
1614 // a_hi.i32 = t_hi.i32 1613 // a_hi.i32 = t_hi.i32
1615 Variable *Spill = Func->makeVariable(IceType_f64, Context.getNode()); 1614 Variable *Spill = Func->makeVariable(IceType_f64, Context.getNode());
1616 Spill->setWeight(RegWeight::Zero); 1615 Spill->setWeight(RegWeight::Zero);
1617 Spill->setPreferredRegister(llvm::dyn_cast<Variable>(Src0RM), true); 1616 Spill->setPreferredRegister(llvm::dyn_cast<Variable>(Src0RM), true);
1618 _mov(Spill, Src0RM); 1617 _movq(Spill, Src0RM);
1619 1618
1620 Variable *DestLo = llvm::cast<Variable>(loOperand(Dest)); 1619 Variable *DestLo = llvm::cast<Variable>(loOperand(Dest));
1621 Variable *DestHi = llvm::cast<Variable>(hiOperand(Dest)); 1620 Variable *DestHi = llvm::cast<Variable>(hiOperand(Dest));
1622 Variable *T_Lo = makeReg(IceType_i32); 1621 Variable *T_Lo = makeReg(IceType_i32);
1623 Variable *T_Hi = makeReg(IceType_i32); 1622 Variable *T_Hi = makeReg(IceType_i32);
1624 VariableSplit *SpillLo = 1623 VariableSplit *SpillLo =
1625 VariableSplit::create(Func, Spill, VariableSplit::Low); 1624 VariableSplit::create(Func, Spill, VariableSplit::Low);
1626 VariableSplit *SpillHi = 1625 VariableSplit *SpillHi =
1627 VariableSplit::create(Func, Spill, VariableSplit::High); 1626 VariableSplit::create(Func, Spill, VariableSplit::High);
1628 1627
(...skipping 22 matching lines...) Expand all
1651 VariableSplit *SpillHi = 1650 VariableSplit *SpillHi =
1652 VariableSplit::create(Func, Spill, VariableSplit::High); 1651 VariableSplit::create(Func, Spill, VariableSplit::High);
1653 _mov(T_Lo, loOperand(Src0)); 1652 _mov(T_Lo, loOperand(Src0));
1654 // Technically, the Spill is defined after the _store happens, but 1653 // Technically, the Spill is defined after the _store happens, but
1655 // SpillLo is considered a "use" of Spill so define Spill before it 1654 // SpillLo is considered a "use" of Spill so define Spill before it
1656 // is used. 1655 // is used.
1657 Context.insert(InstFakeDef::create(Func, Spill)); 1656 Context.insert(InstFakeDef::create(Func, Spill));
1658 _store(T_Lo, SpillLo); 1657 _store(T_Lo, SpillLo);
1659 _mov(T_Hi, hiOperand(Src0)); 1658 _mov(T_Hi, hiOperand(Src0));
1660 _store(T_Hi, SpillHi); 1659 _store(T_Hi, SpillHi);
1661 _mov(Dest, Spill); 1660 _movq(Dest, Spill);
1662 } break; 1661 } break;
1663 } 1662 }
1664 break; 1663 break;
1665 } 1664 }
1666 } 1665 }
1667 } 1666 }
1668 1667
1669 void TargetX8632::lowerFcmp(const InstFcmp *Inst) { 1668 void TargetX8632::lowerFcmp(const InstFcmp *Inst) {
1670 Operand *Src0 = Inst->getSrc(0); 1669 Operand *Src0 = Inst->getSrc(0);
1671 Operand *Src1 = Inst->getSrc(1); 1670 Operand *Src1 = Inst->getSrc(1);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1793 _mov(Dest, One); 1792 _mov(Dest, One);
1794 _br(getIcmp32Mapping(Inst->getCondition()), Label); 1793 _br(getIcmp32Mapping(Inst->getCondition()), Label);
1795 Context.insert(InstFakeUse::create(Func, Dest)); 1794 Context.insert(InstFakeUse::create(Func, Dest));
1796 _mov(Dest, Zero); 1795 _mov(Dest, Zero);
1797 Context.insert(Label); 1796 Context.insert(Label);
1798 } 1797 }
1799 1798
1800 void TargetX8632::lowerIntrinsicCall(const InstIntrinsicCall *Instr) { 1799 void TargetX8632::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
1801 switch (Instr->getIntrinsicInfo().ID) { 1800 switch (Instr->getIntrinsicInfo().ID) {
1802 case Intrinsics::AtomicCmpxchg: 1801 case Intrinsics::AtomicCmpxchg:
1802 if (!Intrinsics::VerifyMemoryOrder(
1803 llvm::cast<ConstantInteger>(Instr->getArg(3))->getValue())) {
1804 Func->setError("Unexpected memory ordering (success) for AtomicCmpxchg");
1805 return;
1806 }
1807 if (!Intrinsics::VerifyMemoryOrder(
1808 llvm::cast<ConstantInteger>(Instr->getArg(4))->getValue())) {
1809 Func->setError("Unexpected memory ordering (failure) for AtomicCmpxchg");
1810 return;
1811 }
1812 // TODO(jvoung): fill it in.
1813 Func->setError("Unhandled intrinsic");
1814 return;
1803 case Intrinsics::AtomicFence: 1815 case Intrinsics::AtomicFence:
1816 if (!Intrinsics::VerifyMemoryOrder(
1817 llvm::cast<ConstantInteger>(Instr->getArg(0))->getValue())) {
1818 Func->setError("Unexpected memory ordering for AtomicFence");
1819 return;
1820 }
1821 _mfence();
1822 return;
1804 case Intrinsics::AtomicFenceAll: 1823 case Intrinsics::AtomicFenceAll:
1805 case Intrinsics::AtomicIsLockFree: 1824 // NOTE: FenceAll should prevent and load/store from being moved
1806 case Intrinsics::AtomicLoad: 1825 // across the fence (both atomic and non-atomic). The InstX8632Mfence
1826 // instruction is currently marked coarsely as "HasSideEffects".
1827 _mfence();
1828 return;
1829 case Intrinsics::AtomicIsLockFree: {
1830 // X86 is always lock free for 8/16/32/64 bit accesses.
1831 // TODO(jvoung): Since the result is constant when given a constant
1832 // byte size, this opens up DCE opportunities.
1833 Operand *ByteSize = Instr->getArg(0);
1834 Variable *Dest = Instr->getDest();
1835 if (ConstantInteger *CI = llvm::dyn_cast<ConstantInteger>(ByteSize)) {
1836 Constant *Result;
1837 switch (CI->getValue()) {
1838 default:
1839 // Some x86-64 processors support the cmpxchg16b intruction, which
1840 // can make 16-byte operations lock free (when used with the LOCK
1841 // prefix). However, that's not supported in 32-bit mode, so just
1842 // return 0 even for large sizes.
1843 Result = Ctx->getConstantZero(IceType_i32);
1844 break;
1845 case 1:
1846 case 2:
1847 case 4:
1848 case 8:
1849 Result = Ctx->getConstantInt(IceType_i32, 1);
1850 break;
1851 }
1852 _mov(Dest, Result);
1853 return;
1854 }
1855 // The PNaCl ABI requires the byte size to be a compile-time constant.
1856 Func->setError("AtomicIsLockFree byte size should be compile-time const");
1857 return;
1858 }
1859 case Intrinsics::AtomicLoad: {
1860 // We require the memory address to be naturally aligned.
1861 // Given that is the case, then normal loads are atomic.
1862 if (!Intrinsics::VerifyMemoryOrder(
1863 llvm::cast<ConstantInteger>(Instr->getArg(1))->getValue())) {
1864 Func->setError("Unexpected memory ordering for AtomicLoad");
1865 return;
1866 }
1867 Variable *Dest = Instr->getDest();
1868 if (Dest->getType() == IceType_i64) {
1869 // Follow what GCC does and use a movq instead of what lowerLoad()
1870 // normally does (split the load into two).
1871 // Thus, this skips load/arithmetic op folding. Load/arithmetic folding
1872 // can't happen anyway, since this is x86-32 and integer arithmetic only
1873 // happens on 32-bit quantities.
1874 Variable *T = makeReg(IceType_f64);
1875 OperandX8632Mem *Addr = FormMemoryOperand(Instr->getArg(0), IceType_f64);
1876 _movq(T, Addr);
1877 // Then cast the bits back out of the XMM register to the i64 Dest.
1878 InstCast *Cast = InstCast::create(Func, InstCast::Bitcast, Dest, T);
1879 lowerCast(Cast);
1880 // Make sure that the atomic load isn't elided.
1881 Context.insert(InstFakeUse::create(Func, Dest->getLo()));
1882 Context.insert(InstFakeUse::create(Func, Dest->getHi()));
1883 return;
1884 }
1885 InstLoad *Load = InstLoad::create(Func, Dest, Instr->getArg(0));
1886 lowerLoad(Load);
1887 // Make sure the atomic load isn't elided.
1888 Context.insert(InstFakeUse::create(Func, Dest));
1889 return;
1890 }
1807 case Intrinsics::AtomicRMW: 1891 case Intrinsics::AtomicRMW:
1808 case Intrinsics::AtomicStore: 1892 if (!Intrinsics::VerifyMemoryOrder(
1893 llvm::cast<ConstantInteger>(Instr->getArg(3))->getValue())) {
1894 Func->setError("Unexpected memory ordering for AtomicRMW");
1895 return;
1896 }
1897 lowerAtomicRMW(Instr->getDest(),
1898 static_cast<uint32_t>(llvm::cast<ConstantInteger>(
1899 Instr->getArg(0))->getValue()),
1900 Instr->getArg(1), Instr->getArg(2));
1901 return;
1902 case Intrinsics::AtomicStore: {
1903 if (!Intrinsics::VerifyMemoryOrder(
1904 llvm::cast<ConstantInteger>(Instr->getArg(2))->getValue())) {
1905 Func->setError("Unexpected memory ordering for AtomicStore");
1906 return;
1907 }
1908 // We require the memory address to be naturally aligned.
1909 // Given that is the case, then normal stores are atomic.
1910 // Add a fence after the store to make it visible.
1911 Operand *Value = Instr->getArg(0);
1912 Operand *Ptr = Instr->getArg(1);
1913 if (Value->getType() == IceType_i64) {
1914 // Use a movq instead of what lowerStore() normally does
1915 // (split the store into two), following what GCC does.
1916 // Cast the bits from int -> to an xmm register first.
1917 Variable *T = makeReg(IceType_f64);
1918 InstCast *Cast = InstCast::create(Func, InstCast::Bitcast, T, Value);
1919 lowerCast(Cast);
1920 // Then store XMM w/ a movq.
1921 OperandX8632Mem *Addr = FormMemoryOperand(Ptr, IceType_f64);
1922 _storeq(T, Addr);
1923 _mfence();
1924 return;
1925 }
1926 InstStore *Store = InstStore::create(Func, Value, Ptr);
1927 lowerStore(Store);
1928 _mfence();
1929 return;
1930 }
1809 case Intrinsics::Bswap: 1931 case Intrinsics::Bswap:
1810 case Intrinsics::Ctlz: 1932 case Intrinsics::Ctlz:
1811 case Intrinsics::Ctpop: 1933 case Intrinsics::Ctpop:
1812 case Intrinsics::Cttz: 1934 case Intrinsics::Cttz:
1935 // TODO(jvoung): fill it in.
1813 Func->setError("Unhandled intrinsic"); 1936 Func->setError("Unhandled intrinsic");
1814 return; 1937 return;
1815 case Intrinsics::Longjmp: { 1938 case Intrinsics::Longjmp: {
1816 InstCall *Call = makeHelperCall("longjmp", NULL, 2); 1939 InstCall *Call = makeHelperCall("longjmp", NULL, 2);
1817 Call->addArg(Instr->getArg(0)); 1940 Call->addArg(Instr->getArg(0));
1818 Call->addArg(Instr->getArg(1)); 1941 Call->addArg(Instr->getArg(1));
1819 lowerCall(Call); 1942 lowerCall(Call);
1820 break; 1943 return;
1821 } 1944 }
1822 case Intrinsics::Memcpy: { 1945 case Intrinsics::Memcpy: {
1823 // In the future, we could potentially emit an inline memcpy/memset, etc. 1946 // In the future, we could potentially emit an inline memcpy/memset, etc.
1824 // for intrinsic calls w/ a known length. 1947 // for intrinsic calls w/ a known length.
1825 InstCall *Call = makeHelperCall("memcpy", NULL, 3); 1948 InstCall *Call = makeHelperCall("memcpy", NULL, 3);
1826 Call->addArg(Instr->getArg(0)); 1949 Call->addArg(Instr->getArg(0));
1827 Call->addArg(Instr->getArg(1)); 1950 Call->addArg(Instr->getArg(1));
1828 Call->addArg(Instr->getArg(2)); 1951 Call->addArg(Instr->getArg(2));
1829 lowerCall(Call); 1952 lowerCall(Call);
1830 break; 1953 return;
1831 } 1954 }
1832 case Intrinsics::Memmove: { 1955 case Intrinsics::Memmove: {
1833 InstCall *Call = makeHelperCall("memmove", NULL, 3); 1956 InstCall *Call = makeHelperCall("memmove", NULL, 3);
1834 Call->addArg(Instr->getArg(0)); 1957 Call->addArg(Instr->getArg(0));
1835 Call->addArg(Instr->getArg(1)); 1958 Call->addArg(Instr->getArg(1));
1836 Call->addArg(Instr->getArg(2)); 1959 Call->addArg(Instr->getArg(2));
1837 lowerCall(Call); 1960 lowerCall(Call);
1838 break; 1961 return;
1839 } 1962 }
1840 case Intrinsics::Memset: { 1963 case Intrinsics::Memset: {
1841 // The value operand needs to be extended to a stack slot size 1964 // The value operand needs to be extended to a stack slot size
1842 // because we "push" only works for a specific operand size. 1965 // because we "push" only works for a specific operand size.
1843 Operand *ValOp = Instr->getArg(1); 1966 Operand *ValOp = Instr->getArg(1);
1844 assert(ValOp->getType() == IceType_i8); 1967 assert(ValOp->getType() == IceType_i8);
1845 Variable *ValExt = makeReg(stackSlotType()); 1968 Variable *ValExt = makeReg(stackSlotType());
1846 _movzx(ValExt, ValOp); 1969 _movzx(ValExt, ValOp);
1847 InstCall *Call = makeHelperCall("memset", NULL, 3); 1970 InstCall *Call = makeHelperCall("memset", NULL, 3);
1848 Call->addArg(Instr->getArg(0)); 1971 Call->addArg(Instr->getArg(0));
1849 Call->addArg(ValExt); 1972 Call->addArg(ValExt);
1850 Call->addArg(Instr->getArg(2)); 1973 Call->addArg(Instr->getArg(2));
1851 lowerCall(Call); 1974 lowerCall(Call);
1852 break; 1975 return;
1853 } 1976 }
1854 case Intrinsics::NaClReadTP: { 1977 case Intrinsics::NaClReadTP: {
1855 Constant *Zero = Ctx->getConstantInt(IceType_i32, 0); 1978 Constant *Zero = Ctx->getConstantZero(IceType_i32);
1856 Operand *Src = OperandX8632Mem::create(Func, IceType_i32, NULL, Zero, NULL, 1979 Operand *Src = OperandX8632Mem::create(Func, IceType_i32, NULL, Zero, NULL,
1857 0, OperandX8632Mem::SegReg_GS); 1980 0, OperandX8632Mem::SegReg_GS);
1858 Variable *Dest = Instr->getDest(); 1981 Variable *Dest = Instr->getDest();
1859 Variable *T = NULL; 1982 Variable *T = NULL;
1860 _mov(T, Src); 1983 _mov(T, Src);
1861 _mov(Dest, T); 1984 _mov(Dest, T);
1862 break; 1985 return;
1863 } 1986 }
1864 case Intrinsics::Setjmp: { 1987 case Intrinsics::Setjmp: {
1865 InstCall *Call = makeHelperCall("setjmp", Instr->getDest(), 1); 1988 InstCall *Call = makeHelperCall("setjmp", Instr->getDest(), 1);
1866 Call->addArg(Instr->getArg(0)); 1989 Call->addArg(Instr->getArg(0));
1867 lowerCall(Call); 1990 lowerCall(Call);
1868 break; 1991 return;
1869 } 1992 }
1870 case Intrinsics::Sqrt: 1993 case Intrinsics::Sqrt:
1871 case Intrinsics::Stacksave: 1994 case Intrinsics::Stacksave:
1872 case Intrinsics::Stackrestore: 1995 case Intrinsics::Stackrestore:
1996 // TODO(jvoung): fill it in.
1873 Func->setError("Unhandled intrinsic"); 1997 Func->setError("Unhandled intrinsic");
1874 return; 1998 return;
1875 case Intrinsics::Trap: 1999 case Intrinsics::Trap:
1876 _ud2(); 2000 _ud2();
1877 break; 2001 return;
1878 case Intrinsics::UnknownIntrinsic: 2002 case Intrinsics::UnknownIntrinsic:
1879 Func->setError("Should not be lowering UnknownIntrinsic"); 2003 Func->setError("Should not be lowering UnknownIntrinsic");
1880 return; 2004 return;
1881 } 2005 }
1882 return; 2006 return;
1883 } 2007 }
1884 2008
2009 void TargetX8632::lowerAtomicRMW(Variable *Dest, uint32_t Operation,
2010 Operand *Ptr, Operand *Val) {
2011 switch (Operation) {
2012 default:
2013 Func->setError("Unknown AtomicRMW operation");
2014 return;
2015 case Intrinsics::AtomicAdd: {
2016 if (Dest->getType() == IceType_i64) {
2017 // Do a nasty cmpxchg8b loop. Factor this into a function.
2018 // TODO(jvoung): fill it in.
2019 Func->setError("Unhandled AtomicRMW operation");
2020 return;
2021 }
2022 OperandX8632Mem *Addr = FormMemoryOperand(Ptr, Dest->getType());
2023 const bool Locked = true;
2024 Variable *T = NULL;
2025 _mov(T, Val);
2026 _xadd(Addr, T, Locked);
2027 _mov(Dest, T);
2028 return;
2029 }
2030 case Intrinsics::AtomicSub: {
2031 if (Dest->getType() == IceType_i64) {
2032 // Do a nasty cmpxchg8b loop.
2033 // TODO(jvoung): fill it in.
2034 Func->setError("Unhandled AtomicRMW operation");
2035 return;
2036 }
2037 // Generate a memory operand from Ptr.
2038 // neg...
2039 // Then do the same as AtomicAdd.
2040 // TODO(jvoung): fill it in.
2041 Func->setError("Unhandled AtomicRMW operation");
2042 return;
2043 }
2044 case Intrinsics::AtomicOr:
2045 case Intrinsics::AtomicAnd:
2046 case Intrinsics::AtomicXor:
2047 case Intrinsics::AtomicExchange:
2048 // TODO(jvoung): fill it in.
2049 Func->setError("Unhandled AtomicRMW operation");
2050 return;
2051 }
2052 }
2053
1885 namespace { 2054 namespace {
1886 2055
1887 bool isAdd(const Inst *Inst) { 2056 bool isAdd(const Inst *Inst) {
1888 if (const InstArithmetic *Arith = 2057 if (const InstArithmetic *Arith =
1889 llvm::dyn_cast_or_null<const InstArithmetic>(Inst)) { 2058 llvm::dyn_cast_or_null<const InstArithmetic>(Inst)) {
1890 return (Arith->getOp() == InstArithmetic::Add); 2059 return (Arith->getOp() == InstArithmetic::Add);
1891 } 2060 }
1892 return false; 2061 return false;
1893 } 2062 }
1894 2063
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2011 2180
2012 } // anonymous namespace 2181 } // anonymous namespace
2013 2182
2014 void TargetX8632::lowerLoad(const InstLoad *Inst) { 2183 void TargetX8632::lowerLoad(const InstLoad *Inst) {
2015 // A Load instruction can be treated the same as an Assign 2184 // A Load instruction can be treated the same as an Assign
2016 // instruction, after the source operand is transformed into an 2185 // instruction, after the source operand is transformed into an
2017 // OperandX8632Mem operand. Note that the address mode 2186 // OperandX8632Mem operand. Note that the address mode
2018 // optimization already creates an OperandX8632Mem operand, so it 2187 // optimization already creates an OperandX8632Mem operand, so it
2019 // doesn't need another level of transformation. 2188 // doesn't need another level of transformation.
2020 Type Ty = Inst->getDest()->getType(); 2189 Type Ty = Inst->getDest()->getType();
2021 Operand *Src0 = Inst->getSourceAddress(); 2190 Operand *Src0 = FormMemoryOperand(Inst->getSourceAddress(), Ty);
2022 // Address mode optimization already creates an OperandX8632Mem
2023 // operand, so it doesn't need another level of transformation.
2024 if (!llvm::isa<OperandX8632Mem>(Src0)) {
2025 Variable *Base = llvm::dyn_cast<Variable>(Src0);
2026 Constant *Offset = llvm::dyn_cast<Constant>(Src0);
2027 assert(Base || Offset);
2028 Src0 = OperandX8632Mem::create(Func, Ty, Base, Offset);
2029 }
2030 2191
2031 // Fuse this load with a subsequent Arithmetic instruction in the 2192 // Fuse this load with a subsequent Arithmetic instruction in the
2032 // following situations: 2193 // following situations:
2033 // a=[mem]; c=b+a ==> c=b+[mem] if last use of a and a not in b 2194 // a=[mem]; c=b+a ==> c=b+[mem] if last use of a and a not in b
2034 // a=[mem]; c=a+b ==> c=b+[mem] if commutative and above is true 2195 // a=[mem]; c=a+b ==> c=b+[mem] if commutative and above is true
2035 // 2196 //
2036 // TODO: Clean up and test thoroughly. 2197 // TODO: Clean up and test thoroughly.
2198 // (E.g., if there is an mfence-all make sure the load ends up on the
2199 // same side of the fence).
2037 // 2200 //
2038 // TODO: Why limit to Arithmetic instructions? This could probably be 2201 // TODO: Why limit to Arithmetic instructions? This could probably be
2039 // applied to most any instruction type. Look at all source operands 2202 // applied to most any instruction type. Look at all source operands
2040 // in the following instruction, and if there is one instance of the 2203 // in the following instruction, and if there is one instance of the
2041 // load instruction's dest variable, and that instruction ends that 2204 // load instruction's dest variable, and that instruction ends that
2042 // variable's live range, then make the substitution. Deal with 2205 // variable's live range, then make the substitution. Deal with
2043 // commutativity optimization in the arithmetic instruction lowering. 2206 // commutativity optimization in the arithmetic instruction lowering.
2044 InstArithmetic *NewArith = NULL; 2207 InstArithmetic *NewArith = NULL;
2045 if (InstArithmetic *Arith = 2208 if (InstArithmetic *Arith =
2046 llvm::dyn_cast_or_null<InstArithmetic>(Context.getNextInst())) { 2209 llvm::dyn_cast_or_null<InstArithmetic>(Context.getNextInst())) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
2157 SrcF = legalize(SrcF, Legal_Reg | Legal_Imm, true); 2320 SrcF = legalize(SrcF, Legal_Reg | Legal_Imm, true);
2158 _mov(Dest, SrcF); 2321 _mov(Dest, SrcF);
2159 } 2322 }
2160 2323
2161 Context.insert(Label); 2324 Context.insert(Label);
2162 } 2325 }
2163 2326
2164 void TargetX8632::lowerStore(const InstStore *Inst) { 2327 void TargetX8632::lowerStore(const InstStore *Inst) {
2165 Operand *Value = Inst->getData(); 2328 Operand *Value = Inst->getData();
2166 Operand *Addr = Inst->getAddr(); 2329 Operand *Addr = Inst->getAddr();
2167 OperandX8632Mem *NewAddr = llvm::dyn_cast<OperandX8632Mem>(Addr); 2330 OperandX8632Mem *NewAddr = FormMemoryOperand(Addr, Value->getType());
2168 // Address mode optimization already creates an OperandX8632Mem
2169 // operand, so it doesn't need another level of transformation.
2170 if (!NewAddr) {
2171 // The address will be either a constant (which represents a global
2172 // variable) or a variable, so either the Base or Offset component
2173 // of the OperandX8632Mem will be set.
2174 Variable *Base = llvm::dyn_cast<Variable>(Addr);
2175 Constant *Offset = llvm::dyn_cast<Constant>(Addr);
2176 assert(Base || Offset);
2177 NewAddr = OperandX8632Mem::create(Func, Value->getType(), Base, Offset);
2178 }
2179 NewAddr = llvm::cast<OperandX8632Mem>(legalize(NewAddr));
2180 2331
2181 if (NewAddr->getType() == IceType_i64) { 2332 if (NewAddr->getType() == IceType_i64) {
2182 Value = legalize(Value); 2333 Value = legalize(Value);
2183 Operand *ValueHi = legalize(hiOperand(Value), Legal_Reg | Legal_Imm, true); 2334 Operand *ValueHi = legalize(hiOperand(Value), Legal_Reg | Legal_Imm, true);
2184 Operand *ValueLo = legalize(loOperand(Value), Legal_Reg | Legal_Imm, true); 2335 Operand *ValueLo = legalize(loOperand(Value), Legal_Reg | Legal_Imm, true);
2185 _store(ValueHi, llvm::cast<OperandX8632Mem>(hiOperand(NewAddr))); 2336 _store(ValueHi, llvm::cast<OperandX8632Mem>(hiOperand(NewAddr)));
2186 _store(ValueLo, llvm::cast<OperandX8632Mem>(loOperand(NewAddr))); 2337 _store(ValueLo, llvm::cast<OperandX8632Mem>(loOperand(NewAddr)));
2187 } else { 2338 } else {
2188 Value = legalize(Value, Legal_Reg | Legal_Imm, true); 2339 Value = legalize(Value, Legal_Reg | Legal_Imm, true);
2189 _store(Value, NewAddr); 2340 _store(Value, NewAddr);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
2287 // 2438 //
2288 // If in the future the implementation is changed to lower undef 2439 // If in the future the implementation is changed to lower undef
2289 // values to uninitialized registers, a FakeDef will be needed: 2440 // values to uninitialized registers, a FakeDef will be needed:
2290 // Context.insert(InstFakeDef::create(Func, Reg)); 2441 // Context.insert(InstFakeDef::create(Func, Reg));
2291 // This is in order to ensure that the live range of Reg is not 2442 // This is in order to ensure that the live range of Reg is not
2292 // overestimated. If the constant being lowered is a 64 bit value, 2443 // overestimated. If the constant being lowered is a 64 bit value,
2293 // then the result should be split and the lo and hi components will 2444 // then the result should be split and the lo and hi components will
2294 // need to go in uninitialized registers. 2445 // need to go in uninitialized registers.
2295 From = Ctx->getConstantZero(From->getType()); 2446 From = Ctx->getConstantZero(From->getType());
2296 } 2447 }
2297 bool NeedsReg = !(Allowed & Legal_Imm) || 2448 bool NeedsReg =
2449 !(Allowed & Legal_Imm) ||
2298 // ConstantFloat and ConstantDouble are actually memory operands. 2450 // ConstantFloat and ConstantDouble are actually memory operands.
2299 (!(Allowed & Legal_Mem) && (From->getType() == IceType_f32 || 2451 (!(Allowed & Legal_Mem) &&
2300 From->getType() == IceType_f64)); 2452 (From->getType() == IceType_f32 || From->getType() == IceType_f64));
2301 if (NeedsReg) { 2453 if (NeedsReg) {
2302 Variable *Reg = makeReg(From->getType(), RegNum); 2454 Variable *Reg = makeReg(From->getType(), RegNum);
2303 _mov(Reg, From); 2455 _mov(Reg, From);
2304 From = Reg; 2456 From = Reg;
2305 } 2457 }
2306 return From; 2458 return From;
2307 } 2459 }
2308 if (Variable *Var = llvm::dyn_cast<Variable>(From)) { 2460 if (Variable *Var = llvm::dyn_cast<Variable>(From)) {
2309 // We need a new physical register for the operand if: 2461 // We need a new physical register for the operand if:
2310 // Mem is not allowed and Var->getRegNum() is unknown, or 2462 // Mem is not allowed and Var->getRegNum() is unknown, or
(...skipping 12 matching lines...) Expand all
2323 llvm_unreachable("Unhandled operand kind in legalize()"); 2475 llvm_unreachable("Unhandled operand kind in legalize()");
2324 return From; 2476 return From;
2325 } 2477 }
2326 2478
2327 // Provide a trivial wrapper to legalize() for this common usage. 2479 // Provide a trivial wrapper to legalize() for this common usage.
2328 Variable *TargetX8632::legalizeToVar(Operand *From, bool AllowOverlap, 2480 Variable *TargetX8632::legalizeToVar(Operand *From, bool AllowOverlap,
2329 int32_t RegNum) { 2481 int32_t RegNum) {
2330 return llvm::cast<Variable>(legalize(From, Legal_Reg, AllowOverlap, RegNum)); 2482 return llvm::cast<Variable>(legalize(From, Legal_Reg, AllowOverlap, RegNum));
2331 } 2483 }
2332 2484
2485 OperandX8632Mem *TargetX8632::FormMemoryOperand(Operand *Operand, Type Ty) {
2486 OperandX8632Mem *Mem = llvm::dyn_cast<OperandX8632Mem>(Operand);
2487 // It may be the case that address mode optimization already creates
2488 // an OperandX8632Mem, so in that case it wouldn't need another level
2489 // of transformation.
2490 if (!Mem) {
2491 Variable *Base = llvm::dyn_cast<Variable>(Operand);
2492 Constant *Offset = llvm::dyn_cast<Constant>(Operand);
2493 assert(Base || Offset);
2494 Mem = OperandX8632Mem::create(Func, Ty, Base, Offset);
2495 }
2496 return llvm::cast<OperandX8632Mem>(legalize(Mem));
2497 }
2498
2333 Variable *TargetX8632::makeReg(Type Type, int32_t RegNum) { 2499 Variable *TargetX8632::makeReg(Type Type, int32_t RegNum) {
2334 // There aren't any 64-bit integer registers for x86-32. 2500 // There aren't any 64-bit integer registers for x86-32.
2335 assert(Type != IceType_i64); 2501 assert(Type != IceType_i64);
2336 Variable *Reg = Func->makeVariable(Type, Context.getNode()); 2502 Variable *Reg = Func->makeVariable(Type, Context.getNode());
2337 if (RegNum == Variable::NoRegister) 2503 if (RegNum == Variable::NoRegister)
2338 Reg->setWeightInfinite(); 2504 Reg->setWeightInfinite();
2339 else 2505 else
2340 Reg->setRegNum(RegNum); 2506 Reg->setRegNum(RegNum);
2341 return Reg; 2507 return Reg;
2342 } 2508 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
2409 // llvm-mc doesn't parse "dword ptr [.L$foo]". 2575 // llvm-mc doesn't parse "dword ptr [.L$foo]".
2410 Str << "dword ptr [L$" << IceType_f32 << "$" << getPoolEntryID() << "]"; 2576 Str << "dword ptr [L$" << IceType_f32 << "$" << getPoolEntryID() << "]";
2411 } 2577 }
2412 2578
2413 template <> void ConstantDouble::emit(GlobalContext *Ctx) const { 2579 template <> void ConstantDouble::emit(GlobalContext *Ctx) const {
2414 Ostream &Str = Ctx->getStrEmit(); 2580 Ostream &Str = Ctx->getStrEmit();
2415 Str << "qword ptr [L$" << IceType_f64 << "$" << getPoolEntryID() << "]"; 2581 Str << "qword ptr [L$" << IceType_f64 << "$" << getPoolEntryID() << "]";
2416 } 2582 }
2417 2583
2418 } // end of namespace Ice 2584 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceTargetLoweringX8632.h ('k') | src/llvm2ice.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698