brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · efdc639 Raw
63 lines · plain
1; RUN: llvm-profgen --format=text --perfscript=%s  --binary=%S/Inputs/fs-discriminator-probe.perfbin --output=%t --show-pseudo-probe --show-disassembly-only | FileCheck %s2 3; CHECK:      <quick_sort>:4; CHECK:       [Probe]: FUNC: quick_sort Index: 1  Type: Block5; CHECK:       [Probe]: FUNC: quick_sort Index: 1  Discriminator: 15360 Type: Block6; CHECK: 			 [Probe]:	FUNC: quick_sort Index: 4  Type: IndirectCall7; CHECK: 		   [Probe]:	FUNC: quick_sort Index: 5  Type: DirectCall8 9 10; original code:11; clang -O3 -g -mllvm --enable-fs-discriminator -fdebug-info-for-profiling -fpseudo-probe-for-profiling qsort.c -o a.out12#include <stdio.h>13#include <stdlib.h>14 15void swap(int *a, int *b) {16	int t = *a;17	*a = *b;18	*b = t;19}20 21int partition_pivot_last(int* array, int low, int high) {22	int pivot = array[high];23	int i = low - 1;24	for (int j = low; j < high; j++)25		if (array[j] < pivot)26			swap(&array[++i], &array[j]);27	swap(&array[i + 1], &array[high]);28	return (i + 1);29}30 31int partition_pivot_first(int* array, int low, int high) {32	int pivot = array[low];33	int i = low + 1;34	for (int j = low + 1; j <= high; j++)35		if (array[j] < pivot) { if (j != i) swap(&array[i], &array[j]); i++;}36	swap(&array[i - 1], &array[low]);37	return i - 1;38}39 40void quick_sort(int* array, int low, int high, int (*partition_func)(int *, int, int)) {41	if (low < high) {42		int pi = (*partition_func)(array, low, high);43		quick_sort(array, low, pi - 1, partition_func);44		quick_sort(array, pi + 1, high, partition_func);45	}46}47 48int main() {49	const int size = 200;50	int sum = 0;51	int *array = malloc(size * sizeof(int));52	for(int i = 0; i < 100 * 1000; i++) {53		for(int j = 0; j < size; j++)54			array[j] = j % 10 ? rand() % size: j;55		int (*fptr)(int *, int, int) = i % 3 ? partition_pivot_last : partition_pivot_first;56		quick_sort(array, 0, size - 1, fptr);57		sum += array[i % size];58	}59	printf("sum=%d\n", sum);60 61	return 0;62}63