[cfe-commits] r157310 - in /cfe/trunk: lib/AST/ExprConstant.cpp lib/CodeGen/CGBuiltin.cpp test/CodeGen/object-size.c
Richard Smith
richard-llvm at metafoo.co.uk
Tue May 22 21:13:20 PDT 2012
Author: rsmith
Date: Tue May 22 23:13:20 2012
New Revision: 157310
URL: http://llvm.org/viewvc/llvm-project?rev=157310&view=rev
Log:
If the first argument of __builtin_object_size can be folded to a constant
pointer, but such folding encounters side-effects, ignore the side-effects
rather than performing them at runtime: CodeGen generates wrong code for
__builtin_object_size in that case.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/lib/CodeGen/CGBuiltin.cpp
cfe/trunk/test/CodeGen/object-size.c
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=157310&r1=157309&r2=157310&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue May 22 23:13:20 2012
@@ -4319,10 +4319,16 @@
}
bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
- // TODO: Perhaps we should let LLVM lower this?
LValue Base;
- if (!EvaluatePointer(E->getArg(0), Base, Info))
- return false;
+
+ {
+ // The operand of __builtin_object_size is never evaluated for side-effects.
+ // If there are any, but we can determine the pointed-to object anyway, then
+ // ignore the side-effects.
+ SpeculativeEvaluationRAII SpeculativeEval(Info);
+ if (!EvaluatePointer(E->getArg(0), Base, Info))
+ return false;
+ }
// If we can prove the base is null, lower to zero now.
if (!Base.getLValueBase()) return Success(0, E);
@@ -4355,13 +4361,16 @@
return true;
// If evaluating the argument has side-effects we can't determine
- // the size of the object and lower it to unknown now.
+ // the size of the object and lower it to unknown now. CodeGen relies on
+ // us to handle all cases where the expression has side-effects.
if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
return Success(-1ULL, E);
return Success(0, E);
}
+ // Expression had no side effects, but we couldn't statically determine the
+ // size of the referenced object.
return Error(E);
}
Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=157310&r1=157309&r2=157310&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Tue May 22 23:13:20 2012
@@ -335,6 +335,10 @@
return RValue::get(Builder.CreateCall(F, ArgValue));
}
case Builtin::BI__builtin_object_size: {
+ // We rely on constant folding to deal with expressions with side effects.
+ assert(!E->getArg(0)->HasSideEffects(getContext()) &&
+ "should have been constant folded");
+
// We pass this builtin onto the optimizer so that it can
// figure out the object size in more complex cases.
llvm::Type *ResType = ConvertType(E->getType());
Modified: cfe/trunk/test/CodeGen/object-size.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/object-size.c?rev=157310&r1=157309&r2=157310&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/object-size.c (original)
+++ cfe/trunk/test/CodeGen/object-size.c Tue May 22 23:13:20 2012
@@ -55,7 +55,10 @@
// CHECK: define void @test7
void test7() {
int i;
- // CHECK: = call i64 @llvm.objectsize.i64(i8* {{.*}}@gbuf{{.*}}, i1 false)
+ // Ensure we only evaluate the side-effect once.
+ // CHECK: = add
+ // CHECK-NOT: = add
+ // CHECK: = call i8* @__strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 63)
strcpy((++i, gbuf), "Hi there");
}
More information about the cfe-commits
mailing list