17
17
18
18
#include " llvm/ADT/ArrayRef.h"
19
19
#include " llvm/ADT/DenseMap.h"
20
- #include " llvm/ADT/Optional.h"
21
20
#include " llvm/ADT/STLExtras.h"
22
21
#include " llvm/ADT/SmallPtrSet.h"
23
22
#include " llvm/ADT/SmallVector.h"
49
48
#include " llvm/Transforms/Utils/Local.h"
50
49
#include " llvm/Transforms/Utils/PromoteMemToReg.h"
51
50
#include < algorithm>
52
-
51
+ # include < cassert >
53
52
#include < iterator>
54
53
#include < utility>
55
54
#include < vector>
@@ -178,16 +177,6 @@ class RenamePassData {
178
177
ValVector Values;
179
178
};
180
179
181
- // / \brief Semi-open interval of instructions that are guaranteed to
182
- // / all execute if the first one does.
183
- class GuaranteedExecutionRange {
184
- public:
185
- unsigned Start;
186
- unsigned End;
187
-
188
- GuaranteedExecutionRange (unsigned S, unsigned E) : Start(S), End(E) {}
189
- };
190
-
191
180
// / \brief This assigns and keeps a per-bb relative ordering of load/store
192
181
// / instructions in the block that directly load or store an alloca.
193
182
// /
@@ -201,108 +190,14 @@ class LargeBlockInfo {
201
190
// / the block.
202
191
DenseMap<const Instruction *, unsigned > InstNumbers;
203
192
204
- // / \brief For each basic block we track, keep track of the intervals
205
- // / of instruction numbers of instructions that transfer control
206
- // / to their successors, for propagating metadata.
207
- DenseMap<const BasicBlock *,
208
- Optional<SmallVector<GuaranteedExecutionRange, 4 >>>
209
- GuaranteedExecutionIntervals;
210
-
211
193
public:
212
- // / This code looks for stores to allocas, and for loads both for
213
- // / allocas and for transferring metadata .
194
+
195
+ // / This code only looks at accesses to allocas .
214
196
static bool isInterestingInstruction (const Instruction *I) {
215
- return isa<LoadInst>(I) ||
197
+ return ( isa<LoadInst>(I) && isa<AllocaInst>(I-> getOperand ( 0 )) ) ||
216
198
(isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand (1 )));
217
199
}
218
200
219
- // / Compute the GuaranteedExecutionIntervals for a given BB.
220
- // /
221
- // / This is valid and remains valid as long as each interesting
222
- // / instruction (see isInterestingInstruction) that
223
- // / A) existed when this LBI was cleared
224
- // / B) has not been deleted (deleting interesting instructions is fine)
225
- // / are run in the same program executions and in the same order
226
- // / as when this LBI was cleared.
227
- // /
228
- // / Because `PromoteMemoryToRegister` does not move memory loads at
229
- // / all, this assumption is satisfied in this pass.
230
- SmallVector<GuaranteedExecutionRange, 4 > computeGEI (const BasicBlock *BB) {
231
- SmallVector<GuaranteedExecutionRange, 4 > GuaranteedExecutionIntervals;
232
-
233
- unsigned InstNo = 0 ;
234
- bool InRange = false ;
235
- unsigned FirstInstInRange = 0 ;
236
- for (const Instruction &BBI : *BB) {
237
- if (isGuaranteedToTransferExecutionToSuccessor (&BBI)) {
238
- if (!InRange && isInterestingInstruction (&BBI)) {
239
- InRange = true ;
240
- FirstInstInRange = InstNo;
241
- }
242
- } else {
243
- if (InRange) {
244
- assert (FirstInstInRange < InstNo &&
245
- " Can't push an empty range here." );
246
- GuaranteedExecutionIntervals.emplace_back (FirstInstInRange, InstNo);
247
- }
248
- InRange = false ;
249
- }
250
-
251
- if (isInterestingInstruction (&BBI)) {
252
- auto It = InstNumbers.find (&BBI);
253
- assert (It != InstNumbers.end () && InstNo <= It->second &&
254
- " missing number for interesting instruction" );
255
- InstNo = It->second + 1 ;
256
- }
257
- }
258
-
259
- if (InRange) {
260
- assert (FirstInstInRange < InstNo && " Can't push an empty range here." );
261
- GuaranteedExecutionIntervals.emplace_back (FirstInstInRange, InstNo);
262
- }
263
-
264
- return GuaranteedExecutionIntervals;
265
- }
266
-
267
- // / Return true if, when CxtI executes, it is guaranteed that either
268
- // / I had executed already or that I is guaranteed to be later executed.
269
- // /
270
- // / The useful property this guarantees is that if I exhibits undefined
271
- // / behavior under some circumstances, then the whole program will exhibit
272
- // / undefined behavior at CxtI.
273
- bool isGuaranteedToBeExecuted (const Instruction *CxtI, const Instruction *I) {
274
- const BasicBlock *BB = CxtI->getParent ();
275
-
276
- if (BB != I->getParent ()) {
277
- // Instructions in different basic blocks, so control flow
278
- // can diverge between them (we could track this with
279
- // postdoms, but we don't bother).
280
- return false ;
281
- }
282
-
283
- unsigned Index1 = getInstructionIndex (CxtI);
284
- unsigned Index2 = getInstructionIndex (I);
285
-
286
- auto &BBGEI = GuaranteedExecutionIntervals[BB];
287
- if (!BBGEI.hasValue ()) {
288
- BBGEI.emplace (computeGEI (BB));
289
- }
290
-
291
- // We want to check whether I and CxtI are in the same range. To do that,
292
- // we notice that CxtI can only be in the first range R where
293
- // CxtI.end < R.end. If we find that range using binary search,
294
- // we can check whether I and CxtI are both in it.
295
- GuaranteedExecutionRange Bound (Index1, Index1);
296
- auto R = std::upper_bound (
297
- BBGEI->begin (), BBGEI->end (), Bound,
298
- [](GuaranteedExecutionRange I_, GuaranteedExecutionRange R) {
299
- return I_.End < R.End ;
300
- });
301
-
302
- return R != BBGEI->end () && R->Start <= Index1 && Index1 < R->End &&
303
- R->Start <= Index2 && Index2 < R->End ;
304
- }
305
-
306
201
// / Get or calculate the index of the specified instruction.
307
202
unsigned getInstructionIndex (const Instruction *I) {
308
203
assert (isInterestingInstruction (I) &&
@@ -318,11 +213,9 @@ class LargeBlockInfo {
318
213
// avoid gratuitus rescans.
319
214
const BasicBlock *BB = I->getParent ();
320
215
unsigned InstNo = 0 ;
321
- GuaranteedExecutionIntervals.erase (BB);
322
216
for (const Instruction &BBI : *BB)
323
217
if (isInterestingInstruction (&BBI))
324
218
InstNumbers[&BBI] = InstNo++;
325
-
326
219
It = InstNumbers.find (I);
327
220
328
221
assert (It != InstNumbers.end () && " Didn't insert instruction?" );
@@ -331,10 +224,7 @@ class LargeBlockInfo {
331
224
332
225
void deleteValue (const Instruction *I) { InstNumbers.erase (I); }
333
226
334
- void clear () {
335
- InstNumbers.clear ();
336
- GuaranteedExecutionIntervals.clear ();
337
- }
227
+ void clear () { InstNumbers.clear (); }
338
228
};
339
229
340
230
struct PromoteMem2Reg {
@@ -412,7 +302,7 @@ struct PromoteMem2Reg {
412
302
const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
413
303
SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
414
304
void RenamePass (BasicBlock *BB, BasicBlock *Pred,
415
- RenamePassData::ValVector &IncVals, LargeBlockInfo &LBI,
305
+ RenamePassData::ValVector &IncVals,
416
306
std::vector<RenamePassData> &Worklist);
417
307
bool QueuePhiNode (BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
418
308
};
@@ -431,29 +321,6 @@ static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
431
321
AC->registerAssumption (CI);
432
322
}
433
323
434
- static void addAssumptionsFromMetadata (LoadInst *LI, Value *ReplVal,
435
- DominatorTree &DT, const DataLayout &DL,
436
- LargeBlockInfo &LBI,
437
- AssumptionCache *AC) {
438
- if (LI->getMetadata (LLVMContext::MD_nonnull) &&
439
- !isKnownNonZero (ReplVal, DL, 0 , AC, LI, &DT)) {
440
- addAssumeNonNull (AC, LI);
441
- }
442
-
443
- if (auto *N = LI->getMetadata (LLVMContext::MD_range)) {
444
- // Range metadata is harder to use as an assumption,
445
- // so don't try to add one, but *do* try to copy
446
- // the metadata to a load in the same BB.
447
- if (LoadInst *NewLI = dyn_cast<LoadInst>(ReplVal)) {
448
- DEBUG (dbgs () << " trying to move !range metadata from" << *LI << " to"
449
- << *NewLI << " \n " );
450
- if (LBI.isGuaranteedToBeExecuted (LI, NewLI)) {
451
- copyRangeMetadata (DL, *LI, N, *NewLI);
452
- }
453
- }
454
- }
455
- }
456
-
457
324
static void removeLifetimeIntrinsicUsers (AllocaInst *AI) {
458
325
// Knowing that this alloca is promotable, we know that it's safe to kill all
459
326
// instructions except for load and store.
@@ -542,7 +409,9 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
542
409
// If the load was marked as nonnull we don't want to lose
543
410
// that information when we erase this Load. So we preserve
544
411
// it with an assume.
545
- addAssumptionsFromMetadata (LI, ReplVal, DT, DL, LBI, AC);
412
+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
413
+ !isKnownNonZero (ReplVal, DL, 0 , AC, LI, &DT))
414
+ addAssumeNonNull (AC, LI);
546
415
547
416
LI->replaceAllUsesWith (ReplVal);
548
417
LI->eraseFromParent ();
@@ -636,7 +505,9 @@ static bool promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
636
505
// Note, if the load was marked as nonnull we don't want to lose that
637
506
// information when we erase it. So we preserve it with an assume.
638
507
Value *ReplVal = std::prev (I)->second ->getOperand (0 );
639
- addAssumptionsFromMetadata (LI, ReplVal, DT, DL, LBI, AC);
508
+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
509
+ !isKnownNonZero (ReplVal, DL, 0 , AC, LI, &DT))
510
+ addAssumeNonNull (AC, LI);
640
511
641
512
LI->replaceAllUsesWith (ReplVal);
642
513
}
@@ -772,6 +643,7 @@ void PromoteMem2Reg::run() {
772
643
773
644
if (Allocas.empty ())
774
645
return ; // All of the allocas must have been trivial!
646
+
775
647
LBI.clear ();
776
648
777
649
// Set the incoming values for the basic block to be null values for all of
@@ -789,10 +661,9 @@ void PromoteMem2Reg::run() {
789
661
RenamePassData RPD = std::move (RenamePassWorkList.back ());
790
662
RenamePassWorkList.pop_back ();
791
663
// RenamePass may add new worklist entries.
792
- RenamePass (RPD.BB , RPD.Pred , RPD.Values , LBI, RenamePassWorkList);
664
+ RenamePass (RPD.BB , RPD.Pred , RPD.Values , RenamePassWorkList);
793
665
} while (!RenamePassWorkList.empty ());
794
666
795
- LBI.clear ();
796
667
// The renamer uses the Visited set to avoid infinite loops. Clear it now.
797
668
Visited.clear ();
798
669
@@ -1004,7 +875,6 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
1004
875
// / predecessor block Pred.
1005
876
void PromoteMem2Reg::RenamePass (BasicBlock *BB, BasicBlock *Pred,
1006
877
RenamePassData::ValVector &IncomingVals,
1007
- LargeBlockInfo &LBI,
1008
878
std::vector<RenamePassData> &Worklist) {
1009
879
NextIteration:
1010
880
// If we are inserting any phi nodes into this BB, they will already be in the
@@ -1071,12 +941,13 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
1071
941
// If the load was marked as nonnull we don't want to lose
1072
942
// that information when we erase this Load. So we preserve
1073
943
// it with an assume.
1074
- addAssumptionsFromMetadata (LI, V, DT, SQ.DL , LBI, AC);
944
+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
945
+ !isKnownNonZero (V, SQ.DL , 0 , AC, LI, &DT))
946
+ addAssumeNonNull (AC, LI);
1075
947
1076
948
// Anything using the load now uses the current value.
1077
949
LI->replaceAllUsesWith (V);
1078
950
BB->getInstList ().erase (LI);
1079
- LBI.deleteValue (LI);
1080
951
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1081
952
// Delete this instruction and mark the name as the current holder of the
1082
953
// value
@@ -1094,7 +965,6 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
1094
965
for (DbgInfoIntrinsic *DII : AllocaDbgDeclares[ai->second ])
1095
966
ConvertDebugDeclareToDebugValue (DII, SI, DIB);
1096
967
BB->getInstList ().erase (SI);
1097
- LBI.deleteValue (SI);
1098
968
}
1099
969
}
1100
970
0 commit comments