xref: /freebsd/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430InstrInfo.td (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1//===-- MSP430InstrInfo.td - MSP430 Instruction defs -------*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes the MSP430 instructions in TableGen format.
10//
11//===----------------------------------------------------------------------===//
12
13include "MSP430InstrFormats.td"
14
15//===----------------------------------------------------------------------===//
16// Type Constraints.
17//===----------------------------------------------------------------------===//
18class SDTCisI8<int OpNum> : SDTCisVT<OpNum, i8>;
19class SDTCisI16<int OpNum> : SDTCisVT<OpNum, i16>;
20
21//===----------------------------------------------------------------------===//
22// Type Profiles.
23//===----------------------------------------------------------------------===//
24def SDT_MSP430Call         : SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
25def SDT_MSP430CallSeqStart : SDCallSeqStart<[SDTCisVT<0, i16>,
26                                             SDTCisVT<1, i16>]>;
27def SDT_MSP430CallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
28def SDT_MSP430Wrapper      : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
29                                                  SDTCisPtrTy<0>]>;
30def SDT_MSP430Cmp          : SDTypeProfile<0, 2, [SDTCisSameAs<0, 1>]>;
31def SDT_MSP430BrCC         : SDTypeProfile<0, 2, [SDTCisVT<0, OtherVT>,
32                                                  SDTCisVT<1, i8>]>;
33def SDT_MSP430SelectCC     : SDTypeProfile<1, 3, [SDTCisSameAs<0, 1>,
34                                                  SDTCisSameAs<1, 2>,
35                                                  SDTCisVT<3, i8>]>;
36def SDT_MSP430DAdd         : SDTypeProfile<1, 2, [SDTCisSameAs<0, 1>,
37                                                  SDTCisSameAs<0, 2>,
38                                                  SDTCisInt<0>]>;
39
40//===----------------------------------------------------------------------===//
41// MSP430 Specific Node Definitions.
42//===----------------------------------------------------------------------===//
43def MSP430retglue  : SDNode<"MSP430ISD::RET_GLUE", SDTNone,
44                       [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
45def MSP430retiglue : SDNode<"MSP430ISD::RETI_GLUE", SDTNone,
46                       [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
47
48def MSP430rra     : SDNode<"MSP430ISD::RRA", SDTIntUnaryOp, []>;
49def MSP430rla     : SDNode<"MSP430ISD::RLA", SDTIntUnaryOp, []>;
50def MSP430rrc     : SDNode<"MSP430ISD::RRC", SDTIntUnaryOp, []>;
51def MSP430rrcl    : SDNode<"MSP430ISD::RRCL", SDTIntUnaryOp, []>;
52
53def MSP430call    : SDNode<"MSP430ISD::CALL", SDT_MSP430Call,
54                     [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue, SDNPVariadic]>;
55def MSP430callseq_start :
56                 SDNode<"ISD::CALLSEQ_START", SDT_MSP430CallSeqStart,
57                        [SDNPHasChain, SDNPOutGlue]>;
58def MSP430callseq_end :
59                 SDNode<"ISD::CALLSEQ_END",   SDT_MSP430CallSeqEnd,
60                        [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
61def MSP430Wrapper : SDNode<"MSP430ISD::Wrapper", SDT_MSP430Wrapper>;
62def MSP430cmp     : SDNode<"MSP430ISD::CMP", SDT_MSP430Cmp, [SDNPOutGlue]>;
63def MSP430brcc    : SDNode<"MSP430ISD::BR_CC", SDT_MSP430BrCC,
64                            [SDNPHasChain, SDNPInGlue]>;
65def MSP430selectcc: SDNode<"MSP430ISD::SELECT_CC", SDT_MSP430SelectCC,
66                            [SDNPInGlue]>;
67def MSP430dadd    : SDNode<"MSP430ISD::DADD", SDT_MSP430DAdd, []>;
68
69//===----------------------------------------------------------------------===//
70// MSP430 Operand Definitions.
71//===----------------------------------------------------------------------===//
72
73def MemAsmOperand : AsmOperandClass {
74  let Name = "Mem";
75}
76
77// Address operands
78def memsrc : Operand<i16> {
79  let PrintMethod = "printSrcMemOperand";
80  let MIOperandInfo = (ops GR16, i16imm);
81  let ParserMatchClass = MemAsmOperand;
82  let EncoderMethod = "getMemOpValue";
83  let DecoderMethod = "DecodeMemOperand";
84}
85
86def memdst : Operand<i16> {
87  let PrintMethod = "printSrcMemOperand";
88  let MIOperandInfo = (ops GR16, i16imm);
89  let ParserMatchClass = MemAsmOperand;
90  let EncoderMethod = "getMemOpValue";
91  let DecoderMethod = "DecodeMemOperand";
92}
93
94def IndRegAsmOperand : AsmOperandClass {
95  let Name = "IndReg";
96  let RenderMethod = "addRegOperands";
97}
98
99def indreg : Operand<i16> {
100  let PrintMethod = "printIndRegOperand";
101  let MIOperandInfo = (ops GR16);
102  let ParserMatchClass = IndRegAsmOperand;
103  let DecoderMethod = "DecodeGR16RegisterClass";
104}
105
106def PostIndRegAsmOperand : AsmOperandClass {
107  let Name = "PostIndReg";
108  let RenderMethod = "addRegOperands";
109}
110
111def postreg : Operand<i16> {
112  let PrintMethod = "printPostIndRegOperand";
113  let MIOperandInfo = (ops GR16);
114  let ParserMatchClass = PostIndRegAsmOperand;
115  let DecoderMethod = "DecodeGR16RegisterClass";
116}
117
118// Short jump targets have OtherVT type and are printed as pcrel imm values.
119def jmptarget : Operand<OtherVT> {
120  let PrintMethod = "printPCRelImmOperand";
121  let EncoderMethod = "getPCRelImmOpValue";
122}
123
124// Operand for printing out a condition code.
125def cc : Operand<i8> {
126  let PrintMethod = "printCCOperand";
127  let EncoderMethod = "getCCOpValue";
128}
129
130def CGImmAsmOperand : AsmOperandClass {
131  let Name = "CGImm";
132  let RenderMethod = "addImmOperands";
133}
134
135def cg8imm : Operand<i8>,
136             ImmLeaf<i8, [{return Imm == 0 || Imm == 1 || Imm == 2 ||
137                                  Imm == 4 || Imm == 8 || Imm == -1;}]> {
138  let ParserMatchClass = CGImmAsmOperand;
139  let EncoderMethod = "getCGImmOpValue";
140  let DecoderMethod = "DecodeCGImm";
141}
142
143def cg16imm : Operand<i16>,
144              ImmLeaf<i16, [{return Imm == 0 || Imm == 1 || Imm == 2 ||
145                                    Imm == 4 || Imm == 8 || Imm == -1;}]> {
146  let ParserMatchClass = CGImmAsmOperand;
147  let EncoderMethod = "getCGImmOpValue";
148  let DecoderMethod = "DecodeCGImm";
149}
150
151//===----------------------------------------------------------------------===//
152// MSP430 Complex Pattern Definitions.
153//===----------------------------------------------------------------------===//
154
155def addr : ComplexPattern<iPTR, 2, "SelectAddr", [], []>;
156
157//===----------------------------------------------------------------------===//
158// Pattern Fragments
159def zextloadi16i8 : PatFrag<(ops node:$ptr), (i16 (zextloadi8 node:$ptr))>;
160def  extloadi16i8 : PatFrag<(ops node:$ptr), (i16 ( extloadi8 node:$ptr))>;
161def bic : PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, (not node:$rhs))>;
162def and_su : PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, node:$rhs), [{
163  return N->hasOneUse();
164}]>;
165//===----------------------------------------------------------------------===//
166// Instruction list..
167
168// ADJCALLSTACKDOWN/UP implicitly use/def SP because they may be expanded into
169// a stack adjustment and the codegen must know that they may modify the stack
170// pointer before prolog-epilog rewriting occurs.
171// Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
172// sub / add which can clobber SR.
173let isCodeGenOnly = 1, Defs = [SP, SR], Uses = [SP] in {
174def ADJCALLSTACKDOWN : Pseudo<(outs), (ins i16imm:$amt1, i16imm:$amt2),
175                              "#ADJCALLSTACKDOWN $amt1 $amt2",
176                              [(MSP430callseq_start timm:$amt1, timm:$amt2)]>;
177def ADJCALLSTACKUP   : Pseudo<(outs), (ins i16imm:$amt1, i16imm:$amt2),
178                              "#ADJCALLSTACKUP $amt1 $amt2",
179                              [(MSP430callseq_end timm:$amt1, timm:$amt2)]>;
180}
181
182let isCodeGenOnly = 1, Defs = [SR], Uses = [SP] in {
183def ADDframe : Pseudo<(outs GR16:$dst), (ins i16imm:$base, i16imm:$offset),
184                      "# ADDframe PSEUDO", []>;
185}
186
187let isCodeGenOnly = 1, usesCustomInserter = 1 in {
188  let Uses = [SR] in {
189  def Select8  : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$src2, i8imm:$cc),
190                        "# Select8 PSEUDO",
191                        [(set GR8:$dst,
192                          (MSP430selectcc GR8:$src, GR8:$src2, imm:$cc))]>;
193  def Select16 : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR16:$src2, i8imm:$cc),
194                        "# Select16 PSEUDO",
195                        [(set GR16:$dst,
196                          (MSP430selectcc GR16:$src, GR16:$src2, imm:$cc))]>;
197  }
198  let Defs = [SR] in {
199  def Shl8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
200                        "# Shl8 PSEUDO",
201                        [(set GR8:$dst, (shl GR8:$src, GR8:$cnt))]>;
202  def Shl16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
203                        "# Shl16 PSEUDO",
204                        [(set GR16:$dst, (shl GR16:$src, GR8:$cnt))]>;
205  def Sra8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
206                        "# Sra8 PSEUDO",
207                        [(set GR8:$dst, (sra GR8:$src, GR8:$cnt))]>;
208  def Sra16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
209                        "# Sra16 PSEUDO",
210                        [(set GR16:$dst, (sra GR16:$src, GR8:$cnt))]>;
211  def Srl8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
212                        "# Srl8 PSEUDO",
213                        [(set GR8:$dst, (srl GR8:$src, GR8:$cnt))]>;
214  def Srl16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
215                        "# Srl16 PSEUDO",
216                        [(set GR16:$dst, (srl GR16:$src, GR8:$cnt))]>;
217  def Rrcl8    : Pseudo<(outs GR8:$dst), (ins GR8:$src), "",
218                        [(set GR8:$dst, (MSP430rrcl GR8:$src))]>;
219  def Rrcl16   : Pseudo<(outs GR16:$dst), (ins GR16:$src), "",
220                        [(set GR16:$dst, (MSP430rrcl GR16:$src))]>;
221  }
222}
223
224//===----------------------------------------------------------------------===//
225//  Control Flow Instructions...
226//
227
228let isReturn = 1, isTerminator = 1, isBarrier = 1 in {
229  def RET  : IForm16<0b0100, DstReg, SrcPostInc, 2,
230                     (outs), (ins), "ret",  [(MSP430retglue)]> {
231    let DecoderNamespace = "Delta";
232    let rs = 1;
233    let rd = 0;
234  }
235  def RETI : IIForm16<0b110, SrcReg, 2,
236                      (outs), (ins), "reti", [(MSP430retiglue)]> {
237    let rs = 0;
238  }
239}
240
241let isBranch = 1, isTerminator = 1 in {
242
243// FIXME: expand opcode & cond field for branches!
244
245// Direct branch
246let isBarrier = 1 in {
247  // Short branch
248  def JMP : CJForm<(outs), (ins jmptarget:$dst),
249                   "jmp\t$dst",
250                   [(br bb:$dst)]> {
251    let cond = 0b111;
252  }
253  let isIndirectBranch = 1, rd = 0 in {
254    // Long branches
255    def Bi  : I16ri<0b0100, (outs), (ins i16imm:$imm),
256                    "br\t$imm",
257                    [(brind tblockaddress:$imm)]>;
258    def Br  : I16rr<0b0100, (outs), (ins GR16:$rs),
259                    "br\t$rs",
260                    [(brind GR16:$rs)]>;
261    def Bm  : I16rm<0b0100, (outs), (ins memsrc:$src),
262                    "br\t$src",
263                    [(brind (load addr:$src))]>;
264  }
265}
266
267// Conditional branches
268let Uses = [SR] in
269  def JCC : CJForm<(outs), (ins jmptarget:$dst, cc:$cond),
270                   "j$cond\t$dst",
271                   [(MSP430brcc bb:$dst, imm:$cond)]>;
272} // isBranch, isTerminator
273
274//===----------------------------------------------------------------------===//
275//  Call Instructions...
276//
277// All calls clobber the non-callee saved registers. SPW is marked as
278// a use to prevent stack-pointer assignments that appear immediately
279// before calls from potentially appearing dead. Uses for argument
280// registers are added manually.
281let isCall = 1,
282    Defs = [R11, R12, R13, R14, R15, SR],
283    Uses = [SP] in {
284  def CALLi     : II16i<0b101,
285                        (outs), (ins i16imm:$imm),
286                        "call\t$imm", [(MSP430call imm:$imm)]>;
287  def CALLr     : II16r<0b101,
288                        (outs), (ins GR16:$rs),
289                        "call\t$rs", [(MSP430call GR16:$rs)]>;
290  def CALLm     : II16m<0b101,
291                        (outs), (ins memsrc:$src),
292                        "call\t$src", [(MSP430call (load addr:$src))]>;
293  def CALLn     : II16n<0b101, (outs), (ins indreg:$rs), "call\t$rs", []>;
294  def CALLp     : II16p<0b101, (outs), (ins postreg:$rs), "call\t$rs", []>;
295}
296
297//===----------------------------------------------------------------------===//
298//  Miscellaneous Instructions...
299//
300let Defs = [SP], Uses = [SP], hasSideEffects = 0 in {
301let mayLoad = 1 in
302def POP16r   : IForm16<0b0100, DstReg, SrcPostInc, 2,
303                       (outs GR16:$rd), (ins), "pop\t$rd", []> {
304  let DecoderNamespace = "Delta";
305  let rs = 1;
306}
307
308let mayStore = 1 in {
309def PUSH8r :  II8r<0b100, (outs), (ins GR8:$rs), "push.b\t$rs", []>;
310def PUSH16r : II16r<0b100, (outs), (ins GR16:$rs), "push\t$rs", []>;
311def PUSH16c : II16c<0b100, (outs), (ins cg16imm:$imm), "push\t$imm", []>;
312def PUSH16i : II16i<0b100, (outs), (ins i16imm:$imm), "push\t$imm", []>;
313}
314}
315
316//===----------------------------------------------------------------------===//
317// Move Instructions
318
319let hasSideEffects = 0 in {
320def MOV8rr  : I8rr<0b0100,
321                   (outs GR8:$rd), (ins GR8:$rs),
322                   "mov.b\t{$rs, $rd}",
323                   []>;
324def MOV16rr : I16rr<0b0100,
325                    (outs GR16:$rd), (ins GR16:$rs),
326                    "mov\t{$rs, $rd}",
327                    []>;
328}
329
330let isReMaterializable = 1, isAsCheapAsAMove = 1 in {
331def MOV8rc : I8rc<0b0100,
332                   (outs GR8:$rd), (ins cg8imm:$imm),
333                   "mov.b\t$imm, $rd",
334                   [(set GR8:$rd, cg8imm:$imm)]>;
335def MOV16rc : I16rc<0b0100,
336                    (outs GR16:$rd), (ins cg16imm:$imm),
337                    "mov\t$imm, $rd",
338                    [(set GR16:$rd, cg16imm:$imm)]>;
339def MOV8ri  : I8ri<0b0100,
340                   (outs GR8:$rd), (ins i8imm:$imm),
341                   "mov.b\t{$imm, $rd}",
342                   [(set GR8:$rd, imm:$imm)]>;
343def MOV16ri : I16ri<0b0100,
344                    (outs GR16:$rd), (ins i16imm:$imm),
345                    "mov\t{$imm, $rd}",
346                    [(set GR16:$rd, imm:$imm)]>;
347}
348
349let canFoldAsLoad = 1, isReMaterializable = 1 in {
350def MOV8rm  : I8rm<0b0100,
351                   (outs GR8:$rd), (ins memsrc:$src),
352                   "mov.b\t{$src, $rd}",
353                   [(set GR8:$rd, (load addr:$src))]>;
354def MOV16rm : I16rm<0b0100,
355                    (outs GR16:$rd), (ins memsrc:$src),
356                    "mov\t{$src, $rd}",
357                    [(set GR16:$rd, (load addr:$src))]>;
358def MOV8rn  : I8rn<0b0100,
359                   (outs GR8:$rd), (ins indreg:$rs),
360                   "mov.b\t{$rs, $rd}",
361                   [(set GR8:$rd, (load addr:$rs))]>;
362def MOV16rn : I16rn<0b0100,
363                    (outs GR16:$rd), (ins indreg:$rs),
364                    "mov\t{$rs, $rd}",
365                    [(set GR16:$rd, (load addr:$rs))]>;
366}
367
368let isCodeGenOnly = 1 in {
369def MOVZX16rr8 : I8rr<0b0100,
370                      (outs GR16:$rd), (ins GR8:$rs),
371                      "mov.b\t{$rs, $rd}",
372                      [(set GR16:$rd, (zext GR8:$rs))]>;
373def MOVZX16rm8 : I8rm<0b0100,
374                      (outs GR16:$rd), (ins memsrc:$src),
375                      "mov.b\t{$src, $rd}",
376                      [(set GR16:$rd, (zextloadi16i8 addr:$src))]>;
377}
378
379let mayLoad = 1, hasExtraDefRegAllocReq = 1, Constraints = "$rs = $wb" in {
380def MOV8rp  : I8rp<0b0100,
381                   (outs GR8:$rd, GR16:$wb), (ins postreg:$rs),
382                   "mov.b\t{$rs, $rd}", []>;
383def MOV16rp : I16rp<0b0100,
384                    (outs GR16:$rd, GR16:$wb), (ins postreg:$rs),
385                    "mov\t{$rs, $rd}", []>;
386}
387
388// Any instruction that defines a 8-bit result leaves the high half of the
389// register. Truncate can be lowered to EXTRACT_SUBREG, and CopyFromReg may
390// be copying from a truncate, but any other 8-bit operation will zero-extend
391// up to 16 bits.
392def def8 : PatLeaf<(i8 GR8:$src), [{
393  return N->getOpcode() != ISD::TRUNCATE &&
394         N->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
395         N->getOpcode() != ISD::CopyFromReg;
396}]>;
397
398// In the case of a 8-bit def that is known to implicitly zero-extend,
399// we can use a SUBREG_TO_REG.
400def : Pat<(i16 (zext def8:$src)),
401          (SUBREG_TO_REG (i16 0), GR8:$src, subreg_8bit)>;
402
403def MOV8mc  : I8mc<0b0100,
404                   (outs), (ins memdst:$dst, cg8imm:$imm),
405                   "mov.b\t{$imm, $dst}",
406                   [(store (i8 cg8imm:$imm), addr:$dst)]>;
407def MOV16mc : I16mc<0b0100,
408                    (outs), (ins memdst:$dst, cg16imm:$imm),
409                    "mov\t{$imm, $dst}",
410                    [(store (i16 cg16imm:$imm), addr:$dst)]>;
411
412def MOV8mi  : I8mi<0b0100,
413                   (outs), (ins memdst:$dst, i8imm:$imm),
414                   "mov.b\t{$imm, $dst}",
415                   [(store (i8 imm:$imm), addr:$dst)]>;
416def MOV16mi : I16mi<0b0100,
417                    (outs), (ins memdst:$dst, i16imm:$imm),
418                    "mov\t{$imm, $dst}",
419                    [(store (i16 imm:$imm), addr:$dst)]>;
420
421def MOV8mr  : I8mr<0b0100,
422                   (outs), (ins memdst:$dst, GR8:$rs),
423                   "mov.b\t{$rs, $dst}",
424                   [(store GR8:$rs, addr:$dst)]>;
425def MOV16mr : I16mr<0b0100,
426                    (outs), (ins memdst:$dst, GR16:$rs),
427                    "mov\t{$rs, $dst}",
428                    [(store GR16:$rs, addr:$dst)]>;
429
430def MOV8mm  : I8mm<0b0100,
431                   (outs), (ins memdst:$dst, memsrc:$src),
432                   "mov.b\t{$src, $dst}",
433                   [(store (i8 (load addr:$src)), addr:$dst)]>;
434def MOV16mm : I16mm<0b0100,
435                    (outs), (ins memdst:$dst, memsrc:$src),
436                    "mov\t{$src, $dst}",
437                    [(store (i16 (load addr:$src)), addr:$dst)]>;
438
439def MOV8mn  : I8mn<0b0100, (outs), (ins memdst:$dst, indreg:$rs),
440                   "mov.b\t{$rs, $dst}", []>;
441def MOV16mn : I16mn<0b0100, (outs), (ins memdst:$dst, indreg:$rs),
442                    "mov\t{$rs, $dst}", []>;
443
444//===----------------------------------------------------------------------===//
445// Arithmetic Instructions
446
447multiclass Arith<bits<4> opcode, string asmstring, SDPatternOperator node,
448                 bit commutes, list<Register> uses> {
449  let Defs = [SR], Uses = uses in {
450  let Constraints = "$src2 = $rd" in {
451  let isCommutable = commutes in {
452  def 8rr : I8rr<opcode, (outs GR8:$rd), (ins GR8:$src2, GR8:$rs),
453                 !strconcat(asmstring, ".b\t$rs, $rd"),
454                 [(set GR8:$rd, (node GR8:$src2, GR8:$rs)),
455                  (implicit SR)]>;
456  def 16rr : I16rr<opcode, (outs GR16:$rd), (ins GR16:$src2, GR16:$rs),
457                   !strconcat(asmstring, "\t$rs, $rd"),
458                   [(set GR16:$rd, (node GR16:$src2, GR16:$rs)),
459                    (implicit SR)]>;
460  }
461  def 8rm : I8rm<opcode, (outs GR8:$rd), (ins GR8:$src2, memsrc:$src),
462                 !strconcat(asmstring, ".b\t$src, $rd"),
463                 [(set GR8:$rd, (node GR8:$src2, (load addr:$src))),
464                  (implicit SR)]>;
465  def 16rm : I16rm<opcode, (outs GR16:$rd), (ins GR16:$src2, memsrc:$src),
466                   !strconcat(asmstring, "\t$src, $rd"),
467                   [(set GR16:$rd, (node GR16:$src2, (load addr:$src))),
468                    (implicit SR)]>;
469  def 8rn : I8rn<opcode, (outs GR8:$rd), (ins GR8:$src2, indreg:$rs),
470                 !strconcat(asmstring, ".b\t$rs, $rd"), []>;
471  def 16rn : I16rn<opcode, (outs GR16:$rd), (ins GR16:$src2, indreg:$rs),
472                   !strconcat(asmstring, "\t$rs, $rd"), []>;
473  let mayLoad = 1,
474      hasExtraDefRegAllocReq = 1,
475      Constraints = "$rs = $wb, $src2 = $rd" in {
476  def 8rp : I8rp<opcode, (outs GR8:$rd, GR16:$wb), (ins GR8:$src2, postreg:$rs),
477                 !strconcat(asmstring, ".b\t$rs, $rd"), []>;
478  def 16rp : I16rp<opcode, (outs GR16:$rd, GR16:$wb), (ins GR16:$src2, postreg:$rs),
479                   !strconcat(asmstring, "\t$rs, $rd"), []>;
480  }
481  def 8rc : I8rc<opcode, (outs GR8:$rd), (ins GR8:$src2, cg8imm:$imm),
482                 !strconcat(asmstring, ".b\t$imm, $rd"),
483                 [(set GR8:$rd, (node GR8:$src2, cg8imm:$imm)),
484                  (implicit SR)]>;
485  def 16rc : I16rc<opcode, (outs GR16:$rd), (ins GR16:$src2, cg16imm:$imm),
486                 !strconcat(asmstring, "\t$imm, $rd"),
487                 [(set GR16:$rd, (node GR16:$src2, cg16imm:$imm)),
488                  (implicit SR)]>;
489  def 8ri : I8ri<opcode, (outs GR8:$rd), (ins GR8:$src2, i8imm:$imm),
490                 !strconcat(asmstring, ".b\t$imm, $rd"),
491                 [(set GR8:$rd, (node GR8:$src2, imm:$imm)),
492                  (implicit SR)]>;
493  def 16ri : I16ri<opcode, (outs GR16:$rd), (ins GR16:$src2, i16imm:$imm),
494                 !strconcat(asmstring, "\t$imm, $rd"),
495                 [(set GR16:$rd, (node GR16:$src2, imm:$imm)),
496                  (implicit SR)]>;
497  }
498  def 8mr : I8mr<opcode, (outs), (ins memdst:$dst, GR8:$rs),
499                 !strconcat(asmstring, ".b\t$rs, $dst"),
500                 [(store (node (load addr:$dst), GR8:$rs), addr:$dst),
501                  (implicit SR)]>;
502  def 16mr : I16mr<opcode, (outs), (ins memdst:$dst, GR16:$rs),
503                   !strconcat(asmstring, "\t$rs, $dst"),
504                   [(store (node (load addr:$dst), GR16:$rs), addr:$dst),
505                    (implicit SR)]>;
506  def 8mc : I8mc<opcode, (outs), (ins memdst:$dst, cg8imm:$imm),
507                 !strconcat(asmstring, ".b\t$imm, $dst"),
508                 [(store (node (load addr:$dst), (i8 cg8imm:$imm)), addr:$dst),
509                  (implicit SR)]>;
510  def 16mc : I16mc<opcode, (outs), (ins memdst:$dst, cg16imm:$imm),
511                   !strconcat(asmstring, "\t$imm, $dst"),
512                   [(store (node (load addr:$dst), (i16 cg16imm:$imm)), addr:$dst),
513                    (implicit SR)]>;
514  def 8mi : I8mi<opcode, (outs), (ins memdst:$dst, i8imm:$imm),
515                 !strconcat(asmstring, ".b\t$imm, $dst"),
516                 [(store (node (load addr:$dst), (i8 imm:$imm)), addr:$dst),
517                  (implicit SR)]>;
518  def 16mi : I16mi<opcode, (outs), (ins memdst:$dst, i16imm:$imm),
519                   !strconcat(asmstring, "\t$imm, $dst"),
520                   [(store (node (load addr:$dst), (i16 imm:$imm)), addr:$dst),
521                    (implicit SR)]>;
522  def 8mm : I8mm<opcode, (outs), (ins memdst:$dst, memsrc:$src),
523                 !strconcat(asmstring, ".b\t$src, $dst"),
524                 [(store (node (load addr:$dst),
525                               (i8 (load addr:$src))), addr:$dst),
526                  (implicit SR)]>;
527  def 16mm : I16mm<opcode, (outs), (ins memdst:$dst, memsrc:$src),
528                   !strconcat(asmstring, "\t$src, $dst"),
529                   [(store (node (load addr:$dst),
530                                 (i16 (load addr:$src))), addr:$dst),
531                    (implicit SR)]>;
532  def 8mn : I8mn<opcode, (outs), (ins memdst:$dst, indreg:$rs),
533                 !strconcat(asmstring, ".b\t$rs, $dst"), []>;
534  def 16mn : I16mn<opcode, (outs), (ins memdst:$dst, indreg:$rs),
535                   !strconcat(asmstring, "\t$rs, $dst"), []>;
536  def 8mp : I8mp<opcode, (outs), (ins memdst:$dst, postreg:$rs),
537                 !strconcat(asmstring, ".b\t$rs, $dst"), []>;
538  def 16mp : I16mp<opcode, (outs), (ins memdst:$dst, postreg:$rs),
539                   !strconcat(asmstring, "\t$rs, $dst"), []>;
540  }
541}
542
543defm ADD  : Arith<0b0101, "add",  add,  1, []>;
544defm ADDC : Arith<0b0110, "addc", adde, 1, [SR]>;
545defm AND  : Arith<0b1111, "and",  and,  1, []>;
546defm BIS  : Arith<0b1101, "bis",  or,   1, []>;
547defm BIC  : Arith<0b1100, "bic",  bic,  0, []>;
548defm XOR  : Arith<0b1110, "xor",  xor,  1, []>;
549defm SUB  : Arith<0b1000, "sub",  sub,  0, []>;
550defm SUBC : Arith<0b0111, "subc", sube, 0, [SR]>;
551defm DADD : Arith<0b1010, "dadd", MSP430dadd, 1, [SR]>;
552
553def ADC8r   : InstAlias<"adc.b\t$dst",  (ADDC8rc   GR8:$dst,     0)>;
554def ADC16r  : InstAlias<"adc\t$dst",    (ADDC16rc  GR16:$dst,    0)>;
555def ADC8m   : InstAlias<"adc.b\t$dst",  (ADDC8mc   memdst:$dst,  0)>;
556def ADC16m  : InstAlias<"adc\t$dst",    (ADDC16mc  memdst:$dst,  0)>;
557
558def DADC8r  : InstAlias<"dadc.b\t$dst", (DADD8rc   GR8:$dst,     0)>;
559def DADC16r : InstAlias<"dadc\t$dst",   (DADD16rc  GR16:$dst,    0)>;
560def DADC8m  : InstAlias<"dadc.b\t$dst", (DADD8mc   memdst:$dst,  0)>;
561def DADC16m : InstAlias<"dadc\t$dst",   (DADD16mc  memdst:$dst,  0)>;
562
563def DEC8r   : InstAlias<"dec.b\t$dst",  (SUB8rc    GR8:$dst,     1)>;
564def DEC16r  : InstAlias<"dec\t$dst",    (SUB16rc   GR16:$dst,    1)>;
565def DEC8m   : InstAlias<"dec.b\t$dst",  (SUB8mc    memdst:$dst,  1)>;
566def DEC16m  : InstAlias<"dec\t$dst",    (SUB16mc   memdst:$dst,  1)>;
567
568def DECD8r  : InstAlias<"decd.b\t$dst", (SUB8rc    GR8:$dst,     2)>;
569def DECD16r : InstAlias<"decd\t$dst",   (SUB16rc   GR16:$dst,    2)>;
570def DECD8m  : InstAlias<"decd.b\t$dst", (SUB8mc    memdst:$dst,  2)>;
571def DECD16m : InstAlias<"decd\t$dst",   (SUB16mc   memdst:$dst,  2)>;
572
573def INC8r   : InstAlias<"inc.b\t$dst",  (ADD8rc    GR8:$dst,     1)>;
574def INC16r  : InstAlias<"inc\t$dst",    (ADD16rc   GR16:$dst,    1)>;
575def INC8m   : InstAlias<"inc.b\t$dst",  (ADD8mc    memdst:$dst,  1)>;
576def INC16m  : InstAlias<"inc\t$dst",    (ADD16mc   memdst:$dst,  1)>;
577
578def INCD8r  : InstAlias<"incd.b\t$dst", (ADD8rc    GR8:$dst,     2)>;
579def INCD16r : InstAlias<"incd\t$dst",   (ADD16rc   GR16:$dst,    2)>;
580def INCD8m  : InstAlias<"incd.b\t$dst", (ADD8mc    memdst:$dst,  2)>;
581def INCD16m : InstAlias<"incd\t$dst",   (ADD16mc   memdst:$dst,  2)>;
582
583def SBC8r   : InstAlias<"sbc.b\t$dst",  (SUBC8rc   GR8:$dst,     0)>;
584def SBC16r  : InstAlias<"sbc\t$dst",    (SUBC16rc  GR16:$dst,    0)>;
585def SBC8m   : InstAlias<"sbc.b\t$dst",  (SUBC8mc   memdst:$dst,  0)>;
586def SBC16m  : InstAlias<"sbc\t$dst",    (SUBC16mc  memdst:$dst,  0)>;
587
588def INV8r   : InstAlias<"inv.b\t$dst",  (XOR8rc    GR8:$dst,    -1)>;
589def INV16r  : InstAlias<"inv\t$dst",    (XOR16rc   GR16:$dst,   -1)>;
590def INV8m   : InstAlias<"inv.b\t$dst",  (XOR8mc    memdst:$dst, -1)>;
591def INV16m  : InstAlias<"inv\t$dst",    (XOR16mc   memdst:$dst, -1)>;
592
593// printAliasInstr() doesn't check $dst operands are actually equal
594// for RLA and RLC aliases below, so disable printing aliases.
595
596def RLA8r   : InstAlias<"rla.b\t$dst",  (ADD8rr    GR8:$dst,     GR8:$dst),    0>;
597def RLA16r  : InstAlias<"rla\t$dst",    (ADD16rr   GR16:$dst,    GR16:$dst),   0>;
598def RLA8m   : InstAlias<"rla.b\t$dst",  (ADD8mm    memdst:$dst,  memdst:$dst), 0>;
599def RLA16m  : InstAlias<"rla\t$dst",    (ADD16mm   memdst:$dst,  memdst:$dst), 0>;
600
601def RLC8r   : InstAlias<"rlc.b\t$dst",  (ADDC8rr   GR8:$dst,     GR8:$dst),    0>;
602def RLC16r  : InstAlias<"rlc\t$dst",    (ADDC16rr  GR16:$dst,    GR16:$dst),   0>;
603def RLC8m   : InstAlias<"rlc.b\t$dst",  (ADDC8mm   memdst:$dst,  memdst:$dst), 0>;
604def RLC16m  : InstAlias<"rlc\t$dst",    (ADDC16mm  memdst:$dst,  memdst:$dst), 0>;
605
606def DINT : InstAlias<"dint", (BIC16rc SR, 8)>;
607def EINT : InstAlias<"eint", (BIS16rc SR, 8)>;
608
609def NOP  : InstAlias<"nop",  (MOV16rc CG, 0)>;
610
611def CLR8r   : InstAlias<"clr.b\t$dst",  (MOV8rc    GR8:$dst,     0)>;
612def CLR16r  : InstAlias<"clr\t$dst",    (MOV16rc   GR16:$dst,    0)>;
613def CLR8m   : InstAlias<"clr.b\t$dst",  (MOV8mc    memdst:$dst,  0)>;
614def CLR16m  : InstAlias<"clr\t$dst",    (MOV16mc   memdst:$dst,  0)>;
615
616def CLRC : InstAlias<"clrc", (BIC16rc SR, 1)>;
617def CLRN : InstAlias<"clrn", (BIC16rc SR, 4)>;
618def CLRZ : InstAlias<"clrz", (BIC16rc SR, 2)>;
619def SETC : InstAlias<"setc", (BIS16rc SR, 1)>;
620def SETN : InstAlias<"setn", (BIS16rc SR, 4)>;
621def SETZ : InstAlias<"setz", (BIS16rc SR, 2)>;
622
623def : Pat<(MSP430rla GR8:$dst),  (ADD8rr  $dst, $dst)>;
624def : Pat<(MSP430rla GR16:$dst), (ADD16rr $dst, $dst)>;
625
626// Format-II (Single Operand) Instruction
627// Register mode
628let Constraints = "$rs = $rd" in {
629
630let Defs = [SR] in {
631def RRA8r :   II8r<0b010,
632                   (outs GR8:$rd), (ins GR8:$rs),
633                   "rra.b\t$rd",
634                   [(set GR8:$rd, (MSP430rra GR8:$rs)),
635                    (implicit SR)]>;
636def RRA16r : II16r<0b010,
637                    (outs GR16:$rd), (ins GR16:$rs),
638                    "rra\t$rd",
639                    [(set GR16:$rd, (MSP430rra GR16:$rs)),
640                     (implicit SR)]>;
641
642let Uses = [SR] in {
643def RRC8r :   II8r<0b000,
644                   (outs GR8:$rd), (ins GR8:$rs),
645                   "rrc.b\t$rd",
646                   [(set GR8:$rd, (MSP430rrc GR8:$rs)),
647                    (implicit SR)]>;
648def RRC16r : II16r<0b000,
649                   (outs GR16:$rd), (ins GR16:$rs),
650                   "rrc\t$rd",
651                   [(set GR16:$rd, (MSP430rrc GR16:$rs)),
652                    (implicit SR)]>;
653} // Uses = [SR]
654
655def SEXT16r : II16r<0b011,
656                    (outs GR16:$rd), (ins GR16:$rs),
657                    "sxt\t$rd",
658                    [(set GR16:$rd, (sext_inreg GR16:$rs, i8)),
659                     (implicit SR)]>;
660
661} // Defs = [SR]
662
663let isCodeGenOnly = 1 in
664def ZEXT16r : I8rr<0b0100,
665                   (outs GR16:$rd), (ins GR16:$rs),
666                   "mov.b\t{$rs, $rd}",
667                   [(set GR16:$rd, (zext (trunc GR16:$rs)))]>;
668
669def SWPB16r : II16r<0b001,
670                    (outs GR16:$rd), (ins GR16:$rs),
671                    "swpb\t$rd",
672                    [(set GR16:$rd, (bswap GR16:$rs))]>;
673
674} // Constraints = "$src = $dst"
675
676// Indexed, indirect register and indirect autoincrement modes
677let Defs = [SR] in {
678def RRA8m  : II8m<0b010,
679                   (outs), (ins memsrc:$src),
680                   "rra.b\t$src",
681                   [(store (MSP430rra (i8 (load addr:$src))), addr:$src),
682                    (implicit SR)]>;
683def RRA16m : II16m<0b010,
684                   (outs), (ins memsrc:$src),
685                   "rra\t$src",
686                   [(store (MSP430rra (i16 (load addr:$src))), addr:$src),
687                    (implicit SR)]>;
688
689def RRA8n  : II8n<0b010, (outs), (ins indreg:$rs), "rra.b\t$rs", []>;
690def RRA16n : II16n<0b010, (outs), (ins indreg:$rs), "rra\t$rs", []>;
691def RRA8p  : II8p<0b010, (outs), (ins postreg:$rs), "rra.b\t$rs", []>;
692def RRA16p : II16p<0b010, (outs), (ins postreg:$rs), "rra\t$rs", []>;
693
694let Uses = [SR] in {
695def RRC8m  : II8m<0b000,
696                   (outs), (ins memsrc:$src),
697                   "rrc.b\t$src",
698                   [(store (MSP430rrc (i8 (load addr:$src))), addr:$src),
699                    (implicit SR)]>;
700def RRC16m : II16m<0b000,
701                   (outs), (ins memsrc:$src),
702                   "rrc\t$src",
703                   [(store (MSP430rrc (i16 (load addr:$src))), addr:$src),
704                    (implicit SR)]>;
705
706def RRC8n  : II8n<0b000, (outs), (ins indreg:$rs), "rrc.b\t$rs", []>;
707def RRC16n : II16n<0b000, (outs), (ins indreg:$rs), "rrc\t$rs", []>;
708def RRC8p  : II8p<0b000, (outs), (ins postreg:$rs), "rrc.b\t$rs", []>;
709def RRC16p : II16p<0b000, (outs), (ins postreg:$rs), "rrc\t$rs", []>;
710
711} // Uses = [SR]
712
713def SEXT16m : II16m<0b011,
714                    (outs), (ins memsrc:$src),
715                    "sxt\t$src",
716                    [(store (sext_inreg (extloadi16i8 addr:$src), i8),
717                             addr:$src),
718                     (implicit SR)]>;
719def SEXT16n : II16n<0b011, (outs), (ins indreg:$rs), "sxt\t$rs", []>;
720def SEXT16p : II16p<0b011, (outs), (ins postreg:$rs), "sxt\t$rs", []>;
721
722} // Defs = [SR]
723
724def SWPB16m : II16m<0b001,
725                   (outs), (ins memsrc:$src),
726                   "swpb\t$src",
727                   [(store (bswap (i16 (load addr:$src))), addr:$src)]>;
728def SWPB16n : II16n<0b001, (outs), (ins indreg:$rs), "swpb\t$rs", []>;
729def SWPB16p : II16p<0b001, (outs), (ins postreg:$rs), "swpb\t$rs", []>;
730
731// Integer comparisons
732let Defs = [SR] in {
733def CMP8rr  : I8rr<0b1001,
734                   (outs), (ins GR8:$rd, GR8:$rs),
735                   "cmp.b\t$rs, $rd",
736                   [(MSP430cmp GR8:$rd, GR8:$rs), (implicit SR)]>;
737def CMP16rr : I16rr<0b1001,
738                    (outs), (ins GR16:$rd, GR16:$rs),
739                    "cmp\t$rs, $rd",
740                    [(MSP430cmp GR16:$rd, GR16:$rs), (implicit SR)]>;
741
742def CMP8rc  : I8rc<0b1001,
743                   (outs), (ins GR8:$rd, cg8imm:$imm),
744                   "cmp.b\t$imm, $rd",
745                   [(MSP430cmp GR8:$rd, cg8imm:$imm), (implicit SR)]>;
746def CMP16rc : I16rc<0b1001,
747                    (outs), (ins GR16:$rd, cg16imm:$imm),
748                    "cmp\t$imm, $rd",
749                    [(MSP430cmp GR16:$rd, cg16imm:$imm), (implicit SR)]>;
750
751def CMP8ri  : I8ri<0b1001,
752                   (outs), (ins GR8:$rd, i8imm:$imm),
753                   "cmp.b\t$imm, $rd",
754                   [(MSP430cmp GR8:$rd, imm:$imm), (implicit SR)]>;
755def CMP16ri : I16ri<0b1001,
756                    (outs), (ins GR16:$rd, i16imm:$imm),
757                    "cmp\t$imm, $rd",
758                    [(MSP430cmp GR16:$rd, imm:$imm), (implicit SR)]>;
759
760def CMP8mc  : I8mc<0b1001,
761                   (outs), (ins memsrc:$dst, cg8imm:$imm),
762                   "cmp.b\t$imm, $dst",
763                   [(MSP430cmp (load addr:$dst), (i8 cg8imm:$imm)),
764                    (implicit SR)]>;
765def CMP16mc : I16mc<0b1001,
766                    (outs), (ins memsrc:$dst, cg16imm:$imm),
767                    "cmp\t$imm, $dst",
768                    [(MSP430cmp (load addr:$dst), (i16 cg16imm:$imm)),
769                     (implicit SR)]>;
770
771def CMP8mi  : I8mi<0b1001,
772                   (outs), (ins memsrc:$dst, i8imm:$imm),
773                   "cmp.b\t$imm, $dst",
774                   [(MSP430cmp (load addr:$dst),
775                               (i8 imm:$imm)), (implicit SR)]>;
776def CMP16mi : I16mi<0b1001,
777                    (outs), (ins memsrc:$dst, i16imm:$imm),
778                    "cmp\t$imm, $dst",
779                     [(MSP430cmp (load addr:$dst),
780                                 (i16 imm:$imm)), (implicit SR)]>;
781
782def CMP8rm  : I8rm<0b1001,
783                   (outs), (ins GR8:$rd, memsrc:$src),
784                   "cmp.b\t$src, $rd",
785                   [(MSP430cmp GR8:$rd, (load addr:$src)),
786                    (implicit SR)]>;
787def CMP16rm : I16rm<0b1001,
788                    (outs), (ins GR16:$rd, memsrc:$src),
789                    "cmp\t$src, $rd",
790                    [(MSP430cmp GR16:$rd, (load addr:$src)),
791                     (implicit SR)]>;
792
793def CMP8rn  : I8rn<0b1001,
794                   (outs), (ins GR8:$rd, indreg:$rs), "cmp.b\t$rs, $rd", []>;
795def CMP16rn : I16rn<0b1001,
796                    (outs), (ins GR16:$rd, indreg:$rs), "cmp\t$rs, $rd", []>;
797
798def CMP8rp  : I8rp<0b1001,
799                   (outs), (ins GR8:$rd, postreg:$rs), "cmp.b\t$rs, $rd", []>;
800def CMP16rp : I16rp<0b1001,
801                    (outs), (ins GR16:$rd, postreg:$rs), "cmp\t$rs, $rd", []>;
802
803def CMP8mr  : I8mr<0b1001,
804                   (outs), (ins memsrc:$dst, GR8:$rs),
805                   "cmp.b\t$rs, $dst",
806                   [(MSP430cmp (load addr:$dst), GR8:$rs),
807                    (implicit SR)]>;
808def CMP16mr : I16mr<0b1001,
809                    (outs), (ins memsrc:$dst, GR16:$rs),
810                    "cmp\t$rs, $dst",
811                    [(MSP430cmp (load addr:$dst), GR16:$rs),
812                     (implicit SR)]>;
813def CMP8mm  : I8mm<0b1001,
814                   (outs), (ins memdst:$dst, memsrc:$src),
815                   "cmp.b\t$src, $dst",
816                   [(MSP430cmp (load addr:$dst), (i8 (load addr:$src))),
817                    (implicit SR)]>;
818def CMP16mm : I16mm<0b1001, (outs), (ins memdst:$dst, memsrc:$src),
819                    "cmp\t$src, $dst",
820                    [(MSP430cmp (load addr:$dst), (i16 (load addr:$src))),
821                     (implicit SR)]>;
822
823def CMP8mn  : I8mn<0b1001, (outs), (ins memsrc:$dst, indreg:$rs),
824                   "cmp.b\t$rs, $dst", []>;
825def CMP16mn : I16mn<0b1001, (outs), (ins memsrc:$dst, indreg:$rs),
826                    "cmp\t$rs, $dst", []>;
827
828def CMP8mp  : I8mp<0b1001, (outs), (ins memsrc:$dst, postreg:$rs),
829                   "cmp.b\t$rs, $dst", []>;
830def CMP16mp : I16mp<0b1001, (outs), (ins memsrc:$dst, postreg:$rs),
831                    "cmp\t$rs, $dst", []>;
832
833// BIT TESTS, just sets condition codes
834// Note that the C condition is set differently than when using CMP.
835let isCommutable = 1 in {
836def BIT8rr  : I8rr<0b1011,
837                   (outs), (ins GR8:$rd, GR8:$rs),
838                   "bit.b\t$rs, $rd",
839                   [(MSP430cmp (and_su GR8:$rd, GR8:$rs), 0),
840                    (implicit SR)]>;
841def BIT16rr : I16rr<0b1011,
842                    (outs), (ins GR16:$rd, GR16:$rs),
843                    "bit\t$rs, $rd",
844                    [(MSP430cmp (and_su GR16:$rd, GR16:$rs), 0),
845                     (implicit SR)]>;
846}
847def BIT8rc  : I8rc<0b1011,
848                   (outs), (ins GR8:$rd, cg8imm:$imm),
849                   "bit.b\t$imm, $rd",
850                   [(MSP430cmp (and_su GR8:$rd, cg8imm:$imm), 0),
851                    (implicit SR)]>;
852def BIT16rc : I16rc<0b1011,
853                    (outs), (ins GR16:$rd, cg16imm:$imm),
854                    "bit\t$imm, $rd",
855                    [(MSP430cmp (and_su GR16:$rd, cg16imm:$imm), 0),
856                     (implicit SR)]>;
857
858def BIT8ri  : I8ri<0b1011,
859                   (outs), (ins GR8:$rd, i8imm:$imm),
860                   "bit.b\t$imm, $rd",
861                   [(MSP430cmp (and_su GR8:$rd, imm:$imm), 0),
862                    (implicit SR)]>;
863def BIT16ri : I16ri<0b1011,
864                    (outs), (ins GR16:$rd, i16imm:$imm),
865                    "bit\t$imm, $rd",
866                    [(MSP430cmp (and_su GR16:$rd, imm:$imm), 0),
867                     (implicit SR)]>;
868
869def BIT8rm  : I8rm<0b1011,
870                   (outs), (ins GR8:$rd, memdst:$src),
871                   "bit.b\t$src, $rd",
872                   [(MSP430cmp (and_su GR8:$rd,  (load addr:$src)), 0),
873                    (implicit SR)]>;
874def BIT16rm : I16rm<0b1011,
875                    (outs), (ins GR16:$rd, memdst:$src),
876                    "bit\t$src, $rd",
877                    [(MSP430cmp (and_su GR16:$rd,  (load addr:$src)), 0),
878                     (implicit SR)]>;
879
880def BIT8rn  : I8rn<0b1011, (outs), (ins GR8:$rd, indreg:$rs),
881                   "bit.b\t$rs, $rd", []>;
882def BIT16rn : I16rn<0b1011, (outs), (ins GR16:$rd, indreg:$rs),
883                    "bit\t$rs, $rd", []>;
884
885def BIT8rp  : I8rp<0b1011, (outs), (ins GR8:$rd, postreg:$rs),
886                   "bit.b\t$rs, $rd", []>;
887def BIT16rp : I16rp<0b1011, (outs), (ins GR16:$rd, postreg:$rs),
888                    "bit\t$rs, $rd", []>;
889
890def BIT8mr  : I8mr<0b1011,
891                  (outs), (ins memsrc:$dst, GR8:$rs),
892                  "bit.b\t$rs, $dst",
893                  [(MSP430cmp (and_su (load addr:$dst), GR8:$rs), 0),
894                   (implicit SR)]>;
895def BIT16mr : I16mr<0b1011,
896                    (outs), (ins memsrc:$dst, GR16:$rs),
897                    "bit\t$rs, $dst",
898                    [(MSP430cmp (and_su (load addr:$dst), GR16:$rs), 0),
899                     (implicit SR)]>;
900
901def BIT8mc  : I8mc<0b1011,
902                   (outs), (ins memsrc:$dst, cg8imm:$imm),
903                   "bit.b\t$imm, $dst",
904                   [(MSP430cmp (and_su (load addr:$dst), (i8 cg8imm:$imm)), 0),
905                    (implicit SR)]>;
906def BIT16mc : I16mc<0b1011,
907                    (outs), (ins memdst:$dst, cg16imm:$imm),
908                    "bit\t$imm, $dst",
909                    [(MSP430cmp (and_su (load addr:$dst), (i16 cg16imm:$imm)), 0),
910                     (implicit SR)]>;
911
912def BIT8mi  : I8mi<0b1011,
913                   (outs), (ins memsrc:$dst, i8imm:$imm),
914                   "bit.b\t$imm, $dst",
915                   [(MSP430cmp (and_su (load addr:$dst), (i8 imm:$imm)), 0),
916                    (implicit SR)]>;
917def BIT16mi : I16mi<0b1011,
918                    (outs), (ins memsrc:$dst, i16imm:$imm),
919                    "bit\t$imm, $dst",
920                    [(MSP430cmp (and_su (load addr:$dst), (i16 imm:$imm)), 0),
921                     (implicit SR)]>;
922
923def BIT8mm  : I8mm<0b1011,
924                   (outs), (ins memsrc:$dst, memsrc:$src),
925                   "bit.b\t$src, $dst",
926                   [(MSP430cmp (and_su (i8 (load addr:$dst)),
927                                       (load addr:$src)),
928                                 0),
929                      (implicit SR)]>;
930def BIT16mm : I16mm<0b1011,
931                    (outs), (ins memsrc:$dst, memsrc:$src),
932                    "bit\t$src, $dst",
933                    [(MSP430cmp (and_su (i16 (load addr:$dst)),
934                                        (load addr:$src)),
935                                 0),
936                     (implicit SR)]>;
937def BIT8mn  : I8mn<0b1011, (outs), (ins memsrc:$dst, indreg:$rs),
938                   "bit.b\t$rs, $dst", []>;
939def BIT16mn : I16mn<0b1011, (outs), (ins memsrc:$dst, indreg:$rs),
940                    "bit\t$rs, $dst", []>;
941
942def BIT8mp  : I8mp<0b1011, (outs), (ins memsrc:$dst, postreg:$rs),
943                   "bit.b\t$rs, $dst", []>;
944def BIT16mp : I16mp<0b1011, (outs), (ins memsrc:$dst, postreg:$rs),
945                    "bit\t$rs, $dst", []>;
946
947} // Defs = [SR]
948
949def TST8r   : InstAlias<"tst.b\t$dst",  (CMP8rc    GR8:$dst,     0)>;
950def TST16r  : InstAlias<"tst\t$dst",    (CMP16rc   GR16:$dst,    0)>;
951def TST8m   : InstAlias<"tst.b\t$dst",  (CMP8mc    memdst:$dst,  0)>;
952def TST16m  : InstAlias<"tst\t$dst",    (CMP16mc   memdst:$dst,  0)>;
953
954//===----------------------------------------------------------------------===//
955// Non-Instruction Patterns
956
957// extload
958def : Pat<(extloadi16i8 addr:$src), (MOVZX16rm8 addr:$src)>;
959
960// anyext
961def : Pat<(i16 (anyext GR8:$src)),
962          (SUBREG_TO_REG (i16 0), GR8:$src, subreg_8bit)>;
963
964// truncs
965def : Pat<(i8 (trunc GR16:$src)),
966          (EXTRACT_SUBREG GR16:$src, subreg_8bit)>;
967
968// GlobalAddress, ExternalSymbol
969def : Pat<(i16 (MSP430Wrapper tglobaladdr:$dst)), (MOV16ri tglobaladdr:$dst)>;
970def : Pat<(i16 (MSP430Wrapper texternalsym:$dst)), (MOV16ri texternalsym:$dst)>;
971def : Pat<(i16 (MSP430Wrapper tblockaddress:$dst)), (MOV16ri tblockaddress:$dst)>;
972
973def : Pat<(add GR16:$src, (MSP430Wrapper tglobaladdr :$src2)),
974          (ADD16ri GR16:$src, tglobaladdr:$src2)>;
975def : Pat<(add GR16:$src, (MSP430Wrapper texternalsym:$src2)),
976          (ADD16ri GR16:$src, texternalsym:$src2)>;
977def : Pat<(add GR16:$src, (MSP430Wrapper tblockaddress:$src2)),
978          (ADD16ri GR16:$src, tblockaddress:$src2)>;
979
980def : Pat<(store (i16 (MSP430Wrapper tglobaladdr:$src)), addr:$dst),
981          (MOV16mi addr:$dst, tglobaladdr:$src)>;
982def : Pat<(store (i16 (MSP430Wrapper texternalsym:$src)), addr:$dst),
983          (MOV16mi addr:$dst, texternalsym:$src)>;
984def : Pat<(store (i16 (MSP430Wrapper tblockaddress:$src)), addr:$dst),
985          (MOV16mi addr:$dst, tblockaddress:$src)>;
986
987// calls
988def : Pat<(MSP430call (i16 tglobaladdr:$dst)),
989          (CALLi tglobaladdr:$dst)>;
990def : Pat<(MSP430call (i16 texternalsym:$dst)),
991          (CALLi texternalsym:$dst)>;
992
993// add and sub always produce carry
994def : Pat<(addc GR16:$src, GR16:$src2),
995          (ADD16rr GR16:$src, GR16:$src2)>;
996def : Pat<(addc GR16:$src, (load addr:$src2)),
997          (ADD16rm GR16:$src, addr:$src2)>;
998def : Pat<(addc GR16:$src, imm:$src2),
999          (ADD16ri GR16:$src, imm:$src2)>;
1000def : Pat<(store (addc (load addr:$dst), GR16:$src), addr:$dst),
1001          (ADD16mr addr:$dst, GR16:$src)>;
1002def : Pat<(store (addc (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
1003          (ADD16mm addr:$dst, addr:$src)>;
1004
1005def : Pat<(addc GR8:$src, GR8:$src2),
1006          (ADD8rr GR8:$src, GR8:$src2)>;
1007def : Pat<(addc GR8:$src, (load addr:$src2)),
1008          (ADD8rm GR8:$src, addr:$src2)>;
1009def : Pat<(addc GR8:$src, imm:$src2),
1010          (ADD8ri GR8:$src, imm:$src2)>;
1011def : Pat<(store (addc (load addr:$dst), GR8:$src), addr:$dst),
1012          (ADD8mr addr:$dst, GR8:$src)>;
1013def : Pat<(store (addc (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
1014          (ADD8mm addr:$dst, addr:$src)>;
1015
1016def : Pat<(subc GR16:$src, GR16:$src2),
1017          (SUB16rr GR16:$src, GR16:$src2)>;
1018def : Pat<(subc GR16:$src, (load addr:$src2)),
1019          (SUB16rm GR16:$src, addr:$src2)>;
1020def : Pat<(subc GR16:$src, imm:$src2),
1021          (SUB16ri GR16:$src, imm:$src2)>;
1022def : Pat<(store (subc (load addr:$dst), GR16:$src), addr:$dst),
1023          (SUB16mr addr:$dst, GR16:$src)>;
1024def : Pat<(store (subc (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
1025          (SUB16mm addr:$dst, addr:$src)>;
1026
1027def : Pat<(subc GR8:$src, GR8:$src2),
1028          (SUB8rr GR8:$src, GR8:$src2)>;
1029def : Pat<(subc GR8:$src, (load addr:$src2)),
1030          (SUB8rm GR8:$src, addr:$src2)>;
1031def : Pat<(subc GR8:$src, imm:$src2),
1032          (SUB8ri GR8:$src, imm:$src2)>;
1033def : Pat<(store (subc (load addr:$dst), GR8:$src), addr:$dst),
1034          (SUB8mr addr:$dst, GR8:$src)>;
1035def : Pat<(store (subc (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
1036          (SUB8mm addr:$dst, addr:$src)>;
1037
1038// peephole patterns
1039def : Pat<(and GR16:$src, 255), (ZEXT16r GR16:$src)>;
1040def : Pat<(MSP430cmp (trunc (and_su GR16:$src, GR16:$src2)), 0),
1041          (BIT8rr (EXTRACT_SUBREG GR16:$src, subreg_8bit),
1042                  (EXTRACT_SUBREG GR16:$src2, subreg_8bit))>;
1043