/// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
                      IRBuilder<> &B, const TargetData *TD) {
  Module *M = B.GetInsertBlock()->getParent()->getParent();
  AttributeWithIndex AWI[3];
  AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
  AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
  AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
  LLVMContext &Context = B.GetInsertBlock()->getContext();
  Constant *F;
  if (File->getType()->isPointerTy())
    F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
                               TD->getIntPtrType(Context),
                               B.getInt8PtrTy(),
                               TD->getIntPtrType(Context),
                               TD->getIntPtrType(Context),
                               File->getType(), NULL);
  else
    F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
                               B.getInt8PtrTy(),
                               TD->getIntPtrType(Context),
                               TD->getIntPtrType(Context),
                               File->getType(), NULL);
  CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
                        ConstantInt::get(TD->getIntPtrType(Context), 1), File);

  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
    CI->setCallingConv(Fn->getCallingConv());
}
Example #2
0
void ASTCodeGenVisitor::Visit(DecrData* s) {
  IRBuilder<> builder = builders_.top();
  Value* ptr_val = builder.CreateLoad(ptr_);
  Value* result = builder.CreateAdd(ptr_val, neg_one);
  builder.CreateStore(result, ptr_);
  VisitNextASTNode(s);
}
Example #3
0
Function* FunctionAST::Codegen()
{
  Function* theFunction = Proto->Codegen();
  if(theFunction == 0)
    return 0;

  BasicBlock* BB = BasicBlock::Create(getGlobalContext(),"entry",theFunction);
  Builder.SetInsertPoint(BB);

  Proto->CreateArgumentAllocas(theFunction);

  vector<ExprAST*>::iterator it = Body.begin();
  Value* last;
  for(it = Body.begin(); it != Body.end(); ++it)
  {
    last = (*it)->Codegen();
    if (!last)
      break;
  }
  
  if(last)
  {
    Builder.CreateRet(last);
    verifyFunction(*theFunction);
    NamedValues.clear();
    return theFunction;
  }
  //If it gets here there's an error! erase the function
  theFunction->eraseFromParent();
  return 0;
}
Example #4
0
Value* MultiplicationExpression::getValue() {
    assert(leftExpression != NULL);
    assert(rightExpression != NULL);

    IRBuilder<>* builder = codegen::getBuilder();
    return builder->CreateMul(leftExpression->getValue(), rightExpression->getValue());
}
static void reorderSubVector(MVT VT, SmallVectorImpl<Value *> &TransposedMatrix,
  ArrayRef<Value *> Vec, ArrayRef<uint32_t> VPShuf,
  unsigned VecElems, unsigned Stride,
  IRBuilder<> Builder) {

  if (VecElems == 16) {
    for (unsigned i = 0; i < Stride; i++)
      TransposedMatrix[i] = Builder.CreateShuffleVector(
        Vec[i], UndefValue::get(Vec[i]->getType()), VPShuf);
    return;
  }

  SmallVector<uint32_t, 32> OptimizeShuf;
  Value *Temp[8];

  for (unsigned i = 0; i < (VecElems / 16) * Stride; i += 2) {
    genShuffleBland(VT, VPShuf, OptimizeShuf, (i / Stride) * 16,
      (i + 1) / Stride * 16);
    Temp[i / 2] = Builder.CreateShuffleVector(
      Vec[i % Stride], Vec[(i + 1) % Stride], OptimizeShuf);
    OptimizeShuf.clear();
  }

  if (VecElems == 32) {
    std::copy(Temp, Temp + Stride, TransposedMatrix.begin());
    return;
  }
  else
    for (unsigned i = 0; i < Stride; i++)
      TransposedMatrix[i] =
      Builder.CreateShuffleVector(Temp[2 * i], Temp[2 * i + 1], Concat);
}
Example #6
0
Value* EqualExpression::getValue() {
    assert(leftExpression != NULL);
    assert(rightExpression != NULL);
    assert(leftExpression->getType() == rightExpression->getType());
    IRBuilder<>* builder = codegen::getBuilder();
    int c;
    if(leftExpression->getType() != COLEXPRESSION) {
        c = leftExpression->getType();
    } else {
        c = convertDTtoET(leftExpression->getDataType());
    }
    switch(c) {
        case LONGVALUEEXPRESSION:
            return builder->CreateICmpEQ(leftExpression->getValue(), rightExpression->getValue());
        case DOUBLEVALUEEXPRESSION:
            return builder->CreateFCmpOEQ(leftExpression->getValue(), rightExpression->getValue());
        case STRINGVALUEEXPRESSION:
        case DATEVALUEEXPRESSION:
            return codegen::stringCmp(leftExpression->getValue(), rightExpression->getValue(), EQUALEXPRESSION);
        default:
            cout << "Unknown expression type!" << endl;
            exit(-1);
    }

    return NULL;
}
Example #7
0
Value* OrExpression::getValue() {
    assert(leftExpression != NULL);
    assert(rightExpression != NULL);

    IRBuilder<>* builder = codegen::getBuilder();
    return builder->CreateOr(leftExpression->getValue(), rightExpression->getValue());
}
Example #8
0
void
code_emitter::emit_main_filter_funcs ()
{
    // init x
#ifdef DEBUG_OUTPUT
    printf("emitting init x for %s\n", filter->name);
#endif
    x_vars_var = setup_init_x_or_y_function(init_x_function_name(filter), "y", x_vars_type);
    compiler_slice_code_for_const(filter_code->first_stmt, CONST_X);
    emit_stmts(filter_code->first_stmt, SLICE_X_CONST);
    builder->CreateRet(ret_var);
    finish_function();

    // init y
#ifdef DEBUG_OUTPUT
    printf("emitting init y for %s\n", filter->name);
#endif
    y_vars_var = setup_init_x_or_y_function(init_y_function_name(filter), "x", y_vars_type);
    compiler_slice_code_for_const(filter_code->first_stmt, CONST_Y);
    emit_stmts(filter_code->first_stmt, SLICE_Y_CONST);
    builder->CreateRet(ret_var);
    finish_function();

    // filter
#ifdef DEBUG_OUTPUT
    printf("emitting main filter func for %s\n", filter->name);
#endif
    setup_filter_function(true);
    compiler_slice_code_for_const(filter_code->first_stmt, CONST_NONE);
    //fetch_all_const_values();
    emit_stmts(filter_code->first_stmt, SLICE_NO_CONST);
    finish_function();
}
void InstructionReplace::insertStores(llvm::Module& M)
{
	for(llvm::Module::iterator F = M.begin(), ME = M.end(); F != ME; ++F) {
		//if(!F->getFnAttributes().hasAttribute(Attributes::AttrVal::MaskedCopy)) { continue; }
		llvm::Function* Fun = maskedfn[F];
		vector<Value*> outputshares;
		ReturnInst* tbd = NULL;
		auto arg = Fun->arg_begin();
		for(unsigned int i = 0; i <= MaskingOrder; i++) { ++arg; }
		for(unsigned int i = 0; i <= MaskingOrder; i++) { outputshares.push_back(arg++); }
		for(llvm::Function::iterator BB = Fun->begin(),
		    FE = Fun->end();
		    BB != FE;
		    ++BB) {
			for( llvm::BasicBlock::iterator i = BB->begin(); i != BB->end(); i++) {
				if(tbd != NULL) {tbd->eraseFromParent(); tbd = NULL;}
				if(!isa<ReturnInst>(i)) {continue;}
				ReturnInst* ri = cast<ReturnInst>(i);
				IRBuilder<> ib = llvm::IRBuilder<>(BB->getContext());
				ib.SetInsertPoint(i);
				vector<Value*> shares = MaskValue(ri->getReturnValue(), ri);
				for(unsigned int i = 0; i <= MaskingOrder; i++) { ib.CreateStore(shares[i], outputshares[i]); }
				ib.CreateRetVoid();
				tbd = ri;
			}
		}
		if(tbd != NULL) {tbd->eraseFromParent(); tbd = NULL;}
	}
}
Example #10
0
void MmixLlvm::Private::emitIncl(VerticeContext& vctx, IRBuilder<>& builder,  MXByte xarg, MXWyde yzarg)
{
	Value *xval0 = vctx.getRegister(xarg);
	Value* result = builder.CreateAdd(xval0, builder.getInt64((MXOcta) yzarg));
	assignRegister(vctx, builder, xarg, result);
	builder.CreateBr(vctx.getOCExit());
}
Example #11
0
void
code_emitter::emit_filter_function ()
{
#ifdef DEBUG_OUTPUT
    printf("emitting filter func for %s\n", filter->name);
#endif

    setup_filter_function(false);

    x_vars_type = build_const_value_infos(CONST_X);
    x_vars_var = builder->CreateAlloca(x_vars_type);
    compiler_slice_code_for_const(filter_code->first_stmt, CONST_X);
    emit_stmts(filter_code->first_stmt, SLICE_X_CONST);
    value_map.clear();

    y_vars_type = build_const_value_infos(CONST_Y);
    y_vars_var = builder->CreateAlloca(y_vars_type);
    compiler_slice_code_for_const(filter_code->first_stmt, CONST_Y);
    emit_stmts(filter_code->first_stmt, SLICE_Y_CONST);
    value_map.clear();

    compiler_slice_code_for_const(filter_code->first_stmt, CONST_NONE);
    emit_stmts(filter_code->first_stmt, SLICE_NO_CONST);

    finish_function();
}
Example #12
0
Value* While::codeGen(CodeGenContext &context) {

  //Value* conditionValue = condition.codeGen(context);

  IRBuilder<> *builder = context.currentBuilder();
  Function *mainFunction = builder->GetInsertBlock()->getParent();

  BasicBlock *condBb = BasicBlock::Create(getGlobalContext(), "whilecond",
      mainFunction);
  BasicBlock *doBb = BasicBlock::Create(getGlobalContext(), "whiledo");
  BasicBlock *afterWhileBb = BasicBlock::Create(getGlobalContext(), "afterWhile");


  builder->CreateBr(condBb);
  builder->SetInsertPoint(condBb);
  Value* conditionValue = condition.codeGen(context);
  builder->CreateCondBr(conditionValue, doBb, afterWhileBb);
  condBb = builder->GetInsertBlock();

  mainFunction->getBasicBlockList().push_back(doBb);
  builder->SetInsertPoint(doBb);
  Value *doValue = (*doStmt).codeGen(context);
  builder->CreateBr(condBb);
  doBb = builder->GetInsertBlock();

  mainFunction->getBasicBlockList().push_back(afterWhileBb);
  builder->SetInsertPoint(afterWhileBb);
  return NULL;

}
Example #13
0
Value*
code_emitter::emit_sizeof (Type *type)
{
    Value *size = builder->CreateGEP(ConstantPointerNull::get(PointerType::getUnqual(type)),
				     make_int_const(1));

    return builder->CreatePtrToInt(size, sizeof(gpointer) == 4 ? Type::Int32Ty : Type::Int64Ty);
}
Example #14
0
void
code_emitter::setup_xy_vars_from_closure ()
{
    Value *t_var = lookup_internal(::lookup_internal(filter->v.mathmap.internals, "t", true));
    Value *xy_vars_untyped = builder->CreateCall4(module->getFunction(string("calc_closure_xy_vars")),
						  invocation_arg, closure_arg, t_var, init_frame_function);
    xy_vars_var = builder->CreateBitCast(xy_vars_untyped, PointerType::getUnqual(xy_vars_type));
}
Example #15
0
Value* Ass::codeGen(CodeGenContext &context) {
  IRBuilder<> *builder = context.currentBuilder();
  if (context.locals().find(lhs.name) == context.locals().end()) {
    return NULL;
  }
  Value *rhsValue = rhs.codeGen(context);
  return builder->CreateStore(rhsValue, context.locals()[lhs.name]);
}
Example #16
0
void EmitMemSet(IRBuilder<>& B, Value* Dst, Value* Val, Value* Len,
                const Analysis& A) {
    Dst = B.CreateBitCast(Dst, PointerType::getUnqual(B.getInt8Ty()));

    CallSite CS = B.CreateMemSet(Dst, Val, Len, 1 /*Align*/, false /*isVolatile*/);
    if (A.CGNode)
        A.CGNode->addCalledFunction(CS, A.CG->getOrInsertFunction(CS.getCalledFunction()));
}
Example #17
0
Value* Id::codeGen(CodeGenContext &context) {
  if (context.locals().find(name) == context.locals().end()) {
    return NULL;
  }

  IRBuilder<> *builder = context.currentBuilder();
  return builder->CreateLoad(context.locals()[name], name);
}
Example #18
0
void CNodeCodeGenVisitor::Visit(CSet* s) {
  IRBuilder<> builder = builders_.top();

  Value* offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset()));
  Value* set_val = GetDataOffset(s->GetAmt());
  builder.CreateStore(set_val, offset_ptr);
  VisitNextCNode(s);
}
Example #19
0
File: opt.cpp Project: Kofel/hhvm
void optimize(IRUnit& unit, IRBuilder& irBuilder, TransKind kind) {
  auto finishPass = [&](const char* msg) {
    dumpTrace(6, unit, folly::format("after {}", msg).str().c_str());
    assert(checkCfg(unit));
    assert(checkTmpsSpanningCalls(unit));
    if (debug) {
      forEachInst(rpoSortCfg(unit), assertOperandTypes);
    }
  };

  auto doPass = [&](void (*fn)(IRUnit&), const char* msg) {
    fn(unit);
    finishPass(msg);
  };

  auto dce = [&](const char* which) {
    if (!RuntimeOption::EvalHHIRDeadCodeElim) return;
    eliminateDeadCode(unit);
    finishPass(folly::format("{} DCE", which).str().c_str());
  };

  if (RuntimeOption::EvalHHIRRelaxGuards) {
    auto const simpleRelax = kind == TransProfile;
    auto changed = relaxGuards(unit, *irBuilder.guards(), simpleRelax);
    if (changed) finishPass("guard relaxation");
  }

  if (RuntimeOption::EvalHHIRRefcountOpts) {
    optimizeRefcounts(unit);
    finishPass("refcount opts");
  }

  dce("initial");

  if (RuntimeOption::EvalHHIRPredictionOpts) {
    doPass(optimizePredictions, "prediction opts");
  }

  if (RuntimeOption::EvalHHIRExtraOptPass
      && (RuntimeOption::EvalHHIRCse
          || RuntimeOption::EvalHHIRSimplification)) {
    irBuilder.reoptimize();
    finishPass("reoptimize");
    // Cleanup any dead code left around by CSE/Simplification
    // Ideally, this would be controlled by a flag returned
    // by optimzeTrace indicating whether DCE is necessary
    dce("reoptimize");
  }

  if (RuntimeOption::EvalHHIRJumpOpts) {
    doPass(optimizeJumps, "jumpopts");
    dce("jump opts");
  }

  if (RuntimeOption::EvalHHIRGenerateAsserts) {
    doPass(insertAsserts, "RefCnt asserts");
  }
}
Example #20
0
void
code_emitter::setup_filter_function (bool is_main_filter_function)
{
    Function *filter_function = is_main_filter_function
	? lookup_main_filter_function(module, filter)
	: lookup_filter_function(module, filter);
    Function::arg_iterator args = filter_function->arg_begin();
    Value *slice_arg = NULL;

    if (is_main_filter_function)
    {
	slice_arg = args++;
	slice_arg->setName("slice");
    }
    else
    {
	invocation_arg = args++;
	invocation_arg->setName("invocation");
    }
    closure_arg = args++;
    closure_arg->setName("closure");
    if (is_main_filter_function)
    {
	x_vars_var = args++;
	x_vars_var->setName("x_vars");
	y_vars_var = args++;
	y_vars_var->setName("y_vars");
    }
    set_internal(::lookup_internal(filter->v.mathmap.internals, "x", true), args++);
    set_internal(::lookup_internal(filter->v.mathmap.internals, "y", true), args++);
    set_internal(::lookup_internal(filter->v.mathmap.internals, "t", true), args++);
    pools_arg = args++;
    pools_arg->setName("pools");

    BasicBlock *block = BasicBlock::Create("entry", filter_function);

    builder = new IRBuilder<> (block);

    if (is_main_filter_function)
    {
	x_vars_var = builder->CreateBitCast(x_vars_var, PointerType::getUnqual(x_vars_type));
	y_vars_var = builder->CreateBitCast(y_vars_var, PointerType::getUnqual(y_vars_type));
	frame_arg = builder->CreateCall(module->getFunction(string("get_slice_frame")), slice_arg);
	invocation_arg = builder->CreateCall(module->getFunction(string("get_frame_invocation")), frame_arg);
    }

    set_internals_from_invocation(invocation_arg);

    if (is_main_filter_function)
	set_xy_vars_from_frame ();
    else
	setup_xy_vars_from_closure ();

    alloc_complex_copy_var();

    current_function = filter_function;
}
Example #21
0
Function* BuildProgramFromCanon(CNode* s, llvm::Module* module,
                                int store_size) {
  CNodeCodeGenVisitor visitor(module, store_size);
  s->Accept(visitor);
  IRBuilder<> builder = visitor.GetLastBuilder();
  builder.CreateRetVoid();
  Function* func = visitor.GetMain();
  return func;
}
Example #22
0
void CNodeCodeGenVisitor::Visit(COutput* s) {
  IRBuilder<> builder = builders_.top();

  Value* offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset()));
  Value* ptr_value = builder.CreateLoad(offset_ptr);

  builder.CreateCall(put_char_, ptr_value);
  VisitNextCNode(s);
}
Example #23
0
void CNodeCodeGenVisitor::Visit(CInput* s) {
  IRBuilder<> builder = builders_.top();

  Value* ptr_offset = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset()));
  Value* input = builder.CreateCall(get_char_);

  builder.CreateStore(input, ptr_offset);
  VisitNextCNode(s);
}
Example #24
0
/// compile_back - Emit code for ']'
void BrainFTraceRecorder::compile_back(BrainFTraceNode *node,
                                       IRBuilder<>& builder) {
  if (node->right != (BrainFTraceNode*)~0ULL)
    compile_opcode(node->right, builder);
  else {
    HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock());
    builder.CreateBr(Header);
  }
}
Example #25
0
static void createCmpXchgInstFun(IRBuilder<> &Builder, Value *Addr,
                                 Value *Loaded, Value *NewVal,
                                 AtomicOrdering MemOpOrder,
                                 Value *&Success, Value *&NewLoaded) {
  Value* Pair = Builder.CreateAtomicCmpXchg(
      Addr, Loaded, NewVal, MemOpOrder,
      AtomicCmpXchgInst::getStrongestFailureOrdering(MemOpOrder));
  Success = Builder.CreateExtractValue(Pair, 1, "success");
  NewLoaded = Builder.CreateExtractValue(Pair, 0, "newloaded");
}
void AtomicExpandLoadLinked::insertTrailingFence(IRBuilder<> &Builder,
                                              AtomicOrdering Ord) {
  if (!TLI->getInsertFencesForAtomic())
    return;

  if (Ord == Acquire || Ord == AcquireRelease)
    Builder.CreateFence(Acquire);
  else if (Ord == SequentiallyConsistent)
    Builder.CreateFence(SequentiallyConsistent);
}
Example #27
0
CodeGenBlock::CodeGenBlock(int args, int locals, CodeGenLexicalScope
		*enclosingScope, CodeGenModule *Mod) 
	: CodeGenLexicalScope(Mod), parentScope(enclosingScope) 
{
	Value *enclosingContext = enclosingScope->getContext();
	// Define the layout of a block
	BlockTy = StructType::get(
		Mod->Context,
		IdTy,                          // 0 - isa.
		IMPTy,                         // 1 - Function pointer.
		Type::getInt32Ty(Mod->Context),// 2 - Number of args.
		enclosingContext->getType(),   // 3 - Context.
		NULL);
	std::vector<const Type*> argTy;
	argTy.push_back(PointerType::getUnqual(BlockTy));

	// FIXME: Broken on Etoile runtime - _cmd needs to be a GEP on _call
	argTy.push_back(SelTy);
	for (int i=0 ; i<args ; ++i) 
	{
		argTy.push_back(IdTy);
	}
	FunctionType *BlockFunctionTy = FunctionType::get(IdTy, argTy, false);

	IRBuilder<> *MethodBuilder = enclosingScope->getBuilder();

	// Create the block object
	
	// The NewBlock function gets a block from a pool.  It should really be
	// inlined.
	Block = MethodBuilder->CreateAlloca(BlockTy);

	Module *TheModule = CGM->getModule();
	// Create the block function
	CurrentFunction = Function::Create(BlockFunctionTy,
		GlobalValue::InternalLinkage, "BlockFunction", TheModule);
	InitialiseFunction(Args, Locals, locals);

	// Set the isa pointer
	Value *isa = MethodBuilder->CreateLoad(
		TheModule->getGlobalVariable(".smalltalk_block_stack_class", true));
	storeInStruct(MethodBuilder, Block, isa, 0);

	// Store the block function in the object
	storeInStruct(MethodBuilder, Block,
		MethodBuilder->CreateBitCast(CurrentFunction, IMPTy), 1);
	// Store the number of arguments
	storeInStruct(MethodBuilder, Block, 
			ConstantInt::get(Type::getInt32Ty(Mod->Context), args), 2);
	// Set the context
	storeInStruct(MethodBuilder, Block, enclosingScope->getContext(), 3);


}
Example #28
0
void CNodeCodeGenVisitor::Visit(CAdd* s) {
  IRBuilder<> builder = builders_.top();

  Value* offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset()));
  Value* offset_val = builder.CreateLoad(offset_ptr);

  Value* add_val = GetDataOffset(s->GetAmt());
  Value* result = builder.CreateAdd(offset_val, add_val);

  builder.CreateStore(result, offset_ptr);
  VisitNextCNode(s);
}
Example #29
0
/// compile_right - Emit code for '>'                                                                               
void BrainFTraceRecorder::compile_right(BrainFTraceNode *node,
                                        IRBuilder<>& builder) {
  Value *OldPtr = DataPtr;
  DataPtr = builder.CreateConstInBoundsGEP1_32(DataPtr, 1);
  if (node->left != (BrainFTraceNode*)~0ULL)
    compile_opcode(node->left, builder);
  else {
    HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock());
    builder.CreateBr(Header);
  }
  DataPtr = OldPtr;
}
Example #30
0
/// compile_set_zero - Emit Code for '0'
void BrainFTraceRecorder::compile_set_zero(BrainFTraceNode *node,
                                           IRBuilder<>& builder) {
  Constant *Zero =
    ConstantInt::get(IntegerType::getInt8Ty(Header->getContext()), 0);
  builder.CreateStore(Zero, DataPtr);
  if (node->left != (BrainFTraceNode*)~0ULL)
    compile_opcode(node->left, builder);
  else {
    HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock());
    builder.CreateBr(Header);
  }
}