[llvm] adefa9c - [SafeStack,NFC] "const" cleanup

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 14 23:15:06 PDT 2020


Author: Vitaly Buka
Date: 2020-06-14T23:05:42-07:00
New Revision: adefa9ca2e19ab197b9efec692d4087ed9ab82e3

URL: https://github.com/llvm/llvm-project/commit/adefa9ca2e19ab197b9efec692d4087ed9ab82e3
DIFF: https://github.com/llvm/llvm-project/commit/adefa9ca2e19ab197b9efec692d4087ed9ab82e3.diff

LOG: [SafeStack,NFC] "const" cleanup

Added: 
    

Modified: 
    llvm/lib/CodeGen/SafeStackColoring.cpp
    llvm/lib/CodeGen/SafeStackColoring.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SafeStackColoring.cpp b/llvm/lib/CodeGen/SafeStackColoring.cpp
index ee7dec789a2d..fd4f31723700 100644
--- a/llvm/lib/CodeGen/SafeStackColoring.cpp
+++ b/llvm/lib/CodeGen/SafeStackColoring.cpp
@@ -26,13 +26,14 @@ using namespace llvm::safestack;
 
 #define DEBUG_TYPE "safestackcoloring"
 
-const StackColoring::LiveRange &StackColoring::getLiveRange(AllocaInst *AI) {
+const StackColoring::LiveRange &
+StackColoring::getLiveRange(const AllocaInst *AI) const {
   const auto IT = AllocaNumbering.find(AI);
   assert(IT != AllocaNumbering.end());
   return LiveRanges[IT->second];
 }
 
-bool StackColoring::readMarker(Instruction *I, bool *IsStart) {
+static bool readMarker(const Instruction *I, bool *IsStart) {
   if (!I->isLifetimeStartOrEnd())
     return false;
 
@@ -44,7 +45,7 @@ bool StackColoring::readMarker(Instruction *I, bool *IsStart) {
 void StackColoring::removeAllMarkers() {
   for (auto *I : Markers) {
     auto *Op = dyn_cast<Instruction>(I->getOperand(1));
-    I->eraseFromParent();
+    const_cast<IntrinsicInst *>(I)->eraseFromParent();
     // Remove the operand bitcast, too, if it has no more uses left.
     if (Op && Op->use_empty())
       Op->eraseFromParent();
@@ -53,16 +54,17 @@ void StackColoring::removeAllMarkers() {
 
 void StackColoring::collectMarkers() {
   InterestingAllocas.resize(NumAllocas);
-  DenseMap<BasicBlock *, SmallDenseMap<IntrinsicInst *, Marker>> BBMarkerSet;
+  DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
+      BBMarkerSet;
 
   // Compute the set of start/end markers per basic block.
   for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
-    AllocaInst *AI = Allocas[AllocaNo];
-    SmallVector<Instruction *, 8> WorkList;
+    const AllocaInst *AI = Allocas[AllocaNo];
+    SmallVector<const Instruction *, 8> WorkList;
     WorkList.push_back(AI);
     while (!WorkList.empty()) {
-      Instruction *I = WorkList.pop_back_val();
-      for (User *U : I->users()) {
+      const Instruction *I = WorkList.pop_back_val();
+      for (const User *U : I->users()) {
         if (auto *BI = dyn_cast<BitCastInst>(U)) {
           WorkList.push_back(BI);
           continue;
@@ -90,7 +92,7 @@ void StackColoring::collectMarkers() {
   // * the sets of allocas whose lifetime starts or ends in this BB
   LLVM_DEBUG(dbgs() << "Instructions:\n");
   unsigned InstNo = 0;
-  for (BasicBlock *BB : depth_first(&F)) {
+  for (const BasicBlock *BB : depth_first(&F)) {
     LLVM_DEBUG(dbgs() << "  " << InstNo << ": BB " << BB->getName() << "\n");
     unsigned BBStart = InstNo++;
 
@@ -104,7 +106,7 @@ void StackColoring::collectMarkers() {
       continue;
     }
 
-    auto ProcessMarker = [&](IntrinsicInst *I, const Marker &M) {
+    auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) {
       LLVM_DEBUG(dbgs() << "  " << InstNo << ":  "
                         << (M.IsStart ? "start " : "end   ") << M.AllocaNo
                         << ", " << *I << "\n");
@@ -129,8 +131,8 @@ void StackColoring::collectMarkers() {
                     BlockMarkerSet.begin()->getSecond());
     } else {
       // Scan the BB to determine the marker order.
-      for (Instruction &I : *BB) {
-        IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
+      for (const Instruction &I : *BB) {
+        const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
         if (!II)
           continue;
         auto It = BlockMarkerSet.find(II);
@@ -151,7 +153,7 @@ void StackColoring::calculateLocalLiveness() {
   while (changed) {
     changed = false;
 
-    for (BasicBlock *BB : depth_first(&F)) {
+    for (const BasicBlock *BB : depth_first(&F)) {
       BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
 
       // Compute LiveIn by unioning together the LiveOut sets of all preds.
@@ -192,7 +194,7 @@ void StackColoring::calculateLocalLiveness() {
 
 void StackColoring::calculateLiveIntervals() {
   for (auto IT : BlockLiveness) {
-    BasicBlock *BB = IT.getFirst();
+    const BasicBlock *BB = IT.getFirst();
     BlockLifetimeInfo &BlockInfo = IT.getSecond();
     unsigned BBStart, BBEnd;
     std::tie(BBStart, BBEnd) = BlockInstRange[BB];
@@ -240,18 +242,18 @@ void StackColoring::calculateLiveIntervals() {
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
-LLVM_DUMP_METHOD void StackColoring::dumpAllocas() {
+LLVM_DUMP_METHOD void StackColoring::dumpAllocas() const {
   dbgs() << "Allocas:\n";
   for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
     dbgs() << "  " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n";
 }
 
-LLVM_DUMP_METHOD void StackColoring::dumpBlockLiveness() {
+LLVM_DUMP_METHOD void StackColoring::dumpBlockLiveness() const {
   dbgs() << "Block liveness:\n";
   for (auto IT : BlockLiveness) {
-    BasicBlock *BB = IT.getFirst();
+    const BasicBlock *BB = IT.getFirst();
     const BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
-    auto BlockRange = BlockInstRange[BB];
+    auto BlockRange = BlockInstRange.find(BB)->getSecond();
     dbgs() << "  BB [" << BlockRange.first << ", " << BlockRange.second
            << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End
            << ", livein " << BlockInfo.LiveIn << ", liveout "
@@ -259,16 +261,15 @@ LLVM_DUMP_METHOD void StackColoring::dumpBlockLiveness() {
   }
 }
 
-LLVM_DUMP_METHOD void StackColoring::dumpLiveRanges() {
+LLVM_DUMP_METHOD void StackColoring::dumpLiveRanges() const {
   dbgs() << "Alloca liveness:\n";
-  for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
-    LiveRange &Range = LiveRanges[AllocaNo];
-    dbgs() << "  " << AllocaNo << ": " << Range << "\n";
-  }
+  for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
+    dbgs() << "  " << AllocaNo << ": " << LiveRanges[AllocaNo] << "\n";
 }
 #endif
 
-StackColoring::StackColoring(Function &F, ArrayRef<AllocaInst *> Allocas)
+StackColoring::StackColoring(const Function &F,
+                             ArrayRef<const AllocaInst *> Allocas)
     : F(F), Allocas(Allocas), NumAllocas(Allocas.size()) {
   LLVM_DEBUG(dumpAllocas());
 

diff  --git a/llvm/lib/CodeGen/SafeStackColoring.h b/llvm/lib/CodeGen/SafeStackColoring.h
index 79141d9c8054..a0c2feb02d10 100644
--- a/llvm/lib/CodeGen/SafeStackColoring.h
+++ b/llvm/lib/CodeGen/SafeStackColoring.h
@@ -73,25 +73,25 @@ class StackColoring {
   };
 
 private:
-  Function &F;
+  const Function &F;
 
   /// Maps active slots (per bit) for each basic block.
-  using LivenessMap = DenseMap<BasicBlock *, BlockLifetimeInfo>;
+  using LivenessMap = DenseMap<const BasicBlock *, BlockLifetimeInfo>;
   LivenessMap BlockLiveness;
 
   /// Number of interesting instructions.
   int NumInst = -1;
 
   /// Numeric ids for interesting instructions.
-  DenseMap<Instruction *, unsigned> InstructionNumbering;
+  DenseMap<const IntrinsicInst *, unsigned> InstructionNumbering;
 
   /// A range [Start, End) of instruction ids for each basic block.
   /// Instructions inside each BB have monotonic and consecutive ids.
   DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
 
-  ArrayRef<AllocaInst *> Allocas;
+  ArrayRef<const AllocaInst *> Allocas;
   unsigned NumAllocas;
-  DenseMap<AllocaInst *, unsigned> AllocaNumbering;
+  DenseMap<const AllocaInst *, unsigned> AllocaNumbering;
 
   /// LiveRange for allocas.
   SmallVector<LiveRange, 8> LiveRanges;
@@ -99,7 +99,7 @@ class StackColoring {
   /// The set of allocas that have at least one lifetime.start. All other
   /// allocas get LiveRange that corresponds to the entire function.
   BitVector InterestingAllocas;
-  SmallVector<IntrinsicInst *, 8> Markers;
+  SmallVector<const IntrinsicInst *, 8> Markers;
 
   struct Marker {
     unsigned AllocaNo;
@@ -107,19 +107,19 @@ class StackColoring {
   };
 
   /// List of {InstNo, {AllocaNo, IsStart}} for each BB, ordered by InstNo.
-  DenseMap<BasicBlock *, SmallVector<std::pair<unsigned, Marker>, 4>> BBMarkers;
+  DenseMap<const BasicBlock *, SmallVector<std::pair<unsigned, Marker>, 4>>
+      BBMarkers;
 
-  void dumpAllocas();
-  void dumpBlockLiveness();
-  void dumpLiveRanges();
+  void dumpAllocas() const;
+  void dumpBlockLiveness() const;
+  void dumpLiveRanges() const;
 
-  bool readMarker(Instruction *II, bool *IsStart);
   void collectMarkers();
   void calculateLocalLiveness();
   void calculateLiveIntervals();
 
 public:
-  StackColoring(Function &F, ArrayRef<AllocaInst *> Allocas);
+  StackColoring(const Function &F, ArrayRef<const AllocaInst *> Allocas);
 
   void run();
   void removeAllMarkers();
@@ -127,11 +127,11 @@ class StackColoring {
   /// Returns a set of "interesting" instructions where the given alloca is
   /// live. Not all instructions in a function are interesting: we pick a set
   /// that is large enough for LiveRange::Overlaps to be correct.
-  const LiveRange &getLiveRange(AllocaInst *AI);
+  const LiveRange &getLiveRange(const AllocaInst *AI) const;
 
   /// Returns a live range that represents an alloca that is live throughout the
   /// entire function.
-  LiveRange getFullLiveRange() {
+  LiveRange getFullLiveRange() const {
     assert(NumInst >= 0);
     LiveRange R;
     R.SetMaximum(NumInst);


        


More information about the llvm-commits mailing list