|
| 1 | +//===--- InstructionUtils.cpp - Utilities for SIL instructions ------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#define DEBUG_TYPE "sil-inst-utils" |
| 14 | +#include "swift/SIL/InstructionUtils.h" |
| 15 | +#include "swift/SIL/SILArgument.h" |
| 16 | +#include "swift/SIL/SILBasicBlock.h" |
| 17 | +#include "swift/SIL/Projection.h" |
| 18 | + |
| 19 | +using namespace swift; |
| 20 | + |
| 21 | +/// Strip off casts/indexing insts/address projections from V until there is |
| 22 | +/// nothing left to strip. |
| 23 | +/// FIXME: Why don't we strip projections after stripping indexes? |
| 24 | +SILValue swift::getUnderlyingObject(SILValue V) { |
| 25 | + while (true) { |
| 26 | + SILValue V2 = stripIndexingInsts(stripAddressProjections(stripCasts(V))); |
| 27 | + if (V2 == V) |
| 28 | + return V2; |
| 29 | + V = V2; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +static bool isRCIdentityPreservingCast(ValueKind Kind) { |
| 34 | + switch (Kind) { |
| 35 | + case ValueKind::UpcastInst: |
| 36 | + case ValueKind::UncheckedRefCastInst: |
| 37 | + case ValueKind::UncheckedRefCastAddrInst: |
| 38 | + case ValueKind::UnconditionalCheckedCastInst: |
| 39 | + case ValueKind::RefToBridgeObjectInst: |
| 40 | + case ValueKind::BridgeObjectToRefInst: |
| 41 | + return true; |
| 42 | + default: |
| 43 | + return false; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Return the underlying SILValue after stripping off identity SILArguments if |
| 48 | +/// we belong to a BB with one predecessor. |
| 49 | +SILValue swift::stripSinglePredecessorArgs(SILValue V) { |
| 50 | + while (true) { |
| 51 | + auto *A = dyn_cast<SILArgument>(V); |
| 52 | + if (!A) |
| 53 | + return V; |
| 54 | + |
| 55 | + SILBasicBlock *BB = A->getParent(); |
| 56 | + |
| 57 | + // First try and grab the single predecessor of our parent BB. If we don't |
| 58 | + // have one, bail. |
| 59 | + SILBasicBlock *Pred = BB->getSinglePredecessor(); |
| 60 | + if (!Pred) |
| 61 | + return V; |
| 62 | + |
| 63 | + // Then grab the terminator of Pred... |
| 64 | + TermInst *PredTI = Pred->getTerminator(); |
| 65 | + |
| 66 | + // And attempt to find our matching argument. |
| 67 | + // |
| 68 | + // *NOTE* We can only strip things here if we know that there is no semantic |
| 69 | + // change in terms of upcasts/downcasts/enum extraction since this is used |
| 70 | + // by other routines here. This means that we can only look through |
| 71 | + // cond_br/br. |
| 72 | + // |
| 73 | + // For instance, routines that use stripUpcasts() do not want to strip off a |
| 74 | + // downcast that results from checked_cast_br. |
| 75 | + if (auto *BI = dyn_cast<BranchInst>(PredTI)) { |
| 76 | + V = BI->getArg(A->getIndex()); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + if (auto *CBI = dyn_cast<CondBranchInst>(PredTI)) { |
| 81 | + if (SILValue Arg = CBI->getArgForDestBB(BB, A)) { |
| 82 | + V = Arg; |
| 83 | + continue; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return V; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +SILValue swift::stripCasts(SILValue V) { |
| 92 | + while (true) { |
| 93 | + V = stripSinglePredecessorArgs(V); |
| 94 | + |
| 95 | + auto K = V->getKind(); |
| 96 | + if (isRCIdentityPreservingCast(K) |
| 97 | + || K == ValueKind::UncheckedTrivialBitCastInst |
| 98 | + || K == ValueKind::MarkDependenceInst) { |
| 99 | + V = cast<SILInstruction>(V.getDef())->getOperand(0); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + return V; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +SILValue swift::stripUpCasts(SILValue V) { |
| 108 | + assert(V.getType().isClassOrClassMetatype() && |
| 109 | + "Expected class or class metatype!"); |
| 110 | + |
| 111 | + V = stripSinglePredecessorArgs(V); |
| 112 | + |
| 113 | + while (isa<UpcastInst>(V)) |
| 114 | + V = stripSinglePredecessorArgs(cast<UpcastInst>(V)->getOperand()); |
| 115 | + |
| 116 | + return V; |
| 117 | +} |
| 118 | + |
| 119 | +SILValue swift::stripClassCasts(SILValue V) { |
| 120 | + while (true) { |
| 121 | + if (auto *UI = dyn_cast<UpcastInst>(V)) { |
| 122 | + V = UI->getOperand(); |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (auto *UCCI = dyn_cast<UnconditionalCheckedCastInst>(V)) { |
| 127 | + V = UCCI->getOperand(); |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + return V; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +SILValue swift::stripAddressProjections(SILValue V) { |
| 136 | + while (true) { |
| 137 | + V = stripSinglePredecessorArgs(V); |
| 138 | + if (!NewProjection::isAddressProjection(V)) |
| 139 | + return V; |
| 140 | + V = cast<SILInstruction>(V.getDef())->getOperand(0); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +SILValue swift::stripValueProjections(SILValue V) { |
| 145 | + while (true) { |
| 146 | + V = stripSinglePredecessorArgs(V); |
| 147 | + if (!NewProjection::isObjectProjection(V)) |
| 148 | + return V; |
| 149 | + V = cast<SILInstruction>(V.getDef())->getOperand(0); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +SILValue swift::stripIndexingInsts(SILValue V) { |
| 154 | + while (true) { |
| 155 | + if (!isa<IndexingInst>(V.getDef())) |
| 156 | + return V; |
| 157 | + V = cast<IndexingInst>(V)->getBase(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +SILValue swift::stripExpectIntrinsic(SILValue V) { |
| 162 | + auto *BI = dyn_cast<BuiltinInst>(V); |
| 163 | + if (!BI) |
| 164 | + return V; |
| 165 | + if (BI->getIntrinsicInfo().ID != llvm::Intrinsic::expect) |
| 166 | + return V; |
| 167 | + return BI->getArguments()[0]; |
| 168 | +} |
0 commit comments