void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) { isl_ast_node *Body; isl_ast_expr *Init, *Inc, *Iterator, *UB; isl_id *IteratorID; Value *ValueLB, *ValueUB, *ValueInc; Type *MaxType; BasicBlock *ExitBlock; Value *IV; CmpInst::Predicate Predicate; bool Parallel; Parallel = IslAstInfo::isInnermostParallel(For) && !IslAstInfo::isReductionParallel(For); Body = isl_ast_node_for_get_body(For); // isl_ast_node_for_is_degenerate(For) // // TODO: For degenerated loops we could generate a plain assignment. // However, for now we just reuse the logic for normal loops, which will // create a loop with a single iteration. Init = isl_ast_node_for_get_init(For); Inc = isl_ast_node_for_get_inc(For); Iterator = isl_ast_node_for_get_iterator(For); IteratorID = isl_ast_expr_get_id(Iterator); UB = getUpperBound(For, Predicate); ValueLB = ExprBuilder.create(Init); ValueUB = ExprBuilder.create(UB); ValueInc = ExprBuilder.create(Inc); MaxType = ExprBuilder.getType(Iterator); MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); if (MaxType != ValueLB->getType()) ValueLB = Builder.CreateSExt(ValueLB, MaxType); if (MaxType != ValueUB->getType()) ValueUB = Builder.CreateSExt(ValueUB, MaxType); if (MaxType != ValueInc->getType()) ValueInc = Builder.CreateSExt(ValueInc, MaxType); IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock, Predicate, &Annotator, Parallel); IDToValue[IteratorID] = IV; create(Body); Annotator.End(); IDToValue.erase(IteratorID); Builder.SetInsertPoint(ExitBlock->begin()); isl_ast_node_free(For); isl_ast_expr_free(Iterator); isl_id_free(IteratorID); }
void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) { isl_ast_node *Body; isl_ast_expr *Init, *Inc, *Iterator, *UB; isl_id *IteratorID; Value *ValueLB, *ValueUB, *ValueInc; Type *MaxType; BasicBlock *AfterBlock; Value *IV; CmpInst::Predicate Predicate; Body = isl_ast_node_for_get_body(For); // isl_ast_node_for_is_degenerate(For) // // TODO: For degenerated loops we could generate a plain assignment. // However, for now we just reuse the logic for normal loops, which will // create a loop with a single iteration. Init = isl_ast_node_for_get_init(For); Inc = isl_ast_node_for_get_inc(For); Iterator = isl_ast_node_for_get_iterator(For); IteratorID = isl_ast_expr_get_id(Iterator); UB = getUpperBound(For, Predicate); ValueLB = ExprBuilder.create(Init); ValueUB = ExprBuilder.create(UB); ValueInc = ExprBuilder.create(Inc); MaxType = ExprBuilder.getType(Iterator); MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); if (MaxType != ValueLB->getType()) ValueLB = Builder.CreateSExt(ValueLB, MaxType); if (MaxType != ValueUB->getType()) ValueUB = Builder.CreateSExt(ValueUB, MaxType); if (MaxType != ValueInc->getType()) ValueInc = Builder.CreateSExt(ValueInc, MaxType); // TODO: In case we can proof a loop is executed at least once, we can // generate the condition iv != UB + stride (consider possible // overflow). This condition will allow LLVM to prove the loop is // executed at least once, which will enable a lot of loop invariant // code motion. IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock, Predicate); IDToValue[IteratorID] = IV; create(Body); IDToValue.erase(IteratorID); Builder.SetInsertPoint(AfterBlock->begin()); isl_ast_node_free(For); isl_ast_expr_free(Iterator); isl_id_free(IteratorID); }
void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, int VectorWidth) { isl_ast_node *Body = isl_ast_node_for_get_body(For); isl_ast_expr *Init = isl_ast_node_for_get_init(For); isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); isl_id *IteratorID = isl_ast_expr_get_id(Iterator); Value *ValueLB = ExprBuilder.create(Init); Value *ValueInc = ExprBuilder.create(Inc); Type *MaxType = ExprBuilder.getType(Iterator); MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); if (MaxType != ValueLB->getType()) ValueLB = Builder.CreateSExt(ValueLB, MaxType); if (MaxType != ValueInc->getType()) ValueInc = Builder.CreateSExt(ValueInc, MaxType); std::vector<Value *> IVS(VectorWidth); IVS[0] = ValueLB; for (int i = 1; i < VectorWidth; i++) IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); isl_union_map *Schedule = IslAstInfo::getSchedule(For); assert(Schedule && "For statement annotation does not contain its schedule"); IDToValue[IteratorID] = ValueLB; switch (isl_ast_node_get_type(Body)) { case isl_ast_node_user: createUserVector(Body, IVS, isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); break; case isl_ast_node_block: { isl_ast_node_list *List = isl_ast_node_block_get_children(Body); for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); isl_ast_node_free(Body); isl_ast_node_list_free(List); break; } default: isl_ast_node_dump(Body); llvm_unreachable("Unhandled isl_ast_node in vectorizer"); } IDToValue.erase(IteratorID); isl_id_free(IteratorID); isl_union_map_free(Schedule); isl_ast_node_free(For); isl_ast_expr_free(Iterator); }