Index: clang/include/clang/Basic/CodeGenOptions.def =================================================================== --- clang/include/clang/Basic/CodeGenOptions.def +++ clang/include/clang/Basic/CodeGenOptions.def @@ -469,6 +469,9 @@ ENUM_CODEGENOPT(ZeroCallUsedRegs, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind, 5, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip) +/// Whether to use opaque pointers. +CODEGENOPT(OpaquePointers, 1, 0) + #undef CODEGENOPT #undef ENUM_CODEGENOPT #undef VALUE_CODEGENOPT Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -5504,6 +5504,13 @@ PosFlag, NegFlag, BothFlags<[], " analyzing function argument and return types for mandatory definedness">>; +defm opaque_pointers : BoolOption<"", + "opaque-pointers", + CodeGenOpts<"OpaquePointers">, + DefaultFalse, + PosFlag, + NegFlag, + BothFlags<[], " opaque pointers">>; def discard_value_names : Flag<["-"], "discard-value-names">, HelpText<"Discard value names in LLVM IR">, MarshallingInfoFlag>; Index: clang/lib/CodeGen/CodeGenAction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenAction.cpp +++ clang/lib/CodeGen/CodeGenAction.cpp @@ -983,6 +983,9 @@ if (BA != Backend_EmitNothing && !OS) return nullptr; + if (CI.getCodeGenOpts().OpaquePointers) + VMContext->enableOpaquePointers(); + // Load bitcode modules to link with, if we need to. if (LinkModules.empty()) for (const CodeGenOptions::BitcodeFileToLink &F : Index: clang/test/CodeGen/opaque-pointers-flag.c =================================================================== --- /dev/null +++ clang/test/CodeGen/opaque-pointers-flag.c @@ -0,0 +1,27 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py +// RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefix=TYPED +// RUN: %clang_cc1 -opaque-pointers -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefix=OPAQUE +// The current default is typed pointers: +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefix=TYPED + +// TYPED-LABEL: @test( +// TYPED-NEXT: entry: +// TYPED-NEXT: [[P_ADDR:%.*]] = alloca i32*, align 8 +// TYPED-NEXT: store i32* [[P:%.*]], i32** [[P_ADDR]], align 8 +// TYPED-NEXT: [[TMP0:%.*]] = load i32*, i32** [[P_ADDR]], align 8 +// TYPED-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], i64 1 +// TYPED-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX]], align 4 +// TYPED-NEXT: ret i32 [[TMP1]] +// +// OPAQUE-LABEL: @test( +// OPAQUE-NEXT: entry: +// OPAQUE-NEXT: [[P_ADDR:%.*]] = alloca ptr, align 8 +// OPAQUE-NEXT: store ptr [[P:%.*]], ptr [[P_ADDR]], align 8 +// OPAQUE-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8 +// OPAQUE-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[TMP0]], i64 1 +// OPAQUE-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX]], align 4 +// OPAQUE-NEXT: ret i32 [[TMP1]] +// +int test(int *p) { + return p[1]; +}