Skip to content
Merged
30 changes: 22 additions & 8 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6466,6 +6466,9 @@ class SwitchReplacement {
/// Return the default value of the switch.
Constant *getDefaultValue();

/// Return true if the replacement is a lookup table.
bool isLookupTable();

private:
// Depending on the switch, there are different alternatives.
enum {
Expand Down Expand Up @@ -6613,7 +6616,6 @@ SwitchReplacement::SwitchReplacement(
(void)M.smul_ov(APInt(M.getBitWidth(), TableSize - 1), MayWrap);
LinearMapValWrapped = NonMonotonic || MayWrap;
Kind = LinearMapKind;
++NumLinearMaps;
return;
}
}
Expand All @@ -6633,7 +6635,6 @@ SwitchReplacement::SwitchReplacement(
BitMap = ConstantInt::get(M.getContext(), TableInt);
BitMapElementTy = IT;
Kind = BitMapKind;
++NumBitMaps;
return;
}

Expand All @@ -6650,6 +6651,7 @@ Value *SwitchReplacement::replaceSwitch(Value *Index, IRBuilder<> &Builder,
case SingleValueKind:
return SingleValue;
case LinearMapKind: {
++NumLinearMaps;
// Derive the result value from the input value.
Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
false, "switch.idx.cast");
Expand All @@ -6665,6 +6667,7 @@ Value *SwitchReplacement::replaceSwitch(Value *Index, IRBuilder<> &Builder,
return Result;
}
case BitMapKind: {
++NumBitMaps;
// Type of the bitmap (e.g. i59).
IntegerType *MapTy = BitMap->getIntegerType();

Expand All @@ -6687,6 +6690,7 @@ Value *SwitchReplacement::replaceSwitch(Value *Index, IRBuilder<> &Builder,
return Builder.CreateTrunc(DownShifted, BitMapElementTy, "switch.masked");
}
case LookupTableKind: {
++NumLookupTables;
auto *Table =
new GlobalVariable(*Func->getParent(), Initializer->getType(),
/*isConstant=*/true, GlobalVariable::PrivateLinkage,
Expand Down Expand Up @@ -6753,6 +6757,8 @@ static bool isTypeLegalForLookupTable(Type *Ty, const TargetTransformInfo &TTI,

Constant *SwitchReplacement::getDefaultValue() { return DefaultValue; }

bool SwitchReplacement::isLookupTable() { return Kind == LookupTableKind; }

static bool isSwitchDense(uint64_t NumCases, uint64_t CaseRange) {
// 40% is the default density for building a jump table in optsize/minsize
// mode. See also TargetLoweringBase::isSuitableForJumpTable(), which this
Expand Down Expand Up @@ -6923,11 +6929,6 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,

BasicBlock *BB = SI->getParent();
Function *Fn = BB->getParent();
// Only build lookup table when we have a target that supports it or the
// attribute is not set.
if (!TTI.shouldBuildLookupTables() ||
(Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
return false;

// FIXME: If the switch is too sparse for a lookup table, perhaps we could
// split off a dense part and build a lookup table for that.
Expand Down Expand Up @@ -7086,6 +7087,20 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
PhiToReplacementMap.insert({PHI, Replacement});
}

bool AnyLookupTables = any_of(
PhiToReplacementMap, [](auto &KV) { return KV.second.isLookupTable(); });

// A few conditions prevent the generation of lookup tables:
// 1. The target does not support lookup tables.
// 2. The "no-jump-tables" function attribute is set.
// However, these objections do not apply to other switch replacements, like
// the bitmap, so we only stop here if any of these conditions are met and we
// want to create a LUT. Otherwise, continue with the switch replacement.
if (AnyLookupTables &&
(!TTI.shouldBuildLookupTables() ||
Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
return false;

Builder.SetInsertPoint(SI);
// TableIndex is the switch condition - TableIndexOffset if we don't
// use the condition directly
Expand Down Expand Up @@ -7227,7 +7242,6 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
if (DTU)
DTU->applyUpdates(Updates);

++NumLookupTables;
if (NeedMask)
++NumLookupTablesHoles;
return true;
Expand Down
Loading