Index: lib/Transforms/Scalar/ConstantHoisting.cpp =================================================================== --- lib/Transforms/Scalar/ConstantHoisting.cpp +++ lib/Transforms/Scalar/ConstantHoisting.cpp @@ -86,6 +86,12 @@ "consthoist-gep", cl::init(false), cl::Hidden, cl::desc("Try hoisting constant gep expressions")); +static cl::opt +MinNumOfDependentToRebase("consthoist-min-num-to-rebase", + cl::desc("Do not rebase if number of dependent constants of a Base is less " + "than this number."), + cl::init(2), cl::Hidden); + namespace { /// The constant hoisting pass. @@ -822,6 +828,7 @@ unsigned UsesNum = 0; unsigned ReBasesNum = 0; + unsigned NotRebasedNum = 0; for (Instruction *IP : IPSet) { Instruction *Base = nullptr; // Hoist and hide the base constant behind a bitcast. @@ -842,6 +849,9 @@ // Emit materialization code for all rebased constants. unsigned Uses = 0; + // First, collect dependent constants of Base. + using RebasedUse = std::tuple; + SmallVector ToBeRebased; for (auto const &RCI : ConstInfo.RebasedConstants) { for (auto const &U : RCI.Uses) { Uses++; @@ -850,26 +860,42 @@ // If Base constant is to be inserted in multiple places, // generate rebase for U using the Base dominating U. if (IPSet.size() == 1 || - DT->dominates(Base->getParent(), OrigMatInsertBB)) { - emitBaseConstants(Base, RCI.Offset, RCI.Ty, U); - ReBasesNum++; - } - - Base->setDebugLoc(DILocation::getMergedLocation( - Base->getDebugLoc(), U.Inst->getDebugLoc())); + DT->dominates(Base->getParent(), OrigMatInsertBB)) + ToBeRebased.push_back(RebasedUse(RCI.Offset, RCI.Ty, U)); } } UsesNum = Uses; - // Use the same debug location as the last user of the constant. + // If there is less than 2 constants depend on Base, skip rebasing, + // assuming Base and the rebased have the same materialization cost. + // Remove Base as well since it's not needed. + if (ToBeRebased.size() < MinNumOfDependentToRebase) { + Base->eraseFromParent(); + NotRebasedNum += ToBeRebased.size(); + continue; + } + + // ToBeRebased.size() >= 2, rebase the constants. + for (auto const &R : ToBeRebased) { + Constant *Off = std::get<0>(R); + Type *Ty = std::get<1>(R); + ConstantUser U = std::get<2>(R); + emitBaseConstants(Base, Off, Ty, U); + ReBasesNum++; + // Use the same debug location as the last user of the constant. + Base->setDebugLoc(DILocation::getMergedLocation( + Base->getDebugLoc(), U.Inst->getDebugLoc())); + } assert(!Base->use_empty() && "The use list is empty!?"); assert(isa(Base->user_back()) && "All uses should be instructions."); } (void)UsesNum; (void)ReBasesNum; + (void)NotRebasedNum; // Expect all uses are rebased after rebase is done. - assert(UsesNum == ReBasesNum && "Not all uses are rebased"); + assert(UsesNum == (ReBasesNum + NotRebasedNum) && + "Not all uses are rebased"); NumConstantsHoisted++; Index: test/CodeGen/Thumb/consthoist-single-dependent.ll =================================================================== --- /dev/null +++ test/CodeGen/Thumb/consthoist-single-dependent.ll @@ -0,0 +1,130 @@ +; RUN: llc %s -o - | FileCheck %s + +target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "thumbv6m-none-unknown-musleabi" + +@global = external dso_local local_unnamed_addr global i1, align 1 +@global.0 = external dso_local local_unnamed_addr global i1, align 1 + +; Test that constant 0 and 1 of i1 type is NOT hoisted due low +; materializing cost. +; CHECK-LABEL: avalon +; CHECK-DAG: movs r{{[0-9]+}}, #0 +; CHECK-DAG: movs r{{[0-9]+}}, #0 +; CHECK-DAG: movs r{{[0-9]+}}, #1 +; CHECK-NOT: add +; Function Attrs: nounwind optsize ssp +define dso_local void @avalon() #0 { +bb: + switch i8 undef, label %bb5 [ + i8 0, label %bb1 + i8 -1, label %bb2 + i8 1, label %bb3 + ] + +bb1: ; preds = %bb + store i1 1, i1* @global, align 1, !tbaa !1 + unreachable + +bb2: ; preds = %bb + store i1 0, i1* @global, align 1, !tbaa !1 + unreachable + +bb3: ; preds = %bb + store i1 0, i1* @global.0, align 1, !tbaa !1 + store i1 0, i1* @global, align 1, !tbaa !1 + unreachable + +bb5: ; preds = %bb + ret void +} + +@global.1 = external dso_local local_unnamed_addr global i8, align 1 +@global.2 = external dso_local local_unnamed_addr global i8, align 1 + +; Test that for i8 type, constant -1 is not rebased since it's the only +; dependent of base constant -2. +; CHECK-LABEL: barney +; CHECK-DAG: movs r{{[0-9]+}}, #254 +; CHECK-DAG: movs r{{[0-9]+}}, #255 +; CHECK-NOT: mvn +; CHECK-NOT: add +; Function Attrs: nounwind optsize ssp +define dso_local void @barney() #0 { +bb: + switch i8 undef, label %bb5 [ + i8 0, label %bb1 + i8 -1, label %bb2 + i8 1, label %bb3 + ] + +bb1: ; preds = %bb + store i8 -1, i8* @global.1, align 1, !tbaa !1 + unreachable + +bb2: ; preds = %bb + store i8 -2, i8* @global.1, align 1, !tbaa !1 + unreachable + +bb3: ; preds = %bb + store i8 -2, i8* @global.2, align 1, !tbaa !1 + store i8 -2, i8* @global.1, align 1, !tbaa !1 + unreachable + +bb5: ; preds = %bb + ret void +} + +@global.3 = external dso_local local_unnamed_addr global i16, align 2 +@global.4 = external dso_local local_unnamed_addr global i16, align 2 + +; Test that for i16 type constant 65532 is not rebased if it's the only +; dependent of base constant 65531. Cost would be the same if rebased. +; If rebased, 3 two-byte instructions: +; movs r0, #4 +; mvns r0, r0 +; adds r0, r0, #1 +; If NOT rebased, 1 two-byte instruction plus 1 four-byte CP entry: +; ldr r1, .LCPI2_3 +; ... +; .LCPI2_3: +; .long 65532 +; CHECK-LABEL: carla +; CHECK-DAG: ldr r{{[0-9]+}}, .LCPI2_1 +; CHECK-DAG: ldr r{{[0-9]+}}, .LCPI2_3 +; CHECK-NOT: mvn +; CHECK-NOT: add +; Function Attrs: nounwind optsize ssp +define dso_local void @carla() #0 { +bb: + switch i8 undef, label %bb5 [ + i8 0, label %bb1 + i8 -1, label %bb2 + i8 1, label %bb3 + ] + +bb1: ; preds = %bb + store i16 65532, i16* @global.3, align 1, !tbaa !1 + unreachable + +bb2: ; preds = %bb + store i16 65531, i16* @global.3, align 1, !tbaa !1 + unreachable + +bb3: ; preds = %bb + store i16 65531, i16* @global.4, align 1, !tbaa !1 + store i16 65531, i16* @global.3, align 1, !tbaa !1 + unreachable + +bb5: ; preds = %bb + ret void +} + +attributes #0 = { nounwind optsize ssp "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="cortex-m0" "target-features"="+armv6-m,+strict-align,+thumb-mode,-crc,-dotprod,-dsp,-fp16fml,-hwdiv,-hwdiv-arm,-ras" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.ident = !{!0} + +!0 = !{!"Snapdragon LLVM ARM Compiler 8.0.0 (based on LLVM 8.0.0)"} +!1 = !{!2, !2, i64 0} +!2 = !{!"omnipotent char", !3, i64 0} +!3 = !{!"Simple C/C++ TBAA"}