28 #define DEBUG_TYPE "loop-vectorize"
34 class PlainCFGBuilder {
67 bool isExternalDef(
Value *Val);
74 : TheLoop(Lp), LI(LI), Plan(
P) {}
87 VPBBPreds.push_back(getOrCreateVPBB(Pred));
93 void PlainCFGBuilder::fixPhiNodes() {
94 for (
auto *Phi : PhisToFix) {
95 assert(IRDef2VPValue.count(Phi) &&
"Missing VPInstruction for PHINode.");
96 VPValue *VPVal = IRDef2VPValue[Phi];
97 assert(isa<VPWidenPHIRecipe>(VPVal) &&
98 "Expected WidenPHIRecipe for phi node.");
99 auto *VPPhi = cast<VPWidenPHIRecipe>(VPVal);
100 assert(VPPhi->getNumOperands() == 0 &&
101 "Expected VPInstruction with no operands.");
103 for (
unsigned I = 0;
I != Phi->getNumOperands(); ++
I)
104 VPPhi->addIncoming(getOrCreateVPOperand(Phi->getIncomingValue(
I)),
105 BB2VPBB[Phi->getIncomingBlock(
I)]);
112 auto BlockIt = BB2VPBB.find(
BB);
113 if (BlockIt != BB2VPBB.end())
115 return BlockIt->second;
118 LLVM_DEBUG(
dbgs() <<
"Creating VPBasicBlock for " <<
BB->getName() <<
"\n");
132 bool PlainCFGBuilder::isExternalDef(
Value *Val) {
140 assert(InstParent &&
"Expected instruction parent.");
144 assert(PH &&
"Expected loop pre-header.");
146 if (InstParent == PH)
151 BasicBlock *Exit = TheLoop->getUniqueExitBlock();
152 assert(Exit &&
"Expected loop with single exit.");
153 if (InstParent == Exit) {
159 return !TheLoop->contains(Inst);
167 VPValue *PlainCFGBuilder::getOrCreateVPOperand(
Value *IRVal) {
168 auto VPValIt = IRDef2VPValue.find(IRVal);
169 if (VPValIt != IRDef2VPValue.end())
172 return VPValIt->second;
181 assert(isExternalDef(IRVal) &&
"Expected external definition as operand.");
185 VPValue *NewVPVal = Plan.getOrAddExternalDef(IRVal);
186 IRDef2VPValue[IRVal] = NewVPVal;
193 void PlainCFGBuilder::createVPInstructionsForVPBB(
VPBasicBlock *VPBB,
195 VPIRBuilder.setInsertPoint(VPBB);
201 assert(!IRDef2VPValue.count(Inst) &&
202 "Instruction shouldn't have been visited.");
204 if (
auto *Br = dyn_cast<BranchInst>(Inst)) {
207 if (Br->isConditional())
208 getOrCreateVPOperand(Br->getCondition());
215 if (
auto *Phi = dyn_cast<PHINode>(Inst)) {
221 PhisToFix.push_back(Phi);
227 VPOperands.push_back(getOrCreateVPOperand(
Op));
231 NewVPV = cast<VPInstruction>(
232 VPIRBuilder.createNaryOp(Inst->
getOpcode(), VPOperands, Inst));
235 IRDef2VPValue[Inst] = NewVPV;
253 BasicBlock *PreheaderBB = TheLoop->getLoopPreheader();
255 "Unexpected loop preheader");
256 VPBasicBlock *PreheaderVPBB = getOrCreateVPBB(PreheaderBB);
257 for (
auto &
I : *PreheaderBB) {
258 if (
I.getType()->isVoidTy())
260 IRDef2VPValue[&
I] = Plan.getOrAddExternalDef(&
I);
263 VPBlockBase *HeaderVPBB = getOrCreateVPBB(TheLoop->getHeader());
264 HeaderVPBB->
setName(
"vector.body");
275 createVPInstructionsForVPBB(VPBB,
BB);
281 assert(TI &&
"Terminator expected.");
286 assert(SuccVPBB &&
"VPBB Successor not found.");
288 }
else if (NumSuccs == 2) {
290 assert(SuccVPBB0 &&
"Successor 0 not found.");
292 assert(SuccVPBB1 &&
"Successor 1 not found.");
295 assert(isa<BranchInst>(TI) &&
"Unsupported terminator!");
296 auto *Br = cast<BranchInst>(TI);
297 Value *BrCond = Br->getCondition();
300 assert(IRDef2VPValue.count(BrCond) &&
301 "Missing condition bit in IRDef2VPValue!");
302 VPValue *VPCondBit = IRDef2VPValue[BrCond];
310 setVPBBPredsFromBB(VPBB,
BB);
316 BasicBlock *LoopExitBB = TheLoop->getUniqueExitBlock();
317 assert(LoopExitBB &&
"Loops with multiple exits are not supported.");
319 createVPInstructionsForVPBB(LoopExitVPBB, LoopExitBB);
322 setVPBBPredsFromBB(LoopExitVPBB, LoopExitBB);
331 TopRegion->setEntry(PreheaderVPBB);
332 TopRegion->setExit(LoopExitVPBB);
337 PlainCFGBuilder PCFGBuilder(TheLoop, LI, Plan);
338 return PCFGBuilder.buildPlainCFG();
352 LLVM_DEBUG(
dbgs() <<
"Dominator Tree after building the plain CFG.\n";