1//===- X86InstrCompiler.td - Compiler Pseudos and Patterns -*- tablegen -*-===// 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 various pseudo instructions used by the compiler, 11// as well as Pat patterns used during instruction selection. 12// 13//===----------------------------------------------------------------------===// 14 15//===----------------------------------------------------------------------===// 16// Pattern Matching Support 17 18def GetLo32XForm : SDNodeXForm<imm, [{ 19 // Transformation function: get the low 32 bits. 20 return getI32Imm((uint32_t)N->getZExtValue(), SDLoc(N)); 21}]>; 22 23def GetLo8XForm : SDNodeXForm<imm, [{ 24 // Transformation function: get the low 8 bits. 25 return getI8Imm((uint8_t)N->getZExtValue(), SDLoc(N)); 26}]>; 27 28 29//===----------------------------------------------------------------------===// 30// Random Pseudo Instructions. 31 32// PIC base construction. This expands to code that looks like this: 33// call $next_inst 34// popl %destreg" 35let hasSideEffects = 0, isNotDuplicable = 1, Uses = [ESP, SSP], 36 SchedRW = [WriteJump] in 37 def MOVPC32r : Ii32<0xE8, Pseudo, (outs GR32:$reg), (ins i32imm:$label), 38 "", []>; 39 40// ADJCALLSTACKDOWN/UP implicitly use/def ESP because they may be expanded into 41// a stack adjustment and the codegen must know that they may modify the stack 42// pointer before prolog-epilog rewriting occurs. 43// Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become 44// sub / add which can clobber EFLAGS. 45let Defs = [ESP, EFLAGS, SSP], Uses = [ESP, SSP], SchedRW = [WriteALU] in { 46def ADJCALLSTACKDOWN32 : I<0, Pseudo, (outs), 47 (ins i32imm:$amt1, i32imm:$amt2, i32imm:$amt3), 48 "#ADJCALLSTACKDOWN", []>, Requires<[NotLP64]>; 49def ADJCALLSTACKUP32 : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2), 50 "#ADJCALLSTACKUP", 51 [(X86callseq_end timm:$amt1, timm:$amt2)]>, 52 Requires<[NotLP64]>; 53} 54def : Pat<(X86callseq_start timm:$amt1, timm:$amt2), 55 (ADJCALLSTACKDOWN32 i32imm:$amt1, i32imm:$amt2, 0)>, Requires<[NotLP64]>; 56 57 58// ADJCALLSTACKDOWN/UP implicitly use/def RSP because they may be expanded into 59// a stack adjustment and the codegen must know that they may modify the stack 60// pointer before prolog-epilog rewriting occurs. 61// Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become 62// sub / add which can clobber EFLAGS. 63let Defs = [RSP, EFLAGS, SSP], Uses = [RSP, SSP], SchedRW = [WriteALU] in { 64def ADJCALLSTACKDOWN64 : I<0, Pseudo, (outs), 65 (ins i32imm:$amt1, i32imm:$amt2, i32imm:$amt3), 66 "#ADJCALLSTACKDOWN", []>, Requires<[IsLP64]>; 67def ADJCALLSTACKUP64 : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2), 68 "#ADJCALLSTACKUP", 69 [(X86callseq_end timm:$amt1, timm:$amt2)]>, 70 Requires<[IsLP64]>; 71} 72def : Pat<(X86callseq_start timm:$amt1, timm:$amt2), 73 (ADJCALLSTACKDOWN64 i32imm:$amt1, i32imm:$amt2, 0)>, Requires<[IsLP64]>; 74 75let SchedRW = [WriteSystem] in { 76 77// x86-64 va_start lowering magic. 78let usesCustomInserter = 1, Defs = [EFLAGS] in { 79def VASTART_SAVE_XMM_REGS : I<0, Pseudo, 80 (outs), 81 (ins GR8:$al, 82 i64imm:$regsavefi, i64imm:$offset, 83 variable_ops), 84 "#VASTART_SAVE_XMM_REGS $al, $regsavefi, $offset", 85 [(X86vastart_save_xmm_regs GR8:$al, 86 imm:$regsavefi, 87 imm:$offset), 88 (implicit EFLAGS)]>; 89 90// The VAARG_64 pseudo-instruction takes the address of the va_list, 91// and places the address of the next argument into a register. 92let Defs = [EFLAGS] in 93def VAARG_64 : I<0, Pseudo, 94 (outs GR64:$dst), 95 (ins i8mem:$ap, i32imm:$size, i8imm:$mode, i32imm:$align), 96 "#VAARG_64 $dst, $ap, $size, $mode, $align", 97 [(set GR64:$dst, 98 (X86vaarg64 addr:$ap, imm:$size, imm:$mode, imm:$align)), 99 (implicit EFLAGS)]>; 100 101 102// When using segmented stacks these are lowered into instructions which first 103// check if the current stacklet has enough free memory. If it does, memory is 104// allocated by bumping the stack pointer. Otherwise memory is allocated from 105// the heap. 106 107let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in 108def SEG_ALLOCA_32 : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$size), 109 "# variable sized alloca for segmented stacks", 110 [(set GR32:$dst, 111 (X86SegAlloca GR32:$size))]>, 112 Requires<[NotLP64]>; 113 114let Defs = [RAX, RSP, EFLAGS], Uses = [RSP] in 115def SEG_ALLOCA_64 : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$size), 116 "# variable sized alloca for segmented stacks", 117 [(set GR64:$dst, 118 (X86SegAlloca GR64:$size))]>, 119 Requires<[In64BitMode]>; 120} 121 122// Dynamic stack allocation yields a _chkstk or _alloca call for all Windows 123// targets. These calls are needed to probe the stack when allocating more than 124// 4k bytes in one go. Touching the stack at 4K increments is necessary to 125// ensure that the guard pages used by the OS virtual memory manager are 126// allocated in correct sequence. 127// The main point of having separate instruction are extra unmodelled effects 128// (compared to ordinary calls) like stack pointer change. 129 130let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in 131def WIN_ALLOCA_32 : I<0, Pseudo, (outs), (ins GR32:$size), 132 "# dynamic stack allocation", 133 [(X86WinAlloca GR32:$size)]>, 134 Requires<[NotLP64]>; 135 136let Defs = [RAX, RSP, EFLAGS], Uses = [RSP] in 137def WIN_ALLOCA_64 : I<0, Pseudo, (outs), (ins GR64:$size), 138 "# dynamic stack allocation", 139 [(X86WinAlloca GR64:$size)]>, 140 Requires<[In64BitMode]>; 141} // SchedRW 142 143// These instructions XOR the frame pointer into a GPR. They are used in some 144// stack protection schemes. These are post-RA pseudos because we only know the 145// frame register after register allocation. 146let Constraints = "$src = $dst", isPseudo = 1, Defs = [EFLAGS] in { 147 def XOR32_FP : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src), 148 "xorl\t$$FP, $src", []>, 149 Requires<[NotLP64]>, Sched<[WriteALU]>; 150 def XOR64_FP : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$src), 151 "xorq\t$$FP $src", []>, 152 Requires<[In64BitMode]>, Sched<[WriteALU]>; 153} 154 155//===----------------------------------------------------------------------===// 156// EH Pseudo Instructions 157// 158let SchedRW = [WriteSystem] in { 159let isTerminator = 1, isReturn = 1, isBarrier = 1, 160 hasCtrlDep = 1, isCodeGenOnly = 1 in { 161def EH_RETURN : I<0xC3, RawFrm, (outs), (ins GR32:$addr), 162 "ret\t#eh_return, addr: $addr", 163 [(X86ehret GR32:$addr)]>, Sched<[WriteJumpLd]>; 164 165} 166 167let isTerminator = 1, isReturn = 1, isBarrier = 1, 168 hasCtrlDep = 1, isCodeGenOnly = 1 in { 169def EH_RETURN64 : I<0xC3, RawFrm, (outs), (ins GR64:$addr), 170 "ret\t#eh_return, addr: $addr", 171 [(X86ehret GR64:$addr)]>, Sched<[WriteJumpLd]>; 172 173} 174 175let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 176 isCodeGenOnly = 1, isReturn = 1, isEHScopeReturn = 1 in { 177 def CLEANUPRET : I<0, Pseudo, (outs), (ins), "# CLEANUPRET", [(cleanupret)]>; 178 179 // CATCHRET needs a custom inserter for SEH. 180 let usesCustomInserter = 1 in 181 def CATCHRET : I<0, Pseudo, (outs), (ins brtarget32:$dst, brtarget32:$from), 182 "# CATCHRET", 183 [(catchret bb:$dst, bb:$from)]>; 184} 185 186let hasSideEffects = 1, hasCtrlDep = 1, isCodeGenOnly = 1, 187 usesCustomInserter = 1 in 188def CATCHPAD : I<0, Pseudo, (outs), (ins), "# CATCHPAD", [(catchpad)]>; 189 190// This instruction is responsible for re-establishing stack pointers after an 191// exception has been caught and we are rejoining normal control flow in the 192// parent function or funclet. It generally sets ESP and EBP, and optionally 193// ESI. It is only needed for 32-bit WinEH, as the runtime restores CSRs for us 194// elsewhere. 195let hasSideEffects = 1, hasCtrlDep = 1, isCodeGenOnly = 1 in 196def EH_RESTORE : I<0, Pseudo, (outs), (ins), "# EH_RESTORE", []>; 197 198let hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1, 199 usesCustomInserter = 1 in { 200 def EH_SjLj_SetJmp32 : I<0, Pseudo, (outs GR32:$dst), (ins i32mem:$buf), 201 "#EH_SJLJ_SETJMP32", 202 [(set GR32:$dst, (X86eh_sjlj_setjmp addr:$buf))]>, 203 Requires<[Not64BitMode]>; 204 def EH_SjLj_SetJmp64 : I<0, Pseudo, (outs GR32:$dst), (ins i64mem:$buf), 205 "#EH_SJLJ_SETJMP64", 206 [(set GR32:$dst, (X86eh_sjlj_setjmp addr:$buf))]>, 207 Requires<[In64BitMode]>; 208 let isTerminator = 1 in { 209 def EH_SjLj_LongJmp32 : I<0, Pseudo, (outs), (ins i32mem:$buf), 210 "#EH_SJLJ_LONGJMP32", 211 [(X86eh_sjlj_longjmp addr:$buf)]>, 212 Requires<[Not64BitMode]>; 213 def EH_SjLj_LongJmp64 : I<0, Pseudo, (outs), (ins i64mem:$buf), 214 "#EH_SJLJ_LONGJMP64", 215 [(X86eh_sjlj_longjmp addr:$buf)]>, 216 Requires<[In64BitMode]>; 217 } 218} 219 220let isBranch = 1, isTerminator = 1, isCodeGenOnly = 1 in { 221 def EH_SjLj_Setup : I<0, Pseudo, (outs), (ins brtarget:$dst), 222 "#EH_SjLj_Setup\t$dst", []>; 223} 224} // SchedRW 225 226//===----------------------------------------------------------------------===// 227// Pseudo instructions used by unwind info. 228// 229let isPseudo = 1, SchedRW = [WriteSystem] in { 230 def SEH_PushReg : I<0, Pseudo, (outs), (ins i32imm:$reg), 231 "#SEH_PushReg $reg", []>; 232 def SEH_SaveReg : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$dst), 233 "#SEH_SaveReg $reg, $dst", []>; 234 def SEH_SaveXMM : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$dst), 235 "#SEH_SaveXMM $reg, $dst", []>; 236 def SEH_StackAlloc : I<0, Pseudo, (outs), (ins i32imm:$size), 237 "#SEH_StackAlloc $size", []>; 238 def SEH_StackAlign : I<0, Pseudo, (outs), (ins i32imm:$align), 239 "#SEH_StackAlign $align", []>; 240 def SEH_SetFrame : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$offset), 241 "#SEH_SetFrame $reg, $offset", []>; 242 def SEH_PushFrame : I<0, Pseudo, (outs), (ins i1imm:$mode), 243 "#SEH_PushFrame $mode", []>; 244 def SEH_EndPrologue : I<0, Pseudo, (outs), (ins), 245 "#SEH_EndPrologue", []>; 246 def SEH_Epilogue : I<0, Pseudo, (outs), (ins), 247 "#SEH_Epilogue", []>; 248} 249 250//===----------------------------------------------------------------------===// 251// Pseudo instructions used by segmented stacks. 252// 253 254// This is lowered into a RET instruction by MCInstLower. We need 255// this so that we don't have to have a MachineBasicBlock which ends 256// with a RET and also has successors. 257let isPseudo = 1, SchedRW = [WriteJumpLd] in { 258def MORESTACK_RET: I<0, Pseudo, (outs), (ins), "", []>; 259 260// This instruction is lowered to a RET followed by a MOV. The two 261// instructions are not generated on a higher level since then the 262// verifier sees a MachineBasicBlock ending with a non-terminator. 263def MORESTACK_RET_RESTORE_R10 : I<0, Pseudo, (outs), (ins), "", []>; 264} 265 266//===----------------------------------------------------------------------===// 267// Alias Instructions 268//===----------------------------------------------------------------------===// 269 270// Alias instruction mapping movr0 to xor. 271// FIXME: remove when we can teach regalloc that xor reg, reg is ok. 272let Defs = [EFLAGS], isReMaterializable = 1, isAsCheapAsAMove = 1, 273 isPseudo = 1, AddedComplexity = 10 in { 274def MOV32r0 : I<0, Pseudo, (outs GR32:$dst), (ins), "", 275 [(set GR32:$dst, 0)]>, Sched<[WriteZero]>; 276def MOV64r0 : I<0, Pseudo, (outs GR64:$dst), (ins), "", 277 [(set GR64:$dst, 0)]>, Sched<[WriteZero]>; 278} 279 280// Other widths can also make use of the 32-bit xor, which may have a smaller 281// encoding and avoid partial register updates. 282let AddedComplexity = 10 in { 283def : Pat<(i8 0), (EXTRACT_SUBREG (MOV32r0), sub_8bit)>; 284def : Pat<(i16 0), (EXTRACT_SUBREG (MOV32r0), sub_16bit)>; 285} 286 287let Predicates = [OptForSize, Not64BitMode], 288 AddedComplexity = 10 in { 289 let SchedRW = [WriteALU] in { 290 // Pseudo instructions for materializing 1 and -1 using XOR+INC/DEC, 291 // which only require 3 bytes compared to MOV32ri which requires 5. 292 let Defs = [EFLAGS], isReMaterializable = 1, isPseudo = 1 in { 293 def MOV32r1 : I<0, Pseudo, (outs GR32:$dst), (ins), "", 294 [(set GR32:$dst, 1)]>; 295 def MOV32r_1 : I<0, Pseudo, (outs GR32:$dst), (ins), "", 296 [(set GR32:$dst, -1)]>; 297 } 298 } // SchedRW 299 300 // MOV16ri is 4 bytes, so the instructions above are smaller. 301 def : Pat<(i16 1), (EXTRACT_SUBREG (MOV32r1), sub_16bit)>; 302 def : Pat<(i16 -1), (EXTRACT_SUBREG (MOV32r_1), sub_16bit)>; 303} 304 305let isReMaterializable = 1, isPseudo = 1, AddedComplexity = 5, 306 SchedRW = [WriteALU] in { 307// AddedComplexity higher than MOV64ri but lower than MOV32r0 and MOV32r1. 308def MOV32ImmSExti8 : I<0, Pseudo, (outs GR32:$dst), (ins i32i8imm:$src), "", 309 [(set GR32:$dst, i32immSExt8:$src)]>, 310 Requires<[OptForMinSize, NotWin64WithoutFP]>; 311def MOV64ImmSExti8 : I<0, Pseudo, (outs GR64:$dst), (ins i64i8imm:$src), "", 312 [(set GR64:$dst, i64immSExt8:$src)]>, 313 Requires<[OptForMinSize, NotWin64WithoutFP]>; 314} 315 316// Materialize i64 constant where top 32-bits are zero. This could theoretically 317// use MOV32ri with a SUBREG_TO_REG to represent the zero-extension, however 318// that would make it more difficult to rematerialize. 319let isReMaterializable = 1, isAsCheapAsAMove = 1, 320 isPseudo = 1, hasSideEffects = 0, SchedRW = [WriteMove] in 321def MOV32ri64 : I<0, Pseudo, (outs GR64:$dst), (ins i64i32imm:$src), "", []>; 322 323// This 64-bit pseudo-move can be used for both a 64-bit constant that is 324// actually the zero-extension of a 32-bit constant and for labels in the 325// x86-64 small code model. 326def mov64imm32 : ComplexPattern<i64, 1, "selectMOV64Imm32", [imm, X86Wrapper]>; 327 328def : Pat<(i64 mov64imm32:$src), (MOV32ri64 mov64imm32:$src)>; 329 330// Use sbb to materialize carry bit. 331let Uses = [EFLAGS], Defs = [EFLAGS], isPseudo = 1, SchedRW = [WriteALU] in { 332// FIXME: These are pseudo ops that should be replaced with Pat<> patterns. 333// However, Pat<> can't replicate the destination reg into the inputs of the 334// result. 335def SETB_C8r : I<0, Pseudo, (outs GR8:$dst), (ins), "", 336 [(set GR8:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>; 337def SETB_C16r : I<0, Pseudo, (outs GR16:$dst), (ins), "", 338 [(set GR16:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>; 339def SETB_C32r : I<0, Pseudo, (outs GR32:$dst), (ins), "", 340 [(set GR32:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>; 341def SETB_C64r : I<0, Pseudo, (outs GR64:$dst), (ins), "", 342 [(set GR64:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>; 343} // isCodeGenOnly 344 345 346def : Pat<(i16 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 347 (SETB_C16r)>; 348def : Pat<(i32 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 349 (SETB_C32r)>; 350def : Pat<(i64 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 351 (SETB_C64r)>; 352 353def : Pat<(i16 (sext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 354 (SETB_C16r)>; 355def : Pat<(i32 (sext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 356 (SETB_C32r)>; 357def : Pat<(i64 (sext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 358 (SETB_C64r)>; 359 360// We canonicalize 'setb' to "(and (sbb reg,reg), 1)" on the hope that the and 361// will be eliminated and that the sbb can be extended up to a wider type. When 362// this happens, it is great. However, if we are left with an 8-bit sbb and an 363// and, we might as well just match it as a setb. 364def : Pat<(and (i8 (X86setcc_c X86_COND_B, EFLAGS)), 1), 365 (SETBr)>; 366 367// (add OP, SETB) -> (adc OP, 0) 368def : Pat<(add (and (i8 (X86setcc_c X86_COND_B, EFLAGS)), 1), GR8:$op), 369 (ADC8ri GR8:$op, 0)>; 370def : Pat<(add (and (i32 (X86setcc_c X86_COND_B, EFLAGS)), 1), GR32:$op), 371 (ADC32ri8 GR32:$op, 0)>; 372def : Pat<(add (and (i64 (X86setcc_c X86_COND_B, EFLAGS)), 1), GR64:$op), 373 (ADC64ri8 GR64:$op, 0)>; 374 375// (sub OP, SETB) -> (sbb OP, 0) 376def : Pat<(sub GR8:$op, (and (i8 (X86setcc_c X86_COND_B, EFLAGS)), 1)), 377 (SBB8ri GR8:$op, 0)>; 378def : Pat<(sub GR32:$op, (and (i32 (X86setcc_c X86_COND_B, EFLAGS)), 1)), 379 (SBB32ri8 GR32:$op, 0)>; 380def : Pat<(sub GR64:$op, (and (i64 (X86setcc_c X86_COND_B, EFLAGS)), 1)), 381 (SBB64ri8 GR64:$op, 0)>; 382 383// (sub OP, SETCC_CARRY) -> (adc OP, 0) 384def : Pat<(sub GR8:$op, (i8 (X86setcc_c X86_COND_B, EFLAGS))), 385 (ADC8ri GR8:$op, 0)>; 386def : Pat<(sub GR32:$op, (i32 (X86setcc_c X86_COND_B, EFLAGS))), 387 (ADC32ri8 GR32:$op, 0)>; 388def : Pat<(sub GR64:$op, (i64 (X86setcc_c X86_COND_B, EFLAGS))), 389 (ADC64ri8 GR64:$op, 0)>; 390 391//===----------------------------------------------------------------------===// 392// String Pseudo Instructions 393// 394let SchedRW = [WriteMicrocoded] in { 395let Defs = [ECX,EDI,ESI], Uses = [ECX,EDI,ESI], isCodeGenOnly = 1 in { 396def REP_MOVSB_32 : I<0xA4, RawFrm, (outs), (ins), 397 "{rep;movsb (%esi), %es:(%edi)|rep movsb es:[edi], [esi]}", 398 [(X86rep_movs i8)]>, REP, AdSize32, 399 Requires<[NotLP64]>; 400def REP_MOVSW_32 : I<0xA5, RawFrm, (outs), (ins), 401 "{rep;movsw (%esi), %es:(%edi)|rep movsw es:[edi], [esi]}", 402 [(X86rep_movs i16)]>, REP, AdSize32, OpSize16, 403 Requires<[NotLP64]>; 404def REP_MOVSD_32 : I<0xA5, RawFrm, (outs), (ins), 405 "{rep;movsl (%esi), %es:(%edi)|rep movsd es:[edi], [esi]}", 406 [(X86rep_movs i32)]>, REP, AdSize32, OpSize32, 407 Requires<[NotLP64]>; 408def REP_MOVSQ_32 : RI<0xA5, RawFrm, (outs), (ins), 409 "{rep;movsq (%esi), %es:(%edi)|rep movsq es:[edi], [esi]}", 410 [(X86rep_movs i64)]>, REP, AdSize32, 411 Requires<[NotLP64, In64BitMode]>; 412} 413 414let Defs = [RCX,RDI,RSI], Uses = [RCX,RDI,RSI], isCodeGenOnly = 1 in { 415def REP_MOVSB_64 : I<0xA4, RawFrm, (outs), (ins), 416 "{rep;movsb (%rsi), %es:(%rdi)|rep movsb es:[rdi], [rsi]}", 417 [(X86rep_movs i8)]>, REP, AdSize64, 418 Requires<[IsLP64]>; 419def REP_MOVSW_64 : I<0xA5, RawFrm, (outs), (ins), 420 "{rep;movsw (%rsi), %es:(%rdi)|rep movsw es:[rdi], [rsi]}", 421 [(X86rep_movs i16)]>, REP, AdSize64, OpSize16, 422 Requires<[IsLP64]>; 423def REP_MOVSD_64 : I<0xA5, RawFrm, (outs), (ins), 424 "{rep;movsl (%rsi), %es:(%rdi)|rep movsdi es:[rdi], [rsi]}", 425 [(X86rep_movs i32)]>, REP, AdSize64, OpSize32, 426 Requires<[IsLP64]>; 427def REP_MOVSQ_64 : RI<0xA5, RawFrm, (outs), (ins), 428 "{rep;movsq (%rsi), %es:(%rdi)|rep movsq es:[rdi], [rsi]}", 429 [(X86rep_movs i64)]>, REP, AdSize64, 430 Requires<[IsLP64]>; 431} 432 433// FIXME: Should use "(X86rep_stos AL)" as the pattern. 434let Defs = [ECX,EDI], isCodeGenOnly = 1 in { 435 let Uses = [AL,ECX,EDI] in 436 def REP_STOSB_32 : I<0xAA, RawFrm, (outs), (ins), 437 "{rep;stosb %al, %es:(%edi)|rep stosb es:[edi], al}", 438 [(X86rep_stos i8)]>, REP, AdSize32, 439 Requires<[NotLP64]>; 440 let Uses = [AX,ECX,EDI] in 441 def REP_STOSW_32 : I<0xAB, RawFrm, (outs), (ins), 442 "{rep;stosw %ax, %es:(%edi)|rep stosw es:[edi], ax}", 443 [(X86rep_stos i16)]>, REP, AdSize32, OpSize16, 444 Requires<[NotLP64]>; 445 let Uses = [EAX,ECX,EDI] in 446 def REP_STOSD_32 : I<0xAB, RawFrm, (outs), (ins), 447 "{rep;stosl %eax, %es:(%edi)|rep stosd es:[edi], eax}", 448 [(X86rep_stos i32)]>, REP, AdSize32, OpSize32, 449 Requires<[NotLP64]>; 450 let Uses = [RAX,RCX,RDI] in 451 def REP_STOSQ_32 : RI<0xAB, RawFrm, (outs), (ins), 452 "{rep;stosq %rax, %es:(%edi)|rep stosq es:[edi], rax}", 453 [(X86rep_stos i64)]>, REP, AdSize32, 454 Requires<[NotLP64, In64BitMode]>; 455} 456 457let Defs = [RCX,RDI], isCodeGenOnly = 1 in { 458 let Uses = [AL,RCX,RDI] in 459 def REP_STOSB_64 : I<0xAA, RawFrm, (outs), (ins), 460 "{rep;stosb %al, %es:(%rdi)|rep stosb es:[rdi], al}", 461 [(X86rep_stos i8)]>, REP, AdSize64, 462 Requires<[IsLP64]>; 463 let Uses = [AX,RCX,RDI] in 464 def REP_STOSW_64 : I<0xAB, RawFrm, (outs), (ins), 465 "{rep;stosw %ax, %es:(%rdi)|rep stosw es:[rdi], ax}", 466 [(X86rep_stos i16)]>, REP, AdSize64, OpSize16, 467 Requires<[IsLP64]>; 468 let Uses = [RAX,RCX,RDI] in 469 def REP_STOSD_64 : I<0xAB, RawFrm, (outs), (ins), 470 "{rep;stosl %eax, %es:(%rdi)|rep stosd es:[rdi], eax}", 471 [(X86rep_stos i32)]>, REP, AdSize64, OpSize32, 472 Requires<[IsLP64]>; 473 474 let Uses = [RAX,RCX,RDI] in 475 def REP_STOSQ_64 : RI<0xAB, RawFrm, (outs), (ins), 476 "{rep;stosq %rax, %es:(%rdi)|rep stosq es:[rdi], rax}", 477 [(X86rep_stos i64)]>, REP, AdSize64, 478 Requires<[IsLP64]>; 479} 480} // SchedRW 481 482//===----------------------------------------------------------------------===// 483// Thread Local Storage Instructions 484// 485let SchedRW = [WriteSystem] in { 486 487// ELF TLS Support 488// All calls clobber the non-callee saved registers. ESP is marked as 489// a use to prevent stack-pointer assignments that appear immediately 490// before calls from potentially appearing dead. 491let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7, 492 ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, 493 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, 494 XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, 495 XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS, DF], 496 usesCustomInserter = 1, Uses = [ESP, SSP] in { 497def TLS_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym), 498 "# TLS_addr32", 499 [(X86tlsaddr tls32addr:$sym)]>, 500 Requires<[Not64BitMode]>; 501def TLS_base_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym), 502 "# TLS_base_addr32", 503 [(X86tlsbaseaddr tls32baseaddr:$sym)]>, 504 Requires<[Not64BitMode]>; 505} 506 507// All calls clobber the non-callee saved registers. RSP is marked as 508// a use to prevent stack-pointer assignments that appear immediately 509// before calls from potentially appearing dead. 510let Defs = [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11, 511 FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7, 512 ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, 513 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, 514 XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, 515 XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS, DF], 516 usesCustomInserter = 1, Uses = [RSP, SSP] in { 517def TLS_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym), 518 "# TLS_addr64", 519 [(X86tlsaddr tls64addr:$sym)]>, 520 Requires<[In64BitMode]>; 521def TLS_base_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym), 522 "# TLS_base_addr64", 523 [(X86tlsbaseaddr tls64baseaddr:$sym)]>, 524 Requires<[In64BitMode]>; 525} 526 527// Darwin TLS Support 528// For i386, the address of the thunk is passed on the stack, on return the 529// address of the variable is in %eax. %ecx is trashed during the function 530// call. All other registers are preserved. 531let Defs = [EAX, ECX, EFLAGS, DF], 532 Uses = [ESP, SSP], 533 usesCustomInserter = 1 in 534def TLSCall_32 : I<0, Pseudo, (outs), (ins i32mem:$sym), 535 "# TLSCall_32", 536 [(X86TLSCall addr:$sym)]>, 537 Requires<[Not64BitMode]>; 538 539// For x86_64, the address of the thunk is passed in %rdi, but the 540// pseudo directly use the symbol, so do not add an implicit use of 541// %rdi. The lowering will do the right thing with RDI. 542// On return the address of the variable is in %rax. All other 543// registers are preserved. 544let Defs = [RAX, EFLAGS, DF], 545 Uses = [RSP, SSP], 546 usesCustomInserter = 1 in 547def TLSCall_64 : I<0, Pseudo, (outs), (ins i64mem:$sym), 548 "# TLSCall_64", 549 [(X86TLSCall addr:$sym)]>, 550 Requires<[In64BitMode]>; 551} // SchedRW 552 553//===----------------------------------------------------------------------===// 554// Conditional Move Pseudo Instructions 555 556// CMOV* - Used to implement the SELECT DAG operation. Expanded after 557// instruction selection into a branch sequence. 558multiclass CMOVrr_PSEUDO<RegisterClass RC, ValueType VT> { 559 def CMOV#NAME : I<0, Pseudo, 560 (outs RC:$dst), (ins RC:$t, RC:$f, i8imm:$cond), 561 "#CMOV_"#NAME#" PSEUDO!", 562 [(set RC:$dst, (VT (X86cmov RC:$t, RC:$f, imm:$cond, 563 EFLAGS)))]>; 564} 565 566let usesCustomInserter = 1, hasNoSchedulingInfo = 1, Uses = [EFLAGS] in { 567 // X86 doesn't have 8-bit conditional moves. Use a customInserter to 568 // emit control flow. An alternative to this is to mark i8 SELECT as Promote, 569 // however that requires promoting the operands, and can induce additional 570 // i8 register pressure. 571 defm _GR8 : CMOVrr_PSEUDO<GR8, i8>; 572 573 let Predicates = [NoCMov] in { 574 defm _GR32 : CMOVrr_PSEUDO<GR32, i32>; 575 defm _GR16 : CMOVrr_PSEUDO<GR16, i16>; 576 } // Predicates = [NoCMov] 577 578 // fcmov doesn't handle all possible EFLAGS, provide a fallback if there is no 579 // SSE1/SSE2. 580 let Predicates = [FPStackf32] in 581 defm _RFP32 : CMOVrr_PSEUDO<RFP32, f32>; 582 583 let Predicates = [FPStackf64] in 584 defm _RFP64 : CMOVrr_PSEUDO<RFP64, f64>; 585 586 defm _RFP80 : CMOVrr_PSEUDO<RFP80, f80>; 587 588 defm _FR32 : CMOVrr_PSEUDO<FR32, f32>; 589 defm _FR64 : CMOVrr_PSEUDO<FR64, f64>; 590 let Predicates = [NoVLX] in { 591 defm _VR128 : CMOVrr_PSEUDO<VR128, v2i64>; 592 defm _VR256 : CMOVrr_PSEUDO<VR256, v4i64>; 593 } 594 let Predicates = [HasVLX] in { 595 defm _VR128X : CMOVrr_PSEUDO<VR128X, v2i64>; 596 defm _VR256X : CMOVrr_PSEUDO<VR256X, v4i64>; 597 } 598 defm _VR512 : CMOVrr_PSEUDO<VR512, v8i64>; 599 defm _VK2 : CMOVrr_PSEUDO<VK2, v2i1>; 600 defm _VK4 : CMOVrr_PSEUDO<VK4, v4i1>; 601 defm _VK8 : CMOVrr_PSEUDO<VK8, v8i1>; 602 defm _VK16 : CMOVrr_PSEUDO<VK16, v16i1>; 603 defm _VK32 : CMOVrr_PSEUDO<VK32, v32i1>; 604 defm _VK64 : CMOVrr_PSEUDO<VK64, v64i1>; 605} // usesCustomInserter = 1, hasNoSchedulingInfo = 1, Uses = [EFLAGS] 606 607def : Pat<(f128 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 608 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 609 610let Predicates = [NoVLX] in { 611 def : Pat<(v16i8 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 612 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 613 def : Pat<(v8i16 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 614 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 615 def : Pat<(v4i32 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 616 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 617 def : Pat<(v4f32 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 618 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 619 def : Pat<(v2f64 (X86cmov VR128:$t, VR128:$f, imm:$cond, EFLAGS)), 620 (CMOV_VR128 VR128:$t, VR128:$f, imm:$cond)>; 621 622 def : Pat<(v32i8 (X86cmov VR256:$t, VR256:$f, imm:$cond, EFLAGS)), 623 (CMOV_VR256 VR256:$t, VR256:$f, imm:$cond)>; 624 def : Pat<(v16i16 (X86cmov VR256:$t, VR256:$f, imm:$cond, EFLAGS)), 625 (CMOV_VR256 VR256:$t, VR256:$f, imm:$cond)>; 626 def : Pat<(v8i32 (X86cmov VR256:$t, VR256:$f, imm:$cond, EFLAGS)), 627 (CMOV_VR256 VR256:$t, VR256:$f, imm:$cond)>; 628 def : Pat<(v8f32 (X86cmov VR256:$t, VR256:$f, imm:$cond, EFLAGS)), 629 (CMOV_VR256 VR256:$t, VR256:$f, imm:$cond)>; 630 def : Pat<(v4f64 (X86cmov VR256:$t, VR256:$f, imm:$cond, EFLAGS)), 631 (CMOV_VR256 VR256:$t, VR256:$f, imm:$cond)>; 632} 633let Predicates = [HasVLX] in { 634 def : Pat<(v16i8 (X86cmov VR128X:$t, VR128X:$f, imm:$cond, EFLAGS)), 635 (CMOV_VR128X VR128X:$t, VR128X:$f, imm:$cond)>; 636 def : Pat<(v8i16 (X86cmov VR128X:$t, VR128X:$f, imm:$cond, EFLAGS)), 637 (CMOV_VR128X VR128X:$t, VR128X:$f, imm:$cond)>; 638 def : Pat<(v4i32 (X86cmov VR128X:$t, VR128X:$f, imm:$cond, EFLAGS)), 639 (CMOV_VR128X VR128X:$t, VR128X:$f, imm:$cond)>; 640 def : Pat<(v4f32 (X86cmov VR128X:$t, VR128X:$f, imm:$cond, EFLAGS)), 641 (CMOV_VR128X VR128X:$t, VR128X:$f, imm:$cond)>; 642 def : Pat<(v2f64 (X86cmov VR128X:$t, VR128X:$f, imm:$cond, EFLAGS)), 643 (CMOV_VR128X VR128X:$t, VR128X:$f, imm:$cond)>; 644 645 def : Pat<(v32i8 (X86cmov VR256X:$t, VR256X:$f, imm:$cond, EFLAGS)), 646 (CMOV_VR256X VR256X:$t, VR256X:$f, imm:$cond)>; 647 def : Pat<(v16i16 (X86cmov VR256X:$t, VR256X:$f, imm:$cond, EFLAGS)), 648 (CMOV_VR256X VR256X:$t, VR256X:$f, imm:$cond)>; 649 def : Pat<(v8i32 (X86cmov VR256X:$t, VR256X:$f, imm:$cond, EFLAGS)), 650 (CMOV_VR256X VR256X:$t, VR256X:$f, imm:$cond)>; 651 def : Pat<(v8f32 (X86cmov VR256X:$t, VR256X:$f, imm:$cond, EFLAGS)), 652 (CMOV_VR256X VR256X:$t, VR256X:$f, imm:$cond)>; 653 def : Pat<(v4f64 (X86cmov VR256X:$t, VR256X:$f, imm:$cond, EFLAGS)), 654 (CMOV_VR256X VR256X:$t, VR256X:$f, imm:$cond)>; 655} 656 657def : Pat<(v64i8 (X86cmov VR512:$t, VR512:$f, imm:$cond, EFLAGS)), 658 (CMOV_VR512 VR512:$t, VR512:$f, imm:$cond)>; 659def : Pat<(v32i16 (X86cmov VR512:$t, VR512:$f, imm:$cond, EFLAGS)), 660 (CMOV_VR512 VR512:$t, VR512:$f, imm:$cond)>; 661def : Pat<(v16i32 (X86cmov VR512:$t, VR512:$f, imm:$cond, EFLAGS)), 662 (CMOV_VR512 VR512:$t, VR512:$f, imm:$cond)>; 663def : Pat<(v16f32 (X86cmov VR512:$t, VR512:$f, imm:$cond, EFLAGS)), 664 (CMOV_VR512 VR512:$t, VR512:$f, imm:$cond)>; 665def : Pat<(v8f64 (X86cmov VR512:$t, VR512:$f, imm:$cond, EFLAGS)), 666 (CMOV_VR512 VR512:$t, VR512:$f, imm:$cond)>; 667 668//===----------------------------------------------------------------------===// 669// Normal-Instructions-With-Lock-Prefix Pseudo Instructions 670//===----------------------------------------------------------------------===// 671 672// FIXME: Use normal instructions and add lock prefix dynamically. 673 674// Memory barriers 675 676// TODO: Get this to fold the constant into the instruction. 677let isCodeGenOnly = 1, Defs = [EFLAGS] in 678def OR32mrLocked : I<0x09, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$zero), 679 "or{l}\t{$zero, $dst|$dst, $zero}", []>, 680 Requires<[Not64BitMode]>, OpSize32, LOCK, 681 Sched<[WriteALURMW]>; 682 683let hasSideEffects = 1 in 684def Int_MemBarrier : I<0, Pseudo, (outs), (ins), 685 "#MEMBARRIER", 686 [(X86MemBarrier)]>, Sched<[WriteLoad]>; 687 688// RegOpc corresponds to the mr version of the instruction 689// ImmOpc corresponds to the mi version of the instruction 690// ImmOpc8 corresponds to the mi8 version of the instruction 691// ImmMod corresponds to the instruction format of the mi and mi8 versions 692multiclass LOCK_ArithBinOp<bits<8> RegOpc, bits<8> ImmOpc, bits<8> ImmOpc8, 693 Format ImmMod, SDNode Op, string mnemonic> { 694let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1, 695 SchedRW = [WriteALURMW] in { 696 697def NAME#8mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4}, 698 RegOpc{3}, RegOpc{2}, RegOpc{1}, 0 }, 699 MRMDestMem, (outs), (ins i8mem:$dst, GR8:$src2), 700 !strconcat(mnemonic, "{b}\t", 701 "{$src2, $dst|$dst, $src2}"), 702 [(set EFLAGS, (Op addr:$dst, GR8:$src2))]>, LOCK; 703 704def NAME#16mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4}, 705 RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 }, 706 MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src2), 707 !strconcat(mnemonic, "{w}\t", 708 "{$src2, $dst|$dst, $src2}"), 709 [(set EFLAGS, (Op addr:$dst, GR16:$src2))]>, 710 OpSize16, LOCK; 711 712def NAME#32mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4}, 713 RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 }, 714 MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src2), 715 !strconcat(mnemonic, "{l}\t", 716 "{$src2, $dst|$dst, $src2}"), 717 [(set EFLAGS, (Op addr:$dst, GR32:$src2))]>, 718 OpSize32, LOCK; 719 720def NAME#64mr : RI<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4}, 721 RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 }, 722 MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2), 723 !strconcat(mnemonic, "{q}\t", 724 "{$src2, $dst|$dst, $src2}"), 725 [(set EFLAGS, (Op addr:$dst, GR64:$src2))]>, LOCK; 726 727def NAME#8mi : Ii8<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4}, 728 ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 0 }, 729 ImmMod, (outs), (ins i8mem :$dst, i8imm :$src2), 730 !strconcat(mnemonic, "{b}\t", 731 "{$src2, $dst|$dst, $src2}"), 732 [(set EFLAGS, (Op addr:$dst, (i8 imm:$src2)))]>, LOCK; 733 734def NAME#16mi : Ii16<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4}, 735 ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 }, 736 ImmMod, (outs), (ins i16mem :$dst, i16imm :$src2), 737 !strconcat(mnemonic, "{w}\t", 738 "{$src2, $dst|$dst, $src2}"), 739 [(set EFLAGS, (Op addr:$dst, (i16 imm:$src2)))]>, 740 OpSize16, LOCK; 741 742def NAME#32mi : Ii32<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4}, 743 ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 }, 744 ImmMod, (outs), (ins i32mem :$dst, i32imm :$src2), 745 !strconcat(mnemonic, "{l}\t", 746 "{$src2, $dst|$dst, $src2}"), 747 [(set EFLAGS, (Op addr:$dst, (i32 imm:$src2)))]>, 748 OpSize32, LOCK; 749 750def NAME#64mi32 : RIi32S<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4}, 751 ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 }, 752 ImmMod, (outs), (ins i64mem :$dst, i64i32imm :$src2), 753 !strconcat(mnemonic, "{q}\t", 754 "{$src2, $dst|$dst, $src2}"), 755 [(set EFLAGS, (Op addr:$dst, i64immSExt32:$src2))]>, 756 LOCK; 757 758def NAME#16mi8 : Ii8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4}, 759 ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 }, 760 ImmMod, (outs), (ins i16mem :$dst, i16i8imm :$src2), 761 !strconcat(mnemonic, "{w}\t", 762 "{$src2, $dst|$dst, $src2}"), 763 [(set EFLAGS, (Op addr:$dst, i16immSExt8:$src2))]>, 764 OpSize16, LOCK; 765 766def NAME#32mi8 : Ii8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4}, 767 ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 }, 768 ImmMod, (outs), (ins i32mem :$dst, i32i8imm :$src2), 769 !strconcat(mnemonic, "{l}\t", 770 "{$src2, $dst|$dst, $src2}"), 771 [(set EFLAGS, (Op addr:$dst, i32immSExt8:$src2))]>, 772 OpSize32, LOCK; 773 774def NAME#64mi8 : RIi8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4}, 775 ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 }, 776 ImmMod, (outs), (ins i64mem :$dst, i64i8imm :$src2), 777 !strconcat(mnemonic, "{q}\t", 778 "{$src2, $dst|$dst, $src2}"), 779 [(set EFLAGS, (Op addr:$dst, i64immSExt8:$src2))]>, 780 LOCK; 781} 782 783} 784 785defm LOCK_ADD : LOCK_ArithBinOp<0x00, 0x80, 0x83, MRM0m, X86lock_add, "add">; 786defm LOCK_SUB : LOCK_ArithBinOp<0x28, 0x80, 0x83, MRM5m, X86lock_sub, "sub">; 787defm LOCK_OR : LOCK_ArithBinOp<0x08, 0x80, 0x83, MRM1m, X86lock_or , "or">; 788defm LOCK_AND : LOCK_ArithBinOp<0x20, 0x80, 0x83, MRM4m, X86lock_and, "and">; 789defm LOCK_XOR : LOCK_ArithBinOp<0x30, 0x80, 0x83, MRM6m, X86lock_xor, "xor">; 790 791multiclass LOCK_ArithUnOp<bits<8> Opc8, bits<8> Opc, Format Form, 792 string frag, string mnemonic> { 793let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1, 794 SchedRW = [WriteALURMW] in { 795def NAME#8m : I<Opc8, Form, (outs), (ins i8mem :$dst), 796 !strconcat(mnemonic, "{b}\t$dst"), 797 [(set EFLAGS, (!cast<PatFrag>(frag # "_8") addr:$dst))]>, 798 LOCK; 799def NAME#16m : I<Opc, Form, (outs), (ins i16mem:$dst), 800 !strconcat(mnemonic, "{w}\t$dst"), 801 [(set EFLAGS, (!cast<PatFrag>(frag # "_16") addr:$dst))]>, 802 OpSize16, LOCK; 803def NAME#32m : I<Opc, Form, (outs), (ins i32mem:$dst), 804 !strconcat(mnemonic, "{l}\t$dst"), 805 [(set EFLAGS, (!cast<PatFrag>(frag # "_32") addr:$dst))]>, 806 OpSize32, LOCK; 807def NAME#64m : RI<Opc, Form, (outs), (ins i64mem:$dst), 808 !strconcat(mnemonic, "{q}\t$dst"), 809 [(set EFLAGS, (!cast<PatFrag>(frag # "_64") addr:$dst))]>, 810 LOCK; 811} 812} 813 814multiclass unary_atomic_intrin<SDNode atomic_op> { 815 def _8 : PatFrag<(ops node:$ptr), 816 (atomic_op node:$ptr), [{ 817 return cast<MemIntrinsicSDNode>(N)->getMemoryVT() == MVT::i8; 818 }]>; 819 def _16 : PatFrag<(ops node:$ptr), 820 (atomic_op node:$ptr), [{ 821 return cast<MemIntrinsicSDNode>(N)->getMemoryVT() == MVT::i16; 822 }]>; 823 def _32 : PatFrag<(ops node:$ptr), 824 (atomic_op node:$ptr), [{ 825 return cast<MemIntrinsicSDNode>(N)->getMemoryVT() == MVT::i32; 826 }]>; 827 def _64 : PatFrag<(ops node:$ptr), 828 (atomic_op node:$ptr), [{ 829 return cast<MemIntrinsicSDNode>(N)->getMemoryVT() == MVT::i64; 830 }]>; 831} 832 833defm X86lock_inc : unary_atomic_intrin<X86lock_inc>; 834defm X86lock_dec : unary_atomic_intrin<X86lock_dec>; 835 836defm LOCK_INC : LOCK_ArithUnOp<0xFE, 0xFF, MRM0m, "X86lock_inc", "inc">; 837defm LOCK_DEC : LOCK_ArithUnOp<0xFE, 0xFF, MRM1m, "X86lock_dec", "dec">; 838 839// Atomic compare and swap. 840multiclass LCMPXCHG_UnOp<bits<8> Opc, Format Form, string mnemonic, 841 SDPatternOperator frag, X86MemOperand x86memop> { 842let isCodeGenOnly = 1, usesCustomInserter = 1 in { 843 def NAME : I<Opc, Form, (outs), (ins x86memop:$ptr), 844 !strconcat(mnemonic, "\t$ptr"), 845 [(frag addr:$ptr)]>, TB, LOCK; 846} 847} 848 849multiclass LCMPXCHG_BinOp<bits<8> Opc8, bits<8> Opc, Format Form, 850 string mnemonic, SDPatternOperator frag> { 851let isCodeGenOnly = 1, SchedRW = [WriteCMPXCHGRMW] in { 852 let Defs = [AL, EFLAGS], Uses = [AL] in 853 def NAME#8 : I<Opc8, Form, (outs), (ins i8mem:$ptr, GR8:$swap), 854 !strconcat(mnemonic, "{b}\t{$swap, $ptr|$ptr, $swap}"), 855 [(frag addr:$ptr, GR8:$swap, 1)]>, TB, LOCK; 856 let Defs = [AX, EFLAGS], Uses = [AX] in 857 def NAME#16 : I<Opc, Form, (outs), (ins i16mem:$ptr, GR16:$swap), 858 !strconcat(mnemonic, "{w}\t{$swap, $ptr|$ptr, $swap}"), 859 [(frag addr:$ptr, GR16:$swap, 2)]>, TB, OpSize16, LOCK; 860 let Defs = [EAX, EFLAGS], Uses = [EAX] in 861 def NAME#32 : I<Opc, Form, (outs), (ins i32mem:$ptr, GR32:$swap), 862 !strconcat(mnemonic, "{l}\t{$swap, $ptr|$ptr, $swap}"), 863 [(frag addr:$ptr, GR32:$swap, 4)]>, TB, OpSize32, LOCK; 864 let Defs = [RAX, EFLAGS], Uses = [RAX] in 865 def NAME#64 : RI<Opc, Form, (outs), (ins i64mem:$ptr, GR64:$swap), 866 !strconcat(mnemonic, "{q}\t{$swap, $ptr|$ptr, $swap}"), 867 [(frag addr:$ptr, GR64:$swap, 8)]>, TB, LOCK; 868} 869} 870 871let Defs = [EAX, EDX, EFLAGS], Uses = [EAX, EBX, ECX, EDX], 872 SchedRW = [WriteCMPXCHGRMW] in { 873defm LCMPXCHG8B : LCMPXCHG_UnOp<0xC7, MRM1m, "cmpxchg8b", X86cas8, i64mem>; 874} 875 876// This pseudo must be used when the frame uses RBX as 877// the base pointer. Indeed, in such situation RBX is a reserved 878// register and the register allocator will ignore any use/def of 879// it. In other words, the register will not fix the clobbering of 880// RBX that will happen when setting the arguments for the instrucion. 881// 882// Unlike the actual related instuction, we mark that this one 883// defines EBX (instead of using EBX). 884// The rationale is that we will define RBX during the expansion of 885// the pseudo. The argument feeding EBX is ebx_input. 886// 887// The additional argument, $ebx_save, is a temporary register used to 888// save the value of RBX across the actual instruction. 889// 890// To make sure the register assigned to $ebx_save does not interfere with 891// the definition of the actual instruction, we use a definition $dst which 892// is tied to $rbx_save. That way, the live-range of $rbx_save spans across 893// the instruction and we are sure we will have a valid register to restore 894// the value of RBX. 895let Defs = [EAX, EDX, EBX, EFLAGS], Uses = [EAX, ECX, EDX], 896 SchedRW = [WriteCMPXCHGRMW], isCodeGenOnly = 1, isPseudo = 1, 897 Constraints = "$ebx_save = $dst", usesCustomInserter = 1 in { 898def LCMPXCHG8B_SAVE_EBX : 899 I<0, Pseudo, (outs GR32:$dst), 900 (ins i64mem:$ptr, GR32:$ebx_input, GR32:$ebx_save), 901 !strconcat("cmpxchg8b", "\t$ptr"), 902 [(set GR32:$dst, (X86cas8save_ebx addr:$ptr, GR32:$ebx_input, 903 GR32:$ebx_save))]>; 904} 905 906 907let Defs = [RAX, RDX, EFLAGS], Uses = [RAX, RBX, RCX, RDX], 908 Predicates = [HasCmpxchg16b], SchedRW = [WriteCMPXCHGRMW] in { 909defm LCMPXCHG16B : LCMPXCHG_UnOp<0xC7, MRM1m, "cmpxchg16b", 910 X86cas16, i128mem>, REX_W; 911} 912 913// Same as LCMPXCHG8B_SAVE_RBX but for the 16 Bytes variant. 914let Defs = [RAX, RDX, RBX, EFLAGS], Uses = [RAX, RCX, RDX], 915 Predicates = [HasCmpxchg16b], SchedRW = [WriteCMPXCHGRMW], 916 isCodeGenOnly = 1, isPseudo = 1, Constraints = "$rbx_save = $dst", 917 usesCustomInserter = 1 in { 918def LCMPXCHG16B_SAVE_RBX : 919 I<0, Pseudo, (outs GR64:$dst), 920 (ins i128mem:$ptr, GR64:$rbx_input, GR64:$rbx_save), 921 !strconcat("cmpxchg16b", "\t$ptr"), 922 [(set GR64:$dst, (X86cas16save_rbx addr:$ptr, GR64:$rbx_input, 923 GR64:$rbx_save))]>; 924} 925 926defm LCMPXCHG : LCMPXCHG_BinOp<0xB0, 0xB1, MRMDestMem, "cmpxchg", X86cas>; 927 928// Atomic exchange and add 929multiclass ATOMIC_LOAD_BINOP<bits<8> opc8, bits<8> opc, string mnemonic, 930 string frag> { 931 let Constraints = "$val = $dst", Defs = [EFLAGS], isCodeGenOnly = 1, 932 SchedRW = [WriteALURMW] in { 933 def NAME#8 : I<opc8, MRMSrcMem, (outs GR8:$dst), 934 (ins GR8:$val, i8mem:$ptr), 935 !strconcat(mnemonic, "{b}\t{$val, $ptr|$ptr, $val}"), 936 [(set GR8:$dst, 937 (!cast<PatFrag>(frag # "_8") addr:$ptr, GR8:$val))]>; 938 def NAME#16 : I<opc, MRMSrcMem, (outs GR16:$dst), 939 (ins GR16:$val, i16mem:$ptr), 940 !strconcat(mnemonic, "{w}\t{$val, $ptr|$ptr, $val}"), 941 [(set 942 GR16:$dst, 943 (!cast<PatFrag>(frag # "_16") addr:$ptr, GR16:$val))]>, 944 OpSize16; 945 def NAME#32 : I<opc, MRMSrcMem, (outs GR32:$dst), 946 (ins GR32:$val, i32mem:$ptr), 947 !strconcat(mnemonic, "{l}\t{$val, $ptr|$ptr, $val}"), 948 [(set 949 GR32:$dst, 950 (!cast<PatFrag>(frag # "_32") addr:$ptr, GR32:$val))]>, 951 OpSize32; 952 def NAME#64 : RI<opc, MRMSrcMem, (outs GR64:$dst), 953 (ins GR64:$val, i64mem:$ptr), 954 !strconcat(mnemonic, "{q}\t{$val, $ptr|$ptr, $val}"), 955 [(set 956 GR64:$dst, 957 (!cast<PatFrag>(frag # "_64") addr:$ptr, GR64:$val))]>; 958 } 959} 960 961defm LXADD : ATOMIC_LOAD_BINOP<0xc0, 0xc1, "xadd", "atomic_load_add">, TB, LOCK; 962 963/* The following multiclass tries to make sure that in code like 964 * x.store (immediate op x.load(acquire), release) 965 * and 966 * x.store (register op x.load(acquire), release) 967 * an operation directly on memory is generated instead of wasting a register. 968 * It is not automatic as atomic_store/load are only lowered to MOV instructions 969 * extremely late to prevent them from being accidentally reordered in the backend 970 * (see below the RELEASE_MOV* / ACQUIRE_MOV* pseudo-instructions) 971 */ 972multiclass RELEASE_BINOP_MI<string Name, SDNode op> { 973 def : Pat<(atomic_store_8 addr:$dst, 974 (op (atomic_load_8 addr:$dst), (i8 imm:$src))), 975 (!cast<Instruction>(Name#"8mi") addr:$dst, imm:$src)>; 976 def : Pat<(atomic_store_16 addr:$dst, 977 (op (atomic_load_16 addr:$dst), (i16 imm:$src))), 978 (!cast<Instruction>(Name#"16mi") addr:$dst, imm:$src)>; 979 def : Pat<(atomic_store_32 addr:$dst, 980 (op (atomic_load_32 addr:$dst), (i32 imm:$src))), 981 (!cast<Instruction>(Name#"32mi") addr:$dst, imm:$src)>; 982 def : Pat<(atomic_store_64 addr:$dst, 983 (op (atomic_load_64 addr:$dst), (i64immSExt32:$src))), 984 (!cast<Instruction>(Name#"64mi32") addr:$dst, (i64immSExt32:$src))>; 985 986 def : Pat<(atomic_store_8 addr:$dst, 987 (op (atomic_load_8 addr:$dst), (i8 GR8:$src))), 988 (!cast<Instruction>(Name#"8mr") addr:$dst, GR8:$src)>; 989 def : Pat<(atomic_store_16 addr:$dst, 990 (op (atomic_load_16 addr:$dst), (i16 GR16:$src))), 991 (!cast<Instruction>(Name#"16mr") addr:$dst, GR16:$src)>; 992 def : Pat<(atomic_store_32 addr:$dst, 993 (op (atomic_load_32 addr:$dst), (i32 GR32:$src))), 994 (!cast<Instruction>(Name#"32mr") addr:$dst, GR32:$src)>; 995 def : Pat<(atomic_store_64 addr:$dst, 996 (op (atomic_load_64 addr:$dst), (i64 GR64:$src))), 997 (!cast<Instruction>(Name#"64mr") addr:$dst, GR64:$src)>; 998} 999defm : RELEASE_BINOP_MI<"ADD", add>; 1000defm : RELEASE_BINOP_MI<"AND", and>; 1001defm : RELEASE_BINOP_MI<"OR", or>; 1002defm : RELEASE_BINOP_MI<"XOR", xor>; 1003defm : RELEASE_BINOP_MI<"SUB", sub>; 1004 1005// Same as above, but for floating-point. 1006// FIXME: imm version. 1007// FIXME: Version that doesn't clobber $src, using AVX's VADDSS. 1008// FIXME: This could also handle SIMD operations with *ps and *pd instructions. 1009let usesCustomInserter = 1, SchedRW = [WriteMicrocoded] in { 1010multiclass RELEASE_FP_BINOP_MI<SDNode op> { 1011 def NAME#32mr : I<0, Pseudo, (outs), (ins i32mem:$dst, FR32:$src), 1012 "#BINOP "#NAME#"32mr PSEUDO!", 1013 [(atomic_store_32 addr:$dst, 1014 (i32 (bitconvert (op 1015 (f32 (bitconvert (i32 (atomic_load_32 addr:$dst)))), 1016 FR32:$src))))]>, Requires<[HasSSE1]>; 1017 def NAME#64mr : I<0, Pseudo, (outs), (ins i64mem:$dst, FR64:$src), 1018 "#BINOP "#NAME#"64mr PSEUDO!", 1019 [(atomic_store_64 addr:$dst, 1020 (i64 (bitconvert (op 1021 (f64 (bitconvert (i64 (atomic_load_64 addr:$dst)))), 1022 FR64:$src))))]>, Requires<[HasSSE2]>; 1023} 1024defm RELEASE_FADD : RELEASE_FP_BINOP_MI<fadd>; 1025// FIXME: Add fsub, fmul, fdiv, ... 1026} 1027 1028multiclass RELEASE_UNOP<string Name, dag dag8, dag dag16, dag dag32, 1029 dag dag64> { 1030 def : Pat<(atomic_store_8 addr:$dst, dag8), 1031 (!cast<Instruction>(Name#8m) addr:$dst)>; 1032 def : Pat<(atomic_store_16 addr:$dst, dag16), 1033 (!cast<Instruction>(Name#16m) addr:$dst)>; 1034 def : Pat<(atomic_store_32 addr:$dst, dag32), 1035 (!cast<Instruction>(Name#32m) addr:$dst)>; 1036 def : Pat<(atomic_store_64 addr:$dst, dag64), 1037 (!cast<Instruction>(Name#64m) addr:$dst)>; 1038} 1039 1040let Predicates = [UseIncDec] in { 1041 defm : RELEASE_UNOP<"INC", 1042 (add (atomic_load_8 addr:$dst), (i8 1)), 1043 (add (atomic_load_16 addr:$dst), (i16 1)), 1044 (add (atomic_load_32 addr:$dst), (i32 1)), 1045 (add (atomic_load_64 addr:$dst), (i64 1))>; 1046 defm : RELEASE_UNOP<"DEC", 1047 (add (atomic_load_8 addr:$dst), (i8 -1)), 1048 (add (atomic_load_16 addr:$dst), (i16 -1)), 1049 (add (atomic_load_32 addr:$dst), (i32 -1)), 1050 (add (atomic_load_64 addr:$dst), (i64 -1))>; 1051} 1052 1053defm : RELEASE_UNOP<"NEG", 1054 (ineg (i8 (atomic_load_8 addr:$dst))), 1055 (ineg (i16 (atomic_load_16 addr:$dst))), 1056 (ineg (i32 (atomic_load_32 addr:$dst))), 1057 (ineg (i64 (atomic_load_64 addr:$dst)))>; 1058defm : RELEASE_UNOP<"NOT", 1059 (not (i8 (atomic_load_8 addr:$dst))), 1060 (not (i16 (atomic_load_16 addr:$dst))), 1061 (not (i32 (atomic_load_32 addr:$dst))), 1062 (not (i64 (atomic_load_64 addr:$dst)))>; 1063 1064def : Pat<(atomic_store_8 addr:$dst, (i8 imm:$src)), 1065 (MOV8mi addr:$dst, imm:$src)>; 1066def : Pat<(atomic_store_16 addr:$dst, (i16 imm:$src)), 1067 (MOV16mi addr:$dst, imm:$src)>; 1068def : Pat<(atomic_store_32 addr:$dst, (i32 imm:$src)), 1069 (MOV32mi addr:$dst, imm:$src)>; 1070def : Pat<(atomic_store_64 addr:$dst, (i64immSExt32:$src)), 1071 (MOV64mi32 addr:$dst, i64immSExt32:$src)>; 1072 1073def : Pat<(atomic_store_8 addr:$dst, GR8:$src), 1074 (MOV8mr addr:$dst, GR8:$src)>; 1075def : Pat<(atomic_store_16 addr:$dst, GR16:$src), 1076 (MOV16mr addr:$dst, GR16:$src)>; 1077def : Pat<(atomic_store_32 addr:$dst, GR32:$src), 1078 (MOV32mr addr:$dst, GR32:$src)>; 1079def : Pat<(atomic_store_64 addr:$dst, GR64:$src), 1080 (MOV64mr addr:$dst, GR64:$src)>; 1081 1082def : Pat<(i8 (atomic_load_8 addr:$src)), (MOV8rm addr:$src)>; 1083def : Pat<(i16 (atomic_load_16 addr:$src)), (MOV16rm addr:$src)>; 1084def : Pat<(i32 (atomic_load_32 addr:$src)), (MOV32rm addr:$src)>; 1085def : Pat<(i64 (atomic_load_64 addr:$src)), (MOV64rm addr:$src)>; 1086 1087//===----------------------------------------------------------------------===// 1088// DAG Pattern Matching Rules 1089//===----------------------------------------------------------------------===// 1090 1091// Use AND/OR to store 0/-1 in memory when optimizing for minsize. This saves 1092// binary size compared to a regular MOV, but it introduces an unnecessary 1093// load, so is not suitable for regular or optsize functions. 1094let Predicates = [OptForMinSize] in { 1095def : Pat<(nonvolatile_store (i16 0), addr:$dst), (AND16mi8 addr:$dst, 0)>; 1096def : Pat<(nonvolatile_store (i32 0), addr:$dst), (AND32mi8 addr:$dst, 0)>; 1097def : Pat<(nonvolatile_store (i64 0), addr:$dst), (AND64mi8 addr:$dst, 0)>; 1098def : Pat<(nonvolatile_store (i16 -1), addr:$dst), (OR16mi8 addr:$dst, -1)>; 1099def : Pat<(nonvolatile_store (i32 -1), addr:$dst), (OR32mi8 addr:$dst, -1)>; 1100def : Pat<(nonvolatile_store (i64 -1), addr:$dst), (OR64mi8 addr:$dst, -1)>; 1101} 1102 1103// In kernel code model, we can get the address of a label 1104// into a register with 'movq'. FIXME: This is a hack, the 'imm' predicate of 1105// the MOV64ri32 should accept these. 1106def : Pat<(i64 (X86Wrapper tconstpool :$dst)), 1107 (MOV64ri32 tconstpool :$dst)>, Requires<[KernelCode]>; 1108def : Pat<(i64 (X86Wrapper tjumptable :$dst)), 1109 (MOV64ri32 tjumptable :$dst)>, Requires<[KernelCode]>; 1110def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)), 1111 (MOV64ri32 tglobaladdr :$dst)>, Requires<[KernelCode]>; 1112def : Pat<(i64 (X86Wrapper texternalsym:$dst)), 1113 (MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>; 1114def : Pat<(i64 (X86Wrapper mcsym:$dst)), 1115 (MOV64ri32 mcsym:$dst)>, Requires<[KernelCode]>; 1116def : Pat<(i64 (X86Wrapper tblockaddress:$dst)), 1117 (MOV64ri32 tblockaddress:$dst)>, Requires<[KernelCode]>; 1118 1119// If we have small model and -static mode, it is safe to store global addresses 1120// directly as immediates. FIXME: This is really a hack, the 'imm' predicate 1121// for MOV64mi32 should handle this sort of thing. 1122def : Pat<(store (i64 (X86Wrapper tconstpool:$src)), addr:$dst), 1123 (MOV64mi32 addr:$dst, tconstpool:$src)>, 1124 Requires<[NearData, IsNotPIC]>; 1125def : Pat<(store (i64 (X86Wrapper tjumptable:$src)), addr:$dst), 1126 (MOV64mi32 addr:$dst, tjumptable:$src)>, 1127 Requires<[NearData, IsNotPIC]>; 1128def : Pat<(store (i64 (X86Wrapper tglobaladdr:$src)), addr:$dst), 1129 (MOV64mi32 addr:$dst, tglobaladdr:$src)>, 1130 Requires<[NearData, IsNotPIC]>; 1131def : Pat<(store (i64 (X86Wrapper texternalsym:$src)), addr:$dst), 1132 (MOV64mi32 addr:$dst, texternalsym:$src)>, 1133 Requires<[NearData, IsNotPIC]>; 1134def : Pat<(store (i64 (X86Wrapper mcsym:$src)), addr:$dst), 1135 (MOV64mi32 addr:$dst, mcsym:$src)>, 1136 Requires<[NearData, IsNotPIC]>; 1137def : Pat<(store (i64 (X86Wrapper tblockaddress:$src)), addr:$dst), 1138 (MOV64mi32 addr:$dst, tblockaddress:$src)>, 1139 Requires<[NearData, IsNotPIC]>; 1140 1141def : Pat<(i32 (X86RecoverFrameAlloc mcsym:$dst)), (MOV32ri mcsym:$dst)>; 1142def : Pat<(i64 (X86RecoverFrameAlloc mcsym:$dst)), (MOV64ri mcsym:$dst)>; 1143 1144// Calls 1145 1146// tls has some funny stuff here... 1147// This corresponds to movabs $foo@tpoff, %rax 1148def : Pat<(i64 (X86Wrapper tglobaltlsaddr :$dst)), 1149 (MOV64ri32 tglobaltlsaddr :$dst)>; 1150// This corresponds to add $foo@tpoff, %rax 1151def : Pat<(add GR64:$src1, (X86Wrapper tglobaltlsaddr :$dst)), 1152 (ADD64ri32 GR64:$src1, tglobaltlsaddr :$dst)>; 1153 1154 1155// Direct PC relative function call for small code model. 32-bit displacement 1156// sign extended to 64-bit. 1157def : Pat<(X86call (i64 tglobaladdr:$dst)), 1158 (CALL64pcrel32 tglobaladdr:$dst)>; 1159def : Pat<(X86call (i64 texternalsym:$dst)), 1160 (CALL64pcrel32 texternalsym:$dst)>; 1161 1162// Tailcall stuff. The TCRETURN instructions execute after the epilog, so they 1163// can never use callee-saved registers. That is the purpose of the GR64_TC 1164// register classes. 1165// 1166// The only volatile register that is never used by the calling convention is 1167// %r11. This happens when calling a vararg function with 6 arguments. 1168// 1169// Match an X86tcret that uses less than 7 volatile registers. 1170def X86tcret_6regs : PatFrag<(ops node:$ptr, node:$off), 1171 (X86tcret node:$ptr, node:$off), [{ 1172 // X86tcret args: (*chain, ptr, imm, regs..., glue) 1173 unsigned NumRegs = 0; 1174 for (unsigned i = 3, e = N->getNumOperands(); i != e; ++i) 1175 if (isa<RegisterSDNode>(N->getOperand(i)) && ++NumRegs > 6) 1176 return false; 1177 return true; 1178}]>; 1179 1180def : Pat<(X86tcret ptr_rc_tailcall:$dst, imm:$off), 1181 (TCRETURNri ptr_rc_tailcall:$dst, imm:$off)>, 1182 Requires<[Not64BitMode, NotUseRetpolineIndirectCalls]>; 1183 1184// FIXME: This is disabled for 32-bit PIC mode because the global base 1185// register which is part of the address mode may be assigned a 1186// callee-saved register. 1187def : Pat<(X86tcret (load addr:$dst), imm:$off), 1188 (TCRETURNmi addr:$dst, imm:$off)>, 1189 Requires<[Not64BitMode, IsNotPIC, NotUseRetpolineIndirectCalls]>; 1190 1191def : Pat<(X86tcret (i32 tglobaladdr:$dst), imm:$off), 1192 (TCRETURNdi tglobaladdr:$dst, imm:$off)>, 1193 Requires<[NotLP64]>; 1194 1195def : Pat<(X86tcret (i32 texternalsym:$dst), imm:$off), 1196 (TCRETURNdi texternalsym:$dst, imm:$off)>, 1197 Requires<[NotLP64]>; 1198 1199def : Pat<(X86tcret ptr_rc_tailcall:$dst, imm:$off), 1200 (TCRETURNri64 ptr_rc_tailcall:$dst, imm:$off)>, 1201 Requires<[In64BitMode, NotUseRetpolineIndirectCalls]>; 1202 1203// Don't fold loads into X86tcret requiring more than 6 regs. 1204// There wouldn't be enough scratch registers for base+index. 1205def : Pat<(X86tcret_6regs (load addr:$dst), imm:$off), 1206 (TCRETURNmi64 addr:$dst, imm:$off)>, 1207 Requires<[In64BitMode, NotUseRetpolineIndirectCalls]>; 1208 1209def : Pat<(X86tcret ptr_rc_tailcall:$dst, imm:$off), 1210 (RETPOLINE_TCRETURN64 ptr_rc_tailcall:$dst, imm:$off)>, 1211 Requires<[In64BitMode, UseRetpolineIndirectCalls]>; 1212 1213def : Pat<(X86tcret ptr_rc_tailcall:$dst, imm:$off), 1214 (RETPOLINE_TCRETURN32 ptr_rc_tailcall:$dst, imm:$off)>, 1215 Requires<[Not64BitMode, UseRetpolineIndirectCalls]>; 1216 1217def : Pat<(X86tcret (i64 tglobaladdr:$dst), imm:$off), 1218 (TCRETURNdi64 tglobaladdr:$dst, imm:$off)>, 1219 Requires<[IsLP64]>; 1220 1221def : Pat<(X86tcret (i64 texternalsym:$dst), imm:$off), 1222 (TCRETURNdi64 texternalsym:$dst, imm:$off)>, 1223 Requires<[IsLP64]>; 1224 1225// Normal calls, with various flavors of addresses. 1226def : Pat<(X86call (i32 tglobaladdr:$dst)), 1227 (CALLpcrel32 tglobaladdr:$dst)>; 1228def : Pat<(X86call (i32 texternalsym:$dst)), 1229 (CALLpcrel32 texternalsym:$dst)>; 1230def : Pat<(X86call (i32 imm:$dst)), 1231 (CALLpcrel32 imm:$dst)>, Requires<[CallImmAddr]>; 1232 1233// Comparisons. 1234 1235// TEST R,R is smaller than CMP R,0 1236def : Pat<(X86cmp GR8:$src1, 0), 1237 (TEST8rr GR8:$src1, GR8:$src1)>; 1238def : Pat<(X86cmp GR16:$src1, 0), 1239 (TEST16rr GR16:$src1, GR16:$src1)>; 1240def : Pat<(X86cmp GR32:$src1, 0), 1241 (TEST32rr GR32:$src1, GR32:$src1)>; 1242def : Pat<(X86cmp GR64:$src1, 0), 1243 (TEST64rr GR64:$src1, GR64:$src1)>; 1244 1245// Conditional moves with folded loads with operands swapped and conditions 1246// inverted. 1247multiclass CMOVmr<PatLeaf InvertedCond, Instruction Inst16, Instruction Inst32, 1248 Instruction Inst64> { 1249 let Predicates = [HasCMov] in { 1250 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, InvertedCond, EFLAGS), 1251 (Inst16 GR16:$src2, addr:$src1)>; 1252 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, InvertedCond, EFLAGS), 1253 (Inst32 GR32:$src2, addr:$src1)>; 1254 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, InvertedCond, EFLAGS), 1255 (Inst64 GR64:$src2, addr:$src1)>; 1256 } 1257} 1258 1259defm : CMOVmr<X86_COND_B , CMOVAE16rm, CMOVAE32rm, CMOVAE64rm>; 1260defm : CMOVmr<X86_COND_AE, CMOVB16rm , CMOVB32rm , CMOVB64rm>; 1261defm : CMOVmr<X86_COND_E , CMOVNE16rm, CMOVNE32rm, CMOVNE64rm>; 1262defm : CMOVmr<X86_COND_NE, CMOVE16rm , CMOVE32rm , CMOVE64rm>; 1263defm : CMOVmr<X86_COND_BE, CMOVA16rm , CMOVA32rm , CMOVA64rm>; 1264defm : CMOVmr<X86_COND_A , CMOVBE16rm, CMOVBE32rm, CMOVBE64rm>; 1265defm : CMOVmr<X86_COND_L , CMOVGE16rm, CMOVGE32rm, CMOVGE64rm>; 1266defm : CMOVmr<X86_COND_GE, CMOVL16rm , CMOVL32rm , CMOVL64rm>; 1267defm : CMOVmr<X86_COND_LE, CMOVG16rm , CMOVG32rm , CMOVG64rm>; 1268defm : CMOVmr<X86_COND_G , CMOVLE16rm, CMOVLE32rm, CMOVLE64rm>; 1269defm : CMOVmr<X86_COND_P , CMOVNP16rm, CMOVNP32rm, CMOVNP64rm>; 1270defm : CMOVmr<X86_COND_NP, CMOVP16rm , CMOVP32rm , CMOVP64rm>; 1271defm : CMOVmr<X86_COND_S , CMOVNS16rm, CMOVNS32rm, CMOVNS64rm>; 1272defm : CMOVmr<X86_COND_NS, CMOVS16rm , CMOVS32rm , CMOVS64rm>; 1273defm : CMOVmr<X86_COND_O , CMOVNO16rm, CMOVNO32rm, CMOVNO64rm>; 1274defm : CMOVmr<X86_COND_NO, CMOVO16rm , CMOVO32rm , CMOVO64rm>; 1275 1276// zextload bool -> zextload byte 1277// i1 stored in one byte in zero-extended form. 1278// Upper bits cleanup should be executed before Store. 1279def : Pat<(zextloadi8i1 addr:$src), (MOV8rm addr:$src)>; 1280def : Pat<(zextloadi16i1 addr:$src), (MOVZX16rm8 addr:$src)>; 1281def : Pat<(zextloadi32i1 addr:$src), (MOVZX32rm8 addr:$src)>; 1282def : Pat<(zextloadi64i1 addr:$src), 1283 (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>; 1284 1285// extload bool -> extload byte 1286// When extloading from 16-bit and smaller memory locations into 64-bit 1287// registers, use zero-extending loads so that the entire 64-bit register is 1288// defined, avoiding partial-register updates. 1289 1290def : Pat<(extloadi8i1 addr:$src), (MOV8rm addr:$src)>; 1291def : Pat<(extloadi16i1 addr:$src), (MOVZX16rm8 addr:$src)>; 1292def : Pat<(extloadi32i1 addr:$src), (MOVZX32rm8 addr:$src)>; 1293def : Pat<(extloadi16i8 addr:$src), (MOVZX16rm8 addr:$src)>; 1294def : Pat<(extloadi32i8 addr:$src), (MOVZX32rm8 addr:$src)>; 1295def : Pat<(extloadi32i16 addr:$src), (MOVZX32rm16 addr:$src)>; 1296 1297// For other extloads, use subregs, since the high contents of the register are 1298// defined after an extload. 1299def : Pat<(extloadi64i1 addr:$src), 1300 (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>; 1301def : Pat<(extloadi64i8 addr:$src), 1302 (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>; 1303def : Pat<(extloadi64i16 addr:$src), 1304 (SUBREG_TO_REG (i64 0), (MOVZX32rm16 addr:$src), sub_32bit)>; 1305def : Pat<(extloadi64i32 addr:$src), 1306 (SUBREG_TO_REG (i64 0), (MOV32rm addr:$src), sub_32bit)>; 1307 1308// anyext. Define these to do an explicit zero-extend to 1309// avoid partial-register updates. 1310def : Pat<(i16 (anyext GR8 :$src)), (EXTRACT_SUBREG 1311 (MOVZX32rr8 GR8 :$src), sub_16bit)>; 1312def : Pat<(i32 (anyext GR8 :$src)), (MOVZX32rr8 GR8 :$src)>; 1313 1314// Except for i16 -> i32 since isel expect i16 ops to be promoted to i32. 1315def : Pat<(i32 (anyext GR16:$src)), 1316 (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR16:$src, sub_16bit)>; 1317 1318def : Pat<(i64 (anyext GR8 :$src)), 1319 (SUBREG_TO_REG (i64 0), (MOVZX32rr8 GR8 :$src), sub_32bit)>; 1320def : Pat<(i64 (anyext GR16:$src)), 1321 (SUBREG_TO_REG (i64 0), (MOVZX32rr16 GR16 :$src), sub_32bit)>; 1322def : Pat<(i64 (anyext GR32:$src)), 1323 (INSERT_SUBREG (i64 (IMPLICIT_DEF)), GR32:$src, sub_32bit)>; 1324 1325 1326// Any instruction that defines a 32-bit result leaves the high half of the 1327// register. Truncate can be lowered to EXTRACT_SUBREG. CopyFromReg may 1328// be copying from a truncate. Any other 32-bit operation will zero-extend 1329// up to 64 bits. AssertSext/AssertZext aren't saying anything about the upper 1330// 32 bits, they're probably just qualifying a CopyFromReg. 1331def def32 : PatLeaf<(i32 GR32:$src), [{ 1332 return N->getOpcode() != ISD::TRUNCATE && 1333 N->getOpcode() != TargetOpcode::EXTRACT_SUBREG && 1334 N->getOpcode() != ISD::CopyFromReg && 1335 N->getOpcode() != ISD::AssertSext && 1336 N->getOpcode() != ISD::AssertZext; 1337}]>; 1338 1339// In the case of a 32-bit def that is known to implicitly zero-extend, 1340// we can use a SUBREG_TO_REG. 1341def : Pat<(i64 (zext def32:$src)), 1342 (SUBREG_TO_REG (i64 0), GR32:$src, sub_32bit)>; 1343 1344//===----------------------------------------------------------------------===// 1345// Pattern match OR as ADD 1346//===----------------------------------------------------------------------===// 1347 1348// If safe, we prefer to pattern match OR as ADD at isel time. ADD can be 1349// 3-addressified into an LEA instruction to avoid copies. However, we also 1350// want to finally emit these instructions as an or at the end of the code 1351// generator to make the generated code easier to read. To do this, we select 1352// into "disjoint bits" pseudo ops. 1353 1354// Treat an 'or' node is as an 'add' if the or'ed bits are known to be zero. 1355def or_is_add : PatFrag<(ops node:$lhs, node:$rhs), (or node:$lhs, node:$rhs),[{ 1356 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1))) 1357 return CurDAG->MaskedValueIsZero(N->getOperand(0), CN->getAPIntValue()); 1358 1359 KnownBits Known0; 1360 CurDAG->computeKnownBits(N->getOperand(0), Known0, 0); 1361 KnownBits Known1; 1362 CurDAG->computeKnownBits(N->getOperand(1), Known1, 0); 1363 return (~Known0.Zero & ~Known1.Zero) == 0; 1364}]>; 1365 1366 1367// (or x1, x2) -> (add x1, x2) if two operands are known not to share bits. 1368// Try this before the selecting to OR. 1369let SchedRW = [WriteALU] in { 1370 1371let isConvertibleToThreeAddress = 1, 1372 Constraints = "$src1 = $dst", Defs = [EFLAGS] in { 1373let isCommutable = 1 in { 1374def ADD16rr_DB : I<0, Pseudo, (outs GR16:$dst), (ins GR16:$src1, GR16:$src2), 1375 "", // orw/addw REG, REG 1376 [(set GR16:$dst, (or_is_add GR16:$src1, GR16:$src2))]>; 1377def ADD32rr_DB : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src1, GR32:$src2), 1378 "", // orl/addl REG, REG 1379 [(set GR32:$dst, (or_is_add GR32:$src1, GR32:$src2))]>; 1380def ADD64rr_DB : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2), 1381 "", // orq/addq REG, REG 1382 [(set GR64:$dst, (or_is_add GR64:$src1, GR64:$src2))]>; 1383} // isCommutable 1384 1385// NOTE: These are order specific, we want the ri8 forms to be listed 1386// first so that they are slightly preferred to the ri forms. 1387 1388def ADD16ri8_DB : I<0, Pseudo, 1389 (outs GR16:$dst), (ins GR16:$src1, i16i8imm:$src2), 1390 "", // orw/addw REG, imm8 1391 [(set GR16:$dst,(or_is_add GR16:$src1,i16immSExt8:$src2))]>; 1392def ADD16ri_DB : I<0, Pseudo, (outs GR16:$dst), (ins GR16:$src1, i16imm:$src2), 1393 "", // orw/addw REG, imm 1394 [(set GR16:$dst, (or_is_add GR16:$src1, imm:$src2))]>; 1395 1396def ADD32ri8_DB : I<0, Pseudo, 1397 (outs GR32:$dst), (ins GR32:$src1, i32i8imm:$src2), 1398 "", // orl/addl REG, imm8 1399 [(set GR32:$dst,(or_is_add GR32:$src1,i32immSExt8:$src2))]>; 1400def ADD32ri_DB : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src1, i32imm:$src2), 1401 "", // orl/addl REG, imm 1402 [(set GR32:$dst, (or_is_add GR32:$src1, imm:$src2))]>; 1403 1404 1405def ADD64ri8_DB : I<0, Pseudo, 1406 (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2), 1407 "", // orq/addq REG, imm8 1408 [(set GR64:$dst, (or_is_add GR64:$src1, 1409 i64immSExt8:$src2))]>; 1410def ADD64ri32_DB : I<0, Pseudo, 1411 (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2), 1412 "", // orq/addq REG, imm 1413 [(set GR64:$dst, (or_is_add GR64:$src1, 1414 i64immSExt32:$src2))]>; 1415} 1416} // AddedComplexity, SchedRW 1417 1418//===----------------------------------------------------------------------===// 1419// Pattern match SUB as XOR 1420//===----------------------------------------------------------------------===// 1421 1422// An immediate in the LHS of a subtract can't be encoded in the instruction. 1423// If there is no possibility of a borrow we can use an XOR instead of a SUB 1424// to enable the immediate to be folded. 1425// TODO: Move this to a DAG combine? 1426 1427def sub_is_xor : PatFrag<(ops node:$lhs, node:$rhs), (sub node:$lhs, node:$rhs),[{ 1428 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 1429 KnownBits Known; 1430 CurDAG->computeKnownBits(N->getOperand(1), Known); 1431 1432 // If all possible ones in the RHS are set in the LHS then there can't be 1433 // a borrow and we can use xor. 1434 return (~Known.Zero).isSubsetOf(CN->getAPIntValue()); 1435 } 1436 1437 return false; 1438}]>; 1439 1440let AddedComplexity = 5 in { 1441def : Pat<(sub_is_xor imm:$src2, GR8:$src1), 1442 (XOR8ri GR8:$src1, imm:$src2)>; 1443def : Pat<(sub_is_xor i16immSExt8:$src2, GR16:$src1), 1444 (XOR16ri8 GR16:$src1, i16immSExt8:$src2)>; 1445def : Pat<(sub_is_xor imm:$src2, GR16:$src1), 1446 (XOR16ri GR16:$src1, imm:$src2)>; 1447def : Pat<(sub_is_xor i32immSExt8:$src2, GR32:$src1), 1448 (XOR32ri8 GR32:$src1, i32immSExt8:$src2)>; 1449def : Pat<(sub_is_xor imm:$src2, GR32:$src1), 1450 (XOR32ri GR32:$src1, imm:$src2)>; 1451def : Pat<(sub_is_xor i64immSExt8:$src2, GR64:$src1), 1452 (XOR64ri8 GR64:$src1, i64immSExt8:$src2)>; 1453def : Pat<(sub_is_xor i64immSExt32:$src2, GR64:$src1), 1454 (XOR64ri32 GR64:$src1, i64immSExt32:$src2)>; 1455} 1456 1457//===----------------------------------------------------------------------===// 1458// Some peepholes 1459//===----------------------------------------------------------------------===// 1460 1461// Odd encoding trick: -128 fits into an 8-bit immediate field while 1462// +128 doesn't, so in this special case use a sub instead of an add. 1463def : Pat<(add GR16:$src1, 128), 1464 (SUB16ri8 GR16:$src1, -128)>; 1465def : Pat<(store (add (loadi16 addr:$dst), 128), addr:$dst), 1466 (SUB16mi8 addr:$dst, -128)>; 1467 1468def : Pat<(add GR32:$src1, 128), 1469 (SUB32ri8 GR32:$src1, -128)>; 1470def : Pat<(store (add (loadi32 addr:$dst), 128), addr:$dst), 1471 (SUB32mi8 addr:$dst, -128)>; 1472 1473def : Pat<(add GR64:$src1, 128), 1474 (SUB64ri8 GR64:$src1, -128)>; 1475def : Pat<(store (add (loadi64 addr:$dst), 128), addr:$dst), 1476 (SUB64mi8 addr:$dst, -128)>; 1477 1478// The same trick applies for 32-bit immediate fields in 64-bit 1479// instructions. 1480def : Pat<(add GR64:$src1, 0x0000000080000000), 1481 (SUB64ri32 GR64:$src1, 0xffffffff80000000)>; 1482def : Pat<(store (add (loadi64 addr:$dst), 0x0000000080000000), addr:$dst), 1483 (SUB64mi32 addr:$dst, 0xffffffff80000000)>; 1484 1485// To avoid needing to materialize an immediate in a register, use a 32-bit and 1486// with implicit zero-extension instead of a 64-bit and if the immediate has at 1487// least 32 bits of leading zeros. If in addition the last 32 bits can be 1488// represented with a sign extension of a 8 bit constant, use that. 1489// This can also reduce instruction size by eliminating the need for the REX 1490// prefix. 1491 1492// AddedComplexity is needed to give priority over i64immSExt8 and i64immSExt32. 1493let AddedComplexity = 1 in { 1494def : Pat<(and GR64:$src, i64immZExt32SExt8:$imm), 1495 (SUBREG_TO_REG 1496 (i64 0), 1497 (AND32ri8 1498 (EXTRACT_SUBREG GR64:$src, sub_32bit), 1499 (i32 (GetLo8XForm imm:$imm))), 1500 sub_32bit)>; 1501 1502def : Pat<(and GR64:$src, i64immZExt32:$imm), 1503 (SUBREG_TO_REG 1504 (i64 0), 1505 (AND32ri 1506 (EXTRACT_SUBREG GR64:$src, sub_32bit), 1507 (i32 (GetLo32XForm imm:$imm))), 1508 sub_32bit)>; 1509} // AddedComplexity = 1 1510 1511 1512// AddedComplexity is needed due to the increased complexity on the 1513// i64immZExt32SExt8 and i64immZExt32 patterns above. Applying this to all 1514// the MOVZX patterns keeps thems together in DAGIsel tables. 1515let AddedComplexity = 1 in { 1516// r & (2^16-1) ==> movz 1517def : Pat<(and GR32:$src1, 0xffff), 1518 (MOVZX32rr16 (EXTRACT_SUBREG GR32:$src1, sub_16bit))>; 1519// r & (2^8-1) ==> movz 1520def : Pat<(and GR32:$src1, 0xff), 1521 (MOVZX32rr8 (EXTRACT_SUBREG GR32:$src1, sub_8bit))>; 1522// r & (2^8-1) ==> movz 1523def : Pat<(and GR16:$src1, 0xff), 1524 (EXTRACT_SUBREG (MOVZX32rr8 (EXTRACT_SUBREG GR16:$src1, sub_8bit)), 1525 sub_16bit)>; 1526 1527// r & (2^32-1) ==> movz 1528def : Pat<(and GR64:$src, 0x00000000FFFFFFFF), 1529 (SUBREG_TO_REG (i64 0), 1530 (MOV32rr (EXTRACT_SUBREG GR64:$src, sub_32bit)), 1531 sub_32bit)>; 1532// r & (2^16-1) ==> movz 1533def : Pat<(and GR64:$src, 0xffff), 1534 (SUBREG_TO_REG (i64 0), 1535 (MOVZX32rr16 (i16 (EXTRACT_SUBREG GR64:$src, sub_16bit))), 1536 sub_32bit)>; 1537// r & (2^8-1) ==> movz 1538def : Pat<(and GR64:$src, 0xff), 1539 (SUBREG_TO_REG (i64 0), 1540 (MOVZX32rr8 (i8 (EXTRACT_SUBREG GR64:$src, sub_8bit))), 1541 sub_32bit)>; 1542} // AddedComplexity = 1 1543 1544 1545// Try to use BTS/BTR/BTC for single bit operations on the upper 32-bits. 1546 1547def BTRXForm : SDNodeXForm<imm, [{ 1548 // Transformation function: Find the lowest 0. 1549 return getI64Imm((uint8_t)N->getAPIntValue().countTrailingOnes(), SDLoc(N)); 1550}]>; 1551 1552def BTCBTSXForm : SDNodeXForm<imm, [{ 1553 // Transformation function: Find the lowest 1. 1554 return getI64Imm((uint8_t)N->getAPIntValue().countTrailingZeros(), SDLoc(N)); 1555}]>; 1556 1557def BTRMask64 : ImmLeaf<i64, [{ 1558 return !isUInt<32>(Imm) && !isInt<32>(Imm) && isPowerOf2_64(~Imm); 1559}]>; 1560 1561def BTCBTSMask64 : ImmLeaf<i64, [{ 1562 return !isInt<32>(Imm) && isPowerOf2_64(Imm); 1563}]>; 1564 1565// For now only do this for optsize. 1566let AddedComplexity = 1, Predicates=[OptForSize] in { 1567 def : Pat<(and GR64:$src1, BTRMask64:$mask), 1568 (BTR64ri8 GR64:$src1, (BTRXForm imm:$mask))>; 1569 def : Pat<(or GR64:$src1, BTCBTSMask64:$mask), 1570 (BTS64ri8 GR64:$src1, (BTCBTSXForm imm:$mask))>; 1571 def : Pat<(xor GR64:$src1, BTCBTSMask64:$mask), 1572 (BTC64ri8 GR64:$src1, (BTCBTSXForm imm:$mask))>; 1573} 1574 1575 1576// sext_inreg patterns 1577def : Pat<(sext_inreg GR32:$src, i16), 1578 (MOVSX32rr16 (EXTRACT_SUBREG GR32:$src, sub_16bit))>; 1579def : Pat<(sext_inreg GR32:$src, i8), 1580 (MOVSX32rr8 (EXTRACT_SUBREG GR32:$src, sub_8bit))>; 1581 1582def : Pat<(sext_inreg GR16:$src, i8), 1583 (EXTRACT_SUBREG (MOVSX32rr8 (EXTRACT_SUBREG GR16:$src, sub_8bit)), 1584 sub_16bit)>; 1585 1586def : Pat<(sext_inreg GR64:$src, i32), 1587 (MOVSX64rr32 (EXTRACT_SUBREG GR64:$src, sub_32bit))>; 1588def : Pat<(sext_inreg GR64:$src, i16), 1589 (MOVSX64rr16 (EXTRACT_SUBREG GR64:$src, sub_16bit))>; 1590def : Pat<(sext_inreg GR64:$src, i8), 1591 (MOVSX64rr8 (EXTRACT_SUBREG GR64:$src, sub_8bit))>; 1592 1593// sext, sext_load, zext, zext_load 1594def: Pat<(i16 (sext GR8:$src)), 1595 (EXTRACT_SUBREG (MOVSX32rr8 GR8:$src), sub_16bit)>; 1596def: Pat<(sextloadi16i8 addr:$src), 1597 (EXTRACT_SUBREG (MOVSX32rm8 addr:$src), sub_16bit)>; 1598def: Pat<(i16 (zext GR8:$src)), 1599 (EXTRACT_SUBREG (MOVZX32rr8 GR8:$src), sub_16bit)>; 1600def: Pat<(zextloadi16i8 addr:$src), 1601 (EXTRACT_SUBREG (MOVZX32rm8 addr:$src), sub_16bit)>; 1602 1603// trunc patterns 1604def : Pat<(i16 (trunc GR32:$src)), 1605 (EXTRACT_SUBREG GR32:$src, sub_16bit)>; 1606def : Pat<(i8 (trunc GR32:$src)), 1607 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)), 1608 sub_8bit)>, 1609 Requires<[Not64BitMode]>; 1610def : Pat<(i8 (trunc GR16:$src)), 1611 (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)), 1612 sub_8bit)>, 1613 Requires<[Not64BitMode]>; 1614def : Pat<(i32 (trunc GR64:$src)), 1615 (EXTRACT_SUBREG GR64:$src, sub_32bit)>; 1616def : Pat<(i16 (trunc GR64:$src)), 1617 (EXTRACT_SUBREG GR64:$src, sub_16bit)>; 1618def : Pat<(i8 (trunc GR64:$src)), 1619 (EXTRACT_SUBREG GR64:$src, sub_8bit)>; 1620def : Pat<(i8 (trunc GR32:$src)), 1621 (EXTRACT_SUBREG GR32:$src, sub_8bit)>, 1622 Requires<[In64BitMode]>; 1623def : Pat<(i8 (trunc GR16:$src)), 1624 (EXTRACT_SUBREG GR16:$src, sub_8bit)>, 1625 Requires<[In64BitMode]>; 1626 1627def immff00_ffff : ImmLeaf<i32, [{ 1628 return Imm >= 0xff00 && Imm <= 0xffff; 1629}]>; 1630 1631// h-register tricks 1632def : Pat<(i8 (trunc (srl_su GR16:$src, (i8 8)))), 1633 (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)>, 1634 Requires<[Not64BitMode]>; 1635def : Pat<(i8 (trunc (srl_su (i32 (anyext GR16:$src)), (i8 8)))), 1636 (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)>, 1637 Requires<[Not64BitMode]>; 1638def : Pat<(i8 (trunc (srl_su GR32:$src, (i8 8)))), 1639 (EXTRACT_SUBREG GR32:$src, sub_8bit_hi)>, 1640 Requires<[Not64BitMode]>; 1641def : Pat<(srl GR16:$src, (i8 8)), 1642 (EXTRACT_SUBREG 1643 (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)), 1644 sub_16bit)>; 1645def : Pat<(i32 (zext (srl_su GR16:$src, (i8 8)))), 1646 (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>; 1647def : Pat<(i32 (anyext (srl_su GR16:$src, (i8 8)))), 1648 (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>; 1649def : Pat<(and (srl_su GR32:$src, (i8 8)), (i32 255)), 1650 (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>; 1651def : Pat<(srl (and_su GR32:$src, immff00_ffff), (i8 8)), 1652 (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>; 1653 1654// h-register tricks. 1655// For now, be conservative on x86-64 and use an h-register extract only if the 1656// value is immediately zero-extended or stored, which are somewhat common 1657// cases. This uses a bunch of code to prevent a register requiring a REX prefix 1658// from being allocated in the same instruction as the h register, as there's 1659// currently no way to describe this requirement to the register allocator. 1660 1661// h-register extract and zero-extend. 1662def : Pat<(and (srl_su GR64:$src, (i8 8)), (i64 255)), 1663 (SUBREG_TO_REG 1664 (i64 0), 1665 (MOVZX32rr8_NOREX 1666 (EXTRACT_SUBREG GR64:$src, sub_8bit_hi)), 1667 sub_32bit)>; 1668def : Pat<(i64 (zext (srl_su GR16:$src, (i8 8)))), 1669 (SUBREG_TO_REG 1670 (i64 0), 1671 (MOVZX32rr8_NOREX 1672 (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)), 1673 sub_32bit)>; 1674def : Pat<(i64 (anyext (srl_su GR16:$src, (i8 8)))), 1675 (SUBREG_TO_REG 1676 (i64 0), 1677 (MOVZX32rr8_NOREX 1678 (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)), 1679 sub_32bit)>; 1680 1681// h-register extract and store. 1682def : Pat<(store (i8 (trunc_su (srl_su GR64:$src, (i8 8)))), addr:$dst), 1683 (MOV8mr_NOREX 1684 addr:$dst, 1685 (EXTRACT_SUBREG GR64:$src, sub_8bit_hi))>; 1686def : Pat<(store (i8 (trunc_su (srl_su GR32:$src, (i8 8)))), addr:$dst), 1687 (MOV8mr_NOREX 1688 addr:$dst, 1689 (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>, 1690 Requires<[In64BitMode]>; 1691def : Pat<(store (i8 (trunc_su (srl_su GR16:$src, (i8 8)))), addr:$dst), 1692 (MOV8mr_NOREX 1693 addr:$dst, 1694 (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>, 1695 Requires<[In64BitMode]>; 1696 1697 1698// (shl x, 1) ==> (add x, x) 1699// Note that if x is undef (immediate or otherwise), we could theoretically 1700// end up with the two uses of x getting different values, producing a result 1701// where the least significant bit is not 0. However, the probability of this 1702// happening is considered low enough that this is officially not a 1703// "real problem". 1704def : Pat<(shl GR8 :$src1, (i8 1)), (ADD8rr GR8 :$src1, GR8 :$src1)>; 1705def : Pat<(shl GR16:$src1, (i8 1)), (ADD16rr GR16:$src1, GR16:$src1)>; 1706def : Pat<(shl GR32:$src1, (i8 1)), (ADD32rr GR32:$src1, GR32:$src1)>; 1707def : Pat<(shl GR64:$src1, (i8 1)), (ADD64rr GR64:$src1, GR64:$src1)>; 1708 1709// Helper imms to check if a mask doesn't change significant shift/rotate bits. 1710def immShift8 : ImmLeaf<i8, [{ 1711 return countTrailingOnes<uint64_t>(Imm) >= 3; 1712}]>; 1713def immShift16 : ImmLeaf<i8, [{ 1714 return countTrailingOnes<uint64_t>(Imm) >= 4; 1715}]>; 1716def immShift32 : ImmLeaf<i8, [{ 1717 return countTrailingOnes<uint64_t>(Imm) >= 5; 1718}]>; 1719def immShift64 : ImmLeaf<i8, [{ 1720 return countTrailingOnes<uint64_t>(Imm) >= 6; 1721}]>; 1722 1723// Shift amount is implicitly masked. 1724multiclass MaskedShiftAmountPats<SDNode frag, string name> { 1725 // (shift x (and y, 31)) ==> (shift x, y) 1726 def : Pat<(frag GR8:$src1, (and CL, immShift32)), 1727 (!cast<Instruction>(name # "8rCL") GR8:$src1)>; 1728 def : Pat<(frag GR16:$src1, (and CL, immShift32)), 1729 (!cast<Instruction>(name # "16rCL") GR16:$src1)>; 1730 def : Pat<(frag GR32:$src1, (and CL, immShift32)), 1731 (!cast<Instruction>(name # "32rCL") GR32:$src1)>; 1732 def : Pat<(store (frag (loadi8 addr:$dst), (and CL, immShift32)), addr:$dst), 1733 (!cast<Instruction>(name # "8mCL") addr:$dst)>; 1734 def : Pat<(store (frag (loadi16 addr:$dst), (and CL, immShift32)), addr:$dst), 1735 (!cast<Instruction>(name # "16mCL") addr:$dst)>; 1736 def : Pat<(store (frag (loadi32 addr:$dst), (and CL, immShift32)), addr:$dst), 1737 (!cast<Instruction>(name # "32mCL") addr:$dst)>; 1738 1739 // (shift x (and y, 63)) ==> (shift x, y) 1740 def : Pat<(frag GR64:$src1, (and CL, immShift64)), 1741 (!cast<Instruction>(name # "64rCL") GR64:$src1)>; 1742 def : Pat<(store (frag (loadi64 addr:$dst), (and CL, immShift64)), addr:$dst), 1743 (!cast<Instruction>(name # "64mCL") addr:$dst)>; 1744} 1745 1746defm : MaskedShiftAmountPats<shl, "SHL">; 1747defm : MaskedShiftAmountPats<srl, "SHR">; 1748defm : MaskedShiftAmountPats<sra, "SAR">; 1749 1750// ROL/ROR instructions allow a stronger mask optimization than shift for 8- and 1751// 16-bit. We can remove a mask of any (bitwidth - 1) on the rotation amount 1752// because over-rotating produces the same result. This is noted in the Intel 1753// docs with: "tempCOUNT <- (COUNT & COUNTMASK) MOD SIZE". Masking the rotation 1754// amount could affect EFLAGS results, but that does not matter because we are 1755// not tracking flags for these nodes. 1756multiclass MaskedRotateAmountPats<SDNode frag, string name> { 1757 // (rot x (and y, BitWidth - 1)) ==> (rot x, y) 1758 def : Pat<(frag GR8:$src1, (and CL, immShift8)), 1759 (!cast<Instruction>(name # "8rCL") GR8:$src1)>; 1760 def : Pat<(frag GR16:$src1, (and CL, immShift16)), 1761 (!cast<Instruction>(name # "16rCL") GR16:$src1)>; 1762 def : Pat<(frag GR32:$src1, (and CL, immShift32)), 1763 (!cast<Instruction>(name # "32rCL") GR32:$src1)>; 1764 def : Pat<(store (frag (loadi8 addr:$dst), (and CL, immShift8)), addr:$dst), 1765 (!cast<Instruction>(name # "8mCL") addr:$dst)>; 1766 def : Pat<(store (frag (loadi16 addr:$dst), (and CL, immShift16)), addr:$dst), 1767 (!cast<Instruction>(name # "16mCL") addr:$dst)>; 1768 def : Pat<(store (frag (loadi32 addr:$dst), (and CL, immShift32)), addr:$dst), 1769 (!cast<Instruction>(name # "32mCL") addr:$dst)>; 1770 1771 // (rot x (and y, 63)) ==> (rot x, y) 1772 def : Pat<(frag GR64:$src1, (and CL, immShift64)), 1773 (!cast<Instruction>(name # "64rCL") GR64:$src1)>; 1774 def : Pat<(store (frag (loadi64 addr:$dst), (and CL, immShift64)), addr:$dst), 1775 (!cast<Instruction>(name # "64mCL") addr:$dst)>; 1776} 1777 1778 1779defm : MaskedRotateAmountPats<rotl, "ROL">; 1780defm : MaskedRotateAmountPats<rotr, "ROR">; 1781 1782// Double shift amount is implicitly masked. 1783multiclass MaskedDoubleShiftAmountPats<SDNode frag, string name> { 1784 // (shift x (and y, 31)) ==> (shift x, y) 1785 def : Pat<(frag GR16:$src1, GR16:$src2, (and CL, immShift32)), 1786 (!cast<Instruction>(name # "16rrCL") GR16:$src1, GR16:$src2)>; 1787 def : Pat<(frag GR32:$src1, GR32:$src2, (and CL, immShift32)), 1788 (!cast<Instruction>(name # "32rrCL") GR32:$src1, GR32:$src2)>; 1789 1790 // (shift x (and y, 63)) ==> (shift x, y) 1791 def : Pat<(frag GR64:$src1, GR64:$src2, (and CL, immShift64)), 1792 (!cast<Instruction>(name # "64rrCL") GR64:$src1, GR64:$src2)>; 1793} 1794 1795defm : MaskedDoubleShiftAmountPats<X86shld, "SHLD">; 1796defm : MaskedDoubleShiftAmountPats<X86shrd, "SHRD">; 1797 1798let Predicates = [HasBMI2] in { 1799 let AddedComplexity = 1 in { 1800 def : Pat<(sra GR32:$src1, (and GR8:$src2, immShift32)), 1801 (SARX32rr GR32:$src1, 1802 (INSERT_SUBREG 1803 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1804 def : Pat<(sra GR64:$src1, (and GR8:$src2, immShift64)), 1805 (SARX64rr GR64:$src1, 1806 (INSERT_SUBREG 1807 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1808 1809 def : Pat<(srl GR32:$src1, (and GR8:$src2, immShift32)), 1810 (SHRX32rr GR32:$src1, 1811 (INSERT_SUBREG 1812 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1813 def : Pat<(srl GR64:$src1, (and GR8:$src2, immShift64)), 1814 (SHRX64rr GR64:$src1, 1815 (INSERT_SUBREG 1816 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1817 1818 def : Pat<(shl GR32:$src1, (and GR8:$src2, immShift32)), 1819 (SHLX32rr GR32:$src1, 1820 (INSERT_SUBREG 1821 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1822 def : Pat<(shl GR64:$src1, (and GR8:$src2, immShift64)), 1823 (SHLX64rr GR64:$src1, 1824 (INSERT_SUBREG 1825 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1826 } 1827 1828 def : Pat<(sra (loadi32 addr:$src1), (and GR8:$src2, immShift32)), 1829 (SARX32rm addr:$src1, 1830 (INSERT_SUBREG 1831 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1832 def : Pat<(sra (loadi64 addr:$src1), (and GR8:$src2, immShift64)), 1833 (SARX64rm addr:$src1, 1834 (INSERT_SUBREG 1835 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1836 1837 def : Pat<(srl (loadi32 addr:$src1), (and GR8:$src2, immShift32)), 1838 (SHRX32rm addr:$src1, 1839 (INSERT_SUBREG 1840 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1841 def : Pat<(srl (loadi64 addr:$src1), (and GR8:$src2, immShift64)), 1842 (SHRX64rm addr:$src1, 1843 (INSERT_SUBREG 1844 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1845 1846 def : Pat<(shl (loadi32 addr:$src1), (and GR8:$src2, immShift32)), 1847 (SHLX32rm addr:$src1, 1848 (INSERT_SUBREG 1849 (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1850 def : Pat<(shl (loadi64 addr:$src1), (and GR8:$src2, immShift64)), 1851 (SHLX64rm addr:$src1, 1852 (INSERT_SUBREG 1853 (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1854} 1855 1856// Use BTR/BTS/BTC for clearing/setting/toggling a bit in a variable location. 1857multiclass one_bit_patterns<RegisterClass RC, ValueType VT, Instruction BTR, 1858 Instruction BTS, Instruction BTC, 1859 ImmLeaf ImmShift> { 1860 def : Pat<(and RC:$src1, (rotl -2, GR8:$src2)), 1861 (BTR RC:$src1, 1862 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1863 def : Pat<(or RC:$src1, (shl 1, GR8:$src2)), 1864 (BTS RC:$src1, 1865 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1866 def : Pat<(xor RC:$src1, (shl 1, GR8:$src2)), 1867 (BTC RC:$src1, 1868 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1869 1870 // Similar to above, but removing unneeded masking of the shift amount. 1871 def : Pat<(and RC:$src1, (rotl -2, (and GR8:$src2, ImmShift))), 1872 (BTR RC:$src1, 1873 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1874 def : Pat<(or RC:$src1, (shl 1, (and GR8:$src2, ImmShift))), 1875 (BTS RC:$src1, 1876 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1877 def : Pat<(xor RC:$src1, (shl 1, (and GR8:$src2, ImmShift))), 1878 (BTC RC:$src1, 1879 (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>; 1880} 1881 1882defm : one_bit_patterns<GR16, i16, BTR16rr, BTS16rr, BTC16rr, immShift16>; 1883defm : one_bit_patterns<GR32, i32, BTR32rr, BTS32rr, BTC32rr, immShift32>; 1884defm : one_bit_patterns<GR64, i64, BTR64rr, BTS64rr, BTC64rr, immShift64>; 1885 1886 1887// (anyext (setcc_carry)) -> (setcc_carry) 1888def : Pat<(i16 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 1889 (SETB_C16r)>; 1890def : Pat<(i32 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))), 1891 (SETB_C32r)>; 1892def : Pat<(i32 (anyext (i16 (X86setcc_c X86_COND_B, EFLAGS)))), 1893 (SETB_C32r)>; 1894 1895//===----------------------------------------------------------------------===// 1896// EFLAGS-defining Patterns 1897//===----------------------------------------------------------------------===// 1898 1899// add reg, reg 1900def : Pat<(add GR8 :$src1, GR8 :$src2), (ADD8rr GR8 :$src1, GR8 :$src2)>; 1901def : Pat<(add GR16:$src1, GR16:$src2), (ADD16rr GR16:$src1, GR16:$src2)>; 1902def : Pat<(add GR32:$src1, GR32:$src2), (ADD32rr GR32:$src1, GR32:$src2)>; 1903def : Pat<(add GR64:$src1, GR64:$src2), (ADD64rr GR64:$src1, GR64:$src2)>; 1904 1905// add reg, mem 1906def : Pat<(add GR8:$src1, (loadi8 addr:$src2)), 1907 (ADD8rm GR8:$src1, addr:$src2)>; 1908def : Pat<(add GR16:$src1, (loadi16 addr:$src2)), 1909 (ADD16rm GR16:$src1, addr:$src2)>; 1910def : Pat<(add GR32:$src1, (loadi32 addr:$src2)), 1911 (ADD32rm GR32:$src1, addr:$src2)>; 1912def : Pat<(add GR64:$src1, (loadi64 addr:$src2)), 1913 (ADD64rm GR64:$src1, addr:$src2)>; 1914 1915// add reg, imm 1916def : Pat<(add GR8 :$src1, imm:$src2), (ADD8ri GR8:$src1 , imm:$src2)>; 1917def : Pat<(add GR16:$src1, imm:$src2), (ADD16ri GR16:$src1, imm:$src2)>; 1918def : Pat<(add GR32:$src1, imm:$src2), (ADD32ri GR32:$src1, imm:$src2)>; 1919def : Pat<(add GR16:$src1, i16immSExt8:$src2), 1920 (ADD16ri8 GR16:$src1, i16immSExt8:$src2)>; 1921def : Pat<(add GR32:$src1, i32immSExt8:$src2), 1922 (ADD32ri8 GR32:$src1, i32immSExt8:$src2)>; 1923def : Pat<(add GR64:$src1, i64immSExt8:$src2), 1924 (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>; 1925def : Pat<(add GR64:$src1, i64immSExt32:$src2), 1926 (ADD64ri32 GR64:$src1, i64immSExt32:$src2)>; 1927 1928// sub reg, reg 1929def : Pat<(sub GR8 :$src1, GR8 :$src2), (SUB8rr GR8 :$src1, GR8 :$src2)>; 1930def : Pat<(sub GR16:$src1, GR16:$src2), (SUB16rr GR16:$src1, GR16:$src2)>; 1931def : Pat<(sub GR32:$src1, GR32:$src2), (SUB32rr GR32:$src1, GR32:$src2)>; 1932def : Pat<(sub GR64:$src1, GR64:$src2), (SUB64rr GR64:$src1, GR64:$src2)>; 1933 1934// sub reg, mem 1935def : Pat<(sub GR8:$src1, (loadi8 addr:$src2)), 1936 (SUB8rm GR8:$src1, addr:$src2)>; 1937def : Pat<(sub GR16:$src1, (loadi16 addr:$src2)), 1938 (SUB16rm GR16:$src1, addr:$src2)>; 1939def : Pat<(sub GR32:$src1, (loadi32 addr:$src2)), 1940 (SUB32rm GR32:$src1, addr:$src2)>; 1941def : Pat<(sub GR64:$src1, (loadi64 addr:$src2)), 1942 (SUB64rm GR64:$src1, addr:$src2)>; 1943 1944// sub reg, imm 1945def : Pat<(sub GR8:$src1, imm:$src2), 1946 (SUB8ri GR8:$src1, imm:$src2)>; 1947def : Pat<(sub GR16:$src1, imm:$src2), 1948 (SUB16ri GR16:$src1, imm:$src2)>; 1949def : Pat<(sub GR32:$src1, imm:$src2), 1950 (SUB32ri GR32:$src1, imm:$src2)>; 1951def : Pat<(sub GR16:$src1, i16immSExt8:$src2), 1952 (SUB16ri8 GR16:$src1, i16immSExt8:$src2)>; 1953def : Pat<(sub GR32:$src1, i32immSExt8:$src2), 1954 (SUB32ri8 GR32:$src1, i32immSExt8:$src2)>; 1955def : Pat<(sub GR64:$src1, i64immSExt8:$src2), 1956 (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>; 1957def : Pat<(sub GR64:$src1, i64immSExt32:$src2), 1958 (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>; 1959 1960// sub 0, reg 1961def : Pat<(X86sub_flag 0, GR8 :$src), (NEG8r GR8 :$src)>; 1962def : Pat<(X86sub_flag 0, GR16:$src), (NEG16r GR16:$src)>; 1963def : Pat<(X86sub_flag 0, GR32:$src), (NEG32r GR32:$src)>; 1964def : Pat<(X86sub_flag 0, GR64:$src), (NEG64r GR64:$src)>; 1965 1966// sub reg, relocImm 1967def : Pat<(X86sub_flag GR64:$src1, i64relocImmSExt8_su:$src2), 1968 (SUB64ri8 GR64:$src1, i64relocImmSExt8_su:$src2)>; 1969def : Pat<(X86sub_flag GR64:$src1, i64relocImmSExt32_su:$src2), 1970 (SUB64ri32 GR64:$src1, i64relocImmSExt32_su:$src2)>; 1971 1972// mul reg, reg 1973def : Pat<(mul GR16:$src1, GR16:$src2), 1974 (IMUL16rr GR16:$src1, GR16:$src2)>; 1975def : Pat<(mul GR32:$src1, GR32:$src2), 1976 (IMUL32rr GR32:$src1, GR32:$src2)>; 1977def : Pat<(mul GR64:$src1, GR64:$src2), 1978 (IMUL64rr GR64:$src1, GR64:$src2)>; 1979 1980// mul reg, mem 1981def : Pat<(mul GR16:$src1, (loadi16 addr:$src2)), 1982 (IMUL16rm GR16:$src1, addr:$src2)>; 1983def : Pat<(mul GR32:$src1, (loadi32 addr:$src2)), 1984 (IMUL32rm GR32:$src1, addr:$src2)>; 1985def : Pat<(mul GR64:$src1, (loadi64 addr:$src2)), 1986 (IMUL64rm GR64:$src1, addr:$src2)>; 1987 1988// mul reg, imm 1989def : Pat<(mul GR16:$src1, imm:$src2), 1990 (IMUL16rri GR16:$src1, imm:$src2)>; 1991def : Pat<(mul GR32:$src1, imm:$src2), 1992 (IMUL32rri GR32:$src1, imm:$src2)>; 1993def : Pat<(mul GR16:$src1, i16immSExt8:$src2), 1994 (IMUL16rri8 GR16:$src1, i16immSExt8:$src2)>; 1995def : Pat<(mul GR32:$src1, i32immSExt8:$src2), 1996 (IMUL32rri8 GR32:$src1, i32immSExt8:$src2)>; 1997def : Pat<(mul GR64:$src1, i64immSExt8:$src2), 1998 (IMUL64rri8 GR64:$src1, i64immSExt8:$src2)>; 1999def : Pat<(mul GR64:$src1, i64immSExt32:$src2), 2000 (IMUL64rri32 GR64:$src1, i64immSExt32:$src2)>; 2001 2002// reg = mul mem, imm 2003def : Pat<(mul (loadi16 addr:$src1), imm:$src2), 2004 (IMUL16rmi addr:$src1, imm:$src2)>; 2005def : Pat<(mul (loadi32 addr:$src1), imm:$src2), 2006 (IMUL32rmi addr:$src1, imm:$src2)>; 2007def : Pat<(mul (loadi16 addr:$src1), i16immSExt8:$src2), 2008 (IMUL16rmi8 addr:$src1, i16immSExt8:$src2)>; 2009def : Pat<(mul (loadi32 addr:$src1), i32immSExt8:$src2), 2010 (IMUL32rmi8 addr:$src1, i32immSExt8:$src2)>; 2011def : Pat<(mul (loadi64 addr:$src1), i64immSExt8:$src2), 2012 (IMUL64rmi8 addr:$src1, i64immSExt8:$src2)>; 2013def : Pat<(mul (loadi64 addr:$src1), i64immSExt32:$src2), 2014 (IMUL64rmi32 addr:$src1, i64immSExt32:$src2)>; 2015 2016// Increment/Decrement reg. 2017// Do not make INC/DEC if it is slow 2018let Predicates = [UseIncDec] in { 2019 def : Pat<(add GR8:$src, 1), (INC8r GR8:$src)>; 2020 def : Pat<(add GR16:$src, 1), (INC16r GR16:$src)>; 2021 def : Pat<(add GR32:$src, 1), (INC32r GR32:$src)>; 2022 def : Pat<(add GR64:$src, 1), (INC64r GR64:$src)>; 2023 def : Pat<(add GR8:$src, -1), (DEC8r GR8:$src)>; 2024 def : Pat<(add GR16:$src, -1), (DEC16r GR16:$src)>; 2025 def : Pat<(add GR32:$src, -1), (DEC32r GR32:$src)>; 2026 def : Pat<(add GR64:$src, -1), (DEC64r GR64:$src)>; 2027} 2028 2029// or reg/reg. 2030def : Pat<(or GR8 :$src1, GR8 :$src2), (OR8rr GR8 :$src1, GR8 :$src2)>; 2031def : Pat<(or GR16:$src1, GR16:$src2), (OR16rr GR16:$src1, GR16:$src2)>; 2032def : Pat<(or GR32:$src1, GR32:$src2), (OR32rr GR32:$src1, GR32:$src2)>; 2033def : Pat<(or GR64:$src1, GR64:$src2), (OR64rr GR64:$src1, GR64:$src2)>; 2034 2035// or reg/mem 2036def : Pat<(or GR8:$src1, (loadi8 addr:$src2)), 2037 (OR8rm GR8:$src1, addr:$src2)>; 2038def : Pat<(or GR16:$src1, (loadi16 addr:$src2)), 2039 (OR16rm GR16:$src1, addr:$src2)>; 2040def : Pat<(or GR32:$src1, (loadi32 addr:$src2)), 2041 (OR32rm GR32:$src1, addr:$src2)>; 2042def : Pat<(or GR64:$src1, (loadi64 addr:$src2)), 2043 (OR64rm GR64:$src1, addr:$src2)>; 2044 2045// or reg/imm 2046def : Pat<(or GR8:$src1 , imm:$src2), (OR8ri GR8 :$src1, imm:$src2)>; 2047def : Pat<(or GR16:$src1, imm:$src2), (OR16ri GR16:$src1, imm:$src2)>; 2048def : Pat<(or GR32:$src1, imm:$src2), (OR32ri GR32:$src1, imm:$src2)>; 2049def : Pat<(or GR16:$src1, i16immSExt8:$src2), 2050 (OR16ri8 GR16:$src1, i16immSExt8:$src2)>; 2051def : Pat<(or GR32:$src1, i32immSExt8:$src2), 2052 (OR32ri8 GR32:$src1, i32immSExt8:$src2)>; 2053def : Pat<(or GR64:$src1, i64immSExt8:$src2), 2054 (OR64ri8 GR64:$src1, i64immSExt8:$src2)>; 2055def : Pat<(or GR64:$src1, i64immSExt32:$src2), 2056 (OR64ri32 GR64:$src1, i64immSExt32:$src2)>; 2057 2058// xor reg/reg 2059def : Pat<(xor GR8 :$src1, GR8 :$src2), (XOR8rr GR8 :$src1, GR8 :$src2)>; 2060def : Pat<(xor GR16:$src1, GR16:$src2), (XOR16rr GR16:$src1, GR16:$src2)>; 2061def : Pat<(xor GR32:$src1, GR32:$src2), (XOR32rr GR32:$src1, GR32:$src2)>; 2062def : Pat<(xor GR64:$src1, GR64:$src2), (XOR64rr GR64:$src1, GR64:$src2)>; 2063 2064// xor reg/mem 2065def : Pat<(xor GR8:$src1, (loadi8 addr:$src2)), 2066 (XOR8rm GR8:$src1, addr:$src2)>; 2067def : Pat<(xor GR16:$src1, (loadi16 addr:$src2)), 2068 (XOR16rm GR16:$src1, addr:$src2)>; 2069def : Pat<(xor GR32:$src1, (loadi32 addr:$src2)), 2070 (XOR32rm GR32:$src1, addr:$src2)>; 2071def : Pat<(xor GR64:$src1, (loadi64 addr:$src2)), 2072 (XOR64rm GR64:$src1, addr:$src2)>; 2073 2074// xor reg/imm 2075def : Pat<(xor GR8:$src1, imm:$src2), 2076 (XOR8ri GR8:$src1, imm:$src2)>; 2077def : Pat<(xor GR16:$src1, imm:$src2), 2078 (XOR16ri GR16:$src1, imm:$src2)>; 2079def : Pat<(xor GR32:$src1, imm:$src2), 2080 (XOR32ri GR32:$src1, imm:$src2)>; 2081def : Pat<(xor GR16:$src1, i16immSExt8:$src2), 2082 (XOR16ri8 GR16:$src1, i16immSExt8:$src2)>; 2083def : Pat<(xor GR32:$src1, i32immSExt8:$src2), 2084 (XOR32ri8 GR32:$src1, i32immSExt8:$src2)>; 2085def : Pat<(xor GR64:$src1, i64immSExt8:$src2), 2086 (XOR64ri8 GR64:$src1, i64immSExt8:$src2)>; 2087def : Pat<(xor GR64:$src1, i64immSExt32:$src2), 2088 (XOR64ri32 GR64:$src1, i64immSExt32:$src2)>; 2089 2090// and reg/reg 2091def : Pat<(and GR8 :$src1, GR8 :$src2), (AND8rr GR8 :$src1, GR8 :$src2)>; 2092def : Pat<(and GR16:$src1, GR16:$src2), (AND16rr GR16:$src1, GR16:$src2)>; 2093def : Pat<(and GR32:$src1, GR32:$src2), (AND32rr GR32:$src1, GR32:$src2)>; 2094def : Pat<(and GR64:$src1, GR64:$src2), (AND64rr GR64:$src1, GR64:$src2)>; 2095 2096// and reg/mem 2097def : Pat<(and GR8:$src1, (loadi8 addr:$src2)), 2098 (AND8rm GR8:$src1, addr:$src2)>; 2099def : Pat<(and GR16:$src1, (loadi16 addr:$src2)), 2100 (AND16rm GR16:$src1, addr:$src2)>; 2101def : Pat<(and GR32:$src1, (loadi32 addr:$src2)), 2102 (AND32rm GR32:$src1, addr:$src2)>; 2103def : Pat<(and GR64:$src1, (loadi64 addr:$src2)), 2104 (AND64rm GR64:$src1, addr:$src2)>; 2105 2106// and reg/imm 2107def : Pat<(and GR8:$src1, imm:$src2), 2108 (AND8ri GR8:$src1, imm:$src2)>; 2109def : Pat<(and GR16:$src1, imm:$src2), 2110 (AND16ri GR16:$src1, imm:$src2)>; 2111def : Pat<(and GR32:$src1, imm:$src2), 2112 (AND32ri GR32:$src1, imm:$src2)>; 2113def : Pat<(and GR16:$src1, i16immSExt8:$src2), 2114 (AND16ri8 GR16:$src1, i16immSExt8:$src2)>; 2115def : Pat<(and GR32:$src1, i32immSExt8:$src2), 2116 (AND32ri8 GR32:$src1, i32immSExt8:$src2)>; 2117def : Pat<(and GR64:$src1, i64immSExt8:$src2), 2118 (AND64ri8 GR64:$src1, i64immSExt8:$src2)>; 2119def : Pat<(and GR64:$src1, i64immSExt32:$src2), 2120 (AND64ri32 GR64:$src1, i64immSExt32:$src2)>; 2121 2122// Bit scan instruction patterns to match explicit zero-undef behavior. 2123def : Pat<(cttz_zero_undef GR16:$src), (BSF16rr GR16:$src)>; 2124def : Pat<(cttz_zero_undef GR32:$src), (BSF32rr GR32:$src)>; 2125def : Pat<(cttz_zero_undef GR64:$src), (BSF64rr GR64:$src)>; 2126def : Pat<(cttz_zero_undef (loadi16 addr:$src)), (BSF16rm addr:$src)>; 2127def : Pat<(cttz_zero_undef (loadi32 addr:$src)), (BSF32rm addr:$src)>; 2128def : Pat<(cttz_zero_undef (loadi64 addr:$src)), (BSF64rm addr:$src)>; 2129 2130// When HasMOVBE is enabled it is possible to get a non-legalized 2131// register-register 16 bit bswap. This maps it to a ROL instruction. 2132let Predicates = [HasMOVBE] in { 2133 def : Pat<(bswap GR16:$src), (ROL16ri GR16:$src, (i8 8))>; 2134} 2135