llvm.org GIT mirror llvm / 8be5c4f
Merging r341094: ------------------------------------------------------------------------ r341094 | efriedma | 2018-08-30 20:59:24 +0200 (Thu, 30 Aug 2018) | 11 lines [SROA] Fix alignment for uses of PHI nodes. Splitting an alloca can decrease the alignment of GEPs into the partition. Normally, rewriting accounts for this, but the code was missing for uses of PHI nodes and select instructions. Fixes https://bugs.llvm.org/show_bug.cgi?id=38707 . Differential Revision: https://reviews.llvm.org/D51335 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_70@341220 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 2 years ago
2 changed file(s) with 74 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
30453045 return true;
30463046 }
30473047
3048 void fixLoadStoreAlign(Instruction &Root) {
3049 // This algorithm implements the same visitor loop as
3050 // hasUnsafePHIOrSelectUse, and fixes the alignment of each load
3051 // or store found.
3052 SmallPtrSet Visited;
3053 SmallVector Uses;
3054 Visited.insert(&Root);
3055 Uses.push_back(&Root);
3056 do {
3057 Instruction *I = Uses.pop_back_val();
3058
3059 if (LoadInst *LI = dyn_cast(I)) {
3060 unsigned LoadAlign = LI->getAlignment();
3061 if (!LoadAlign)
3062 LoadAlign = DL.getABITypeAlignment(LI->getType());
3063 LI->setAlignment(std::min(LoadAlign, getSliceAlign()));
3064 continue;
3065 }
3066 if (StoreInst *SI = dyn_cast(I)) {
3067 unsigned StoreAlign = SI->getAlignment();
3068 if (!StoreAlign) {
3069 Value *Op = SI->getOperand(0);
3070 StoreAlign = DL.getABITypeAlignment(Op->getType());
3071 }
3072 SI->setAlignment(std::min(StoreAlign, getSliceAlign()));
3073 continue;
3074 }
3075
3076 assert(isa(I) || isa(I) ||
3077 isa(I) || isa(I));
3078 for (User *U : I->users())
3079 if (Visited.insert(cast(U)).second)
3080 Uses.push_back(cast(U));
3081 } while (!Uses.empty());
3082 }
3083
30483084 bool visitPHINode(PHINode &PN) {
30493085 LLVM_DEBUG(dbgs() << " original: " << PN << "\n");
30503086 assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable");
30683104 LLVM_DEBUG(dbgs() << " to: " << PN << "\n");
30693105 deleteIfTriviallyDead(OldPtr);
30703106
3107 // Fix the alignment of any loads or stores using this PHI node.
3108 fixLoadStoreAlign(PN);
3109
30713110 // PHIs can't be promoted on their own, but often can be speculated. We
30723111 // check the speculation outside of the rewriter so that we see the
30733112 // fully-rewritten alloca.
30913130
30923131 LLVM_DEBUG(dbgs() << " to: " << SI << "\n");
30933132 deleteIfTriviallyDead(OldPtr);
3133
3134 // Fix the alignment of any loads or stores using this select.
3135 fixLoadStoreAlign(SI);
30943136
30953137 // Selects can't be promoted on their own, but often can be speculated. We
30963138 // check the speculation outside of the rewriter so that we see the
599599 store %struct.S undef, %struct.S* %f1, align 4
600600 ret void
601601 }
602
603 define i32 @phi_align(i32* %z) {
604 ; CHECK-LABEL: @phi_align(
605 entry:
606 %a = alloca [8 x i8], align 8
607 ; CHECK: alloca [7 x i8]
608
609 %a0x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 1
610 %a0 = bitcast i8* %a0x to i32*
611 %a1x = getelementptr [8 x i8], [8 x i8]* %a, i64 0, i32 4
612 %a1 = bitcast i8* %a1x to i32*
613 ; CHECK: store i32 0, {{.*}}, align 1
614 store i32 0, i32* %a0, align 1
615 ; CHECK: store i32 1, {{.*}}, align 1
616 store i32 1, i32* %a1, align 4
617 ; CHECK: load {{.*}}, align 1
618 %v0 = load i32, i32* %a0, align 1
619 ; CHECK: load {{.*}}, align 1
620 %v1 = load i32, i32* %a1, align 4
621 %cond = icmp sle i32 %v0, %v1
622 br i1 %cond, label %then, label %exit
623
624 then:
625 br label %exit
626
627 exit:
628 ; CHECK: %phi = phi i32* [ {{.*}}, %then ], [ %z, %entry ]
629 ; CHECK-NEXT: %result = load i32, i32* %phi, align 1
630 %phi = phi i32* [ %a1, %then ], [ %z, %entry ]
631 %result = load i32, i32* %phi, align 4
632 ret i32 %result
633 }