141 lines · plain
1; RUN: opt -passes=consthoist -S < %s | FileCheck %s2target triple = "thumbv6m-none-eabi"3 4; Allocas in the entry block get handled (for free) by5; prologue/epilogue. Elsewhere they're fair game though.6define void @avoid_allocas() {7; CHECK-LABEL: @avoid_allocas8; CHECK: %addr1 = alloca i8, i32 10009; CHECK: %addr2 = alloca i8, i32 102010 11 %addr1 = alloca i8, i32 100012 %addr2 = alloca i8, i32 102013 br label %elsewhere14 15elsewhere:16; CHECK: [[BASE:%.*]] = bitcast i32 1000 to i3217; CHECK: alloca i8, i32 [[BASE]]18; CHECK: [[NEXT:%.*]] = add i32 [[BASE]], 2019; CHECK: alloca i8, i32 [[NEXT]]20 21 %addr3 = alloca i8, i32 100022 %addr4 = alloca i8, i32 102023 24 ret void25}26 27; The case values of switch instructions are required to be constants.28define void @avoid_switch(i32 %in) {29; CHECK-LABEL: @avoid_switch30; CHECK: switch i32 %in, label %default [31; CHECK: i32 1000, label %bb132; CHECK: i32 1020, label %bb233; CHECK: ]34 35 switch i32 %in, label %default36 [ i32 1000, label %bb137 i32 1020, label %bb2 ]38 39bb1:40 ret void41 42bb2:43 ret void44 45default:46 ret void47}48 49; We don't want to convert constant divides because the benefit from converting50; them to a mul in the backend is larget than constant materialization savings.51define void @signed_const_division(i32 %in1, i32 %in2, ptr %addr) {52; CHECK-LABEL: @signed_const_division53; CHECK: %res1 = sdiv i32 %l1, 100000000054; CHECK: %res2 = srem i32 %l2, 100000000055entry:56 br label %loop57 58loop:59 %l1 = phi i32 [%res1, %loop], [%in1, %entry]60 %l2 = phi i32 [%res2, %loop], [%in2, %entry]61 %res1 = sdiv i32 %l1, 100000000062 store volatile i32 %res1, ptr %addr63 %res2 = srem i32 %l2, 100000000064 store volatile i32 %res2, ptr %addr65 %again = icmp eq i32 %res1, %res266 br i1 %again, label %loop, label %end67 68end:69 ret void70}71 72define void @unsigned_const_division(i32 %in1, i32 %in2, ptr %addr) {73; CHECK-LABEL: @unsigned_const_division74; CHECK: %res1 = udiv i32 %l1, 100000000075; CHECK: %res2 = urem i32 %l2, 100000000076 77entry:78 br label %loop79 80loop:81 %l1 = phi i32 [%res1, %loop], [%in1, %entry]82 %l2 = phi i32 [%res2, %loop], [%in2, %entry]83 %res1 = udiv i32 %l1, 100000000084 store volatile i32 %res1, ptr %addr85 %res2 = urem i32 %l2, 100000000086 store volatile i32 %res2, ptr %addr87 %again = icmp eq i32 %res1, %res288 br i1 %again, label %loop, label %end89 90end:91 ret void92}93 94;PR 28282: even when data type is larger than 64-bit, the bit width of the95;constant operand could be smaller than 64-bit. In this case, there is no96;benefit to hoist the constant.97define i32 @struct_type_test(i96 %a0, i96 %a1) {98;CHECK-LABEL: @struct_type_test99entry:100;CHECK-NOT: %const = bitcast i96 32 to i96101;CHECK: lshr0 = lshr i96 %a0, 32102 %lshr0 = lshr i96 %a0, 32103 %cast0 = trunc i96 %lshr0 to i32104;CHECK: lshr1 = lshr i96 %a1, 32105 %lshr1 = lshr i96 %a1, 32106 %cast1 = trunc i96 %lshr1 to i32107 %ret = add i32 %cast0, %cast1108 ret i32 %ret109}110 111@exception_type = external global i8112 113; Constants in inline ASM should not be hoisted.114define i32 @inline_asm_invoke() personality ptr null {115;CHECK-LABEL: @inline_asm_invoke116;CHECK-NOT: %const = 214672117;CHECK: %X = invoke i32 asm "bswap $0", "=r,r"(i32 214672)118 %X = invoke i32 asm "bswap $0", "=r,r"(i32 214672)119 to label %L unwind label %lpad120;CHECK: %Y = invoke i32 asm "bswap $0", "=r,r"(i32 214672)121 %Y = invoke i32 asm "bswap $0", "=r,r"(i32 214672)122 to label %L unwind label %lpad123L:124 ret i32 %X125lpad:126 %lp = landingpad i32127 cleanup128 catch ptr @exception_type129 ret i32 1130}131 132define i32 @inline_asm_call() {133;CHECK-LABEL: @inline_asm_call134;CHECK-NOT: %const = 214672135;CHECK: %X = call i32 asm "bswap $0", "=r,r"(i32 214672)136 %X = call i32 asm "bswap $0", "=r,r"(i32 214672)137;CHECK: %Y = call i32 asm "bswap $0", "=r,r"(i32 214672)138 %Y = call i32 asm "bswap $0", "=r,r"(i32 214672)139 ret i32 %X140}141