Author: void Date: Tue Dec 18 14:54:03 2018 New Revision: 349561 URL: http://llvm.org/viewvc/llvm-project?rev=349561&view=rev Log: Emit ASM input in a constant context
Summary: Some ASM input constraints (e.g., "i" and "n") require immediate values. At O0, very few code transformations are performed. So if we cannot resolve to an immediate when emitting the ASM input we shouldn't delay its processing. Reviewers: rsmith, efriedma Reviewed By: efriedma Subscribers: rehana, efriedma, craig.topper, jyknight, cfe-commits Differential Revision: https://reviews.llvm.org/D55616 Modified: cfe/trunk/lib/Basic/TargetInfo.cpp cfe/trunk/lib/CodeGen/CGStmt.cpp cfe/trunk/lib/Sema/SemaStmtAsm.cpp cfe/trunk/test/CodeGen/builtin-constant-p.c Modified: cfe/trunk/lib/Basic/TargetInfo.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/TargetInfo.cpp?rev=349561&r1=349560&r2=349561&view=diff ============================================================================== --- cfe/trunk/lib/Basic/TargetInfo.cpp (original) +++ cfe/trunk/lib/Basic/TargetInfo.cpp Tue Dec 18 14:54:03 2018 @@ -685,7 +685,9 @@ bool TargetInfo::validateInputConstraint // FIXME: Fail if % is used with the last operand. break; case 'i': // immediate integer. + break; case 'n': // immediate integer with a known value. + Info.setRequiresImmediate(); break; case 'I': // Various constant constraints with target-specific meanings. case 'J': Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=349561&r1=349560&r2=349561&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGStmt.cpp (original) +++ cfe/trunk/lib/CodeGen/CGStmt.cpp Tue Dec 18 14:54:03 2018 @@ -1820,11 +1820,14 @@ llvm::Value* CodeGenFunction::EmitAsmInp // If this can't be a register or memory, i.e., has to be a constant // (immediate or symbolic), try to emit it as such. if (!Info.allowsRegister() && !Info.allowsMemory()) { + if (Info.requiresImmediateConstant()) { + llvm::APSInt AsmConst = InputExpr->EvaluateKnownConstInt(getContext()); + return llvm::ConstantInt::get(getLLVMContext(), AsmConst); + } + Expr::EvalResult Result; if (InputExpr->EvaluateAsInt(Result, getContext())) return llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt()); - assert(!Info.requiresImmediateConstant() && - "Required-immediate inlineasm arg isn't constant?"); } if (Info.allowsRegister() || !Info.allowsMemory()) Modified: cfe/trunk/lib/Sema/SemaStmtAsm.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmtAsm.cpp?rev=349561&r1=349560&r2=349561&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaStmtAsm.cpp (original) +++ cfe/trunk/lib/Sema/SemaStmtAsm.cpp Tue Dec 18 14:54:03 2018 @@ -378,17 +378,17 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceL << InputExpr->getSourceRange()); } else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) { if (!InputExpr->isValueDependent()) { - Expr::EvalResult EVResult; - if (!InputExpr->EvaluateAsInt(EVResult, Context)) + llvm::SmallVector<PartialDiagnosticAt, 1> Diags; + llvm::APSInt Result = InputExpr->EvaluateKnownConstInt(Context, &Diags); + if (!Diags.empty()) return StmtError( Diag(InputExpr->getBeginLoc(), diag::err_asm_immediate_expected) << Info.getConstraintStr() << InputExpr->getSourceRange()); - llvm::APSInt Result = EVResult.Val.getInt(); - if (!Info.isValidAsmImmediate(Result)) - return StmtError(Diag(InputExpr->getBeginLoc(), - diag::err_invalid_asm_value_for_constraint) - << Result.toString(10) << Info.getConstraintStr() - << InputExpr->getSourceRange()); + if (!Info.isValidAsmImmediate(Result)) + return StmtError(Diag(InputExpr->getBeginLoc(), + diag::err_invalid_asm_value_for_constraint) + << Result.toString(10) << Info.getConstraintStr() + << InputExpr->getSourceRange()); } } else { Modified: cfe/trunk/test/CodeGen/builtin-constant-p.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtin-constant-p.c?rev=349561&r1=349560&r2=349561&view=diff ============================================================================== --- cfe/trunk/test/CodeGen/builtin-constant-p.c (original) +++ cfe/trunk/test/CodeGen/builtin-constant-p.c Tue Dec 18 14:54:03 2018 @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O2 | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O0 | FileCheck --check-prefix=O0 %s int a = 42; @@ -166,3 +167,13 @@ struct { const char *t; int a; } test15[ extern char test16_v; struct { int a; } test16 = { __builtin_constant_p(test16_v) }; + +extern unsigned long long test17_v; + +void test17() { + // O0: define void @test17 + // O0: call void asm sideeffect "", {{.*}}(i32 -1) + // CHECK: define void @test17 + // CHECK: call void asm sideeffect "", {{.*}}(i32 -1) + __asm__ __volatile__("" :: "n"( (__builtin_constant_p(test17_v) || 0) ? 1 : -1)); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits