func (c *CG) Init() {
	var err error
	c.Mod = c.NewModule("kaleidoscope")
	c.EE, err = llvm.NewInterpreter(c.Mod)
	ck(err)
	c.Fpm = llvm.NewFunctionPassManagerForModule(c.Mod)

	c.Fpm.Add(c.EE.TargetData())
	c.Fpm.AddInstructionCombiningPass()
	c.Fpm.AddReassociatePass()
	c.Fpm.AddGVNPass()
	c.Fpm.AddCFGSimplificationPass()
	c.Fpm.InitializeFunc()
}
Exemple #2
0
func runPasses(opts *driverOptions, tm llvm.TargetMachine, m llvm.Module) {
	fpm := llvm.NewFunctionPassManagerForModule(m)
	defer fpm.Dispose()

	mpm := llvm.NewPassManager()
	defer mpm.Dispose()

	pmb := llvm.NewPassManagerBuilder()
	defer pmb.Dispose()

	pmb.SetOptLevel(opts.optLevel)
	pmb.SetSizeLevel(opts.sizeLevel)

	target := tm.TargetData()
	mpm.Add(target)
	fpm.Add(target)
	tm.AddAnalysisPasses(mpm)
	tm.AddAnalysisPasses(fpm)

	mpm.AddVerifierPass()
	fpm.AddVerifierPass()

	pmb.Populate(mpm)
	pmb.PopulateFunc(fpm)

	if opts.optLevel == 0 {
		// Remove references (via the descriptor) to dead functions,
		// for compatibility with other compilers.
		mpm.AddGlobalDCEPass()
	}

	opts.sanitizer.addPasses(mpm, fpm)

	fpm.InitializeFunc()
	for fn := m.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
		fpm.RunFunc(fn)
	}
	fpm.FinalizeFunc()

	mpm.Run(m)
}