OSDN Git Service

Subzero: Use cvttss2si and similar instead of cvtss2si for fp->int casts.
[android-x86/external-swiftshader.git] / src / IceInstX8632.cpp
1 //===- subzero/src/IceInstX8632.cpp - X86-32 instruction implementation ---===//
2 //
3 //                        The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the InstX8632 and OperandX8632 classes,
11 // primarily the constructors and the dump()/emit() methods.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "IceCfg.h"
16 #include "IceCfgNode.h"
17 #include "IceInst.h"
18 #include "IceInstX8632.h"
19 #include "IceTargetLoweringX8632.h"
20 #include "IceOperand.h"
21
22 namespace Ice {
23
24 namespace {
25
26 const struct InstX8632BrAttributes_ {
27   const char *DisplayString;
28   const char *EmitString;
29 } InstX8632BrAttributes[] = {
30 #define X(tag, dump, emit)                                                     \
31   { dump, emit }                                                               \
32   ,
33     ICEINSTX8632BR_TABLE
34 #undef X
35   };
36
37 const struct InstX8632CmppsAttributes_ {
38   const char *EmitString;
39 } InstX8632CmppsAttributes[] = {
40 #define X(tag, emit)                                                           \
41   { emit }                                                                     \
42   ,
43     ICEINSTX8632CMPPS_TABLE
44 #undef X
45   };
46
47 const struct TypeX8632Attributes_ {
48   const char *CvtString;   // i (integer), s (single FP), d (double FP)
49   const char *SdSsString;  // ss, sd, or <blank>
50   const char *PackString;  // b, w, d, or <blank>
51   const char *WidthString; // {byte,word,dword,qword} ptr
52 } TypeX8632Attributes[] = {
53 #define X(tag, elementty, cvt, sdss, pack, width)                              \
54   { cvt, "" sdss, pack, width }                                                \
55   ,
56     ICETYPEX8632_TABLE
57 #undef X
58   };
59
60 const char *InstX8632SegmentRegNames[] = {
61 #define X(val, name) name,
62   SEG_REGX8632_TABLE
63 #undef X
64 };
65
66 } // end of anonymous namespace
67
68 const char *InstX8632::getWidthString(Type Ty) {
69   return TypeX8632Attributes[Ty].WidthString;
70 }
71
72 OperandX8632Mem::OperandX8632Mem(Cfg *Func, Type Ty, Variable *Base,
73                                  Constant *Offset, Variable *Index,
74                                  uint16_t Shift, SegmentRegisters SegmentReg)
75     : OperandX8632(kMem, Ty), Base(Base), Offset(Offset), Index(Index),
76       Shift(Shift), SegmentReg(SegmentReg) {
77   assert(Shift <= 3);
78   Vars = NULL;
79   NumVars = 0;
80   if (Base)
81     ++NumVars;
82   if (Index)
83     ++NumVars;
84   if (NumVars) {
85     Vars = Func->allocateArrayOf<Variable *>(NumVars);
86     SizeT I = 0;
87     if (Base)
88       Vars[I++] = Base;
89     if (Index)
90       Vars[I++] = Index;
91     assert(I == NumVars);
92   }
93 }
94
95 InstX8632AdjustStack::InstX8632AdjustStack(Cfg *Func, SizeT Amount)
96     : InstX8632(Func, InstX8632::Adjuststack, 0, NULL), Amount(Amount) {}
97
98 InstX8632Mul::InstX8632Mul(Cfg *Func, Variable *Dest, Variable *Source1,
99                            Operand *Source2)
100     : InstX8632(Func, InstX8632::Mul, 2, Dest) {
101   addSource(Source1);
102   addSource(Source2);
103 }
104
105 InstX8632Shld::InstX8632Shld(Cfg *Func, Variable *Dest, Variable *Source1,
106                              Variable *Source2)
107     : InstX8632(Func, InstX8632::Shld, 3, Dest) {
108   addSource(Dest);
109   addSource(Source1);
110   addSource(Source2);
111 }
112
113 InstX8632Shrd::InstX8632Shrd(Cfg *Func, Variable *Dest, Variable *Source1,
114                              Variable *Source2)
115     : InstX8632(Func, InstX8632::Shrd, 3, Dest) {
116   addSource(Dest);
117   addSource(Source1);
118   addSource(Source2);
119 }
120
121 InstX8632Label::InstX8632Label(Cfg *Func, TargetX8632 *Target)
122     : InstX8632(Func, InstX8632::Label, 0, NULL),
123       Number(Target->makeNextLabelNumber()) {}
124
125 IceString InstX8632Label::getName(const Cfg *Func) const {
126   char buf[30];
127   snprintf(buf, llvm::array_lengthof(buf), "%u", Number);
128   return ".L" + Func->getFunctionName() + "$local$__" + buf;
129 }
130
131 InstX8632Br::InstX8632Br(Cfg *Func, CfgNode *TargetTrue, CfgNode *TargetFalse,
132                          InstX8632Label *Label, InstX8632::BrCond Condition)
133     : InstX8632(Func, InstX8632::Br, 0, NULL), Condition(Condition),
134       TargetTrue(TargetTrue), TargetFalse(TargetFalse), Label(Label) {}
135
136 InstX8632Call::InstX8632Call(Cfg *Func, Variable *Dest, Operand *CallTarget)
137     : InstX8632(Func, InstX8632::Call, 1, Dest) {
138   HasSideEffects = true;
139   addSource(CallTarget);
140 }
141
142 InstX8632Cmov::InstX8632Cmov(Cfg *Func, Variable *Dest, Operand *Source,
143                              InstX8632::BrCond Condition)
144     : InstX8632(Func, InstX8632::Cmov, 2, Dest), Condition(Condition) {
145   // The final result is either the original Dest, or Source, so mark
146   // both as sources.
147   addSource(Dest);
148   addSource(Source);
149 }
150
151 InstX8632Cmpps::InstX8632Cmpps(Cfg *Func, Variable *Dest, Operand *Source,
152                                InstX8632Cmpps::CmppsCond Condition)
153     : InstX8632(Func, InstX8632::Cmpps, 2, Dest), Condition(Condition) {
154   addSource(Dest);
155   addSource(Source);
156 }
157
158 InstX8632Cmpxchg::InstX8632Cmpxchg(Cfg *Func, Operand *DestOrAddr,
159                                    Variable *Eax, Variable *Desired,
160                                    bool Locked)
161     : InstX8632Lockable(Func, InstX8632::Cmpxchg, 3,
162                         llvm::dyn_cast<Variable>(DestOrAddr), Locked) {
163   assert(Eax->getRegNum() == TargetX8632::Reg_eax);
164   addSource(DestOrAddr);
165   addSource(Eax);
166   addSource(Desired);
167 }
168
169 InstX8632Cmpxchg8b::InstX8632Cmpxchg8b(Cfg *Func, OperandX8632 *Addr,
170                                        Variable *Edx, Variable *Eax,
171                                        Variable *Ecx, Variable *Ebx,
172                                        bool Locked)
173     : InstX8632Lockable(Func, InstX8632::Cmpxchg, 5, NULL, Locked) {
174   assert(Edx->getRegNum() == TargetX8632::Reg_edx);
175   assert(Eax->getRegNum() == TargetX8632::Reg_eax);
176   assert(Ecx->getRegNum() == TargetX8632::Reg_ecx);
177   assert(Ebx->getRegNum() == TargetX8632::Reg_ebx);
178   addSource(Addr);
179   addSource(Edx);
180   addSource(Eax);
181   addSource(Ecx);
182   addSource(Ebx);
183 }
184
185 InstX8632Cvt::InstX8632Cvt(Cfg *Func, Variable *Dest, Operand *Source,
186                            bool Trunc)
187     : InstX8632(Func, InstX8632::Cvt, 1, Dest), Trunc(Trunc) {
188   addSource(Source);
189 }
190
191 InstX8632Icmp::InstX8632Icmp(Cfg *Func, Operand *Src0, Operand *Src1)
192     : InstX8632(Func, InstX8632::Icmp, 2, NULL) {
193   addSource(Src0);
194   addSource(Src1);
195 }
196
197 InstX8632Ucomiss::InstX8632Ucomiss(Cfg *Func, Operand *Src0, Operand *Src1)
198     : InstX8632(Func, InstX8632::Ucomiss, 2, NULL) {
199   addSource(Src0);
200   addSource(Src1);
201 }
202
203 InstX8632UD2::InstX8632UD2(Cfg *Func)
204     : InstX8632(Func, InstX8632::UD2, 0, NULL) {}
205
206 InstX8632Test::InstX8632Test(Cfg *Func, Operand *Src1, Operand *Src2)
207     : InstX8632(Func, InstX8632::Test, 2, NULL) {
208   addSource(Src1);
209   addSource(Src2);
210 }
211
212 InstX8632Mfence::InstX8632Mfence(Cfg *Func)
213     : InstX8632(Func, InstX8632::Mfence, 0, NULL) {
214   HasSideEffects = true;
215 }
216
217 InstX8632Store::InstX8632Store(Cfg *Func, Operand *Value, OperandX8632 *Mem)
218     : InstX8632(Func, InstX8632::Store, 2, NULL) {
219   addSource(Value);
220   addSource(Mem);
221 }
222
223 InstX8632StoreP::InstX8632StoreP(Cfg *Func, Operand *Value, OperandX8632 *Mem)
224     : InstX8632(Func, InstX8632::StoreP, 2, NULL) {
225   addSource(Value);
226   addSource(Mem);
227 }
228
229 InstX8632StoreQ::InstX8632StoreQ(Cfg *Func, Operand *Value, OperandX8632 *Mem)
230     : InstX8632(Func, InstX8632::StoreQ, 2, NULL) {
231   addSource(Value);
232   addSource(Mem);
233 }
234
235 InstX8632Movsx::InstX8632Movsx(Cfg *Func, Variable *Dest, Operand *Source)
236     : InstX8632(Func, InstX8632::Movsx, 1, Dest) {
237   addSource(Source);
238 }
239
240 InstX8632Movzx::InstX8632Movzx(Cfg *Func, Variable *Dest, Operand *Source)
241     : InstX8632(Func, InstX8632::Movzx, 1, Dest) {
242   addSource(Source);
243 }
244
245 InstX8632Nop::InstX8632Nop(Cfg *Func, InstX8632Nop::NopVariant Variant)
246     : InstX8632(Func, InstX8632::Nop, 0, NULL), Variant(Variant) {}
247
248 InstX8632Fld::InstX8632Fld(Cfg *Func, Operand *Src)
249     : InstX8632(Func, InstX8632::Fld, 1, NULL) {
250   addSource(Src);
251 }
252
253 InstX8632Fstp::InstX8632Fstp(Cfg *Func, Variable *Dest)
254     : InstX8632(Func, InstX8632::Fstp, 0, Dest) {}
255
256 InstX8632Pop::InstX8632Pop(Cfg *Func, Variable *Dest)
257     : InstX8632(Func, InstX8632::Pop, 0, Dest) {}
258
259 InstX8632Push::InstX8632Push(Cfg *Func, Operand *Source,
260                              bool SuppressStackAdjustment)
261     : InstX8632(Func, InstX8632::Push, 1, NULL),
262       SuppressStackAdjustment(SuppressStackAdjustment) {
263   addSource(Source);
264 }
265
266 InstX8632Ret::InstX8632Ret(Cfg *Func, Variable *Source)
267     : InstX8632(Func, InstX8632::Ret, Source ? 1 : 0, NULL) {
268   if (Source)
269     addSource(Source);
270 }
271
272 InstX8632Xadd::InstX8632Xadd(Cfg *Func, Operand *Dest, Variable *Source,
273                              bool Locked)
274     : InstX8632Lockable(Func, InstX8632::Xadd, 2,
275                         llvm::dyn_cast<Variable>(Dest), Locked) {
276   addSource(Dest);
277   addSource(Source);
278 }
279
280 InstX8632Xchg::InstX8632Xchg(Cfg *Func, Operand *Dest, Variable *Source)
281     : InstX8632(Func, InstX8632::Xchg, 2, llvm::dyn_cast<Variable>(Dest)) {
282   addSource(Dest);
283   addSource(Source);
284 }
285
286 // ======================== Dump routines ======================== //
287
288 void InstX8632::dump(const Cfg *Func) const {
289   Ostream &Str = Func->getContext()->getStrDump();
290   Str << "[X8632] ";
291   Inst::dump(Func);
292 }
293
294 void InstX8632Label::emit(const Cfg *Func) const {
295   Ostream &Str = Func->getContext()->getStrEmit();
296   Str << getName(Func) << ":\n";
297 }
298
299 void InstX8632Label::dump(const Cfg *Func) const {
300   Ostream &Str = Func->getContext()->getStrDump();
301   Str << getName(Func) << ":";
302 }
303
304 void InstX8632Br::emit(const Cfg *Func) const {
305   Ostream &Str = Func->getContext()->getStrEmit();
306   Str << "\t";
307
308   if (Condition == Br_None) {
309     Str << "jmp";
310   } else {
311     Str << InstX8632BrAttributes[Condition].EmitString;
312   }
313
314   if (Label) {
315     Str << "\t" << Label->getName(Func) << "\n";
316   } else {
317     if (Condition == Br_None) {
318       Str << "\t" << getTargetFalse()->getAsmName() << "\n";
319     } else {
320       Str << "\t" << getTargetTrue()->getAsmName() << "\n";
321       if (getTargetFalse()) {
322         Str << "\tjmp\t" << getTargetFalse()->getAsmName() << "\n";
323       }
324     }
325   }
326 }
327
328 void InstX8632Br::dump(const Cfg *Func) const {
329   Ostream &Str = Func->getContext()->getStrDump();
330   Str << "br ";
331
332   if (Condition == Br_None) {
333     Str << "label %"
334         << (Label ? Label->getName(Func) : getTargetFalse()->getName());
335     return;
336   }
337
338   Str << InstX8632BrAttributes[Condition].DisplayString;
339   if (Label) {
340     Str << ", label %" << Label->getName(Func);
341   } else {
342     Str << ", label %" << getTargetTrue()->getName();
343     if (getTargetFalse()) {
344       Str << ", label %" << getTargetFalse()->getName();
345     }
346   }
347 }
348
349 void InstX8632Call::emit(const Cfg *Func) const {
350   Ostream &Str = Func->getContext()->getStrEmit();
351   assert(getSrcSize() == 1);
352   Str << "\tcall\t";
353   getCallTarget()->emit(Func);
354   Str << "\n";
355   Func->getTarget()->resetStackAdjustment();
356 }
357
358 void InstX8632Call::dump(const Cfg *Func) const {
359   Ostream &Str = Func->getContext()->getStrDump();
360   if (getDest()) {
361     dumpDest(Func);
362     Str << " = ";
363   }
364   Str << "call ";
365   getCallTarget()->dump(Func);
366 }
367
368 // The ShiftHack parameter is used to emit "cl" instead of "ecx" for
369 // shift instructions, in order to be syntactically valid.  The
370 // Opcode parameter needs to be char* and not IceString because of
371 // template issues.
372 void emitTwoAddress(const char *Opcode, const Inst *Inst, const Cfg *Func,
373                     bool ShiftHack) {
374   Ostream &Str = Func->getContext()->getStrEmit();
375   assert(Inst->getSrcSize() == 2);
376   assert(Inst->getDest() == Inst->getSrc(0));
377   Str << "\t" << Opcode << "\t";
378   Inst->getDest()->emit(Func);
379   Str << ", ";
380   bool EmittedSrc1 = false;
381   if (ShiftHack) {
382     Variable *ShiftReg = llvm::dyn_cast<Variable>(Inst->getSrc(1));
383     if (ShiftReg && ShiftReg->getRegNum() == TargetX8632::Reg_ecx) {
384       Str << "cl";
385       EmittedSrc1 = true;
386     }
387   }
388   if (!EmittedSrc1)
389     Inst->getSrc(1)->emit(Func);
390   Str << "\n";
391 }
392
393 bool checkForRedundantAssign(const Variable *Dest, const Operand *Source) {
394   const Variable *Src = llvm::dyn_cast<const Variable>(Source);
395   if (Src == NULL)
396     return false;
397   if (Dest->hasReg() && Dest->getRegNum() == Src->getRegNum()) {
398     // TODO: On x86-64, instructions like "mov eax, eax" are used to
399     // clear the upper 32 bits of rax.  We need to recognize and
400     // preserve these.
401     return true;
402   }
403   if (!Dest->hasReg() && !Src->hasReg() &&
404       Dest->getStackOffset() == Src->getStackOffset())
405     return true;
406   return false;
407 }
408
409 // In-place ops
410 template <> const char *InstX8632Bswap::Opcode = "bswap";
411 template <> const char *InstX8632Neg::Opcode = "neg";
412 // Unary ops
413 template <> const char *InstX8632Bsf::Opcode = "bsf";
414 template <> const char *InstX8632Bsr::Opcode = "bsr";
415 template <> const char *InstX8632Lea::Opcode = "lea";
416 template <> const char *InstX8632Movd::Opcode = "movd";
417 template <> const char *InstX8632Sqrtss::Opcode = "sqrtss";
418 template <> const char *InstX8632Cbwdq::Opcode = "cbw/cwd/cdq";
419 // Mov-like ops
420 template <> const char *InstX8632Mov::Opcode = "mov";
421 template <> const char *InstX8632Movp::Opcode = "movups";
422 template <> const char *InstX8632Movq::Opcode = "movq";
423 // Binary ops
424 template <> const char *InstX8632Add::Opcode = "add";
425 template <> const char *InstX8632Addps::Opcode = "addps";
426 template <> const char *InstX8632Adc::Opcode = "adc";
427 template <> const char *InstX8632Addss::Opcode = "addss";
428 template <> const char *InstX8632Padd::Opcode = "padd";
429 template <> const char *InstX8632Sub::Opcode = "sub";
430 template <> const char *InstX8632Subps::Opcode = "subps";
431 template <> const char *InstX8632Subss::Opcode = "subss";
432 template <> const char *InstX8632Sbb::Opcode = "sbb";
433 template <> const char *InstX8632Psub::Opcode = "psub";
434 template <> const char *InstX8632And::Opcode = "and";
435 template <> const char *InstX8632Pand::Opcode = "pand";
436 template <> const char *InstX8632Pandn::Opcode = "pandn";
437 template <> const char *InstX8632Or::Opcode = "or";
438 template <> const char *InstX8632Por::Opcode = "por";
439 template <> const char *InstX8632Xor::Opcode = "xor";
440 template <> const char *InstX8632Pxor::Opcode = "pxor";
441 template <> const char *InstX8632Imul::Opcode = "imul";
442 template <> const char *InstX8632Mulps::Opcode = "mulps";
443 template <> const char *InstX8632Mulss::Opcode = "mulss";
444 template <> const char *InstX8632Pmull::Opcode = "pmull";
445 template <> const char *InstX8632Pmuludq::Opcode = "pmuludq";
446 template <> const char *InstX8632Div::Opcode = "div";
447 template <> const char *InstX8632Divps::Opcode = "divps";
448 template <> const char *InstX8632Idiv::Opcode = "idiv";
449 template <> const char *InstX8632Divss::Opcode = "divss";
450 template <> const char *InstX8632Rol::Opcode = "rol";
451 template <> const char *InstX8632Shl::Opcode = "shl";
452 template <> const char *InstX8632Psll::Opcode = "psll";
453 template <> const char *InstX8632Shr::Opcode = "shr";
454 template <> const char *InstX8632Sar::Opcode = "sar";
455 template <> const char *InstX8632Psra::Opcode = "psra";
456 template <> const char *InstX8632Pcmpeq::Opcode = "pcmpeq";
457 template <> const char *InstX8632Pcmpgt::Opcode = "pcmpgt";
458 template <> const char *InstX8632Movss::Opcode = "movss";
459 // Ternary ops
460 template <> const char *InstX8632Insertps::Opcode = "insertps";
461 template <> const char *InstX8632Shufps::Opcode = "shufps";
462 template <> const char *InstX8632Pinsr::Opcode = "pinsr";
463 template <> const char *InstX8632Blendvps::Opcode = "blendvps";
464 template <> const char *InstX8632Pblendvb::Opcode = "pblendvb";
465 // Three address ops
466 template <> const char *InstX8632Pextr::Opcode = "pextr";
467 template <> const char *InstX8632Pshufd::Opcode = "pshufd";
468
469 template <> void InstX8632Sqrtss::emit(const Cfg *Func) const {
470   Ostream &Str = Func->getContext()->getStrEmit();
471   assert(getSrcSize() == 1);
472   Type Ty = getSrc(0)->getType();
473   assert(Ty == IceType_f32 || Ty == IceType_f64);
474   Str << "\tsqrt" << TypeX8632Attributes[Ty].SdSsString << "\t";
475   getDest()->emit(Func);
476   Str << ", ";
477   getSrc(0)->emit(Func);
478   Str << "\n";
479 }
480
481 template <> void InstX8632Addss::emit(const Cfg *Func) const {
482   char buf[30];
483   snprintf(buf, llvm::array_lengthof(buf), "add%s",
484            TypeX8632Attributes[getDest()->getType()].SdSsString);
485   emitTwoAddress(buf, this, Func);
486 }
487
488 template <> void InstX8632Padd::emit(const Cfg *Func) const {
489   char buf[30];
490   snprintf(buf, llvm::array_lengthof(buf), "padd%s",
491            TypeX8632Attributes[getDest()->getType()].PackString);
492   emitTwoAddress(buf, this, Func);
493 }
494
495 template <> void InstX8632Pmull::emit(const Cfg *Func) const {
496   char buf[30];
497   bool TypesAreValid = getDest()->getType() == IceType_v4i32 ||
498                        getDest()->getType() == IceType_v8i16;
499   bool InstructionSetIsValid =
500       getDest()->getType() == IceType_v8i16 ||
501       static_cast<TargetX8632 *>(Func->getTarget())->getInstructionSet() >=
502           TargetX8632::SSE4_1;
503   (void)TypesAreValid;
504   (void)InstructionSetIsValid;
505   assert(TypesAreValid);
506   assert(InstructionSetIsValid);
507   snprintf(buf, llvm::array_lengthof(buf), "pmull%s",
508            TypeX8632Attributes[getDest()->getType()].PackString);
509   emitTwoAddress(buf, this, Func);
510 }
511
512 template <> void InstX8632Subss::emit(const Cfg *Func) const {
513   char buf[30];
514   snprintf(buf, llvm::array_lengthof(buf), "sub%s",
515            TypeX8632Attributes[getDest()->getType()].SdSsString);
516   emitTwoAddress(buf, this, Func);
517 }
518
519 template <> void InstX8632Psub::emit(const Cfg *Func) const {
520   char buf[30];
521   snprintf(buf, llvm::array_lengthof(buf), "psub%s",
522            TypeX8632Attributes[getDest()->getType()].PackString);
523   emitTwoAddress(buf, this, Func);
524 }
525
526 template <> void InstX8632Mulss::emit(const Cfg *Func) const {
527   char buf[30];
528   snprintf(buf, llvm::array_lengthof(buf), "mul%s",
529            TypeX8632Attributes[getDest()->getType()].SdSsString);
530   emitTwoAddress(buf, this, Func);
531 }
532
533 template <> void InstX8632Pmuludq::emit(const Cfg *Func) const {
534   assert(getSrc(0)->getType() == IceType_v4i32 &&
535          getSrc(1)->getType() == IceType_v4i32);
536   emitTwoAddress(Opcode, this, Func);
537 }
538
539 template <> void InstX8632Divss::emit(const Cfg *Func) const {
540   char buf[30];
541   snprintf(buf, llvm::array_lengthof(buf), "div%s",
542            TypeX8632Attributes[getDest()->getType()].SdSsString);
543   emitTwoAddress(buf, this, Func);
544 }
545
546 template <> void InstX8632Div::emit(const Cfg *Func) const {
547   Ostream &Str = Func->getContext()->getStrEmit();
548   assert(getSrcSize() == 3);
549   Str << "\t" << Opcode << "\t";
550   getSrc(1)->emit(Func);
551   Str << "\n";
552 }
553
554 template <> void InstX8632Idiv::emit(const Cfg *Func) const {
555   Ostream &Str = Func->getContext()->getStrEmit();
556   assert(getSrcSize() == 3);
557   Str << "\t" << Opcode << "\t";
558   getSrc(1)->emit(Func);
559   Str << "\n";
560 }
561
562
563 namespace {
564
565 // pblendvb and blendvps take xmm0 as a final implicit argument.
566 void emitVariableBlendInst(const char *Opcode, const Inst *Inst,
567                            const Cfg *Func) {
568   Ostream &Str = Func->getContext()->getStrEmit();
569   assert(Inst->getSrcSize() == 3);
570   assert(llvm::isa<Variable>(Inst->getSrc(2)));
571   assert(llvm::cast<Variable>(Inst->getSrc(2))->getRegNum() ==
572          TargetX8632::Reg_xmm0);
573   Str << "\t" << Opcode << "\t";
574   Inst->getDest()->emit(Func);
575   Str << ", ";
576   Inst->getSrc(1)->emit(Func);
577   Str << "\n";
578 }
579
580 } // end anonymous namespace
581
582 template <> void InstX8632Blendvps::emit(const Cfg *Func) const {
583   assert(static_cast<TargetX8632 *>(Func->getTarget())->getInstructionSet() >=
584          TargetX8632::SSE4_1);
585   emitVariableBlendInst(Opcode, this, Func);
586 }
587
588 template <> void InstX8632Pblendvb::emit(const Cfg *Func) const {
589   assert(static_cast<TargetX8632 *>(Func->getTarget())->getInstructionSet() >=
590          TargetX8632::SSE4_1);
591   emitVariableBlendInst(Opcode, this, Func);
592 }
593
594 template <> void InstX8632Imul::emit(const Cfg *Func) const {
595   Ostream &Str = Func->getContext()->getStrEmit();
596   assert(getSrcSize() == 2);
597   if (getDest()->getType() == IceType_i8) {
598     // The 8-bit version of imul only allows the form "imul r/m8".
599     Variable *Src0 = llvm::dyn_cast<Variable>(getSrc(0));
600     (void)Src0;
601     assert(Src0 && Src0->getRegNum() == TargetX8632::Reg_eax);
602     Str << "\timul\t";
603     getSrc(1)->emit(Func);
604     Str << "\n";
605   } else if (llvm::isa<Constant>(getSrc(1))) {
606     Str << "\timul\t";
607     getDest()->emit(Func);
608     Str << ", ";
609     getSrc(0)->emit(Func);
610     Str << ", ";
611     getSrc(1)->emit(Func);
612     Str << "\n";
613   } else {
614     emitTwoAddress("imul", this, Func);
615   }
616 }
617
618 template <> void InstX8632Cbwdq::emit(const Cfg *Func) const {
619   Ostream &Str = Func->getContext()->getStrEmit();
620   assert(getSrcSize() == 1);
621   Operand *Src0 = getSrc(0);
622   assert(llvm::isa<Variable>(Src0));
623   assert(llvm::cast<Variable>(Src0)->getRegNum() == TargetX8632::Reg_eax);
624   switch (Src0->getType()) {
625   default:
626     llvm_unreachable("unexpected source type!");
627     break;
628   case IceType_i8:
629     assert(getDest()->getRegNum() == TargetX8632::Reg_eax);
630     Str << "\tcbw\n";
631     break;
632   case IceType_i16:
633     assert(getDest()->getRegNum() == TargetX8632::Reg_edx);
634     Str << "\tcwd\n";
635     break;
636   case IceType_i32:
637     assert(getDest()->getRegNum() == TargetX8632::Reg_edx);
638     Str << "\tcdq\n";
639     break;
640   }
641 }
642
643 void InstX8632Mul::emit(const Cfg *Func) const {
644   Ostream &Str = Func->getContext()->getStrEmit();
645   assert(getSrcSize() == 2);
646   assert(llvm::isa<Variable>(getSrc(0)));
647   assert(llvm::dyn_cast<Variable>(getSrc(0))->getRegNum() ==
648          TargetX8632::Reg_eax);
649   assert(getDest()->getRegNum() == TargetX8632::Reg_eax); // TODO: allow edx?
650   Str << "\tmul\t";
651   getSrc(1)->emit(Func);
652   Str << "\n";
653 }
654
655 void InstX8632Mul::dump(const Cfg *Func) const {
656   Ostream &Str = Func->getContext()->getStrDump();
657   dumpDest(Func);
658   Str << " = mul." << getDest()->getType() << " ";
659   dumpSources(Func);
660 }
661
662 void InstX8632Shld::emit(const Cfg *Func) const {
663   Ostream &Str = Func->getContext()->getStrEmit();
664   assert(getSrcSize() == 3);
665   assert(getDest() == getSrc(0));
666   Str << "\tshld\t";
667   getDest()->emit(Func);
668   Str << ", ";
669   getSrc(1)->emit(Func);
670   Str << ", ";
671   if (Variable *ShiftReg = llvm::dyn_cast<Variable>(getSrc(2))) {
672     (void)ShiftReg;
673     assert(ShiftReg->getRegNum() == TargetX8632::Reg_ecx);
674     Str << "cl";
675   } else {
676     getSrc(2)->emit(Func);
677   }
678   Str << "\n";
679 }
680
681 void InstX8632Shld::dump(const Cfg *Func) const {
682   Ostream &Str = Func->getContext()->getStrDump();
683   dumpDest(Func);
684   Str << " = shld." << getDest()->getType() << " ";
685   dumpSources(Func);
686 }
687
688 void InstX8632Shrd::emit(const Cfg *Func) const {
689   Ostream &Str = Func->getContext()->getStrEmit();
690   assert(getSrcSize() == 3);
691   assert(getDest() == getSrc(0));
692   Str << "\tshrd\t";
693   getDest()->emit(Func);
694   Str << ", ";
695   getSrc(1)->emit(Func);
696   Str << ", ";
697   if (Variable *ShiftReg = llvm::dyn_cast<Variable>(getSrc(2))) {
698     (void)ShiftReg;
699     assert(ShiftReg->getRegNum() == TargetX8632::Reg_ecx);
700     Str << "cl";
701   } else {
702     getSrc(2)->emit(Func);
703   }
704   Str << "\n";
705 }
706
707 void InstX8632Shrd::dump(const Cfg *Func) const {
708   Ostream &Str = Func->getContext()->getStrDump();
709   dumpDest(Func);
710   Str << " = shrd." << getDest()->getType() << " ";
711   dumpSources(Func);
712 }
713
714 void InstX8632Cmov::emit(const Cfg *Func) const {
715   Ostream &Str = Func->getContext()->getStrEmit();
716   Str << "\t";
717   assert(Condition != Br_None);
718   assert(getDest()->hasReg());
719   Str << "cmov" << InstX8632BrAttributes[Condition].DisplayString << "\t";
720   getDest()->emit(Func);
721   Str << ", ";
722   getSrc(1)->emit(Func);
723   Str << "\n";
724 }
725
726 void InstX8632Cmov::dump(const Cfg *Func) const {
727   Ostream &Str = Func->getContext()->getStrDump();
728   Str << "cmov" << InstX8632BrAttributes[Condition].DisplayString << ".";
729   Str << getDest()->getType() << " ";
730   dumpDest(Func);
731   Str << ", ";
732   dumpSources(Func);
733 }
734
735 void InstX8632Cmpps::emit(const Cfg *Func) const {
736   Ostream &Str = Func->getContext()->getStrEmit();
737   assert(getSrcSize() == 2);
738   assert(Condition < Cmpps_Invalid);
739   Str << "\t";
740   Str << "cmp" << InstX8632CmppsAttributes[Condition].EmitString << "ps"
741       << "\t";
742   getDest()->emit(Func);
743   Str << ", ";
744   getSrc(1)->emit(Func);
745   Str << "\n";
746 }
747
748 void InstX8632Cmpps::dump(const Cfg *Func) const {
749   Ostream &Str = Func->getContext()->getStrDump();
750   assert(Condition < Cmpps_Invalid);
751   dumpDest(Func);
752   Str << " = cmp" << InstX8632CmppsAttributes[Condition].EmitString << "ps"
753       << "\t";
754   dumpSources(Func);
755 }
756
757 void InstX8632Cmpxchg::emit(const Cfg *Func) const {
758   Ostream &Str = Func->getContext()->getStrEmit();
759   assert(getSrcSize() == 3);
760   if (Locked) {
761     Str << "\tlock";
762   }
763   Str << "\tcmpxchg\t";
764   getSrc(0)->emit(Func);
765   Str << ", ";
766   getSrc(2)->emit(Func);
767   Str << "\n";
768 }
769
770 void InstX8632Cmpxchg::dump(const Cfg *Func) const {
771   Ostream &Str = Func->getContext()->getStrDump();
772   if (Locked) {
773     Str << "lock ";
774   }
775   Str << "cmpxchg." << getSrc(0)->getType() << " ";
776   dumpSources(Func);
777 }
778
779 void InstX8632Cmpxchg8b::emit(const Cfg *Func) const {
780   Ostream &Str = Func->getContext()->getStrEmit();
781   assert(getSrcSize() == 5);
782   if (Locked) {
783     Str << "\tlock";
784   }
785   Str << "\tcmpxchg8b\t";
786   getSrc(0)->emit(Func);
787   Str << "\n";
788 }
789
790 void InstX8632Cmpxchg8b::dump(const Cfg *Func) const {
791   Ostream &Str = Func->getContext()->getStrDump();
792   if (Locked) {
793     Str << "lock ";
794   }
795   Str << "cmpxchg8b ";
796   dumpSources(Func);
797 }
798
799 void InstX8632Cvt::emit(const Cfg *Func) const {
800   Ostream &Str = Func->getContext()->getStrEmit();
801   assert(getSrcSize() == 1);
802   Str << "\tcvt";
803   if (Trunc)
804     Str << "t";
805   Str << TypeX8632Attributes[getSrc(0)->getType()].CvtString << "2"
806       << TypeX8632Attributes[getDest()->getType()].CvtString << "\t";
807   getDest()->emit(Func);
808   Str << ", ";
809   getSrc(0)->emit(Func);
810   Str << "\n";
811 }
812
813 void InstX8632Cvt::dump(const Cfg *Func) const {
814   Ostream &Str = Func->getContext()->getStrDump();
815   dumpDest(Func);
816   Str << " = cvt";
817   if (Trunc)
818     Str << "t";
819   Str << TypeX8632Attributes[getSrc(0)->getType()].CvtString << "2"
820       << TypeX8632Attributes[getDest()->getType()].CvtString << " ";
821   dumpSources(Func);
822 }
823
824 void InstX8632Icmp::emit(const Cfg *Func) const {
825   Ostream &Str = Func->getContext()->getStrEmit();
826   assert(getSrcSize() == 2);
827   Str << "\tcmp\t";
828   getSrc(0)->emit(Func);
829   Str << ", ";
830   getSrc(1)->emit(Func);
831   Str << "\n";
832 }
833
834 void InstX8632Icmp::dump(const Cfg *Func) const {
835   Ostream &Str = Func->getContext()->getStrDump();
836   Str << "cmp." << getSrc(0)->getType() << " ";
837   dumpSources(Func);
838 }
839
840 void InstX8632Ucomiss::emit(const Cfg *Func) const {
841   Ostream &Str = Func->getContext()->getStrEmit();
842   assert(getSrcSize() == 2);
843   Str << "\tucomi" << TypeX8632Attributes[getSrc(0)->getType()].SdSsString
844       << "\t";
845   getSrc(0)->emit(Func);
846   Str << ", ";
847   getSrc(1)->emit(Func);
848   Str << "\n";
849 }
850
851 void InstX8632Ucomiss::dump(const Cfg *Func) const {
852   Ostream &Str = Func->getContext()->getStrDump();
853   Str << "ucomiss." << getSrc(0)->getType() << " ";
854   dumpSources(Func);
855 }
856
857 void InstX8632UD2::emit(const Cfg *Func) const {
858   Ostream &Str = Func->getContext()->getStrEmit();
859   assert(getSrcSize() == 0);
860   Str << "\tud2\n";
861 }
862
863 void InstX8632UD2::dump(const Cfg *Func) const {
864   Ostream &Str = Func->getContext()->getStrDump();
865   Str << "ud2\n";
866 }
867
868 void InstX8632Test::emit(const Cfg *Func) const {
869   Ostream &Str = Func->getContext()->getStrEmit();
870   assert(getSrcSize() == 2);
871   Str << "\ttest\t";
872   getSrc(0)->emit(Func);
873   Str << ", ";
874   getSrc(1)->emit(Func);
875   Str << "\n";
876 }
877
878 void InstX8632Test::dump(const Cfg *Func) const {
879   Ostream &Str = Func->getContext()->getStrDump();
880   Str << "test." << getSrc(0)->getType() << " ";
881   dumpSources(Func);
882 }
883
884 void InstX8632Mfence::emit(const Cfg *Func) const {
885   Ostream &Str = Func->getContext()->getStrEmit();
886   assert(getSrcSize() == 0);
887   Str << "\tmfence\n";
888 }
889
890 void InstX8632Mfence::dump(const Cfg *Func) const {
891   Ostream &Str = Func->getContext()->getStrDump();
892   Str << "mfence\n";
893 }
894
895 void InstX8632Store::emit(const Cfg *Func) const {
896   Ostream &Str = Func->getContext()->getStrEmit();
897   assert(getSrcSize() == 2);
898   Str << "\tmov" << TypeX8632Attributes[getSrc(0)->getType()].SdSsString
899       << "\t";
900   getSrc(1)->emit(Func);
901   Str << ", ";
902   getSrc(0)->emit(Func);
903   Str << "\n";
904 }
905
906 void InstX8632Store::dump(const Cfg *Func) const {
907   Ostream &Str = Func->getContext()->getStrDump();
908   Str << "mov." << getSrc(0)->getType() << " ";
909   getSrc(1)->dump(Func);
910   Str << ", ";
911   getSrc(0)->dump(Func);
912 }
913
914 void InstX8632StoreP::emit(const Cfg *Func) const {
915   Ostream &Str = Func->getContext()->getStrEmit();
916   assert(getSrcSize() == 2);
917   Str << "\tmovups\t";
918   getSrc(1)->emit(Func);
919   Str << ", ";
920   getSrc(0)->emit(Func);
921   Str << "\n";
922 }
923
924 void InstX8632StoreP::dump(const Cfg *Func) const {
925   Ostream &Str = Func->getContext()->getStrDump();
926   Str << "storep." << getSrc(0)->getType() << " ";
927   getSrc(1)->dump(Func);
928   Str << ", ";
929   getSrc(0)->dump(Func);
930 }
931
932 void InstX8632StoreQ::emit(const Cfg *Func) const {
933   Ostream &Str = Func->getContext()->getStrEmit();
934   assert(getSrcSize() == 2);
935   assert(getSrc(1)->getType() == IceType_i64 ||
936          getSrc(1)->getType() == IceType_f64);
937   Str << "\tmovq\t";
938   getSrc(1)->emit(Func);
939   Str << ", ";
940   getSrc(0)->emit(Func);
941   Str << "\n";
942 }
943
944 void InstX8632StoreQ::dump(const Cfg *Func) const {
945   Ostream &Str = Func->getContext()->getStrDump();
946   Str << "storeq." << getSrc(0)->getType() << " ";
947   getSrc(1)->dump(Func);
948   Str << ", ";
949   getSrc(0)->dump(Func);
950 }
951
952 template <> void InstX8632Lea::emit(const Cfg *Func) const {
953   Ostream &Str = Func->getContext()->getStrEmit();
954   assert(getSrcSize() == 1);
955   assert(getDest()->hasReg());
956   Str << "\tlea\t";
957   getDest()->emit(Func);
958   Str << ", ";
959   Operand *Src0 = getSrc(0);
960   if (Variable *VSrc0 = llvm::dyn_cast<Variable>(Src0)) {
961     Type Ty = VSrc0->getType();
962     // lea on x86-32 doesn't accept mem128 operands, so cast VSrc0 to an
963     // acceptable type.
964     VSrc0->asType(isVectorType(Ty) ? IceType_i32 : Ty).emit(Func);
965   } else {
966     Src0->emit(Func);
967   }
968   Str << "\n";
969 }
970
971 template <> void InstX8632Mov::emit(const Cfg *Func) const {
972   Ostream &Str = Func->getContext()->getStrEmit();
973   assert(getSrcSize() == 1);
974   Operand *Src = getSrc(0);
975   // The llvm-mc assembler using Intel syntax has a bug in which "mov
976   // reg, RelocatableConstant" does not generate the right instruction
977   // with a relocation.  To work around, we emit "lea reg,
978   // RelocatableConstant".  Also, the lowering and legalization is
979   // changed to allow relocatable constants only in Assign and Call
980   // instructions or in Mem operands.  TODO(stichnot): remove LEAHACK
981   // once a proper emitter is used.
982   //
983   // In addition, llvm-mc doesn't like "lea eax, bp" or "lea eax, Sp"
984   // or "lea eax, flags" etc., when the relocatable constant name is a
985   // reserved word.  The hack-on-top-of-hack is to temporarily drop
986   // into AT&T syntax for this lea instruction.
987   bool UseLeaHack = llvm::isa<ConstantRelocatable>(Src);
988   if (UseLeaHack) {
989     Str << ".att_syntax\n";
990     Str << "\tleal";
991   } else {
992     Str << "\tmov" << TypeX8632Attributes[getDest()->getType()].SdSsString;
993   }
994   Str << "\t";
995   // For an integer truncation operation, src is wider than dest.
996   // Ideally, we use a mov instruction whose data width matches the
997   // narrower dest.  This is a problem if e.g. src is a register like
998   // esi or si where there is no 8-bit version of the register.  To be
999   // safe, we instead widen the dest to match src.  This works even
1000   // for stack-allocated dest variables because typeWidthOnStack()
1001   // pads to a 4-byte boundary even if only a lower portion is used.
1002   // TODO: This assert disallows usages such as copying a floating point
1003   // value between a vector and a scalar (which movss is used for).
1004   // Clean this up.
1005   assert(Func->getTarget()->typeWidthInBytesOnStack(getDest()->getType()) ==
1006          Func->getTarget()->typeWidthInBytesOnStack(Src->getType()));
1007   if (UseLeaHack) {
1008     Src->emit(Func);
1009     Str << ", %";
1010     getDest()->emit(Func);
1011     Str << "\n";
1012     Str << ".intel_syntax\n";
1013   } else {
1014     getDest()->asType(Src->getType()).emit(Func);
1015     Str << ", ";
1016     Src->emit(Func);
1017     Str << "\n";
1018   }
1019 }
1020
1021 template <> void InstX8632Movp::emit(const Cfg *Func) const {
1022   // TODO(wala,stichnot): movups works with all vector operands, but
1023   // there exist other instructions (movaps, movdqa, movdqu) that may
1024   // perform better, depending on the data type and alignment of the
1025   // operands.
1026   Ostream &Str = Func->getContext()->getStrEmit();
1027   assert(getSrcSize() == 1);
1028   Str << "\tmovups\t";
1029   getDest()->emit(Func);
1030   Str << ", ";
1031   getSrc(0)->emit(Func);
1032   Str << "\n";
1033 }
1034
1035 template <> void InstX8632Movq::emit(const Cfg *Func) const {
1036   Ostream &Str = Func->getContext()->getStrEmit();
1037   assert(getSrcSize() == 1);
1038   assert(getDest()->getType() == IceType_i64 ||
1039          getDest()->getType() == IceType_f64);
1040   Str << "\tmovq\t";
1041   getDest()->emit(Func);
1042   Str << ", ";
1043   getSrc(0)->emit(Func);
1044   Str << "\n";
1045 }
1046
1047 void InstX8632Movsx::emit(const Cfg *Func) const {
1048   Ostream &Str = Func->getContext()->getStrEmit();
1049   assert(getSrcSize() == 1);
1050   Str << "\tmovsx\t";
1051   getDest()->emit(Func);
1052   Str << ", ";
1053   getSrc(0)->emit(Func);
1054   Str << "\n";
1055 }
1056
1057 void InstX8632Movsx::dump(const Cfg *Func) const {
1058   Ostream &Str = Func->getContext()->getStrDump();
1059   Str << "movsx." << getDest()->getType() << "." << getSrc(0)->getType();
1060   Str << " ";
1061   dumpDest(Func);
1062   Str << ", ";
1063   dumpSources(Func);
1064 }
1065
1066 void InstX8632Movzx::emit(const Cfg *Func) const {
1067   Ostream &Str = Func->getContext()->getStrEmit();
1068   assert(getSrcSize() == 1);
1069   Str << "\tmovzx\t";
1070   getDest()->emit(Func);
1071   Str << ", ";
1072   getSrc(0)->emit(Func);
1073   Str << "\n";
1074 }
1075
1076 void InstX8632Movzx::dump(const Cfg *Func) const {
1077   Ostream &Str = Func->getContext()->getStrDump();
1078   Str << "movzx." << getDest()->getType() << "." << getSrc(0)->getType();
1079   Str << " ";
1080   dumpDest(Func);
1081   Str << ", ";
1082   dumpSources(Func);
1083 }
1084
1085 void InstX8632Nop::emit(const Cfg *Func) const {
1086   Ostream &Str = Func->getContext()->getStrEmit();
1087   // TODO: Emit the right code for each variant.
1088   Str << "\tnop\t# variant = " << Variant << "\n";
1089 }
1090
1091 void InstX8632Nop::dump(const Cfg *Func) const {
1092   Ostream &Str = Func->getContext()->getStrDump();
1093   Str << "nop (variant = " << Variant << ")";
1094 }
1095
1096 void InstX8632Fld::emit(const Cfg *Func) const {
1097   Ostream &Str = Func->getContext()->getStrEmit();
1098   assert(getSrcSize() == 1);
1099   Type Ty = getSrc(0)->getType();
1100   Variable *Var = llvm::dyn_cast<Variable>(getSrc(0));
1101   if (Var && Var->hasReg()) {
1102     // This is a physical xmm register, so we need to spill it to a
1103     // temporary stack slot.
1104     SizeT Width = typeWidthInBytes(Ty);
1105     Str << "\tsub\tesp, " << Width << "\n";
1106     Str << "\tmov" << TypeX8632Attributes[Ty].SdSsString << "\t"
1107         << TypeX8632Attributes[Ty].WidthString << " [esp], ";
1108     Var->emit(Func);
1109     Str << "\n";
1110     Str << "\tfld\t" << TypeX8632Attributes[Ty].WidthString << " [esp]\n";
1111     Str << "\tadd\tesp, " << Width << "\n";
1112     return;
1113   }
1114   Str << "\tfld\t";
1115   getSrc(0)->emit(Func);
1116   Str << "\n";
1117 }
1118
1119 void InstX8632Fld::dump(const Cfg *Func) const {
1120   Ostream &Str = Func->getContext()->getStrDump();
1121   Str << "fld." << getSrc(0)->getType() << " ";
1122   dumpSources(Func);
1123 }
1124
1125 void InstX8632Fstp::emit(const Cfg *Func) const {
1126   Ostream &Str = Func->getContext()->getStrEmit();
1127   assert(getSrcSize() == 0);
1128   if (getDest() == NULL) {
1129     Str << "\tfstp\tst(0)\n";
1130     return;
1131   }
1132   if (!getDest()->hasReg()) {
1133     Str << "\tfstp\t";
1134     getDest()->emit(Func);
1135     Str << "\n";
1136     return;
1137   }
1138   // Dest is a physical (xmm) register, so st(0) needs to go through
1139   // memory.  Hack this by creating a temporary stack slot, spilling
1140   // st(0) there, loading it into the xmm register, and deallocating
1141   // the stack slot.
1142   Type Ty = getDest()->getType();
1143   size_t Width = typeWidthInBytes(Ty);
1144   Str << "\tsub\tesp, " << Width << "\n";
1145   Str << "\tfstp\t" << TypeX8632Attributes[Ty].WidthString << " [esp]\n";
1146   Str << "\tmov" << TypeX8632Attributes[Ty].SdSsString << "\t";
1147   getDest()->emit(Func);
1148   Str << ", " << TypeX8632Attributes[Ty].WidthString << " [esp]\n";
1149   Str << "\tadd\tesp, " << Width << "\n";
1150 }
1151
1152 void InstX8632Fstp::dump(const Cfg *Func) const {
1153   Ostream &Str = Func->getContext()->getStrDump();
1154   dumpDest(Func);
1155   Str << " = fstp." << getDest()->getType() << ", st(0)";
1156   Str << "\n";
1157 }
1158
1159 template <> void InstX8632Pcmpeq::emit(const Cfg *Func) const {
1160   char buf[30];
1161   snprintf(buf, llvm::array_lengthof(buf), "pcmpeq%s",
1162            TypeX8632Attributes[getDest()->getType()].PackString);
1163   emitTwoAddress(buf, this, Func);
1164 }
1165
1166 template <> void InstX8632Pcmpgt::emit(const Cfg *Func) const {
1167   char buf[30];
1168   snprintf(buf, llvm::array_lengthof(buf), "pcmpgt%s",
1169            TypeX8632Attributes[getDest()->getType()].PackString);
1170   emitTwoAddress(buf, this, Func);
1171 }
1172
1173 template <> void InstX8632Pextr::emit(const Cfg *Func) const {
1174   Ostream &Str = Func->getContext()->getStrEmit();
1175   assert(getSrcSize() == 2);
1176   // pextrb and pextrd are SSE4.1 instructions.
1177   assert(getSrc(0)->getType() == IceType_v8i16 ||
1178          getSrc(0)->getType() == IceType_v8i1 ||
1179          static_cast<TargetX8632 *>(Func->getTarget())->getInstructionSet()
1180              >= TargetX8632::SSE4_1);
1181   Str << "\t" << Opcode
1182       << TypeX8632Attributes[getSrc(0)->getType()].PackString << "\t";
1183   Variable *Dest = getDest();
1184   // pextrw must take a register dest.
1185   assert(Dest->getType() != IceType_i16 || Dest->hasReg());
1186   Dest->asType(IceType_i32).emit(Func);
1187   Str << ", ";
1188   getSrc(0)->emit(Func);
1189   Str << ", ";
1190   getSrc(1)->emit(Func);
1191   Str << "\n";
1192 }
1193
1194 template <> void InstX8632Pinsr::emit(const Cfg *Func) const {
1195   Ostream &Str = Func->getContext()->getStrEmit();
1196   assert(getSrcSize() == 3);
1197   // pinsrb and pinsrd are SSE4.1 instructions.
1198   assert(getDest()->getType() == IceType_v8i16 ||
1199          getDest()->getType() == IceType_v8i1 ||
1200          static_cast<TargetX8632 *>(Func->getTarget())->getInstructionSet()
1201              >= TargetX8632::SSE4_1);
1202   Str << "\t" << Opcode
1203       << TypeX8632Attributes[getDest()->getType()].PackString << "\t";
1204   getDest()->emit(Func);
1205   Str << ", ";
1206   Operand *Src1 = getSrc(1);
1207   if (Variable *VSrc1 = llvm::dyn_cast<Variable>(Src1)) {
1208     // If src1 is a register, it should always be r32.
1209     if (VSrc1->hasReg()) {
1210       VSrc1->asType(IceType_i32).emit(Func);
1211     } else {
1212       VSrc1->emit(Func);
1213     }
1214   } else {
1215     Src1->emit(Func);
1216   }
1217   Str << ", ";
1218   getSrc(2)->emit(Func);
1219   Str << "\n";
1220 }
1221
1222 void InstX8632Pop::emit(const Cfg *Func) const {
1223   Ostream &Str = Func->getContext()->getStrEmit();
1224   assert(getSrcSize() == 0);
1225   Str << "\tpop\t";
1226   getDest()->emit(Func);
1227   Str << "\n";
1228 }
1229
1230 void InstX8632Pop::dump(const Cfg *Func) const {
1231   Ostream &Str = Func->getContext()->getStrDump();
1232   dumpDest(Func);
1233   Str << " = pop." << getDest()->getType() << " ";
1234 }
1235
1236 void InstX8632AdjustStack::emit(const Cfg *Func) const {
1237   Ostream &Str = Func->getContext()->getStrEmit();
1238   Str << "\tsub\tesp, " << Amount << "\n";
1239   Func->getTarget()->updateStackAdjustment(Amount);
1240 }
1241
1242 void InstX8632AdjustStack::dump(const Cfg *Func) const {
1243   Ostream &Str = Func->getContext()->getStrDump();
1244   Str << "esp = sub.i32 esp, " << Amount;
1245 }
1246
1247 void InstX8632Push::emit(const Cfg *Func) const {
1248   Ostream &Str = Func->getContext()->getStrEmit();
1249   assert(getSrcSize() == 1);
1250   Type Ty = getSrc(0)->getType();
1251   Variable *Var = llvm::dyn_cast<Variable>(getSrc(0));
1252   if ((isVectorType(Ty) || Ty == IceType_f32 || Ty == IceType_f64) && Var &&
1253       Var->hasReg()) {
1254     // The xmm registers can't be directly pushed, so we fake it by
1255     // decrementing esp and then storing to [esp].
1256     Str << "\tsub\tesp, " << typeWidthInBytes(Ty) << "\n";
1257     if (!SuppressStackAdjustment)
1258       Func->getTarget()->updateStackAdjustment(typeWidthInBytes(Ty));
1259     if (isVectorType(Ty)) {
1260       Str << "\tmovups\txmmword ptr [esp], ";
1261     } else {
1262       Str << "\tmov" << TypeX8632Attributes[Ty].SdSsString << "\t"
1263           << TypeX8632Attributes[Ty].WidthString << " [esp], ";
1264     }
1265     getSrc(0)->emit(Func);
1266     Str << "\n";
1267   } else if (Ty == IceType_f64 && (!Var || !Var->hasReg())) {
1268     // A double on the stack has to be pushed as two halves.  Push the
1269     // upper half followed by the lower half for little-endian.  TODO:
1270     // implement.
1271     llvm_unreachable("Missing support for pushing doubles from memory");
1272   } else {
1273     Str << "\tpush\t";
1274     getSrc(0)->emit(Func);
1275     Str << "\n";
1276     if (!SuppressStackAdjustment)
1277       Func->getTarget()->updateStackAdjustment(4);
1278   }
1279 }
1280
1281 void InstX8632Push::dump(const Cfg *Func) const {
1282   Ostream &Str = Func->getContext()->getStrDump();
1283   Str << "push." << getSrc(0)->getType() << " ";
1284   dumpSources(Func);
1285 }
1286
1287 template <> void InstX8632Psll::emit(const Cfg *Func) const {
1288   assert(getDest()->getType() == IceType_v8i16 ||
1289          getDest()->getType() == IceType_v8i1 ||
1290          getDest()->getType() == IceType_v4i32 ||
1291          getDest()->getType() == IceType_v4i1);
1292   char buf[30];
1293   snprintf(buf, llvm::array_lengthof(buf), "psll%s",
1294            TypeX8632Attributes[getDest()->getType()].PackString);
1295   emitTwoAddress(buf, this, Func);
1296 }
1297
1298 template <> void InstX8632Psra::emit(const Cfg *Func) const {
1299   assert(getDest()->getType() == IceType_v8i16 ||
1300          getDest()->getType() == IceType_v8i1 ||
1301          getDest()->getType() == IceType_v4i32 ||
1302          getDest()->getType() == IceType_v4i1);
1303   char buf[30];
1304   snprintf(buf, llvm::array_lengthof(buf), "psra%s",
1305            TypeX8632Attributes[getDest()->getType()].PackString);
1306   emitTwoAddress(buf, this, Func);
1307 }
1308
1309 void InstX8632Ret::emit(const Cfg *Func) const {
1310   Ostream &Str = Func->getContext()->getStrEmit();
1311   Str << "\tret\n";
1312 }
1313
1314 void InstX8632Ret::dump(const Cfg *Func) const {
1315   Ostream &Str = Func->getContext()->getStrDump();
1316   Type Ty = (getSrcSize() == 0 ? IceType_void : getSrc(0)->getType());
1317   Str << "ret." << Ty << " ";
1318   dumpSources(Func);
1319 }
1320
1321 void InstX8632Xadd::emit(const Cfg *Func) const {
1322   Ostream &Str = Func->getContext()->getStrEmit();
1323   if (Locked) {
1324     Str << "\tlock";
1325   }
1326   Str << "\txadd\t";
1327   getSrc(0)->emit(Func);
1328   Str << ", ";
1329   getSrc(1)->emit(Func);
1330   Str << "\n";
1331 }
1332
1333 void InstX8632Xadd::dump(const Cfg *Func) const {
1334   Ostream &Str = Func->getContext()->getStrDump();
1335   if (Locked) {
1336     Str << "lock ";
1337   }
1338   Type Ty = getSrc(0)->getType();
1339   Str << "xadd." << Ty << " ";
1340   dumpSources(Func);
1341 }
1342
1343 void InstX8632Xchg::emit(const Cfg *Func) const {
1344   Ostream &Str = Func->getContext()->getStrEmit();
1345   Str << "\txchg\t";
1346   getSrc(0)->emit(Func);
1347   Str << ", ";
1348   getSrc(1)->emit(Func);
1349   Str << "\n";
1350 }
1351
1352 void InstX8632Xchg::dump(const Cfg *Func) const {
1353   Ostream &Str = Func->getContext()->getStrDump();
1354   Type Ty = getSrc(0)->getType();
1355   Str << "xchg." << Ty << " ";
1356   dumpSources(Func);
1357 }
1358
1359 void OperandX8632::dump(const Cfg *Func) const {
1360   Ostream &Str = Func->getContext()->getStrDump();
1361   Str << "<OperandX8632>";
1362 }
1363
1364 void OperandX8632Mem::emit(const Cfg *Func) const {
1365   Ostream &Str = Func->getContext()->getStrEmit();
1366   Str << TypeX8632Attributes[getType()].WidthString << " ";
1367   if (SegmentReg != DefaultSegment) {
1368     assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM);
1369     Str << InstX8632SegmentRegNames[SegmentReg] << ":";
1370   }
1371   // TODO: The following is an almost verbatim paste of dump().
1372   bool Dumped = false;
1373   Str << "[";
1374   if (Base) {
1375     Base->emit(Func);
1376     Dumped = true;
1377   }
1378   if (Index) {
1379     assert(Base);
1380     Str << "+";
1381     if (Shift > 0)
1382       Str << (1u << Shift) << "*";
1383     Index->emit(Func);
1384     Dumped = true;
1385   }
1386   // Pretty-print the Offset.
1387   bool OffsetIsZero = false;
1388   bool OffsetIsNegative = false;
1389   if (Offset == NULL) {
1390     OffsetIsZero = true;
1391   } else if (ConstantInteger *CI = llvm::dyn_cast<ConstantInteger>(Offset)) {
1392     OffsetIsZero = (CI->getValue() == 0);
1393     OffsetIsNegative = (static_cast<int64_t>(CI->getValue()) < 0);
1394   }
1395   if (Dumped) {
1396     if (!OffsetIsZero) {     // Suppress if Offset is known to be 0
1397       if (!OffsetIsNegative) // Suppress if Offset is known to be negative
1398         Str << "+";
1399       Offset->emit(Func);
1400     }
1401   } else {
1402     // There is only the offset.
1403     Offset->emit(Func);
1404   }
1405   Str << "]";
1406 }
1407
1408 void OperandX8632Mem::dump(const Cfg *Func) const {
1409   Ostream &Str = Func->getContext()->getStrDump();
1410   if (SegmentReg != DefaultSegment) {
1411     assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM);
1412     Str << InstX8632SegmentRegNames[SegmentReg] << ":";
1413   }
1414   bool Dumped = false;
1415   Str << "[";
1416   if (Base) {
1417     Base->dump(Func);
1418     Dumped = true;
1419   }
1420   if (Index) {
1421     assert(Base);
1422     Str << "+";
1423     if (Shift > 0)
1424       Str << (1u << Shift) << "*";
1425     Index->dump(Func);
1426     Dumped = true;
1427   }
1428   // Pretty-print the Offset.
1429   bool OffsetIsZero = false;
1430   bool OffsetIsNegative = false;
1431   if (Offset == NULL) {
1432     OffsetIsZero = true;
1433   } else if (ConstantInteger *CI = llvm::dyn_cast<ConstantInteger>(Offset)) {
1434     OffsetIsZero = (CI->getValue() == 0);
1435     OffsetIsNegative = (static_cast<int64_t>(CI->getValue()) < 0);
1436   }
1437   if (Dumped) {
1438     if (!OffsetIsZero) {     // Suppress if Offset is known to be 0
1439       if (!OffsetIsNegative) // Suppress if Offset is known to be negative
1440         Str << "+";
1441       Offset->dump(Func);
1442     }
1443   } else {
1444     // There is only the offset.
1445     Offset->dump(Func);
1446   }
1447   Str << "]";
1448 }
1449
1450 void VariableSplit::emit(const Cfg *Func) const {
1451   Ostream &Str = Func->getContext()->getStrEmit();
1452   assert(Var->getLocalUseNode() == NULL ||
1453          Var->getLocalUseNode() == Func->getCurrentNode());
1454   assert(!Var->hasReg());
1455   // The following is copied/adapted from TargetX8632::emitVariable().
1456   const TargetLowering *Target = Func->getTarget();
1457   const Type Ty = IceType_i32;
1458   Str << TypeX8632Attributes[Ty].WidthString << " ["
1459       << Target->getRegName(Target->getFrameOrStackReg(), Ty);
1460   int32_t Offset = Var->getStackOffset() + Target->getStackAdjustment();
1461   if (Part == High)
1462     Offset += 4;
1463   if (Offset) {
1464     if (Offset > 0)
1465       Str << "+";
1466     Str << Offset;
1467   }
1468   Str << "]";
1469 }
1470
1471 void VariableSplit::dump(const Cfg *Func) const {
1472   Ostream &Str = Func->getContext()->getStrDump();
1473   switch (Part) {
1474   case Low:
1475     Str << "low";
1476     break;
1477   case High:
1478     Str << "high";
1479     break;
1480   default:
1481     Str << "???";
1482     break;
1483   }
1484   Str << "(";
1485   Var->dump(Func);
1486   Str << ")";
1487 }
1488
1489 } // end of namespace Ice