ifcvt-size.mir 15 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
# RUN: llc %s -o - -run-pass=if-converter -debug-only=if-converter 2>%t| FileCheck %s
# RUN: FileCheck %s < %t --check-prefix=DEBUG
# REQUIRES: asserts

# When optimising for size, we use a different set of heuristics for
# if-conversion, which take into account the size of the instructions, not the
# time taken to execute them. This is more complicated for Thumb, where it if
# also affected by selection of narrow branch instructions, insertion if IT
# instructions, and selection of the CB(N)Z instructions.

--- |
  target triple = "thumbv7-unknown-linux-gnueabi"

  define void @fn1() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn2() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn3() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn4() minsize "target-features"="-thumb-mode" {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn5() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn6() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if2.then:
    unreachable
  if2.else:
    unreachable
  }

  define void @fn7() minsize "target-features"="-thumb-mode" {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn8() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  if.end:
    unreachable
  }

  define void @fn9() minsize {
  entry:
    unreachable
  if.then:
    unreachable
  if.else:
    unreachable
  lab1:
    unreachable
  }
...
---
name:            fn1
alignment:       1
tracksRegLiveness: true

# If-conversion is profitable here because it will remove two branches of 2
# bytes each (assuming they can become narrow branches later), and will only
# add 2 bytes with the IT instruction.

# CHECK-LABEL: name:            fn1
# CHECK:      t2CMPri
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRSHi12
# CHECK-NEXT: t2MOVi

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn1'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=4, CommonBytes=0, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    t2CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    t2B %bb.3, 14, $noreg

  bb.2.if.else:
    successors: %bb.3(0x80000000)
    liveins: $r1, $r3

    renamable $r0 = t2LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg

  bb.3.if.end:
    liveins: $r0, $r3

    renamable $r1 = t2MOVi 0, 14, $noreg, $noreg
    t2STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

---
name:            fn2
alignment:       1
tracksRegLiveness: true

# If-conversion is not profitable here, because the 5 conditional instructions
# would require 2 IT instructions.

# CHECK-LABEL: name:            fn2
# CHECK:      t2CMPri
# CHECK-NEXT: t2Bcc

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn2'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=4, CommonBytes=0, NumPredicatedInstructions=5, ExtraPredicateBytes=4)

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    t2CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    t2B %bb.3, 14, $noreg

  bb.2.if.else:
    successors: %bb.3(0x80000000)
    liveins: $r1, $r3

    renamable $r0 = t2LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg

  bb.3.if.end:
    liveins: $r0, $r3

    renamable $r1 = t2MOVi 0, 14, $noreg, $noreg
    t2STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

---
name:            fn3
alignment:       1
tracksRegLiveness: true

# Here, the true and false blocks both end in a tBX_RET instruction. One of
# these will be removed, saving 2 bytes, and the remaining one isn't
# conditional, so doesn't push us over the limit of 4 instructions in an IT
# block.

# CHECK-LABEL: name:            fn3
# CHECK:      t2CMPri
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRSHi12
# CHECK-NEXT: tBX_RET

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn3'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=2, CommonBytes=2, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    t2CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

  bb.2.if.else:
    liveins: $r1, $r3

    renamable $r0 = t2LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

---
name:            fn4
alignment:       1
tracksRegLiveness: true

# This is the same as fn2, but compiled for ARM, which doesn't need IT
# instructions, so if-conversion is profitable.

# CHECK-LABEL: name:            fn4
# CHECK:      CMPri
# CHECK-NEXT: LDRi12
# CHECK-NEXT: LDRi12
# CHECK-NEXT: LDRSH
# CHECK-NEXT: LDRi12
# CHECK-NEXT: LDRi12
# CHECK-NEXT: MOVi

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn4'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=8, CommonBytes=0, NumPredicatedInstructions=5, ExtraPredicateBytes=0)

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    B %bb.3

  bb.2.if.else:
    successors: %bb.3(0x80000000)
    liveins: $r1, $r3

    renamable $r0 = LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRSH killed renamable $r0, $noreg, 0, 14, $noreg

  bb.3.if.end:
    liveins: $r0, $r3

    renamable $r1 = MOVi 0, 14, $noreg, $noreg
    STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    BX_RET 14, $noreg, implicit $r0

---
name:            fn5
alignment:       1
tracksRegLiveness: true

# Here, the compare and conditional branch can be turned into a CBZ, so we
# don't want to if-convert.

# CHECK-LABEL: name:            fn5
# CHECK: t2CMPri
# CHECK: t2Bcc

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn5'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=0, CommonBytes=2, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x30000000), %bb.2(0x50000000)
    liveins: $r0, $r1, $r2

    t2CMPri killed renamable $r2, 0, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 1, killed $cpsr

  bb.1.if.then:
    liveins: $r0

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

  bb.2.if.else:
    liveins: $r1

    renamable $r0 = t2LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

---
name:            fn6
alignment:       1
tracksRegLiveness: true

# This is a forked-diamond pattern, we recognise that the conditional branches
# at the ends of the true and false blocks are the same, and can be shared.

