Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1 | //===- WholeProgramDevirt.cpp - Whole program virtual call optimization ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements whole program optimization of virtual calls in cases |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 11 | // where we know (via !type metadata) that the list of callees is fixed. This |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 12 | // includes the following: |
| 13 | // - Single implementation devirtualization: if a virtual call has a single |
| 14 | // possible callee, replace all calls with a direct call to that callee. |
| 15 | // - Virtual constant propagation: if the virtual function's return type is an |
| 16 | // integer <=64 bits and all possible callees are readnone, for each class and |
| 17 | // each list of constant arguments: evaluate the function, store the return |
| 18 | // value alongside the virtual table, and rewrite each virtual call as a load |
| 19 | // from the virtual table. |
| 20 | // - Uniform return value optimization: if the conditions for virtual constant |
| 21 | // propagation hold and each function returns the same constant value, replace |
| 22 | // each virtual call with that constant. |
| 23 | // - Unique return value optimization for i1 return values: if the conditions |
| 24 | // for virtual constant propagation hold and a single vtable's function |
| 25 | // returns 0, or a single vtable's function returns 1, replace each virtual |
| 26 | // call with a comparison of the vptr against that vtable's address. |
| 27 | // |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 28 | // This pass is intended to be used during the regular and thin LTO pipelines. |
| 29 | // During regular LTO, the pass determines the best optimization for each |
| 30 | // virtual call and applies the resolutions directly to virtual calls that are |
| 31 | // eligible for virtual call optimization (i.e. calls that use either of the |
| 32 | // llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics). During |
| 33 | // ThinLTO, the pass operates in two phases: |
| 34 | // - Export phase: this is run during the thin link over a single merged module |
| 35 | // that contains all vtables with !type metadata that participate in the link. |
| 36 | // The pass computes a resolution for each virtual call and stores it in the |
| 37 | // type identifier summary. |
| 38 | // - Import phase: this is run during the thin backends over the individual |
| 39 | // modules. The pass applies the resolutions previously computed during the |
| 40 | // import phase to each eligible virtual call. |
| 41 | // |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | #include "llvm/Transforms/IPO/WholeProgramDevirt.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/ArrayRef.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/DenseMap.h" |
| 47 | #include "llvm/ADT/DenseMapInfo.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/DenseSet.h" |
| 49 | #include "llvm/ADT/MapVector.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/iterator_range.h" |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/AliasAnalysis.h" |
| 53 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 54 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 55 | #include "llvm/Analysis/TypeMetadataUtils.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 56 | #include "llvm/IR/CallSite.h" |
| 57 | #include "llvm/IR/Constants.h" |
| 58 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 59 | #include "llvm/IR/DebugLoc.h" |
| 60 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Function.h" |
| 62 | #include "llvm/IR/GlobalAlias.h" |
| 63 | #include "llvm/IR/GlobalVariable.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 64 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 65 | #include "llvm/IR/InstrTypes.h" |
| 66 | #include "llvm/IR/Instruction.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Instructions.h" |
| 68 | #include "llvm/IR/Intrinsics.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 69 | #include "llvm/IR/LLVMContext.h" |
| 70 | #include "llvm/IR/Metadata.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 71 | #include "llvm/IR/Module.h" |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 72 | #include "llvm/IR/ModuleSummaryIndexYAML.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 73 | #include "llvm/Pass.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 74 | #include "llvm/PassRegistry.h" |
| 75 | #include "llvm/PassSupport.h" |
| 76 | #include "llvm/Support/Casting.h" |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Error.h" |
| 78 | #include "llvm/Support/FileSystem.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 79 | #include "llvm/Support/MathExtras.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 80 | #include "llvm/Transforms/IPO.h" |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 81 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/Utils/Evaluator.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 83 | #include <algorithm> |
| 84 | #include <cstddef> |
| 85 | #include <map> |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 86 | #include <set> |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 87 | #include <string> |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 88 | |
| 89 | using namespace llvm; |
| 90 | using namespace wholeprogramdevirt; |
| 91 | |
| 92 | #define DEBUG_TYPE "wholeprogramdevirt" |
| 93 | |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 94 | static cl::opt<PassSummaryAction> ClSummaryAction( |
| 95 | "wholeprogramdevirt-summary-action", |
| 96 | cl::desc("What to do with the summary when running this pass"), |
| 97 | cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"), |
| 98 | clEnumValN(PassSummaryAction::Import, "import", |
| 99 | "Import typeid resolutions from summary and globals"), |
| 100 | clEnumValN(PassSummaryAction::Export, "export", |
| 101 | "Export typeid resolutions to summary and globals")), |
| 102 | cl::Hidden); |
| 103 | |
| 104 | static cl::opt<std::string> ClReadSummary( |
| 105 | "wholeprogramdevirt-read-summary", |
| 106 | cl::desc("Read summary from given YAML file before running pass"), |
| 107 | cl::Hidden); |
| 108 | |
| 109 | static cl::opt<std::string> ClWriteSummary( |
| 110 | "wholeprogramdevirt-write-summary", |
| 111 | cl::desc("Write summary to given YAML file after running pass"), |
| 112 | cl::Hidden); |
| 113 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 114 | // Find the minimum offset that we may store a value of size Size bits at. If |
| 115 | // IsAfter is set, look for an offset before the object, otherwise look for an |
| 116 | // offset after the object. |
| 117 | uint64_t |
| 118 | wholeprogramdevirt::findLowestOffset(ArrayRef<VirtualCallTarget> Targets, |
| 119 | bool IsAfter, uint64_t Size) { |
| 120 | // Find a minimum offset taking into account only vtable sizes. |
| 121 | uint64_t MinByte = 0; |
| 122 | for (const VirtualCallTarget &Target : Targets) { |
| 123 | if (IsAfter) |
| 124 | MinByte = std::max(MinByte, Target.minAfterBytes()); |
| 125 | else |
| 126 | MinByte = std::max(MinByte, Target.minBeforeBytes()); |
| 127 | } |
| 128 | |
| 129 | // Build a vector of arrays of bytes covering, for each target, a slice of the |
| 130 | // used region (see AccumBitVector::BytesUsed in |
| 131 | // llvm/Transforms/IPO/WholeProgramDevirt.h) starting at MinByte. Effectively, |
| 132 | // this aligns the used regions to start at MinByte. |
| 133 | // |
| 134 | // In this example, A, B and C are vtables, # is a byte already allocated for |
| 135 | // a virtual function pointer, AAAA... (etc.) are the used regions for the |
| 136 | // vtables and Offset(X) is the value computed for the Offset variable below |
| 137 | // for X. |
| 138 | // |
| 139 | // Offset(A) |
| 140 | // | | |
| 141 | // |MinByte |
| 142 | // A: ################AAAAAAAA|AAAAAAAA |
| 143 | // B: ########BBBBBBBBBBBBBBBB|BBBB |
| 144 | // C: ########################|CCCCCCCCCCCCCCCC |
| 145 | // | Offset(B) | |
| 146 | // |
| 147 | // This code produces the slices of A, B and C that appear after the divider |
| 148 | // at MinByte. |
| 149 | std::vector<ArrayRef<uint8_t>> Used; |
| 150 | for (const VirtualCallTarget &Target : Targets) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 151 | ArrayRef<uint8_t> VTUsed = IsAfter ? Target.TM->Bits->After.BytesUsed |
| 152 | : Target.TM->Bits->Before.BytesUsed; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 153 | uint64_t Offset = IsAfter ? MinByte - Target.minAfterBytes() |
| 154 | : MinByte - Target.minBeforeBytes(); |
| 155 | |
| 156 | // Disregard used regions that are smaller than Offset. These are |
| 157 | // effectively all-free regions that do not need to be checked. |
| 158 | if (VTUsed.size() > Offset) |
| 159 | Used.push_back(VTUsed.slice(Offset)); |
| 160 | } |
| 161 | |
| 162 | if (Size == 1) { |
| 163 | // Find a free bit in each member of Used. |
| 164 | for (unsigned I = 0;; ++I) { |
| 165 | uint8_t BitsUsed = 0; |
| 166 | for (auto &&B : Used) |
| 167 | if (I < B.size()) |
| 168 | BitsUsed |= B[I]; |
| 169 | if (BitsUsed != 0xff) |
| 170 | return (MinByte + I) * 8 + |
| 171 | countTrailingZeros(uint8_t(~BitsUsed), ZB_Undefined); |
| 172 | } |
| 173 | } else { |
| 174 | // Find a free (Size/8) byte region in each member of Used. |
| 175 | // FIXME: see if alignment helps. |
| 176 | for (unsigned I = 0;; ++I) { |
| 177 | for (auto &&B : Used) { |
| 178 | unsigned Byte = 0; |
| 179 | while ((I + Byte) < B.size() && Byte < (Size / 8)) { |
| 180 | if (B[I + Byte]) |
| 181 | goto NextI; |
| 182 | ++Byte; |
| 183 | } |
| 184 | } |
| 185 | return (MinByte + I) * 8; |
| 186 | NextI:; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void wholeprogramdevirt::setBeforeReturnValues( |
| 192 | MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocBefore, |
| 193 | unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) { |
| 194 | if (BitWidth == 1) |
| 195 | OffsetByte = -(AllocBefore / 8 + 1); |
| 196 | else |
| 197 | OffsetByte = -((AllocBefore + 7) / 8 + (BitWidth + 7) / 8); |
| 198 | OffsetBit = AllocBefore % 8; |
| 199 | |
| 200 | for (VirtualCallTarget &Target : Targets) { |
| 201 | if (BitWidth == 1) |
| 202 | Target.setBeforeBit(AllocBefore); |
| 203 | else |
| 204 | Target.setBeforeBytes(AllocBefore, (BitWidth + 7) / 8); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void wholeprogramdevirt::setAfterReturnValues( |
| 209 | MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocAfter, |
| 210 | unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) { |
| 211 | if (BitWidth == 1) |
| 212 | OffsetByte = AllocAfter / 8; |
| 213 | else |
| 214 | OffsetByte = (AllocAfter + 7) / 8; |
| 215 | OffsetBit = AllocAfter % 8; |
| 216 | |
| 217 | for (VirtualCallTarget &Target : Targets) { |
| 218 | if (BitWidth == 1) |
| 219 | Target.setAfterBit(AllocAfter); |
| 220 | else |
| 221 | Target.setAfterBytes(AllocAfter, (BitWidth + 7) / 8); |
| 222 | } |
| 223 | } |
| 224 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 225 | VirtualCallTarget::VirtualCallTarget(Function *Fn, const TypeMemberInfo *TM) |
| 226 | : Fn(Fn), TM(TM), |
Ivan Krasin | 89439a7 | 2016-08-12 01:40:10 +0000 | [diff] [blame] | 227 | IsBigEndian(Fn->getParent()->getDataLayout().isBigEndian()), WasDevirt(false) {} |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 228 | |
| 229 | namespace { |
| 230 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 231 | // A slot in a set of virtual tables. The TypeID identifies the set of virtual |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 232 | // tables, and the ByteOffset is the offset in bytes from the address point to |
| 233 | // the virtual function pointer. |
| 234 | struct VTableSlot { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 235 | Metadata *TypeID; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 236 | uint64_t ByteOffset; |
| 237 | }; |
| 238 | |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 239 | } // end anonymous namespace |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 240 | |
Peter Collingbourne | 9b65652 | 2016-02-09 23:01:38 +0000 | [diff] [blame] | 241 | namespace llvm { |
| 242 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 243 | template <> struct DenseMapInfo<VTableSlot> { |
| 244 | static VTableSlot getEmptyKey() { |
| 245 | return {DenseMapInfo<Metadata *>::getEmptyKey(), |
| 246 | DenseMapInfo<uint64_t>::getEmptyKey()}; |
| 247 | } |
| 248 | static VTableSlot getTombstoneKey() { |
| 249 | return {DenseMapInfo<Metadata *>::getTombstoneKey(), |
| 250 | DenseMapInfo<uint64_t>::getTombstoneKey()}; |
| 251 | } |
| 252 | static unsigned getHashValue(const VTableSlot &I) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 253 | return DenseMapInfo<Metadata *>::getHashValue(I.TypeID) ^ |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 254 | DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset); |
| 255 | } |
| 256 | static bool isEqual(const VTableSlot &LHS, |
| 257 | const VTableSlot &RHS) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 258 | return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 259 | } |
| 260 | }; |
| 261 | |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 262 | } // end namespace llvm |
Peter Collingbourne | 9b65652 | 2016-02-09 23:01:38 +0000 | [diff] [blame] | 263 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 264 | namespace { |
| 265 | |
| 266 | // A virtual call site. VTable is the loaded virtual table pointer, and CS is |
| 267 | // the indirect virtual call. |
| 268 | struct VirtualCallSite { |
| 269 | Value *VTable; |
| 270 | CallSite CS; |
| 271 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 272 | // If non-null, this field points to the associated unsafe use count stored in |
| 273 | // the DevirtModule::NumUnsafeUsesForTypeTest map below. See the description |
| 274 | // of that field for details. |
| 275 | unsigned *NumUnsafeUses; |
| 276 | |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 277 | void |
| 278 | emitRemark(const StringRef OptName, const StringRef TargetName, |
| 279 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter) { |
Ivan Krasin | 5474645 | 2016-07-12 02:38:37 +0000 | [diff] [blame] | 280 | Function *F = CS.getCaller(); |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 281 | DebugLoc DLoc = CS->getDebugLoc(); |
| 282 | BasicBlock *Block = CS.getParent(); |
| 283 | |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 284 | using namespace ore; |
Peter Collingbourne | 9110cb4 | 2018-01-05 00:27:51 +0000 | [diff] [blame^] | 285 | OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, OptName, DLoc, Block) |
| 286 | << NV("Optimization", OptName) |
| 287 | << ": devirtualized a call to " |
| 288 | << NV("FunctionName", TargetName)); |
Ivan Krasin | 5474645 | 2016-07-12 02:38:37 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 291 | void replaceAndErase( |
| 292 | const StringRef OptName, const StringRef TargetName, bool RemarksEnabled, |
| 293 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter, |
| 294 | Value *New) { |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 295 | if (RemarksEnabled) |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 296 | emitRemark(OptName, TargetName, OREGetter); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 297 | CS->replaceAllUsesWith(New); |
| 298 | if (auto II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 299 | BranchInst::Create(II->getNormalDest(), CS.getInstruction()); |
| 300 | II->getUnwindDest()->removePredecessor(II->getParent()); |
| 301 | } |
| 302 | CS->eraseFromParent(); |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 303 | // This use is no longer unsafe. |
| 304 | if (NumUnsafeUses) |
| 305 | --*NumUnsafeUses; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 306 | } |
| 307 | }; |
| 308 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 309 | // Call site information collected for a specific VTableSlot and possibly a list |
| 310 | // of constant integer arguments. The grouping by arguments is handled by the |
| 311 | // VTableSlotInfo class. |
| 312 | struct CallSiteInfo { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 313 | /// The set of call sites for this slot. Used during regular LTO and the |
| 314 | /// import phase of ThinLTO (as well as the export phase of ThinLTO for any |
| 315 | /// call sites that appear in the merged module itself); in each of these |
| 316 | /// cases we are directly operating on the call sites at the IR level. |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 317 | std::vector<VirtualCallSite> CallSites; |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 318 | |
| 319 | // These fields are used during the export phase of ThinLTO and reflect |
| 320 | // information collected from function summaries. |
| 321 | |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 322 | /// Whether any function summary contains an llvm.assume(llvm.type.test) for |
| 323 | /// this slot. |
| 324 | bool SummaryHasTypeTestAssumeUsers; |
| 325 | |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 326 | /// CFI-specific: a vector containing the list of function summaries that use |
| 327 | /// the llvm.type.checked.load intrinsic and therefore will require |
| 328 | /// resolutions for llvm.type.test in order to implement CFI checks if |
| 329 | /// devirtualization was unsuccessful. If devirtualization was successful, the |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 330 | /// pass will clear this vector by calling markDevirt(). If at the end of the |
| 331 | /// pass the vector is non-empty, we will need to add a use of llvm.type.test |
| 332 | /// to each of the function summaries in the vector. |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 333 | std::vector<FunctionSummary *> SummaryTypeCheckedLoadUsers; |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 334 | |
| 335 | bool isExported() const { |
| 336 | return SummaryHasTypeTestAssumeUsers || |
| 337 | !SummaryTypeCheckedLoadUsers.empty(); |
| 338 | } |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 339 | |
| 340 | /// As explained in the comment for SummaryTypeCheckedLoadUsers. |
| 341 | void markDevirt() { SummaryTypeCheckedLoadUsers.clear(); } |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | // Call site information collected for a specific VTableSlot. |
| 345 | struct VTableSlotInfo { |
| 346 | // The set of call sites which do not have all constant integer arguments |
| 347 | // (excluding "this"). |
| 348 | CallSiteInfo CSInfo; |
| 349 | |
| 350 | // The set of call sites with all constant integer arguments (excluding |
| 351 | // "this"), grouped by argument list. |
| 352 | std::map<std::vector<uint64_t>, CallSiteInfo> ConstCSInfo; |
| 353 | |
| 354 | void addCallSite(Value *VTable, CallSite CS, unsigned *NumUnsafeUses); |
| 355 | |
| 356 | private: |
| 357 | CallSiteInfo &findCallSiteInfo(CallSite CS); |
| 358 | }; |
| 359 | |
| 360 | CallSiteInfo &VTableSlotInfo::findCallSiteInfo(CallSite CS) { |
| 361 | std::vector<uint64_t> Args; |
| 362 | auto *CI = dyn_cast<IntegerType>(CS.getType()); |
| 363 | if (!CI || CI->getBitWidth() > 64 || CS.arg_empty()) |
| 364 | return CSInfo; |
| 365 | for (auto &&Arg : make_range(CS.arg_begin() + 1, CS.arg_end())) { |
| 366 | auto *CI = dyn_cast<ConstantInt>(Arg); |
| 367 | if (!CI || CI->getBitWidth() > 64) |
| 368 | return CSInfo; |
| 369 | Args.push_back(CI->getZExtValue()); |
| 370 | } |
| 371 | return ConstCSInfo[Args]; |
| 372 | } |
| 373 | |
| 374 | void VTableSlotInfo::addCallSite(Value *VTable, CallSite CS, |
| 375 | unsigned *NumUnsafeUses) { |
| 376 | findCallSiteInfo(CS).CallSites.push_back({VTable, CS, NumUnsafeUses}); |
| 377 | } |
| 378 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 379 | struct DevirtModule { |
| 380 | Module &M; |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 381 | function_ref<AAResults &(Function &)> AARGetter; |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 382 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 383 | ModuleSummaryIndex *ExportSummary; |
| 384 | const ModuleSummaryIndex *ImportSummary; |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 385 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 386 | IntegerType *Int8Ty; |
| 387 | PointerType *Int8PtrTy; |
| 388 | IntegerType *Int32Ty; |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 389 | IntegerType *Int64Ty; |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 390 | IntegerType *IntPtrTy; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 391 | |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 392 | bool RemarksEnabled; |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 393 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter; |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 394 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 395 | MapVector<VTableSlot, VTableSlotInfo> CallSlots; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 396 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 397 | // This map keeps track of the number of "unsafe" uses of a loaded function |
| 398 | // pointer. The key is the associated llvm.type.test intrinsic call generated |
| 399 | // by this pass. An unsafe use is one that calls the loaded function pointer |
| 400 | // directly. Every time we eliminate an unsafe use (for example, by |
| 401 | // devirtualizing it or by applying virtual constant propagation), we |
| 402 | // decrement the value stored in this map. If a value reaches zero, we can |
| 403 | // eliminate the type check by RAUWing the associated llvm.type.test call with |
| 404 | // true. |
| 405 | std::map<CallInst *, unsigned> NumUnsafeUsesForTypeTest; |
| 406 | |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 407 | DevirtModule(Module &M, function_ref<AAResults &(Function &)> AARGetter, |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 408 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter, |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 409 | ModuleSummaryIndex *ExportSummary, |
| 410 | const ModuleSummaryIndex *ImportSummary) |
| 411 | : M(M), AARGetter(AARGetter), ExportSummary(ExportSummary), |
| 412 | ImportSummary(ImportSummary), Int8Ty(Type::getInt8Ty(M.getContext())), |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 413 | Int8PtrTy(Type::getInt8PtrTy(M.getContext())), |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 414 | Int32Ty(Type::getInt32Ty(M.getContext())), |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 415 | Int64Ty(Type::getInt64Ty(M.getContext())), |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 416 | IntPtrTy(M.getDataLayout().getIntPtrType(M.getContext(), 0)), |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 417 | RemarksEnabled(areRemarksEnabled()), OREGetter(OREGetter) { |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 418 | assert(!(ExportSummary && ImportSummary)); |
| 419 | } |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 420 | |
| 421 | bool areRemarksEnabled(); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 422 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 423 | void scanTypeTestUsers(Function *TypeTestFunc, Function *AssumeFunc); |
| 424 | void scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc); |
| 425 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 426 | void buildTypeIdentifierMap( |
| 427 | std::vector<VTableBits> &Bits, |
| 428 | DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap); |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 429 | Constant *getPointerAtOffset(Constant *I, uint64_t Offset); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 430 | bool |
| 431 | tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot, |
| 432 | const std::set<TypeMemberInfo> &TypeMemberInfos, |
| 433 | uint64_t ByteOffset); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 434 | |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 435 | void applySingleImplDevirt(VTableSlotInfo &SlotInfo, Constant *TheFn, |
| 436 | bool &IsExported); |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 437 | bool trySingleImplDevirt(MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 438 | VTableSlotInfo &SlotInfo, |
| 439 | WholeProgramDevirtResolution *Res); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 440 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 441 | bool tryEvaluateFunctionsWithArgs( |
| 442 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 443 | ArrayRef<uint64_t> Args); |
| 444 | |
| 445 | void applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, |
| 446 | uint64_t TheRetVal); |
| 447 | bool tryUniformRetValOpt(MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 77a8d56 | 2017-03-04 01:34:53 +0000 | [diff] [blame] | 448 | CallSiteInfo &CSInfo, |
| 449 | WholeProgramDevirtResolution::ByArg *Res); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 450 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 451 | // Returns the global symbol name that is used to export information about the |
| 452 | // given vtable slot and list of arguments. |
| 453 | std::string getGlobalName(VTableSlot Slot, ArrayRef<uint64_t> Args, |
| 454 | StringRef Name); |
| 455 | |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 456 | bool shouldExportConstantsAsAbsoluteSymbols(); |
| 457 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 458 | // This function is called during the export phase to create a symbol |
| 459 | // definition containing information about the given vtable slot and list of |
| 460 | // arguments. |
| 461 | void exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name, |
| 462 | Constant *C); |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 463 | void exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name, |
| 464 | uint32_t Const, uint32_t &Storage); |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 465 | |
| 466 | // This function is called during the import phase to create a reference to |
| 467 | // the symbol definition created during the export phase. |
| 468 | Constant *importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 469 | StringRef Name); |
| 470 | Constant *importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, |
| 471 | StringRef Name, IntegerType *IntTy, |
| 472 | uint32_t Storage); |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 473 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 474 | void applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, bool IsOne, |
| 475 | Constant *UniqueMemberAddr); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 476 | bool tryUniqueRetValOpt(unsigned BitWidth, |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 477 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 478 | CallSiteInfo &CSInfo, |
| 479 | WholeProgramDevirtResolution::ByArg *Res, |
| 480 | VTableSlot Slot, ArrayRef<uint64_t> Args); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 481 | |
| 482 | void applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName, |
| 483 | Constant *Byte, Constant *Bit); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 484 | bool tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 77a8d56 | 2017-03-04 01:34:53 +0000 | [diff] [blame] | 485 | VTableSlotInfo &SlotInfo, |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 486 | WholeProgramDevirtResolution *Res, VTableSlot Slot); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 487 | |
| 488 | void rebuildGlobal(VTableBits &B); |
| 489 | |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 490 | // Apply the summary resolution for Slot to all virtual calls in SlotInfo. |
| 491 | void importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo); |
| 492 | |
| 493 | // If we were able to eliminate all unsafe uses for a type checked load, |
| 494 | // eliminate the associated type tests by replacing them with true. |
| 495 | void removeRedundantTypeTests(); |
| 496 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 497 | bool run(); |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 498 | |
| 499 | // Lower the module using the action and summary passed as command line |
| 500 | // arguments. For testing purposes only. |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 501 | static bool runForTesting( |
| 502 | Module &M, function_ref<AAResults &(Function &)> AARGetter, |
| 503 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | struct WholeProgramDevirt : public ModulePass { |
| 507 | static char ID; |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 508 | |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 509 | bool UseCommandLine = false; |
| 510 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 511 | ModuleSummaryIndex *ExportSummary; |
| 512 | const ModuleSummaryIndex *ImportSummary; |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 513 | |
| 514 | WholeProgramDevirt() : ModulePass(ID), UseCommandLine(true) { |
| 515 | initializeWholeProgramDevirtPass(*PassRegistry::getPassRegistry()); |
| 516 | } |
| 517 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 518 | WholeProgramDevirt(ModuleSummaryIndex *ExportSummary, |
| 519 | const ModuleSummaryIndex *ImportSummary) |
| 520 | : ModulePass(ID), ExportSummary(ExportSummary), |
| 521 | ImportSummary(ImportSummary) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 522 | initializeWholeProgramDevirtPass(*PassRegistry::getPassRegistry()); |
| 523 | } |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 524 | |
| 525 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 526 | if (skipModule(M)) |
| 527 | return false; |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 528 | |
Peter Collingbourne | 9110cb4 | 2018-01-05 00:27:51 +0000 | [diff] [blame^] | 529 | // In the new pass manager, we can request the optimization |
| 530 | // remark emitter pass on a per-function-basis, which the |
| 531 | // OREGetter will do for us. |
| 532 | // In the old pass manager, this is harder, so we just build |
| 533 | // an optimization remark emitter on the fly, when we need it. |
| 534 | std::unique_ptr<OptimizationRemarkEmitter> ORE; |
| 535 | auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & { |
| 536 | ORE = make_unique<OptimizationRemarkEmitter>(F); |
| 537 | return *ORE; |
| 538 | }; |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 539 | |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 540 | if (UseCommandLine) |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 541 | return DevirtModule::runForTesting(M, LegacyAARGetter(*this), OREGetter); |
| 542 | |
| 543 | return DevirtModule(M, LegacyAARGetter(*this), OREGetter, ExportSummary, |
| 544 | ImportSummary) |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 545 | .run(); |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 549 | AU.addRequired<AssumptionCacheTracker>(); |
| 550 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 551 | } |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 554 | } // end anonymous namespace |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 555 | |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 556 | INITIALIZE_PASS_BEGIN(WholeProgramDevirt, "wholeprogramdevirt", |
| 557 | "Whole program devirtualization", false, false) |
| 558 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 559 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 560 | INITIALIZE_PASS_END(WholeProgramDevirt, "wholeprogramdevirt", |
| 561 | "Whole program devirtualization", false, false) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 562 | char WholeProgramDevirt::ID = 0; |
| 563 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 564 | ModulePass * |
| 565 | llvm::createWholeProgramDevirtPass(ModuleSummaryIndex *ExportSummary, |
| 566 | const ModuleSummaryIndex *ImportSummary) { |
| 567 | return new WholeProgramDevirt(ExportSummary, ImportSummary); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 570 | PreservedAnalyses WholeProgramDevirtPass::run(Module &M, |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 571 | ModuleAnalysisManager &AM) { |
| 572 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 573 | auto AARGetter = [&](Function &F) -> AAResults & { |
| 574 | return FAM.getResult<AAManager>(F); |
| 575 | }; |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 576 | auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & { |
| 577 | return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F); |
| 578 | }; |
| 579 | if (!DevirtModule(M, AARGetter, OREGetter, nullptr, nullptr).run()) |
Davide Italiano | d737dd2 | 2016-06-14 21:44:19 +0000 | [diff] [blame] | 580 | return PreservedAnalyses::all(); |
| 581 | return PreservedAnalyses::none(); |
| 582 | } |
| 583 | |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 584 | bool DevirtModule::runForTesting( |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 585 | Module &M, function_ref<AAResults &(Function &)> AARGetter, |
| 586 | function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter) { |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 587 | ModuleSummaryIndex Summary; |
| 588 | |
| 589 | // Handle the command-line summary arguments. This code is for testing |
| 590 | // purposes only, so we handle errors directly. |
| 591 | if (!ClReadSummary.empty()) { |
| 592 | ExitOnError ExitOnErr("-wholeprogramdevirt-read-summary: " + ClReadSummary + |
| 593 | ": "); |
| 594 | auto ReadSummaryFile = |
| 595 | ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary))); |
| 596 | |
| 597 | yaml::Input In(ReadSummaryFile->getBuffer()); |
| 598 | In >> Summary; |
| 599 | ExitOnErr(errorCodeToError(In.error())); |
| 600 | } |
| 601 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 602 | bool Changed = |
| 603 | DevirtModule( |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 604 | M, AARGetter, OREGetter, |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 605 | ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr, |
| 606 | ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr) |
| 607 | .run(); |
Peter Collingbourne | 2b33f65 | 2017-02-13 19:26:18 +0000 | [diff] [blame] | 608 | |
| 609 | if (!ClWriteSummary.empty()) { |
| 610 | ExitOnError ExitOnErr( |
| 611 | "-wholeprogramdevirt-write-summary: " + ClWriteSummary + ": "); |
| 612 | std::error_code EC; |
| 613 | raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::F_Text); |
| 614 | ExitOnErr(errorCodeToError(EC)); |
| 615 | |
| 616 | yaml::Output Out(OS); |
| 617 | Out << Summary; |
| 618 | } |
| 619 | |
| 620 | return Changed; |
| 621 | } |
| 622 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 623 | void DevirtModule::buildTypeIdentifierMap( |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 624 | std::vector<VTableBits> &Bits, |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 625 | DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 626 | DenseMap<GlobalVariable *, VTableBits *> GVToBits; |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 627 | Bits.reserve(M.getGlobalList().size()); |
| 628 | SmallVector<MDNode *, 2> Types; |
| 629 | for (GlobalVariable &GV : M.globals()) { |
| 630 | Types.clear(); |
| 631 | GV.getMetadata(LLVMContext::MD_type, Types); |
| 632 | if (Types.empty()) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 633 | continue; |
| 634 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 635 | VTableBits *&BitsPtr = GVToBits[&GV]; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 636 | if (!BitsPtr) { |
| 637 | Bits.emplace_back(); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 638 | Bits.back().GV = &GV; |
| 639 | Bits.back().ObjectSize = |
| 640 | M.getDataLayout().getTypeAllocSize(GV.getInitializer()->getType()); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 641 | BitsPtr = &Bits.back(); |
| 642 | } |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 643 | |
| 644 | for (MDNode *Type : Types) { |
| 645 | auto TypeID = Type->getOperand(1).get(); |
| 646 | |
| 647 | uint64_t Offset = |
| 648 | cast<ConstantInt>( |
| 649 | cast<ConstantAsMetadata>(Type->getOperand(0))->getValue()) |
| 650 | ->getZExtValue(); |
| 651 | |
| 652 | TypeIdMap[TypeID].insert({BitsPtr, Offset}); |
| 653 | } |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 657 | Constant *DevirtModule::getPointerAtOffset(Constant *I, uint64_t Offset) { |
| 658 | if (I->getType()->isPointerTy()) { |
| 659 | if (Offset == 0) |
| 660 | return I; |
| 661 | return nullptr; |
| 662 | } |
| 663 | |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 664 | const DataLayout &DL = M.getDataLayout(); |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 665 | |
| 666 | if (auto *C = dyn_cast<ConstantStruct>(I)) { |
| 667 | const StructLayout *SL = DL.getStructLayout(C->getType()); |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 668 | if (Offset >= SL->getSizeInBytes()) |
| 669 | return nullptr; |
| 670 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 671 | unsigned Op = SL->getElementContainingOffset(Offset); |
| 672 | return getPointerAtOffset(cast<Constant>(I->getOperand(Op)), |
| 673 | Offset - SL->getElementOffset(Op)); |
| 674 | } |
| 675 | if (auto *C = dyn_cast<ConstantArray>(I)) { |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 676 | ArrayType *VTableTy = C->getType(); |
| 677 | uint64_t ElemSize = DL.getTypeAllocSize(VTableTy->getElementType()); |
| 678 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 679 | unsigned Op = Offset / ElemSize; |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 680 | if (Op >= C->getNumOperands()) |
| 681 | return nullptr; |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 682 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 683 | return getPointerAtOffset(cast<Constant>(I->getOperand(Op)), |
| 684 | Offset % ElemSize); |
| 685 | } |
| 686 | return nullptr; |
Peter Collingbourne | 7a1e5bb | 2016-12-09 00:33:27 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 689 | bool DevirtModule::tryFindVirtualCallTargets( |
| 690 | std::vector<VirtualCallTarget> &TargetsForSlot, |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 691 | const std::set<TypeMemberInfo> &TypeMemberInfos, uint64_t ByteOffset) { |
| 692 | for (const TypeMemberInfo &TM : TypeMemberInfos) { |
| 693 | if (!TM.Bits->GV->isConstant()) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 694 | return false; |
| 695 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 696 | Constant *Ptr = getPointerAtOffset(TM.Bits->GV->getInitializer(), |
| 697 | TM.Offset + ByteOffset); |
| 698 | if (!Ptr) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 699 | return false; |
| 700 | |
Peter Collingbourne | 8786754 | 2016-12-09 01:10:11 +0000 | [diff] [blame] | 701 | auto Fn = dyn_cast<Function>(Ptr->stripPointerCasts()); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 702 | if (!Fn) |
| 703 | return false; |
| 704 | |
| 705 | // We can disregard __cxa_pure_virtual as a possible call target, as |
| 706 | // calls to pure virtuals are UB. |
| 707 | if (Fn->getName() == "__cxa_pure_virtual") |
| 708 | continue; |
| 709 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 710 | TargetsForSlot.push_back({Fn, &TM}); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // Give up if we couldn't find any targets. |
| 714 | return !TargetsForSlot.empty(); |
| 715 | } |
| 716 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 717 | void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo, |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 718 | Constant *TheFn, bool &IsExported) { |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 719 | auto Apply = [&](CallSiteInfo &CSInfo) { |
| 720 | for (auto &&VCallSite : CSInfo.CallSites) { |
| 721 | if (RemarksEnabled) |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 722 | VCallSite.emitRemark("single-impl", TheFn->getName(), OREGetter); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 723 | VCallSite.CS.setCalledFunction(ConstantExpr::getBitCast( |
| 724 | TheFn, VCallSite.CS.getCalledValue()->getType())); |
| 725 | // This use is no longer unsafe. |
| 726 | if (VCallSite.NumUnsafeUses) |
| 727 | --*VCallSite.NumUnsafeUses; |
| 728 | } |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 729 | if (CSInfo.isExported()) { |
| 730 | IsExported = true; |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 731 | CSInfo.markDevirt(); |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 732 | } |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 733 | }; |
| 734 | Apply(SlotInfo.CSInfo); |
| 735 | for (auto &P : SlotInfo.ConstCSInfo) |
| 736 | Apply(P.second); |
| 737 | } |
| 738 | |
Peter Collingbourne | e236741 | 2017-02-15 02:13:08 +0000 | [diff] [blame] | 739 | bool DevirtModule::trySingleImplDevirt( |
| 740 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 741 | VTableSlotInfo &SlotInfo, WholeProgramDevirtResolution *Res) { |
Peter Collingbourne | e236741 | 2017-02-15 02:13:08 +0000 | [diff] [blame] | 742 | // See if the program contains a single implementation of this virtual |
| 743 | // function. |
| 744 | Function *TheFn = TargetsForSlot[0].Fn; |
| 745 | for (auto &&Target : TargetsForSlot) |
| 746 | if (TheFn != Target.Fn) |
| 747 | return false; |
| 748 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 749 | // If so, update each call site to call that implementation directly. |
Peter Collingbourne | e236741 | 2017-02-15 02:13:08 +0000 | [diff] [blame] | 750 | if (RemarksEnabled) |
| 751 | TargetsForSlot[0].WasDevirt = true; |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 752 | |
| 753 | bool IsExported = false; |
| 754 | applySingleImplDevirt(SlotInfo, TheFn, IsExported); |
| 755 | if (!IsExported) |
| 756 | return false; |
| 757 | |
| 758 | // If the only implementation has local linkage, we must promote to external |
| 759 | // to make it visible to thin LTO objects. We can only get here during the |
| 760 | // ThinLTO export phase. |
| 761 | if (TheFn->hasLocalLinkage()) { |
Peter Collingbourne | 88a58cf | 2017-09-08 00:10:53 +0000 | [diff] [blame] | 762 | std::string NewName = (TheFn->getName() + "$merged").str(); |
| 763 | |
| 764 | // Since we are renaming the function, any comdats with the same name must |
| 765 | // also be renamed. This is required when targeting COFF, as the comdat name |
| 766 | // must match one of the names of the symbols in the comdat. |
| 767 | if (Comdat *C = TheFn->getComdat()) { |
| 768 | if (C->getName() == TheFn->getName()) { |
| 769 | Comdat *NewC = M.getOrInsertComdat(NewName); |
| 770 | NewC->setSelectionKind(C->getSelectionKind()); |
| 771 | for (GlobalObject &GO : M.global_objects()) |
| 772 | if (GO.getComdat() == C) |
| 773 | GO.setComdat(NewC); |
| 774 | } |
| 775 | } |
| 776 | |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 777 | TheFn->setLinkage(GlobalValue::ExternalLinkage); |
| 778 | TheFn->setVisibility(GlobalValue::HiddenVisibility); |
Peter Collingbourne | 88a58cf | 2017-09-08 00:10:53 +0000 | [diff] [blame] | 779 | TheFn->setName(NewName); |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | Res->TheKind = WholeProgramDevirtResolution::SingleImpl; |
| 783 | Res->SingleImplName = TheFn->getName(); |
| 784 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 785 | return true; |
| 786 | } |
| 787 | |
| 788 | bool DevirtModule::tryEvaluateFunctionsWithArgs( |
| 789 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 790 | ArrayRef<uint64_t> Args) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 791 | // Evaluate each function and store the result in each target's RetVal |
| 792 | // field. |
| 793 | for (VirtualCallTarget &Target : TargetsForSlot) { |
| 794 | if (Target.Fn->arg_size() != Args.size() + 1) |
| 795 | return false; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 796 | |
| 797 | Evaluator Eval(M.getDataLayout(), nullptr); |
| 798 | SmallVector<Constant *, 2> EvalArgs; |
| 799 | EvalArgs.push_back( |
| 800 | Constant::getNullValue(Target.Fn->getFunctionType()->getParamType(0))); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 801 | for (unsigned I = 0; I != Args.size(); ++I) { |
| 802 | auto *ArgTy = dyn_cast<IntegerType>( |
| 803 | Target.Fn->getFunctionType()->getParamType(I + 1)); |
| 804 | if (!ArgTy) |
| 805 | return false; |
| 806 | EvalArgs.push_back(ConstantInt::get(ArgTy, Args[I])); |
| 807 | } |
| 808 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 809 | Constant *RetVal; |
| 810 | if (!Eval.EvaluateFunction(Target.Fn, RetVal, EvalArgs) || |
| 811 | !isa<ConstantInt>(RetVal)) |
| 812 | return false; |
| 813 | Target.RetVal = cast<ConstantInt>(RetVal)->getZExtValue(); |
| 814 | } |
| 815 | return true; |
| 816 | } |
| 817 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 818 | void DevirtModule::applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, |
| 819 | uint64_t TheRetVal) { |
| 820 | for (auto Call : CSInfo.CallSites) |
| 821 | Call.replaceAndErase( |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 822 | "uniform-ret-val", FnName, RemarksEnabled, OREGetter, |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 823 | ConstantInt::get(cast<IntegerType>(Call.CS.getType()), TheRetVal)); |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 824 | CSInfo.markDevirt(); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 827 | bool DevirtModule::tryUniformRetValOpt( |
Peter Collingbourne | 77a8d56 | 2017-03-04 01:34:53 +0000 | [diff] [blame] | 828 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, CallSiteInfo &CSInfo, |
| 829 | WholeProgramDevirtResolution::ByArg *Res) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 830 | // Uniform return value optimization. If all functions return the same |
| 831 | // constant, replace all calls with that constant. |
| 832 | uint64_t TheRetVal = TargetsForSlot[0].RetVal; |
| 833 | for (const VirtualCallTarget &Target : TargetsForSlot) |
| 834 | if (Target.RetVal != TheRetVal) |
| 835 | return false; |
| 836 | |
Peter Collingbourne | 77a8d56 | 2017-03-04 01:34:53 +0000 | [diff] [blame] | 837 | if (CSInfo.isExported()) { |
| 838 | Res->TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal; |
| 839 | Res->Info = TheRetVal; |
| 840 | } |
| 841 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 842 | applyUniformRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), TheRetVal); |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 843 | if (RemarksEnabled) |
| 844 | for (auto &&Target : TargetsForSlot) |
| 845 | Target.WasDevirt = true; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 846 | return true; |
| 847 | } |
| 848 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 849 | std::string DevirtModule::getGlobalName(VTableSlot Slot, |
| 850 | ArrayRef<uint64_t> Args, |
| 851 | StringRef Name) { |
| 852 | std::string FullName = "__typeid_"; |
| 853 | raw_string_ostream OS(FullName); |
| 854 | OS << cast<MDString>(Slot.TypeID)->getString() << '_' << Slot.ByteOffset; |
| 855 | for (uint64_t Arg : Args) |
| 856 | OS << '_' << Arg; |
| 857 | OS << '_' << Name; |
| 858 | return OS.str(); |
| 859 | } |
| 860 | |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 861 | bool DevirtModule::shouldExportConstantsAsAbsoluteSymbols() { |
| 862 | Triple T(M.getTargetTriple()); |
| 863 | return (T.getArch() == Triple::x86 || T.getArch() == Triple::x86_64) && |
| 864 | T.getObjectFormat() == Triple::ELF; |
| 865 | } |
| 866 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 867 | void DevirtModule::exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, |
| 868 | StringRef Name, Constant *C) { |
| 869 | GlobalAlias *GA = GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage, |
| 870 | getGlobalName(Slot, Args, Name), C, &M); |
| 871 | GA->setVisibility(GlobalValue::HiddenVisibility); |
| 872 | } |
| 873 | |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 874 | void DevirtModule::exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, |
| 875 | StringRef Name, uint32_t Const, |
| 876 | uint32_t &Storage) { |
| 877 | if (shouldExportConstantsAsAbsoluteSymbols()) { |
| 878 | exportGlobal( |
| 879 | Slot, Args, Name, |
| 880 | ConstantExpr::getIntToPtr(ConstantInt::get(Int32Ty, Const), Int8PtrTy)); |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | Storage = Const; |
| 885 | } |
| 886 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 887 | Constant *DevirtModule::importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 888 | StringRef Name) { |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 889 | Constant *C = M.getOrInsertGlobal(getGlobalName(Slot, Args, Name), Int8Ty); |
| 890 | auto *GV = dyn_cast<GlobalVariable>(C); |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 891 | if (GV) |
| 892 | GV->setVisibility(GlobalValue::HiddenVisibility); |
| 893 | return C; |
| 894 | } |
| 895 | |
| 896 | Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, |
| 897 | StringRef Name, IntegerType *IntTy, |
| 898 | uint32_t Storage) { |
| 899 | if (!shouldExportConstantsAsAbsoluteSymbols()) |
| 900 | return ConstantInt::get(IntTy, Storage); |
| 901 | |
| 902 | Constant *C = importGlobal(Slot, Args, Name); |
| 903 | auto *GV = cast<GlobalVariable>(C->stripPointerCasts()); |
| 904 | C = ConstantExpr::getPtrToInt(C, IntTy); |
| 905 | |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 906 | // We only need to set metadata if the global is newly created, in which |
| 907 | // case it would not have hidden visibility. |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 908 | if (GV->getMetadata(LLVMContext::MD_absolute_symbol)) |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 909 | return C; |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 910 | |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 911 | auto SetAbsRange = [&](uint64_t Min, uint64_t Max) { |
| 912 | auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min)); |
| 913 | auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max)); |
| 914 | GV->setMetadata(LLVMContext::MD_absolute_symbol, |
| 915 | MDNode::get(M.getContext(), {MinC, MaxC})); |
| 916 | }; |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 917 | unsigned AbsWidth = IntTy->getBitWidth(); |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 918 | if (AbsWidth == IntPtrTy->getBitWidth()) |
| 919 | SetAbsRange(~0ull, ~0ull); // Full set. |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 920 | else |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 921 | SetAbsRange(0, 1ull << AbsWidth); |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 922 | return C; |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 925 | void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, |
| 926 | bool IsOne, |
| 927 | Constant *UniqueMemberAddr) { |
| 928 | for (auto &&Call : CSInfo.CallSites) { |
| 929 | IRBuilder<> B(Call.CS.getInstruction()); |
Peter Collingbourne | 001052a | 2017-08-22 21:41:19 +0000 | [diff] [blame] | 930 | Value *Cmp = |
| 931 | B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, |
| 932 | B.CreateBitCast(Call.VTable, Int8PtrTy), UniqueMemberAddr); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 933 | Cmp = B.CreateZExt(Cmp, Call.CS->getType()); |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 934 | Call.replaceAndErase("unique-ret-val", FnName, RemarksEnabled, OREGetter, |
| 935 | Cmp); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 936 | } |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 937 | CSInfo.markDevirt(); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 940 | bool DevirtModule::tryUniqueRetValOpt( |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 941 | unsigned BitWidth, MutableArrayRef<VirtualCallTarget> TargetsForSlot, |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 942 | CallSiteInfo &CSInfo, WholeProgramDevirtResolution::ByArg *Res, |
| 943 | VTableSlot Slot, ArrayRef<uint64_t> Args) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 944 | // IsOne controls whether we look for a 0 or a 1. |
| 945 | auto tryUniqueRetValOptFor = [&](bool IsOne) { |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 946 | const TypeMemberInfo *UniqueMember = nullptr; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 947 | for (const VirtualCallTarget &Target : TargetsForSlot) { |
Peter Collingbourne | 3866cc5 | 2016-03-08 03:50:36 +0000 | [diff] [blame] | 948 | if (Target.RetVal == (IsOne ? 1 : 0)) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 949 | if (UniqueMember) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 950 | return false; |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 951 | UniqueMember = Target.TM; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 955 | // We should have found a unique member or bailed out by now. We already |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 956 | // checked for a uniform return value in tryUniformRetValOpt. |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 957 | assert(UniqueMember); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 958 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 959 | Constant *UniqueMemberAddr = |
| 960 | ConstantExpr::getBitCast(UniqueMember->Bits->GV, Int8PtrTy); |
| 961 | UniqueMemberAddr = ConstantExpr::getGetElementPtr( |
| 962 | Int8Ty, UniqueMemberAddr, |
| 963 | ConstantInt::get(Int64Ty, UniqueMember->Offset)); |
| 964 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 965 | if (CSInfo.isExported()) { |
| 966 | Res->TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal; |
| 967 | Res->Info = IsOne; |
| 968 | |
| 969 | exportGlobal(Slot, Args, "unique_member", UniqueMemberAddr); |
| 970 | } |
| 971 | |
| 972 | // Replace each call with the comparison. |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 973 | applyUniqueRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), IsOne, |
| 974 | UniqueMemberAddr); |
| 975 | |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 976 | // Update devirtualization statistics for targets. |
| 977 | if (RemarksEnabled) |
| 978 | for (auto &&Target : TargetsForSlot) |
| 979 | Target.WasDevirt = true; |
| 980 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 981 | return true; |
| 982 | }; |
| 983 | |
| 984 | if (BitWidth == 1) { |
| 985 | if (tryUniqueRetValOptFor(true)) |
| 986 | return true; |
| 987 | if (tryUniqueRetValOptFor(false)) |
| 988 | return true; |
| 989 | } |
| 990 | return false; |
| 991 | } |
| 992 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 993 | void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName, |
| 994 | Constant *Byte, Constant *Bit) { |
| 995 | for (auto Call : CSInfo.CallSites) { |
| 996 | auto *RetType = cast<IntegerType>(Call.CS.getType()); |
| 997 | IRBuilder<> B(Call.CS.getInstruction()); |
Peter Collingbourne | 001052a | 2017-08-22 21:41:19 +0000 | [diff] [blame] | 998 | Value *Addr = |
| 999 | B.CreateGEP(Int8Ty, B.CreateBitCast(Call.VTable, Int8PtrTy), Byte); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1000 | if (RetType->getBitWidth() == 1) { |
| 1001 | Value *Bits = B.CreateLoad(Addr); |
| 1002 | Value *BitsAndBit = B.CreateAnd(Bits, Bit); |
| 1003 | auto IsBitSet = B.CreateICmpNE(BitsAndBit, ConstantInt::get(Int8Ty, 0)); |
| 1004 | Call.replaceAndErase("virtual-const-prop-1-bit", FnName, RemarksEnabled, |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 1005 | OREGetter, IsBitSet); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1006 | } else { |
| 1007 | Value *ValAddr = B.CreateBitCast(Addr, RetType->getPointerTo()); |
| 1008 | Value *Val = B.CreateLoad(RetType, ValAddr); |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 1009 | Call.replaceAndErase("virtual-const-prop", FnName, RemarksEnabled, |
| 1010 | OREGetter, Val); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1013 | CSInfo.markDevirt(); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1016 | bool DevirtModule::tryVirtualConstProp( |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 1017 | MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo, |
| 1018 | WholeProgramDevirtResolution *Res, VTableSlot Slot) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1019 | // This only works if the function returns an integer. |
| 1020 | auto RetType = dyn_cast<IntegerType>(TargetsForSlot[0].Fn->getReturnType()); |
| 1021 | if (!RetType) |
| 1022 | return false; |
| 1023 | unsigned BitWidth = RetType->getBitWidth(); |
| 1024 | if (BitWidth > 64) |
| 1025 | return false; |
| 1026 | |
Peter Collingbourne | 17febdb | 2017-02-09 23:46:26 +0000 | [diff] [blame] | 1027 | // Make sure that each function is defined, does not access memory, takes at |
| 1028 | // least one argument, does not use its first argument (which we assume is |
| 1029 | // 'this'), and has the same return type. |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 1030 | // |
| 1031 | // Note that we test whether this copy of the function is readnone, rather |
| 1032 | // than testing function attributes, which must hold for any copy of the |
| 1033 | // function, even a less optimized version substituted at link time. This is |
| 1034 | // sound because the virtual constant propagation optimizations effectively |
| 1035 | // inline all implementations of the virtual function into each call site, |
| 1036 | // rather than using function attributes to perform local optimization. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1037 | for (VirtualCallTarget &Target : TargetsForSlot) { |
Peter Collingbourne | 37317f1 | 2017-02-17 18:17:04 +0000 | [diff] [blame] | 1038 | if (Target.Fn->isDeclaration() || |
| 1039 | computeFunctionBodyMemoryAccess(*Target.Fn, AARGetter(*Target.Fn)) != |
| 1040 | MAK_ReadNone || |
Peter Collingbourne | 17febdb | 2017-02-09 23:46:26 +0000 | [diff] [blame] | 1041 | Target.Fn->arg_empty() || !Target.Fn->arg_begin()->use_empty() || |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1042 | Target.Fn->getReturnType() != RetType) |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1046 | for (auto &&CSByConstantArg : SlotInfo.ConstCSInfo) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1047 | if (!tryEvaluateFunctionsWithArgs(TargetsForSlot, CSByConstantArg.first)) |
| 1048 | continue; |
| 1049 | |
Peter Collingbourne | 77a8d56 | 2017-03-04 01:34:53 +0000 | [diff] [blame] | 1050 | WholeProgramDevirtResolution::ByArg *ResByArg = nullptr; |
| 1051 | if (Res) |
| 1052 | ResByArg = &Res->ResByArg[CSByConstantArg.first]; |
| 1053 | |
| 1054 | if (tryUniformRetValOpt(TargetsForSlot, CSByConstantArg.second, ResByArg)) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1055 | continue; |
| 1056 | |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 1057 | if (tryUniqueRetValOpt(BitWidth, TargetsForSlot, CSByConstantArg.second, |
| 1058 | ResByArg, Slot, CSByConstantArg.first)) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1059 | continue; |
| 1060 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1061 | // Find an allocation offset in bits in all vtables associated with the |
| 1062 | // type. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1063 | uint64_t AllocBefore = |
| 1064 | findLowestOffset(TargetsForSlot, /*IsAfter=*/false, BitWidth); |
| 1065 | uint64_t AllocAfter = |
| 1066 | findLowestOffset(TargetsForSlot, /*IsAfter=*/true, BitWidth); |
| 1067 | |
| 1068 | // Calculate the total amount of padding needed to store a value at both |
| 1069 | // ends of the object. |
| 1070 | uint64_t TotalPaddingBefore = 0, TotalPaddingAfter = 0; |
| 1071 | for (auto &&Target : TargetsForSlot) { |
| 1072 | TotalPaddingBefore += std::max<int64_t>( |
| 1073 | (AllocBefore + 7) / 8 - Target.allocatedBeforeBytes() - 1, 0); |
| 1074 | TotalPaddingAfter += std::max<int64_t>( |
| 1075 | (AllocAfter + 7) / 8 - Target.allocatedAfterBytes() - 1, 0); |
| 1076 | } |
| 1077 | |
| 1078 | // If the amount of padding is too large, give up. |
| 1079 | // FIXME: do something smarter here. |
| 1080 | if (std::min(TotalPaddingBefore, TotalPaddingAfter) > 128) |
| 1081 | continue; |
| 1082 | |
| 1083 | // Calculate the offset to the value as a (possibly negative) byte offset |
| 1084 | // and (if applicable) a bit offset, and store the values in the targets. |
| 1085 | int64_t OffsetByte; |
| 1086 | uint64_t OffsetBit; |
| 1087 | if (TotalPaddingBefore <= TotalPaddingAfter) |
| 1088 | setBeforeReturnValues(TargetsForSlot, AllocBefore, BitWidth, OffsetByte, |
| 1089 | OffsetBit); |
| 1090 | else |
| 1091 | setAfterReturnValues(TargetsForSlot, AllocAfter, BitWidth, OffsetByte, |
| 1092 | OffsetBit); |
| 1093 | |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1094 | if (RemarksEnabled) |
| 1095 | for (auto &&Target : TargetsForSlot) |
| 1096 | Target.WasDevirt = true; |
| 1097 | |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (CSByConstantArg.second.isExported()) { |
| 1100 | ResByArg->TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp; |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 1101 | exportConstant(Slot, CSByConstantArg.first, "byte", OffsetByte, |
| 1102 | ResByArg->Byte); |
| 1103 | exportConstant(Slot, CSByConstantArg.first, "bit", 1ULL << OffsetBit, |
| 1104 | ResByArg->Bit); |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | // Rewrite each call to a load from OffsetByte/OffsetBit. |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 1108 | Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte); |
| 1109 | Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit); |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1110 | applyVirtualConstProp(CSByConstantArg.second, |
| 1111 | TargetsForSlot[0].Fn->getName(), ByteConst, BitConst); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1112 | } |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
| 1116 | void DevirtModule::rebuildGlobal(VTableBits &B) { |
| 1117 | if (B.Before.Bytes.empty() && B.After.Bytes.empty()) |
| 1118 | return; |
| 1119 | |
| 1120 | // Align each byte array to pointer width. |
| 1121 | unsigned PointerSize = M.getDataLayout().getPointerSize(); |
| 1122 | B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), PointerSize)); |
| 1123 | B.After.Bytes.resize(alignTo(B.After.Bytes.size(), PointerSize)); |
| 1124 | |
| 1125 | // Before was stored in reverse order; flip it now. |
| 1126 | for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I) |
| 1127 | std::swap(B.Before.Bytes[I], B.Before.Bytes[Size - 1 - I]); |
| 1128 | |
| 1129 | // Build an anonymous global containing the before bytes, followed by the |
| 1130 | // original initializer, followed by the after bytes. |
| 1131 | auto NewInit = ConstantStruct::getAnon( |
| 1132 | {ConstantDataArray::get(M.getContext(), B.Before.Bytes), |
| 1133 | B.GV->getInitializer(), |
| 1134 | ConstantDataArray::get(M.getContext(), B.After.Bytes)}); |
| 1135 | auto NewGV = |
| 1136 | new GlobalVariable(M, NewInit->getType(), B.GV->isConstant(), |
| 1137 | GlobalVariable::PrivateLinkage, NewInit, "", B.GV); |
| 1138 | NewGV->setSection(B.GV->getSection()); |
| 1139 | NewGV->setComdat(B.GV->getComdat()); |
| 1140 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1141 | // Copy the original vtable's metadata to the anonymous global, adjusting |
| 1142 | // offsets as required. |
| 1143 | NewGV->copyMetadata(B.GV, B.Before.Bytes.size()); |
| 1144 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1145 | // Build an alias named after the original global, pointing at the second |
| 1146 | // element (the original initializer). |
| 1147 | auto Alias = GlobalAlias::create( |
| 1148 | B.GV->getInitializer()->getType(), 0, B.GV->getLinkage(), "", |
| 1149 | ConstantExpr::getGetElementPtr( |
| 1150 | NewInit->getType(), NewGV, |
| 1151 | ArrayRef<Constant *>{ConstantInt::get(Int32Ty, 0), |
| 1152 | ConstantInt::get(Int32Ty, 1)}), |
| 1153 | &M); |
| 1154 | Alias->setVisibility(B.GV->getVisibility()); |
| 1155 | Alias->takeName(B.GV); |
| 1156 | |
| 1157 | B.GV->replaceAllUsesWith(Alias); |
| 1158 | B.GV->eraseFromParent(); |
| 1159 | } |
| 1160 | |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1161 | bool DevirtModule::areRemarksEnabled() { |
| 1162 | const auto &FL = M.getFunctionList(); |
| 1163 | if (FL.empty()) |
| 1164 | return false; |
| 1165 | const Function &Fn = FL.front(); |
Adam Nemet | de53bfb | 2017-02-23 23:11:11 +0000 | [diff] [blame] | 1166 | |
| 1167 | const auto &BBL = Fn.getBasicBlockList(); |
| 1168 | if (BBL.empty()) |
| 1169 | return false; |
| 1170 | auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBL.front()); |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1171 | return DI.isEnabled(); |
| 1172 | } |
| 1173 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1174 | void DevirtModule::scanTypeTestUsers(Function *TypeTestFunc, |
| 1175 | Function *AssumeFunc) { |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1176 | // Find all virtual calls via a virtual table pointer %p under an assumption |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1177 | // of the form llvm.assume(llvm.type.test(%p, %md)). This indicates that %p |
| 1178 | // points to a member of the type identifier %md. Group calls by (type ID, |
| 1179 | // offset) pair (effectively the identity of the virtual function) and store |
| 1180 | // to CallSlots. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1181 | DenseSet<Value *> SeenPtrs; |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1182 | for (auto I = TypeTestFunc->use_begin(), E = TypeTestFunc->use_end(); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1183 | I != E;) { |
| 1184 | auto CI = dyn_cast<CallInst>(I->getUser()); |
| 1185 | ++I; |
| 1186 | if (!CI) |
| 1187 | continue; |
| 1188 | |
Peter Collingbourne | ccdc225 | 2016-05-10 18:07:21 +0000 | [diff] [blame] | 1189 | // Search for virtual calls based on %p and add them to DevirtCalls. |
| 1190 | SmallVector<DevirtCallSite, 1> DevirtCalls; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1191 | SmallVector<CallInst *, 1> Assumes; |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1192 | findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1193 | |
Peter Collingbourne | ccdc225 | 2016-05-10 18:07:21 +0000 | [diff] [blame] | 1194 | // If we found any, add them to CallSlots. Only do this if we haven't seen |
| 1195 | // the vtable pointer before, as it may have been CSE'd with pointers from |
| 1196 | // other call sites, and we don't want to process call sites multiple times. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1197 | if (!Assumes.empty()) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1198 | Metadata *TypeId = |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1199 | cast<MetadataAsValue>(CI->getArgOperand(1))->getMetadata(); |
| 1200 | Value *Ptr = CI->getArgOperand(0)->stripPointerCasts(); |
Peter Collingbourne | ccdc225 | 2016-05-10 18:07:21 +0000 | [diff] [blame] | 1201 | if (SeenPtrs.insert(Ptr).second) { |
| 1202 | for (DevirtCallSite Call : DevirtCalls) { |
Peter Collingbourne | 001052a | 2017-08-22 21:41:19 +0000 | [diff] [blame] | 1203 | CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CS, nullptr); |
Peter Collingbourne | ccdc225 | 2016-05-10 18:07:21 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1208 | // We no longer need the assumes or the type test. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1209 | for (auto Assume : Assumes) |
| 1210 | Assume->eraseFromParent(); |
| 1211 | // We can't use RecursivelyDeleteTriviallyDeadInstructions here because we |
| 1212 | // may use the vtable argument later. |
| 1213 | if (CI->use_empty()) |
| 1214 | CI->eraseFromParent(); |
| 1215 | } |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) { |
| 1219 | Function *TypeTestFunc = Intrinsic::getDeclaration(&M, Intrinsic::type_test); |
| 1220 | |
| 1221 | for (auto I = TypeCheckedLoadFunc->use_begin(), |
| 1222 | E = TypeCheckedLoadFunc->use_end(); |
| 1223 | I != E;) { |
| 1224 | auto CI = dyn_cast<CallInst>(I->getUser()); |
| 1225 | ++I; |
| 1226 | if (!CI) |
| 1227 | continue; |
| 1228 | |
| 1229 | Value *Ptr = CI->getArgOperand(0); |
| 1230 | Value *Offset = CI->getArgOperand(1); |
| 1231 | Value *TypeIdValue = CI->getArgOperand(2); |
| 1232 | Metadata *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata(); |
| 1233 | |
| 1234 | SmallVector<DevirtCallSite, 1> DevirtCalls; |
| 1235 | SmallVector<Instruction *, 1> LoadedPtrs; |
| 1236 | SmallVector<Instruction *, 1> Preds; |
| 1237 | bool HasNonCallUses = false; |
| 1238 | findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds, |
| 1239 | HasNonCallUses, CI); |
| 1240 | |
| 1241 | // Start by generating "pessimistic" code that explicitly loads the function |
| 1242 | // pointer from the vtable and performs the type check. If possible, we will |
| 1243 | // eliminate the load and the type check later. |
| 1244 | |
| 1245 | // If possible, only generate the load at the point where it is used. |
| 1246 | // This helps avoid unnecessary spills. |
| 1247 | IRBuilder<> LoadB( |
| 1248 | (LoadedPtrs.size() == 1 && !HasNonCallUses) ? LoadedPtrs[0] : CI); |
| 1249 | Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset); |
| 1250 | Value *GEPPtr = LoadB.CreateBitCast(GEP, PointerType::getUnqual(Int8PtrTy)); |
| 1251 | Value *LoadedValue = LoadB.CreateLoad(Int8PtrTy, GEPPtr); |
| 1252 | |
| 1253 | for (Instruction *LoadedPtr : LoadedPtrs) { |
| 1254 | LoadedPtr->replaceAllUsesWith(LoadedValue); |
| 1255 | LoadedPtr->eraseFromParent(); |
| 1256 | } |
| 1257 | |
| 1258 | // Likewise for the type test. |
| 1259 | IRBuilder<> CallB((Preds.size() == 1 && !HasNonCallUses) ? Preds[0] : CI); |
| 1260 | CallInst *TypeTestCall = CallB.CreateCall(TypeTestFunc, {Ptr, TypeIdValue}); |
| 1261 | |
| 1262 | for (Instruction *Pred : Preds) { |
| 1263 | Pred->replaceAllUsesWith(TypeTestCall); |
| 1264 | Pred->eraseFromParent(); |
| 1265 | } |
| 1266 | |
| 1267 | // We have already erased any extractvalue instructions that refer to the |
| 1268 | // intrinsic call, but the intrinsic may have other non-extractvalue uses |
| 1269 | // (although this is unlikely). In that case, explicitly build a pair and |
| 1270 | // RAUW it. |
| 1271 | if (!CI->use_empty()) { |
| 1272 | Value *Pair = UndefValue::get(CI->getType()); |
| 1273 | IRBuilder<> B(CI); |
| 1274 | Pair = B.CreateInsertValue(Pair, LoadedValue, {0}); |
| 1275 | Pair = B.CreateInsertValue(Pair, TypeTestCall, {1}); |
| 1276 | CI->replaceAllUsesWith(Pair); |
| 1277 | } |
| 1278 | |
| 1279 | // The number of unsafe uses is initially the number of uses. |
| 1280 | auto &NumUnsafeUses = NumUnsafeUsesForTypeTest[TypeTestCall]; |
| 1281 | NumUnsafeUses = DevirtCalls.size(); |
| 1282 | |
| 1283 | // If the function pointer has a non-call user, we cannot eliminate the type |
| 1284 | // check, as one of those users may eventually call the pointer. Increment |
| 1285 | // the unsafe use count to make sure it cannot reach zero. |
| 1286 | if (HasNonCallUses) |
| 1287 | ++NumUnsafeUses; |
| 1288 | for (DevirtCallSite Call : DevirtCalls) { |
Peter Collingbourne | 50cbd7c | 2017-02-15 21:56:51 +0000 | [diff] [blame] | 1289 | CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CS, |
| 1290 | &NumUnsafeUses); |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | CI->eraseFromParent(); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1297 | void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) { |
Peter Collingbourne | 9a3f979 | 2017-03-22 18:04:39 +0000 | [diff] [blame] | 1298 | const TypeIdSummary *TidSummary = |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1299 | ImportSummary->getTypeIdSummary(cast<MDString>(Slot.TypeID)->getString()); |
Peter Collingbourne | 9a3f979 | 2017-03-22 18:04:39 +0000 | [diff] [blame] | 1300 | if (!TidSummary) |
| 1301 | return; |
| 1302 | auto ResI = TidSummary->WPDRes.find(Slot.ByteOffset); |
| 1303 | if (ResI == TidSummary->WPDRes.end()) |
| 1304 | return; |
| 1305 | const WholeProgramDevirtResolution &Res = ResI->second; |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) { |
| 1308 | // The type of the function in the declaration is irrelevant because every |
| 1309 | // call site will cast it to the correct type. |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 1310 | auto *SingleImpl = M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1311 | Res.SingleImplName, Type::getVoidTy(M.getContext())); |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1312 | |
| 1313 | // This is the import phase so we should not be exporting anything. |
| 1314 | bool IsExported = false; |
| 1315 | applySingleImplDevirt(SlotInfo, SingleImpl, IsExported); |
| 1316 | assert(!IsExported); |
| 1317 | } |
Peter Collingbourne | 0152c81 | 2017-03-09 01:11:15 +0000 | [diff] [blame] | 1318 | |
| 1319 | for (auto &CSByConstantArg : SlotInfo.ConstCSInfo) { |
| 1320 | auto I = Res.ResByArg.find(CSByConstantArg.first); |
| 1321 | if (I == Res.ResByArg.end()) |
| 1322 | continue; |
| 1323 | auto &ResByArg = I->second; |
| 1324 | // FIXME: We should figure out what to do about the "function name" argument |
| 1325 | // to the apply* functions, as the function names are unavailable during the |
| 1326 | // importing phase. For now we just pass the empty string. This does not |
| 1327 | // impact correctness because the function names are just used for remarks. |
| 1328 | switch (ResByArg.TheKind) { |
| 1329 | case WholeProgramDevirtResolution::ByArg::UniformRetVal: |
| 1330 | applyUniformRetValOpt(CSByConstantArg.second, "", ResByArg.Info); |
| 1331 | break; |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 1332 | case WholeProgramDevirtResolution::ByArg::UniqueRetVal: { |
| 1333 | Constant *UniqueMemberAddr = |
| 1334 | importGlobal(Slot, CSByConstantArg.first, "unique_member"); |
| 1335 | applyUniqueRetValOpt(CSByConstantArg.second, "", ResByArg.Info, |
| 1336 | UniqueMemberAddr); |
| 1337 | break; |
| 1338 | } |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1339 | case WholeProgramDevirtResolution::ByArg::VirtualConstProp: { |
Peter Collingbourne | b15a35e | 2017-09-11 22:34:42 +0000 | [diff] [blame] | 1340 | Constant *Byte = importConstant(Slot, CSByConstantArg.first, "byte", |
| 1341 | Int32Ty, ResByArg.Byte); |
| 1342 | Constant *Bit = importConstant(Slot, CSByConstantArg.first, "bit", Int8Ty, |
| 1343 | ResByArg.Bit); |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1344 | applyVirtualConstProp(CSByConstantArg.second, "", Byte, Bit); |
Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 1345 | break; |
Peter Collingbourne | 14dcf02 | 2017-03-10 20:13:58 +0000 | [diff] [blame] | 1346 | } |
Peter Collingbourne | 0152c81 | 2017-03-09 01:11:15 +0000 | [diff] [blame] | 1347 | default: |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | void DevirtModule::removeRedundantTypeTests() { |
| 1354 | auto True = ConstantInt::getTrue(M.getContext()); |
| 1355 | for (auto &&U : NumUnsafeUsesForTypeTest) { |
| 1356 | if (U.second == 0) { |
| 1357 | U.first->replaceAllUsesWith(True); |
| 1358 | U.first->eraseFromParent(); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1363 | bool DevirtModule::run() { |
| 1364 | Function *TypeTestFunc = |
| 1365 | M.getFunction(Intrinsic::getName(Intrinsic::type_test)); |
| 1366 | Function *TypeCheckedLoadFunc = |
| 1367 | M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load)); |
| 1368 | Function *AssumeFunc = M.getFunction(Intrinsic::getName(Intrinsic::assume)); |
| 1369 | |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1370 | // Normally if there are no users of the devirtualization intrinsics in the |
| 1371 | // module, this pass has nothing to do. But if we are exporting, we also need |
| 1372 | // to handle any users that appear only in the function summaries. |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1373 | if (!ExportSummary && |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1374 | (!TypeTestFunc || TypeTestFunc->use_empty() || !AssumeFunc || |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1375 | AssumeFunc->use_empty()) && |
| 1376 | (!TypeCheckedLoadFunc || TypeCheckedLoadFunc->use_empty())) |
| 1377 | return false; |
| 1378 | |
| 1379 | if (TypeTestFunc && AssumeFunc) |
| 1380 | scanTypeTestUsers(TypeTestFunc, AssumeFunc); |
| 1381 | |
| 1382 | if (TypeCheckedLoadFunc) |
| 1383 | scanTypeCheckedLoadUsers(TypeCheckedLoadFunc); |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1384 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1385 | if (ImportSummary) { |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1386 | for (auto &S : CallSlots) |
| 1387 | importResolution(S.first, S.second); |
| 1388 | |
| 1389 | removeRedundantTypeTests(); |
| 1390 | |
| 1391 | // The rest of the code is only necessary when exporting or during regular |
| 1392 | // LTO, so we are done. |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1396 | // Rebuild type metadata into a map for easy lookup. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1397 | std::vector<VTableBits> Bits; |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1398 | DenseMap<Metadata *, std::set<TypeMemberInfo>> TypeIdMap; |
| 1399 | buildTypeIdentifierMap(Bits, TypeIdMap); |
| 1400 | if (TypeIdMap.empty()) |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1401 | return true; |
| 1402 | |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1403 | // Collect information from summary about which calls to try to devirtualize. |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1404 | if (ExportSummary) { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1405 | DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID; |
| 1406 | for (auto &P : TypeIdMap) { |
| 1407 | if (auto *TypeId = dyn_cast<MDString>(P.first)) |
| 1408 | MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back( |
| 1409 | TypeId); |
| 1410 | } |
| 1411 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1412 | for (auto &P : *ExportSummary) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 1413 | for (auto &S : P.second.SummaryList) { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1414 | auto *FS = dyn_cast<FunctionSummary>(S.get()); |
| 1415 | if (!FS) |
| 1416 | continue; |
| 1417 | // FIXME: Only add live functions. |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1418 | for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) { |
| 1419 | for (Metadata *MD : MetadataByGUID[VF.GUID]) { |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 1420 | CallSlots[{MD, VF.Offset}].CSInfo.SummaryHasTypeTestAssumeUsers = |
| 1421 | true; |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) { |
| 1425 | for (Metadata *MD : MetadataByGUID[VF.GUID]) { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1426 | CallSlots[{MD, VF.Offset}] |
| 1427 | .CSInfo.SummaryTypeCheckedLoadUsers.push_back(FS); |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1430 | for (const FunctionSummary::ConstVCall &VC : |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1431 | FS->type_test_assume_const_vcalls()) { |
| 1432 | for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) { |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 1433 | CallSlots[{MD, VC.VFunc.Offset}] |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1434 | .ConstCSInfo[VC.Args] |
| 1435 | .SummaryHasTypeTestAssumeUsers = true; |
| 1436 | } |
| 1437 | } |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 1438 | for (const FunctionSummary::ConstVCall &VC : |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1439 | FS->type_checked_load_const_vcalls()) { |
| 1440 | for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1441 | CallSlots[{MD, VC.VFunc.Offset}] |
| 1442 | .ConstCSInfo[VC.Args] |
| 1443 | .SummaryTypeCheckedLoadUsers.push_back(FS); |
George Rimar | 5d8aea1 | 2017-03-10 10:31:56 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1450 | // For each (type, offset) pair: |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1451 | bool DidVirtualConstProp = false; |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1452 | std::map<std::string, Function*> DevirtTargets; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1453 | for (auto &S : CallSlots) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1454 | // Search each of the members of the type identifier for the virtual |
| 1455 | // function implementation at offset S.first.ByteOffset, and add to |
| 1456 | // TargetsForSlot. |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1457 | std::vector<VirtualCallTarget> TargetsForSlot; |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1458 | if (tryFindVirtualCallTargets(TargetsForSlot, TypeIdMap[S.first.TypeID], |
| 1459 | S.first.ByteOffset)) { |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 1460 | WholeProgramDevirtResolution *Res = nullptr; |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1461 | if (ExportSummary && isa<MDString>(S.first.TypeID)) |
| 1462 | Res = &ExportSummary |
Peter Collingbourne | 9a3f979 | 2017-03-22 18:04:39 +0000 | [diff] [blame] | 1463 | ->getOrInsertTypeIdSummary( |
| 1464 | cast<MDString>(S.first.TypeID)->getString()) |
| 1465 | .WPDRes[S.first.ByteOffset]; |
Peter Collingbourne | 2325bb3 | 2017-03-04 01:31:01 +0000 | [diff] [blame] | 1466 | |
| 1467 | if (!trySingleImplDevirt(TargetsForSlot, S.second, Res) && |
Peter Collingbourne | 59675ba | 2017-03-10 20:09:11 +0000 | [diff] [blame] | 1468 | tryVirtualConstProp(TargetsForSlot, S.second, Res, S.first)) |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1469 | DidVirtualConstProp = true; |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1470 | |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1471 | // Collect functions devirtualized at least for one call site for stats. |
| 1472 | if (RemarksEnabled) |
| 1473 | for (const auto &T : TargetsForSlot) |
| 1474 | if (T.WasDevirt) |
| 1475 | DevirtTargets[T.Fn->getName()] = T.Fn; |
| 1476 | } |
| 1477 | |
| 1478 | // CFI-specific: if we are exporting and any llvm.type.checked.load |
| 1479 | // intrinsics were *not* devirtualized, we need to add the resulting |
| 1480 | // llvm.type.test intrinsics to the function summaries so that the |
| 1481 | // LowerTypeTests pass will export them. |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 1482 | if (ExportSummary && isa<MDString>(S.first.TypeID)) { |
Peter Collingbourne | b406baa | 2017-03-04 01:23:30 +0000 | [diff] [blame] | 1483 | auto GUID = |
| 1484 | GlobalValue::getGUID(cast<MDString>(S.first.TypeID)->getString()); |
| 1485 | for (auto FS : S.second.CSInfo.SummaryTypeCheckedLoadUsers) |
| 1486 | FS->addTypeTest(GUID); |
| 1487 | for (auto &CCS : S.second.ConstCSInfo) |
| 1488 | for (auto FS : CCS.second.SummaryTypeCheckedLoadUsers) |
| 1489 | FS->addTypeTest(GUID); |
| 1490 | } |
Ivan Krasin | f3403fd | 2016-08-11 19:09:02 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | if (RemarksEnabled) { |
| 1494 | // Generate remarks for each devirtualized function. |
| 1495 | for (const auto &DT : DevirtTargets) { |
| 1496 | Function *F = DT.second; |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 1497 | |
Sam Elliott | e963c89 | 2017-08-21 16:57:21 +0000 | [diff] [blame] | 1498 | using namespace ore; |
Peter Collingbourne | 9110cb4 | 2018-01-05 00:27:51 +0000 | [diff] [blame^] | 1499 | OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, "Devirtualized", F) |
| 1500 | << "devirtualized " |
| 1501 | << NV("FunctionName", F->getName())); |
Ivan Krasin | b05e06e | 2016-08-05 19:45:16 +0000 | [diff] [blame] | 1502 | } |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Peter Collingbourne | 6d284fa | 2017-03-09 00:21:25 +0000 | [diff] [blame] | 1505 | removeRedundantTypeTests(); |
Peter Collingbourne | 0312f61 | 2016-06-25 00:23:04 +0000 | [diff] [blame] | 1506 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 1507 | // Rebuild each global we touched as part of virtual constant propagation to |
| 1508 | // include the before and after bytes. |
| 1509 | if (DidVirtualConstProp) |
| 1510 | for (VTableBits &B : Bits) |
| 1511 | rebuildGlobal(B); |
| 1512 | |
| 1513 | return true; |
| 1514 | } |