[llvm-commits] [llvm] r138722 - in /llvm/trunk: lib/Analysis/ConstantFolding.cpp lib/Transforms/InstCombine/InstCombineCasts.cpp test/Transforms/InstCombine/cast.ll

Nadav Rotem nadav.rotem at intel.com
Sun Aug 28 04:51:08 PDT 2011


Author: nadav
Date: Sun Aug 28 06:51:08 2011
New Revision: 138722

URL: http://llvm.org/viewvc/llvm-project?rev=138722&view=rev
Log:
Bitcasts are transitive. Bitcast-Bitcast-X becomes Bitcast-X.


Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/trunk/test/Transforms/InstCombine/cast.ll

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=138722&r1=138721&r2=138722&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Sun Aug 28 06:51:08 2011
@@ -51,6 +51,12 @@
   if (C->isAllOnesValue() && !DestTy->isX86_MMXTy())
     return Constant::getAllOnesValue(DestTy);
 
+  // Bitcast of Bitcast can be done using a single cast.
+  ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
+  if (CE && CE->getOpcode() == Instruction::BitCast) {
+    return ConstantExpr::getBitCast(CE->getOperand(0), DestTy);
+  }
+
   // The code below only handles casts to vectors currently.
   VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
   if (DestVTy == 0)

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=138722&r1=138721&r2=138722&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Sun Aug 28 06:51:08 2011
@@ -1659,6 +1659,11 @@
   if (DestTy == Src->getType())
     return ReplaceInstUsesWith(CI, Src);
 
+  // Bitcasts are transitive.
+  if (BitCastInst* BSrc = dyn_cast<BitCastInst>(Src)) {
+    return CastInst::Create(Instruction::BitCast, BSrc->getOperand(0), DestTy);
+  }
+
   if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
     PointerType *SrcPTy = cast<PointerType>(SrcTy);
     Type *DstElTy = DstPTy->getElementType();

Modified: llvm/trunk/test/Transforms/InstCombine/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=138722&r1=138721&r2=138722&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cast.ll Sun Aug 28 06:51:08 2011
@@ -630,3 +630,17 @@
 ; CHECK: uitofp
 }
 
+define <4 x float> @test64(<4 x float> %c) nounwind {
+  %t0 = bitcast <4 x float> %c to <4 x i32>
+  %t1 = bitcast <4 x i32> %t0 to <2 x double>
+  %t2 = bitcast <2 x double> %t1 to <4 x float>
+  ret <4 x float> %t2
+; CHECK: @test64
+; CHECK-NEXT: ret <4 x float> %c
+}
+
+define float @test2c() {
+  ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0)
+; CHECK: @test2c
+; CHECK-NOT: extractelement
+}





More information about the llvm-commits mailing list