# CHECK-LABEL: name:            fn6
# CHECK:      t2CMPri
# CHECK-NEXT: t2LDRSHi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2CMPri
# CHECK-NEXT: t2Bcc

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn6'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=2, CommonBytes=12, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x30000000), %bb.2(0x50000000)
    liveins: $r0, $r1, $r2, $r3

    t2CMPri killed renamable $r2, 4, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 1, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x30000000), %bb.4(0x50000000)
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    t2CMPri renamable $r0, 0, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.3.if2.then, 1, killed $cpsr
    t2B %bb.4.if2.else, 14, $noreg

  bb.2.if.else:
    successors: %bb.3(0x30000000), %bb.4(0x50000000)
    liveins: $r0, $r1, $r3

    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg
    t2CMPri renamable $r0, 0, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.3.if2.then, 1, killed $cpsr
    t2B %bb.4.if2.else, 14, $noreg

  bb.3.if2.then:
    liveins: $r0, $r1, $r3

    t2STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

  bb.4.if2.else:
    liveins: $r0

    tBX_RET 14, $noreg, implicit $r0

---
name:            fn7
alignment:       1
tracksRegLiveness: true

# When compiling for ARM, it would be good for code size to generate very long
# runs of conditional instructions, but we put an (arbitrary) limit on this to
# avoid generating code which is very bad for performance, and only saves a few
# bytes of code size.

# CHECK-LABEL: name:            fn7
# CHECK:      CMPri
# CHECK-NEXT: Bcc

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    B %bb.3

  bb.2.if.else:
    successors: %bb.3(0x80000000)
    liveins: $r1, $r3

    renamable $r0 = LDRi12 killed renamable $r1, 0, 14, $noreg
    renamable $r0 = LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = LDRSH killed renamable $r0, $noreg, 0, 14, $noreg

  bb.3.if.end:
    liveins: $r0, $r3

    renamable $r1 = MOVi 0, 14, $noreg, $noreg
    STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    BX_RET 14, $noreg, implicit $r0

---
name:            fn8
alignment:       1
tracksRegLiveness: true

# The first t2LDRi12 instruction in each branch is the same, so one copy of it
# will be removed, and it doesn't need to be predicated, keeping us under the 4
# instruction IT block limit.

# CHECK-LABEL: name:            fn8
# CHECK:      t2CMPri
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRi12
# CHECK-NEXT: t2LDRSHi12
# CHECK-NEXT: t2MOVi

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn8'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=4, CommonBytes=4, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x40000000), %bb.2(0x40000000)
    liveins: $r0, $r1, $r2, $r3

    t2CMPri killed renamable $r2, 5, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.2, 11, killed $cpsr

  bb.1.if.then:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 4, 14, $noreg
    t2B %bb.3, 14, $noreg

  bb.2.if.else:
    successors: %bb.3(0x80000000)
    liveins: $r0, $r3

    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRi12 killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg

  bb.3.if.end:
    liveins: $r0, $r3

    renamable $r1 = t2MOVi 0, 14, $noreg, $noreg
    t2STRi12 killed renamable $r1, killed renamable $r3, 0, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0

---
name:            fn9
alignment:       2
tracksRegLiveness: true

# The INLINEASM_BR instructions aren't analyzable, but they are identical so we
# can still do diamond if-conversion. From a code-size POV, they are common
# instructions, so one will be removed, and they don't need an IT block slot.

# CHECK-LABEL: name:            fn9
# CHECK:      tCMPi8
# CHECK-NEXT: tLDRi
# CHECK-NEXT: tLDRi
# CHECK-NEXT: tLDRi
# CHECK-NEXT: t2LDRSHi12
# CHECK-NEXT: INLINEASM_BR

# DEBUG-LABEL: Ifcvt: function ({{[0-9]+}}) 'fn9'
# DEBUG: MeetIfcvtSizeLimit(BranchBytes=2, CommonBytes=6, NumPredicatedInstructions=4, ExtraPredicateBytes=2)

body:             |
  bb.0.entry:
    successors: %bb.1(0x30000000), %bb.3(0x50000000)
    liveins: $r0, $r1, $r2

    tCMPi8 killed renamable $r2, 42, 14, $noreg, implicit-def $cpsr
    t2Bcc %bb.3, 1, killed $cpsr

  bb.1.if.then:
    successors:  %bb.5(0x7fffffff)
    liveins: $r0

    renamable $r0 = tLDRi killed renamable $r0, 0, 14, $noreg
    INLINEASM_BR &"b ${0:l}", 1, 13, blockaddress(@fn9, %ir-block.lab1)

  bb.3.if.else:
    successors: %bb.5(0x7fffffff)
    liveins: $r1

    renamable $r0 = tLDRi killed renamable $r1, 0, 14, $noreg
    renamable $r0 = tLDRi killed renamable $r0, 0, 14, $noreg
    renamable $r0 = t2LDRSHi12 killed renamable $r0, 0, 14, $noreg
    INLINEASM_BR &"b ${0:l}", 1, 13, blockaddress(@fn9, %ir-block.lab1)

  bb.5.lab1 (address-taken):
    liveins: $r0

    renamable $r0, dead $cpsr = nsw tADDi8 killed renamable $r0, 5, 14, $noreg
    tBX_RET 14, $noreg, implicit $r0
...