99// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010//
1111// ===----------------------------------------------------------------------===//
12- //
13- // Simplify aggregate instructions into scalar instructions.
14- //
12+ // /
13+ // / \file
14+ // /
15+ // / Simplify aggregate instructions into scalar instructions using simple
16+ // / peephole transformations.
17+ // /
1518// ===----------------------------------------------------------------------===//
1619
1720#define DEBUG_TYPE " sil-lower-aggregate-instrs"
2629#include " swift/SILOptimizer/Utils/InstOptUtils.h"
2730#include " llvm/ADT/Statistic.h"
2831#include " llvm/Support/Debug.h"
32+
2933using namespace swift ;
3034using namespace swift ::Lowering;
3135
@@ -70,154 +74,156 @@ STATISTIC(NumExpand, "Number of instructions expanded");
7074// / // no retain of %new!
7175// / // no load/release of %old!
7276// / store %new to %1 : $*T
73- static bool expandCopyAddr (CopyAddrInst *CA ) {
74- SILModule &M = CA-> getModule ();
75- SILFunction *F = CA-> getFunction ();
76- SILValue Source = CA ->getSrc ();
77+ static bool expandCopyAddr (CopyAddrInst *cai ) {
78+ SILFunction *fn = cai-> getFunction ();
79+ SILModule & module = cai-> getModule ();
80+ SILValue source = cai ->getSrc ();
7781
7882 // If we have an address only type don't do anything.
79- SILType SrcType = Source ->getType ();
80- if (SrcType .isAddressOnly (*F ))
83+ SILType srcType = source ->getType ();
84+ if (srcType .isAddressOnly (*fn ))
8185 return false ;
8286
83- bool expand = shouldExpand (M, SrcType .getObjectType ());
87+ bool expand = shouldExpand (module , srcType .getObjectType ());
8488 using TypeExpansionKind = Lowering::TypeLowering::TypeExpansionKind;
8589 auto expansionKind = expand ? TypeExpansionKind::MostDerivedDescendents
8690 : TypeExpansionKind::None;
8791
88- SILBuilderWithScope Builder (CA );
92+ SILBuilderWithScope builder (cai );
8993
9094 // %new = load %0 : $*T
91- LoadInst *New = Builder .createLoad (CA ->getLoc (), Source ,
92- LoadOwnershipQualifier::Unqualified);
95+ LoadInst *newValue = builder .createLoad (cai ->getLoc (), source ,
96+ LoadOwnershipQualifier::Unqualified);
9397
94- SILValue Destination = CA ->getDest ();
98+ SILValue destAddr = cai ->getDest ();
9599
96100 // If our object type is not trivial, we may need to release the old value and
97101 // retain the new one.
98102
99- auto &TL = F ->getTypeLowering (SrcType );
103+ auto &typeLowering = fn ->getTypeLowering (srcType );
100104
101105 // If we have a non-trivial type...
102- if (!TL.isTrivial ()) {
103-
106+ if (!typeLowering.isTrivial ()) {
104107 // If we are not initializing:
105108 // %old = load %1 : $*T
106- IsInitialization_t IsInit = CA ->isInitializationOfDest ();
107- LoadInst *Old = nullptr ;
108- if (IsInitialization_t::IsNotInitialization == IsInit ) {
109- Old = Builder .createLoad (CA ->getLoc (), Destination ,
110- LoadOwnershipQualifier::Unqualified);
109+ auto isInit = cai ->isInitializationOfDest ();
110+ LoadInst *oldValue = nullptr ;
111+ if (IsInitialization_t::IsNotInitialization == isInit ) {
112+ oldValue = builder .createLoad (cai ->getLoc (), destAddr ,
113+ LoadOwnershipQualifier::Unqualified);
111114 }
112115
113116 // If we are not taking and have a reference type:
114117 // strong_retain %new : $*T
115118 // or if we have a non-trivial non-reference type.
116119 // retain_value %new : $*T
117- IsTake_t IsTake = CA ->isTakeOfSrc ();
118- if (IsTake_t::IsNotTake == IsTake) {
119- TL. emitLoweredCopyValue (Builder, CA-> getLoc (), New, expansionKind);
120+ if ( IsTake_t::IsNotTake == cai ->isTakeOfSrc ()) {
121+ typeLowering. emitLoweredCopyValue (builder, cai-> getLoc (), newValue,
122+ expansionKind);
120123 }
121124
122125 // If we are not initializing:
123126 // strong_release %old : $*T
124127 // *or*
125128 // release_value %old : $*T
126- if (Old) {
127- TL.emitLoweredDestroyValue (Builder, CA->getLoc (), Old, expansionKind);
129+ if (oldValue) {
130+ typeLowering.emitLoweredDestroyValue (builder, cai->getLoc (), oldValue,
131+ expansionKind);
128132 }
129133 }
130134
131135 // Create the store.
132- Builder .createStore (CA ->getLoc (), New, Destination ,
136+ builder .createStore (cai ->getLoc (), newValue, destAddr ,
133137 StoreOwnershipQualifier::Unqualified);
134138
135139 ++NumExpand;
136140 return true ;
137141}
138142
139- static bool expandDestroyAddr (DestroyAddrInst *DA ) {
140- SILFunction *F = DA ->getFunction ();
141- SILModule &Module = DA ->getModule ();
142- SILBuilderWithScope Builder (DA );
143+ static bool expandDestroyAddr (DestroyAddrInst *dai ) {
144+ SILFunction *fn = dai ->getFunction ();
145+ SILModule &module = dai ->getModule ();
146+ SILBuilderWithScope builder (dai );
143147
144148 // Strength reduce destroy_addr inst into release/store if
145149 // we have a non-address only type.
146- SILValue Addr = DA ->getOperand ();
150+ SILValue addr = dai ->getOperand ();
147151
148152 // If we have an address only type, do nothing.
149- SILType Type = Addr ->getType ();
150- if (Type .isAddressOnly (*F ))
153+ SILType type = addr ->getType ();
154+ if (type .isAddressOnly (*fn ))
151155 return false ;
152156
153- bool expand = shouldExpand (Module, Type .getObjectType ());
157+ bool expand = shouldExpand (module , type .getObjectType ());
154158
155159 // If we have a non-trivial type...
156- if (!Type .isTrivial (*F )) {
160+ if (!type .isTrivial (*fn )) {
157161 // If we have a type with reference semantics, emit a load/strong release.
158- LoadInst *LI = Builder .createLoad (DA ->getLoc (), Addr ,
162+ LoadInst *li = builder .createLoad (dai ->getLoc (), addr ,
159163 LoadOwnershipQualifier::Unqualified);
160- auto &TL = F ->getTypeLowering (Type );
164+ auto &typeLowering = fn ->getTypeLowering (type );
161165 using TypeExpansionKind = Lowering::TypeLowering::TypeExpansionKind;
162166 auto expansionKind = expand ? TypeExpansionKind::MostDerivedDescendents
163167 : TypeExpansionKind::None;
164- TL.emitLoweredDestroyValue (Builder, DA->getLoc (), LI, expansionKind);
168+ typeLowering.emitLoweredDestroyValue (builder, dai->getLoc (), li,
169+ expansionKind);
165170 }
166171
167172 ++NumExpand;
168173 return true ;
169174}
170175
171- static bool expandReleaseValue (ReleaseValueInst *DV ) {
172- SILFunction *F = DV ->getFunction ();
173- SILModule &Module = DV ->getModule ();
174- SILBuilderWithScope Builder (DV );
176+ static bool expandReleaseValue (ReleaseValueInst *rvi ) {
177+ SILFunction *fn = rvi ->getFunction ();
178+ SILModule &module = rvi ->getModule ();
179+ SILBuilderWithScope builder (rvi );
175180
176181 // Strength reduce destroy_addr inst into release/store if
177182 // we have a non-address only type.
178- SILValue Value = DV ->getOperand ();
183+ SILValue value = rvi ->getOperand ();
179184
180185 // If we have an address only type, do nothing.
181- SILType Type = Value ->getType ();
182- assert (!SILModuleConventions (Module ).useLoweredAddresses ()
183- || Type .isLoadable (*F ) &&
184- " release_value should never be called on a non-loadable type." );
186+ SILType type = value ->getType ();
187+ assert (!SILModuleConventions (module ).useLoweredAddresses () ||
188+ type .isLoadable (*fn ) &&
189+ " release_value should never be called on a non-loadable type." );
185190
186- if (!shouldExpand (Module, Type .getObjectType ()))
191+ if (!shouldExpand (module , type .getObjectType ()))
187192 return false ;
188193
189- auto &TL = F ->getTypeLowering (Type );
190- TL.emitLoweredDestroyValueMostDerivedDescendents (Builder, DV ->getLoc (),
191- Value );
194+ auto &TL = fn ->getTypeLowering (type );
195+ TL.emitLoweredDestroyValueMostDerivedDescendents (builder, rvi ->getLoc (),
196+ value );
192197
193- LLVM_DEBUG (llvm::dbgs () << " Expanding Destroy Value : " << *DV );
198+ LLVM_DEBUG (llvm::dbgs () << " Expanding: " << *rvi );
194199
195200 ++NumExpand;
196201 return true ;
197202}
198203
199- static bool expandRetainValue (RetainValueInst *CV ) {
200- SILFunction *F = CV ->getFunction ();
201- SILModule &Module = CV ->getModule ();
202- SILBuilderWithScope Builder (CV );
204+ static bool expandRetainValue (RetainValueInst *rvi ) {
205+ SILFunction *fn = rvi ->getFunction ();
206+ SILModule &module = rvi ->getModule ();
207+ SILBuilderWithScope builder (rvi );
203208
204209 // Strength reduce destroy_addr inst into release/store if
205210 // we have a non-address only type.
206- SILValue Value = CV ->getOperand ();
211+ SILValue value = rvi ->getOperand ();
207212
208213 // If we have an address only type, do nothing.
209- SILType Type = Value ->getType ();
210- assert (!SILModuleConventions (Module ).useLoweredAddresses ()
211- || Type .isLoadable (*F ) &&
212- " Copy Value can only be called on loadable types." );
214+ SILType type = value ->getType ();
215+ assert (!SILModuleConventions (module ).useLoweredAddresses () ||
216+ type .isLoadable (*fn ) &&
217+ " Copy Value can only be called on loadable types." );
213218
214- if (!shouldExpand (Module, Type .getObjectType ()))
219+ if (!shouldExpand (module , type .getObjectType ()))
215220 return false ;
216221
217- auto &TL = F->getTypeLowering (Type);
218- TL.emitLoweredCopyValueMostDerivedDescendents (Builder, CV->getLoc (), Value);
222+ auto &typeLowering = fn->getTypeLowering (type);
223+ typeLowering.emitLoweredCopyValueMostDerivedDescendents (builder,
224+ rvi->getLoc (), value);
219225
220- LLVM_DEBUG (llvm::dbgs () << " Expanding Copy Value : " << *CV );
226+ LLVM_DEBUG (llvm::dbgs () << " Expanding: " << *rvi );
221227
222228 ++NumExpand;
223229 return true ;
@@ -227,73 +233,77 @@ static bool expandRetainValue(RetainValueInst *CV) {
227233// Top Level Driver
228234// ===----------------------------------------------------------------------===//
229235
230- static bool processFunction (SILFunction &Fn ) {
231- bool Changed = false ;
232- for (auto BI = Fn. begin (), BE = Fn. end (); BI != BE; ++BI ) {
233- auto II = BI-> begin (), IE = BI-> end ();
234- while (II != IE ) {
235- SILInstruction *Inst = &*II ;
236+ static bool processFunction (SILFunction &fn ) {
237+ bool changed = false ;
238+ for (auto &block : fn ) {
239+ auto ii = block. begin (), ie = block. end ();
240+ while (ii != ie ) {
241+ SILInstruction *inst = &*ii ;
236242
237- LLVM_DEBUG (llvm::dbgs () << " Visiting: " << *Inst );
243+ LLVM_DEBUG (llvm::dbgs () << " Visiting: " << *inst );
238244
239- if (auto *CA = dyn_cast<CopyAddrInst>(Inst ))
240- if (expandCopyAddr (CA )) {
241- ++II ;
242- CA ->eraseFromParent ();
243- Changed = true ;
245+ if (auto *cai = dyn_cast<CopyAddrInst>(inst ))
246+ if (expandCopyAddr (cai )) {
247+ ++ii ;
248+ cai ->eraseFromParent ();
249+ changed = true ;
244250 continue ;
245251 }
246252
247- if (auto *DA = dyn_cast<DestroyAddrInst>(Inst ))
248- if (expandDestroyAddr (DA )) {
249- ++II ;
250- DA ->eraseFromParent ();
251- Changed = true ;
253+ if (auto *dai = dyn_cast<DestroyAddrInst>(inst ))
254+ if (expandDestroyAddr (dai )) {
255+ ++ii ;
256+ dai ->eraseFromParent ();
257+ changed = true ;
252258 continue ;
253259 }
254260
255- if (auto *CV = dyn_cast<RetainValueInst>(Inst ))
256- if (expandRetainValue (CV )) {
257- ++II ;
258- CV ->eraseFromParent ();
259- Changed = true ;
261+ if (auto *rvi = dyn_cast<RetainValueInst>(inst ))
262+ if (expandRetainValue (rvi )) {
263+ ++ii ;
264+ rvi ->eraseFromParent ();
265+ changed = true ;
260266 continue ;
261267 }
262268
263- if (auto *DV = dyn_cast<ReleaseValueInst>(Inst ))
264- if (expandReleaseValue (DV )) {
265- ++II ;
266- DV ->eraseFromParent ();
267- Changed = true ;
269+ if (auto *rvi = dyn_cast<ReleaseValueInst>(inst ))
270+ if (expandReleaseValue (rvi )) {
271+ ++ii ;
272+ rvi ->eraseFromParent ();
273+ changed = true ;
268274 continue ;
269275 }
270276
271- ++II ;
277+ ++ii ;
272278 }
273279 }
274- return Changed ;
280+ return changed ;
275281}
276282
283+ // ===----------------------------------------------------------------------===//
284+ // Top Level Entrypoint
285+ // ===----------------------------------------------------------------------===//
286+
277287namespace {
288+
278289class SILLowerAggregate : public SILFunctionTransform {
279290
280291 // / The entry point to the transformation.
281292 void run () override {
282- SILFunction *F = getFunction ();
293+ SILFunction *f = getFunction ();
283294 // FIXME: Can we support ownership?
284- if (F ->hasOwnership ())
295+ if (f ->hasOwnership ())
285296 return ;
286- LLVM_DEBUG (llvm::dbgs () << " ***** LowerAggregate on function: " <<
287- F ->getName () << " *****\n " );
288- bool Changed = processFunction (*F );
289- if (Changed ) {
297+ LLVM_DEBUG (llvm::dbgs () << " ***** LowerAggregate on function: "
298+ << f ->getName () << " *****\n " );
299+ bool changed = processFunction (*f );
300+ if (changed ) {
290301 invalidateAnalysis (SILAnalysis::InvalidationKind::CallsAndInstructions);
291302 }
292303 }
293-
294304};
295- } // end anonymous namespace
296305
306+ } // end anonymous namespace
297307
298308SILTransform *swift::createLowerAggregateInstrs () {
299309 return new SILLowerAggregate ();
0 commit comments