llvm.org GIT mirror llvm / e37fa0b
Reapply: [InstSimplify] Add support for bitcasts" This reverts commit r276700 and reapplies r276698. The relevant clang tests have been updated. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@276727 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 3 years ago
5 changed file(s) with 65 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
244244 AssumptionCache *AC = nullptr,
245245 const Instruction *CxtI = nullptr);
246246
247 /// Given operands for an BitCastInst, fold the result or return null.
248 Value *SimplifyBitCastInst(Value *Op, Type *Ty, const DataLayout &DL,
249 const TargetLibraryInfo *TLI = nullptr,
250 const DominatorTree *DT = nullptr,
251 AssumptionCache *AC = nullptr,
252 const Instruction *CxtI = nullptr);
253
247254 //=== Helper functions for higher up the class hierarchy.
248255
249256
6969 static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned);
7070 static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned);
7171 static Value *SimplifyTruncInst(Value *, Type *, const Query &, unsigned);
72 static Value *SimplifyBitCastInst(Value *, Type *, const Query &, unsigned);
7273
7374 /// For a boolean type, or a vector of boolean type, return false, or
7475 /// a vector with every element false, as appropriate for the type.
38093810 RecursionLimit);
38103811 }
38113812
3813 static Value *SimplifyBitCastInst(Value *Op, Type *Ty, const Query &Q, unsigned) {
3814 if (auto *C = dyn_cast(Op))
3815 return ConstantFoldCastOperand(Instruction::BitCast, C, Ty, Q.DL);
3816
3817 // bitcast x -> x
3818 if (Op->getType() == Ty)
3819 return Op;
3820
3821 // bitcast(bitcast x) -> x
3822 if (auto *BC = dyn_cast(Op))
3823 if (BC->getOperand(0)->getType() == Ty)
3824 return BC->getOperand(0);
3825
3826 return nullptr;
3827 }
3828
3829 Value *llvm::SimplifyBitCastInst(Value *Op, Type *Ty, const DataLayout &DL,
3830 const TargetLibraryInfo *TLI,
3831 const DominatorTree *DT, AssumptionCache *AC,
3832 const Instruction *CxtI) {
3833 return ::SimplifyBitCastInst(Op, Ty, Query(DL, TLI, DT, AC, CxtI),
3834 RecursionLimit);
3835 }
3836
38123837 //=== Helper functions for higher up the class hierarchy.
38133838
38143839 /// Given operands for a BinaryOperator, see if we can fold the result.
42794304 Result =
42804305 SimplifyTruncInst(I->getOperand(0), I->getType(), DL, TLI, DT, AC, I);
42814306 break;
4307 case Instruction::BitCast:
4308 Result =
4309 SimplifyBitCastInst(I->getOperand(0), I->getType(), DL, TLI, DT, AC, I);
4310 break;
42824311 }
42834312
42844313 // In general, it is possible for computeKnownBits to determine all bits in a
3939 ; Check that the first @llvm.aarch64.neon.st2 is optimized away by Early CSE.
4040 ; CHECK-LABEL: @test_cse2
4141 ; CHECK-NOT: call void @llvm.aarch64.neon.st2.v4i32.p0i8(<4 x i32> %3, <4 x i32> %3, i8* %0)
42 ; CHECK: call void @llvm.aarch64.neon.st2.v4i32.p0i8(<4 x i32> %3, <4 x i32> %4, i8* %0)
42 ; 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)
4343 %s.coerce.fca.0.extract = extractvalue [2 x <4 x i32>] %s.coerce, 0
4444 %s.coerce.fca.1.extract = extractvalue [2 x <4 x i32>] %s.coerce, 1
4545 br label %for.cond
1717 ; CHECK: %v4 = bitcast <2 x i32>* %v1 to <2 x i8*>*
1818 ; CHECK: store <2 x i8*> %v3, <2 x i8*>* %v4
1919 ; CHECK: %1 = ptrtoint <2 x i8*> %v3 to <2 x i32>
20 ; CHECK: %2 = bitcast <2 x i32> %1 to i64
21 ; CHECK: %3 = bitcast i64 %2 to <2 x i32>
22 ; CHECK: ret <2 x i32> %3
20 ; CHECK: ret <2 x i32> %1
2321 }
2422
2523 declare void @anything(<2 x i32>*)
0 ; RUN: opt -S -instsimplify < %s | FileCheck %s
1
2 define i1 @test1(i1 %V) {
3 entry:
4 %Z = zext i1 %V to i32
5 %T = trunc i32 %Z to i1
6 ret i1 %T
7 ; CHECK-LABEL: define i1 @test1(
8 ; CHECK: ret i1 %V
9 }
10
11 define i8* @test2(i8* %V) {
12 entry:
13 %BC1 = bitcast i8* %V to i32*
14 %BC2 = bitcast i32* %BC1 to i8*
15 ret i8* %BC2
16 ; CHECK-LABEL: define i8* @test2(
17 ; CHECK: ret i8* %V
18 }
19
20 define i8* @test3(i8* %V) {
21 entry:
22 %BC = bitcast i8* %V to i8*
23 ret i8* %BC
24 ; CHECK-LABEL: define i8* @test3(
25 ; CHECK: ret i8* %V
26 }