John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceInstX86BaseImpl.h - Generic X86 instructions -*- C++ -*=// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the InstX86Base class and its descendants. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SUBZERO_SRC_ICEINSTX86BASEIMPL_H |
| 16 | #define SUBZERO_SRC_ICEINSTX86BASEIMPL_H |
| 17 | |
| 18 | #include "IceInstX86Base.h" |
| 19 | |
| 20 | #include "IceAssemblerX86Base.h" |
| 21 | #include "IceCfg.h" |
| 22 | #include "IceCfgNode.h" |
| 23 | #include "IceDefs.h" |
| 24 | #include "IceInst.h" |
| 25 | #include "IceOperand.h" |
| 26 | #include "IceTargetLowering.h" |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 27 | #include "IceTargetLoweringX86Base.h" |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 28 | |
| 29 | namespace Ice { |
| 30 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 31 | namespace X86NAMESPACE { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 32 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 33 | template <typename TraitsType> |
| 34 | const char *InstImpl<TraitsType>::InstX86Base::getWidthString(Type Ty) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 35 | return Traits::TypeAttributes[Ty].WidthString; |
| 36 | } |
| 37 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 38 | template <typename TraitsType> |
| 39 | const char *InstImpl<TraitsType>::InstX86Base::getFldString(Type Ty) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 40 | return Traits::TypeAttributes[Ty].FldString; |
| 41 | } |
| 42 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 43 | template <typename TraitsType> |
| 44 | typename InstImpl<TraitsType>::Cond::BrCond |
| 45 | InstImpl<TraitsType>::InstX86Base::getOppositeCondition(BrCond Cond) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 46 | return Traits::InstBrAttributes[Cond].Opposite; |
| 47 | } |
| 48 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 49 | template <typename TraitsType> |
| 50 | InstImpl<TraitsType>::InstX86FakeRMW::InstX86FakeRMW(Cfg *Func, Operand *Data, |
| 51 | Operand *Addr, |
| 52 | InstArithmetic::OpKind Op, |
| 53 | Variable *Beacon) |
| 54 | : InstX86Base(Func, InstX86Base::FakeRMW, 3, nullptr), Op(Op) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 55 | this->addSource(Data); |
| 56 | this->addSource(Addr); |
| 57 | this->addSource(Beacon); |
| 58 | } |
| 59 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 60 | template <typename TraitsType> |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 61 | InstImpl<TraitsType>::InstX86GetIP::InstX86GetIP(Cfg *Func, Variable *Dest) |
| 62 | : InstX86Base(Func, InstX86Base::GetIP, 0, Dest) {} |
| 63 | |
| 64 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 65 | InstImpl<TraitsType>::InstX86Mul::InstX86Mul(Cfg *Func, Variable *Dest, |
| 66 | Variable *Source1, |
| 67 | Operand *Source2) |
| 68 | : InstX86Base(Func, InstX86Base::Mul, 2, Dest) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 69 | this->addSource(Source1); |
| 70 | this->addSource(Source2); |
| 71 | } |
| 72 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 73 | template <typename TraitsType> |
| 74 | InstImpl<TraitsType>::InstX86Shld::InstX86Shld(Cfg *Func, Variable *Dest, |
| 75 | Variable *Source1, |
| 76 | Operand *Source2) |
| 77 | : InstX86Base(Func, InstX86Base::Shld, 3, Dest) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 78 | this->addSource(Dest); |
| 79 | this->addSource(Source1); |
| 80 | this->addSource(Source2); |
| 81 | } |
| 82 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 83 | template <typename TraitsType> |
| 84 | InstImpl<TraitsType>::InstX86Shrd::InstX86Shrd(Cfg *Func, Variable *Dest, |
| 85 | Variable *Source1, |
| 86 | Operand *Source2) |
| 87 | : InstX86Base(Func, InstX86Base::Shrd, 3, Dest) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 88 | this->addSource(Dest); |
| 89 | this->addSource(Source1); |
| 90 | this->addSource(Source2); |
| 91 | } |
| 92 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 93 | template <typename TraitsType> |
| 94 | InstImpl<TraitsType>::InstX86Label::InstX86Label(Cfg *Func, |
| 95 | TargetLowering *Target) |
| 96 | : InstX86Base(Func, InstX86Base::Label, 0, nullptr), |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 97 | Number(Target->makeNextLabelNumber()) {} |
| 98 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 99 | template <typename TraitsType> |
| 100 | IceString InstImpl<TraitsType>::InstX86Label::getName(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 101 | return ".L" + Func->getFunctionName() + "$local$__" + std::to_string(Number); |
| 102 | } |
| 103 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 104 | template <typename TraitsType> |
| 105 | InstImpl<TraitsType>::InstX86Br::InstX86Br( |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 106 | Cfg *Func, const CfgNode *TargetTrue, const CfgNode *TargetFalse, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 107 | const InstImpl<TraitsType>::InstX86Label *Label, BrCond Condition, |
| 108 | Mode Kind) |
| 109 | : InstX86Base(Func, InstX86Base::Br, 0, nullptr), Condition(Condition), |
| 110 | TargetTrue(TargetTrue), TargetFalse(TargetFalse), Label(Label), |
| 111 | Kind(Kind) {} |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 112 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 113 | template <typename TraitsType> |
| 114 | bool InstImpl<TraitsType>::InstX86Br::optimizeBranch(const CfgNode *NextNode) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 115 | // If there is no next block, then there can be no fallthrough to optimize. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 116 | if (NextNode == nullptr) |
| 117 | return false; |
| 118 | // Intra-block conditional branches can't be optimized. |
| 119 | if (Label) |
| 120 | return false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 121 | // If there is no fallthrough node, such as a non-default case label for a |
| 122 | // switch instruction, then there is no opportunity to optimize. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 123 | if (getTargetFalse() == nullptr) |
| 124 | return false; |
| 125 | |
| 126 | // Unconditional branch to the next node can be removed. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 127 | if (Condition == Cond::Br_None && getTargetFalse() == NextNode) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 128 | assert(getTargetTrue() == nullptr); |
| 129 | this->setDeleted(); |
| 130 | return true; |
| 131 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 132 | // If the fallthrough is to the next node, set fallthrough to nullptr to |
| 133 | // indicate. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 134 | if (getTargetFalse() == NextNode) { |
| 135 | TargetFalse = nullptr; |
| 136 | return true; |
| 137 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 138 | // If TargetTrue is the next node, and TargetFalse is not nullptr (which was |
| 139 | // already tested above), then invert the branch condition, swap the targets, |
| 140 | // and set new fallthrough to nullptr. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 141 | if (getTargetTrue() == NextNode) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 142 | assert(Condition != Cond::Br_None); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 143 | Condition = this->getOppositeCondition(Condition); |
| 144 | TargetTrue = getTargetFalse(); |
| 145 | TargetFalse = nullptr; |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 151 | template <typename TraitsType> |
| 152 | bool InstImpl<TraitsType>::InstX86Br::repointEdges(CfgNode *OldNode, |
| 153 | CfgNode *NewNode) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 154 | bool Found = false; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 155 | if (TargetFalse == OldNode) { |
| 156 | TargetFalse = NewNode; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 157 | Found = true; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 158 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 159 | if (TargetTrue == OldNode) { |
| 160 | TargetTrue = NewNode; |
| 161 | Found = true; |
| 162 | } |
| 163 | return Found; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 166 | template <typename TraitsType> |
| 167 | InstImpl<TraitsType>::InstX86Jmp::InstX86Jmp(Cfg *Func, Operand *Target) |
| 168 | : InstX86Base(Func, InstX86Base::Jmp, 1, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 169 | this->addSource(Target); |
| 170 | } |
| 171 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 172 | template <typename TraitsType> |
| 173 | InstImpl<TraitsType>::InstX86Call::InstX86Call(Cfg *Func, Variable *Dest, |
| 174 | Operand *CallTarget) |
| 175 | : InstX86Base(Func, InstX86Base::Call, 1, Dest) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 176 | this->HasSideEffects = true; |
| 177 | this->addSource(CallTarget); |
| 178 | } |
| 179 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 180 | template <typename TraitsType> |
| 181 | InstImpl<TraitsType>::InstX86Cmov::InstX86Cmov(Cfg *Func, Variable *Dest, |
| 182 | Operand *Source, |
| 183 | BrCond Condition) |
| 184 | : InstX86Base(Func, InstX86Base::Cmov, 2, Dest), Condition(Condition) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 185 | // The final result is either the original Dest, or Source, so mark both as |
| 186 | // sources. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 187 | this->addSource(Dest); |
| 188 | this->addSource(Source); |
| 189 | } |
| 190 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 191 | template <typename TraitsType> |
| 192 | InstImpl<TraitsType>::InstX86Cmpps::InstX86Cmpps(Cfg *Func, Variable *Dest, |
| 193 | Operand *Source, |
| 194 | CmppsCond Condition) |
| 195 | : InstX86Base(Func, InstX86Base::Cmpps, 2, Dest), Condition(Condition) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 196 | this->addSource(Dest); |
| 197 | this->addSource(Source); |
| 198 | } |
| 199 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 200 | template <typename TraitsType> |
| 201 | InstImpl<TraitsType>::InstX86Cmpxchg::InstX86Cmpxchg(Cfg *Func, |
| 202 | Operand *DestOrAddr, |
| 203 | Variable *Eax, |
| 204 | Variable *Desired, |
| 205 | bool Locked) |
| 206 | : InstImpl<TraitsType>::InstX86BaseLockable( |
| 207 | Func, InstX86Base::Cmpxchg, 3, llvm::dyn_cast<Variable>(DestOrAddr), |
| 208 | Locked) { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 209 | constexpr uint16_t Encoded_rAX = 0; |
| 210 | (void)Encoded_rAX; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 211 | assert(Traits::getEncodedGPR(Eax->getRegNum()) == Encoded_rAX); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 212 | this->addSource(DestOrAddr); |
| 213 | this->addSource(Eax); |
| 214 | this->addSource(Desired); |
| 215 | } |
| 216 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 217 | template <typename TraitsType> |
| 218 | InstImpl<TraitsType>::InstX86Cmpxchg8b::InstX86Cmpxchg8b( |
| 219 | Cfg *Func, X86OperandMem *Addr, Variable *Edx, Variable *Eax, Variable *Ecx, |
| 220 | Variable *Ebx, bool Locked) |
| 221 | : InstImpl<TraitsType>::InstX86BaseLockable(Func, InstX86Base::Cmpxchg, 5, |
| 222 | nullptr, Locked) { |
| 223 | assert(Edx->getRegNum() == RegisterSet::Reg_edx); |
| 224 | assert(Eax->getRegNum() == RegisterSet::Reg_eax); |
| 225 | assert(Ecx->getRegNum() == RegisterSet::Reg_ecx); |
| 226 | assert(Ebx->getRegNum() == RegisterSet::Reg_ebx); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 227 | this->addSource(Addr); |
| 228 | this->addSource(Edx); |
| 229 | this->addSource(Eax); |
| 230 | this->addSource(Ecx); |
| 231 | this->addSource(Ebx); |
| 232 | } |
| 233 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 234 | template <typename TraitsType> |
| 235 | InstImpl<TraitsType>::InstX86Cvt::InstX86Cvt(Cfg *Func, Variable *Dest, |
| 236 | Operand *Source, |
| 237 | CvtVariant Variant) |
| 238 | : InstX86Base(Func, InstX86Base::Cvt, 1, Dest), Variant(Variant) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 239 | this->addSource(Source); |
| 240 | } |
| 241 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 242 | template <typename TraitsType> |
| 243 | InstImpl<TraitsType>::InstX86Icmp::InstX86Icmp(Cfg *Func, Operand *Src0, |
| 244 | Operand *Src1) |
| 245 | : InstX86Base(Func, InstX86Base::Icmp, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 246 | this->addSource(Src0); |
| 247 | this->addSource(Src1); |
| 248 | } |
| 249 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 250 | template <typename TraitsType> |
| 251 | InstImpl<TraitsType>::InstX86Ucomiss::InstX86Ucomiss(Cfg *Func, Operand *Src0, |
| 252 | Operand *Src1) |
| 253 | : InstX86Base(Func, InstX86Base::Ucomiss, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 254 | this->addSource(Src0); |
| 255 | this->addSource(Src1); |
| 256 | } |
| 257 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 258 | template <typename TraitsType> |
| 259 | InstImpl<TraitsType>::InstX86UD2::InstX86UD2(Cfg *Func) |
| 260 | : InstX86Base(Func, InstX86Base::UD2, 0, nullptr) {} |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 261 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 262 | template <typename TraitsType> |
| 263 | InstImpl<TraitsType>::InstX86Test::InstX86Test(Cfg *Func, Operand *Src1, |
| 264 | Operand *Src2) |
| 265 | : InstX86Base(Func, InstX86Base::Test, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 266 | this->addSource(Src1); |
| 267 | this->addSource(Src2); |
| 268 | } |
| 269 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 270 | template <typename TraitsType> |
| 271 | InstImpl<TraitsType>::InstX86Mfence::InstX86Mfence(Cfg *Func) |
| 272 | : InstX86Base(Func, InstX86Base::Mfence, 0, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 273 | this->HasSideEffects = true; |
| 274 | } |
| 275 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 276 | template <typename TraitsType> |
| 277 | InstImpl<TraitsType>::InstX86Store::InstX86Store(Cfg *Func, Operand *Value, |
| 278 | X86Operand *Mem) |
| 279 | : InstX86Base(Func, InstX86Base::Store, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 280 | this->addSource(Value); |
| 281 | this->addSource(Mem); |
| 282 | } |
| 283 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 284 | template <typename TraitsType> |
| 285 | InstImpl<TraitsType>::InstX86StoreP::InstX86StoreP(Cfg *Func, Variable *Value, |
| 286 | X86OperandMem *Mem) |
| 287 | : InstX86Base(Func, InstX86Base::StoreP, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 288 | this->addSource(Value); |
| 289 | this->addSource(Mem); |
| 290 | } |
| 291 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 292 | template <typename TraitsType> |
| 293 | InstImpl<TraitsType>::InstX86StoreQ::InstX86StoreQ(Cfg *Func, Variable *Value, |
| 294 | X86OperandMem *Mem) |
| 295 | : InstX86Base(Func, InstX86Base::StoreQ, 2, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 296 | this->addSource(Value); |
| 297 | this->addSource(Mem); |
| 298 | } |
| 299 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 300 | template <typename TraitsType> |
| 301 | InstImpl<TraitsType>::InstX86Nop::InstX86Nop(Cfg *Func, |
| 302 | InstX86Nop::NopVariant Variant) |
| 303 | : InstX86Base(Func, InstX86Base::Nop, 0, nullptr), Variant(Variant) {} |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 304 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 305 | template <typename TraitsType> |
| 306 | InstImpl<TraitsType>::InstX86Fld::InstX86Fld(Cfg *Func, Operand *Src) |
| 307 | : InstX86Base(Func, InstX86Base::Fld, 1, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 308 | this->addSource(Src); |
| 309 | } |
| 310 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 311 | template <typename TraitsType> |
| 312 | InstImpl<TraitsType>::InstX86Fstp::InstX86Fstp(Cfg *Func, Variable *Dest) |
| 313 | : InstX86Base(Func, InstX86Base::Fstp, 0, Dest) {} |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 314 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 315 | template <typename TraitsType> |
| 316 | InstImpl<TraitsType>::InstX86Pop::InstX86Pop(Cfg *Func, Variable *Dest) |
| 317 | : InstX86Base(Func, InstX86Base::Pop, 0, Dest) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 318 | // A pop instruction affects the stack pointer and so it should not be |
| 319 | // allowed to be automatically dead-code eliminated. (The corresponding push |
| 320 | // instruction doesn't need this treatment because it has no dest variable |
| 321 | // and therefore won't be dead-code eliminated.) This is needed for |
| 322 | // late-stage liveness analysis (e.g. asm-verbose mode). |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 323 | this->HasSideEffects = true; |
| 324 | } |
| 325 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 326 | template <typename TraitsType> |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 327 | InstImpl<TraitsType>::InstX86Push::InstX86Push(Cfg *Func, Operand *Source) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 328 | : InstX86Base(Func, InstX86Base::Push, 1, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 329 | this->addSource(Source); |
| 330 | } |
| 331 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 332 | template <typename TraitsType> |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 333 | InstImpl<TraitsType>::InstX86Push::InstX86Push(Cfg *Func, InstX86Label *L) |
| 334 | : InstX86Base(Func, InstX86Base::Push, 0, nullptr), Label(L) {} |
| 335 | |
| 336 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 337 | InstImpl<TraitsType>::InstX86Ret::InstX86Ret(Cfg *Func, Variable *Source) |
| 338 | : InstX86Base(Func, InstX86Base::Ret, Source ? 1 : 0, nullptr) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 339 | if (Source) |
| 340 | this->addSource(Source); |
| 341 | } |
| 342 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 343 | template <typename TraitsType> |
| 344 | InstImpl<TraitsType>::InstX86Setcc::InstX86Setcc(Cfg *Func, Variable *Dest, |
| 345 | BrCond Cond) |
| 346 | : InstX86Base(Func, InstX86Base::Setcc, 0, Dest), Condition(Cond) {} |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 347 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 348 | template <typename TraitsType> |
| 349 | InstImpl<TraitsType>::InstX86Xadd::InstX86Xadd(Cfg *Func, Operand *Dest, |
| 350 | Variable *Source, bool Locked) |
| 351 | : InstImpl<TraitsType>::InstX86BaseLockable( |
| 352 | Func, InstX86Base::Xadd, 2, llvm::dyn_cast<Variable>(Dest), Locked) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 353 | this->addSource(Dest); |
| 354 | this->addSource(Source); |
| 355 | } |
| 356 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 357 | template <typename TraitsType> |
| 358 | InstImpl<TraitsType>::InstX86Xchg::InstX86Xchg(Cfg *Func, Operand *Dest, |
| 359 | Variable *Source) |
| 360 | : InstX86Base(Func, InstX86Base::Xchg, 2, llvm::dyn_cast<Variable>(Dest)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 361 | this->addSource(Dest); |
| 362 | this->addSource(Source); |
| 363 | } |
| 364 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 365 | template <typename TraitsType> |
| 366 | InstImpl<TraitsType>::InstX86IacaStart::InstX86IacaStart(Cfg *Func) |
| 367 | : InstX86Base(Func, InstX86Base::IacaStart, 0, nullptr) { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 368 | assert(Func->getContext()->getFlags().getAllowIacaMarks()); |
| 369 | } |
| 370 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 371 | template <typename TraitsType> |
| 372 | InstImpl<TraitsType>::InstX86IacaEnd::InstX86IacaEnd(Cfg *Func) |
| 373 | : InstX86Base(Func, InstX86Base::IacaEnd, 0, nullptr) { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 374 | assert(Func->getContext()->getFlags().getAllowIacaMarks()); |
| 375 | } |
| 376 | |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 377 | // ======================== Dump routines ======================== // |
| 378 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 379 | template <typename TraitsType> |
| 380 | void InstImpl<TraitsType>::InstX86Base::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 381 | if (!BuildDefs::dump()) |
| 382 | return; |
| 383 | Ostream &Str = Func->getContext()->getStrDump(); |
| 384 | Str << "[" << Traits::TargetName << "] "; |
| 385 | Inst::dump(Func); |
| 386 | } |
| 387 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 388 | template <typename TraitsType> |
| 389 | void InstImpl<TraitsType>::InstX86FakeRMW::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 390 | if (!BuildDefs::dump()) |
| 391 | return; |
| 392 | Ostream &Str = Func->getContext()->getStrDump(); |
| 393 | Type Ty = getData()->getType(); |
| 394 | Str << "rmw " << InstArithmetic::getOpName(getOp()) << " " << Ty << " *"; |
| 395 | getAddr()->dump(Func); |
| 396 | Str << ", "; |
| 397 | getData()->dump(Func); |
| 398 | Str << ", beacon="; |
| 399 | getBeacon()->dump(Func); |
| 400 | } |
| 401 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 402 | template <typename TraitsType> |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 403 | void InstImpl<TraitsType>::InstX86GetIP::emit(const Cfg *Func) const { |
| 404 | if (!BuildDefs::dump()) |
| 405 | return; |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 406 | const auto *Dest = this->getDest(); |
| 407 | assert(Dest->hasReg()); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 408 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 409 | Str << "\t" |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 410 | "call" |
| 411 | "\t"; |
| 412 | auto *Target = static_cast<TargetLowering *>(Func->getTarget()); |
| 413 | Target->emitWithoutPrefix(Target->createGetIPForRegister(Dest)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | template <typename TraitsType> |
| 417 | void InstImpl<TraitsType>::InstX86GetIP::emitIAS(const Cfg *Func) const { |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 418 | const auto *Dest = this->getDest(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 419 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 420 | assert(Dest->hasReg()); |
| 421 | Asm->call(static_cast<TargetLowering *>(Func->getTarget()) |
| 422 | ->createGetIPForRegister(Dest)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | template <typename TraitsType> |
| 426 | void InstImpl<TraitsType>::InstX86GetIP::dump(const Cfg *Func) const { |
| 427 | if (!BuildDefs::dump()) |
| 428 | return; |
| 429 | Ostream &Str = Func->getContext()->getStrDump(); |
| 430 | this->getDest()->dump(Func); |
| 431 | Str << " = call getIP"; |
| 432 | } |
| 433 | |
| 434 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 435 | void InstImpl<TraitsType>::InstX86Label::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 436 | if (!BuildDefs::dump()) |
| 437 | return; |
| 438 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 439 | Str << getName(Func) << ":"; |
| 440 | } |
| 441 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 442 | template <typename TraitsType> |
| 443 | void InstImpl<TraitsType>::InstX86Label::emitIAS(const Cfg *Func) const { |
| 444 | Assembler *Asm = Func->getAssembler<Assembler>(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 445 | Asm->bindLocalLabel(Number); |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 446 | if (OffsetReloc != nullptr) { |
| 447 | Asm->bindRelocOffset(OffsetReloc); |
| 448 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 449 | } |
| 450 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 451 | template <typename TraitsType> |
| 452 | void InstImpl<TraitsType>::InstX86Label::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 453 | if (!BuildDefs::dump()) |
| 454 | return; |
| 455 | Ostream &Str = Func->getContext()->getStrDump(); |
| 456 | Str << getName(Func) << ":"; |
| 457 | } |
| 458 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 459 | template <typename TraitsType> |
| 460 | void InstImpl<TraitsType>::InstX86Br::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 461 | if (!BuildDefs::dump()) |
| 462 | return; |
| 463 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 464 | Str << "\t"; |
| 465 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 466 | if (Condition == Cond::Br_None) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 467 | Str << "jmp"; |
| 468 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 469 | Str << Traits::InstBrAttributes[Condition].EmitString; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (Label) { |
| 473 | Str << "\t" << Label->getName(Func); |
| 474 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 475 | if (Condition == Cond::Br_None) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 476 | Str << "\t" << getTargetFalse()->getAsmName(); |
| 477 | } else { |
| 478 | Str << "\t" << getTargetTrue()->getAsmName(); |
| 479 | if (getTargetFalse()) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 480 | Str << "\n\t" |
| 481 | "jmp\t" << getTargetFalse()->getAsmName(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 487 | template <typename TraitsType> |
| 488 | void InstImpl<TraitsType>::InstX86Br::emitIAS(const Cfg *Func) const { |
| 489 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 490 | if (Label) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 491 | auto *L = Asm->getOrCreateLocalLabel(Label->getNumber()); |
| 492 | if (Condition == Cond::Br_None) { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 493 | Asm->jmp(L, isNear()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 494 | } else { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 495 | Asm->j(Condition, L, isNear()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 496 | } |
| 497 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 498 | if (Condition == Cond::Br_None) { |
| 499 | auto *L = Asm->getOrCreateCfgNodeLabel(getTargetFalse()->getIndex()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 500 | assert(!getTargetTrue()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 501 | Asm->jmp(L, isNear()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 502 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 503 | auto *L = Asm->getOrCreateCfgNodeLabel(getTargetTrue()->getIndex()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 504 | Asm->j(Condition, L, isNear()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 505 | if (getTargetFalse()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 506 | auto *L2 = Asm->getOrCreateCfgNodeLabel(getTargetFalse()->getIndex()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 507 | Asm->jmp(L2, isNear()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 513 | template <typename TraitsType> |
| 514 | void InstImpl<TraitsType>::InstX86Br::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 515 | if (!BuildDefs::dump()) |
| 516 | return; |
| 517 | Ostream &Str = Func->getContext()->getStrDump(); |
| 518 | Str << "br "; |
| 519 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 520 | if (Condition == Cond::Br_None) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 521 | Str << "label %" |
| 522 | << (Label ? Label->getName(Func) : getTargetFalse()->getName()); |
| 523 | return; |
| 524 | } |
| 525 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 526 | Str << Traits::InstBrAttributes[Condition].DisplayString; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 527 | if (Label) { |
| 528 | Str << ", label %" << Label->getName(Func); |
| 529 | } else { |
| 530 | Str << ", label %" << getTargetTrue()->getName(); |
| 531 | if (getTargetFalse()) { |
| 532 | Str << ", label %" << getTargetFalse()->getName(); |
| 533 | } |
| 534 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 535 | |
| 536 | Str << " // (" << (isNear() ? "near" : "far") << " jump)"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 537 | } |
| 538 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 539 | template <typename TraitsType> |
| 540 | void InstImpl<TraitsType>::InstX86Jmp::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 541 | if (!BuildDefs::dump()) |
| 542 | return; |
| 543 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 544 | assert(this->getSrcSize() == 1); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 545 | const Operand *Src = this->getSrc(0); |
| 546 | if (Traits::Is64Bit) { |
| 547 | if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(Src)) { |
| 548 | Str << "\t" |
| 549 | "jmp" |
| 550 | "\t" << CR->getName(); |
| 551 | return; |
| 552 | } |
| 553 | } |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 554 | Str << "\t" |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 555 | "jmp" |
| 556 | "\t*"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 557 | getJmpTarget()->emit(Func); |
| 558 | } |
| 559 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 560 | template <typename TraitsType> |
| 561 | void InstImpl<TraitsType>::InstX86Jmp::emitIAS(const Cfg *Func) const { |
| 562 | // Note: Adapted (mostly copied) from |
| 563 | // InstImpl<TraitsType>::InstX86Call::emitIAS(). |
| 564 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 565 | Operand *Target = getJmpTarget(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 566 | if (const auto *Var = llvm::dyn_cast<Variable>(Target)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 567 | if (Var->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 568 | Asm->jmp(Traits::getEncodedGPR(Var->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 569 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 570 | // The jmp instruction with a memory operand should be possible to |
| 571 | // encode, but it isn't a valid sandboxed instruction, and there |
| 572 | // shouldn't be a register allocation issue to jump through a scratch |
| 573 | // register, so we don't really need to bother implementing it. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 574 | llvm::report_fatal_error("Assembler can't jmp to memory operand"); |
| 575 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 576 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Target)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 577 | (void)Mem; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 578 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 579 | llvm::report_fatal_error("Assembler can't jmp to memory operand"); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 580 | } else if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(Target)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 581 | Asm->jmp(CR); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 582 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Target)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 583 | // NaCl trampoline calls refer to an address within the sandbox directly. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 584 | // This is usually only needed for non-IRT builds and otherwise not very |
| 585 | // portable or stable. Usually this is only done for "calls" and not jumps. |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 586 | Asm->jmp(AssemblerImmediate(Imm->getValue())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 587 | } else { |
| 588 | llvm::report_fatal_error("Unexpected operand type"); |
| 589 | } |
| 590 | } |
| 591 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 592 | template <typename TraitsType> |
| 593 | void InstImpl<TraitsType>::InstX86Jmp::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 594 | if (!BuildDefs::dump()) |
| 595 | return; |
| 596 | Ostream &Str = Func->getContext()->getStrDump(); |
| 597 | Str << "jmp "; |
| 598 | getJmpTarget()->dump(Func); |
| 599 | } |
| 600 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 601 | template <typename TraitsType> |
| 602 | void InstImpl<TraitsType>::InstX86Call::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 603 | if (!BuildDefs::dump()) |
| 604 | return; |
| 605 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 606 | assert(this->getSrcSize() == 1); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 607 | Str << "\t" |
| 608 | "call\t"; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 609 | Operand *CallTarget = getCallTarget(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 610 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 611 | if (const auto *CI = llvm::dyn_cast<ConstantInteger32>(CallTarget)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 612 | // Emit without a leading '$'. |
| 613 | Str << CI->getValue(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 614 | } else if (const auto DirectCallTarget = |
| 615 | llvm::dyn_cast<ConstantRelocatable>(CallTarget)) { |
| 616 | DirectCallTarget->emitWithoutPrefix(Target); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 617 | } else { |
| 618 | Str << "*"; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 619 | CallTarget->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 620 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 621 | } |
| 622 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 623 | template <typename TraitsType> |
| 624 | void InstImpl<TraitsType>::InstX86Call::emitIAS(const Cfg *Func) const { |
| 625 | Assembler *Asm = Func->getAssembler<Assembler>(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 626 | Operand *CallTarget = getCallTarget(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 627 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 628 | if (const auto *Var = llvm::dyn_cast<Variable>(CallTarget)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 629 | if (Var->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 630 | Asm->call(Traits::getEncodedGPR(Var->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 631 | } else { |
David Sehr | b8e49c1 | 2015-11-12 14:41:22 -0800 | [diff] [blame] | 632 | Asm->call(Target->stackVarToAsmOperand(Var)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 633 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 634 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(CallTarget)) { |
| 635 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 636 | Asm->call(Mem->toAsmAddress(Asm, Target)); |
| 637 | } else if (const auto *CR = llvm::dyn_cast<ConstantRelocatable>(CallTarget)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 638 | Asm->call(CR); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 639 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(CallTarget)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 640 | Asm->call(AssemblerImmediate(Imm->getValue())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 641 | } else { |
| 642 | llvm_unreachable("Unexpected operand type"); |
| 643 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 644 | } |
| 645 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 646 | template <typename TraitsType> |
| 647 | void InstImpl<TraitsType>::InstX86Call::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 648 | if (!BuildDefs::dump()) |
| 649 | return; |
| 650 | Ostream &Str = Func->getContext()->getStrDump(); |
| 651 | if (this->getDest()) { |
| 652 | this->dumpDest(Func); |
| 653 | Str << " = "; |
| 654 | } |
| 655 | Str << "call "; |
| 656 | getCallTarget()->dump(Func); |
| 657 | } |
| 658 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 659 | // The this->Opcode parameter needs to be char* and not IceString because of |
| 660 | // template issues. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 661 | template <typename TraitsType> |
| 662 | void InstImpl<TraitsType>::InstX86Base::emitTwoAddress( |
| 663 | const Cfg *Func, const char *Opcode, const char *Suffix) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 664 | if (!BuildDefs::dump()) |
| 665 | return; |
| 666 | Ostream &Str = Func->getContext()->getStrEmit(); |
David Sehr | 6da7ae2 | 2015-12-17 15:01:30 -0800 | [diff] [blame] | 667 | assert(getSrcSize() == 2); |
| 668 | Operand *Dest = getDest(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 669 | if (Dest == nullptr) |
David Sehr | 6da7ae2 | 2015-12-17 15:01:30 -0800 | [diff] [blame] | 670 | Dest = getSrc(0); |
| 671 | assert(Dest == getSrc(0)); |
| 672 | Operand *Src1 = getSrc(1); |
| 673 | Str << "\t" << Opcode << Suffix |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 674 | << InstX86Base::getWidthString(Dest->getType()) << "\t"; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 675 | Src1->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 676 | Str << ", "; |
| 677 | Dest->emit(Func); |
| 678 | } |
| 679 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 680 | template <typename TraitsType> |
| 681 | void InstImpl<TraitsType>::emitIASOpTyGPR(const Cfg *Func, Type Ty, |
| 682 | const Operand *Op, |
| 683 | const GPREmitterOneOp &Emitter) { |
| 684 | auto *Target = InstX86Base::getTarget(Func); |
| 685 | Assembler *Asm = Func->getAssembler<Assembler>(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 686 | if (const auto *Var = llvm::dyn_cast<Variable>(Op)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 687 | if (Var->hasReg()) { |
| 688 | // We cheat a little and use GPRRegister even for byte operations. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 689 | GPRRegister VarReg = Traits::getEncodedGPR(Var->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 690 | (Asm->*(Emitter.Reg))(Ty, VarReg); |
| 691 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 692 | Address StackAddr(Target->stackVarToAsmOperand(Var)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 693 | (Asm->*(Emitter.Addr))(Ty, StackAddr); |
| 694 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 695 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Op)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 696 | Mem->emitSegmentOverride(Asm); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 697 | (Asm->*(Emitter.Addr))(Ty, Mem->toAsmAddress(Asm, Target)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 698 | } else { |
| 699 | llvm_unreachable("Unexpected operand type"); |
| 700 | } |
| 701 | } |
| 702 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 703 | template <typename TraitsType> |
| 704 | template <bool VarCanBeByte, bool SrcCanBeByte> |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 705 | void InstImpl<TraitsType>::emitIASRegOpTyGPR(const Cfg *Func, bool IsLea, |
| 706 | Type Ty, const Variable *Var, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 707 | const Operand *Src, |
| 708 | const GPREmitterRegOp &Emitter) { |
| 709 | auto *Target = InstX86Base::getTarget(Func); |
| 710 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 711 | assert(Var->hasReg()); |
| 712 | // We cheat a little and use GPRRegister even for byte operations. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 713 | GPRRegister VarReg = VarCanBeByte ? Traits::getEncodedGPR(Var->getRegNum()) |
| 714 | : Traits::getEncodedGPR(Var->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 715 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 716 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 717 | GPRRegister SrcReg = SrcCanBeByte |
| 718 | ? Traits::getEncodedGPR(SrcVar->getRegNum()) |
| 719 | : Traits::getEncodedGPR(SrcVar->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 720 | (Asm->*(Emitter.GPRGPR))(Ty, VarReg, SrcReg); |
| 721 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 722 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 723 | (Asm->*(Emitter.GPRAddr))(Ty, VarReg, SrcStackAddr); |
| 724 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 725 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 726 | Mem->emitSegmentOverride(Asm); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 727 | (Asm->*(Emitter.GPRAddr))(Ty, VarReg, |
| 728 | Mem->toAsmAddress(Asm, Target, IsLea)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 729 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 730 | (Asm->*(Emitter.GPRImm))(Ty, VarReg, AssemblerImmediate(Imm->getValue())); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 731 | } else if (const auto *Reloc = llvm::dyn_cast<ConstantRelocatable>(Src)) { |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 732 | const auto FixupKind = Reloc->getName() == GlobalOffsetTable |
| 733 | ? Traits::FK_GotPC |
| 734 | : Traits::TargetLowering::getAbsFixup(); |
| 735 | AssemblerFixup *Fixup = Asm->createFixup(FixupKind, Reloc); |
| 736 | (Asm->*(Emitter.GPRImm))(Ty, VarReg, AssemblerImmediate(Fixup)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 737 | } else if (const auto *Split = llvm::dyn_cast<VariableSplit>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 738 | (Asm->*(Emitter.GPRAddr))(Ty, VarReg, Split->toAsmAddress(Func)); |
| 739 | } else { |
| 740 | llvm_unreachable("Unexpected operand type"); |
| 741 | } |
| 742 | } |
| 743 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 744 | template <typename TraitsType> |
| 745 | void InstImpl<TraitsType>::emitIASAddrOpTyGPR(const Cfg *Func, Type Ty, |
| 746 | const Address &Addr, |
| 747 | const Operand *Src, |
| 748 | const GPREmitterAddrOp &Emitter) { |
| 749 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 750 | // Src can only be Reg or AssemblerImmediate. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 751 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 752 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 753 | GPRRegister SrcReg = Traits::getEncodedGPR(SrcVar->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 754 | (Asm->*(Emitter.AddrGPR))(Ty, Addr, SrcReg); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 755 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 756 | (Asm->*(Emitter.AddrImm))(Ty, Addr, AssemblerImmediate(Imm->getValue())); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 757 | } else if (const auto *Reloc = llvm::dyn_cast<ConstantRelocatable>(Src)) { |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 758 | const auto FixupKind = Reloc->getName() == GlobalOffsetTable |
| 759 | ? Traits::FK_GotPC |
| 760 | : Traits::TargetLowering::getAbsFixup(); |
| 761 | AssemblerFixup *Fixup = Asm->createFixup(FixupKind, Reloc); |
| 762 | (Asm->*(Emitter.AddrImm))(Ty, Addr, AssemblerImmediate(Fixup)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 763 | } else { |
| 764 | llvm_unreachable("Unexpected operand type"); |
| 765 | } |
| 766 | } |
| 767 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 768 | template <typename TraitsType> |
| 769 | void InstImpl<TraitsType>::emitIASAsAddrOpTyGPR( |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 770 | const Cfg *Func, Type Ty, const Operand *Op0, const Operand *Op1, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 771 | const GPREmitterAddrOp &Emitter) { |
| 772 | auto *Target = InstX86Base::getTarget(Func); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 773 | if (const auto *Op0Var = llvm::dyn_cast<Variable>(Op0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 774 | assert(!Op0Var->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 775 | Address StackAddr(Target->stackVarToAsmOperand(Op0Var)); |
| 776 | emitIASAddrOpTyGPR(Func, Ty, StackAddr, Op1, Emitter); |
| 777 | } else if (const auto *Op0Mem = llvm::dyn_cast<X86OperandMem>(Op0)) { |
| 778 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 779 | Op0Mem->emitSegmentOverride(Asm); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 780 | emitIASAddrOpTyGPR(Func, Ty, Op0Mem->toAsmAddress(Asm, Target), Op1, |
| 781 | Emitter); |
| 782 | } else if (const auto *Split = llvm::dyn_cast<VariableSplit>(Op0)) { |
| 783 | emitIASAddrOpTyGPR(Func, Ty, Split->toAsmAddress(Func), Op1, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 784 | } else { |
| 785 | llvm_unreachable("Unexpected operand type"); |
| 786 | } |
| 787 | } |
| 788 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 789 | template <typename TraitsType> |
| 790 | void InstImpl<TraitsType>::emitIASGPRShift(const Cfg *Func, Type Ty, |
| 791 | const Variable *Var, |
| 792 | const Operand *Src, |
| 793 | const GPREmitterShiftOp &Emitter) { |
| 794 | Assembler *Asm = Func->getAssembler<Assembler>(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 795 | // Technically, the Dest Var can be mem as well, but we only use Reg. We can |
| 796 | // extend this to check Dest if we decide to use that form. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 797 | assert(Var->hasReg()); |
| 798 | // We cheat a little and use GPRRegister even for byte operations. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 799 | GPRRegister VarReg = Traits::getEncodedGPR(Var->getRegNum()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 800 | // Src must be reg == ECX or an Imm8. This is asserted by the assembler. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 801 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 802 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 803 | GPRRegister SrcReg = Traits::getEncodedGPR(SrcVar->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 804 | (Asm->*(Emitter.GPRGPR))(Ty, VarReg, SrcReg); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 805 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 806 | (Asm->*(Emitter.GPRImm))(Ty, VarReg, AssemblerImmediate(Imm->getValue())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 807 | } else { |
| 808 | llvm_unreachable("Unexpected operand type"); |
| 809 | } |
| 810 | } |
| 811 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 812 | template <typename TraitsType> |
| 813 | void InstImpl<TraitsType>::emitIASGPRShiftDouble( |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 814 | const Cfg *Func, const Variable *Dest, const Operand *Src1Op, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 815 | const Operand *Src2Op, const GPREmitterShiftD &Emitter) { |
| 816 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 817 | // Dest can be reg or mem, but we only use the reg variant. |
| 818 | assert(Dest->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 819 | GPRRegister DestReg = Traits::getEncodedGPR(Dest->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 820 | // SrcVar1 must be reg. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 821 | const auto *SrcVar1 = llvm::cast<Variable>(Src1Op); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 822 | assert(SrcVar1->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 823 | GPRRegister SrcReg = Traits::getEncodedGPR(SrcVar1->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 824 | Type Ty = SrcVar1->getType(); |
| 825 | // Src2 can be the implicit CL register or an immediate. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 826 | if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Src2Op)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 827 | (Asm->*(Emitter.GPRGPRImm))(Ty, DestReg, SrcReg, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 828 | AssemblerImmediate(Imm->getValue())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 829 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 830 | assert(llvm::cast<Variable>(Src2Op)->getRegNum() == RegisterSet::Reg_cl); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 831 | (Asm->*(Emitter.GPRGPR))(Ty, DestReg, SrcReg); |
| 832 | } |
| 833 | } |
| 834 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 835 | template <typename TraitsType> |
| 836 | void InstImpl<TraitsType>::emitIASXmmShift(const Cfg *Func, Type Ty, |
| 837 | const Variable *Var, |
| 838 | const Operand *Src, |
| 839 | const XmmEmitterShiftOp &Emitter) { |
| 840 | auto *Target = InstX86Base::getTarget(Func); |
| 841 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 842 | assert(Var->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 843 | XmmRegister VarReg = Traits::getEncodedXmm(Var->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 844 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 845 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 846 | XmmRegister SrcReg = Traits::getEncodedXmm(SrcVar->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 847 | (Asm->*(Emitter.XmmXmm))(Ty, VarReg, SrcReg); |
| 848 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 849 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 850 | (Asm->*(Emitter.XmmAddr))(Ty, VarReg, SrcStackAddr); |
| 851 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 852 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
| 853 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 854 | (Asm->*(Emitter.XmmAddr))(Ty, VarReg, Mem->toAsmAddress(Asm, Target)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 855 | } else if (const auto *Imm = llvm::dyn_cast<ConstantInteger32>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 856 | (Asm->*(Emitter.XmmImm))(Ty, VarReg, AssemblerImmediate(Imm->getValue())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 857 | } else { |
| 858 | llvm_unreachable("Unexpected operand type"); |
| 859 | } |
| 860 | } |
| 861 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 862 | template <typename TraitsType> |
| 863 | void InstImpl<TraitsType>::emitIASRegOpTyXMM(const Cfg *Func, Type Ty, |
| 864 | const Variable *Var, |
| 865 | const Operand *Src, |
| 866 | const XmmEmitterRegOp &Emitter) { |
| 867 | auto *Target = InstX86Base::getTarget(Func); |
| 868 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 869 | assert(Var->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 870 | XmmRegister VarReg = Traits::getEncodedXmm(Var->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 871 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 872 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 873 | XmmRegister SrcReg = Traits::getEncodedXmm(SrcVar->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 874 | (Asm->*(Emitter.XmmXmm))(Ty, VarReg, SrcReg); |
| 875 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 876 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 877 | (Asm->*(Emitter.XmmAddr))(Ty, VarReg, SrcStackAddr); |
| 878 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 879 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
| 880 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 881 | (Asm->*(Emitter.XmmAddr))(Ty, VarReg, Mem->toAsmAddress(Asm, Target)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 882 | } else if (const auto *Imm = llvm::dyn_cast<Constant>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 883 | (Asm->*(Emitter.XmmAddr))(Ty, VarReg, |
| 884 | Traits::Address::ofConstPool(Asm, Imm)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 885 | } else { |
| 886 | llvm_unreachable("Unexpected operand type"); |
| 887 | } |
| 888 | } |
| 889 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 890 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 891 | template <typename DReg_t, typename SReg_t, DReg_t (*destEnc)(RegNumT), |
| 892 | SReg_t (*srcEnc)(RegNumT)> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 893 | void InstImpl<TraitsType>::emitIASCastRegOp( |
| 894 | const Cfg *Func, Type DestTy, const Variable *Dest, Type SrcTy, |
| 895 | const Operand *Src, const CastEmitterRegOp<DReg_t, SReg_t> &Emitter) { |
| 896 | auto *Target = InstX86Base::getTarget(Func); |
| 897 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 898 | assert(Dest->hasReg()); |
| 899 | DReg_t DestReg = destEnc(Dest->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 900 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 901 | if (SrcVar->hasReg()) { |
| 902 | SReg_t SrcReg = srcEnc(SrcVar->getRegNum()); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 903 | (Asm->*(Emitter.RegReg))(DestTy, DestReg, SrcTy, SrcReg); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 904 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 905 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 906 | (Asm->*(Emitter.RegAddr))(DestTy, DestReg, SrcTy, SrcStackAddr); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 907 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 908 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 909 | Mem->emitSegmentOverride(Asm); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 910 | (Asm->*(Emitter.RegAddr))(DestTy, DestReg, SrcTy, |
| 911 | Mem->toAsmAddress(Asm, Target)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 912 | } else { |
| 913 | llvm_unreachable("Unexpected operand type"); |
| 914 | } |
| 915 | } |
| 916 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 917 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 918 | template <typename DReg_t, typename SReg_t, DReg_t (*destEnc)(RegNumT), |
| 919 | SReg_t (*srcEnc)(RegNumT)> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 920 | void InstImpl<TraitsType>::emitIASThreeOpImmOps( |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 921 | const Cfg *Func, Type DispatchTy, const Variable *Dest, const Operand *Src0, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 922 | const Operand *Src1, const ThreeOpImmEmitter<DReg_t, SReg_t> Emitter) { |
| 923 | auto *Target = InstX86Base::getTarget(Func); |
| 924 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 925 | // This only handles Dest being a register, and Src1 being an immediate. |
| 926 | assert(Dest->hasReg()); |
| 927 | DReg_t DestReg = destEnc(Dest->getRegNum()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 928 | AssemblerImmediate Imm(llvm::cast<ConstantInteger32>(Src1)->getValue()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 929 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 930 | if (SrcVar->hasReg()) { |
| 931 | SReg_t SrcReg = srcEnc(SrcVar->getRegNum()); |
| 932 | (Asm->*(Emitter.RegRegImm))(DispatchTy, DestReg, SrcReg, Imm); |
| 933 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 934 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 935 | (Asm->*(Emitter.RegAddrImm))(DispatchTy, DestReg, SrcStackAddr, Imm); |
| 936 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 937 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 938 | Mem->emitSegmentOverride(Asm); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 939 | (Asm->*(Emitter.RegAddrImm))(DispatchTy, DestReg, |
| 940 | Mem->toAsmAddress(Asm, Target), Imm); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 941 | } else { |
| 942 | llvm_unreachable("Unexpected operand type"); |
| 943 | } |
| 944 | } |
| 945 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 946 | template <typename TraitsType> |
| 947 | void InstImpl<TraitsType>::emitIASMovlikeXMM(const Cfg *Func, |
| 948 | const Variable *Dest, |
| 949 | const Operand *Src, |
| 950 | const XmmEmitterMovOps Emitter) { |
| 951 | auto *Target = InstX86Base::getTarget(Func); |
| 952 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 953 | if (Dest->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 954 | XmmRegister DestReg = Traits::getEncodedXmm(Dest->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 955 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 956 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 957 | (Asm->*(Emitter.XmmXmm))(DestReg, |
| 958 | Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 959 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 960 | Address StackAddr(Target->stackVarToAsmOperand(SrcVar)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 961 | (Asm->*(Emitter.XmmAddr))(DestReg, StackAddr); |
| 962 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 963 | } else if (const auto *SrcMem = llvm::dyn_cast<X86OperandMem>(Src)) { |
| 964 | assert(SrcMem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 965 | (Asm->*(Emitter.XmmAddr))(DestReg, SrcMem->toAsmAddress(Asm, Target)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 966 | } else { |
| 967 | llvm_unreachable("Unexpected operand type"); |
| 968 | } |
| 969 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 970 | Address StackAddr(Target->stackVarToAsmOperand(Dest)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 971 | // Src must be a register in this case. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 972 | const auto *SrcVar = llvm::cast<Variable>(Src); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 973 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 974 | (Asm->*(Emitter.AddrXmm))(StackAddr, |
| 975 | Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 979 | template <typename TraitsType> |
| 980 | void InstImpl<TraitsType>::InstX86Sqrtss::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 981 | if (!BuildDefs::dump()) |
| 982 | return; |
| 983 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 984 | assert(this->getSrcSize() == 1); |
| 985 | Type Ty = this->getSrc(0)->getType(); |
| 986 | assert(isScalarFloatingType(Ty)); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 987 | Str << "\t" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 988 | "sqrt" << Traits::TypeAttributes[Ty].SdSsString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 989 | this->getSrc(0)->emit(Func); |
| 990 | Str << ", "; |
| 991 | this->getDest()->emit(Func); |
| 992 | } |
| 993 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 994 | template <typename TraitsType> |
| 995 | void InstImpl<TraitsType>::InstX86Div::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 996 | if (!BuildDefs::dump()) |
| 997 | return; |
| 998 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 999 | assert(this->getSrcSize() == 3); |
| 1000 | Operand *Src1 = this->getSrc(1); |
| 1001 | Str << "\t" << this->Opcode << this->getWidthString(Src1->getType()) << "\t"; |
| 1002 | Src1->emit(Func); |
| 1003 | } |
| 1004 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1005 | template <typename TraitsType> |
| 1006 | void InstImpl<TraitsType>::InstX86Div::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1007 | assert(this->getSrcSize() == 3); |
| 1008 | const Operand *Src = this->getSrc(1); |
| 1009 | Type Ty = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1010 | static GPREmitterOneOp Emitter = {&Assembler::div, &Assembler::div}; |
| 1011 | emitIASOpTyGPR(Func, Ty, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1014 | template <typename TraitsType> |
| 1015 | void InstImpl<TraitsType>::InstX86Idiv::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1016 | if (!BuildDefs::dump()) |
| 1017 | return; |
| 1018 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1019 | assert(this->getSrcSize() == 3); |
| 1020 | Operand *Src1 = this->getSrc(1); |
| 1021 | Str << "\t" << this->Opcode << this->getWidthString(Src1->getType()) << "\t"; |
| 1022 | Src1->emit(Func); |
| 1023 | } |
| 1024 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1025 | template <typename TraitsType> |
| 1026 | void InstImpl<TraitsType>::InstX86Idiv::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1027 | assert(this->getSrcSize() == 3); |
| 1028 | const Operand *Src = this->getSrc(1); |
| 1029 | Type Ty = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1030 | static const GPREmitterOneOp Emitter = {&Assembler::idiv, &Assembler::idiv}; |
| 1031 | emitIASOpTyGPR(Func, Ty, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | // pblendvb and blendvps take xmm0 as a final implicit argument. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1035 | template <typename TraitsType> |
| 1036 | void InstImpl<TraitsType>::emitVariableBlendInst(const char *Opcode, |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1037 | const Inst *Instr, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1038 | const Cfg *Func) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1039 | if (!BuildDefs::dump()) |
| 1040 | return; |
| 1041 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1042 | assert(Instr->getSrcSize() == 3); |
| 1043 | assert(llvm::cast<Variable>(Instr->getSrc(2))->getRegNum() == |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1044 | RegisterSet::Reg_xmm0); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1045 | Str << "\t" << Opcode << "\t"; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1046 | Instr->getSrc(1)->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1047 | Str << ", "; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1048 | Instr->getDest()->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1051 | template <typename TraitsType> |
| 1052 | void InstImpl<TraitsType>::emitIASVariableBlendInst( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1053 | const Inst *Instr, const Cfg *Func, const XmmEmitterRegOp &Emitter) { |
| 1054 | assert(Instr->getSrcSize() == 3); |
| 1055 | assert(llvm::cast<Variable>(Instr->getSrc(2))->getRegNum() == |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1056 | RegisterSet::Reg_xmm0); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1057 | const Variable *Dest = Instr->getDest(); |
| 1058 | const Operand *Src = Instr->getSrc(1); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1059 | emitIASRegOpTyXMM(Func, Dest->getType(), Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1062 | template <typename TraitsType> |
| 1063 | void InstImpl<TraitsType>::InstX86Blendvps::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1064 | if (!BuildDefs::dump()) |
| 1065 | return; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1066 | emitVariableBlendInst(this->Opcode, this, Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1069 | template <typename TraitsType> |
| 1070 | void InstImpl<TraitsType>::InstX86Blendvps::emitIAS(const Cfg *Func) const { |
| 1071 | static const XmmEmitterRegOp Emitter = {&Assembler::blendvps, |
| 1072 | &Assembler::blendvps}; |
| 1073 | emitIASVariableBlendInst(this, Func, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1076 | template <typename TraitsType> |
| 1077 | void InstImpl<TraitsType>::InstX86Pblendvb::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1078 | if (!BuildDefs::dump()) |
| 1079 | return; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1080 | emitVariableBlendInst(this->Opcode, this, Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1083 | template <typename TraitsType> |
| 1084 | void InstImpl<TraitsType>::InstX86Pblendvb::emitIAS(const Cfg *Func) const { |
| 1085 | static const XmmEmitterRegOp Emitter = {&Assembler::pblendvb, |
| 1086 | &Assembler::pblendvb}; |
| 1087 | emitIASVariableBlendInst(this, Func, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1090 | template <typename TraitsType> |
| 1091 | void InstImpl<TraitsType>::InstX86Imul::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1092 | if (!BuildDefs::dump()) |
| 1093 | return; |
| 1094 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1095 | assert(this->getSrcSize() == 2); |
| 1096 | Variable *Dest = this->getDest(); |
| 1097 | if (isByteSizedArithType(Dest->getType())) { |
| 1098 | // The 8-bit version of imul only allows the form "imul r/m8". |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1099 | const auto *Src0Var = llvm::dyn_cast<Variable>(this->getSrc(0)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1100 | (void)Src0Var; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1101 | assert(Src0Var->getRegNum() == RegisterSet::Reg_al); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1102 | Str << "\t" |
| 1103 | "imulb\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1104 | this->getSrc(1)->emit(Func); |
| 1105 | } else if (llvm::isa<Constant>(this->getSrc(1))) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1106 | Str << "\t" |
| 1107 | "imul" << this->getWidthString(Dest->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1108 | this->getSrc(1)->emit(Func); |
| 1109 | Str << ", "; |
| 1110 | this->getSrc(0)->emit(Func); |
| 1111 | Str << ", "; |
| 1112 | Dest->emit(Func); |
| 1113 | } else { |
David Sehr | 6da7ae2 | 2015-12-17 15:01:30 -0800 | [diff] [blame] | 1114 | this->emitTwoAddress(Func, this->Opcode); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1118 | template <typename TraitsType> |
| 1119 | void InstImpl<TraitsType>::InstX86Imul::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1120 | assert(this->getSrcSize() == 2); |
| 1121 | const Variable *Var = this->getDest(); |
| 1122 | Type Ty = Var->getType(); |
| 1123 | const Operand *Src = this->getSrc(1); |
| 1124 | if (isByteSizedArithType(Ty)) { |
| 1125 | // The 8-bit version of imul only allows the form "imul r/m8". |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1126 | const auto *Src0Var = llvm::dyn_cast<Variable>(this->getSrc(0)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1127 | (void)Src0Var; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1128 | assert(Src0Var->getRegNum() == RegisterSet::Reg_al); |
| 1129 | static const GPREmitterOneOp Emitter = {&Assembler::imul, &Assembler::imul}; |
| 1130 | emitIASOpTyGPR(Func, Ty, this->getSrc(1), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1131 | } else { |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 1132 | // The two-address version is used when multiplying by a non-constant |
| 1133 | // or doing an 8-bit multiply. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1134 | assert(Var == this->getSrc(0)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1135 | static const GPREmitterRegOp Emitter = {&Assembler::imul, &Assembler::imul, |
| 1136 | &Assembler::imul}; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1137 | constexpr bool NotLea = false; |
| 1138 | emitIASRegOpTyGPR(Func, NotLea, Ty, Var, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1142 | template <typename TraitsType> |
| 1143 | void InstImpl<TraitsType>::InstX86ImulImm::emit(const Cfg *Func) const { |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 1144 | if (!BuildDefs::dump()) |
| 1145 | return; |
| 1146 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1147 | assert(this->getSrcSize() == 2); |
| 1148 | Variable *Dest = this->getDest(); |
| 1149 | assert(Dest->getType() == IceType_i16 || Dest->getType() == IceType_i32); |
| 1150 | assert(llvm::isa<Constant>(this->getSrc(1))); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1151 | Str << "\t" |
| 1152 | "imul" << this->getWidthString(Dest->getType()) << "\t"; |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 1153 | this->getSrc(1)->emit(Func); |
| 1154 | Str << ", "; |
| 1155 | this->getSrc(0)->emit(Func); |
| 1156 | Str << ", "; |
| 1157 | Dest->emit(Func); |
| 1158 | } |
| 1159 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1160 | template <typename TraitsType> |
| 1161 | void InstImpl<TraitsType>::InstX86ImulImm::emitIAS(const Cfg *Func) const { |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 1162 | assert(this->getSrcSize() == 2); |
| 1163 | const Variable *Dest = this->getDest(); |
| 1164 | Type Ty = Dest->getType(); |
| 1165 | assert(llvm::isa<Constant>(this->getSrc(1))); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1166 | static const ThreeOpImmEmitter<GPRRegister, GPRRegister> Emitter = { |
| 1167 | &Assembler::imul, &Assembler::imul}; |
| 1168 | emitIASThreeOpImmOps<GPRRegister, GPRRegister, Traits::getEncodedGPR, |
| 1169 | Traits::getEncodedGPR>(Func, Ty, Dest, this->getSrc(0), |
| 1170 | this->getSrc(1), Emitter); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1173 | template <typename TraitsType> |
| 1174 | void InstImpl<TraitsType>::InstX86Insertps::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1175 | assert(this->getSrcSize() == 3); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1176 | assert(InstX86Base::getTarget(Func)->getInstructionSet() >= Traits::SSE4_1); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1177 | const Variable *Dest = this->getDest(); |
| 1178 | assert(Dest == this->getSrc(0)); |
| 1179 | Type Ty = Dest->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1180 | static const ThreeOpImmEmitter<XmmRegister, XmmRegister> Emitter = { |
| 1181 | &Assembler::insertps, &Assembler::insertps}; |
| 1182 | emitIASThreeOpImmOps<XmmRegister, XmmRegister, Traits::getEncodedXmm, |
| 1183 | Traits::getEncodedXmm>(Func, Ty, Dest, this->getSrc(1), |
| 1184 | this->getSrc(2), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1187 | template <typename TraitsType> |
| 1188 | void InstImpl<TraitsType>::InstX86Cbwdq::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1189 | if (!BuildDefs::dump()) |
| 1190 | return; |
| 1191 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1192 | assert(this->getSrcSize() == 1); |
| 1193 | Operand *Src0 = this->getSrc(0); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 1194 | const auto DestReg = this->getDest()->getRegNum(); |
| 1195 | const auto SrcReg = llvm::cast<Variable>(Src0)->getRegNum(); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1196 | (void)DestReg; |
| 1197 | (void)SrcReg; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1198 | switch (Src0->getType()) { |
| 1199 | default: |
| 1200 | llvm_unreachable("unexpected source type!"); |
| 1201 | break; |
| 1202 | case IceType_i8: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1203 | assert(SrcReg == RegisterSet::Reg_al); |
| 1204 | assert(DestReg == RegisterSet::Reg_ax || DestReg == RegisterSet::Reg_ah); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1205 | Str << "\t" |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1206 | "cbtw"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1207 | break; |
| 1208 | case IceType_i16: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1209 | assert(SrcReg == RegisterSet::Reg_ax); |
| 1210 | assert(DestReg == RegisterSet::Reg_dx); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1211 | Str << "\t" |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1212 | "cwtd"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1213 | break; |
| 1214 | case IceType_i32: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1215 | assert(SrcReg == RegisterSet::Reg_eax); |
| 1216 | assert(DestReg == RegisterSet::Reg_edx); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1217 | Str << "\t" |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1218 | "cltd"; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1219 | break; |
| 1220 | case IceType_i64: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1221 | assert(Traits::Is64Bit); |
| 1222 | assert(SrcReg == Traits::getRaxOrDie()); |
| 1223 | assert(DestReg == Traits::getRdxOrDie()); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1224 | Str << "\t" |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1225 | "cqo"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1230 | template <typename TraitsType> |
| 1231 | void InstImpl<TraitsType>::InstX86Cbwdq::emitIAS(const Cfg *Func) const { |
| 1232 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1233 | assert(this->getSrcSize() == 1); |
| 1234 | Operand *Src0 = this->getSrc(0); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 1235 | const auto DestReg = this->getDest()->getRegNum(); |
| 1236 | const auto SrcReg = llvm::cast<Variable>(Src0)->getRegNum(); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1237 | (void)DestReg; |
| 1238 | (void)SrcReg; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1239 | switch (Src0->getType()) { |
| 1240 | default: |
| 1241 | llvm_unreachable("unexpected source type!"); |
| 1242 | break; |
| 1243 | case IceType_i8: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1244 | assert(SrcReg == RegisterSet::Reg_al); |
| 1245 | assert(DestReg == RegisterSet::Reg_ax || DestReg == RegisterSet::Reg_ah); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1246 | Asm->cbw(); |
| 1247 | break; |
| 1248 | case IceType_i16: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1249 | assert(SrcReg == RegisterSet::Reg_ax); |
| 1250 | assert(DestReg == RegisterSet::Reg_dx); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1251 | Asm->cwd(); |
| 1252 | break; |
| 1253 | case IceType_i32: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1254 | assert(SrcReg == RegisterSet::Reg_eax); |
| 1255 | assert(DestReg == RegisterSet::Reg_edx); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1256 | Asm->cdq(); |
| 1257 | break; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1258 | case IceType_i64: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1259 | assert(Traits::Is64Bit); |
| 1260 | assert(SrcReg == Traits::getRaxOrDie()); |
| 1261 | assert(DestReg == Traits::getRdxOrDie()); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1262 | Asm->cqo(); |
| 1263 | break; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1267 | template <typename TraitsType> |
| 1268 | void InstImpl<TraitsType>::InstX86Mul::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1269 | if (!BuildDefs::dump()) |
| 1270 | return; |
| 1271 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1272 | assert(this->getSrcSize() == 2); |
| 1273 | assert(llvm::isa<Variable>(this->getSrc(0))); |
| 1274 | assert(llvm::cast<Variable>(this->getSrc(0))->getRegNum() == |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1275 | RegisterSet::Reg_eax); |
| 1276 | assert(this->getDest()->getRegNum() == RegisterSet::Reg_eax); // TODO: |
| 1277 | // allow |
| 1278 | // edx? |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1279 | Str << "\t" |
| 1280 | "mul" << this->getWidthString(this->getDest()->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1281 | this->getSrc(1)->emit(Func); |
| 1282 | } |
| 1283 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1284 | template <typename TraitsType> |
| 1285 | void InstImpl<TraitsType>::InstX86Mul::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1286 | assert(this->getSrcSize() == 2); |
| 1287 | assert(llvm::isa<Variable>(this->getSrc(0))); |
| 1288 | assert(llvm::cast<Variable>(this->getSrc(0))->getRegNum() == |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1289 | RegisterSet::Reg_eax); |
| 1290 | assert(this->getDest()->getRegNum() == RegisterSet::Reg_eax); // TODO: |
| 1291 | // allow |
| 1292 | // edx? |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1293 | const Operand *Src = this->getSrc(1); |
| 1294 | Type Ty = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1295 | static const GPREmitterOneOp Emitter = {&Assembler::mul, &Assembler::mul}; |
| 1296 | emitIASOpTyGPR(Func, Ty, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1299 | template <typename TraitsType> |
| 1300 | void InstImpl<TraitsType>::InstX86Mul::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1301 | if (!BuildDefs::dump()) |
| 1302 | return; |
| 1303 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1304 | this->dumpDest(Func); |
| 1305 | Str << " = mul." << this->getDest()->getType() << " "; |
| 1306 | this->dumpSources(Func); |
| 1307 | } |
| 1308 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1309 | template <typename TraitsType> |
| 1310 | void InstImpl<TraitsType>::InstX86Shld::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1311 | if (!BuildDefs::dump()) |
| 1312 | return; |
| 1313 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1314 | Variable *Dest = this->getDest(); |
| 1315 | assert(this->getSrcSize() == 3); |
| 1316 | assert(Dest == this->getSrc(0)); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1317 | Str << "\t" |
| 1318 | "shld" << this->getWidthString(Dest->getType()) << "\t"; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1319 | this->getSrc(2)->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1320 | Str << ", "; |
| 1321 | this->getSrc(1)->emit(Func); |
| 1322 | Str << ", "; |
| 1323 | Dest->emit(Func); |
| 1324 | } |
| 1325 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1326 | template <typename TraitsType> |
| 1327 | void InstImpl<TraitsType>::InstX86Shld::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1328 | assert(this->getSrcSize() == 3); |
| 1329 | assert(this->getDest() == this->getSrc(0)); |
| 1330 | const Variable *Dest = this->getDest(); |
| 1331 | const Operand *Src1 = this->getSrc(1); |
| 1332 | const Operand *Src2 = this->getSrc(2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1333 | static const GPREmitterShiftD Emitter = {&Assembler::shld, &Assembler::shld}; |
| 1334 | emitIASGPRShiftDouble(Func, Dest, Src1, Src2, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1337 | template <typename TraitsType> |
| 1338 | void InstImpl<TraitsType>::InstX86Shld::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1339 | if (!BuildDefs::dump()) |
| 1340 | return; |
| 1341 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1342 | this->dumpDest(Func); |
| 1343 | Str << " = shld." << this->getDest()->getType() << " "; |
| 1344 | this->dumpSources(Func); |
| 1345 | } |
| 1346 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1347 | template <typename TraitsType> |
| 1348 | void InstImpl<TraitsType>::InstX86Shrd::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1349 | if (!BuildDefs::dump()) |
| 1350 | return; |
| 1351 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1352 | Variable *Dest = this->getDest(); |
| 1353 | assert(this->getSrcSize() == 3); |
| 1354 | assert(Dest == this->getSrc(0)); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1355 | Str << "\t" |
| 1356 | "shrd" << this->getWidthString(Dest->getType()) << "\t"; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1357 | this->getSrc(2)->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1358 | Str << ", "; |
| 1359 | this->getSrc(1)->emit(Func); |
| 1360 | Str << ", "; |
| 1361 | Dest->emit(Func); |
| 1362 | } |
| 1363 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1364 | template <typename TraitsType> |
| 1365 | void InstImpl<TraitsType>::InstX86Shrd::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1366 | assert(this->getSrcSize() == 3); |
| 1367 | assert(this->getDest() == this->getSrc(0)); |
| 1368 | const Variable *Dest = this->getDest(); |
| 1369 | const Operand *Src1 = this->getSrc(1); |
| 1370 | const Operand *Src2 = this->getSrc(2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1371 | static const GPREmitterShiftD Emitter = {&Assembler::shrd, &Assembler::shrd}; |
| 1372 | emitIASGPRShiftDouble(Func, Dest, Src1, Src2, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1375 | template <typename TraitsType> |
| 1376 | void InstImpl<TraitsType>::InstX86Shrd::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1377 | if (!BuildDefs::dump()) |
| 1378 | return; |
| 1379 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1380 | this->dumpDest(Func); |
| 1381 | Str << " = shrd." << this->getDest()->getType() << " "; |
| 1382 | this->dumpSources(Func); |
| 1383 | } |
| 1384 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1385 | template <typename TraitsType> |
| 1386 | void InstImpl<TraitsType>::InstX86Cmov::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1387 | if (!BuildDefs::dump()) |
| 1388 | return; |
| 1389 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1390 | Variable *Dest = this->getDest(); |
| 1391 | Str << "\t"; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1392 | assert(Condition != Cond::Br_None); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1393 | assert(this->getDest()->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1394 | Str << "cmov" << Traits::InstBrAttributes[Condition].DisplayString |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1395 | << this->getWidthString(Dest->getType()) << "\t"; |
| 1396 | this->getSrc(1)->emit(Func); |
| 1397 | Str << ", "; |
| 1398 | Dest->emit(Func); |
| 1399 | } |
| 1400 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1401 | template <typename TraitsType> |
| 1402 | void InstImpl<TraitsType>::InstX86Cmov::emitIAS(const Cfg *Func) const { |
| 1403 | assert(Condition != Cond::Br_None); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1404 | assert(this->getDest()->hasReg()); |
| 1405 | assert(this->getSrcSize() == 2); |
| 1406 | Operand *Src = this->getSrc(1); |
| 1407 | Type SrcTy = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1408 | assert(SrcTy == IceType_i16 || SrcTy == IceType_i32 || (Traits::Is64Bit)); |
| 1409 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 1410 | auto *Target = InstX86Base::getTarget(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1411 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
| 1412 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1413 | Asm->cmov(SrcTy, Condition, |
| 1414 | Traits::getEncodedGPR(this->getDest()->getRegNum()), |
| 1415 | Traits::getEncodedGPR(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1416 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1417 | Asm->cmov(SrcTy, Condition, |
| 1418 | Traits::getEncodedGPR(this->getDest()->getRegNum()), |
David Sehr | 21fd103 | 2015-11-13 16:32:37 -0800 | [diff] [blame] | 1419 | Target->stackVarToAsmOperand(SrcVar)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1420 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1421 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
| 1422 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
| 1423 | Asm->cmov(SrcTy, Condition, |
| 1424 | Traits::getEncodedGPR(this->getDest()->getRegNum()), |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1425 | Mem->toAsmAddress(Asm, Target)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1426 | } else { |
| 1427 | llvm_unreachable("Unexpected operand type"); |
| 1428 | } |
| 1429 | } |
| 1430 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1431 | template <typename TraitsType> |
| 1432 | void InstImpl<TraitsType>::InstX86Cmov::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1433 | if (!BuildDefs::dump()) |
| 1434 | return; |
| 1435 | Ostream &Str = Func->getContext()->getStrDump(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1436 | Str << "cmov" << Traits::InstBrAttributes[Condition].DisplayString << "."; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1437 | Str << this->getDest()->getType() << " "; |
| 1438 | this->dumpDest(Func); |
| 1439 | Str << ", "; |
| 1440 | this->dumpSources(Func); |
| 1441 | } |
| 1442 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1443 | template <typename TraitsType> |
| 1444 | void InstImpl<TraitsType>::InstX86Cmpps::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1445 | if (!BuildDefs::dump()) |
| 1446 | return; |
| 1447 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1448 | assert(this->getSrcSize() == 2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1449 | assert(Condition < Cond::Cmpps_Invalid); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1450 | Type DestTy = this->Dest->getType(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1451 | Str << "\t" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1452 | "cmp" << Traits::InstCmppsAttributes[Condition].EmitString |
| 1453 | << Traits::TypeAttributes[DestTy].PdPsString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1454 | this->getSrc(1)->emit(Func); |
| 1455 | Str << ", "; |
| 1456 | this->getDest()->emit(Func); |
| 1457 | } |
| 1458 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1459 | template <typename TraitsType> |
| 1460 | void InstImpl<TraitsType>::InstX86Cmpps::emitIAS(const Cfg *Func) const { |
| 1461 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1462 | assert(this->getSrcSize() == 2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1463 | assert(Condition < Cond::Cmpps_Invalid); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1464 | // Assuming there isn't any load folding for cmpps, and vector constants are |
| 1465 | // not allowed in PNaCl. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1466 | assert(llvm::isa<Variable>(this->getSrc(1))); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1467 | auto *Target = InstX86Base::getTarget(Func); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1468 | const auto *SrcVar = llvm::cast<Variable>(this->getSrc(1)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1469 | if (SrcVar->hasReg()) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1470 | Asm->cmpps(this->getDest()->getType(), |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1471 | Traits::getEncodedXmm(this->getDest()->getRegNum()), |
| 1472 | Traits::getEncodedXmm(SrcVar->getRegNum()), Condition); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1473 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1474 | Address SrcStackAddr = Target->stackVarToAsmOperand(SrcVar); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1475 | Asm->cmpps(this->getDest()->getType(), |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1476 | Traits::getEncodedXmm(this->getDest()->getRegNum()), |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1477 | SrcStackAddr, Condition); |
| 1478 | } |
| 1479 | } |
| 1480 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1481 | template <typename TraitsType> |
| 1482 | void InstImpl<TraitsType>::InstX86Cmpps::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1483 | if (!BuildDefs::dump()) |
| 1484 | return; |
| 1485 | Ostream &Str = Func->getContext()->getStrDump(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1486 | assert(Condition < Cond::Cmpps_Invalid); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1487 | this->dumpDest(Func); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1488 | Str << " = cmp" << Traits::InstCmppsAttributes[Condition].EmitString << "ps" |
| 1489 | "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1490 | this->dumpSources(Func); |
| 1491 | } |
| 1492 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1493 | template <typename TraitsType> |
| 1494 | void InstImpl<TraitsType>::InstX86Cmpxchg::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1495 | if (!BuildDefs::dump()) |
| 1496 | return; |
| 1497 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1498 | assert(this->getSrcSize() == 3); |
| 1499 | if (this->Locked) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1500 | Str << "\t" |
| 1501 | "lock"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1502 | } |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1503 | Str << "\t" |
| 1504 | "cmpxchg" << this->getWidthString(this->getSrc(0)->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1505 | this->getSrc(2)->emit(Func); |
| 1506 | Str << ", "; |
| 1507 | this->getSrc(0)->emit(Func); |
| 1508 | } |
| 1509 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1510 | template <typename TraitsType> |
| 1511 | void InstImpl<TraitsType>::InstX86Cmpxchg::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1512 | assert(this->getSrcSize() == 3); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1513 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1514 | Type Ty = this->getSrc(0)->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1515 | auto *Target = InstX86Base::getTarget(Func); |
| 1516 | const auto Mem = llvm::cast<X86OperandMem>(this->getSrc(0)); |
| 1517 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
| 1518 | const Address Addr = Mem->toAsmAddress(Asm, Target); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1519 | const auto *VarReg = llvm::cast<Variable>(this->getSrc(2)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1520 | assert(VarReg->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1521 | const GPRRegister Reg = Traits::getEncodedGPR(VarReg->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1522 | Asm->cmpxchg(Ty, Addr, Reg, this->Locked); |
| 1523 | } |
| 1524 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1525 | template <typename TraitsType> |
| 1526 | void InstImpl<TraitsType>::InstX86Cmpxchg::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1527 | if (!BuildDefs::dump()) |
| 1528 | return; |
| 1529 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1530 | if (this->Locked) { |
| 1531 | Str << "lock "; |
| 1532 | } |
| 1533 | Str << "cmpxchg." << this->getSrc(0)->getType() << " "; |
| 1534 | this->dumpSources(Func); |
| 1535 | } |
| 1536 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1537 | template <typename TraitsType> |
| 1538 | void InstImpl<TraitsType>::InstX86Cmpxchg8b::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1539 | if (!BuildDefs::dump()) |
| 1540 | return; |
| 1541 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1542 | assert(this->getSrcSize() == 5); |
| 1543 | if (this->Locked) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1544 | Str << "\t" |
| 1545 | "lock"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1546 | } |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1547 | Str << "\t" |
| 1548 | "cmpxchg8b\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1549 | this->getSrc(0)->emit(Func); |
| 1550 | } |
| 1551 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1552 | template <typename TraitsType> |
| 1553 | void InstImpl<TraitsType>::InstX86Cmpxchg8b::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1554 | assert(this->getSrcSize() == 5); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1555 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 1556 | const auto Mem = llvm::cast<X86OperandMem>(this->getSrc(0)); |
| 1557 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
| 1558 | auto *Target = InstX86Base::getTarget(Func); |
| 1559 | const Address Addr = Mem->toAsmAddress(Asm, Target); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1560 | Asm->cmpxchg8b(Addr, this->Locked); |
| 1561 | } |
| 1562 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1563 | template <typename TraitsType> |
| 1564 | void InstImpl<TraitsType>::InstX86Cmpxchg8b::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1565 | if (!BuildDefs::dump()) |
| 1566 | return; |
| 1567 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1568 | if (this->Locked) { |
| 1569 | Str << "lock "; |
| 1570 | } |
| 1571 | Str << "cmpxchg8b "; |
| 1572 | this->dumpSources(Func); |
| 1573 | } |
| 1574 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1575 | template <typename TraitsType> |
| 1576 | void InstImpl<TraitsType>::InstX86Cvt::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1577 | if (!BuildDefs::dump()) |
| 1578 | return; |
| 1579 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1580 | assert(this->getSrcSize() == 1); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1581 | Str << "\t" |
| 1582 | "cvt"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1583 | if (isTruncating()) |
| 1584 | Str << "t"; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1585 | Str << Traits::TypeAttributes[this->getSrc(0)->getType()].CvtString << "2" |
| 1586 | << Traits::TypeAttributes[this->getDest()->getType()].CvtString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1587 | this->getSrc(0)->emit(Func); |
| 1588 | Str << ", "; |
| 1589 | this->getDest()->emit(Func); |
| 1590 | } |
| 1591 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1592 | template <typename TraitsType> |
| 1593 | void InstImpl<TraitsType>::InstX86Cvt::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1594 | assert(this->getSrcSize() == 1); |
| 1595 | const Variable *Dest = this->getDest(); |
| 1596 | const Operand *Src = this->getSrc(0); |
| 1597 | Type DestTy = Dest->getType(); |
| 1598 | Type SrcTy = Src->getType(); |
| 1599 | switch (Variant) { |
| 1600 | case Si2ss: { |
| 1601 | assert(isScalarIntegerType(SrcTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1602 | if (!Traits::Is64Bit) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1603 | assert(typeWidthInBytes(SrcTy) <= 4); |
| 1604 | } else { |
| 1605 | assert(SrcTy == IceType_i32 || SrcTy == IceType_i64); |
| 1606 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1607 | assert(isScalarFloatingType(DestTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1608 | static const CastEmitterRegOp<XmmRegister, GPRRegister> Emitter = { |
| 1609 | &Assembler::cvtsi2ss, &Assembler::cvtsi2ss}; |
| 1610 | emitIASCastRegOp<XmmRegister, GPRRegister, Traits::getEncodedXmm, |
| 1611 | Traits::getEncodedGPR>(Func, DestTy, Dest, SrcTy, Src, |
| 1612 | Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1613 | return; |
| 1614 | } |
| 1615 | case Tss2si: { |
| 1616 | assert(isScalarFloatingType(SrcTy)); |
| 1617 | assert(isScalarIntegerType(DestTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1618 | if (!Traits::Is64Bit) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1619 | assert(typeWidthInBytes(DestTy) <= 4); |
| 1620 | } else { |
| 1621 | assert(DestTy == IceType_i32 || DestTy == IceType_i64); |
| 1622 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1623 | static const CastEmitterRegOp<GPRRegister, XmmRegister> Emitter = { |
| 1624 | &Assembler::cvttss2si, &Assembler::cvttss2si}; |
| 1625 | emitIASCastRegOp<GPRRegister, XmmRegister, Traits::getEncodedGPR, |
| 1626 | Traits::getEncodedXmm>(Func, DestTy, Dest, SrcTy, Src, |
| 1627 | Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1628 | return; |
| 1629 | } |
| 1630 | case Float2float: { |
| 1631 | assert(isScalarFloatingType(SrcTy)); |
| 1632 | assert(isScalarFloatingType(DestTy)); |
| 1633 | assert(DestTy != SrcTy); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1634 | static const XmmEmitterRegOp Emitter = {&Assembler::cvtfloat2float, |
| 1635 | &Assembler::cvtfloat2float}; |
| 1636 | emitIASRegOpTyXMM(Func, SrcTy, Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1637 | return; |
| 1638 | } |
| 1639 | case Dq2ps: { |
| 1640 | assert(isVectorIntegerType(SrcTy)); |
| 1641 | assert(isVectorFloatingType(DestTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1642 | static const XmmEmitterRegOp Emitter = {&Assembler::cvtdq2ps, |
| 1643 | &Assembler::cvtdq2ps}; |
| 1644 | emitIASRegOpTyXMM(Func, DestTy, Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1645 | return; |
| 1646 | } |
| 1647 | case Tps2dq: { |
| 1648 | assert(isVectorFloatingType(SrcTy)); |
| 1649 | assert(isVectorIntegerType(DestTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1650 | static const XmmEmitterRegOp Emitter = {&Assembler::cvttps2dq, |
| 1651 | &Assembler::cvttps2dq}; |
| 1652 | emitIASRegOpTyXMM(Func, DestTy, Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1658 | template <typename TraitsType> |
| 1659 | void InstImpl<TraitsType>::InstX86Cvt::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1660 | if (!BuildDefs::dump()) |
| 1661 | return; |
| 1662 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1663 | this->dumpDest(Func); |
| 1664 | Str << " = cvt"; |
| 1665 | if (isTruncating()) |
| 1666 | Str << "t"; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1667 | Str << Traits::TypeAttributes[this->getSrc(0)->getType()].CvtString << "2" |
| 1668 | << Traits::TypeAttributes[this->getDest()->getType()].CvtString << " "; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1669 | this->dumpSources(Func); |
| 1670 | } |
| 1671 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1672 | template <typename TraitsType> |
| 1673 | void InstImpl<TraitsType>::InstX86Icmp::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1674 | if (!BuildDefs::dump()) |
| 1675 | return; |
| 1676 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1677 | assert(this->getSrcSize() == 2); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1678 | Str << "\t" |
| 1679 | "cmp" << this->getWidthString(this->getSrc(0)->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1680 | this->getSrc(1)->emit(Func); |
| 1681 | Str << ", "; |
| 1682 | this->getSrc(0)->emit(Func); |
| 1683 | } |
| 1684 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1685 | template <typename TraitsType> |
| 1686 | void InstImpl<TraitsType>::InstX86Icmp::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1687 | assert(this->getSrcSize() == 2); |
| 1688 | const Operand *Src0 = this->getSrc(0); |
| 1689 | const Operand *Src1 = this->getSrc(1); |
| 1690 | Type Ty = Src0->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1691 | static const GPREmitterRegOp RegEmitter = {&Assembler::cmp, &Assembler::cmp, |
| 1692 | &Assembler::cmp}; |
| 1693 | static const GPREmitterAddrOp AddrEmitter = {&Assembler::cmp, |
| 1694 | &Assembler::cmp}; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1695 | if (const auto *SrcVar0 = llvm::dyn_cast<Variable>(Src0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1696 | if (SrcVar0->hasReg()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1697 | constexpr bool NotLea = false; |
| 1698 | emitIASRegOpTyGPR(Func, NotLea, Ty, SrcVar0, Src1, RegEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1699 | return; |
| 1700 | } |
| 1701 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1702 | emitIASAsAddrOpTyGPR(Func, Ty, Src0, Src1, AddrEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1705 | template <typename TraitsType> |
| 1706 | void InstImpl<TraitsType>::InstX86Icmp::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1707 | if (!BuildDefs::dump()) |
| 1708 | return; |
| 1709 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1710 | Str << "cmp." << this->getSrc(0)->getType() << " "; |
| 1711 | this->dumpSources(Func); |
| 1712 | } |
| 1713 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1714 | template <typename TraitsType> |
| 1715 | void InstImpl<TraitsType>::InstX86Ucomiss::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1716 | if (!BuildDefs::dump()) |
| 1717 | return; |
| 1718 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1719 | assert(this->getSrcSize() == 2); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1720 | Str << "\t" |
| 1721 | "ucomi" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1722 | << Traits::TypeAttributes[this->getSrc(0)->getType()].SdSsString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1723 | this->getSrc(1)->emit(Func); |
| 1724 | Str << ", "; |
| 1725 | this->getSrc(0)->emit(Func); |
| 1726 | } |
| 1727 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1728 | template <typename TraitsType> |
| 1729 | void InstImpl<TraitsType>::InstX86Ucomiss::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1730 | assert(this->getSrcSize() == 2); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1731 | // Currently src0 is always a variable by convention, to avoid having two |
| 1732 | // memory operands. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1733 | assert(llvm::isa<Variable>(this->getSrc(0))); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1734 | const auto *Src0Var = llvm::cast<Variable>(this->getSrc(0)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1735 | Type Ty = Src0Var->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1736 | static const XmmEmitterRegOp Emitter = {&Assembler::ucomiss, |
| 1737 | &Assembler::ucomiss}; |
| 1738 | emitIASRegOpTyXMM(Func, Ty, Src0Var, this->getSrc(1), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1741 | template <typename TraitsType> |
| 1742 | void InstImpl<TraitsType>::InstX86Ucomiss::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1743 | if (!BuildDefs::dump()) |
| 1744 | return; |
| 1745 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1746 | Str << "ucomiss." << this->getSrc(0)->getType() << " "; |
| 1747 | this->dumpSources(Func); |
| 1748 | } |
| 1749 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1750 | template <typename TraitsType> |
| 1751 | void InstImpl<TraitsType>::InstX86UD2::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1752 | if (!BuildDefs::dump()) |
| 1753 | return; |
| 1754 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1755 | assert(this->getSrcSize() == 0); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1756 | Str << "\t" |
| 1757 | "ud2"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1760 | template <typename TraitsType> |
| 1761 | void InstImpl<TraitsType>::InstX86UD2::emitIAS(const Cfg *Func) const { |
| 1762 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1763 | Asm->ud2(); |
| 1764 | } |
| 1765 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1766 | template <typename TraitsType> |
| 1767 | void InstImpl<TraitsType>::InstX86UD2::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1768 | if (!BuildDefs::dump()) |
| 1769 | return; |
| 1770 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1771 | Str << "ud2"; |
| 1772 | } |
| 1773 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1774 | template <typename TraitsType> |
| 1775 | void InstImpl<TraitsType>::InstX86Test::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1776 | if (!BuildDefs::dump()) |
| 1777 | return; |
| 1778 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1779 | assert(this->getSrcSize() == 2); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1780 | Str << "\t" |
| 1781 | "test" << this->getWidthString(this->getSrc(0)->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1782 | this->getSrc(1)->emit(Func); |
| 1783 | Str << ", "; |
| 1784 | this->getSrc(0)->emit(Func); |
| 1785 | } |
| 1786 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1787 | template <typename TraitsType> |
| 1788 | void InstImpl<TraitsType>::InstX86Test::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1789 | assert(this->getSrcSize() == 2); |
| 1790 | const Operand *Src0 = this->getSrc(0); |
| 1791 | const Operand *Src1 = this->getSrc(1); |
| 1792 | Type Ty = Src0->getType(); |
| 1793 | // The Reg/Addr form of test is not encodeable. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1794 | static const GPREmitterRegOp RegEmitter = {&Assembler::test, nullptr, |
| 1795 | &Assembler::test}; |
| 1796 | static const GPREmitterAddrOp AddrEmitter = {&Assembler::test, |
| 1797 | &Assembler::test}; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1798 | if (const auto *SrcVar0 = llvm::dyn_cast<Variable>(Src0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1799 | if (SrcVar0->hasReg()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1800 | constexpr bool NotLea = false; |
| 1801 | emitIASRegOpTyGPR(Func, NotLea, Ty, SrcVar0, Src1, RegEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1802 | return; |
| 1803 | } |
| 1804 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1805 | emitIASAsAddrOpTyGPR(Func, Ty, Src0, Src1, AddrEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1808 | template <typename TraitsType> |
| 1809 | void InstImpl<TraitsType>::InstX86Test::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1810 | if (!BuildDefs::dump()) |
| 1811 | return; |
| 1812 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1813 | Str << "test." << this->getSrc(0)->getType() << " "; |
| 1814 | this->dumpSources(Func); |
| 1815 | } |
| 1816 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1817 | template <typename TraitsType> |
| 1818 | void InstImpl<TraitsType>::InstX86Mfence::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1819 | if (!BuildDefs::dump()) |
| 1820 | return; |
| 1821 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1822 | assert(this->getSrcSize() == 0); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1823 | Str << "\t" |
| 1824 | "mfence"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1827 | template <typename TraitsType> |
| 1828 | void InstImpl<TraitsType>::InstX86Mfence::emitIAS(const Cfg *Func) const { |
| 1829 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1830 | Asm->mfence(); |
| 1831 | } |
| 1832 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1833 | template <typename TraitsType> |
| 1834 | void InstImpl<TraitsType>::InstX86Mfence::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1835 | if (!BuildDefs::dump()) |
| 1836 | return; |
| 1837 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1838 | Str << "mfence"; |
| 1839 | } |
| 1840 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1841 | template <typename TraitsType> |
| 1842 | void InstImpl<TraitsType>::InstX86Store::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1843 | if (!BuildDefs::dump()) |
| 1844 | return; |
| 1845 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1846 | assert(this->getSrcSize() == 2); |
| 1847 | Type Ty = this->getSrc(0)->getType(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1848 | Str << "\t" |
| 1849 | "mov" << this->getWidthString(Ty) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1850 | << Traits::TypeAttributes[Ty].SdSsString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1851 | this->getSrc(0)->emit(Func); |
| 1852 | Str << ", "; |
| 1853 | this->getSrc(1)->emit(Func); |
| 1854 | } |
| 1855 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1856 | template <typename TraitsType> |
| 1857 | void InstImpl<TraitsType>::InstX86Store::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1858 | assert(this->getSrcSize() == 2); |
| 1859 | const Operand *Dest = this->getSrc(1); |
| 1860 | const Operand *Src = this->getSrc(0); |
| 1861 | Type DestTy = Dest->getType(); |
| 1862 | if (isScalarFloatingType(DestTy)) { |
| 1863 | // Src must be a register, since Dest is a Mem operand of some kind. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1864 | const auto *SrcVar = llvm::cast<Variable>(Src); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1865 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1866 | XmmRegister SrcReg = Traits::getEncodedXmm(SrcVar->getRegNum()); |
| 1867 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 1868 | auto *Target = InstX86Base::getTarget(Func); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1869 | if (const auto *DestVar = llvm::dyn_cast<Variable>(Dest)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1870 | assert(!DestVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1871 | Address StackAddr(Target->stackVarToAsmOperand(DestVar)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1872 | Asm->movss(DestTy, StackAddr, SrcReg); |
| 1873 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1874 | const auto DestMem = llvm::cast<X86OperandMem>(Dest); |
| 1875 | assert(DestMem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1876 | Asm->movss(DestTy, DestMem->toAsmAddress(Asm, Target), SrcReg); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1877 | } |
| 1878 | return; |
| 1879 | } else { |
| 1880 | assert(isScalarIntegerType(DestTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1881 | static const GPREmitterAddrOp GPRAddrEmitter = {&Assembler::mov, |
| 1882 | &Assembler::mov}; |
| 1883 | emitIASAsAddrOpTyGPR(Func, DestTy, Dest, Src, GPRAddrEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1887 | template <typename TraitsType> |
| 1888 | void InstImpl<TraitsType>::InstX86Store::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1889 | if (!BuildDefs::dump()) |
| 1890 | return; |
| 1891 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1892 | Str << "mov." << this->getSrc(0)->getType() << " "; |
| 1893 | this->getSrc(1)->dump(Func); |
| 1894 | Str << ", "; |
| 1895 | this->getSrc(0)->dump(Func); |
| 1896 | } |
| 1897 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1898 | template <typename TraitsType> |
| 1899 | void InstImpl<TraitsType>::InstX86StoreP::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1900 | if (!BuildDefs::dump()) |
| 1901 | return; |
| 1902 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1903 | assert(this->getSrcSize() == 2); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 1904 | assert(isVectorType(this->getSrc(1)->getType())); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1905 | Str << "\t" |
| 1906 | "movups\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1907 | this->getSrc(0)->emit(Func); |
| 1908 | Str << ", "; |
| 1909 | this->getSrc(1)->emit(Func); |
| 1910 | } |
| 1911 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1912 | template <typename TraitsType> |
| 1913 | void InstImpl<TraitsType>::InstX86StoreP::emitIAS(const Cfg *Func) const { |
| 1914 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1915 | assert(this->getSrcSize() == 2); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1916 | const auto *SrcVar = llvm::cast<Variable>(this->getSrc(0)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1917 | const auto DestMem = llvm::cast<X86OperandMem>(this->getSrc(1)); |
| 1918 | assert(DestMem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1919 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1920 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1921 | Asm->movups(DestMem->toAsmAddress(Asm, Target), |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1922 | Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1925 | template <typename TraitsType> |
| 1926 | void InstImpl<TraitsType>::InstX86StoreP::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1927 | if (!BuildDefs::dump()) |
| 1928 | return; |
| 1929 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1930 | Str << "storep." << this->getSrc(0)->getType() << " "; |
| 1931 | this->getSrc(1)->dump(Func); |
| 1932 | Str << ", "; |
| 1933 | this->getSrc(0)->dump(Func); |
| 1934 | } |
| 1935 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1936 | template <typename TraitsType> |
| 1937 | void InstImpl<TraitsType>::InstX86StoreQ::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1938 | if (!BuildDefs::dump()) |
| 1939 | return; |
| 1940 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1941 | assert(this->getSrcSize() == 2); |
| 1942 | assert(this->getSrc(1)->getType() == IceType_i64 || |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 1943 | this->getSrc(1)->getType() == IceType_f64 || |
| 1944 | isVectorType(this->getSrc(1)->getType())); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1945 | Str << "\t" |
| 1946 | "movq\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1947 | this->getSrc(0)->emit(Func); |
| 1948 | Str << ", "; |
| 1949 | this->getSrc(1)->emit(Func); |
| 1950 | } |
| 1951 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1952 | template <typename TraitsType> |
| 1953 | void InstImpl<TraitsType>::InstX86StoreQ::emitIAS(const Cfg *Func) const { |
| 1954 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1955 | assert(this->getSrcSize() == 2); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1956 | const auto *SrcVar = llvm::cast<Variable>(this->getSrc(0)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1957 | const auto DestMem = llvm::cast<X86OperandMem>(this->getSrc(1)); |
| 1958 | assert(DestMem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1959 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1960 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1961 | Asm->movq(DestMem->toAsmAddress(Asm, Target), |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1962 | Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1965 | template <typename TraitsType> |
| 1966 | void InstImpl<TraitsType>::InstX86StoreQ::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1967 | if (!BuildDefs::dump()) |
| 1968 | return; |
| 1969 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1970 | Str << "storeq." << this->getSrc(0)->getType() << " "; |
| 1971 | this->getSrc(1)->dump(Func); |
| 1972 | Str << ", "; |
| 1973 | this->getSrc(0)->dump(Func); |
| 1974 | } |
| 1975 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1976 | template <typename TraitsType> |
| 1977 | void InstImpl<TraitsType>::InstX86Lea::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1978 | if (!BuildDefs::dump()) |
| 1979 | return; |
| 1980 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 1981 | assert(this->getSrcSize() == 1); |
| 1982 | assert(this->getDest()->hasReg()); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 1983 | Str << "\t" |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1984 | "lea" << this->getWidthString(this->getDest()->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1985 | Operand *Src0 = this->getSrc(0); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1986 | if (const auto *Src0Var = llvm::dyn_cast<Variable>(Src0)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1987 | Type Ty = Src0Var->getType(); |
| 1988 | // lea on x86-32 doesn't accept mem128 operands, so cast VSrc0 to an |
| 1989 | // acceptable type. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 1990 | Src0Var->asType(isVectorType(Ty) ? IceType_i32 : Ty, RegNumT::NoRegister) |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1991 | ->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 1992 | } else { |
| 1993 | Src0->emit(Func); |
| 1994 | } |
| 1995 | Str << ", "; |
| 1996 | this->getDest()->emit(Func); |
| 1997 | } |
| 1998 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1999 | template <typename TraitsType> |
| 2000 | void InstImpl<TraitsType>::InstX86Mov::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2001 | if (!BuildDefs::dump()) |
| 2002 | return; |
| 2003 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2004 | assert(this->getSrcSize() == 1); |
| 2005 | Operand *Src = this->getSrc(0); |
| 2006 | Type SrcTy = Src->getType(); |
| 2007 | Type DestTy = this->getDest()->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2008 | if (Traits::Is64Bit && DestTy == IceType_i64 && |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2009 | llvm::isa<ConstantInteger64>(Src)) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2010 | Str << "\t" |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2011 | "movabs" |
| 2012 | "\t"; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2013 | } else { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2014 | Str << "\t" |
| 2015 | "mov" << (!isScalarFloatingType(DestTy) |
| 2016 | ? this->getWidthString(DestTy) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2017 | : Traits::TypeAttributes[DestTy].SdSsString) << "\t"; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2018 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2019 | // For an integer truncation operation, src is wider than dest. In this case, |
| 2020 | // we use a mov instruction whose data width matches the narrower dest. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2021 | // TODO: This assert disallows usages such as copying a floating |
| 2022 | // point value between a vector and a scalar (which movss is used for). Clean |
| 2023 | // this up. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2024 | assert(InstX86Base::getTarget(Func)->typeWidthInBytesOnStack(DestTy) == |
| 2025 | InstX86Base::getTarget(Func)->typeWidthInBytesOnStack(SrcTy)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2026 | const Operand *NewSrc = Src; |
| 2027 | if (auto *SrcVar = llvm::dyn_cast<Variable>(Src)) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 2028 | auto NewRegNum = RegNumT::NoRegister; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2029 | if (SrcVar->hasReg()) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2030 | NewRegNum = Traits::getGprForType(DestTy, SrcVar->getRegNum()); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2031 | if (SrcTy != DestTy) |
| 2032 | NewSrc = SrcVar->asType(DestTy, NewRegNum); |
| 2033 | } |
| 2034 | NewSrc->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2035 | Str << ", "; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2036 | this->getDest()->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2039 | template <typename TraitsType> |
| 2040 | void InstImpl<TraitsType>::InstX86Mov::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2041 | assert(this->getSrcSize() == 1); |
| 2042 | const Variable *Dest = this->getDest(); |
| 2043 | const Operand *Src = this->getSrc(0); |
| 2044 | Type DestTy = Dest->getType(); |
| 2045 | Type SrcTy = Src->getType(); |
| 2046 | // Mov can be used for GPRs or XMM registers. Also, the type does not |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2047 | // necessarily match (Mov can be used for bitcasts). However, when the type |
| 2048 | // does not match, one of the operands must be a register. Thus, the strategy |
| 2049 | // is to find out if Src or Dest are a register, then use that register's |
| 2050 | // type to decide on which emitter set to use. The emitter set will include |
| 2051 | // reg-reg movs, but that case should be unused when the types don't match. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2052 | static const XmmEmitterRegOp XmmRegEmitter = {&Assembler::movss, |
| 2053 | &Assembler::movss}; |
| 2054 | static const GPREmitterRegOp GPRRegEmitter = { |
| 2055 | &Assembler::mov, &Assembler::mov, &Assembler::mov}; |
| 2056 | static const GPREmitterAddrOp GPRAddrEmitter = {&Assembler::mov, |
| 2057 | &Assembler::mov}; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2058 | // For an integer truncation operation, src is wider than dest. In this case, |
| 2059 | // we use a mov instruction whose data width matches the narrower dest. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2060 | // TODO: This assert disallows usages such as copying a floating |
| 2061 | // point value between a vector and a scalar (which movss is used for). Clean |
| 2062 | // this up. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2063 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 2064 | assert(Target->typeWidthInBytesOnStack(this->getDest()->getType()) == |
| 2065 | Target->typeWidthInBytesOnStack(Src->getType())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2066 | if (Dest->hasReg()) { |
| 2067 | if (isScalarFloatingType(DestTy)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2068 | emitIASRegOpTyXMM(Func, DestTy, Dest, Src, XmmRegEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2069 | return; |
| 2070 | } else { |
| 2071 | assert(isScalarIntegerType(DestTy)); |
| 2072 | // Widen DestTy for truncation (see above note). We should only do this |
| 2073 | // when both Src and Dest are integer types. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2074 | if (Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2075 | if (const auto *C64 = llvm::dyn_cast<ConstantInteger64>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2076 | Func->getAssembler<Assembler>()->movabs( |
| 2077 | Traits::getEncodedGPR(Dest->getRegNum()), C64->getValue()); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2078 | return; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2079 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2080 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2081 | if (isScalarIntegerType(SrcTy)) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2082 | SrcTy = DestTy; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2083 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2084 | constexpr bool NotLea = false; |
| 2085 | emitIASRegOpTyGPR(Func, NotLea, DestTy, Dest, Src, GPRRegEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2086 | return; |
| 2087 | } |
| 2088 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2089 | // Dest must be Stack and Src *could* be a register. Use Src's type to |
| 2090 | // decide on the emitters. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2091 | Address StackAddr(Target->stackVarToAsmOperand(Dest)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2092 | if (isScalarFloatingType(SrcTy)) { |
| 2093 | // Src must be a register. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2094 | const auto *SrcVar = llvm::cast<Variable>(Src); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2095 | assert(SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2096 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 2097 | Asm->movss(SrcTy, StackAddr, Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2098 | return; |
| 2099 | } else { |
| 2100 | // Src can be a register or immediate. |
| 2101 | assert(isScalarIntegerType(SrcTy)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2102 | emitIASAddrOpTyGPR(Func, SrcTy, StackAddr, Src, GPRAddrEmitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2103 | return; |
| 2104 | } |
| 2105 | return; |
| 2106 | } |
| 2107 | } |
| 2108 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2109 | template <typename TraitsType> |
| 2110 | void InstImpl<TraitsType>::InstX86Movd::emit(const Cfg *Func) const { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2111 | if (!BuildDefs::dump()) |
| 2112 | return; |
| 2113 | assert(this->getSrcSize() == 1); |
| 2114 | Variable *Dest = this->getDest(); |
| 2115 | Operand *Src = this->getSrc(0); |
| 2116 | |
| 2117 | if (Dest->getType() == IceType_i64 || Src->getType() == IceType_i64) { |
| 2118 | assert(Dest->getType() == IceType_f64 || Src->getType() == IceType_f64); |
| 2119 | assert(Dest->getType() != Src->getType()); |
| 2120 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2121 | Str << "\t" |
| 2122 | "movq" |
| 2123 | "\t"; |
| 2124 | Src->emit(Func); |
| 2125 | Str << ", "; |
| 2126 | Dest->emit(Func); |
| 2127 | return; |
| 2128 | } |
| 2129 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2130 | InstX86BaseUnaryopXmm<InstX86Base::Movd>::emit(Func); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2133 | template <typename TraitsType> |
| 2134 | void InstImpl<TraitsType>::InstX86Movd::emitIAS(const Cfg *Func) const { |
| 2135 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2136 | assert(this->getSrcSize() == 1); |
| 2137 | const Variable *Dest = this->getDest(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2138 | auto *Target = InstX86Base::getTarget(Func); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2139 | // For insert/extract element (one of Src/Dest is an Xmm vector and the other |
| 2140 | // is an int type). |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2141 | if (const auto *SrcVar = llvm::dyn_cast<Variable>(this->getSrc(0))) { |
| 2142 | if (SrcVar->getType() == IceType_i32 || |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2143 | (Traits::Is64Bit && SrcVar->getType() == IceType_i64)) { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2144 | assert(isVectorType(Dest->getType()) || |
| 2145 | (isScalarFloatingType(Dest->getType()) && |
| 2146 | typeWidthInBytes(SrcVar->getType()) == |
| 2147 | typeWidthInBytes(Dest->getType()))); |
| 2148 | assert(Dest->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2149 | XmmRegister DestReg = Traits::getEncodedXmm(Dest->getRegNum()); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2150 | if (SrcVar->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2151 | Asm->movd(SrcVar->getType(), DestReg, |
| 2152 | Traits::getEncodedGPR(SrcVar->getRegNum())); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2153 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2154 | Address StackAddr(Target->stackVarToAsmOperand(SrcVar)); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2155 | Asm->movd(SrcVar->getType(), DestReg, StackAddr); |
| 2156 | } |
| 2157 | } else { |
| 2158 | assert(isVectorType(SrcVar->getType()) || |
| 2159 | (isScalarFloatingType(SrcVar->getType()) && |
| 2160 | typeWidthInBytes(SrcVar->getType()) == |
| 2161 | typeWidthInBytes(Dest->getType()))); |
| 2162 | assert(SrcVar->hasReg()); |
| 2163 | assert(Dest->getType() == IceType_i32 || |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2164 | (Traits::Is64Bit && Dest->getType() == IceType_i64)); |
| 2165 | XmmRegister SrcReg = Traits::getEncodedXmm(SrcVar->getRegNum()); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2166 | if (Dest->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2167 | Asm->movd(Dest->getType(), Traits::getEncodedGPR(Dest->getRegNum()), |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2168 | SrcReg); |
| 2169 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2170 | Address StackAddr(Target->stackVarToAsmOperand(Dest)); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2171 | Asm->movd(Dest->getType(), StackAddr, SrcReg); |
| 2172 | } |
| 2173 | } |
| 2174 | } else { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2175 | assert(Dest->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2176 | XmmRegister DestReg = Traits::getEncodedXmm(Dest->getRegNum()); |
| 2177 | auto *Mem = llvm::cast<X86OperandMem>(this->getSrc(0)); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2178 | Asm->movd(Mem->getType(), DestReg, Mem->toAsmAddress(Asm, Target)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2182 | template <typename TraitsType> |
| 2183 | void InstImpl<TraitsType>::InstX86Movp::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2184 | if (!BuildDefs::dump()) |
| 2185 | return; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2186 | // TODO(wala,stichnot): movups works with all vector operands, but there |
| 2187 | // exist other instructions (movaps, movdqa, movdqu) that may perform better, |
| 2188 | // depending on the data type and alignment of the operands. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2189 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2190 | assert(this->getSrcSize() == 1); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2191 | Str << "\t" |
| 2192 | "movups\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2193 | this->getSrc(0)->emit(Func); |
| 2194 | Str << ", "; |
| 2195 | this->getDest()->emit(Func); |
| 2196 | } |
| 2197 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2198 | template <typename TraitsType> |
| 2199 | void InstImpl<TraitsType>::InstX86Movp::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2200 | assert(this->getSrcSize() == 1); |
| 2201 | assert(isVectorType(this->getDest()->getType())); |
| 2202 | const Variable *Dest = this->getDest(); |
| 2203 | const Operand *Src = this->getSrc(0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2204 | static const XmmEmitterMovOps Emitter = { |
| 2205 | &Assembler::movups, &Assembler::movups, &Assembler::movups}; |
| 2206 | emitIASMovlikeXMM(Func, Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2209 | template <typename TraitsType> |
| 2210 | void InstImpl<TraitsType>::InstX86Movq::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2211 | if (!BuildDefs::dump()) |
| 2212 | return; |
| 2213 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2214 | assert(this->getSrcSize() == 1); |
| 2215 | assert(this->getDest()->getType() == IceType_i64 || |
| 2216 | this->getDest()->getType() == IceType_f64); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2217 | Str << "\t" |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2218 | "movq" |
| 2219 | "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2220 | this->getSrc(0)->emit(Func); |
| 2221 | Str << ", "; |
| 2222 | this->getDest()->emit(Func); |
| 2223 | } |
| 2224 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2225 | template <typename TraitsType> |
| 2226 | void InstImpl<TraitsType>::InstX86Movq::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2227 | assert(this->getSrcSize() == 1); |
| 2228 | assert(this->getDest()->getType() == IceType_i64 || |
| 2229 | this->getDest()->getType() == IceType_f64); |
| 2230 | const Variable *Dest = this->getDest(); |
| 2231 | const Operand *Src = this->getSrc(0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2232 | static const XmmEmitterMovOps Emitter = {&Assembler::movq, &Assembler::movq, |
| 2233 | &Assembler::movq}; |
| 2234 | emitIASMovlikeXMM(Func, Dest, Src, Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2237 | template <typename TraitsType> |
| 2238 | void InstImpl<TraitsType>::InstX86MovssRegs::emitIAS(const Cfg *Func) const { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2239 | // This is Binop variant is only intended to be used for reg-reg moves where |
| 2240 | // part of the Dest register is untouched. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2241 | assert(this->getSrcSize() == 2); |
| 2242 | const Variable *Dest = this->getDest(); |
| 2243 | assert(Dest == this->getSrc(0)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2244 | const auto *SrcVar = llvm::cast<Variable>(this->getSrc(1)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2245 | assert(Dest->hasReg() && SrcVar->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2246 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 2247 | Asm->movss(IceType_f32, Traits::getEncodedXmm(Dest->getRegNum()), |
| 2248 | Traits::getEncodedXmm(SrcVar->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2251 | template <typename TraitsType> |
| 2252 | void InstImpl<TraitsType>::InstX86Movsx::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2253 | assert(this->getSrcSize() == 1); |
| 2254 | const Variable *Dest = this->getDest(); |
| 2255 | const Operand *Src = this->getSrc(0); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2256 | // Dest must be a > 8-bit register, but Src can be 8-bit. In practice we just |
| 2257 | // use the full register for Dest to avoid having an OperandSizeOverride |
| 2258 | // prefix. It also allows us to only dispatch on SrcTy. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2259 | Type SrcTy = Src->getType(); |
| 2260 | assert(typeWidthInBytes(Dest->getType()) > 1); |
| 2261 | assert(typeWidthInBytes(Dest->getType()) > typeWidthInBytes(SrcTy)); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2262 | constexpr bool NotLea = false; |
| 2263 | emitIASRegOpTyGPR<false, true>(Func, NotLea, SrcTy, Dest, Src, this->Emitter); |
| 2264 | } |
| 2265 | |
| 2266 | template <typename TraitsType> |
| 2267 | bool InstImpl<TraitsType>::InstX86Movzx::mayBeElided( |
| 2268 | const Variable *Dest, const Operand *SrcOpnd) const { |
| 2269 | assert(Traits::Is64Bit); |
| 2270 | const auto *Src = llvm::dyn_cast<Variable>(SrcOpnd); |
| 2271 | |
| 2272 | // Src is not a Variable, so it does not have a register. Movzx can't be |
| 2273 | // elided. |
| 2274 | if (Src == nullptr) |
| 2275 | return false; |
| 2276 | |
| 2277 | // Movzx to/from memory can't be elided. |
| 2278 | if (!Src->hasReg() || !Dest->hasReg()) |
| 2279 | return false; |
| 2280 | |
| 2281 | // Reg/reg move with different source and dest can't be elided. |
| 2282 | if (Traits::getEncodedGPR(Src->getRegNum()) != |
| 2283 | Traits::getEncodedGPR(Dest->getRegNum())) |
| 2284 | return false; |
| 2285 | |
| 2286 | // A must-keep movzx 32- to 64-bit is sometimes needed in x86-64 sandboxing. |
| 2287 | return !MustKeep; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2290 | template <typename TraitsType> |
| 2291 | void InstImpl<TraitsType>::InstX86Movzx::emit(const Cfg *Func) const { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2292 | if (!BuildDefs::dump()) |
| 2293 | return; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2294 | if (Traits::Is64Bit) { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2295 | // There's no movzx %eXX, %rXX. To zero extend 32- to 64-bits, we emit a |
| 2296 | // mov %eXX, %eXX. The processor will still do a movzx[bw]q. |
| 2297 | assert(this->getSrcSize() == 1); |
| 2298 | const Operand *Src = this->getSrc(0); |
| 2299 | const Variable *Dest = this->Dest; |
| 2300 | if (Src->getType() == IceType_i32 && Dest->getType() == IceType_i64) { |
| 2301 | Ostream &Str = Func->getContext()->getStrEmit(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2302 | if (mayBeElided(Dest, Src)) { |
| 2303 | Str << "\t/* elided movzx */"; |
| 2304 | } else { |
| 2305 | Str << "\t" |
| 2306 | "mov" |
| 2307 | "\t"; |
| 2308 | Src->emit(Func); |
| 2309 | Str << ", "; |
| 2310 | Dest->asType(IceType_i32, |
| 2311 | Traits::getGprForType(IceType_i32, Dest->getRegNum())) |
| 2312 | ->emit(Func); |
| 2313 | Str << " /* movzx */"; |
| 2314 | } |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2315 | return; |
| 2316 | } |
| 2317 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2318 | InstX86BaseUnaryopGPR<InstX86Base::Movzx>::emit(Func); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2319 | } |
| 2320 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2321 | template <typename TraitsType> |
| 2322 | void InstImpl<TraitsType>::InstX86Movzx::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2323 | assert(this->getSrcSize() == 1); |
| 2324 | const Variable *Dest = this->getDest(); |
| 2325 | const Operand *Src = this->getSrc(0); |
| 2326 | Type SrcTy = Src->getType(); |
| 2327 | assert(typeWidthInBytes(Dest->getType()) > 1); |
| 2328 | assert(typeWidthInBytes(Dest->getType()) > typeWidthInBytes(SrcTy)); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2329 | if (Traits::Is64Bit) { |
| 2330 | if (Src->getType() == IceType_i32 && Dest->getType() == IceType_i64 && |
| 2331 | mayBeElided(Dest, Src)) { |
| 2332 | return; |
| 2333 | } |
| 2334 | } |
| 2335 | constexpr bool NotLea = false; |
| 2336 | emitIASRegOpTyGPR<false, true>(Func, NotLea, SrcTy, Dest, Src, this->Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2339 | template <typename TraitsType> |
| 2340 | void InstImpl<TraitsType>::InstX86Nop::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2341 | if (!BuildDefs::dump()) |
| 2342 | return; |
| 2343 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2344 | // TODO: Emit the right code for each variant. |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2345 | Str << "\t" |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 2346 | "nop\t/* variant = " << Variant << " */"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2349 | template <typename TraitsType> |
| 2350 | void InstImpl<TraitsType>::InstX86Nop::emitIAS(const Cfg *Func) const { |
| 2351 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2352 | // TODO: Emit the right code for the variant. |
| 2353 | Asm->nop(); |
| 2354 | } |
| 2355 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2356 | template <typename TraitsType> |
| 2357 | void InstImpl<TraitsType>::InstX86Nop::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2358 | if (!BuildDefs::dump()) |
| 2359 | return; |
| 2360 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2361 | Str << "nop (variant = " << Variant << ")"; |
| 2362 | } |
| 2363 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2364 | template <typename TraitsType> |
| 2365 | void InstImpl<TraitsType>::InstX86Fld::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2366 | if (!BuildDefs::dump()) |
| 2367 | return; |
| 2368 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2369 | assert(this->getSrcSize() == 1); |
| 2370 | Type Ty = this->getSrc(0)->getType(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2371 | const auto *Var = llvm::dyn_cast<Variable>(this->getSrc(0)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2372 | if (Var && Var->hasReg()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2373 | // This is a physical xmm register, so we need to spill it to a temporary |
David Sehr | 0d9cf48 | 2015-11-16 17:00:38 -0800 | [diff] [blame] | 2374 | // stack slot. Function prolog emission guarantees that there is sufficient |
| 2375 | // space to do this. |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2376 | Str << "\t" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2377 | "mov" << Traits::TypeAttributes[Ty].SdSsString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2378 | Var->emit(Func); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2379 | Str << ", (%esp)\n" |
| 2380 | "\t" |
| 2381 | "fld" << this->getFldString(Ty) << "\t" |
| 2382 | "(%esp)"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2383 | return; |
| 2384 | } |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2385 | Str << "\t" |
| 2386 | "fld" << this->getFldString(Ty) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2387 | this->getSrc(0)->emit(Func); |
| 2388 | } |
| 2389 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2390 | template <typename TraitsType> |
| 2391 | void InstImpl<TraitsType>::InstX86Fld::emitIAS(const Cfg *Func) const { |
| 2392 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2393 | assert(this->getSrcSize() == 1); |
| 2394 | const Operand *Src = this->getSrc(0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2395 | auto *Target = InstX86Base::getTarget(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2396 | Type Ty = Src->getType(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2397 | if (const auto *Var = llvm::dyn_cast<Variable>(Src)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2398 | if (Var->hasReg()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2399 | // This is a physical xmm register, so we need to spill it to a temporary |
David Sehr | 0d9cf48 | 2015-11-16 17:00:38 -0800 | [diff] [blame] | 2400 | // stack slot. Function prolog emission guarantees that there is |
| 2401 | // sufficient space to do this. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2402 | Address StackSlot = |
| 2403 | Address(RegisterSet::Encoded_Reg_esp, 0, AssemblerFixup::NoFixup); |
| 2404 | Asm->movss(Ty, StackSlot, Traits::getEncodedXmm(Var->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2405 | Asm->fld(Ty, StackSlot); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2406 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2407 | Address StackAddr(Target->stackVarToAsmOperand(Var)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2408 | Asm->fld(Ty, StackAddr); |
| 2409 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2410 | } else if (const auto *Mem = llvm::dyn_cast<X86OperandMem>(Src)) { |
| 2411 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 2412 | Asm->fld(Ty, Mem->toAsmAddress(Asm, Target)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2413 | } else if (const auto *Imm = llvm::dyn_cast<Constant>(Src)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2414 | Asm->fld(Ty, Traits::Address::ofConstPool(Asm, Imm)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2415 | } else { |
| 2416 | llvm_unreachable("Unexpected operand type"); |
| 2417 | } |
| 2418 | } |
| 2419 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2420 | template <typename TraitsType> |
| 2421 | void InstImpl<TraitsType>::InstX86Fld::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2422 | if (!BuildDefs::dump()) |
| 2423 | return; |
| 2424 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2425 | Str << "fld." << this->getSrc(0)->getType() << " "; |
| 2426 | this->dumpSources(Func); |
| 2427 | } |
| 2428 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2429 | template <typename TraitsType> |
| 2430 | void InstImpl<TraitsType>::InstX86Fstp::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2431 | if (!BuildDefs::dump()) |
| 2432 | return; |
| 2433 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2434 | assert(this->getSrcSize() == 0); |
| 2435 | // TODO(jvoung,stichnot): Utilize this by setting Dest to nullptr to |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2436 | // "partially" delete the fstp if the Dest is unused. Even if Dest is unused, |
| 2437 | // the fstp should be kept for the SideEffects of popping the stack. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2438 | if (!this->getDest()) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2439 | Str << "\t" |
| 2440 | "fstp\t" |
| 2441 | "st(0)"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2442 | return; |
| 2443 | } |
| 2444 | Type Ty = this->getDest()->getType(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2445 | if (!this->getDest()->hasReg()) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2446 | Str << "\t" |
| 2447 | "fstp" << this->getFldString(Ty) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2448 | this->getDest()->emit(Func); |
| 2449 | return; |
| 2450 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2451 | // Dest is a physical (xmm) register, so st(0) needs to go through memory. |
David Sehr | 21fd103 | 2015-11-13 16:32:37 -0800 | [diff] [blame] | 2452 | // Hack this by using caller-reserved memory at the top of stack, spilling |
| 2453 | // st(0) there, and loading it into the xmm register. |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2454 | Str << "\t" |
| 2455 | "fstp" << this->getFldString(Ty) << "\t" |
| 2456 | "(%esp)\n"; |
| 2457 | Str << "\t" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2458 | "mov" << Traits::TypeAttributes[Ty].SdSsString << "\t" |
| 2459 | "(%esp), "; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2460 | this->getDest()->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2463 | template <typename TraitsType> |
| 2464 | void InstImpl<TraitsType>::InstX86Fstp::emitIAS(const Cfg *Func) const { |
| 2465 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2466 | assert(this->getSrcSize() == 0); |
| 2467 | const Variable *Dest = this->getDest(); |
| 2468 | // TODO(jvoung,stichnot): Utilize this by setting Dest to nullptr to |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2469 | // "partially" delete the fstp if the Dest is unused. Even if Dest is unused, |
| 2470 | // the fstp should be kept for the SideEffects of popping the stack. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2471 | if (!Dest) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2472 | Asm->fstp(RegisterSet::getEncodedSTReg(0)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2473 | return; |
| 2474 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2475 | auto *Target = InstX86Base::getTarget(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2476 | Type Ty = Dest->getType(); |
| 2477 | if (!Dest->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2478 | Address StackAddr(Target->stackVarToAsmOperand(Dest)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2479 | Asm->fstp(Ty, StackAddr); |
| 2480 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2481 | // Dest is a physical (xmm) register, so st(0) needs to go through memory. |
David Sehr | 21fd103 | 2015-11-13 16:32:37 -0800 | [diff] [blame] | 2482 | // Hack this by using caller-reserved memory at the top of stack, spilling |
| 2483 | // st(0) there, and loading it into the xmm register. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2484 | Address StackSlot = |
| 2485 | Address(RegisterSet::Encoded_Reg_esp, 0, AssemblerFixup::NoFixup); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2486 | Asm->fstp(Ty, StackSlot); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2487 | Asm->movss(Ty, Traits::getEncodedXmm(Dest->getRegNum()), StackSlot); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2488 | } |
| 2489 | } |
| 2490 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2491 | template <typename TraitsType> |
| 2492 | void InstImpl<TraitsType>::InstX86Fstp::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2493 | if (!BuildDefs::dump()) |
| 2494 | return; |
| 2495 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2496 | this->dumpDest(Func); |
| 2497 | Str << " = fstp." << this->getDest()->getType() << ", st(0)"; |
| 2498 | } |
| 2499 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2500 | template <typename TraitsType> |
| 2501 | void InstImpl<TraitsType>::InstX86Pextr::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2502 | if (!BuildDefs::dump()) |
| 2503 | return; |
| 2504 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2505 | assert(this->getSrcSize() == 2); |
| 2506 | // pextrb and pextrd are SSE4.1 instructions. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2507 | Str << "\t" << this->Opcode |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2508 | << Traits::TypeAttributes[this->getSrc(0)->getType()].PackString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2509 | this->getSrc(1)->emit(Func); |
| 2510 | Str << ", "; |
| 2511 | this->getSrc(0)->emit(Func); |
| 2512 | Str << ", "; |
| 2513 | Variable *Dest = this->getDest(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2514 | // pextrw must take a register dest. There is an SSE4.1 version that takes a |
| 2515 | // memory dest, but we aren't using it. For uniformity, just restrict them |
| 2516 | // all to have a register dest for now. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2517 | assert(Dest->hasReg()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2518 | Dest->asType(IceType_i32, Dest->getRegNum())->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2521 | template <typename TraitsType> |
| 2522 | void InstImpl<TraitsType>::InstX86Pextr::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2523 | assert(this->getSrcSize() == 2); |
| 2524 | // pextrb and pextrd are SSE4.1 instructions. |
| 2525 | const Variable *Dest = this->getDest(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2526 | Type DispatchTy = Traits::getInVectorElementType(this->getSrc(0)->getType()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2527 | // pextrw must take a register dest. There is an SSE4.1 version that takes a |
| 2528 | // memory dest, but we aren't using it. For uniformity, just restrict them |
| 2529 | // all to have a register dest for now. |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2530 | assert(Dest->hasReg()); |
| 2531 | // pextrw's Src(0) must be a register (both SSE4.1 and SSE2). |
| 2532 | assert(llvm::cast<Variable>(this->getSrc(0))->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2533 | static const ThreeOpImmEmitter<GPRRegister, XmmRegister> Emitter = { |
| 2534 | &Assembler::pextr, nullptr}; |
| 2535 | emitIASThreeOpImmOps<GPRRegister, XmmRegister, Traits::getEncodedGPR, |
| 2536 | Traits::getEncodedXmm>( |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2537 | Func, DispatchTy, Dest, this->getSrc(0), this->getSrc(1), Emitter); |
| 2538 | } |
| 2539 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2540 | template <typename TraitsType> |
| 2541 | void InstImpl<TraitsType>::InstX86Pinsr::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2542 | if (!BuildDefs::dump()) |
| 2543 | return; |
| 2544 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2545 | assert(this->getSrcSize() == 3); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2546 | Str << "\t" << this->Opcode |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2547 | << Traits::TypeAttributes[this->getDest()->getType()].PackString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2548 | this->getSrc(2)->emit(Func); |
| 2549 | Str << ", "; |
| 2550 | Operand *Src1 = this->getSrc(1); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2551 | if (const auto *Src1Var = llvm::dyn_cast<Variable>(Src1)) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2552 | // If src1 is a register, it should always be r32. |
| 2553 | if (Src1Var->hasReg()) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 2554 | const auto NewRegNum = Traits::getBaseReg(Src1Var->getRegNum()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2555 | const Variable *NewSrc = Src1Var->asType(IceType_i32, NewRegNum); |
| 2556 | NewSrc->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2557 | } else { |
| 2558 | Src1Var->emit(Func); |
| 2559 | } |
| 2560 | } else { |
| 2561 | Src1->emit(Func); |
| 2562 | } |
| 2563 | Str << ", "; |
| 2564 | this->getDest()->emit(Func); |
| 2565 | } |
| 2566 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2567 | template <typename TraitsType> |
| 2568 | void InstImpl<TraitsType>::InstX86Pinsr::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2569 | assert(this->getSrcSize() == 3); |
| 2570 | assert(this->getDest() == this->getSrc(0)); |
| 2571 | // pinsrb and pinsrd are SSE4.1 instructions. |
| 2572 | const Operand *Src0 = this->getSrc(1); |
| 2573 | Type DispatchTy = Src0->getType(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2574 | // If src1 is a register, it should always be r32 (this should fall out from |
| 2575 | // the encodings for ByteRegs overlapping the encodings for r32), but we have |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2576 | // to make sure the register allocator didn't choose an 8-bit high register |
| 2577 | // like "ah". |
| 2578 | if (BuildDefs::asserts()) { |
| 2579 | if (auto *Src0Var = llvm::dyn_cast<Variable>(Src0)) { |
| 2580 | if (Src0Var->hasReg()) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame^] | 2581 | const auto RegNum = Src0Var->getRegNum(); |
| 2582 | const auto BaseRegNum = Traits::getBaseReg(RegNum); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2583 | (void)BaseRegNum; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2584 | assert(Traits::getEncodedGPR(RegNum) == |
| 2585 | Traits::getEncodedGPR(BaseRegNum)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2589 | static const ThreeOpImmEmitter<XmmRegister, GPRRegister> Emitter = { |
| 2590 | &Assembler::pinsr, &Assembler::pinsr}; |
| 2591 | emitIASThreeOpImmOps<XmmRegister, GPRRegister, Traits::getEncodedXmm, |
| 2592 | Traits::getEncodedGPR>(Func, DispatchTy, this->getDest(), |
| 2593 | Src0, this->getSrc(2), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2596 | template <typename TraitsType> |
| 2597 | void InstImpl<TraitsType>::InstX86Pshufd::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2598 | assert(this->getSrcSize() == 2); |
| 2599 | const Variable *Dest = this->getDest(); |
| 2600 | Type Ty = Dest->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2601 | static const ThreeOpImmEmitter<XmmRegister, XmmRegister> Emitter = { |
| 2602 | &Assembler::pshufd, &Assembler::pshufd}; |
| 2603 | emitIASThreeOpImmOps<XmmRegister, XmmRegister, Traits::getEncodedXmm, |
| 2604 | Traits::getEncodedXmm>(Func, Ty, Dest, this->getSrc(0), |
| 2605 | this->getSrc(1), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2608 | template <typename TraitsType> |
| 2609 | void InstImpl<TraitsType>::InstX86Shufps::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2610 | assert(this->getSrcSize() == 3); |
| 2611 | const Variable *Dest = this->getDest(); |
| 2612 | assert(Dest == this->getSrc(0)); |
| 2613 | Type Ty = Dest->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2614 | static const ThreeOpImmEmitter<XmmRegister, XmmRegister> Emitter = { |
| 2615 | &Assembler::shufps, &Assembler::shufps}; |
| 2616 | emitIASThreeOpImmOps<XmmRegister, XmmRegister, Traits::getEncodedXmm, |
| 2617 | Traits::getEncodedXmm>(Func, Ty, Dest, this->getSrc(1), |
| 2618 | this->getSrc(2), Emitter); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2621 | template <typename TraitsType> |
| 2622 | void InstImpl<TraitsType>::InstX86Pop::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2623 | if (!BuildDefs::dump()) |
| 2624 | return; |
| 2625 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2626 | assert(this->getSrcSize() == 0); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2627 | Str << "\t" |
| 2628 | "pop\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2629 | this->getDest()->emit(Func); |
| 2630 | } |
| 2631 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2632 | template <typename TraitsType> |
| 2633 | void InstImpl<TraitsType>::InstX86Pop::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2634 | assert(this->getSrcSize() == 0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2635 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2636 | if (this->getDest()->hasReg()) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2637 | Asm->popl(Traits::getEncodedGPR(this->getDest()->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2638 | } else { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2639 | auto *Target = InstX86Base::getTarget(Func); |
David Sehr | b8e49c1 | 2015-11-12 14:41:22 -0800 | [diff] [blame] | 2640 | Asm->popl(Target->stackVarToAsmOperand(this->getDest())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2641 | } |
| 2642 | } |
| 2643 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2644 | template <typename TraitsType> |
| 2645 | void InstImpl<TraitsType>::InstX86Pop::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2646 | if (!BuildDefs::dump()) |
| 2647 | return; |
| 2648 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2649 | this->dumpDest(Func); |
| 2650 | Str << " = pop." << this->getDest()->getType() << " "; |
| 2651 | } |
| 2652 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2653 | template <typename TraitsType> |
| 2654 | void InstImpl<TraitsType>::InstX86Push::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2655 | if (!BuildDefs::dump()) |
| 2656 | return; |
| 2657 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2658 | Str << "\t" |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2659 | "push" |
| 2660 | "\t"; |
| 2661 | assert(this->getSrcSize() == 1); |
| 2662 | const Operand *Src = this->getSrc(0); |
| 2663 | Src->emit(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2666 | template <typename TraitsType> |
| 2667 | void InstImpl<TraitsType>::InstX86Push::emitIAS(const Cfg *Func) const { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2668 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 2669 | |
| 2670 | assert(this->getSrcSize() == 1); |
| 2671 | const Operand *Src = this->getSrc(0); |
| 2672 | |
| 2673 | if (const auto *Var = llvm::dyn_cast<Variable>(Src)) { |
| 2674 | Asm->pushl(Traits::getEncodedGPR(Var->getRegNum())); |
| 2675 | } else if (const auto *Const32 = llvm::dyn_cast<ConstantInteger32>(Src)) { |
| 2676 | Asm->pushl(AssemblerImmediate(Const32->getValue())); |
| 2677 | } else if (auto *CR = llvm::dyn_cast<ConstantRelocatable>(Src)) { |
| 2678 | Asm->pushl(CR); |
| 2679 | } else { |
| 2680 | llvm_unreachable("Unexpected operand type"); |
| 2681 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2682 | } |
| 2683 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2684 | template <typename TraitsType> |
| 2685 | void InstImpl<TraitsType>::InstX86Push::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2686 | if (!BuildDefs::dump()) |
| 2687 | return; |
| 2688 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2689 | Str << "push." << this->getSrc(0)->getType() << " "; |
| 2690 | this->dumpSources(Func); |
| 2691 | } |
| 2692 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2693 | template <typename TraitsType> |
| 2694 | void InstImpl<TraitsType>::InstX86Ret::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2695 | if (!BuildDefs::dump()) |
| 2696 | return; |
| 2697 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2698 | Str << "\t" |
| 2699 | "ret"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2702 | template <typename TraitsType> |
| 2703 | void InstImpl<TraitsType>::InstX86Ret::emitIAS(const Cfg *Func) const { |
| 2704 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2705 | Asm->ret(); |
| 2706 | } |
| 2707 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2708 | template <typename TraitsType> |
| 2709 | void InstImpl<TraitsType>::InstX86Ret::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2710 | if (!BuildDefs::dump()) |
| 2711 | return; |
| 2712 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2713 | Type Ty = |
| 2714 | (this->getSrcSize() == 0 ? IceType_void : this->getSrc(0)->getType()); |
| 2715 | Str << "ret." << Ty << " "; |
| 2716 | this->dumpSources(Func); |
| 2717 | } |
| 2718 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2719 | template <typename TraitsType> |
| 2720 | void InstImpl<TraitsType>::InstX86Setcc::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2721 | if (!BuildDefs::dump()) |
| 2722 | return; |
| 2723 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2724 | Str << "\t" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2725 | "set" << Traits::InstBrAttributes[Condition].DisplayString << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2726 | this->Dest->emit(Func); |
| 2727 | } |
| 2728 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2729 | template <typename TraitsType> |
| 2730 | void InstImpl<TraitsType>::InstX86Setcc::emitIAS(const Cfg *Func) const { |
| 2731 | assert(Condition != Cond::Br_None); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2732 | assert(this->getDest()->getType() == IceType_i1); |
| 2733 | assert(this->getSrcSize() == 0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2734 | Assembler *Asm = Func->getAssembler<Assembler>(); |
| 2735 | auto *Target = InstX86Base::getTarget(Func); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2736 | if (this->getDest()->hasReg()) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2737 | Asm->setcc(Condition, |
| 2738 | Traits::getEncodedByteReg(this->getDest()->getRegNum())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2739 | else |
David Sehr | b8e49c1 | 2015-11-12 14:41:22 -0800 | [diff] [blame] | 2740 | Asm->setcc(Condition, Target->stackVarToAsmOperand(this->getDest())); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2741 | return; |
| 2742 | } |
| 2743 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2744 | template <typename TraitsType> |
| 2745 | void InstImpl<TraitsType>::InstX86Setcc::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2746 | if (!BuildDefs::dump()) |
| 2747 | return; |
| 2748 | Ostream &Str = Func->getContext()->getStrDump(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2749 | Str << "setcc." << Traits::InstBrAttributes[Condition].DisplayString << " "; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2750 | this->dumpDest(Func); |
| 2751 | } |
| 2752 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2753 | template <typename TraitsType> |
| 2754 | void InstImpl<TraitsType>::InstX86Xadd::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2755 | if (!BuildDefs::dump()) |
| 2756 | return; |
| 2757 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2758 | if (this->Locked) { |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2759 | Str << "\t" |
| 2760 | "lock"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2761 | } |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2762 | Str << "\t" |
| 2763 | "xadd" << this->getWidthString(this->getSrc(0)->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2764 | this->getSrc(1)->emit(Func); |
| 2765 | Str << ", "; |
| 2766 | this->getSrc(0)->emit(Func); |
| 2767 | } |
| 2768 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2769 | template <typename TraitsType> |
| 2770 | void InstImpl<TraitsType>::InstX86Xadd::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2771 | assert(this->getSrcSize() == 2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2772 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2773 | Type Ty = this->getSrc(0)->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2774 | const auto Mem = llvm::cast<X86OperandMem>(this->getSrc(0)); |
| 2775 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
| 2776 | auto *Target = InstX86Base::getTarget(Func); |
| 2777 | const Address Addr = Mem->toAsmAddress(Asm, Target); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2778 | const auto *VarReg = llvm::cast<Variable>(this->getSrc(1)); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2779 | assert(VarReg->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2780 | const GPRRegister Reg = Traits::getEncodedGPR(VarReg->getRegNum()); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2781 | Asm->xadd(Ty, Addr, Reg, this->Locked); |
| 2782 | } |
| 2783 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2784 | template <typename TraitsType> |
| 2785 | void InstImpl<TraitsType>::InstX86Xadd::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2786 | if (!BuildDefs::dump()) |
| 2787 | return; |
| 2788 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2789 | if (this->Locked) { |
| 2790 | Str << "lock "; |
| 2791 | } |
| 2792 | Type Ty = this->getSrc(0)->getType(); |
| 2793 | Str << "xadd." << Ty << " "; |
| 2794 | this->dumpSources(Func); |
| 2795 | } |
| 2796 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2797 | template <typename TraitsType> |
| 2798 | void InstImpl<TraitsType>::InstX86Xchg::emit(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2799 | if (!BuildDefs::dump()) |
| 2800 | return; |
| 2801 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2802 | Str << "\t" |
| 2803 | "xchg" << this->getWidthString(this->getSrc(0)->getType()) << "\t"; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2804 | this->getSrc(1)->emit(Func); |
| 2805 | Str << ", "; |
| 2806 | this->getSrc(0)->emit(Func); |
| 2807 | } |
| 2808 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2809 | template <typename TraitsType> |
| 2810 | void InstImpl<TraitsType>::InstX86Xchg::emitIAS(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2811 | assert(this->getSrcSize() == 2); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2812 | Assembler *Asm = Func->getAssembler<Assembler>(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2813 | Type Ty = this->getSrc(0)->getType(); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 2814 | const auto *VarReg1 = llvm::cast<Variable>(this->getSrc(1)); |
| 2815 | assert(VarReg1->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2816 | const GPRRegister Reg1 = Traits::getEncodedGPR(VarReg1->getRegNum()); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 2817 | |
| 2818 | if (const auto *VarReg0 = llvm::dyn_cast<Variable>(this->getSrc(0))) { |
| 2819 | assert(VarReg0->hasReg()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2820 | const GPRRegister Reg0 = Traits::getEncodedGPR(VarReg0->getRegNum()); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 2821 | Asm->xchg(Ty, Reg0, Reg1); |
| 2822 | return; |
| 2823 | } |
| 2824 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2825 | const auto *Mem = llvm::cast<X86OperandMem>(this->getSrc(0)); |
| 2826 | assert(Mem->getSegmentRegister() == X86OperandMem::DefaultSegment); |
| 2827 | auto *Target = InstX86Base::getTarget(Func); |
| 2828 | const Address Addr = Mem->toAsmAddress(Asm, Target); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 2829 | Asm->xchg(Ty, Addr, Reg1); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2830 | } |
| 2831 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2832 | template <typename TraitsType> |
| 2833 | void InstImpl<TraitsType>::InstX86Xchg::dump(const Cfg *Func) const { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2834 | if (!BuildDefs::dump()) |
| 2835 | return; |
| 2836 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2837 | Type Ty = this->getSrc(0)->getType(); |
| 2838 | Str << "xchg." << Ty << " "; |
| 2839 | this->dumpSources(Func); |
| 2840 | } |
| 2841 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2842 | template <typename TraitsType> |
| 2843 | void InstImpl<TraitsType>::InstX86IacaStart::emit(const Cfg *Func) const { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2844 | if (!BuildDefs::dump()) |
| 2845 | return; |
| 2846 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2847 | Str << "\t# IACA_START\n" |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2848 | "\t.byte 0x0F, 0x0B\n" |
| 2849 | "\t" |
| 2850 | "movl\t$111, %ebx\n" |
| 2851 | "\t.byte 0x64, 0x67, 0x90"; |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2854 | template <typename TraitsType> |
| 2855 | void InstImpl<TraitsType>::InstX86IacaStart::emitIAS(const Cfg *Func) const { |
| 2856 | Assembler *Asm = Func->getAssembler<Assembler>(); |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2857 | Asm->iaca_start(); |
| 2858 | } |
| 2859 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2860 | template <typename TraitsType> |
| 2861 | void InstImpl<TraitsType>::InstX86IacaStart::dump(const Cfg *Func) const { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2862 | if (!BuildDefs::dump()) |
| 2863 | return; |
| 2864 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2865 | Str << "IACA_START"; |
| 2866 | } |
| 2867 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2868 | template <typename TraitsType> |
| 2869 | void InstImpl<TraitsType>::InstX86IacaEnd::emit(const Cfg *Func) const { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2870 | if (!BuildDefs::dump()) |
| 2871 | return; |
| 2872 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 2873 | Str << "\t# IACA_END\n" |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 2874 | "\t" |
| 2875 | "movl\t$222, %ebx\n" |
| 2876 | "\t.byte 0x64, 0x67, 0x90\n" |
| 2877 | "\t.byte 0x0F, 0x0B"; |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2880 | template <typename TraitsType> |
| 2881 | void InstImpl<TraitsType>::InstX86IacaEnd::emitIAS(const Cfg *Func) const { |
| 2882 | Assembler *Asm = Func->getAssembler<Assembler>(); |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2883 | Asm->iaca_end(); |
| 2884 | } |
| 2885 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2886 | template <typename TraitsType> |
| 2887 | void InstImpl<TraitsType>::InstX86IacaEnd::dump(const Cfg *Func) const { |
Andrew Scull | 2c86252 | 2015-08-06 08:41:53 -0700 | [diff] [blame] | 2888 | if (!BuildDefs::dump()) |
| 2889 | return; |
| 2890 | Ostream &Str = Func->getContext()->getStrDump(); |
| 2891 | Str << "IACA_END"; |
| 2892 | } |
| 2893 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2894 | } // end of namespace X86NAMESPACE |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2895 | |
| 2896 | } // end of namespace Ice |
| 2897 | |
| 2898 | #endif // SUBZERO_SRC_ICEINSTX86BASEIMPL_H |