[llvm] r276727 - Reapply: [InstSimplify] Add support for bitcasts"

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 25 22:52:29 PDT 2016


Author: majnemer
Date: Tue Jul 26 00:52:29 2016
New Revision: 276727

URL: http://llvm.org/viewvc/llvm-project?rev=276727&view=rev
Log:
Reapply: [InstSimplify] Add support for bitcasts"

This reverts commit r276700 and reapplies r276698.
The relevant clang tests have been updated.

Added:
    llvm/trunk/test/Transforms/InstSimplify/cast.ll
Modified:
    llvm/trunk/include/llvm/Analysis/InstructionSimplify.h
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/test/Transforms/EarlyCSE/AArch64/intrinsics.ll
    llvm/trunk/test/Transforms/GVN/pr14166.ll

Modified: llvm/trunk/include/llvm/Analysis/InstructionSimplify.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/InstructionSimplify.h?rev=276727&r1=276726&r2=276727&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/InstructionSimplify.h (original)
+++ llvm/trunk/include/llvm/Analysis/InstructionSimplify.h Tue Jul 26 00:52:29 2016
@@ -245,6 +245,13 @@ namespace llvm {
                            AssumptionCache *AC = nullptr,
                            const Instruction *CxtI = nullptr);
 
+  /// Given operands for an BitCastInst, fold the result or return null.
+  Value *SimplifyBitCastInst(Value *Op, Type *Ty, const DataLayout &DL,
+                             const TargetLibraryInfo *TLI = nullptr,
+                             const DominatorTree *DT = nullptr,
+                             AssumptionCache *AC = nullptr,
+                             const Instruction *CxtI = nullptr);
+
   //=== Helper functions for higher up the class hierarchy.
 
 

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=276727&r1=276726&r2=276727&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Tue Jul 26 00:52:29 2016
@@ -70,6 +70,7 @@ static Value *SimplifyCmpInst(unsigned,
 static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned);
 static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned);
 static Value *SimplifyTruncInst(Value *, Type *, const Query &, unsigned);
+static Value *SimplifyBitCastInst(Value *, Type *, const Query &, unsigned);
 
 /// For a boolean type, or a vector of boolean type, return false, or
 /// a vector with every element false, as appropriate for the type.
@@ -3810,6 +3811,30 @@ Value *llvm::SimplifyTruncInst(Value *Op
                              RecursionLimit);
 }
 
+static Value *SimplifyBitCastInst(Value *Op, Type *Ty, const Query &Q, unsigned) {
+  if (auto *C = dyn_cast<Constant>(Op))
+    return ConstantFoldCastOperand(Instruction::BitCast, C, Ty, Q.DL);
+
+  // bitcast x -> x
+  if (Op->getType() == Ty)
+    return Op;
+
+  // bitcast(bitcast x) -> x
+  if (auto *BC = dyn_cast<BitCastInst>(Op))
+    if (BC->getOperand(0)->getType() == Ty)
+      return BC->getOperand(0);
+
+  return nullptr;
+}
+
+Value *llvm::SimplifyBitCastInst(Value *Op, Type *Ty, const DataLayout &DL,
+                               const TargetLibraryInfo *TLI,
+                               const DominatorTree *DT, AssumptionCache *AC,
+                               const Instruction *CxtI) {
+  return ::SimplifyBitCastInst(Op, Ty, Query(DL, TLI, DT, AC, CxtI),
+                               RecursionLimit);
+}
+
 //=== Helper functions for higher up the class hierarchy.
 
 /// Given operands for a BinaryOperator, see if we can fold the result.
@@ -4280,6 +4305,10 @@ Value *llvm::SimplifyInstruction(Instruc
     Result =
         SimplifyTruncInst(I->getOperand(0), I->getType(), DL, TLI, DT, AC, I);
     break;
+  case Instruction::BitCast:
+    Result =
+        SimplifyBitCastInst(I->getOperand(0), I->getType(), DL, TLI, DT, AC, I);
+    break;
   }
 
   // In general, it is possible for computeKnownBits to determine all bits in a

Modified: llvm/trunk/test/Transforms/EarlyCSE/AArch64/intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/EarlyCSE/AArch64/intrinsics.ll?rev=276727&r1=276726&r2=276727&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/EarlyCSE/AArch64/intrinsics.ll (original)
+++ llvm/trunk/test/Transforms/EarlyCSE/AArch64/intrinsics.ll Tue Jul 26 00:52:29 2016
@@ -40,7 +40,7 @@ entry:
 ; Check that the first @llvm.aarch64.neon.st2 is optimized away by Early CSE.
 ; CHECK-LABEL: @test_cse2
 ; CHECK-NOT: call void @llvm.aarch64.neon.st2.v4i32.p0i8(<4 x i32> %3, <4 x i32> %3, i8* %0)
-; CHECK: call void @llvm.aarch64.neon.st2.v4i32.p0i8(<4 x i32> %3, <4 x i32> %4, i8* %0)
+; CHECK: call void @llvm.aarch64.neon.st2.v4i32.p0i8(<4 x i32> %s.coerce.fca.0.extract, <4 x i32> %s.coerce.fca.1.extract, i8* %0)
   %s.coerce.fca.0.extract = extractvalue [2 x <4 x i32>] %s.coerce, 0
   %s.coerce.fca.1.extract = extractvalue [2 x <4 x i32>] %s.coerce, 1
   br label %for.cond

Modified: llvm/trunk/test/Transforms/GVN/pr14166.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/pr14166.ll?rev=276727&r1=276726&r2=276727&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/GVN/pr14166.ll (original)
+++ llvm/trunk/test/Transforms/GVN/pr14166.ll Tue Jul 26 00:52:29 2016
@@ -18,9 +18,7 @@ define <2 x i32> @test1() {
 ; CHECK: %v4 = bitcast <2 x i32>* %v1 to <2 x i8*>*
 ; CHECK: store <2 x i8*> %v3, <2 x i8*>* %v4
 ; CHECK: %1 = ptrtoint <2 x i8*> %v3 to <2 x i32>
-; CHECK: %2 = bitcast <2 x i32> %1 to i64
-; CHECK: %3 = bitcast i64 %2 to <2 x i32>
-; CHECK: ret <2 x i32> %3
+; CHECK: ret <2 x i32> %1
 }
 
 declare void @anything(<2 x i32>*)

Added: llvm/trunk/test/Transforms/InstSimplify/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/cast.ll?rev=276727&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/cast.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/cast.ll Tue Jul 26 00:52:29 2016
@@ -0,0 +1,27 @@
+; RUN: opt -S -instsimplify < %s | FileCheck %s
+
+define i1 @test1(i1 %V) {
+entry:
+  %Z = zext i1 %V to i32
+  %T = trunc i32 %Z to i1
+  ret i1 %T
+; CHECK-LABEL: define i1 @test1(
+; CHECK: ret i1 %V
+}
+
+define i8* @test2(i8* %V) {
+entry:
+  %BC1 = bitcast i8* %V to i32*
+  %BC2 = bitcast i32* %BC1 to i8*
+  ret i8* %BC2
+; CHECK-LABEL: define i8* @test2(
+; CHECK: ret i8* %V
+}
+
+define i8* @test3(i8* %V) {
+entry:
+  %BC = bitcast i8* %V to i8*
+  ret i8* %BC
+; CHECK-LABEL: define i8* @test3(
+; CHECK: ret i8* %V
+}




More information about the llvm-commits mailing list