Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceInstX8632.cpp - X86-32 instruction implementation ---===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Defines X8632 specific data related to X8632 Instructions and |
| 12 | /// Instruction traits. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 13 | /// |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 14 | /// These are declared in the IceTargetLoweringX8632Traits.h header file. This |
| 15 | /// file also defines X8632 operand specific methods (dump and emit.) |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 16 | /// |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 18 | #include "IceInstX8632.h" |
| 19 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 20 | #include "IceAssemblerX8632.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 21 | #include "IceCfg.h" |
| 22 | #include "IceCfgNode.h" |
Jan Voung | bd385e4 | 2014-09-18 18:18:10 -0700 | [diff] [blame] | 23 | #include "IceConditionCodesX8632.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 24 | #include "IceInst.h" |
Jan Voung | bd385e4 | 2014-09-18 18:18:10 -0700 | [diff] [blame] | 25 | #include "IceRegistersX8632.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 26 | #include "IceTargetLoweringX8632.h" |
| 27 | #include "IceOperand.h" |
| 28 | |
| 29 | namespace Ice { |
| 30 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 31 | namespace X8632 { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 32 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 33 | const TargetX8632Traits::InstBrAttributesType |
| 34 | TargetX8632Traits::InstBrAttributes[] = { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 35 | #define X(val, encode, opp, dump, emit) \ |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 36 | { X8632::Traits::Cond::opp, dump, emit } \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 37 | , |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 38 | ICEINSTX8632BR_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 39 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 40 | }; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 41 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 42 | const TargetX8632Traits::InstCmppsAttributesType |
| 43 | TargetX8632Traits::InstCmppsAttributes[] = { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 44 | #define X(val, emit) \ |
Matt Wala | ce0ca8f | 2014-07-24 12:34:20 -0700 | [diff] [blame] | 45 | { emit } \ |
| 46 | , |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 47 | ICEINSTX8632CMPPS_TABLE |
Matt Wala | ce0ca8f | 2014-07-24 12:34:20 -0700 | [diff] [blame] | 48 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 49 | }; |
Matt Wala | ce0ca8f | 2014-07-24 12:34:20 -0700 | [diff] [blame] | 50 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 51 | const TargetX8632Traits::TypeAttributesType |
| 52 | TargetX8632Traits::TypeAttributes[] = { |
Nicolas Capens | 7638e27 | 2016-10-06 11:33:55 -0400 | [diff] [blame] | 53 | #define X(tag, elty, cvt, sdss, pdps, spsd, int_, unpack, pack, width, fld) \ |
| 54 | { cvt, sdss, pdps, spsd, int_, unpack, pack, width, fld } \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 55 | , |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 56 | ICETYPEX8632_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 57 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 58 | }; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 59 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 60 | const char *TargetX8632Traits::InstSegmentRegNames[] = { |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 61 | #define X(val, name, prefix) name, |
| 62 | SEG_REGX8632_TABLE |
| 63 | #undef X |
| 64 | }; |
| 65 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 66 | uint8_t TargetX8632Traits::InstSegmentPrefixes[] = { |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 67 | #define X(val, name, prefix) prefix, |
| 68 | SEG_REGX8632_TABLE |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 69 | #undef X |
| 70 | }; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 71 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 72 | void TargetX8632Traits::X86Operand::dump(const Cfg *, Ostream &Str) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 73 | if (BuildDefs::dump()) |
| 74 | Str << "<OperandX8632>"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 77 | TargetX8632Traits::X86OperandMem::X86OperandMem( |
| 78 | Cfg *Func, Type Ty, Variable *Base, Constant *Offset, Variable *Index, |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 79 | uint16_t Shift, SegmentRegisters SegmentReg, bool IsRebased) |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 80 | : X86Operand(kMem, Ty), Base(Base), Offset(Offset), Index(Index), |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 81 | Shift(Shift), SegmentReg(SegmentReg), IsRebased(IsRebased) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 82 | assert(Shift <= 3); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 83 | Vars = nullptr; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 84 | NumVars = 0; |
| 85 | if (Base) |
| 86 | ++NumVars; |
| 87 | if (Index) |
| 88 | ++NumVars; |
| 89 | if (NumVars) { |
| 90 | Vars = Func->allocateArrayOf<Variable *>(NumVars); |
| 91 | SizeT I = 0; |
| 92 | if (Base) |
| 93 | Vars[I++] = Base; |
| 94 | if (Index) |
| 95 | Vars[I++] = Index; |
| 96 | assert(I == NumVars); |
| 97 | } |
| 98 | } |
| 99 | |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 100 | namespace { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 101 | |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 102 | int32_t getRematerializableOffset(Variable *Var, |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 103 | const Ice::X8632::TargetX8632 *Target) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 104 | int32_t Disp = Var->getStackOffset(); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 105 | const auto RegNum = Var->getRegNum(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 106 | if (RegNum == Target->getFrameReg()) { |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 107 | Disp += Target->getFrameFixedAllocaOffset(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 108 | } else if (RegNum != Target->getStackReg()) { |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 109 | llvm::report_fatal_error("Unexpected rematerializable register type"); |
| 110 | } |
| 111 | return Disp; |
| 112 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 113 | |
| 114 | void validateMemOperandPIC(const TargetX8632Traits::X86OperandMem *Mem, |
| 115 | bool UseNonsfi) { |
| 116 | if (!BuildDefs::asserts()) |
| 117 | return; |
| 118 | const bool HasCR = |
| 119 | Mem->getOffset() && llvm::isa<ConstantRelocatable>(Mem->getOffset()); |
| 120 | (void)HasCR; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 121 | const bool IsRebased = Mem->getIsRebased(); |
| 122 | (void)IsRebased; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 123 | if (UseNonsfi) |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 124 | assert(HasCR == IsRebased); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 125 | else |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 126 | assert(!IsRebased); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 127 | } |
| 128 | |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 129 | } // end of anonymous namespace |
| 130 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 131 | void TargetX8632Traits::X86OperandMem::emit(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 132 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 133 | return; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 134 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 135 | validateMemOperandPIC(this, UseNonsfi); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 136 | const auto *Target = |
| 137 | static_cast<const ::Ice::X8632::TargetX8632 *>(Func->getTarget()); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 138 | // If the base is rematerializable, we need to replace it with the correct |
| 139 | // physical register (esp or ebp), and update the Offset. |
| 140 | int32_t Disp = 0; |
| 141 | if (getBase() && getBase()->isRematerializable()) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 142 | Disp += getRematerializableOffset(getBase(), Target); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 143 | } |
| 144 | // The index should never be rematerializable. But if we ever allow it, then |
| 145 | // we should make sure the rematerialization offset is shifted by the Shift |
| 146 | // value. |
| 147 | if (getIndex()) |
| 148 | assert(!getIndex()->isRematerializable()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 149 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 150 | if (SegmentReg != DefaultSegment) { |
Jim Stichnoth | 5acafbc | 2014-07-30 15:37:39 -0700 | [diff] [blame] | 151 | assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 152 | Str << "%" << X8632::Traits::InstSegmentRegNames[SegmentReg] << ":"; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 153 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 154 | // Emit as Offset(Base,Index,1<<Shift). Offset is emitted without the leading |
| 155 | // '$'. Omit the (Base,Index,1<<Shift) part if Base==nullptr. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 156 | if (getOffset() == nullptr && Disp == 0) { |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 157 | // No offset, emit nothing. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 158 | } else if (getOffset() == nullptr && Disp != 0) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 159 | Str << Disp; |
| 160 | } else if (const auto *CI = llvm::dyn_cast<ConstantInteger32>(getOffset())) { |
| 161 | if (getBase() == nullptr || CI->getValue() || Disp != 0) |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 162 | // Emit a non-zero offset without a leading '$'. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 163 | Str << CI->getValue() + Disp; |
| 164 | } else if (const auto *CR = |
| 165 | llvm::dyn_cast<ConstantRelocatable>(getOffset())) { |
| 166 | // TODO(sehr): ConstantRelocatable still needs updating for |
| 167 | // rematerializable base/index and Disp. |
| 168 | assert(Disp == 0); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 169 | CR->emitWithoutPrefix(Target, UseNonsfi ? "@GOTOFF" : ""); |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 170 | } else { |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 171 | llvm_unreachable("Invalid offset type for x86 mem operand"); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 172 | } |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 173 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 174 | if (getBase() || getIndex()) { |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 175 | Str << "("; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 176 | if (getBase()) |
| 177 | getBase()->emit(Func); |
| 178 | if (getIndex()) { |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 179 | Str << ","; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 180 | getIndex()->emit(Func); |
| 181 | if (getShift()) |
| 182 | Str << "," << (1u << getShift()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 183 | } |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 184 | Str << ")"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 185 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 188 | void TargetX8632Traits::X86OperandMem::dump(const Cfg *Func, |
| 189 | Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 190 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 191 | return; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 192 | if (SegmentReg != DefaultSegment) { |
Jim Stichnoth | 5acafbc | 2014-07-30 15:37:39 -0700 | [diff] [blame] | 193 | assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 194 | Str << X8632::Traits::InstSegmentRegNames[SegmentReg] << ":"; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 195 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 196 | bool Dumped = false; |
| 197 | Str << "["; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 198 | int32_t Disp = 0; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 199 | const auto *Target = |
| 200 | static_cast<const ::Ice::X8632::TargetX8632 *>(Func->getTarget()); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 201 | if (getBase() && getBase()->isRematerializable()) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 202 | Disp += getRematerializableOffset(getBase(), Target); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 203 | } |
| 204 | if (getBase()) { |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 205 | if (Func) |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 206 | getBase()->dump(Func); |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 207 | else |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 208 | getBase()->dump(Str); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 209 | Dumped = true; |
| 210 | } |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 211 | if (getIndex()) { |
| 212 | assert(!getIndex()->isRematerializable()); |
| 213 | if (getBase()) |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 214 | Str << "+"; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 215 | if (getShift() > 0) |
| 216 | Str << (1u << getShift()) << "*"; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 217 | if (Func) |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 218 | getIndex()->dump(Func); |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 219 | else |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 220 | getIndex()->dump(Str); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 221 | Dumped = true; |
| 222 | } |
Jim Stichnoth | 8099e9a | 2015-11-12 10:26:34 -0800 | [diff] [blame] | 223 | if (Disp) { |
| 224 | if (Disp > 0) |
| 225 | Str << "+"; |
| 226 | Str << Disp; |
| 227 | Dumped = true; |
| 228 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 229 | // Pretty-print the Offset. |
| 230 | bool OffsetIsZero = false; |
| 231 | bool OffsetIsNegative = false; |
Jim Stichnoth | 8099e9a | 2015-11-12 10:26:34 -0800 | [diff] [blame] | 232 | if (getOffset() == nullptr) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 233 | OffsetIsZero = true; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 234 | } else if (const auto *CI = llvm::dyn_cast<ConstantInteger32>(getOffset())) { |
Jim Stichnoth | 8099e9a | 2015-11-12 10:26:34 -0800 | [diff] [blame] | 235 | OffsetIsZero = (CI->getValue() == 0); |
| 236 | OffsetIsNegative = (static_cast<int32_t>(CI->getValue()) < 0); |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 237 | } else { |
Jim Stichnoth | 8099e9a | 2015-11-12 10:26:34 -0800 | [diff] [blame] | 238 | assert(llvm::isa<ConstantRelocatable>(getOffset())); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 239 | } |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 240 | if (Dumped) { |
| 241 | if (!OffsetIsZero) { // Suppress if Offset is known to be 0 |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 242 | if (!OffsetIsNegative) // Suppress if Offset is known to be negative |
| 243 | Str << "+"; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 244 | getOffset()->dump(Func, Str); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 245 | } |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 246 | } else { |
| 247 | // There is only the offset. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 248 | getOffset()->dump(Func, Str); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 249 | } |
| 250 | Str << "]"; |
| 251 | } |
| 252 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 253 | void TargetX8632Traits::X86OperandMem::emitSegmentOverride( |
| 254 | TargetX8632Traits::Assembler *Asm) const { |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 255 | if (SegmentReg != DefaultSegment) { |
| 256 | assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 257 | Asm->emitSegmentOverride(X8632::Traits::InstSegmentPrefixes[SegmentReg]); |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 261 | TargetX8632Traits::Address TargetX8632Traits::X86OperandMem::toAsmAddress( |
| 262 | TargetX8632Traits::Assembler *Asm, |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 263 | const Ice::TargetLowering *TargetLowering, bool /*IsLeaAddr*/) const { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 264 | const auto *Target = |
| 265 | static_cast<const ::Ice::X8632::TargetX8632 *>(TargetLowering); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 266 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 267 | validateMemOperandPIC(this, UseNonsfi); |
| 268 | int32_t Disp = 0; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 269 | if (getBase() && getBase()->isRematerializable()) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 270 | Disp += getRematerializableOffset(getBase(), Target); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 271 | } |
| 272 | // The index should never be rematerializable. But if we ever allow it, then |
| 273 | // we should make sure the rematerialization offset is shifted by the Shift |
| 274 | // value. |
| 275 | if (getIndex()) |
| 276 | assert(!getIndex()->isRematerializable()); |
Jan Voung | 359c036 | 2014-11-04 10:40:03 -0800 | [diff] [blame] | 277 | AssemblerFixup *Fixup = nullptr; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 278 | // Determine the offset (is it relocatable?) |
| 279 | if (getOffset()) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 280 | if (const auto *CI = llvm::dyn_cast<ConstantInteger32>(getOffset())) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 281 | Disp += static_cast<int32_t>(CI->getValue()); |
Jan Voung | 359c036 | 2014-11-04 10:40:03 -0800 | [diff] [blame] | 282 | } else if (const auto CR = |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 283 | llvm::dyn_cast<ConstantRelocatable>(getOffset())) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 284 | Disp += CR->getOffset(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 285 | Fixup = Asm->createFixup(Target->getAbsFixup(), CR); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 286 | } else { |
| 287 | llvm_unreachable("Unexpected offset type"); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Now convert to the various possible forms. |
| 292 | if (getBase() && getIndex()) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 293 | return X8632::Traits::Address(getEncodedGPR(getBase()->getRegNum()), |
| 294 | getEncodedGPR(getIndex()->getRegNum()), |
| 295 | X8632::Traits::ScaleFactor(getShift()), Disp, |
| 296 | Fixup); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 297 | } else if (getBase()) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 298 | return X8632::Traits::Address(getEncodedGPR(getBase()->getRegNum()), Disp, |
| 299 | Fixup); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 300 | } else if (getIndex()) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 301 | return X8632::Traits::Address(getEncodedGPR(getIndex()->getRegNum()), |
| 302 | X8632::Traits::ScaleFactor(getShift()), Disp, |
| 303 | Fixup); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 304 | } else { |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 305 | return X8632::Traits::Address(Disp, Fixup); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 309 | TargetX8632Traits::Address |
| 310 | TargetX8632Traits::VariableSplit::toAsmAddress(const Cfg *Func) const { |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 311 | assert(!Var->hasReg()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 312 | const ::Ice::TargetLowering *Target = Func->getTarget(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 313 | int32_t Offset = Var->getStackOffset() + getOffset(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 314 | return X8632::Traits::Address(getEncodedGPR(Target->getFrameOrStackReg()), |
| 315 | Offset, AssemblerFixup::NoFixup); |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 316 | } |
| 317 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 318 | void TargetX8632Traits::VariableSplit::emit(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 319 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 320 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 321 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 322 | assert(!Var->hasReg()); |
| 323 | // The following is copied/adapted from TargetX8632::emitVariable(). |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 324 | const ::Ice::TargetLowering *Target = Func->getTarget(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 325 | constexpr Type Ty = IceType_i32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 326 | int32_t Offset = Var->getStackOffset() + getOffset(); |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 327 | if (Offset) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 328 | Str << Offset; |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 329 | Str << "(%" << Target->getRegName(Target->getFrameOrStackReg(), Ty) << ")"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 330 | } |
| 331 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 332 | void TargetX8632Traits::VariableSplit::dump(const Cfg *Func, |
| 333 | Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 334 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 335 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 336 | switch (Part) { |
| 337 | case Low: |
| 338 | Str << "low"; |
| 339 | break; |
| 340 | case High: |
| 341 | Str << "high"; |
| 342 | break; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 343 | } |
| 344 | Str << "("; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 345 | if (Func) |
| 346 | Var->dump(Func); |
| 347 | else |
| 348 | Var->dump(Str); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 349 | Str << ")"; |
| 350 | } |
| 351 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 352 | } // namespace X8632 |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 353 | } // end of namespace Ice |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 354 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 355 | X86INSTS_DEFINE_STATIC_DATA(X8632, X8632::Traits) |