]> granicus.if.org Git - clang/commitdiff
[CodeGen] Use pointer-sized integers for ptrtoint sources
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 17 Jun 2016 17:47:24 +0000 (17:47 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 17 Jun 2016 17:47:24 +0000 (17:47 +0000)
Given something like:
void *v = (void *)100;

We need to synthesize a ptrtoint operation from 100.  During constant
emission, we choose i64 as the type for our constant because it
guaranteed not to drop any bits from our CharUnits representation of the
value.  However, this is suboptimal for 32-bit targets: LLVM passes like
GlobalOpt will get confused by these sorts of casts resulting in
pessimization.

Instead, make sure the ptrtoint operand has a pointer-sized integer
type.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@273020 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprConstant.cpp
test/CodeGen/const-init.c

index d64c5876ee2d6a280c6ad4f79b1f3ae4d0face02..8ad1a4d29cd5c74f9c411d1f1ad12b0a49409968 100644 (file)
@@ -1314,8 +1314,14 @@ llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value,
 
       // Convert to the appropriate type; this could be an lvalue for
       // an integer.
-      if (isa<llvm::PointerType>(DestTy))
+      if (isa<llvm::PointerType>(DestTy)) {
+        // Convert the integer to a pointer-sized integer before converting it
+        // to a pointer.
+        C = llvm::ConstantExpr::getIntegerCast(
+            C, getDataLayout().getIntPtrType(DestTy),
+            /*isSigned=*/false);
         return llvm::ConstantExpr::getIntToPtr(C, DestTy);
+      }
 
       // If the types don't match this should only be a truncate.
       if (C->getType() != DestTy)
index 9434f1d1b24b3a1b8b74b29d330155e8ceef5fae..3fd231b630ee7d1286770544c79c4599b674bcef 100644 (file)
@@ -84,7 +84,7 @@ struct g13_s0 g13[] = {
    { (long) &g12_tmp }
 };
 
-// CHECK: @g14 = global i8* inttoptr (i64 100 to i8*)
+// CHECK: @g14 = global i8* inttoptr (i32 100 to i8*)
 void *g14 = (void*) 100;
 
 // CHECK: @g15 = global i32 -1