#include "llvm/Target/TargetOptions.h"
#include "llvm/Module.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/ModuleProvider.h"
#include "llvm/ExecutionEngine/JIT.h"

using namespace llvm;

static Function *createFunction(Module *module)
{
	Function *function = cast<Function>(module->getOrInsertFunction("", Type::VoidTy, (Type *)0));
	BasicBlock *BB = BasicBlock::Create("", function);
	IRBuilder *builder = new IRBuilder(BB);

	Value *address = new AllocaInst(VectorType::get(Type::FloatTy, 4), "", BB);
	Value *vector = builder->CreateLoad(address);
	Value *compare = builder->CreateVFCmp(CmpInst::FCMP_OGT, vector, vector);   // Fails when UnsafePFMath = true
//	Value *compare = builder->CreateVFCmp(CmpInst::FCMP_UGT, vector, vector);   // Always fails on x86/SSE
	Value *result = new AllocaInst(VectorType::get(Type::Int32Ty, 4), "", BB);
	builder->CreateStore(compare, result);

	builder->CreateRetVoid();

	return function;
}


int main()
{
	Module *module = new Module("");
	Function *function = createFunction(module);
	ExistingModuleProvider *MP = new ExistingModuleProvider(module);
	ExecutionEngine *EE = ExecutionEngine::create(MP, false);

	UnsafeFPMath = true;

	((void(*)())EE->getPointerToFunction(function))();
}
