Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 1 | //===-- LowerBitSets.cpp - Bitset lowering pass ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass lowers bitset metadata and calls to the llvm.bitset.test intrinsic. |
| 11 | // See http://llvm.org/docs/LangRef.html#bitsets for more information. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/IPO/LowerBitSets.h" |
| 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/ADT/EquivalenceClasses.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
Peter Collingbourne | c9f277f | 2015-03-14 00:00:49 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constant.h" |
| 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/GlobalVariable.h" |
| 23 | #include "llvm/IR/IRBuilder.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/Intrinsics.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/IR/Operator.h" |
| 28 | #include "llvm/Pass.h" |
| 29 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "lowerbitsets" |
| 34 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 35 | STATISTIC(ByteArraySizeBits, "Byte array size in bits"); |
| 36 | STATISTIC(ByteArraySizeBytes, "Byte array size in bytes"); |
| 37 | STATISTIC(NumByteArraysCreated, "Number of byte arrays created"); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 38 | STATISTIC(NumBitSetCallsLowered, "Number of bitset calls lowered"); |
| 39 | STATISTIC(NumBitSetDisjointSets, "Number of disjoint sets of bitsets"); |
| 40 | |
Peter Collingbourne | 994ba3d | 2015-03-19 22:02:10 +0000 | [diff] [blame] | 41 | static cl::opt<bool> AvoidReuse( |
| 42 | "lowerbitsets-avoid-reuse", |
| 43 | cl::desc("Try to avoid reuse of byte array addresses using aliases"), |
| 44 | cl::Hidden, cl::init(true)); |
| 45 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 46 | bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const { |
| 47 | if (Offset < ByteOffset) |
| 48 | return false; |
| 49 | |
| 50 | if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0) |
| 51 | return false; |
| 52 | |
| 53 | uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2; |
| 54 | if (BitOffset >= BitSize) |
| 55 | return false; |
| 56 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 57 | return Bits.count(BitOffset); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool BitSetInfo::containsValue( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 61 | const DataLayout &DL, |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 62 | const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout, Value *V, |
| 63 | uint64_t COffset) const { |
| 64 | if (auto GV = dyn_cast<GlobalVariable>(V)) { |
| 65 | auto I = GlobalLayout.find(GV); |
| 66 | if (I == GlobalLayout.end()) |
| 67 | return false; |
| 68 | return containsGlobalOffset(I->second + COffset); |
| 69 | } |
| 70 | |
| 71 | if (auto GEP = dyn_cast<GEPOperator>(V)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 72 | APInt APOffset(DL.getPointerSizeInBits(0), 0); |
| 73 | bool Result = GEP->accumulateConstantOffset(DL, APOffset); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 74 | if (!Result) |
| 75 | return false; |
| 76 | COffset += APOffset.getZExtValue(); |
| 77 | return containsValue(DL, GlobalLayout, GEP->getPointerOperand(), |
| 78 | COffset); |
| 79 | } |
| 80 | |
| 81 | if (auto Op = dyn_cast<Operator>(V)) { |
| 82 | if (Op->getOpcode() == Instruction::BitCast) |
| 83 | return containsValue(DL, GlobalLayout, Op->getOperand(0), COffset); |
| 84 | |
| 85 | if (Op->getOpcode() == Instruction::Select) |
| 86 | return containsValue(DL, GlobalLayout, Op->getOperand(1), COffset) && |
| 87 | containsValue(DL, GlobalLayout, Op->getOperand(2), COffset); |
| 88 | } |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | BitSetInfo BitSetBuilder::build() { |
| 94 | if (Min > Max) |
| 95 | Min = 0; |
| 96 | |
| 97 | // Normalize each offset against the minimum observed offset, and compute |
| 98 | // the bitwise OR of each of the offsets. The number of trailing zeros |
| 99 | // in the mask gives us the log2 of the alignment of all offsets, which |
| 100 | // allows us to compress the bitset by only storing one bit per aligned |
| 101 | // address. |
| 102 | uint64_t Mask = 0; |
| 103 | for (uint64_t &Offset : Offsets) { |
| 104 | Offset -= Min; |
| 105 | Mask |= Offset; |
| 106 | } |
| 107 | |
| 108 | BitSetInfo BSI; |
| 109 | BSI.ByteOffset = Min; |
| 110 | |
| 111 | BSI.AlignLog2 = 0; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 112 | if (Mask != 0) |
| 113 | BSI.AlignLog2 = countTrailingZeros(Mask, ZB_Undefined); |
| 114 | |
| 115 | // Build the compressed bitset while normalizing the offsets against the |
| 116 | // computed alignment. |
| 117 | BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 118 | for (uint64_t Offset : Offsets) { |
| 119 | Offset >>= BSI.AlignLog2; |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 120 | BSI.Bits.insert(Offset); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return BSI; |
| 124 | } |
| 125 | |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 126 | void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) { |
| 127 | // Create a new fragment to hold the layout for F. |
| 128 | Fragments.emplace_back(); |
| 129 | std::vector<uint64_t> &Fragment = Fragments.back(); |
| 130 | uint64_t FragmentIndex = Fragments.size() - 1; |
| 131 | |
| 132 | for (auto ObjIndex : F) { |
| 133 | uint64_t OldFragmentIndex = FragmentMap[ObjIndex]; |
| 134 | if (OldFragmentIndex == 0) { |
| 135 | // We haven't seen this object index before, so just add it to the current |
| 136 | // fragment. |
| 137 | Fragment.push_back(ObjIndex); |
| 138 | } else { |
| 139 | // This index belongs to an existing fragment. Copy the elements of the |
| 140 | // old fragment into this one and clear the old fragment. We don't update |
| 141 | // the fragment map just yet, this ensures that any further references to |
| 142 | // indices from the old fragment in this fragment do not insert any more |
| 143 | // indices. |
| 144 | std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex]; |
| 145 | Fragment.insert(Fragment.end(), OldFragment.begin(), OldFragment.end()); |
| 146 | OldFragment.clear(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Update the fragment map to point our object indices to this fragment. |
| 151 | for (uint64_t ObjIndex : Fragment) |
| 152 | FragmentMap[ObjIndex] = FragmentIndex; |
| 153 | } |
| 154 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 155 | void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits, |
| 156 | uint64_t BitSize, uint64_t &AllocByteOffset, |
| 157 | uint8_t &AllocMask) { |
| 158 | // Find the smallest current allocation. |
| 159 | unsigned Bit = 0; |
| 160 | for (unsigned I = 1; I != BitsPerByte; ++I) |
| 161 | if (BitAllocs[I] < BitAllocs[Bit]) |
| 162 | Bit = I; |
| 163 | |
| 164 | AllocByteOffset = BitAllocs[Bit]; |
| 165 | |
| 166 | // Add our size to it. |
| 167 | unsigned ReqSize = AllocByteOffset + BitSize; |
| 168 | BitAllocs[Bit] = ReqSize; |
| 169 | if (Bytes.size() < ReqSize) |
| 170 | Bytes.resize(ReqSize); |
| 171 | |
| 172 | // Set our bits. |
| 173 | AllocMask = 1 << Bit; |
| 174 | for (uint64_t B : Bits) |
| 175 | Bytes[AllocByteOffset + B] |= AllocMask; |
| 176 | } |
| 177 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 178 | namespace { |
| 179 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 180 | struct ByteArrayInfo { |
| 181 | std::set<uint64_t> Bits; |
| 182 | uint64_t BitSize; |
| 183 | GlobalVariable *ByteArray; |
| 184 | Constant *Mask; |
| 185 | }; |
| 186 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 187 | struct LowerBitSets : public ModulePass { |
| 188 | static char ID; |
| 189 | LowerBitSets() : ModulePass(ID) { |
| 190 | initializeLowerBitSetsPass(*PassRegistry::getPassRegistry()); |
| 191 | } |
| 192 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 193 | Module *M; |
| 194 | |
Peter Collingbourne | c9f277f | 2015-03-14 00:00:49 +0000 | [diff] [blame] | 195 | bool LinkerSubsectionsViaSymbols; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 196 | IntegerType *Int1Ty; |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 197 | IntegerType *Int8Ty; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 198 | IntegerType *Int32Ty; |
| 199 | Type *Int32PtrTy; |
| 200 | IntegerType *Int64Ty; |
| 201 | Type *IntPtrTy; |
| 202 | |
| 203 | // The llvm.bitsets named metadata. |
| 204 | NamedMDNode *BitSetNM; |
| 205 | |
| 206 | // Mapping from bitset mdstrings to the call sites that test them. |
| 207 | DenseMap<MDString *, std::vector<CallInst *>> BitSetTestCallSites; |
| 208 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 209 | std::vector<ByteArrayInfo> ByteArrayInfos; |
| 210 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 211 | BitSetInfo |
| 212 | buildBitSet(MDString *BitSet, |
| 213 | const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 214 | ByteArrayInfo *createByteArray(BitSetInfo &BSI); |
| 215 | void allocateByteArrays(); |
| 216 | Value *createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, ByteArrayInfo *&BAI, |
| 217 | Value *BitOffset); |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 218 | Value * |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 219 | lowerBitSetCall(CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, |
| 220 | GlobalVariable *CombinedGlobal, |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 221 | const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 222 | void buildBitSetsFromGlobals(const std::vector<MDString *> &BitSets, |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 223 | const std::vector<GlobalVariable *> &Globals); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 224 | bool buildBitSets(); |
| 225 | bool eraseBitSetMetadata(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 226 | |
| 227 | bool doInitialization(Module &M) override; |
| 228 | bool runOnModule(Module &M) override; |
| 229 | }; |
| 230 | |
| 231 | } // namespace |
| 232 | |
| 233 | INITIALIZE_PASS_BEGIN(LowerBitSets, "lowerbitsets", |
| 234 | "Lower bitset metadata", false, false) |
| 235 | INITIALIZE_PASS_END(LowerBitSets, "lowerbitsets", |
| 236 | "Lower bitset metadata", false, false) |
| 237 | char LowerBitSets::ID = 0; |
| 238 | |
| 239 | ModulePass *llvm::createLowerBitSetsPass() { return new LowerBitSets; } |
| 240 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 241 | bool LowerBitSets::doInitialization(Module &Mod) { |
| 242 | M = &Mod; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 243 | const DataLayout &DL = Mod.getDataLayout(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 244 | |
Peter Collingbourne | c9f277f | 2015-03-14 00:00:49 +0000 | [diff] [blame] | 245 | Triple TargetTriple(M->getTargetTriple()); |
| 246 | LinkerSubsectionsViaSymbols = TargetTriple.isMacOSX(); |
| 247 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 248 | Int1Ty = Type::getInt1Ty(M->getContext()); |
| 249 | Int8Ty = Type::getInt8Ty(M->getContext()); |
| 250 | Int32Ty = Type::getInt32Ty(M->getContext()); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 251 | Int32PtrTy = PointerType::getUnqual(Int32Ty); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 252 | Int64Ty = Type::getInt64Ty(M->getContext()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 253 | IntPtrTy = DL.getIntPtrType(M->getContext(), 0); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 254 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 255 | BitSetNM = M->getNamedMetadata("llvm.bitsets"); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 256 | |
| 257 | BitSetTestCallSites.clear(); |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
NAKAMURA Takumi | 6c24684 | 2015-02-22 09:51:42 +0000 | [diff] [blame] | 262 | /// Build a bit set for BitSet using the object layouts in |
| 263 | /// GlobalLayout. |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 264 | BitSetInfo LowerBitSets::buildBitSet( |
| 265 | MDString *BitSet, |
| 266 | const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout) { |
| 267 | BitSetBuilder BSB; |
| 268 | |
| 269 | // Compute the byte offset of each element of this bitset. |
| 270 | if (BitSetNM) { |
| 271 | for (MDNode *Op : BitSetNM->operands()) { |
| 272 | if (Op->getOperand(0) != BitSet || !Op->getOperand(1)) |
| 273 | continue; |
Peter Collingbourne | ba4c8b5 | 2015-06-27 00:17:51 +0000 | [diff] [blame] | 274 | auto OpGlobal = dyn_cast<GlobalVariable>( |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 275 | cast<ConstantAsMetadata>(Op->getOperand(1))->getValue()); |
Peter Collingbourne | ba4c8b5 | 2015-06-27 00:17:51 +0000 | [diff] [blame] | 276 | if (!OpGlobal) |
| 277 | continue; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 278 | uint64_t Offset = |
| 279 | cast<ConstantInt>(cast<ConstantAsMetadata>(Op->getOperand(2)) |
| 280 | ->getValue())->getZExtValue(); |
| 281 | |
| 282 | Offset += GlobalLayout.find(OpGlobal)->second; |
| 283 | |
| 284 | BSB.addOffset(Offset); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return BSB.build(); |
| 289 | } |
| 290 | |
NAKAMURA Takumi | 6c24684 | 2015-02-22 09:51:42 +0000 | [diff] [blame] | 291 | /// Build a test that bit BitOffset mod sizeof(Bits)*8 is set in |
| 292 | /// Bits. This pattern matches to the bt instruction on x86. |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 293 | static Value *createMaskedBitTest(IRBuilder<> &B, Value *Bits, |
| 294 | Value *BitOffset) { |
| 295 | auto BitsType = cast<IntegerType>(Bits->getType()); |
| 296 | unsigned BitWidth = BitsType->getBitWidth(); |
| 297 | |
| 298 | BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType); |
| 299 | Value *BitIndex = |
| 300 | B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1)); |
| 301 | Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex); |
| 302 | Value *MaskedBits = B.CreateAnd(Bits, BitMask); |
| 303 | return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0)); |
| 304 | } |
| 305 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 306 | ByteArrayInfo *LowerBitSets::createByteArray(BitSetInfo &BSI) { |
| 307 | // Create globals to stand in for byte arrays and masks. These never actually |
| 308 | // get initialized, we RAUW and erase them later in allocateByteArrays() once |
| 309 | // we know the offset and mask to use. |
| 310 | auto ByteArrayGlobal = new GlobalVariable( |
| 311 | *M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr); |
| 312 | auto MaskGlobal = new GlobalVariable( |
| 313 | *M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr); |
| 314 | |
| 315 | ByteArrayInfos.emplace_back(); |
| 316 | ByteArrayInfo *BAI = &ByteArrayInfos.back(); |
| 317 | |
| 318 | BAI->Bits = BSI.Bits; |
| 319 | BAI->BitSize = BSI.BitSize; |
| 320 | BAI->ByteArray = ByteArrayGlobal; |
| 321 | BAI->Mask = ConstantExpr::getPtrToInt(MaskGlobal, Int8Ty); |
| 322 | return BAI; |
| 323 | } |
| 324 | |
| 325 | void LowerBitSets::allocateByteArrays() { |
| 326 | std::stable_sort(ByteArrayInfos.begin(), ByteArrayInfos.end(), |
| 327 | [](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) { |
| 328 | return BAI1.BitSize > BAI2.BitSize; |
| 329 | }); |
| 330 | |
| 331 | std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size()); |
| 332 | |
| 333 | ByteArrayBuilder BAB; |
| 334 | for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) { |
| 335 | ByteArrayInfo *BAI = &ByteArrayInfos[I]; |
| 336 | |
| 337 | uint8_t Mask; |
| 338 | BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask); |
| 339 | |
| 340 | BAI->Mask->replaceAllUsesWith(ConstantInt::get(Int8Ty, Mask)); |
| 341 | cast<GlobalVariable>(BAI->Mask->getOperand(0))->eraseFromParent(); |
| 342 | } |
| 343 | |
| 344 | Constant *ByteArrayConst = ConstantDataArray::get(M->getContext(), BAB.Bytes); |
| 345 | auto ByteArray = |
| 346 | new GlobalVariable(*M, ByteArrayConst->getType(), /*isConstant=*/true, |
| 347 | GlobalValue::PrivateLinkage, ByteArrayConst); |
| 348 | |
| 349 | for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) { |
| 350 | ByteArrayInfo *BAI = &ByteArrayInfos[I]; |
| 351 | |
| 352 | Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0), |
| 353 | ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])}; |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 354 | Constant *GEP = ConstantExpr::getInBoundsGetElementPtr( |
| 355 | ByteArrayConst->getType(), ByteArray, Idxs); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 356 | |
| 357 | // Create an alias instead of RAUW'ing the gep directly. On x86 this ensures |
| 358 | // that the pc-relative displacement is folded into the lea instead of the |
| 359 | // test instruction getting another displacement. |
Peter Collingbourne | ad0bdcd | 2015-03-16 23:36:24 +0000 | [diff] [blame] | 360 | if (LinkerSubsectionsViaSymbols) { |
| 361 | BAI->ByteArray->replaceAllUsesWith(GEP); |
| 362 | } else { |
David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 363 | GlobalAlias *Alias = |
| 364 | GlobalAlias::create(PointerType::getUnqual(Int8Ty), |
| 365 | GlobalValue::PrivateLinkage, "bits", GEP, M); |
Peter Collingbourne | ad0bdcd | 2015-03-16 23:36:24 +0000 | [diff] [blame] | 366 | BAI->ByteArray->replaceAllUsesWith(Alias); |
| 367 | } |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 368 | BAI->ByteArray->eraseFromParent(); |
| 369 | } |
| 370 | |
| 371 | ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] + |
| 372 | BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] + |
| 373 | BAB.BitAllocs[6] + BAB.BitAllocs[7]; |
| 374 | ByteArraySizeBytes = BAB.Bytes.size(); |
| 375 | } |
| 376 | |
NAKAMURA Takumi | 6c24684 | 2015-02-22 09:51:42 +0000 | [diff] [blame] | 377 | /// Build a test that bit BitOffset is set in BSI, where |
| 378 | /// BitSetGlobal is a global containing the bits in BSI. |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 379 | Value *LowerBitSets::createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, |
| 380 | ByteArrayInfo *&BAI, Value *BitOffset) { |
| 381 | if (BSI.BitSize <= 64) { |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 382 | // If the bit set is sufficiently small, we can avoid a load by bit testing |
| 383 | // a constant. |
| 384 | IntegerType *BitsTy; |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 385 | if (BSI.BitSize <= 32) |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 386 | BitsTy = Int32Ty; |
| 387 | else |
| 388 | BitsTy = Int64Ty; |
| 389 | |
| 390 | uint64_t Bits = 0; |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 391 | for (auto Bit : BSI.Bits) |
| 392 | Bits |= uint64_t(1) << Bit; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 393 | Constant *BitsConst = ConstantInt::get(BitsTy, Bits); |
| 394 | return createMaskedBitTest(B, BitsConst, BitOffset); |
| 395 | } else { |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 396 | if (!BAI) { |
| 397 | ++NumByteArraysCreated; |
| 398 | BAI = createByteArray(BSI); |
| 399 | } |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 400 | |
Peter Collingbourne | 994ba3d | 2015-03-19 22:02:10 +0000 | [diff] [blame] | 401 | Constant *ByteArray = BAI->ByteArray; |
David Blaikie | 93c5444 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 402 | Type *Ty = BAI->ByteArray->getValueType(); |
Peter Collingbourne | 994ba3d | 2015-03-19 22:02:10 +0000 | [diff] [blame] | 403 | if (!LinkerSubsectionsViaSymbols && AvoidReuse) { |
| 404 | // Each use of the byte array uses a different alias. This makes the |
| 405 | // backend less likely to reuse previously computed byte array addresses, |
| 406 | // improving the security of the CFI mechanism based on this pass. |
David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 407 | ByteArray = GlobalAlias::create(BAI->ByteArray->getType(), |
David Blaikie | 93c5444 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 408 | GlobalValue::PrivateLinkage, "bits_use", |
| 409 | ByteArray, M); |
Peter Collingbourne | 994ba3d | 2015-03-19 22:02:10 +0000 | [diff] [blame] | 410 | } |
| 411 | |
David Blaikie | 93c5444 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 412 | Value *ByteAddr = B.CreateGEP(Ty, ByteArray, BitOffset); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 413 | Value *Byte = B.CreateLoad(ByteAddr); |
| 414 | |
| 415 | Value *ByteAndMask = B.CreateAnd(Byte, BAI->Mask); |
| 416 | return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0)); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 420 | /// Lower a llvm.bitset.test call to its implementation. Returns the value to |
| 421 | /// replace the call with. |
| 422 | Value *LowerBitSets::lowerBitSetCall( |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 423 | CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 424 | GlobalVariable *CombinedGlobal, |
| 425 | const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout) { |
| 426 | Value *Ptr = CI->getArgOperand(0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 427 | const DataLayout &DL = M->getDataLayout(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 428 | |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 429 | if (BSI.containsValue(DL, GlobalLayout, Ptr)) |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 430 | return ConstantInt::getTrue(CombinedGlobal->getParent()->getContext()); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 431 | |
| 432 | Constant *GlobalAsInt = ConstantExpr::getPtrToInt(CombinedGlobal, IntPtrTy); |
| 433 | Constant *OffsetedGlobalAsInt = ConstantExpr::getAdd( |
| 434 | GlobalAsInt, ConstantInt::get(IntPtrTy, BSI.ByteOffset)); |
| 435 | |
| 436 | BasicBlock *InitialBB = CI->getParent(); |
| 437 | |
| 438 | IRBuilder<> B(CI); |
| 439 | |
| 440 | Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy); |
| 441 | |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 442 | if (BSI.isSingleOffset()) |
| 443 | return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 444 | |
| 445 | Value *PtrOffset = B.CreateSub(PtrAsInt, OffsetedGlobalAsInt); |
| 446 | |
| 447 | Value *BitOffset; |
| 448 | if (BSI.AlignLog2 == 0) { |
| 449 | BitOffset = PtrOffset; |
| 450 | } else { |
| 451 | // We need to check that the offset both falls within our range and is |
| 452 | // suitably aligned. We can check both properties at the same time by |
| 453 | // performing a right rotate by log2(alignment) followed by an integer |
| 454 | // comparison against the bitset size. The rotate will move the lower |
| 455 | // order bits that need to be zero into the higher order bits of the |
| 456 | // result, causing the comparison to fail if they are nonzero. The rotate |
| 457 | // also conveniently gives us a bit offset to use during the load from |
| 458 | // the bitset. |
| 459 | Value *OffsetSHR = |
| 460 | B.CreateLShr(PtrOffset, ConstantInt::get(IntPtrTy, BSI.AlignLog2)); |
| 461 | Value *OffsetSHL = B.CreateShl( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 462 | PtrOffset, |
| 463 | ConstantInt::get(IntPtrTy, DL.getPointerSizeInBits(0) - BSI.AlignLog2)); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 464 | BitOffset = B.CreateOr(OffsetSHR, OffsetSHL); |
| 465 | } |
| 466 | |
| 467 | Constant *BitSizeConst = ConstantInt::get(IntPtrTy, BSI.BitSize); |
| 468 | Value *OffsetInRange = B.CreateICmpULT(BitOffset, BitSizeConst); |
| 469 | |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 470 | // If the bit set is all ones, testing against it is unnecessary. |
| 471 | if (BSI.isAllOnes()) |
| 472 | return OffsetInRange; |
| 473 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 474 | TerminatorInst *Term = SplitBlockAndInsertIfThen(OffsetInRange, CI, false); |
| 475 | IRBuilder<> ThenB(Term); |
| 476 | |
| 477 | // Now that we know that the offset is in range and aligned, load the |
| 478 | // appropriate bit from the bitset. |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 479 | Value *Bit = createBitSetTest(ThenB, BSI, BAI, BitOffset); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 480 | |
| 481 | // The value we want is 0 if we came directly from the initial block |
| 482 | // (having failed the range or alignment checks), or the loaded bit if |
| 483 | // we came from the block in which we loaded it. |
| 484 | B.SetInsertPoint(CI); |
| 485 | PHINode *P = B.CreatePHI(Int1Ty, 2); |
| 486 | P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB); |
| 487 | P->addIncoming(Bit, ThenB.GetInsertBlock()); |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 488 | return P; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /// Given a disjoint set of bitsets and globals, layout the globals, build the |
| 492 | /// bit sets and lower the llvm.bitset.test calls. |
| 493 | void LowerBitSets::buildBitSetsFromGlobals( |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 494 | const std::vector<MDString *> &BitSets, |
| 495 | const std::vector<GlobalVariable *> &Globals) { |
| 496 | // Build a new global with the combined contents of the referenced globals. |
| 497 | std::vector<Constant *> GlobalInits; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 498 | const DataLayout &DL = M->getDataLayout(); |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 499 | for (GlobalVariable *G : Globals) { |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 500 | GlobalInits.push_back(G->getInitializer()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 501 | uint64_t InitSize = DL.getTypeAllocSize(G->getInitializer()->getType()); |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 502 | |
| 503 | // Compute the amount of padding required to align the next element to the |
| 504 | // next power of 2. |
| 505 | uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize; |
| 506 | |
| 507 | // Cap at 128 was found experimentally to have a good data/instruction |
| 508 | // overhead tradeoff. |
| 509 | if (Padding > 128) |
| 510 | Padding = RoundUpToAlignment(InitSize, 128) - InitSize; |
| 511 | |
| 512 | GlobalInits.push_back( |
| 513 | ConstantAggregateZero::get(ArrayType::get(Int8Ty, Padding))); |
| 514 | } |
| 515 | if (!GlobalInits.empty()) |
| 516 | GlobalInits.pop_back(); |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 517 | Constant *NewInit = ConstantStruct::getAnon(M->getContext(), GlobalInits); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 518 | auto CombinedGlobal = |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 519 | new GlobalVariable(*M, NewInit->getType(), /*isConstant=*/true, |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 520 | GlobalValue::PrivateLinkage, NewInit); |
| 521 | |
| 522 | const StructLayout *CombinedGlobalLayout = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 523 | DL.getStructLayout(cast<StructType>(NewInit->getType())); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 524 | |
| 525 | // Compute the offsets of the original globals within the new global. |
| 526 | DenseMap<GlobalVariable *, uint64_t> GlobalLayout; |
| 527 | for (unsigned I = 0; I != Globals.size(); ++I) |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 528 | // Multiply by 2 to account for padding elements. |
| 529 | GlobalLayout[Globals[I]] = CombinedGlobalLayout->getElementOffset(I * 2); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 530 | |
| 531 | // For each bitset in this disjoint set... |
| 532 | for (MDString *BS : BitSets) { |
| 533 | // Build the bitset. |
| 534 | BitSetInfo BSI = buildBitSet(BS, GlobalLayout); |
| 535 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 536 | ByteArrayInfo *BAI = 0; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 537 | |
| 538 | // Lower each call to llvm.bitset.test for this bitset. |
| 539 | for (CallInst *CI : BitSetTestCallSites[BS]) { |
| 540 | ++NumBitSetCallsLowered; |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 541 | Value *Lowered = lowerBitSetCall(CI, BSI, BAI, CombinedGlobal, GlobalLayout); |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 542 | CI->replaceAllUsesWith(Lowered); |
| 543 | CI->eraseFromParent(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | // Build aliases pointing to offsets into the combined global for each |
| 548 | // global from which we built the combined global, and replace references |
| 549 | // to the original globals with references to the aliases. |
| 550 | for (unsigned I = 0; I != Globals.size(); ++I) { |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 551 | // Multiply by 2 to account for padding elements. |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 552 | Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0), |
Peter Collingbourne | eba7f73 | 2015-02-25 20:42:41 +0000 | [diff] [blame] | 553 | ConstantInt::get(Int32Ty, I * 2)}; |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 554 | Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr( |
| 555 | NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs); |
Peter Collingbourne | ad0bdcd | 2015-03-16 23:36:24 +0000 | [diff] [blame] | 556 | if (LinkerSubsectionsViaSymbols) { |
| 557 | Globals[I]->replaceAllUsesWith(CombinedGlobalElemPtr); |
| 558 | } else { |
David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 559 | GlobalAlias *GAlias = |
| 560 | GlobalAlias::create(Globals[I]->getType(), Globals[I]->getLinkage(), |
Peter Collingbourne | 4fc603d | 2015-06-17 18:31:02 +0000 | [diff] [blame] | 561 | "", CombinedGlobalElemPtr, M); |
| 562 | GAlias->takeName(Globals[I]); |
Peter Collingbourne | ad0bdcd | 2015-03-16 23:36:24 +0000 | [diff] [blame] | 563 | Globals[I]->replaceAllUsesWith(GAlias); |
| 564 | } |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 565 | Globals[I]->eraseFromParent(); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /// Lower all bit sets in this module. |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 570 | bool LowerBitSets::buildBitSets() { |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 571 | Function *BitSetTestFunc = |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 572 | M->getFunction(Intrinsic::getName(Intrinsic::bitset_test)); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 573 | if (!BitSetTestFunc) |
| 574 | return false; |
| 575 | |
| 576 | // Equivalence class set containing bitsets and the globals they reference. |
| 577 | // This is used to partition the set of bitsets in the module into disjoint |
| 578 | // sets. |
| 579 | typedef EquivalenceClasses<PointerUnion<GlobalVariable *, MDString *>> |
| 580 | GlobalClassesTy; |
| 581 | GlobalClassesTy GlobalClasses; |
| 582 | |
| 583 | for (const Use &U : BitSetTestFunc->uses()) { |
| 584 | auto CI = cast<CallInst>(U.getUser()); |
| 585 | |
| 586 | auto BitSetMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1)); |
| 587 | if (!BitSetMDVal || !isa<MDString>(BitSetMDVal->getMetadata())) |
| 588 | report_fatal_error( |
| 589 | "Second argument of llvm.bitset.test must be metadata string"); |
| 590 | auto BitSet = cast<MDString>(BitSetMDVal->getMetadata()); |
| 591 | |
| 592 | // Add the call site to the list of call sites for this bit set. We also use |
| 593 | // BitSetTestCallSites to keep track of whether we have seen this bit set |
| 594 | // before. If we have, we don't need to re-add the referenced globals to the |
| 595 | // equivalence class. |
| 596 | std::pair<DenseMap<MDString *, std::vector<CallInst *>>::iterator, |
| 597 | bool> Ins = |
| 598 | BitSetTestCallSites.insert( |
| 599 | std::make_pair(BitSet, std::vector<CallInst *>())); |
| 600 | Ins.first->second.push_back(CI); |
| 601 | if (!Ins.second) |
| 602 | continue; |
| 603 | |
| 604 | // Add the bitset to the equivalence class. |
| 605 | GlobalClassesTy::iterator GCI = GlobalClasses.insert(BitSet); |
| 606 | GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI); |
| 607 | |
| 608 | if (!BitSetNM) |
| 609 | continue; |
| 610 | |
| 611 | // Verify the bitset metadata and add the referenced globals to the bitset's |
| 612 | // equivalence class. |
| 613 | for (MDNode *Op : BitSetNM->operands()) { |
| 614 | if (Op->getNumOperands() != 3) |
| 615 | report_fatal_error( |
| 616 | "All operands of llvm.bitsets metadata must have 3 elements"); |
| 617 | |
| 618 | if (Op->getOperand(0) != BitSet || !Op->getOperand(1)) |
| 619 | continue; |
| 620 | |
| 621 | auto OpConstMD = dyn_cast<ConstantAsMetadata>(Op->getOperand(1)); |
| 622 | if (!OpConstMD) |
| 623 | report_fatal_error("Bit set element must be a constant"); |
| 624 | auto OpGlobal = dyn_cast<GlobalVariable>(OpConstMD->getValue()); |
| 625 | if (!OpGlobal) |
Peter Collingbourne | ba4c8b5 | 2015-06-27 00:17:51 +0000 | [diff] [blame] | 626 | continue; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 627 | |
| 628 | auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Op->getOperand(2)); |
| 629 | if (!OffsetConstMD) |
| 630 | report_fatal_error("Bit set element offset must be a constant"); |
| 631 | auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue()); |
| 632 | if (!OffsetInt) |
| 633 | report_fatal_error( |
| 634 | "Bit set element offset must be an integer constant"); |
| 635 | |
| 636 | CurSet = GlobalClasses.unionSets( |
| 637 | CurSet, GlobalClasses.findLeader(GlobalClasses.insert(OpGlobal))); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (GlobalClasses.empty()) |
| 642 | return false; |
| 643 | |
| 644 | // For each disjoint set we found... |
| 645 | for (GlobalClassesTy::iterator I = GlobalClasses.begin(), |
| 646 | E = GlobalClasses.end(); |
| 647 | I != E; ++I) { |
| 648 | if (!I->isLeader()) continue; |
| 649 | |
| 650 | ++NumBitSetDisjointSets; |
| 651 | |
| 652 | // Build the list of bitsets and referenced globals in this disjoint set. |
| 653 | std::vector<MDString *> BitSets; |
| 654 | std::vector<GlobalVariable *> Globals; |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 655 | llvm::DenseMap<MDString *, uint64_t> BitSetIndices; |
| 656 | llvm::DenseMap<GlobalVariable *, uint64_t> GlobalIndices; |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 657 | for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I); |
| 658 | MI != GlobalClasses.member_end(); ++MI) { |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 659 | if ((*MI).is<MDString *>()) { |
| 660 | BitSetIndices[MI->get<MDString *>()] = BitSets.size(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 661 | BitSets.push_back(MI->get<MDString *>()); |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 662 | } else { |
| 663 | GlobalIndices[MI->get<GlobalVariable *>()] = Globals.size(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 664 | Globals.push_back(MI->get<GlobalVariable *>()); |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 665 | } |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 668 | // For each bitset, build a set of indices that refer to globals referenced |
| 669 | // by the bitset. |
| 670 | std::vector<std::set<uint64_t>> BitSetMembers(BitSets.size()); |
| 671 | if (BitSetNM) { |
| 672 | for (MDNode *Op : BitSetNM->operands()) { |
| 673 | // Op = { bitset name, global, offset } |
| 674 | if (!Op->getOperand(1)) |
| 675 | continue; |
| 676 | auto I = BitSetIndices.find(cast<MDString>(Op->getOperand(0))); |
| 677 | if (I == BitSetIndices.end()) |
| 678 | continue; |
| 679 | |
Peter Collingbourne | ba4c8b5 | 2015-06-27 00:17:51 +0000 | [diff] [blame] | 680 | auto OpGlobal = dyn_cast<GlobalVariable>( |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 681 | cast<ConstantAsMetadata>(Op->getOperand(1))->getValue()); |
Peter Collingbourne | ba4c8b5 | 2015-06-27 00:17:51 +0000 | [diff] [blame] | 682 | if (!OpGlobal) |
| 683 | continue; |
Peter Collingbourne | 1baeaa3 | 2015-02-24 23:17:02 +0000 | [diff] [blame] | 684 | BitSetMembers[I->second].insert(GlobalIndices[OpGlobal]); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // Order the sets of indices by size. The GlobalLayoutBuilder works best |
| 689 | // when given small index sets first. |
| 690 | std::stable_sort( |
| 691 | BitSetMembers.begin(), BitSetMembers.end(), |
| 692 | [](const std::set<uint64_t> &O1, const std::set<uint64_t> &O2) { |
| 693 | return O1.size() < O2.size(); |
| 694 | }); |
| 695 | |
| 696 | // Create a GlobalLayoutBuilder and provide it with index sets as layout |
| 697 | // fragments. The GlobalLayoutBuilder tries to lay out members of fragments |
| 698 | // as close together as possible. |
| 699 | GlobalLayoutBuilder GLB(Globals.size()); |
| 700 | for (auto &&MemSet : BitSetMembers) |
| 701 | GLB.addFragment(MemSet); |
| 702 | |
| 703 | // Build a vector of globals with the computed layout. |
| 704 | std::vector<GlobalVariable *> OrderedGlobals(Globals.size()); |
| 705 | auto OGI = OrderedGlobals.begin(); |
| 706 | for (auto &&F : GLB.Fragments) |
| 707 | for (auto &&Offset : F) |
| 708 | *OGI++ = Globals[Offset]; |
| 709 | |
| 710 | // Order bitsets by name for determinism. |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 711 | std::sort(BitSets.begin(), BitSets.end(), [](MDString *S1, MDString *S2) { |
| 712 | return S1->getString() < S2->getString(); |
| 713 | }); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 714 | |
| 715 | // Build the bitsets from this disjoint set. |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 716 | buildBitSetsFromGlobals(BitSets, OrderedGlobals); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 719 | allocateByteArrays(); |
| 720 | |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 721 | return true; |
| 722 | } |
| 723 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 724 | bool LowerBitSets::eraseBitSetMetadata() { |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 725 | if (!BitSetNM) |
| 726 | return false; |
| 727 | |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 728 | M->eraseNamedMetadata(BitSetNM); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | |
| 732 | bool LowerBitSets::runOnModule(Module &M) { |
Peter Collingbourne | da2dbf2 | 2015-03-03 00:49:28 +0000 | [diff] [blame] | 733 | bool Changed = buildBitSets(); |
| 734 | Changed |= eraseBitSetMetadata(); |
Peter Collingbourne | e6909c8 | 2015-02-20 20:30:47 +0000 | [diff] [blame] | 735 | return Changed; |
| 736 | } |