Author: Amara Emerson Date: 2020-12-10T12:20:02-08:00 New Revision: c29af37c6c9d74ca330bd7f1d084f1f676ba2824
URL: https://github.com/llvm/llvm-project/commit/c29af37c6c9d74ca330bd7f1d084f1f676ba2824 DIFF: https://github.com/llvm/llvm-project/commit/c29af37c6c9d74ca330bd7f1d084f1f676ba2824.diff LOG: [AArch64] Don't try to compress jump tables if there are any inline asm instructions. Inline asm can contain constructs like .bytes which may have arbitrary size. In some cases, this causes us to miscalculate the size of blocks and therefore offsets, causing us to incorrectly compress a JT. To be safe, just bail out of the whole thing if we find any inline asm. Fixes PR48255 Differential Revision: https://reviews.llvm.org/D92865 Added: Modified: llvm/lib/Target/AArch64/AArch64CompressJumpTables.cpp llvm/test/CodeGen/AArch64/jump-table-compress.mir llvm/test/CodeGen/AArch64/jump-table.ll Removed: ################################################################################ diff --git a/llvm/lib/Target/AArch64/AArch64CompressJumpTables.cpp b/llvm/lib/Target/AArch64/AArch64CompressJumpTables.cpp index c265592d05a7..2328a8b4deb8 100644 --- a/llvm/lib/Target/AArch64/AArch64CompressJumpTables.cpp +++ b/llvm/lib/Target/AArch64/AArch64CompressJumpTables.cpp @@ -37,8 +37,13 @@ class AArch64CompressJumpTables : public MachineFunctionPass { MachineFunction *MF; SmallVector<int, 8> BlockInfo; - int computeBlockSize(MachineBasicBlock &MBB); - void scanFunction(); + /// Returns the size in instructions of the block \p MBB, or None if we + /// couldn't get a safe upper bound. + Optional<int> computeBlockSize(MachineBasicBlock &MBB); + + /// Gather information about the function, returns false if we can't perform + /// this optimization for some reason. + bool scanFunction(); bool compressJumpTable(MachineInstr &MI, int Offset); @@ -64,14 +69,22 @@ char AArch64CompressJumpTables::ID = 0; INITIALIZE_PASS(AArch64CompressJumpTables, DEBUG_TYPE, "AArch64 compress jump tables pass", false, false) -int AArch64CompressJumpTables::computeBlockSize(MachineBasicBlock &MBB) { +Optional<int> +AArch64CompressJumpTables::computeBlockSize(MachineBasicBlock &MBB) { int Size = 0; - for (const MachineInstr &MI : MBB) + for (const MachineInstr &MI : MBB) { + // Inline asm may contain some directives like .bytes which we don't + // currently have the ability to parse accurately. To be safe, just avoid + // computing a size and bail out. + if (MI.getOpcode() == AArch64::INLINEASM || + MI.getOpcode() == AArch64::INLINEASM_BR) + return None; Size += TII->getInstSizeInBytes(MI); + } return Size; } -void AArch64CompressJumpTables::scanFunction() { +bool AArch64CompressJumpTables::scanFunction() { BlockInfo.clear(); BlockInfo.resize(MF->getNumBlockIDs()); @@ -84,8 +97,12 @@ void AArch64CompressJumpTables::scanFunction() { else AlignedOffset = alignTo(Offset, Alignment); BlockInfo[MBB.getNumber()] = AlignedOffset; - Offset = AlignedOffset + computeBlockSize(MBB); + auto BlockSize = computeBlockSize(MBB); + if (!BlockSize) + return false; + Offset = AlignedOffset + *BlockSize; } + return true; } bool AArch64CompressJumpTables::compressJumpTable(MachineInstr &MI, @@ -152,7 +169,8 @@ bool AArch64CompressJumpTables::runOnMachineFunction(MachineFunction &MFIn) { if (ST.force32BitJumpTables() && !MF->getFunction().hasMinSize()) return false; - scanFunction(); + if (!scanFunction()) + return false; for (MachineBasicBlock &MBB : *MF) { int Offset = BlockInfo[MBB.getNumber()]; diff --git a/llvm/test/CodeGen/AArch64/jump-table-compress.mir b/llvm/test/CodeGen/AArch64/jump-table-compress.mir index 272de36f8b6e..a46b7c6ac9c0 100644 --- a/llvm/test/CodeGen/AArch64/jump-table-compress.mir +++ b/llvm/test/CodeGen/AArch64/jump-table-compress.mir @@ -4,6 +4,8 @@ unreachable } + define void @test_inline_asm_no_compress() { ret void } + ... --- name: test_jumptable @@ -110,3 +112,88 @@ body: | early-clobber $x10, dead early-clobber $x11 = JumpTableDest32 undef killed $x9, undef killed $x8, %jump-table.5 BR killed $x10 ... +--- +name: test_inline_asm_no_compress +alignment: 4 +tracksRegLiveness: true +liveins: + - { reg: '$w0' } + - { reg: '$w1' } + - { reg: '$w2' } +frameInfo: + maxAlignment: 1 + maxCallFrameSize: 0 +machineFunctionInfo: + hasRedZone: false +jumpTable: + kind: label- diff erence32 + entries: + - id: 0 + blocks: [ '%bb.2', '%bb.4', '%bb.5', '%bb.6', '%bb.7', '%bb.8' ] +body: | + bb.0: + successors: %bb.3(0x12492492), %bb.1(0x6db6db6e) + liveins: $w0, $w1, $w2 + + dead $wzr = SUBSWri renamable $w0, 5, 0, implicit-def $nzcv + Bcc 8, %bb.3, implicit $nzcv + + bb.1: + successors: %bb.2, %bb.4, %bb.5, %bb.6, %bb.7, %bb.8 + liveins: $w0, $w1, $w2 + ; We check that if there's an inline asm instruction in the jump table, + ; that we skip compression. This is due to not being able to rely on + ; finding the instruction size in some cases. + ; CHECK-LABEL: test_inline_asm_no_compress + ; CHECK-LABEL: bb.1 + ; CHECK: JumpTableDest32 + renamable $w8 = ORRWrs $wzr, killed renamable $w0, 0, implicit-def $x8 + $x9 = ADRP target-flags(aarch64-page) %jump-table.0 + renamable $x9 = ADDXri $x9, target-flags(aarch64-pageoff, aarch64-nc) %jump-table.0, 0 + early-clobber renamable $x10, dead early-clobber renamable $x11 = JumpTableDest32 killed renamable $x9, killed renamable $x8, %jump-table.0 + BR killed renamable $x10 + + bb.2: + liveins: $w1, $w2 + + INLINEASM &".byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09", 1 /* sideeffect attdialect */ + INLINEASM &".byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09.byte 0x1f,0x20,0x03,0xd5,0x1f,0x20,0x03,0xd5\0A\09", 1 /* sideeffect attdialect */ + $w0 = ADDWrs killed renamable $w2, killed renamable $w1, 0 + RET undef $lr, implicit $w0 + + bb.3: + $w0 = MOVZWi 0, 0 + RET undef $lr, implicit $w0 + + bb.4: + liveins: $w1, $w2 + + renamable $w0 = nsw MADDWrrr killed renamable $w2, killed renamable $w1, $wzr + RET undef $lr, implicit $w0 + + bb.5: + liveins: $w1, $w2 + + $w0 = SUBWrs killed renamable $w1, killed renamable $w2, 0 + RET undef $lr, implicit $w0 + + bb.6: + liveins: $w1, $w2 + + $w0 = SUBWrs killed renamable $w2, killed renamable $w1, 0 + RET undef $lr, implicit $w0 + + bb.7: + liveins: $w1, $w2 + + renamable $w0 = MADDWrrr killed renamable $w1, renamable $w1, killed renamable $w2 + RET undef $lr, implicit $w0 + + bb.8: + liveins: $w1, $w2 + + renamable $w8 = nsw MADDWrrr renamable $w2, renamable $w2, $wzr + renamable $w0 = MADDWrrr killed renamable $w8, killed renamable $w2, killed renamable $w1 + RET undef $lr, implicit $w0 + +... diff --git a/llvm/test/CodeGen/AArch64/jump-table.ll b/llvm/test/CodeGen/AArch64/jump-table.ll index cbd187e9a49f..091d29fddb40 100644 --- a/llvm/test/CodeGen/AArch64/jump-table.ll +++ b/llvm/test/CodeGen/AArch64/jump-table.ll @@ -101,22 +101,7 @@ lbl1: ret i32 1 lbl2: - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() - call void asm sideeffect "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16", ""() + call i64 @llvm.aarch64.space(i32 1024, i64 undef) ret i32 2 lbl3: _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits