- ConstantPointerRef *ArrayCPR = ConstantPointerRef::get(Array);
- std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::LongTy));
- Args[2] = ConstantExpr::getGetElementPtr(ArrayCPR, GEPIndices);
-
- unsigned NumElements =
- cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
- Args[3] = ConstantUInt::get(Type::UIntTy, NumElements);
-
- // Skip over any allocas in the entry block.
- BasicBlock *Entry = MainFn->begin();
- BasicBlock::iterator InsertPos = Entry->begin();
- while (isa<AllocaInst>(InsertPos)) ++InsertPos;
-
- new CallInst(InitFn, Args, "", InsertPos);
+ unsigned NumBlocks = 0;
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+ if (!I->isDeclaration())
+ NumBlocks += I->size();
+
+ const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
+ GlobalVariable *Counters =
+ new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
+ Constant::getNullValue(ATy), "BlockProfCounters");
+
+ // Instrument all of the blocks...
+ unsigned i = 0;
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
+ if (I->isDeclaration()) continue;
+ for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
+ // Insert counter at the start of the block
+ IncrementCounterInBlock(BB, i++, Counters);
+ }
+
+ // Add the initialization call to main.
+ InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
+ return true;