@@ -3046,6 +3046,42 @@ class llvm::sroa::AllocaSliceRewriter
3046
3046
return true ;
3047
3047
}
3048
3048
3049
+ void fixLoadStoreAlign (Instruction &Root) {
3050
+ // This algorithm implements the same visitor loop as
3051
+ // hasUnsafePHIOrSelectUse, and fixes the alignment of each load
3052
+ // or store found.
3053
+ SmallPtrSet<Instruction *, 4 > Visited;
3054
+ SmallVector<Instruction *, 4 > Uses;
3055
+ Visited.insert (&Root);
3056
+ Uses.push_back (&Root);
3057
+ do {
3058
+ Instruction *I = Uses.pop_back_val ();
3059
+
3060
+ if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
3061
+ unsigned LoadAlign = LI->getAlignment ();
3062
+ if (!LoadAlign)
3063
+ LoadAlign = DL.getABITypeAlignment (LI->getType ());
3064
+ LI->setAlignment (std::min (LoadAlign, getSliceAlign ()));
3065
+ continue ;
3066
+ }
3067
+ if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
3068
+ unsigned StoreAlign = SI->getAlignment ();
3069
+ if (!StoreAlign) {
3070
+ Value *Op = SI->getOperand (0 );
3071
+ StoreAlign = DL.getABITypeAlignment (Op->getType ());
3072
+ }
3073
+ SI->setAlignment (std::min (StoreAlign, getSliceAlign ()));
3074
+ continue ;
3075
+ }
3076
+
3077
+ assert (isa<BitCastInst>(I) || isa<PHINode>(I) ||
3078
+ isa<SelectInst>(I) || isa<GetElementPtrInst>(I));
3079
+ for (User *U : I->users ())
3080
+ if (Visited.insert (cast<Instruction>(U)).second )
3081
+ Uses.push_back (cast<Instruction>(U));
3082
+ } while (!Uses.empty ());
3083
+ }
3084
+
3049
3085
bool visitPHINode (PHINode &PN) {
3050
3086
LLVM_DEBUG (dbgs () << " original: " << PN << " \n " );
3051
3087
assert (BeginOffset >= NewAllocaBeginOffset && " PHIs are unsplittable" );
@@ -3069,6 +3105,9 @@ class llvm::sroa::AllocaSliceRewriter
3069
3105
LLVM_DEBUG (dbgs () << " to: " << PN << " \n " );
3070
3106
deleteIfTriviallyDead (OldPtr);
3071
3107
3108
+ // Fix the alignment of any loads or stores using this PHI node.
3109
+ fixLoadStoreAlign (PN);
3110
+
3072
3111
// PHIs can't be promoted on their own, but often can be speculated. We
3073
3112
// check the speculation outside of the rewriter so that we see the
3074
3113
// fully-rewritten alloca.
@@ -3093,6 +3132,9 @@ class llvm::sroa::AllocaSliceRewriter
3093
3132
LLVM_DEBUG (dbgs () << " to: " << SI << " \n " );
3094
3133
deleteIfTriviallyDead (OldPtr);
3095
3134
3135
+ // Fix the alignment of any loads or stores using this select.
3136
+ fixLoadStoreAlign (SI);
3137
+
3096
3138
// Selects can't be promoted on their own, but often can be speculated. We
3097
3139
// check the speculation outside of the rewriter so that we see the
3098
3140
// fully-rewritten alloca.
0 commit comments