[PATCH] D26196: Add support for non-zero null pointers

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 8 14:01:28 PST 2016


yaxunl marked an inline comment as done.
yaxunl added inline comments.


================
Comment at: lib/CodeGen/CGExprConstant.cpp:1340
+          return C;
+        return getNullPtr(PT, DestType);
       }
----------------
efriedma wrote:
> yaxunl wrote:
> > yaxunl wrote:
> > > rjmccall wrote:
> > > > efriedma wrote:
> > > > > Consider code like the following:
> > > > > 
> > > > >     int x = 0;
> > > > >     auto y1 = (__specialaddrspace int*)0;
> > > > >     auto y2 = (__specialaddrspace int*)((void)0, 0);
> > > > >     auto y3 = (__specialaddrspace int*)x;
> > > > > 
> > > > > How do you expect these three cases to behave?  (The first case involves a C null pointer constant, the second and third cases are different ways of writing a general int->ptr conversion.)
> > > > Yeah, I think you probably need to fix APValue to be unambiguous about whether the value is a formal null pointer (CK_NullToPointer) or just a cast of an integer (CK_IntegralToPointer).  It looks like PointerExprEvaluator will generate the exact same value for both.
> > > It seems the current implementation generates the correct IR.
> > > 
> > > I tried the following sample and I saw correct IR generated.
> > > 
> > > 
> > > ```
> > > private int* test_cast_0_to_ptr(void) {
> > >   return (private int*)0;
> > > }
> > > 
> > > private int* test_cast_int_to_ptr1(void) {
> > >   return (private int*)((void)0, 0);
> > > }
> > > 
> > > private int* test_cast_int_to_ptr2(void) {
> > >   int x = 0;
> > >   return (private int*)x;
> > > }
> > > 
> > > ```
> > > 
> > > The dumped AST is
> > > 
> > > 
> > > ```
> > > |-FunctionDecl 0x95fdc88 <ptr.cl:3:1, line:5:1> line:3:14 test_cast_0_to_ptr 'int *(void)'
> > > | `-CompoundStmt 0x95fdde8 <col:39, line:5:1>
> > > |   `-ReturnStmt 0x95fddd0 <line:4:3, col:24>
> > > |     `-CStyleCastExpr 0x95fdda8 <col:10, col:24> 'int *' <NullToPointer>
> > > |       `-IntegerLiteral 0x95fdd70 <col:24> 'int' 0
> > > |-FunctionDecl 0x95fdea0 <line:13:1, line:15:1> line:13:14 test_cast_int_to_ptr1 'int *(void)'
> > > | `-CompoundStmt 0x95fe098 <col:42, line:15:1>
> > > |   `-ReturnStmt 0x95fe080 <line:14:3, col:35>
> > > |     `-CStyleCastExpr 0x95fe058 <col:10, col:35> 'int *' <IntegralToPointer>
> > > |       `-ParenExpr 0x95fe038 <col:24, col:35> 'int'
> > > |         `-BinaryOperator 0x95fe010 <col:25, col:34> 'int' ','
> > > |           |-CStyleCastExpr 0x95fdf78 <col:25, col:31> 'void' <ToVoid>
> > > |           | `-IntegerLiteral 0x95fdf48 <col:31> 'int' 0
> > > |           `-IntegerLiteral 0x95fdfa0 <col:34> 'int' 0
> > > `-FunctionDecl 0x95fe150 <line:19:1, line:22:1> line:19:14 test_cast_int_to_ptr2 'int *(void)'
> > >   `-CompoundStmt 0x9620130 <col:42, line:22:1>
> > >     |-DeclStmt 0x9620080 <line:20:3, col:12>
> > >     | `-VarDecl 0x95fe210 <col:3, col:11> col:7 used x 'int' cinit
> > >     |   `-IntegerLiteral 0x9620060 <col:11> 'int' 0
> > >     `-ReturnStmt 0x9620118 <line:21:3, col:24>
> > >       `-CStyleCastExpr 0x96200f0 <col:10, col:24> 'int *' <IntegralToPointer>
> > > ```
> > > 
> > > Since only CK_NullToPointer is translated to null pointer through getNullPtr, CK_IntegralToPointer will result in either zero-valued pointer or inttoptr, the generated IR is correct.
> > Basically in the second and third case the destination type is not pointer, so they do not need to be emitted as null pointer. If a literal 0 is casted to a pointer type, then it should be emitted as a null pointer.
> What happens in the following case?
> 
>     static private int* x = (private int*)((void)0, 0);
You are right. This needs to be fixed.


https://reviews.llvm.org/D26196





More information about the cfe-commits mailing list