|
| 1 | +//===--- TargetConstantFolding.cpp ----------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// This pass lowers loadable SILTypes. On completion, the SILType of every |
| 12 | +// function argument is an address instead of the type itself. |
| 13 | +// This reduces the code size. |
| 14 | +// Consequently, this pass is required for IRGen. |
| 15 | +// It is a mandatory IRGen preparation pass (not a diagnostic pass). |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#define DEBUG_TYPE "target-constant-folding" |
| 20 | +#include "IRGenModule.h" |
| 21 | +#include "swift/SIL/SILBuilder.h" |
| 22 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 23 | +#include "swift/SILOptimizer/Utils/InstructionDeleter.h" |
| 24 | +#include "llvm/Support/Debug.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | +using namespace swift::irgen; |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +/// Performs constant folding for target-specific values. |
| 32 | +/// |
| 33 | +/// Specifically, this optimization constant folds |
| 34 | +/// ``` |
| 35 | +/// MemoryLayout<S>.size |
| 36 | +/// MemoryLayout<S>.alignment |
| 37 | +/// MemoryLayout<S>.stride |
| 38 | +/// ``` |
| 39 | +/// Constant folding those expressions in the middle of the SIL pipeline |
| 40 | +/// enables other optimizations to e.g. allow such expressions in statically |
| 41 | +/// allocated global variables (done by the GlobalOpt pass). |
| 42 | +class TargetConstantFolding : public SILModuleTransform { |
| 43 | +private: |
| 44 | + /// The entry point to the transformation. |
| 45 | + void run() override { |
| 46 | + auto *irgenOpts = getModule()->getIRGenOptionsOrNull(); |
| 47 | + if (!irgenOpts) |
| 48 | + return; |
| 49 | + |
| 50 | + // We need an IRGenModule to get the actual sizes from type lowering. |
| 51 | + // Creating an IRGenModule involves some effort. Therefore this is a |
| 52 | + // module pass rather than a function pass so that this one-time setup |
| 53 | + // only needs to be done once and not for all functions in a module. |
| 54 | + IRGenerator irgen(*irgenOpts, *getModule()); |
| 55 | + auto targetMachine = irgen.createTargetMachine(); |
| 56 | + if (!targetMachine) |
| 57 | + return; |
| 58 | + IRGenModule IGM(irgen, std::move(targetMachine)); |
| 59 | + |
| 60 | + // Scan all instructions in the module for constant foldable instructions. |
| 61 | + for (SILFunction &function : *getModule()) { |
| 62 | + |
| 63 | + if (!function.shouldOptimize()) |
| 64 | + continue; |
| 65 | + |
| 66 | + bool changed = false; |
| 67 | + for (SILBasicBlock &block : function) { |
| 68 | + InstructionDeleter deleter; |
| 69 | + |
| 70 | + for (SILInstruction *inst : deleter.updatingRange(&block)) { |
| 71 | + if (constFold(inst, IGM)) { |
| 72 | + deleter.forceDelete(inst); |
| 73 | + changed = true; |
| 74 | + } |
| 75 | + } |
| 76 | + deleter.cleanupDeadInstructions(); |
| 77 | + } |
| 78 | + if (changed) { |
| 79 | + invalidateAnalysis(&function, SILAnalysis::InvalidationKind::Instructions); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Constant fold a single instruction. |
| 85 | + /// |
| 86 | + /// Returns true if `inst` was replaced and can be deleted. |
| 87 | + bool constFold(SILInstruction *inst, IRGenModule &IGM) { |
| 88 | + auto *bi = dyn_cast<BuiltinInst>(inst); |
| 89 | + if (!bi) |
| 90 | + return false; |
| 91 | + |
| 92 | + llvm::Constant *c = nullptr; |
| 93 | + uint64_t offset = 0; |
| 94 | + |
| 95 | + switch (bi->getBuiltinInfo().ID) { |
| 96 | + case BuiltinValueKind::Sizeof: |
| 97 | + c = getTypeInfoOfBuiltin(bi, IGM).getStaticSize(IGM); |
| 98 | + break; |
| 99 | + case BuiltinValueKind::Alignof: |
| 100 | + c = getTypeInfoOfBuiltin(bi, IGM).getStaticAlignmentMask(IGM); |
| 101 | + // The constant is the alignment _mask_. We get the actual alignment by |
| 102 | + // adding 1. |
| 103 | + offset = 1; |
| 104 | + break; |
| 105 | + case BuiltinValueKind::Strideof: |
| 106 | + c = getTypeInfoOfBuiltin(bi, IGM).getStaticStride(IGM); |
| 107 | + break; |
| 108 | + default: |
| 109 | + return false; |
| 110 | + } |
| 111 | + auto *intConst = dyn_cast_or_null<llvm::ConstantInt>(c); |
| 112 | + if (!intConst) |
| 113 | + return false; |
| 114 | + |
| 115 | + APInt value = intConst->getValue(); |
| 116 | + value += APInt(value.getBitWidth(), offset); |
| 117 | + |
| 118 | + // Replace the builtin by an integer literal. |
| 119 | + SILBuilderWithScope builder(bi); |
| 120 | + auto *intLit = builder.createIntegerLiteral(bi->getLoc(), bi->getType(), |
| 121 | + value); |
| 122 | + bi->replaceAllUsesWith(intLit); |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + const TypeInfo &getTypeInfoOfBuiltin(BuiltinInst *bi, IRGenModule &IGM) { |
| 127 | + SubstitutionMap subs = bi->getSubstitutions(); |
| 128 | + SILType lowered = IGM.getLoweredType(subs.getReplacementTypes()[0]); |
| 129 | + return IGM.getTypeInfo(lowered); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +} // end anonymous namespace |
| 134 | + |
| 135 | +SILTransform *swift::createTargetConstantFolding() { |
| 136 | + return new TargetConstantFolding(); |
| 137 | +} |
0 commit comments