llvm.org GIT mirror llvm / 5501985
[InstSimplify] Teach InstSimplify how to simplify extractvalue git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242007 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
5 changed file(s) with 68 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
7171 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
7272 ArrayRef Idxs);
7373
74 /// \brief Attempt to constant fold an extractvalue instruction with the
75 /// specified operands and indices. The constant result is returned if
76 /// successful; if not, null is returned.
77 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
78 ArrayRef Idxs);
79
7480 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
7581 /// produce if it is constant and determinable. If this is not determinable,
7682 /// return null.
243243 AssumptionCache *AC = nullptr,
244244 const Instruction *CxtI = nullptr);
245245
246 /// \brief Given operands for an ExtractValueInst, see if we can fold the
247 /// result. If not, this returns null.
248 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef Idxs,
249 const DataLayout &DL,
250 const TargetLibraryInfo *TLI = nullptr,
251 const DominatorTree *DT = nullptr,
252 AssumptionCache *AC = nullptr,
253 const Instruction *CxtI = nullptr);
254
246255 /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
247256 /// the result. If not, this returns null.
248257 Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout &DL,
35193519 RecursionLimit);
35203520 }
35213521
3522 /// SimplifyExtractValueInst - Given operands for an ExtractValueInst, see if we
3523 /// can fold the result. If not, this returns null.
3524 static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef Idxs,
3525 const Query &, unsigned) {
3526 if (auto *CAgg = dyn_cast(Agg))
3527 return ConstantFoldExtractValueInstruction(CAgg, Idxs);
3528
3529 // extractvalue x, (insertvalue y, elt, n), n -> elt
3530 unsigned NumIdxs = Idxs.size();
3531 for (auto *IVI = dyn_cast(Agg); IVI != nullptr;
3532 IVI = dyn_cast(IVI->getAggregateOperand())) {
3533 ArrayRef InsertValueIdxs = IVI->getIndices();
3534 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
3535 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
3536 if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
3537 Idxs.slice(0, NumCommonIdxs)) {
3538 if (NumIdxs == NumInsertValueIdxs)
3539 return IVI->getInsertedValueOperand();
3540 break;
3541 }
3542 }
3543
3544 return nullptr;
3545 }
3546
3547 Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef Idxs,
3548 const DataLayout &DL,
3549 const TargetLibraryInfo *TLI,
3550 const DominatorTree *DT,
3551 AssumptionCache *AC,
3552 const Instruction *CxtI) {
3553 return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI),
3554 RecursionLimit);
3555 }
3556
35223557 /// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
35233558 static Value *SimplifyPHINode(PHINode *PN, const Query &Q) {
35243559 // If all of the PHI's incoming values are the same then replace the PHI node
39283963 IV->getIndices(), DL, TLI, DT, AC, I);
39293964 break;
39303965 }
3966 case Instruction::ExtractValue: {
3967 auto *EVI = cast(I);
3968 Result = SimplifyExtractValueInst(EVI->getAggregateOperand(),
3969 EVI->getIndices(), DL, TLI, DT, AC, I);
3970 break;
3971 }
39313972 case Instruction::PHI:
39323973 Result = SimplifyPHINode(cast(I), Query(DL, TLI, DT, AC, I));
39333974 break;
21732173 if (!EV.hasIndices())
21742174 return ReplaceInstUsesWith(EV, Agg);
21752175
2176 if (Constant *C = dyn_cast(Agg)) {
2177 if (Constant *C2 = C->getAggregateElement(*EV.idx_begin())) {
2178 if (EV.getNumIndices() == 0)
2179 return ReplaceInstUsesWith(EV, C2);
2180 // Extract the remaining indices out of the constant indexed by the
2181 // first index
2182 return ExtractValueInst::Create(C2, EV.getIndices().slice(1));
2183 }
2184 return nullptr; // Can't handle other constants
2185 }
2176 if (Value *V =
2177 SimplifyExtractValueInst(Agg, EV.getIndices(), DL, TLI, DT, AC))
2178 return ReplaceInstUsesWith(EV, V);
21862179
21872180 if (InsertValueInst *IV = dyn_cast(Agg)) {
21882181 // We're extracting from an insertvalue instruction, compare the indices
2626 ret { i8, i32 } %ins
2727 ; CHECK-LABEL: @test2(
2828 }
29
30 define i32 @test3(i32 %a, float %b) {
31 %agg1 = insertvalue {i32, float} undef, i32 %a, 0
32 %agg2 = insertvalue {i32, float} %agg1, float %b, 1
33 %ev = extractvalue {i32, float} %agg2, 0
34 ret i32 %ev
35 ; CHECK-LABEL: @test3(
36 ; CHECK: ret i32 %a
37 }