Add micro-optimization which allows us to fold imm into cmp. This allows us to save...
[oota-llvm.git] / lib / Target / MSP430 / MSP430InstrInfo.td
1 //===- MSP430InstrInfo.td - MSP430 Instruction defs -----------*- tblgen-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
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 describes the MSP430 instructions in TableGen format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 include "MSP430InstrFormats.td"
15
16 //===----------------------------------------------------------------------===//
17 // Type Constraints.
18 //===----------------------------------------------------------------------===//
19 class SDTCisI8<int OpNum> : SDTCisVT<OpNum, i8>;
20 class SDTCisI16<int OpNum> : SDTCisVT<OpNum, i16>;
21
22 //===----------------------------------------------------------------------===//
23 // Type Profiles.
24 //===----------------------------------------------------------------------===//
25 def SDT_MSP430Call         : SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
26 def SDT_MSP430CallSeqStart : SDCallSeqStart<[SDTCisVT<0, i16>]>;
27 def SDT_MSP430CallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
28 def SDT_MSP430Wrapper      : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, SDTCisPtrTy<0>]>;
29 def SDT_MSP430Cmp          : SDTypeProfile<0, 2, [SDTCisSameAs<0, 1>]>;
30 def SDT_MSP430BrCC         : SDTypeProfile<0, 2, [SDTCisVT<0, OtherVT>,
31                                                   SDTCisVT<1, i8>]>;
32 def SDT_MSP430SelectCC     : SDTypeProfile<1, 3, [SDTCisSameAs<0, 1>, SDTCisSameAs<1, 2>, 
33                                                   SDTCisVT<3, i8>]>;
34 def SDT_MSP430Shift        : SDTypeProfile<1, 2, [SDTCisSameAs<0, 1>, SDTCisI8<2>]>;
35
36 //===----------------------------------------------------------------------===//
37 // MSP430 Specific Node Definitions.
38 //===----------------------------------------------------------------------===//
39 def MSP430retflag  : SDNode<"MSP430ISD::RET_FLAG", SDTNone,
40                        [SDNPHasChain, SDNPOptInFlag]>;
41 def MSP430retiflag : SDNode<"MSP430ISD::RETI_FLAG", SDTNone,
42                        [SDNPHasChain, SDNPOptInFlag]>;
43
44 def MSP430rra     : SDNode<"MSP430ISD::RRA", SDTIntUnaryOp, []>;
45 def MSP430rla     : SDNode<"MSP430ISD::RLA", SDTIntUnaryOp, []>;
46 def MSP430rrc     : SDNode<"MSP430ISD::RRC", SDTIntUnaryOp, []>;
47
48 def MSP430call    : SDNode<"MSP430ISD::CALL", SDT_MSP430Call,
49                      [SDNPHasChain, SDNPOutFlag, SDNPOptInFlag]>;
50 def MSP430callseq_start :
51                  SDNode<"ISD::CALLSEQ_START", SDT_MSP430CallSeqStart,
52                         [SDNPHasChain, SDNPOutFlag]>;
53 def MSP430callseq_end :
54                  SDNode<"ISD::CALLSEQ_END",   SDT_MSP430CallSeqEnd,
55                         [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
56 def MSP430Wrapper : SDNode<"MSP430ISD::Wrapper", SDT_MSP430Wrapper>;
57 def MSP430cmp     : SDNode<"MSP430ISD::CMP", SDT_MSP430Cmp, [SDNPOutFlag]>;
58 def MSP430brcc    : SDNode<"MSP430ISD::BR_CC", SDT_MSP430BrCC, [SDNPHasChain, SDNPInFlag]>;
59 def MSP430selectcc: SDNode<"MSP430ISD::SELECT_CC", SDT_MSP430SelectCC, [SDNPInFlag]>;
60 def MSP430shl     : SDNode<"MSP430ISD::SHL", SDT_MSP430Shift, []>;
61 def MSP430sra     : SDNode<"MSP430ISD::SRA", SDT_MSP430Shift, []>;
62 def MSP430srl     : SDNode<"MSP430ISD::SRL", SDT_MSP430Shift, []>;
63
64 //===----------------------------------------------------------------------===//
65 // MSP430 Operand Definitions.
66 //===----------------------------------------------------------------------===//
67
68 // Address operands
69 def memsrc : Operand<i16> {
70   let PrintMethod = "printSrcMemOperand";
71   let MIOperandInfo = (ops GR16, i16imm);
72 }
73
74 def memdst : Operand<i16> {
75   let PrintMethod = "printSrcMemOperand";
76   let MIOperandInfo = (ops GR16, i16imm);
77 }
78
79 // Branch targets have OtherVT type.
80 def brtarget : Operand<OtherVT> {
81   let PrintMethod = "printPCRelImmOperand";
82 }
83
84 // Operand for printing out a condition code.
85 def cc : Operand<i8> {
86   let PrintMethod = "printCCOperand";
87 }
88
89 //===----------------------------------------------------------------------===//
90 // MSP430 Complex Pattern Definitions.
91 //===----------------------------------------------------------------------===//
92
93 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [], []>;
94
95 //===----------------------------------------------------------------------===//
96 // Pattern Fragments
97 def zextloadi16i8 : PatFrag<(ops node:$ptr), (i16 (zextloadi8 node:$ptr))>;
98 def  extloadi16i8 : PatFrag<(ops node:$ptr), (i16 ( extloadi8 node:$ptr))>;
99 def and_su : PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, node:$rhs), [{
100   return N->hasOneUse();
101 }]>;
102 //===----------------------------------------------------------------------===//
103 // Instruction list..
104
105 // ADJCALLSTACKDOWN/UP implicitly use/def SP because they may be expanded into
106 // a stack adjustment and the codegen must know that they may modify the stack
107 // pointer before prolog-epilog rewriting occurs.
108 // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
109 // sub / add which can clobber SRW.
110 let Defs = [SPW, SRW], Uses = [SPW] in {
111 def ADJCALLSTACKDOWN : Pseudo<(outs), (ins i16imm:$amt),
112                               "#ADJCALLSTACKDOWN",
113                               [(MSP430callseq_start timm:$amt)]>;
114 def ADJCALLSTACKUP   : Pseudo<(outs), (ins i16imm:$amt1, i16imm:$amt2),
115                               "#ADJCALLSTACKUP",
116                               [(MSP430callseq_end timm:$amt1, timm:$amt2)]>;
117 }
118
119 let usesCustomInserter = 1 in {
120   def Select8  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2, i8imm:$cc),
121                         "# Select8 PSEUDO",
122                         [(set GR8:$dst,
123                           (MSP430selectcc GR8:$src1, GR8:$src2, imm:$cc))]>;
124   def Select16 : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2, i8imm:$cc),
125                         "# Select16 PSEUDO",
126                         [(set GR16:$dst,
127                           (MSP430selectcc GR16:$src1, GR16:$src2, imm:$cc))]>;
128   let Defs = [SRW] in {
129   def Shl8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
130                         "# Shl8 PSEUDO",
131                         [(set GR8:$dst, (MSP430shl GR8:$src, GR8:$cnt))]>;
132   def Shl16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
133                         "# Shl16 PSEUDO",
134                         [(set GR16:$dst, (MSP430shl GR16:$src, GR8:$cnt))]>;
135   def Sra8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
136                         "# Sra8 PSEUDO",
137                         [(set GR8:$dst, (MSP430sra GR8:$src, GR8:$cnt))]>;
138   def Sra16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
139                         "# Sra16 PSEUDO",
140                         [(set GR16:$dst, (MSP430sra GR16:$src, GR8:$cnt))]>;
141   def Srl8     : Pseudo<(outs GR8:$dst), (ins GR8:$src, GR8:$cnt),
142                         "# Srl8 PSEUDO",
143                         [(set GR8:$dst, (MSP430srl GR8:$src, GR8:$cnt))]>;
144   def Srl16    : Pseudo<(outs GR16:$dst), (ins GR16:$src, GR8:$cnt),
145                         "# Srl16 PSEUDO",
146                         [(set GR16:$dst, (MSP430srl GR16:$src, GR8:$cnt))]>;
147
148   }
149 }
150
151 let neverHasSideEffects = 1 in
152 def NOP : Pseudo<(outs), (ins), "nop", []>;
153
154 //===----------------------------------------------------------------------===//
155 //  Control Flow Instructions...
156 //
157
158 // FIXME: Provide proper encoding!
159 let isReturn = 1, isTerminator = 1, isBarrier = 1 in {
160   def RET  : Pseudo<(outs), (ins), "ret",  [(MSP430retflag)]>;
161   def RETI : Pseudo<(outs), (ins), "reti", [(MSP430retiflag)]>;
162 }
163
164 let isBranch = 1, isTerminator = 1 in {
165
166 // Direct branch
167 let isBarrier = 1 in
168   def JMP : Pseudo<(outs), (ins brtarget:$dst),
169                    "jmp\t$dst",
170                    [(br bb:$dst)]>;
171
172 // Conditional branches
173 let Uses = [SRW] in
174   def JCC : Pseudo<(outs), (ins brtarget:$dst, cc:$cc),
175                             "j$cc\t$dst",
176                             [(MSP430brcc bb:$dst, imm:$cc)]>;
177 } // isBranch, isTerminator
178
179 //===----------------------------------------------------------------------===//
180 //  Call Instructions...
181 //
182 let isCall = 1 in
183   // All calls clobber the non-callee saved registers. SPW is marked as
184   // a use to prevent stack-pointer assignments that appear immediately
185   // before calls from potentially appearing dead. Uses for argument
186   // registers are added manually.
187   let Defs = [R12W, R13W, R14W, R15W, SRW],
188       Uses = [SPW] in {
189     def CALLi     : Pseudo<(outs), (ins i16imm:$dst, variable_ops),
190                            "call\t$dst", [(MSP430call imm:$dst)]>;
191     def CALLr     : Pseudo<(outs), (ins GR16:$dst, variable_ops),
192                            "call\t$dst", [(MSP430call GR16:$dst)]>;
193     def CALLm     : Pseudo<(outs), (ins memsrc:$dst, variable_ops),
194                            "call\t${dst:mem}", [(MSP430call (load addr:$dst))]>;
195   }
196
197
198 //===----------------------------------------------------------------------===//
199 //  Miscellaneous Instructions...
200 //
201 let Defs = [SPW], Uses = [SPW], neverHasSideEffects=1 in {
202 let mayLoad = 1 in
203 def POP16r   : Pseudo<(outs GR16:$reg), (ins), "pop.w\t$reg", []>;
204
205 let mayStore = 1 in
206 def PUSH16r  : Pseudo<(outs), (ins GR16:$reg), "push.w\t$reg",[]>;
207 }
208
209 //===----------------------------------------------------------------------===//
210 // Move Instructions
211
212 // FIXME: Provide proper encoding!
213 let neverHasSideEffects = 1 in {
214 def MOV8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src),
215                      "mov.b\t{$src, $dst}",
216                      []>;
217 def MOV16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src),
218                      "mov.w\t{$src, $dst}",
219                      []>;
220 }
221
222 // FIXME: Provide proper encoding!
223 let isReMaterializable = 1, isAsCheapAsAMove = 1 in {
224 def MOV8ri  : Pseudo<(outs GR8:$dst), (ins i8imm:$src),
225                      "mov.b\t{$src, $dst}",
226                      [(set GR8:$dst, imm:$src)]>;
227 def MOV16ri : Pseudo<(outs GR16:$dst), (ins i16imm:$src),
228                      "mov.w\t{$src, $dst}",
229                      [(set GR16:$dst, imm:$src)]>;
230 }
231
232 let canFoldAsLoad = 1, isReMaterializable = 1, mayHaveSideEffects = 1 in {
233 def MOV8rm  : Pseudo<(outs GR8:$dst), (ins memsrc:$src),
234                 "mov.b\t{$src, $dst}",
235                 [(set GR8:$dst, (load addr:$src))]>;
236 def MOV16rm : Pseudo<(outs GR16:$dst), (ins memsrc:$src),
237                 "mov.w\t{$src, $dst}",
238                 [(set GR16:$dst, (load addr:$src))]>;
239 }
240
241 def MOVZX16rr8 : Pseudo<(outs GR16:$dst), (ins GR8:$src),
242                 "mov.b\t{$src, $dst}",
243                 [(set GR16:$dst, (zext GR8:$src))]>;
244 def MOVZX16rm8 : Pseudo<(outs GR16:$dst), (ins memsrc:$src),
245                 "mov.b\t{$src, $dst}",
246                 [(set GR16:$dst, (zextloadi16i8 addr:$src))]>;
247
248 let mayLoad = 1, hasExtraDefRegAllocReq = 1, Constraints = "$base = $base_wb" in {
249 def MOV8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR16:$base),
250                           "mov.b\t{@$base+, $dst}", []>;
251 def MOV16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$base),
252                           "mov.w\t{@$base+, $dst}", []>;
253 }
254
255 // Any instruction that defines a 8-bit result leaves the high half of the
256 // register. Truncate can be lowered to EXTRACT_SUBREG, and CopyFromReg may
257 // be copying from a truncate, but any other 8-bit operation will zero-extend
258 // up to 16 bits.
259 def def8 : PatLeaf<(i8 GR8:$src), [{
260   return N->getOpcode() != ISD::TRUNCATE &&
261          N->getOpcode() != TargetInstrInfo::EXTRACT_SUBREG &&
262          N->getOpcode() != ISD::CopyFromReg;
263 }]>;
264
265 // In the case of a 8-bit def that is known to implicitly zero-extend,
266 // we can use a SUBREG_TO_REG.
267 def : Pat<(i16 (zext def8:$src)),
268           (SUBREG_TO_REG (i16 0), GR8:$src, subreg_8bit)>;
269
270
271 def MOV8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
272                 "mov.b\t{$src, $dst}",
273                 [(store (i8 imm:$src), addr:$dst)]>;
274 def MOV16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
275                 "mov.w\t{$src, $dst}",
276                 [(store (i16 imm:$src), addr:$dst)]>;
277
278 def MOV8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
279                 "mov.b\t{$src, $dst}",
280                 [(store GR8:$src, addr:$dst)]>;
281 def MOV16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
282                 "mov.w\t{$src, $dst}",
283                 [(store GR16:$src, addr:$dst)]>;
284
285 def MOV8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
286                 "mov.b\t{$src, $dst}",
287                 [(store (i8 (load addr:$src)), addr:$dst)]>;
288 def MOV16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
289                 "mov.w\t{$src, $dst}",
290                 [(store (i16 (load addr:$src)), addr:$dst)]>;
291
292 //===----------------------------------------------------------------------===//
293 // Arithmetic Instructions
294
295 let isTwoAddress = 1 in {
296
297 let Defs = [SRW] in {
298
299 let isCommutable = 1 in { // X = ADD Y, Z  == X = ADD Z, Y
300 // FIXME: Provide proper encoding!
301 def ADD8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
302                      "add.b\t{$src2, $dst}",
303                      [(set GR8:$dst, (add GR8:$src1, GR8:$src2)),
304                       (implicit SRW)]>;
305 def ADD16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
306                      "add.w\t{$src2, $dst}",
307                      [(set GR16:$dst, (add GR16:$src1, GR16:$src2)),
308                       (implicit SRW)]>;
309 }
310
311 def ADD8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
312                      "add.b\t{$src2, $dst}",
313                      [(set GR8:$dst, (add GR8:$src1, (load addr:$src2))),
314                       (implicit SRW)]>;
315 def ADD16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
316                      "add.w\t{$src2, $dst}",
317                      [(set GR16:$dst, (add GR16:$src1, (load addr:$src2))),
318                       (implicit SRW)]>;
319
320 let mayLoad = 1, hasExtraDefRegAllocReq = 1, 
321 Constraints = "$base = $base_wb, $src1 = $dst" in {
322 def ADD8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR8:$src1, GR16:$base),
323                           "add.b\t{@$base+, $dst}", []>;
324 def ADD16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$src1, GR16:$base),
325                           "add.w\t{@$base+, $dst}", []>;
326 }
327
328
329 def ADD8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
330                      "add.b\t{$src2, $dst}",
331                      [(set GR8:$dst, (add GR8:$src1, imm:$src2)),
332                       (implicit SRW)]>;
333 def ADD16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
334                      "add.w\t{$src2, $dst}",
335                      [(set GR16:$dst, (add GR16:$src1, imm:$src2)),
336                       (implicit SRW)]>;
337
338 let isTwoAddress = 0 in {
339 def ADD8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
340                 "add.b\t{$src, $dst}",
341                 [(store (add (load addr:$dst), GR8:$src), addr:$dst),
342                  (implicit SRW)]>;
343 def ADD16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
344                 "add.w\t{$src, $dst}",
345                 [(store (add (load addr:$dst), GR16:$src), addr:$dst),
346                  (implicit SRW)]>;
347
348 def ADD8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
349                 "add.b\t{$src, $dst}",
350                 [(store (add (load addr:$dst), (i8 imm:$src)), addr:$dst),
351                  (implicit SRW)]>;
352 def ADD16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
353                 "add.w\t{$src, $dst}",
354                 [(store (add (load addr:$dst), (i16 imm:$src)), addr:$dst),
355                  (implicit SRW)]>;
356
357 def ADD8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
358                 "add.b\t{$src, $dst}",
359                 [(store (add (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
360                  (implicit SRW)]>;
361 def ADD16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
362                 "add.w\t{$src, $dst}",
363                 [(store (add (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
364                  (implicit SRW)]>;
365 }
366
367 let Uses = [SRW] in {
368
369 let isCommutable = 1 in { // X = ADDC Y, Z  == X = ADDC Z, Y
370 def ADC8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
371                      "addc.b\t{$src2, $dst}",
372                      [(set GR8:$dst, (adde GR8:$src1, GR8:$src2)),
373                       (implicit SRW)]>;
374 def ADC16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
375                      "addc.w\t{$src2, $dst}",
376                      [(set GR16:$dst, (adde GR16:$src1, GR16:$src2)),
377                       (implicit SRW)]>;
378 } // isCommutable
379
380 def ADC8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
381                      "addc.b\t{$src2, $dst}",
382                      [(set GR8:$dst, (adde GR8:$src1, imm:$src2)),
383                       (implicit SRW)]>;
384 def ADC16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
385                      "addc.w\t{$src2, $dst}",
386                      [(set GR16:$dst, (adde GR16:$src1, imm:$src2)),
387                       (implicit SRW)]>;
388
389 def ADC8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
390                      "addc.b\t{$src2, $dst}",
391                      [(set GR8:$dst, (adde GR8:$src1, (load addr:$src2))),
392                       (implicit SRW)]>;
393 def ADC16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
394                      "addc.w\t{$src2, $dst}",
395                      [(set GR16:$dst, (adde GR16:$src1, (load addr:$src2))),
396                       (implicit SRW)]>;
397
398 let isTwoAddress = 0 in {
399 def ADC8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
400                 "addc.b\t{$src, $dst}",
401                 [(store (adde (load addr:$dst), GR8:$src), addr:$dst),
402                  (implicit SRW)]>;
403 def ADC16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
404                 "addc.w\t{$src, $dst}",
405                 [(store (adde (load addr:$dst), GR16:$src), addr:$dst),
406                  (implicit SRW)]>;
407
408 def ADC8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
409                 "addc.b\t{$src, $dst}",
410                 [(store (adde (load addr:$dst), (i8 imm:$src)), addr:$dst),
411                  (implicit SRW)]>;
412 def ADC16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
413                 "addc.w\t{$src, $dst}",
414                 [(store (adde (load addr:$dst), (i16 imm:$src)), addr:$dst),
415                  (implicit SRW)]>;
416
417 def ADC8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
418                 "addc.b\t{$src, $dst}",
419                 [(store (adde (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
420                  (implicit SRW)]>;
421 def ADC16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
422                 "addc.w\t{$src, $dst}",
423                 [(store (adde (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
424                  (implicit SRW)]>;
425 }
426
427 } // Uses = [SRW]
428
429 let isCommutable = 1 in { // X = AND Y, Z  == X = AND Z, Y
430 def AND8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
431                      "and.b\t{$src2, $dst}",
432                      [(set GR8:$dst, (and GR8:$src1, GR8:$src2)),
433                       (implicit SRW)]>;
434 def AND16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
435                      "and.w\t{$src2, $dst}",
436                      [(set GR16:$dst, (and GR16:$src1, GR16:$src2)),
437                       (implicit SRW)]>;
438 }
439
440 def AND8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
441                      "and.b\t{$src2, $dst}",
442                      [(set GR8:$dst, (and GR8:$src1, imm:$src2)),
443                       (implicit SRW)]>;
444 def AND16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
445                      "and.w\t{$src2, $dst}",
446                      [(set GR16:$dst, (and GR16:$src1, imm:$src2)),
447                       (implicit SRW)]>;
448
449 def AND8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
450                      "and.b\t{$src2, $dst}",
451                      [(set GR8:$dst, (and GR8:$src1, (load addr:$src2))),
452                       (implicit SRW)]>;
453 def AND16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
454                      "and.w\t{$src2, $dst}",
455                      [(set GR16:$dst, (and GR16:$src1, (load addr:$src2))),
456                       (implicit SRW)]>;
457
458 let mayLoad = 1, hasExtraDefRegAllocReq = 1, 
459 Constraints = "$base = $base_wb, $src1 = $dst" in {
460 def AND8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR8:$src1, GR16:$base),
461                           "and.b\t{@$base+, $dst}", []>;
462 def AND16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$src1, GR16:$base),
463                           "and.w\t{@$base+, $dst}", []>;
464 }
465
466 let isTwoAddress = 0 in {
467 def AND8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
468                 "and.b\t{$src, $dst}",
469                 [(store (and (load addr:$dst), GR8:$src), addr:$dst),
470                  (implicit SRW)]>;
471 def AND16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
472                 "and.w\t{$src, $dst}",
473                 [(store (and (load addr:$dst), GR16:$src), addr:$dst),
474                  (implicit SRW)]>;
475
476 def AND8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
477                 "and.b\t{$src, $dst}",
478                 [(store (and (load addr:$dst), (i8 imm:$src)), addr:$dst),
479                  (implicit SRW)]>;
480 def AND16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
481                 "and.w\t{$src, $dst}",
482                 [(store (and (load addr:$dst), (i16 imm:$src)), addr:$dst),
483                  (implicit SRW)]>;
484
485 def AND8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
486                 "and.b\t{$src, $dst}",
487                 [(store (and (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
488                  (implicit SRW)]>;
489 def AND16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
490                 "and.w\t{$src, $dst}",
491                 [(store (and (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
492                  (implicit SRW)]>;
493 }
494
495 let isCommutable = 1 in { // X = OR Y, Z  == X = OR Z, Y
496 def OR8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
497                     "bis.b\t{$src2, $dst}",
498                     [(set GR8:$dst, (or GR8:$src1, GR8:$src2))]>;
499 def OR16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
500                     "bis.w\t{$src2, $dst}",
501                     [(set GR16:$dst, (or GR16:$src1, GR16:$src2))]>;
502 }
503
504 def OR8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
505                     "bis.b\t{$src2, $dst}",
506                     [(set GR8:$dst, (or GR8:$src1, imm:$src2))]>;
507 def OR16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
508                     "bis.w\t{$src2, $dst}",
509                     [(set GR16:$dst, (or GR16:$src1, imm:$src2))]>;
510
511 def OR8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
512                     "bis.b\t{$src2, $dst}",
513                     [(set GR8:$dst, (or GR8:$src1, (load addr:$src2)))]>;
514 def OR16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
515                     "bis.w\t{$src2, $dst}",
516                     [(set GR16:$dst, (or GR16:$src1, (load addr:$src2)))]>;
517
518 let mayLoad = 1, hasExtraDefRegAllocReq = 1, 
519 Constraints = "$base = $base_wb, $src1 = $dst" in {
520 def OR8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR8:$src1, GR16:$base),
521                         "bis.b\t{@$base+, $dst}", []>;
522 def OR16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$src1, GR16:$base),
523                          "bis.w\t{@$base+, $dst}", []>;
524 }
525
526 let isTwoAddress = 0 in {
527 def OR8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
528                 "bis.b\t{$src, $dst}",
529                 [(store (or (load addr:$dst), GR8:$src), addr:$dst)]>;
530 def OR16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
531                 "bis.w\t{$src, $dst}",
532                 [(store (or (load addr:$dst), GR16:$src), addr:$dst)]>;
533
534 def OR8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
535                 "bis.b\t{$src, $dst}",
536                 [(store (or (load addr:$dst), (i8 imm:$src)), addr:$dst)]>;
537 def OR16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
538                 "bis.w\t{$src, $dst}",
539                 [(store (or (load addr:$dst), (i16 imm:$src)), addr:$dst)]>;
540
541 def OR8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
542                 "bis.b\t{$src, $dst}",
543                 [(store (or (i8 (load addr:$dst)),
544                             (i8 (load addr:$src))), addr:$dst)]>;
545 def OR16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
546                 "bis.w\t{$src, $dst}",
547                  [(store (or (i16 (load addr:$dst)),
548                              (i16 (load addr:$src))), addr:$dst)]>;
549 }
550
551 // bic does not modify condition codes
552 def BIC8rr :  Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
553                 "bic.b\t{$src2, $dst}",
554                 [(set GR8:$dst, (and GR8:$src1, (not GR8:$src2)))]>;
555 def BIC16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
556                 "bic.w\t{$src2, $dst}",
557                 [(set GR16:$dst, (and GR16:$src1, (not GR16:$src2)))]>;
558
559 def BIC8rm :  Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
560                 "bic.b\t{$src2, $dst}",
561                 [(set GR8:$dst, (and GR8:$src1, (not (i8 (load addr:$src2)))))]>;
562 def BIC16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
563                 "bic.w\t{$src2, $dst}",
564                 [(set GR16:$dst, (and GR16:$src1, (not (i16 (load addr:$src2)))))]>;
565
566 let isTwoAddress = 0 in {
567 def BIC8mr :  Pseudo<(outs), (ins memdst:$dst, GR8:$src),
568                 "bic.b\t{$src, $dst}",
569                 [(store (and (load addr:$dst), (not GR8:$src)), addr:$dst)]>;
570 def BIC16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
571                 "bic.w\t{$src, $dst}",
572                 [(store (and (load addr:$dst), (not GR16:$src)), addr:$dst)]>;
573
574 def BIC8mm :  Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
575                 "bic.b\t{$src, $dst}",
576                 [(store (and (load addr:$dst), (not (i8 (load addr:$src)))), addr:$dst)]>;
577 def BIC16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
578                 "bic.w\t{$src, $dst}",
579                 [(store (and (load addr:$dst), (not (i16 (load addr:$src)))), addr:$dst)]>;
580 }
581
582 let isCommutable = 1 in { // X = XOR Y, Z  == X = XOR Z, Y
583 def XOR8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
584                      "xor.b\t{$src2, $dst}",
585                      [(set GR8:$dst, (xor GR8:$src1, GR8:$src2)),
586                       (implicit SRW)]>;
587 def XOR16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
588                      "xor.w\t{$src2, $dst}",
589                      [(set GR16:$dst, (xor GR16:$src1, GR16:$src2)),
590                       (implicit SRW)]>;
591 }
592
593 def XOR8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
594                      "xor.b\t{$src2, $dst}",
595                      [(set GR8:$dst, (xor GR8:$src1, imm:$src2)),
596                       (implicit SRW)]>;
597 def XOR16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
598                      "xor.w\t{$src2, $dst}",
599                      [(set GR16:$dst, (xor GR16:$src1, imm:$src2)),
600                       (implicit SRW)]>;
601
602 def XOR8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
603                      "xor.b\t{$src2, $dst}",
604                      [(set GR8:$dst, (xor GR8:$src1, (load addr:$src2))),
605                       (implicit SRW)]>;
606 def XOR16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
607                      "xor.w\t{$src2, $dst}",
608                      [(set GR16:$dst, (xor GR16:$src1, (load addr:$src2))),
609                       (implicit SRW)]>;
610
611 let mayLoad = 1, hasExtraDefRegAllocReq = 1, 
612 Constraints = "$base = $base_wb, $src1 = $dst" in {
613 def XOR8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR8:$src1, GR16:$base),
614                           "xor.b\t{@$base+, $dst}", []>;
615 def XOR16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$src1, GR16:$base),
616                           "xor.w\t{@$base+, $dst}", []>;
617 }
618
619 let isTwoAddress = 0 in {
620 def XOR8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
621                 "xor.b\t{$src, $dst}",
622                 [(store (xor (load addr:$dst), GR8:$src), addr:$dst),
623                  (implicit SRW)]>;
624 def XOR16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
625                 "xor.w\t{$src, $dst}",
626                 [(store (xor (load addr:$dst), GR16:$src), addr:$dst),
627                  (implicit SRW)]>;
628
629 def XOR8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
630                 "xor.b\t{$src, $dst}",
631                 [(store (xor (load addr:$dst), (i8 imm:$src)), addr:$dst),
632                  (implicit SRW)]>;
633 def XOR16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
634                 "xor.w\t{$src, $dst}",
635                 [(store (xor (load addr:$dst), (i16 imm:$src)), addr:$dst),
636                  (implicit SRW)]>;
637
638 def XOR8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
639                 "xor.b\t{$src, $dst}",
640                 [(store (xor (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
641                  (implicit SRW)]>;
642 def XOR16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
643                 "xor.w\t{$src, $dst}",
644                 [(store (xor (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
645                  (implicit SRW)]>;
646 }
647
648
649 def SUB8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
650                      "sub.b\t{$src2, $dst}",
651                      [(set GR8:$dst, (sub GR8:$src1, GR8:$src2)),
652                       (implicit SRW)]>;
653 def SUB16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
654                      "sub.w\t{$src2, $dst}",
655                      [(set GR16:$dst, (sub GR16:$src1, GR16:$src2)),
656                       (implicit SRW)]>;
657
658 def SUB8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
659                      "sub.b\t{$src2, $dst}",
660                      [(set GR8:$dst, (sub GR8:$src1, imm:$src2)),
661                       (implicit SRW)]>;
662 def SUB16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
663                      "sub.w\t{$src2, $dst}",
664                      [(set GR16:$dst, (sub GR16:$src1, imm:$src2)),
665                       (implicit SRW)]>;
666
667 def SUB8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
668                      "sub.b\t{$src2, $dst}",
669                      [(set GR8:$dst, (sub GR8:$src1, (load addr:$src2))),
670                       (implicit SRW)]>;
671 def SUB16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
672                      "sub.w\t{$src2, $dst}",
673                      [(set GR16:$dst, (sub GR16:$src1, (load addr:$src2))),
674                       (implicit SRW)]>;
675
676 let mayLoad = 1, hasExtraDefRegAllocReq = 1, 
677 Constraints = "$base = $base_wb, $src1 = $dst" in {
678 def SUB8rm_POST : Pseudo<(outs GR8:$dst, GR16:$base_wb), (ins GR8:$src1, GR16:$base),
679                           "sub.b\t{@$base+, $dst}", []>;
680 def SUB16rm_POST : Pseudo<(outs GR16:$dst, GR16:$base_wb), (ins GR16:$src1, GR16:$base),
681                           "sub.w\t{@$base+, $dst}", []>;
682 }
683
684 let isTwoAddress = 0 in {
685 def SUB8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
686                 "sub.b\t{$src, $dst}",
687                 [(store (sub (load addr:$dst), GR8:$src), addr:$dst),
688                  (implicit SRW)]>;
689 def SUB16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
690                 "sub.w\t{$src, $dst}",
691                 [(store (sub (load addr:$dst), GR16:$src), addr:$dst),
692                  (implicit SRW)]>;
693
694 def SUB8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
695                 "sub.b\t{$src, $dst}",
696                 [(store (sub (load addr:$dst), (i8 imm:$src)), addr:$dst),
697                  (implicit SRW)]>;
698 def SUB16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
699                 "sub.w\t{$src, $dst}",
700                 [(store (sub (load addr:$dst), (i16 imm:$src)), addr:$dst),
701                  (implicit SRW)]>;
702
703 def SUB8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
704                 "sub.b\t{$src, $dst}",
705                 [(store (sub (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
706                  (implicit SRW)]>;
707 def SUB16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
708                 "sub.w\t{$src, $dst}",
709                 [(store (sub (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
710                  (implicit SRW)]>;
711 }
712
713 let Uses = [SRW] in {
714 def SBC8rr  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
715                      "subc.b\t{$src2, $dst}",
716                      [(set GR8:$dst, (sube GR8:$src1, GR8:$src2)),
717                       (implicit SRW)]>;
718 def SBC16rr : Pseudo<(outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
719                      "subc.w\t{$src2, $dst}",
720                      [(set GR16:$dst, (sube GR16:$src1, GR16:$src2)),
721                       (implicit SRW)]>;
722
723 def SBC8ri  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
724                      "subc.b\t{$src2, $dst}",
725                      [(set GR8:$dst, (sube GR8:$src1, imm:$src2)),
726                       (implicit SRW)]>;
727 def SBC16ri : Pseudo<(outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
728                      "subc.w\t{$src2, $dst}",
729                      [(set GR16:$dst, (sube GR16:$src1, imm:$src2)),
730                       (implicit SRW)]>;
731
732 def SBC8rm  : Pseudo<(outs GR8:$dst), (ins GR8:$src1, memsrc:$src2),
733                      "subc.b\t{$src2, $dst}",
734                      [(set GR8:$dst, (sube GR8:$src1, (load addr:$src2))),
735                       (implicit SRW)]>;
736 def SBC16rm : Pseudo<(outs GR16:$dst), (ins GR16:$src1, memsrc:$src2),
737                      "subc.w\t{$src2, $dst}",
738                      [(set GR16:$dst, (sube GR16:$src1, (load addr:$src2))),
739                       (implicit SRW)]>;
740
741 let isTwoAddress = 0 in {
742 def SBC8mr  : Pseudo<(outs), (ins memdst:$dst, GR8:$src),
743                 "subc.b\t{$src, $dst}",
744                 [(store (sube (load addr:$dst), GR8:$src), addr:$dst),
745                  (implicit SRW)]>;
746 def SBC16mr : Pseudo<(outs), (ins memdst:$dst, GR16:$src),
747                 "subc.w\t{$src, $dst}",
748                 [(store (sube (load addr:$dst), GR16:$src), addr:$dst),
749                  (implicit SRW)]>;
750
751 def SBC8mi  : Pseudo<(outs), (ins memdst:$dst, i8imm:$src),
752                 "subc.b\t{$src, $dst}",
753                 [(store (sube (load addr:$dst), (i8 imm:$src)), addr:$dst),
754                  (implicit SRW)]>;
755 def SBC16mi : Pseudo<(outs), (ins memdst:$dst, i16imm:$src),
756                 "subc.w\t{$src, $dst}",
757                 [(store (sube (load addr:$dst), (i16 imm:$src)), addr:$dst),
758                  (implicit SRW)]>;
759
760 def SBC8mm  : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
761                 "subc.b\t{$src, $dst}",
762                 [(store (sube (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
763                  (implicit SRW)]>;
764 def SBC16mm : Pseudo<(outs), (ins memdst:$dst, memsrc:$src),
765                 "subc.w\t{$src, $dst}",
766                 [(store (sube (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
767                  (implicit SRW)]>;
768 }
769
770 } // Uses = [SRW]
771
772 // FIXME: Provide proper encoding!
773 def SAR8r1  : Pseudo<(outs GR8:$dst), (ins GR8:$src),
774                      "rra.b\t$dst",
775                      [(set GR8:$dst, (MSP430rra GR8:$src)),
776                       (implicit SRW)]>;
777 def SAR16r1 : Pseudo<(outs GR16:$dst), (ins GR16:$src),
778                      "rra.w\t$dst",
779                      [(set GR16:$dst, (MSP430rra GR16:$src)),
780                       (implicit SRW)]>;
781
782 def SHL8r1  : Pseudo<(outs GR8:$dst), (ins GR8:$src),
783                      "rla.b\t$dst",
784                      [(set GR8:$dst, (MSP430rla GR8:$src)),
785                       (implicit SRW)]>;
786 def SHL16r1 : Pseudo<(outs GR16:$dst), (ins GR16:$src),
787                      "rla.w\t$dst",
788                      [(set GR16:$dst, (MSP430rla GR16:$src)),
789                       (implicit SRW)]>;
790
791 def SAR8r1c  : Pseudo<(outs GR8:$dst), (ins GR8:$src),
792                       "clrc\n\t"
793                       "rrc.b\t$dst",
794                       [(set GR8:$dst, (MSP430rrc GR8:$src)),
795                        (implicit SRW)]>;
796 def SAR16r1c : Pseudo<(outs GR16:$dst), (ins GR16:$src),
797                       "clrc\n\t"
798                       "rrc.w\t$dst",
799                       [(set GR16:$dst, (MSP430rrc GR16:$src)),
800                        (implicit SRW)]>;
801
802 def SEXT16r : Pseudo<(outs GR16:$dst), (ins GR16:$src),
803                      "sxt\t$dst",
804                      [(set GR16:$dst, (sext_inreg GR16:$src, i8)),
805                       (implicit SRW)]>;
806
807 } // Defs = [SRW]
808
809 def ZEXT16r : Pseudo<(outs GR16:$dst), (ins GR16:$src),
810                      "mov.b\t{$src, $dst}",
811                      [(set GR16:$dst, (zext (trunc GR16:$src)))]>;
812
813 def SWPB16r : Pseudo<(outs GR16:$dst), (ins GR16:$src),
814                      "swpb\t$dst",
815                      [(set GR16:$dst, (bswap GR16:$src))]>;
816
817 } // isTwoAddress = 1
818
819 // Integer comparisons
820 let Defs = [SRW] in {
821 def CMP8rr  : Pseudo<(outs), (ins GR8:$src1, GR8:$src2),
822                      "cmp.b\t{$src2, $src1}",
823                      [(MSP430cmp GR8:$src1, GR8:$src2), (implicit SRW)]>;
824 def CMP16rr : Pseudo<(outs), (ins GR16:$src1, GR16:$src2),
825                      "cmp.w\t{$src2, $src1}",
826                      [(MSP430cmp GR16:$src1, GR16:$src2), (implicit SRW)]>;
827
828 def CMP8ri  : Pseudo<(outs), (ins GR8:$src1, i8imm:$src2),
829                    "cmp.b\t{$src2, $src1}",
830                    [(MSP430cmp GR8:$src1, imm:$src2), (implicit SRW)]>;
831 def CMP16ri : Pseudo<(outs), (ins GR16:$src1, i16imm:$src2),
832                      "cmp.w\t{$src2, $src1}",
833                      [(MSP430cmp GR16:$src1, imm:$src2), (implicit SRW)]>;
834
835 def CMP8mi  : Pseudo<(outs), (ins memsrc:$src1, i8imm:$src2),
836                      "cmp.b\t{$src2, $src1}",
837                       [(MSP430cmp (load addr:$src1),
838                                   (i8 imm:$src2)), (implicit SRW)]>;
839 def CMP16mi : Pseudo<(outs), (ins memsrc:$src1, i16imm:$src2),
840                      "cmp.w\t{$src2, $src1}",
841                       [(MSP430cmp (load addr:$src1),
842                                   (i16 imm:$src2)), (implicit SRW)]>;
843
844 def CMP8rm  : Pseudo<(outs), (ins GR8:$src1, memsrc:$src2),
845                      "cmp.b\t{$src2, $src1}",
846                      [(MSP430cmp GR8:$src1, (load addr:$src2)), (implicit SRW)]>;
847 def CMP16rm : Pseudo<(outs), (ins GR16:$src1, memsrc:$src2),
848                      "cmp.w\t{$src2, $src1}",
849                      [(MSP430cmp GR16:$src1, (load addr:$src2)), (implicit SRW)]>;
850
851 def CMP8mr  : Pseudo<(outs), (ins memsrc:$src1, GR8:$src2),
852                 "cmp.b\t{$src2, $src1}",
853                 [(MSP430cmp (load addr:$src1), GR8:$src2), (implicit SRW)]>;
854 def CMP16mr : Pseudo<(outs), (ins memsrc:$src1, GR16:$src2),
855                 "cmp.w\t{$src2, $src1}",
856                 [(MSP430cmp (load addr:$src1), GR16:$src2), (implicit SRW)]>;
857
858
859 // BIT TESTS, just sets condition codes
860 // Note that the C condition is set differently than when using CMP.
861 let isCommutable = 1 in {
862 def BIT8rr  : Pseudo<(outs), (ins GR8:$src1, GR8:$src2),
863                      "bit.b\t{$src2, $src1}",
864                      [(MSP430cmp 0, (and_su GR8:$src1, GR8:$src2)),
865                       (implicit SRW)]>;
866 def BIT16rr : Pseudo<(outs), (ins GR16:$src1, GR16:$src2),
867                      "bit.w\t{$src2, $src1}",
868                      [(MSP430cmp 0, (and_su GR16:$src1, GR16:$src2)),
869                       (implicit SRW)]>;
870 }
871 def BIT8ri  : Pseudo<(outs), (ins GR8:$src1, i8imm:$src2),
872                      "bit.b\t{$src2, $src1}",
873                      [(MSP430cmp 0, (and_su GR8:$src1, imm:$src2)),
874                       (implicit SRW)]>;
875 def BIT16ri : Pseudo<(outs), (ins GR16:$src1, i16imm:$src2),
876                      "bit.w\t{$src2, $src1}",
877                      [(MSP430cmp 0, (and_su GR16:$src1, imm:$src2)),
878                       (implicit SRW)]>;
879
880 def BIT8rm  : Pseudo<(outs), (ins GR8:$src1, memdst:$src2),
881                      "bit.b\t{$src2, $src1}",
882                      [(MSP430cmp 0, (and_su GR8:$src1,  (load addr:$src2))),
883                       (implicit SRW)]>;
884 def BIT16rm : Pseudo<(outs), (ins GR16:$src1, memdst:$src2),
885                      "bit.w\t{$src2, $src1}",
886                      [(MSP430cmp 0, (and_su GR16:$src1,  (load addr:$src2))),
887                       (implicit SRW)]>;
888
889 def BIT8mr  : Pseudo<(outs), (ins memsrc:$src1, GR8:$src2),
890                      "bit.b\t{$src2, $src1}",
891                      [(MSP430cmp 0, (and_su (load addr:$src1), GR8:$src2)),
892                       (implicit SRW)]>;
893 def BIT16mr : Pseudo<(outs), (ins memsrc:$src1, GR16:$src2),
894                      "bit.w\t{$src2, $src1}",
895                      [(MSP430cmp 0, (and_su (load addr:$src1), GR16:$src2)),
896                       (implicit SRW)]>;
897
898 def BIT8mi  : Pseudo<(outs), (ins memsrc:$src1, i8imm:$src2),
899                      "bit.b\t{$src2, $src1}",
900                      [(MSP430cmp 0, (and_su (load addr:$src1), (i8 imm:$src2))),
901                       (implicit SRW)]>;
902 def BIT16mi : Pseudo<(outs), (ins memsrc:$src1, i16imm:$src2),
903                      "bit.w\t{$src2, $src1}",
904                      [(MSP430cmp 0, (and_su (load addr:$src1), (i16 imm:$src2))),
905                       (implicit SRW)]>;
906
907 def BIT8mm  : Pseudo<(outs), (ins memsrc:$src1, memsrc:$src2),
908                      "bit.b\t{$src2, $src1}",
909                      [(MSP430cmp 0, (and_su (i8 (load addr:$src1)), 
910                                             (load addr:$src2))),
911                       (implicit SRW)]>;
912 def BIT16mm : Pseudo<(outs), (ins memsrc:$src1, memsrc:$src2),
913                      "bit.w\t{$src2, $src1}",
914                      [(MSP430cmp 0, (and_su (i16 (load addr:$src1)),
915                                             (load addr:$src2))),
916                       (implicit SRW)]>;
917 } // Defs = [SRW]
918
919 //===----------------------------------------------------------------------===//
920 // Non-Instruction Patterns
921
922 // extload
923 def : Pat<(extloadi16i8 addr:$src), (MOVZX16rm8 addr:$src)>;
924
925 // anyext
926 def : Pat<(anyext addr:$src), (MOVZX16rr8 GR8:$src)>;
927
928 // truncs
929 def : Pat<(i8 (trunc GR16:$src)),
930           (EXTRACT_SUBREG GR16:$src, subreg_8bit)>;
931
932 // GlobalAddress, ExternalSymbol
933 def : Pat<(i16 (MSP430Wrapper tglobaladdr:$dst)), (MOV16ri tglobaladdr:$dst)>;
934 def : Pat<(i16 (MSP430Wrapper texternalsym:$dst)), (MOV16ri texternalsym:$dst)>;
935
936 def : Pat<(add GR16:$src1, (MSP430Wrapper tglobaladdr :$src2)),
937           (ADD16ri GR16:$src1, tglobaladdr:$src2)>;
938 def : Pat<(add GR16:$src1, (MSP430Wrapper texternalsym:$src2)),
939           (ADD16ri GR16:$src1, texternalsym:$src2)>;
940
941 def : Pat<(store (i16 (MSP430Wrapper tglobaladdr:$src)), addr:$dst),
942           (MOV16mi addr:$dst, tglobaladdr:$src)>;
943 def : Pat<(store (i16 (MSP430Wrapper texternalsym:$src)), addr:$dst),
944           (MOV16mi addr:$dst, texternalsym:$src)>;
945
946 // calls
947 def : Pat<(MSP430call (i16 tglobaladdr:$dst)),
948           (CALLi tglobaladdr:$dst)>;
949 def : Pat<(MSP430call (i16 texternalsym:$dst)),
950           (CALLi texternalsym:$dst)>;
951
952 // add and sub always produce carry
953 def : Pat<(addc GR16:$src1, GR16:$src2),
954           (ADD16rr GR16:$src1, GR16:$src2)>;
955 def : Pat<(addc GR16:$src1, (load addr:$src2)),
956           (ADD16rm GR16:$src1, addr:$src2)>;
957 def : Pat<(addc GR16:$src1, imm:$src2),
958           (ADD16ri GR16:$src1, imm:$src2)>;
959 def : Pat<(store (addc (load addr:$dst), GR16:$src), addr:$dst),
960           (ADD16mr addr:$dst, GR16:$src)>;
961 def : Pat<(store (addc (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
962           (ADD16mm addr:$dst, addr:$src)>;
963
964 def : Pat<(addc GR8:$src1, GR8:$src2),
965           (ADD8rr GR8:$src1, GR8:$src2)>;
966 def : Pat<(addc GR8:$src1, (load addr:$src2)),
967           (ADD8rm GR8:$src1, addr:$src2)>;
968 def : Pat<(addc GR8:$src1, imm:$src2),
969           (ADD8ri GR8:$src1, imm:$src2)>;
970 def : Pat<(store (addc (load addr:$dst), GR8:$src), addr:$dst),
971           (ADD8mr addr:$dst, GR8:$src)>;
972 def : Pat<(store (addc (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
973           (ADD8mm addr:$dst, addr:$src)>;
974
975 def : Pat<(subc GR16:$src1, GR16:$src2),
976           (SUB16rr GR16:$src1, GR16:$src2)>;
977 def : Pat<(subc GR16:$src1, (load addr:$src2)),
978           (SUB16rm GR16:$src1, addr:$src2)>;
979 def : Pat<(subc GR16:$src1, imm:$src2),
980           (SUB16ri GR16:$src1, imm:$src2)>;
981 def : Pat<(store (subc (load addr:$dst), GR16:$src), addr:$dst),
982           (SUB16mr addr:$dst, GR16:$src)>;
983 def : Pat<(store (subc (load addr:$dst), (i16 (load addr:$src))), addr:$dst),
984           (SUB16mm addr:$dst, addr:$src)>;
985
986 def : Pat<(subc GR8:$src1, GR8:$src2),
987           (SUB8rr GR8:$src1, GR8:$src2)>;
988 def : Pat<(subc GR8:$src1, (load addr:$src2)),
989           (SUB8rm GR8:$src1, addr:$src2)>;
990 def : Pat<(subc GR8:$src1, imm:$src2),
991           (SUB8ri GR8:$src1, imm:$src2)>;
992 def : Pat<(store (subc (load addr:$dst), GR8:$src), addr:$dst),
993           (SUB8mr addr:$dst, GR8:$src)>;
994 def : Pat<(store (subc (load addr:$dst), (i8 (load addr:$src))), addr:$dst),
995           (SUB8mm addr:$dst, addr:$src)>;
996
997 // peephole patterns
998 def : Pat<(and GR16:$src, 255), (ZEXT16r GR16:$src)>;
999 def : Pat<(MSP430cmp 0, (trunc (and_su GR16:$src1, GR16:$src2))),
1000           (BIT8rr (EXTRACT_SUBREG GR16:$src1, subreg_8bit),
1001                   (EXTRACT_SUBREG GR16:$src2, subreg_8bit))>;