llvm-project/clang/lib/CodeGen
Reid Kleckner 399d96e39c [MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:

  struct Incomplete;
  struct A {
    virtual A* foo(Incomplete p) = 0;
  };
  struct B : virtual A {
    void foo(Incomplete p) override;
  };
  struct C : B { int c; };

This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.

Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.

This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.

These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.

This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.

Fixes PR25641

Reviewers: rjmccall, rsmith, hans

Subscribers: Prazek, cfe-commits

Differential Revision: https://reviews.llvm.org/D45112

llvm-svn: 329009
2018-04-02 20:20:33 +00:00
..
ABIInfo.h Delete BuiltinCC. NFC. 2018-03-20 22:02:57 +00:00
Address.h
BackendUtil.cpp [Coroutines] Schedule coro-split before asan 2018-04-01 23:55:21 +00:00
CGAtomic.cpp Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGBlocks.cpp [ObjC] Allow declaring __weak pointer fields in C structs in ARC. 2018-03-19 17:38:40 +00:00
CGBlocks.h [CodeGen][ObjC] Block captures should inherit the type of the captured 2016-09-16 00:02:06 +00:00
CGBuilder.h Change memcpy/memove/memset to have dest and source alignment attributes. 2018-01-28 17:27:45 +00:00
CGBuiltin.cpp [coroutines] Add __builtin_coro_noop => llvm.coro.noop 2018-04-02 17:35:37 +00:00
CGCUDANV.cpp [CUDA] Include single GPU binary, NFCI. 2018-02-28 17:53:46 +00:00
CGCUDARuntime.cpp Refactor call emission to package the function pointer together with 2016-10-26 23:46:34 +00:00
CGCUDARuntime.h
CGCXX.cpp [MS] Always use base dtors in place of complete/vbase dtors when possible 2018-03-16 19:40:50 +00:00
CGCXXABI.cpp [MS] Always use base dtors in place of complete/vbase dtors when possible 2018-03-16 19:40:50 +00:00
CGCXXABI.h [MS] Always use base dtors in place of complete/vbase dtors when possible 2018-03-16 19:40:50 +00:00
CGCall.cpp [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
CGCall.h Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGClass.cpp Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGCleanup.cpp Allocate and access NormalCleanupDest with the natural alignment of i32. 2018-01-12 22:07:01 +00:00
CGCleanup.h Use the correct ObjC EH personality 2017-01-08 22:58:07 +00:00
CGCoroutine.cpp Remove redundant casts. NFC 2018-03-01 05:43:23 +00:00
CGDebugInfo.cpp Fix some DenseMap use-after-rehash bugs and hoist MethodVFTableLocation 2018-04-02 20:00:39 +00:00
CGDebugInfo.h [DebugInfo] Support DWARF v5 source code embedding extension 2018-02-26 17:32:31 +00:00
CGDecl.cpp [CUDA] Let device-side shared variables be initialized with undef 2018-04-02 17:38:24 +00:00
CGDeclCXX.cpp [OPENMP] Codegen for ctor|dtor of declare target variables. 2018-03-28 14:28:54 +00:00
CGException.cpp [MS] Don't escape MS C++ names with \01 2018-03-16 20:36:49 +00:00
CGExpr.cpp [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CGExprAgg.cpp [CodeGen] Ignore OpaqueValueExprs that are unique references to their 2018-03-20 01:47:58 +00:00
CGExprCXX.cpp [Builtins] Overload __builtin_operator_new/delete to allow forwarding to usual allocation/deallocation functions. 2018-03-21 19:19:48 +00:00
CGExprComplex.cpp Delete BuiltinCC. NFC. 2018-03-20 22:02:57 +00:00
CGExprConstant.cpp [CodeGen] Use the zero initializer instead of storing an all zero representation. 2018-02-09 22:10:09 +00:00
CGExprScalar.cpp [CodeGen] Ignore OpaqueValueExprs that are unique references to their 2018-03-20 01:47:58 +00:00
CGGPUBuiltin.cpp Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGLoopInfo.cpp [CodeGen] Pass objects that are expensive to copy by const ref. 2016-11-24 16:01:20 +00:00
CGLoopInfo.h [CodeGen] Pass objects that are expensive to copy by const ref. 2016-11-24 16:01:20 +00:00
CGNonTrivialStruct.cpp [ObjC] Allow declaring __weak pointer fields in C structs in ARC. 2018-03-19 17:38:40 +00:00
CGObjC.cpp [CodeGen] Add funclet token to ARC marker 2018-03-19 19:34:39 +00:00
CGObjCGNU.cpp Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGObjCMac.cpp Recommit r326946 after reducing CallArgList memory footprint 2018-03-15 15:25:19 +00:00
CGObjCRuntime.cpp [CodeGen] Propagate may-alias'ness of lvalues with TBAA info 2017-10-31 11:05:34 +00:00
CGObjCRuntime.h Clean up CGObjCMac's APIs for deriving class references. NFC. 2016-11-30 23:54:50 +00:00
CGOpenCLRuntime.cpp [OpenCL] Remove block invoke function from emitted block literal struct 2018-03-07 19:32:58 +00:00
CGOpenCLRuntime.h [OpenCL] Remove block invoke function from emitted block literal struct 2018-03-07 19:32:58 +00:00
CGOpenMPRuntime.cpp [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CGOpenMPRuntime.h [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CGOpenMPRuntimeNVPTX.cpp [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CGOpenMPRuntimeNVPTX.h [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CGRecordLayout.h
CGRecordLayoutBuilder.cpp Add helper to determine if a field is a zero-length bitfield. 2018-04-02 18:29:43 +00:00
CGStmt.cpp [AMDGPU] Fix codegen for inline assembly 2018-03-23 19:43:42 +00:00
CGStmtOpenMP.cpp [OPENMP, NVPTX] Globalization of the private redeclarations. 2018-03-20 14:45:59 +00:00
CGVTT.cpp Recommit r324107 again. 2018-02-07 22:15:33 +00:00
CGVTables.cpp [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
CGVTables.h [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
CGValue.h CodeGen: Reduce LValue and CallArgList memory footprint before recommitting r326946 2018-03-14 15:02:28 +00:00
CMakeLists.txt [ObjC] Allow declaring __strong pointer fields in structs in Objective-C 2018-02-28 07:15:55 +00:00
CodeGenABITypes.cpp Include getting generated struct offsets in CodegenABITypes 2017-10-10 23:54:21 +00:00
CodeGenAction.cpp Add possibility to specify output stream for CompilerInstance 2018-03-02 12:11:40 +00:00
CodeGenFunction.cpp [libFuzzer] Use OptForFuzzing attribute with -fsanitize=fuzzer. 2018-03-23 23:35:28 +00:00
CodeGenFunction.h [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
CodeGenModule.cpp [OPENMP] Added emission of offloading data sections for declare target 2018-03-30 18:31:07 +00:00
CodeGenModule.h [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
CodeGenPGO.cpp Mark all library options as hidden. 2017-12-01 00:53:10 +00:00
CodeGenPGO.h Remove a dead field. NFC. 2017-04-24 20:54:36 +00:00
CodeGenTBAA.cpp [CodeGen] Fix generation of TBAA tags for may-alias accesses 2018-02-20 12:33:04 +00:00
CodeGenTBAA.h [CodeGen] Fix generation of TBAA tags for may-alias accesses 2018-02-20 12:33:04 +00:00
CodeGenTypeCache.h Delete BuiltinCC. NFC. 2018-03-20 22:02:57 +00:00
CodeGenTypes.cpp Remove redundant casts. NFC 2018-03-01 05:43:23 +00:00
CodeGenTypes.h [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
ConstantEmitter.h Convert clang::LangAS to a strongly typed enum 2017-10-15 18:48:14 +00:00
ConstantInitBuilder.cpp Further fixes and improvements to the ConstantInitBuilder API. 2017-03-06 19:04:16 +00:00
CoverageMappingGen.cpp [Coverage] Handle break/continue outside of loop bodies 2018-02-16 07:59:43 +00:00
CoverageMappingGen.h [Lexer] Report more precise skipped regions (PR34166) 2017-09-11 20:47:42 +00:00
EHScopeStack.h Spelling mistakes in comments. NFCI. (PR27635) 2017-03-30 14:13:19 +00:00
ItaniumCXXABI.cpp [ObjC++] Make parameter passing and function return compatible with ObjC 2018-03-28 21:13:14 +00:00
MacroPPCallbacks.cpp [NFC] Refactor the Preprocessor function that handles Macro definitions and rename Arguments to Parameters in Macro Definitions. 2017-07-17 17:18:43 +00:00
MacroPPCallbacks.h Fix API breaks 2017-04-26 20:58:21 +00:00
MicrosoftCXXABI.cpp [MS] Emit vftable thunks for functions with incomplete prototypes 2018-04-02 20:20:33 +00:00
ModuleBuilder.cpp D34444: Teach codegen to work in incremental processing mode. 2017-08-27 10:58:03 +00:00
ObjectFilePCHContainerOperations.cpp -gmodules: Emit debug info for implicit module imports via #include. 2018-01-03 19:10:21 +00:00
README.txt
SanitizerMetadata.cpp Hardware-assisted AddressSanitizer (clang part). 2017-12-09 01:32:07 +00:00
SanitizerMetadata.h
SwiftCallingConv.cpp Fix a major swiftcall ABI bug with trivial C++ class types. 2018-04-01 21:04:30 +00:00
TargetInfo.cpp Add helper to determine if a field is a zero-length bitfield. 2018-04-02 18:29:43 +00:00
TargetInfo.h Set calling convention for CUDA kernel 2018-03-29 15:02:08 +00:00
VarBypassDetector.cpp [CodeGen] Don't emit lifetime intrinsics for some local variables 2016-10-26 05:42:30 +00:00
VarBypassDetector.h [CodeGen] Don't emit lifetime intrinsics for some local variables 2016-10-26 05:42:30 +00:00

README.txt

IRgen optimization opportunities.

//===---------------------------------------------------------------------===//

The common pattern of
--
short x; // or char, etc
(x == 10)
--
generates an zext/sext of x which can easily be avoided.

//===---------------------------------------------------------------------===//

Bitfields accesses can be shifted to simplify masking and sign
extension. For example, if the bitfield width is 8 and it is
appropriately aligned then is is a lot shorter to just load the char
directly.

//===---------------------------------------------------------------------===//

It may be worth avoiding creation of alloca's for formal arguments
for the common situation where the argument is never written to or has
its address taken. The idea would be to begin generating code by using
the argument directly and if its address is taken or it is stored to
then generate the alloca and patch up the existing code.

In theory, the same optimization could be a win for block local
variables as long as the declaration dominates all statements in the
block.

NOTE: The main case we care about this for is for -O0 -g compile time
performance, and in that scenario we will need to emit the alloca
anyway currently to emit proper debug info. So this is blocked by
being able to emit debug information which refers to an LLVM
temporary, not an alloca.

//===---------------------------------------------------------------------===//

We should try and avoid generating basic blocks which only contain
jumps. At -O0, this penalizes us all the way from IRgen (malloc &
instruction overhead), all the way down through code generation and
assembly time.

On 176.gcc:expr.ll, it looks like over 12% of basic blocks are just
direct branches!

//===---------------------------------------------------------------------===//