<div dir="ltr">Does clang need to be passed a special flag for it to emit the bitset information this uses?<div><br></div><div>-- Sean Silva</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 9, 2016 at 2:50 PM, Peter Collingbourne via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: pcc<br>
Date: Tue Feb 9 16:50:34 2016<br>
New Revision: 260312<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=260312&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=260312&view=rev</a><br>
Log:<br>
WholeProgramDevirt: introduce.<br>
<br>
This pass implements whole program optimization of virtual calls in cases<br>
where we know (via bitset information) that the list of callees is fixed. This<br>
includes the following:<br>
<br>
- Single implementation devirtualization: if a virtual call has a single<br>
possible callee, replace all calls with a direct call to that callee.<br>
<br>
- Virtual constant propagation: if the virtual function's return type is an<br>
integer <=64 bits and all possible callees are readnone, for each class and<br>
each list of constant arguments: evaluate the function, store the return<br>
value alongside the virtual table, and rewrite each virtual call as a load<br>
from the virtual table.<br>
<br>
- Uniform return value optimization: if the conditions for virtual constant<br>
propagation hold and each function returns the same constant value, replace<br>
each virtual call with that constant.<br>
<br>
- Unique return value optimization for i1 return values: if the conditions<br>
for virtual constant propagation hold and a single vtable's function<br>
returns 0, or a single vtable's function returns 1, replace each virtual<br>
call with a comparison of the vptr against that vtable's address.<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D16795" rel="noreferrer" target="_blank">http://reviews.llvm.org/D16795</a><br>
<br>
Added:<br>
llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h<br>
llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll<br>
llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll<br>
llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp<br>
Modified:<br>
llvm/trunk/include/llvm/InitializePasses.h<br>
llvm/trunk/include/llvm/Transforms/IPO.h<br>
llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h<br>
llvm/trunk/lib/Transforms/IPO/CMakeLists.txt<br>
llvm/trunk/lib/Transforms/IPO/IPO.cpp<br>
llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
llvm/trunk/test/tools/gold/X86/disable-verify.ll<br>
llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Tue Feb 9 16:50:34 2016<br>
@@ -316,6 +316,7 @@ void initializeFuncletLayoutPass(PassReg<br>
void initializeLoopLoadEliminationPass(PassRegistry&);<br>
void initializeFunctionImportPassPass(PassRegistry &);<br>
void initializeLoopVersioningPassPass(PassRegistry &);<br>
+void initializeWholeProgramDevirtPass(PassRegistry &);<br>
}<br>
<br>
#endif<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/IPO.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/IPO.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/IPO.h Tue Feb 9 16:50:34 2016<br>
@@ -226,6 +226,10 @@ ModulePass *createLowerBitSetsPass();<br>
/// \brief This pass export CFI checks for use by external modules.<br>
ModulePass *createCrossDSOCFIPass();<br>
<br>
+/// \brief This pass implements whole-program devirtualization using bitset<br>
+/// metadata.<br>
+ModulePass *createWholeProgramDevirtPass();<br>
+<br>
//===----------------------------------------------------------------------===//<br>
// SampleProfilePass - Loads sample profile data from disk and generates<br>
// IR metadata to reflect the profile.<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h Tue Feb 9 16:50:34 2016<br>
@@ -157,6 +157,7 @@ private:<br>
legacy::PassManagerBase &PM) const;<br>
void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;<br>
void addLTOOptimizationPasses(legacy::PassManagerBase &PM);<br>
+ void addEarlyLTOOptimizationPasses(legacy::PassManagerBase &PM);<br>
void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);<br>
void addPGOInstrPasses(legacy::PassManagerBase &MPM);<br>
<br>
<br>
Added: llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h (added)<br>
+++ llvm/trunk/include/llvm/Transforms/IPO/WholeProgramDevirt.h Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,215 @@<br>
+//===- WholeProgramDevirt.h - Whole-program devirt pass ---------*- C++ -*-===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file defines parts of the whole-program devirtualization pass<br>
+// implementation that may be usefully unit tested.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H<br>
+#define LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H<br>
+<br>
+#include "llvm/ADT/ArrayRef.h"<br>
+#include "llvm/ADT/DenseMapInfo.h"<br>
+#include <utility><br>
+#include <vector><br>
+#include <assert.h><br>
+#include <stdint.h><br>
+<br>
+namespace llvm {<br>
+<br>
+class Function;<br>
+class GlobalVariable;<br>
+<br>
+namespace wholeprogramdevirt {<br>
+<br>
+// A bit vector that keeps track of which bits are used. We use this to<br>
+// pack constant values compactly before and after each virtual table.<br>
+struct AccumBitVector {<br>
+ std::vector<uint8_t> Bytes;<br>
+<br>
+ // Bits in BytesUsed[I] are 1 if matching bit in Bytes[I] is used, 0 if not.<br>
+ std::vector<uint8_t> BytesUsed;<br>
+<br>
+ std::pair<uint8_t *, uint8_t *> getPtrToData(uint64_t Pos, uint8_t Size) {<br>
+ if (Bytes.size() < Pos + Size) {<br>
+ Bytes.resize(Pos + Size);<br>
+ BytesUsed.resize(Pos + Size);<br>
+ }<br>
+ return std::make_pair(Bytes.data() + Pos, BytesUsed.data() + Pos);<br>
+ }<br>
+<br>
+ // Set little-endian value Val with size Size at bit position Pos,<br>
+ // and mark bytes as used.<br>
+ void setLE(uint64_t Pos, uint64_t Val, uint8_t Size) {<br>
+ assert(Pos % 8 == 0);<br>
+ auto DataUsed = getPtrToData(Pos / 8, Size);<br>
+ for (unsigned I = 0; I != Size; ++I) {<br>
+ DataUsed.first[I] = Val >> (I * 8);<br>
+ assert(!DataUsed.second[I]);<br>
+ DataUsed.second[I] = 0xff;<br>
+ }<br>
+ }<br>
+<br>
+ // Set big-endian value Val with size Size at bit position Pos,<br>
+ // and mark bytes as used.<br>
+ void setBE(uint64_t Pos, uint64_t Val, uint8_t Size) {<br>
+ assert(Pos % 8 == 0);<br>
+ auto DataUsed = getPtrToData(Pos / 8, Size);<br>
+ for (unsigned I = 0; I != Size; ++I) {<br>
+ DataUsed.first[Size - I - 1] = Val >> (I * 8);<br>
+ assert(!DataUsed.second[Size - I - 1]);<br>
+ DataUsed.second[Size - I - 1] = 0xff;<br>
+ }<br>
+ }<br>
+<br>
+ // Set bit at bit position Pos to b and mark bit as used.<br>
+ void setBit(uint64_t Pos, bool b) {<br>
+ auto DataUsed = getPtrToData(Pos / 8, 1);<br>
+ if (b)<br>
+ *DataUsed.first |= 1 << (Pos % 8);<br>
+ assert(!(*DataUsed.second & (1 << Pos % 8)));<br>
+ *DataUsed.second |= 1 << (Pos % 8);<br>
+ }<br>
+};<br>
+<br>
+// The bits that will be stored before and after a particular vtable.<br>
+struct VTableBits {<br>
+ // The vtable global.<br>
+ GlobalVariable *GV;<br>
+<br>
+ // Cache of the vtable's size in bytes.<br>
+ uint64_t ObjectSize = 0;<br>
+<br>
+ // The bit vector that will be laid out before the vtable. Note that these<br>
+ // bytes are stored in reverse order until the globals are rebuilt. This means<br>
+ // that any values in the array must be stored using the opposite endianness<br>
+ // from the target.<br>
+ AccumBitVector Before;<br>
+<br>
+ // The bit vector that will be laid out after the vtable.<br>
+ AccumBitVector After;<br>
+};<br>
+<br>
+// Information about an entry in a particular bitset.<br>
+struct BitSetInfo {<br>
+ // The VTableBits for the vtable.<br>
+ VTableBits *Bits;<br>
+<br>
+ // The offset in bytes from the start of the vtable (i.e. the address point).<br>
+ uint64_t Offset;<br>
+<br>
+ bool operator<(const BitSetInfo &other) const {<br>
+ return Bits < other.Bits || (Bits == other.Bits && Offset < other.Offset);<br>
+ }<br>
+};<br>
+<br>
+// A virtual call target, i.e. an entry in a particular vtable.<br>
+struct VirtualCallTarget {<br>
+ VirtualCallTarget(Function *Fn, const BitSetInfo *BS);<br>
+<br>
+ // For testing only.<br>
+ VirtualCallTarget(const BitSetInfo *BS, bool IsBigEndian)<br>
+ : Fn(nullptr), BS(BS), IsBigEndian(IsBigEndian) {}<br>
+<br>
+ // The function stored in the vtable.<br>
+ Function *Fn;<br>
+<br>
+ // A pointer to the bitset through which the pointer to Fn is accessed.<br>
+ const BitSetInfo *BS;<br>
+<br>
+ // When doing virtual constant propagation, this stores the return value for<br>
+ // the function when passed the currently considered argument list.<br>
+ uint64_t RetVal;<br>
+<br>
+ // Whether the target is big endian.<br>
+ bool IsBigEndian;<br>
+<br>
+ // The minimum byte offset before the address point. This covers the bytes in<br>
+ // the vtable object before the address point (e.g. RTTI, access-to-top,<br>
+ // vtables for other base classes) and is equal to the offset from the start<br>
+ // of the vtable object to the address point.<br>
+ uint64_t minBeforeBytes() const { return BS->Offset; }<br>
+<br>
+ // The minimum byte offset after the address point. This covers the bytes in<br>
+ // the vtable object after the address point (e.g. the vtable for the current<br>
+ // class and any later base classes) and is equal to the size of the vtable<br>
+ // object minus the offset from the start of the vtable object to the address<br>
+ // point.<br>
+ uint64_t minAfterBytes() const { return BS->Bits->ObjectSize - BS->Offset; }<br>
+<br>
+ // The number of bytes allocated (for the vtable plus the byte array) before<br>
+ // the address point.<br>
+ uint64_t allocatedBeforeBytes() const {<br>
+ return minBeforeBytes() + BS->Bits->Before.Bytes.size();<br>
+ }<br>
+<br>
+ // The number of bytes allocated (for the vtable plus the byte array) after<br>
+ // the address point.<br>
+ uint64_t allocatedAfterBytes() const {<br>
+ return minAfterBytes() + BS->Bits->After.Bytes.size();<br>
+ }<br>
+<br>
+ // Set the bit at position Pos before the address point to RetVal.<br>
+ void setBeforeBit(uint64_t Pos) {<br>
+ assert(Pos >= 8 * minBeforeBytes());<br>
+ BS->Bits->Before.setBit(Pos - 8 * minBeforeBytes(), RetVal);<br>
+ }<br>
+<br>
+ // Set the bit at position Pos after the address point to RetVal.<br>
+ void setAfterBit(uint64_t Pos) {<br>
+ assert(Pos >= 8 * minAfterBytes());<br>
+ BS->Bits->After.setBit(Pos - 8 * minAfterBytes(), RetVal);<br>
+ }<br>
+<br>
+ // Set the bytes at position Pos before the address point to RetVal.<br>
+ // Because the bytes in Before are stored in reverse order, we use the<br>
+ // opposite endianness to the target.<br>
+ void setBeforeBytes(uint64_t Pos, uint8_t Size) {<br>
+ assert(Pos >= 8 * minBeforeBytes());<br>
+ if (IsBigEndian)<br>
+ BS->Bits->Before.setLE(Pos - 8 * minBeforeBytes(), RetVal, Size);<br>
+ else<br>
+ BS->Bits->Before.setBE(Pos - 8 * minBeforeBytes(), RetVal, Size);<br>
+ }<br>
+<br>
+ // Set the bytes at position Pos after the address point to RetVal.<br>
+ void setAfterBytes(uint64_t Pos, uint8_t Size) {<br>
+ assert(Pos >= 8 * minAfterBytes());<br>
+ if (IsBigEndian)<br>
+ BS->Bits->After.setBE(Pos - 8 * minAfterBytes(), RetVal, Size);<br>
+ else<br>
+ BS->Bits->After.setLE(Pos - 8 * minAfterBytes(), RetVal, Size);<br>
+ }<br>
+};<br>
+<br>
+// Find the minimum offset that we may store a value of size Size bits at. If<br>
+// IsAfter is set, look for an offset before the object, otherwise look for an<br>
+// offset after the object.<br>
+uint64_t findLowestOffset(ArrayRef<VirtualCallTarget> Targets, bool IsAfter,<br>
+ uint64_t Size);<br>
+<br>
+// Set the stored value in each of Targets to VirtualCallTarget::RetVal at the<br>
+// given allocation offset before the vtable address. Stores the computed<br>
+// byte/bit offset to OffsetByte/OffsetBit.<br>
+void setBeforeReturnValues(MutableArrayRef<VirtualCallTarget> Targets,<br>
+ uint64_t AllocBefore, unsigned BitWidth,<br>
+ int64_t &OffsetByte, uint64_t &OffsetBit);<br>
+<br>
+// Set the stored value in each of Targets to VirtualCallTarget::RetVal at the<br>
+// given allocation offset after the vtable address. Stores the computed<br>
+// byte/bit offset to OffsetByte/OffsetBit.<br>
+void setAfterReturnValues(MutableArrayRef<VirtualCallTarget> Targets,<br>
+ uint64_t AllocAfter, unsigned BitWidth,<br>
+ int64_t &OffsetByte, uint64_t &OffsetBit);<br>
+<br>
+}<br>
+}<br>
+<br>
+#endif<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/CMakeLists.txt?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/CMakeLists.txt?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/CMakeLists.txt Tue Feb 9 16:50:34 2016<br>
@@ -27,6 +27,7 @@ add_llvm_library(LLVMipo<br>
SampleProfile.cpp<br>
StripDeadPrototypes.cpp<br>
StripSymbols.cpp<br>
+ WholeProgramDevirt.cpp<br>
<br>
ADDITIONAL_HEADER_DIRS<br>
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/IPO.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/IPO.cpp?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/IPO.cpp?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/IPO.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/IPO.cpp Tue Feb 9 16:50:34 2016<br>
@@ -53,6 +53,7 @@ void llvm::initializeIPO(PassRegistry &R<br>
initializeEliminateAvailableExternallyPass(Registry);<br>
initializeSampleProfileLoaderPass(Registry);<br>
initializeFunctionImportPassPass(Registry);<br>
+ initializeWholeProgramDevirtPass(Registry);<br>
}<br>
<br>
void LLVMInitializeIPO(LLVMPassRegistryRef R) {<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Tue Feb 9 16:50:34 2016<br>
@@ -651,6 +651,16 @@ void PassManagerBuilder::addLTOOptimizat<br>
PM.add(createJumpThreadingPass());<br>
}<br>
<br>
+void PassManagerBuilder::addEarlyLTOOptimizationPasses(<br>
+ legacy::PassManagerBase &PM) {<br>
+ // Remove unused virtual tables to improve the quality of code generated by<br>
+ // whole-program devirtualization and bitset lowering.<br>
+ PM.add(createGlobalDCEPass());<br>
+<br>
+ // Apply whole-program devirtualization and virtual constant propagation.<br>
+ PM.add(createWholeProgramDevirtPass());<br>
+}<br>
+<br>
void PassManagerBuilder::addLateLTOOptimizationPasses(<br>
legacy::PassManagerBase &PM) {<br>
// Delete basic blocks, which optimization passes may have killed.<br>
@@ -675,6 +685,9 @@ void PassManagerBuilder::populateLTOPass<br>
if (VerifyInput)<br>
PM.add(createVerifierPass());<br>
<br>
+ if (OptLevel != 0)<br>
+ addEarlyLTOOptimizationPasses(PM);<br>
+<br>
if (OptLevel > 1)<br>
addLTOOptimizationPasses(PM);<br>
<br>
<br>
Added: llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,724 @@<br>
+//===- WholeProgramDevirt.cpp - Whole program virtual call optimization ---===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This pass implements whole program optimization of virtual calls in cases<br>
+// where we know (via bitset information) that the list of callee is fixed. This<br>
+// includes the following:<br>
+// - Single implementation devirtualization: if a virtual call has a single<br>
+// possible callee, replace all calls with a direct call to that callee.<br>
+// - Virtual constant propagation: if the virtual function's return type is an<br>
+// integer <=64 bits and all possible callees are readnone, for each class and<br>
+// each list of constant arguments: evaluate the function, store the return<br>
+// value alongside the virtual table, and rewrite each virtual call as a load<br>
+// from the virtual table.<br>
+// - Uniform return value optimization: if the conditions for virtual constant<br>
+// propagation hold and each function returns the same constant value, replace<br>
+// each virtual call with that constant.<br>
+// - Unique return value optimization for i1 return values: if the conditions<br>
+// for virtual constant propagation hold and a single vtable's function<br>
+// returns 0, or a single vtable's function returns 1, replace each virtual<br>
+// call with a comparison of the vptr against that vtable's address.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/Transforms/IPO/WholeProgramDevirt.h"<br>
+#include "llvm/Transforms/IPO.h"<br>
+#include "llvm/ADT/DenseSet.h"<br>
+#include "llvm/ADT/MapVector.h"<br>
+#include "llvm/IR/CallSite.h"<br>
+#include "llvm/IR/Constants.h"<br>
+#include "llvm/IR/DataLayout.h"<br>
+#include "llvm/IR/IRBuilder.h"<br>
+#include "llvm/IR/Instructions.h"<br>
+#include "llvm/IR/Intrinsics.h"<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/Pass.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+#include "llvm/Transforms/Utils/Evaluator.h"<br>
+#include "llvm/Transforms/Utils/Local.h"<br>
+<br>
+#include <set><br>
+<br>
+using namespace llvm;<br>
+using namespace wholeprogramdevirt;<br>
+<br>
+#define DEBUG_TYPE "wholeprogramdevirt"<br>
+<br>
+// Find the minimum offset that we may store a value of size Size bits at. If<br>
+// IsAfter is set, look for an offset before the object, otherwise look for an<br>
+// offset after the object.<br>
+uint64_t<br>
+wholeprogramdevirt::findLowestOffset(ArrayRef<VirtualCallTarget> Targets,<br>
+ bool IsAfter, uint64_t Size) {<br>
+ // Find a minimum offset taking into account only vtable sizes.<br>
+ uint64_t MinByte = 0;<br>
+ for (const VirtualCallTarget &Target : Targets) {<br>
+ if (IsAfter)<br>
+ MinByte = std::max(MinByte, Target.minAfterBytes());<br>
+ else<br>
+ MinByte = std::max(MinByte, Target.minBeforeBytes());<br>
+ }<br>
+<br>
+ // Build a vector of arrays of bytes covering, for each target, a slice of the<br>
+ // used region (see AccumBitVector::BytesUsed in<br>
+ // llvm/Transforms/IPO/WholeProgramDevirt.h) starting at MinByte. Effectively,<br>
+ // this aligns the used regions to start at MinByte.<br>
+ //<br>
+ // In this example, A, B and C are vtables, # is a byte already allocated for<br>
+ // a virtual function pointer, AAAA... (etc.) are the used regions for the<br>
+ // vtables and Offset(X) is the value computed for the Offset variable below<br>
+ // for X.<br>
+ //<br>
+ // Offset(A)<br>
+ // | |<br>
+ // |MinByte<br>
+ // A: ################AAAAAAAA|AAAAAAAA<br>
+ // B: ########BBBBBBBBBBBBBBBB|BBBB<br>
+ // C: ########################|CCCCCCCCCCCCCCCC<br>
+ // | Offset(B) |<br>
+ //<br>
+ // This code produces the slices of A, B and C that appear after the divider<br>
+ // at MinByte.<br>
+ std::vector<ArrayRef<uint8_t>> Used;<br>
+ for (const VirtualCallTarget &Target : Targets) {<br>
+ ArrayRef<uint8_t> VTUsed = IsAfter ? Target.BS->Bits->After.BytesUsed<br>
+ : Target.BS->Bits->Before.BytesUsed;<br>
+ uint64_t Offset = IsAfter ? MinByte - Target.minAfterBytes()<br>
+ : MinByte - Target.minBeforeBytes();<br>
+<br>
+ // Disregard used regions that are smaller than Offset. These are<br>
+ // effectively all-free regions that do not need to be checked.<br>
+ if (VTUsed.size() > Offset)<br>
+ Used.push_back(VTUsed.slice(Offset));<br>
+ }<br>
+<br>
+ if (Size == 1) {<br>
+ // Find a free bit in each member of Used.<br>
+ for (unsigned I = 0;; ++I) {<br>
+ uint8_t BitsUsed = 0;<br>
+ for (auto &&B : Used)<br>
+ if (I < B.size())<br>
+ BitsUsed |= B[I];<br>
+ if (BitsUsed != 0xff)<br>
+ return (MinByte + I) * 8 +<br>
+ countTrailingZeros(uint8_t(~BitsUsed), ZB_Undefined);<br>
+ }<br>
+ } else {<br>
+ // Find a free (Size/8) byte region in each member of Used.<br>
+ // FIXME: see if alignment helps.<br>
+ for (unsigned I = 0;; ++I) {<br>
+ for (auto &&B : Used) {<br>
+ unsigned Byte = 0;<br>
+ while ((I + Byte) < B.size() && Byte < (Size / 8)) {<br>
+ if (B[I + Byte])<br>
+ goto NextI;<br>
+ ++Byte;<br>
+ }<br>
+ }<br>
+ return (MinByte + I) * 8;<br>
+ NextI:;<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+void wholeprogramdevirt::setBeforeReturnValues(<br>
+ MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocBefore,<br>
+ unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {<br>
+ if (BitWidth == 1)<br>
+ OffsetByte = -(AllocBefore / 8 + 1);<br>
+ else<br>
+ OffsetByte = -((AllocBefore + 7) / 8 + (BitWidth + 7) / 8);<br>
+ OffsetBit = AllocBefore % 8;<br>
+<br>
+ for (VirtualCallTarget &Target : Targets) {<br>
+ if (BitWidth == 1)<br>
+ Target.setBeforeBit(AllocBefore);<br>
+ else<br>
+ Target.setBeforeBytes(AllocBefore, (BitWidth + 7) / 8);<br>
+ }<br>
+}<br>
+<br>
+void wholeprogramdevirt::setAfterReturnValues(<br>
+ MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocAfter,<br>
+ unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {<br>
+ if (BitWidth == 1)<br>
+ OffsetByte = AllocAfter / 8;<br>
+ else<br>
+ OffsetByte = (AllocAfter + 7) / 8;<br>
+ OffsetBit = AllocAfter % 8;<br>
+<br>
+ for (VirtualCallTarget &Target : Targets) {<br>
+ if (BitWidth == 1)<br>
+ Target.setAfterBit(AllocAfter);<br>
+ else<br>
+ Target.setAfterBytes(AllocAfter, (BitWidth + 7) / 8);<br>
+ }<br>
+}<br>
+<br>
+VirtualCallTarget::VirtualCallTarget(Function *Fn, const BitSetInfo *BS)<br>
+ : Fn(Fn), BS(BS),<br>
+ IsBigEndian(Fn->getParent()->getDataLayout().isBigEndian()) {}<br>
+<br>
+namespace {<br>
+<br>
+// A slot in a set of virtual tables. The BitSetID identifies the set of virtual<br>
+// tables, and the ByteOffset is the offset in bytes from the address point to<br>
+// the virtual function pointer.<br>
+struct VTableSlot {<br>
+ Metadata *BitSetID;<br>
+ uint64_t ByteOffset;<br>
+};<br>
+<br>
+}<br>
+<br>
+template <> struct DenseMapInfo<VTableSlot> {<br>
+ static VTableSlot getEmptyKey() {<br>
+ return {DenseMapInfo<Metadata *>::getEmptyKey(),<br>
+ DenseMapInfo<uint64_t>::getEmptyKey()};<br>
+ }<br>
+ static VTableSlot getTombstoneKey() {<br>
+ return {DenseMapInfo<Metadata *>::getTombstoneKey(),<br>
+ DenseMapInfo<uint64_t>::getTombstoneKey()};<br>
+ }<br>
+ static unsigned getHashValue(const VTableSlot &I) {<br>
+ return DenseMapInfo<Metadata *>::getHashValue(I.BitSetID) ^<br>
+ DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset);<br>
+ }<br>
+ static bool isEqual(const VTableSlot &LHS,<br>
+ const VTableSlot &RHS) {<br>
+ return LHS.BitSetID == RHS.BitSetID && LHS.ByteOffset == RHS.ByteOffset;<br>
+ }<br>
+};<br>
+<br>
+namespace {<br>
+<br>
+// A virtual call site. VTable is the loaded virtual table pointer, and CS is<br>
+// the indirect virtual call.<br>
+struct VirtualCallSite {<br>
+ Value *VTable;<br>
+ CallSite CS;<br>
+<br>
+ void replaceAndErase(Value *New) {<br>
+ CS->replaceAllUsesWith(New);<br>
+ if (auto II = dyn_cast<InvokeInst>(CS.getInstruction())) {<br>
+ BranchInst::Create(II->getNormalDest(), CS.getInstruction());<br>
+ II->getUnwindDest()->removePredecessor(II->getParent());<br>
+ }<br>
+ CS->eraseFromParent();<br>
+ }<br>
+};<br>
+<br>
+struct DevirtModule {<br>
+ Module &M;<br>
+ IntegerType *Int8Ty;<br>
+ PointerType *Int8PtrTy;<br>
+ IntegerType *Int32Ty;<br>
+<br>
+ MapVector<VTableSlot, std::vector<VirtualCallSite>> CallSlots;<br>
+<br>
+ DevirtModule(Module &M)<br>
+ : M(M), Int8Ty(Type::getInt8Ty(M.getContext())),<br>
+ Int8PtrTy(Type::getInt8PtrTy(M.getContext())),<br>
+ Int32Ty(Type::getInt32Ty(M.getContext())) {}<br>
+ void findLoadCallsAtConstantOffset(Metadata *BitSet, Value *Ptr,<br>
+ uint64_t Offset, Value *VTable);<br>
+ void findCallsAtConstantOffset(Metadata *BitSet, Value *Ptr, uint64_t Offset,<br>
+ Value *VTable);<br>
+<br>
+ void buildBitSets(std::vector<VTableBits> &Bits,<br>
+ DenseMap<Metadata *, std::set<BitSetInfo>> &BitSets);<br>
+ bool tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot,<br>
+ const std::set<BitSetInfo> &BitSetInfos,<br>
+ uint64_t ByteOffset);<br>
+ bool trySingleImplDevirt(ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites);<br>
+ bool tryEvaluateFunctionsWithArgs(<br>
+ MutableArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ ArrayRef<ConstantInt *> Args);<br>
+ bool tryUniformRetValOpt(IntegerType *RetType,<br>
+ ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites);<br>
+ bool tryUniqueRetValOpt(unsigned BitWidth,<br>
+ ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites);<br>
+ bool tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ ArrayRef<VirtualCallSite> CallSites);<br>
+<br>
+ void rebuildGlobal(VTableBits &B);<br>
+<br>
+ bool run();<br>
+};<br>
+<br>
+struct WholeProgramDevirt : public ModulePass {<br>
+ static char ID;<br>
+ WholeProgramDevirt() : ModulePass(ID) {<br>
+ initializeWholeProgramDevirtPass(*PassRegistry::getPassRegistry());<br>
+ }<br>
+ bool runOnModule(Module &M) { return DevirtModule(M).run(); }<br>
+};<br>
+<br>
+} // anonymous namespace<br>
+<br>
+INITIALIZE_PASS(WholeProgramDevirt, "wholeprogramdevirt",<br>
+ "Whole program devirtualization", false, false)<br>
+char WholeProgramDevirt::ID = 0;<br>
+<br>
+ModulePass *llvm::createWholeProgramDevirtPass() {<br>
+ return new WholeProgramDevirt;<br>
+}<br>
+<br>
+// Search for virtual calls that call FPtr and add them to CallSlots.<br>
+void DevirtModule::findCallsAtConstantOffset(Metadata *BitSet, Value *FPtr,<br>
+ uint64_t Offset, Value *VTable) {<br>
+ for (const Use &U : FPtr->uses()) {<br>
+ Value *User = U.getUser();<br>
+ if (isa<BitCastInst>(User)) {<br>
+ findCallsAtConstantOffset(BitSet, User, Offset, VTable);<br>
+ } else if (auto CI = dyn_cast<CallInst>(User)) {<br>
+ CallSlots[{BitSet, Offset}].push_back({VTable, CI});<br>
+ } else if (auto II = dyn_cast<InvokeInst>(User)) {<br>
+ CallSlots[{BitSet, Offset}].push_back({VTable, II});<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+// Search for virtual calls that load from VPtr and add them to CallSlots.<br>
+void DevirtModule::findLoadCallsAtConstantOffset(Metadata *BitSet, Value *VPtr,<br>
+ uint64_t Offset,<br>
+ Value *VTable) {<br>
+ for (const Use &U : VPtr->uses()) {<br>
+ Value *User = U.getUser();<br>
+ if (isa<BitCastInst>(User)) {<br>
+ findLoadCallsAtConstantOffset(BitSet, User, Offset, VTable);<br>
+ } else if (isa<LoadInst>(User)) {<br>
+ findCallsAtConstantOffset(BitSet, User, Offset, VTable);<br>
+ } else if (auto GEP = dyn_cast<GetElementPtrInst>(User)) {<br>
+ // Take into account the GEP offset.<br>
+ if (VPtr == GEP->getPointerOperand() && GEP->hasAllConstantIndices()) {<br>
+ SmallVector<Value *, 8> Indices(GEP->op_begin() + 1, GEP->op_end());<br>
+ uint64_t GEPOffset = M.getDataLayout().getIndexedOffsetInType(<br>
+ GEP->getSourceElementType(), Indices);<br>
+ findLoadCallsAtConstantOffset(BitSet, User, Offset + GEPOffset, VTable);<br>
+ }<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+void DevirtModule::buildBitSets(<br>
+ std::vector<VTableBits> &Bits,<br>
+ DenseMap<Metadata *, std::set<BitSetInfo>> &BitSets) {<br>
+ NamedMDNode *BitSetNM = M.getNamedMetadata("llvm.bitsets");<br>
+ if (!BitSetNM)<br>
+ return;<br>
+<br>
+ DenseMap<GlobalVariable *, VTableBits *> GVToBits;<br>
+ Bits.reserve(BitSetNM->getNumOperands());<br>
+ for (auto Op : BitSetNM->operands()) {<br>
+ auto OpConstMD = dyn_cast_or_null<ConstantAsMetadata>(Op->getOperand(1));<br>
+ if (!OpConstMD)<br>
+ continue;<br>
+ auto BitSetID = Op->getOperand(0).get();<br>
+<br>
+ Constant *OpConst = OpConstMD->getValue();<br>
+ if (auto GA = dyn_cast<GlobalAlias>(OpConst))<br>
+ OpConst = GA->getAliasee();<br>
+ auto OpGlobal = dyn_cast<GlobalVariable>(OpConst);<br>
+ if (!OpGlobal)<br>
+ continue;<br>
+<br>
+ uint64_t Offset =<br>
+ cast<ConstantInt>(<br>
+ cast<ConstantAsMetadata>(Op->getOperand(2))->getValue())<br>
+ ->getZExtValue();<br>
+<br>
+ VTableBits *&BitsPtr = GVToBits[OpGlobal];<br>
+ if (!BitsPtr) {<br>
+ Bits.emplace_back();<br>
+ Bits.back().GV = OpGlobal;<br>
+ Bits.back().ObjectSize = M.getDataLayout().getTypeAllocSize(<br>
+ OpGlobal->getInitializer()->getType());<br>
+ BitsPtr = &Bits.back();<br>
+ }<br>
+ BitSets[BitSetID].insert({BitsPtr, Offset});<br>
+ }<br>
+}<br>
+<br>
+bool DevirtModule::tryFindVirtualCallTargets(<br>
+ std::vector<VirtualCallTarget> &TargetsForSlot,<br>
+ const std::set<BitSetInfo> &BitSetInfos, uint64_t ByteOffset) {<br>
+ for (const BitSetInfo &BS : BitSetInfos) {<br>
+ if (!BS.Bits->GV->isConstant())<br>
+ return false;<br>
+<br>
+ auto Init = dyn_cast<ConstantArray>(BS.Bits->GV->getInitializer());<br>
+ if (!Init)<br>
+ return false;<br>
+ ArrayType *VTableTy = Init->getType();<br>
+<br>
+ uint64_t ElemSize =<br>
+ M.getDataLayout().getTypeAllocSize(VTableTy->getElementType());<br>
+ uint64_t GlobalSlotOffset = BS.Offset + ByteOffset;<br>
+ if (GlobalSlotOffset % ElemSize != 0)<br>
+ return false;<br>
+<br>
+ unsigned Op = GlobalSlotOffset / ElemSize;<br>
+ if (Op >= Init->getNumOperands())<br>
+ return false;<br>
+<br>
+ auto Fn = dyn_cast<Function>(Init->getOperand(Op)->stripPointerCasts());<br>
+ if (!Fn)<br>
+ return false;<br>
+<br>
+ // We can disregard __cxa_pure_virtual as a possible call target, as<br>
+ // calls to pure virtuals are UB.<br>
+ if (Fn->getName() == "__cxa_pure_virtual")<br>
+ continue;<br>
+<br>
+ TargetsForSlot.push_back({Fn, &BS});<br>
+ }<br>
+<br>
+ // Give up if we couldn't find any targets.<br>
+ return !TargetsForSlot.empty();<br>
+}<br>
+<br>
+bool DevirtModule::trySingleImplDevirt(<br>
+ ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites) {<br>
+ // See if the program contains a single implementation of this virtual<br>
+ // function.<br>
+ Function *TheFn = TargetsForSlot[0].Fn;<br>
+ for (auto &&Target : TargetsForSlot)<br>
+ if (TheFn != Target.Fn)<br>
+ return false;<br>
+<br>
+ // If so, update each call site to call that implementation directly.<br>
+ for (auto &&VCallSite : CallSites) {<br>
+ VCallSite.CS.setCalledFunction(ConstantExpr::getBitCast(<br>
+ TheFn, VCallSite.CS.getCalledValue()->getType()));<br>
+ }<br>
+ return true;<br>
+}<br>
+<br>
+bool DevirtModule::tryEvaluateFunctionsWithArgs(<br>
+ MutableArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ ArrayRef<ConstantInt *> Args) {<br>
+ // Evaluate each function and store the result in each target's RetVal<br>
+ // field.<br>
+ for (VirtualCallTarget &Target : TargetsForSlot) {<br>
+ if (Target.Fn->arg_size() != Args.size() + 1)<br>
+ return false;<br>
+ for (unsigned I = 0; I != Args.size(); ++I)<br>
+ if (Target.Fn->getFunctionType()->getParamType(I + 1) !=<br>
+ Args[I]->getType())<br>
+ return false;<br>
+<br>
+ Evaluator Eval(M.getDataLayout(), nullptr);<br>
+ SmallVector<Constant *, 2> EvalArgs;<br>
+ EvalArgs.push_back(<br>
+ Constant::getNullValue(Target.Fn->getFunctionType()->getParamType(0)));<br>
+ EvalArgs.insert(EvalArgs.end(), Args.begin(), Args.end());<br>
+ Constant *RetVal;<br>
+ if (!Eval.EvaluateFunction(Target.Fn, RetVal, EvalArgs) ||<br>
+ !isa<ConstantInt>(RetVal))<br>
+ return false;<br>
+ Target.RetVal = cast<ConstantInt>(RetVal)->getZExtValue();<br>
+ }<br>
+ return true;<br>
+}<br>
+<br>
+bool DevirtModule::tryUniformRetValOpt(<br>
+ IntegerType *RetType, ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites) {<br>
+ // Uniform return value optimization. If all functions return the same<br>
+ // constant, replace all calls with that constant.<br>
+ uint64_t TheRetVal = TargetsForSlot[0].RetVal;<br>
+ for (const VirtualCallTarget &Target : TargetsForSlot)<br>
+ if (Target.RetVal != TheRetVal)<br>
+ return false;<br>
+<br>
+ auto TheRetValConst = ConstantInt::get(RetType, TheRetVal);<br>
+ for (auto Call : CallSites)<br>
+ Call.replaceAndErase(TheRetValConst);<br>
+ return true;<br>
+}<br>
+<br>
+bool DevirtModule::tryUniqueRetValOpt(<br>
+ unsigned BitWidth, ArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ MutableArrayRef<VirtualCallSite> CallSites) {<br>
+ // IsOne controls whether we look for a 0 or a 1.<br>
+ auto tryUniqueRetValOptFor = [&](bool IsOne) {<br>
+ const BitSetInfo *UniqueBitSet = 0;<br>
+ for (const VirtualCallTarget &Target : TargetsForSlot) {<br>
+ if (Target.RetVal == IsOne ? 1 : 0) {<br>
+ if (UniqueBitSet)<br>
+ return false;<br>
+ UniqueBitSet = Target.BS;<br>
+ }<br>
+ }<br>
+<br>
+ // We should have found a unique bit set or bailed out by now. We already<br>
+ // checked for a uniform return value in tryUniformRetValOpt.<br>
+ assert(UniqueBitSet);<br>
+<br>
+ // Replace each call with the comparison.<br>
+ for (auto &&Call : CallSites) {<br>
+ IRBuilder<> B(Call.CS.getInstruction());<br>
+ Value *OneAddr = B.CreateBitCast(UniqueBitSet->Bits->GV, Int8PtrTy);<br>
+ OneAddr = B.CreateConstGEP1_64(OneAddr, UniqueBitSet->Offset);<br>
+ Value *Cmp = B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,<br>
+ Call.VTable, OneAddr);<br>
+ Call.replaceAndErase(Cmp);<br>
+ }<br>
+ return true;<br>
+ };<br>
+<br>
+ if (BitWidth == 1) {<br>
+ if (tryUniqueRetValOptFor(true))<br>
+ return true;<br>
+ if (tryUniqueRetValOptFor(false))<br>
+ return true;<br>
+ }<br>
+ return false;<br>
+}<br>
+<br>
+bool DevirtModule::tryVirtualConstProp(<br>
+ MutableArrayRef<VirtualCallTarget> TargetsForSlot,<br>
+ ArrayRef<VirtualCallSite> CallSites) {<br>
+ // This only works if the function returns an integer.<br>
+ auto RetType = dyn_cast<IntegerType>(TargetsForSlot[0].Fn->getReturnType());<br>
+ if (!RetType)<br>
+ return false;<br>
+ unsigned BitWidth = RetType->getBitWidth();<br>
+ if (BitWidth > 64)<br>
+ return false;<br>
+<br>
+ // Make sure that each function does not access memory, takes at least one<br>
+ // argument, does not use its first argument (which we assume is 'this'),<br>
+ // and has the same return type.<br>
+ for (VirtualCallTarget &Target : TargetsForSlot) {<br>
+ if (!Target.Fn->doesNotAccessMemory() || Target.Fn->arg_empty() ||<br>
+ !Target.Fn->arg_begin()->use_empty() ||<br>
+ Target.Fn->getReturnType() != RetType)<br>
+ return false;<br>
+ }<br>
+<br>
+ // Group call sites by the list of constant arguments they pass.<br>
+ // The comparator ensures deterministic ordering.<br>
+ struct ByAPIntValue {<br>
+ bool operator()(const std::vector<ConstantInt *> &A,<br>
+ const std::vector<ConstantInt *> &B) const {<br>
+ return std::lexicographical_compare(<br>
+ A.begin(), A.end(), B.begin(), B.end(),<br>
+ [](ConstantInt *AI, ConstantInt *BI) {<br>
+ return AI->getValue().ult(BI->getValue());<br>
+ });<br>
+ }<br>
+ };<br>
+ std::map<std::vector<ConstantInt *>, std::vector<VirtualCallSite>,<br>
+ ByAPIntValue><br>
+ VCallSitesByConstantArg;<br>
+ for (auto &&VCallSite : CallSites) {<br>
+ std::vector<ConstantInt *> Args;<br>
+ if (VCallSite.CS.getType() != RetType)<br>
+ continue;<br>
+ for (auto &&Arg :<br>
+ make_range(VCallSite.CS.arg_begin() + 1, VCallSite.CS.arg_end())) {<br>
+ if (!isa<ConstantInt>(Arg))<br>
+ break;<br>
+ Args.push_back(cast<ConstantInt>(&Arg));<br>
+ }<br>
+ if (Args.size() + 1 != VCallSite.CS.arg_size())<br>
+ continue;<br>
+<br>
+ VCallSitesByConstantArg[Args].push_back(VCallSite);<br>
+ }<br>
+<br>
+ for (auto &&CSByConstantArg : VCallSitesByConstantArg) {<br>
+ if (!tryEvaluateFunctionsWithArgs(TargetsForSlot, CSByConstantArg.first))<br>
+ continue;<br>
+<br>
+ if (tryUniformRetValOpt(RetType, TargetsForSlot, CSByConstantArg.second))<br>
+ continue;<br>
+<br>
+ if (tryUniqueRetValOpt(BitWidth, TargetsForSlot, CSByConstantArg.second))<br>
+ continue;<br>
+<br>
+ // Find an allocation offset in bits in all vtables in the bitset.<br>
+ uint64_t AllocBefore =<br>
+ findLowestOffset(TargetsForSlot, /*IsAfter=*/false, BitWidth);<br>
+ uint64_t AllocAfter =<br>
+ findLowestOffset(TargetsForSlot, /*IsAfter=*/true, BitWidth);<br>
+<br>
+ // Calculate the total amount of padding needed to store a value at both<br>
+ // ends of the object.<br>
+ uint64_t TotalPaddingBefore = 0, TotalPaddingAfter = 0;<br>
+ for (auto &&Target : TargetsForSlot) {<br>
+ TotalPaddingBefore += std::max<int64_t>(<br>
+ (AllocBefore + 7) / 8 - Target.allocatedBeforeBytes() - 1, 0);<br>
+ TotalPaddingAfter += std::max<int64_t>(<br>
+ (AllocAfter + 7) / 8 - Target.allocatedAfterBytes() - 1, 0);<br>
+ }<br>
+<br>
+ // If the amount of padding is too large, give up.<br>
+ // FIXME: do something smarter here.<br>
+ if (std::min(TotalPaddingBefore, TotalPaddingAfter) > 128)<br>
+ continue;<br>
+<br>
+ // Calculate the offset to the value as a (possibly negative) byte offset<br>
+ // and (if applicable) a bit offset, and store the values in the targets.<br>
+ int64_t OffsetByte;<br>
+ uint64_t OffsetBit;<br>
+ if (TotalPaddingBefore <= TotalPaddingAfter)<br>
+ setBeforeReturnValues(TargetsForSlot, AllocBefore, BitWidth, OffsetByte,<br>
+ OffsetBit);<br>
+ else<br>
+ setAfterReturnValues(TargetsForSlot, AllocAfter, BitWidth, OffsetByte,<br>
+ OffsetBit);<br>
+<br>
+ // Rewrite each call to a load from OffsetByte/OffsetBit.<br>
+ for (auto Call : CSByConstantArg.second) {<br>
+ IRBuilder<> B(Call.CS.getInstruction());<br>
+ Value *Addr = B.CreateConstGEP1_64(Call.VTable, OffsetByte);<br>
+ if (BitWidth == 1) {<br>
+ Value *Bits = B.CreateLoad(Addr);<br>
+ Value *Bit = ConstantInt::get(Int8Ty, 1 << OffsetBit);<br>
+ Value *BitsAndBit = B.CreateAnd(Bits, Bit);<br>
+ auto IsBitSet = B.CreateICmpNE(BitsAndBit, ConstantInt::get(Int8Ty, 0));<br>
+ Call.replaceAndErase(IsBitSet);<br>
+ } else {<br>
+ Value *ValAddr = B.CreateBitCast(Addr, RetType->getPointerTo());<br>
+ Value *Val = B.CreateLoad(RetType, ValAddr);<br>
+ Call.replaceAndErase(Val);<br>
+ }<br>
+ }<br>
+ }<br>
+ return true;<br>
+}<br>
+<br>
+void DevirtModule::rebuildGlobal(VTableBits &B) {<br>
+ if (B.Before.Bytes.empty() && B.After.Bytes.empty())<br>
+ return;<br>
+<br>
+ // Align each byte array to pointer width.<br>
+ unsigned PointerSize = M.getDataLayout().getPointerSize();<br>
+ B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), PointerSize));<br>
+ B.After.Bytes.resize(alignTo(B.After.Bytes.size(), PointerSize));<br>
+<br>
+ // Before was stored in reverse order; flip it now.<br>
+ for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I)<br>
+ std::swap(B.Before.Bytes[I], B.Before.Bytes[Size - 1 - I]);<br>
+<br>
+ // Build an anonymous global containing the before bytes, followed by the<br>
+ // original initializer, followed by the after bytes.<br>
+ auto NewInit = ConstantStruct::getAnon(<br>
+ {ConstantDataArray::get(M.getContext(), B.Before.Bytes),<br>
+ B.GV->getInitializer(),<br>
+ ConstantDataArray::get(M.getContext(), B.After.Bytes)});<br>
+ auto NewGV =<br>
+ new GlobalVariable(M, NewInit->getType(), B.GV->isConstant(),<br>
+ GlobalVariable::PrivateLinkage, NewInit, "", B.GV);<br>
+ NewGV->setSection(B.GV->getSection());<br>
+ NewGV->setComdat(B.GV->getComdat());<br>
+<br>
+ // Build an alias named after the original global, pointing at the second<br>
+ // element (the original initializer).<br>
+ auto Alias = GlobalAlias::create(<br>
+ B.GV->getInitializer()->getType(), 0, B.GV->getLinkage(), "",<br>
+ ConstantExpr::getGetElementPtr(<br>
+ NewInit->getType(), NewGV,<br>
+ ArrayRef<Constant *>{ConstantInt::get(Int32Ty, 0),<br>
+ ConstantInt::get(Int32Ty, 1)}),<br>
+ &M);<br>
+ Alias->setVisibility(B.GV->getVisibility());<br>
+ Alias->takeName(B.GV);<br>
+<br>
+ B.GV->replaceAllUsesWith(Alias);<br>
+ B.GV->eraseFromParent();<br>
+}<br>
+<br>
+bool DevirtModule::run() {<br>
+ Function *BitSetTestFunc =<br>
+ M.getFunction(Intrinsic::getName(Intrinsic::bitset_test));<br>
+ if (!BitSetTestFunc || BitSetTestFunc->use_empty())<br>
+ return false;<br>
+<br>
+ Function *AssumeFunc = M.getFunction(Intrinsic::getName(Intrinsic::assume));<br>
+ if (!AssumeFunc || AssumeFunc->use_empty())<br>
+ return false;<br>
+<br>
+ // Find all virtual calls via a virtual table pointer %p under an assumption<br>
+ // of the form llvm.assume(llvm.bitset.test(%p, %md)). This indicates that %p<br>
+ // points to a vtable in the bitset %md. Group calls by (bitset, offset) pair<br>
+ // (effectively the identity of the virtual function) and store to CallSlots.<br>
+ DenseSet<Value *> SeenPtrs;<br>
+ for (auto I = BitSetTestFunc->use_begin(), E = BitSetTestFunc->use_end();<br>
+ I != E;) {<br>
+ auto CI = dyn_cast<CallInst>(I->getUser());<br>
+ ++I;<br>
+ if (!CI)<br>
+ continue;<br>
+<br>
+ // Find llvm.assume intrinsics for this llvm.bitset.test call.<br>
+ SmallVector<CallInst *, 1> Assumes;<br>
+ for (const Use &CIU : CI->uses()) {<br>
+ auto AssumeCI = dyn_cast<CallInst>(CIU.getUser());<br>
+ if (AssumeCI && AssumeCI->getCalledValue() == AssumeFunc)<br>
+ Assumes.push_back(AssumeCI);<br>
+ }<br>
+<br>
+ // If we found any, search for virtual calls based on %p and add them to<br>
+ // CallSlots.<br>
+ if (!Assumes.empty()) {<br>
+ Metadata *BitSet =<br>
+ cast<MetadataAsValue>(CI->getArgOperand(1))->getMetadata();<br>
+ Value *Ptr = CI->getArgOperand(0)->stripPointerCasts();<br>
+ if (SeenPtrs.insert(Ptr).second)<br>
+ findLoadCallsAtConstantOffset(BitSet, Ptr, 0, CI->getArgOperand(0));<br>
+ }<br>
+<br>
+ // We no longer need the assumes or the bitset test.<br>
+ for (auto Assume : Assumes)<br>
+ Assume->eraseFromParent();<br>
+ // We can't use RecursivelyDeleteTriviallyDeadInstructions here because we<br>
+ // may use the vtable argument later.<br>
+ if (CI->use_empty())<br>
+ CI->eraseFromParent();<br>
+ }<br>
+<br>
+ // Rebuild llvm.bitsets metadata into a map for easy lookup.<br>
+ std::vector<VTableBits> Bits;<br>
+ DenseMap<Metadata *, std::set<BitSetInfo>> BitSets;<br>
+ buildBitSets(Bits, BitSets);<br>
+ if (BitSets.empty())<br>
+ return true;<br>
+<br>
+ // For each (bitset, offset) pair:<br>
+ bool DidVirtualConstProp = false;<br>
+ for (auto &S : CallSlots) {<br>
+ // Search each of the vtables in the bitset for the virtual function<br>
+ // implementation at offset S.first.ByteOffset, and add to TargetsForSlot.<br>
+ std::vector<VirtualCallTarget> TargetsForSlot;<br>
+ if (!tryFindVirtualCallTargets(TargetsForSlot, BitSets[S.first.BitSetID],<br>
+ S.first.ByteOffset))<br>
+ continue;<br>
+<br>
+ if (trySingleImplDevirt(TargetsForSlot, S.second))<br>
+ continue;<br>
+<br>
+ DidVirtualConstProp |= tryVirtualConstProp(TargetsForSlot, S.second);<br>
+ }<br>
+<br>
+ // Rebuild each global we touched as part of virtual constant propagation to<br>
+ // include the before and after bytes.<br>
+ if (DidVirtualConstProp)<br>
+ for (VTableBits &B : Bits)<br>
+ rebuildGlobal(B);<br>
+<br>
+ return true;<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,64 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt = global [2 x i8*] [i8* zeroinitializer, i8* bitcast (void (i8*)* @vf to i8*)]<br>
+<br>
+define void @vf(i8* %this) {<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @unaligned<br>
+define void @unaligned(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr i8, i8* %vtablei8, i32 1<br>
+ %fptrptr_casted = bitcast i8* %fptrptr to i8**<br>
+ %fptr = load i8*, i8** %fptrptr_casted<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void %<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @outofbounds<br>
+define void @outofbounds(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr i8, i8* %vtablei8, i32 16<br>
+ %fptrptr_casted = bitcast i8* %fptrptr to i8**<br>
+ %fptr = load i8*, i8** %fptrptr_casted<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void %<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @nonfunction<br>
+define void @nonfunction(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr i8, i8* %vtablei8, i32 0<br>
+ %fptrptr_casted = bitcast i8* %fptrptr to i8**<br>
+ %fptr = load i8*, i8** %fptrptr_casted<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void %<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [2 x i8*]* @vt, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/constant-arg.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,79 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf1 to i8*)], [0 x i8] zeroinitializer }<br>
+; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\02", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf2 to i8*)], [0 x i8] zeroinitializer }<br>
+; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf4 to i8*)], [0 x i8] zeroinitializer }<br>
+; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\02", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf8 to i8*)], [0 x i8] zeroinitializer }<br>
+<br>
+@vt1 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf1 to i8*)]<br>
+@vt2 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf2 to i8*)]<br>
+@vt4 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf4 to i8*)]<br>
+@vt8 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf8 to i8*)]<br>
+<br>
+define i1 @vf1(i8* %this, i32 %arg) readnone {<br>
+ %and = and i32 %arg, 1<br>
+ %cmp = icmp ne i32 %and, 0<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+define i1 @vf2(i8* %this, i32 %arg) readnone {<br>
+ %and = and i32 %arg, 2<br>
+ %cmp = icmp ne i32 %and, 0<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+define i1 @vf4(i8* %this, i32 %arg) readnone {<br>
+ %and = and i32 %arg, 4<br>
+ %cmp = icmp ne i32 %and, 0<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+define i1 @vf8(i8* %this, i32 %arg) readnone {<br>
+ %and = and i32 %arg, 8<br>
+ %cmp = icmp ne i32 %and, 0<br>
+ ret i1 %cmp<br>
+}<br>
+<br>
+; CHECK: define i1 @call1<br>
+define i1 @call1(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)*<br>
+ ; CHECK: getelementptr {{.*}} -1<br>
+ ; CHECK: and {{.*}}, 1<br>
+ %result = call i1 %fptr_casted(i8* %obj, i32 5)<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i1 @call2<br>
+define i1 @call2(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)*<br>
+ ; CHECK: getelementptr {{.*}} -1<br>
+ ; CHECK: and {{.*}}, 2<br>
+ %result = call i1 %fptr_casted(i8* %obj, i32 10)<br>
+ ret i1 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!2 = !{!"bitset", [1 x i8*]* @vt4, i32 0}<br>
+!3 = !{!"bitset", [1 x i8*]* @vt8, i32 0}<br>
+!llvm.bitsets = !{!0, !1, !2, !3}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,33 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)]<br>
+@vt2 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)]<br>
+<br>
+define void @vf(i8* %this) {<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @call<br>
+define void @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void @vf(<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0, !1}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/non-array-vtable.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,31 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt = constant i8* bitcast (void (i8*)* @vf to i8*)<br>
+<br>
+define void @vf(i8* %this) {<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @call<br>
+define void @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void %<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", i8** @vt, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,31 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt = global [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)]<br>
+<br>
+define void @vf(i8* %this) {<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @call<br>
+define void @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to void (i8*)*<br>
+ ; CHECK: call void %<br>
+ call void %fptr_casted(i8* %obj)<br>
+ ret void<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,45 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)]<br>
+@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this) readnone {<br>
+ ret i32 123<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this) readnone {<br>
+ ret i32 123<br>
+}<br>
+<br>
+; CHECK: define i32 @call<br>
+define i32 @call(i8* %obj) personality i8* undef {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*)*<br>
+ ; CHECK: br label %[[RET:[0-9A-Za-z]*]]<br>
+ %result = invoke i32 %fptr_casted(i8* %obj) to label %ret unwind label %unwind<br>
+<br>
+unwind:<br>
+ %x = landingpad i32 cleanup<br>
+ unreachable<br>
+<br>
+ret:<br>
+ ; CHECK: [[RET]]:<br>
+ ; CHECK-NEXT: ret i32 123<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0, !1}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/uniform-retval.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,38 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)]<br>
+@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this) readnone {<br>
+ ret i32 123<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this) readnone {<br>
+ ret i32 123<br>
+}<br>
+<br>
+; CHECK: define i32 @call<br>
+define i32 @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*)*<br>
+ %result = call i32 %fptr_casted(i8* %obj)<br>
+ ; CHECK-NOT: call<br>
+ ; CHECK: ret i32 123<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0, !1}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/unique-retval.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,61 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf0 to i8*)]<br>
+@vt2 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf0 to i8*)]<br>
+@vt3 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf1 to i8*)]<br>
+@vt4 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf1 to i8*)]<br>
+<br>
+define i1 @vf0(i8* %this) readnone {<br>
+ ret i1 0<br>
+}<br>
+<br>
+define i1 @vf1(i8* %this) readnone {<br>
+ ret i1 1<br>
+}<br>
+<br>
+; CHECK: define i1 @call1<br>
+define i1 @call1(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT1:%[^ ]*]] = bitcast [1 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset1")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[RES1:%[^ ]*]] = icmp eq i8* [[VT1]], bitcast ([1 x i8*]* @vt3 to i8*)<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i1 [[RES1]]<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i1 @call2<br>
+define i1 @call2(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT2:%[^ ]*]] = bitcast [1 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset2")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[RES1:%[^ ]*]] = icmp ne i8* [[VT1]], bitcast ([1 x i8*]* @vt2 to i8*)<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ret i1 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset1", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset1", [1 x i8*]* @vt2, i32 0}<br>
+!2 = !{!"bitset1", [1 x i8*]* @vt3, i32 0}<br>
+!3 = !{!"bitset2", [1 x i8*]* @vt2, i32 0}<br>
+!4 = !{!"bitset2", [1 x i8*]* @vt3, i32 0}<br>
+!5 = !{!"bitset2", [1 x i8*]* @vt4, i32 0}<br>
+!llvm.bitsets = !{!0, !1, !2, !3, !4, !5}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,37 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this, i32 %arg) {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this, i32 %arg) {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+; CHECK: define i32 @call<br>
+define i32 @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted(i8* %obj, i32 1)<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-no-this.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,37 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i32 ()* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i32 ()* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1() readnone {<br>
+ ret i32 1<br>
+}<br>
+<br>
+define i32 @vf2() readnone {<br>
+ ret i32 2<br>
+}<br>
+<br>
+; CHECK: define i32 @call<br>
+define i32 @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 ()*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted()<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,37 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this, i32 %arg) readnone {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this, i32 %arg) readnone {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+; CHECK: define void @call<br>
+define void @call(i8* %obj, i32 %arg) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted(i8* %obj, i32 %arg)<br>
+ ret void<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,37 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i128 (i8*, i128)* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i128 (i8*, i128)* @vf2 to i8*)]<br>
+<br>
+define i128 @vf1(i8* %this, i128 %arg) readnone {<br>
+ ret i128 %arg<br>
+}<br>
+<br>
+define i128 @vf2(i8* %this, i128 %arg) readnone {<br>
+ ret i128 %arg<br>
+}<br>
+<br>
+; CHECK: define i128 @call<br>
+define i128 @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i128 (i8*, i128)*<br>
+ ; CHECK: call i128 %<br>
+ %result = call i128 %fptr_casted(i8* %obj, i128 1)<br>
+ ret i128 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,67 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this, i32 %arg) readnone {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this, i32 %arg) readnone {<br>
+ ret i32 %arg<br>
+}<br>
+<br>
+; CHECK: define i32 @bad_arg_type<br>
+define i32 @bad_arg_type(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*, i64)*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted(i8* %obj, i64 1)<br>
+ ret i32 %result<br>
+}<br>
+<br>
+; CHECK: define i32 @bad_arg_count<br>
+define i32 @bad_arg_count(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*, i64, i64)*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted(i8* %obj, i64 1, i64 2)<br>
+ ret i32 %result<br>
+}<br>
+<br>
+; CHECK: define i64 @bad_return_type<br>
+define i64 @bad_return_type(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i64 (i8*, i32)*<br>
+ ; CHECK: call i64 %<br>
+ %result = call i64 %fptr_casted(i8* %obj, i32 1)<br>
+ ret i64 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,39 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@vt1 = global [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)]<br>
+@vt2 = global [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)]<br>
+<br>
+define i32 @vf1(i8* %this) readnone {<br>
+ %this_int = ptrtoint i8* %this to i32<br>
+ ret i32 %this_int<br>
+}<br>
+<br>
+define i32 @vf2(i8* %this) readnone {<br>
+ %this_int = ptrtoint i8* %this to i32<br>
+ ret i32 %this_int<br>
+}<br>
+<br>
+; CHECK: define i32 @call<br>
+define i32 @call(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [1 x i8*]**<br>
+ %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr<br>
+ %vtablei8 = bitcast [1 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*)*<br>
+ ; CHECK: call i32 %<br>
+ %result = call i32 %fptr_casted(i8* %obj)<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [1 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [1 x i8*]* @vt2, i32 0}<br>
+!llvm.bitsets = !{!0}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,137 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: [[VT1DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\01\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf1i32 to i8*)], [0 x i8] zeroinitializer }, section "vt1sec"<br>
+@vt1 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf1i32 to i8*)<br>
+], section "vt1sec"<br>
+<br>
+; CHECK: [[VT2DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\02\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf2i32 to i8*)], [0 x i8] zeroinitializer }{{$}}<br>
+@vt2 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf2i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: [[VT3DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\03\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf3i32 to i8*)], [0 x i8] zeroinitializer }{{$}}<br>
+@vt3 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf3i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: [[VT4DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\04\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf4i32 to i8*)], [0 x i8] zeroinitializer }{{$}}<br>
+@vt4 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf4i32 to i8*)<br>
+]<br>
+<br>
+@vt5 = constant [3 x i8*] [<br>
+i8* bitcast (void ()* @__cxa_pure_virtual to i8*),<br>
+i8* bitcast (void ()* @__cxa_pure_virtual to i8*),<br>
+i8* bitcast (void ()* @__cxa_pure_virtual to i8*)<br>
+]<br>
+<br>
+; CHECK: @vt1 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT1DATA]], i32 0, i32 1)<br>
+; CHECK: @vt2 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT2DATA]], i32 0, i32 1)<br>
+; CHECK: @vt3 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT3DATA]], i32 0, i32 1)<br>
+; CHECK: @vt4 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT4DATA]], i32 0, i32 1)<br>
+<br>
+define i1 @vf0i1(i8* %this) readnone {<br>
+ ret i1 0<br>
+}<br>
+<br>
+define i1 @vf1i1(i8* %this) readnone {<br>
+ ret i1 1<br>
+}<br>
+<br>
+define i32 @vf1i32(i8* %this) readnone {<br>
+ ret i32 1<br>
+}<br>
+<br>
+define i32 @vf2i32(i8* %this) readnone {<br>
+ ret i32 2<br>
+}<br>
+<br>
+define i32 @vf3i32(i8* %this) readnone {<br>
+ ret i32 3<br>
+}<br>
+<br>
+define i32 @vf4i32(i8* %this) readnone {<br>
+ ret i32 4<br>
+}<br>
+<br>
+; CHECK: define i1 @call1(<br>
+define i1 @call1(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[VTGEP1:%[^ ]*]] = getelementptr i8, i8* [[VT1]], i64 -5<br>
+ ; CHECK: [[VTLOAD1:%[^ ]*]] = load i8, i8* [[VTGEP1]]<br>
+ ; CHECK: [[VTAND1:%[^ ]*]] = and i8 [[VTLOAD1]], 2<br>
+ ; CHECK: [[VTCMP1:%[^ ]*]] = icmp ne i8 [[VTAND1]], 0<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i1 [[VTCMP1]]<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i1 @call2(<br>
+define i1 @call2(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 1<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[VTGEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2]], i64 -5<br>
+ ; CHECK: [[VTLOAD2:%[^ ]*]] = load i8, i8* [[VTGEP2]]<br>
+ ; CHECK: [[VTAND2:%[^ ]*]] = and i8 [[VTLOAD2]], 1<br>
+ ; CHECK: [[VTCMP2:%[^ ]*]] = icmp ne i8 [[VTAND2]], 0<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i1 [[VTCMP2]]<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i32 @call3(<br>
+define i32 @call3(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 2<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*)*<br>
+ ; CHECK: [[VTGEP3:%[^ ]*]] = getelementptr i8, i8* [[VT3]], i64 -4<br>
+ ; CHECK: [[VTBC3:%[^ ]*]] = bitcast i8* [[VTGEP3]] to i32*<br>
+ ; CHECK: [[VTLOAD3:%[^ ]*]] = load i32, i32* [[VTBC3]]<br>
+ %result = call i32 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i32 [[VTLOAD3]]<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+declare void @__cxa_pure_virtual()<br>
+<br>
+!0 = !{!"bitset", [3 x i8*]* @vt1, i32 0}<br>
+!1 = !{!"bitset", [3 x i8*]* @vt2, i32 0}<br>
+!2 = !{!"bitset", [3 x i8*]* @vt3, i32 0}<br>
+!3 = !{!"bitset", [3 x i8*]* @vt4, i32 0}<br>
+!4 = !{!"bitset", [3 x i8*]* @vt5, i32 0}<br>
+!llvm.bitsets = !{!0, !1, !2, !3, !4}<br>
<br>
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll (added)<br>
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,131 @@<br>
+; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s<br>
+<br>
+target datalayout = "e-p:64:64"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: [[VT1DATA:@[^ ]*]] = private constant { [0 x i8], [4 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [4 x i8*] [i8* null, i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf1i32 to i8*)], [8 x i8] c"\01\00\00\00\01\00\00\00" }<br>
+@vt1 = constant [4 x i8*] [<br>
+i8* null,<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf1i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: [[VT2DATA:@[^ ]*]] = private constant { [0 x i8], [3 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf2i32 to i8*)], [8 x i8] c"\02\00\00\00\02\00\00\00" }<br>
+@vt2 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf2i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: [[VT3DATA:@[^ ]*]] = private constant { [0 x i8], [4 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [4 x i8*] [i8* null, i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf3i32 to i8*)], [8 x i8] c"\03\00\00\00\01\00\00\00" }<br>
+@vt3 = constant [4 x i8*] [<br>
+i8* null,<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf3i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: [[VT4DATA:@[^ ]*]] = private constant { [0 x i8], [3 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf4i32 to i8*)], [8 x i8] c"\04\00\00\00\02\00\00\00" }<br>
+@vt4 = constant [3 x i8*] [<br>
+i8* bitcast (i1 (i8*)* @vf1i1 to i8*),<br>
+i8* bitcast (i1 (i8*)* @vf0i1 to i8*),<br>
+i8* bitcast (i32 (i8*)* @vf4i32 to i8*)<br>
+]<br>
+<br>
+; CHECK: @vt1 = alias [4 x i8*], getelementptr inbounds ({ [0 x i8], [4 x i8*], [8 x i8] }, { [0 x i8], [4 x i8*], [8 x i8] }* [[VT1DATA]], i32 0, i32 1)<br>
+; CHECK: @vt2 = alias [3 x i8*], getelementptr inbounds ({ [0 x i8], [3 x i8*], [8 x i8] }, { [0 x i8], [3 x i8*], [8 x i8] }* [[VT2DATA]], i32 0, i32 1)<br>
+; CHECK: @vt3 = alias [4 x i8*], getelementptr inbounds ({ [0 x i8], [4 x i8*], [8 x i8] }, { [0 x i8], [4 x i8*], [8 x i8] }* [[VT3DATA]], i32 0, i32 1)<br>
+; CHECK: @vt4 = alias [3 x i8*], getelementptr inbounds ({ [0 x i8], [3 x i8*], [8 x i8] }, { [0 x i8], [3 x i8*], [8 x i8] }* [[VT4DATA]], i32 0, i32 1)<br>
+<br>
+define i1 @vf0i1(i8* %this) readnone {<br>
+ ret i1 0<br>
+}<br>
+<br>
+define i1 @vf1i1(i8* %this) readnone {<br>
+ ret i1 1<br>
+}<br>
+<br>
+define i32 @vf1i32(i8* %this) readnone {<br>
+ ret i32 1<br>
+}<br>
+<br>
+define i32 @vf2i32(i8* %this) readnone {<br>
+ ret i32 2<br>
+}<br>
+<br>
+define i32 @vf3i32(i8* %this) readnone {<br>
+ ret i32 3<br>
+}<br>
+<br>
+define i32 @vf4i32(i8* %this) readnone {<br>
+ ret i32 4<br>
+}<br>
+<br>
+; CHECK: define i1 @call1(<br>
+define i1 @call1(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[VTGEP1:%[^ ]*]] = getelementptr i8, i8* [[VT1]], i64 28<br>
+ ; CHECK: [[VTLOAD1:%[^ ]*]] = load i8, i8* [[VTGEP1]]<br>
+ ; CHECK: [[VTAND1:%[^ ]*]] = and i8 [[VTLOAD1]], 2<br>
+ ; CHECK: [[VTCMP1:%[^ ]*]] = icmp ne i8 [[VTAND1]], 0<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i1 [[VTCMP1]]<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i1 @call2(<br>
+define i1 @call2(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 1<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*)*<br>
+ ; CHECK: [[VTGEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2]], i64 28<br>
+ ; CHECK: [[VTLOAD2:%[^ ]*]] = load i8, i8* [[VTGEP2]]<br>
+ ; CHECK: [[VTAND2:%[^ ]*]] = and i8 [[VTLOAD2]], 1<br>
+ ; CHECK: [[VTCMP2:%[^ ]*]] = icmp ne i8 [[VTAND2]], 0<br>
+ %result = call i1 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i1 [[VTCMP2]]<br>
+ ret i1 %result<br>
+}<br>
+<br>
+; CHECK: define i32 @call3(<br>
+define i32 @call3(i8* %obj) {<br>
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**<br>
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr<br>
+ ; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*<br>
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*<br>
+ %p = call i1 @llvm.bitset.test(i8* %vtablei8, metadata !"bitset")<br>
+ call void @llvm.assume(i1 %p)<br>
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 2<br>
+ %fptr = load i8*, i8** %fptrptr<br>
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*)*<br>
+ ; CHECK: [[VTGEP3:%[^ ]*]] = getelementptr i8, i8* [[VT3]], i64 24<br>
+ ; CHECK: [[VTBC3:%[^ ]*]] = bitcast i8* [[VTGEP3]] to i32*<br>
+ ; CHECK: [[VTLOAD3:%[^ ]*]] = load i32, i32* [[VTBC3]]<br>
+ %result = call i32 %fptr_casted(i8* %obj)<br>
+ ; CHECK: ret i32 [[VTLOAD3]]<br>
+ ret i32 %result<br>
+}<br>
+<br>
+declare i1 @llvm.bitset.test(i8*, metadata)<br>
+declare void @llvm.assume(i1)<br>
+<br>
+!0 = !{!"bitset", [4 x i8*]* @vt1, i32 8}<br>
+!1 = !{!"bitset", [3 x i8*]* @vt2, i32 0}<br>
+!2 = !{!"bitset", [4 x i8*]* @vt3, i32 8}<br>
+!3 = !{!"bitset", [3 x i8*]* @vt4, i32 0}<br>
+!llvm.bitsets = !{!0, !1, !2, !3}<br>
<br>
Modified: llvm/trunk/test/tools/gold/X86/disable-verify.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/disable-verify.ll?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/disable-verify.ll?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/gold/X86/disable-verify.ll (original)<br>
+++ llvm/trunk/test/tools/gold/X86/disable-verify.ll Tue Feb 9 16:50:34 2016<br>
@@ -14,7 +14,7 @@ target triple = "x86_64-unknown-linux-gn<br>
<br>
; -disable-verify should disable output verification from the optimization<br>
; pipeline.<br>
-; CHECK: Pass Arguments: {{.*}} -verify -forceattrs<br>
+; CHECK: Pass Arguments: {{.*}} -verify -<br>
; CHECK-NOT: -verify<br>
<br>
; VERIFY: Pass Arguments: {{.*}} -verify {{.*}} -verify<br>
<br>
Modified: llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt?rev=260312&r1=260311&r2=260312&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt?rev=260312&r1=260311&r2=260312&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt (original)<br>
+++ llvm/trunk/unittests/Transforms/IPO/CMakeLists.txt Tue Feb 9 16:50:34 2016<br>
@@ -6,4 +6,5 @@ set(LLVM_LINK_COMPONENTS<br>
<br>
add_llvm_unittest(IPOTests<br>
LowerBitSets.cpp<br>
+ WholeProgramDevirt.cpp<br>
)<br>
<br>
Added: llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp?rev=260312&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp?rev=260312&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp (added)<br>
+++ llvm/trunk/unittests/Transforms/IPO/WholeProgramDevirt.cpp Tue Feb 9 16:50:34 2016<br>
@@ -0,0 +1,164 @@<br>
+//===- WholeProgramDevirt.cpp - Unit tests for whole-program devirt -------===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/Transforms/IPO/WholeProgramDevirt.h"<br>
+#include "gtest/gtest.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace wholeprogramdevirt;<br>
+<br>
+TEST(WholeProgramDevirt, findLowestOffset) {<br>
+ VTableBits VT1;<br>
+ VT1.ObjectSize = 8;<br>
+ VT1.Before.BytesUsed = {1 << 0};<br>
+ VT1.After.BytesUsed = {1 << 1};<br>
+<br>
+ VTableBits VT2;<br>
+ VT2.ObjectSize = 8;<br>
+ VT2.Before.BytesUsed = {1 << 1};<br>
+ VT2.After.BytesUsed = {1 << 0};<br>
+<br>
+ BitSetInfo BS1{&VT1, 0};<br>
+ BitSetInfo BS2{&VT2, 0};<br>
+ VirtualCallTarget Targets[] = {<br>
+ {&BS1, /*IsBigEndian=*/false},<br>
+ {&BS2, /*IsBigEndian=*/false},<br>
+ };<br>
+<br>
+ EXPECT_EQ(2ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));<br>
+ EXPECT_EQ(66ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));<br>
+<br>
+ EXPECT_EQ(8ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));<br>
+ EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));<br>
+<br>
+ BS1.Offset = 4;<br>
+ EXPECT_EQ(33ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));<br>
+ EXPECT_EQ(65ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));<br>
+<br>
+ EXPECT_EQ(40ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));<br>
+ EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));<br>
+<br>
+ BS1.Offset = 8;<br>
+ BS2.Offset = 8;<br>
+ EXPECT_EQ(66ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));<br>
+ EXPECT_EQ(2ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));<br>
+<br>
+ EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));<br>
+ EXPECT_EQ(8ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));<br>
+<br>
+ VT1.After.BytesUsed = {0xff, 0, 0, 0, 0xff};<br>
+ VT2.After.BytesUsed = {0xff, 1, 0, 0, 0};<br>
+ EXPECT_EQ(16ull, findLowestOffset(Targets, /*IsAfter=*/true, 16));<br>
+ EXPECT_EQ(40ull, findLowestOffset(Targets, /*IsAfter=*/true, 32));<br>
+}<br>
+<br>
+TEST(WholeProgramDevirt, setReturnValues) {<br>
+ VTableBits VT1;<br>
+ VT1.ObjectSize = 8;<br>
+<br>
+ VTableBits VT2;<br>
+ VT2.ObjectSize = 8;<br>
+<br>
+ BitSetInfo BS1{&VT1, 0};<br>
+ BitSetInfo BS2{&VT2, 0};<br>
+ VirtualCallTarget Targets[] = {<br>
+ {&BS1, /*IsBigEndian=*/false},<br>
+ {&BS2, /*IsBigEndian=*/false},<br>
+ };<br>
+<br>
+ BS1.Offset = 4;<br>
+ BS2.Offset = 4;<br>
+<br>
+ int64_t OffsetByte;<br>
+ uint64_t OffsetBit;<br>
+<br>
+ Targets[0].RetVal = 1;<br>
+ Targets[1].RetVal = 0;<br>
+ setBeforeReturnValues(Targets, 32, 1, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(-5ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.BytesUsed);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0}, VT2.Before.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT2.Before.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 0;<br>
+ Targets[1].RetVal = 1;<br>
+ setBeforeReturnValues(Targets, 39, 1, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(-5ll, OffsetByte);<br>
+ EXPECT_EQ(7ull, OffsetBit);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x81}, VT1.Before.BytesUsed);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x80}, VT2.Before.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x81}, VT2.Before.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 12;<br>
+ Targets[1].RetVal = 34;<br>
+ setBeforeReturnValues(Targets, 40, 8, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(-6ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ((std::vector<uint8_t>{1, 12}), VT1.Before.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT1.Before.BytesUsed);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x80, 34}), VT2.Before.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT2.Before.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 56;<br>
+ Targets[1].RetVal = 78;<br>
+ setBeforeReturnValues(Targets, 48, 16, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(-8ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ((std::vector<uint8_t>{1, 12, 0, 56}), VT1.Before.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),<br>
+ VT1.Before.BytesUsed);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x80, 34, 0, 78}), VT2.Before.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),<br>
+ VT2.Before.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 1;<br>
+ Targets[1].RetVal = 0;<br>
+ setAfterReturnValues(Targets, 32, 1, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(4ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.BytesUsed);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0}, VT2.After.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT2.After.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 0;<br>
+ Targets[1].RetVal = 1;<br>
+ setAfterReturnValues(Targets, 39, 1, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(4ll, OffsetByte);<br>
+ EXPECT_EQ(7ull, OffsetBit);<br>
+ EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x81}, VT1.After.BytesUsed);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x80}, VT2.After.Bytes);<br>
+ EXPECT_EQ(std::vector<uint8_t>{0x81}, VT2.After.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 12;<br>
+ Targets[1].RetVal = 34;<br>
+ setAfterReturnValues(Targets, 40, 8, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(5ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ((std::vector<uint8_t>{1, 12}), VT1.After.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT1.After.BytesUsed);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x80, 34}), VT2.After.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT2.After.BytesUsed);<br>
+<br>
+ Targets[0].RetVal = 56;<br>
+ Targets[1].RetVal = 78;<br>
+ setAfterReturnValues(Targets, 48, 16, OffsetByte, OffsetBit);<br>
+ EXPECT_EQ(6ll, OffsetByte);<br>
+ EXPECT_EQ(0ull, OffsetBit);<br>
+ EXPECT_EQ((std::vector<uint8_t>{1, 12, 56, 0}), VT1.After.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),<br>
+ VT1.After.BytesUsed);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x80, 34, 78, 0}), VT2.After.Bytes);<br>
+ EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),<br>
+ VT2.After.BytesUsed);<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>