From 79de3d7b3aae9c7cc1038a3223dc96dbdafbeb3f Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Mon, 21 Oct 2013 18:55:08 +0000 Subject: [PATCH] Teach SimplifyCFG about address spaces git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193104 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyCFG.cpp | 14 +-- test/Transforms/SimplifyCFG/MagicPointer.ll | 94 +++++++++++++++++--- test/Transforms/SimplifyCFG/switch_create.ll | 21 ++++- 3 files changed, 113 insertions(+), 16 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 61c44fc4b2e..083328652fd 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -475,9 +475,13 @@ Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) { CV = ICI->getOperand(0); // Unwrap any lossless ptrtoint cast. - if (TD && CV && CV->getType() == TD->getIntPtrType(CV->getContext())) - if (PtrToIntInst *PTII = dyn_cast(CV)) - CV = PTII->getOperand(0); + if (TD && CV) { + if (PtrToIntInst *PTII = dyn_cast(CV)) { + Value *Ptr = PTII->getPointerOperand(); + if (PTII->getType() == TD->getIntPtrType(Ptr->getType())) + CV = Ptr; + } + } return CV; } @@ -925,7 +929,7 @@ bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI, // Convert pointer to int before we switch. if (CV->getType()->isPointerTy()) { assert(TD && "Cannot switch on pointer without DataLayout"); - CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getContext()), + CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getType()), "magicptr"); } @@ -2788,7 +2792,7 @@ static bool SimplifyBranchOnICmpChain(BranchInst *BI, const DataLayout *TD, if (CompVal->getType()->isPointerTy()) { assert(TD && "Cannot switch on pointer without DataLayout"); CompVal = Builder.CreatePtrToInt(CompVal, - TD->getIntPtrType(CompVal->getContext()), + TD->getIntPtrType(CompVal->getType()), "magicptr"); } diff --git a/test/Transforms/SimplifyCFG/MagicPointer.ll b/test/Transforms/SimplifyCFG/MagicPointer.ll index 93b9a276eac..b8b8cbd9dc6 100644 --- a/test/Transforms/SimplifyCFG/MagicPointer.ll +++ b/test/Transforms/SimplifyCFG/MagicPointer.ll @@ -2,6 +2,24 @@ ; ; RUN: opt < %s -simplifycfg -S | FileCheck %s +target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" +target triple = "x86_64-apple-darwin10.0.0" + +@.str = private constant [5 x i8] c"null\00" ; <[5 x i8]*> [#uses=2] +@.str1 = private constant [4 x i8] c"one\00" ; <[4 x i8]*> [#uses=2] +@.str2 = private constant [4 x i8] c"two\00" ; <[4 x i8]*> [#uses=2] +@.str3 = private constant [5 x i8] c"four\00" ; <[5 x i8]*> [#uses=2] + +@.str_as1 = private addrspace(1) constant [5 x i8] c"null\00" ; <[5 x i8]*> [#uses=2] +@.str1_as1 = private addrspace(1) constant [4 x i8] c"one\00" ; <[4 x i8]*> [#uses=2] +@.str2_as1 = private addrspace(1) constant [4 x i8] c"two\00" ; <[4 x i8]*> [#uses=2] +@.str3_as1 = private addrspace(1) constant [5 x i8] c"four\00" ; <[5 x i8]*> [#uses=2] + +declare i32 @puts(i8*) +declare i32 @puts_as1(i8 addrspace(1)*) + +define void @f(i8* %x) nounwind ssp { +; CHECK-LABEL: @f( ; CHECK: switch i64 %magicptr ; CHECK: i64 0, label ; CHECK: i64 1, label @@ -10,15 +28,6 @@ ; CHECK: i64 4, label ; CHECK: } -target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" -target triple = "x86_64-apple-darwin10.0.0" - -@.str = private constant [5 x i8] c"null\00" ; <[5 x i8]*> [#uses=2] -@.str1 = private constant [4 x i8] c"one\00" ; <[4 x i8]*> [#uses=2] -@.str2 = private constant [4 x i8] c"two\00" ; <[4 x i8]*> [#uses=2] -@.str3 = private constant [5 x i8] c"four\00" ; <[5 x i8]*> [#uses=2] - -define void @f(i8* %x) nounwind ssp { entry: %tobool = icmp eq i8* %x, null ; [#uses=1] br i1 %tobool, label %if.then, label %if.else @@ -72,4 +81,69 @@ if.end21: ; preds = %if.end20, %if.then ret void } -declare i32 @puts(i8*) +; Is it useful to test a version where the ptrtoints are to the same +; size? +define void @f_as1(i8 addrspace(1)* %x) nounwind ssp { +; CHECK-LABEL: @f_as1( +; CHECK: ptrtoint i8 addrspace(1)* %x to i16 +; CHECK: switch i16 %magicptr +; CHECK: i16 0, label +; CHECK: i16 1, label +; CHECK: i16 2, label +; CHECK: i16 3, label +; CHECK: i16 4, label +; CHECK: } + +entry: + %tobool = icmp eq i8 addrspace(1)* %x, null ; [#uses=1] + br i1 %tobool, label %if.then, label %if.else + +if.then: ; preds = %entry + %call = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([5 x i8] addrspace(1)* @.str_as1, i64 0, i64 0)) nounwind ; [#uses=0] + br label %if.end21 + +if.else: ; preds = %entry + %cmp = icmp eq i8 addrspace(1)* %x, inttoptr (i64 1 to i8 addrspace(1)*) ; [#uses=1] + br i1 %cmp, label %if.then2, label %if.else4 + +if.then2: ; preds = %if.else + %call3 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([4 x i8] addrspace(1)* @.str1_as1, i64 0, i64 0)) nounwind ; [#uses=0] + br label %if.end20 + +if.else4: ; preds = %if.else + %cmp6 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 2 to i8 addrspace(1)*) ; [#uses=1] + br i1 %cmp6, label %if.then9, label %lor.lhs.false + +lor.lhs.false: ; preds = %if.else4 + %cmp8 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 3 to i8 addrspace(1)*) ; [#uses=1] + br i1 %cmp8, label %if.then9, label %if.else11 + +if.then9: ; preds = %lor.lhs.false, %if.else4 + %call10 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([4 x i8] addrspace(1)* @.str2_as1, i64 0, i64 0)) nounwind ; [#uses=0] + br label %if.end19 + +if.else11: ; preds = %lor.lhs.false + %cmp13 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 4 to i8 addrspace(1)*) ; [#uses=1] + br i1 %cmp13, label %if.then14, label %if.else16 + +if.then14: ; preds = %if.else11 + %call15 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([5 x i8] addrspace(1)* @.str3_as1, i64 0, i64 0)) nounwind ; [#uses=0] + br label %if.end + +if.else16: ; preds = %if.else11 + %call18 = call i32 @puts_as1(i8 addrspace(1)* %x) nounwind ; [#uses=0] + br label %if.end + +if.end: ; preds = %if.else16, %if.then14 + br label %if.end19 + +if.end19: ; preds = %if.end, %if.then9 + br label %if.end20 + +if.end20: ; preds = %if.end19, %if.then2 + br label %if.end21 + +if.end21: ; preds = %if.end20, %if.then + ret void +} + diff --git a/test/Transforms/SimplifyCFG/switch_create.ll b/test/Transforms/SimplifyCFG/switch_create.ll index fe20f17eed4..caacf7e40f5 100644 --- a/test/Transforms/SimplifyCFG/switch_create.ll +++ b/test/Transforms/SimplifyCFG/switch_create.ll @@ -1,5 +1,5 @@ ; RUN: opt -S -simplifycfg < %s | FileCheck -check-prefix=CHECK %s -; RUN: opt -S -default-data-layout="p:32:32" -simplifycfg < %s | FileCheck -check-prefix=DL %s +; RUN: opt -S -default-data-layout="p:32:32-p1:16:16" -simplifycfg < %s | FileCheck -check-prefix=DL %s ; TODO: Other tests should also have check lines with datalayout @@ -44,6 +44,25 @@ F: ; preds = %0 ; DL: ] } +define void @test1_ptr_as1(i32 addrspace(1)* %V) { + %C1 = icmp eq i32 addrspace(1)* %V, inttoptr (i32 4 to i32 addrspace(1)*) + %C2 = icmp eq i32 addrspace(1)* %V, inttoptr (i32 17 to i32 addrspace(1)*) + %CN = or i1 %C1, %C2 ; [#uses=1] + br i1 %CN, label %T, label %F +T: ; preds = %0 + call void @foo1( ) + ret void +F: ; preds = %0 + call void @foo2( ) + ret void +; DL-LABEL: @test1_ptr_as1( +; DL: %magicptr = ptrtoint i32 addrspace(1)* %V to i16 +; DL: switch i16 %magicptr, label %F [ +; DL: i16 17, label %T +; DL: i16 4, label %T +; DL: ] +} + define void @test2(i32 %V) { %C1 = icmp ne i32 %V, 4 ; [#uses=1] %C2 = icmp ne i32 %V, 17 ; [#uses=1]