diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 3d51e9ede95..a7c99849058 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -2996,9 +2996,13 @@ void SelectionDAGBuilder::visitBitCast(const User &I) { if (DestVT != N.getValueType()) setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(), DestVT, N)); // convert types. - else if(ConstantSDNode *C = dyn_cast(N)) - setValue(&I, DAG.getConstant(C->getAPIntValue(), C->getValueType(0), - /*isTarget=*/false, /*isOpaque*/true)); + // Check if the original LLVM IR Operand was a ConstantInt, because getValue() + // might fold any kind of constant expression to an integer constant and that + // is not what we are looking for. Only regcognize a bitcast of a genuine + // constant integer as an opaque constant. + else if(ConstantInt *C = dyn_cast(I.getOperand(0))) + setValue(&I, DAG.getConstant(C->getValue(), DestVT, /*isTarget=*/false, + /*isOpaque*/true)); else setValue(&I, N); // noop cast. } diff --git a/test/CodeGen/X86/opaque-constant-asm.ll b/test/CodeGen/X86/opaque-constant-asm.ll new file mode 100644 index 00000000000..ab3d4e8b168 --- /dev/null +++ b/test/CodeGen/X86/opaque-constant-asm.ll @@ -0,0 +1,13 @@ +; RUN: llc < %s -mtriple=x86_64-apple-darwin | FileCheck %s +; This tests makes sure that we not mistake the bitcast inside the asm statement +; as an opaque constant. If we do, then the compilation will simply fail. + +%struct2 = type <{ i32, i32, i32, i32 }> +%union.anon = type { [2 x i64], [4 x i32] } +%struct1 = type { i32, %union.anon } + +define void @test() { +; CHECK: #ASM $16 + call void asm sideeffect "#ASM $0", "n"(i32 ptrtoint (i32* getelementptr inbounds (%struct2* bitcast (%union.anon* getelementptr inbounds (%struct1* null, i32 0, i32 1) to %struct2*), i32 0, i32 2) to i32)) + ret void +}